9
0
Fork 0

UDP test/example

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@399 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2007-11-22 21:57:38 +00:00
parent a5d1271051
commit 0eabb79c11
6 changed files with 667 additions and 0 deletions

106
nuttx/examples/udp/Makefile Normal file
View File

@ -0,0 +1,106 @@
############################################################
# 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 = target.c
ifeq ($(CONFIG_EXAMPLE_UDP_SERVER),y)
TARG_CSRCS += udp-server.c
else
TARG_CSRCS += udp-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_EXAMPLE_UDP_HOST=1
ifeq ($(CONFIG_EXAMPLE_UDP_SERVER),y)
HOSTCFLAGS += -DCONFIG_EXAMPLE_UDP_SERVER=1 \
-DCONFIG_EXAMPLE_UDP_CLIENTIP="$(CONFIG_EXAMPLE_UDP_CLIENTIP)"
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
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

63
nuttx/examples/udp/host.c Normal file
View File

@ -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;
}

104
nuttx/examples/udp/target.c Normal file
View File

@ -0,0 +1,104 @@
/****************************************************************************
* examples/udp/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 <debug.h>
#include <net/uip/uip.h>
#include <net/uip/uip-lib.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;
}

View File

@ -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", 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);
}

View File

@ -0,0 +1,94 @@
/****************************************************************************
* examples/udp/udp-internal.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_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
/* Get errno using a pointer */
# define errno *get_errno_ptr()
/* 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 */

View File

@ -0,0 +1,167 @@
/****************************************************************************
* examples/udp/udp-server.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 "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;
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);
message("server: %d. Recieved %d bytes\n", offset, nbytes);
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);
}