summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorderaadt <>1999-07-01 15:35:59 +0000
committerderaadt <>1999-07-01 15:35:59 +0000
commit46185cb50e9a84c11cc2fadb8473a7e5fade5720 (patch)
treefedc56b026976844b92f1a6a2c1ada08e2dccc4e
parentdd458924abbd1c3b8bc94a76408b499c98e3b95f (diff)
downloadopenbsd-46185cb50e9a84c11cc2fadb8473a7e5fade5720.tar.gz
openbsd-46185cb50e9a84c11cc2fadb8473a7e5fade5720.tar.bz2
openbsd-46185cb50e9a84c11cc2fadb8473a7e5fade5720.zip
indent and audit
-rw-r--r--src/lib/libc/net/if_indextoname.c154
-rw-r--r--src/lib/libc/net/if_nameindex.c207
-rw-r--r--src/lib/libc/net/if_nametoindex.c117
3 files changed, 220 insertions, 258 deletions
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c
index b3c89fb9a8..cca862de61 100644
--- a/src/lib/libc/net/if_indextoname.c
+++ b/src/lib/libc/net/if_indextoname.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 <sys/types.h> 34#include <sys/types.h>
@@ -67,75 +38,82 @@
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
71static char __name[IFNAMSIZ + 1]; 43static char __name[IFNAMSIZ];
72 44
73char *if_indextoname(unsigned int index, char *name) 45char *
46if_indextoname(unsigned int index, char *name)
74{ 47{
75 int i, fd; 48 int i, fd, len;
76 struct ifconf ifconf; 49 struct ifconf ifconf;
77 void *p; 50 char lastname[IFNAMSIZ], iname[IFNAMSIZ], *retname = NULL;
78 int len; 51 struct sockaddr *sa;
79 char lastname[IFNAMSIZ + 1]; 52 void *p;
80 char iname[IFNAMSIZ + 1];
81 char *retname = NULL;
82 53
83 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 54 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
84 return 0; 55 return 0;
85 56
86 if (!name) 57 if (!name)
87 name = __name; 58 name = __name;
88 59
89 ifconf.ifc_len = 0; 60 ifconf.ifc_len = 0;
90 ifconf.ifc_buf = 0; 61 ifconf.ifc_buf = 0;
91 if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) 62 if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf))
92 goto ret; 63 goto ret;
93 if (ifconf.ifc_len < IFNAMSIZ) 64 if (ifconf.ifc_len < IFNAMSIZ)
94 goto ret; 65 goto ret;
95 if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) 66 if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len)))
96 goto ret; 67 goto ret;
97 if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) 68 if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf))
98 goto ret; 69 goto ret;
99 70
100 i = 0; 71 i = 0;
101 p = ifconf.ifc_buf; 72 p = ifconf.ifc_buf;
102 len = ifconf.ifc_len; 73 len = ifconf.ifc_len;
103 lastname[0] = 0; 74 lastname[0] = 0;
104 lastname[IFNAMSIZ] = 0; 75 lastname[sizeof(lastname)-1] = 0;
105 iname[0] = 0; 76 iname[0] = 0;
106 77
107 while(len > 0) { 78 while (len > 0) {
108 if (len < (IFNAMSIZ + sizeof(struct sockaddr))) 79 if (len < (IFNAMSIZ + sizeof(struct sockaddr)))
109 goto ret; 80 goto ret;
110 if (strncmp(lastname, p, IFNAMSIZ)) { 81 if (strncmp(lastname, p, IFNAMSIZ)) {
111 if (i == index) 82 if (i == index)
112 strcpy(iname, lastname); 83 memcpy(iname, lastname, sizeof(iname));
113 memcpy(lastname, p, IFNAMSIZ); 84 strlcpy(lastname, p, sizeof(lastname));
114 i++; 85 i++;
115 }; 86 }
116 len -= IFNAMSIZ; 87 len -= IFNAMSIZ;
117 p += IFNAMSIZ; 88 p += IFNAMSIZ;
89 sa = p;
118 90
119 if (((struct sockaddr *)p)->sa_family == AF_LINK) 91 if (sa->sa_family == AF_LINK) {
120 if (((struct sockaddr_dl *)p)->sdl_index == index) { 92 struct sockaddr_dl *sd = p;
121 strcpy(retname = name, lastname);
122 goto ret;
123 };
124 93
125 if (len < SA_LEN((struct sockaddr *)p)) 94 if (sd->sdl_index == index) {
126 goto ret; 95 strlcpy(name, lastname, sizeof(name));
127 len -= SA_LEN((struct sockaddr *)p); 96 retname = name;
128 p += SA_LEN((struct sockaddr *)p); 97 goto ret;
129 }; 98 }
99 }
130 100
131 if (i == index) 101 if (len < sa->sa_len)
132 strcpy(iname, lastname); 102 goto ret;
103 len -= sa->sa_len;
104 p += sa->sa_len;
105 }
133 106
134 if (iname[0]) 107 if (i == index)
135 strcpy(retname = name, iname); 108 strlcpy(iname, lastname, sizeof(iname));
136 109
110 if (iname[0]) {
111 strlcpy(name, iname, sizeof(name));
112 retname = name;
113 }
137ret: 114ret:
138 close(fd); 115 close(fd);
139 free(ifconf.ifc_buf); 116 if (ifconf.ifc_buf)
140 return retname; 117 free(ifconf.ifc_buf);
141}; 118 return (retname);
119}
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
71struct if_nameindex *if_nameindex(void) 43struct if_nameindex *
44if_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
164ret: 140ret:
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}
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c
index ad05387e34..0318f60680 100644
--- a/src/lib/libc/net/if_nametoindex.c
+++ b/src/lib/libc/net/if_nametoindex.c
@@ -1,7 +1,7 @@
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 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
@@ -17,7 +17,7 @@
17 * 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
18 * 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
19 * without specific prior written permission. 19 * without specific prior written permission.
20 * 20 *
21 * 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
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -29,7 +29,6 @@
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 *
33 */ 32 */
34 33
35#include <sys/types.h> 34#include <sys/types.h>
@@ -38,69 +37,77 @@
38#include <sys/ioctl.h> 37#include <sys/ioctl.h>
39#include <net/if.h> 38#include <net/if.h>
40#include <net/if_dl.h> 39#include <net/if_dl.h>
40#include <string.h>
41#include <errno.h> 41#include <errno.h>
42 42
43unsigned int if_nametoindex(const char *name) 43unsigned int
44if_nametoindex(const char *name)
44{ 45{
45 int i, fd; 46 int i, fd, len;
46 struct ifconf ifconf; 47 struct ifconf ifconf;
47 void *p; 48 char lastname[IFNAMSIZ], *thisname;
48 int len; 49 unsigned int index = 0;
49 char lastname[IFNAMSIZ + 1], *thisname; 50 struct sockaddr *sa;
50 unsigned int index = 0; 51 void *p;
52
53 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
54 return 0;
51 55
52 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 56 ifconf.ifc_len = 0;
53 return 0; 57 ifconf.ifc_buf = 0;
58 if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf))
59 goto ret;
60 if (ifconf.ifc_len < IFNAMSIZ)
61 goto ret;
62 if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len)))
63 goto ret;
64 if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf))
65 goto ret;
54 66
55 ifconf.ifc_len = 0; 67 i = 0;
56 ifconf.ifc_buf = 0; 68 p = ifconf.ifc_buf;
57 if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) 69 len = ifconf.ifc_len;
58 goto ret; 70 lastname[0] = 0;
59 if (ifconf.ifc_len < IFNAMSIZ) 71 lastname[sizeof(lastname)-1] = 0;
60 goto ret;
61 if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len)))
62 goto ret;
63 if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf))
64 goto ret;
65 72
66 i = 0; 73 while (len > 0) {
67 p = ifconf.ifc_buf; 74 if (len < IFNAMSIZ + sizeof(struct sockaddr))
68 len = ifconf.ifc_len; 75 goto ret;
69 lastname[0] = 0;
70 lastname[IFNAMSIZ] = 0;
71 76
72 while(len > 0) { 77 thisname = p;
73 if (len < (IFNAMSIZ + sizeof(struct sockaddr))) 78 if (strncmp(lastname, p, IFNAMSIZ)) {
74 goto ret; 79 if (!strcmp(lastname, name)) {
80 index = i;
81 goto ret;
82 }
83 memcpy(lastname, thisname, IFNAMSIZ);
84 i++;
85 }
75 86
76 if (strncmp(lastname, thisname = p, IFNAMSIZ)) { 87 len -= IFNAMSIZ;
77 if (!strcmp(lastname, name)) { 88 p += IFNAMSIZ;
78 index = i; 89 sa = (struct sockaddr *)p;
79 goto ret;
80 };
81 memcpy(lastname, thisname, IFNAMSIZ);
82 i++;
83 };
84 90
85 len -= IFNAMSIZ; 91 if (!strncmp(thisname, name, IFNAMSIZ) &&
86 p += IFNAMSIZ; 92 sa->sa_family == AF_LINK) {
93 struct sockaddr_dl *sd = p;
87 94
88 if (!strncmp(thisname, name, IFNAMSIZ) && (((struct sockaddr *)p)->sa_family == AF_LINK)) { 95 index = sd->sdl_index;
89 index = ((struct sockaddr_dl *)p)->sdl_index; 96 goto ret;
90 goto ret; 97 }
91 };
92 98
93 if (len < SA_LEN((struct sockaddr *)p)) 99 if (len < sa->sa_len)
94 goto ret; 100 goto ret;
95 len -= SA_LEN((struct sockaddr *)p); 101 len -= sa->sa_len;
96 p += SA_LEN((struct sockaddr *)p); 102 p += sa->sa_len;
97 }; 103 }
98 104
99 if (!strcmp(lastname, name)) 105 if (!strcmp(lastname, name))
100 index = i; 106 index = i;
101 107
102ret: 108ret:
103 close(fd); 109 close(fd);
104 free(ifconf.ifc_buf); 110 if (ifconf.ifc_buf)
105 return index; 111 free(ifconf.ifc_buf);
106}; 112 return index;
113}