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