]> The Tcpdump Group git mirrors - libpcap/commitdiff
Have a pcap_if_list_t structure for use by the findalldevs code.
authorGuy Harris <[email protected]>
Wed, 18 Jan 2017 21:36:27 +0000 (13:36 -0800)
committerGuy Harris <[email protected]>
Wed, 18 Jan 2017 21:36:27 +0000 (13:36 -0800)
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.

33 files changed:
fad-getad.c
fad-gifc.c
fad-glifc.c
pcap-bpf.c
pcap-bt-linux.c
pcap-bt-linux.h
pcap-bt-monitor-linux.c
pcap-bt-monitor-linux.h
pcap-dag.c
pcap-dag.h
pcap-dbus.c
pcap-dbus.h
pcap-dlpi.c
pcap-dos.c
pcap-int.h
pcap-libdlpi.c
pcap-linux.c
pcap-netfilter-linux.c
pcap-netfilter-linux.h
pcap-nit.c
pcap-null.c
pcap-pf.c
pcap-septel.c
pcap-septel.h
pcap-sita.c
pcap-snf.c
pcap-snf.h
pcap-snit.c
pcap-snoop.c
pcap-usb-linux.c
pcap-usb-linux.h
pcap-win32.c
pcap.c

index b67b5cdcd46aa9c2c34dc508e62c1d637555d8c1..a4625dfd7ec485b0702068ff4df594b7c737f18f 100644 (file)
@@ -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);
 }
index 1b24af2929853f59efbb97fb4eb50917934f45c3..d431ba162d0b654fa989180ad05b1473e12bace0 100644 (file)
@@ -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);
 }
index 511481cf96368bdf8edc6b30e794cba4c0e3f726..e53bad76c27b1361b7e3c7098367297cfb5d9ae5 100644 (file)
@@ -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);
 }
index 60086e42f17dcb2756f68b63595e76dc63bec83c..d4f5523864da1d67097613ae2c1f5d3b39bf9105 100644 (file)
@@ -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
 
index 2a36e2649c23c2bc1c7127030a7f69b39cd1f4e2..8a258ebf4b1b3f7b92df60f3be1fe999005bf66d 100644 (file)
@@ -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;
index e098654dcbc8e2da56302584958e96db85add6ec..163bd341fc782d6b594c65c9e35f4b52011abeaf 100644 (file)
@@ -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);
index 3cd6178bf6a6a418d9726a1902b4a88d9643fbe3..63863a9ff8691a52355fc111b9ce2ba705c84719 100644 (file)
@@ -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;
index aada2bc77bfbe20133c5e67b1e727dcdc8fe6dc8..8133710a2617a0bf8e6458c82f13f8338661a2b6 100644 (file)
@@ -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);
index ee024dfdbe11a6726d4e681c7ace4488259a793b..fbd1297398121175cae1c736470f8ae933c75fe6 100644 (file)
@@ -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);
 }
 
index dfeccd8b71169e8c75221bf0043b5ae2c1782164..67361af9adef9f34fd420adc7335141228cae3dc 100644 (file)
@@ -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);
index 6408fe516433c55a80b1eb162dcc4115c17ae56f..41f50411245b0f692c1cc770ddd8abd4df7eb5aa 100644 (file)
@@ -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;
 }
index 67493cce93cfa36c6fae6b4c07c0ef22cd4e77f6..c97f2e121e6ee197036e46450a415544d039f3b9 100644 (file)
@@ -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);
index 04251dd85c13ac3a13769930d519afaba84ddc89..dc243752c11e2db267b13a87e48b4885dff24cc3 100644 (file)
@@ -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
index dbbfc78fdc02e42c5323422a41bc450d223f1c59..20428547e5242ba256a0679241754d638759e03b 100644 (file)
@@ -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);
 }
 
index 65b7b52d15ad8d576b47d37ea592d310c2e99a2c..b5fc8ea3dc34aae4e3fd41335ce945f088d643af 100644 (file)
@@ -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
index 933f2bdc039c3b2cf9873aeda24d5d03bf1e870c..d004f62e740d03470fc50b8f11255881559a079e 100644 (file)
@@ -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:
index b9a4bd77140fe8fd6d920e4d936329171eccfdfe..d48fb8c3227f468ad6855380f9f699d86dccf804 100644 (file)
@@ -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);
 
index 446f5f089a4c037edaa76bd9bdb567024345895f..49438cda6352eb22fb1cff9a8eeeb4412685b76c 100644 (file)
@@ -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;
 }
index 01d9b394f5a806d6c3129912484937dcbc595e05..97b73108fee470e3447cb1ac3e73111fcf17eb29 100644 (file)
@@ -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);
index 1b626e215a22cc20f6c3c23e43b807ac2cba619e..19f10fc3e8c8e1132d56a5f7826811065d5da410 100644 (file)
@@ -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));
 }
index b5fa3ab91fabfc576a22796e402ab9728ecd6d8b..de20f227c311cf6cf7c063fd33e54c466f5ce12c 100644 (file)
@@ -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);
 }
index 73469086d8a46479b1d7a195938f8c185d38dd96..97a33023ff5c8f9dbf4c5d2a6fc032a3e1887c73 100644 (file)
--- a/pcap-pf.c
+++ b/pcap-pf.c
@@ -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
index e15b1d338cf2e9f1ea024138fdbb38eb2aadca78..dc120dd5dbf7355f994d8f59d158184ddb4fa1cc 100644 (file)
@@ -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);
 }
 
index b6e1168b0ef8535dc10d415273f2d16ddf29ec12..0e648b20c60188c51afc4128e622c60e1d77de99 100644 (file)
@@ -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);
index 1cd5f7510a0414fcf92b91e12aab6b3d8e357f50..075aee777a6a64e34d453b2e31b0c2fdd966c150 100644 (file)
@@ -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;
index 1679d4df2f41dc6bc9518b5bdb0d4fb3741adc98..ca0f31785477cfc706c55068aaf12bc8ac341659 100644 (file)
@@ -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);
 }
 
index c9d7722bf4fc87ba38a766603a1a8a585586c2c4..ffc64adf3673ec0b3e881593a160033df0f9e175 100644 (file)
@@ -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);
index 79349456bbac14e98ae073ad8e29d5c14d0e05ba..0c00860b47071760c8b1ef52b477b9b2025617e5 100644 (file)
@@ -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));
 }
index 687b1d907b4f3a9273df1732e90ffb32996dc1f3..5eab4a2d66e05dcafb8ddac2e27aa579f4f71025 100644 (file)
@@ -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));
 }
index 14305a77bb7c736c3a0dbeb2a0380981e50553b5..bf9a4e5a23db17608e843832c9f249ddc67744c6 100644 (file)
@@ -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);
index 234a262f9093c0bab8f9045508518de01bd880b6..3ddc12f4840def70a1b2961665491a8cc168610f 100644 (file)
@@ -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);
index b64c8a629fa20f83538d2c2b4c41e18af0ab78ac..aa24a42585a2b5bf6ec51fd1c52d4bef089034fb 100644 (file)
@@ -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);
 }
diff --git a/pcap.c b/pcap.c
index ff47860c5c5711ea7bfa0a88b6a0f53a172e2f5e..9c2a2fac752ac6a761343d8b6fa6aba2c867391e 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -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);