]> The Tcpdump Group git mirrors - libpcap/blobdiff - nametoaddr.c
pcap_create_interface() needs the interface name on Linux.
[libpcap] / nametoaddr.c
index 5203e514e3fdd2e20887904479c41d6bdb26c6a7..71280b31c48d73cd46af38c7aee240e10ef60ad0 100644 (file)
  * These functions are not time critical.
  */
 
-#ifndef lint
-static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.83 2008-02-06 10:21:30 guy Exp $ (LBL)";
-#endif
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#ifdef WIN32
+#ifdef DECNETLIB
+#include <sys/types.h>
+#include <netdnet/dnetdb.h>
+#endif
+
+#ifdef _WIN32
 #include <pcap-stdinc.h>
 
-#else /* WIN32 */
+#ifdef INET6
+/*
+ * To quote the MSDN page for getaddrinfo() at
+ *
+ *    https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
+ *
+ * "Support for getaddrinfo on Windows 2000 and older versions
+ * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
+ * later. To execute an application that uses this function on earlier
+ * versions of Windows, then you need to include the Ws2tcpip.h and
+ * Wspiapi.h files. When the Wspiapi.h include file is added, the
+ * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
+ * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
+ * function is implemented in such a way that if the Ws2_32.dll or the
+ * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
+ * Preview for Windows 2000) does not include getaddrinfo, then a
+ * version of getaddrinfo is implemented inline based on code in the
+ * Wspiapi.h header file. This inline code will be used on older Windows
+ * platforms that do not natively support the getaddrinfo function."
+ *
+ * We use getaddrinfo(), so we include Wspiapi.h here.  pcap-stdinc.h
+ * includes Ws2tcpip.h, so we don't need to include it ourselves.
+ */
+#include <Wspiapi.h>
+#endif
+
+#else /* _WIN32 */
 
 #include <sys/param.h>
 #include <sys/types.h>                         /* concession to AIX */
@@ -42,16 +68,9 @@ static const char rcsid[] _U_ =
 #include <sys/time.h>
 
 #include <netinet/in.h>
-#endif /* WIN32 */
-
-/*
- * XXX - why was this included even on UNIX?
- */
-#ifdef __MINGW32__
-#include "IP6_misc.h"
-#endif
+#endif /* _WIN32 */
 
-#ifndef WIN32
+#ifndef _WIN32
 #ifdef HAVE_ETHER_HOSTTON
 /*
  * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
@@ -69,7 +88,7 @@ struct rtentry;               /* declarations in <net/if.h> */
 #endif /* HAVE_ETHER_HOSTTON */
 #include <arpa/inet.h>
 #include <netdb.h>
-#endif /* WIN32 */
+#endif /* _WIN32 */
 
 #include <ctype.h>
 #include <errno.h>
@@ -81,6 +100,7 @@ struct rtentry;              /* declarations in <net/if.h> */
 
 #include "gencode.h"
 #include <pcap/namedb.h>
+#include "nametoaddr.h"
 
 #ifdef HAVE_OS_PROTO_H
 #include "os-proto.h"
@@ -147,7 +167,7 @@ pcap_nametoaddrinfo(const char *name)
 bpf_u_int32
 pcap_nametonetaddr(const char *name)
 {
-#ifndef WIN32
+#ifndef _WIN32
        struct netent *np;
 
        if ((np = getnetbyname(name)) != NULL)
@@ -157,6 +177,15 @@ pcap_nametonetaddr(const char *name)
 #else
        /*
         * There's no "getnetbyname()" on Windows.
+        *
+        * XXX - I guess we could use the BSD code to read
+        * C:\Windows\System32\drivers\etc/networks, assuming
+        * that's its home on all the versions of Windows
+        * we use, but that file probably just has the loopback
+        * network on 127/24 on 99 44/100% of Windows machines.
+        *
+        * (Heck, these days it probably just has that on 99 44/100%
+        * of *UN*X* machines.)
         */
        return 0;
 #endif
@@ -249,6 +278,7 @@ pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
                        free(cpy);
                        return 0;
                }
+               free(cpy);
 
                if (*proto != save_proto)
                        *proto = PROTO_UNDEF;
@@ -280,8 +310,14 @@ struct eproto {
        u_short p;
 };
 
-/* Static data base of ether protocol types. */
-struct eproto eproto_db[] = {
+/*
+ * Static data base of ether protocol types.
+ * tcpdump used to import this, and it's declared as an export on
+ * Debian, at least, so make it a public symbol, even though we
+ * don't officially export it by declaring it in a header file.
+ * (Programs *should* do this themselves, as tcpdump now does.)
+ */
+PCAP_API_DEF struct eproto eproto_db[] = {
        { "pup", ETHERTYPE_PUP },
        { "xns", ETHERTYPE_NS },
        { "ip", ETHERTYPE_IP },
@@ -389,7 +425,7 @@ __pcap_atodn(const char *s, bpf_u_int32 *addr)
        u_int node, area;
 
        if (sscanf(s, "%d.%d", &area, &node) != 2)
-               bpf_error("malformed decnet address '%s'", s);
+               return(0);
 
        *addr = (area << AREASHIFT) & AREAMASK;
        *addr |= (node & NODEMASK);
@@ -416,6 +452,8 @@ pcap_ether_aton(const char *s)
        register u_int d;
 
        e = ep = (u_char *)malloc(6);
+       if (e == NULL)
+               return (NULL);
 
        while (*s) {
                if (*s == ':' || *s == '.' || *s == '-')
@@ -491,23 +529,20 @@ pcap_ether_hostton(const char *name)
 }
 #endif
 
-u_short
-__pcap_nametodnaddr(const char *name)
+int
+__pcap_nametodnaddr(const char *name, u_short *res)
 {
 #ifdef DECNETLIB
        struct nodeent *getnodebyname();
        struct nodeent *nep;
-       unsigned short res;
 
        nep = getnodebyname(name);
        if (nep == ((struct nodeent *)0))
-               bpf_error("unknown decnet host name '%s'\n", name);
+               return(0);
 
-       memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
-       return(res);
+       memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
+       return(1);
 #else
-       bpf_error("decnet name support not included, '%s' cannot be translated\n",
-               name);
        return(0);
 #endif
 }