]> The Tcpdump Group git mirrors - tcpdump/blobdiff - addrtostr.c
Don't require IPv6 library support in order to support IPv6 addresses.
[tcpdump] / addrtostr.c
similarity index 85%
rename from missing/inet_ntop.c
rename to addrtostr.c
index 77928db96421a821e1542ff848471fd9901aab58..654f89226d086ec8e00f0028ec7fe95dfa983141 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999 Kungliga Tekniska Högskolan
+ * Copyright (c) 1999 Kungliga Tekniska Högskolan
  * (Royal Institute of Technology, Stockholm, Sweden).
  * All rights reserved.
  *
@@ -17,7 +17,7 @@
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *      This product includes software developed by the Kungliga Tekniska
- *      Högskolan and its contributors.
+ *      Högskolan and its contributors.
  *
  * 4. Neither the name of the Institute nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
 #endif
 
 #include <netdissect-stdinc.h>
+#include "addrtostr.h"
 
 #include <stdio.h>
+#include <string.h>
 
 /*
  *
 #define INT16SZ     2    /* word size */
 #endif
 
-static const char *
-inet_ntop_v4 (const void *src, char *dst, size_t size)
+const char *
+addrtostr (const void *src, char *dst, size_t size)
 {
+    const u_char *srcaddr = (const u_char *)src;
     const char digits[] = "0123456789";
     int i;
-    struct in_addr *addr = (struct in_addr *)src;
-    u_long a = ntohl(addr->s_addr);
     const char *orig_dst = dst;
 
     if (size < INET_ADDRSTRLEN) {
@@ -70,7 +71,7 @@ inet_ntop_v4 (const void *src, char *dst, size_t size)
        return NULL;
     }
     for (i = 0; i < 4; ++i) {
-       int n = (a >> (24 - i * 8)) & 0xFF;
+       int n = *srcaddr++;
        int non_zerop = 0;
 
        if (non_zerop || n / 100 > 0) {
@@ -91,12 +92,11 @@ inet_ntop_v4 (const void *src, char *dst, size_t size)
     return orig_dst;
 }
 
-#ifdef INET6
 /*
  * Convert IPv6 binary address into presentation (printable) format.
  */
-static const char *
-inet_ntop_v6 (const u_char *src, char *dst, size_t size)
+const char *
+addrtostr6 (const void *src, char *dst, size_t size)
 {
   /*
    * Note that int32_t and int16_t need only be "at least" large enough
@@ -105,6 +105,7 @@ inet_ntop_v6 (const u_char *src, char *dst, size_t size)
    * Keep this in mind if you think this function should have been coded
    * to use pointer overlays.  All the world's not a VAX.
    */
+  const u_char *srcaddr = (const u_char *)src;
   char  tmp [INET6_ADDRSTRLEN+1];
   char *tp;
   struct {
@@ -120,7 +121,7 @@ inet_ntop_v6 (const u_char *src, char *dst, size_t size)
    */
   memset (words, 0, sizeof(words));
   for (i = 0; i < IN6ADDRSZ; i++)
-      words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
+      words[i/2] |= (srcaddr[i] << ((1 - (i % 2)) << 3));
 
   best.len = 0;
   best.base = -1;
@@ -170,9 +171,10 @@ inet_ntop_v6 (const u_char *src, char *dst, size_t size)
     if (i == 6 && best.base == 0 &&
         (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
     {
-      if (!inet_ntop_v4(src+12, tp, sizeof(tmp) - (tp - tmp)))
+      if (!addrtostr(srcaddr+12, tp, sizeof(tmp) - (tp - tmp)))
       {
         errno = ENOSPC;
+fprintf(stderr, "Bleah 1\n");
         return (NULL);
       }
       tp += strlen(tp);
@@ -192,25 +194,8 @@ inet_ntop_v6 (const u_char *src, char *dst, size_t size)
   if ((size_t)(tp - tmp) > size)
   {
     errno = ENOSPC;
+fprintf(stderr, "Bleah 2\n");
     return (NULL);
   }
   return strcpy (dst, tmp);
 }
-#endif   /* INET6 */
-
-
-const char *
-inet_ntop(int af, const void *src, char *dst, size_t size)
-{
-    switch (af) {
-    case AF_INET :
-       return inet_ntop_v4 (src, dst, size);
-#ifdef INET6
-    case AF_INET6:
-         return inet_ntop_v6 ((const u_char*)src, dst, size);
-#endif
-    default :
-       errno = EAFNOSUPPORT;
-       return NULL;
-    }
-}