]> git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/if_nameindex.c
Libinfo-542.40.3.tar.gz
[apple/libinfo.git] / gen.subproj / if_nameindex.c
1 /*
2 * Copyright (c) 2018 Apple Inc. All rights reserved.
3 */
4 /* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */
5
6 /*-
7 * Copyright (c) 1997, 2000
8 * Berkeley Software Design, Inc. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
29 */
30
31 #include "libinfo_common.h"
32
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <net/if_dl.h>
36 #include <net/if.h>
37 #include <ifaddrs.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <os/overflow.h>
41 #include <sys/errno.h>
42
43 /*
44 * From RFC 2553:
45 *
46 * 4.3 Return All Interface Names and Indexes
47 *
48 * The if_nameindex structure holds the information about a single
49 * interface and is defined as a result of including the <net/if.h>
50 * header.
51 *
52 * struct if_nameindex {
53 * unsigned int if_index;
54 * char *if_name;
55 * };
56 *
57 * The final function returns an array of if_nameindex structures, one
58 * structure per interface.
59 *
60 * struct if_nameindex *if_nameindex(void);
61 *
62 * The end of the array of structures is indicated by a structure with
63 * an if_index of 0 and an if_name of NULL. The function returns a NULL
64 * pointer upon an error, and would set errno to the appropriate value.
65 *
66 * The memory used for this array of structures along with the interface
67 * names pointed to by the if_name members is obtained dynamically.
68 * This memory is freed by the next function.
69 *
70 * 4.4. Free Memory
71 *
72 * The following function frees the dynamic memory that was allocated by
73 * if_nameindex().
74 *
75 * #include <net/if.h>
76 *
77 * void if_freenameindex(struct if_nameindex *ptr);
78 *
79 * The argument to this function must be a pointer that was returned by
80 * if_nameindex().
81 */
82
83 LIBINFO_EXPORT
84 struct if_nameindex *
85 if_nameindex(void)
86 {
87 struct ifaddrs *ifaddrs, *ifa;
88 unsigned int ni;
89 size_t nbytes;
90 struct if_nameindex *ifni= NULL, *ifni2;
91 char *cp;
92 size_t cpsz;
93
94 if (getifaddrs(&ifaddrs) < 0)
95 return(NULL);
96
97 /*
98 * First, find out how many interfaces there are, and how
99 * much space we need for the string names.
100 */
101 ni = 0;
102 nbytes = 0;
103 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
104 if (ifa->ifa_addr &&
105 ifa->ifa_addr->sa_family == AF_LINK) {
106 /*
107 * Security Check: Verify nbytes and ni do not overflow
108 */
109 if (os_add_overflow(nbytes, strlen(ifa->ifa_name) + 1, &nbytes) ||
110 os_add_overflow(ni, 1, &ni)) {
111 errno = EOVERFLOW;
112 goto out;
113 }
114 }
115 }
116
117 /*
118 * Security Check: Verify cpsz does not overflow in next 3 operations
119 */
120 if (os_add_overflow(ni, 1, &cpsz)) {
121 errno = EOVERFLOW;
122 goto out;
123 }
124 if (os_mul_overflow(cpsz, sizeof(struct if_nameindex), &cpsz)) {
125 errno = EOVERFLOW;
126 goto out;
127 }
128 if (os_add_overflow(cpsz, nbytes, &cpsz)) {
129 errno = EOVERFLOW;
130 goto out;
131 }
132 /*
133 * Next, allocate a chunk of memory, use the first part
134 * for the array of structures, and the last part for
135 * the strings.
136 */
137 cp = malloc(cpsz);
138 ifni = (struct if_nameindex *)cp;
139 if (ifni == NULL)
140 goto out;
141 cp += (ni + 1) * sizeof(struct if_nameindex);
142
143 /*
144 * Now just loop through the list of interfaces again,
145 * filling in the if_nameindex array and making copies
146 * of all the strings.
147 */
148 ifni2 = ifni;
149 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
150 if (ifa->ifa_addr &&
151 ifa->ifa_addr->sa_family == AF_LINK) {
152 ifni2->if_index =
153 ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
154 ifni2->if_name = cp;
155 strcpy(cp, ifa->ifa_name);
156 ifni2++;
157 cp += strlen(cp) + 1;
158 }
159 }
160 /*
161 * Finally, don't forget to terminate the array.
162 */
163 ifni2->if_index = 0;
164 ifni2->if_name = NULL;
165 out:
166 freeifaddrs(ifaddrs);
167 return(ifni);
168 }
169
170 #ifndef __OpenBSD__
171 LIBINFO_EXPORT
172 void
173 if_freenameindex(struct if_nameindex *ptr)
174 {
175 free(ptr);
176 }
177 #endif