diff --git a/sys/include/net/ng_netreg.h b/sys/include/net/ng_netreg.h index 68e0fa5f8450..85779d0a3bb7 100644 --- a/sys/include/net/ng_netreg.h +++ b/sys/include/net/ng_netreg.h @@ -127,6 +127,22 @@ 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 Calculates the checksum for a header. + * + * @param[in] hdr The header the checksum should be calculated + * for. + * @param[in] pseudo_hdr The header the pseudo header shall be generated + * from. NULL if none is needed. + * + * @return 0, on success. + * @return -EINVAL, if @p pseudo_hdr is NULL but a pseudo header was required. + * @return -ENOENT, if @ref net_netreg does not know how to calculate checksum + * for ng_pktsnip_t::type of @p hdr. + */ + +int ng_netreg_calc_csum(ng_pktsnip_t *hdr, ng_pktsnip_t *pseudo_hdr); + /** * @brief Builds a header for sending and adds it to the packet buffer. * diff --git a/sys/net/crosslayer/ng_netreg/ng_netreg.c b/sys/net/crosslayer/ng_netreg/ng_netreg.c index 8458b2263116..5b5d02f84ba6 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_pkt.h" #include "net/ng_ipv6.h" #define _INVALID_TYPE(type) (((type) < NG_NETTYPE_UNDEF) || ((type) >= NG_NETTYPE_NUMOF)) @@ -102,6 +103,33 @@ ng_netreg_entry_t *ng_netreg_getnext(ng_netreg_entry_t *entry) return entry; } +int ng_netreg_calc_csum(ng_pktsnip_t *hdr, ng_pktsnip_t *pseudo_hdr) +{ + if (pseudo_hdr == NULL) { + /* XXX: Might be allowed for future checksums. + * If this is the case: move this to the branches were it + * is needed. */ + return -EINVAL; + } + + switch (hdr->type) { +#ifdef MODULE_NG_ICMPV6 + case NG_NETTYPE_ICMPV6: + return ng_icmpv6_calc_csum(hdr, pseudo_hdr); +#endif +#ifdef MODULE_NG_TCP + case NG_NETTYPE_TCP: + return ng_tcp_calc_csum(hdr, pseudo_hdr); +#endif +#ifdef MODULE_NG_UDP + case NG_NETTYPE_UDP: + return ng_udp_calc_csum(hdr, pseudo_hdr); +#endif + default: + return -ENOENT; + } +} + 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)