tests: port tests/lwip to sock

This commit is contained in:
Martine Lenders 2017-01-15 17:35:56 +01:00
parent 14ed6d2b90
commit d1c75b4162
7 changed files with 392 additions and 133 deletions

View File

@ -1,49 +1,30 @@
APPLICATION = lwip
# overwrite board, do not set native as default
BOARD ?= iotlab-m3
include ../Makefile.tests_common
BOARD_BLACKLIST := arduino-mega2560 msb-430h telosb waspmote-pro z1 arduino-uno \
arduino-duemilanove msb-430 wsn430-v1_4 wsn430-v1_3b
BOARD_INSUFFICIENT_MEMORY := airfy-beacon arduino-mega2560 msb-430h nrf6310 \
nucleo32-f031 nucleo32-f031 nucleo32-f042 nucleo32-f303 \
nucleo32-l031 nucleo-f030 nucleo-f072 \
nucleo-f302 nucleo-f334 nucleo-l053 pca10005 stm32f0discovery \
telosb weio yunjia-nrf51822 z1
# lwIP's memory management doesn't seem to work on non 32-bit platforms at the
# moment.
BOARD_BLACKLIST := arduino-uno arduino-duemilanove arduino-mega2560 chronos \
msb-430 msb-430h telosb waspmote-pro wsn430-v1_3b \
wsn430-v1_4 z1
BOARD_INSUFFICIENT_MEMORY := airfy-beacon nrf6310 nucleo32-f031 nucleo32-f042 \
nucleo32-l031 nucleo-f030 nucleo-f334 nucleo-l053 \
pca10005 stm32f0discovery weio yunjia-nrf51822
# including lwip_ipv6_mld would currently break this test on at86rf2xx radios
USEMODULE += lwip lwip_ipv6_autoconfig lwip_conn_ip lwip_netdev
USEMODULE += lwip_udp lwip_conn_udp
USEMODULE += lwip lwip_ipv6_autoconfig lwip_sock_ip lwip_netdev
USEMODULE += lwip_udp lwip_sock_udp
USEMODULE += lwip_tcp lwip_sock_tcp
USEMODULE += ipv6_addr
USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps
USEMODULE += od
USEMODULE += netdev_default
# use the at86rf231 as fallback device
DRIVER := at86rf231
# define the driver to be used for selected boards
ifneq (,$(filter samr21-xpro,$(BOARD)))
DRIVER := at86rf233
endif
ifneq (,$(filter iotlab-m3 fox,$(BOARD)))
DRIVER := at86rf231
endif
ifneq (,$(filter mulle,$(BOARD)))
DRIVER := at86rf212b
endif
ifneq (,$(filter native,$(BOARD)))
DRIVER := netdev_tap
ifeq ($(BOARD),native)
USEMODULE += lwip_ethernet
endif
ifneq (,$(filter at86rf2%,$(DRIVER)))
FEATURES_REQUIRED = periph_spi periph_gpio
endif
USEMODULE += $(DRIVER)
include $(RIOTBASE)/Makefile.include
test:

View File

@ -29,7 +29,7 @@ extern "C" {
* @brief Application configuration
* @{
*/
#define CONN_INBUF_SIZE (256)
#define SOCK_INBUF_SIZE (256)
#define SERVER_MSG_QUEUE_SIZE (8)
#define SERVER_BUFFER_SIZE (64)
/**
@ -47,7 +47,7 @@ extern "C" {
*/
size_t hex2ints(uint8_t *out, const char *in);
#ifdef MODULE_CONN_IP
#ifdef MODULE_SOCK_IP
/**
* @brief Raw IP shell command
*
@ -60,7 +60,20 @@ size_t hex2ints(uint8_t *out, const char *in);
int ip_cmd(int argc, char **argv);
#endif
#ifdef MODULE_CONN_UDP
#ifdef MODULE_SOCK_TCP
/**
* @brief TCP IP shell command
*
* @param[in] argc number of arguments
* @param[in] argv array of arguments
*
* @return 0 on success
* @return other on error
*/
int tcp_cmd(int argc, char **argv);
#endif
#ifdef MODULE_SOCK_UDP
/**
* @brief UDP IP shell command
*

View File

@ -7,14 +7,12 @@
*/
/**
* @ingroup examples
* @ingroup tests
* @{
*
* @file
* @brief Demonstrating the sending and receiving of UDP data over POSIX sockets.
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*
* @}
*/
@ -25,44 +23,50 @@
#include "common.h"
#include "od.h"
#include "net/af.h"
#include "net/conn/ip.h"
#include "net/sock/ip.h"
#include "net/ipv6.h"
#include "shell.h"
#include "thread.h"
#include "xtimer.h"
#ifdef MODULE_CONN_IP
static char conn_inbuf[CONN_INBUF_SIZE];
#ifdef MODULE_SOCK_IP
static char sock_inbuf[SOCK_INBUF_SIZE];
static bool server_running;
static conn_ip_t server_conn;
static sock_ip_t server_sock;
static char server_stack[THREAD_STACKSIZE_DEFAULT];
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
static void *_server_thread(void *args)
{
ipv6_addr_t server_addr = IPV6_ADDR_UNSPECIFIED;
sock_ip_ep_t server_addr = SOCK_IPV6_EP_ANY;
uint8_t protocol;
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
/* parse protocol */
protocol = (uint8_t)atoi((char *)args);
if (conn_ip_create(&server_conn, &server_addr, sizeof(server_addr), AF_INET6, protocol) < 0) {
protocol = atoi(args);
if (sock_ip_create(&server_sock, &server_addr, NULL, protocol, 0) < 0) {
return NULL;
}
server_running = true;
printf("Success: started IP server on protocol %u\n", protocol);
while (1) {
int res;
ipv6_addr_t src;
size_t src_len = sizeof(ipv6_addr_t);
if ((res = conn_ip_recvfrom(&server_conn, conn_inbuf, sizeof(conn_inbuf), &src,
&src_len)) < 0) {
sock_ip_ep_t src;
if ((res = sock_ip_recv(&server_sock, sock_inbuf, sizeof(sock_inbuf),
SOCK_NO_TIMEOUT, &src)) < 0) {
puts("Error on receive");
}
else if (res == 0) {
puts("No data received");
}
else {
od_hex_dump(conn_inbuf, res, 0);
char addrstr[IPV6_ADDR_MAX_STR_LEN];
printf("Received IP data from [%s]:\n",
ipv6_addr_to_str(addrstr, (ipv6_addr_t *)&src.addr.ipv6,
sizeof(addrstr)));
od_hex_dump(sock_inbuf, res, 0);
}
}
return NULL;
@ -71,26 +75,30 @@ static void *_server_thread(void *args)
static int ip_send(char *addr_str, char *port_str, char *data, unsigned int num,
unsigned int delay)
{
ipv6_addr_t src = IPV6_ADDR_UNSPECIFIED, dst;
sock_ip_ep_t dst = SOCK_IPV6_EP_ANY;
uint8_t protocol;
uint8_t byte_data[strlen(data) / 2];
uint8_t byte_data[SHELL_DEFAULT_BUFSIZE / 2];
size_t data_len;
/* parse destination address */
if (ipv6_addr_from_str(&dst, addr_str) == NULL) {
if (ipv6_addr_from_str((ipv6_addr_t *)&dst.addr.ipv6, addr_str) == NULL) {
puts("Error: unable to parse destination address");
return 1;
}
/* parse protocol */
protocol = (uint8_t)atoi(port_str);
protocol = atoi(port_str);
data_len = hex2ints(byte_data, data);
for (unsigned int i = 0; i < num; i++) {
if (conn_ip_sendto(byte_data, data_len, &src, sizeof(src), (struct sockaddr *)&dst,
sizeof(dst), AF_INET6, protocol) < 0) {
sock_ip_t *sock = NULL;
if (server_running) {
sock = &server_sock;
}
if (sock_ip_send(sock, byte_data, data_len, protocol, &dst) < 0) {
puts("could not send");
}
else {
printf("Success: send %u byte to %s (next header: %u)\n",
printf("Success: send %u byte over IPv6 to %s (next header: %u)\n",
(unsigned)data_len, addr_str, protocol);
}
xtimer_usleep(delay);
@ -124,10 +132,10 @@ int ip_cmd(int argc, char **argv)
return 1;
}
if (argc > 5) {
num = (uint32_t)atoi(argv[5]);
num = atoi(argv[5]);
}
if (argc > 6) {
delay = (uint32_t)atoi(argv[6]);
delay = atoi(argv[6]);
}
return ip_send(argv[2], argv[3], argv[4], num, delay);
}

View File

@ -11,14 +11,11 @@
* @{
*
* @file
* @brief Test for raw IPv6 connections
* @brief Test for lwIP
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This test application tests the gnrc_conn_ip module. If you select protocol 58 you can also
* test if gnrc is able to deal with multiple subscribers to ICMPv6 (gnrc_icmpv6 and this
* application).
*
* This test application tests the lwIP package.
* @}
*/
@ -52,10 +49,13 @@ static int ifconfig(int argc, char **argv)
}
static const shell_command_t shell_commands[] = {
#ifdef MODULE_CONN_IP
#ifdef MODULE_SOCK_IP
{ "ip", "Send IP packets and listen for packets of certain type", ip_cmd },
#endif
#ifdef MODULE_CONN_UDP
#ifdef MODULE_SOCK_TCP
{ "tcp", "Send TCP messages and listen for messages on TCP port", tcp_cmd },
#endif
#ifdef MODULE_SOCK_UDP
{ "udp", "Send UDP messages and listen for messages on UDP port", udp_cmd },
#endif
{ "ifconfig", "Shows assigned IPv6 addresses", ifconfig },
@ -63,8 +63,6 @@ static const shell_command_t shell_commands[] = {
};
static char line_buf[SHELL_DEFAULT_BUFSIZE];
char conn_inbuf[CONN_INBUF_SIZE];
int main(void)
{
puts("RIOT lwip test application");

224
tests/lwip/tcp.c Normal file
View File

@ -0,0 +1,224 @@
/*
* Copyright (C) 2017 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup tests
* @{
*
* @file
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
* @}
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "common.h"
#include "od.h"
#include "net/af.h"
#include "net/sock/tcp.h"
#include "net/ipv6.h"
#include "shell.h"
#include "thread.h"
#include "xtimer.h"
#ifdef MODULE_SOCK_TCP
static char sock_inbuf[SOCK_INBUF_SIZE];
static bool server_running = false, client_running = false;
static sock_tcp_t server_sock, client_sock;
static sock_tcp_queue_t server_queue;
static char server_stack[THREAD_STACKSIZE_DEFAULT];
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
static void *_server_thread(void *args)
{
sock_tcp_ep_t server_addr = SOCK_IPV6_EP_ANY;
int res;
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
/* parse port */
server_addr.port = atoi(args);
if ((res = sock_tcp_listen(&server_queue, &server_addr, &server_sock, 1,
0)) < 0) {
printf("Unable to open TCP server on port %" PRIu16 " (error code %d)\n",
server_addr.port, -res);
return NULL;
}
server_running = true;
printf("Success: started TCP server on port %" PRIu16 "\n",
server_addr.port);
while (1) {
char client_addr[IPV6_ADDR_MAX_STR_LEN];
sock_tcp_t *sock = NULL;
int res;
unsigned client_port;
if ((res = sock_tcp_accept(&server_queue, &sock, SOCK_NO_TIMEOUT)) < 0) {
puts("Error on TCP accept");
continue;
}
else {
sock_tcp_ep_t client;
sock_tcp_get_remote(sock, &client);
ipv6_addr_to_str(client_addr, (ipv6_addr_t *)&client.addr.ipv6,
sizeof(client_addr));
client_port = client.port;
printf("TCP client [%s]:%u connected\n",
client_addr, client_port);
}
/* we don't use timeouts so all errors should be related to a lost
* connection */
while ((res = sock_tcp_read(sock, sock_inbuf, sizeof(sock_inbuf),
SOCK_NO_TIMEOUT)) >= 0) {
printf("Received TCP data from client [%s]:%u:\n",
client_addr, client_port);
if (res > 0) {
od_hex_dump(sock_inbuf, res, 0);
}
else {
puts("(nul)");
}
}
printf("TCP connection to [%s]:%u reset, starting to accept again\n",
client_addr, client_port);
sock_tcp_disconnect(sock);
}
return NULL;
}
static int tcp_connect(char *addr_str, char *port_str, char *local_port_str)
{
sock_tcp_ep_t dst = SOCK_IPV6_EP_ANY;
uint16_t local_port = 0;
if (client_running) {
puts("Cient already connected");
}
/* parse destination address */
if (ipv6_addr_from_str((ipv6_addr_t *)&dst.addr.ipv6, addr_str) == NULL) {
puts("Error: unable to parse destination address");
return 1;
}
/* parse port */
dst.port = atoi(port_str);
if (local_port_str != NULL) {
local_port = atoi(port_str);
}
if (sock_tcp_connect(&client_sock, &dst, local_port, 0) < 0) {
puts("Error: unable to connect");
return 1;
}
client_running = true;
return 0;
}
static int tcp_disconnect(void)
{
sock_tcp_disconnect(&client_sock);
client_running = false;
return 0;
}
static int tcp_send(char *data, unsigned int num, unsigned int delay)
{
uint8_t byte_data[SHELL_DEFAULT_BUFSIZE / 2];
size_t data_len;
data_len = hex2ints(byte_data, data);
for (unsigned int i = 0; i < num; i++) {
if (sock_tcp_write(&client_sock, byte_data, data_len) < 0) {
puts("could not send");
}
else {
printf("Success: send %u byte over TCP to server\n", (unsigned)data_len);
}
xtimer_usleep(delay);
}
return 0;
}
static int tcp_start_server(char *port_str)
{
if (thread_create(server_stack, sizeof(server_stack), THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_STACKTEST, _server_thread, port_str,
"TCP server") <= KERNEL_PID_UNDEF) {
return 1;
}
return 0;
}
int tcp_cmd(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s [connect|disconnect|send|server]\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "connect") == 0) {
char *local_port = NULL;
if (argc < 4) {
printf("usage: %s connect <addr> <port> [local_port]\n",
argv[0]);
return 1;
}
if (argc > 4) {
local_port = argv[4];
}
return tcp_connect(argv[2], argv[3], local_port);
}
if (strcmp(argv[1], "disconnect") == 0) {
return tcp_disconnect();
}
else if (strcmp(argv[1], "send") == 0) {
uint32_t num = 1;
uint32_t delay = 1000000UL;
if (argc < 3) {
printf("usage: %s send <hex data> [<num> [<delay in us>]]\n",
argv[0]);
return 1;
}
if (argc > 3) {
num = atoi(argv[3]);
}
if (argc > 4) {
delay = atoi(argv[4]);
}
return tcp_send(argv[2], num, delay);
}
else if (strcmp(argv[1], "server") == 0) {
if (argc < 3) {
printf("usage: %s server [start|stop]\n", argv[0]);
return 1;
}
if (strcmp(argv[2], "start") == 0) {
if (argc < 4) {
printf("usage %s server start <port>\n", argv[0]);
return 1;
}
return tcp_start_server(argv[3]);
}
else {
puts("error: invalid command");
return 1;
}
}
else {
puts("error: invalid command");
return 1;
}
}
#else
typedef int dont_be_pedantic;
#endif
/** @} */

View File

@ -154,8 +154,8 @@ def default_test_case(board_group, application, env=None):
if env != None:
env.update(env)
env.update(board.to_env())
with pexpect.spawn("make", ["-C", application, "term"], env=env,
timeout=DEFAULT_TIMEOUT,
with pexpect.spawnu("make", ["-C", application, "term"], env=env,
timeout=DEFAULT_TIMEOUT,
logfile=sys.stdout) as spawn:
spawn.expect("TEST: SUCCESS")
@ -173,7 +173,7 @@ class TestStrategy(ApplicationStrategy):
def get_ipv6_address(spawn):
spawn.sendline(u"ifconfig")
spawn.expect(u"[A-Za-z0-9]{2}[0-9]+: inet6 (fe80::[0-9a-f:]+)")
spawn.expect(u"[A-Za-z0-9]{2}_[0-9]+: inet6 (fe80::[0-9a-f:]+)")
return spawn.match.group(1)
def test_ipv6_send(board_group, application, env=None):
@ -185,22 +185,19 @@ def test_ipv6_send(board_group, application, env=None):
if env != None:
env_receiver.update(env)
env_receiver.update(board_group.boards[1].to_env())
with pexpect.spawn("make", ["-C", application, "term"], env=env_sender,
timeout=DEFAULT_TIMEOUT) as sender, \
pexpect.spawn("make", ["-C", application, "term"], env=env_receiver,
timeout=DEFAULT_TIMEOUT) as receiver:
with pexpect.spawnu("make", ["-C", application, "term"], env=env_sender,
timeout=DEFAULT_TIMEOUT) as sender, \
pexpect.spawnu("make", ["-C", application, "term"], env=env_receiver,
timeout=DEFAULT_TIMEOUT) as receiver:
ipprot = random.randint(0x00, 0xff)
receiver_ip = get_ipv6_address(receiver)
receiver.sendline(u"ip server start %d" % ipprot)
# wait for neighbor discovery to be done
time.sleep(5)
sender.sendline(u"ip send %s %d 01:23:45:67:89:ab:cd:ef" % (receiver_ip, ipprot))
sender.expect_exact(u"Success: send 8 byte to %s (next header: %d)" %
sender.expect_exact(u"Success: send 8 byte over IPv6 to %s (next header: %d)" %
(receiver_ip, ipprot))
receiver.expect(u"000000 60 00 00 00 00 08 %s ff fe 80 00 00 00 00 00 00" % hex(ipprot)[2:])
receiver.expect(u"000010( [0-9a-f]{2}){8} fe 80 00 00 00 00 00 00")
receiver.expect(u"000020( [0-9a-f]{2}){8} 01 23 45 67 89 ab cd ef")
receiver.expect(u"000000 01 23 45 67 89 ab cd ef")
def test_udpv6_send(board_group, application, env=None):
env_sender = os.environ.copy()
@ -211,10 +208,10 @@ def test_udpv6_send(board_group, application, env=None):
if env != None:
env_receiver.update(env)
env_receiver.update(board_group.boards[1].to_env())
with pexpect.spawn("make", ["-C", application, "term"], env=env_sender,
timeout=DEFAULT_TIMEOUT) as sender, \
pexpect.spawn("make", ["-C", application, "term"], env=env_receiver,
timeout=DEFAULT_TIMEOUT) as receiver:
with pexpect.spawnu("make", ["-C", application, "term"], env=env_sender,
timeout=DEFAULT_TIMEOUT) as sender, \
pexpect.spawnu("make", ["-C", application, "term"], env=env_receiver,
timeout=DEFAULT_TIMEOUT) as receiver:
port = random.randint(0x0000, 0xffff)
receiver_ip = get_ipv6_address(receiver)
@ -222,11 +219,40 @@ def test_udpv6_send(board_group, application, env=None):
# wait for neighbor discovery to be done
time.sleep(5)
sender.sendline(u"udp send %s %d ab:cd:ef" % (receiver_ip, port))
sender.expect_exact(u"Success: send 3 byte to [%s]:%d" %
sender.expect_exact(u"Success: send 3 byte over UDP to [%s]:%d" %
(receiver_ip, port))
receiver.expect(u"000000 ab cd ef")
def test_dual_send(board_group, application, env=None):
def test_tcpv6_send(board_group, application, env=None):
env_client = os.environ.copy()
if env != None:
env_client.update(env)
env_client.update(board_group.boards[0].to_env())
env_server = os.environ.copy()
if env != None:
env_server.update(env)
env_server.update(board_group.boards[1].to_env())
with pexpect.spawnu("make", ["-C", application, "term"], env=env_client,
timeout=DEFAULT_TIMEOUT) as client, \
pexpect.spawnu("make", ["-C", application, "term"], env=env_server,
timeout=DEFAULT_TIMEOUT) as server:
port = random.randint(0x0000, 0xffff)
server_ip = get_ipv6_address(server)
client_ip = get_ipv6_address(client)
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))
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")
server.expect(u"000000 af fe ab e0")
client.sendline(u"tcp disconnect")
client.sendline(u"tcp send affe:abe")
client.expect_exact(u"could not send")
def test_triple_send(board_group, application, env=None):
env_sender = os.environ.copy()
if env != None:
env_sender.update(env)
@ -235,32 +261,39 @@ def test_dual_send(board_group, application, env=None):
if env != None:
env_receiver.update(env)
env_receiver.update(board_group.boards[1].to_env())
with pexpect.spawn("make", ["-C", application, "term"], env=env_sender,
timeout=DEFAULT_TIMEOUT) as sender, \
pexpect.spawn("make", ["-C", application, "term"], env=env_receiver,
timeout=DEFAULT_TIMEOUT) as receiver:
port = random.randint(0x0000, 0xffff)
with pexpect.spawnu("make", ["-C", application, "term"], env=env_sender,
timeout=DEFAULT_TIMEOUT) as sender, \
pexpect.spawnu("make", ["-C", application, "term"], env=env_receiver,
timeout=DEFAULT_TIMEOUT) as receiver:
udp_port = random.randint(0x0000, 0xffff)
tcp_port = random.randint(0x0000, 0xffff)
ipprot = random.randint(0x00, 0xff)
receiver_ip = get_ipv6_address(receiver)
sender_ip = get_ipv6_address(sender)
receiver.sendline(u"ip server start %d" % ipprot)
receiver.sendline(u"udp server start %d" % port)
receiver.sendline(u"udp server start %d" % udp_port)
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, port))
sender.expect_exact(u"Success: send 2 byte to [%s]:%d" %
(receiver_ip, 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"000000 01 23")
sender.sendline(u"ip send %s %d 01:02:03:04" % (receiver_ip, ipprot))
sender.expect_exact(u"Success: send 4 byte to %s (next header: %d)" %
sender.expect_exact(u"Success: send 4 byte over IPv6 to %s (next header: %d)" %
(receiver_ip, ipprot))
receiver.expect(u"000000 60 00 00 00 00 04 %s ff fe 80 00 00 00 00 00 00" % hex(ipprot)[2:])
receiver.expect(u"000010( [0-9a-f]{2}){8} fe 80 00 00 00 00 00 00")
receiver.expect(u"000020( [0-9a-f]{2}){8} 01 02 03 04")
receiver.expect(u"000000 01 02 03 04")
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")
receiver.expect(u"000000 de ad be ef")
if __name__ == "__main__":
del os.environ['TERMFLAGS']
TestStrategy().execute([BoardGroup((Board("native", "tap0"), \
Board("native", "tap1")))], \
[test_ipv6_send, test_udpv6_send, test_dual_send])
[test_ipv6_send, test_udpv6_send, test_tcpv6_send,
test_triple_send])

View File

@ -7,14 +7,12 @@
*/
/**
* @ingroup examples
* @ingroup tests
* @{
*
* @file
* @brief Demonstrating the sending and receiving of UDP data over POSIX sockets.
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*
* @}
*/
@ -25,42 +23,41 @@
#include "common.h"
#include "od.h"
#include "net/af.h"
#include "net/conn/udp.h"
#include "net/sock/udp.h"
#include "net/ipv6.h"
#include "shell.h"
#include "thread.h"
#include "xtimer.h"
#ifdef MODULE_CONN_UDP
static char conn_inbuf[CONN_INBUF_SIZE];
#ifdef MODULE_SOCK_UDP
static char sock_inbuf[SOCK_INBUF_SIZE];
static bool server_running;
static conn_udp_t server_conn;
static sock_udp_t server_sock;
static char server_stack[THREAD_STACKSIZE_DEFAULT];
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
static void *_server_thread(void *args)
{
ipv6_addr_t server_addr = IPV6_ADDR_UNSPECIFIED;
uint16_t port;
sock_udp_ep_t server_addr = SOCK_IPV6_EP_ANY;
int res;
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
/* parse port */
port = (uint16_t)atoi((char *)args);
if ((res = conn_udp_create(&server_conn, &server_addr,
sizeof(server_addr), AF_INET6, port)) < 0) {
server_addr.port = atoi(args);
if ((res = sock_udp_create(&server_sock, &server_addr, NULL, 0)) < 0) {
printf("Unable to open UDP server on port %" PRIu16 " (error code %d)\n",
port, -res);
server_addr.port, -res);
return NULL;
}
server_running = true;
printf("Success: started UDP server on port %" PRIu16 "\n", port);
printf("Success: started UDP server on port %" PRIu16 "\n",
server_addr.port);
while (1) {
sock_udp_ep_t src;
int res;
ipv6_addr_t src;
size_t src_len = sizeof(ipv6_addr_t);
uint16_t sport;
if ((res = conn_udp_recvfrom(&server_conn, conn_inbuf, sizeof(conn_inbuf), &src,
&src_len, &sport)) < 0) {
if ((res = sock_udp_recv(&server_sock, sock_inbuf, sizeof(sock_inbuf),
SOCK_NO_TIMEOUT, &src)) < 0) {
puts("Error on receive");
}
else if (res == 0) {
@ -68,9 +65,11 @@ static void *_server_thread(void *args)
}
else {
char addrstr[IPV6_ADDR_MAX_STR_LEN];
printf("Received from [%s]:%" PRIu16 ":\n", ipv6_addr_to_str(addrstr, &src,
sizeof(addrstr)), sport);
od_hex_dump(conn_inbuf, res, 0);
printf("Received UDP data from [%s]:%" PRIu16 ":\n",
ipv6_addr_to_str(addrstr, (ipv6_addr_t *)&src.addr.ipv6,
sizeof(addrstr)), src.port);
od_hex_dump(sock_inbuf, res, 0);
}
}
return NULL;
@ -79,27 +78,30 @@ static void *_server_thread(void *args)
static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num,
unsigned int delay)
{
ipv6_addr_t src = IPV6_ADDR_UNSPECIFIED, dst;
uint16_t port;
uint8_t byte_data[strlen(data) / 2];
sock_udp_ep_t dst = SOCK_IPV6_EP_ANY;
uint8_t byte_data[SHELL_DEFAULT_BUFSIZE / 2];
size_t data_len;
/* parse destination address */
if (ipv6_addr_from_str(&dst, addr_str) == NULL) {
if (ipv6_addr_from_str((ipv6_addr_t *)&dst.addr.ipv6, addr_str) == NULL) {
puts("Error: unable to parse destination address");
return 1;
}
/* parse port */
port = (uint16_t)atoi(port_str);
dst.port = atoi(port_str);
data_len = hex2ints(byte_data, data);
for (unsigned int i = 0; i < num; i++) {
if (conn_udp_sendto(byte_data, data_len, &src, sizeof(src), (struct sockaddr *)&dst,
sizeof(dst), AF_INET6, port, port) < 0) {
sock_udp_t *sock = NULL;
if (server_running) {
sock = &server_sock;
}
if (sock_udp_send(sock, byte_data, data_len, &dst) < 0) {
puts("could not send");
}
else {
printf("Success: send %u byte to [%s]:%" PRIu16 ")\n",
(unsigned)data_len, addr_str, port);
printf("Success: send %u byte over UDP to [%s]:%" PRIu16 "\n",
(unsigned)data_len, addr_str, dst.port);
}
xtimer_usleep(delay);
}
@ -132,10 +134,10 @@ int udp_cmd(int argc, char **argv)
return 1;
}
if (argc > 5) {
num = (uint32_t)atoi(argv[5]);
num = atoi(argv[5]);
}
if (argc > 6) {
delay = (uint32_t)atoi(argv[6]);
delay = atoi(argv[6]);
}
return udp_send(argv[2], argv[3], argv[4], num, delay);
}