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