mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
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.
This commit is contained in:
parent
7b01509b19
commit
acf1478a51
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user