|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Freie Universität Berlin |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @ingroup sys_shell_commands |
| 11 | + * @{ |
| 12 | + * |
| 13 | + * @file |
| 14 | + * @brief Shell command for sending raw text on network |
| 15 | + * |
| 16 | + * @author Martine Lenders <m.lenders@fu-berlin.de> |
| 17 | + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> |
| 18 | + * @author Oliver Hahm <oliver.hahm@inria.fr> |
| 19 | + */ |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include <string.h> |
| 23 | + |
| 24 | +#include "net/gnrc.h" |
| 25 | +#include "net/gnrc.h" |
| 26 | +#include "net/gnrc/netif/hdr.h" |
| 27 | +#include "net/ipv6/addr.h" |
| 28 | +#include "shell.h" |
| 29 | +#include "container.h" |
| 30 | + |
| 31 | +static int _gnrc_netif_send(int argc, char **argv) |
| 32 | +{ |
| 33 | + netif_t *iface; |
| 34 | + uint8_t addr[GNRC_NETIF_L2ADDR_MAXLEN]; |
| 35 | + size_t addr_len; |
| 36 | + gnrc_pktsnip_t *pkt, *hdr; |
| 37 | + gnrc_netif_hdr_t *nethdr; |
| 38 | + uint8_t flags = 0x00; |
| 39 | + |
| 40 | + if (argc < 4) { |
| 41 | + printf("usage: %s <if> [<L2 addr>|bcast] <data>\n", argv[0]); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + |
| 45 | + iface = netif_get_by_name(argv[1]); |
| 46 | + if (!iface) { |
| 47 | + printf("error: invalid interface given\n"); |
| 48 | + return 1; |
| 49 | + } |
| 50 | + |
| 51 | + /* parse address */ |
| 52 | + addr_len = gnrc_netif_addr_from_str(argv[2], addr); |
| 53 | + |
| 54 | + if (addr_len == 0) { |
| 55 | + if (strcmp(argv[2], "bcast") == 0) { |
| 56 | + flags |= GNRC_NETIF_HDR_FLAGS_BROADCAST; |
| 57 | + } |
| 58 | + else { |
| 59 | + printf("error: invalid address given\n"); |
| 60 | + return 1; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /* put packet together */ |
| 65 | + pkt = gnrc_pktbuf_add(NULL, argv[3], strlen(argv[3]), GNRC_NETTYPE_UNDEF); |
| 66 | + if (pkt == NULL) { |
| 67 | + printf("error: packet buffer full\n"); |
| 68 | + return 1; |
| 69 | + } |
| 70 | + hdr = gnrc_netif_hdr_build(NULL, 0, addr, addr_len); |
| 71 | + if (hdr == NULL) { |
| 72 | + printf("error: packet buffer full\n"); |
| 73 | + gnrc_pktbuf_release(pkt); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + pkt = gnrc_pkt_prepend(pkt, hdr); |
| 77 | + nethdr = (gnrc_netif_hdr_t *)hdr->data; |
| 78 | + nethdr->flags = flags; |
| 79 | + /* and send it */ |
| 80 | + if (gnrc_netif_send(container_of(iface, gnrc_netif_t, netif), pkt) < 1) { |
| 81 | + printf("error: unable to send\n"); |
| 82 | + gnrc_pktbuf_release(pkt); |
| 83 | + return 1; |
| 84 | + } |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +SHELL_COMMAND(txtsnd, "Sends a custom string as is over the link layer", _gnrc_netif_send); |
0 commit comments