summaryrefslogtreecommitdiffstats
path: root/src/shared
diff options
context:
space:
mode:
authorSimon Rettberg2015-12-02 15:43:40 +0100
committerSimon Rettberg2015-12-02 15:43:40 +0100
commit4a3ea16925c56328f457720749375877d2eb086f (patch)
tree70bfdde8e2d9c117b8e3724db1e6e7ac0c910d87 /src/shared
parent[FUSE] Request alt servers from connected server (diff)
downloaddnbd3-4a3ea16925c56328f457720749375877d2eb086f.tar.gz
dnbd3-4a3ea16925c56328f457720749375877d2eb086f.tar.xz
dnbd3-4a3ea16925c56328f457720749375877d2eb086f.zip
[FUSE] Add virtual status file
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/sockhelper.c39
-rw-r--r--src/shared/sockhelper.h4
2 files changed, 39 insertions, 4 deletions
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, "<?addrtype=%d>", (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.