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