]> The Tcpdump Group git mirrors - libpcap/blobdiff - inet.c
Have routines to set various internal debugging flags.
[libpcap] / inet.c
diff --git a/inet.c b/inet.c
index fad7e25bcab1c9fb025b54ce555eb34bcfeb3e51..bd59a525dfd790500dffe39ff86b7b6f7be0f24f 100644 (file)
--- a/inet.c
+++ b/inet.c
@@ -36,9 +36,9 @@
 #include "config.h"
 #endif
 
-#ifdef WIN32
+#ifdef _WIN32
 #include <pcap-stdinc.h>
-#else /* WIN32 */
+#else /* _WIN32 */
 
 #include <sys/param.h>
 #ifndef MSDOS
@@ -54,7 +54,7 @@ struct mbuf;          /* Squelch compiler warnings on some platforms for */
 struct rtentry;                /* declarations in <net/if.h> */
 #include <net/if.h>
 #include <netinet/in.h>
-#endif /* WIN32 */
+#endif /* _WIN32 */
 
 #include <ctype.h>
 #include <errno.h>
@@ -62,9 +62,9 @@ struct rtentry;               /* declarations in <net/if.h> */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#if !defined(WIN32) && !defined(__BORLANDC__)
+#if !defined(_WIN32) && !defined(__BORLANDC__)
 #include <unistd.h>
-#endif /* !WIN32 && !__BORLANDC__ */
+#endif /* !_WIN32 && !__BORLANDC__ */
 #ifdef HAVE_LIMITS_H
 #include <limits.h>
 #else
@@ -239,7 +239,7 @@ add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
                        en_name_len = strlen(name) - 1;
                        en_name = malloc(en_name_len + 1);
                        if (en_name == NULL) {
-                               (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                               (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                                    "malloc: %s", pcap_strerror(errno));
                                return (-1);
                        }
@@ -315,7 +315,7 @@ add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
                 */
                curdev = malloc(sizeof(pcap_if_t));
                if (curdev == NULL) {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "malloc: %s", pcap_strerror(errno));
                        return (-1);
                }
@@ -326,7 +326,7 @@ add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
                curdev->next = NULL;
                curdev->name = strdup(name);
                if (curdev->name == NULL) {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "malloc: %s", pcap_strerror(errno));
                        free(curdev);
                        return (-1);
@@ -337,7 +337,7 @@ add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
                         */
                        curdev->description = strdup(description);
                        if (curdev->description == NULL) {
-                               (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                               (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                                    "malloc: %s", pcap_strerror(errno));
                                free(curdev->name);
                                free(curdev);
@@ -572,7 +572,8 @@ get_if_description(const char *name)
  * 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, add the specified address to it and return 0.
+ * 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:
  *
@@ -583,9 +584,17 @@ get_if_description(const char *name)
  *
  *     Otherwise, attempt to add an entry for it, with the specified
  *     ifnet flags and description, and, if that succeeds, add the
- *     specified address to it, set *curdev_ret to point to the new
- *     entry, and return 0, otherwise return PCAP_ERROR and set errbuf
- *     to an error message.
+ *     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.
+ *
+ * (We can get called with a null address because we might get a list
+ * of interface name/address combinations from the underlying OS, with
+ * the address being absent in some cases, rather than a list of
+ * interfaces with each interface having a list of addresses, so this
+ * call may be the only call made to add to the list, and we want to
+ * add interfaces even if they have no addresses.)
  */
 int
 add_addr_to_iflist(pcap_if_t **alldevs, const char *name, u_int flags,
@@ -616,14 +625,24 @@ add_addr_to_iflist(pcap_if_t **alldevs, const char *name, u_int flags,
                return (0);
        }
 
+       if (addr == NULL) {
+               /*
+                * There's no address to add; this entry just meant
+                * "here's a new interface".
+                */
+               return (0);
+       }
+
        /*
-        * "curdev" is an entry for this interface; add an entry for this
-        * address to its list of addresses.
+        * "curdev" is an entry for this interface, and we have an
+        * address for it; add an entry for that address to the
+        * interface's list of addresses.
         *
         * Allocate the new entry and fill it in.
         */
-       return (add_addr_to_dev(curdev, addr, addr_size, netmask, netmask_size,
-           broadaddr, broadaddr_size, dstaddr, dstaddr_size, errbuf));
+       return (add_addr_to_dev(curdev, addr, addr_size, netmask,
+           netmask_size, broadaddr, broadaddr_size, dstaddr,
+           dstaddr_size, errbuf));
 }
 
 /*
@@ -644,7 +663,7 @@ add_addr_to_dev(pcap_if_t *curdev,
 
        curaddr = malloc(sizeof(pcap_addr_t));
        if (curaddr == NULL) {
-               (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+               (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "malloc: %s", pcap_strerror(errno));
                return (-1);
        }
@@ -653,7 +672,7 @@ add_addr_to_dev(pcap_if_t *curdev,
        if (addr != NULL) {
                curaddr->addr = dup_sockaddr(addr, addr_size);
                if (curaddr->addr == NULL) {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "malloc: %s", pcap_strerror(errno));
                        free(curaddr);
                        return (-1);
@@ -664,7 +683,7 @@ add_addr_to_dev(pcap_if_t *curdev,
        if (netmask != NULL) {
                curaddr->netmask = dup_sockaddr(netmask, netmask_size);
                if (curaddr->netmask == NULL) {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "malloc: %s", pcap_strerror(errno));
                        if (curaddr->addr != NULL)
                                free(curaddr->addr);
@@ -677,7 +696,7 @@ add_addr_to_dev(pcap_if_t *curdev,
        if (broadaddr != NULL) {
                curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size);
                if (curaddr->broadaddr == NULL) {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "malloc: %s", pcap_strerror(errno));
                        if (curaddr->netmask != NULL)
                                free(curaddr->netmask);
@@ -692,7 +711,7 @@ add_addr_to_dev(pcap_if_t *curdev,
        if (dstaddr != NULL) {
                curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size);
                if (curaddr->dstaddr == NULL) {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "malloc: %s", pcap_strerror(errno));
                        if (curaddr->broadaddr != NULL)
                                free(curaddr->broadaddr);
@@ -809,7 +828,7 @@ pcap_freealldevs(pcap_if_t *alldevs)
        }
 }
 
-#if !defined(WIN32) && !defined(MSDOS)
+#if !defined(_WIN32) && !defined(MSDOS)
 
 /*
  * Return the name of a network interface attached to the system, or NULL
@@ -896,7 +915,7 @@ pcap_lookupnet(device, netp, maskp, errbuf)
 
        fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (fd < 0) {
-               (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
+               (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
                    pcap_strerror(errno));
                return (-1);
        }
@@ -908,10 +927,10 @@ pcap_lookupnet(device, netp, maskp, errbuf)
        (void)strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
        if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
                if (errno == EADDRNOTAVAIL) {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "%s: no IPv4 address assigned", device);
                } else {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "SIOCGIFADDR: %s: %s",
                            device, pcap_strerror(errno));
                }
@@ -927,7 +946,7 @@ pcap_lookupnet(device, netp, maskp, errbuf)
 #endif
        (void)strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
        if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
-               (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+               (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
                (void)close(fd);
                return (-1);
@@ -942,7 +961,7 @@ pcap_lookupnet(device, netp, maskp, errbuf)
                else if (IN_CLASSC(*netp))
                        *maskp = IN_CLASSC_NET;
                else {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
                            "inet class for 0x%x unknown", *netp);
                        return (-1);
                }
@@ -951,12 +970,16 @@ pcap_lookupnet(device, netp, maskp, errbuf)
        return (0);
 }
 
-#elif defined(WIN32)
+#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.
  */
 char *
 pcap_lookupdev(errbuf)
@@ -964,6 +987,8 @@ pcap_lookupdev(errbuf)
 {
        DWORD dwVersion;
        DWORD dwWindowsMajorVersion;
+       char our_errbuf[PCAP_ERRBUF_SIZE+1];
+
        dwVersion = GetVersion();       /* get the OS version */
        dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
 
@@ -991,15 +1016,15 @@ pcap_lookupdev(errbuf)
 
                if(TAdaptersName == NULL)
                {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
                        return NULL;
                }
 
                if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
                {
-                       (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
-                               "PacketGetAdapterNames: %s",
-                               pcap_win32strerror());
+                       pcap_win32_err_to_str(GetLastError(), our_errbuf);
+                       (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                               "PacketGetAdapterNames: %s", our_errbuf);
                        free(TAdaptersName);
                        return NULL;
                }
@@ -1079,4 +1104,4 @@ pcap_lookupnet(device, netp, maskp, errbuf)
        return (0);
 }
 
-#endif /* !WIN32 && !MSDOS */
+#endif /* !_WIN32 && !MSDOS */