Skip to content

Commit a15cc70

Browse files
mniestrojcarlescufi
authored andcommitted
samples: net: echo_server: set IPV6_V6ONLY
This sample assumes that two separate sockets can be bound on IPv4 and IPv6. On Linux (via Native Simulator Offloaded Sockets) this is possible when there is no IPv4 to IPv6 mapping. Same can be true to other offloaded sockets. CONFIG_NET_IPV4_MAPPING_TO_IPV6 is disabled for this sample, so IPv4 to IPv6 mapping is disabled for Zephyr native IPv6 layer. For offloaded sockets this option does not define whether mapping is enabled or not, so try to unconditionally (and without error checking) disable it. This patch fixes compatibility with NSOS, since two separate sockets can be bound on the same address and port, one for IPv4 and second for IPv6. Signed-off-by: Marcin Niestroj <[email protected]>
1 parent 3a9ae18 commit a15cc70

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

samples/net/sockets/echo_server/src/tcp.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ static int start_tcp_proto(struct data *data,
101101
}
102102
#endif
103103

104-
/* Prefer IPv6 temporary addresses */
105104
if (bind_addr->sa_family == AF_INET6) {
105+
/* Prefer IPv6 temporary addresses */
106106
optval = IPV6_PREFER_SRC_PUBLIC;
107107
(void)setsockopt(data->tcp.sock, IPPROTO_IPV6,
108108
IPV6_ADDR_PREFERENCES,
109109
&optval, sizeof(optval));
110+
111+
/*
112+
* Bind only to IPv6 without mapping to IPv4, since we bind to
113+
* IPv4 using another socket
114+
*/
115+
optval = 1;
116+
(void)setsockopt(data->tcp.sock, IPPROTO_IPV6, IPV6_V6ONLY,
117+
&optval, sizeof(optval));
110118
}
111119

112120
ret = bind(data->tcp.sock, bind_addr, bind_addrlen);

samples/net/sockets/echo_server/src/udp.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,20 @@ static int start_udp_proto(struct data *data, struct sockaddr *bind_addr,
7878
}
7979
#endif
8080

81-
/* Prefer IPv6 temporary addresses */
8281
if (bind_addr->sa_family == AF_INET6) {
82+
/* Prefer IPv6 temporary addresses */
8383
optval = IPV6_PREFER_SRC_PUBLIC;
8484
(void)setsockopt(data->tcp.sock, IPPROTO_IPV6,
8585
IPV6_ADDR_PREFERENCES,
8686
&optval, sizeof(optval));
87+
88+
/*
89+
* Bind only to IPv6 without mapping to IPv4, since we bind to
90+
* IPv4 using another socket
91+
*/
92+
optval = 1;
93+
(void)setsockopt(data->tcp.sock, IPPROTO_IPV6, IPV6_V6ONLY,
94+
&optval, sizeof(optval));
8795
}
8896

8997
ret = bind(data->udp.sock, bind_addr, bind_addrlen);

0 commit comments

Comments
 (0)