mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-20 00:03:33 +08:00
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:
@@ -0,0 +1,13 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see misc/tools/kconfig-language.txt.
|
||||
#
|
||||
|
||||
config EXAMPLES_UDP
|
||||
bool "UDP example"
|
||||
default n
|
||||
---help---
|
||||
Enable the UDP example
|
||||
|
||||
if EXAMPLES_UDP
|
||||
endif
|
||||
@@ -0,0 +1,124 @@
|
||||
############################################################################
|
||||
# apps/examples/udp/Makefile
|
||||
#
|
||||
# Copyright (C) 2007-2008, 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
|
||||
|
||||
# UDP Test
|
||||
|
||||
TARG_ASRCS =
|
||||
|
||||
TARG_CSRCS = target.c
|
||||
ifeq ($(CONFIG_EXAMPLE_UDP_SERVER),y)
|
||||
TARG_CSRCS += udp-server.c
|
||||
else
|
||||
TARG_CSRCS += udp-client.c
|
||||
endif
|
||||
|
||||
TARG_AOBJS = $(TARG_ASRCS:.S=$(OBJEXT))
|
||||
TARG_COBJS = $(TARG_CSRCS:.c=$(OBJEXT))
|
||||
|
||||
TARG_SRCS = $(TARG_ASRCS) $(TARG_CSRCS)
|
||||
TARG_OBJS = $(TARG_AOBJS) $(TARG_COBJS)
|
||||
|
||||
ifeq ($(WINTOOL),y)
|
||||
TARG_BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
|
||||
else
|
||||
TARG_BIN = "$(APPDIR)/libapps$(LIBEXT)"
|
||||
endif
|
||||
|
||||
HOSTCFLAGS += -DCONFIG_EXAMPLE_UDP_HOST=1
|
||||
ifeq ($(CONFIG_EXAMPLE_UDP_SERVER),y)
|
||||
HOSTCFLAGS += -DCONFIG_EXAMPLE_UDP_SERVER=1 \
|
||||
-DCONFIG_EXAMPLE_UDP_SERVERIP="$(CONFIG_EXAMPLE_UDP_SERVERIP)"
|
||||
endif
|
||||
|
||||
HOST_SRCS = host.c
|
||||
ifeq ($(CONFIG_EXAMPLE_UDP_SERVER),y)
|
||||
HOST_SRCS += udp-client.c
|
||||
else
|
||||
HOST_SRCS += udp-server.c
|
||||
endif
|
||||
|
||||
HOST_OBJS = $(HOST_SRCS:.c=.o)
|
||||
HOST_BIN = host
|
||||
|
||||
ROOTDEPPATH = --dep-path .
|
||||
|
||||
# Common build
|
||||
|
||||
VPATH =
|
||||
|
||||
all: .built
|
||||
.PHONY: 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
|
||||
$(HOSTCC) -c $(HOSTCFLAGS) $< -o $@
|
||||
|
||||
$(HOST_BIN): $(HOST_OBJS)
|
||||
$(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS) -o $@
|
||||
|
||||
.built: $(TARG_BIN) $(HOST_BIN)
|
||||
@touch .built
|
||||
|
||||
context:
|
||||
|
||||
.depend: Makefile $(TARG_SRCS)
|
||||
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(TARG_SRCS) >Make.dep
|
||||
@touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
@rm -f $(TARG_BIN) $(HOST_BIN) *.o *~ .*.swp .built
|
||||
$(call CLEAN)
|
||||
|
||||
distclean: clean
|
||||
@rm -f Make.dep .depend
|
||||
|
||||
-include Make.dep
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/****************************************************************************
|
||||
* examples/udp/host.c
|
||||
*
|
||||
* Copyright (C) 2007 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 "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;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/****************************************************************************
|
||||
* examples/udp/nettest.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 NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdio.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <apps/netutils/uiplib.h>
|
||||
|
||||
#include "udp-internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* udp_main
|
||||
****************************************************************************/
|
||||
|
||||
int udp_main(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;
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/****************************************************************************
|
||||
* examples/udp/udp-client.c
|
||||
*
|
||||
* Copyright (C) 2007 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 <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "udp-internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline void fill_buffer(unsigned char *buf, int offset)
|
||||
{
|
||||
int ch;
|
||||
int j;
|
||||
|
||||
buf[0] = offset;
|
||||
for (ch = 0x20, j = offset + 1; ch < 0x7f; ch++, j++)
|
||||
{
|
||||
if (j >= SENDSIZE)
|
||||
{
|
||||
j = 1;
|
||||
}
|
||||
buf[j] = ch;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void send_client(void)
|
||||
{
|
||||
struct sockaddr_in server;
|
||||
unsigned char outbuf[SENDSIZE];
|
||||
int sockfd;
|
||||
int nbytes;
|
||||
int offset;
|
||||
|
||||
/* Create a new TCP socket */
|
||||
|
||||
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
message("client socket failure %d\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Then send and receive 256 messages */
|
||||
|
||||
for (offset = 0; offset < 256; offset++)
|
||||
{
|
||||
/* Set up the output buffer */
|
||||
|
||||
fill_buffer(outbuf, offset);
|
||||
|
||||
/* Send the message */
|
||||
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = HTONS(PORTNO);
|
||||
server.sin_addr.s_addr = HTONL(CONFIG_EXAMPLE_UDP_SERVERIP);
|
||||
|
||||
message("client: %d. Sending %d bytes\n", offset, SENDSIZE);
|
||||
nbytes = sendto(sockfd, outbuf, SENDSIZE, 0,
|
||||
(struct sockaddr*)&server, sizeof(struct sockaddr_in));
|
||||
message("client: %d. Sent %d bytes\n", offset, nbytes);
|
||||
|
||||
if (nbytes < 0)
|
||||
{
|
||||
message("client: %d. sendto failed: %d\n", offset, errno);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
}
|
||||
else if (nbytes != SENDSIZE)
|
||||
{
|
||||
message("client: %d. Bad send length: %d Expected: %d\n",
|
||||
offset, nbytes, SENDSIZE);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/* Now, sleep a bit. No packets should be dropped due to overrunning
|
||||
* the server.
|
||||
*/
|
||||
|
||||
sleep(2);
|
||||
}
|
||||
close(sockfd);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/****************************************************************************
|
||||
* examples/udp/udp-internal.h
|
||||
*
|
||||
* Copyright (C) 2007, 2008 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_UIP_INTERNAL_H
|
||||
#define __EXAMPLES_UIP_INTERNAL_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_UDP_HOST
|
||||
#else
|
||||
# include <debug.h>
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_UDP_HOST
|
||||
/* HTONS/L macros are unique to uIP */
|
||||
|
||||
# define HTONS(a) htons(a)
|
||||
# define HTONL(a) htonl(a)
|
||||
|
||||
/* Used printf for debug output */
|
||||
|
||||
# define message(...) printf(__VA_ARGS__)
|
||||
|
||||
/* Have SO_LINGER */
|
||||
|
||||
#else
|
||||
|
||||
/* If debug is enabled, use the synchronous lib_lowprintf so that the
|
||||
* program output does not get disassociated in the debug output.
|
||||
*/
|
||||
|
||||
# ifdef CONFIG_DEBUG
|
||||
# define message(...) lib_lowprintf(__VA_ARGS__)
|
||||
# else
|
||||
# define message(...) printf(__VA_ARGS__)
|
||||
# endif
|
||||
|
||||
#endif
|
||||
|
||||
#define PORTNO 5471
|
||||
|
||||
#define ASCIISIZE (0x7f - 0x20)
|
||||
#define SENDSIZE (ASCIISIZE+1)
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
extern void send_client(void);
|
||||
extern void recv_server(void);
|
||||
|
||||
#endif /* __EXAMPLES_UIP_INTERNAL_H */
|
||||
@@ -0,0 +1,173 @@
|
||||
/****************************************************************************
|
||||
* examples/udp/udp-server.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 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 <errno.h>
|
||||
|
||||
#include "udp-internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline int check_buffer(unsigned char *buf)
|
||||
{
|
||||
int ret = 1;
|
||||
int offset;
|
||||
int ch;
|
||||
int j;
|
||||
|
||||
offset = buf[0];
|
||||
for (ch = 0x20, j = offset + 1; ch < 0x7f; ch++, j++)
|
||||
{
|
||||
if (j >= SENDSIZE)
|
||||
{
|
||||
j = 1;
|
||||
}
|
||||
if (buf[j] != ch)
|
||||
{
|
||||
message("server: Buffer content error for offset=%d, index=%d\n", offset, j);
|
||||
ret = 0;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void recv_server(void)
|
||||
{
|
||||
struct sockaddr_in server;
|
||||
struct sockaddr_in client;
|
||||
in_addr_t tmpaddr;
|
||||
unsigned char inbuf[1024];
|
||||
int sockfd;
|
||||
int nbytes;
|
||||
int optval;
|
||||
int offset;
|
||||
socklen_t addrlen;
|
||||
|
||||
/* Create a new UDP socket */
|
||||
|
||||
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
message("server: socket failure: %d\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Set socket to reuse address */
|
||||
|
||||
optval = 1;
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
||||
{
|
||||
message("server: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Bind the socket to a local address */
|
||||
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = HTONS(PORTNO);
|
||||
server.sin_addr.s_addr = HTONL(INADDR_ANY);
|
||||
|
||||
if (bind(sockfd, (struct sockaddr*)&server, sizeof(struct sockaddr_in)) < 0)
|
||||
{
|
||||
message("server: bind failure: %d\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Then receive up to 256 packets of data */
|
||||
|
||||
for (offset = 0; offset < 256; offset++)
|
||||
{
|
||||
message("server: %d. Receiving up 1024 bytes\n", offset);
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
nbytes = recvfrom(sockfd, inbuf, 1024, 0,
|
||||
(struct sockaddr*)&client, &addrlen);
|
||||
|
||||
tmpaddr = ntohl(client.sin_addr.s_addr);
|
||||
message("server: %d. Received %d bytes from %d.%d.%d.%d:%d\n",
|
||||
offset, nbytes,
|
||||
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
|
||||
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff,
|
||||
ntohs(client.sin_port));
|
||||
|
||||
if (nbytes < 0)
|
||||
{
|
||||
message("server: %d. recv failed: %d\n", offset, errno);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (nbytes != SENDSIZE)
|
||||
{
|
||||
message("server: %d. recv size incorrect: %d vs %d\n", offset, nbytes, SENDSIZE);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (offset < inbuf[0])
|
||||
{
|
||||
message("server: %d. %d packets lost, resetting offset\n", offset, inbuf[0] - offset);
|
||||
offset = inbuf[0];
|
||||
}
|
||||
else if (offset > inbuf[0])
|
||||
{
|
||||
message("server: %d. Bad offset in buffer: %d\n", offset, inbuf[0]);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (!check_buffer(inbuf))
|
||||
{
|
||||
message("server: %d. Bad buffer contents\n", offset);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
close(sockfd);
|
||||
}
|
||||
Reference in New Issue
Block a user