summaryrefslogtreecommitdiff
path: root/apps/examples/udp
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-03-20 18:18:19 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-03-20 18:18:19 +0000
commiteafaeb9398216dacb92de69683ccdda6007efb1d (patch)
treee513e4fd791670d9b74d33821682d4059ed73098 /apps/examples/udp
parentf68c474e8ae9cfa5c9124a6eb92ec06fe2bf40a1 (diff)
downloadnuttx-eafaeb9398216dacb92de69683ccdda6007efb1d.tar.gz
nuttx-eafaeb9398216dacb92de69683ccdda6007efb1d.tar.bz2
nuttx-eafaeb9398216dacb92de69683ccdda6007efb1d.zip
Move nuttx/examples to apps/examples
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3405 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'apps/examples/udp')
-rw-r--r--apps/examples/udp/Makefile119
-rw-r--r--apps/examples/udp/host.c63
-rw-r--r--apps/examples/udp/target.c104
-rw-r--r--apps/examples/udp/udp-client.c133
-rw-r--r--apps/examples/udp/udp-internal.h90
-rw-r--r--apps/examples/udp/udp-server.c173
6 files changed, 682 insertions, 0 deletions
diff --git a/apps/examples/udp/Makefile b/apps/examples/udp/Makefile
new file mode 100644
index 000000000..e0789eda8
--- /dev/null
+++ b/apps/examples/udp/Makefile
@@ -0,0 +1,119 @@
+############################################################################
+# apps/examples/udp/Makefile
+#
+# Copyright (C) 2007-2008, 2010 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name NuttX nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################################
+
+-include $(TOPDIR)/.config
+-include $(TOPDIR)/Make.defs
+include $(APPDIR)/Make.defs
+
+# UDP Test
+
+TARG_ASRCS =
+
+TARG_CSRCS = target.c
+ifeq ($(CONFIG_EXAMPLE_UDP_SERVER),y)
+TARG_CSRCS += udp-server.c
+else
+TARG_CSRCS += udp-client.c
+endif
+
+TARG_AOBJS = $(TARG_ASRCS:.S=$(OBJEXT))
+TARG_COBJS = $(TARG_CSRCS:.c=$(OBJEXT))
+
+TARG_SRCS = $(TARG_ASRCS) $(TARG_CSRCS)
+TARG_OBJS = $(TARG_AOBJS) $(TARG_COBJS)
+
+TARG_BIN = ../../libapps$(LIBEXT)
+
+HOSTCFLAGS += -DCONFIG_EXAMPLE_UDP_HOST=1
+ifeq ($(CONFIG_EXAMPLE_UDP_SERVER),y)
+HOSTCFLAGS += -DCONFIG_EXAMPLE_UDP_SERVER=1 \
+ -DCONFIG_EXAMPLE_UDP_SERVERIP="$(CONFIG_EXAMPLE_UDP_SERVERIP)"
+endif
+
+HOST_SRCS = host.c
+ifeq ($(CONFIG_EXAMPLE_UDP_SERVER),y)
+HOST_SRCS += udp-client.c
+else
+HOST_SRCS += udp-server.c
+endif
+
+HOST_OBJS = $(HOST_SRCS:.c=.o)
+HOST_BIN = host
+
+ROOTDEPPATH = --dep-path .
+
+# Common build
+
+VPATH =
+
+all: .built
+.PHONY: .built clean depend disclean
+
+$(TARG_AOBJS): %$(OBJEXT): %.S
+ $(call ASSEMBLE, $<, $@)
+
+$(TARG_COBJS): %$(OBJEXT): %.c
+ $(call COMPILE, $<, $@)
+
+$(TARG_BIN): $(TARG_OBJS) $(HOST_BIN)
+ @( for obj in $(TARG_OBJS) ; do \
+ $(call ARCHIVE, $@, $${obj}); \
+ done ; )
+ @touch .built
+
+$(HOST_OBJS): %.o: %.c
+ $(HOSTCC) -c $(HOSTCFLAGS) $< -o $@
+
+$(HOST_BIN): $(HOST_OBJS)
+ $(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS) -o $@
+
+.built: $(TARG_BIN) $(HOST_BIN)
+
+.depend: Makefile $(TARG_SRCS)
+ @$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(TARG_SRCS) >Make.dep
+ @touch $@
+
+# Register application
+depend: .depend
+
+clean:
+ @rm -f $(TARG_BIN) $(HOST_BIN) *.o *~ .*.swp .built
+ $(call CLEAN)
+
+distclean: clean
+ @rm -f Make.dep .depend
+
+-include Make.dep
+
diff --git a/apps/examples/udp/host.c b/apps/examples/udp/host.c
new file mode 100644
index 000000000..34fd96765
--- /dev/null
+++ b/apps/examples/udp/host.c
@@ -0,0 +1,63 @@
+/****************************************************************************
+ * examples/udp/host.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "udp-internal.h"
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * main
+ ****************************************************************************/
+
+int main(int argc, char **argv, char **envp)
+{
+#ifdef CONFIG_EXAMPLE_UDP_SERVER
+ send_client();
+#else
+ recv_server();
+#endif
+
+ return 0;
+}
diff --git a/apps/examples/udp/target.c b/apps/examples/udp/target.c
new file mode 100644
index 000000000..5e31fdb36
--- /dev/null
+++ b/apps/examples/udp/target.c
@@ -0,0 +1,104 @@
+/****************************************************************************
+ * examples/udp/nettest.c
+ *
+ * Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <stdio.h>
+#include <debug.h>
+
+#include <net/uip/uip.h>
+#include <apps/netutils/uiplib.h>
+
+#include "udp-internal.h"
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * user_initialize
+ ****************************************************************************/
+
+#ifndef CONFIG_HAVE_WEAKFUNCTIONS
+void user_initialize(void)
+{
+ /* Stub that must be provided only if the toolchain does
+ * not support weak functions.
+ */
+}
+#endif
+
+/****************************************************************************
+ * user_start
+ ****************************************************************************/
+
+int user_start(int argc, char *argv[])
+{
+ struct in_addr addr;
+
+ /* Set up our host address */
+
+ addr.s_addr = HTONL(CONFIG_EXAMPLE_UDP_IPADDR);
+ uip_sethostaddr("eth0", &addr);
+
+ /* Set up the default router address */
+
+ addr.s_addr = HTONL(CONFIG_EXAMPLE_UDP_DRIPADDR);
+ uip_setdraddr("eth0", &addr);
+
+ /* Setup the subnet mask */
+
+ addr.s_addr = HTONL(CONFIG_EXAMPLE_UDP_NETMASK);
+ uip_setnetmask("eth0", &addr);
+
+#ifdef CONFIG_EXAMPLE_UDP_SERVER
+ recv_server();
+#else
+ send_client();
+#endif
+
+ return 0;
+}
diff --git a/apps/examples/udp/udp-client.c b/apps/examples/udp/udp-client.c
new file mode 100644
index 000000000..545a3a6c3
--- /dev/null
+++ b/apps/examples/udp/udp-client.c
@@ -0,0 +1,133 @@
+/****************************************************************************
+ * examples/udp/udp-client.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "udp-internal.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static inline void fill_buffer(unsigned char *buf, int offset)
+{
+ int ch;
+ int j;
+
+ buf[0] = offset;
+ for (ch = 0x20, j = offset + 1; ch < 0x7f; ch++, j++)
+ {
+ if (j >= SENDSIZE)
+ {
+ j = 1;
+ }
+ buf[j] = ch;
+ }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void send_client(void)
+{
+ struct sockaddr_in server;
+ unsigned char outbuf[SENDSIZE];
+ int sockfd;
+ int nbytes;
+ int offset;
+
+ /* Create a new TCP socket */
+
+ sockfd = socket(PF_INET, SOCK_DGRAM, 0);
+ if (sockfd < 0)
+ {
+ message("client socket failure %d\n", errno);
+ exit(1);
+ }
+
+ /* Then send and receive 256 messages */
+
+ for (offset = 0; offset < 256; offset++)
+ {
+ /* Set up the output buffer */
+
+ fill_buffer(outbuf, offset);
+
+ /* Send the message */
+
+ server.sin_family = AF_INET;
+ server.sin_port = HTONS(PORTNO);
+ server.sin_addr.s_addr = HTONL(CONFIG_EXAMPLE_UDP_SERVERIP);
+
+ message("client: %d. Sending %d bytes\n", offset, SENDSIZE);
+ nbytes = sendto(sockfd, outbuf, SENDSIZE, 0,
+ (struct sockaddr*)&server, sizeof(struct sockaddr_in));
+ message("client: %d. Sent %d bytes\n", offset, nbytes);
+
+ if (nbytes < 0)
+ {
+ message("client: %d. sendto failed: %d\n", offset, errno);
+ close(sockfd);
+ exit(-1);
+ }
+ else if (nbytes != SENDSIZE)
+ {
+ message("client: %d. Bad send length: %d Expected: %d\n",
+ offset, nbytes, SENDSIZE);
+ close(sockfd);
+ exit(-1);
+ }
+
+ /* Now, sleep a bit. No packets should be dropped due to overrunning
+ * the server.
+ */
+
+ sleep(2);
+ }
+ close(sockfd);
+}
diff --git a/apps/examples/udp/udp-internal.h b/apps/examples/udp/udp-internal.h
new file mode 100644
index 000000000..94fe1e482
--- /dev/null
+++ b/apps/examples/udp/udp-internal.h
@@ -0,0 +1,90 @@
+/****************************************************************************
+ * examples/udp/udp-internal.h
+ *
+ * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+#ifndef __EXAMPLES_UIP_INTERNAL_H
+#define __EXAMPLES_UIP_INTERNAL_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#ifdef CONFIG_EXAMPLE_UDP_HOST
+#else
+# include <debug.h>
+#endif
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_EXAMPLE_UDP_HOST
+ /* HTONS/L macros are unique to uIP */
+
+# define HTONS(a) htons(a)
+# define HTONL(a) htonl(a)
+
+ /* Used printf for debug output */
+
+# define message(...) printf(__VA_ARGS__)
+
+ /* Have SO_LINGER */
+
+#else
+
+ /* If debug is enabled, use the synchronous lib_lowprintf so that the
+ * program output does not get disassociated in the debug output.
+ */
+
+# ifdef CONFIG_DEBUG
+# define message(...) lib_lowprintf(__VA_ARGS__)
+# else
+# define message(...) printf(__VA_ARGS__)
+# endif
+
+#endif
+
+#define PORTNO 5471
+
+#define ASCIISIZE (0x7f - 0x20)
+#define SENDSIZE (ASCIISIZE+1)
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+extern void send_client(void);
+extern void recv_server(void);
+
+#endif /* __EXAMPLES_UIP_INTERNAL_H */
diff --git a/apps/examples/udp/udp-server.c b/apps/examples/udp/udp-server.c
new file mode 100644
index 000000000..495a71320
--- /dev/null
+++ b/apps/examples/udp/udp-server.c
@@ -0,0 +1,173 @@
+/****************************************************************************
+ * examples/udp/udp-server.c
+ *
+ * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "udp-internal.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static inline int check_buffer(unsigned char *buf)
+{
+ int ret = 1;
+ int offset;
+ int ch;
+ int j;
+
+ offset = buf[0];
+ for (ch = 0x20, j = offset + 1; ch < 0x7f; ch++, j++)
+ {
+ if (j >= SENDSIZE)
+ {
+ j = 1;
+ }
+ if (buf[j] != ch)
+ {
+ message("server: Buffer content error for offset=%d, index=%d\n", offset, j);
+ ret = 0;
+ }
+ }
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void recv_server(void)
+{
+ struct sockaddr_in server;
+ struct sockaddr_in client;
+ in_addr_t tmpaddr;
+ unsigned char inbuf[1024];
+ int sockfd;
+ int nbytes;
+ int optval;
+ int offset;
+ socklen_t addrlen;
+
+ /* Create a new UDP socket */
+
+ sockfd = socket(PF_INET, SOCK_DGRAM, 0);
+ if (sockfd < 0)
+ {
+ message("server: socket failure: %d\n", errno);
+ exit(1);
+ }
+
+ /* Set socket to reuse address */
+
+ optval = 1;
+ if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
+ {
+ message("server: setsockopt SO_REUSEADDR failure: %d\n", errno);
+ exit(1);
+ }
+
+ /* Bind the socket to a local address */
+
+ server.sin_family = AF_INET;
+ server.sin_port = HTONS(PORTNO);
+ server.sin_addr.s_addr = HTONL(INADDR_ANY);
+
+ if (bind(sockfd, (struct sockaddr*)&server, sizeof(struct sockaddr_in)) < 0)
+ {
+ message("server: bind failure: %d\n", errno);
+ exit(1);
+ }
+
+ /* Then receive up to 256 packets of data */
+
+ for (offset = 0; offset < 256; offset++)
+ {
+ message("server: %d. Receiving up 1024 bytes\n", offset);
+ addrlen = sizeof(struct sockaddr_in);
+ nbytes = recvfrom(sockfd, inbuf, 1024, 0,
+ (struct sockaddr*)&client, &addrlen);
+
+ tmpaddr = ntohl(client.sin_addr.s_addr);
+ message("server: %d. Received %d bytes from %d.%d.%d.%d:%d\n",
+ offset, nbytes,
+ tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
+ (tmpaddr >> 8) & 0xff, tmpaddr & 0xff,
+ ntohs(client.sin_port));
+
+ if (nbytes < 0)
+ {
+ message("server: %d. recv failed: %d\n", offset, errno);
+ close(sockfd);
+ exit(-1);
+ }
+
+ if (nbytes != SENDSIZE)
+ {
+ message("server: %d. recv size incorrect: %d vs %d\n", offset, nbytes, SENDSIZE);
+ close(sockfd);
+ exit(-1);
+ }
+
+ if (offset < inbuf[0])
+ {
+ message("server: %d. %d packets lost, resetting offset\n", offset, inbuf[0] - offset);
+ offset = inbuf[0];
+ }
+ else if (offset > inbuf[0])
+ {
+ message("server: %d. Bad offset in buffer: %d\n", offset, inbuf[0]);
+ close(sockfd);
+ exit(-1);
+ }
+
+ if (!check_buffer(inbuf))
+ {
+ message("server: %d. Bad buffer contents\n", offset);
+ close(sockfd);
+ exit(-1);
+ }
+ }
+ close(sockfd);
+}