Merge branch 'bugfix/ipv6_examples_4.1' into 'release/v4.1'

socket-examples: IPv6 related update for examples to set correct scoped id (v4.1)

See merge request espressif/esp-idf!8583
This commit is contained in:
David Čermák 2020-05-15 13:58:42 +08:00
commit 32e20304a7
4 changed files with 21 additions and 13 deletions

View file

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

View file

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

View file

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

View file

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