|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 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 tests |
| 11 | + * @{ |
| 12 | + * |
| 13 | + * @file |
| 14 | + * @brief Tests extension header handling of gnrc stack. |
| 15 | + * |
| 16 | + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> |
| 17 | + * @author Takuo Yonezawa <Yonezawa-T2@mail.dnp.co.jp> |
| 18 | + * |
| 19 | + * @} |
| 20 | + */ |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | + |
| 24 | +#include "shell.h" |
| 25 | +#include "msg.h" |
| 26 | +#include "net/ipv6/addr.h" |
| 27 | +#include "net/gnrc/ipv6/netif.h" |
| 28 | +#include "net/gnrc/pkt.h" |
| 29 | +#include "net/gnrc/pktbuf.h" |
| 30 | +#include "net/gnrc/netreg.h" |
| 31 | +#include "net/gnrc/netapi.h" |
| 32 | +#include "net/gnrc/netif.h" |
| 33 | + |
| 34 | +#define MAIN_QUEUE_SIZE (8) |
| 35 | +static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; |
| 36 | + |
| 37 | +static void _send_packet(void) { |
| 38 | + kernel_pid_t ifs[GNRC_NETIF_NUMOF]; |
| 39 | + ipv6_addr_t addr = IPV6_ADDR_UNSPECIFIED; |
| 40 | + |
| 41 | + gnrc_netif_get(ifs); |
| 42 | + |
| 43 | + addr.u8[0] = 0xfd; |
| 44 | + addr.u8[1] = 0x01; |
| 45 | + addr.u8[15] = 0x02; |
| 46 | + /* fd01::02 */ |
| 47 | + gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST); |
| 48 | + |
| 49 | + addr.u8[15] = 0x03; |
| 50 | + /* fd01::03 */ |
| 51 | + gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST); |
| 52 | + |
| 53 | + uint8_t data[] = { |
| 54 | + /* IPv6 Header */ |
| 55 | + 0x60, 0x00, 0x00, 0x00, /* version, traffic class, flow label */ |
| 56 | + 0x00, 0x28, /* payload length: 40 */ |
| 57 | + 0x00, /* next header: Hop-by-Hop Option */ |
| 58 | + 0x10, /* hop limit: 16 */ |
| 59 | + /* source address: fd01::1 */ |
| 60 | + 0xfd, 0x01, 0x00, 0x00, |
| 61 | + 0x00, 0x00, 0x00, 0x00, |
| 62 | + 0x00, 0x00, 0x00, 0x00, |
| 63 | + 0x00, 0x00, 0x00, 0x01, |
| 64 | + /* destination address: fd01::2 */ |
| 65 | + 0xfd, 0x01, 0x00, 0x00, |
| 66 | + 0x00, 0x00, 0x00, 0x00, |
| 67 | + 0x00, 0x00, 0x00, 0x00, |
| 68 | + 0x00, 0x00, 0x00, 0x02, |
| 69 | + |
| 70 | + /* Hop-by-Hop Options Header */ |
| 71 | + /* https://tools.ietf.org/html/rfc6553 */ |
| 72 | + 0x2b, /* next header: IPv6-Route */ |
| 73 | + 0x00, /* hdr ext len: 0 * 8 + 8 = 8 */ |
| 74 | + 0x63, /* option type: RPL Option */ |
| 75 | + 0x04, /* opt data len: 4 */ |
| 76 | + 0x80, /* flags, Down: 1, Rank-Error: 0, Forwarding-Error: 0 */ |
| 77 | + 0x00, /* RPLInstanceID */ |
| 78 | + 0x80, 0x00, /* SenderRank */ |
| 79 | + |
| 80 | + /* RPL Routing Header */ |
| 81 | + /* https://tools.ietf.org/html/rfc6554 */ |
| 82 | + 0x11, /* next header: UDP */ |
| 83 | + 0x02, /* hdr ext len: 2 * 8 + 8 = 24 */ |
| 84 | + 0x03, /* routing type: SRH */ |
| 85 | + 0x02, /* segments left: 2 */ |
| 86 | + 0xef, /* ComprI: 14, ComprE: 15 */ |
| 87 | + 0xd0, 0x00, 0x00, /* pad and reserved */ |
| 88 | + /* address: fd01::3, fd01::2 */ |
| 89 | + 0x00, 0x03, 0x02, 0x00, |
| 90 | + 0x00, 0x00, 0x00, 0x00, |
| 91 | + 0x00, 0x00, 0x00, 0x00, |
| 92 | + 0x00, 0x00, 0x00, 0x00, |
| 93 | + |
| 94 | + /* UDP (ignored) */ |
| 95 | + 0x1f, 0x90, /* source port: 8080 */ |
| 96 | + 0x1f, 0x90, /* destination port: 8080 */ |
| 97 | + 0x00, 0x08, /* length: 8 */ |
| 98 | + 0xff, 0xff, /* checksum */ |
| 99 | + }; |
| 100 | + |
| 101 | + gnrc_pktsnip_t *pkt = gnrc_pktbuf_add(NULL, data, sizeof(data), GNRC_NETTYPE_UNDEF); |
| 102 | + |
| 103 | + gnrc_netapi_dispatch_receive(GNRC_NETTYPE_IPV6, GNRC_NETREG_DEMUX_CTX_ALL, pkt); |
| 104 | + |
| 105 | + printf("pkt->users: %d\n", pkt->users); |
| 106 | + assert(pkt->users == 0); |
| 107 | +} |
| 108 | + |
| 109 | +int main(void) |
| 110 | +{ |
| 111 | + /* we need a message queue for the thread running the shell in order to |
| 112 | + * receive potentially fast incoming networking packets */ |
| 113 | + msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); |
| 114 | + puts("RIOT network stack example application"); |
| 115 | + |
| 116 | + _send_packet(); |
| 117 | + |
| 118 | + /* start shell */ |
| 119 | + puts("All up, running the shell now"); |
| 120 | + char line_buf[SHELL_DEFAULT_BUFSIZE]; |
| 121 | + shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE); |
| 122 | + |
| 123 | + /* should be never reached */ |
| 124 | + return 0; |
| 125 | +} |
0 commit comments