Merge pull request #4784 from authmillenon/gnrc_pkt/api/search-type-function

gnrc_pkt: provide type search function
This commit is contained in:
Martine Lenders 2016-02-15 13:33:45 +01:00
commit a66ce9c3eb
16 changed files with 66 additions and 15 deletions

View File

@ -379,6 +379,7 @@ ifneq (,$(filter gnrc_pktbuf, $(USEMODULE)))
ifeq (,$(filter gnrc_pktbuf_%, $(USEMODULE))) ifeq (,$(filter gnrc_pktbuf_%, $(USEMODULE)))
USEMODULE += gnrc_pktbuf_static USEMODULE += gnrc_pktbuf_static
endif endif
USEMODULE += gnrc_pkt
endif endif
ifneq (,$(filter gnrc_pktbuf_%, $(USEMODULE))) ifneq (,$(filter gnrc_pktbuf_%, $(USEMODULE)))

View File

@ -155,6 +155,18 @@ static inline size_t gnrc_pkt_count(const gnrc_pktsnip_t *pkt)
return count; return count;
} }
/**
* @brief Searches the packet for a packet snip of a specific type
*
* @param[in] pkt list of packet snips
* @param[in] type the type to search for
*
* @return the packet snip in @p pkt with @ref gnrc_nettype_t @p type
* @return NULL, if none of the snips in @p pkt is of @p type
*/
gnrc_pktsnip_t *gnrc_pktsnip_search_type(gnrc_pktsnip_t *pkt,
gnrc_nettype_t type);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -73,6 +73,9 @@ endif
ifneq (,$(filter gnrc_nomac,$(USEMODULE))) ifneq (,$(filter gnrc_nomac,$(USEMODULE)))
DIRS += link_layer/nomac DIRS += link_layer/nomac
endif endif
ifneq (,$(filter gnrc_pkt,$(USEMODULE)))
DIRS += pkt
endif
ifneq (,$(filter gnrc_pktbuf_static,$(USEMODULE))) ifneq (,$(filter gnrc_pktbuf_static,$(USEMODULE)))
DIRS += pktbuf_static DIRS += pktbuf_static
endif endif

View File

@ -598,10 +598,10 @@ tftp_state _tftp_state_processes(tftp_context_t *ctxt, msg_t *m)
gnrc_pktsnip_t *pkt = (gnrc_pktsnip_t *)(m->content.ptr); gnrc_pktsnip_t *pkt = (gnrc_pktsnip_t *)(m->content.ptr);
gnrc_pktsnip_t *tmp; gnrc_pktsnip_t *tmp;
LL_SEARCH_SCALAR(pkt, tmp, type, GNRC_NETTYPE_UDP); tmp = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_UDP);
udp_hdr_t *udp = (udp_hdr_t *)tmp->data; udp_hdr_t *udp = (udp_hdr_t *)tmp->data;
LL_SEARCH_SCALAR(pkt, tmp, type, GNRC_NETTYPE_IPV6); tmp = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6);
ipv6_hdr_t *ip = (ipv6_hdr_t *)tmp->data; ipv6_hdr_t *ip = (ipv6_hdr_t *)tmp->data;
uint8_t *data = (uint8_t *)pkt->data; uint8_t *data = (uint8_t *)pkt->data;

View File

@ -36,7 +36,7 @@ int gnrc_conn_recvfrom(conn_t *conn, void *data, size_t max_len, void *addr, siz
if (pkt->size > max_len) { if (pkt->size > max_len) {
return -ENOMEM; return -ENOMEM;
} }
LL_SEARCH_SCALAR(pkt, l3hdr, type, conn->l3_type); l3hdr = gnrc_pktsnip_search_type(pkt, conn->l3_type);
if (l3hdr == NULL) { if (l3hdr == NULL) {
msg_send_to_self(&msg); /* requeue invalid messages */ msg_send_to_self(&msg); /* requeue invalid messages */
continue; continue;
@ -44,7 +44,7 @@ int gnrc_conn_recvfrom(conn_t *conn, void *data, size_t max_len, void *addr, siz
#if defined(MODULE_CONN_UDP) || defined(MODULE_CONN_TCP) #if defined(MODULE_CONN_UDP) || defined(MODULE_CONN_TCP)
if ((conn->l4_type != GNRC_NETTYPE_UNDEF) && (port != NULL)) { if ((conn->l4_type != GNRC_NETTYPE_UNDEF) && (port != NULL)) {
gnrc_pktsnip_t *l4hdr; gnrc_pktsnip_t *l4hdr;
LL_SEARCH_SCALAR(pkt, l4hdr, type, conn->l4_type); l4hdr = gnrc_pktsnip_search_type(pkt, conn->l4_type);
if (l4hdr == NULL) { if (l4hdr == NULL) {
msg_send_to_self(&msg); /* requeue invalid messages */ msg_send_to_self(&msg); /* requeue invalid messages */
continue; continue;

View File

@ -59,13 +59,13 @@ void gnrc_icmpv6_demux(kernel_pid_t iface, gnrc_pktsnip_t *pkt)
icmpv6_hdr_t *hdr; icmpv6_hdr_t *hdr;
gnrc_netreg_entry_t *sendto; gnrc_netreg_entry_t *sendto;
LL_SEARCH_SCALAR(pkt, icmpv6, type, GNRC_NETTYPE_ICMPV6); icmpv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_ICMPV6);
assert(icmpv6 != NULL); assert(icmpv6 != NULL);
/* there can be extension headers between IPv6 and ICMPv6 header so we have /* there can be extension headers between IPv6 and ICMPv6 header so we have
* to search it */ * to search it */
LL_SEARCH_SCALAR(icmpv6, ipv6, type, GNRC_NETTYPE_IPV6); ipv6 = gnrc_pktsnip_search_type(icmpv6, GNRC_NETTYPE_IPV6);
assert(ipv6 != NULL); assert(ipv6 != NULL);

View File

@ -710,7 +710,7 @@ static void _receive(gnrc_pktsnip_t *pkt)
assert(pkt != NULL); assert(pkt != NULL);
LL_SEARCH_SCALAR(pkt, netif, type, GNRC_NETTYPE_NETIF); netif = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_NETIF);
if (netif != NULL) { if (netif != NULL) {
iface = ((gnrc_netif_hdr_t *)netif->data)->if_pid; iface = ((gnrc_netif_hdr_t *)netif->data)->if_pid;

View File

@ -275,7 +275,7 @@ void gnrc_ndp_nbr_adv_handle(kernel_pid_t iface, gnrc_pktsnip_t *pkt,
#endif #endif
} }
LL_SEARCH_SCALAR(pkt, netif, type, GNRC_NETTYPE_NETIF); netif = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_NETIF);
if (netif != NULL) { if (netif != NULL) {
netif_hdr = netif->data; netif_hdr = netif->data;

View File

@ -66,7 +66,7 @@ kernel_pid_t gnrc_ndp_node_next_hop_l2addr(uint8_t *l2addr, uint8_t *l2addr_len,
#ifdef MODULE_GNRC_IPV6_EXT_RH #ifdef MODULE_GNRC_IPV6_EXT_RH
ipv6_hdr_t *hdr; ipv6_hdr_t *hdr;
gnrc_pktsnip_t *ipv6; gnrc_pktsnip_t *ipv6;
LL_SEARCH_SCALAR(pkt, ipv6, type, GNRC_NETTYPE_IPV6); ipv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6);
assert(ipv6); assert(ipv6);
hdr = ipv6->data; hdr = ipv6->data;
next_hop_ip = ipv6_ext_rh_next_hop(hdr); next_hop_ip = ipv6_ext_rh_next_hop(hdr);

View File

@ -84,7 +84,7 @@ static void _receive(gnrc_pktsnip_t *pkt)
pkt = payload; /* reset pkt from temporary variable */ pkt = payload; /* reset pkt from temporary variable */
LL_SEARCH_SCALAR(pkt, payload, type, GNRC_NETTYPE_SIXLOWPAN); payload = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_SIXLOWPAN);
if ((payload == NULL) || (payload->size < 1)) { if ((payload == NULL) || (payload->size < 1)) {
DEBUG("6lo: Received packet has no 6LoWPAN payload\n"); DEBUG("6lo: Received packet has no 6LoWPAN payload\n");

View File

@ -127,7 +127,7 @@ kernel_pid_t gnrc_sixlowpan_nd_next_hop_l2addr(uint8_t *l2addr, uint8_t *l2addr_
#ifdef MODULE_GNRC_IPV6_EXT_RH #ifdef MODULE_GNRC_IPV6_EXT_RH
ipv6_hdr_t *hdr; ipv6_hdr_t *hdr;
gnrc_pktsnip_t *ipv6; gnrc_pktsnip_t *ipv6;
LL_SEARCH_SCALAR(pkt, ipv6, type, GNRC_NETTYPE_IPV6); ipv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6);
assert(ipv6); assert(ipv6);
hdr = ipv6->data; hdr = ipv6->data;
next_hop = ipv6_ext_rh_next_hop(hdr); next_hop = ipv6_ext_rh_next_hop(hdr);

View File

@ -0,0 +1,3 @@
MODULE := gnrc_pkt
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2016 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.
*/
/**
* @{
*
* @file
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#include <assert.h>
#include "net/gnrc/pkt.h"
gnrc_pktsnip_t *gnrc_pktsnip_search_type(gnrc_pktsnip_t *ptr,
gnrc_nettype_t type)
{
while (ptr != NULL) {
if (ptr->type == type) {
return ptr;
}
ptr = ptr->next;
}
return NULL;
}
/** @} */

View File

@ -121,7 +121,7 @@ static void _receive(gnrc_pktsnip_t *icmpv6)
ipv6_hdr_t *ipv6_hdr = NULL; ipv6_hdr_t *ipv6_hdr = NULL;
icmpv6_hdr_t *icmpv6_hdr = NULL; icmpv6_hdr_t *icmpv6_hdr = NULL;
LL_SEARCH_SCALAR(icmpv6, ipv6, type, GNRC_NETTYPE_IPV6); ipv6 = gnrc_pktsnip_search_type(icmpv6, GNRC_NETTYPE_IPV6);
assert(ipv6 != NULL); assert(ipv6 != NULL);

View File

@ -105,7 +105,7 @@ static void _receive(gnrc_pktsnip_t *pkt)
} }
pkt = udp; pkt = udp;
LL_SEARCH_SCALAR(pkt, ipv6, type, GNRC_NETTYPE_IPV6); ipv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6);
assert(ipv6 != NULL); assert(ipv6 != NULL);

View File

@ -86,8 +86,8 @@ int _handle_reply(gnrc_pktsnip_t *pkt, uint32_t time)
icmpv6_echo_t *icmpv6_hdr; icmpv6_echo_t *icmpv6_hdr;
uint16_t seq; uint16_t seq;
LL_SEARCH_SCALAR(pkt, ipv6, type, GNRC_NETTYPE_IPV6); ipv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6);
LL_SEARCH_SCALAR(pkt, icmpv6, type, GNRC_NETTYPE_ICMPV6); icmpv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_ICMPV6);
if ((ipv6 == NULL) || (icmpv6 == NULL)) { if ((ipv6 == NULL) || (icmpv6 == NULL)) {
puts("error: IPv6 header or ICMPv6 header not found in reply"); puts("error: IPv6 header or ICMPv6 header not found in reply");