Revert "strcpy -> strlcpy."
[elinks.git] / src / osdep / getifaddrs.c
blob3936ba7e19951c1d5db3442e704fcd960ef17ac0
1 /* Provide stub function getifaddrs(). */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #ifndef HAVE_GETIFADDRS
8 /* Downloaded from http://ftp.uninett.no/pub/OpenBSD/src/kerberosV/src/lib/roken/getifaddrs.c */
11 * Copyright (c) 2000 - 2001 Kungliga Tekniska Högskolan
12 * (Royal Institute of Technology, Stockholm, Sweden).
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the Institute nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
43 /* ELinksification and improvement of alloc failure handling by Zas.
44 * This file was borrowed from dkftpbench-0.45 sources (http://www.kegel.com/dkftpbench). */
46 #include <errno.h>
47 #include <sys/types.h> /* SunOS needs this before net/if.h */
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h> /* SunOS needs this before net/if.h, and need to be after sys/types.h */
50 #endif
51 #ifdef HAVE_NET_IF_H
52 #include <net/if.h>
53 #endif
54 #ifdef HAVE_NETDB_H
55 #include <netdb.h>
56 #endif
57 #ifdef HAVE_NETINET_IN_H
58 #include <netinet/in.h>
59 #endif
60 #ifdef HAVE_NETINET_IN6_VAR_H
61 #include <netinet/in6_var.h>
62 #endif
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #ifdef HAVE_SYS_IOCTL_H
67 #include <sys/ioctl.h>
68 #endif
69 #ifdef HAVE_FCNTL_H
70 #include <fcntl.h> /* OS/2 needs this after sys/types.h */
71 #endif
72 #ifdef HAVE_UNISTD_H
73 #include <unistd.h>
74 #endif
75 #ifdef HAVE_SYS_SOCKIO_H
76 #include <sys/sockio.h>
77 #endif
79 #include "elinks.h"
81 #include "osdep/osdep.h"
82 #include "osdep/getifaddrs.h"
85 #if (defined(PF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS)) \
86 || (defined(PF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)) \
87 || (defined(CONFIG_IPV6) && defined(SIOCGIFCONF))
89 static int
90 getifaddrs2(struct ifaddrs **ifap,
91 int pf, int siocgifconf, int siocgifflags, size_t ifreq_sz)
93 struct sockaddr sa_zero;
94 struct ifreq *ifr;
95 struct ifaddrs *start;
96 struct ifaddrs **end = &start;
97 struct ifconf ifconf;
98 char *p;
99 char *buf = NULL;
100 size_t buf_size;
101 size_t sz;
102 int ret;
103 int fd;
104 int num, j = 0;
106 memset(&sa_zero, 0, sizeof(sa_zero));
107 fd = socket(pf, SOCK_DGRAM, 0);
108 if (fd < 0)
109 return -1;
111 buf_size = 8192;
112 for (;;) {
113 buf = calloc(1, buf_size);
114 if (buf == NULL) {
115 ret = ENOMEM;
116 goto error_out;
118 ifconf.ifc_len = buf_size;
119 ifconf.ifc_buf = buf;
122 * Solaris returns EINVAL when the buffer is too small.
124 if (ioctl(fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
125 ret = errno;
126 goto error_out;
129 * Can the difference between a full and a overfull buf
130 * be determined?
133 if (ifconf.ifc_len < (int) buf_size)
134 break;
136 free(buf);
137 buf_size *= 2;
140 num = ifconf.ifc_len / ifreq_sz;
141 j = 0;
142 for (p = ifconf.ifc_buf; p < ifconf.ifc_buf + ifconf.ifc_len; p += sz) {
143 struct ifreq ifreq;
144 struct sockaddr *sa;
145 size_t salen;
146 void *tmp_end, *tmp_name, *tmp_addr;
148 ifr = (struct ifreq *) p;
149 sa = &ifr->ifr_addr;
151 sz = ifreq_sz;
152 salen = sizeof(*sa);
153 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
154 salen = sa->sa_len;
155 sz = int_max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
156 #endif
157 #ifdef SA_LEN
158 salen = SA_LEN(sa);
159 sz = int_max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
160 #endif
161 memset(&ifreq, 0, sizeof(ifreq));
162 memcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
164 if (ioctl(fd, siocgifflags, &ifreq) < 0) {
165 ret = errno;
166 goto error_out;
169 tmp_end = malloc(sizeof(**end));
170 if (!tmp_end) {
171 ret = ENOMEM;
172 goto error_out;
175 tmp_name = strdup(ifr->ifr_name);
176 if (!tmp_name) {
177 free(tmp_end);
178 ret = ENOMEM;
179 goto error_out;
182 tmp_addr = malloc(salen);
183 if (!tmp_addr) {
184 free(tmp_end);
185 free(tmp_name);
186 ret = ENOMEM;
187 goto error_out;
190 *end = tmp_end;
191 (*end)->ifa_next = NULL;
192 (*end)->ifa_name = tmp_name;
193 (*end)->ifa_flags = ifreq.ifr_flags;
194 (*end)->ifa_addr = tmp_addr;
195 memcpy((*end)->ifa_addr, sa, salen);
196 (*end)->ifa_netmask = NULL;
198 #if 0
199 /* fix these when we actually need them */
200 if (ifreq.ifr_flags & IFF_BROADCAST) {
201 (*end)->ifa_broadaddr =
202 malloc(sizeof(ifr->ifr_broadaddr));
203 memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
204 sizeof(ifr->ifr_broadaddr));
205 } else if (ifreq.ifr_flags & IFF_POINTOPOINT) {
206 (*end)->ifa_dstaddr = malloc(sizeof(ifr->ifr_dstaddr));
207 memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
208 sizeof(ifr->ifr_dstaddr));
209 } else
210 (*end)->ifa_dstaddr = NULL;
211 #else
212 (*end)->ifa_dstaddr = NULL;
213 #endif
215 (*end)->ifa_data = NULL;
217 end = &(*end)->ifa_next;
221 *ifap = start;
222 close(fd);
223 free(buf);
225 return 0;
227 error_out:
228 close(fd);
229 if (buf) free(buf);
230 errno = ret;
232 return -1;
234 #endif
237 getifaddrs(struct ifaddrs **ifap)
239 int ret = -1;
241 errno = ENXIO;
243 #if defined(PF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS)
244 if (ret)
245 ret = getifaddrs2(ifap, PF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS,
246 sizeof(struct in6_ifreq));
247 #endif
248 #if defined(CONFIG_IPV6) && defined(SIOCGIFCONF)
249 if (ret)
250 ret = getifaddrs2(ifap, PF_INET6, SIOCGIFCONF, SIOCGIFFLAGS,
251 sizeof(struct ifreq));
252 #endif
253 #if defined(PF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)
254 if (ret)
255 ret = getifaddrs2(ifap, PF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
256 sizeof(struct ifreq));
257 #endif
259 return ret;
262 void
263 freeifaddrs(struct ifaddrs *ifp)
265 struct ifaddrs *p, *q;
267 for (p = ifp; p;) {
268 free(p->ifa_name);
269 if (p->ifa_addr)
270 free(p->ifa_addr);
271 if (p->ifa_dstaddr)
272 free(p->ifa_dstaddr);
273 if (p->ifa_netmask)
274 free(p->ifa_netmask);
275 if (p->ifa_data)
276 free(p->ifa_data);
277 q = p;
278 p = p->ifa_next;
279 free(q);
283 #ifdef TEST_GETIFADDRS
285 void
286 print_addr(const char *s, struct sockaddr *sa)
288 int i;
290 printf(" %s=%d/", s, sa->sa_family);
291 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
292 for (i = 0;
293 i < sa->sa_len - ((long) sa->sa_data - (long) &sa->sa_family); i++)
294 printf("%02x", ((unsigned char *) sa->sa_data)[i]);
295 #else
296 for (i = 0; i < sizeof(sa->sa_data); i++)
297 printf("%02x", ((unsigned char *) sa->sa_data)[i]);
298 #endif
299 printf("\n");
302 void
303 print_ifaddrs(struct ifaddrs *x)
305 struct ifaddrs *p;
307 for (p = x; p; p = p->ifa_next) {
308 printf("%s\n", p->ifa_name);
309 printf(" flags=%x\n", p->ifa_flags);
310 if (p->ifa_addr)
311 print_addr("addr", p->ifa_addr);
312 if (p->ifa_dstaddr)
313 print_addr("dstaddr", p->ifa_dstaddr);
314 if (p->ifa_netmask)
315 print_addr("netmask", p->ifa_netmask);
316 printf(" %p\n", p->ifa_data);
321 main()
323 struct ifaddrs *a = NULL, *b;
325 getifaddrs2(&a, PF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
326 sizeof(struct ifreq));
327 print_ifaddrs(a);
328 printf("---\n");
329 getifaddrs(&b);
330 print_ifaddrs(b);
331 return 0;
334 #endif /* TEST_GETIFADDRS */
336 #endif /* HAVE_GETIFADDRS */