From 69c593b80b9b3a6d232a7d51890204323f7c4465 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Sat, 7 Mar 2015 16:53:09 +0100 Subject: [PATCH 1/2] netreg: add multiplexer for header building --- sys/include/net/ng_netreg.h | 20 ++++++++++++++++++ sys/net/crosslayer/ng_netreg/ng_netreg.c | 27 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/sys/include/net/ng_netreg.h b/sys/include/net/ng_netreg.h index c7d4e0b468..68e0fa5f84 100644 --- a/sys/include/net/ng_netreg.h +++ b/sys/include/net/ng_netreg.h @@ -24,6 +24,7 @@ #include "kernel_types.h" #include "net/ng_nettype.h" +#include "net/ng_pkt.h" #ifdef __cplusplus extern "C" { @@ -126,6 +127,25 @@ int ng_netreg_num(ng_nettype_t type, uint32_t demux_ctx); */ ng_netreg_entry_t *ng_netreg_getnext(ng_netreg_entry_t *entry); +/** + * @brief Builds a header for sending and adds it to the packet buffer. + * + * @param[in] type Type of the header. + * @param[in] payload Payload for the packet. + * @param[in] src Source address for the header. Can be NULL if not + * known or required. + * @param[in] src_len Length of @p src. Can be 0 if not known or required. + * @param[in] dst Destination address for the header. Can be NULL if not + * known or required. + * @param[in] dst_len Length of @p dst. Can be 0 if not known or required. + * + * @return The header for the protocol on success. + * @return NULL on error. + */ +ng_pktsnip_t *ng_netreg_hdr_build(ng_nettype_t type, ng_pktsnip_t *payload, + uint8_t *src, uint8_t src_len, + uint8_t *dst, uint8_t dst_len); + #ifdef __cplusplus } #endif diff --git a/sys/net/crosslayer/ng_netreg/ng_netreg.c b/sys/net/crosslayer/ng_netreg/ng_netreg.c index 3e3b9216d6..8458b22631 100644 --- a/sys/net/crosslayer/ng_netreg/ng_netreg.c +++ b/sys/net/crosslayer/ng_netreg/ng_netreg.c @@ -19,6 +19,7 @@ #include "utlist.h" #include "net/ng_netreg.h" #include "net/ng_nettype.h" +#include "net/ng_ipv6.h" #define _INVALID_TYPE(type) (((type) < NG_NETTYPE_UNDEF) || ((type) >= NG_NETTYPE_NUMOF)) @@ -101,4 +102,30 @@ ng_netreg_entry_t *ng_netreg_getnext(ng_netreg_entry_t *entry) return entry; } +ng_pktsnip_t *ng_netreg_hdr_build(ng_nettype_t type, ng_pktsnip_t *payload, + uint8_t *src, uint8_t src_len, + uint8_t *dst, uint8_t dst_len) +{ + switch (type) { +#ifdef MODULE_NG_IPV6 + + case NG_NETTYPE_IPV6: + return ng_ipv6_hdr_build(payload, src, src_len, dst, dst_len); +#endif +#ifdef MODULE_NG_TCP + + case NG_NETTYPE_TCP: + return ng_tcp_hdr_build(payload, src, src_len, dst, dst_len); +#endif +#ifdef MODULE_NG_UDP + + case NG_NETTYPE_UDP: + return ng_udp_hdr_build(payload, src, src_len, dst, dst_len); +#endif + + default: + return NULL; + } +} + /** @} */ From 1ea70be0e86b88a2d1bd2835231ad7f5760a16c4 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Sat, 7 Mar 2015 16:53:52 +0100 Subject: [PATCH 2/2] netif: add convinience function for header building --- sys/include/net/ng_netif/hdr.h | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/sys/include/net/ng_netif/hdr.h b/sys/include/net/ng_netif/hdr.h index 09904f15ff..a7510b2a08 100644 --- a/sys/include/net/ng_netif/hdr.h +++ b/sys/include/net/ng_netif/hdr.h @@ -15,6 +15,7 @@ * @brief Generic network interface header * * @author Hauke Petersen + * @author Martine Lenders */ #ifndef NETIF_HDR_H_ @@ -24,6 +25,8 @@ #include #include "kernel.h" +#include "net/ng_pkt.h" +#include "net/ng_pktbuf.h" #ifdef __cplusplus extern "C" { @@ -134,6 +137,44 @@ static inline void ng_netif_hdr_set_dst_addr(ng_netif_hdr_t *hdr, uint8_t *addr, memcpy(((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len, addr, addr_len); } +/** + * @brief Builds a generic network interface header for sending and + * adds it to the packet buffer. + * + * @param[in] src Source address for the header. Can be NULL if not + * known or required. + * @param[in] src_len Length of @p src. Can be 0 if not known or required. + * @param[in] dst Destination address for the header. Can be NULL if not + * known or required. + * @param[in] dst_len Length of @p dst. Can be 0 if not known or required. + * + * @return The generic network layer header on success. + * @return NULL on error. + */ +static inline ng_pktsnip_t *ng_netif_hdr_build(uint8_t *src, uint8_t src_len, + uint8_t *dst, uint8_t dst_len) +{ + ng_pktsnip_t *pkt = ng_pktbuf_add(NULL, NULL, + sizeof(ng_netif_hdr_t) + src_len + dst_len, + NG_NETTYPE_NETIF); + + if (pkt == NULL) { + return NULL; + } + + ng_netif_hdr_init(pkt->data, src_len, dst_len); + + if (src != NULL && src_len > 0) { + ng_netif_hdr_set_src_addr(pkt->data, src, src_len); + } + + if (dst != NULL && dst_len > 0) { + ng_netif_hdr_set_dst_addr(pkt->data, dst, dst_len); + } + + return pkt; +} + #ifdef __cplusplus } #endif