2014-11-17 17:53:43 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/bridge/bridge_main.c
|
|
|
|
*
|
2015-07-12 19:53:23 +02:00
|
|
|
* Copyright (C) 2014-2015 Gregory Nutt. All rights reserved.
|
2014-11-17 17:53:43 +01:00
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/arp.h>
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC) || \
|
|
|
|
defined(CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC)
|
|
|
|
# include <arpa/inet.h>
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "netutils/dhcpc.h"
|
2014-11-17 17:53:43 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static in_addr_t g_net1_ipaddr;
|
|
|
|
static in_addr_t g_net2_ipaddr;
|
|
|
|
static uint8_t g_net1_buffer[CONFIG_EXAMPLES_BRIDGE_NET1_IOBUFIZE];
|
|
|
|
static uint8_t g_net2_buffer[CONFIG_EXAMPLES_BRIDGE_NET2_IOBUFIZE];
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: briget_net1_setup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int briget_net1_setup(void)
|
|
|
|
{
|
|
|
|
/* If this task is executed as an NSH built-in function, then the
|
|
|
|
* network has already been configured by NSH's start-up logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
struct in_addr addr;
|
|
|
|
#if defined(CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC) || \
|
|
|
|
defined(CONFIG_EXAMPLES_BRIDGE_NET1_NOMAC)
|
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC
|
|
|
|
struct dhcpc_state ds;
|
|
|
|
void *handle;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("NET1: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME);
|
|
|
|
|
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET1_NOMAC
|
|
|
|
mac[0] = (CONFIG_EXAMPLES_BRIDGE_NET1_MACADDR >> (8 * 5)) & 0xff;
|
|
|
|
mac[1] = (CONFIG_EXAMPLES_BRIDGE_NET1_MACADDR >> (8 * 4)) & 0xff;
|
|
|
|
mac[2] = (CONFIG_EXAMPLES_BRIDGE_NET1_MACADDR >> (8 * 3)) & 0xff;
|
|
|
|
mac[3] = (CONFIG_EXAMPLES_BRIDGE_NET1_MACADDR >> (8 * 2)) & 0xff;
|
|
|
|
mac[4] = (CONFIG_EXAMPLES_BRIDGE_NET1_MACADDR >> (8 * 1)) & 0xff;
|
|
|
|
mac[5] = (CONFIG_EXAMPLES_BRIDGE_NET1_MACADDR >> (8 * 0)) & 0xff;
|
|
|
|
netlib_setmacaddr(CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME, mac);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up our host address */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC
|
|
|
|
addr.s_addr = 0;
|
|
|
|
#else
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET1_IPADDR);
|
|
|
|
#endif
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr(CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME, &addr);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET1_DRIPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr(CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME, &addr);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET1_NETMASK);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask(CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME, &addr);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
2017-05-19 17:30:26 +02:00
|
|
|
/* New versions of netlib_set_ipvXaddr will not bring the network up,
|
2017-05-19 17:50:26 +02:00
|
|
|
* So ensure the network is really up at this point.
|
|
|
|
*/
|
2017-05-19 17:30:26 +02:00
|
|
|
|
|
|
|
netlib_ifup("eth0");
|
|
|
|
|
2014-11-17 17:53:43 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC
|
|
|
|
/* Get the MAC address of the NIC */
|
|
|
|
|
|
|
|
netlib_getmacaddr(CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME, mac);
|
|
|
|
|
|
|
|
/* Set up the DHCPC modules */
|
|
|
|
|
2017-05-19 22:34:00 +02:00
|
|
|
handle = dhcpc_open("eth0", &mac, IFHWADDRLEN);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* Get an IP address. Note: there is no logic here for renewing the address in this
|
|
|
|
* example. The address should be renewed in ds.lease_time/2 seconds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: dhcpc_open failed\n");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dhcpc_request(handle, &ds) != OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: dhcpc_request failed\n");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr(CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME, &ds.ipaddr);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
if (ds.netmask.s_addr != 0)
|
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask(CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME, &ds.netmask);
|
2014-11-17 17:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ds.default_router.s_addr != 0)
|
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr(CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME, &ds.default_router);
|
2014-11-17 17:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ds.dnsaddr.s_addr != 0)
|
|
|
|
{
|
2015-07-12 19:53:23 +02:00
|
|
|
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
|
2014-11-17 17:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dhcpc_close(handle);
|
|
|
|
printf("NET1: Assigned IP: %s\n", inet_ntoa(ds.ipaddr));
|
|
|
|
|
|
|
|
/* Save the IP address in network order */
|
|
|
|
|
|
|
|
g_net1_ipaddr = ds.ipaddr;
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Save the IP address in network order */
|
|
|
|
|
|
|
|
g_net1_ipaddr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET1_IPADDR);
|
|
|
|
|
|
|
|
#endif /* CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC */
|
2014-11-21 15:20:25 +01:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2014-11-17 17:53:43 +01:00
|
|
|
#endif /* CONFIG_NSH_BUILTIN_APPS */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: briget_net2_setup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int briget_net2_setup(void)
|
|
|
|
{
|
|
|
|
/* If this task is executed as an NSH built-in function, then the
|
|
|
|
* network has already been configured by NSH's start-up logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
struct in_addr addr;
|
|
|
|
#if defined(CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC) || \
|
|
|
|
defined(CONFIG_EXAMPLES_BRIDGE_NET2_NOMAC)
|
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC
|
|
|
|
struct dhcpc_state ds;
|
|
|
|
void *handle;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("NET2: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME);
|
|
|
|
|
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET2_NOMAC
|
|
|
|
mac[0] = (CONFIG_EXAMPLES_BRIDGE_NET2_MACADDR >> (8 * 5)) & 0xff;
|
|
|
|
mac[1] = (CONFIG_EXAMPLES_BRIDGE_NET2_MACADDR >> (8 * 4)) & 0xff;
|
|
|
|
mac[2] = (CONFIG_EXAMPLES_BRIDGE_NET2_MACADDR >> (8 * 3)) & 0xff;
|
|
|
|
mac[3] = (CONFIG_EXAMPLES_BRIDGE_NET2_MACADDR >> (8 * 2)) & 0xff;
|
|
|
|
mac[4] = (CONFIG_EXAMPLES_BRIDGE_NET2_MACADDR >> (8 * 1)) & 0xff;
|
|
|
|
mac[5] = (CONFIG_EXAMPLES_BRIDGE_NET2_MACADDR >> (8 * 0)) & 0xff;
|
|
|
|
netlib_setmacaddr(CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME, mac);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up our host address */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC
|
|
|
|
addr.s_addr = 0;
|
|
|
|
#else
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET2_IPADDR);
|
|
|
|
#endif
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr(CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME, &addr);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET2_DRIPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr(CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME, &addr);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET2_NETMASK);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask(CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME, &addr);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC
|
|
|
|
/* Get the MAC address of the NIC */
|
|
|
|
|
|
|
|
netlib_getmacaddr(CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME, mac);
|
|
|
|
|
|
|
|
/* Set up the DHCPC modules */
|
|
|
|
|
|
|
|
handle = dhcpc_open(&mac, IFHWADDRLEN);
|
|
|
|
|
|
|
|
/* Get an IP address. Note: there is no logic here for renewing the address in this
|
|
|
|
* example. The address should be renewed in ds.lease_time/2 seconds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: dhcpc_open failed\n");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dhcpc_request(handle, &ds) != OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: dhcpc_request failed\n");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr(CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME, &ds.ipaddr);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
if (ds.netmask.s_addr != 0)
|
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask(CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME, &ds.netmask);
|
2014-11-17 17:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ds.default_router.s_addr != 0)
|
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr(CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME, &ds.default_router);
|
2014-11-17 17:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ds.dnsaddr.s_addr != 0)
|
|
|
|
{
|
2015-07-12 19:53:23 +02:00
|
|
|
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
|
2014-11-17 17:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dhcpc_close(handle);
|
|
|
|
printf("NET1: Assigned IP: %s\n", inet_ntoa(ds.ipaddr));
|
|
|
|
|
|
|
|
/* Save the IP address in network order */
|
|
|
|
|
|
|
|
g_net2_ipaddr = ds.ipaddr;
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Save the IP address in network order */
|
|
|
|
|
|
|
|
g_net2_ipaddr = HTONL(CONFIG_EXAMPLES_BRIDGE_NET2_IPADDR);
|
|
|
|
|
|
|
|
#endif /* CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC */
|
2014-11-21 15:20:25 +01:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2014-11-17 17:53:43 +01:00
|
|
|
#endif /* CONFIG_NSH_BUILTIN_APPS */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bridge_net1_worker
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bridge_net1_worker(int argc, char *argv[])
|
|
|
|
{
|
2014-11-21 17:16:19 +01:00
|
|
|
struct sockaddr_in receiver;
|
|
|
|
struct sockaddr_in sender;
|
|
|
|
struct sockaddr_in fromaddr;
|
|
|
|
struct sockaddr_in toaddr;
|
2014-11-17 17:53:43 +01:00
|
|
|
socklen_t addrlen;
|
|
|
|
in_addr_t tmpaddr;
|
|
|
|
ssize_t nrecvd;
|
|
|
|
ssize_t nsent;
|
|
|
|
int optval;
|
|
|
|
int recvsd;
|
|
|
|
int sndsd;
|
|
|
|
|
|
|
|
printf("NET1: Worker started. PID=%d\n", getpid());
|
|
|
|
|
|
|
|
/* Create a UDP receive socket on network 1 */
|
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
tmpaddr = ntohl(g_net1_ipaddr);
|
|
|
|
printf("NET1: Create receive socket: %d.%d.%d.%d:%d\n",
|
|
|
|
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
|
|
|
|
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff,
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET1_RECVPORT);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
recvsd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (recvsd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: Failed to create receive socket: %d\n", errno);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set socket to reuse address */
|
|
|
|
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(recvsd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the socket to a local address */
|
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
receiver.sin_family = AF_INET;
|
|
|
|
receiver.sin_port = HTONS(CONFIG_EXAMPLES_BRIDGE_NET1_RECVPORT);
|
|
|
|
receiver.sin_addr.s_addr = g_net1_ipaddr;
|
2014-11-17 17:53:43 +01:00
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
if (bind(recvsd, (struct sockaddr*)&receiver, sizeof(struct sockaddr_in)) < 0)
|
2014-11-17 17:53:43 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: bind failure: %d\n", errno);
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a UDP send socket on network 2 */
|
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
tmpaddr = ntohl(g_net2_ipaddr);
|
|
|
|
printf("NET1: Create send socket: %d.%d.%d.%d:INPORT_ANY\n",
|
|
|
|
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
|
|
|
|
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
sndsd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sndsd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: Failed to create send socket: %d\n", errno);
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set socket to reuse address */
|
|
|
|
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(sndsd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the socket to a local address */
|
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
sender.sin_family = AF_INET;
|
|
|
|
sender.sin_port = 0;
|
|
|
|
sender.sin_addr.s_addr = g_net2_ipaddr;
|
2014-11-17 17:53:43 +01:00
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
if (bind(sndsd, (struct sockaddr*)&sender, sizeof(struct sockaddr_in)) < 0)
|
2014-11-17 17:53:43 +01:00
|
|
|
{
|
|
|
|
printf("NET1: bind failure: %d\n", errno);
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then receive and forward UDP packets forever */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Read a packet on network 1 */
|
|
|
|
|
|
|
|
printf("NET1: Receiving up to %d bytes on network 1\n",
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET1_IOBUFIZE);
|
|
|
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
nrecvd = recvfrom(recvsd, g_net1_buffer, CONFIG_EXAMPLES_BRIDGE_NET1_IOBUFIZE, 0,
|
2014-11-21 17:16:19 +01:00
|
|
|
(FAR struct sockaddr*)&fromaddr, &addrlen);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
tmpaddr = ntohl(fromaddr.sin_addr.s_addr);
|
2014-11-25 15:09:57 +01:00
|
|
|
printf("NET1: Received %ld bytes from %d.%d.%d.%d:%d\n",
|
|
|
|
(long)nrecvd,
|
2014-11-17 17:53:43 +01:00
|
|
|
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
|
|
|
|
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff,
|
2014-11-21 17:16:19 +01:00
|
|
|
ntohs(fromaddr.sin_port));
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* 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, "NET1 ERROR: recvfrom failed: %d\n", errno);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: recvfrom returned zero\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the newly received packet out network 2 */
|
|
|
|
|
2014-11-25 15:09:57 +01:00
|
|
|
printf("NET1: Sending %ld bytes on network 2: %d.%d.%d.%d:%d\n",
|
|
|
|
(long)nrecvd,
|
2014-11-21 17:16:19 +01:00
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST >> 24,
|
|
|
|
(CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST >> 16) & 0xff,
|
|
|
|
(CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST >> 8) & 0xff,
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST & 0xff,
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET2_HOSTPORT);
|
|
|
|
|
|
|
|
toaddr.sin_family = AF_INET;
|
|
|
|
toaddr.sin_port = htons(CONFIG_EXAMPLES_BRIDGE_NET2_HOSTPORT);
|
|
|
|
toaddr.sin_addr.s_addr = htonl(CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST);
|
|
|
|
|
2014-11-17 17:53:43 +01:00
|
|
|
nsent = sendto(sndsd, g_net1_buffer, nrecvd, 0,
|
2014-11-21 17:16:19 +01:00
|
|
|
(struct sockaddr*)&toaddr, sizeof(struct sockaddr_in));
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* Check for send errors */
|
|
|
|
|
|
|
|
if (nsent < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET1 ERROR: sendto failed: %d\n", errno);
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
else if (nsent != nrecvd)
|
|
|
|
{
|
2014-11-25 21:51:10 +01:00
|
|
|
fprintf(stderr, "NET1 ERROR: Bad send length: %ld Expected: %ld\n",
|
|
|
|
(long)nsent, (long)nrecvd);
|
2014-11-17 17:53:43 +01:00
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(recvsd);
|
|
|
|
close(recvsd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
errout_with_sendsd:
|
|
|
|
close(sndsd);
|
|
|
|
errout_with_recvsd:
|
|
|
|
close(recvsd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bridge_net2_worker
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bridge_net2_worker(int argc, char *argv[])
|
|
|
|
{
|
2014-11-21 17:16:19 +01:00
|
|
|
struct sockaddr_in receiver;
|
|
|
|
struct sockaddr_in sender;
|
|
|
|
struct sockaddr_in fromaddr;
|
|
|
|
struct sockaddr_in toaddr;
|
2014-11-17 17:53:43 +01:00
|
|
|
socklen_t addrlen;
|
|
|
|
in_addr_t tmpaddr;
|
|
|
|
ssize_t nrecvd;
|
|
|
|
ssize_t nsent;
|
|
|
|
int optval;
|
|
|
|
int recvsd;
|
|
|
|
int sndsd;
|
|
|
|
|
|
|
|
printf("NET2: Worker started. PID=%d\n", getpid());
|
|
|
|
|
|
|
|
/* Create a UDP receive socket on network 2 */
|
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
tmpaddr = ntohl(g_net2_ipaddr);
|
|
|
|
printf("NET2: Create receive socket: %d.%d.%d.%d:%d\n",
|
|
|
|
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
|
|
|
|
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff,
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET2_RECVPORT);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
recvsd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (recvsd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: Failed to create receive socket: %d\n", errno);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set socket to reuse address */
|
|
|
|
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(recvsd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the socket to a local address */
|
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
receiver.sin_family = AF_INET;
|
|
|
|
receiver.sin_port = HTONS(CONFIG_EXAMPLES_BRIDGE_NET2_RECVPORT);
|
|
|
|
receiver.sin_addr.s_addr = g_net2_ipaddr;
|
2014-11-17 17:53:43 +01:00
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
if (bind(recvsd, (struct sockaddr*)&receiver, sizeof(struct sockaddr_in)) < 0)
|
2014-11-17 17:53:43 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: bind failure: %d\n", errno);
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a UDP send socket on network 1 */
|
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
tmpaddr = ntohl(g_net1_ipaddr);
|
|
|
|
printf("NET2: Create send socket: %d.%d.%d.%d:INPORT_ANY\n",
|
|
|
|
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
|
|
|
|
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
sndsd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sndsd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: Failed to create send socket: %d\n", errno);
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set socket to reuse address */
|
|
|
|
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(sndsd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the socket to a local address */
|
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
sender.sin_family = AF_INET;
|
|
|
|
sender.sin_port = 0;
|
|
|
|
sender.sin_addr.s_addr = g_net1_ipaddr;
|
2014-11-17 17:53:43 +01:00
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
if (bind(sndsd, (struct sockaddr*)&sender, sizeof(struct sockaddr_in)) < 0)
|
2014-11-17 17:53:43 +01:00
|
|
|
{
|
|
|
|
printf("NET2: bind failure: %d\n", errno);
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then receive and forward UDP packets forever */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Read a packet on network 2 */
|
|
|
|
|
|
|
|
printf("NET2: Receiving up to %d bytes on network 2\n",
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET2_IOBUFIZE);
|
|
|
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
nrecvd = recvfrom(recvsd, g_net2_buffer, CONFIG_EXAMPLES_BRIDGE_NET2_IOBUFIZE, 0,
|
2014-11-21 17:16:19 +01:00
|
|
|
(FAR struct sockaddr*)&fromaddr, &addrlen);
|
2014-11-17 17:53:43 +01:00
|
|
|
|
2014-11-21 17:16:19 +01:00
|
|
|
tmpaddr = ntohl(fromaddr.sin_addr.s_addr);
|
2014-11-25 15:09:57 +01:00
|
|
|
printf("NET2: Received %ld bytes from %d.%d.%d.%d:%d\n",
|
|
|
|
(long)nrecvd,
|
2014-11-17 17:53:43 +01:00
|
|
|
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
|
|
|
|
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff,
|
2014-11-21 17:16:19 +01:00
|
|
|
ntohs(fromaddr.sin_port));
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* 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, "NET2 ERROR: recvfrom failed: %d\n", errno);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: recvfrom returned zero\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the newly received packet out network 1 */
|
|
|
|
|
2014-11-25 15:09:57 +01:00
|
|
|
printf("NET2: Sending %ld bytes on network 1: %d.%d.%d.%d:%d\n",
|
|
|
|
(long)nrecvd,
|
2014-11-21 17:16:19 +01:00
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST >> 24,
|
|
|
|
(CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST >> 16) & 0xff,
|
|
|
|
(CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST >> 8) & 0xff,
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST & 0xff,
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET1_HOSTPORT);
|
|
|
|
|
|
|
|
toaddr.sin_family = AF_INET;
|
|
|
|
toaddr.sin_port = htons(CONFIG_EXAMPLES_BRIDGE_NET1_HOSTPORT);
|
|
|
|
toaddr.sin_addr.s_addr = htonl(CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST);
|
|
|
|
|
2014-11-17 17:53:43 +01:00
|
|
|
nsent = sendto(sndsd, g_net2_buffer, nrecvd, 0,
|
2014-11-21 17:16:19 +01:00
|
|
|
(struct sockaddr*)&toaddr, sizeof(struct sockaddr_in));
|
2014-11-17 17:53:43 +01:00
|
|
|
|
|
|
|
/* Check for send errors */
|
|
|
|
|
|
|
|
if (nsent < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "NET2 ERROR: sendto failed: %d\n", errno);
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
else if (nsent != nrecvd)
|
|
|
|
{
|
2014-11-25 15:09:57 +01:00
|
|
|
fprintf(stderr, "NET2 ERROR: Bad send length: %ld Expected: %ld\n",
|
|
|
|
(long)nsent, (long)nrecvd);
|
2014-11-17 17:53:43 +01:00
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(recvsd);
|
|
|
|
close(recvsd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
errout_with_sendsd:
|
|
|
|
close(sndsd);
|
|
|
|
errout_with_recvsd:
|
|
|
|
close(recvsd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* bridge_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_BUILD_KERNEL
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
#else
|
|
|
|
int bridge_main(int argc, char *argv[])
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
pid_t net1_worker;
|
|
|
|
pid_t net2_worker;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize networks */
|
|
|
|
|
|
|
|
ret = briget_net1_setup();
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to setup network 1\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = briget_net2_setup();
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to setup network 2\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start network daemons */
|
|
|
|
|
|
|
|
printf("Start network 1 worker\n");
|
|
|
|
|
|
|
|
net1_worker = task_create("NET1 Worker", CONFIG_EXAMPLES_BRIDGE_NET1_PRIORITY,
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET1_STACKSIZE, bridge_net1_worker,
|
|
|
|
NULL);
|
|
|
|
if (net1_worker < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to start network daemon 1\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Start network 2 worker\n");
|
|
|
|
|
|
|
|
net2_worker = task_create("NET2 Worker", CONFIG_EXAMPLES_BRIDGE_NET2_PRIORITY,
|
|
|
|
CONFIG_EXAMPLES_BRIDGE_NET2_STACKSIZE, bridge_net2_worker,
|
|
|
|
NULL);
|
|
|
|
if (net2_worker < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to start network daemon 2\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|