From 2fa07780c240a5b89d8915bc8a1b41421aac95a7 Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Mon, 29 Aug 2011 15:52:02 +0200 Subject: [PATCH] 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 | 35 +++++++++++++++++++++++++++++++++++ src/util-print.h | 1 + 2 files changed, 36 insertions(+) diff --git a/src/util-print.c b/src/util-print.c index e2d78de8fe..8d1c211342 100644 --- a/src/util-print.c +++ b/src/util-print.c @@ -124,3 +124,38 @@ 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 ad005e4a16..056d6f2de1 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__ */