From ccd518a7d05e28ad7de0c5aef6aada4c0686d2dc Mon Sep 17 00:00:00 2001 From: bol-van Date: Fri, 5 Mar 2021 20:15:56 +0300 Subject: [PATCH] nfqws: ntop46 functions --- nfq/helpers.c | 36 +++++++++++++++++++++++++++++------- nfq/helpers.h | 3 +++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/nfq/helpers.c b/nfq/helpers.c index 10e0380..6d54689 100644 --- a/nfq/helpers.c +++ b/nfq/helpers.c @@ -68,23 +68,45 @@ bool load_file_nonempty(const char *filename,void *buffer,size_t *buffer_size) -void print_sockaddr(const struct sockaddr *sa) +void ntop46(const struct sockaddr *sa, char *str, size_t len) { - char str[64]; + if (!len) return; + *str=0; switch (sa->sa_family) { case AF_INET: - if (inet_ntop(sa->sa_family, &((struct sockaddr_in*)sa)->sin_addr, str, sizeof(str))) - printf("%s:%d", str, ntohs(((struct sockaddr_in*)sa)->sin_port)); + inet_ntop(sa->sa_family, &((struct sockaddr_in*)sa)->sin_addr, str, len); break; case AF_INET6: - if (inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, str, sizeof(str))) - printf("[%s]:%d", str, ntohs(((struct sockaddr_in6*)sa)->sin6_port)); + inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, str, len); break; default: - printf("UNKNOWN_FAMILY_%d", sa->sa_family); + snprintf(str,len,"UNKNOWN_FAMILY_%d",sa->sa_family); } } +void ntop46_port(const struct sockaddr *sa, char *str, size_t len) +{ + char ip[40]; + ntop46(sa,ip,sizeof(ip)); + switch (sa->sa_family) + { + case AF_INET: + snprintf(str,len,"%s:%u",ip,ntohs(((struct sockaddr_in*)sa)->sin_port)); + break; + case AF_INET6: + snprintf(str,len,"[%s]:%u",ip,ntohs(((struct sockaddr_in6*)sa)->sin6_port)); + break; + default: + snprintf(str,len,"%s",ip); + } +} +void print_sockaddr(const struct sockaddr *sa) +{ + char ip_port[48]; + + ntop46_port(sa,ip_port,sizeof(ip_port)); + printf("%s",ip_port); +} void dbgprint_socket_buffers(int fd) { diff --git a/nfq/helpers.h b/nfq/helpers.h index fac7c24..bd54f58 100644 --- a/nfq/helpers.h +++ b/nfq/helpers.h @@ -14,5 +14,8 @@ bool load_file(const char *filename,void *buffer,size_t *buffer_size); bool load_file_nonempty(const char *filename,void *buffer,size_t *buffer_size); void print_sockaddr(const struct sockaddr *sa); +void ntop46(const struct sockaddr *sa, char *str, size_t len); +void ntop46_port(const struct sockaddr *sa, char *str, size_t len); + void dbgprint_socket_buffers(int fd); bool set_socket_buffers(int fd, int rcvbuf, int sndbuf);