From 084089e7ab45a75e5c6edfabeef283fdcfc589ed Mon Sep 17 00:00:00 2001
From: Eric Leblond <eric@regit.org>
Date: Mon, 29 Aug 2011 14:52:08 +0200
Subject: [PATCH 1/2] Introduce PrintInet function

This function has the same signature than inet_ntop() and it
will be used as substitution in the code. For IPv4 this is a simple
wrapper. For IPv6, it display addresses with fixed length.
---
 src/util-print.c |   36 ++++++++++++++++++++++++++++++++++++
 src/util-print.h |    1 +
 2 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/src/util-print.c b/src/util-print.c
index e2d78de..1ad77dd 100644
--- a/src/util-print.c
+++ b/src/util-print.c
@@ -124,3 +124,39 @@ void PrintRawDataFp(FILE *fp, uint8_t *buf, uint32_t buflen) {
         fprintf(fp, "\n");
 }
 
+static const char *PrintInetIPv6(const void *src, char *dst, socklen_t size)
+{
+    struct in6_addr * insrc = (struct in6_addr *) src;
+    int i;
+    char s_part[6];
+
+    /* current IPv6 format is fixed size */
+    if (size < 8 * 5) {
+        SCLogWarning(SC_ERR_ARG_LEN_LONG, "Too small buffer to write IPv6 address");
+        return NULL;
+    }
+    memset(dst, 0, size);
+    for(i = 0; i < 8; i++) {
+        snprintf(s_part, 6, "%04x:", htons(insrc->s6_addr16[i]));
+        strlcat(dst, s_part, size);
+    }
+    /* suppress last ':' */
+    dst[strlen(dst) - 1] = 0;
+
+    return dst;
+}
+
+const char *PrintInet(int af, const void *src, char *dst, socklen_t size)
+{
+    switch (af) {
+        case AF_INET:
+            return inet_ntop(af, src, dst, size);
+        case AF_INET6:
+            /* Format IPv6 without deleting zeroes */
+            return PrintInetIPv6(src, dst, size);
+        default:
+            SCLogError(SC_ERR_INVALID_VALUE, "Unsupported protocol: %d", af);
+    }
+    return NULL;
+}
+
diff --git a/src/util-print.h b/src/util-print.h
index ad005e4..056d6f2 100644
--- a/src/util-print.h
+++ b/src/util-print.h
@@ -28,6 +28,7 @@ void PrintRawLineHexFp(FILE *, uint8_t *, uint32_t);
 void PrintRawUriFp(FILE *, uint8_t *, uint32_t);
 void PrintRawDataFp(FILE *, uint8_t *, uint32_t);
 void PrintRawLineHexBuf(char *, uint32_t, uint8_t *, uint32_t );
+const char *PrintInet(int , const void *, char *, socklen_t);
 
 #endif /* __UTIL_PRINT_H__ */
 
-- 
1.7.5.4

