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 <m.niestroj@emb.dev>
This commit is contained in:
Marcin Niestroj 2024-05-06 09:41:12 +02:00 committed by Carles Cufí
commit a15cc704e0
2 changed files with 18 additions and 2 deletions

View file

@ -101,12 +101,20 @@ static int start_tcp_proto(struct data *data,
}
#endif
/* Prefer IPv6 temporary addresses */
if (bind_addr->sa_family == AF_INET6) {
/* Prefer IPv6 temporary addresses */
optval = IPV6_PREFER_SRC_PUBLIC;
(void)setsockopt(data->tcp.sock, IPPROTO_IPV6,
IPV6_ADDR_PREFERENCES,
&optval, sizeof(optval));
/*
* Bind only to IPv6 without mapping to IPv4, since we bind to
* IPv4 using another socket
*/
optval = 1;
(void)setsockopt(data->tcp.sock, IPPROTO_IPV6, IPV6_V6ONLY,
&optval, sizeof(optval));
}
ret = bind(data->tcp.sock, bind_addr, bind_addrlen);

View file

@ -78,12 +78,20 @@ static int start_udp_proto(struct data *data, struct sockaddr *bind_addr,
}
#endif
/* Prefer IPv6 temporary addresses */
if (bind_addr->sa_family == AF_INET6) {
/* Prefer IPv6 temporary addresses */
optval = IPV6_PREFER_SRC_PUBLIC;
(void)setsockopt(data->tcp.sock, IPPROTO_IPV6,
IPV6_ADDR_PREFERENCES,
&optval, sizeof(optval));
/*
* Bind only to IPv6 without mapping to IPv4, since we bind to
* IPv4 using another socket
*/
optval = 1;
(void)setsockopt(data->tcp.sock, IPPROTO_IPV6, IPV6_V6ONLY,
&optval, sizeof(optval));
}
ret = bind(data->udp.sock, bind_addr, bind_addrlen);