getifaddrs needs private libwebsockets.h
[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 !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 HAVE_SYS_SOCKIO_H
50 #include <sys/sockio.h>
51 #endif
52
53 #ifdef 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,
65             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
78         struct ifaddrs *start,  **end = &start;
79
80         buf = NULL;
81
82         memset(&sa_zero, 0, sizeof(sa_zero));
83         fd = socket(af, SOCK_DGRAM, 0);
84         if (fd < 0)
85                 return -1;
86
87         buf_size = 8192;
88         for (;;) {
89                 buf = lws_zalloc(buf_size);
90                 if (buf == NULL) {
91                         ret = ENOMEM;
92                         goto error_out;
93                 }
94                 ifconf.ifc_len = buf_size;
95                 ifconf.ifc_buf = buf;
96
97                 /*
98                  * Solaris returns EINVAL when the buffer is too small.
99                  */
100                 if (ioctl(fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
101                         ret = errno;
102                         goto error_out;
103                 }
104                 /*
105                  * Can the difference between a full and a overfull buf
106                  * be determined?
107                  */
108
109                 if (ifconf.ifc_len < (int)buf_size)
110                         break;
111                 lws_free(buf);
112                 buf_size *= 2;
113         }
114
115         for (p = ifconf.ifc_buf; p < ifconf.ifc_buf + ifconf.ifc_len; p += sz) {
116                 struct ifreq ifreq;
117                 struct sockaddr *sa;
118                 size_t salen;
119
120                 ifr = (struct ifreq *)p;
121                 sa  = &ifr->ifr_addr;
122
123                 sz = ifreq_sz;
124                 salen = sizeof(struct sockaddr);
125 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
126                 salen = sa->sa_len;
127                 sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
128 #endif
129 #ifdef SA_LEN
130                 salen = SA_LEN(sa);
131                 sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
132 #endif
133                 memset(&ifreq, 0, sizeof(ifreq));
134                 memcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
135
136                 if (ioctl(fd, siocgifflags, &ifreq) < 0) {
137                         ret = errno;
138                         goto error_out;
139                 }
140
141                 *end = lws_malloc(sizeof(**end));
142
143                 (*end)->ifa_next = NULL;
144                 (*end)->ifa_name = strdup(ifr->ifr_name);
145                 (*end)->ifa_flags = ifreq.ifr_flags;
146                 (*end)->ifa_addr = lws_malloc(salen);
147                 memcpy((*end)->ifa_addr, sa, salen);
148                 (*end)->ifa_netmask = NULL;
149
150 #if 0
151                 /* fix these when we actually need them */
152                 if (ifreq.ifr_flags & IFF_BROADCAST) {
153                         (*end)->ifa_broadaddr =
154                                 lws_malloc(sizeof(ifr->ifr_broadaddr));
155                         memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
156                                                     sizeof(ifr->ifr_broadaddr));
157                 } else if (ifreq.ifr_flags & IFF_POINTOPOINT) {
158                         (*end)->ifa_dstaddr =
159                                 lws_malloc(sizeof(ifr->ifr_dstaddr));
160                         memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
161                                                       sizeof(ifr->ifr_dstaddr));
162                 } else
163                         (*end)->ifa_dstaddr = NULL;
164 #else
165                 (*end)->ifa_dstaddr = NULL;
166 #endif
167                 (*end)->ifa_data = NULL;
168
169                 end = &(*end)->ifa_next;
170
171         }
172         *ifap = start;
173         close(fd);
174         lws_free(buf);
175         return 0;
176
177 error_out:
178         close(fd);
179         lws_free(buf);
180         errno = ret;
181
182         return -1;
183 }
184
185 int
186 getifaddrs(struct ifaddrs **ifap)
187 {
188         int ret = -1;
189         errno = ENXIO;
190 #if defined(AF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS)
191         if (ret)
192                 ret = getifaddrs2(ifap, AF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS,
193                            sizeof(struct in6_ifreq));
194 #endif
195 #if defined(HAVE_IPV6) && defined(SIOCGIFCONF)
196         if (ret)
197                 ret = getifaddrs2(ifap, AF_INET6, SIOCGIFCONF, SIOCGIFFLAGS,
198                            sizeof(struct ifreq));
199 #endif
200 #if defined(AF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)
201         if (ret)
202                 ret = getifaddrs2(ifap, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
203                            sizeof(struct ifreq));
204 #endif
205         return ret;
206 }
207
208 void
209 freeifaddrs(struct ifaddrs *ifp)
210 {
211         struct ifaddrs *p, *q;
212
213         for (p = ifp; p; ) {
214                 lws_free(p->ifa_name);
215                 lws_free(p->ifa_addr);
216                 lws_free(p->ifa_dstaddr);
217                 lws_free(p->ifa_netmask);
218                 lws_free(p->ifa_data);
219                 q = p;
220                 p = p->ifa_next;
221                 lws_free(q);
222         }
223 }
224
225 #ifdef TEST
226
227 void
228 print_addr(const char *s, struct sockaddr *sa)
229 {
230         int i;
231         printf("  %s=%d/", s, sa->sa_family);
232 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
233         for (i = 0;
234                i < sa->sa_len - ((long)sa->sa_data - (long)&sa->sa_family); i++)
235                 printf("%02x", ((unsigned char *)sa->sa_data)[i]);
236 #else
237         for (i = 0; i < sizeof(sa->sa_data); i++)
238                 printf("%02x", ((unsigned char *)sa->sa_data)[i]);
239 #endif
240         printf("\n");
241 }
242
243 void
244 print_ifaddrs(struct ifaddrs *x)
245 {
246         struct ifaddrs *p;
247
248         for (p = x; p; p = p->ifa_next) {
249                 printf("%s\n", p->ifa_name);
250                 printf("  flags=%x\n", p->ifa_flags);
251                 if (p->ifa_addr)
252                         print_addr("addr", p->ifa_addr);
253                 if (p->ifa_dstaddr)
254                         print_addr("dstaddr", p->ifa_dstaddr);
255                 if (p->ifa_netmask)
256                         print_addr("netmask", p->ifa_netmask);
257                 printf("  %p\n", p->ifa_data);
258         }
259 }
260
261 int
262 main()
263 {
264         struct ifaddrs *a = NULL, *b;
265         getifaddrs2(&a, AF_INET, SIOCGIFCONF,
266                                 SIOCGIFFLAGS, sizeof(struct ifreq));
267         print_ifaddrs(a);
268         printf("---\n");
269         getifaddrs(&b);
270         print_ifaddrs(b);
271         return 0;
272 }
273 #endif
274 #endif