summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcmetz <>1999-06-23 22:26:03 +0000
committercmetz <>1999-06-23 22:26:03 +0000
commitbb8e4a1e2b3fa93fc2def309cb510f1734df4d77 (patch)
treefb30f8c734151c7d33f82c5347fcd2a0d5b67671
parent8deb91d95656ce00b75b7bbf5b424aa7b9d7faff (diff)
downloadopenbsd-bb8e4a1e2b3fa93fc2def309cb510f1734df4d77.tar.gz
openbsd-bb8e4a1e2b3fa93fc2def309cb510f1734df4d77.tar.bz2
openbsd-bb8e4a1e2b3fa93fc2def309cb510f1734df4d77.zip
Removed portability ifdefs and calls to nonstandard back-end.
-rw-r--r--src/lib/libc/net/__siocgifconf.c131
-rw-r--r--src/lib/libc/net/if_indextoname.c60
-rw-r--r--src/lib/libc/net/if_nameindex.c70
-rw-r--r--src/lib/libc/net/if_nametoindex.c87
4 files changed, 33 insertions, 315 deletions
diff --git a/src/lib/libc/net/__siocgifconf.c b/src/lib/libc/net/__siocgifconf.c
deleted file mode 100644
index 4dac955f56..0000000000
--- a/src/lib/libc/net/__siocgifconf.c
+++ /dev/null
@@ -1,131 +0,0 @@
1/*
2 * %%% copyright-cmetz-98-bsd
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 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Craig Metz and
44 * by other contributors.
45 * 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
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * 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
59 * SUCH DAMAGE.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by Craig Metz and
72 * by other contributors.
73 * 4. Neither the name of the author nor the names of contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 */
89
90#include <sys/types.h>
91#include <stdlib.h>
92#include <sys/socket.h>
93#include <net/if.h>
94#include <sys/ioctl.h>
95#include <errno.h>
96
97int __siocgifconf(int fd, struct ifconf *ifconf)
98{
99 int i, j;
100
101 ifconf->ifc_len = 0;
102 ifconf->ifc_buf = 0;
103 if (!ioctl(fd, SIOCGIFCONF, (void *)ifconf) && ifconf->ifc_len) {
104 if (!(ifconf->ifc_buf = malloc(ifconf->ifc_len))) {
105 errno = ENOMEM;
106 return -1;
107 };
108 if (ioctl(fd, SIOCGIFCONF, (void *)ifconf)) {
109 free(ifconf->ifc_buf);
110 return -1;
111 };
112 return 0;
113 };
114
115 for (i = 5; i < 12; i++) {
116 if (!(ifconf->ifc_buf = malloc(ifconf->ifc_len = j = sizeof(struct ifconf) * (1 << i)))) {
117 errno = ENOMEM;
118 return -1;
119 };
120 if (ioctl(fd, SIOCGIFCONF, (void *)ifconf)) {
121 free(ifconf->ifc_buf);
122 return -1;
123 };
124 if ((ifconf->ifc_len > IFNAMSIZ + sizeof(struct sockaddr))
125 && (ifconf->ifc_len < j))
126 return 0;
127 free(ifconf->ifc_buf);
128 };
129 return -1;
130};
131
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c
index ff7fcada91..1301224444 100644
--- a/src/lib/libc/net/if_indextoname.c
+++ b/src/lib/libc/net/if_indextoname.c
@@ -58,33 +58,6 @@
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE. 59 * SUCH DAMAGE.
60 * 60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by Craig Metz and
72 * by other contributors.
73 * 4. Neither the name of the author nor the names of contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 */ 61 */
89 62
90#include <sys/types.h> 63#include <sys/types.h>
@@ -92,9 +65,7 @@
92#include <sys/socket.h> 65#include <sys/socket.h>
93#include <sys/ioctl.h> 66#include <sys/ioctl.h>
94#include <net/if.h> 67#include <net/if.h>
95#ifdef AF_LINK
96#include <net/if_dl.h> 68#include <net/if_dl.h>
97#endif /* AF_LINK */
98#include <errno.h> 69#include <errno.h>
99 70
100static char __name[IFNAMSIZ + 1]; 71static char __name[IFNAMSIZ + 1];
@@ -102,16 +73,12 @@ static char __name[IFNAMSIZ + 1];
102char *if_indextoname(unsigned int index, char *name) 73char *if_indextoname(unsigned int index, char *name)
103{ 74{
104 int i, fd; 75 int i, fd;
105#ifdef SIOCGIFNAME
106 struct ifreq ifreq;
107#else /* SIOCGIFNAME */
108 struct ifconf ifconf; 76 struct ifconf ifconf;
109 void *p; 77 void *p;
110 int len; 78 int len;
111 char lastname[IFNAMSIZ + 1]; 79 char lastname[IFNAMSIZ + 1];
112 char iname[IFNAMSIZ + 1]; 80 char iname[IFNAMSIZ + 1];
113 char *retname = NULL; 81 char *retname = NULL;
114#endif /* SIOCGIFNAME */
115 82
116 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 83 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
117 return 0; 84 return 0;
@@ -119,20 +86,16 @@ char *if_indextoname(unsigned int index, char *name)
119 if (!name) 86 if (!name)
120 name = __name; 87 name = __name;
121 88
122#ifdef SIOCGIFNAME 89 ifconf.ifc_len = 0;
123 ifreq.ifr_ifindex = index; 90 ifconf.ifc_buf = 0;
124 i = ioctl(fd, SIOCGIFNAME, &ifreq); 91 if (ioctl(fd, SIOCGIFCONF, (void *)ifconf))
125 close(fd); 92 goto ret;
126 if (i) 93 if (ifconf->ifc_len < IFNAMSIZ)
127 return NULL; 94 goto ret;
128 95 if (!(ifconf->ifc_buf = malloc(ifconf->ifc_len)))
129 strcpy(name, ifreq.ifr_name); 96 goto ret;
130 return name; 97 if (ioctl(fd, SIOCGIFCONF, (void *)ifconf))
131#else /* SIOCGIFNAME */ 98 goto ret;
132 if (__siocgifconf(fd, &ifconf)) {
133 close(fd);
134 return NULL;
135 };
136 99
137 i = 0; 100 i = 0;
138 p = ifconf.ifc_buf; 101 p = ifconf.ifc_buf;
@@ -153,13 +116,11 @@ char *if_indextoname(unsigned int index, char *name)
153 len -= IFNAMSIZ; 116 len -= IFNAMSIZ;
154 p += IFNAMSIZ; 117 p += IFNAMSIZ;
155 118
156#ifdef AF_LINK
157 if (((struct sockaddr *)p)->sa_family == AF_LINK) 119 if (((struct sockaddr *)p)->sa_family == AF_LINK)
158 if (((struct sockaddr_dl *)p)->sdl_index == index) { 120 if (((struct sockaddr_dl *)p)->sdl_index == index) {
159 strcpy(retname = name, lastname); 121 strcpy(retname = name, lastname);
160 goto ret; 122 goto ret;
161 }; 123 };
162#endif /* AF_LINK */
163 124
164 if (len < SA_LEN((struct sockaddr *)p)) 125 if (len < SA_LEN((struct sockaddr *)p))
165 goto ret; 126 goto ret;
@@ -177,5 +138,4 @@ ret:
177 close(fd); 138 close(fd);
178 free(ifconf.ifc_buf); 139 free(ifconf.ifc_buf);
179 return retname; 140 return retname;
180#endif /* SIOCGIFNAME */
181}; 141};
diff --git a/src/lib/libc/net/if_nameindex.c b/src/lib/libc/net/if_nameindex.c
index 65bdbde9af..ba7f5e5525 100644
--- a/src/lib/libc/net/if_nameindex.c
+++ b/src/lib/libc/net/if_nameindex.c
@@ -58,33 +58,6 @@
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE. 59 * SUCH DAMAGE.
60 * 60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by Craig Metz and
72 * by other contributors.
73 * 4. Neither the name of the author nor the names of contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 */ 61 */
89 62
90#include <stdlib.h> 63#include <stdlib.h>
@@ -92,9 +65,7 @@
92#include <sys/socket.h> 65#include <sys/socket.h>
93#include <sys/ioctl.h> 66#include <sys/ioctl.h>
94#include <net/if.h> 67#include <net/if.h>
95#ifdef AF_LINK
96#include <net/if_dl.h> 68#include <net/if_dl.h>
97#endif /* AF_LINK */
98#include <errno.h> 69#include <errno.h>
99 70
100struct if_nameindex *if_nameindex(void) 71struct if_nameindex *if_nameindex(void)
@@ -109,10 +80,16 @@ struct if_nameindex *if_nameindex(void)
109 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 80 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
110 return NULL; 81 return NULL;
111 82
112 if (__siocgifconf(fd, &ifconf)) { 83 ifconf.ifc_len = 0;
113 close(fd); 84 ifconf.ifc_buf = 0;
114 return NULL; 85 if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf))
115 }; 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;
116 93
117 i = sizeof(struct if_nameindex); 94 i = sizeof(struct if_nameindex);
118 j = 0; 95 j = 0;
@@ -145,9 +122,6 @@ struct if_nameindex *if_nameindex(void)
145 memset(nameindex, 0, i + j); 122 memset(nameindex, 0, i + j);
146 123
147 { 124 {
148#ifdef SIOCGIFINDEX
149 struct ifreq ifreq;
150#endif /* SIOCGIFINDEX */
151 struct if_nameindex *n; 125 struct if_nameindex *n;
152 char *c; 126 char *c;
153 127
@@ -163,16 +137,8 @@ struct if_nameindex *if_nameindex(void)
163 goto ret; 137 goto ret;
164 if (strncmp(lastname, p, IFNAMSIZ)) { 138 if (strncmp(lastname, p, IFNAMSIZ)) {
165 if (i) { 139 if (i) {
166 if (!n->if_index) { 140 if (!n->if_index)
167#ifdef SIOCGIFINDEX 141 n->if_index = i;
168 strcpy(ifreq.ifr_name, lastname);
169 if (ioctl(fd, SIOCGIFINDEX, &ifreq))
170 goto ret;
171 n->if_index = ifreq.ifr_ifindex;
172#else /* SIOCGIFINDEX */
173 n->if_index = i;
174#endif /* SIOCGIFINDEX */
175 };
176 n++; 142 n++;
177 }; 143 };
178 i++; 144 i++;
@@ -185,24 +151,14 @@ struct if_nameindex *if_nameindex(void)
185 151
186 if (len < SA_LEN((struct sockaddr *)p)) 152 if (len < SA_LEN((struct sockaddr *)p))
187 goto ret; 153 goto ret;
188#ifdef AF_LINK
189 if (((struct sockaddr *)p)->sa_family == AF_LINK) 154 if (((struct sockaddr *)p)->sa_family == AF_LINK)
190 n->if_index = ((struct sockaddr_dl *)p)->sdl_index; 155 n->if_index = ((struct sockaddr_dl *)p)->sdl_index;
191#endif /* AF_LINK */
192 len -= SA_LEN((struct sockaddr *)p); 156 len -= SA_LEN((struct sockaddr *)p);
193 p += SA_LEN((struct sockaddr *)p); 157 p += SA_LEN((struct sockaddr *)p);
194 }; 158 };
195 159
196 if (!n->if_index) { 160 if (!n->if_index)
197#ifdef SIOCGIFINDEX
198 strcpy(ifreq.ifr_name, lastname);
199 if (ioctl(fd, SIOCGIFINDEX, &ifreq))
200 goto ret;
201 n->if_index = ifreq.ifr_ifindex;
202#else /* SIOCGIFINDEX */
203 n->if_index = i; 161 n->if_index = i;
204#endif /* SIOCGIFINDEX */
205 };
206 }; 162 };
207 163
208ret: 164ret:
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c
index 8ec2b233d1..2b9810d314 100644
--- a/src/lib/libc/net/if_nametoindex.c
+++ b/src/lib/libc/net/if_nametoindex.c
@@ -30,61 +30,6 @@
30 * 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
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 * 32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Craig Metz and
44 * by other contributors.
45 * 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
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * 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
59 * SUCH DAMAGE.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by Craig Metz and
72 * by other contributors.
73 * 4. Neither the name of the author nor the names of contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 */ 33 */
89 34
90#include <sys/types.h> 35#include <sys/types.h>
@@ -92,40 +37,31 @@
92#include <sys/socket.h> 37#include <sys/socket.h>
93#include <sys/ioctl.h> 38#include <sys/ioctl.h>
94#include <net/if.h> 39#include <net/if.h>
95#ifdef AF_LINK
96#include <net/if_dl.h> 40#include <net/if_dl.h>
97#endif /* AF_LINK */
98#include <errno.h> 41#include <errno.h>
99 42
100unsigned int if_nametoindex(const char *name) 43unsigned int if_nametoindex(const char *name)
101{ 44{
102 int i, fd; 45 int i, fd;
103#ifdef SIOCGIFINDEX
104 struct ifreq ifreq;
105#else /* SIOCGIFINDEX */
106 struct ifconf ifconf; 46 struct ifconf ifconf;
107 void *p; 47 void *p;
108 int len; 48 int len;
109 char lastname[IFNAMSIZ + 1], *thisname; 49 char lastname[IFNAMSIZ + 1], *thisname;
110 unsigned int index = 0; 50 unsigned int index = 0;
111#endif /* SIOCGIFINDEX */
112 51
113 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 52 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
114 return 0; 53 return 0;
115 54
116#ifdef SIOCGIFINDEX 55 ifconf.ifc_len = 0;
117 strcpy(ifreq.ifr_name, name); 56 ifconf.ifc_buf = 0;
118 i = ioctl(fd, SIOCGIFINDEX, &ifreq); 57 if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf))
119 close(fd); 58 goto ret;
120 if (i) 59 if (ifconf->ifc_len < IFNAMSIZ)
121 return 0; 60 goto ret;
122 61 if (!(ifconf->ifc_buf = malloc(ifconf->ifc_len)))
123 return ifreq.ifr_ifindex; 62 goto ret;
124#else /* SIOCGIFINDEX */ 63 if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf))
125 if (__siocgifconf(fd, &ifconf)) { 64 goto ret;
126 close(fd);
127 return 0;
128 };
129 65
130 i = 0; 66 i = 0;
131 p = ifconf.ifc_buf; 67 p = ifconf.ifc_buf;
@@ -149,12 +85,10 @@ unsigned int if_nametoindex(const char *name)
149 len -= IFNAMSIZ; 85 len -= IFNAMSIZ;
150 p += IFNAMSIZ; 86 p += IFNAMSIZ;
151 87
152#ifdef AF_LINK
153 if (!strncmp(thisname, name, IFNAMSIZ) && (((struct sockaddr *)p)->sa_family == AF_LINK)) { 88 if (!strncmp(thisname, name, IFNAMSIZ) && (((struct sockaddr *)p)->sa_family == AF_LINK)) {
154 index = ((struct sockaddr_dl *)p)->sdl_index; 89 index = ((struct sockaddr_dl *)p)->sdl_index;
155 goto ret; 90 goto ret;
156 }; 91 };
157#endif /* AF_LINK */
158 92
159 if (len < SA_LEN((struct sockaddr *)p)) 93 if (len < SA_LEN((struct sockaddr *)p))
160 goto ret; 94 goto ret;
@@ -169,5 +103,4 @@ ret:
169 close(fd); 103 close(fd);
170 free(ifconf.ifc_buf); 104 free(ifconf.ifc_buf);
171 return index; 105 return index;
172#endif /* SIOCGIFINDEX */
173}; 106};