From 08964079e0b1b859e416268fecb998742c257ebc Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Tue, 21 Nov 2017 10:39:58 +0100 Subject: [PATCH] examples: tests: adapt udp shell commands for new forwarding engine The NIB's forwarding engine behaves more correctly than GNRC's old forwarding engine in that link-local addresses require also an interface to be provided to be meaningful. As with other commands this is simplified for the special case that there only is one interface that that interface is chosen. --- examples/dtls-echo/dtls-client.c | 16 ++++++++++++++++ examples/dtls-echo/dtls-server.c | 17 ++++++++++++++++- examples/gnrc_networking/udp.c | 19 ++++++++++++++++++- examples/gnrc_networking_mac/udp.c | 16 ++++++++++++++++ tests/gnrc_udp/udp.c | 16 ++++++++++++++++ 5 files changed, 82 insertions(+), 2 deletions(-) diff --git a/examples/dtls-echo/dtls-client.c b/examples/dtls-echo/dtls-client.c index 63a8d76cc6..669e6b95c3 100644 --- a/examples/dtls-echo/dtls-client.c +++ b/examples/dtls-echo/dtls-client.c @@ -25,9 +25,12 @@ #include "net/gnrc.h" #include "net/gnrc/ipv6.h" +#include "net/gnrc/netif.h" +#include "net/gnrc/netif/hdr.h" #include "net/gnrc/udp.h" #include "net/gnrc/pktdump.h" #include "timex.h" +#include "utlist.h" #include "xtimer.h" #define ENABLE_DEBUG (0) @@ -228,9 +231,15 @@ static int peer_verify_ecdsa_key(struct dtls_context_t *ctx, */ static int gnrc_sending(char *addr_str, char *data, size_t data_len ) { + int iface; ipv6_addr_t addr; gnrc_pktsnip_t *payload, *udp, *ip; + /* get interface, if available */ + iface = ipv6_addr_split_iface(addr_str); + if ((iface < 0) && (gnrc_netif_numof() == 1)) { + iface = gnrc_netif_iter(NULL)->pid; + } /* parse destination address */ if (ipv6_addr_from_str(&addr, addr_str) == NULL) { puts("Error: unable to parse destination address"); @@ -260,6 +269,13 @@ static int gnrc_sending(char *addr_str, char *data, size_t data_len ) gnrc_pktbuf_release(udp); return -1; } + /* add netif header, if interface was given */ + if (iface > 0) { + gnrc_pktsnip_t *netif = gnrc_netif_hdr_build(NULL, 0, NULL, 0); + + ((gnrc_netif_hdr_t *)netif->data)->if_pid = (kernel_pid_t)iface; + LL_PREPEND(ip, netif); + } /* * WARNING: Too fast and the nodes dies in middle of retransmissions. diff --git a/examples/dtls-echo/dtls-server.c b/examples/dtls-echo/dtls-server.c index 0bbd58d8ab..083764b211 100644 --- a/examples/dtls-echo/dtls-server.c +++ b/examples/dtls-echo/dtls-server.c @@ -26,8 +26,11 @@ #include "net/gnrc.h" #include "net/gnrc/ipv6.h" +#include "net/gnrc/netif.h" +#include "net/gnrc/netif/hdr.h" #include "net/gnrc/udp.h" #include "timex.h" +#include "utlist.h" #include "xtimer.h" #include "msg.h" @@ -142,10 +145,15 @@ static int read_from_peer(struct dtls_context_t *ctx, */ static int gnrc_sending(char *addr_str, char *data, size_t data_len, unsigned short rem_port ) { - + int iface; ipv6_addr_t addr; gnrc_pktsnip_t *payload, *udp, *ip; + /* get interface, if available */ + iface = ipv6_addr_split_iface(addr_str); + if ((iface < 0) && (gnrc_netif_numof() == 1)) { + iface = gnrc_netif_iter(NULL)->pid; + } /* parse destination address */ if (ipv6_addr_from_str(&addr, addr_str) == NULL) { puts("Error: unable to parse destination address"); @@ -174,6 +182,13 @@ static int gnrc_sending(char *addr_str, char *data, size_t data_len, unsigned sh gnrc_pktbuf_release(udp); return -1; } + /* add netif header, if interface was given */ + if (iface > 0) { + gnrc_pktsnip_t *netif = gnrc_netif_hdr_build(NULL, 0, NULL, 0); + + ((gnrc_netif_hdr_t *)netif->data)->if_pid = (kernel_pid_t)iface; + LL_PREPEND(ip, netif); + } /* send packet */ DEBUG("DBG-Server: Sending record to peer\n"); diff --git a/examples/gnrc_networking/udp.c b/examples/gnrc_networking/udp.c index fcfb54e870..ada54e569e 100644 --- a/examples/gnrc_networking/udp.c +++ b/examples/gnrc_networking/udp.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Freie Universität Berlin + * Copyright (C) 2015-17 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 @@ -14,6 +14,7 @@ * @brief Demonstrating the sending and receiving of UDP data * * @author Hauke Petersen + * @author Martine Lenders * * @} */ @@ -23,9 +24,12 @@ #include "net/gnrc.h" #include "net/gnrc/ipv6.h" +#include "net/gnrc/netif.h" +#include "net/gnrc/netif/hdr.h" #include "net/gnrc/udp.h" #include "net/gnrc/pktdump.h" #include "timex.h" +#include "utlist.h" #include "xtimer.h" static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, @@ -35,9 +39,15 @@ static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX static void send(char *addr_str, char *port_str, char *data, unsigned int num, unsigned int delay) { + int iface; uint16_t port; ipv6_addr_t addr; + /* get interface, if available */ + iface = ipv6_addr_split_iface(addr_str); + if ((iface < 0) && (gnrc_netif_numof() == 1)) { + iface = gnrc_netif_iter(NULL)->pid; + } /* parse destination address */ if (ipv6_addr_from_str(&addr, addr_str) == NULL) { puts("Error: unable to parse destination address"); @@ -75,6 +85,13 @@ static void send(char *addr_str, char *port_str, char *data, unsigned int num, gnrc_pktbuf_release(udp); return; } + /* add netif header, if interface was given */ + if (iface > 0) { + gnrc_pktsnip_t *netif = gnrc_netif_hdr_build(NULL, 0, NULL, 0); + + ((gnrc_netif_hdr_t *)netif->data)->if_pid = (kernel_pid_t)iface; + LL_PREPEND(ip, netif); + } /* send packet */ if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) { puts("Error: unable to locate UDP thread"); diff --git a/examples/gnrc_networking_mac/udp.c b/examples/gnrc_networking_mac/udp.c index b3feccf973..cb6d16835d 100644 --- a/examples/gnrc_networking_mac/udp.c +++ b/examples/gnrc_networking_mac/udp.c @@ -23,9 +23,12 @@ #include "net/gnrc.h" #include "net/gnrc/ipv6.h" +#include "net/gnrc/netif.h" +#include "net/gnrc/netif/hdr.h" #include "net/gnrc/udp.h" #include "net/gnrc/pktdump.h" #include "timex.h" +#include "utlist.h" #include "xtimer.h" static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, @@ -35,9 +38,15 @@ static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX static void send(char *addr_str, char *port_str, char *data, unsigned int num, unsigned int delay) { + int iface; uint16_t port; ipv6_addr_t addr; + /* get interface, if available */ + iface = ipv6_addr_split_iface(addr_str); + if ((iface < 0) && (gnrc_netif_numof() == 1)) { + iface = gnrc_netif_iter(NULL)->pid; + } /* parse destination address */ if (ipv6_addr_from_str(&addr, addr_str) == NULL) { puts("Error: unable to parse destination address"); @@ -75,6 +84,13 @@ static void send(char *addr_str, char *port_str, char *data, unsigned int num, gnrc_pktbuf_release(udp); return; } + /* add netif header, if interface was given */ + if (iface > 0) { + gnrc_pktsnip_t *netif = gnrc_netif_hdr_build(NULL, 0, NULL, 0); + + ((gnrc_netif_hdr_t *)netif->data)->if_pid = (kernel_pid_t)iface; + LL_PREPEND(ip, netif); + } /* send packet */ if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) { puts("Error: unable to locate UDP thread"); diff --git a/tests/gnrc_udp/udp.c b/tests/gnrc_udp/udp.c index 097d031c4e..e6db56537e 100644 --- a/tests/gnrc_udp/udp.c +++ b/tests/gnrc_udp/udp.c @@ -22,9 +22,12 @@ #include "msg.h" #include "net/gnrc.h" #include "net/gnrc/ipv6.h" +#include "net/gnrc/netif.h" +#include "net/gnrc/netif/hdr.h" #include "net/gnrc/udp.h" #include "net/gnrc/pktdump.h" #include "timex.h" +#include "utlist.h" #include "xtimer.h" #define SERVER_MSG_QUEUE_SIZE (8U) @@ -78,10 +81,16 @@ static void *_eventloop(void *arg) static void send(char *addr_str, char *port_str, char *data_len_str, unsigned int num, unsigned int delay) { + int iface; uint16_t port; ipv6_addr_t addr; size_t data_len; + /* get interface, if available */ + iface = ipv6_addr_split_iface(addr_str); + if ((iface < 0) && (gnrc_netif_numof() == 1)) { + iface = gnrc_netif_iter(NULL)->pid; + } /* parse destination address */ if (ipv6_addr_from_str(&addr, addr_str) == NULL) { puts("Error: unable to parse destination address"); @@ -123,6 +132,13 @@ static void send(char *addr_str, char *port_str, char *data_len_str, unsigned in gnrc_pktbuf_release(udp); return; } + /* add netif header, if interface was given */ + if (iface > 0) { + gnrc_pktsnip_t *netif = gnrc_netif_hdr_build(NULL, 0, NULL, 0); + + ((gnrc_netif_hdr_t *)netif->data)->if_pid = (kernel_pid_t)iface; + LL_PREPEND(ip, netif); + } /* send packet */ if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) { puts("Error: unable to locate UDP thread");