samples: sockets: echo_async: Make IPV6 optional

Changing the example to not use IPv6 when it is not configured,
given IPv6 is not enabled on some platforms such as cc3220sf_launchxl.

Signed-off-by: Vincent Wan <vincent.wan@linaro.org>
This commit is contained in:
Vincent Wan 2019-03-13 21:09:12 -07:00 committed by Kumar Gala
parent a69955701d
commit 734e247d02

View File

@ -18,12 +18,18 @@
#include <fcntl.h>
#include <poll.h>
#define USE_IPV6
#else
#include <fcntl.h>
#include <net/socket.h>
#include <kernel.h>
#ifdef CONFIG_NET_IPV6
#define USE_IPV6
#endif
#endif
/* For Zephyr, keep max number of fd's in sync with max poll() capacity */
@ -102,7 +108,8 @@ int main(void)
{
int res;
static int counter;
int serv4, serv6;
int num_servs = 0;
int serv4;
struct sockaddr_in bind_addr4 = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
@ -110,11 +117,14 @@ int main(void)
.s_addr = htonl(INADDR_ANY),
},
};
#ifdef USE_IPV6
int serv6;
struct sockaddr_in6 bind_addr6 = {
.sin6_family = AF_INET6,
.sin6_port = htons(PORT),
.sin6_addr = IN6ADDR_ANY_INIT,
};
#endif
serv4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serv4 < 0) {
@ -126,7 +136,13 @@ int main(void)
if (res == -1) {
printf("Cannot bind IPv4, errno: %d\n", errno);
}
num_servs++;
setblocking(serv4, false);
listen(serv4, 5);
pollfds_add(serv4);
#ifdef USE_IPV6
serv6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (serv6 < 0) {
printf("error: socket(AF_INET6): %d\n", errno);
@ -147,14 +163,12 @@ int main(void)
if (res == -1) {
printf("Cannot bind IPv6, errno: %d\n", errno);
}
num_servs++;
setblocking(serv4, false);
setblocking(serv6, false);
listen(serv4, 5);
listen(serv6, 5);
pollfds_add(serv4);
pollfds_add(serv6);
#endif
printf("Asynchronous TCP echo server waits for connections on port %d...\n", PORT);
@ -174,7 +188,7 @@ int main(void)
continue;
}
int fd = pollfds[i].fd;
if (i < 2) {
if (i < num_servs) {
/* If server socket */
int client = accept(fd, (struct sockaddr *)&client_addr,
&client_addr_len);