From 444922ea93378aedc200c5a998d39fa4118d8b71 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 8 Aug 2017 08:16:47 -0600 Subject: [PATCH] apps/examples/udp: Enable testing with the broadcast address. --- examples/udp/Kconfig | 7 +++++++ examples/udp/udp_client.c | 14 ++++++++++++++ examples/udp/udp_server.c | 13 +++++++++++++ 3 files changed, 34 insertions(+) diff --git a/examples/udp/Kconfig b/examples/udp/Kconfig index ba81ca0de..0d6b65a63 100644 --- a/examples/udp/Kconfig +++ b/examples/udp/Kconfig @@ -101,6 +101,13 @@ config EXAMPLES_UDP_IPv6 endchoice # IP Domain +config EXAMPLES_UDP_BROADCAST + bool "Broadcast outgoing messages" + default n + depends on NET_BROADCAST + ---help--- + Send and receive all UDP packets using the broadcast address. + if EXAMPLES_UDP_IPv4 comment "IPv4 addresses" diff --git a/examples/udp/udp_client.c b/examples/udp/udp_client.c index 6387963b7..3aabcbd2e 100644 --- a/examples/udp/udp_client.c +++ b/examples/udp/udp_client.c @@ -142,6 +142,10 @@ void udp_client(void) int sockfd; int nbytes; int offset; +#ifdef CONFIG_EXAMPLES_UDP_BROADCAST + int optval; + int ret; +#endif /* Create a new UDP socket */ @@ -152,6 +156,16 @@ void udp_client(void) exit(1); } +#ifdef CONFIG_EXAMPLES_UDP_BROADCAST + optval = 1; + ret = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(int)); + if (ret < 0) + { + printf("Failed to set SO_BROADCAST\n"); + exit(1); + } +#endif + /* Then send and receive 256 messages */ for (offset = 0; offset < 256; offset++) diff --git a/examples/udp/udp_server.c b/examples/udp/udp_server.c index 08de11a76..18f29b6a2 100644 --- a/examples/udp/udp_server.c +++ b/examples/udp/udp_server.c @@ -101,6 +101,9 @@ void udp_server(void) int nbytes; int optval; int offset; +#ifdef CONFIG_EXAMPLES_UDP_BROADCAST + int ret; +#endif /* Create a new UDP socket */ @@ -120,6 +123,16 @@ void udp_server(void) exit(1); } +#ifdef CONFIG_EXAMPLES_UDP_BROADCAST + optval = 1; + ret = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(int)); + if (ret < 0) + { + printf("Failed to set SO_BROADCAST\n"); + exit(1); + } +#endif + /* Bind the socket to a local address */ #ifdef CONFIG_EXAMPLES_UDP_IPv6