X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;ds=sidebyside;f=src%2Fnetutl.c;h=9f2319ec7629c10136af55cf544b6557fab4b966;hb=48053fbb098f832784d2e775822e712fac84fe3c;hp=983ad91485de40a5a6692a8c1613b16452a76e50;hpb=76c7550c8ab0e9c0ee14a9c396baa008cfb9bc42;p=meshlink diff --git a/src/netutl.c b/src/netutl.c index 983ad914..9f2319ec 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -1,6 +1,6 @@ /* netutl.c -- some supporting network utility code - Copyright (C) 2014 Guus Sliepen + Copyright (C) 2014-2017 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -32,11 +32,13 @@ bool hostnames = false; Return NULL on failure. */ struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) { - struct addrinfo *ai, hint = {0}; + struct addrinfo *ai; int err; - hint.ai_family = addressfamily; - hint.ai_socktype = socktype; + struct addrinfo hint = { + .ai_family = addressfamily, + .ai_socktype = socktype, + }; err = getaddrinfo(address, service, &hint, &ai); @@ -49,13 +51,17 @@ struct addrinfo *str2addrinfo(const char *address, const char *service, int sock } sockaddr_t str2sockaddr(const char *address, const char *port) { - struct addrinfo *ai, hint = {0}; - sockaddr_t result = {{0}}; + struct addrinfo *ai; + sockaddr_t result; int err; - hint.ai_family = AF_UNSPEC; - hint.ai_flags = AI_NUMERICHOST; - hint.ai_socktype = SOCK_STREAM; + memset(&result, 0, sizeof(result)); + + struct addrinfo hint = { + .ai_family = AF_UNSPEC, + .ai_flags = AI_NUMERICHOST, + .ai_socktype = SOCK_STREAM, + }; err = getaddrinfo(address, port, &hint, &ai); @@ -80,10 +86,14 @@ void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) { int err; if(sa->sa.sa_family == AF_UNKNOWN) { - if(addrstr) + if(addrstr) { *addrstr = xstrdup(sa->unknown.address); - if(portstr) + } + + if(portstr) { *portstr = xstrdup(sa->unknown.port); + } + return; } @@ -96,13 +106,17 @@ void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) { scopeid = strchr(address, '%'); - if(scopeid) - *scopeid = '\0'; /* Descope. */ + if(scopeid) { + *scopeid = '\0'; /* Descope. */ + } - if(addrstr) + if(addrstr) { *addrstr = xstrdup(address); - if(portstr) + } + + if(portstr) { *portstr = xstrdup(port); + } } char *sockaddr2hostname(const sockaddr_t *sa) { @@ -118,8 +132,10 @@ char *sockaddr2hostname(const sockaddr_t *sa) { err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV)); - if(err) + + if(err) { logger(NULL, MESHLINK_ERROR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err)); + } xasprintf(&str, "%s port %s", address, port); @@ -131,8 +147,9 @@ int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) { result = a->sa.sa_family - b->sa.sa_family; - if(result) + if(result) { return result; + } switch(a->sa.sa_family) { case AF_UNSPEC: @@ -159,8 +176,9 @@ int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) { result = a->sa.sa_family - b->sa.sa_family; - if(result) + if(result) { return result; + } switch(a->sa.sa_family) { case AF_UNSPEC: @@ -169,24 +187,27 @@ int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) { case AF_UNKNOWN: result = strcmp(a->unknown.address, b->unknown.address); - if(result) + if(result) { return result; + } return strcmp(a->unknown.port, b->unknown.port); case AF_INET: result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a)->in.sin_addr); - if(result) + if(result) { return result; + } return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a)->in.sin_port); case AF_INET6: result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a)->in6.sin6_addr); - if(result) + if(result) { return result; + } return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a)->in6.sin6_port); @@ -198,9 +219,9 @@ int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) { } void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) { - if(b->sa.sa_family != AF_UNKNOWN) + if(b->sa.sa_family != AF_UNKNOWN) { *a = *b; - else { + } else { a->unknown.family = AF_UNKNOWN; a->unknown.address = xstrdup(b->unknown.address); a->unknown.port = xstrdup(b->unknown.port);