]> The Tcpdump Group git mirrors - libpcap/commitdiff
Clean up findalldevs code.
authorGuy Harris <[email protected]>
Sun, 18 Dec 2016 02:23:23 +0000 (18:23 -0800)
committerGuy Harris <[email protected]>
Sun, 18 Dec 2016 02:23:23 +0000 (18:23 -0800)
Have a routine that unconditionally adds a device to the list of
devices, without bothering to check whether there's already a device
with that name, and a separate routine that does the check and, if it
doesn't find the device, calls the routine to add ti.  That avoids
scanning the entire list in cases where we know the search will fail.
The only reasons for doing the check are that we're on a platform where
we find the list of interfaces by a call that returns a list of
*addresses*, with an interface name attached to each address, and

1) for each address, we need to make sure we don't already have
   the interface, create it if we don't, and add the address to
   the now-guaranteed-to-exist entry for the interface;

2) we might have to make a *separate* call to enumerate
   interfaces, to find interfaces with *no* addresses, and that
   call also enumerates the ones that *do* have addresses, and
   we don't want to create a duplicate entry for them.

Change some findalldevs helper routines to have "dev" rather than "if"
in the name, as not all devices are regular network interfaces.

For the DAG findalldevs routine, make sure it always provides an error
message if it fails.

We don't need add_addr_to_iflist() or get_if_description() on Windows,
so don't define them on Windows.

Update comments to reflect reality.

15 files changed:
fad-helpers.c
pcap-bpf.c
pcap-bt-linux.c
pcap-bt-monitor-linux.c
pcap-dag.c
pcap-dbus.c
pcap-dlpi.c
pcap-dos.c
pcap-int.h
pcap-libdlpi.c
pcap-linux.c
pcap-netfilter-linux.c
pcap-septel.c
pcap-usb-linux.c
pcap-win32.c

index 65b7e1bfd6ff5b2bd55518b56105c8508b544aa5..9038a259be3eb1bc1a93fd14075c915c7d2e53e0 100644 (file)
@@ -180,6 +180,7 @@ get_figure_of_merit(pcap_if_t *dev)
        return (n);
 }
 
+#ifndef _WIN32
 /*
  * Try to get a description for a given device.
  * Returns a mallocated description if it could and NULL if it couldn't.
@@ -356,175 +357,13 @@ get_if_description(const char *name)
 /*
  * Look for a given device in the specified list of devices.
  *
- * If we find it, return 0 and set *curdev_ret to point to it.
- *
- * If we don't find it, attempt to add an entry for it, with the specified
- * ifnet flags and description, and, if that succeeds, return 0 and set
- * *curdev_ret to point to the new entry, otherwise return PCAP_ERROR
- * and set errbuf to an error message.  If we weren't given a description,
- * try to get one.
- */
-int
-add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
-    bpf_u_int32 flags, const char *description, char *errbuf)
-{
-       pcap_if_t *curdev, *prevdev, *nextdev;
-       u_int this_figure_of_merit, nextdev_figure_of_merit;
-
-       /*
-        * Is there already an entry in the list for this interface?
-        */
-       for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
-               if (strcmp(name, curdev->name) == 0)
-                       break;  /* yes, we found it */
-       }
-
-       if (curdev == NULL) {
-               /*
-                * No, we didn't find it.  Add it to the list of
-                * devices.
-                */
-               curdev = malloc(sizeof(pcap_if_t));
-               if (curdev == NULL) {
-                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
-                           "malloc: %s", pcap_strerror(errno));
-                       return (-1);
-               }
-
-               /*
-                * Fill in the entry.
-                */
-               curdev->next = NULL;
-               curdev->name = strdup(name);
-               if (curdev->name == NULL) {
-                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
-                           "malloc: %s", pcap_strerror(errno));
-                       free(curdev);
-                       return (-1);
-               }
-               if (description == NULL) {
-                       /*
-                        * We weren't handed a description for the
-                        * interface, so see if we can generate one
-                        * ourselves.
-                        */
-                       curdev->description = get_if_description(name);
-               } else {
-                       /*
-                        * We were handed a description; make a copy.
-                        */
-                       curdev->description = strdup(description);
-                       if (curdev->description == NULL) {
-                               (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
-                                   "malloc: %s", pcap_strerror(errno));
-                               free(curdev->name);
-                               free(curdev);
-                               return (-1);
-                       }
-               }
-               curdev->addresses = NULL;       /* list starts out as empty */
-               curdev->flags = flags;
-
-               /*
-                * Add it to the list, in the appropriate location.
-                * First, get the "figure of merit" for this
-                * interface.
-                */
-               this_figure_of_merit = get_figure_of_merit(curdev);
-
-               /*
-                * Now look for the last interface with an figure of merit
-                * less than or equal to the new interface's figure of
-                * merit.
-                *
-                * We start with "prevdev" being NULL, meaning we're before
-                * the first element in the list.
-                */
-               prevdev = NULL;
-               for (;;) {
-                       /*
-                        * Get the interface after this one.
-                        */
-                       if (prevdev == NULL) {
-                               /*
-                                * The next element is the first element.
-                                */
-                               nextdev = *alldevs;
-                       } else
-                               nextdev = prevdev->next;
-
-                       /*
-                        * Are we at the end of the list?
-                        */
-                       if (nextdev == NULL) {
-                               /*
-                                * Yes - we have to put the new entry
-                                * after "prevdev".
-                                */
-                               break;
-                       }
-
-                       /*
-                        * Is the new interface's figure of merit less
-                        * than the next interface's figure of merit,
-                        * meaning that the new interface is better
-                        * than the next interface?
-                        */
-                       nextdev_figure_of_merit = get_figure_of_merit(nextdev);
-                       if (this_figure_of_merit < nextdev_figure_of_merit) {
-                               /*
-                                * Yes - we should put the new entry
-                                * before "nextdev", i.e. after "prevdev".
-                                */
-                               break;
-                       }
-
-                       prevdev = nextdev;
-               }
-
-               /*
-                * Insert before "nextdev".
-                */
-               curdev->next = nextdev;
-
-               /*
-                * Insert after "prevdev" - unless "prevdev" is null,
-                * in which case this is the first interface.
-                */
-               if (prevdev == NULL) {
-                       /*
-                        * This is the first interface.  Pass back a
-                        * pointer to it, and put "curdev" before
-                        * "nextdev".
-                        */
-                       *alldevs = curdev;
-               } else
-                       prevdev->next = curdev;
-       }
-
-       *curdev_ret = curdev;
-       return (0);
-}
-
-/*
- * Try to get a description for a given device, and then look for that
- * device in the specified list of devices.
- *
  * If we find it, then, if the specified address isn't null, add it to
  * the list of addresses for the device and return 0.
  *
- * If we don't find it, check whether we can open it:
- *
- *     If that fails with PCAP_ERROR_NO_SUCH_DEVICE or
- *     PCAP_ERROR_IFACE_NOT_UP, don't attempt to add an entry for
- *     it, as that probably means it exists but doesn't support
- *     packet capture.
- *
- *     Otherwise, attempt to add an entry for it, with the specified
- *     ifnet flags, and, if that succeeds, add the specified address
- *     to its list of addresses if that address is non-null, set
- *     *curdev_ret to point to the new entry, and return 0, otherwise
- *     return PCAP_ERROR and set errbuf to an error message.
+ * If we don't find it, attempt to add an entry for it, with the specified
+ * flags and description, and, if that succeeds, add the specified
+ * address to its list of addresses if that address is non-null, and
+ * return 0, otherwise return -1 and set errbuf to an error message.
  *
  * (We can get called with a null address because we might get a list
  * of interface name/address combinations from the underlying OS, with
@@ -543,19 +382,18 @@ add_addr_to_iflist(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags,
 {
        pcap_if_t *curdev;
 
-       if (add_or_find_if(&curdev, alldevs, name, flags, NULL, errbuf) == -1) {
+       /*
+        * Attempt to find an entry for this device; if we don't find one,
+        * attempt to add one.
+        */
+       curdev = find_or_add_dev(alldevs, name, flags, get_if_description(name),
+           errbuf);
+       if (curdev == NULL) {
                /*
                 * Error - give up.
                 */
                return (-1);
        }
-       if (curdev == NULL) {
-               /*
-                * Device wasn't added because it can't be opened.
-                * Not a fatal error.
-                */
-               return (0);
-       }
 
        if (addr == NULL) {
                /*
@@ -576,6 +414,7 @@ add_addr_to_iflist(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags,
            netmask_size, broadaddr, broadaddr_size, dstaddr,
            dstaddr_size, errbuf));
 }
+#endif /* _WIN32 */
 
 /*
  * Add an entry to the list of addresses for an interface.
@@ -689,30 +528,166 @@ add_addr_to_dev(pcap_if_t *curdev,
 /*
  * Look for a given device in the specified list of devices.
  *
- * If we find it, return 0.
- *
- * If we don't find it, check whether we can open it:
- *
- *     If that fails with PCAP_ERROR_NO_SUCH_DEVICE or
- *     PCAP_ERROR_IFACE_NOT_UP, don't attempt to add an entry for
- *     it, as that probably means it exists but doesn't support
- *     packet capture.
+ * If we find it, return 0 and set *curdev_ret to point to it.
  *
- *     Otherwise, attempt to add an entry for it, with the specified
- *     ifnet flags and description, and, if that succeeds, return 0
- *     and set *curdev_ret to point to the new entry, otherwise
- *     return PCAP_ERROR and set errbuf to an error message.
+ * If we don't find it, attempt to add an entry for it, with the specified
+ * flags and description, and, if that succeeds, return 0, otherwise
+ * return -1 and set errbuf to an error message.
  */
-int
-pcap_add_if(pcap_if_t **devlist, const char *name, u_int flags,
+pcap_if_t *
+find_or_add_dev(pcap_if_t **alldevs, const char *name, bpf_u_int32 flags,
     const char *description, char *errbuf)
 {
        pcap_if_t *curdev;
 
-       return (add_or_find_if(&curdev, devlist, name, flags, description,
-           errbuf));
+       /*
+        * Is there already an entry in the list for this interface?
+        */
+       for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
+               if (strcmp(name, curdev->name) == 0) {
+                       /*
+                        * We found it, so, yes, there is.  No need to
+                        * add it.  Provide the entry we found to our
+                        * caller.
+                        */
+                       return (curdev);
+               }
+       }
+
+       /*
+        * No, we didn't find it.  Try to add it to the list of devices.
+        */
+       return (add_dev(alldevs, name, flags, description, errbuf));
 }
 
+/*
+ * Attempt to add an entry for a device, with the specified flags
+ * and description, and, if that succeeds, return 0 and return a pointer
+ * to the new entry, otherwise return NULL and set errbuf to an error
+ * message.
+ *
+ * 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,
+    const char *description, char *errbuf)
+{
+       pcap_if_t *curdev, *prevdev, *nextdev;
+       u_int this_figure_of_merit, nextdev_figure_of_merit;
+
+       curdev = malloc(sizeof(pcap_if_t));
+       if (curdev == NULL) {
+               (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                   "malloc: %s", pcap_strerror(errno));
+               return (NULL);
+       }
+
+       /*
+        * Fill in the entry.
+        */
+       curdev->next = NULL;
+       curdev->name = strdup(name);
+       if (curdev->name == NULL) {
+               (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                   "malloc: %s", pcap_strerror(errno));
+               free(curdev);
+               return (NULL);
+       }
+       if (description == NULL) {
+               /*
+                * We weren't handed a description for the interface.
+                */
+               curdev->description = NULL;
+       } else {
+               /*
+                * We were handed a description; make a copy.
+                */
+               curdev->description = strdup(description);
+               if (curdev->description == NULL) {
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                           "malloc: %s", pcap_strerror(errno));
+                       free(curdev->name);
+                       free(curdev);
+                       return (NULL);
+               }
+       }
+       curdev->addresses = NULL;       /* list starts out as empty */
+       curdev->flags = flags;
+
+       /*
+        * Add it to the list, in the appropriate location.
+        * First, get the "figure of merit" for this interface.
+        */
+       this_figure_of_merit = get_figure_of_merit(curdev);
+
+       /*
+        * Now look for the last interface with an figure of merit
+        * less than or equal to the new interface's figure of merit.
+        *
+        * We start with "prevdev" being NULL, meaning we're before
+        * the first element in the list.
+        */
+       prevdev = NULL;
+       for (;;) {
+               /*
+                * Get the interface after this one.
+                */
+               if (prevdev == NULL) {
+                       /*
+                        * The next element is the first element.
+                        */
+                       nextdev = *alldevs;
+               } else
+                       nextdev = prevdev->next;
+
+               /*
+                * Are we at the end of the list?
+                */
+               if (nextdev == NULL) {
+                       /*
+                        * Yes - we have to put the new entry after "prevdev".
+                        */
+                       break;
+               }
+
+               /*
+                * Is the new interface's figure of merit less
+                * than the next interface's figure of merit,
+                * meaning that the new interface is better
+                * than the next interface?
+                */
+               nextdev_figure_of_merit = get_figure_of_merit(nextdev);
+               if (this_figure_of_merit < nextdev_figure_of_merit) {
+                       /*
+                        * Yes - we should put the new entry
+                        * before "nextdev", i.e. after "prevdev".
+                        */
+                       break;
+               }
+
+               prevdev = nextdev;
+       }
+
+       /*
+        * Insert before "nextdev".
+        */
+       curdev->next = nextdev;
+
+       /*
+        * Insert after "prevdev" - unless "prevdev" is null,
+        * in which case this is the first interface.
+        */
+       if (prevdev == NULL) {
+               /*
+                * This is the first interface.  Pass back a
+                * pointer to it, and put "curdev" before
+                * "nextdev".
+                */
+               *alldevs = curdev;
+       } else
+               prevdev->next = curdev;
+       return (curdev);
+}
 
 /*
  * Free a list of interfaces.
index 4c190672cdacbe2f641352445d42dac8c0d15111..9677fb7c1ab3e0d6825dcd8ae79b2b68abdc86ba 100644 (file)
@@ -2678,11 +2678,10 @@ finddevs_usb(pcap_if_t **alldevsp, char *errbuf)
                memcpy(name, usbus_prefix, USBUS_PREFIX_LEN);
                memcpy(name + USBUS_PREFIX_LEN, usbitem->d_name, busnumlen);
                *(name + USBUS_PREFIX_LEN + busnumlen) = '\0';
-               err = pcap_add_if(alldevsp, name, PCAP_IF_UP, NULL, errbuf);
-               if (err != 0) {
+               if (add_dev(alldevsp, name, PCAP_IF_UP, NULL, errbuf) == NULL) {
                        free(name);
                        closedir(usbdir);
-                       return (err);
+                       return (PCAP_ERROR);
                }
        }
        free(name);
index 4452fe44e3796da9ceb02b5918abaa4a1ad344e2..2a36e2649c23c2bc1c7127030a7f69b39cd1f4e2 100644 (file)
@@ -119,8 +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 (pcap_add_if(alldevsp, dev_name, 0,
-                      dev_descr, err_str) < 0)
+               if (add_dev(alldevsp, dev_name, 0, dev_descr, err_str) == NULL)
                {
                        ret = -1;
                        break;
index 521d6d500d056994749343dabbd02897e3b21b08..3cd6178bf6a6a418d9726a1902b4a88d9643fbe3 100644 (file)
@@ -64,8 +64,8 @@ bt_monitor_findalldevs(pcap_if_t **alldevsp, char *err_str)
 {
     int         ret = 0;
 
-    if (pcap_add_if(alldevsp, INTERFACE_NAME, 0,
-               "Bluetooth Linux Monitor", err_str) < 0)
+    if (add_dev(alldevsp, INTERFACE_NAME, 0,
+                "Bluetooth Linux Monitor", err_str) == NULL)
     {
         ret = -1;
     }
index ea1c09592c21fe275d58cff4adbffd6525552087..ee024dfdbe11a6726d4e681c7ace4488259a793b 100644 (file)
@@ -1065,13 +1065,7 @@ dag_stats(pcap_t *p, struct pcap_stat *ps) {
 }
 
 /*
- * Previously we just generated a list of all possible names and let
- * pcap_add_if() attempt to open each one, but with streams this adds up
- * to 81 possibilities which is inefficient.
- *
- * Since we know more about the devices we can prune the tree here.
- * pcap_add_if() will still retest each device but the total number of
- * open attempts will still be much less than the naive approach.
+ * Add all DAG devices.
  */
 int
 dag_findalldevs(pcap_if_t **devlistp, char *errbuf)
@@ -1090,13 +1084,15 @@ dag_findalldevs(pcap_if_t **devlistp, char *errbuf)
                pcap_snprintf(name, 12, "dag%d", c);
                if (-1 == dag_parse_name(name, dagname, DAGNAME_BUFSIZE, &dagstream))
                {
+                       (void) pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                           "dag: device name %s can't be parsed", name);
                        return -1;
                }
-               description = NULL;
                if ( (dagfd = dag_open(dagname)) >= 0 ) {
+                       description = NULL;
                        if ((inf = dag_pciinfo(dagfd)))
                                description = dag_device_name(inf->device_code, 1);
-                       if (pcap_add_if(devlistp, name, 0, description, errbuf) == -1) {
+                       if (add_dev(devlistp, name, 0, description, errbuf) == NULL) {
                                /*
                                 * Failure.
                                 */
@@ -1111,7 +1107,7 @@ dag_findalldevs(pcap_if_t **devlistp, char *errbuf)
                                                dag_detach_stream(dagfd, stream);
 
                                                pcap_snprintf(name,  10, "dag%d:%d", c, stream);
-                                               if (pcap_add_if(devlistp, name, 0, description, errbuf) == -1) {
+                                               if (add_dev(devlistp, name, 0, description, errbuf) == NULL) {
                                                        /*
                                                         * Failure.
                                                         */
index 8e92093da11070a88c078353061fc8cebad4e15d..6408fe516433c55a80b1eb162dcc4115c17ae56f 100644 (file)
@@ -270,9 +270,9 @@ dbus_create(const char *device, char *ebuf, int *is_ours)
 int
 dbus_findalldevs(pcap_if_t **alldevsp, char *err_str)
 {
-       if (pcap_add_if(alldevsp, "dbus-system", 0, "D-Bus system bus", err_str) < 0)
+       if (add_dev(alldevsp, "dbus-system", 0, "D-Bus system bus", err_str) == NULL)
                return -1;
-       if (pcap_add_if(alldevsp, "dbus-session", 0, "D-Bus session bus", err_str) < 0)
+       if (add_dev(alldevsp, "dbus-session", 0, "D-Bus session bus", err_str) == NULL)
                return -1;
        return 0;
 }
index a431e7c1a3fa350111f66db70d13e168f9e27565..04251dd85c13ac3a13769930d519afaba84ddc89 100644 (file)
@@ -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 (pcap_add_if(alldevsp, baname, 0, NULL, errbuf) < 0)
+               if (add_dev(alldevsp, baname, 0, NULL, errbuf) == NULL)
                        return (-1);
        }
 #endif
index ea6b225a23efaa6bacb607223955262ce968cdcb..dbbfc78fdc02e42c5323422a41bc450d223f1c59 100644 (file)
@@ -573,16 +573,16 @@ 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 (pcap_add_if(&devlist, dev->name, dev->flags,
-                    dev->long_name, errbuf) < 0)
+    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_iflist(&devlist, dev->name, dev->flags, addr, addr_size,
-                           netmask, addr_size, broadaddr, addr_size,
-                           dstaddr, addr_size, errbuf) < 0)
+    if (add_addr_to_dev(curdev, addr, addr_size,
+                        netmask, addr_size, broadaddr, addr_size,
+                        dstaddr, addr_size, errbuf) < 0)
     {
       ret = -1;
       break;
index 7db7ff5e688d967e5aa14901de6eb3d69d78df8a..d48f61c91c0197161c6b1be7b610189751352ad8 100644 (file)
@@ -418,18 +418,18 @@ int       pcap_platform_finddevs(pcap_if_t **, char *);
 int    pcap_findalldevs_interfaces(pcap_if_t **, char *,
            int (*)(const char *));
 #endif
-int    add_addr_to_iflist(pcap_if_t **, const char *, bpf_u_int32,
-           struct sockaddr *, size_t, struct sockaddr *, size_t,
-           struct sockaddr *, size_t, struct sockaddr *, size_t, char *);
+pcap_if_t *find_or_add_dev(pcap_if_t **, const char *, bpf_u_int32,
+           const char *, char *);
+pcap_if_t *add_dev(pcap_if_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);
-int    pcap_add_if(pcap_if_t **, const char *, bpf_u_int32, const char *,
-           char *);
-int    add_or_find_if(pcap_if_t **, pcap_if_t **, const char *, bpf_u_int32,
-           const char *, char *);
 #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,
+           struct sockaddr *, size_t, struct sockaddr *, size_t,
+           struct sockaddr *, size_t, struct sockaddr *, size_t, char *);
 #endif
 
 /*
index 625f1e0a31ed5168000f626b8a68aa633d2e6e6f..933f2bdc039c3b2cf9873aeda24d5d03bf1e870c 100644 (file)
@@ -298,6 +298,13 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
 
        /* dlpi_walk() for loopback will be added here. */
 
+       /*
+        * Find all DLPI devices in the current zone.
+        *
+        * XXX - will pcap_findalldevs_interfaces() find any devices
+        * outside the current zone?  If not, the only reason to call
+        * it would be to get the interface addresses.
+        */
        dlpi_walk(list_interfaces, &lw, 0);
 
        if (lw.lw_err != 0) {
@@ -309,7 +316,11 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
 
        /* Add linkname if it does not exist on the list. */
        for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
-               if (pcap_add_if(alldevsp, entry->linkname, 0, NULL, errbuf) < 0)
+               /*
+                * 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)
                        retv = -1;
        }
 done:
index 924df42af06156b38aaa989a2febc1e25203ec47..6b56d58cbb93a126436902adac0d6ec7c09ead60 100644 (file)
@@ -2287,11 +2287,12 @@ add_linux_if(pcap_if_t **devlistp, const char *ifname, int fd, char *errbuf)
        }
 
        /*
-        * Add an entry for this interface, with no addresses.
+        * Add an entry for this interface, with no addresses, if it's
+        * not already in the list.
         */
-       if (pcap_add_if(devlistp, name,
+       if (find_or_add_dev(devlistp, name,
            if_flags_to_pcap_flags(name, ifrflags.ifr_flags), NULL,
-           errbuf) == -1) {
+           errbuf) == NULL) {
                /*
                 * Failure.
                 */
@@ -2564,8 +2565,8 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
        /*
         * Add the "any" device.
         */
-       if (pcap_add_if(alldevsp, "any", PCAP_IF_UP|PCAP_IF_RUNNING,
-           any_descr, errbuf) < 0)
+       if (add_dev(alldevsp, "any", PCAP_IF_UP|PCAP_IF_RUNNING,
+           any_descr, errbuf) == NULL)
                return (-1);
 
        return (0);
index bde648f3ea59b8e3e2e9aaf84542c0f36add59a7..446f5f089a4c037edaa76bd9bdb567024345895f 100644 (file)
@@ -650,9 +650,9 @@ netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str)
        }
        close(sock);
 
-       if (pcap_add_if(alldevsp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) < 0)
+       if (add_dev(alldevsp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) == NULL)
                return -1;
-       if (pcap_add_if(alldevsp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) < 0)
+       if (add_dev(alldevsp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) == NULL)
                return -1;
        return 0;
 }
index 88dc89df62dc485b05de1f7eb01d9cc5a1ebc7e0..e15b1d338cf2e9f1ea024138fdbb38eb2aadca78 100644 (file)
@@ -254,8 +254,9 @@ static int septel_stats(pcap_t *p, struct pcap_stat *ps) {
 int
 septel_findalldevs(pcap_if_t **devlistp, char *errbuf)
 {
-  return (pcap_add_if(devlistp,"septel",0,
-                      "Intel/Septel device",errbuf));
+  if (add_dev(devlistp,"septel",0,"Intel/Septel device",errbuf) == NULL)
+    return -1;
+  return 0;
 }
 
 
index fb1188a7d33c4940818d30b67f7c4cb53931758a..59b90bd5e65ee7c742381ba9fd4d3c0ee906d388 100644 (file)
@@ -147,8 +147,7 @@ usb_dev_add(pcap_if_t** alldevsp, int n, char *err_str)
        pcap_snprintf(dev_name, 10, USB_IFACE"%d", n);
        pcap_snprintf(dev_descr, 30, "USB bus number %d", n);
 
-       if (pcap_add_if(alldevsp, dev_name, 0,
-           dev_descr, err_str) < 0)
+       if (add_dev(alldevsp, dev_name, 0, dev_descr, err_str) == NULL)
                return -1;
        return 0;
 }
@@ -174,8 +173,8 @@ usb_findalldevs(pcap_if_t **alldevsp, char *err_str)
                 * Yes.
                 */
                close(fd);
-               if (pcap_add_if(alldevsp, "usbmon0", 0, "All USB buses",
-                   err_str) < 0)
+               if (add_dev(alldevsp, "usbmon0", 0, "All USB buses",
+                   err_str) == NULL)
                        return -1;
        } else {
                /*
@@ -193,8 +192,8 @@ usb_findalldevs(pcap_if_t **alldevsp, char *err_str)
                         * We found it.
                         */
                        close(fd);
-                       if (pcap_add_if(alldevsp, "usbmon0", 0, "All USB buses",
-                           err_str) < 0)
+                       if (add_dev(alldevsp, "usbmon0", 0, "All USB buses",
+                           err_str) == NULL)
                                return -1;
                }
        }
index d998637e6baa58621b8d6db7b6ea6deb3230ebe7..b64c8a629fa20f83538d2c2b4c41e18af0ab78ac 100644 (file)
@@ -1359,8 +1359,8 @@ pcap_add_if_win32(pcap_if_t **devlist, char *name, bpf_u_int32 flags,
        /*
         * Add an entry for this interface, with no addresses.
         */
-       if (add_or_find_if(&curdev, devlist, name, flags, description,
-           errbuf) == -1) {
+       curdev = add_dev(devlist, name, flags, description, errbuf);
+       if (curdev == NULL) {
                /*
                 * Failure.
                 */
@@ -1391,8 +1391,6 @@ pcap_add_if_win32(pcap_if_t **devlist, char *name, bpf_u_int32 flags,
                 * "curdev" is an entry for this interface; add an entry for
                 * this address to its list of addresses.
                 */
-               if(curdev == NULL)
-                       break;
                res = add_addr_to_dev(curdev,
                    (struct sockaddr *)&if_addrs[if_addr_size].IPAddress,
                    sizeof (struct sockaddr_storage),