apps/examples/bridge: Add host-side test driver
This commit is contained in:
parent
40dd9dd221
commit
be61a5d297
4
examples/bridge/.gitignore
vendored
4
examples/bridge/.gitignore
vendored
@ -1,6 +1,10 @@
|
|||||||
/Make.dep
|
/Make.dep
|
||||||
/.depend
|
/.depend
|
||||||
/.built
|
/.built
|
||||||
|
/bridge_config.h
|
||||||
|
/host1
|
||||||
|
/host2
|
||||||
|
/*.exe
|
||||||
/*.asm
|
/*.asm
|
||||||
/*.obj
|
/*.obj
|
||||||
/*.rel
|
/*.rel
|
||||||
|
@ -74,6 +74,8 @@ config EXAMPLES_BRIDGE_NET1_IPADDR
|
|||||||
hex "IP address"
|
hex "IP address"
|
||||||
default 0x0a000002
|
default 0x0a000002
|
||||||
depends on !EXAMPLES_BRIDGE_NET1_DHCPC
|
depends on !EXAMPLES_BRIDGE_NET1_DHCPC
|
||||||
|
---help---
|
||||||
|
IP address of network 1 for the target
|
||||||
|
|
||||||
config EXAMPLES_BRIDGE_NET1_DRIPADDR
|
config EXAMPLES_BRIDGE_NET1_DRIPADDR
|
||||||
hex "Default Router IP address (Gateway)"
|
hex "Default Router IP address (Gateway)"
|
||||||
@ -85,6 +87,12 @@ config EXAMPLES_BRIDGE_NET1_NETMASK
|
|||||||
|
|
||||||
endif # !NSH_BUILTIN_APPS
|
endif # !NSH_BUILTIN_APPS
|
||||||
|
|
||||||
|
config EXAMPLES_BRIDGE_NET1_IPHOST
|
||||||
|
hex "Host IP address"
|
||||||
|
default 0x0a000001
|
||||||
|
---help---
|
||||||
|
IP address of network 1 for the host
|
||||||
|
|
||||||
config EXAMPLES_BRIDGE_NET1_STACKSIZE
|
config EXAMPLES_BRIDGE_NET1_STACKSIZE
|
||||||
int "Network 1 daemon stacksize"
|
int "Network 1 daemon stacksize"
|
||||||
default 2048
|
default 2048
|
||||||
@ -148,6 +156,8 @@ config EXAMPLES_BRIDGE_NET2_IPADDR
|
|||||||
hex "IP address"
|
hex "IP address"
|
||||||
default 0x0a000003
|
default 0x0a000003
|
||||||
depends on !EXAMPLES_BRIDGE_NET2_DHCPC
|
depends on !EXAMPLES_BRIDGE_NET2_DHCPC
|
||||||
|
---help---
|
||||||
|
IP address of network 2 for the target
|
||||||
|
|
||||||
config EXAMPLES_BRIDGE_NET2_DRIPADDR
|
config EXAMPLES_BRIDGE_NET2_DRIPADDR
|
||||||
hex "Default Router IP address (Gateway)"
|
hex "Default Router IP address (Gateway)"
|
||||||
@ -159,6 +169,12 @@ config EXAMPLES_BRIDGE_NET2_NETMASK
|
|||||||
|
|
||||||
endif # !NSH_BUILTIN_APPS
|
endif # !NSH_BUILTIN_APPS
|
||||||
|
|
||||||
|
config EXAMPLES_BRIDGE_NET2_IPHOST
|
||||||
|
hex "Host IP address"
|
||||||
|
default 0x0a000001
|
||||||
|
---help---
|
||||||
|
IP address of network 2 for the host
|
||||||
|
|
||||||
config EXAMPLES_BRIDGE_NET2_STACKSIZE
|
config EXAMPLES_BRIDGE_NET2_STACKSIZE
|
||||||
int "Network 2 daemon stacksize"
|
int "Network 2 daemon stacksize"
|
||||||
default 2048
|
default 2048
|
||||||
|
@ -40,43 +40,54 @@ include $(APPDIR)/Make.defs
|
|||||||
|
|
||||||
# Discover built-in application info
|
# Discover built-in application info
|
||||||
|
|
||||||
APPNAME = bridge
|
TARG_APPNAME = bridge
|
||||||
PRIORITY = SCHED_PRIORITY_DEFAULT
|
TARG_PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||||
STACKSIZE = 2048
|
TARG_STACKSIZE = 2048
|
||||||
|
|
||||||
ASRCS =
|
TARG_ASRCS =
|
||||||
CSRCS =
|
TARG_CSRCS =
|
||||||
MAINSRC = bridge_main.c
|
MAINSRC = bridge_main.c
|
||||||
|
|
||||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
TARG_AOBJS = $(TARG_ASRCS:.S=$(OBJEXT))
|
||||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
TARG_COBJS = $(TARG_CSRCS:.c=$(OBJEXT))
|
||||||
MAINOBJ = $(MAINSRC:.c=$(OBJEXT))
|
TARG_MAINOBJ = $(MAINSRC:.c=$(OBJEXT))
|
||||||
|
|
||||||
SRCS = $(ASRCS) $(CSRCS) $(MAINSRC)
|
TARG_SRCS = $(TARG_ASRCS) $(TARG_CSRCS) $(MAINSRC)
|
||||||
OBJS = $(AOBJS) $(COBJS)
|
TARG_OBJS = $(TARG_AOBJS) $(TARG_COBJS)
|
||||||
|
|
||||||
ifneq ($(CONFIG_BUILD_KERNEL),y)
|
ifneq ($(CONFIG_BUILD_KERNEL),y)
|
||||||
OBJS += $(MAINOBJ)
|
TARG_OBJS += $(TARG_MAINOBJ)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
HOST_SRCS1 = host_net1.c
|
||||||
|
HOST_SRCS2 = host_net2.c
|
||||||
|
HOST_SRCS = $(HOST_SRCS1) $(HOST_SRCS2)
|
||||||
|
|
||||||
|
HOST_OBJS1 = $(HOST_SRCS1:.c=.o)
|
||||||
|
HOST_OBJS2 = $(HOST_SRCS2:.c=.o)
|
||||||
|
HOST_OBJS = $(HOST_OBJS1) $(HOST_OBJS2)
|
||||||
|
|
||||||
|
HOST_BIN1 = host1
|
||||||
|
HOST_BIN2 = host2
|
||||||
|
|
||||||
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
||||||
BIN = ..\..\libapps$(LIBEXT)
|
TARG_BIN = ..\..\libapps$(LIBEXT)
|
||||||
else
|
else
|
||||||
ifeq ($(WINTOOL),y)
|
ifeq ($(WINTOOL),y)
|
||||||
BIN = ..\\..\\libapps$(LIBEXT)
|
TARG_BIN = ..\\..\\libapps$(LIBEXT)
|
||||||
else
|
else
|
||||||
BIN = ../../libapps$(LIBEXT)
|
TARG_BIN = ../../libapps$(LIBEXT)
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(WINTOOL),y)
|
ifeq ($(WINTOOL),y)
|
||||||
INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}"
|
TARG_INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}"
|
||||||
else
|
else
|
||||||
INSTALL_DIR = $(BIN_DIR)
|
TARG_INSTALL_DIR = $(BIN_DIR)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CONFIG_EXAMPLE_BRIDGE_PROGNAME ?= bridge$(EXEEXT)
|
CONFIG_EXAMPLE_BRIDGE_PROGNAME ?= bridge$(EXEEXT)
|
||||||
PROGNAME = $(CONFIG_EXAMPLE_BRIDGE_PROGNAME)
|
TARG_PROGNAME = $(CONFIG_EXAMPLE_BRIDGE_PROGNAME)
|
||||||
|
|
||||||
ROOTDEPPATH = --dep-path .
|
ROOTDEPPATH = --dep-path .
|
||||||
|
|
||||||
@ -87,23 +98,37 @@ VPATH =
|
|||||||
all: .built
|
all: .built
|
||||||
.PHONY: clean depend distclean
|
.PHONY: clean depend distclean
|
||||||
|
|
||||||
$(AOBJS): %$(OBJEXT): %.S
|
$(TARG_AOBJS): %$(OBJEXT): %.S
|
||||||
$(call ASSEMBLE, $<, $@)
|
$(call ASSEMBLE, $<, $@)
|
||||||
|
|
||||||
$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c
|
$(TARG_COBJS) $(TARG_MAINOBJ): %$(OBJEXT): %.c
|
||||||
$(call COMPILE, $<, $@)
|
$(call COMPILE, $<, $@)
|
||||||
|
|
||||||
.built: $(OBJS)
|
$(HOST_OBJS): %.o: %.c
|
||||||
$(call ARCHIVE, $(BIN), $(OBJS))
|
$(HOSTCC) -c $(HOSTCFLAGS) $< -o $@
|
||||||
|
|
||||||
|
$(TARG_BIN): $(TARG_OBJS)
|
||||||
|
$(call ARCHIVE, $(TARG_BIN), $(TARG_OBJS))
|
||||||
|
|
||||||
|
bridge_config.h: $(TOPDIR)/include/nuttx/config.h
|
||||||
|
cp $(TOPDIR)/include/nuttx/config.h bridge_config.h
|
||||||
|
|
||||||
|
$(HOST_BIN1): bridge_config.h $(HOST_OBJS1)
|
||||||
|
$(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS1) -o $@
|
||||||
|
|
||||||
|
$(HOST_BIN2): bridge_config.h $(HOST_OBJS2)
|
||||||
|
$(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS2) -o $@
|
||||||
|
|
||||||
|
.built: $(TARG_BIN) $(HOST_BIN1) $(HOST_BIN2)
|
||||||
@touch .built
|
@touch .built
|
||||||
|
|
||||||
ifeq ($(CONFIG_BUILD_KERNEL),y)
|
ifeq ($(CONFIG_BUILD_KERNEL),y)
|
||||||
$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ)
|
$(BIN_DIR)$(DELIM)$(TARG_PROGNAME): $(TARG_OBJS) $(TARG_MAINOBJ)
|
||||||
@echo "LD: $(PROGNAME)"
|
@echo "LD: $(TARG_PROGNAME)"
|
||||||
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS)
|
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(TARG_INSTALL_DIR)$(DELIM)$(TARG_PROGNAME) $(ARCHCRT0OBJ) $(TARG_MAINOBJ) $(LDLIBS)
|
||||||
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME)
|
$(Q) $(NM) -u $(TARG_INSTALL_DIR)$(DELIM)$(TARG_PROGNAME)
|
||||||
|
|
||||||
install: $(BIN_DIR)$(DELIM)$(PROGNAME)
|
install: $(BIN_DIR)$(DELIM)$(TARG_PROGNAME)
|
||||||
|
|
||||||
else
|
else
|
||||||
install:
|
install:
|
||||||
@ -111,26 +136,29 @@ install:
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
|
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
|
||||||
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile
|
$(BUILTIN_REGISTRY)$(DELIM)$(TARG_APPNAME)_main.bdat: $(DEPCONFIG) Makefile
|
||||||
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
|
$(call REGISTER,$(TARG_APPNAME),$(TARG_PRIORITY),$(TARG_STACKSIZE),$(TARG_APPNAME)_main)
|
||||||
|
|
||||||
context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat
|
context: $(BUILTIN_REGISTRY)$(DELIM)$(TARG_APPNAME)_main.bdat
|
||||||
else
|
else
|
||||||
context:
|
context:
|
||||||
endif
|
endif
|
||||||
|
|
||||||
.depend: Makefile $(SRCS)
|
.depend: Makefile $(TARG_SRCS)
|
||||||
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
|
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(TARG_SRCS) >Make.dep
|
||||||
@touch $@
|
@touch $@
|
||||||
|
|
||||||
depend: .depend
|
depend: .depend
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(call DELFILE, .built)
|
$(call DELFILE, .built)
|
||||||
|
$(call DELFILE, $(HOST_BIN1))
|
||||||
|
$(call DELFILE, $(HOST_BIN2))
|
||||||
$(call CLEAN)
|
$(call CLEAN)
|
||||||
|
|
||||||
distclean: clean
|
distclean: clean
|
||||||
$(call DELFILE, Make.dep)
|
$(call DELFILE, Make.dep)
|
||||||
$(call DELFILE, .depend)
|
$(call DELFILE, .depend)
|
||||||
|
$(call DELFILE, bridge_config.h)
|
||||||
|
|
||||||
-include Make.dep
|
-include Make.dep
|
||||||
|
@ -63,9 +63,6 @@
|
|||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#define BRIDGE_MAXLINE 64
|
|
||||||
#define BRIDGE_POLLTIMEOUT 10000
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Data
|
* Private Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -192,6 +189,13 @@ printf("NET1: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME);
|
|||||||
g_net1_ipaddr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET1_IPADDR);
|
g_net1_ipaddr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET1_IPADDR);
|
||||||
|
|
||||||
#endif /* CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC */
|
#endif /* CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC */
|
||||||
|
|
||||||
|
#else /* CONFIG_NSH_BUILTIN_APPS */
|
||||||
|
/* Hmmm.. there is an issue here. Where do we get the IP address if we
|
||||||
|
* are a builtin in application?
|
||||||
|
*/
|
||||||
|
# warning Missing logic
|
||||||
|
|
||||||
#endif /* CONFIG_NSH_BUILTIN_APPS */
|
#endif /* CONFIG_NSH_BUILTIN_APPS */
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
@ -310,6 +314,13 @@ printf("NET2: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME);
|
|||||||
g_net2_ipaddr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET2_IPADDR);
|
g_net2_ipaddr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET2_IPADDR);
|
||||||
|
|
||||||
#endif /* CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC */
|
#endif /* CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC */
|
||||||
|
|
||||||
|
#else /* CONFIG_NSH_BUILTIN_APPS */
|
||||||
|
/* Hmmm.. there is an issue here. Where do we get the IP address if we
|
||||||
|
* are a builtin in application?
|
||||||
|
*/
|
||||||
|
# warning Missing logic
|
||||||
|
|
||||||
#endif /* CONFIG_NSH_BUILTIN_APPS */
|
#endif /* CONFIG_NSH_BUILTIN_APPS */
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
|
253
examples/bridge/host_main.c
Normal file
253
examples/bridge/host_main.c
Normal file
@ -0,0 +1,253 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* examples/bridge/host_main.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
|
*
|
||||||
|
* Authors: 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 <sys/socket.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static uint8_t g_sndmessage[] = MESSAGE;
|
||||||
|
static uint8_t g_rdbuffer[EXAMPLES_BRIDGE_SEND_IOBUFIZE];
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* bridge_main
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
struct sockaddr_in recvaddr;
|
||||||
|
struct sockaddr_in listenaddr;
|
||||||
|
struct sockaddr_in sendaddr;
|
||||||
|
socklen_t addrlen;
|
||||||
|
in_addr_t tmpaddr;
|
||||||
|
ssize_t nrecvd;
|
||||||
|
ssize_t nsent;
|
||||||
|
int optval;
|
||||||
|
int recvsd;
|
||||||
|
int sndsd;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
/* Create a UDP send socket */
|
||||||
|
|
||||||
|
printf(LABEL "Create send socket\n");
|
||||||
|
|
||||||
|
sndsd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (sndsd < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: Failed to create send socket: %d\n", errno);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set socket to reuse address */
|
||||||
|
|
||||||
|
optval = 1;
|
||||||
|
if (setsockopt(sndsd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
||||||
|
goto errout_with_sendsd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Bind the socket to a local address */
|
||||||
|
|
||||||
|
sendaddr.sin_family = AF_INET;
|
||||||
|
sendaddr.sin_port = htons(EXAMPLES_BRIDGE_SEND_SNDPORT);
|
||||||
|
sendaddr.sin_addr.s_addr = htonl(EXAMPLES_BRIDGE_SEND_IPHOST);
|
||||||
|
|
||||||
|
if (bind(sndsd, (struct sockaddr*)&sendaddr, sizeof(struct sockaddr_in)) < 0)
|
||||||
|
{
|
||||||
|
printf(LABEL "bind failure: %d\n", errno);
|
||||||
|
goto errout_with_sendsd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create a UDP receive socket */
|
||||||
|
|
||||||
|
printf(LABEL "Create receive socket\n");
|
||||||
|
|
||||||
|
recvsd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (recvsd < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: Failed to create receive socket: %d\n", errno);
|
||||||
|
goto errout_with_sendsd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set socket to reuse address */
|
||||||
|
|
||||||
|
optval = 1;
|
||||||
|
if (setsockopt(recvsd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
||||||
|
goto errout_with_recvsd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Bind the socket to a local address */
|
||||||
|
|
||||||
|
listenaddr.sin_family = AF_INET;
|
||||||
|
listenaddr.sin_port = htons(EXAMPLES_BRIDGE_RECV_RECVPORT);
|
||||||
|
listenaddr.sin_addr.s_addr = htonl(EXAMPLES_BRIDGE_RECV_IPHOST);
|
||||||
|
|
||||||
|
if (bind(recvsd, (struct sockaddr*)&listenaddr, sizeof(struct sockaddr_in)) < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: bind failure: %d\n", errno);
|
||||||
|
goto errout_with_recvsd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send a packet */
|
||||||
|
|
||||||
|
printf(LABEL "Sending %lu bytes\n", sizeof(g_sndmessage));
|
||||||
|
nsent = sendto(sndsd, g_sndmessage, sizeof(g_sndmessage), 0,
|
||||||
|
(struct sockaddr*)&sendaddr, sizeof(struct sockaddr_in));
|
||||||
|
|
||||||
|
/* Check for send errors */
|
||||||
|
|
||||||
|
if (nsent < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: sendto failed: %d\n", errno);
|
||||||
|
goto errout_with_recvsd;
|
||||||
|
}
|
||||||
|
else if (nsent != sizeof(g_sndmessage))
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: Bad send length: %ld Expected: %d\n",
|
||||||
|
(long)nsent, sizeof(g_sndmessage));
|
||||||
|
goto errout_with_recvsd;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(LABEL "Message sent successfully\n");
|
||||||
|
|
||||||
|
/* Read a packet */
|
||||||
|
|
||||||
|
printf(LABEL "Receiving up to %d bytes\n", EXAMPLES_BRIDGE_SEND_IOBUFIZE);
|
||||||
|
|
||||||
|
addrlen = sizeof(struct sockaddr_in);
|
||||||
|
nrecvd = recvfrom(recvsd, g_rdbuffer, EXAMPLES_BRIDGE_SEND_IOBUFIZE, 0,
|
||||||
|
(struct sockaddr*)&recvaddr, &addrlen);
|
||||||
|
|
||||||
|
tmpaddr = ntohl(recvaddr.sin_addr.s_addr);
|
||||||
|
printf(LABEL "Received %ld bytes from %d.%d.%d.%d:%d\n",
|
||||||
|
(long)nrecvd,
|
||||||
|
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
|
||||||
|
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff,
|
||||||
|
ntohs(recvaddr.sin_port));
|
||||||
|
|
||||||
|
/* Check for a receive error or zero bytes received. The negative
|
||||||
|
* return value indicates a receive error; Zero would mean that the
|
||||||
|
* other side of the "connection" performed an "orderly" shutdown.
|
||||||
|
* This should not occur with a UDP socket and so must also be an
|
||||||
|
* error of some kind.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (nrecvd <= 0)
|
||||||
|
{
|
||||||
|
if (nrecvd < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: recvfrom failed: %d\n", errno);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: recvfrom returned zero\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
goto errout_with_recvsd;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nrecvd != nrecvd)
|
||||||
|
{
|
||||||
|
fprintf(stderr, LABEL "ERROR: Number of bytes received differs from number sent\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Dump the received packet */
|
||||||
|
|
||||||
|
for (i = 0, j = 0; i < nrecvd; i++)
|
||||||
|
{
|
||||||
|
if ( g_rdbuffer[i] == ' ' && j >= 64)
|
||||||
|
{
|
||||||
|
putchar('\n');
|
||||||
|
j = 0;
|
||||||
|
}
|
||||||
|
else if (isprint(g_rdbuffer[i]))
|
||||||
|
{
|
||||||
|
putchar(g_rdbuffer[i]);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
else if (g_rdbuffer[i] == '\n')
|
||||||
|
{
|
||||||
|
putchar('\n');
|
||||||
|
j = 0;
|
||||||
|
}
|
||||||
|
else if (g_rdbuffer[i] != '\r')
|
||||||
|
{
|
||||||
|
printf("\%03o", g_rdbuffer[i]);
|
||||||
|
j += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(recvsd);
|
||||||
|
close(recvsd);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
|
errout_with_recvsd:
|
||||||
|
close(recvsd);
|
||||||
|
errout_with_sendsd:
|
||||||
|
close(sndsd);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
96
examples/bridge/host_net1.c
Normal file
96
examples/bridge/host_net1.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* examples/bridge/host_net1.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
|
*
|
||||||
|
* Authors: 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 "bridge_config.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Send on network 1 */
|
||||||
|
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_IFNAME CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_RECVPORT CONFIG_EXAMPLES_BRIDGE_NET1_RECVPORT
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_SNDPORT CONFIG_EXAMPLES_BRIDGE_NET1_SNDPORT
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_IOBUFIZE CONFIG_EXAMPLES_BRIDGE_NET1_IOBUFIZE
|
||||||
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET1_NOMAC
|
||||||
|
# define EXAMPLES_BRIDGE_SEND_NOMAC
|
||||||
|
#endif
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_MACADDR CONFIG_EXAMPLES_BRIDGE_NET1_MACADDR
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_IPADDR CONFIG_EXAMPLES_BRIDGE_NET1_IPADDR
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_DRIPADDR CONFIG_EXAMPLES_BRIDGE_NET1_DRIPADDR
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_NETMASK CONFIG_EXAMPLES_BRIDGE_NET1_NETMASK
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_IPHOST CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_STACKSIZE CONFIG_EXAMPLES_BRIDGE_NET1_STACKSIZE
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_PRIORITY CONFIG_EXAMPLES_BRIDGE_NET1_PRIORITY
|
||||||
|
|
||||||
|
/* Receive on network 2 */
|
||||||
|
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_IFNAME CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_RECVPORT CONFIG_EXAMPLES_BRIDGE_NET2_RECVPORT
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_SNDPORT CONFIG_EXAMPLES_BRIDGE_NET2_SNDPORT
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_IOBUFIZE CONFIG_EXAMPLES_BRIDGE_NET2_IOBUFIZE
|
||||||
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET2_NOMAC
|
||||||
|
# define EXAMPLES_BRIDGE_RECV_NOMAC
|
||||||
|
#endif
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_MACADDR CONFIG_EXAMPLES_BRIDGE_NET2_MACADDR
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_IPADDR CONFIG_EXAMPLES_BRIDGE_NET2_IPADDR
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_DRIPADDR CONFIG_EXAMPLES_BRIDGE_NET2_DRIPADDR
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_NETMASK CONFIG_EXAMPLES_BRIDGE_NET2_NETMASK
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_IPHOST CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_STACKSIZE CONFIG_EXAMPLES_BRIDGE_NET2_STACKSIZE
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_PRIORITY CONFIG_EXAMPLES_BRIDGE_NET2_PRIORITY
|
||||||
|
|
||||||
|
#define LABEL "NET1->2: "
|
||||||
|
|
||||||
|
#define MESSAGE \
|
||||||
|
"Great ambition is the passion of a great character. Those endowed" \
|
||||||
|
"with it may perform very good or very bad acts. All depends on the" \
|
||||||
|
"principles which direct them. -- Napoleon Bonaparte"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: main
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "host_main.c"
|
95
examples/bridge/host_net2.c
Normal file
95
examples/bridge/host_net2.c
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* examples/bridge/host_net2.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
|
*
|
||||||
|
* Authors: 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 "bridge_config.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Send on network 1 */
|
||||||
|
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_IFNAME CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_RECVPORT CONFIG_EXAMPLES_BRIDGE_NET2_RECVPORT
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_SNDPORT CONFIG_EXAMPLES_BRIDGE_NET2_SNDPORT
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_IOBUFIZE CONFIG_EXAMPLES_BRIDGE_NET2_IOBUFIZE
|
||||||
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET2_NOMAC
|
||||||
|
# define EXAMPLES_BRIDGE_SEND_NOMAC
|
||||||
|
#endif
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_MACADDR CONFIG_EXAMPLES_BRIDGE_NET2_MACADDR
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_IPADDR CONFIG_EXAMPLES_BRIDGE_NET2_IPADDR
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_DRIPADDR CONFIG_EXAMPLES_BRIDGE_NET2_DRIPADDR
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_NETMASK CONFIG_EXAMPLES_BRIDGE_NET2_NETMASK
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_IPHOST CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_STACKSIZE CONFIG_EXAMPLES_BRIDGE_NET2_STACKSIZE
|
||||||
|
#define EXAMPLES_BRIDGE_SEND_PRIORITY CONFIG_EXAMPLES_BRIDGE_NET2_PRIORITY
|
||||||
|
|
||||||
|
/* Receive on network 2 */
|
||||||
|
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_IFNAME CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_RECVPORT CONFIG_EXAMPLES_BRIDGE_NET1_RECVPORT
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_SNDPORT CONFIG_EXAMPLES_BRIDGE_NET1_SNDPORT
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_IOBUFIZE CONFIG_EXAMPLES_BRIDGE_NET1_IOBUFIZE
|
||||||
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET1_NOMAC
|
||||||
|
# define EXAMPLES_BRIDGE_RECV_NOMAC
|
||||||
|
#endif
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_MACADDR CONFIG_EXAMPLES_BRIDGE_NET1_MACADDR
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_IPADDR CONFIG_EXAMPLES_BRIDGE_NET1_IPADDR
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_DRIPADDR CONFIG_EXAMPLES_BRIDGE_NET1_DRIPADDR
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_NETMASK CONFIG_EXAMPLES_BRIDGE_NET1_NETMASK
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_IPHOST CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_STACKSIZE CONFIG_EXAMPLES_BRIDGE_NET1_STACKSIZE
|
||||||
|
#define EXAMPLES_BRIDGE_RECV_PRIORITY CONFIG_EXAMPLES_BRIDGE_NET1_PRIORITY
|
||||||
|
|
||||||
|
#define LABEL "NET2->1: "
|
||||||
|
|
||||||
|
#define MESSAGE \
|
||||||
|
"Ideas are more powerful than guns. We would not let our enemies have" \
|
||||||
|
"guns, why should we let them have ideas. -- Joseph Stalin"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: main
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "host_main.c"
|
Loading…
x
Reference in New Issue
Block a user