]> The Tcpdump Group git mirrors - tcpdump/blobdiff - addrtoname.c
CI: Add warning exemptions for Sun C (suncc-5.15) on Solaris 10
[tcpdump] / addrtoname.c
index 33b93784b231da3e8e9d36721a646222ece93fce..43bfc47d82a28be0c4bbbe565949f88addfdfbe5 100644 (file)
@@ -22,9 +22,7 @@
  *  and address to string conversion routines
  */
 
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#endif
 
 #ifdef HAVE_CASPER
 #include <libcasper.h>
 
 #include "netdissect-stdinc.h"
 
-#ifdef _WIN32
-  /*
-   * We have our own ether_ntohost(), reading from the system's
-   * Ethernet address file.
-   */
-  #include "missing/win_ether_ntohost.h"
-#else
-  #ifdef USE_ETHER_NTOHOST
-    #if defined(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
-      /*
-       * OK, just include <net/ethernet.h>.
-       */
-      #include <net/ethernet.h>
-    #elif defined(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
-      /*
-       * OK, just include <netinet/ether.h>
-       */
-      #include <netinet/ether.h>
-    #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
-      /*
-       * OK, just include <sys/ethernet.h>
-       */
-      #include <sys/ethernet.h>
-    #elif defined(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
-      /*
-       * OK, just include <arpa/inet.h>
-       */
-      #include <arpa/inet.h>
-    #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
-      /*
-       * OK, include <netinet/if_ether.h>, after all the other stuff we
-       * need to include or define for its benefit.
-       */
+#ifdef USE_ETHER_NTOHOST
+  #if defined(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
+    /*
+     * OK, just include <net/ethernet.h>.
+     */
+    #include <net/ethernet.h>
+  #elif defined(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
+    /*
+     * OK, just include <netinet/ether.h>
+     */
+    #include <netinet/ether.h>
+  #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
+    /*
+     * OK, just include <sys/ethernet.h>
+     */
+    #include <sys/ethernet.h>
+  #elif defined(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
+    /*
+     * OK, just include <arpa/inet.h>
+     */
+    #include <arpa/inet.h>
+  #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
+    /*
+     * OK, include <netinet/if_ether.h>, after all the other stuff we
+     * need to include or define for its benefit.
+     */
+    #define NEED_NETINET_IF_ETHER_H
+  #else
+    /*
+     * We'll have to declare it ourselves.
+     * If <netinet/if_ether.h> defines struct ether_addr, include
+     * it.  Otherwise, define it ourselves.
+     */
+    #ifdef HAVE_STRUCT_ETHER_ADDR
       #define NEED_NETINET_IF_ETHER_H
-    #else
-      /*
-       * We'll have to declare it ourselves.
-       * If <netinet/if_ether.h> defines struct ether_addr, include
-       * it.  Otherwise, define it ourselves.
-       */
-      #ifdef HAVE_STRUCT_ETHER_ADDR
-        #define NEED_NETINET_IF_ETHER_H
-      #else /* HAVE_STRUCT_ETHER_ADDR */
+    #else /* HAVE_STRUCT_ETHER_ADDR */
        struct ether_addr {
-               unsigned char ether_addr_octet[MAC_ADDR_LEN];
+               /* Beware FreeBSD calls this "octet". */
+               unsigned char ether_addr_octet[MAC48_LEN];
        };
-      #endif /* HAVE_STRUCT_ETHER_ADDR */
-    #endif /* what declares ether_ntohost() */
-
-    #ifdef NEED_NETINET_IF_ETHER_H
-      #include <net/if.h>      /* Needed on some platforms */
-      #include <netinet/in.h>  /* Needed on some platforms */
-      #include <netinet/if_ether.h>
-    #endif /* NEED_NETINET_IF_ETHER_H */
-
-    #ifndef HAVE_DECL_ETHER_NTOHOST
-      /*
-       * No header declares it, so declare it ourselves.
-       */
-      extern int ether_ntohost(char *, const struct ether_addr *);
-    #endif /* !defined(HAVE_DECL_ETHER_NTOHOST) */
-  #endif /* USE_ETHER_NTOHOST */
-#endif /* _WIN32 */
+    #endif /* HAVE_STRUCT_ETHER_ADDR */
+  #endif /* what declares ether_ntohost() */
+
+  #ifdef NEED_NETINET_IF_ETHER_H
+    #include <net/if.h>                /* Needed on some platforms */
+    #include <netinet/in.h>    /* Needed on some platforms */
+    #include <netinet/if_ether.h>
+  #endif /* NEED_NETINET_IF_ETHER_H */
+
+  #ifndef HAVE_DECL_ETHER_NTOHOST
+    /*
+     * No header declares it, so declare it ourselves.
+     */
+    extern int ether_ntohost(char *, const struct ether_addr *);
+  #endif /* !defined(HAVE_DECL_ETHER_NTOHOST) */
+#endif /* USE_ETHER_NTOHOST */
 
 #include <pcap.h>
 #include <pcap-namedb.h>
@@ -150,14 +141,15 @@ win32_gethostbyaddr(const char *addr, int len, int type)
        static struct hostent host;
        static char hostbuf[NI_MAXHOST];
        char hname[NI_MAXHOST];
-       struct sockaddr_in6 addr6;
 
        host.h_name = hostbuf;
        switch (type) {
        case AF_INET:
                return gethostbyaddr(addr, len, type);
                break;
-       case AF_INET6:
+#ifdef AF_INET6
+       case AF_INET6: {
+               struct sockaddr_in6 addr6;
                memset(&addr6, 0, sizeof(addr6));
                addr6.sin6_family = AF_INET6;
                memcpy(&addr6.sin6_addr, addr, len);
@@ -169,6 +161,8 @@ win32_gethostbyaddr(const char *addr, int len, int type)
                        return &host;
                }
                break;
+       }
+#endif /* AF_INET6 */
        default:
                return NULL;
        }
@@ -253,7 +247,7 @@ intoa(uint32_t addr)
 static uint32_t f_netmask;
 static uint32_t f_localnet;
 #ifdef HAVE_CASPER
-extern cap_channel_t *capdns;
+cap_channel_t *capdns;
 #endif
 
 /*
@@ -296,6 +290,7 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
         *      (2) Address is foreign and -f was given. (If -f was not
         *          given, f_netmask and f_localnet are 0 and the test
         *          evaluates to true)
+        * Both addr and f_netmask and f_localnet are in network byte order.
         */
        if (!ndo->ndo_nflag &&
            (addr & f_netmask) == f_localnet) {
@@ -360,6 +355,7 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
        /*
         * Do not print names if -n was given.
         */
+#ifdef AF_INET6
        if (!ndo->ndo_nflag) {
 #ifdef HAVE_CASPER
                if (capdns != NULL) {
@@ -385,6 +381,7 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
                        return (p->name);
                }
        }
+#endif /* AF_INET6 */
        cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
        p->name = strdup(cp);
        if (p->name == NULL)
@@ -528,8 +525,7 @@ lookup_nsap(netdissect_options *ndo, const u_char *nsap,
                k = (ensap[0] << 8) | ensap[1];
                j = (ensap[2] << 8) | ensap[3];
                i = (ensap[4] << 8) | ensap[5];
-       }
-       else
+       } else
                i = j = k = 0;
 
        tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
@@ -587,7 +583,7 @@ lookup_protoid(netdissect_options *ndo, const u_char *pi)
 }
 
 const char *
-etheraddr_string(netdissect_options *ndo, const uint8_t *ep)
+mac48_string(netdissect_options *ndo, const uint8_t *ep)
 {
        int i;
        char *cp;
@@ -601,8 +597,15 @@ etheraddr_string(netdissect_options *ndo, const uint8_t *ep)
 #ifdef USE_ETHER_NTOHOST
        if (!ndo->ndo_nflag) {
                char buf2[BUFSIZE];
+               /*
+                * This is a non-const copy of ep for ether_ntohost(), which
+                * has its second argument non-const in OpenBSD. Also saves a
+                * type cast.
+                */
+               struct ether_addr ea;
 
-               if (ether_ntohost(buf2, (const struct ether_addr *)ep) == 0) {
+               memcpy (&ea, ep, MAC48_LEN);
+               if (ether_ntohost(buf2, &ea) == 0) {
                        tp->e_name = strdup(buf2);
                        if (tp->e_name == NULL)
                                (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
@@ -632,7 +635,16 @@ etheraddr_string(netdissect_options *ndo, const uint8_t *ep)
 }
 
 const char *
-le64addr_string(netdissect_options *ndo, const uint8_t *ep)
+eui64_string(netdissect_options *ndo, const uint8_t *ep)
+{
+       return (linkaddr_string(ndo, ep, LINKADDR_EUI64, EUI64_LEN));
+}
+
+/*
+ * EUI-64 with the rightmost octet first.
+ */
+const char *
+eui64le_string(netdissect_options *ndo, const uint8_t *ep)
 {
        const unsigned int len = 8;
        u_int i;
@@ -672,8 +684,8 @@ linkaddr_string(netdissect_options *ndo, const uint8_t *ep,
        if (len == 0)
                return ("<empty>");
 
-       if (type == LINKADDR_ETHER && len == MAC_ADDR_LEN)
-               return (etheraddr_string(ndo, ep));
+       if (type == LINKADDR_MAC48 && len == MAC48_LEN)
+               return (mac48_string(ndo, ep));
 
        if (type == LINKADDR_FRELAY)
                return (q922_string(ndo, ep, len));
@@ -920,7 +932,7 @@ init_protoidarray(netdissect_options *ndo)
 }
 
 static const struct etherlist {
-       const nd_mac_addr addr;
+       const nd_mac48 addr;
        const char *name;
 } etherlist[] = {
        {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
@@ -931,7 +943,7 @@ static const struct etherlist {
  * Initialize the ethers hash table.  We take two different approaches
  * depending on whether or not the system provides the ethers name
  * service.  If it does, we just wire in a few names at startup,
- * and etheraddr_string() fills in the table on demand.  If it doesn't,
+ * and mac48_string() fills in the table on demand.  If it doesn't,
  * then we suck in the entire /etc/ethers file at startup.  The idea
  * is that parsing the local file will be fast, but spinning through
  * all the ethers entries via NIS & next_etherent might be very slow.
@@ -977,7 +989,10 @@ init_etherarray(netdissect_options *ndo)
                /*
                 * Use YP/NIS version of name if available.
                 */
-               if (ether_ntohost(name, (const struct ether_addr *)el->addr) == 0) {
+               /* Same workaround as in mac48_string(). */
+               struct ether_addr ea;
+               memcpy (&ea, el->addr, MAC48_LEN);
+               if (ether_ntohost(name, &ea) == 0) {
                        tp->e_name = strdup(name);
                        if (tp->e_name == NULL)
                                (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
@@ -1108,7 +1123,7 @@ static const struct ipxsap_ent {
        { 0x030a, "GalacticommWorldgroupServer" },
        { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
        { 0x0320, "AttachmateGateway" },
-       { 0x0327, "MicrosoftDiagnostiocs" },
+       { 0x0327, "MicrosoftDiagnostics" },
        { 0x0328, "WATCOM SQL Server" },
        { 0x0335, "MultiTechSystems MultisynchCommServer" },
        { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
@@ -1230,7 +1245,7 @@ init_ipxsaparray(netdissect_options *ndo)
  * Initialize the address to name translation machinery.  We map all
  * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
  * (i.e., to prevent blocking on the nameserver).  localnet is the IP address
- * of the local network.  mask is its subnet mask.
+ * of the local network, mask is its subnet mask, both in network byte order.
  */
 void
 init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
@@ -1277,7 +1292,7 @@ newhnamemem(netdissect_options *ndo)
        static struct hnamemem *ptr = NULL;
        static u_int num = 0;
 
-       if (num  <= 0) {
+       if (num  == 0) {
                num = 64;
                ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
                if (ptr == NULL)
@@ -1297,7 +1312,7 @@ newh6namemem(netdissect_options *ndo)
        static struct h6namemem *ptr = NULL;
        static u_int num = 0;
 
-       if (num  <= 0) {
+       if (num  == 0) {
                num = 64;
                ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
                if (ptr == NULL)