]> The Tcpdump Group git mirrors - libpcap/commitdiff
Move the packet32.dll-based pcap_lookup{dev,net} to pcap-win32.c.
authorGuy Harris <[email protected]>
Thu, 7 Sep 2017 01:20:18 +0000 (18:20 -0700)
committerGuy Harris <[email protected]>
Thu, 7 Sep 2017 01:20:18 +0000 (18:20 -0700)
That way, inet.c has only the UN*X versions; pcap-dos.c already has the
MS-DOS version.

Build inet.c only on UN*X.

Remove tests for Windows and MS-DOS from inet.c, as it's now UN*X-only.

CMakeLists.txt
inet.c
msdos/makefile
msdos/makefile.dj
msdos/makefile.wc
pcap-win32.c

index 9299e7f3a699a02158cea4dc1ad72e6b689780ad..88d8fc3bde3efb4289c460348454bc37a4bc25e5 100644 (file)
@@ -188,7 +188,6 @@ set(PROJECT_SOURCE_LIST_C
     bpf_image.c
     etherent.c
     gencode.c
     bpf_image.c
     etherent.c
     gencode.c
-    inet.c
     nametoaddr.c
     optimize.c
     pcap-common.c
     nametoaddr.c
     optimize.c
     pcap-common.c
@@ -225,8 +224,15 @@ if( WIN32 )
     set( PCAP_TYPE win32 )
 else()
     #
     set( PCAP_TYPE win32 )
 else()
     #
-    # UN*X - figure out what type of packet capture mechanism we
-    # have.
+    # UN*X.
+    #
+    # We use the pcap_lookupdev() and pcap_lookupnet() from
+    # inet.c.
+    #
+    set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} inet.c)
+
+    #
+    # Figure out what type of packet capture mechanism we have.
     #
     if( EXISTS /dev/bpf )
        #
     #
     if( EXISTS /dev/bpf )
        #
diff --git a/inet.c b/inet.c
index b63d865577690713e9277a8f559a652a00125f86..72f0b6e7b522b2f0878ebde458bf43d4cfd9e83f 100644 (file)
--- a/inet.c
+++ b/inet.c
 #include <config.h>
 #endif
 
 #include <config.h>
 #endif
 
-#ifndef _WIN32
 #include <sys/param.h>
 #include <sys/param.h>
-#ifndef MSDOS
 #include <sys/file.h>
 #include <sys/file.h>
-#endif
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #ifdef HAVE_SYS_SOCKIO_H
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #ifdef HAVE_SYS_SOCKIO_H
@@ -51,16 +48,13 @@ struct mbuf;                /* Squelch compiler warnings on some platforms for */
 struct rtentry;                /* declarations in <net/if.h> */
 #include <net/if.h>
 #include <netinet/in.h>
 struct rtentry;                /* declarations in <net/if.h> */
 #include <net/if.h>
 #include <netinet/in.h>
-#endif /* _WIN32 */
 
 #include <errno.h>
 #include <memory.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <errno.h>
 #include <memory.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#if !defined(_WIN32) && !defined(__BORLANDC__)
 #include <unistd.h>
 #include <unistd.h>
-#endif /* !_WIN32 && !__BORLANDC__ */
 
 #include "pcap-int.h"
 
 
 #include "pcap-int.h"
 
@@ -68,9 +62,9 @@ struct rtentry;               /* declarations in <net/if.h> */
 #include "os-proto.h"
 #endif
 
 #include "os-proto.h"
 #endif
 
-#if !defined(_WIN32) && !defined(MSDOS)
-
 /*
 /*
+ * 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.
  * 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.
@@ -117,6 +111,11 @@ pcap_lookupdev(errbuf)
        return (ret);
 }
 
        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
+ * much work to get just one device's netmask.
+ */
 int
 pcap_lookupnet(device, netp, maskp, errbuf)
        register const char *device;
 int
 pcap_lookupnet(device, netp, maskp, errbuf)
        register const char *device;
@@ -213,192 +212,3 @@ pcap_lookupnet(device, netp, maskp, errbuf)
        *netp &= *maskp;
        return (0);
 }
        *netp &= *maskp;
        return (0);
 }
-
-#elif defined(_WIN32)
-
-/*
- * 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.
- *
- * In the best of all possible worlds, this would be the same as on
- * UN*X, but there may be software that expects this to return a
- * full list of devices after the first device.
- */
-#define ADAPTERSNAME_LEN       8192
-char *
-pcap_lookupdev(errbuf)
-       register char *errbuf;
-{
-       DWORD dwVersion;
-       DWORD dwWindowsMajorVersion;
-       char our_errbuf[PCAP_ERRBUF_SIZE+1];
-
-#pragma warning (push)
-#pragma warning (disable: 4996) /* disable MSVC's GetVersion() deprecated warning here */
-       dwVersion = GetVersion();       /* get the OS version */
-#pragma warning (pop)
-       dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
-
-       if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
-               /*
-                * Windows 95, 98, ME.
-                */
-               ULONG NameLength = ADAPTERSNAME_LEN;
-               static char AdaptersName[ADAPTERSNAME_LEN];
-
-               if (PacketGetAdapterNames(AdaptersName,&NameLength) )
-                       return (AdaptersName);
-               else
-                       return NULL;
-       } else {
-               /*
-                * Windows NT (NT 4.0 and later).
-                * Convert the names to Unicode for backward compatibility.
-                */
-               ULONG NameLength = ADAPTERSNAME_LEN;
-               static WCHAR AdaptersName[ADAPTERSNAME_LEN];
-               size_t BufferSpaceLeft;
-               char *tAstr;
-               WCHAR *Unameptr;
-               char *Adescptr;
-               size_t namelen, i;
-               WCHAR *TAdaptersName = (WCHAR*)malloc(ADAPTERSNAME_LEN * sizeof(WCHAR));
-               int NAdapts = 0;
-
-               if(TAdaptersName == NULL)
-               {
-                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
-                       return NULL;
-               }
-
-               if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
-               {
-                       pcap_win32_err_to_str(GetLastError(), our_errbuf);
-                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
-                               "PacketGetAdapterNames: %s", our_errbuf);
-                       free(TAdaptersName);
-                       return NULL;
-               }
-
-
-               BufferSpaceLeft = ADAPTERSNAME_LEN * sizeof(WCHAR);
-               tAstr = (char*)TAdaptersName;
-               Unameptr = AdaptersName;
-
-               /*
-                * Convert the device names to Unicode into AdapterName.
-                */
-               do {
-                       /*
-                        * Length of the name, including the terminating
-                        * NUL.
-                        */
-                       namelen = strlen(tAstr) + 1;
-
-                       /*
-                        * Do we have room for the name in the Unicode
-                        * buffer?
-                        */
-                       if (BufferSpaceLeft < namelen * sizeof(WCHAR)) {
-                               /*
-                                * No.
-                                */
-                               goto quit;
-                       }
-                       BufferSpaceLeft -= namelen * sizeof(WCHAR);
-
-                       /*
-                        * Copy the name, converting ASCII to Unicode.
-                        * namelen includes the NUL, so we copy it as
-                        * well.
-                        */
-                       for (i = 0; i < namelen; i++)
-                               *Unameptr++ = *tAstr++;
-
-                       /*
-                        * Count this adapter.
-                        */
-                       NAdapts++;
-               } while (namelen != 1);
-
-               /*
-                * Copy the descriptions, but don't convert them from
-                * ASCII to Unicode.
-                */
-               Adescptr = (char *)Unameptr;
-               while(NAdapts--)
-               {
-                       size_t desclen;
-
-                       desclen = strlen(tAstr) + 1;
-
-                       /*
-                        * Do we have room for the name in the Unicode
-                        * buffer?
-                        */
-                       if (BufferSpaceLeft < desclen) {
-                               /*
-                                * No.
-                                */
-                               goto quit;
-                       }
-
-                       /*
-                        * Just copy the ASCII string.
-                        * namelen includes the NUL, so we copy it as
-                        * well.
-                        */
-                       memcpy(Adescptr, tAstr, desclen);
-                       Adescptr += desclen;
-                       tAstr += desclen;
-                       BufferSpaceLeft -= desclen;
-               }
-
-       quit:
-               free(TAdaptersName);
-               return (char *)(AdaptersName);
-       }
-}
-
-
-int
-pcap_lookupnet(device, netp, maskp, errbuf)
-       register const char *device;
-       register bpf_u_int32 *netp, *maskp;
-       register char *errbuf;
-{
-       /*
-        * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
-        * in order to skip non IPv4 (i.e. IPv6 addresses)
-        */
-       npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
-       LONG if_addr_size = MAX_NETWORK_ADDRESSES;
-       struct sockaddr_in *t_addr;
-       LONG i;
-
-       if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
-               *netp = *maskp = 0;
-               return (0);
-       }
-
-       for(i = 0; i < if_addr_size; i++)
-       {
-               if(if_addrs[i].IPAddress.ss_family == AF_INET)
-               {
-                       t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
-                       *netp = t_addr->sin_addr.S_un.S_addr;
-                       t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
-                       *maskp = t_addr->sin_addr.S_un.S_addr;
-
-                       *netp &= *maskp;
-                       return (0);
-               }
-
-       }
-
-       *netp = *maskp = 0;
-       return (0);
-}
-
-#endif /* !_WIN32 && !MSDOS */
index 234a2763dbc9619146ead5fe29a5e30e6249aea6..599a619f402efb8f1d66e5b9e05beab21aea4390 100644 (file)
@@ -23,7 +23,7 @@ ASM    = tasm.exe -t -l -mx -m2 -DDEBUG
 
 SOURCE = grammar.c  scanner.c bpf_filt.c bpf_imag.c bpf_dump.c \
          etherent.c gencode.c nametoad.c pcap-dos.c optimize.c \
 
 SOURCE = grammar.c  scanner.c bpf_filt.c bpf_imag.c bpf_dump.c \
          etherent.c gencode.c nametoad.c pcap-dos.c optimize.c \
-         savefile.c pcap.c inet.c msdos\ndis2.c msdos\pktdrvr.c \
+         savefile.c pcap.c msdos\ndis2.c msdos\pktdrvr.c \
          missing\snprintf.c
 
 BORLAND_OBJ = $(SOURCE:.c=.obj) msdos\pkt_rx0.obj msdos\ndis_0.obj
          missing\snprintf.c
 
 BORLAND_OBJ = $(SOURCE:.c=.obj) msdos\pkt_rx0.obj msdos\ndis_0.obj
@@ -118,8 +118,6 @@ savefile.obj: savefile.c pcap-int.h pcap.h pcap-bpf.h
 
 pcap.obj: pcap.c pcap-dos.h pcap-int.h pcap.h pcap-bpf.h
 
 
 pcap.obj: pcap.c pcap-dos.h pcap-int.h pcap.h pcap-bpf.h
 
-inet.obj: inet.c pcap-int.h pcap.h pcap-bpf.h
-
 grammar.obj: grammar.c pcap-int.h pcap.h pcap-bpf.h gencode.h \
   pf.h pcap-namedb.h
 
 grammar.obj: grammar.c pcap-int.h pcap.h pcap-bpf.h gencode.h \
   pf.h pcap-namedb.h
 
@@ -158,8 +156,6 @@ savefile.o32: savefile.c pcap-int.h pcap.h pcap-bpf.h
 
 pcap.o32: pcap.c pcap-dos.h pcap-int.h pcap.h pcap-bpf.h
 
 
 pcap.o32: pcap.c pcap-dos.h pcap-int.h pcap.h pcap-bpf.h
 
-inet.o32: inet.c pcap-int.h pcap.h pcap-bpf.h
-
 grammar.o32: grammar.c pcap-int.h pcap.h pcap-bpf.h gencode.h \
   pf.h pcap-namedb.h
 
 grammar.o32: grammar.c pcap-int.h pcap.h pcap-bpf.h gencode.h \
   pf.h pcap-namedb.h
 
index 3f56da968600ceaf4fbc0fc863e70ffbae77480e..58933e779f80ea0bcd078f9b167970a9e045b09a 100644 (file)
@@ -24,7 +24,7 @@ CFLAGS += -Dyylval=pcap_lval # -DBDEBUG -DNDEBUG
 
 SOURCES = grammar.c scanner.c bpf/net/bpf_filter.c bpf_image.c bpf_dump.c \
           etherent.c gencode.c nametoaddr.c pcap-common.c pcap-dos.c optimize.c \
 
 SOURCES = grammar.c scanner.c bpf/net/bpf_filter.c bpf_image.c bpf_dump.c \
           etherent.c gencode.c nametoaddr.c pcap-common.c pcap-dos.c optimize.c \
-          savefile.c pcap.c sf-pcap.c sf-pcap-ng.c inet.c \
+          savefile.c pcap.c sf-pcap.c sf-pcap-ng.c \
           msdos/pktdrvr.c msdos/ndis2.c # missing/snprintf.c
 
 OBJECTS = $(addprefix $(OBJ_DIR)/, $(notdir $(SOURCES:.c=.o)))
           msdos/pktdrvr.c msdos/ndis2.c # missing/snprintf.c
 
 OBJECTS = $(addprefix $(OBJ_DIR)/, $(notdir $(SOURCES:.c=.o)))
index 61ba137820ee451add100ec260edf322e0fae3e6..0246012050336f25f397ccfb44483b8206aad60c 100644 (file)
@@ -28,7 +28,7 @@ OBJS = $(OBJDIR)\grammar.obj    $(OBJDIR)\scanner.obj  $(OBJDIR)\pcap.obj     &
        $(OBJDIR)\bpf_filter.obj $(OBJDIR)\bpf_imag.obj $(OBJDIR)\bpf_dump.obj &
        $(OBJDIR)\etherent.obj   $(OBJDIR)\gencode.obj  $(OBJDIR)\nametoad.obj &
        $(OBJDIR)\pcap-dos.obj   $(OBJDIR)\pktdrvr.obj  $(OBJDIR)\optimize.obj &
        $(OBJDIR)\bpf_filter.obj $(OBJDIR)\bpf_imag.obj $(OBJDIR)\bpf_dump.obj &
        $(OBJDIR)\etherent.obj   $(OBJDIR)\gencode.obj  $(OBJDIR)\nametoad.obj &
        $(OBJDIR)\pcap-dos.obj   $(OBJDIR)\pktdrvr.obj  $(OBJDIR)\optimize.obj &
-       $(OBJDIR)\savefile.obj   $(OBJDIR)\inet.obj     $(OBJDIR)\ndis2.obj
+       $(OBJDIR)\savefile.obj   $(OBJDIR)\ndis2.obj
 
 CFLAGS = $(DEFS) $(YYDEFS) -I. -I$(%watt_root)\inc -I.\msdos\pm_drvr &
          -$(MODEL) -mf -zff -zgf -zq -bt=dos -fr=nul -w6 -fpi        &
 
 CFLAGS = $(DEFS) $(YYDEFS) -I. -I$(%watt_root)\inc -I.\msdos\pm_drvr &
          -$(MODEL) -mf -zff -zgf -zq -bt=dos -fr=nul -w6 -fpi        &
@@ -108,8 +108,6 @@ $(OBJDIR)\savefile.obj: savefile.c pcap-int.h pcap.h pcap-bpf.h
 
 $(OBJDIR)\pcap.obj: pcap.c pcap-dos.h pcap-int.h pcap.h pcap-bpf.h
 
 
 $(OBJDIR)\pcap.obj: pcap.c pcap-dos.h pcap-int.h pcap.h pcap-bpf.h
 
-$(OBJDIR)\inet.obj: inet.c pcap-int.h pcap.h pcap-bpf.h
-
 $(OBJDIR)\grammar.obj: grammar.c pcap-int.h pcap.h pcap-bpf.h gencode.h &
   pcap-namedb.h
 
 $(OBJDIR)\grammar.obj: grammar.c pcap-int.h pcap.h pcap-bpf.h gencode.h &
   pcap-namedb.h
 
index b81aae825753de91a5b8789e8f3b9d259f6f17ad..4b3a18d3893dad783ce2573abd00317479617f4d 100644 (file)
@@ -1518,3 +1518,195 @@ pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
        free(AdaptersName);
        return (ret);
 }
        free(AdaptersName);
        return (ret);
 }
+
+/*
+ * 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.
+ *
+ * In the best of all possible worlds, this would be the same as on
+ * UN*X, but there may be software that expects this to return a
+ * full list of devices after the first device.
+ */
+#define ADAPTERSNAME_LEN       8192
+char *
+pcap_lookupdev(errbuf)
+       register char *errbuf;
+{
+       DWORD dwVersion;
+       DWORD dwWindowsMajorVersion;
+       char our_errbuf[PCAP_ERRBUF_SIZE+1];
+
+#pragma warning (push)
+#pragma warning (disable: 4996) /* disable MSVC's GetVersion() deprecated warning here */
+       dwVersion = GetVersion();       /* get the OS version */
+#pragma warning (pop)
+       dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
+
+       if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
+               /*
+                * Windows 95, 98, ME.
+                */
+               ULONG NameLength = ADAPTERSNAME_LEN;
+               static char AdaptersName[ADAPTERSNAME_LEN];
+
+               if (PacketGetAdapterNames(AdaptersName,&NameLength) )
+                       return (AdaptersName);
+               else
+                       return NULL;
+       } else {
+               /*
+                * Windows NT (NT 4.0 and later).
+                * Convert the names to Unicode for backward compatibility.
+                */
+               ULONG NameLength = ADAPTERSNAME_LEN;
+               static WCHAR AdaptersName[ADAPTERSNAME_LEN];
+               size_t BufferSpaceLeft;
+               char *tAstr;
+               WCHAR *Unameptr;
+               char *Adescptr;
+               size_t namelen, i;
+               WCHAR *TAdaptersName = (WCHAR*)malloc(ADAPTERSNAME_LEN * sizeof(WCHAR));
+               int NAdapts = 0;
+
+               if(TAdaptersName == NULL)
+               {
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
+                       return NULL;
+               }
+
+               if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
+               {
+                       pcap_win32_err_to_str(GetLastError(), our_errbuf);
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                               "PacketGetAdapterNames: %s", our_errbuf);
+                       free(TAdaptersName);
+                       return NULL;
+               }
+
+
+               BufferSpaceLeft = ADAPTERSNAME_LEN * sizeof(WCHAR);
+               tAstr = (char*)TAdaptersName;
+               Unameptr = AdaptersName;
+
+               /*
+                * Convert the device names to Unicode into AdapterName.
+                */
+               do {
+                       /*
+                        * Length of the name, including the terminating
+                        * NUL.
+                        */
+                       namelen = strlen(tAstr) + 1;
+
+                       /*
+                        * Do we have room for the name in the Unicode
+                        * buffer?
+                        */
+                       if (BufferSpaceLeft < namelen * sizeof(WCHAR)) {
+                               /*
+                                * No.
+                                */
+                               goto quit;
+                       }
+                       BufferSpaceLeft -= namelen * sizeof(WCHAR);
+
+                       /*
+                        * Copy the name, converting ASCII to Unicode.
+                        * namelen includes the NUL, so we copy it as
+                        * well.
+                        */
+                       for (i = 0; i < namelen; i++)
+                               *Unameptr++ = *tAstr++;
+
+                       /*
+                        * Count this adapter.
+                        */
+                       NAdapts++;
+               } while (namelen != 1);
+
+               /*
+                * Copy the descriptions, but don't convert them from
+                * ASCII to Unicode.
+                */
+               Adescptr = (char *)Unameptr;
+               while(NAdapts--)
+               {
+                       size_t desclen;
+
+                       desclen = strlen(tAstr) + 1;
+
+                       /*
+                        * Do we have room for the name in the Unicode
+                        * buffer?
+                        */
+                       if (BufferSpaceLeft < desclen) {
+                               /*
+                                * No.
+                                */
+                               goto quit;
+                       }
+
+                       /*
+                        * Just copy the ASCII string.
+                        * namelen includes the NUL, so we copy it as
+                        * well.
+                        */
+                       memcpy(Adescptr, tAstr, desclen);
+                       Adescptr += desclen;
+                       tAstr += desclen;
+                       BufferSpaceLeft -= desclen;
+               }
+
+       quit:
+               free(TAdaptersName);
+               return (char *)(AdaptersName);
+       }
+}
+
+/*
+ * We can't use the same code that we use on UN*X, as that's doing
+ * UN*X-specific calls.
+ *
+ * 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
+ * much work to get just one device's netmask.
+ */
+int
+pcap_lookupnet(device, netp, maskp, errbuf)
+       register const char *device;
+       register bpf_u_int32 *netp, *maskp;
+       register char *errbuf;
+{
+       /*
+        * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
+        * in order to skip non IPv4 (i.e. IPv6 addresses)
+        */
+       npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
+       LONG if_addr_size = MAX_NETWORK_ADDRESSES;
+       struct sockaddr_in *t_addr;
+       LONG i;
+
+       if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
+               *netp = *maskp = 0;
+               return (0);
+       }
+
+       for(i = 0; i < if_addr_size; i++)
+       {
+               if(if_addrs[i].IPAddress.ss_family == AF_INET)
+               {
+                       t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
+                       *netp = t_addr->sin_addr.S_un.S_addr;
+                       t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
+                       *maskp = t_addr->sin_addr.S_un.S_addr;
+
+                       *netp &= *maskp;
+                       return (0);
+               }
+
+       }
+
+       *netp = *maskp = 0;
+       return (0);
+}