Deleted Added
sdiff udiff text old ( 78527 ) new ( 90926 )
full compact
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
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36RCSID("$Id: getifaddrs.c,v 1.5 2001/04/17 08:27:47 joda Exp $");
37#endif
38#include "roken.h"
39
40#ifdef __osf__
41/* hate */
42struct rtentry;
43struct mbuf;
44#endif
45#ifdef HAVE_NET_IF_H
46#include <net/if.h>
47#endif
48
49#ifdef HAVE_SYS_SOCKIO_H
50#include <sys/sockio.h>
51#endif /* HAVE_SYS_SOCKIO_H */
52
53#ifdef HAVE_NETINET_IN6_VAR_H
54#include <netinet/in6_var.h>
55#endif /* HAVE_NETINET_IN6_VAR_H */
56
57#include <ifaddrs.h>
58
59static int
60getifaddrs2(struct ifaddrs **ifap,
61 int af, int siocgifconf, int siocgifflags,
62 size_t ifreq_sz)
63{
64 int ret;
65 int fd;
66 size_t buf_size;
67 char *buf;
68 struct ifconf ifconf;
69 int num, j = 0;
70 char *p;
71 size_t sz;
72 struct sockaddr sa_zero;
73 struct ifreq *ifr;
74
75 struct ifaddrs *start, **end = &start;
76
77 buf = NULL;
78
79 memset (&sa_zero, 0, sizeof(sa_zero));
80 fd = socket(af, SOCK_DGRAM, 0);
81 if (fd < 0)
82 return -1;
83
84 buf_size = 8192;
85 for (;;) {
86 buf = calloc(1, buf_size);
87 if (buf == NULL) {
88 ret = ENOMEM;
89 goto error_out;
90 }
91 ifconf.ifc_len = buf_size;
92 ifconf.ifc_buf = buf;
93
94 /*
95 * Solaris returns EINVAL when the buffer is too small.
96 */
97 if (ioctl (fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
98 ret = errno;
99 goto error_out;
100 }
101 /*
102 * Can the difference between a full and a overfull buf
103 * be determined?
104 */
105
106 if (ifconf.ifc_len < buf_size)
107 break;
108 free (buf);
109 buf_size *= 2;
110 }
111
112 num = ifconf.ifc_len / ifreq_sz;
113 j = 0;
114 for (p = ifconf.ifc_buf;
115 p < ifconf.ifc_buf + ifconf.ifc_len;
116 p += sz) {
117 struct ifreq ifreq;
118 struct sockaddr *sa;
119 size_t salen;
120
121 ifr = (struct ifreq *)p;
122 sa = &ifr->ifr_addr;
123
124 sz = ifreq_sz;
125 salen = sizeof(struct sockaddr);
126#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
127 salen = sa->sa_len;
128 sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
129#endif
130#ifdef SA_LEN
131 salen = SA_LEN(sa);
132 sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
133#endif
134 memset (&ifreq, 0, sizeof(ifreq));
135 memcpy (ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
136
137 if (ioctl(fd, siocgifflags, &ifreq) < 0) {
138 ret = errno;
139 goto error_out;
140 }
141
142 *end = malloc(sizeof(**end));
143
144 (*end)->ifa_next = NULL;
145 (*end)->ifa_name = strdup(ifr->ifr_name);
146 (*end)->ifa_flags = ifreq.ifr_flags;
147 (*end)->ifa_addr = malloc(salen);
148 memcpy((*end)->ifa_addr, sa, salen);
149 (*end)->ifa_netmask = NULL;
150
151#if 0
152 /* fix these when we actually need them */
153 if(ifreq.ifr_flags & IFF_BROADCAST) {
154 (*end)->ifa_broadaddr = 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 = 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
167 (*end)->ifa_data = NULL;
168
169 end = &(*end)->ifa_next;
170
171 }
172 *ifap = start;
173 close(fd);
174 free(buf);
175 return 0;
176 error_out:
177 close(fd);
178 free(buf);
179 errno = ret;
180 return -1;
181}
182
183int
184getifaddrs(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(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
206void
207freeifaddrs(struct ifaddrs *ifp)
208{
209 struct ifaddrs *p, *q;
210
211 for(p = ifp; p; ) {
212 free(p->ifa_name);
213 if(p->ifa_addr)
214 free(p->ifa_addr);
215 if(p->ifa_dstaddr)
216 free(p->ifa_dstaddr);
217 if(p->ifa_netmask)
218 free(p->ifa_netmask);
219 if(p->ifa_data)
220 free(p->ifa_data);
221 q = p;
222 p = p->ifa_next;
223 free(q);
224 }
225}
226
227#ifdef TEST
228
229void
230print_addr(const char *s, struct sockaddr *sa)
231{
232 int i;
233 printf(" %s=%d/", s, sa->sa_family);
234#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
235 for(i = 0; i < sa->sa_len - ((long)sa->sa_data - (long)&sa->sa_family); i++)
236 printf("%02x", ((unsigned char*)sa->sa_data)[i]);
237#else
238 for(i = 0; i < sizeof(sa->sa_data); i++)
239 printf("%02x", ((unsigned char*)sa->sa_data)[i]);
240#endif
241 printf("\n");
242}
243
244void
245print_ifaddrs(struct ifaddrs *x)
246{
247 struct ifaddrs *p;
248
249 for(p = x; p; p = p->ifa_next) {
250 printf("%s\n", p->ifa_name);
251 printf(" flags=%x\n", p->ifa_flags);
252 if(p->ifa_addr)
253 print_addr("addr", p->ifa_addr);
254 if(p->ifa_dstaddr)
255 print_addr("dstaddr", p->ifa_dstaddr);
256 if(p->ifa_netmask)
257 print_addr("netmask", p->ifa_netmask);
258 printf(" %p\n", p->ifa_data);
259 }
260}
261
262int
263main()
264{
265 struct ifaddrs *a = NULL, *b;
266 getifaddrs2(&a, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS, sizeof(struct ifreq));
267 print_ifaddrs(a);
268 printf("---\n");
269 getifaddrs(&b);
270 print_ifaddrs(b);
271 return 0;
272}
273#endif