Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/net/sock_util: fix compilation for IPv4-only mode #17766

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 29 additions & 19 deletions sys/net/sock/sock_util.c
Expand Up @@ -42,22 +42,26 @@ int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint,
const void *addr_ptr;
*addr_str = '\0';

if (IS_ACTIVE(SOCK_HAS_IPV4) && (endpoint->family == AF_INET)) {
switch (endpoint->family) {
#ifdef SOCK_HAS_IPV4
case AF_INET:
addr_ptr = &endpoint->addr.ipv4;
}
else if (IS_ACTIVE(SOCK_HAS_IPV6) && (endpoint->family == AF_INET6)) {
break;
#endif
#ifdef SOCK_HAS_IPV6
case AF_INET6:
addr_ptr = &endpoint->addr.ipv6;
}
else {
break;
#endif
default:
return -ENOTSUP;
}

if (!inet_ntop(endpoint->family, addr_ptr, addr_str, INET6_ADDRSTRLEN)) {
return 0;
}

if (IS_ACTIVE(SOCK_HAS_IPV6) && (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++ = '%';
Expand Down Expand Up @@ -231,19 +235,21 @@ int sock_tl_str2ep(struct _sock_tl_ep *ep_out, const char *str)

hostbuf[hostlen] = '\0';

if (!brackets_flag && IS_ACTIVE(SOCK_HAS_IPV4)) {
if (!brackets_flag) {
#ifdef SOCK_HAS_IPV4
if (inet_pton(AF_INET, hostbuf, &ep_out->addr.ipv4) == 1) {
ep_out->family = AF_INET;
return 0;
}
#endif
}

if (IS_ACTIVE(SOCK_HAS_IPV6)) {
if (inet_pton(AF_INET6, hostbuf, ep_out->addr.ipv6) == 1) {
ep_out->family = AF_INET6;
return 0;
}
#ifdef SOCK_HAS_IPV6
if (inet_pton(AF_INET6, hostbuf, ep_out->addr.ipv6) == 1) {
ep_out->family = AF_INET6;
return 0;
}
#endif

return -EINVAL;
}
Expand All @@ -259,12 +265,16 @@ bool sock_tl_ep_equal(const struct _sock_tl_ep *a,
}

/* compare addresses */
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)) {
switch (a->family) {
#ifdef SOCK_HAS_IPV4
case AF_INET:
return memcmp(a->addr.ipv4, b->addr.ipv4, 4) == 0;
#endif
#ifdef SOCK_HAS_IPV6
case AF_INET6:
return memcmp(a->addr.ipv6, b->addr.ipv6, 16) == 0;
#endif
default:
return false;
}

return false;
}