]>
The Tcpdump Group git mirrors - libpcap/blob - fad-gifc.c
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #ifdef HAVE_SYS_SOCKIO_H
43 #include <sys/sockio.h>
45 #include <sys/time.h> /* concession to AIX */
47 struct mbuf
; /* Squelch compiler warnings on some platforms for */
48 struct rtentry
; /* declarations in <net/if.h> */
50 #include <netinet/in.h>
62 #ifdef HAVE_OS_PROTO_H
69 * In older BSD systems, socket addresses were fixed-length, and
70 * "sizeof (struct sockaddr)" gave the size of the structure.
71 * All addresses fit within a "struct sockaddr".
73 * In newer BSD systems, the socket address is variable-length, and
74 * there's an "sa_len" field giving the length of the structure;
75 * this allows socket addresses to be longer than 2 bytes of family
76 * and 14 bytes of data.
78 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
79 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
80 * than "struct sockaddr"), and some use the new BSD scheme.
82 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
83 * macro that determines the size based on the address family. Other
84 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
85 * but not in the final version).
87 * We assume that a UNIX that doesn't have "getifaddrs()" and doesn't have
88 * SIOCGLIFCONF, but has SIOCGIFCONF, uses "struct sockaddr" for the
89 * address in an entry returned by SIOCGIFCONF.
92 #ifdef HAVE_SOCKADDR_SA_LEN
93 #define SA_LEN(addr) ((addr)->sa_len)
94 #else /* HAVE_SOCKADDR_SA_LEN */
95 #define SA_LEN(addr) (sizeof (struct sockaddr))
96 #endif /* HAVE_SOCKADDR_SA_LEN */
102 * There is no ioctl that returns the amount of space required for all
103 * the data that SIOCGIFCONF could return, and if a buffer is supplied
104 * that's not large enough for all the data SIOCGIFCONF could return,
105 * on at least some platforms it just returns the data that'd fit with
106 * no indication that there wasn't enough room for all the data, much
107 * less an indication of how much more room is required.
109 * The only way to ensure that we got all the data is to pass a buffer
110 * large enough that the amount of space in the buffer *not* filled in
111 * is greater than the largest possible entry.
113 * We assume that's "sizeof(ifreq.ifr_name)" plus 255, under the assumption
114 * that no address is more than 255 bytes (on systems where the "sa_len"
115 * field in a "struct sockaddr" is 1 byte, e.g. newer BSDs, that's the
116 * case, and addresses are unlikely to be bigger than that in any case).
118 #define MAX_SA_LEN 255
121 * Get a list of all interfaces that are up and that we can open.
122 * Returns -1 on error, 0 otherwise.
123 * The list, as returned through "alldevsp", may be null if no interfaces
124 * were up and could be opened.
126 * This is the implementation used on platforms that have SIOCGIFCONF but
127 * don't have any other mechanism for getting a list of interfaces.
129 * XXX - or platforms that have other, better mechanisms but for which
130 * we don't yet have code to use that mechanism; I think there's a better
131 * way on Linux, for example, but if that better way is "getifaddrs()",
132 * we already have that.
135 pcap_findalldevs_interfaces(pcap_if_t
**alldevsp
, char *errbuf
)
137 pcap_if_t
*devlist
= NULL
;
139 register struct ifreq
*ifrp
, *ifend
, *ifnext
;
144 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
147 struct ifreq ifrflags
, ifrnetmask
, ifrbroadaddr
, ifrdstaddr
;
148 struct sockaddr
*netmask
, *broadaddr
, *dstaddr
;
149 size_t netmask_size
, broadaddr_size
, dstaddr_size
;
153 * Create a socket from which to fetch the list of interfaces.
155 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
157 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
158 "socket: %s", pcap_strerror(errno
));
163 * Start with an 8K buffer, and keep growing the buffer until
164 * we have more than "sizeof(ifrp->ifr_name) + MAX_SA_LEN"
165 * bytes left over in the buffer or we fail to get the
166 * interface list for some reason other than EINVAL (which is
167 * presumed here to mean "buffer is too small").
171 buf
= malloc(buf_size
);
173 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
174 "malloc: %s", pcap_strerror(errno
));
179 ifc
.ifc_len
= buf_size
;
181 memset(buf
, 0, buf_size
);
182 if (ioctl(fd
, SIOCGIFCONF
, (char *)&ifc
) < 0
183 && errno
!= EINVAL
) {
184 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
185 "SIOCGIFCONF: %s", pcap_strerror(errno
));
190 if (ifc
.ifc_len
< buf_size
&&
191 (buf_size
- ifc
.ifc_len
) > sizeof(ifrp
->ifr_name
) + MAX_SA_LEN
)
197 ifrp
= (struct ifreq
*)buf
;
198 ifend
= (struct ifreq
*)(buf
+ ifc
.ifc_len
);
200 for (; ifrp
< ifend
; ifrp
= ifnext
) {
202 * XXX - what if this isn't an IPv4 address? Can
203 * we still get the netmask, etc. with ioctls on
206 * The answer is probably platform-dependent, and
207 * if the answer is "no" on more than one platform,
208 * the way you work around it is probably platform-
211 n
= SA_LEN(&ifrp
->ifr_addr
) + sizeof(ifrp
->ifr_name
);
212 if (n
< sizeof(*ifrp
))
215 ifnext
= (struct ifreq
*)((char *)ifrp
+ n
);
218 * XXX - The 32-bit compatibility layer for Linux on IA-64
219 * is slightly broken. It correctly converts the structures
220 * to and from kernel land from 64 bit to 32 bit but
221 * doesn't update ifc.ifc_len, leaving it larger than the
222 * amount really used. This means we read off the end
223 * of the buffer and encounter an interface with an
224 * "empty" name. Since this is highly unlikely to ever
225 * occur in a valid case we can just finish looking for
226 * interfaces if we see an empty name.
228 if (!(*ifrp
->ifr_name
))
232 * Skip entries that begin with "dummy".
233 * XXX - what are these? Is this Linux-specific?
234 * Are there platforms on which we shouldn't do this?
236 if (strncmp(ifrp
->ifr_name
, "dummy", 5) == 0)
240 * Get the flags for this interface.
242 strncpy(ifrflags
.ifr_name
, ifrp
->ifr_name
,
243 sizeof(ifrflags
.ifr_name
));
244 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifrflags
) < 0) {
247 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
248 "SIOCGIFFLAGS: %.*s: %s",
249 (int)sizeof(ifrflags
.ifr_name
),
251 pcap_strerror(errno
));
257 * Get the netmask for this address on this interface.
259 strncpy(ifrnetmask
.ifr_name
, ifrp
->ifr_name
,
260 sizeof(ifrnetmask
.ifr_name
));
261 memcpy(&ifrnetmask
.ifr_addr
, &ifrp
->ifr_addr
,
262 sizeof(ifrnetmask
.ifr_addr
));
263 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifrnetmask
) < 0) {
264 if (errno
== EADDRNOTAVAIL
) {
271 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
272 "SIOCGIFNETMASK: %.*s: %s",
273 (int)sizeof(ifrnetmask
.ifr_name
),
275 pcap_strerror(errno
));
280 netmask
= &ifrnetmask
.ifr_addr
;
281 netmask_size
= SA_LEN(netmask
);
285 * Get the broadcast address for this address on this
286 * interface (if any).
288 if (ifrflags
.ifr_flags
& IFF_BROADCAST
) {
289 strncpy(ifrbroadaddr
.ifr_name
, ifrp
->ifr_name
,
290 sizeof(ifrbroadaddr
.ifr_name
));
291 memcpy(&ifrbroadaddr
.ifr_addr
, &ifrp
->ifr_addr
,
292 sizeof(ifrbroadaddr
.ifr_addr
));
293 if (ioctl(fd
, SIOCGIFBRDADDR
,
294 (char *)&ifrbroadaddr
) < 0) {
295 if (errno
== EADDRNOTAVAIL
) {
302 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
303 "SIOCGIFBRDADDR: %.*s: %s",
304 (int)sizeof(ifrbroadaddr
.ifr_name
),
305 ifrbroadaddr
.ifr_name
,
306 pcap_strerror(errno
));
311 broadaddr
= &ifrbroadaddr
.ifr_broadaddr
;
312 broadaddr_size
= SA_LEN(broadaddr
);
316 * Not a broadcast interface, so no broadcast
324 * Get the destination address for this address on this
325 * interface (if any).
327 if (ifrflags
.ifr_flags
& IFF_POINTOPOINT
) {
328 strncpy(ifrdstaddr
.ifr_name
, ifrp
->ifr_name
,
329 sizeof(ifrdstaddr
.ifr_name
));
330 memcpy(&ifrdstaddr
.ifr_addr
, &ifrp
->ifr_addr
,
331 sizeof(ifrdstaddr
.ifr_addr
));
332 if (ioctl(fd
, SIOCGIFDSTADDR
,
333 (char *)&ifrdstaddr
) < 0) {
334 if (errno
== EADDRNOTAVAIL
) {
341 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
342 "SIOCGIFDSTADDR: %.*s: %s",
343 (int)sizeof(ifrdstaddr
.ifr_name
),
345 pcap_strerror(errno
));
350 dstaddr
= &ifrdstaddr
.ifr_dstaddr
;
351 dstaddr_size
= SA_LEN(dstaddr
);
355 * Not a point-to-point interface, so no destination
362 #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
364 * If this entry has a colon followed by a number at
365 * the end, it's a logical interface. Those are just
366 * the way you assign multiple IP addresses to a real
367 * interface, so an entry for a logical interface should
368 * be treated like the entry for the real interface;
369 * we do that by stripping off the ":" and the number.
371 p
= strchr(ifrp
->ifr_name
, ':');
374 * We have a ":"; is it followed by a number?
377 while (isdigit((unsigned char)*q
))
381 * All digits after the ":" until the end.
382 * Strip off the ":" and everything after
391 * Add information for this address to the list.
393 if (add_addr_to_iflist(&devlist
, ifrp
->ifr_name
,
394 ifrflags
.ifr_flags
, &ifrp
->ifr_addr
,
395 SA_LEN(&ifrp
->ifr_addr
), netmask
, netmask_size
,
396 broadaddr
, broadaddr_size
, dstaddr
, dstaddr_size
,
407 * We had an error; free the list we've been constructing.
409 if (devlist
!= NULL
) {
410 pcap_freealldevs(devlist
);