]>
The Tcpdump Group git mirrors - libpcap/blob - inet.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
36 static const char rcsid
[] =
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.41 2001-10-09 05:43:20 guy Exp $ (LBL)";
44 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #ifdef HAVE_SYS_SOCKIO_H
49 #include <sys/sockio.h>
51 #include <sys/time.h> /* concession to AIX */
56 #include <netinet/in.h>
68 #define INT_MAX 2147483647
76 #ifdef HAVE_OS_PROTO_H
80 /* Not all systems have IFF_LOOPBACK */
82 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
84 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
85 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
91 * In older BSD systems, socket addresses were fixed-length, and
92 * "sizeof (struct sockaddr)" gave the size of the structure.
93 * All addresses fit within a "struct sockaddr".
95 * In newer BSD systems, the socket address is variable-length, and
96 * there's an "sa_len" field giving the length of the structure;
97 * this allows socket addresses to be longer than 2 bytes of family
98 * and 14 bytes of data.
100 * Some commercial UNIXes use the old BSD scheme, and some might use
101 * the new BSD scheme.
103 * GNU libc uses neither scheme, but has an "SA_LEN()" macro that
104 * determines the size based on the address family.
107 #ifdef HAVE_SOCKADDR_SA_LEN
108 #define SA_LEN(addr) ((addr)->sa_len)
109 #else /* HAVE_SOCKADDR_SA_LEN */
110 #define SA_LEN(addr) (sizeof (struct sockaddr))
111 #endif /* HAVE_SOCKADDR_SA_LEN */
114 static struct sockaddr
*
115 dup_sockaddr(struct sockaddr
*sa
)
117 struct sockaddr
*newsa
;
121 if ((newsa
= malloc(size
)) == NULL
)
123 return (memcpy(newsa
, sa
, size
));
127 get_instance(char *name
)
132 if (strcmp(name
, "any") == 0) {
134 * Give the "any" device an artificially high instance
135 * number, so it shows up after all other non-loopback
141 endcp
= name
+ strlen(name
);
142 for (cp
= name
; cp
< endcp
&& !isdigit((unsigned char)*cp
); ++cp
)
145 if (isdigit((unsigned char)*cp
))
153 add_or_find_if(pcap_if_t
**curdev_ret
, pcap_if_t
**alldevs
, char *name
,
154 u_int flags
, char *errbuf
)
157 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
161 * Can we open this interface for live capture?
163 p
= pcap_open_live(name
, 68, 0, 0, errbuf
);
166 * No. Don't bother including it.
167 * Don't treat this as an error, though.
175 * Is there already an entry in the list for this interface?
177 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
178 if (strcmp(name
, curdev
->name
) == 0)
179 break; /* yes, we found it */
181 if (curdev
== NULL
) {
183 * No, we didn't find it.
184 * Allocate a new entry.
186 curdev
= malloc(sizeof(pcap_if_t
));
187 if (curdev
== NULL
) {
188 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
189 "malloc: %s", pcap_strerror(errno
));
197 curdev
->name
= malloc(strlen(name
) + 1);
198 strcpy(curdev
->name
, name
);
199 curdev
->description
= NULL
; /* not available */
200 curdev
->addresses
= NULL
; /* list starts out as empty */
201 curdev
->is_loopback
= ISLOOPBACK(name
, flags
);
204 * Add it to the list, in the appropriate location.
205 * First, get the instance number of this interface.
207 this_instance
= get_instance(name
);
210 * Now look for the last interface with an instance number
211 * less than or equal to the new interface's instance
212 * number - except that non-loopback interfaces are
213 * arbitrarily treated as having interface numbers less
214 * than those of loopback interfaces, so the loopback
215 * interfaces are put at the end of the list.
217 * We start with "prevdev" being NULL, meaning we're before
218 * the first element in the list.
223 * Get the interface after this one.
225 if (prevdev
== NULL
) {
227 * The next element is the first element.
231 nextdev
= prevdev
->next
;
234 * Are we at the end of the list?
236 if (nextdev
== NULL
) {
238 * Yes - we have to put the new entry
245 * Is the new interface a non-loopback interface
246 * and the next interface a loopback interface?
248 if (!curdev
->is_loopback
&& nextdev
->is_loopback
) {
250 * Yes, we should put the new entry
251 * before "nextdev", i.e. after "prevdev".
257 * Is the new interface's instance number less
258 * than the next interface's instance number,
259 * and is it the case that the new interface is a
260 * non-loopback interface or the next interface is
261 * a loopback interface?
263 * (The goal of both loopback tests is to make
264 * sure that we never put a loopback interface
265 * before any non-loopback interface and that we
266 * always put a non-loopback interface before all
267 * loopback interfaces.)
269 if (this_instance
< get_instance(nextdev
->name
) &&
270 (!curdev
->is_loopback
|| nextdev
->is_loopback
)) {
272 * Yes - we should put the new entry
273 * before "nextdev", i.e. after "prevdev".
282 * Insert before "nextdev".
284 curdev
->next
= nextdev
;
287 * Insert after "prevdev" - unless "prevdev" is null,
288 * in which case this is the first interface.
290 if (prevdev
== NULL
) {
292 * This is the first interface. Pass back a
293 * pointer to it, and put "curdev" before
298 prevdev
->next
= curdev
;
301 *curdev_ret
= curdev
;
306 add_addr_to_iflist(pcap_if_t
**alldevs
, char *name
, u_int flags
,
307 struct sockaddr
*addr
, struct sockaddr
*netmask
,
308 struct sockaddr
*broadaddr
, struct sockaddr
*dstaddr
, char *errbuf
)
311 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
313 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, errbuf
) == -1) {
319 if (curdev
== NULL
) {
321 * Device wasn't added because it can't be opened.
328 * "curdev" is an entry for this interface; add an entry for this
329 * address to its list of addresses.
331 * Allocate the new entry and fill it in.
333 curaddr
= malloc(sizeof(pcap_addr_t
));
334 if (curaddr
== NULL
) {
335 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
336 "malloc: %s", pcap_strerror(errno
));
340 curaddr
->next
= NULL
;
342 curaddr
->addr
= dup_sockaddr(addr
);
343 if (curaddr
->addr
== NULL
) {
344 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
345 "malloc: %s", pcap_strerror(errno
));
350 curaddr
->addr
= NULL
;
352 if (netmask
!= NULL
) {
353 curaddr
->netmask
= dup_sockaddr(netmask
);
354 if (curaddr
->netmask
== NULL
) {
355 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
356 "malloc: %s", pcap_strerror(errno
));
361 curaddr
->netmask
= NULL
;
363 if (broadaddr
!= NULL
) {
364 curaddr
->broadaddr
= dup_sockaddr(broadaddr
);
365 if (curaddr
->broadaddr
== NULL
) {
366 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
367 "malloc: %s", pcap_strerror(errno
));
372 curaddr
->broadaddr
= NULL
;
374 if (dstaddr
!= NULL
) {
375 curaddr
->dstaddr
= dup_sockaddr(dstaddr
);
376 if (curaddr
->dstaddr
== NULL
) {
377 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
378 "malloc: %s", pcap_strerror(errno
));
383 curaddr
->dstaddr
= NULL
;
386 * Find the end of the list of addresses.
388 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
389 nextaddr
= prevaddr
->next
;
390 if (nextaddr
== NULL
) {
392 * This is the end of the list.
398 if (prevaddr
== NULL
) {
400 * The list was empty; this is the first member.
402 curdev
->addresses
= curaddr
;
405 * "prevaddr" is the last member of the list; append
408 prevaddr
->next
= curaddr
;
415 pcap_add_if(pcap_if_t
**devlist
, char *name
, u_int flags
, char *errbuf
)
419 return (add_or_find_if(&curdev
, devlist
, name
, flags
, errbuf
));
423 * Get a list of all interfaces that are up and that we can open.
424 * Returns -1 on error, 0 otherwise.
425 * The list, as returned through "alldevsp", may be null if no interfaces
426 * were up and could be opened.
428 #ifdef HAVE_IFADDRS_H
430 pcap_findalldevs(alldevsp
, errbuf
)
431 pcap_if_t
**alldevsp
;
432 register char *errbuf
;
434 pcap_if_t
*devlist
= NULL
;
435 struct ifaddrs
*ifap
, *ifa
;
436 struct sockaddr
*broadaddr
, *dstaddr
;
440 * Get the list of interface addresses.
442 * Note: this won't return information about interfaces
443 * with no addresses; are there any such interfaces
444 * that would be capable of receiving packets?
445 * (Interfaces incapable of receiving packets aren't
446 * very interesting from libpcap's point of view.)
448 * LAN interfaces will probably have link-layer
449 * addresses; I don't know whether all implementations
450 * of "getifaddrs()" now, or in the future, will return
453 if (getifaddrs(&ifap
) != 0) {
454 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
455 "getifaddrs: %s", pcap_strerror(errno
));
458 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
460 * Is this interface up?
462 if (!(ifa
->ifa_flags
& IFF_UP
)) {
464 * No, so don't add it to the list.
470 * "ifa_broadaddr" may be non-null even on
471 * non-broadcast interfaces; "ifa_dstaddr"
472 * was, on at least one FreeBSD 4.1 system,
473 * non-null on a non-point-to-point
476 if (ifa
->ifa_flags
& IFF_BROADCAST
)
477 broadaddr
= ifa
->ifa_broadaddr
;
480 if (ifa
->ifa_flags
& IFF_POINTOPOINT
)
481 dstaddr
= ifa
->ifa_dstaddr
;
486 * Add information for this address to the list.
488 if (add_addr_to_iflist(&devlist
, ifa
->ifa_name
,
489 ifa
->ifa_flags
, ifa
->ifa_addr
, ifa
->ifa_netmask
,
490 broadaddr
, dstaddr
, errbuf
) < 0) {
500 * We haven't had any errors yet; add the "any" device,
503 if (pcap_add_if(&devlist
, "any", 0, errbuf
) < 0)
509 * We had an error; free the list we've been constructing.
511 if (devlist
!= NULL
) {
512 pcap_freealldevs(devlist
);
520 #else /* HAVE_IFADDRS_H */
522 pcap_findalldevs(alldevsp
, errbuf
)
523 pcap_if_t
**alldevsp
;
524 register char *errbuf
;
526 pcap_if_t
*devlist
= NULL
;
528 register struct ifreq
*ifrp
, *ifend
, *ifnext
;
533 struct ifreq ifrflags
, ifrnetmask
, ifrbroadaddr
, ifrdstaddr
;
534 struct sockaddr
*netmask
, *broadaddr
, *dstaddr
;
535 #ifdef HAVE_PROC_NET_DEV
540 char name
[512]; /* XXX - pick a size */
546 * Create a socket from which to fetch the list of interfaces.
548 * XXX - on Linux, SIOCGIFCONF reports only interfaces with
549 * IPv4 addresses; you need to read "/proc/net/dev" to get
550 * the names of all the interfaces.
552 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
554 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
555 "socket: %s", pcap_strerror(errno
));
560 * Start with an 8K buffer, and keep growing the buffer until
561 * we get the entire interface list or fail to get it for some
562 * reason other than EINVAL (which is presumed here to mean
563 * "buffer is too small").
567 buf
= malloc(buf_size
);
569 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
570 "malloc: %s", pcap_strerror(errno
));
575 ifc
.ifc_len
= buf_size
;
577 memset(buf
, 0, buf_size
);
578 if (ioctl(fd
, SIOCGIFCONF
, (char *)&ifc
) < 0
579 && errno
!= EINVAL
) {
580 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
581 "SIOCGIFCONF: %s", pcap_strerror(errno
));
586 if (ifc
.ifc_len
< buf_size
)
592 ifrp
= (struct ifreq
*)buf
;
593 ifend
= (struct ifreq
*)(buf
+ ifc
.ifc_len
);
595 for (; ifrp
< ifend
; ifrp
= ifnext
) {
596 n
= SA_LEN(&ifrp
->ifr_addr
) + sizeof(ifrp
->ifr_name
);
597 if (n
< sizeof(*ifrp
))
600 ifnext
= (struct ifreq
*)((char *)ifrp
+ n
);
603 * Get the flags for this interface, and skip it if it's
606 strncpy(ifrflags
.ifr_name
, ifrp
->ifr_name
,
607 sizeof(ifrflags
.ifr_name
));
608 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifrflags
) < 0) {
611 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
612 "SIOCGIFFLAGS: %.*s: %s",
613 (int)sizeof(ifrflags
.ifr_name
),
615 pcap_strerror(errno
));
619 if (!(ifrflags
.ifr_flags
& IFF_UP
))
623 * Get the netmask for this address on this interface.
625 strncpy(ifrnetmask
.ifr_name
, ifrp
->ifr_name
,
626 sizeof(ifrnetmask
.ifr_name
));
627 memcpy(&ifrnetmask
.ifr_addr
, &ifrp
->ifr_addr
,
628 sizeof(ifrnetmask
.ifr_addr
));
629 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifrnetmask
) < 0) {
630 if (errno
== EADDRNOTAVAIL
) {
636 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
637 "SIOCGIFNETMASK: %.*s: %s",
638 (int)sizeof(ifrnetmask
.ifr_name
),
640 pcap_strerror(errno
));
645 netmask
= &ifrnetmask
.ifr_addr
;
648 * Get the broadcast address for this address on this
649 * interface (if any).
651 if (ifrflags
.ifr_flags
& IFF_BROADCAST
) {
652 strncpy(ifrbroadaddr
.ifr_name
, ifrp
->ifr_name
,
653 sizeof(ifrbroadaddr
.ifr_name
));
654 memcpy(&ifrbroadaddr
.ifr_addr
, &ifrp
->ifr_addr
,
655 sizeof(ifrbroadaddr
.ifr_addr
));
656 if (ioctl(fd
, SIOCGIFBRDADDR
,
657 (char *)&ifrbroadaddr
) < 0) {
658 if (errno
== EADDRNOTAVAIL
) {
664 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
665 "SIOCGIFBRDADDR: %.*s: %s",
666 (int)sizeof(ifrbroadaddr
.ifr_name
),
667 ifrbroadaddr
.ifr_name
,
668 pcap_strerror(errno
));
673 broadaddr
= &ifrbroadaddr
.ifr_broadaddr
;
676 * Not a broadcast interface, so no broadcast
683 * Get the destination address for this address on this
684 * interface (if any).
686 if (ifrflags
.ifr_flags
& IFF_POINTOPOINT
) {
687 strncpy(ifrdstaddr
.ifr_name
, ifrp
->ifr_name
,
688 sizeof(ifrdstaddr
.ifr_name
));
689 memcpy(&ifrdstaddr
.ifr_addr
, &ifrp
->ifr_addr
,
690 sizeof(ifrdstaddr
.ifr_addr
));
691 if (ioctl(fd
, SIOCGIFDSTADDR
,
692 (char *)&ifrdstaddr
) < 0) {
693 if (errno
== EADDRNOTAVAIL
) {
699 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
700 "SIOCGIFDSTADDR: %.*s: %s",
701 (int)sizeof(ifrdstaddr
.ifr_name
),
703 pcap_strerror(errno
));
708 dstaddr
= &ifrdstaddr
.ifr_dstaddr
;
713 * Add information for this address to the list.
715 if (add_addr_to_iflist(&devlist
, ifrp
->ifr_name
,
716 ifrflags
.ifr_flags
, &ifrp
->ifr_addr
,
717 netmask
, broadaddr
, dstaddr
, errbuf
) < 0) {
724 #ifdef HAVE_PROC_NET_DEV
726 * OK, now read "/proc/net/dev", and add to the list of
727 * interfaces all interfaces listed there that we don't
730 * We don't bother getting any addresses for them;
731 * it appears you can't use SIOCGIFADDR to get
732 * IPv6 addresses for interfaces, and, although some
733 * other types of addresses can be fetched with
734 * SIOCGIFADDR, we don't bother with them for now.
736 * We also don't fail if we couldn't open "/proc/net/dev";
737 * we just return the interfaces we've already found.
739 proc_net_f
= fopen("/proc/net/dev", "r");
740 if (proc_net_f
!= NULL
) {
742 * Skip the first two lines - they're headers.
744 for (i
= 0; i
< 2; i
++) {
745 if (fgets(linebuf
, sizeof linebuf
, proc_net_f
) == NULL
) {
746 if (ferror(proc_net_f
)) {
747 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
748 "Error reading /proc/net/dev: %s",
749 pcap_strerror(errno
));
751 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
752 "EOF reading header line %d of /proc/net/dev: %s",
753 i
+ 1, pcap_strerror(errno
));
761 * Now read the rest of the lines.
764 fgets(linebuf
, sizeof linebuf
, proc_net_f
) != NULL
) {
768 * Skip leading white space.
770 while (*p
!= '\0' && isspace(*p
))
772 if (*p
== '\0' || *p
== '\n')
773 continue; /* blank line */
776 * Get the interface name.
779 while (*p
!= '\0' && !isspace(*p
)) {
782 * This could be the separator
783 * between a name and an alias
784 * number, or it could be the
785 * separator between a name with
786 * no alias number and the next
788 * If there's a colon after digits,
789 * it separates the name and the alias
790 * number, otherwise it separates the
791 * name and the next field.
798 * That was the next field,
799 * not the alias number.
810 * Get the flags for this interface, and skip it if
813 strncpy(ifrflags
.ifr_name
, name
,
814 sizeof(ifrflags
.ifr_name
));
815 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifrflags
) < 0) {
818 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
819 "SIOCGIFFLAGS: %.*s: %s",
820 (int)sizeof(ifrflags
.ifr_name
),
822 pcap_strerror(errno
));
826 if (!(ifrflags
.ifr_flags
& IFF_UP
))
830 * Add an entry for this interface, with no addresses.
832 if (pcap_add_if(&devlist
, name
, ifrflags
.ifr_flags
,
841 (void)fclose(proc_net_f
);
849 * We haven't had any errors yet; add the "any" device,
852 if (pcap_add_if(&devlist
, "any", 0, errbuf
) < 0) {
854 * Oops, we had a fatal error.
862 * We had an error; free the list we've been constructing.
864 if (devlist
!= NULL
) {
865 pcap_freealldevs(devlist
);
873 #endif /* HAVE_IFADDRS_H */
876 * Free a list of interfaces.
879 pcap_freealldevs(pcap_if_t
*alldevs
)
881 pcap_if_t
*curdev
, *nextdev
;
882 pcap_addr_t
*curaddr
, *nextaddr
;
884 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
885 nextdev
= curdev
->next
;
888 * Free all addresses.
890 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
891 nextaddr
= curaddr
->next
;
894 if (curaddr
->netmask
)
895 free(curaddr
->netmask
);
896 if (curaddr
->broadaddr
)
897 free(curaddr
->broadaddr
);
898 if (curaddr
->dstaddr
)
899 free(curaddr
->dstaddr
);
904 * Free the name string.
909 * Free the description string, if any.
911 if (curdev
->description
!= NULL
)
912 free(curdev
->description
);
915 * Free the interface.
922 * Return the name of a network interface attached to the system, or NULL
923 * if none can be found. The interface must be configured up; the
924 * lowest unit number is preferred; loopback is ignored.
927 pcap_lookupdev(errbuf
)
928 register char *errbuf
;
931 /* for old BSD systems, including bsdi3 */
933 #define IF_NAMESIZE IFNAMSIZ
935 static char device
[IF_NAMESIZE
+ 1];
938 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
941 if (alldevs
== NULL
|| alldevs
->is_loopback
) {
943 * There are no devices on the list, or the first device
944 * on the list is a loopback device, which means there
945 * are no non-loopback devices on the list. This means
946 * we can't return any device.
948 * XXX - why not return a loopback device? If we can't
949 * capture on it, it won't be on the list, and if it's
950 * on the list, there aren't any non-loopback devices,
951 * so why not just supply it as the default device?
953 (void)strlcpy(errbuf
, "no suitable device found",
958 * Return the name of the first device on the list.
960 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
964 pcap_freealldevs(alldevs
);
969 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
970 register char *device
;
971 register bpf_u_int32
*netp
, *maskp
;
972 register char *errbuf
;
975 register struct sockaddr_in
*sin
;
979 * The pseudo-device "any" listens on all interfaces and therefore
980 * has the network address and -mask "0.0.0.0" therefore catching
981 * all traffic. Using NULL for the interface is the same as "any".
983 if (!device
|| strcmp(device
, "any") == 0) {
988 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
990 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
991 pcap_strerror(errno
));
994 memset(&ifr
, 0, sizeof(ifr
));
996 /* XXX Work around Linux kernel bug */
997 ifr
.ifr_addr
.sa_family
= AF_INET
;
999 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
1000 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
1001 if (errno
== EADDRNOTAVAIL
) {
1002 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1003 "%s: no IPv4 address assigned", device
);
1005 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1006 "SIOCGIFADDR: %s: %s",
1007 device
, pcap_strerror(errno
));
1012 sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
1013 *netp
= sin
->sin_addr
.s_addr
;
1014 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
1015 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1016 "SIOCGIFNETMASK: %s: %s", device
, pcap_strerror(errno
));
1021 *maskp
= sin
->sin_addr
.s_addr
;
1023 if (IN_CLASSA(*netp
))
1024 *maskp
= IN_CLASSA_NET
;
1025 else if (IN_CLASSB(*netp
))
1026 *maskp
= IN_CLASSB_NET
;
1027 else if (IN_CLASSC(*netp
))
1028 *maskp
= IN_CLASSC_NET
;
1030 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1031 "inet class for 0x%x unknown", *netp
);