]> The Tcpdump Group git mirrors - libpcap/commitdiff
Move the pcap_lookupdev() implemented atop pcap_findalldevs() to pcap.c.
authorGuy Harris <[email protected]>
Thu, 7 Sep 2017 19:58:02 +0000 (12:58 -0700)
committerGuy Harris <[email protected]>
Thu, 7 Sep 2017 19:58:02 +0000 (12:58 -0700)
The only platforms where it's not used are 1) Windows, where
pcap_lookupdev() returned more than just the preferred device, it also
returned names of other devices, following the null-terminated preferred
device, and 2) MS-DOS.

In the future, other non-UN*X platforms may use it as well.

inet.c
pcap.c

diff --git a/inet.c b/inet.c
index 72f0b6e7b522b2f0878ebde458bf43d4cfd9e83f..cb9ca4fcc393af6df54c1d343293d225e58da630 100644 (file)
--- a/inet.c
+++ b/inet.c
@@ -64,53 +64,7 @@ struct rtentry;              /* declarations in <net/if.h> */
 
 /*
  * UN*X.
- *
- * Return the name of a network interface attached to the system, or NULL
- * if none can be found.  The interface must be configured up; the
- * lowest unit number is preferred; loopback is ignored.
  */
-char *
-pcap_lookupdev(errbuf)
-       register char *errbuf;
-{
-       pcap_if_t *alldevs;
-/* for old BSD systems, including bsdi3 */
-#ifndef IF_NAMESIZE
-#define IF_NAMESIZE IFNAMSIZ
-#endif
-       static char device[IF_NAMESIZE + 1];
-       char *ret;
-
-       if (pcap_findalldevs(&alldevs, errbuf) == -1)
-               return (NULL);
-
-       if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
-               /*
-                * There are no devices on the list, or the first device
-                * on the list is a loopback device, which means there
-                * are no non-loopback devices on the list.  This means
-                * we can't return any device.
-                *
-                * XXX - why not return a loopback device?  If we can't
-                * capture on it, it won't be on the list, and if it's
-                * on the list, there aren't any non-loopback devices,
-                * so why not just supply it as the default device?
-                */
-               (void)strlcpy(errbuf, "no suitable device found",
-                   PCAP_ERRBUF_SIZE);
-               ret = NULL;
-       } else {
-               /*
-                * Return the name of the first device on the list.
-                */
-               (void)strlcpy(device, alldevs->name, sizeof(device));
-               ret = device;
-       }
-
-       pcap_freealldevs(alldevs);
-       return (ret);
-}
-
 /*
  * We don't just fetch the entire list of devices, search for the
  * particular device, and use its first IPv4 address, as that's too
diff --git a/pcap.c b/pcap.c
index c4b5a98e2b2e760a3ab8b76a0360a7631c8290ba..54296e30b5dc2eb1de02fb2264f56a5394c1d28f 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -1105,6 +1105,63 @@ pcap_freealldevs(pcap_if_t *alldevs)
        }
 }
 
+/*
+ * Windows has its own pcap_lookupdev(), for compatibility reasons, as
+ * it actually returns the names of all interfaces, with a NUL separator
+ * between them; some callers may depend on that.
+ *
+ * MS-DOS has its own pcap_lookupdev(), but that might be useful only
+ * as an optimization.
+ */
+#if !defined(_WIN32) && !defined(MSDOS)
+/*
+ * Return the name of a network interface attached to the system, or NULL
+ * if none can be found.  The interface must be configured up; the
+ * lowest unit number is preferred; loopback is ignored.
+ */
+char *
+pcap_lookupdev(errbuf)
+       register char *errbuf;
+{
+       pcap_if_t *alldevs;
+/* for old BSD systems, including bsdi3 */
+#ifndef IF_NAMESIZE
+#define IF_NAMESIZE IFNAMSIZ
+#endif
+       static char device[IF_NAMESIZE + 1];
+       char *ret;
+
+       if (pcap_findalldevs(&alldevs, errbuf) == -1)
+               return (NULL);
+
+       if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
+               /*
+                * There are no devices on the list, or the first device
+                * on the list is a loopback device, which means there
+                * are no non-loopback devices on the list.  This means
+                * we can't return any device.
+                *
+                * XXX - why not return a loopback device?  If we can't
+                * capture on it, it won't be on the list, and if it's
+                * on the list, there aren't any non-loopback devices,
+                * so why not just supply it as the default device?
+                */
+               (void)strlcpy(errbuf, "no suitable device found",
+                   PCAP_ERRBUF_SIZE);
+               ret = NULL;
+       } else {
+               /*
+                * Return the name of the first device on the list.
+                */
+               (void)strlcpy(device, alldevs->name, sizeof(device));
+               ret = device;
+       }
+
+       pcap_freealldevs(alldevs);
+       return (ret);
+}
+#endif
+
 #ifdef HAVE_REMOTE
 #include "pcap-rpcap.h"