From eafaeb9398216dacb92de69683ccdda6007efb1d Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 20 Mar 2011 18:18:19 +0000 Subject: Move nuttx/examples to apps/examples git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3405 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/nettest/Makefile | 121 +++++++++++++++++ apps/examples/nettest/host.c | 63 +++++++++ apps/examples/nettest/nettest.c | 122 +++++++++++++++++ apps/examples/nettest/nettest.h | 95 +++++++++++++ apps/examples/nettest/nettest_client.c | 204 ++++++++++++++++++++++++++++ apps/examples/nettest/nettest_server.c | 234 +++++++++++++++++++++++++++++++++ 6 files changed, 839 insertions(+) create mode 100644 apps/examples/nettest/Makefile create mode 100644 apps/examples/nettest/host.c create mode 100644 apps/examples/nettest/nettest.c create mode 100644 apps/examples/nettest/nettest.h create mode 100644 apps/examples/nettest/nettest_client.c create mode 100644 apps/examples/nettest/nettest_server.c (limited to 'apps/examples/nettest') diff --git a/apps/examples/nettest/Makefile b/apps/examples/nettest/Makefile new file mode 100644 index 000000000..37d00914e --- /dev/null +++ b/apps/examples/nettest/Makefile @@ -0,0 +1,121 @@ +############################################################################ +# examples/nettest/Makefile +# +# Copyright (C) 2007-2008, 2010-2010 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# 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 + +# Basic TCP networking test + +TARG_ASRCS = +TARG_AOBJS = $(TARG_ASRCS:.S=$(OBJEXT)) + +TARG_CSRCS = nettest.c +ifeq ($(CONFIG_EXAMPLE_NETTEST_SERVER),y) +TARG_CSRCS += nettest_server.c +else +TARG_CSRCS += nettest_client.c +endif + +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_NETTEST_HOST=1 +ifeq ($(CONFIG_EXAMPLE_NETTEST_SERVER),y) +HOSTCFLAGS += -DCONFIG_EXAMPLE_NETTEST_SERVER=1 \ + -DCONFIG_EXAMPLE_NETTEST_CLIENTIP="$(CONFIG_EXAMPLE_NETTEST_CLIENTIP)" +endif + +HOST_SRCS = host.c +ifeq ($(CONFIG_EXAMPLE_NETTEST_SERVER),y) +HOST_SRCS += nettest_client.c +else +HOST_SRCS += nettest_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 ; ) + +$(HOST_OBJS): %.o: %.c + @echo "CC: $<" + @$(HOSTCC) -c $(HOSTCFLAGS) $< -o $@ + +$(HOST_BIN): $(HOST_OBJS) + @echo "LD: $@" + @$(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS) -o $@ + +.built: $(TARG_BIN) $(HOST_BIN) + @touch .built + +.depend: Makefile $(TARG_SRCS) + @$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(TARG_SRCS) >Make.dep + @touch $@ + +# Register application +depend: .depend + +clean: + @rm -f $(TARG_BIN) $(HOST_BIN) .built *.o *~ .*.swp + $(call CLEAN) + +distclean: clean + @rm -f Make.dep .depend + +-include Make.dep + diff --git a/apps/examples/nettest/host.c b/apps/examples/nettest/host.c new file mode 100644 index 000000000..8b0bffd7d --- /dev/null +++ b/apps/examples/nettest/host.c @@ -0,0 +1,63 @@ +/**************************************************************************** + * examples/nettest/host.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 "nettest.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * main + ****************************************************************************/ + +int main(int argc, char **argv, char **envp) +{ +#ifdef CONFIG_EXAMPLE_NETTEST_SERVER + send_client(); +#else + recv_server(); +#endif + + return 0; +} diff --git a/apps/examples/nettest/nettest.c b/apps/examples/nettest/nettest.c new file mode 100644 index 000000000..c7492fff6 --- /dev/null +++ b/apps/examples/nettest/nettest.c @@ -0,0 +1,122 @@ +/**************************************************************************** + * examples/nettest/nettest.c + * + * Copyright (C) 2007, 2009-2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include + +#include +#include +#include + +#include "nettest.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; +#ifdef CONFIG_EXAMPLE_NETTEST_NOMAC + uint8_t mac[IFHWADDRLEN]; +#endif + +/* Many embedded network interfaces must have a software assigned MAC */ + +#ifdef CONFIG_EXAMPLE_NETTEST_NOMAC + mac[0] = 0x00; + mac[1] = 0xe0; + mac[2] = 0xb0; + mac[3] = 0x0b; + mac[4] = 0xba; + mac[5] = 0xbe; + uip_setmacaddr("eth0", mac); +#endif + + /* Set up our host address */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_NETTEST_IPADDR); + uip_sethostaddr("eth0", &addr); + + /* Set up the default router address */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_NETTEST_DRIPADDR); + uip_setdraddr("eth0", &addr); + + /* Setup the subnet mask */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_NETTEST_NETMASK); + uip_setnetmask("eth0", &addr); + +#ifdef CONFIG_EXAMPLE_NETTEST_SERVER + recv_server(); +#else + send_client(); +#endif + + return 0; +} diff --git a/apps/examples/nettest/nettest.h b/apps/examples/nettest/nettest.h new file mode 100644 index 000000000..b5b619175 --- /dev/null +++ b/apps/examples/nettest/nettest.h @@ -0,0 +1,95 @@ +/**************************************************************************** + * examples/nettest/nettest.h + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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_NETTEST_H +#define __EXAMPLES_NETTEST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLE_NETTEST_HOST +#else +# include +#endif + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLE_NETTEST_HOST + /* HTONS/L macros are unique to uIP */ + +# define HTONS(a) htons(a) +# define HTONL(a) htonl(a) + + /* Used printf for debug output */ + +# ifdef CONFIG_CPP_HAVE_VARARGS +# define message(...) printf(__VA_ARGS__) +# else +# define message printf +# endif + + /* Have SO_LINGER */ + +# define NETTEST_HAVE_SOLINGER 1 + +#else + + /* Used lib_rawprintf() so that there is not confusion from buffered IO */ + +# ifdef CONFIG_CPP_HAVE_VARARGS +# define message(...) lib_rawprintf(__VA_ARGS__) +# else +# define message lib_rawprintf +# endif + + /* At present, uIP does only abortive disconnects */ + +# undef NETTEST_HAVE_SOLINGER +#endif + +#define PORTNO 5471 +#define SENDSIZE 4096 + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +extern void send_client(void); +extern void recv_server(void); + +#endif /* __EXAMPLES_NETTEST_H */ diff --git a/apps/examples/nettest/nettest_client.c b/apps/examples/nettest/nettest_client.c new file mode 100644 index 000000000..b879f8438 --- /dev/null +++ b/apps/examples/nettest/nettest_client.c @@ -0,0 +1,204 @@ +/**************************************************************************** + * examples/nettest/nettest-client.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#include + +#include +#include +#include +#include +#include + +#include "nettest.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void send_client(void) +{ + struct sockaddr_in myaddr; + char *outbuf; +#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE + char *inbuf; +#endif + int sockfd; + int nbytessent; +#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE + int nbytesrecvd; + int totalbytesrecvd; +#endif + int ch; + int i; + + /* Allocate buffers */ + + outbuf = (char*)malloc(SENDSIZE); +#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE + inbuf = (char*)malloc(SENDSIZE); + if (!outbuf || !inbuf) +#else + if (!outbuf) +#endif + { + message("client: failed to allocate buffers\n"); + exit(1); + } + + + /* Create a new TCP socket */ + + sockfd = socket(PF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + { + message("client socket failure %d\n", errno); + goto errout_with_buffers; + } + + /* Connect the socket to the server */ + + myaddr.sin_family = AF_INET; + myaddr.sin_port = HTONS(PORTNO); +#if 0 + myaddr.sin_addr.s_addr = HTONL(INADDR_LOOPBACK); +#else + myaddr.sin_addr.s_addr = HTONL(CONFIG_EXAMPLE_NETTEST_CLIENTIP); +#endif + + message("client: Connecting...\n"); + if (connect( sockfd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0) + { + message("client: connect failure: %d\n", errno); + goto errout_with_socket; + } + message("client: Connected\n"); + + /* Initialize the buffer */ + + ch = 0x20; + for (i = 0; i < SENDSIZE; i++ ) + { + outbuf[i] = ch; + if (++ch > 0x7e) + { + ch = 0x20; + } + } + +#ifdef CONFIG_EXAMPLE_NETTEST_PERFORMANCE + /* Then receive messages forever */ + + for (;;) + { + nbytessent = send(sockfd, outbuf, 512, 0); + if (nbytessent < 0) + { + message("client: send failed: %d\n", errno); + goto errout_with_socket; + } + else if (nbytessent != 512) + { + message("client: Bad send length=%d: %d\n", nbytessent); + goto errout_with_socket; + } + } +#else + /* Then send and receive one message */ + + message("client: Sending %d bytes\n", SENDSIZE); + nbytessent = send(sockfd, outbuf, SENDSIZE, 0); + message("client: Sent %d bytes\n", nbytessent); + + if (nbytessent < 0) + { + message("client: send failed: %d\n", errno); + goto errout_with_socket; + } + else if (nbytessent != SENDSIZE) + { + message("client: Bad send length: %d Expected: %d\n", nbytessent, SENDSIZE); + goto errout_with_socket; + } + + totalbytesrecvd = 0; + do + { + message("client: Receiving...\n"); + nbytesrecvd = recv(sockfd, &inbuf[totalbytesrecvd], SENDSIZE - totalbytesrecvd, 0); + + if (nbytesrecvd < 0) + { + message("client: recv failed: %d\n", errno); + goto errout_with_socket; + } + totalbytesrecvd += nbytesrecvd; + message("client: Received %d of %d bytes\n", totalbytesrecvd, SENDSIZE); + } + while (totalbytesrecvd < SENDSIZE); + + if (totalbytesrecvd != SENDSIZE) + { + message("client: Bad recv length: %d Expected: %d\n", totalbytesrecvd, SENDSIZE); + goto errout_with_socket; + } + else if (memcmp(inbuf, outbuf, SENDSIZE) != 0) + { + message("client: Received buffer does not match sent buffer\n"); + goto errout_with_socket; + } + + close(sockfd); + free(outbuf); +#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE + free(inbuf); +#endif + return; +#endif + +errout_with_socket: + close(sockfd); + +errout_with_buffers: + free(outbuf); +#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE + free(inbuf); +#endif + exit(1); +} diff --git a/apps/examples/nettest/nettest_server.c b/apps/examples/nettest/nettest_server.c new file mode 100644 index 000000000..6d9ccaeba --- /dev/null +++ b/apps/examples/nettest/nettest_server.c @@ -0,0 +1,234 @@ +/**************************************************************************** + * examples/nettest/nettest-server.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#include +#include + +#include +#include +#include +#include + +#include "nettest.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void recv_server(void) +{ + struct sockaddr_in myaddr; +#ifdef NETTEST_HAVE_SOLINGER + struct linger ling; +#endif + char *buffer; + int listensd; + int acceptsd; + socklen_t addrlen; + int nbytesread; +#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE + int totalbytesread; + int nbytessent; + int ch; + int i; +#endif + int optval; + + /* Allocate a BIG buffer */ + + buffer = (char*)malloc(2*SENDSIZE); + if (!buffer) + { + message("server: failed to allocate buffer\n"); + exit(1); + } + + + /* Create a new TCP socket */ + + listensd = socket(PF_INET, SOCK_STREAM, 0); + if (listensd < 0) + { + message("server: socket failure: %d\n", errno); + goto errout_with_buffer; + } + + /* Set socket to reuse address */ + + optval = 1; + if (setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0) + { + message("server: setsockopt SO_REUSEADDR failure: %d\n", errno); + goto errout_with_listensd; + } + + /* Bind the socket to a local address */ + + myaddr.sin_family = AF_INET; + myaddr.sin_port = HTONS(PORTNO); + myaddr.sin_addr.s_addr = INADDR_ANY; + + if (bind(listensd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0) + { + message("server: bind failure: %d\n", errno); + goto errout_with_listensd; + } + + /* Listen for connections on the bound TCP socket */ + + if (listen(listensd, 5) < 0) + { + message("server: listen failure %d\n", errno); + goto errout_with_listensd; + } + + /* Accept only one connection */ + + message("server: Accepting connections on port %d\n", PORTNO); + addrlen = sizeof(struct sockaddr_in); + acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen); + if (acceptsd < 0) + { + message("server: accept failure: %d\n", errno); + goto errout_with_listensd; + } + message("server: Connection accepted -- receiving\n"); + + /* Configure to "linger" until all data is sent when the socket is closed */ + +#ifdef NETTEST_HAVE_SOLINGER + ling.l_onoff = 1; + ling.l_linger = 30; /* timeout is seconds */ + if (setsockopt(acceptsd, SOL_SOCKET, SO_LINGER, &ling, sizeof(struct linger)) < 0) + { + message("server: setsockopt SO_LINGER failure: %d\n", errno); + goto errout_with_acceptsd; + } +#endif + +#ifdef CONFIG_EXAMPLE_NETTEST_PERFORMANCE + /* Then receive data forever */ + + for (;;) + { + nbytesread = recv(acceptsd, buffer, 2*SENDSIZE, 0); + if (nbytesread <= 0) + { + message("server: recv failed: %d\n", errno); + goto errout_with_acceptsd; + } + } +#else + /* Receive canned message */ + + totalbytesread = 0; + while (totalbytesread < SENDSIZE) + { + message("server: Reading...\n"); + nbytesread = recv(acceptsd, &buffer[totalbytesread], 2*SENDSIZE - totalbytesread, 0); + if (nbytesread <= 0) + { + message("server: recv failed: %d\n", errno); + goto errout_with_acceptsd; + } + + totalbytesread += nbytesread; + message("server: Received %d of %d bytes\n", totalbytesread, SENDSIZE); + } + + /* Verify the message */ + + if (totalbytesread != SENDSIZE) + { + message("server: Received %d / Expected %d bytes\n", totalbytesread, SENDSIZE); + goto errout_with_acceptsd; + } + + ch = 0x20; + for (i = 0; i < SENDSIZE; i++ ) + { + if (buffer[i] != ch) + { + message("server: Byte %d is %02x / Expected %02x\n", i, buffer[i], ch); + goto errout_with_acceptsd; + } + + if (++ch > 0x7e) + { + ch = 0x20; + } + } + + /* Then send the same data back to the client */ + + message("server: Sending %d bytes\n", totalbytesread); + nbytessent = send(acceptsd, buffer, totalbytesread, 0); + if (nbytessent <= 0) + { + message("server: send failed: %d\n", errno); + goto errout_with_acceptsd; + } + message("server: Sent %d bytes\n", nbytessent); + + /* If this platform only does abortive disconnects, then wait a bit to get the + * client side a change to receive the data. + */ + +#if 1 /* Do it for all platforms */ + message("server: Wait before closing\n"); + sleep(60); +#endif + + close(listensd); + close(acceptsd); + free(buffer); + return; +#endif + +errout_with_acceptsd: + close(acceptsd); + +errout_with_listensd: + close(listensd); + +errout_with_buffer: + free(buffer); + exit(1); +} -- cgit v1.2.3