diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2015-03-10 12:30:12 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2015-03-11 10:08:12 -0400 |
commit | fc25ad25e019eb2fbce9d3c4be052098cb2c7122 (patch) | |
tree | ac08c6a855701e3b62a20a55778226be5f604981 /src | |
parent | 48b4eab497c8d337e0c892291405ea4201da4572 (diff) |
socket-util: refactor error handling in sockaddr_pretty
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/socket-util.c | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c index 5820279a27..c832cfe3f9 100644 --- a/src/shared/socket-util.c +++ b/src/shared/socket-util.c @@ -482,6 +482,7 @@ int sockaddr_port(const struct sockaddr *_sa) { int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret) { union sockaddr_union *sa = (union sockaddr_union*) _sa; char *p; + int r; assert(sa); assert(salen >= sizeof(sa->sa.sa_family)); @@ -493,19 +494,17 @@ int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ a = ntohl(sa->in.sin_addr.s_addr); - if (include_port) { - if (asprintf(&p, + if (include_port) + r = asprintf(&p, "%u.%u.%u.%u:%u", a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, - ntohs(sa->in.sin_port)) < 0) - return -ENOMEM; - } else { - if (asprintf(&p, + ntohs(sa->in.sin_port)); + else + r = asprintf(&p, "%u.%u.%u.%u", - a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF) < 0) - return -ENOMEM; - } - + a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF); + if (r < 0) + return -ENOMEM; break; } @@ -514,30 +513,31 @@ int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF }; - if (translate_ipv6 && memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { + if (translate_ipv6 && + memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { const uint8_t *a = sa->in6.sin6_addr.s6_addr+12; - if (include_port) { - if (asprintf(&p, + if (include_port) + r = asprintf(&p, "%u.%u.%u.%u:%u", a[0], a[1], a[2], a[3], - ntohs(sa->in6.sin6_port)) < 0) - return -ENOMEM; - } else { - if (asprintf(&p, + ntohs(sa->in6.sin6_port)); + else + r = asprintf(&p, "%u.%u.%u.%u", - a[0], a[1], a[2], a[3]) < 0) - return -ENOMEM; - } + a[0], a[1], a[2], a[3]); + if (r < 0) + return -ENOMEM; } else { char a[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a)); if (include_port) { - if (asprintf(&p, + r = asprintf(&p, "[%s]:%u", a, - ntohs(sa->in6.sin6_port)) < 0) + ntohs(sa->in6.sin6_port)); + if (r < 0) return -ENOMEM; } else { p = strdup(a); |