diff options
author | Guy Harris <guy@alum.mit.edu> | 2017-01-18 13:36:27 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2017-01-18 13:36:27 -0800 |
commit | 2a6286aa06b043240dd8c3524668455562d466d8 (patch) | |
tree | 062ee1df238c3e113a0451e60bb2b030867ffb17 | |
parent | 5fa9ce62e3235688ce3ed5928cbce7754ec6291c (diff) | |
download | libpcap-2a6286aa06b043240dd8c3524668455562d466d8.tar.gz |
Have a pcap_if_list_t structure for use by the findalldevs code.
It's not part of the API, but it's an internal structure used by the
findalldevs code. Currently, it just has a pointer to the beginning of
the list, but it could change in order to speed up the process of adding
to the list.
-rw-r--r-- | fad-getad.c | 16 | ||||
-rw-r--r-- | fad-gifc.c | 16 | ||||
-rw-r--r-- | fad-glifc.c | 16 | ||||
-rw-r--r-- | pcap-bpf.c | 10 | ||||
-rw-r--r-- | pcap-bt-linux.c | 4 | ||||
-rw-r--r-- | pcap-bt-linux.h | 2 | ||||
-rw-r--r-- | pcap-bt-monitor-linux.c | 4 | ||||
-rw-r--r-- | pcap-bt-monitor-linux.h | 2 | ||||
-rw-r--r-- | pcap-dag.c | 5 | ||||
-rw-r--r-- | pcap-dag.h | 2 | ||||
-rw-r--r-- | pcap-dbus.c | 6 | ||||
-rw-r--r-- | pcap-dbus.h | 2 | ||||
-rw-r--r-- | pcap-dlpi.c | 6 | ||||
-rw-r--r-- | pcap-dos.c | 41 | ||||
-rw-r--r-- | pcap-int.h | 16 | ||||
-rw-r--r-- | pcap-libdlpi.c | 8 | ||||
-rw-r--r-- | pcap-linux.c | 16 | ||||
-rw-r--r-- | pcap-netfilter-linux.c | 6 | ||||
-rw-r--r-- | pcap-netfilter-linux.h | 2 | ||||
-rw-r--r-- | pcap-nit.c | 4 | ||||
-rw-r--r-- | pcap-null.c | 3 | ||||
-rw-r--r-- | pcap-pf.c | 4 | ||||
-rw-r--r-- | pcap-septel.c | 5 | ||||
-rw-r--r-- | pcap-septel.h | 2 | ||||
-rw-r--r-- | pcap-sita.c | 8 | ||||
-rw-r--r-- | pcap-snf.c | 11 | ||||
-rw-r--r-- | pcap-snf.h | 2 | ||||
-rw-r--r-- | pcap-snit.c | 4 | ||||
-rw-r--r-- | pcap-snoop.c | 4 | ||||
-rw-r--r-- | pcap-usb-linux.c | 12 | ||||
-rw-r--r-- | pcap-usb-linux.h | 2 | ||||
-rw-r--r-- | pcap-win32.c | 28 | ||||
-rw-r--r-- | pcap.c | 53 |
33 files changed, 143 insertions, 179 deletions
diff --git a/fad-getad.c b/fad-getad.c index b67b5cdc..a4625dfd 100644 --- a/fad-getad.c +++ b/fad-getad.c @@ -144,10 +144,9 @@ get_sa_len(struct sockaddr *addr) * could be opened. */ int -pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, +pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, int (*check_usable)(const char *)) { - pcap_if_t *devlist = NULL; struct ifaddrs *ifap, *ifa; struct sockaddr *addr, *netmask, *broadaddr, *dstaddr; size_t addr_size, broadaddr_size, dstaddr_size; @@ -265,7 +264,7 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, /* * Add information for this address to the list. */ - if (add_addr_to_iflist(&devlist, ifa->ifa_name, + if (add_addr_to_iflist(devlistp, ifa->ifa_name, if_flags_to_pcap_flags(ifa->ifa_name, ifa->ifa_flags), addr, addr_size, netmask, addr_size, broadaddr, broadaddr_size, dstaddr, dstaddr_size, @@ -277,16 +276,5 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, freeifaddrs(ifap); - if (ret == -1) { - /* - * We had an error; free the list we've been constructing. - */ - if (devlist != NULL) { - pcap_freealldevs(devlist); - devlist = NULL; - } - } - - *alldevsp = devlist; return (ret); } @@ -132,10 +132,9 @@ struct rtentry; /* declarations in <net/if.h> */ * we already have that. */ int -pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, +pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, int (*check_usable)(const char *)) { - pcap_if_t *devlist = NULL; register int fd; register struct ifreq *ifrp, *ifend, *ifnext; size_t n; @@ -401,7 +400,7 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, /* * Add information for this address to the list. */ - if (add_addr_to_iflist(&devlist, ifrp->ifr_name, + if (add_addr_to_iflist(devlistp, ifrp->ifr_name, if_flags_to_pcap_flags(ifrp->ifr_name, ifrflags.ifr_flags), &ifrp->ifr_addr, SA_LEN(&ifrp->ifr_addr), netmask, netmask_size, broadaddr, broadaddr_size, @@ -413,16 +412,5 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, free(buf); (void)close(fd); - if (ret == -1) { - /* - * We had an error; free the list we've been constructing. - */ - if (devlist != NULL) { - pcap_freealldevs(devlist); - devlist = NULL; - } - } - - *alldevsp = devlist; return (ret); } diff --git a/fad-glifc.c b/fad-glifc.c index 511481cf..e53bad76 100644 --- a/fad-glifc.c +++ b/fad-glifc.c @@ -75,10 +75,9 @@ struct rtentry; /* declarations in <net/if.h> */ * SIOCGLIFCONF rather than SIOCGIFCONF in order to get IPv6 addresses.) */ int -pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, +pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, int (*check_usable)(const char *)) { - pcap_if_t *devlist = NULL; register int fd4, fd6, fd; register struct lifreq *ifrp, *ifend; struct lifnum ifn; @@ -329,7 +328,7 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, /* * Add information for this address to the list. */ - if (add_addr_to_iflist(&devlist, ifrp->lifr_name, + if (add_addr_to_iflist(devlistp, ifrp->lifr_name, if_flags_to_pcap_flags(ifrp->lifr_name, ifrflags.lifr_flags), (struct sockaddr *)&ifrp->lifr_addr, sizeof (struct sockaddr_storage), @@ -344,16 +343,5 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf, (void)close(fd6); (void)close(fd4); - if (ret == -1) { - /* - * We had an error; free the list we've been constructing. - */ - if (devlist != NULL) { - pcap_freealldevs(devlist); - devlist = NULL; - } - } - - *alldevsp = devlist; return (ret); } @@ -2622,7 +2622,7 @@ check_bpf_bindable(const char *name) #if defined(__FreeBSD__) && defined(SIOCIFCREATE2) static int -finddevs_usb(pcap_if_t **alldevsp, char *errbuf) +finddevs_usb(pcap_if_list_t *devlistp, char *errbuf) { DIR *usbdir; struct dirent *usbitem; @@ -2684,7 +2684,7 @@ finddevs_usb(pcap_if_t **alldevsp, char *errbuf) * so we need to avoid adding multiple capture devices * for each bus. */ - if (find_or_add_dev(alldevsp, name, PCAP_IF_UP, NULL, errbuf) == NULL) { + if (find_or_add_dev(devlistp, name, PCAP_IF_UP, NULL, errbuf) == NULL) { free(name); closedir(usbdir); return (PCAP_ERROR); @@ -2697,16 +2697,16 @@ finddevs_usb(pcap_if_t **alldevsp, char *errbuf) #endif int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { /* * Get the list of regular interfaces first. */ - if (pcap_findalldevs_interfaces(alldevsp, errbuf, check_bpf_bindable) == -1) + if (pcap_findalldevs_interfaces(devlistp, errbuf, check_bpf_bindable) == -1) return (-1); /* failure */ #if defined(__FreeBSD__) && defined(SIOCIFCREATE2) - if (finddevs_usb(alldevsp, errbuf) == -1) + if (finddevs_usb(devlistp, errbuf) == -1) return (-1); #endif diff --git a/pcap-bt-linux.c b/pcap-bt-linux.c index 2a36e264..8a258ebf 100644 --- a/pcap-bt-linux.c +++ b/pcap-bt-linux.c @@ -74,7 +74,7 @@ struct pcap_bt { }; int -bt_findalldevs(pcap_if_t **alldevsp, char *err_str) +bt_findalldevs(pcap_if_list_t *devlistp, char *err_str) { struct hci_dev_list_req *dev_list; struct hci_dev_req *dev_req; @@ -119,7 +119,7 @@ bt_findalldevs(pcap_if_t **alldevsp, char *err_str) pcap_snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id); pcap_snprintf(dev_descr, 30, "Bluetooth adapter number %d", i); - if (add_dev(alldevsp, dev_name, 0, dev_descr, err_str) == NULL) + if (add_dev(devlistp, dev_name, 0, dev_descr, err_str) == NULL) { ret = -1; break; diff --git a/pcap-bt-linux.h b/pcap-bt-linux.h index e098654d..163bd341 100644 --- a/pcap-bt-linux.h +++ b/pcap-bt-linux.h @@ -34,5 +34,5 @@ /* * Prototypes for Bluetooth-related functions */ -int bt_findalldevs(pcap_if_t **alldevsp, char *err_str); +int bt_findalldevs(pcap_if_list_t *devlistp, char *err_str); pcap_t *bt_create(const char *device, char *ebuf, int *is_ours); diff --git a/pcap-bt-monitor-linux.c b/pcap-bt-monitor-linux.c index 3cd6178b..63863a9f 100644 --- a/pcap-bt-monitor-linux.c +++ b/pcap-bt-monitor-linux.c @@ -60,11 +60,11 @@ struct hci_mon_hdr { } __attribute__((packed)); int -bt_monitor_findalldevs(pcap_if_t **alldevsp, char *err_str) +bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str) { int ret = 0; - if (add_dev(alldevsp, INTERFACE_NAME, 0, + if (add_dev(devlistp, INTERFACE_NAME, 0, "Bluetooth Linux Monitor", err_str) == NULL) { ret = -1; diff --git a/pcap-bt-monitor-linux.h b/pcap-bt-monitor-linux.h index aada2bc7..8133710a 100644 --- a/pcap-bt-monitor-linux.h +++ b/pcap-bt-monitor-linux.h @@ -28,5 +28,5 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -int bt_monitor_findalldevs(pcap_if_t **alldevsp, char *err_str); +int bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str); pcap_t *bt_monitor_create(const char *device, char *ebuf, int *is_ours); @@ -1068,7 +1068,7 @@ dag_stats(pcap_t *p, struct pcap_stat *ps) { * Add all DAG devices. */ int -dag_findalldevs(pcap_if_t **devlistp, char *errbuf) +dag_findalldevs(pcap_if_list_t *devlistp, char *errbuf) { char name[12]; /* XXX - pick a size */ int ret = 0; @@ -1356,9 +1356,8 @@ dag_get_datalink(pcap_t *p) * There are no regular interfaces, just DAG interfaces. */ int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp _U_, char *errbuf) { - *alldevsp = NULL; return (0); } @@ -9,4 +9,4 @@ */ pcap_t *dag_create(const char *, char *, int *); -int dag_findalldevs(pcap_if_t **devlistp, char *errbuf); +int dag_findalldevs(pcap_if_list_t *devlistp, char *errbuf); diff --git a/pcap-dbus.c b/pcap-dbus.c index 6408fe51..41f50411 100644 --- a/pcap-dbus.c +++ b/pcap-dbus.c @@ -268,11 +268,11 @@ dbus_create(const char *device, char *ebuf, int *is_ours) } int -dbus_findalldevs(pcap_if_t **alldevsp, char *err_str) +dbus_findalldevs(pcap_if_list_t *devlistp, char *err_str) { - if (add_dev(alldevsp, "dbus-system", 0, "D-Bus system bus", err_str) == NULL) + if (add_dev(devlistp, "dbus-system", 0, "D-Bus system bus", err_str) == NULL) return -1; - if (add_dev(alldevsp, "dbus-session", 0, "D-Bus session bus", err_str) == NULL) + if (add_dev(devlistp, "dbus-session", 0, "D-Bus session bus", err_str) == NULL) return -1; return 0; } diff --git a/pcap-dbus.h b/pcap-dbus.h index 67493cce..c97f2e12 100644 --- a/pcap-dbus.h +++ b/pcap-dbus.h @@ -1,2 +1,2 @@ pcap_t *dbus_create(const char *, char *, int *); -int dbus_findalldevs(pcap_if_t **devlistp, char *errbuf); +int dbus_findalldevs(pcap_if_list_t *devlistp, char *errbuf); diff --git a/pcap-dlpi.c b/pcap-dlpi.c index 04251dd8..dc243752 100644 --- a/pcap-dlpi.c +++ b/pcap-dlpi.c @@ -1026,7 +1026,7 @@ is_dlpi_interface(const char *name) } int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { #ifdef HAVE_SOLARIS int fd; @@ -1042,7 +1042,7 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) /* * Get the list of regular interfaces first. */ - if (pcap_findalldevs_interfaces(alldevsp, errbuf, is_dlpi_interface) == -1) + if (pcap_findalldevs_interfaces(devlistp, errbuf, is_dlpi_interface) == -1) return (-1); /* failure */ #ifdef HAVE_SOLARIS @@ -1068,7 +1068,7 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) } for (i = 0; i < buf.nunits; i++) { pcap_snprintf(baname, sizeof baname, "ba%u", i); - if (add_dev(alldevsp, baname, 0, NULL, errbuf) == NULL) + if (add_dev(devlistp, baname, 0, NULL, errbuf) == NULL) return (-1); } #endif @@ -539,17 +539,17 @@ int pcap_lookupnet (const char *device, bpf_u_int32 *localnet, /* * Get a list of all interfaces that are present and that we probe okay. * Returns -1 on error, 0 otherwise. - * The list, as returned through "alldevsp", may be NULL if no interfaces - * were up and could be opened. + * The list may be NULL epty if no interfaces were up and could be opened. */ -int pcap_platform_finddevs (pcap_if_t **alldevsp, char *errbuf) +int pcap_platform_finddevs (pcap_if_list_t *devlistp, char *errbuf) { struct device *dev; +#if 0 /* Pkt drivers should have no addresses */ struct sockaddr_in sa_ll_1, sa_ll_2; struct sockaddr *addr, *netmask, *broadaddr, *dstaddr; - pcap_if_t *devlist = NULL; +#endif int ret = 0; - size_t addr_size = sizeof(*addr); + int found = 0; for (dev = (struct device*)dev_base; dev; dev = dev->next) { @@ -562,6 +562,14 @@ int pcap_platform_finddevs (pcap_if_t **alldevsp, char *errbuf) FLUSHK(); (*dev->close) (dev); + if (add_dev(devlistp, dev->name, dev->flags, + dev->long_name, errbuf) == NULL) + { + ret = -1; + break; + } + found = 1; +#if 0 /* Pkt drivers should have no addresses */ memset (&sa_ll_1, 0, sizeof(sa_ll_1)); memset (&sa_ll_2, 0, sizeof(sa_ll_2)); sa_ll_1.sin_family = AF_INET; @@ -573,16 +581,10 @@ int pcap_platform_finddevs (pcap_if_t **alldevsp, char *errbuf) broadaddr = (struct sockaddr*) &sa_ll_2; memset (&sa_ll_2.sin_addr, 0xFF, sizeof(sa_ll_2.sin_addr)); - if (add_dev(&devlist, dev->name, dev->flags, - dev->long_name, errbuf) == NULL) - { - ret = -1; - break; - } -#if 0 /* Pkt drivers should have no addresses */ - if (add_addr_to_dev(curdev, addr, addr_size, - netmask, addr_size, broadaddr, addr_size, - dstaddr, addr_size, errbuf) < 0) + if (add_addr_to_dev(curdev, addr, sizeof(*addr), + netmask, sizeof(*netmask), + broadaddr, sizeof(*broadaddr), + dstaddr, sizeof(*dstaddr), errbuf) < 0) { ret = -1; break; @@ -590,16 +592,9 @@ int pcap_platform_finddevs (pcap_if_t **alldevsp, char *errbuf) #endif } - if (devlist && ret < 0) - { - pcap_freealldevs (devlist); - devlist = NULL; - } - else - if (!devlist) + if (ret == 0 && !found) strcpy (errbuf, "No drivers found"); - *alldevsp = devlist; return (ret); } @@ -404,6 +404,8 @@ int pcap_check_activated(pcap_t *); /* * Internal interfaces for "pcap_findalldevs()". * + * A pcap_if_list_t * is a reference to a list of devices. + * * "pcap_platform_finddevs()" is the platform-dependent routine to * find local network interfaces. * @@ -417,22 +419,24 @@ int pcap_check_activated(pcap_t *); * "find_or_add_dev()" checks whether a device is already in a pcap_if_list_t * and, if not, adds an entry for it. */ -int pcap_platform_finddevs(pcap_if_t **, char *); +struct pcap_if_list; +typedef struct pcap_if_list pcap_if_list_t; +int pcap_platform_finddevs(pcap_if_list_t *, char *); #if !defined(_WIN32) && !defined(MSDOS) -int pcap_findalldevs_interfaces(pcap_if_t **, char *, +int pcap_findalldevs_interfaces(pcap_if_list_t *, char *, int (*)(const char *)); #endif -pcap_if_t *find_or_add_dev(pcap_if_t **, const char *, bpf_u_int32, +pcap_if_t *find_or_add_dev(pcap_if_list_t *, const char *, bpf_u_int32, const char *, char *); -pcap_if_t *find_dev(pcap_if_t **, const char *); -pcap_if_t *add_dev(pcap_if_t **, const char *, bpf_u_int32, const char *, +pcap_if_t *find_dev(pcap_if_list_t *, const char *); +pcap_if_t *add_dev(pcap_if_list_t *, const char *, bpf_u_int32, const char *, char *); int add_addr_to_dev(pcap_if_t *, struct sockaddr *, size_t, struct sockaddr *, size_t, struct sockaddr *, size_t, struct sockaddr *dstaddr, size_t, char *errbuf); #ifndef _WIN32 bpf_u_int32 if_flags_to_pcap_flags(const char *, u_int); -int add_addr_to_iflist(pcap_if_t **, const char *, bpf_u_int32, +int add_addr_to_iflist(pcap_if_list_t *, const char *, bpf_u_int32, struct sockaddr *, size_t, struct sockaddr *, size_t, struct sockaddr *, size_t, struct sockaddr *, size_t, char *); #endif diff --git a/pcap-libdlpi.c b/pcap-libdlpi.c index 933f2bdc..d004f62e 100644 --- a/pcap-libdlpi.c +++ b/pcap-libdlpi.c @@ -282,7 +282,7 @@ is_dlpi_interface(const char *name _U_) * additional network links present in the system. */ int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { int retv = 0; @@ -293,7 +293,8 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) /* * Get the list of regular interfaces first. */ - if (pcap_findalldevs_interfaces(alldevsp, errbuf, is_dlpi_interface) == -1) + if (pcap_findalldevs_interfaces(devlistp, errbuf, + is_dlpi_interface) == -1) return (-1); /* failure */ /* dlpi_walk() for loopback will be added here. */ @@ -320,7 +321,8 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) * If it isn't already in the list of devices, try to * add it. */ - if (find_or_add_dev(alldevsp, entry->linkname, 0, NULL, errbuf) == NULL) + if (find_or_add_dev(devlistp, entry->linkname, 0, NULL, + errbuf) == NULL) retv = -1; } done: diff --git a/pcap-linux.c b/pcap-linux.c index b9a4bd77..d48fb8c3 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -2230,7 +2230,7 @@ pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats) } static int -add_linux_if(pcap_if_t **devlistp, const char *ifname, int fd, char *errbuf) +add_linux_if(pcap_if_list_t *devlistp, const char *ifname, int fd, char *errbuf) { const char *p; char name[512]; /* XXX - pick a size */ @@ -2319,7 +2319,7 @@ add_linux_if(pcap_if_t **devlistp, const char *ifname, int fd, char *errbuf) * Otherwise, we return 1 if we don't get an error and -1 if we do. */ static int -scan_sys_class_net(pcap_if_t **devlistp, char *errbuf) +scan_sys_class_net(pcap_if_list_t *devlistp, char *errbuf) { DIR *sys_class_net_d; int fd; @@ -2437,7 +2437,7 @@ scan_sys_class_net(pcap_if_t **devlistp, char *errbuf) * See comments from scan_sys_class_net(). */ static int -scan_proc_net_dev(pcap_if_t **devlistp, char *errbuf) +scan_proc_net_dev(pcap_if_list_t *devlistp, char *errbuf) { FILE *proc_net_f; int fd; @@ -2533,14 +2533,14 @@ can_be_bound(const char *name _U_) } int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { int ret; /* * Get the list of regular interfaces first. */ - if (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound) == -1) + if (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound) == -1) return (-1); /* failure */ /* @@ -2551,21 +2551,21 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) * interfaces with no addresses, so you need to read "/sys/class/net" * to get the names of the rest of the interfaces. */ - ret = scan_sys_class_net(alldevsp, errbuf); + ret = scan_sys_class_net(devlistp, errbuf); if (ret == -1) return (-1); /* failed */ if (ret == 0) { /* * No /sys/class/net; try reading /proc/net/dev instead. */ - if (scan_proc_net_dev(alldevsp, errbuf) == -1) + if (scan_proc_net_dev(devlistp, errbuf) == -1) return (-1); } /* * Add the "any" device. */ - if (add_dev(alldevsp, "any", PCAP_IF_UP|PCAP_IF_RUNNING, + if (add_dev(devlistp, "any", PCAP_IF_UP|PCAP_IF_RUNNING, any_descr, errbuf) == NULL) return (-1); diff --git a/pcap-netfilter-linux.c b/pcap-netfilter-linux.c index 446f5f08..49438cda 100644 --- a/pcap-netfilter-linux.c +++ b/pcap-netfilter-linux.c @@ -635,7 +635,7 @@ netfilter_create(const char *device, char *ebuf, int *is_ours) } int -netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str) +netfilter_findalldevs(pcap_if_list_t *devlistp, char *err_str) { int sock; @@ -650,9 +650,9 @@ netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str) } close(sock); - if (add_dev(alldevsp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) == NULL) + if (add_dev(devlistp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) == NULL) return -1; - if (add_dev(alldevsp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) == NULL) + if (add_dev(devlistp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) == NULL) return -1; return 0; } diff --git a/pcap-netfilter-linux.h b/pcap-netfilter-linux.h index 01d9b394..97b73108 100644 --- a/pcap-netfilter-linux.h +++ b/pcap-netfilter-linux.h @@ -31,5 +31,5 @@ /* * Prototypes for netlink-related functions */ -int netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str); +int netfilter_findalldevs(pcap_if_list_t *devlistp, char *err_str); pcap_t *netfilter_create(const char *device, char *ebuf, int *is_ours); @@ -378,7 +378,7 @@ can_be_bound(const char *name _U_) } int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { - return (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound)); + return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound)); } diff --git a/pcap-null.c b/pcap-null.c index b5fa3ab9..de20f227 100644 --- a/pcap-null.c +++ b/pcap-null.c @@ -43,11 +43,10 @@ pcap_create_interface(const char *device _U_, char *ebuf) } int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { /* * There are no interfaces on which we can capture. */ - *alldevsp = NULL; return (0); } @@ -529,9 +529,9 @@ can_be_bound(const char *name _U_) } int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { - return (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound)); + return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound)); } static int diff --git a/pcap-septel.c b/pcap-septel.c index e15b1d33..dc120dd5 100644 --- a/pcap-septel.c +++ b/pcap-septel.c @@ -252,7 +252,7 @@ static int septel_stats(pcap_t *p, struct pcap_stat *ps) { int -septel_findalldevs(pcap_if_t **devlistp, char *errbuf) +septel_findalldevs(pcap_if_list_t *devlistp, char *errbuf) { if (add_dev(devlistp,"septel",0,"Intel/Septel device",errbuf) == NULL) return -1; @@ -303,9 +303,8 @@ septel_setnonblock(pcap_t *p, int nonblock, char *errbuf) * There are no regular interfaces, just Septel interfaces. */ int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { - *alldevsp = NULL; return (0); } diff --git a/pcap-septel.h b/pcap-septel.h index b6e1168b..0e648b20 100644 --- a/pcap-septel.h +++ b/pcap-septel.h @@ -10,4 +10,4 @@ */ pcap_t *septel_create(const char *device, char *ebuf, int *is_ours); -int septel_findalldevs(pcap_if_t **devlistp, char *errbuf); +int septel_findalldevs(pcap_if_list_t *devlistp, char *errbuf); diff --git a/pcap-sita.c b/pcap-sita.c index 1cd5f751..075aee77 100644 --- a/pcap-sita.c +++ b/pcap-sita.c @@ -821,9 +821,9 @@ static int acn_open_live(const char *name, char *errbuf, int *linktype) { /* re int chassis, geoslot; unit_t *u; iface_t *p; - pcap_if_t *alldevsp; + pcap_if_list_t devlist; - pcap_platform_finddevs(&alldevsp, errbuf); + pcap_platform_finddevs(&devlist, errbuf); for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { /* scan the table... */ for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { u = &units[chassis][geoslot]; @@ -1025,7 +1025,7 @@ pcap_t *pcap_create_interface(const char *device _U_, char *ebuf) { return (p); } -int pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) { +int pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { //printf("pcap_findalldevs()\n"); // fulko @@ -1042,7 +1042,7 @@ int pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) { //printf("pcap_findalldevs() returning BAD after findalldevs\n"); // fulko return -1; } - *alldevsp = acn_if_list; + devlistp->beginning = acn_if_list; acn_if_list = 0; /* then forget our list head, because someone will call pcap_freealldevs() to empty the malloc'ed stuff */ //printf("pcap_findalldevs() returning ZERO OK\n"); // fulko return 0; @@ -319,7 +319,7 @@ snf_activate(pcap_t* p) #define MAX_DESC_LENGTH 128 int -snf_findalldevs(pcap_if_t **devlistp, char *errbuf) +snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf) { pcap_if_t *dev; struct snf_ifaddrs *ifaddrs, *ifa; @@ -393,7 +393,7 @@ snf_findalldevs(pcap_if_t **devlistp, char *errbuf) * See if there's already an entry for the device * with the name ifa->snf_ifa_name. */ - dev = find_dev(alldevs, ifa->snf_ifa_name); + dev = find_dev(devlistp, ifa->snf_ifa_name); if (dev != NULL) { /* * Yes. Update its description. @@ -412,7 +412,7 @@ snf_findalldevs(pcap_if_t **devlistp, char *errbuf) /* * No. Add an entry for it. */ - dev = add_dev(alldevs, ifa->snf_ifa_name, 0, desc, + dev = add_dev(devlistp, ifa->snf_ifa_name, 0, desc, errbuf); if (dev == NULL) return -1; @@ -429,7 +429,7 @@ snf_findalldevs(pcap_if_t **devlistp, char *errbuf) (void)pcap_snprintf(name,MAX_DESC_LENGTH,"snf%d",allports); (void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d", allports); - if (add_dev(alldevs, name, 0, desc, errbuf) == NULL) + if (add_dev(devlistp, name, 0, desc, errbuf) == NULL) return (-1); /* * XXX - should we give it a list of addresses with all @@ -525,9 +525,8 @@ snf_create(const char *device, char *ebuf, int *is_ours) * There are no regular interfaces, just SNF interfaces. */ int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { - *alldevsp = NULL; return (0); } @@ -1,2 +1,2 @@ pcap_t *snf_create(const char *, char *, int *); -int snf_findalldevs(pcap_if_t **devlistp, char *errbuf); +int snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf); diff --git a/pcap-snit.c b/pcap-snit.c index 79349456..0c00860b 100644 --- a/pcap-snit.c +++ b/pcap-snit.c @@ -454,7 +454,7 @@ can_be_bound(const char *name _U_) } int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { - return (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound)); + return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound)); } diff --git a/pcap-snoop.c b/pcap-snoop.c index 687b1d90..5eab4a2d 100644 --- a/pcap-snoop.c +++ b/pcap-snoop.c @@ -429,7 +429,7 @@ can_be_bound(const char *name _U_) } int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { - return (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound)); + return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound)); } diff --git a/pcap-usb-linux.c b/pcap-usb-linux.c index 14305a77..bf9a4e5a 100644 --- a/pcap-usb-linux.c +++ b/pcap-usb-linux.c @@ -225,7 +225,7 @@ have_binary_usbmon(void) /* facility to add an USB device to the device list*/ static int -usb_dev_add(pcap_if_t** alldevsp, int n, char *err_str) +usb_dev_add(pcap_if_list_t *devlistp, int n, char *err_str) { char dev_name[10]; char dev_descr[30]; @@ -235,13 +235,13 @@ usb_dev_add(pcap_if_t** alldevsp, int n, char *err_str) else pcap_snprintf(dev_descr, 30, "USB bus number %d", n); - if (add_dev(alldevsp, dev_name, 0, dev_descr, err_str) == NULL) + if (add_dev(devlistp, dev_name, 0, dev_descr, err_str) == NULL) return -1; return 0; } int -usb_findalldevs(pcap_if_t **alldevsp, char *err_str) +usb_findalldevs(pcap_if_list_t *devlistp, char *err_str) { char usb_mon_dir[PATH_MAX]; char *usb_mon_prefix; @@ -292,7 +292,7 @@ usb_findalldevs(pcap_if_t **alldevsp, char *err_str) if (sscanf(&name[usb_mon_prefix_len], "%d", &n) == 0) continue; /* failed */ - ret = usb_dev_add(alldevsp, n, err_str); + ret = usb_dev_add(devlistp, n, err_str); } closedir(dir); @@ -329,7 +329,7 @@ usb_findalldevs(pcap_if_t **alldevsp, char *err_str) if (sscanf(&name[3], "%d", &n) == 0) continue; - ret = usb_dev_add(alldevsp, n, err_str); + ret = usb_dev_add(devlistp, n, err_str); } closedir(dir); @@ -350,7 +350,7 @@ usb_findalldevs(pcap_if_t **alldevsp, char *err_str) if (sscanf(&name[len+1], "%d", &n) != 1) continue; - ret = usb_dev_add(alldevsp, n, err_str); + ret = usb_dev_add(devlistp, n, err_str); } closedir(dir); diff --git a/pcap-usb-linux.h b/pcap-usb-linux.h index 234a262f..3ddc12f4 100644 --- a/pcap-usb-linux.h +++ b/pcap-usb-linux.h @@ -34,5 +34,5 @@ /* * Prototypes for USB-related functions */ -int usb_findalldevs(pcap_if_t **alldevsp, char *err_str); +int usb_findalldevs(pcap_if_list_t *devlistp, char *err_str); pcap_t *usb_create(const char *device, char *ebuf, int *is_ours); diff --git a/pcap-win32.c b/pcap-win32.c index b64c8a62..aa24a425 100644 --- a/pcap-win32.c +++ b/pcap-win32.c @@ -1346,7 +1346,7 @@ pcap_setnonblock_win32(pcap_t *p, int nonblock, char *errbuf) } static int -pcap_add_if_win32(pcap_if_t **devlist, char *name, bpf_u_int32 flags, +pcap_add_if_win32(pcap_if_list_t *devlistp, char *name, bpf_u_int32 flags, const char *description, char *errbuf) { pcap_if_t *curdev; @@ -1359,7 +1359,7 @@ pcap_add_if_win32(pcap_if_t **devlist, char *name, bpf_u_int32 flags, /* * Add an entry for this interface, with no addresses. */ - curdev = add_dev(devlist, name, flags, description, errbuf); + curdev = add_dev(devlistp, name, flags, description, errbuf); if (curdev == NULL) { /* * Failure. @@ -1413,9 +1413,8 @@ pcap_add_if_win32(pcap_if_t **devlist, char *name, bpf_u_int32 flags, } int -pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) +pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { - pcap_if_t *devlist = NULL; int ret = 0; const char *desc; char *AdaptersName; @@ -1454,13 +1453,9 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) } } - if (NameLength > 0) - AdaptersName = (char*) malloc(NameLength); - else - { - *alldevsp = NULL; + if (NameLength <= 0) return 0; - } + AdaptersName = (char*) malloc(NameLength); if (AdaptersName == NULL) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters."); @@ -1518,7 +1513,7 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) /* * Add an entry for this interface. */ - if (pcap_add_if_win32(&devlist, name, flags, desc, + if (pcap_add_if_win32(devlistp, name, flags, desc, errbuf) == -1) { /* * Failure. @@ -1530,17 +1525,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) desc += strlen(desc) + 1; } - if (ret == -1) { - /* - * We had an error; free the list we've been constructing. - */ - if (devlist != NULL) { - pcap_freealldevs(devlist); - devlist = NULL; - } - } - - *alldevsp = devlist; free(AdaptersName); return (ret); } @@ -315,8 +315,15 @@ pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s)); } +/* + * Implementation of a pcap_if_list_t. + */ +struct pcap_if_list { + pcap_if_t *beginning; +}; + static struct capture_source_type { - int (*findalldevs_op)(pcap_if_t **, char *); + int (*findalldevs_op)(pcap_if_list_t *, char *); pcap_t *(*create_op)(const char *, char *, int *); } capture_source_types[] = { #ifdef HAVE_DAG_API @@ -359,32 +366,45 @@ int pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf) { size_t i; + pcap_if_list_t devlist; /* * Find all the local network interfaces on which we * can capture. */ - if (pcap_platform_finddevs(alldevsp, errbuf) == -1) + devlist.beginning = NULL; + if (pcap_platform_finddevs(&devlist, errbuf) == -1) { + /* + * Failed - free all of the entries we were given + * before we failed. + */ + if (devlist.beginning != NULL) + pcap_freealldevs(devlist.beginning); + *alldevsp = NULL; return (-1); + } /* * Ask each of the non-local-network-interface capture * source types what interfaces they have. */ for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) { - if (capture_source_types[i].findalldevs_op(alldevsp, errbuf) == -1) { + if (capture_source_types[i].findalldevs_op(&devlist, errbuf) == -1) { /* * We had an error; free the list we've been * constructing. */ - if (*alldevsp != NULL) { - pcap_freealldevs(*alldevsp); - *alldevsp = NULL; - } + if (devlist.beginning != NULL) + pcap_freealldevs(devlist.beginning); + *alldevsp = NULL; return (-1); } } + /* + * Return the first entry of the list of all devices. + */ + *alldevsp = devlist.beginning; return (0); } @@ -687,7 +707,7 @@ get_if_description(const char *name) * add interfaces even if they have no addresses.) */ int -add_addr_to_iflist(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags, +add_addr_to_iflist(pcap_if_list_t *alldevs, const char *name, bpf_u_int32 flags, struct sockaddr *addr, size_t addr_size, struct sockaddr *netmask, size_t netmask_size, struct sockaddr *broadaddr, size_t broadaddr_size, @@ -849,7 +869,7 @@ add_addr_to_dev(pcap_if_t *curdev, * return -1 and set errbuf to an error message. */ pcap_if_t * -find_or_add_dev(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags, +find_or_add_dev(pcap_if_list_t *alldevs, const char *name, bpf_u_int32 flags, const char *description, char *errbuf) { pcap_if_t *curdev; @@ -876,14 +896,14 @@ find_or_add_dev(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags, * the entry for it if we find it or NULL if we don't. */ pcap_if_t * -find_dev(pcap_if_t **alldevs, const char *name) +find_dev(pcap_if_list_t *alldevs, const char *name) { pcap_if_t *curdev; /* * Is there an entry in the list for this device? */ - for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) { + for (curdev = alldevs->beginning; curdev != NULL; curdev = curdev->next) { if (strcmp(name, curdev->name) == 0) { /* * We found it, so, yes, there is. No need to @@ -909,7 +929,7 @@ find_dev(pcap_if_t **alldevs, const char *name) * If we weren't given a description, try to get one. */ pcap_if_t * -add_dev(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags, +add_dev(pcap_if_list_t *alldevs, const char *name, bpf_u_int32 flags, const char *description, char *errbuf) { pcap_if_t *curdev, *prevdev, *nextdev; @@ -976,7 +996,7 @@ add_dev(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags, /* * The next element is the first element. */ - nextdev = *alldevs; + nextdev = alldevs->beginning; } else nextdev = prevdev->next; @@ -1019,11 +1039,10 @@ add_dev(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags, */ if (prevdev == NULL) { /* - * This is the first interface. Pass back a - * pointer to it, and put "curdev" before - * "nextdev". + * This is the first interface. Make it + * the first element in the list of devices. */ - *alldevs = curdev; + alldevs->beginning = curdev; } else prevdev->next = curdev; return (curdev); |