1 /*
2 * Copyright (c) 2000 - 2001 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its 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 INSTITUTE 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 INSTITUTE 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 * originally downloaded from
34 *
35 * http://ftp.uninett.no/pub/OpenBSD/src/kerberosV/src/lib/roken/getifaddrs.c
36 */
37
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/ioctl.h>
45 #include <unistd.h>
46
47 #include <libwebsockets.h>
48 #include "private-lib-core.h"
49
50 #ifdef LWS_HAVE_SYS_SOCKIO_H
51 #include <sys/sockio.h>
52 #endif
53
54 #ifdef LWS_HAVE_NETINET_IN6_VAR_H
55 #include <netinet/in6_var.h>
56 #endif
57
58 #ifndef max
59 #define max(a, b) ((a) > (b) ? (a) : (b))
60 #endif
61
62 #include "getifaddrs.h"
63
64 static int
getifaddrs2(struct ifaddrs ** ifap,int af,int siocgifconf,int siocgifflags,size_t ifreq_sz)65 getifaddrs2(struct ifaddrs **ifap, int af, int siocgifconf, int siocgifflags,
66 size_t ifreq_sz)
67 {
68 int ret;
69 int fd;
70 size_t buf_size;
71 char *buf;
72 struct ifconf ifconf;
73 char *p;
74 size_t sz;
75 struct sockaddr sa_zero;
76 struct ifreq *ifr;
77 struct ifaddrs *start, **end = &start;
78
79 buf = NULL;
80
81 memset(&sa_zero, 0, sizeof(sa_zero));
82 fd = socket(af, SOCK_DGRAM, 0);
83 if (fd < 0)
84 return -1;
85
86 buf_size = 8192;
87 for (;;) {
88 buf = lws_zalloc(buf_size, "getifaddrs2");
89 if (buf == NULL) {
90 ret = ENOMEM;
91 goto error_out;
92 }
93 #if defined(__QNX__)
94 ifconf.ifc_len = (short)(int)buf_size;
95 #else
96 ifconf.ifc_len = (int)buf_size;
97 #endif
98 ifconf.ifc_buf = buf;
99
100 /*
101 * Solaris returns EINVAL when the buffer is too small.
102 */
103 if (ioctl(fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
104 ret = errno;
105 goto error_out;
106 }
107 /*
108 * Can the difference between a full and a overfull buf
109 * be determined?
110 */
111
112 if (ifconf.ifc_len < (int)buf_size)
113 break;
114 lws_free(buf);
115 buf_size *= 2;
116 }
117
118 for (p = ifconf.ifc_buf; p < ifconf.ifc_buf + ifconf.ifc_len; p += sz) {
119 struct ifreq ifreq;
120 struct sockaddr *sa;
121 size_t salen;
122
123 ifr = (struct ifreq *)p;
124 sa = &ifr->ifr_addr;
125
126 sz = ifreq_sz;
127 salen = sizeof(struct sockaddr);
128 #ifdef LWS_HAVE_STRUCT_SOCKADDR_SA_LEN
129 salen = sa->sa_len;
130 sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
131 #endif
132 #ifdef SA_LEN
133 salen = SA_LEN(sa);
134 sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
135 #endif
136 memset(&ifreq, 0, sizeof(ifreq));
137 memcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
138
139 if (ioctl(fd, siocgifflags, &ifreq) < 0) {
140 ret = errno;
141 goto error_out;
142 }
143
144 *end = lws_malloc(sizeof(**end), "getifaddrs");
145
146 (*end)->ifa_next = NULL;
147 (*end)->ifa_name = strdup(ifr->ifr_name);
148 (*end)->ifa_flags = (unsigned int)ifreq.ifr_flags;
149 (*end)->ifa_addr = lws_malloc(salen, "getifaddrs");
150 memcpy((*end)->ifa_addr, sa, salen);
151 (*end)->ifa_netmask = NULL;
152
153 #if 0
154 /* fix these when we actually need them */
155 if (ifreq.ifr_flags & IFF_BROADCAST) {
156 (*end)->ifa_broadaddr =
157 lws_malloc(sizeof(ifr->ifr_broadaddr), "getifaddrs");
158 memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
159 sizeof(ifr->ifr_broadaddr));
160 } else if (ifreq.ifr_flags & IFF_POINTOPOINT) {
161 (*end)->ifa_dstaddr =
162 lws_malloc(sizeof(ifr->ifr_dstaddr), "getifaddrs");
163 memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
164 sizeof(ifr->ifr_dstaddr));
165 } else
166 (*end)->ifa_dstaddr = NULL;
167 #else
168 (*end)->ifa_dstaddr = NULL;
169 #endif
170 (*end)->ifa_data = NULL;
171
172 end = &(*end)->ifa_next;
173
174 }
175 *ifap = start;
176 close(fd);
177 lws_free(buf);
178 return 0;
179
180 error_out:
181 close(fd);
182 lws_free(buf);
183 errno = ret;
184
185 return -1;
186 }
187
188 int
getifaddrs(struct ifaddrs ** ifap)189 getifaddrs(struct ifaddrs **ifap)
190 {
191 int ret = -1;
192 errno = ENXIO;
193 #if defined(AF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS)
194 if (ret)
195 ret = getifaddrs2(ifap, AF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS,
196 sizeof(struct in6_ifreq));
197 #endif
198 #if defined(LWS_HAVE_IPV6) && defined(SIOCGIFCONF)
199 if (ret)
200 ret = getifaddrs2(ifap, AF_INET6, SIOCGIFCONF, SIOCGIFFLAGS,
201 sizeof(struct ifreq));
202 #endif
203 #if defined(AF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)
204 if (ret)
205 ret = getifaddrs2(ifap, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
206 sizeof(struct ifreq));
207 #endif
208 return ret;
209 }
210
211 void
freeifaddrs(struct ifaddrs * ifp)212 freeifaddrs(struct ifaddrs *ifp)
213 {
214 struct ifaddrs *p, *q;
215
216 for (p = ifp; p; ) {
217 lws_free(p->ifa_name);
218 lws_free(p->ifa_addr);
219 lws_free(p->ifa_dstaddr);
220 lws_free(p->ifa_netmask);
221 lws_free(p->ifa_data);
222 q = p;
223 p = p->ifa_next;
224 lws_free(q);
225 }
226 }
227
228 #ifdef TEST
229
230 void
print_addr(const char * s,struct sockaddr * sa)231 print_addr(const char *s, struct sockaddr *sa)
232 {
233 int i;
234 printf(" %s=%d/", s, sa->sa_family);
235 #ifdef LWS_HAVE_STRUCT_SOCKADDR_SA_LEN
236 for (i = 0;
237 i < sa->sa_len - ((lws_intptr_t)sa->sa_data - (lws_intptr_t)&sa->sa_family); i++)
238 printf("%02x", ((unsigned char *)sa->sa_data)[i]);
239 #else
240 for (i = 0; i < sizeof(sa->sa_data); i++)
241 printf("%02x", ((unsigned char *)sa->sa_data)[i]);
242 #endif
243 printf("\n");
244 }
245
246 void
print_ifaddrs(struct ifaddrs * x)247 print_ifaddrs(struct ifaddrs *x)
248 {
249 struct ifaddrs *p;
250
251 for (p = x; p; p = p->ifa_next) {
252 printf("%s\n", p->ifa_name);
253 printf(" flags=%x\n", p->ifa_flags);
254 if (p->ifa_addr)
255 print_addr("addr", p->ifa_addr);
256 if (p->ifa_dstaddr)
257 print_addr("dstaddr", p->ifa_dstaddr);
258 if (p->ifa_netmask)
259 print_addr("netmask", p->ifa_netmask);
260 printf(" %p\n", p->ifa_data);
261 }
262 }
263
264 int
main()265 main()
266 {
267 struct ifaddrs *a = NULL, *b;
268 getifaddrs2(&a, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
269 sizeof(struct ifreq));
270 print_ifaddrs(a);
271 printf("---\n");
272 getifaddrs(&b);
273 print_ifaddrs(b);
274 return 0;
275 }
276 #endif
277