1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

tests/lwip: adapt for IPv4/6 dual-stack usage

This commit is contained in:
Martine S. Lenders 2020-06-24 17:58:50 +02:00
parent e80c473830
commit efbac9f1f7
No known key found for this signature in database
GPG Key ID: CCD317364F63286F
8 changed files with 137 additions and 114 deletions

View File

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

View File

@ -1,6 +1,7 @@
BOARD_INSUFFICIENT_MEMORY := \
airfy-beacon \
blackpill \
bluepill \
hifive1 \
hifive1b \
i-nucleo-lrwan1 \

View File

@ -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
* @{

View File

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

View File

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

View File

@ -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();

View File

@ -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")

View File

@ -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) {