use inet_pton() and inet_ntop(), which are RFC2553 standard function
authoritojun <itojun@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 14 Oct 1999 05:41:33 +0000 (05:41 +0000)
committeritojun <itojun@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 14 Oct 1999 05:41:33 +0000 (05:41 +0000)
for converting IPv[46] numeric notation to/from binary form.
recent BIND includes those functions so fallback is not necessary on
most of the platforms.
sorry if it raises any portability problem on other platforms.

remove partial inclusion of inet_ntop() in packet-ipv6.c.

move ip6_to_str() to packet.c, it fits better there than packet-ipv6.c.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@829 f5534014-38df-0310-8fa8-9805f1628bb7

Makefile.am
configure.in
inet_ntop.c [new file with mode: 0644]
inet_pton.c [new file with mode: 0644]
packet-ipv6.c
packet-ipv6.h
packet.c
packet.h
resolv.c

index e6e1068bd32ab60d9607af2268cee5ef102e5ff5..a19930e25a6515019f145b214a887a49709e40d0 100644 (file)
@@ -1,7 +1,7 @@
 # Makefile.am
 # Automake file for Ethereal
 #
-# $Id: Makefile.am,v 1.85 1999/10/14 05:10:31 guy Exp $
+# $Id: Makefile.am,v 1.86 1999/10/14 05:41:28 itojun Exp $
 #
 # Ethereal - Network traffic analyzer
 # By Gerald Combs <gerald@zing.org>
@@ -160,12 +160,16 @@ EXTRA_ethereal_SOURCES = \
        strerror.h      \
        strncasecmp.c   \
        mkstemp.c       \
-       inet_aton.c
+       inet_aton.c     \
+       inet_pton.c     \
+       inet_ntop.c
 
 ethereal_DEPENDENCIES = @SNMP_O@ @SNPRINTF_O@ @STRERROR_O@ \
-@STRNCASECMP_O@ @MKSTEMP_O@ @INET_ATON_O@ wiretap/libwiretap.a gtk/libui.a
+@STRNCASECMP_O@ @MKSTEMP_O@ @INET_ATON_O@ @INET_PTON_O@ @INET_NTOP_O@\
+wiretap/libwiretap.a gtk/libui.a
 ethereal_LDADD = @SNMP_O@ @SNPRINTF_O@ @STRERROR_O@ \
-@STRNCASECMP_O@ @MKSTEMP_O@ @INET_ATON_O@ wiretap/libwiretap.a gtk/libui.a \
+@STRNCASECMP_O@ @MKSTEMP_O@ @INET_ATON_O@ @INET_ATON_O@ @INET_NTOP_O@\
+wiretap/libwiretap.a gtk/libui.a \
 @SNMP_A@
 
 ps.c: print.ps rdps
index b2171cb859dd0131b337ddd8e957bd3f9c008b2b..dfd62b59eb1f5adcc038e6e9e404871d886d956c 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: configure.in,v 1.50 1999/10/14 03:50:27 itojun Exp $
+# $Id: configure.in,v 1.51 1999/10/14 05:41:28 itojun Exp $
 dnl Process this file with autoconf to produce a configure script.
 AC_INIT(etypes.h)
 
@@ -187,7 +187,7 @@ fi
 AC_SUBST(MKSTEMP_C)
 AC_SUBST(MKSTEMP_O)
 
-AC_CHECK_FUNC(inet_aton, INET_ADDR_O="",
+AC_CHECK_FUNC(inet_aton, INET_ATON_O="",
   INET_ATON_O="inet_aton.o")
 if test "$ac_cv_func_inet_aton" = no ; then
   INET_ATON_C="inet_aton.c"
@@ -196,7 +196,47 @@ fi
 AC_SUBST(INET_ATON_C)
 AC_SUBST(INET_ATON_O)
 
-AC_CHECK_FUNCS(getprotobynumber)
+AC_CHECK_FUNC(inet_pton, [
+  dnl check for pre-BIND82 inet_pton() bug.
+  AC_MSG_CHECKING(for broken inet_pton)
+  AC_TRY_RUN([#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+int main()
+{
+#ifdef AF_INET6
+  char buf[16];
+  /* this should return 0 (error) */
+  return inet_pton(AF_INET6, "0:1:2:3:4:5:6:7:", buf);
+#else
+  return 1;
+#endif
+}], [AC_MSG_RESULT(ok);
+have_inet_pton=yes], [AC_MSG_RESULT(broken);
+have_inet_pton=no], [AC_MSG_RESULT(cross compiling, assume it is broken);
+have_inet_pton=no])],
+have_inet_pton=no)
+if test "$have_inet_pton" = no; then
+  INET_PTON_C="inet_pton.c"
+  INET_PTON_O="inet_pton.o"
+else
+  INET_PTON_C=""
+  INET_PTON_O=""
+fi
+AC_SUBST(INET_PTON_C)
+AC_SUBST(INET_PTON_O)
+
+AC_CHECK_FUNC(inet_ntop, INET_NTOP_O="",
+  INET_ATON_O="inet_ntop.o")
+if test "$ac_cv_func_inet_ntop" = no ; then
+  INET_ATON_C="inet_ntop.c"
+  INET_ATON_O="inet_ntop.o"
+fi
+AC_SUBST(INET_NTOP_C)
+AC_SUBST(INET_NTOP_O)
+
+AC_CHECK_FUNCS(getprotobynumber gethostbyname2)
 
 dnl blank for now, but will be used in future
 AC_SUBST(ethereal_SUBDIRS)
diff --git a/inet_ntop.c b/inet_ntop.c
new file mode 100644 (file)
index 0000000..009ac4e
--- /dev/null
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 1996-1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
+ * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
+ * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char rcsid[] = "$Id: inet_ntop.c,v 1.1 1999/10/14 05:41:29 itojun Exp $";
+#endif /* LIBC_SCCS and not lint */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#ifdef NEED_SNPRINTF_H
+# ifdef HAVE_STDARG_H
+#  include <stdarg.h>
+# else
+#  include <varargs.h>
+# endif
+# include "snprintf.h"
+#endif
+
+#ifndef NS_INADDRSZ
+#define NS_INADDRSZ    4
+#endif
+#ifndef NS_IN6ADDRSZ
+#define NS_IN6ADDRSZ   16
+#endif
+#ifndef NS_INT16SZ
+#define NS_INT16SZ     2
+#endif
+
+/*
+ * WARNING: Don't even consider trying to compile this on a system where
+ * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
+ */
+
+static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
+static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
+
+/* char *
+ * inet_ntop(af, src, dst, size)
+ *     convert a network format address to presentation format.
+ * return:
+ *     pointer to presentation format address (`dst'), or NULL (see errno).
+ * author:
+ *     Paul Vixie, 1996.
+ */
+const char *
+inet_ntop(af, src, dst, size)
+       int af;
+       const void *src;
+       char *dst;
+       size_t size;
+{
+       switch (af) {
+       case AF_INET:
+               return (inet_ntop4(src, dst, size));
+       case AF_INET6:
+               return (inet_ntop6(src, dst, size));
+       default:
+               errno = EAFNOSUPPORT;
+               return (NULL);
+       }
+       /* NOTREACHED */
+}
+
+/* const char *
+ * inet_ntop4(src, dst, size)
+ *     format an IPv4 address
+ * return:
+ *     `dst' (as a const)
+ * notes:
+ *     (1) uses no statics
+ *     (2) takes a u_char* not an in_addr as input
+ * author:
+ *     Paul Vixie, 1996.
+ */
+static const char *
+inet_ntop4(src, dst, size)
+       const u_char *src;
+       char *dst;
+       size_t size;
+{
+       static const char fmt[] = "%u.%u.%u.%u";
+       char tmp[sizeof "255.255.255.255"];
+
+       if (snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]) > size) {
+               errno = ENOSPC;
+               return (NULL);
+       }
+       strcpy(dst, tmp);
+       return (dst);
+}
+
+/* const char *
+ * inet_ntop6(src, dst, size)
+ *     convert IPv6 binary address into presentation (printable) format
+ * author:
+ *     Paul Vixie, 1996.
+ */
+static const char *
+inet_ntop6(src, dst, size)
+       const u_char *src;
+       char *dst;
+       size_t size;
+{
+       /*
+        * Note that int32_t and int16_t need only be "at least" large enough
+        * to contain a value of the specified size.  On some systems, like
+        * Crays, there is no such thing as an integer variable with 16 bits.
+        * Keep this in mind if you think this function should have been coded
+        * to use pointer overlays.  All the world's not a VAX.
+        */
+       char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
+       struct { int base, len; } best, cur;
+       u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
+       int i;
+
+       /*
+        * Preprocess:
+        *      Copy the input (bytewise) array into a wordwise array.
+        *      Find the longest run of 0x00's in src[] for :: shorthanding.
+        */
+       memset(words, '\0', sizeof words);
+       for (i = 0; i < NS_IN6ADDRSZ; i++)
+               words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
+       best.base = -1;
+       cur.base = -1;
+       for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+               if (words[i] == 0) {
+                       if (cur.base == -1)
+                               cur.base = i, cur.len = 1;
+                       else
+                               cur.len++;
+               } else {
+                       if (cur.base != -1) {
+                               if (best.base == -1 || cur.len > best.len)
+                                       best = cur;
+                               cur.base = -1;
+                       }
+               }
+       }
+       if (cur.base != -1) {
+               if (best.base == -1 || cur.len > best.len)
+                       best = cur;
+       }
+       if (best.base != -1 && best.len < 2)
+               best.base = -1;
+
+       /*
+        * Format the result.
+        */
+       tp = tmp;
+       for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+               /* Are we inside the best run of 0x00's? */
+               if (best.base != -1 && i >= best.base &&
+                   i < (best.base + best.len)) {
+                       if (i == best.base)
+                               *tp++ = ':';
+                       continue;
+               }
+               /* Are we following an initial run of 0x00s or any real hex? */
+               if (i != 0)
+                       *tp++ = ':';
+               /* Is this address an encapsulated IPv4? */
+               if (i == 6 && best.base == 0 &&
+                   (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
+                       if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
+                               return (NULL);
+                       tp += strlen(tp);
+                       break;
+               }
+               tp += sprintf(tp, "%x", words[i]);
+       }
+       /* Was it a trailing run of 0x00's? */
+       if (best.base != -1 && (best.base + best.len) == 
+           (NS_IN6ADDRSZ / NS_INT16SZ))
+               *tp++ = ':';
+       *tp++ = '\0';
+
+       /*
+        * Check for overflow, copy, and we're done.
+        */
+       if ((size_t)(tp - tmp) > size) {
+               errno = ENOSPC;
+               return (NULL);
+       }
+       strcpy(dst, tmp);
+       return (dst);
+}
diff --git a/inet_pton.c b/inet_pton.c
new file mode 100644 (file)
index 0000000..8769a7b
--- /dev/null
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
+ * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
+ * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char rcsid[] = "$Id: inet_pton.c,v 1.1 1999/10/14 05:41:29 itojun Exp $";
+#endif /* LIBC_SCCS and not lint */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <string.h>
+#include <errno.h>
+
+#ifndef NS_INADDRSZ
+#define NS_INADDRSZ    4
+#endif
+#ifndef NS_IN6ADDRSZ
+#define NS_IN6ADDRSZ   16
+#endif
+#ifndef NS_INT16SZ
+#define NS_INT16SZ     2
+#endif
+
+/*
+ * WARNING: Don't even consider trying to compile this on a system where
+ * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
+ */
+
+#ifdef AF_INET
+static int     inet_pton4 __P((const char *src, u_char *dst));
+#endif
+#ifdef AF_INET6
+static int     inet_pton6 __P((const char *src, u_char *dst));
+#endif
+
+/* int
+ * inet_pton(af, src, dst)
+ *     convert from presentation format (which usually means ASCII printable)
+ *     to network format (which is usually some kind of binary format).
+ * return:
+ *     1 if the address was valid for the specified address family
+ *     0 if the address wasn't valid (`dst' is untouched in this case)
+ *     -1 if some other error occurred (`dst' is untouched in this case, too)
+ * author:
+ *     Paul Vixie, 1996.
+ */
+int
+inet_pton(af, src, dst)
+       int af;
+       const char *src;
+       void *dst;
+{
+       switch (af) {
+#ifdef AF_INET
+       case AF_INET:
+               return (inet_pton4(src, dst));
+#endif
+#ifdef AF_INET6
+       case AF_INET6:
+               return (inet_pton6(src, dst));
+#endif
+       default:
+               errno = EAFNOSUPPORT;
+               return (-1);
+       }
+       /* NOTREACHED */
+}
+
+#ifdef AF_INET
+/* int
+ * inet_pton4(src, dst)
+ *     like inet_aton() but without all the hexadecimal and shorthand.
+ * return:
+ *     1 if `src' is a valid dotted quad, else 0.
+ * notice:
+ *     does not touch `dst' unless it's returning 1.
+ * author:
+ *     Paul Vixie, 1996.
+ */
+static int
+inet_pton4(src, dst)
+       const char *src;
+       u_char *dst;
+{
+       static const char digits[] = "0123456789";
+       int saw_digit, octets, ch;
+       u_char tmp[NS_INADDRSZ], *tp;
+
+       saw_digit = 0;
+       octets = 0;
+       *(tp = tmp) = 0;
+       while ((ch = *src++) != '\0') {
+               const char *pch;
+
+               if ((pch = strchr(digits, ch)) != NULL) {
+                       u_int new = *tp * 10 + (pch - digits);
+
+                       if (new > 255)
+                               return (0);
+                       *tp = new;
+                       if (! saw_digit) {
+                               if (++octets > 4)
+                                       return (0);
+                               saw_digit = 1;
+                       }
+               } else if (ch == '.' && saw_digit) {
+                       if (octets == 4)
+                               return (0);
+                       *++tp = 0;
+                       saw_digit = 0;
+               } else
+                       return (0);
+       }
+       if (octets < 4)
+               return (0);
+       memcpy(dst, tmp, NS_INADDRSZ);
+       return (1);
+}
+#endif
+
+#ifdef AF_INET6
+/* int
+ * inet_pton6(src, dst)
+ *     convert presentation level address to network order binary form.
+ * return:
+ *     1 if `src' is a valid [RFC1884 2.2] address, else 0.
+ * notice:
+ *     (1) does not touch `dst' unless it's returning 1.
+ *     (2) :: in a full address is silently ignored.
+ * credit:
+ *     inspired by Mark Andrews.
+ * author:
+ *     Paul Vixie, 1996.
+ */
+static int
+inet_pton6(src, dst)
+       const char *src;
+       u_char *dst;
+{
+       static const char xdigits_l[] = "0123456789abcdef",
+                         xdigits_u[] = "0123456789ABCDEF";
+       u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
+       const char *xdigits, *curtok;
+       int ch, saw_xdigit;
+       u_int val;
+
+       memset((tp = tmp), '\0', NS_IN6ADDRSZ);
+       endp = tp + NS_IN6ADDRSZ;
+       colonp = NULL;
+       /* Leading :: requires some special handling. */
+       if (*src == ':')
+               if (*++src != ':')
+                       return (0);
+       curtok = src;
+       saw_xdigit = 0;
+       val = 0;
+       while ((ch = *src++) != '\0') {
+               const char *pch;
+
+               if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
+                       pch = strchr((xdigits = xdigits_u), ch);
+               if (pch != NULL) {
+                       val <<= 4;
+                       val |= (pch - xdigits);
+                       if (val > 0xffff)
+                               return (0);
+                       saw_xdigit = 1;
+                       continue;
+               }
+               if (ch == ':') {
+                       curtok = src;
+                       if (!saw_xdigit) {
+                               if (colonp)
+                                       return (0);
+                               colonp = tp;
+                               continue;
+                       } else if (*src == '\0') {
+                               return (0);
+                       }
+                       if (tp + NS_INT16SZ > endp)
+                               return (0);
+                       *tp++ = (u_char) (val >> 8) & 0xff;
+                       *tp++ = (u_char) val & 0xff;
+                       saw_xdigit = 0;
+                       val = 0;
+                       continue;
+               }
+               if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
+                   inet_pton4(curtok, tp) > 0) {
+                       tp += NS_INADDRSZ;
+                       saw_xdigit = 0;
+                       break;  /* '\0' was seen by inet_pton4(). */
+               }
+               return (0);
+       }
+       if (saw_xdigit) {
+               if (tp + NS_INT16SZ > endp)
+                       return (0);
+               *tp++ = (u_char) (val >> 8) & 0xff;
+               *tp++ = (u_char) val & 0xff;
+       }
+       if (colonp != NULL) {
+               /*
+                * Since some memmove()'s erroneously fail to handle
+                * overlapping regions, we'll do the shift by hand.
+                */
+               const int n = tp - colonp;
+               int i;
+
+               if (tp == endp)
+                       return (0);
+               for (i = 1; i <= n; i++) {
+                       endp[- i] = colonp[n - i];
+                       colonp[n - i] = 0;
+               }
+               tp = endp;
+       }
+       if (tp != endp)
+               return (0);
+       memcpy(dst, tmp, NS_IN6ADDRSZ);
+       return (1);
+}
+#endif
index 231d1a0f44033a178733bfd09bc405fa836f21b0..6658add6df7484aa9af0de96b2de09f1aa5d1efd 100644 (file)
@@ -1,7 +1,7 @@
 /* packet-ipv6.c
  * Routines for IPv6 packet disassembly 
  *
- * $Id: packet-ipv6.c,v 1.19 1999/10/14 03:50:29 itojun Exp $
+ * $Id: packet-ipv6.c,v 1.20 1999/10/14 05:41:29 itojun Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -55,12 +55,6 @@ static int hf_ipv6_dst = -1;
 #define        offsetof(type, member)  ((size_t)(&((type *)0)->member))
 #endif
 
-static const char *
-inet_ntop6(const u_char *src, char *dst, size_t size);
-
-static const char *
-inet_ntop4(const u_char *src, char *dst, size_t size);
-
 static int
 dissect_routing6(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
     struct ip6_rthdr rt;
@@ -373,164 +367,6 @@ again:
     }
 }
 
-gchar *
-ip6_to_str(struct e_in6_addr *ad) {
-  static gchar buf[4 * 8 + 8];
-
-  inet_ntop6((u_char*)ad, (gchar*)buf, sizeof(buf));
-  return buf;
-}
-
-#ifndef NS_IN6ADDRSZ
-#define NS_IN6ADDRSZ   16
-#endif
-
-#ifndef NS_INT16SZ
-#define NS_INT16SZ     (sizeof(guint16))
-#endif
-
-#define SPRINTF(x) ((size_t)sprintf x)
-
-/*
- * Copyright (c) 1996-1999 by Internet Software Consortium.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
- * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
- * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- * SOFTWARE.
- */
-
-/* const char *
- * inet_ntop4(src, dst, size)
- *     format an IPv4 address
- * return:
- *     `dst' (as a const)
- * notes:
- *     (1) uses no statics
- *     (2) takes a u_char* not an in_addr as input
- * author:
- *     Paul Vixie, 1996.
- */
-static const char *
-inet_ntop4(src, dst, size)
-       const u_char *src;
-       char *dst;
-       size_t size;
-{
-       static const char fmt[] = "%u.%u.%u.%u";
-       char tmp[sizeof "255.255.255.255"];
-
-       if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
-               return (NULL);
-       }
-       strcpy(dst, tmp);
-       return (dst);
-}
-
-/* const char *
- * inet_ntop6(src, dst, size)
- *     convert IPv6 binary address into presentation (printable) format
- * author:
- *     Paul Vixie, 1996.
- */
-static const char *
-inet_ntop6(src, dst, size)
-       const u_char *src;
-       char *dst;
-       size_t size;
-{
-       /*
-        * Note that int32_t and int16_t need only be "at least" large enough
-        * to contain a value of the specified size.  On some systems, like
-        * Crays, there is no such thing as an integer variable with 16 bits.
-        * Keep this in mind if you think this function should have been coded
-        * to use pointer overlays.  All the world's not a VAX.
-        */
-       char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
-       struct { int base, len; } best, cur;
-       u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
-       int i;
-
-       /*
-        * Preprocess:
-        *      Copy the input (bytewise) array into a wordwise array.
-        *      Find the longest run of 0x00's in src[] for :: shorthanding.
-        */
-       memset(words, '\0', sizeof words);
-       for (i = 0; i < NS_IN6ADDRSZ; i++)
-               words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
-       best.base = -1;
-       cur.base = -1;
-       for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
-               if (words[i] == 0) {
-                       if (cur.base == -1)
-                               cur.base = i, cur.len = 1;
-                       else
-                               cur.len++;
-               } else {
-                       if (cur.base != -1) {
-                               if (best.base == -1 || cur.len > best.len)
-                                       best = cur;
-                               cur.base = -1;
-                       }
-               }
-       }
-       if (cur.base != -1) {
-               if (best.base == -1 || cur.len > best.len)
-                       best = cur;
-       }
-       if (best.base != -1 && best.len < 2)
-               best.base = -1;
-
-       /*
-        * Format the result.
-        */
-       tp = tmp;
-       for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
-               /* Are we inside the best run of 0x00's? */
-               if (best.base != -1 && i >= best.base &&
-                   i < (best.base + best.len)) {
-                       if (i == best.base)
-                               *tp++ = ':';
-                       continue;
-               }
-               /* Are we following an initial run of 0x00s or any real hex? */
-               if (i != 0)
-                       *tp++ = ':';
-               /* Is this address an encapsulated IPv4? */
-               if (i == 6 && best.base == 0 &&
-                   (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
-                       if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
-                               return (NULL);
-                       tp += strlen(tp);
-                       break;
-               }
-               tp += SPRINTF((tp, "%x", words[i]));
-       }
-       /* Was it a trailing run of 0x00's? */
-       if (best.base != -1 && (best.base + best.len) == 
-           (NS_IN6ADDRSZ / NS_INT16SZ))
-               *tp++ = ':';
-       *tp++ = '\0';
-
-       /*
-        * Check for overflow, copy, and we're done.
-        */
-       if ((size_t)(tp - tmp) > size) {
-               return (NULL);
-       }
-       strcpy(dst, tmp);
-       return (dst);
-}
-
 void
 proto_register_ipv6(void)
 {
index c41c4c57484cfd86e9ad8f3b58f6af75dd7034b1..f451de7b533526395ad15cf66b705f268202ca15 100644 (file)
@@ -1,7 +1,7 @@
 /* packet-ipv6.h
  * Definitions for IPv6 packet disassembly 
  *
- * $Id: packet-ipv6.h,v 1.5 1999/09/10 07:19:40 guy Exp $
+ * $Id: packet-ipv6.h,v 1.6 1999/10/14 05:41:30 itojun Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -517,7 +517,4 @@ struct rr_result {          /* router renumbering result message */
 };
 #endif /* BYTE_ORDER */
 
-struct e_in6_addr;
-gchar*     ip6_to_str(struct e_in6_addr *);
-
 #endif /* __PACKET_IPV6_H_DEFINED__ */
index 8c7e3af8c7d825bd52bcf547e5c994bfaac36888..e13d31d7be14f75be4d10e616521b99cdf921f94 100644 (file)
--- a/packet.c
+++ b/packet.c
@@ -1,7 +1,7 @@
 /* packet.c
  * Routines for packet disassembly
  *
- * $Id: packet.c,v 1.46 1999/10/12 06:20:21 gram Exp $
+ * $Id: packet.c,v 1.47 1999/10/14 05:41:30 itojun Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -139,6 +139,18 @@ ip_to_str(const guint8 *ad) {
   return p;
 }
 
+gchar *
+ip6_to_str(struct e_in6_addr *ad) {
+#ifndef INET6_ADDRSTRLEN
+#define INET6_ADDRSTRLEN 46
+#endif
+  static gchar buf[INET6_ADDRSTRLEN];
+
+  inet_ntop(AF_INET6, (u_char*)ad, (gchar*)buf, sizeof(buf));
+  return buf;
+}
+
+
 #define        PLURALIZE(n)    (((n) > 1) ? "s" : "")
 #define        COMMA(do_it)    ((do_it) ? ", " : "")
 
index 0dfe45791e7203e3d48fb862918c37fb255042e8..5e52a2038a1b68df540d6d614546adee66466898 100644 (file)
--- a/packet.h
+++ b/packet.h
@@ -1,7 +1,7 @@
 /* packet.h
  * Definitions for packet disassembly structures and routines
  *
- * $Id: packet.h,v 1.109 1999/10/14 05:10:33 guy Exp $
+ * $Id: packet.h,v 1.110 1999/10/14 05:41:31 itojun Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -372,6 +372,8 @@ extern gboolean          tree_is_expanded[NUM_TREE_TYPES];
 /* Utility routines used by packet*.c */
 gchar*     ether_to_str(const guint8 *);
 gchar*     ip_to_str(const guint8 *);
+struct e_in6_addr;
+gchar*     ip6_to_str(struct e_in6_addr *);
 gchar*    abs_time_to_str(struct timeval*);
 gchar*    rel_time_to_str(struct timeval*);
 gchar*     time_secs_to_str(guint32);
index 8048f2e15dd9ad5a3086c1aaf4205cc86f416574..4c8efb64048b839cb12188fce68cc3a96a7d33b5 100644 (file)
--- a/resolv.c
+++ b/resolv.c
@@ -1,7 +1,7 @@
 /* resolv.c
  * Routines for network object lookup
  *
- * $Id: resolv.c,v 1.13 1999/10/11 03:03:11 guy Exp $
+ * $Id: resolv.c,v 1.14 1999/10/14 05:41:33 itojun Exp $
  *
  * Laurent Deniel <deniel@worldnet.fr>
  *
@@ -858,6 +858,11 @@ gboolean get_host_ipaddr(const char *host, guint32 *addrp)
        struct in_addr          ipaddr;
        struct hostent          *hp;
 
+       /*
+        * don't change it to inet_pton(AF_INET), they are not 100% compatible.
+        * inet_pton(AF_INET) does not support hexadecimal notation nor
+        * less-than-4 octet notation.
+        */
        if (!inet_aton(host, &ipaddr)) {
                /* It's not a valid dotted-quad IP address; is it a valid
                 * host name? */
@@ -875,3 +880,29 @@ gboolean get_host_ipaddr(const char *host, guint32 *addrp)
        *addrp = ntohl(ipaddr.s_addr);
        return TRUE;
 }
+
+/*
+ * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
+ * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
+ * return FALSE if we fail.
+ */
+gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
+{
+       struct hostent *hp;
+
+       if (inet_pton(AF_INET6, host, addrp) == 1)
+               return TRUE;
+
+       /* try FQDN */
+#ifdef HAVE_GETHOSTBYNAME2
+       hp = gethostbyname2(host, AF_INET6);
+#else
+       hp = NULL;
+#endif
+       if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
+               memcpy(addrp, hp->h_addr, hp->h_length);
+               return TRUE;
+       }
+
+       return FALSE;
+}