From fa59e3122cee5e5682b06d52aacf2f4579fde32a Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Thu, 12 Mar 2015 11:15:23 +0100 Subject: [PATCH] ng_nettype: add translation capabilities to ethertype and protnum --- sys/include/net/ng_nettype.h | 121 +++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/sys/include/net/ng_nettype.h b/sys/include/net/ng_nettype.h index 4e8fda8ff2..d5364c1852 100644 --- a/sys/include/net/ng_nettype.h +++ b/sys/include/net/ng_nettype.h @@ -22,6 +22,11 @@ #ifndef NG_NETTYPE_H_ #define NG_NETTYPE_H_ +#include + +#include "net/ng_ethertype.h" +#include "net/ng_protnum.h" + #ifdef __cplusplus extern "C" { #endif @@ -78,6 +83,122 @@ typedef enum { NG_NETTYPE_NUMOF, /**< maximum number of available protocols */ } ng_nettype_t; +/** + * @brief Translates an Ether Type number to @ref net_ng_nettype + * @see + * IANA, ETHER TYPES + * + * + * @param[in] type An Ether Type number + * + * @return The corresponding @ref net_ng_nettype to @p type. + * @return @ref NG_NETTYPE_UNDEF if @p type not translatable. + */ +static inline ng_nettype_t ng_nettype_from_ethertype(uint16_t type) +{ + switch (type) { +#ifdef MODULE_NG_IPV6 + case NG_ETHERTYPE_IPV6: + return NG_NETTYPE_IPV6; +#endif + default: + return NG_NETTYPE_UNDEF; + } +} + +/** + * @brief Translates @ref net_ng_nettype to an Ether Type number + * @see + * IANA, ETHER TYPES + * + * + * @param[in] type A protocol type + * + * @return The corresponding Ether Type number to @p type. + * @return @ref NG_ETHERTYPE_RESERVED if @p type not translatable. + */ +static inline uint16_t ng_nettype_to_ethertype(ng_nettype_t type) +{ + switch (type) { +#ifdef MODULE_NG_IPV6 + case NG_NETTYPE_IPV6: + return NG_ETHERTYPE_IPV6; +#endif + default: + return NG_ETHERTYPE_RESERVED; + } +} + +/** + * @brief Translates a Protocol Number to @ref net_ng_nettype + * @see + * IANA, Assigned Internet Protocol Numbers + * + * + * @param[in] num A Protocol Number + * + * @return The corresponding @ref net_ng_nettype to @p num. + * @return @ref NG_NETTYPE_UNDEF if @p num not translatable. + */ +static inline ng_nettype_t ng_nettype_from_protnum(uint8_t num) +{ + switch (num) { +#ifdef MODULE_NG_ICMPV6 + case NG_PROTNUM_ICMPV6: + return NG_NETTYPE_IPCMPV6; +#endif +#ifdef MODULE_NG_IPV6 + case NG_PROTNUM_IPV6: + return NG_NETTYPE_IPV6; +#endif +#ifdef MODULE_NG_TCP + case NG_PROTNUM_TCP: + return NG_NETTYPE_TCP; +#endif +#ifdef MODULE_NG_UDP + case NG_PROTNUM_UDP: + return NG_NETTYPE_UDP; +#endif + default: + return NG_NETTYPE_UNDEF; + } +} + +/** + * @brief Translates @ref net_ng_nettype to a Protocol Number + * @see + * IANA, Assigned Internet Protocol Numbers + * + * + * @param[in] type A protocol type + * + * @return The corresponding Protocol Number to @p type. + * @return @ref NG_PROTNUM_RESERVED if @p type not translatable. + */ +static inline uint8_t ng_nettype_to_protnum(ng_nettype_t type) +{ + switch (type) { +#ifdef MODULE_NG_ICMPV6 + case NG_NETTYPE_ICMPV6: + return NG_PROTNUM_IPCMPV6; +#endif +#ifdef MODULE_NG_IPV6 + case NG_NETTYPE_IPV6: + return NG_PROTNUM_IPV6; +#endif +#ifdef MODULE_NG_TCP + case NG_NETTYPE_TCP: + return NG_PROTNUM_TCP; +#endif +#ifdef MODULE_NG_UDP + case NG_NETTYPE_UDP: + return NG_PROTNUM_UDP; +#endif + default: + return NG_PROTNUM_RESERVED; + } +} + #ifdef __cplusplus } #endif