From 356c8008dd8c8c135bf8caeb250a6cb8cea5c36a Mon Sep 17 00:00:00 2001 From: Frank Engelhardt Date: Thu, 11 Jun 2020 16:22:26 +0200 Subject: [PATCH 1/2] posix_sockets: use sin6_scope_id field to specify netif --- sys/posix/sockets/posix_sockets.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sys/posix/sockets/posix_sockets.c b/sys/posix/sockets/posix_sockets.c index f98cb233ad..5781e63899 100644 --- a/sys/posix/sockets/posix_sockets.c +++ b/sys/posix/sockets/posix_sockets.c @@ -238,6 +238,9 @@ static int _sockaddr_to_ep(const struct sockaddr *address, socklen_t address_len out->family = AF_INET6; memcpy(&out->addr.ipv6, &in6_addr->sin6_addr, sizeof(out->addr.ipv6)); out->port = ntohs(in6_addr->sin6_port); + if (in6_addr->sin6_scope_id != 0) { + out->netif = (uint16_t) in6_addr->sin6_scope_id; + } break; #endif default: From 76cdc1b4d87d1217c3a7138065cc24027d39000b Mon Sep 17 00:00:00 2001 From: Frank Engelhardt Date: Thu, 11 Jun 2020 16:24:29 +0200 Subject: [PATCH 2/2] examples/posix_sockets: parse ipv6 interface id --- examples/posix_sockets/udp.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/examples/posix_sockets/udp.c b/examples/posix_sockets/udp.c index b49edf9fc6..72c5dff351 100644 --- a/examples/posix_sockets/udp.c +++ b/examples/posix_sockets/udp.c @@ -33,6 +33,11 @@ #include #include +#ifdef SOCK_HAS_IPV6 +#include "net/ipv6/addr.h" /* for interface parsing */ +#include "net/netif.h" /* for resolving ipv6 scope */ +#endif /* SOCK_HAS_IPV6 */ + #include "thread.h" #define SERVER_MSG_QUEUE_SIZE (8) @@ -98,6 +103,20 @@ static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num src.sin6_family = AF_INET6; dst.sin6_family = AF_INET6; memset(&src.sin6_addr, 0, sizeof(src.sin6_addr)); + /* parse interface id */ +#ifdef SOCK_HAS_IPV6 + char *iface; + iface = ipv6_addr_split_iface(addr_str); /* also removes interface id */ + if (iface) { + netif_t *netif = netif_get_by_name(iface); + if (netif) { + dst.sin6_scope_id = (uint32_t) netif_get_id(netif); + } + else { + printf("unknown network interface %s\n", iface); + } + } +#endif /* SOCK_HAS_IPV6 */ /* parse destination address */ if (inet_pton(AF_INET6, addr_str, &dst.sin6_addr) != 1) { puts("Error: unable to parse destination address");