Resync new repository with old repo r5166

git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5153 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2012-09-17 18:18:44 +00:00
commit 57623d42eb
5691 changed files with 1361853 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
#
# For a description of the syntax of this configuration file,
# see misc/tools/kconfig-language.txt.
#
config EXAMPLES_NETTEST
bool "Network test example"
default n
---help---
Enable the network test example
if EXAMPLES_NETTEST
endif
+142
View File
@@ -0,0 +1,142 @@
############################################################################
# examples/nettest/Makefile
#
# Copyright (C) 2007-2008, 2010-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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_POSIX = "$(APPDIR)/libapps$(LIBEXT)"
ifeq ($(WINTOOL),y)
TARG_BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
TARG_BIN = "$(TARG_POSIX)"
endif
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
ifeq ($(CONFIG_EXAMPLE_NETTEST_PERFORMANCE),y)
HOSTCFLAGS += -DCONFIG_EXAMPLE_NETTEST_PERFORMANCE=1
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 .
# NET test built-in application info
APPNAME = nettest
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built $(HOST_BIN)
.PHONY: clean depend disclean
$(TARG_AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(TARG_COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
$(HOST_OBJS): %.o: %.c
@echo "CC: $<"
@$(HOSTCC) -c $(HOSTCFLAGS) $< -o $@
$(HOST_BIN): $(HOST_OBJS)
@echo "LD: $@"
@$(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS) -o $@
.built: $(TARG_OBJS)
@( for obj in $(TARG_OBJS) ; do \
$(call ARCHIVE, $(TARG_BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(TARG_SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(TARG_SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f $(TARG_POSIX) $(HOST_BIN) .built *.o *~ .*.swp
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep
+63
View File
@@ -0,0 +1,63 @@
/****************************************************************************
* examples/nettest/host.c
*
* Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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;
}
+109
View File
@@ -0,0 +1,109 @@
/****************************************************************************
* examples/nettest/nettest.c
*
* Copyright (C) 2007, 2009-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <stdint.h>
#include <stdio.h>
#include <debug.h>
#include <net/if.h>
#include <nuttx/net/uip/uip.h>
#include <apps/netutils/uiplib.h>
#include "nettest.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* nettest_main
****************************************************************************/
int nettest_main(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] = 0xde;
mac[3] = 0xad;
mac[4] = 0xbe;
mac[5] = 0xef;
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;
}
+95
View File
@@ -0,0 +1,95 @@
/****************************************************************************
* examples/nettest/nettest.h
*
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <debug.h>
#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 */
+210
View File
@@ -0,0 +1,210 @@
/****************************************************************************
* examples/nettest/nettest-client.c
*
* Copyright (C) 2007, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <string.h>
#include <errno.h>
#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 send messages forever */
for (;;)
{
nbytessent = send(sockfd, outbuf, SENDSIZE, 0);
if (nbytessent < 0)
{
message("client: send failed: %d\n", errno);
goto errout_with_socket;
}
else if (nbytessent != SENDSIZE)
{
message("client: Bad send length=%d: %d of \n",
nbytessent, SENDSIZE);
goto errout_with_socket;
}
message("Sent %d bytes\n", nbytessent);
}
#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;
}
else if (nbytesrecvd == 0)
{
message("client: The server closed the connection\n");
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);
}
+245
View File
@@ -0,0 +1,245 @@
/****************************************************************************
* examples/nettest/nettest-server.c
*
* Copyright (C) 2007, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <errno.h>
#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 if (nbytesread == 0)
{
message("server: The client broke the connection\n");
goto errout_with_acceptsd;
}
message("Received %d bytes\n", nbytesread);
}
#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;
}
else if (nbytesread == 0)
{
message("server: The client broke the connection\n");
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);
}