summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/if_nameindex.c
diff options
context:
space:
mode:
authorcmetz <>1999-06-23 21:55:29 +0000
committercmetz <>1999-06-23 21:55:29 +0000
commitbdf7624d67f3bba1a3faed9abf0d2d27291c1cce (patch)
tree5cb85a83aaa8913b4f148b657a99b1fb4982d71d /src/lib/libc/net/if_nameindex.c
parent119cda95bf950cdcba7a16178e7f48fdf84f58d0 (diff)
downloadopenbsd-bdf7624d67f3bba1a3faed9abf0d2d27291c1cce.tar.gz
openbsd-bdf7624d67f3bba1a3faed9abf0d2d27291c1cce.tar.bz2
openbsd-bdf7624d67f3bba1a3faed9abf0d2d27291c1cce.zip
Added some protocol independent interfaces (supposedly IPv6 support APIs, but
ones that are useful for all protocols, not just IPv6).
Diffstat (limited to 'src/lib/libc/net/if_nameindex.c')
-rw-r--r--src/lib/libc/net/if_nameindex.c212
1 files changed, 212 insertions, 0 deletions
diff --git a/src/lib/libc/net/if_nameindex.c b/src/lib/libc/net/if_nameindex.c
new file mode 100644
index 0000000000..65bdbde9af
--- /dev/null
+++ b/src/lib/libc/net/if_nameindex.c
@@ -0,0 +1,212 @@
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 <stdlib.h>
91#include <sys/types.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
100struct if_nameindex *if_nameindex(void)
101{
102 int i, j, fd;
103 struct if_nameindex *nameindex = NULL;
104 struct ifconf ifconf;
105 void *p;
106 int len;
107 char lastname[IFNAMSIZ + 1];
108
109 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
110 return NULL;
111
112 if (__siocgifconf(fd, &ifconf)) {
113 close(fd);
114 return NULL;
115 };
116
117 i = sizeof(struct if_nameindex);
118 j = 0;
119 p = ifconf.ifc_buf;
120 len = ifconf.ifc_len;
121 lastname[0] = 0;
122 lastname[IFNAMSIZ] = 0;
123
124 while(len > 0) {
125 if (len < (IFNAMSIZ + sizeof(struct sockaddr)))
126 goto ret;
127 if (strncmp(lastname, p, IFNAMSIZ)) {
128 memcpy(lastname, p, IFNAMSIZ);
129 i += sizeof(struct if_nameindex);
130 j += strlen(lastname) + 1;
131 };
132 len -= IFNAMSIZ;
133 p += IFNAMSIZ;
134
135 if (len < SA_LEN((struct sockaddr *)p))
136 goto ret;
137 len -= SA_LEN((struct sockaddr *)p);
138 p += SA_LEN((struct sockaddr *)p);
139 };
140
141 if (!(nameindex = malloc(i + j))) {
142 errno = ENOMEM;
143 goto ret;
144 };
145 memset(nameindex, 0, i + j);
146
147 {
148#ifdef SIOCGIFINDEX
149 struct ifreq ifreq;
150#endif /* SIOCGIFINDEX */
151 struct if_nameindex *n;
152 char *c;
153
154 n = nameindex;
155 p = ifconf.ifc_buf;
156 c = (void *)nameindex + i;
157 i = 0;
158 len = ifconf.ifc_len;
159 lastname[0] = 0;
160
161 while(len > 0) {
162 if (len < (IFNAMSIZ + sizeof(struct sockaddr)))
163 goto ret;
164 if (strncmp(lastname, p, IFNAMSIZ)) {
165 if (i) {
166 if (!n->if_index) {
167#ifdef SIOCGIFINDEX
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++;
177 };
178 i++;
179 memcpy(lastname, p, IFNAMSIZ);
180 strcpy(n->if_name = c, lastname);
181 c += strlen(c) + 1;
182 };
183 len -= IFNAMSIZ;
184 p += IFNAMSIZ;
185
186 if (len < SA_LEN((struct sockaddr *)p))
187 goto ret;
188#ifdef AF_LINK
189 if (((struct sockaddr *)p)->sa_family == AF_LINK)
190 n->if_index = ((struct sockaddr_dl *)p)->sdl_index;
191#endif /* AF_LINK */
192 len -= SA_LEN((struct sockaddr *)p);
193 p += SA_LEN((struct sockaddr *)p);
194 };
195
196 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;
204#endif /* SIOCGIFINDEX */
205 };
206 };
207
208ret:
209 close(fd);
210 free(ifconf.ifc_buf);
211 return nameindex;
212};