diff --git a/sys/net/sock/sock_util.c b/sys/net/sock/sock_util.c index 8241eb767d..0d84db97f9 100644 --- a/sys/net/sock/sock_util.c +++ b/sys/net/sock/sock_util.c @@ -39,32 +39,25 @@ int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint, char *addr_str, uint16_t *port) { - void *addr_ptr; + const void *addr_ptr; *addr_str = '\0'; - switch (endpoint->family) { - case AF_INET: - { - addr_ptr = (void*)&endpoint->addr.ipv4; - break; - } -#if defined(SOCK_HAS_IPV6) - case AF_INET6: - { - addr_ptr = (void*)&endpoint->addr.ipv6; - break; - } -#endif /* else fall through */ - default: - return -ENOTSUP; + if (IS_ACTIVE(SOCK_HAS_IPV4) && (endpoint->family == AF_INET)) { + addr_ptr = &endpoint->addr.ipv4; + } + else if (IS_ACTIVE(SOCK_HAS_IPV6) && (endpoint->family == AF_INET6)) { + addr_ptr = &endpoint->addr.ipv6; + } + else { + return -ENOTSUP; } if (!inet_ntop(endpoint->family, addr_ptr, addr_str, INET6_ADDRSTRLEN)) { return 0; } -#if defined(SOCK_HAS_IPV6) - if ((endpoint->family == AF_INET6) && endpoint->netif) { + if (IS_ACTIVE(SOCK_HAS_IPV6) && (endpoint->family == AF_INET6) && + endpoint->netif) { #ifdef MODULE_FMT char *tmp = addr_str + strlen(addr_str); *tmp++ = '%'; @@ -74,7 +67,6 @@ int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint, sprintf(addr_str + strlen(addr_str), "%%%4u", endpoint->netif); #endif } -#endif if (port) { *port = endpoint->port; @@ -239,18 +231,20 @@ int sock_tl_str2ep(struct _sock_tl_ep *ep_out, const char *str) hostbuf[hostlen] = '\0'; - if (!brackets_flag) { + if (!brackets_flag && IS_ACTIVE(SOCK_HAS_IPV4)) { if (inet_pton(AF_INET, hostbuf, &ep_out->addr.ipv4) == 1) { ep_out->family = AF_INET; return 0; } } -#if defined(SOCK_HAS_IPV6) - if (inet_pton(AF_INET6, hostbuf, ep_out->addr.ipv6) == 1) { - ep_out->family = AF_INET6; - return 0; + + if (IS_ACTIVE(SOCK_HAS_IPV6)) { + if (inet_pton(AF_INET6, hostbuf, ep_out->addr.ipv6) == 1) { + ep_out->family = AF_INET6; + return 0; + } } -#endif + return -EINVAL; } @@ -265,15 +259,12 @@ bool sock_tl_ep_equal(const struct _sock_tl_ep *a, } /* compare addresses */ - switch (a->family) { -#ifdef SOCK_HAS_IPV6 - case AF_INET6: - return (memcmp(a->addr.ipv6, b->addr.ipv6, 16) == 0); - -#endif - case AF_INET: - return (memcmp(a->addr.ipv4, b->addr.ipv4, 4) == 0); - default: - return false; + if (IS_ACTIVE(SOCK_HAS_IPV4) && (a->family == AF_INET)) { + return memcmp(a->addr.ipv4, b->addr.ipv6, 4) == 0; } + else if (IS_ACTIVE(SOCK_HAS_IPV6) && (a->family == AF_INET6)) { + return memcmp(a->addr.ipv6, b->addr.ipv6, 16) == 0; + } + + return false; } diff --git a/tests/unittests/tests-sock_util/Makefile.include b/tests/unittests/tests-sock_util/Makefile.include index 4d1e90cd79..79f2ccd850 100644 --- a/tests/unittests/tests-sock_util/Makefile.include +++ b/tests/unittests/tests-sock_util/Makefile.include @@ -4,3 +4,5 @@ USEMODULE += gnrc_ipv6 USEMODULE += ipv4_addr USEMODULE += ipv6_addr USEMODULE += posix_headers +# GNRC has no support for IPv4, still force it on for the unit test +USEMODULE += ipv4