nfqws: ntop46 functions

This commit is contained in:
bol-van 2021-03-05 20:15:56 +03:00
parent 7d917d8334
commit ccd518a7d0
2 changed files with 32 additions and 7 deletions

View File

@ -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) switch (sa->sa_family)
{ {
case AF_INET: case AF_INET:
if (inet_ntop(sa->sa_family, &((struct sockaddr_in*)sa)->sin_addr, str, sizeof(str))) inet_ntop(sa->sa_family, &((struct sockaddr_in*)sa)->sin_addr, str, len);
printf("%s:%d", str, ntohs(((struct sockaddr_in*)sa)->sin_port));
break; break;
case AF_INET6: case AF_INET6:
if (inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, str, sizeof(str))) inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, str, len);
printf("[%s]:%d", str, ntohs(((struct sockaddr_in6*)sa)->sin6_port));
break; break;
default: 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) void dbgprint_socket_buffers(int fd)
{ {

View File

@ -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); bool load_file_nonempty(const char *filename,void *buffer,size_t *buffer_size);
void print_sockaddr(const struct sockaddr *sa); 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); void dbgprint_socket_buffers(int fd);
bool set_socket_buffers(int fd, int rcvbuf, int sndbuf); bool set_socket_buffers(int fd, int rcvbuf, int sndbuf);