diff options
Diffstat (limited to 'src/lib/libc/net/if_nametoindex.c')
-rw-r--r-- | src/lib/libc/net/if_nametoindex.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c new file mode 100644 index 0000000000..8ec2b233d1 --- /dev/null +++ b/src/lib/libc/net/if_nametoindex.c | |||
@@ -0,0 +1,173 @@ | |||
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 <sys/ioctl.h> | ||
94 | #include <net/if.h> | ||
95 | #ifdef AF_LINK | ||
96 | #include <net/if_dl.h> | ||
97 | #endif /* AF_LINK */ | ||
98 | #include <errno.h> | ||
99 | |||
100 | unsigned int if_nametoindex(const char *name) | ||
101 | { | ||
102 | int i, fd; | ||
103 | #ifdef SIOCGIFINDEX | ||
104 | struct ifreq ifreq; | ||
105 | #else /* SIOCGIFINDEX */ | ||
106 | struct ifconf ifconf; | ||
107 | void *p; | ||
108 | int len; | ||
109 | char lastname[IFNAMSIZ + 1], *thisname; | ||
110 | unsigned int index = 0; | ||
111 | #endif /* SIOCGIFINDEX */ | ||
112 | |||
113 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | ||
114 | return 0; | ||
115 | |||
116 | #ifdef SIOCGIFINDEX | ||
117 | strcpy(ifreq.ifr_name, name); | ||
118 | i = ioctl(fd, SIOCGIFINDEX, &ifreq); | ||
119 | close(fd); | ||
120 | if (i) | ||
121 | return 0; | ||
122 | |||
123 | return ifreq.ifr_ifindex; | ||
124 | #else /* SIOCGIFINDEX */ | ||
125 | if (__siocgifconf(fd, &ifconf)) { | ||
126 | close(fd); | ||
127 | return 0; | ||
128 | }; | ||
129 | |||
130 | i = 0; | ||
131 | p = ifconf.ifc_buf; | ||
132 | len = ifconf.ifc_len; | ||
133 | lastname[0] = 0; | ||
134 | lastname[IFNAMSIZ] = 0; | ||
135 | |||
136 | while(len > 0) { | ||
137 | if (len < (IFNAMSIZ + sizeof(struct sockaddr))) | ||
138 | goto ret; | ||
139 | |||
140 | if (strncmp(lastname, thisname = p, IFNAMSIZ)) { | ||
141 | if (!strcmp(lastname, name)) { | ||
142 | index = i; | ||
143 | goto ret; | ||
144 | }; | ||
145 | memcpy(lastname, thisname, IFNAMSIZ); | ||
146 | i++; | ||
147 | }; | ||
148 | |||
149 | len -= IFNAMSIZ; | ||
150 | p += IFNAMSIZ; | ||
151 | |||
152 | #ifdef AF_LINK | ||
153 | if (!strncmp(thisname, name, IFNAMSIZ) && (((struct sockaddr *)p)->sa_family == AF_LINK)) { | ||
154 | index = ((struct sockaddr_dl *)p)->sdl_index; | ||
155 | goto ret; | ||
156 | }; | ||
157 | #endif /* AF_LINK */ | ||
158 | |||
159 | if (len < SA_LEN((struct sockaddr *)p)) | ||
160 | goto ret; | ||
161 | len -= SA_LEN((struct sockaddr *)p); | ||
162 | p += SA_LEN((struct sockaddr *)p); | ||
163 | }; | ||
164 | |||
165 | if (!strcmp(lastname, name)) | ||
166 | index = i; | ||
167 | |||
168 | ret: | ||
169 | close(fd); | ||
170 | free(ifconf.ifc_buf); | ||
171 | return index; | ||
172 | #endif /* SIOCGIFINDEX */ | ||
173 | }; | ||