1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/net/gnrc/pktdump/gnrc_pktdump.c

212 lines
6.1 KiB
C
Raw Normal View History

2015-03-21 11:47:25 +01:00
/*
* Copyright (C) 2015 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
* directory for more details.
*/
/**
2018-06-01 13:19:39 +02:00
* @ingroup net_gnrc_pktdump
2015-03-21 11:47:25 +01:00
* @{
*
* @file
* @brief Generic module to dump packages received via netapi to STDOUT
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
2015-03-25 03:01:34 +01:00
#include <inttypes.h>
2015-03-21 11:47:25 +01:00
#include <stdio.h>
#include <errno.h>
2015-03-25 03:01:34 +01:00
#include "byteorder.h"
2015-03-21 11:47:25 +01:00
#include "thread.h"
#include "msg.h"
#include "net/gnrc/pktdump.h"
2015-08-10 02:41:08 +02:00
#include "net/gnrc.h"
2017-01-27 16:13:56 +01:00
#include "net/icmpv6.h"
2015-08-10 00:26:36 +02:00
#include "net/ipv6/addr.h"
#include "net/ipv6/hdr.h"
2017-02-07 13:42:15 +01:00
#include "net/tcp.h"
2015-08-10 03:16:53 +02:00
#include "net/udp.h"
#include "net/sixlowpan.h"
#include "od.h"
2015-03-21 11:47:25 +01:00
/**
* @brief PID of the pktdump thread
*/
2016-03-11 14:22:20 +01:00
kernel_pid_t gnrc_pktdump_pid = KERNEL_PID_UNDEF;
/**
* @brief Stack for the pktdump thread
*/
static char _stack[GNRC_PKTDUMP_STACKSIZE];
static void _dump_snip(gnrc_pktsnip_t *pkt)
2015-03-21 11:47:25 +01:00
{
size_t hdr_len = 0;
switch (pkt->type) {
case GNRC_NETTYPE_NETIF:
printf("NETTYPE_NETIF (%i)\n", pkt->type);
if (IS_USED(MODULE_GNRC_NETIF_HDR)) {
gnrc_netif_hdr_print(pkt->data);
hdr_len = pkt->size;
}
break;
case GNRC_NETTYPE_UNDEF:
printf("NETTYPE_UNDEF (%i)\n", pkt->type);
break;
#if IS_USED(MODULE_GNRC_NETTYPE_SIXLOWPAN)
case GNRC_NETTYPE_SIXLOWPAN:
2015-04-19 16:46:32 +02:00
printf("NETTYPE_SIXLOWPAN (%i)\n", pkt->type);
if (IS_USED(MODULE_SIXLOWPAN)) {
sixlowpan_print(pkt->data, pkt->size);
hdr_len = pkt->size;
}
2015-03-21 11:47:25 +01:00
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_SIXLOWPAN) */
#if IS_USED(MODULE_GNRC_NETTYPE_IPV6)
case GNRC_NETTYPE_IPV6:
2015-03-25 03:01:34 +01:00
printf("NETTYPE_IPV6 (%i)\n", pkt->type);
if (IS_USED(MODULE_IPV6_HDR)) {
ipv6_hdr_print(pkt->data);
hdr_len = sizeof(ipv6_hdr_t);
}
2015-03-21 11:47:25 +01:00
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_IPV6) */
#if IS_USED(MODULE_GNRC_NETTYPE_IPV6_EXT)
case GNRC_NETTYPE_IPV6_EXT:
printf("NETTYPE_IPV6_EXT (%i)\n", pkt->type);
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_IPV6_EXT) */
#if IS_USED(MODULE_GNRC_NETTYPE_ICMPV6)
case GNRC_NETTYPE_ICMPV6:
2015-04-19 16:46:32 +02:00
printf("NETTYPE_ICMPV6 (%i)\n", pkt->type);
if (IS_USED(MODULE_ICMPV6)) {
icmpv6_hdr_print(pkt->data);
hdr_len = sizeof(icmpv6_hdr_t);
}
2015-03-21 11:47:25 +01:00
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_ICMPV6) */
#if IS_USED(MODULE_GNRC_NETTYPE_TCP)
case GNRC_NETTYPE_TCP:
2015-04-19 16:46:32 +02:00
printf("NETTYPE_TCP (%i)\n", pkt->type);
if (IS_USED(MODULE_TCP)) {
tcp_hdr_print(pkt->data);
hdr_len = sizeof(tcp_hdr_t);
}
2015-03-21 11:47:25 +01:00
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_TCP) */
#if IS_USED(MODULE_GNRC_NETTYPE_UDP)
case GNRC_NETTYPE_UDP:
2015-04-19 16:46:32 +02:00
printf("NETTYPE_UDP (%i)\n", pkt->type);
if (IS_USED(MODULE_UDP)) {
udp_hdr_print(pkt->data);
hdr_len = sizeof(udp_hdr_t);
}
2015-03-21 11:47:25 +01:00
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_UDP) */
#if IS_USED(MODULE_GNRC_NETTYPE_CCN)
case GNRC_NETTYPE_CCN_CHUNK:
printf("GNRC_NETTYPE_CCN_CHUNK (%i)\n", pkt->type);
printf("Content is: %.*s\n", (int)pkt->size, (char*)pkt->data);
hdr_len = pkt->size;
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_CCN) */
#if IS_USED(MODULE_GNRC_NETTYPE_NDN)
2016-10-06 16:13:05 +02:00
case GNRC_NETTYPE_NDN:
printf("NETTYPE_NDN (%i)\n", pkt->type);
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_NDN) */
#if IS_USED(MODULE_GNRC_NETTYPE_LORAWAN)
2019-11-01 15:45:17 +01:00
case GNRC_NETTYPE_LORAWAN:
printf("NETTYPE_LORAWAN (%i)\n", pkt->type);
break;
#endif /* IS_USED(MODULE_GNRC_NETTYPE_LORAWAN) */
2015-03-21 11:47:25 +01:00
#ifdef TEST_SUITES
case GNRC_NETTYPE_TEST:
2015-04-19 16:46:32 +02:00
printf("NETTYPE_TEST (%i)\n", pkt->type);
2015-03-21 11:47:25 +01:00
break;
#endif
default:
2015-04-19 16:46:32 +02:00
printf("NETTYPE_UNKNOWN (%i)\n", pkt->type);
2015-03-21 11:47:25 +01:00
break;
}
if (hdr_len < pkt->size) {
size_t size = pkt->size - hdr_len;
od_hex_dump(((uint8_t *)pkt->data) + hdr_len, size, OD_WIDTH_DEFAULT);
}
2015-03-21 11:47:25 +01:00
}
static void _dump(gnrc_pktsnip_t *pkt)
2015-03-21 11:47:25 +01:00
{
int snips = 0;
int size = 0;
gnrc_pktsnip_t *snip = pkt;
2015-03-21 11:47:25 +01:00
2015-03-24 22:14:16 +01:00
while (snip != NULL) {
printf("~~ SNIP %2i - size: %3u byte, type: ", snips,
(unsigned int)snip->size);
_dump_snip(snip);
2015-03-21 11:47:25 +01:00
++snips;
2015-03-24 22:14:16 +01:00
size += snip->size;
snip = snip->next;
2015-03-21 11:47:25 +01:00
}
2015-03-21 11:47:25 +01:00
printf("~~ PKT - %2i snips, total size: %3i byte\n", snips, size);
gnrc_pktbuf_release(pkt);
2015-03-21 11:47:25 +01:00
}
static void *_eventloop(void *arg)
2015-03-21 11:47:25 +01:00
{
(void)arg;
msg_t msg, reply;
msg_t msg_queue[GNRC_PKTDUMP_MSG_QUEUE_SIZE];
2015-03-25 23:47:19 +01:00
/* setup the message queue */
msg_init_queue(msg_queue, GNRC_PKTDUMP_MSG_QUEUE_SIZE);
2015-03-21 11:47:25 +01:00
reply.content.value = (uint32_t)(-ENOTSUP);
reply.type = GNRC_NETAPI_MSG_TYPE_ACK;
2015-03-21 11:47:25 +01:00
while (1) {
msg_receive(&msg);
switch (msg.type) {
case GNRC_NETAPI_MSG_TYPE_RCV:
2015-03-21 11:47:25 +01:00
puts("PKTDUMP: data received:");
_dump(msg.content.ptr);
2015-03-21 11:47:25 +01:00
break;
case GNRC_NETAPI_MSG_TYPE_SND:
2015-03-21 11:47:25 +01:00
puts("PKTDUMP: data to send:");
_dump(msg.content.ptr);
2015-03-21 11:47:25 +01:00
break;
case GNRC_NETAPI_MSG_TYPE_GET:
case GNRC_NETAPI_MSG_TYPE_SET:
msg_reply(&msg, &reply);
break;
2015-03-21 11:47:25 +01:00
default:
puts("PKTDUMP: received something unexpected");
break;
}
}
/* never reached */
return NULL;
}
kernel_pid_t gnrc_pktdump_init(void)
{
2016-03-11 14:22:20 +01:00
if (gnrc_pktdump_pid == KERNEL_PID_UNDEF) {
gnrc_pktdump_pid = thread_create(_stack, sizeof(_stack), GNRC_PKTDUMP_PRIO,
2015-12-02 12:00:19 +01:00
THREAD_CREATE_STACKTEST,
_eventloop, NULL, "pktdump");
}
2016-03-11 14:22:20 +01:00
return gnrc_pktdump_pid;
2015-03-21 11:47:25 +01:00
}