9
0
Fork 0

Add simple network test

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@358 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2007-10-31 23:26:24 +00:00
parent 2a8575cf15
commit 94b5f56c23
6 changed files with 672 additions and 0 deletions

View File

@ -0,0 +1,105 @@
############################################################
# Makefile
#
# 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.
#
############################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
MKDEP = $(TOPDIR)/tools/mkdeps.sh
TARG_ASRCS =
TARG_AOBJS = $(TARG_ASRCS:.S=$(OBJEXT))
TARG_CSRCS = nettest.c
ifeq ($(CONFIG_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 = lib$(CONFIG_EXAMPLE)$(LIBEXT)
HOSTCFLAGS += -DCONFIG_NETTEST_HOST=1
ifeq ($(CONFIG_NETTEST_SERVER),y)
HOSTCFLAGS += -DCONFIG_NETTEST_SERVER=1
endif
HOST_SRCS = host.c
ifeq ($(CONFIG_NETTEST_SERVER),y)
HOST_SRCS += nettest-client.c
else
HOST_SRCS += nettest-server.c
endif
HOST_OBJS = $(HOST_SRCS:.c=.o)
HOST_BIN = host
all: $(TARG_BIN)
$(TARG_AOBJS): %$(OBJEXT): %.S
$(CC) -c $(CFLAGS) $< -o $@
$(TARG_COBJS): %$(OBJEXT): %.c
$(CC) -c $(CFLAGS) $< -o $@
$(TARG_BIN): $(TARG_OBJS) $(HOST_BIN)
( for obj in $(TARG_OBJS) ; do \
$(AR) $@ $${obj} || \
{ echo "$(AR) $@ $obj FAILED!" ; exit 1 ; } ; \
done ; )
$(HOST_OBJS): %.o: %.c
$(HOSTCC) -c $(HOSTCFLAGS) $< -o $@
$(HOST_BIN): $(HOST_OBJS)
$(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS) -o $@
.depend: Makefile $(TARG_SRCS)
$(MKDEP) $(CC) -- $(CFLAGS) -- $(TARG_SRCS) >Make.dep
touch $@
depend: .depend
clean:
rm -f $(TARG_BIN) $(HOST_BIN) *.o *.rel *.asm *.lst *.sym *.adb *~
distclean: clean
rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,63 @@
/****************************************************************************
* examples/nettest/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 "nettest.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* main
****************************************************************************/
int main(int argc, char **argv, char **envp)
{
#ifdef CONFIG_NETTEST_SERVER
send_client();
#else
recv_server();
#endif
return 0;
}

View File

@ -0,0 +1,174 @@
/****************************************************************************
* examples/nettest/nettestn-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 "nettest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
void send_client(void)
{
struct sockaddr_in myaddr;
char outbuf[SENDSIZE];
#ifndef CONFIG_NETTEST_PERFORMANCE
char inbuf[SENDSIZE];
#endif
int sockfd;
int nbytessent;
#ifndef CONFIG_NETTEST_PERFORMANCE
int nbytesrecvd;
#endif
int ch;
int i;
/* Create a new TCP socket */
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
printf("client socket failure %d\n", errno);
exit(1);
}
/* 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(192 << 24 | 168 << 16 | 0 << 8 |106);
#endif
printf("client: Connecting...\n");
if (connect( sockfd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0)
{
printf("client: connect failure: %d\n", errno);
exit(1);
}
printf("clilent: Connected\n");
/* Initialize the buffer */
ch = 0x20;
for (i = 0; i < SENDSIZE; i++ )
{
outbuf[i] = ch;
if (++ch > 0x7e)
{
ch = 0x20;
}
}
#ifdef CONFIG_NETTEST_PERFORMANCE
/* Then receive messages forever */
for (;;)
{
nbytessent = send(sockfd, outbuf, 512, 0);
if (nbytessent < 0)
{
printf("client: send failed: %d\n", errno);
close(sockfd);
exit(-1);
}
else if (nbytessent != 512)
{
printf("client: Bad send length=%d: %d\n", nbytessent);
close(sockfd);
exit(-1);
}
}
#else
/* Then send and receive one message */
printf("client: Sending %d bytes\n", SENDSIZE);
nbytessent = send(sockfd, outbuf, SENDSIZE, 0);
printf("client: Sent %d bytes\n", nbytessent);
if (nbytessent < 0)
{
printf("client: send failed: %d\n", errno);
close(sockfd);
exit(-1);
}
else if (nbytessent != SENDSIZE)
{
printf("client: Bad send length=%d: %d\n", nbytessent);
close(sockfd);
exit(-1);
}
printf("client: Receiving...\n");
nbytesrecvd = recv(sockfd, inbuf, SENDSIZE, 0);
printf("client: Received %d bytes\n", nbytesrecvd);
if (nbytesrecvd < 0)
{
printf("client: recv failed: %d\n", errno);
close(sockfd);
exit(-1);
}
else if (nbytesrecvd != SENDSIZE)
{
printf("client: Bad recv length=%d: %d\n", nbytessent);
close(sockfd);
exit(-1);
}
else if (memcmp(inbuf, outbuf, SENDSIZE) != 0)
{
printf("client: Received buffer does not match sent buffer\n");
close(sockfd);
exit(-1);
}
close(sockfd);
#endif
}

View File

@ -0,0 +1,158 @@
/****************************************************************************
* examples/nettest/nettest.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 <errno.h>
#include "nettest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
void recv_server(void)
{
struct sockaddr_in myaddr;
char buffer[1024];
int listensd;
int acceptsd;
socklen_t addrlen;
int nbytesread;
#ifndef CONFIG_NETTEST_PERFORMANCE
int nbytessent;
#endif
int optval;
/* Create a new TCP socket */
listensd = socket(PF_INET, SOCK_STREAM, 0);
if (listensd < 0)
{
printf("server: socket failure: %d\n", errno);
exit(1);
}
/* Set socket to reuse address */
optval = 1;
if (setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
{
printf("server: setsockopt failure: %d\n", errno);
exit(1);
}
/* Bind the TCP 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)
{
printf("server: bind failure: %d\n", errno);
exit(1);
}
/* Listen for connections on the bound TCP socket */
if (listen(listensd, 5) < 0)
{
printf("server: listen failure %d\n", errno);
exit(1);
}
/* Accept only one connection */
printf("server: Accepting connections on port %d\n", PORTNO);
addrlen = sizeof(struct sockaddr_in);
acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen);
if (acceptsd < 0)
{
printf("server: accept failure: %d\n", errno);
exit(1);
}
printf("server: Connection accepted -- receiving\n");
#ifdef CONFIG_NETTEST_PERFORMANCE
/* Then receive data forever */
for (;;)
{
nbytesread = recv(acceptsd, buffer, 1024, 0);
if (nbytesread <= 0)
{
printf("server: recv failed: %d\n", errno);
close(listensd);
close(acceptsd);
exit(-1);
}
}
#else
/* Receive and echo own message */
nbytesread = recv(acceptsd, buffer, 1024, 0);
if (nbytesread <= 0)
{
printf("server: recv failed: %d\n", errno);
close(listensd);
close(acceptsd);
exit(-1);
}
printf("server: Received %d bytes\n", nbytesread);
nbytessent = send(acceptsd, buffer, nbytesread, 0);
if (nbytessent <= 0)
{
printf("server: send failed: %d\n", errno);
close(listensd);
close(acceptsd);
exit(-1);
}
printf("server: Sent %d bytes\n", nbytessent);
close(listensd);
close(acceptsd);
#endif
}

View File

@ -0,0 +1,108 @@
/****************************************************************************
* examples/nettest/nettest.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 <nuttx/config.h>
#include <stdio.h>
#include <net/uip/uip.h>
#include <net/uip/uip-lib.h>
#include "nettest.h"
/****************************************************************************
* 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 */
uip_ipaddr(addr.s_addr, 192, 168, 0, 128 );
uip_sethostaddr("eth0", &addr);
/* Set up the default router address */
uip_ipaddr(addr.s_addr, 192, 168, 0, 1);
uip_setdraddr("eth0", &addr);
/* Setup the subnet mask */
uip_ipaddr(addr.s_addr, 255, 255, 255, 0);
uip_setnetmask("eth0", &addr);
#ifdef CONFIG_NETTEST_SERVER
recv_server();
#else
send_client();
#endif
return 0;
}
/****************************************************************************
* uip_log
****************************************************************************/
void uip_log(char *m)
{
printf("uIP log message: %s\n", m);
}

View File

@ -0,0 +1,64 @@
/****************************************************************************
* examples/nettest/nettest.h
*
* 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.
*
****************************************************************************/
#ifndef __EXAMPLES_NETTEST_H
#define __EXAMPLES_NETTEST_H
/****************************************************************************
* Included Files
****************************************************************************/
/****************************************************************************
* Definitions
****************************************************************************/
#ifdef CONFIG_NETTEST_HOST
# define HTONS(a) htons(a)
# define HTONL(a) htonl(a)
#else
# define errno *get_errno_ptr()
#endif
#define PORTNO 5471
#define SENDSIZE 512
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
extern void send_client(void);
extern void recv_server(void);
#endif /* __EXAMPLES_NETTEST_H */