|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Freie Universität Berlin |
| 3 | + * Copyright (C) 2016 Hochschule für Angewandte Wissenschaften Hamburg |
| 4 | + * |
| 5 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 6 | + * General Public License v2.1. See the file LICENSE in the top level |
| 7 | + * directory for more details. |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * @ingroup examples |
| 12 | + * @{ |
| 13 | + * |
| 14 | + * @file |
| 15 | + * @brief Example application for demonstrating the RIOT network stack |
| 16 | + * |
| 17 | + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> |
| 18 | + * @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de> |
| 19 | + * |
| 20 | + * @} |
| 21 | + */ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | + |
| 25 | +#include "shell.h" |
| 26 | +#include "msg.h" |
| 27 | +#include "thread.h" |
| 28 | + |
| 29 | +#include "net/gnrc.h" |
| 30 | +#include "net/gnrc/ipv6/netif.h" |
| 31 | +#include "net/gnrc/ipv6.h" |
| 32 | + |
| 33 | +#define MAIN_QUEUE_SIZE (8) |
| 34 | +static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; |
| 35 | + |
| 36 | +extern int udp_cmd(int argc, char **argv); |
| 37 | + |
| 38 | +static const shell_command_t shell_commands[] = { |
| 39 | + { "udp", "send data over UDP and listen on UDP ports", udp_cmd }, |
| 40 | + { NULL, NULL, NULL } |
| 41 | +}; |
| 42 | + |
| 43 | +#ifdef MODULE_GNRC_SIXLOWPAN |
| 44 | +static char _stack[THREAD_STACKSIZE_MAIN]; |
| 45 | + |
| 46 | +static void *_ipv6_fwd_eventloop(void *arg) |
| 47 | +{ |
| 48 | + (void)arg; |
| 49 | + |
| 50 | + msg_t msg, msg_q[8]; |
| 51 | + gnrc_netreg_entry_t me_reg; |
| 52 | + |
| 53 | + msg_init_queue(msg_q, 8); |
| 54 | + |
| 55 | + me_reg.demux_ctx = GNRC_NETREG_DEMUX_CTX_ALL; |
| 56 | + me_reg.pid = thread_getpid(); |
| 57 | + |
| 58 | + gnrc_netreg_register(GNRC_NETTYPE_SIXLOWPAN, &me_reg); |
| 59 | + |
| 60 | + while(1) { |
| 61 | + msg_receive(&msg); |
| 62 | + gnrc_pktsnip_t *pkt = msg.content.ptr; |
| 63 | + if(msg.type == GNRC_NETAPI_MSG_TYPE_SND) { |
| 64 | + gnrc_pktsnip_t *ipv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6); |
| 65 | + ipv6 = ipv6->data; |
| 66 | + |
| 67 | + ipv6_hdr_t *ipv6_hdr =(ipv6_hdr_t *)ipv6; |
| 68 | + |
| 69 | + /* get the first IPv6 interface and prints its address */ |
| 70 | + kernel_pid_t ifs[GNRC_NETIF_NUMOF]; |
| 71 | + gnrc_netif_get(ifs); |
| 72 | + gnrc_ipv6_netif_t *entry = gnrc_ipv6_netif_get(ifs[0]); |
| 73 | + for (int i = 0; i < GNRC_IPV6_NETIF_ADDR_NUMOF; i++) { |
| 74 | + if ( (!ipv6_addr_is_link_local(&entry->addrs[i].addr)) && (!ipv6_addr_is_link_local(&ipv6_hdr->src)) |
| 75 | + && (!ipv6_addr_is_link_local(&ipv6_hdr->dst)) && !(entry->addrs[i].flags & GNRC_IPV6_NETIF_ADDR_FLAGS_NON_UNICAST) |
| 76 | + && (!ipv6_addr_is_unspecified(&entry->addrs[i].addr)) ) { |
| 77 | + |
| 78 | + if(!ipv6_addr_equal(&entry->addrs[i].addr, &(ipv6_hdr->src))){ |
| 79 | + |
| 80 | + char addr_str[IPV6_ADDR_MAX_STR_LEN]; |
| 81 | + printf("IPv6 ROUTER: forward from src = %s ", ipv6_addr_to_str(addr_str, &(ipv6_hdr->src), sizeof(addr_str)) ); |
| 82 | + printf("to dst = %s\n",ipv6_addr_to_str(addr_str, &(ipv6_hdr->dst), sizeof(addr_str))); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + gnrc_pktbuf_release(pkt); |
| 88 | + } |
| 89 | + /* never reached */ |
| 90 | + return NULL; |
| 91 | +} |
| 92 | +#endif |
| 93 | + |
| 94 | +int main(void) |
| 95 | +{ |
| 96 | + /* we need a message queue for the thread running the shell in order to |
| 97 | + * receive potentially fast incoming networking packets */ |
| 98 | + msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); |
| 99 | + puts("RIOT network stack example application"); |
| 100 | + |
| 101 | +#ifdef MODULE_GNRC_SIXLOWPAN |
| 102 | + thread_create(_stack, sizeof(_stack), (THREAD_PRIORITY_MAIN - 4), |
| 103 | + THREAD_CREATE_STACKTEST, _ipv6_fwd_eventloop, NULL, "ipv6_fwd"); |
| 104 | +#endif |
| 105 | + /* start shell */ |
| 106 | + puts("All up, running the shell now"); |
| 107 | + char line_buf[SHELL_DEFAULT_BUFSIZE]; |
| 108 | + shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); |
| 109 | + |
| 110 | + /* should be never reached */ |
| 111 | + return 0; |
| 112 | +} |
0 commit comments