From 3418fff4df3aaa68c1016260a33ddc76826a50cc Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Tue, 31 Oct 2017 09:16:27 +0100 Subject: [PATCH] Move packet processing example as a new, simpler example: ipv6-hooks --- examples/ipv6-hooks/Makefile | 4 + examples/ipv6-hooks/README.md | 2 + examples/ipv6-hooks/ipv6-hooks.c | 58 +++++ examples/rpl-udp/Makefile | 2 +- examples/rpl-udp/rpl-udp-packet-debug.csc | 285 --------------------- examples/rpl-udp/udp-client-packet-debug.c | 110 -------- 6 files changed, 65 insertions(+), 396 deletions(-) create mode 100644 examples/ipv6-hooks/Makefile create mode 100644 examples/ipv6-hooks/README.md create mode 100644 examples/ipv6-hooks/ipv6-hooks.c delete mode 100644 examples/rpl-udp/rpl-udp-packet-debug.csc delete mode 100644 examples/rpl-udp/udp-client-packet-debug.c diff --git a/examples/ipv6-hooks/Makefile b/examples/ipv6-hooks/Makefile new file mode 100644 index 000000000..bbd4b7168 --- /dev/null +++ b/examples/ipv6-hooks/Makefile @@ -0,0 +1,4 @@ +all: ipv6-hooks + +CONTIKI=../.. +include $(CONTIKI)/Makefile.include diff --git a/examples/ipv6-hooks/README.md b/examples/ipv6-hooks/README.md new file mode 100644 index 000000000..463965d6c --- /dev/null +++ b/examples/ipv6-hooks/README.md @@ -0,0 +1,2 @@ +A simple example of how to set up IPv6 packet hooks with netstack_ip_packet_processor_add. +You can run this example as native and then ping the node to see printouts from the hooks, at every packet input/output. diff --git a/examples/ipv6-hooks/ipv6-hooks.c b/examples/ipv6-hooks/ipv6-hooks.c new file mode 100644 index 000000000..30269b030 --- /dev/null +++ b/examples/ipv6-hooks/ipv6-hooks.c @@ -0,0 +1,58 @@ +#include "contiki.h" +#include "rpl.h" +#include "random.h" +#include "net/netstack.h" +#include "net/ipv6/simple-udp.h" +#include "net/ipv6/uipbuf.h" + +#include "sys/log.h" +#define LOG_MODULE "App" +#define LOG_LEVEL LOG_LEVEL_INFO + +#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) + +/*---------------------------------------------------------------------------*/ +PROCESS(ipv6_hooks_process, "IPv6 Hooks"); +AUTOSTART_PROCESSES(&ipv6_hooks_process); +/*---------------------------------------------------------------------------*/ +static enum netstack_ip_action +ip_input(void) +{ + uint8_t proto = 0; + uipbuf_get_last_header(uip_buf, uip_len, &proto); + LOG_INFO("Incoming packet proto: %d from ", proto); + LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr); + LOG_INFO_("\n"); + return NETSTACK_IP_PROCESS; +} +/*---------------------------------------------------------------------------*/ +static enum netstack_ip_action +ip_output(const linkaddr_t *localdest) +{ + uint8_t proto; + uint8_t is_me = 0; + uipbuf_get_last_header(uip_buf, uip_len, &proto); + is_me = uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr); + LOG_INFO("Outgoing packet (%s) proto: %d to ", is_me ? "send" : "fwd ", proto); + LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); + LOG_INFO_("\n"); + return NETSTACK_IP_PROCESS; +} +/*---------------------------------------------------------------------------*/ +struct netstack_ip_packet_processor packet_processor = { + .process_input = ip_input, + .process_output = ip_output +}; +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(ipv6_hooks_process, ev, data) +{ + PROCESS_BEGIN(); + + /* Register packet processor */ + netstack_ip_packet_processor_add(&packet_processor); + + /* Do nothing */ + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/examples/rpl-udp/Makefile b/examples/rpl-udp/Makefile index 0a4d4d4e5..e03221806 100644 --- a/examples/rpl-udp/Makefile +++ b/examples/rpl-udp/Makefile @@ -1,4 +1,4 @@ -all: udp-client udp-server udp-client-packet-debug +all: udp-client udp-server CONTIKI=../.. include $(CONTIKI)/Makefile.include diff --git a/examples/rpl-udp/rpl-udp-packet-debug.csc b/examples/rpl-udp/rpl-udp-packet-debug.csc deleted file mode 100644 index 2349934e6..000000000 --- a/examples/rpl-udp/rpl-udp-packet-debug.csc +++ /dev/null @@ -1,285 +0,0 @@ - - - [APPS_DIR]/mrm - [APPS_DIR]/mspsim - [APPS_DIR]/avrora - [APPS_DIR]/serial_socket - [APPS_DIR]/collect-view - [APPS_DIR]/powertracker - - RPL-UDP Example -- Packet Debug - generated - 5000000 - - org.contikios.cooja.radiomediums.UDGM - 50.0 - 50.0 - 1.0 - 1.0 - - - 40000 - - - org.contikios.cooja.mspmote.SkyMoteType - sky1 - Sky Mote Type #sky1 - [CONTIKI_DIR]/examples/rpl-udp/udp-server.c - make clean udp-server.sky TARGET=sky - [CONTIKI_DIR]/examples/rpl-udp/udp-server.sky - org.contikios.cooja.interfaces.Position - org.contikios.cooja.interfaces.RimeAddress - org.contikios.cooja.interfaces.IPAddress - org.contikios.cooja.interfaces.Mote2MoteRelations - org.contikios.cooja.interfaces.MoteAttributes - org.contikios.cooja.mspmote.interfaces.MspClock - org.contikios.cooja.mspmote.interfaces.MspMoteID - org.contikios.cooja.mspmote.interfaces.SkyButton - org.contikios.cooja.mspmote.interfaces.SkyFlash - org.contikios.cooja.mspmote.interfaces.SkyCoffeeFilesystem - org.contikios.cooja.mspmote.interfaces.Msp802154Radio - org.contikios.cooja.mspmote.interfaces.MspSerial - org.contikios.cooja.mspmote.interfaces.SkyLED - org.contikios.cooja.mspmote.interfaces.MspDebugOutput - org.contikios.cooja.mspmote.interfaces.SkyTemperature - - - org.contikios.cooja.mspmote.SkyMoteType - sky2 - Sky Mote Type #sky2 - [CONTIKI_DIR]/examples/rpl-udp/udp-client-packet-debug.c - make clean udp-client-packet-debug.sky TARGET=sky - [CONTIKI_DIR]/examples/rpl-udp/udp-client-packet-debug.sky - org.contikios.cooja.interfaces.Position - org.contikios.cooja.interfaces.RimeAddress - org.contikios.cooja.interfaces.IPAddress - org.contikios.cooja.interfaces.Mote2MoteRelations - org.contikios.cooja.interfaces.MoteAttributes - org.contikios.cooja.mspmote.interfaces.MspClock - org.contikios.cooja.mspmote.interfaces.MspMoteID - org.contikios.cooja.mspmote.interfaces.SkyButton - org.contikios.cooja.mspmote.interfaces.SkyFlash - org.contikios.cooja.mspmote.interfaces.SkyCoffeeFilesystem - org.contikios.cooja.mspmote.interfaces.Msp802154Radio - org.contikios.cooja.mspmote.interfaces.MspSerial - org.contikios.cooja.mspmote.interfaces.SkyLED - org.contikios.cooja.mspmote.interfaces.MspDebugOutput - org.contikios.cooja.mspmote.interfaces.SkyTemperature - - - - - org.contikios.cooja.interfaces.Position - 30.051578821079996 - -64.69428746901113 - 0.0 - - - org.contikios.cooja.mspmote.interfaces.MspClock - 1.0 - - - org.contikios.cooja.mspmote.interfaces.MspMoteID - 1 - - sky1 - - - - - org.contikios.cooja.interfaces.Position - 21.31366587648077 - -34.91404431659299 - 0.0 - - - org.contikios.cooja.mspmote.interfaces.MspClock - 1.0 - - - org.contikios.cooja.mspmote.interfaces.MspMoteID - 2 - - sky2 - - - - - org.contikios.cooja.interfaces.Position - 57.165216906562264 - -34.02362118358309 - 0.0 - - - org.contikios.cooja.mspmote.interfaces.MspClock - 1.0 - - - org.contikios.cooja.mspmote.interfaces.MspMoteID - 3 - - sky2 - - - - - org.contikios.cooja.interfaces.Position - 75.1786320042507 - -60.441618586411096 - 0.0 - - - org.contikios.cooja.mspmote.interfaces.MspClock - 1.0 - - - org.contikios.cooja.mspmote.interfaces.MspMoteID - 4 - - sky2 - - - - - org.contikios.cooja.interfaces.Position - 59.11488934836 - -7.544844766953879 - 0.0 - - - org.contikios.cooja.mspmote.interfaces.MspClock - 1.0 - - - org.contikios.cooja.mspmote.interfaces.MspMoteID - 5 - - sky2 - - - - - org.contikios.cooja.interfaces.Position - 91.720430365082 - -22.736406051209734 - 0.0 - - - org.contikios.cooja.mspmote.interfaces.MspClock - 1.0 - - - org.contikios.cooja.mspmote.interfaces.MspMoteID - 6 - - sky2 - - - - - org.contikios.cooja.interfaces.Position - 92.90372351580778 - 2.532304558933846 - 0.0 - - - org.contikios.cooja.mspmote.interfaces.MspClock - 1.0 - - - org.contikios.cooja.mspmote.interfaces.MspMoteID - 7 - - sky2 - - - - - org.contikios.cooja.interfaces.Position - 120.28246466847423 - 19.133402903417824 - 0.0 - - - org.contikios.cooja.mspmote.interfaces.MspClock - 1.0 - - - org.contikios.cooja.mspmote.interfaces.MspMoteID - 8 - - sky2 - - - - org.contikios.cooja.plugins.SimControl - 249 - 4 - 184 - 3 - 15 - - - org.contikios.cooja.plugins.Visualizer - - org.contikios.cooja.plugins.skins.IDVisualizerSkin - org.contikios.cooja.plugins.skins.UDGMVisualizerSkin - org.contikios.cooja.plugins.skins.AttributeVisualizerSkin - 1.3598488697820064 0.0 0.0 1.3598488697820064 6.142207908179105 118.20877091196155 - - 234 - 1 - 227 - 14 - 210 - - - org.contikios.cooja.plugins.LogListener - - - - - - 1011 - 0 - 556 - 759 - 7 - - - org.contikios.cooja.plugins.TimeLine - - 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - - - - 681.712557066089 - - 1804 - 3 - 352 - 0 - 567 - - - org.contikios.cooja.plugins.RadioLogger - - 150 - - false - false - - 500 - 2 - 546 - 259 - 12 - - diff --git a/examples/rpl-udp/udp-client-packet-debug.c b/examples/rpl-udp/udp-client-packet-debug.c deleted file mode 100644 index 032075060..000000000 --- a/examples/rpl-udp/udp-client-packet-debug.c +++ /dev/null @@ -1,110 +0,0 @@ -#include "contiki.h" -#include "rpl.h" -#include "random.h" -#include "net/netstack.h" -#include "net/ipv6/simple-udp.h" -#include "net/ipv6/uipbuf.h" - -#include "sys/log.h" -#define LOG_MODULE "App" -#define LOG_LEVEL LOG_LEVEL_INFO - -#define WITH_SERVER_REPLY 1 -#define UDP_CLIENT_PORT 8765 -#define UDP_SERVER_PORT 5678 - -static struct simple_udp_connection udp_conn; - -#define START_INTERVAL (15 * CLOCK_SECOND) -#define SEND_INTERVAL (60 * CLOCK_SECOND) - -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - -static struct simple_udp_connection udp_conn; -static uip_ipaddr_t server_ipaddr; - -/*---------------------------------------------------------------------------*/ -PROCESS(udp_client_process, "UDP client -- packet debug"); -AUTOSTART_PROCESSES(&udp_client_process); -/*---------------------------------------------------------------------------*/ -static enum netstack_ip_action -ip_input(void) -{ - uint8_t proto = 0; - uipbuf_get_last_header(uip_buf, uip_len, &proto); - LOG_INFO("Incoming packet proto: %d from ", proto); - LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr); - LOG_INFO_("\n"); - return NETSTACK_IP_PROCESS; -} -/*---------------------------------------------------------------------------*/ -static enum netstack_ip_action -ip_output(const linkaddr_t *localdest) -{ - uint8_t proto; - uint8_t is_me = 0; - uipbuf_get_last_header(uip_buf, uip_len, &proto); - is_me = uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr); - LOG_INFO("Outgoing packet (%s) proto: %d to ", is_me ? "send" : "fwd ", proto); - LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); - LOG_INFO_("\n"); - return NETSTACK_IP_PROCESS; -} -/*---------------------------------------------------------------------------*/ -struct netstack_ip_packet_processor packet_processor = { - .process_input = ip_input, - .process_output = ip_output -}; -/*---------------------------------------------------------------------------*/ -static void -udp_rx_callback(struct simple_udp_connection *c, - const uip_ipaddr_t *sender_addr, - uint16_t sender_port, - const uip_ipaddr_t *receiver_addr, - uint16_t receiver_port, - const uint8_t *data, - uint16_t datalen) -{ - unsigned count = *(unsigned *)data; - LOG_INFO("Received response %u from ", count); - LOG_INFO_6ADDR(sender_addr); - LOG_INFO_("\n"); -} -/*---------------------------------------------------------------------------*/ -PROCESS_THREAD(udp_client_process, ev, data) -{ - static struct etimer periodic_timer; - static unsigned count; - - PROCESS_BEGIN(); - - INIT_SERVER_IPADDR(server_ipaddr); - /* Initialize UDP connection */ - simple_udp_register(&udp_conn, UDP_CLIENT_PORT, NULL, - UDP_SERVER_PORT, udp_rx_callback); - - /* register packet processor */ - netstack_ip_packet_processor_add(&packet_processor); - - etimer_set(&periodic_timer, random_rand() % SEND_INTERVAL); - PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); - etimer_set(&periodic_timer, SEND_INTERVAL); - while(1) { - PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); - - if(rpl_is_reachable()) { - LOG_INFO("Sending request %u to ", count); - LOG_INFO_6ADDR(&server_ipaddr); - LOG_INFO_("\n"); - simple_udp_sendto(&udp_conn, &count, sizeof(count), &server_ipaddr); - count++; - } else { - LOG_INFO("Not reachable yet %p\n", rpl_get_any_dag()); - } - - etimer_reset(&periodic_timer); - } - - PROCESS_END(); -} -/*---------------------------------------------------------------------------*/