1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/net/sock_util: make use of SOCK_HAS_IPV4

This commit is contained in:
Benjamin Valentin 2022-01-13 12:55:06 +01:00
parent 22c46b04c9
commit 67ef5faeed
2 changed files with 28 additions and 35 deletions

View File

@ -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;
}

View File

@ -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