2015-04-30 16:39:26 +02:00
|
|
|
/*
|
2017-11-21 10:39:58 +01:00
|
|
|
* Copyright (C) 2015-17 Freie Universität Berlin
|
2015-04-30 16:39:26 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-06-30 16:18:29 +02:00
|
|
|
* @ingroup sys_shell_commands
|
2015-04-30 16:39:26 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Demonstrating the sending and receiving of UDP data
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2017-11-21 10:39:58 +01:00
|
|
|
* @author Martine Lenders <m.lenders@fu-berlin.de>
|
2015-04-30 16:39:26 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-07-06 17:51:21 +02:00
|
|
|
#include <inttypes.h>
|
2015-04-30 16:39:26 +02:00
|
|
|
|
2015-08-10 02:41:08 +02:00
|
|
|
#include "net/gnrc.h"
|
2015-08-17 15:41:29 +02:00
|
|
|
#include "net/gnrc/ipv6.h"
|
2017-11-21 10:39:58 +01:00
|
|
|
#include "net/gnrc/netif.h"
|
|
|
|
#include "net/gnrc/netif/hdr.h"
|
2015-08-17 15:41:29 +02:00
|
|
|
#include "net/gnrc/pktdump.h"
|
2022-05-31 23:27:27 +02:00
|
|
|
#include "net/gnrc/udp.h"
|
2021-07-10 15:12:03 +02:00
|
|
|
#include "net/utils.h"
|
2022-05-31 23:27:27 +02:00
|
|
|
#include "shell.h"
|
2015-07-27 12:32:13 +02:00
|
|
|
#include "timex.h"
|
2017-11-21 10:39:58 +01:00
|
|
|
#include "utlist.h"
|
2022-03-15 22:21:53 +01:00
|
|
|
#if IS_USED(MODULE_ZTIMER_USEC) || IS_USED(MODULE_ZTIMER_MSEC)
|
2021-04-15 13:17:33 +02:00
|
|
|
#include "ztimer.h"
|
2022-03-15 22:21:53 +01:00
|
|
|
#endif
|
|
|
|
#if IS_USED(MODULE_XTIMER)
|
2015-11-10 12:42:45 +01:00
|
|
|
#include "xtimer.h"
|
2021-04-15 13:17:33 +02:00
|
|
|
#endif
|
2015-04-30 16:39:26 +02:00
|
|
|
|
2021-04-19 08:18:31 +02:00
|
|
|
static gnrc_netreg_entry_t server =
|
|
|
|
GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
|
|
|
|
KERNEL_PID_UNDEF);
|
2015-04-30 16:39:26 +02:00
|
|
|
|
2021-12-13 07:50:43 +01:00
|
|
|
static void _send(const char *addr_str, const char *port_str,
|
|
|
|
const char *data, size_t num, unsigned int delay)
|
2015-04-30 16:39:26 +02:00
|
|
|
{
|
2021-07-10 15:12:03 +02:00
|
|
|
netif_t *netif;
|
2016-03-20 23:07:39 +01:00
|
|
|
uint16_t port;
|
2015-08-10 00:26:36 +02:00
|
|
|
ipv6_addr_t addr;
|
2015-04-30 16:39:26 +02:00
|
|
|
|
|
|
|
/* parse destination address */
|
2021-07-10 15:12:03 +02:00
|
|
|
if (netutils_get_ipv6(&addr, &netif, addr_str) < 0) {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("Error: unable to parse destination address\n");
|
2015-04-30 16:39:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* parse port */
|
2017-05-13 12:39:48 +02:00
|
|
|
port = atoi(port_str);
|
2016-03-20 23:07:39 +01:00
|
|
|
if (port == 0) {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("Error: unable to parse destination port\n");
|
2015-04-30 16:39:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:53:21 +01:00
|
|
|
while (num--) {
|
2015-07-27 12:32:13 +02:00
|
|
|
gnrc_pktsnip_t *payload, *udp, *ip;
|
2016-06-06 19:15:08 +02:00
|
|
|
unsigned payload_size;
|
2015-07-27 12:32:13 +02:00
|
|
|
/* allocate payload */
|
|
|
|
payload = gnrc_pktbuf_add(NULL, data, strlen(data), GNRC_NETTYPE_UNDEF);
|
|
|
|
if (payload == NULL) {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("Error: unable to copy data to packet buffer\n");
|
2015-07-27 12:32:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-06 19:15:08 +02:00
|
|
|
/* store size for output */
|
|
|
|
payload_size = (unsigned)payload->size;
|
2015-07-27 12:32:13 +02:00
|
|
|
/* allocate UDP header, set source port := destination port */
|
2016-03-20 23:07:39 +01:00
|
|
|
udp = gnrc_udp_hdr_build(payload, port, port);
|
2015-07-27 12:32:13 +02:00
|
|
|
if (udp == NULL) {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("Error: unable to allocate UDP header\n");
|
2015-07-27 12:32:13 +02:00
|
|
|
gnrc_pktbuf_release(payload);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* allocate IPv6 header */
|
2016-03-21 16:24:40 +01:00
|
|
|
ip = gnrc_ipv6_hdr_build(udp, NULL, &addr);
|
2015-07-27 12:32:13 +02:00
|
|
|
if (ip == NULL) {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("Error: unable to allocate IPv6 header\n");
|
2015-07-27 12:32:13 +02:00
|
|
|
gnrc_pktbuf_release(udp);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 10:39:58 +01:00
|
|
|
/* add netif header, if interface was given */
|
2018-12-12 19:59:55 +01:00
|
|
|
if (netif != NULL) {
|
|
|
|
gnrc_pktsnip_t *netif_hdr = gnrc_netif_hdr_build(NULL, 0, NULL, 0);
|
2023-06-22 20:59:22 +02:00
|
|
|
if (netif_hdr == NULL) {
|
|
|
|
printf("Error: unable to allocate netif header\n");
|
|
|
|
gnrc_pktbuf_release(ip);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-10 15:12:03 +02:00
|
|
|
gnrc_netif_hdr_set_netif(netif_hdr->data,
|
|
|
|
container_of(netif, gnrc_netif_t, netif));
|
2020-10-12 15:51:30 +02:00
|
|
|
ip = gnrc_pkt_prepend(ip, netif_hdr);
|
2017-11-21 10:39:58 +01:00
|
|
|
}
|
2015-07-27 12:32:13 +02:00
|
|
|
/* send packet */
|
2021-04-19 08:18:31 +02:00
|
|
|
if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP,
|
|
|
|
GNRC_NETREG_DEMUX_CTX_ALL, ip)) {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("Error: unable to locate UDP thread\n");
|
2015-07-27 12:32:13 +02:00
|
|
|
gnrc_pktbuf_release(ip);
|
|
|
|
return;
|
|
|
|
}
|
2021-04-19 08:18:31 +02:00
|
|
|
/* access to `payload` was implicitly given up with the send operation
|
|
|
|
* above
|
2016-06-06 19:15:08 +02:00
|
|
|
* => use temporary variable for output */
|
2016-12-27 13:50:42 +01:00
|
|
|
printf("Success: sent %u byte(s) to [%s]:%u\n", payload_size, addr_str,
|
2016-06-06 19:15:08 +02:00
|
|
|
port);
|
2022-02-14 17:53:21 +01:00
|
|
|
if (num) {
|
2022-02-14 18:06:14 +01:00
|
|
|
#if IS_USED(MODULE_ZTIMER_USEC)
|
|
|
|
ztimer_sleep(ZTIMER_USEC, delay);
|
|
|
|
#elif IS_USED(MODULE_XTIMER)
|
2022-02-14 17:53:21 +01:00
|
|
|
xtimer_usleep(delay);
|
2022-02-14 18:06:14 +01:00
|
|
|
#elif IS_USED(MODULE_ZTIMER_MSEC)
|
|
|
|
ztimer_sleep(ZTIMER_MSEC, (delay + US_PER_MS - 1) / US_PER_MS);
|
2021-04-15 13:17:33 +02:00
|
|
|
#endif
|
2022-02-14 17:53:21 +01:00
|
|
|
}
|
2015-04-30 16:39:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 07:50:43 +01:00
|
|
|
static void _start_server(const char *port_str)
|
2015-04-30 16:39:26 +02:00
|
|
|
{
|
|
|
|
uint16_t port;
|
|
|
|
|
|
|
|
/* check if server is already running */
|
2016-08-11 15:13:50 +02:00
|
|
|
if (server.target.pid != KERNEL_PID_UNDEF) {
|
2015-04-30 16:39:26 +02:00
|
|
|
printf("Error: server already running on port %" PRIu32 "\n",
|
2015-07-27 12:32:13 +02:00
|
|
|
server.demux_ctx);
|
2015-04-30 16:39:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* parse port */
|
2017-05-13 12:39:48 +02:00
|
|
|
port = atoi(port_str);
|
2015-04-30 16:39:26 +02:00
|
|
|
if (port == 0) {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("Error: invalid port specified\n");
|
2015-04-30 16:39:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* start server (which means registering pktdump for the chosen port) */
|
2016-08-11 15:13:50 +02:00
|
|
|
server.target.pid = gnrc_pktdump_pid;
|
2015-04-30 16:39:26 +02:00
|
|
|
server.demux_ctx = (uint32_t)port;
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_netreg_register(GNRC_NETTYPE_UDP, &server);
|
2015-04-30 16:39:26 +02:00
|
|
|
printf("Success: started UDP server on port %" PRIu16 "\n", port);
|
|
|
|
}
|
|
|
|
|
2021-12-13 07:50:43 +01:00
|
|
|
static void _stop_server(void)
|
2015-04-30 16:39:26 +02:00
|
|
|
{
|
|
|
|
/* check if server is running at all */
|
2016-08-11 15:13:50 +02:00
|
|
|
if (server.target.pid == KERNEL_PID_UNDEF) {
|
2015-04-30 16:39:26 +02:00
|
|
|
printf("Error: server was not running\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* stop server */
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &server);
|
2016-08-11 15:13:50 +02:00
|
|
|
server.target.pid = KERNEL_PID_UNDEF;
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("Success: stopped UDP server\n");
|
2015-04-30 16:39:26 +02:00
|
|
|
}
|
|
|
|
|
2022-06-08 08:51:38 +02:00
|
|
|
static int _gnrc_udp_cmd(int argc, char **argv)
|
2015-04-30 16:39:26 +02:00
|
|
|
{
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: %s [send|server]\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "send") == 0) {
|
2015-07-27 12:32:13 +02:00
|
|
|
uint32_t num = 1;
|
2022-02-14 17:52:55 +01:00
|
|
|
uint32_t delay;
|
2015-04-30 16:39:26 +02:00
|
|
|
if (argc < 5) {
|
2021-04-19 08:18:31 +02:00
|
|
|
printf("usage: %s send "
|
|
|
|
"<addr> <port> <data> [<num> [<delay in us>]]\n", argv[0]);
|
2015-04-30 16:39:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-07-27 12:32:13 +02:00
|
|
|
if (argc > 5) {
|
2017-05-13 12:39:48 +02:00
|
|
|
num = atoi(argv[5]);
|
2015-07-27 12:32:13 +02:00
|
|
|
}
|
|
|
|
if (argc > 6) {
|
2017-05-13 12:39:48 +02:00
|
|
|
delay = atoi(argv[6]);
|
2022-02-14 17:52:55 +01:00
|
|
|
} else {
|
2022-02-14 18:06:14 +01:00
|
|
|
delay = US_PER_SEC;
|
2015-07-27 12:32:13 +02:00
|
|
|
}
|
2021-12-13 07:50:43 +01:00
|
|
|
_send(argv[2], argv[3], argv[4], num, delay);
|
2015-04-30 16:39:26 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2021-12-13 07:50:43 +01:00
|
|
|
_start_server(argv[3]);
|
2015-04-30 16:39:26 +02:00
|
|
|
}
|
|
|
|
else if (strcmp(argv[2], "stop") == 0) {
|
2021-12-13 07:50:43 +01:00
|
|
|
_stop_server();
|
2015-04-30 16:39:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("error: invalid command\n");
|
2015-04-30 16:39:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-02-27 15:21:04 +01:00
|
|
|
printf("error: invalid command\n");
|
2015-04-30 16:39:26 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2022-05-31 23:27:27 +02:00
|
|
|
|
|
|
|
SHELL_COMMAND(udp, "send data over UDP and listen on UDP ports", _gnrc_udp_cmd);
|