mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #14349 from miri64/lwip/enh/dual-stack
pkg/lwip: enable IPv4/IPv6 dual stack mode
This commit is contained in:
commit
49ab078c03
@ -148,27 +148,33 @@ extern void stm32_eth_netdev_setup(netdev_t *netdev);
|
||||
extern netdev_ieee802154_t nrf802154_dev;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Add interface with IPv4 or dual stack support.
|
||||
*/
|
||||
static struct netif *_netif_add(struct netif *netif, void *state,
|
||||
netif_init_fn init, netif_input_fn input)
|
||||
{
|
||||
#if IS_USED(MODULE_LWIP_IPV4)
|
||||
return netif_add(netif, ip_2_ip4(IP4_ADDR_ANY), ip_2_ip4(IP4_ADDR_ANY),
|
||||
ip_2_ip4(IP4_ADDR_ANY), state, init, input);
|
||||
#else /* IS_USED(MODULE_LWIP_IPV4) */
|
||||
return netif_add(netif, state, init, input);
|
||||
#endif /* IS_USED(MODULE_LWIP_IPV4) */
|
||||
}
|
||||
|
||||
void lwip_bootstrap(void)
|
||||
{
|
||||
(void)_netif_add; /* in case it is not used */
|
||||
/* TODO: do for every eligible netdev */
|
||||
#ifdef LWIP_NETIF_NUMOF
|
||||
#ifdef MODULE_NETDEV_TAP
|
||||
for (unsigned i = 0; i < LWIP_NETIF_NUMOF; i++) {
|
||||
netdev_tap_setup(&netdev_taps[i], &netdev_tap_params[i]);
|
||||
#ifdef MODULE_LWIP_IPV4
|
||||
if (netif_add(&netif[i], IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY,
|
||||
&netdev_taps[i], lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
if (_netif_add(&netif[i], &netdev_taps[i], lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add netdev_tap device\n");
|
||||
return;
|
||||
}
|
||||
#else /* MODULE_LWIP_IPV4 */
|
||||
if (netif_add(&netif[i], &netdev_taps[i], lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add netdev_tap device\n");
|
||||
return;
|
||||
}
|
||||
#endif /* MODULE_LWIP_IPV4 */
|
||||
}
|
||||
#elif defined(MODULE_MRF24J40)
|
||||
for (unsigned i = 0; i < LWIP_NETIF_NUMOF; i++) {
|
||||
@ -191,19 +197,11 @@ void lwip_bootstrap(void)
|
||||
#elif defined(MODULE_ENC28J60)
|
||||
for (unsigned i = 0; i < LWIP_NETIF_NUMOF; i++) {
|
||||
enc28j60_setup(&enc28j60_devs[i], &enc28j60_params[i]);
|
||||
#ifdef MODULE_LWIP_IPV4
|
||||
if (netif_add(&netif[0], IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY,
|
||||
&enc28j60_devs[i], lwip_netdev_init, tcpip_input) == NULL) {
|
||||
if (_netif_add(&netif[0], &enc28j60_devs[i], lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add enc28j60 device\n");
|
||||
return;
|
||||
}
|
||||
#else /* MODULE_LWIP_IPV4 */
|
||||
if (netif_add(&netif[0], &enc28j60_devs[i], lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add enc28j60 device\n");
|
||||
return;
|
||||
}
|
||||
#endif /* MODULE_LWIP_IPV4 */
|
||||
}
|
||||
#elif defined(MODULE_SOCKET_ZEP)
|
||||
for (unsigned i = 0; i < LWIP_NETIF_NUMOF; i++) {
|
||||
@ -216,49 +214,25 @@ void lwip_bootstrap(void)
|
||||
}
|
||||
#elif defined(MODULE_ESP_ETH)
|
||||
esp_eth_setup(&_esp_eth_dev);
|
||||
#ifdef MODULE_LWIP_IPV4
|
||||
if (netif_add(&netif[0], IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY,
|
||||
&_esp_eth_dev, lwip_netdev_init, tcpip_input) == NULL) {
|
||||
if (_netif_add(&netif[0], &_esp_eth_dev, lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add esp_eth device\n");
|
||||
return;
|
||||
}
|
||||
#else /* MODULE_LWIP_IPV4 */
|
||||
if (netif_add(&netif[0], &_esp_eth_dev, lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add esp_eth device\n");
|
||||
return;
|
||||
}
|
||||
#endif /* MODULE_LWIP_IPV4 */
|
||||
#elif defined(MODULE_ESP_WIFI)
|
||||
esp_wifi_setup(&_esp_wifi_dev);
|
||||
#ifdef MODULE_LWIP_IPV4
|
||||
if (netif_add(&netif[0], IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY,
|
||||
&_esp_wifi_dev, lwip_netdev_init, tcpip_input) == NULL) {
|
||||
if (_netif_add(&netif[0], &_esp_wifi_dev, lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add esp_wifi device\n");
|
||||
return;
|
||||
}
|
||||
#else /* MODULE_LWIP_IPV4 */
|
||||
if (netif_add(&netif[0], &_esp_wifi_dev, lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add esp_wifi device\n");
|
||||
return;
|
||||
}
|
||||
#endif /* MODULE_LWIP_IPV4 */
|
||||
#elif defined(MODULE_STM32_ETH)
|
||||
stm32_eth_netdev_setup(&stm32_eth);
|
||||
#ifdef MODULE_LWIP_IPV4
|
||||
if (netif_add(&netif[0], IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY,
|
||||
&stm32_eth, lwip_netdev_init, tcpip_input) == NULL) {
|
||||
if (_netif_add(&netif[0], &stm32_eth, lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add stm32_eth device\n");
|
||||
return;
|
||||
}
|
||||
#else /* MODULE_LWIP_IPV4 */
|
||||
if (netif_add(&netif[0], &stm32_eth, lwip_netdev_init,
|
||||
tcpip_input) == NULL) {
|
||||
DEBUG("Could not add stm32_eth device\n");
|
||||
return;
|
||||
}
|
||||
#endif /* MODULE_LWIP_IPV4 */
|
||||
#elif defined(MODULE_NRF802154)
|
||||
if (netif_add(&netif[0], &nrf802154_dev, lwip_netdev_init,
|
||||
tcpip_6lowpan_input) == NULL) {
|
||||
|
@ -330,11 +330,6 @@ static int _create(int type, int proto, uint16_t flags, struct netconn **out)
|
||||
return -ENOMEM;
|
||||
}
|
||||
netconn_set_callback_arg(*out, NULL);
|
||||
#if LWIP_IPV4 && LWIP_IPV6
|
||||
if (type & NETCONN_TYPE_IPV6) {
|
||||
netconn_set_ipv6only(*out, 1);
|
||||
}
|
||||
#endif
|
||||
#if SO_REUSE
|
||||
if (flags & SOCK_FLAGS_REUSE_EP) {
|
||||
ip_set_option((*out)->pcb.ip, SOF_REUSEADDR);
|
||||
|
@ -29,22 +29,56 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "net/sock/udp.h"
|
||||
#include "net/sock/tcp.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Format UDP endpoint to string and port
|
||||
* @brief Format common IP-based transport layer endpoint to string and port
|
||||
*
|
||||
* @param[in] endpoint endpoint to format
|
||||
* @param[out] addr_str where to write address as string
|
||||
* @param[out] port where to write prt number as uint16_t
|
||||
* @param[out] port where to write port number as uint16_t
|
||||
*
|
||||
* @returns number of bytes written to @p addr_str on success
|
||||
* @returns <0 otherwise
|
||||
*/
|
||||
int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint, char *addr_str, uint16_t *port);
|
||||
int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint,
|
||||
char *addr_str, uint16_t *port);
|
||||
|
||||
/**
|
||||
* @brief Format TCP endpoint to string and port
|
||||
*
|
||||
* @param[in] endpoint endpoint to format
|
||||
* @param[out] addr_str where to write address as string
|
||||
* @param[out] port where to write port number as uint16_t
|
||||
*
|
||||
* @returns number of bytes written to @p addr_str on success
|
||||
* @returns <0 otherwise
|
||||
*/
|
||||
static inline int sock_tcp_ep_fmt(const sock_tcp_ep_t *endpoint,
|
||||
char *addr_str, uint16_t *port)
|
||||
{
|
||||
return sock_tl_ep_fmt(endpoint, addr_str, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Format UDP endpoint to string and port
|
||||
*
|
||||
* @param[in] endpoint endpoint to format
|
||||
* @param[out] addr_str where to write address as string
|
||||
* @param[out] port where to write port number as uint16_t
|
||||
*
|
||||
* @returns number of bytes written to @p addr_str on success
|
||||
* @returns <0 otherwise
|
||||
*/
|
||||
static inline int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint,
|
||||
char *addr_str, uint16_t *port)
|
||||
{
|
||||
return sock_tl_ep_fmt(endpoint, addr_str, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Split url to host:port and url path
|
||||
@ -68,6 +102,37 @@ int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint, char *addr_str, uint16_t *por
|
||||
*/
|
||||
int sock_urlsplit(const char *url, char *hostport, char *urlpath);
|
||||
|
||||
/**
|
||||
* @brief Convert string to common IP-based transport layer endpoint
|
||||
*
|
||||
* Takes eg., "[2001:db8::1]:1234" and converts it into the corresponding UDP
|
||||
* endpoint structure.
|
||||
*
|
||||
* @param[out] ep_out endpoint structure to fill
|
||||
* @param[in] str string to read from
|
||||
*
|
||||
* @returns 0 on success
|
||||
* @returns <0 otherwise
|
||||
*/
|
||||
int sock_tl_str2ep(struct _sock_tl_ep *ep_out, const char *str);
|
||||
|
||||
/**
|
||||
* @brief Convert string to TCP endpoint
|
||||
*
|
||||
* Takes eg., "[2001:db8::1]:1234" and converts it into the corresponding UDP
|
||||
* endpoint structure.
|
||||
*
|
||||
* @param[out] ep_out endpoint structure to fill
|
||||
* @param[in] str string to read from
|
||||
*
|
||||
* @returns 0 on success
|
||||
* @returns <0 otherwise
|
||||
*/
|
||||
static inline int sock_tcp_str2ep(sock_tcp_ep_t *ep_out, const char *str)
|
||||
{
|
||||
return sock_tl_str2ep(ep_out, str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert string to UDP endpoint
|
||||
*
|
||||
@ -80,7 +145,45 @@ int sock_urlsplit(const char *url, char *hostport, char *urlpath);
|
||||
* @returns 0 on success
|
||||
* @returns <0 otherwise
|
||||
*/
|
||||
int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str);
|
||||
static inline int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str)
|
||||
{
|
||||
return sock_tl_str2ep(ep_out, str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare the two given common IP-based transport layer endpoints
|
||||
*
|
||||
* The given endpoint identifiers are compared by checking their address family,
|
||||
* their addresses, and their port value.
|
||||
*
|
||||
* @param[in] a Endpoint A
|
||||
* @param[in] b Endpoint B
|
||||
*
|
||||
* @return true if given endpoint identifiers point to the same destination
|
||||
* @return false if given endpoint identifiers do not point to the same
|
||||
* destination, or if the address family is unknown
|
||||
*/
|
||||
bool sock_tl_ep_equal(const struct _sock_tl_ep *a,
|
||||
const struct _sock_tl_ep *b);
|
||||
|
||||
/**
|
||||
* @brief Compare the two given TCP endpoints
|
||||
*
|
||||
* The given endpoint identifiers are compared by checking their address family,
|
||||
* their addresses, and their port value.
|
||||
*
|
||||
* @param[in] a Endpoint A
|
||||
* @param[in] b Endpoint B
|
||||
*
|
||||
* @return true if given endpoint identifiers point to the same destination
|
||||
* @return false if given endpoint identifiers do not point to the same
|
||||
* destination, or if the address family is unknown
|
||||
*/
|
||||
static inline bool sock_tcp_ep_equal(const sock_tcp_ep_t *a,
|
||||
const sock_tcp_ep_t *b)
|
||||
{
|
||||
return sock_tl_ep_equal(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare the two given UDP endpoints
|
||||
@ -95,7 +198,11 @@ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str);
|
||||
* @return false if given endpoint identifiers do not point to the same
|
||||
* destination, or if the address family is unknown
|
||||
*/
|
||||
bool sock_udp_ep_equal(const sock_udp_ep_t *a, const sock_udp_ep_t *b);
|
||||
static inline bool sock_udp_ep_equal(const sock_udp_ep_t *a,
|
||||
const sock_udp_ep_t *b)
|
||||
{
|
||||
return sock_tl_ep_equal(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* @defgroup net_sock_util_conf SOCK utility functions compile configurations
|
||||
|
@ -36,19 +36,18 @@
|
||||
#define PORT_STR_LEN (5)
|
||||
#define NETIF_STR_LEN (5)
|
||||
|
||||
int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint, char *addr_str, uint16_t *port)
|
||||
int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint,
|
||||
char *addr_str, uint16_t *port)
|
||||
{
|
||||
void *addr_ptr;
|
||||
*addr_str = '\0';
|
||||
|
||||
switch (endpoint->family) {
|
||||
#if defined(SOCK_HAS_IPV4)
|
||||
case AF_INET:
|
||||
{
|
||||
addr_ptr = (void*)&endpoint->addr.ipv4;
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
#if defined(SOCK_HAS_IPV6)
|
||||
case AF_INET6:
|
||||
{
|
||||
@ -189,7 +188,7 @@ int _parse_netif(sock_udp_ep_t *ep_out, char *netifstart)
|
||||
return (netifend - netifstart);
|
||||
}
|
||||
|
||||
int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str)
|
||||
int sock_tl_str2ep(struct _sock_tl_ep *ep_out, const char *str)
|
||||
{
|
||||
unsigned brackets_flag;
|
||||
char *hoststart = (char*)str;
|
||||
@ -255,7 +254,8 @@ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
bool sock_udp_ep_equal(const sock_udp_ep_t *a, const sock_udp_ep_t *b)
|
||||
bool sock_tl_ep_equal(const struct _sock_tl_ep *a,
|
||||
const struct _sock_tl_ep *b)
|
||||
{
|
||||
assert(a && b);
|
||||
|
||||
|
@ -10,10 +10,14 @@ ifneq (0, $(LWIP_IPV4))
|
||||
CFLAGS += -DETHARP_SUPPORT_STATIC_ENTRIES=1
|
||||
LWIP_IPV6 ?= 0
|
||||
else
|
||||
# use IPv6 as default IP protocol when IPv4 is not explicitly selected
|
||||
LWIP_IPV6 ?= 1
|
||||
endif
|
||||
|
||||
ifneq (0, $(LWIP_IPV6))
|
||||
USEMODULE += ipv6_addr
|
||||
USEMODULE += lwip_ipv6
|
||||
USEMODULE += lwip_ipv6_autoconfig
|
||||
LWIP_IPV6 ?= 1
|
||||
endif
|
||||
|
||||
# including lwip_ipv6_mld would currently break this test on at86rf2xx radios
|
||||
@ -22,6 +26,7 @@ USEMODULE += lwip_udp lwip_sock_udp
|
||||
USEMODULE += lwip_tcp lwip_sock_tcp
|
||||
USEMODULE += lwip_sock_async
|
||||
USEMODULE += sock_async_event
|
||||
USEMODULE += sock_util
|
||||
USEMODULE += shell
|
||||
USEMODULE += shell_commands
|
||||
USEMODULE += ps
|
||||
|
@ -1,6 +1,7 @@
|
||||
BOARD_INSUFFICIENT_MEMORY := \
|
||||
airfy-beacon \
|
||||
blackpill \
|
||||
bluepill \
|
||||
hifive1 \
|
||||
hifive1b \
|
||||
i-nucleo-lrwan1 \
|
||||
|
@ -21,10 +21,20 @@
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "kernel_defines.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if IS_USED(MODULE_LWIP_IPV6)
|
||||
#include "net/ipv6.h"
|
||||
#define SOCK_IP_EP_ANY SOCK_IPV6_EP_ANY
|
||||
#elif IS_USED(MODULE_LWIP_IPV4)
|
||||
#include "net/ipv4.h"
|
||||
#define SOCK_IP_EP_ANY SOCK_IPV4_EP_ANY
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Application configuration
|
||||
* @{
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "kernel_defines.h"
|
||||
#include "od.h"
|
||||
#include "net/af.h"
|
||||
#include "net/sock/async/event.h"
|
||||
@ -30,14 +31,6 @@
|
||||
#include "test_utils/expect.h"
|
||||
#include "xtimer.h"
|
||||
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
#include "net/ipv6.h"
|
||||
#define SOCK_IP_EP_ANY SOCK_IPV6_EP_ANY
|
||||
#else
|
||||
#include "net/ipv4.h"
|
||||
#define SOCK_IP_EP_ANY SOCK_IPV4_EP_ANY
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_SOCK_IP
|
||||
static char sock_inbuf[SOCK_INBUF_SIZE];
|
||||
static bool server_running;
|
||||
@ -62,15 +55,28 @@ static void _ip_recv(sock_ip_t *sock, sock_async_flags_t flags, void *arg)
|
||||
else {
|
||||
char addrstr[IPV6_ADDR_MAX_STR_LEN];
|
||||
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
printf("Received IP data from [%s]:\n",
|
||||
ipv6_addr_to_str(addrstr, (ipv6_addr_t *)&src.addr.ipv6,
|
||||
sizeof(addrstr)));
|
||||
#else
|
||||
printf("Received IP data from [%s]:\n",
|
||||
ipv4_addr_to_str(addrstr, (ipv4_addr_t *)&src.addr.ipv4,
|
||||
sizeof(addrstr)));
|
||||
printf("Received IP data from ");
|
||||
switch (src.family) {
|
||||
#if IS_USED(MODULE_LWIP_IPV4)
|
||||
case AF_INET:
|
||||
printf("[%s]:\n",
|
||||
ipv4_addr_to_str(addrstr,
|
||||
(ipv4_addr_t *)&src.addr.ipv4,
|
||||
sizeof(addrstr)));
|
||||
break;
|
||||
#endif
|
||||
#if IS_USED(MODULE_LWIP_IPV6)
|
||||
case AF_INET6:
|
||||
printf("[%s]:\n",
|
||||
ipv6_addr_to_str(addrstr,
|
||||
(ipv6_addr_t *)&src.addr.ipv6,
|
||||
sizeof(addrstr)));
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
printf("unspecified source\n");
|
||||
break;
|
||||
}
|
||||
od_hex_dump(sock_inbuf, res, 0);
|
||||
}
|
||||
}
|
||||
@ -105,14 +111,31 @@ static int ip_send(char *addr_str, char *port_str, char *data, unsigned int num,
|
||||
size_t data_len;
|
||||
|
||||
/* parse destination address */
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
if (ipv6_addr_from_str((ipv6_addr_t *)&dst.addr.ipv6, addr_str) == NULL) {
|
||||
#else
|
||||
if (ipv4_addr_from_str((ipv4_addr_t *)&dst.addr.ipv4, addr_str) == NULL) {
|
||||
#if IS_USED(MODULE_LWIP_IPV6)
|
||||
if (strchr(addr_str, ':')) {
|
||||
if (ipv6_addr_from_str((ipv6_addr_t *)&dst.addr.ipv6,
|
||||
addr_str) == NULL) {
|
||||
puts("Error: unable to parse destination address");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
dst.family = AF_INET6;
|
||||
}
|
||||
}
|
||||
#if IS_USED(MODULE_LWIP_IPV4)
|
||||
else
|
||||
#endif
|
||||
#endif
|
||||
#if IS_USED(MODULE_LWIP_IPV4)
|
||||
if (ipv4_addr_from_str((ipv4_addr_t *)&dst.addr.ipv4,
|
||||
addr_str) == NULL) {
|
||||
puts("Error: unable to parse destination address");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
dst.family = AF_INET;
|
||||
}
|
||||
#endif
|
||||
/* parse protocol */
|
||||
protocol = atoi(port_str);
|
||||
data_len = hex2ints(byte_data, data);
|
||||
@ -126,13 +149,10 @@ static int ip_send(char *addr_str, char *port_str, char *data, unsigned int num,
|
||||
puts("could not send");
|
||||
}
|
||||
else {
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
printf("Success: send %u byte over IPv6 to %s (next header: %u)\n",
|
||||
(unsigned)data_len, addr_str, protocol);
|
||||
#else
|
||||
printf("Success: send %u byte over IPv4 to %s (next header: %u)\n",
|
||||
(unsigned)data_len, addr_str, protocol);
|
||||
#endif
|
||||
printf("Success: send %u byte over %s to %s (next header: %u)\n",
|
||||
(unsigned)data_len,
|
||||
(dst.family == AF_INET6) ? "IPv6" : "IPv4",
|
||||
addr_str, protocol);
|
||||
}
|
||||
xtimer_usleep(delay);
|
||||
}
|
||||
|
@ -32,24 +32,36 @@
|
||||
#endif
|
||||
#include "shell.h"
|
||||
|
||||
#define IFCONFIG_FILLER " "
|
||||
|
||||
static int ifconfig(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
for (struct netif *iface = netif_list; iface != NULL; iface = iface->next) {
|
||||
printf("%s_%02u: ", iface->name, iface->num);
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
#if IS_USED(MODULE_LWIP_IPV6)
|
||||
char addrstr[IPV6_ADDR_MAX_STR_LEN];
|
||||
#elif IS_USED(MODULE_LWIP_IPV4)
|
||||
char addrstr[IPV4_ADDR_MAX_STR_LEN];
|
||||
#endif
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
|
||||
if (!ipv6_addr_is_unspecified((ipv6_addr_t *)&iface->ip6_addr[i])) {
|
||||
printf(" inet6 %s\n", ipv6_addr_to_str(addrstr, (ipv6_addr_t *)&iface->ip6_addr[i],
|
||||
sizeof(addrstr)));
|
||||
printf("%s inet6 %s\n", (i == 0) ? "" : IFCONFIG_FILLER,
|
||||
ipv6_addr_to_str(addrstr,
|
||||
(ipv6_addr_t *)&iface->ip6_addr[i],
|
||||
sizeof(addrstr)));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef MODULE_LWIP_IPV4
|
||||
char addrstr[IPV4_ADDR_MAX_STR_LEN];
|
||||
printf(" inet %s\n", ipv4_addr_to_str(addrstr, (ipv4_addr_t *)&iface->ip_addr,
|
||||
if (IS_USED(MODULE_LWIP_IPV6)) {
|
||||
/* insert spaces to be aligned with inet6 */
|
||||
printf(IFCONFIG_FILLER);
|
||||
}
|
||||
printf(" inet %s\n", ipv4_addr_to_str(addrstr,
|
||||
(ipv4_addr_t *)&iface->ip_addr,
|
||||
sizeof(addrstr)));
|
||||
#endif
|
||||
puts("");
|
||||
@ -67,7 +79,7 @@ static const shell_command_t shell_commands[] = {
|
||||
#ifdef MODULE_SOCK_UDP
|
||||
{ "udp", "Send UDP messages and listen for messages on UDP port", udp_cmd },
|
||||
#endif
|
||||
{ "ifconfig", "Shows assigned IPv6 addresses", ifconfig },
|
||||
{ "ifconfig", "Shows assigned IP addresses", ifconfig },
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
|
@ -25,19 +25,12 @@
|
||||
#include "net/af.h"
|
||||
#include "net/sock/async/event.h"
|
||||
#include "net/sock/tcp.h"
|
||||
#include "net/sock/util.h"
|
||||
#include "shell.h"
|
||||
#include "test_utils/expect.h"
|
||||
#include "thread.h"
|
||||
#include "xtimer.h"
|
||||
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
#include "net/ipv6.h"
|
||||
#define SOCK_IP_EP_ANY SOCK_IPV6_EP_ANY
|
||||
#else
|
||||
#include "net/ipv4.h"
|
||||
#define SOCK_IP_EP_ANY SOCK_IPV4_EP_ANY
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_SOCK_TCP
|
||||
static char sock_inbuf[SOCK_INBUF_SIZE];
|
||||
static bool server_running = false, client_running = false;
|
||||
@ -51,19 +44,14 @@ static event_queue_t _ev_queue;
|
||||
static void _tcp_recv(sock_tcp_t *sock, sock_async_flags_t flags, void *arg)
|
||||
{
|
||||
sock_tcp_ep_t client;
|
||||
uint16_t port;
|
||||
|
||||
expect(strcmp(arg, "test") == 0);
|
||||
if (sock_tcp_get_remote(sock, &client) < 0) {
|
||||
/* socket was disconnected between event firing and this handler */
|
||||
return;
|
||||
}
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
ipv6_addr_to_str(_addr_str, (ipv6_addr_t *)&client.addr.ipv6,
|
||||
sizeof(_addr_str));
|
||||
#else
|
||||
ipv4_addr_to_str(_addr_str, (ipv4_addr_t *)&client.addr.ipv4,
|
||||
sizeof(_addr_str));
|
||||
#endif
|
||||
sock_tcp_ep_fmt(&client, _addr_str, &port);
|
||||
if (flags & SOCK_ASYNC_MSG_RECV) {
|
||||
int res;
|
||||
|
||||
@ -71,8 +59,7 @@ static void _tcp_recv(sock_tcp_t *sock, sock_async_flags_t flags, void *arg)
|
||||
* connection */
|
||||
while ((res = sock_tcp_read(sock, sock_inbuf, sizeof(sock_inbuf),
|
||||
0)) >= 0) {
|
||||
printf("Received TCP data from client [%s]:%u:\n", _addr_str,
|
||||
client.port);
|
||||
printf("Received TCP data from client [%s]:%u\n", _addr_str, port);
|
||||
if (res > 0) {
|
||||
od_hex_dump(sock_inbuf, res, 0);
|
||||
}
|
||||
@ -82,7 +69,7 @@ static void _tcp_recv(sock_tcp_t *sock, sock_async_flags_t flags, void *arg)
|
||||
}
|
||||
}
|
||||
if (flags & SOCK_ASYNC_CONN_FIN) {
|
||||
printf("TCP connection to [%s]:%u reset\n", _addr_str, client.port);
|
||||
printf("TCP connection to [%s]:%u reset\n", _addr_str, port);
|
||||
sock_tcp_disconnect(sock);
|
||||
}
|
||||
}
|
||||
@ -100,17 +87,12 @@ static void _tcp_accept(sock_tcp_queue_t *queue, sock_async_flags_t flags,
|
||||
}
|
||||
else {
|
||||
sock_tcp_ep_t client;
|
||||
uint16_t port;
|
||||
|
||||
sock_tcp_event_init(sock, &_ev_queue, _tcp_recv, "test");
|
||||
sock_tcp_get_remote(sock, &client);
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
ipv6_addr_to_str(_addr_str, (ipv6_addr_t *)&client.addr.ipv6,
|
||||
sizeof(_addr_str));
|
||||
#else
|
||||
ipv4_addr_to_str(_addr_str, (ipv4_addr_t *)&client.addr.ipv4,
|
||||
sizeof(_addr_str));
|
||||
#endif
|
||||
printf("TCP client [%s]:%u connected\n", _addr_str, client.port);
|
||||
sock_tcp_ep_fmt(&client, _addr_str, &port);
|
||||
printf("TCP client [%s]:%u connected\n", _addr_str, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -138,28 +120,31 @@ static void *_server_thread(void *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int tcp_connect(char *addr_str, char *port_str, char *local_port_str)
|
||||
static int tcp_connect(char *addr_str, char *local_port_str)
|
||||
{
|
||||
sock_tcp_ep_t dst = SOCK_IP_EP_ANY;
|
||||
uint16_t local_port = 0;
|
||||
|
||||
if (client_running) {
|
||||
puts("Client already connected");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* parse destination address */
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
if (ipv6_addr_from_str((ipv6_addr_t *)&dst.addr.ipv6, addr_str) == NULL) {
|
||||
#else
|
||||
if (ipv4_addr_from_str((ipv4_addr_t *)&dst.addr.ipv4, addr_str) == NULL) {
|
||||
#endif
|
||||
if (sock_tcp_str2ep(&dst, addr_str) < 0) {
|
||||
puts("Error: unable to parse destination address");
|
||||
return 1;
|
||||
}
|
||||
/* parse port */
|
||||
dst.port = atoi(port_str);
|
||||
if (dst.port == 0) {
|
||||
puts("Error: no port or illegal port value provided");
|
||||
return 1;
|
||||
}
|
||||
if (local_port_str != NULL) {
|
||||
local_port = atoi(port_str);
|
||||
local_port = atoi(local_port_str);
|
||||
if (local_port == 0) {
|
||||
puts("Error: Illegal local port 0");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (sock_tcp_connect(&client_sock, &dst, local_port, 0) < 0) {
|
||||
puts("Error: unable to connect");
|
||||
@ -214,15 +199,15 @@ int tcp_cmd(int argc, char **argv)
|
||||
if (strcmp(argv[1], "connect") == 0) {
|
||||
char *local_port = NULL;
|
||||
|
||||
if (argc < 4) {
|
||||
printf("usage: %s connect <addr> <port> [local_port]\n",
|
||||
if (argc < 3) {
|
||||
printf("usage: %s connect <addr>:<port> [local_port]\n",
|
||||
argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc > 4) {
|
||||
local_port = argv[4];
|
||||
if (argc > 3) {
|
||||
local_port = argv[3];
|
||||
}
|
||||
return tcp_connect(argv[2], argv[3], local_port);
|
||||
return tcp_connect(argv[2], local_port);
|
||||
}
|
||||
if (strcmp(argv[1], "disconnect") == 0) {
|
||||
return tcp_disconnect();
|
||||
|
@ -235,7 +235,7 @@ def test_udpv6_send(board_group, application, env=None):
|
||||
receiver.sendline(u"udp server start %d" % port)
|
||||
# wait for neighbor discovery to be done
|
||||
time.sleep(5)
|
||||
sender.sendline(u"udp send %s %d ab:cd:ef" % (receiver_ip, port))
|
||||
sender.sendline(u"udp send [%s]:%d ab:cd:ef" % (receiver_ip, port))
|
||||
sender.expect_exact("Success: send 3 byte over UDP to [{}]:{}"
|
||||
.format(receiver_ip, port))
|
||||
receiver.expect(u"00000000 AB CD EF")
|
||||
@ -261,7 +261,7 @@ def test_tcpv6_send(board_group, application, env=None):
|
||||
server.sendline(u"tcp server start %d" % port)
|
||||
# wait for neighbor discovery to be done
|
||||
time.sleep(5)
|
||||
client.sendline(u"tcp connect %s %d" % (server_ip, port))
|
||||
client.sendline(u"tcp connect [%s]:%d" % (server_ip, port))
|
||||
server.expect(u"TCP client \\[%s\\]:[0-9]+ connected" % client_ip)
|
||||
client.sendline(u"tcp send affe:abe")
|
||||
client.expect_exact(u"Success: send 4 byte over TCP to server")
|
||||
@ -301,14 +301,14 @@ def test_tcpv6_multiconnect(board_group, application, env=None):
|
||||
server.sendline(u"tcp server start %d" % port)
|
||||
# wait for neighbor discovery to be done
|
||||
time.sleep(5)
|
||||
client.sendline(u"tcp connect %s %d" % (server_ip, port))
|
||||
client.sendline(u"tcp connect [%s]:%d" % (server_ip, port))
|
||||
server.expect(u"TCP client \\[%s\\]:[0-9]+ connected" % client_ip)
|
||||
with socket.socket(socket.AF_INET6) as sock:
|
||||
sock.connect(connect_addr)
|
||||
server.expect(u"Error on TCP accept \\[-[0-9]+\\]")
|
||||
client.sendline(u"tcp disconnect")
|
||||
server.expect(u"TCP connection to \\[%s\\]:[0-9]+ reset" % client_ip)
|
||||
client.sendline(u"tcp connect %s %d" % (server_ip, port))
|
||||
client.sendline(u"tcp connect [%s]:%d" % (server_ip, port))
|
||||
server.expect(u"TCP client \\[%s\\]:[0-9]+ connected" % client_ip)
|
||||
client.sendline(u"tcp disconnect")
|
||||
server.expect(u"TCP connection to \\[%s\\]:[0-9]+ reset" % client_ip)
|
||||
@ -342,7 +342,7 @@ def test_triple_send(board_group, application, env=None):
|
||||
receiver.sendline(u"tcp server start %d" % tcp_port)
|
||||
# wait for neighbor discovery to be done
|
||||
time.sleep(5)
|
||||
sender.sendline(u"udp send %s %d 01:23" % (receiver_ip, udp_port))
|
||||
sender.sendline(u"udp send [%s]:%d 01:23" % (receiver_ip, udp_port))
|
||||
sender.expect_exact(u"Success: send 2 byte over UDP to [%s]:%d" %
|
||||
(receiver_ip, udp_port))
|
||||
receiver.expect(u"00000000 01 23")
|
||||
@ -351,7 +351,7 @@ def test_triple_send(board_group, application, env=None):
|
||||
sender.expect_exact(u"Success: send 4 byte over IPv6 to %s (next header: %d)" %
|
||||
(receiver_ip, ipprot))
|
||||
receiver.expect(u"00000000 01 02 03 04")
|
||||
sender.sendline(u"tcp connect %s %d" % (receiver_ip, tcp_port))
|
||||
sender.sendline(u"tcp connect [%s]:%d" % (receiver_ip, tcp_port))
|
||||
receiver.expect(u"TCP client \\[%s\\]:[0-9]+ connected" % sender_ip)
|
||||
sender.sendline(u"tcp send dead:beef")
|
||||
sender.expect_exact(u"Success: send 4 byte over TCP to server")
|
||||
|
@ -25,19 +25,12 @@
|
||||
#include "net/af.h"
|
||||
#include "net/sock/async/event.h"
|
||||
#include "net/sock/udp.h"
|
||||
#include "net/sock/util.h"
|
||||
#include "shell.h"
|
||||
#include "test_utils/expect.h"
|
||||
#include "thread.h"
|
||||
#include "xtimer.h"
|
||||
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
#include "net/ipv6.h"
|
||||
#define SOCK_IP_EP_ANY SOCK_IPV6_EP_ANY
|
||||
#else
|
||||
#include "net/ipv4.h"
|
||||
#define SOCK_IP_EP_ANY SOCK_IPV4_EP_ANY
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_SOCK_UDP
|
||||
static char sock_inbuf[SOCK_INBUF_SIZE];
|
||||
static bool server_running;
|
||||
@ -61,16 +54,15 @@ static void _udp_recv(sock_udp_t *sock, sock_async_flags_t flags, void *arg)
|
||||
}
|
||||
else {
|
||||
char addrstr[IPV6_ADDR_MAX_STR_LEN];
|
||||
uint16_t port;
|
||||
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
printf("Received UDP data from [%s]:%" PRIu16 ":\n",
|
||||
ipv6_addr_to_str(addrstr, (ipv6_addr_t *)&src.addr.ipv6,
|
||||
sizeof(addrstr)), src.port);
|
||||
#else
|
||||
printf("Received UDP data from [%s]:%" PRIu16 ":\n",
|
||||
ipv4_addr_to_str(addrstr, (ipv4_addr_t *)&src.addr.ipv4,
|
||||
sizeof(addrstr)), src.port);
|
||||
#endif
|
||||
printf("Received UDP data from ");
|
||||
if (sock_udp_ep_fmt(&src, addrstr, &port) >= 0) {
|
||||
printf("[%s]:%u\n", addrstr, port);
|
||||
}
|
||||
else {
|
||||
printf("unspecified source\n");
|
||||
}
|
||||
od_hex_dump(sock_inbuf, res, 0);
|
||||
}
|
||||
}
|
||||
@ -99,7 +91,7 @@ static void *_server_thread(void *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num,
|
||||
static int udp_send(char *addr_str, char *data, unsigned int num,
|
||||
unsigned int delay)
|
||||
{
|
||||
sock_udp_ep_t dst = SOCK_IP_EP_ANY;
|
||||
@ -107,16 +99,14 @@ static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num
|
||||
size_t data_len;
|
||||
|
||||
/* parse destination address */
|
||||
#ifdef MODULE_LWIP_IPV6
|
||||
if (ipv6_addr_from_str((ipv6_addr_t *)&dst.addr.ipv6, addr_str) == NULL) {
|
||||
#else
|
||||
if (ipv4_addr_from_str((ipv4_addr_t *)&dst.addr.ipv4, addr_str) == NULL) {
|
||||
#endif
|
||||
if (sock_udp_str2ep(&dst, addr_str) < 0) {
|
||||
puts("Error: unable to parse destination address");
|
||||
return 1;
|
||||
}
|
||||
/* parse port */
|
||||
dst.port = atoi(port_str);
|
||||
if (dst.port == 0) {
|
||||
puts("Error: no port or illegal port value provided");
|
||||
return 1;
|
||||
}
|
||||
data_len = hex2ints(byte_data, data);
|
||||
for (unsigned int i = 0; i < num; i++) {
|
||||
sock_udp_t *sock = NULL;
|
||||
@ -128,8 +118,8 @@ static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num
|
||||
puts("could not send");
|
||||
}
|
||||
else {
|
||||
printf("Success: send %u byte over UDP to [%s]:%" PRIu16 "\n",
|
||||
(unsigned)data_len, addr_str, dst.port);
|
||||
printf("Success: send %u byte over UDP to %s\n",
|
||||
(unsigned)data_len, addr_str);
|
||||
}
|
||||
xtimer_usleep(delay);
|
||||
}
|
||||
@ -156,18 +146,18 @@ int udp_cmd(int argc, char **argv)
|
||||
if (strcmp(argv[1], "send") == 0) {
|
||||
uint32_t num = 1;
|
||||
uint32_t delay = 1000000;
|
||||
if (argc < 5) {
|
||||
printf("usage: %s send <addr> <port> <hex data> [<num> [<delay in us>]]\n",
|
||||
if (argc < 4) {
|
||||
printf("usage: %s send <addr>:<port> <hex data> [<num> [<delay in us>]]\n",
|
||||
argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc > 4) {
|
||||
num = atoi(argv[4]);
|
||||
}
|
||||
if (argc > 5) {
|
||||
num = atoi(argv[5]);
|
||||
delay = atoi(argv[5]);
|
||||
}
|
||||
if (argc > 6) {
|
||||
delay = atoi(argv[6]);
|
||||
}
|
||||
return udp_send(argv[2], argv[3], argv[4], num, delay);
|
||||
return udp_send(argv[2], argv[3], num, delay);
|
||||
}
|
||||
else if (strcmp(argv[1], "server") == 0) {
|
||||
if (argc < 3) {
|
||||
|
Loading…
Reference in New Issue
Block a user