From 4a3ea16925c56328f457720749375877d2eb086f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 2 Dec 2015 15:43:40 +0100 Subject: [FUSE] Add virtual status file --- src/shared/sockhelper.c | 39 ++++++++++++++++++++++++++++++++++++--- src/shared/sockhelper.h | 4 +++- 2 files changed, 39 insertions(+), 4 deletions(-) (limited to 'src/shared') diff --git a/src/shared/sockhelper.c b/src/shared/sockhelper.c index f2f8b97..d4995db 100644 --- a/src/shared/sockhelper.c +++ b/src/shared/sockhelper.c @@ -163,12 +163,45 @@ void sock_destroyPollList(poll_list_t *list) free( list ); } -bool sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int len) +int sock_printHost(const dnbd3_host_t * const host, char * const buffer, const int len) +{ + // Worst case: Port 5 chars, ':' to separate ip and port 1 char, terminating null 1 char = 7, [] for IPv6 + if ( len < 10 ) return 0; + char *output = buffer; + if ( host->type == AF_INET6 ) { + *output++ = '['; + inet_ntop( AF_INET6, host->addr, output, len - 10 ); + output += strlen( output ); + *output++ = ']'; + } else if ( host->type == AF_INET ) { + inet_ntop( AF_INET, host->addr, output, len - 8 ); + output += strlen( output ); + } else { + int ret = snprintf( output, len, "", (int)host->type ); + return MIN( ret, len-1 ); + } + *output = '\0'; + if ( host->port != 0 ) { + // There are still at least 7 bytes left in the buffer, port is at most 5 bytes + ':' + '\0' = 7 + int ret = snprintf( output, 7, ":%d", (int)ntohs( host->port ) ); + output += MIN( ret, 6 ); + } + return output - buffer; +} + +int sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int len) { char host[100], port[10]; + int outlen = 0; int ret = getnameinfo( addr, addrLen, host, 100, port, 10, NI_NUMERICHOST | NI_NUMERICSERV ); - if ( ret == 0 ) snprintf( output, len, "[%s]:%s", host, port ); - return ret == 0; + if ( ret == 0 ) { + if ( addr->sa_family == AF_INET ) { + outlen = snprintf( output, len, "%s:%s", host, port ); + } else { + outlen = snprintf( output, len, "[%s]:%s", host, port ); + } + } + return MIN( outlen, len-1 ); } bool sock_listen(poll_list_t* list, char* bind_addr, uint16_t port) diff --git a/src/shared/sockhelper.h b/src/shared/sockhelper.h index 6ffc31a..ad3ee65 100644 --- a/src/shared/sockhelper.h +++ b/src/shared/sockhelper.h @@ -31,7 +31,9 @@ int sock_resolveToDnbd3Host(const char * const address, dnbd3_host_t * const des void sock_setTimeout(const int sockfd, const int milliseconds); -bool sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int len); +int sock_printHost(const dnbd3_host_t * const host, char *output, const int len); + +int sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int len); /** * Create new poll list. -- cgit v1.2.3-55-g7522