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