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.
remotes/origin/master-1.1.x
Eric Leblond 14 years ago committed by Victor Julien
parent 7e1d911215
commit 2fa07780c2

@ -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;
}

@ -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__ */

Loading…
Cancel
Save