diff options
Diffstat (limited to 'src/lib/libc/net/if_nameindex.c')
-rw-r--r-- | src/lib/libc/net/if_nameindex.c | 207 |
1 files changed, 92 insertions, 115 deletions
diff --git a/src/lib/libc/net/if_nameindex.c b/src/lib/libc/net/if_nameindex.c index 2acbeeb3f8..8714c1f95a 100644 --- a/src/lib/libc/net/if_nameindex.c +++ b/src/lib/libc/net/if_nameindex.c | |||
@@ -1,34 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * %%% copyright-cmetz-98-bsd | 2 | * %%% copyright-cmetz-98-bsd |
3 | * Copyright (c) 1998-1999, Craig Metz, All rights reserved. | 3 | * Copyright (c) 1998-1999, Craig Metz, All rights reserved. |
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. All advertising materials mentioning features or use of this software | ||
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by Craig Metz and | ||
16 | * by other contributors. | ||
17 | * 4. Neither the name of the author nor the names of contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * | 4 | * |
33 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
34 | * modification, are permitted provided that the following conditions | 6 | * modification, are permitted provided that the following conditions |
@@ -45,7 +17,7 @@ | |||
45 | * 4. Neither the name of the author nor the names of contributors | 17 | * 4. Neither the name of the author nor the names of contributors |
46 | * may be used to endorse or promote products derived from this software | 18 | * may be used to endorse or promote products derived from this software |
47 | * without specific prior written permission. | 19 | * without specific prior written permission. |
48 | * | 20 | * |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -57,7 +29,6 @@ | |||
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
59 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
60 | * | ||
61 | */ | 32 | */ |
62 | 33 | ||
63 | #include <stdlib.h> | 34 | #include <stdlib.h> |
@@ -67,102 +38,108 @@ | |||
67 | #include <net/if.h> | 38 | #include <net/if.h> |
68 | #include <net/if_dl.h> | 39 | #include <net/if_dl.h> |
69 | #include <errno.h> | 40 | #include <errno.h> |
41 | #include <string.h> | ||
70 | 42 | ||
71 | struct if_nameindex *if_nameindex(void) | 43 | struct if_nameindex * |
44 | if_nameindex(void) | ||
72 | { | 45 | { |
73 | int i, j, fd; | 46 | int i, j, fd, len; |
74 | struct if_nameindex *nameindex = NULL; | 47 | struct if_nameindex *nameindex = NULL; |
75 | struct ifconf ifconf; | 48 | struct ifconf ifconf; |
76 | void *p; | 49 | char lastname[IFNAMSIZ], *c; |
77 | int len; | 50 | struct if_nameindex *n; |
78 | char lastname[IFNAMSIZ + 1]; | 51 | struct sockaddr_dl *sd; |
79 | 52 | struct sockaddr *sa; | |
80 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | 53 | void *p; |
81 | return NULL; | ||
82 | 54 | ||
83 | ifconf.ifc_len = 0; | 55 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
84 | ifconf.ifc_buf = 0; | 56 | return NULL; |
85 | if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) | ||
86 | goto ret; | ||
87 | if (ifconf.ifc_len < IFNAMSIZ) | ||
88 | goto ret; | ||
89 | if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) | ||
90 | goto ret; | ||
91 | if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) | ||
92 | goto ret; | ||
93 | 57 | ||
94 | i = sizeof(struct if_nameindex); | 58 | ifconf.ifc_len = 0; |
95 | j = 0; | 59 | ifconf.ifc_buf = 0; |
96 | p = ifconf.ifc_buf; | 60 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) |
97 | len = ifconf.ifc_len; | 61 | goto ret; |
98 | lastname[0] = 0; | 62 | if (ifconf.ifc_len < IFNAMSIZ) |
99 | lastname[IFNAMSIZ] = 0; | 63 | goto ret; |
64 | if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) | ||
65 | goto ret; | ||
66 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) | ||
67 | goto ret; | ||
100 | 68 | ||
101 | while(len > 0) { | 69 | i = sizeof(struct if_nameindex); |
102 | if (len < (IFNAMSIZ + sizeof(struct sockaddr))) | 70 | j = 0; |
103 | goto ret; | 71 | p = ifconf.ifc_buf; |
104 | if (strncmp(lastname, p, IFNAMSIZ)) { | 72 | len = ifconf.ifc_len; |
105 | memcpy(lastname, p, IFNAMSIZ); | 73 | lastname[0] = 0; |
106 | i += sizeof(struct if_nameindex); | 74 | lastname[sizeof(lastname)-1] = 0; |
107 | j += strlen(lastname) + 1; | ||
108 | }; | ||
109 | len -= IFNAMSIZ; | ||
110 | p += IFNAMSIZ; | ||
111 | 75 | ||
112 | if (len < SA_LEN((struct sockaddr *)p)) | 76 | while (len > 0) { |
113 | goto ret; | 77 | if (len < (IFNAMSIZ + sizeof(struct sockaddr))) |
114 | len -= SA_LEN((struct sockaddr *)p); | 78 | goto ret; |
115 | p += SA_LEN((struct sockaddr *)p); | 79 | if (strncmp(lastname, p, IFNAMSIZ)) { |
116 | }; | 80 | strlcpy(lastname, p, sizeof(lastname)); |
81 | i += sizeof(struct if_nameindex); | ||
82 | j += strlen(lastname) + 1; | ||
83 | } | ||
84 | len -= IFNAMSIZ; | ||
85 | p += IFNAMSIZ; | ||
86 | sa = p; | ||
117 | 87 | ||
118 | if (!(nameindex = malloc(i + j))) { | 88 | if (len < sa->sa_len) |
119 | errno = ENOMEM; | 89 | goto ret; |
120 | goto ret; | 90 | len -= sa->sa_len; |
121 | }; | 91 | p += sa->sa_len; |
122 | memset(nameindex, 0, i + j); | 92 | } |
123 | 93 | ||
124 | { | 94 | nameindex = malloc(i + j); |
125 | struct if_nameindex *n; | 95 | if (nameindex == NULL) { |
126 | char *c; | 96 | errno = ENOMEM; |
97 | goto ret; | ||
98 | } | ||
99 | memset(nameindex, 0, i + j); | ||
127 | 100 | ||
128 | n = nameindex; | 101 | n = nameindex; |
129 | p = ifconf.ifc_buf; | 102 | p = ifconf.ifc_buf; |
130 | c = (void *)nameindex + i; | 103 | c = (void *) nameindex + i; |
131 | i = 0; | 104 | i = 0; |
132 | len = ifconf.ifc_len; | 105 | len = ifconf.ifc_len; |
133 | lastname[0] = 0; | 106 | lastname[0] = 0; |
134 | 107 | ||
135 | while(len > 0) { | 108 | while (len > 0) { |
136 | if (len < (IFNAMSIZ + sizeof(struct sockaddr))) | 109 | if (len < IFNAMSIZ + sizeof(struct sockaddr)) |
137 | goto ret; | 110 | goto ret; |
138 | if (strncmp(lastname, p, IFNAMSIZ)) { | 111 | if (strncmp(lastname, p, IFNAMSIZ)) { |
139 | if (i) { | 112 | if (i) { |
140 | if (!n->if_index) | 113 | if (!n->if_index) |
141 | n->if_index = i; | 114 | n->if_index = i; |
142 | n++; | 115 | n++; |
143 | }; | 116 | } |
144 | i++; | 117 | i++; |
145 | memcpy(lastname, p, IFNAMSIZ); | 118 | memcpy(lastname, p, sizeof(lastname)); |
146 | strcpy(n->if_name = c, lastname); | 119 | strlcpy(c, lastname, sizeof(lastname)); |
147 | c += strlen(c) + 1; | 120 | n->if_name = c; |
148 | }; | 121 | c += strlen(c) + 1; |
149 | len -= IFNAMSIZ; | 122 | } |
150 | p += IFNAMSIZ; | 123 | len -= IFNAMSIZ; |
124 | p += IFNAMSIZ; | ||
125 | sa = p; | ||
151 | 126 | ||
152 | if (len < SA_LEN((struct sockaddr *)p)) | 127 | if (len < sa->sa_len) |
153 | goto ret; | 128 | goto ret; |
154 | if (((struct sockaddr *)p)->sa_family == AF_LINK) | 129 | if (sa->sa_family == AF_LINK) { |
155 | n->if_index = ((struct sockaddr_dl *)p)->sdl_index; | 130 | struct sockaddr_dl *sd = (struct sockaddr_dl *)sa; |
156 | len -= SA_LEN((struct sockaddr *)p); | 131 | n->if_index = sd->sdl_index; |
157 | p += SA_LEN((struct sockaddr *)p); | 132 | } |
158 | }; | 133 | len -= sa->sa_len; |
134 | p += sa->sa_len; | ||
135 | } | ||
159 | 136 | ||
160 | if (!n->if_index) | 137 | if (n->if_index == 0) |
161 | n->if_index = i; | 138 | n->if_index = i; |
162 | }; | ||
163 | 139 | ||
164 | ret: | 140 | ret: |
165 | close(fd); | 141 | close(fd); |
166 | free(ifconf.ifc_buf); | 142 | if (ifconf.ifc_buf) |
167 | return nameindex; | 143 | free(ifconf.ifc_buf); |
168 | }; | 144 | return (nameindex); |
145 | } | ||