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.45 2001/10/28 20:40:43 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 */
115 * Description string for the "any" device.
117 static const char any_descr[] = "Pseudo-device that captures on all interfaces";
119 static struct sockaddr *
120 dup_sockaddr(struct sockaddr *sa)
122 struct sockaddr *newsa;
126 if ((newsa = malloc(size)) == NULL)
128 return (memcpy(newsa, sa, size));
132 get_instance(char *name)
137 if (strcmp(name, "any") == 0) {
139 * Give the "any" device an artificially high instance
140 * number, so it shows up after all other non-loopback
146 endcp = name + strlen(name);
147 for (cp = name; cp < endcp && !isdigit((unsigned char)*cp); ++cp)
150 if (isdigit((unsigned char)*cp))
158 add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, char *name,
159 u_int flags, const char *description, char *errbuf)
162 pcap_if_t *curdev, *prevdev, *nextdev;
166 * Can we open this interface for live capture?
168 p = pcap_open_live(name, 68, 0, 0, errbuf);
171 * No. Don't bother including it.
172 * Don't treat this as an error, though.
180 * Is there already an entry in the list for this interface?
182 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
183 if (strcmp(name, curdev->name) == 0)
184 break; /* yes, we found it */
186 if (curdev == NULL) {
188 * No, we didn't find it.
189 * Allocate a new entry.
191 curdev = malloc(sizeof(pcap_if_t));
192 if (curdev == NULL) {
193 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
194 "malloc: %s", pcap_strerror(errno));
202 curdev->name = malloc(strlen(name) + 1);
203 strcpy(curdev->name, name);
204 if (description != NULL) {
206 * We have a description for this interface.
208 curdev->description = malloc(strlen(description) + 1);
209 strcpy(curdev->description, description);
214 curdev->description = NULL;
216 curdev->addresses = NULL; /* list starts out as empty */
218 if (ISLOOPBACK(name, flags))
219 curdev->flags |= PCAP_IF_LOOPBACK;
222 * Add it to the list, in the appropriate location.
223 * First, get the instance number of this interface.
225 this_instance = get_instance(name);
228 * Now look for the last interface with an instance number
229 * less than or equal to the new interface's instance
230 * number - except that non-loopback interfaces are
231 * arbitrarily treated as having interface numbers less
232 * than those of loopback interfaces, so the loopback
233 * interfaces are put at the end of the list.
235 * We start with "prevdev" being NULL, meaning we're before
236 * the first element in the list.
241 * Get the interface after this one.
243 if (prevdev == NULL) {
245 * The next element is the first element.
249 nextdev = prevdev->next;
252 * Are we at the end of the list?
254 if (nextdev == NULL) {
256 * Yes - we have to put the new entry
263 * Is the new interface a non-loopback interface
264 * and the next interface a loopback interface?
266 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
267 (nextdev->flags & PCAP_IF_LOOPBACK)) {
269 * Yes, we should put the new entry
270 * before "nextdev", i.e. after "prevdev".
276 * Is the new interface's instance number less
277 * than the next interface's instance number,
278 * and is it the case that the new interface is a
279 * non-loopback interface or the next interface is
280 * a loopback interface?
282 * (The goal of both loopback tests is to make
283 * sure that we never put a loopback interface
284 * before any non-loopback interface and that we
285 * always put a non-loopback interface before all
286 * loopback interfaces.)
288 if (this_instance < get_instance(nextdev->name) &&
289 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
290 (nextdev->flags & PCAP_IF_LOOPBACK))) {
292 * Yes - we should put the new entry
293 * before "nextdev", i.e. after "prevdev".
302 * Insert before "nextdev".
304 curdev->next = nextdev;
307 * Insert after "prevdev" - unless "prevdev" is null,
308 * in which case this is the first interface.
310 if (prevdev == NULL) {
312 * This is the first interface. Pass back a
313 * pointer to it, and put "curdev" before
318 prevdev->next = curdev;
321 *curdev_ret = curdev;
326 add_addr_to_iflist(pcap_if_t **alldevs, char *name, u_int flags,
327 struct sockaddr *addr, struct sockaddr *netmask,
328 struct sockaddr *broadaddr, struct sockaddr *dstaddr, char *errbuf)
331 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
333 if (add_or_find_if(&curdev, alldevs, name, flags, NULL, errbuf) == -1) {
339 if (curdev == NULL) {
341 * Device wasn't added because it can't be opened.
348 * "curdev" is an entry for this interface; add an entry for this
349 * address to its list of addresses.
351 * Allocate the new entry and fill it in.
353 curaddr = malloc(sizeof(pcap_addr_t));
354 if (curaddr == NULL) {
355 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
356 "malloc: %s", pcap_strerror(errno));
360 curaddr->next = NULL;
362 curaddr->addr = dup_sockaddr(addr);
363 if (curaddr->addr == NULL) {
364 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
365 "malloc: %s", pcap_strerror(errno));
370 curaddr->addr = NULL;
372 if (netmask != NULL) {
373 curaddr->netmask = dup_sockaddr(netmask);
374 if (curaddr->netmask == NULL) {
375 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
376 "malloc: %s", pcap_strerror(errno));
381 curaddr->netmask = NULL;
383 if (broadaddr != NULL) {
384 curaddr->broadaddr = dup_sockaddr(broadaddr);
385 if (curaddr->broadaddr == NULL) {
386 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
387 "malloc: %s", pcap_strerror(errno));
392 curaddr->broadaddr = NULL;
394 if (dstaddr != NULL) {
395 curaddr->dstaddr = dup_sockaddr(dstaddr);
396 if (curaddr->dstaddr == NULL) {
397 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
398 "malloc: %s", pcap_strerror(errno));
403 curaddr->dstaddr = NULL;
406 * Find the end of the list of addresses.
408 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
409 nextaddr = prevaddr->next;
410 if (nextaddr == NULL) {
412 * This is the end of the list.
418 if (prevaddr == NULL) {
420 * The list was empty; this is the first member.
422 curdev->addresses = curaddr;
425 * "prevaddr" is the last member of the list; append
428 prevaddr->next = curaddr;
435 pcap_add_if(pcap_if_t **devlist, char *name, u_int flags,
436 const char *description, char *errbuf)
440 return (add_or_find_if(&curdev, devlist, name, flags, description,
445 * Get a list of all interfaces that are up and that we can open.
446 * Returns -1 on error, 0 otherwise.
447 * The list, as returned through "alldevsp", may be null if no interfaces
448 * were up and could be opened.
450 #ifdef HAVE_IFADDRS_H
452 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
454 pcap_if_t *devlist = NULL;
455 struct ifaddrs *ifap, *ifa;
456 struct sockaddr *broadaddr, *dstaddr;
460 * Get the list of interface addresses.
462 * Note: this won't return information about interfaces
463 * with no addresses; are there any such interfaces
464 * that would be capable of receiving packets?
465 * (Interfaces incapable of receiving packets aren't
466 * very interesting from libpcap's point of view.)
468 * LAN interfaces will probably have link-layer
469 * addresses; I don't know whether all implementations
470 * of "getifaddrs()" now, or in the future, will return
473 if (getifaddrs(&ifap) != 0) {
474 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
475 "getifaddrs: %s", pcap_strerror(errno));
478 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
480 * Is this interface up?
482 if (!(ifa->ifa_flags & IFF_UP)) {
484 * No, so don't add it to the list.
490 * "ifa_broadaddr" may be non-null even on
491 * non-broadcast interfaces; "ifa_dstaddr"
492 * was, on at least one FreeBSD 4.1 system,
493 * non-null on a non-point-to-point
496 if (ifa->ifa_flags & IFF_BROADCAST)
497 broadaddr = ifa->ifa_broadaddr;
500 if (ifa->ifa_flags & IFF_POINTOPOINT)
501 dstaddr = ifa->ifa_dstaddr;
506 * Add information for this address to the list.
508 if (add_addr_to_iflist(&devlist, ifa->ifa_name,
509 ifa->ifa_flags, ifa->ifa_addr, ifa->ifa_netmask,
510 broadaddr, dstaddr, errbuf) < 0) {
520 * We haven't had any errors yet; add the "any" device,
523 if (pcap_add_if(&devlist, "any", 0, any_descr, errbuf) < 0)
529 * We had an error; free the list we've been constructing.
531 if (devlist != NULL) {
532 pcap_freealldevs(devlist);
540 #else /* HAVE_IFADDRS_H */
541 #ifdef HAVE_PROC_NET_DEV
543 * Get from "/proc/net/dev" all interfaces listed there; if they're
544 * already in the list of interfaces we have, that won't add another
545 * instance, but if they're not, that'll add them.
547 * We don't bother getting any addresses for them; it appears you can't
548 * use SIOCGIFADDR on Linux to get IPv6 addresses for interfaces, and,
549 * although some other types of addresses can be fetched with SIOCGIFADDR,
550 * we don't bother with them for now.
552 * We also don't fail if we couldn't open "/proc/net/dev"; we just leave
553 * the list of interfaces as is.
556 scan_proc_net_dev(pcap_if_t **devlistp, int fd, char *errbuf)
562 char name[512]; /* XXX - pick a size */
564 struct ifreq ifrflags;
567 proc_net_f = fopen("/proc/net/dev", "r");
568 if (proc_net_f == NULL)
572 fgets(linebuf, sizeof linebuf, proc_net_f) != NULL; linenum++) {
574 * Skip the first two lines - they're headers.
582 * Skip leading white space.
584 while (*p != '\0' && isspace(*p))
586 if (*p == '\0' || *p == '\n')
587 continue; /* blank line */
590 * Get the interface name.
593 while (*p != '\0' && !isspace(*p)) {
596 * This could be the separator between a
597 * name and an alias number, or it could be
598 * the separator between a name with no
599 * alias number and the next field.
601 * If there's a colon after digits, it
602 * separates the name and the alias number,
603 * otherwise it separates the name and the
611 * That was the next field,
612 * not the alias number.
623 * Get the flags for this interface, and skip it if
626 strncpy(ifrflags.ifr_name, name, sizeof(ifrflags.ifr_name));
627 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
630 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
631 "SIOCGIFFLAGS: %.*s: %s",
632 (int)sizeof(ifrflags.ifr_name),
634 pcap_strerror(errno));
638 if (!(ifrflags.ifr_flags & IFF_UP))
642 * Add an entry for this interface, with no addresses.
644 if (pcap_add_if(devlistp, name, ifrflags.ifr_flags, NULL,
655 * Well, we didn't fail for any other reason; did we
656 * fail due to an error reading the file?
658 if (ferror(proc_net_f)) {
659 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
660 "Error reading /proc/net/dev: %s",
661 pcap_strerror(errno));
666 (void)fclose(proc_net_f);
669 #endif /* HAVE_PROC_NET_DEV */
672 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
674 pcap_if_t *devlist = NULL;
676 register struct ifreq *ifrp, *ifend, *ifnext;
681 struct ifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
682 struct sockaddr *netmask, *broadaddr, *dstaddr;
686 * Create a socket from which to fetch the list of interfaces.
688 fd = socket(AF_INET, SOCK_DGRAM, 0);
690 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
691 "socket: %s", pcap_strerror(errno));
696 * Start with an 8K buffer, and keep growing the buffer until
697 * we get the entire interface list or fail to get it for some
698 * reason other than EINVAL (which is presumed here to mean
699 * "buffer is too small").
703 buf = malloc(buf_size);
705 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
706 "malloc: %s", pcap_strerror(errno));
711 ifc.ifc_len = buf_size;
713 memset(buf, 0, buf_size);
714 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0
715 && errno != EINVAL) {
716 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
717 "SIOCGIFCONF: %s", pcap_strerror(errno));
722 if (ifc.ifc_len < buf_size)
728 ifrp = (struct ifreq *)buf;
729 ifend = (struct ifreq *)(buf + ifc.ifc_len);
731 for (; ifrp < ifend; ifrp = ifnext) {
732 n = SA_LEN(&ifrp->ifr_addr) + sizeof(ifrp->ifr_name);
733 if (n < sizeof(*ifrp))
736 ifnext = (struct ifreq *)((char *)ifrp + n);
739 * Get the flags for this interface, and skip it if it's
742 strncpy(ifrflags.ifr_name, ifrp->ifr_name,
743 sizeof(ifrflags.ifr_name));
744 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
747 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
748 "SIOCGIFFLAGS: %.*s: %s",
749 (int)sizeof(ifrflags.ifr_name),
751 pcap_strerror(errno));
755 if (!(ifrflags.ifr_flags & IFF_UP))
759 * Get the netmask for this address on this interface.
761 strncpy(ifrnetmask.ifr_name, ifrp->ifr_name,
762 sizeof(ifrnetmask.ifr_name));
763 memcpy(&ifrnetmask.ifr_addr, &ifrp->ifr_addr,
764 sizeof(ifrnetmask.ifr_addr));
765 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifrnetmask) < 0) {
766 if (errno == EADDRNOTAVAIL) {
772 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
773 "SIOCGIFNETMASK: %.*s: %s",
774 (int)sizeof(ifrnetmask.ifr_name),
776 pcap_strerror(errno));
781 netmask = &ifrnetmask.ifr_addr;
784 * Get the broadcast address for this address on this
785 * interface (if any).
787 if (ifrflags.ifr_flags & IFF_BROADCAST) {
788 strncpy(ifrbroadaddr.ifr_name, ifrp->ifr_name,
789 sizeof(ifrbroadaddr.ifr_name));
790 memcpy(&ifrbroadaddr.ifr_addr, &ifrp->ifr_addr,
791 sizeof(ifrbroadaddr.ifr_addr));
792 if (ioctl(fd, SIOCGIFBRDADDR,
793 (char *)&ifrbroadaddr) < 0) {
794 if (errno == EADDRNOTAVAIL) {
800 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
801 "SIOCGIFBRDADDR: %.*s: %s",
802 (int)sizeof(ifrbroadaddr.ifr_name),
803 ifrbroadaddr.ifr_name,
804 pcap_strerror(errno));
809 broadaddr = &ifrbroadaddr.ifr_broadaddr;
812 * Not a broadcast interface, so no broadcast
819 * Get the destination address for this address on this
820 * interface (if any).
822 if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
823 strncpy(ifrdstaddr.ifr_name, ifrp->ifr_name,
824 sizeof(ifrdstaddr.ifr_name));
825 memcpy(&ifrdstaddr.ifr_addr, &ifrp->ifr_addr,
826 sizeof(ifrdstaddr.ifr_addr));
827 if (ioctl(fd, SIOCGIFDSTADDR,
828 (char *)&ifrdstaddr) < 0) {
829 if (errno == EADDRNOTAVAIL) {
835 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
836 "SIOCGIFDSTADDR: %.*s: %s",
837 (int)sizeof(ifrdstaddr.ifr_name),
839 pcap_strerror(errno));
844 dstaddr = &ifrdstaddr.ifr_dstaddr;
849 * Add information for this address to the list.
851 if (add_addr_to_iflist(&devlist, ifrp->ifr_name,
852 ifrflags.ifr_flags, &ifrp->ifr_addr,
853 netmask, broadaddr, dstaddr, errbuf) < 0) {
860 #ifdef HAVE_PROC_NET_DEV
863 * We haven't had any errors yet; now read "/proc/net/dev",
864 * and add to the list of interfaces all interfaces listed
865 * there that we don't already have, because, on Linux,
866 * SIOCGIFCONF reports only interfaces with IPv4 addresses,
867 * so you need to read "/proc/net/dev" to get the names of
868 * the rest of the interfaces.
870 ret = scan_proc_net_dev(&devlist, fd, errbuf);
877 * We haven't had any errors yet; add the "any" device,
880 if (pcap_add_if(&devlist, "any", 0, any_descr, errbuf) < 0) {
882 * Oops, we had a fatal error.
890 * We had an error; free the list we've been constructing.
892 if (devlist != NULL) {
893 pcap_freealldevs(devlist);
901 #endif /* HAVE_IFADDRS_H */
904 * Free a list of interfaces.
907 pcap_freealldevs(pcap_if_t *alldevs)
909 pcap_if_t *curdev, *nextdev;
910 pcap_addr_t *curaddr, *nextaddr;
912 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
913 nextdev = curdev->next;
916 * Free all addresses.
918 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
919 nextaddr = curaddr->next;
922 if (curaddr->netmask)
923 free(curaddr->netmask);
924 if (curaddr->broadaddr)
925 free(curaddr->broadaddr);
926 if (curaddr->dstaddr)
927 free(curaddr->dstaddr);
932 * Free the name string.
937 * Free the description string, if any.
939 if (curdev->description != NULL)
940 free(curdev->description);
943 * Free the interface.
950 * Return the name of a network interface attached to the system, or NULL
951 * if none can be found. The interface must be configured up; the
952 * lowest unit number is preferred; loopback is ignored.
955 pcap_lookupdev(errbuf)
956 register char *errbuf;
959 /* for old BSD systems, including bsdi3 */
961 #define IF_NAMESIZE IFNAMSIZ
963 static char device[IF_NAMESIZE + 1];
966 if (pcap_findalldevs(&alldevs, errbuf) == -1)
969 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
971 * There are no devices on the list, or the first device
972 * on the list is a loopback device, which means there
973 * are no non-loopback devices on the list. This means
974 * we can't return any device.
976 * XXX - why not return a loopback device? If we can't
977 * capture on it, it won't be on the list, and if it's
978 * on the list, there aren't any non-loopback devices,
979 * so why not just supply it as the default device?
981 (void)strlcpy(errbuf, "no suitable device found",
986 * Return the name of the first device on the list.
988 (void)strlcpy(device, alldevs->name, sizeof(device));
992 pcap_freealldevs(alldevs);
997 pcap_lookupnet(device, netp, maskp, errbuf)
998 register char *device;
999 register bpf_u_int32 *netp, *maskp;
1000 register char *errbuf;
1003 register struct sockaddr_in *sin;
1007 * The pseudo-device "any" listens on all interfaces and therefore
1008 * has the network address and -mask "0.0.0.0" therefore catching
1009 * all traffic. Using NULL for the interface is the same as "any".
1011 if (!device || strcmp(device, "any") == 0) {
1016 fd = socket(AF_INET, SOCK_DGRAM, 0);
1018 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
1019 pcap_strerror(errno));
1022 memset(&ifr, 0, sizeof(ifr));
1024 /* XXX Work around Linux kernel bug */
1025 ifr.ifr_addr.sa_family = AF_INET;
1027 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1028 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
1029 if (errno == EADDRNOTAVAIL) {
1030 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
1031 "%s: no IPv4 address assigned", device);
1033 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
1034 "SIOCGIFADDR: %s: %s",
1035 device, pcap_strerror(errno));
1040 sin = (struct sockaddr_in *)&ifr.ifr_addr;
1041 *netp = sin->sin_addr.s_addr;
1042 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
1043 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
1044 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
1049 *maskp = sin->sin_addr.s_addr;
1051 if (IN_CLASSA(*netp))
1052 *maskp = IN_CLASSA_NET;
1053 else if (IN_CLASSB(*netp))
1054 *maskp = IN_CLASSB_NET;
1055 else if (IN_CLASSC(*netp))
1056 *maskp = IN_CLASSC_NET;
1058 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
1059 "inet class for 0x%x unknown", *netp);