From 87141efd1eb85efe8333519ec4902304feccd1a6 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 4 Feb 2020 16:38:23 +0100 Subject: [PATCH] socket-examples: IPv6 related update for examples to set correct scoped id The scope id must be present when connecting to IPv6 Local Link address. --- examples/protocols/sockets/scripts/tcpclient.py | 14 ++++++++------ examples/protocols/sockets/scripts/udpclient.py | 12 +++++++----- .../protocols/sockets/tcp_client/main/tcp_client.c | 4 +++- .../protocols/sockets/udp_client/main/udp_client.c | 4 +++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/examples/protocols/sockets/scripts/tcpclient.py b/examples/protocols/sockets/scripts/tcpclient.py index 84332e938..a1c1056fc 100644 --- a/examples/protocols/sockets/scripts/tcpclient.py +++ b/examples/protocols/sockets/scripts/tcpclient.py @@ -12,17 +12,19 @@ import sys # ----------- Config ---------- PORT = 3333 -IP_VERSION = 'IPv4' -IPV4 = '192.168.0.167' -IPV6 = 'FE80::32AE:A4FF:FE80:5288' +IP_VERSION = 'IPv6' +IPV4 = '192.168.0.42' +IPV6 = 'fd00:0000:0000:0000:260a:c4ff:fe09:885c' # ------------------------------- if IP_VERSION == 'IPv4': family_addr = socket.AF_INET - host = IPV4 + addr = (IPV4, PORT) elif IP_VERSION == 'IPv6': family_addr = socket.AF_INET6 - host = IPV6 + for res in socket.getaddrinfo(IPV6, PORT, socket.AF_INET6, + socket.SOCK_STREAM, socket.SOL_TCP): + af, socktype, proto, canonname, addr = res else: print('IP_VERSION must be IPv4 or IPv6') sys.exit(1) @@ -34,7 +36,7 @@ except socket.error as msg: sys.exit(1) try: - sock.connect((host, PORT)) + sock.connect((IPV6, PORT)) except socket.error as msg: print('Could not open socket: ', msg) sock.close() diff --git a/examples/protocols/sockets/scripts/udpclient.py b/examples/protocols/sockets/scripts/udpclient.py index 342cadbfe..2c31c419c 100644 --- a/examples/protocols/sockets/scripts/udpclient.py +++ b/examples/protocols/sockets/scripts/udpclient.py @@ -12,17 +12,19 @@ import sys # ----------- Config ---------- PORT = 3333 -IP_VERSION = 'IPv4' +IP_VERSION = 'IPv6' IPV4 = '192.168.0.167' -IPV6 = 'FE80::32AE:A4FF:FE80:5288' +IPV6 = 'fe80:0000:0000:0000:260a:c4ff:fe11:a1e0%wlp1s0' # ------------------------------- if IP_VERSION == 'IPv4': - host = IPV4 + addr = (IPV4, PORT) family_addr = socket.AF_INET elif IP_VERSION == 'IPv6': - host = IPV6 family_addr = socket.AF_INET6 + for res in socket.getaddrinfo(IPV6, PORT, socket.AF_INET6, + socket.SOCK_DGRAM, socket.SOL_UDP): + af, socktype, proto, canonname, addr = res else: print('IP_VERSION must be IPv4 or IPv6') sys.exit(1) @@ -37,7 +39,7 @@ except socket.error: while True: msg = input('Enter message to send : ') try: - sock.sendto(msg.encode(), (host, PORT)) + sock.sendto(msg.encode(), addr) reply, addr = sock.recvfrom(128) if not reply: break diff --git a/examples/protocols/sockets/tcp_client/main/tcp_client.c b/examples/protocols/sockets/tcp_client/main/tcp_client.c index 2d2e515a0..c50b87427 100644 --- a/examples/protocols/sockets/tcp_client/main/tcp_client.c +++ b/examples/protocols/sockets/tcp_client/main/tcp_client.c @@ -54,10 +54,12 @@ static void tcp_client_task(void *pvParameters) ip_protocol = IPPROTO_IP; inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1); #else // IPV6 - struct sockaddr_in6 dest_addr; + struct sockaddr_in6 dest_addr = { 0 }; inet6_aton(HOST_IP_ADDR, &dest_addr.sin6_addr); dest_addr.sin6_family = AF_INET6; dest_addr.sin6_port = htons(PORT); + // Setting scope_id to the connecting interface for correct routing if IPv6 Local Link supplied + dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); addr_family = AF_INET6; ip_protocol = IPPROTO_IPV6; inet6_ntoa_r(dest_addr.sin6_addr, addr_str, sizeof(addr_str) - 1); diff --git a/examples/protocols/sockets/udp_client/main/udp_client.c b/examples/protocols/sockets/udp_client/main/udp_client.c index 402d27274..28c9f79ce 100644 --- a/examples/protocols/sockets/udp_client/main/udp_client.c +++ b/examples/protocols/sockets/udp_client/main/udp_client.c @@ -55,10 +55,12 @@ static void udp_client_task(void *pvParameters) ip_protocol = IPPROTO_IP; inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1); #else // IPV6 - struct sockaddr_in6 dest_addr; + struct sockaddr_in6 dest_addr = { 0 }; inet6_aton(HOST_IP_ADDR, &dest_addr.sin6_addr); dest_addr.sin6_family = AF_INET6; dest_addr.sin6_port = htons(PORT); + // Setting scope_id to the connecting interface for correct routing if IPv6 Local Link supplied + dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); addr_family = AF_INET6; ip_protocol = IPPROTO_IPV6; inet6_ntoa_r(dest_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);