Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / libpcap / dist / fad-gifc.c
blob1e25e5bccb9dc8d370e28863d1a1d7335fd114e0
1 /* $NetBSD: fad-gifc.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
3 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
4 /*
5 * Copyright (c) 1994, 1995, 1996, 1997, 1998
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the Computer Systems
19 * Engineering Group at Lawrence Berkeley Laboratory.
20 * 4. Neither the name of the University nor of the Laboratory may be used
21 * to endorse or promote products derived from this software without
22 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: fad-gifc.c,v 1.3 2015/03/31 21:39:42 christos Exp $");
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #ifdef HAVE_SYS_SOCKIO_H
48 #include <sys/sockio.h>
49 #endif
50 #include <sys/time.h> /* concession to AIX */
52 struct mbuf; /* Squelch compiler warnings on some platforms for */
53 struct rtentry; /* declarations in <net/if.h> */
54 #include <net/if.h>
55 #include <netinet/in.h>
57 #include <ctype.h>
58 #include <errno.h>
59 #include <memory.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
65 #include "pcap-int.h"
67 #ifdef HAVE_OS_PROTO_H
68 #include "os-proto.h"
69 #endif
72 * This is fun.
74 * In older BSD systems, socket addresses were fixed-length, and
75 * "sizeof (struct sockaddr)" gave the size of the structure.
76 * All addresses fit within a "struct sockaddr".
78 * In newer BSD systems, the socket address is variable-length, and
79 * there's an "sa_len" field giving the length of the structure;
80 * this allows socket addresses to be longer than 2 bytes of family
81 * and 14 bytes of data.
83 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
84 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
85 * than "struct sockaddr"), and some use the new BSD scheme.
87 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
88 * macro that determines the size based on the address family. Other
89 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
90 * but not in the final version).
92 * We assume that a UNIX that doesn't have "getifaddrs()" and doesn't have
93 * SIOCGLIFCONF, but has SIOCGIFCONF, uses "struct sockaddr" for the
94 * address in an entry returned by SIOCGIFCONF.
96 #ifndef SA_LEN
97 #ifdef HAVE_SOCKADDR_SA_LEN
98 #define SA_LEN(addr) ((addr)->sa_len)
99 #else /* HAVE_SOCKADDR_SA_LEN */
100 #define SA_LEN(addr) (sizeof (struct sockaddr))
101 #endif /* HAVE_SOCKADDR_SA_LEN */
102 #endif /* SA_LEN */
105 * This is also fun.
107 * There is no ioctl that returns the amount of space required for all
108 * the data that SIOCGIFCONF could return, and if a buffer is supplied
109 * that's not large enough for all the data SIOCGIFCONF could return,
110 * on at least some platforms it just returns the data that'd fit with
111 * no indication that there wasn't enough room for all the data, much
112 * less an indication of how much more room is required.
114 * The only way to ensure that we got all the data is to pass a buffer
115 * large enough that the amount of space in the buffer *not* filled in
116 * is greater than the largest possible entry.
118 * We assume that's "sizeof(ifreq.ifr_name)" plus 255, under the assumption
119 * that no address is more than 255 bytes (on systems where the "sa_len"
120 * field in a "struct sockaddr" is 1 byte, e.g. newer BSDs, that's the
121 * case, and addresses are unlikely to be bigger than that in any case).
123 #define MAX_SA_LEN 255
126 * Get a list of all interfaces that are up and that we can open.
127 * Returns -1 on error, 0 otherwise.
128 * The list, as returned through "alldevsp", may be null if no interfaces
129 * were up and could be opened.
131 * This is the implementation used on platforms that have SIOCGIFCONF but
132 * don't have any other mechanism for getting a list of interfaces.
134 * XXX - or platforms that have other, better mechanisms but for which
135 * we don't yet have code to use that mechanism; I think there's a better
136 * way on Linux, for example, but if that better way is "getifaddrs()",
137 * we already have that.
140 pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
142 pcap_if_t *devlist = NULL;
143 register int fd;
144 register struct ifreq *ifrp, *ifend, *ifnext;
145 int n;
146 struct ifconf ifc;
147 char *buf = NULL;
148 unsigned buf_size;
149 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
150 char *p, *q;
151 #endif
152 struct ifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
153 struct sockaddr *netmask, *broadaddr, *dstaddr;
154 size_t netmask_size, broadaddr_size, dstaddr_size;
155 int ret = 0;
158 * Create a socket from which to fetch the list of interfaces.
160 fd = socket(AF_INET, SOCK_DGRAM, 0);
161 if (fd < 0) {
162 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
163 "socket: %s", pcap_strerror(errno));
164 return (-1);
168 * Start with an 8K buffer, and keep growing the buffer until
169 * we have more than "sizeof(ifrp->ifr_name) + MAX_SA_LEN"
170 * bytes left over in the buffer or we fail to get the
171 * interface list for some reason other than EINVAL (which is
172 * presumed here to mean "buffer is too small").
174 buf_size = 8192;
175 for (;;) {
176 buf = malloc(buf_size);
177 if (buf == NULL) {
178 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
179 "malloc: %s", pcap_strerror(errno));
180 (void)close(fd);
181 return (-1);
184 ifc.ifc_len = buf_size;
185 ifc.ifc_buf = buf;
186 memset(buf, 0, buf_size);
187 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0
188 && errno != EINVAL) {
189 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
190 "SIOCGIFCONF: %s", pcap_strerror(errno));
191 (void)close(fd);
192 free(buf);
193 return (-1);
195 if (ifc.ifc_len < buf_size &&
196 (buf_size - ifc.ifc_len) > sizeof(ifrp->ifr_name) + MAX_SA_LEN)
197 break;
198 free(buf);
199 buf_size *= 2;
202 ifrp = (struct ifreq *)buf;
203 ifend = (struct ifreq *)(buf + ifc.ifc_len);
205 for (; ifrp < ifend; ifrp = ifnext) {
207 * XXX - what if this isn't an IPv4 address? Can
208 * we still get the netmask, etc. with ioctls on
209 * an IPv4 socket?
211 * The answer is probably platform-dependent, and
212 * if the answer is "no" on more than one platform,
213 * the way you work around it is probably platform-
214 * dependent as well.
216 n = SA_LEN(&ifrp->ifr_addr) + sizeof(ifrp->ifr_name);
217 if (n < sizeof(*ifrp))
218 ifnext = ifrp + 1;
219 else
220 ifnext = (struct ifreq *)((char *)ifrp + n);
223 * XXX - The 32-bit compatibility layer for Linux on IA-64
224 * is slightly broken. It correctly converts the structures
225 * to and from kernel land from 64 bit to 32 bit but
226 * doesn't update ifc.ifc_len, leaving it larger than the
227 * amount really used. This means we read off the end
228 * of the buffer and encounter an interface with an
229 * "empty" name. Since this is highly unlikely to ever
230 * occur in a valid case we can just finish looking for
231 * interfaces if we see an empty name.
233 if (!(*ifrp->ifr_name))
234 break;
237 * Skip entries that begin with "dummy".
238 * XXX - what are these? Is this Linux-specific?
239 * Are there platforms on which we shouldn't do this?
241 if (strncmp(ifrp->ifr_name, "dummy", 5) == 0)
242 continue;
245 * Get the flags for this interface.
247 strncpy(ifrflags.ifr_name, ifrp->ifr_name,
248 sizeof(ifrflags.ifr_name));
249 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
250 if (errno == ENXIO)
251 continue;
252 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
253 "SIOCGIFFLAGS: %.*s: %s",
254 (int)sizeof(ifrflags.ifr_name),
255 ifrflags.ifr_name,
256 pcap_strerror(errno));
257 ret = -1;
258 break;
262 * Get the netmask for this address on this interface.
264 strncpy(ifrnetmask.ifr_name, ifrp->ifr_name,
265 sizeof(ifrnetmask.ifr_name));
266 memcpy(&ifrnetmask.ifr_addr, &ifrp->ifr_addr,
267 sizeof(ifrnetmask.ifr_addr));
268 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifrnetmask) < 0) {
269 if (errno == EADDRNOTAVAIL) {
271 * Not available.
273 netmask = NULL;
274 netmask_size = 0;
275 } else {
276 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
277 "SIOCGIFNETMASK: %.*s: %s",
278 (int)sizeof(ifrnetmask.ifr_name),
279 ifrnetmask.ifr_name,
280 pcap_strerror(errno));
281 ret = -1;
282 break;
284 } else {
285 netmask = &ifrnetmask.ifr_addr;
286 netmask_size = SA_LEN(netmask);
290 * Get the broadcast address for this address on this
291 * interface (if any).
293 if (ifrflags.ifr_flags & IFF_BROADCAST) {
294 strncpy(ifrbroadaddr.ifr_name, ifrp->ifr_name,
295 sizeof(ifrbroadaddr.ifr_name));
296 memcpy(&ifrbroadaddr.ifr_addr, &ifrp->ifr_addr,
297 sizeof(ifrbroadaddr.ifr_addr));
298 if (ioctl(fd, SIOCGIFBRDADDR,
299 (char *)&ifrbroadaddr) < 0) {
300 if (errno == EADDRNOTAVAIL) {
302 * Not available.
304 broadaddr = NULL;
305 broadaddr_size = 0;
306 } else {
307 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
308 "SIOCGIFBRDADDR: %.*s: %s",
309 (int)sizeof(ifrbroadaddr.ifr_name),
310 ifrbroadaddr.ifr_name,
311 pcap_strerror(errno));
312 ret = -1;
313 break;
315 } else {
316 broadaddr = &ifrbroadaddr.ifr_broadaddr;
317 broadaddr_size = SA_LEN(broadaddr);
319 } else {
321 * Not a broadcast interface, so no broadcast
322 * address.
324 broadaddr = NULL;
325 broadaddr_size = 0;
329 * Get the destination address for this address on this
330 * interface (if any).
332 if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
333 strncpy(ifrdstaddr.ifr_name, ifrp->ifr_name,
334 sizeof(ifrdstaddr.ifr_name));
335 memcpy(&ifrdstaddr.ifr_addr, &ifrp->ifr_addr,
336 sizeof(ifrdstaddr.ifr_addr));
337 if (ioctl(fd, SIOCGIFDSTADDR,
338 (char *)&ifrdstaddr) < 0) {
339 if (errno == EADDRNOTAVAIL) {
341 * Not available.
343 dstaddr = NULL;
344 dstaddr_size = 0;
345 } else {
346 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
347 "SIOCGIFDSTADDR: %.*s: %s",
348 (int)sizeof(ifrdstaddr.ifr_name),
349 ifrdstaddr.ifr_name,
350 pcap_strerror(errno));
351 ret = -1;
352 break;
354 } else {
355 dstaddr = &ifrdstaddr.ifr_dstaddr;
356 dstaddr_size = SA_LEN(dstaddr);
358 } else {
360 * Not a point-to-point interface, so no destination
361 * address.
363 dstaddr = NULL;
364 dstaddr_size = 0;
367 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
369 * If this entry has a colon followed by a number at
370 * the end, it's a logical interface. Those are just
371 * the way you assign multiple IP addresses to a real
372 * interface, so an entry for a logical interface should
373 * be treated like the entry for the real interface;
374 * we do that by stripping off the ":" and the number.
376 p = strchr(ifrp->ifr_name, ':');
377 if (p != NULL) {
379 * We have a ":"; is it followed by a number?
381 q = p + 1;
382 while (isdigit((unsigned char)*q))
383 q++;
384 if (*q == '\0') {
386 * All digits after the ":" until the end.
387 * Strip off the ":" and everything after
388 * it.
390 *p = '\0';
393 #endif
396 * Add information for this address to the list.
398 if (add_addr_to_iflist(&devlist, ifrp->ifr_name,
399 ifrflags.ifr_flags, &ifrp->ifr_addr,
400 SA_LEN(&ifrp->ifr_addr), netmask, netmask_size,
401 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
402 errbuf) < 0) {
403 ret = -1;
404 break;
407 free(buf);
408 (void)close(fd);
410 if (ret == -1) {
412 * We had an error; free the list we've been constructing.
414 if (devlist != NULL) {
415 pcap_freealldevs(devlist);
416 devlist = NULL;
420 *alldevsp = devlist;
421 return (ret);