diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/includes/hicn/base.h | 192 | ||||
-rw-r--r-- | lib/includes/hicn/packet.h | 55 | ||||
-rw-r--r-- | lib/src/base.c | 29 | ||||
-rw-r--r-- | lib/src/ops.c | 6 | ||||
-rw-r--r-- | lib/src/ops.h | 119 | ||||
-rw-r--r-- | lib/src/packet.c | 72 | ||||
-rw-r--r-- | lib/src/protocol/ah.c | 6 | ||||
-rw-r--r-- | lib/src/protocol/icmp.c | 6 | ||||
-rw-r--r-- | lib/src/protocol/ipv4.c | 46 | ||||
-rw-r--r-- | lib/src/protocol/ipv6.c | 47 | ||||
-rw-r--r-- | lib/src/protocol/new.c | 14 | ||||
-rw-r--r-- | lib/src/protocol/tcp.c | 37 | ||||
-rw-r--r-- | lib/src/protocol/udp.c | 38 | ||||
-rw-r--r-- | lib/src/test/test_name.cc | 3 | ||||
-rw-r--r-- | lib/src/test/test_udp_header.cc | 4 |
15 files changed, 142 insertions, 532 deletions
diff --git a/lib/includes/hicn/base.h b/lib/includes/hicn/base.h index 50961354b..2c80d42e6 100644 --- a/lib/includes/hicn/base.h +++ b/lib/includes/hicn/base.h @@ -65,59 +65,33 @@ typedef u32 hicn_lifetime_t; * [IPPROTO_ICMPRD, IPPROTO_AH, IPPROTO_ICMP, IPPROTO_IPV6] */ -#define HICN_FORMAT_LEN 4 +typedef uint32_t hicn_packet_format_t; -typedef union -{ - /** protocol layers representation */ - struct - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - u8 l1; /**< First layer */ - u8 l2; /**< Second layer */ - u8 l3; /**< Third layer */ - u8 l4; /**< Fourth layer */ -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - u8 l4; /**< Fourth layer */ - u8 l3; /**< Third layer */ - u8 l2; /**< Second layer */ - u8 l1; /**< First layer */ -#elif _WIN32 /* Windows is assumed little-endian */ - u8 l1; - u8 l2; - u8 l3; - u8 l4; -#else -#error "Unsupported endianness" -#endif - }; - /** u32 representation */ - u32 as_u32; - u8 as_u8[HICN_FORMAT_LEN]; -} hicn_packet_format_t; - -/* Common protocol layers */ /* Common protocol layers */ -#ifndef _WIN32 #define HICN_PACKET_FORMAT(x, y, z, t) \ - (hicn_packet_format_t) \ - { \ + (uint32_t) (((x) << 24) + ((y) << 16) + ((z) << 8) + (t)) + +#define HICN_PACKET_FORMAT_SIZE 4 + +// i = 0..3 +#define HICN_PACKET_FORMAT_GET(format, i) \ + (i < 0 || i > 3) ? IPPROTO_NONE : ((format >> ((3 - (i)) << 3)) & 0xFF) + +#define HICN_PACKET_FORMAT_SET(format, i, val) \ + format = ((val << ((3 - i) << 3)) | \ + (format & (0xFFFFFFFF ^ (0xFF << ((3 - i) << 3))))) + +#define HICN_PACKET_FORMAT_ENUMERATE(FORMAT, POS, PROTOCOL, BODY) \ + for (unsigned POS = 0; POS <= HICN_PACKET_FORMAT_SIZE - 1; POS++) \ { \ - .l1 = x, .l2 = y, .l3 = z, .l4 = t \ - } \ - } -#else -inline const hicn_packet_format_t -HICN_PACKET_FORMAT (int x, int y, int z, int t) -{ - hicn_packet_format_t format; - format.l1 = x; - format.l2 = y; - format.l3 = z; - format.l4 = t; - return format; -} -#endif + uint8_t PROTOCOL = HICN_PACKET_FORMAT_GET (FORMAT, POS); \ + BODY; \ + } + +#define HICN_PACKET_FORMAT_L1(format) HICN_PACKET_FORMAT_L ((format), 0) +#define HICN_PACKET_FORMAT_L2(format) HICN_PACKET_FORMAT_L ((format), 1) +#define HICN_PACKET_FORMAT_L3(format) HICN_PACKET_FORMAT_L ((format), 2) +#define HICN_PACKET_FORMAT_L4(format) HICN_PACKET_FORMAT_L ((format), 3) extern const char *const _protocol_str[]; @@ -132,37 +106,36 @@ int hicn_packet_format_snprintf (char *s, size_t size, #define constexpr const #endif -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_TCP = - HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_ICMP = - HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_NONE, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_TCP = - HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_ICMP = - HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_NONE, - IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_NEW = - HICN_PACKET_FORMAT (IPPROTO_ENCAP, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_UDP = - HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_UDP = - HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_TCP_AH = - HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_ICMP_AH = - HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_AH, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_TCP_AH = - HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_ICMP_AH = - HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_AH, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_NEW_AH = - HICN_PACKET_FORMAT (IPPROTO_ENCAP, IPPROTO_AH, IPPROTO_NONE, IPPROTO_NONE); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_UDP_AH = - HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_UDP_AH = - HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH); -static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_NONE = - HICN_PACKET_FORMAT (IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE); +#define HICN_PACKET_FORMAT_IPV4_TCP \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV4_ICMP \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_TCP \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_ICMP \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_NEW \ + HICN_PACKET_FORMAT (IPPROTO_ENCAP, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV4_UDP \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_UDP \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV4_TCP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV4_ICMP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_TCP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_ICMP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_AH, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_NEW_AH \ + HICN_PACKET_FORMAT (IPPROTO_ENCAP, IPPROTO_AH, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_UDP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH) +#define HICN_PACKET_FORMAT_IPV4_UDP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH) +#define HICN_PACKET_FORMAT_NONE \ + HICN_PACKET_FORMAT (IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) /** * @brief Return the hICN format with an additional AH header @@ -172,21 +145,21 @@ static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_NONE = static inline hicn_packet_format_t hicn_get_ah_format (hicn_packet_format_t format) { - hicn_packet_format_t ret = format; - for (unsigned i = 0; i < HICN_FORMAT_LEN; i++) - { - switch (ret.as_u8[i]) + HICN_PACKET_FORMAT_ENUMERATE (format, i, protocol, { + switch (protocol) + { { case IPPROTO_AH: - return ret; + return format; case IPPROTO_NONE: - ret.as_u8[i] = IPPROTO_AH; - return ret; + HICN_PACKET_FORMAT_SET (format, i, IPPROTO_AH); + return format; default: break; } - } - return ret; + } + }); + return format; } /* @@ -196,27 +169,22 @@ hicn_get_ah_format (hicn_packet_format_t format) */ #define HICN_HDRLEN_MAX 72 -/** - * @brief Check if type is none. - * @return 1 if none, 0 otherwise - */ -static inline int -hicn_type_is_none (hicn_packet_format_t format) -{ - return (format.l1 == IPPROTO_NONE) && (format.l2 == IPPROTO_NONE) && - (format.l3 == IPPROTO_NONE) && (format.l4 == IPPROTO_NONE); -} +#define HICN_PACKET_FORMAT_IS_NONE(format) \ + ((HICN_PACKET_FORMAT_GET (format, 0) == IPPROTO_NONE) && \ + (HICN_PACKET_FORMAT_GET (format, 1) == IPPROTO_NONE) && \ + (HICN_PACKET_FORMAT_GET (format, 2) == IPPROTO_NONE) && \ + (HICN_PACKET_FORMAT_GET (format, 3) == IPPROTO_NONE)) + +#define HICN_PACKET_FORMAT_IS_AH(format) \ + ((HICN_PACKET_FORMAT_GET (format, 0) == IPPROTO_AH) || \ + (HICN_PACKET_FORMAT_GET (format, 1) == IPPROTO_AH) || \ + (HICN_PACKET_FORMAT_GET (format, 2) == IPPROTO_AH) || \ + (HICN_PACKET_FORMAT_GET (format, 3) == IPPROTO_AH)) -#define _is_ipv4(format) (format.l1 == IPPROTO_IP) -#define _is_ipv6(format) (format.l1 == IPPROTO_IPV6) -#define _is_tcp(format) (format.l2 == IPPROTO_TCP) -#define _is_udp(format) (format.l2 == IPPROTO_UDP) -#define _is_icmp(format) \ - ((format.l2 == IPPROTO_ICMP) || (format.l2 == IPPROTO_ICMPV6)) -#define _is_cmpr(format) ((format & HFO_CMPR) >> 5) -#define _is_ah(format) \ - ((format.l1 == IPPROTO_AH) || (format.l2 == IPPROTO_AH) || \ - (format.l3 == IPPROTO_AH)) +#define HICN_PACKET_FORMAT_IS_IPV4(format) \ + (HICN_PACKET_FORMAT_GET (format, 0) == IPPROTO_IP) +#define HICN_PACKET_FORMAT_IS_IPV6(format) \ + (HICN_PACKET_FORMAT_GET (format, 0) == IPPROTO_IPV6) /* * @brief hICN packet types @@ -276,9 +244,9 @@ typedef u8 hicn_path_label_t; /** * @brief Path label computations * - * Path label is computed by accumulating the identifiers of successive output - * faces as a Data packet is traveling from its producer back to the consumer - * originating the Interest. + * Path label is computed by accumulating the identifiers of successive + * output faces as a Data packet is traveling from its producer back to the + * consumer originating the Interest. * * NOTE: this computation is not (yet) part of the hICN specification. */ diff --git a/lib/includes/hicn/packet.h b/lib/includes/hicn/packet.h index 85f76d21b..c4a0dd80e 100644 --- a/lib/includes/hicn/packet.h +++ b/lib/includes/hicn/packet.h @@ -233,6 +233,10 @@ void hicn_packet_set_format (hicn_packet_buffer_t *pkbuf, hicn_packet_format_t format); hicn_packet_type_t hicn_packet_get_type (const hicn_packet_buffer_t *pkbuf); + +void hicn_packet_initialize_type (hicn_packet_buffer_t *pkbuf, + hicn_packet_type_t type); + void hicn_packet_set_type (hicn_packet_buffer_t *pkbuf, hicn_packet_type_t type); @@ -644,57 +648,6 @@ int hicn_packet_compute_header_checksum (const hicn_packet_buffer_t *pkbuf, int hicn_packet_check_integrity_no_payload (const hicn_packet_buffer_t *pkbuf, u16 init_sum); -/** - * @brief Returns the packet TTL - * @param [in] pkbuf - hICN packet buffer - * @param [out] hops - Pointer to the variable receiving the TTL value - * @return hICN error code - */ -int hicn_packet_get_ttl (const hicn_packet_buffer_t *pkbuf, u8 *hops); - -/** - * @brief Returns the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [in, out] pos - Current position in the sequence of headers while - * @param [out] hops - The TTL value to set - * @return hICN error code - */ -int hicn_packet_set_ttl (const hicn_packet_buffer_t *pkbuf, u8 hops); - -/** - * @brief Returns the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [out] port - Pointer to the variable that will receive the port - * number - * @return hICN error code - */ -int hicn_packet_get_src_port (const hicn_packet_buffer_t *pkbuf, u16 *port); - -/** - * @brief Sets the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [out] port - The port number to set - * @return hICN error code - */ -int hicn_packet_set_src_port (const hicn_packet_buffer_t *pkbuf, u16 port); - -/** - * @brief Returns the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [out] port - Pointer to the variable that will receive the port - * number - * @return hICN error code - */ -int hicn_packet_get_dst_port (const hicn_packet_buffer_t *pkbuf, u16 *port); - -/** - * @brief Sets the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [out] port - The port number to set - * @return hICN error code - */ -int hicn_packet_set_dst_port (const hicn_packet_buffer_t *pkbuf, u16 port); - int hicn_interest_rewrite (const hicn_packet_buffer_t *pkbuf, const hicn_ip_address_t *addr_new, hicn_ip_address_t *addr_old); diff --git a/lib/src/base.c b/lib/src/base.c index a15d55938..8c689da50 100644 --- a/lib/src/base.c +++ b/lib/src/base.c @@ -32,21 +32,20 @@ hicn_packet_format_snprintf (char *s, size_t size, hicn_packet_format_t format) { char *cur = s; int rc; - for (unsigned i = 0; i < 4; i++) - { - if (i > 0) - { - rc = snprintf (cur, size - (cur - s), " %s ", "/"); - if (rc < 0 || rc >= size - (cur - s)) - return rc; - cur += rc; - } - rc = snprintf (cur, size - (cur - s), "%s", - hicn_ops_vft[format.as_u8[i]]->name); - if (rc < 0 || rc >= size - (cur - s)) - return rc; - cur += rc; - } + HICN_PACKET_FORMAT_ENUMERATE (format, i, protocol, { + if (i > 1) + { + rc = snprintf (cur, size - (cur - s), " %s ", "/"); + if (rc < 0 || rc >= size - (cur - s)) + return rc; + cur += rc; + } + + rc = snprintf (cur, size - (cur - s), "%s", hicn_ops_vft[protocol]->name); + if (rc < 0 || rc >= size - (cur - s)) + return rc; + cur += rc; + }); return (int) (cur - s); } diff --git a/lib/src/ops.c b/lib/src/ops.c index 0174a3902..2c38b5374 100644 --- a/lib/src/ops.c +++ b/lib/src/ops.c @@ -86,12 +86,6 @@ DECLARE_get_signature_padding (none, NONE); DECLARE_set_signature_padding (none, NONE); DECLARE_is_last_data (none, NONE); DECLARE_set_last_data (none, NONE); -DECLARE_get_ttl (none, NONE); -DECLARE_set_ttl (none, NONE); -DECLARE_get_src_port (none, NONE); -DECLARE_set_src_port (none, NONE); -DECLARE_get_dst_port (none, NONE); -DECLARE_set_dst_port (none, NONE); DECLARE_HICN_OPS (none, 0); /** diff --git a/lib/src/ops.h b/lib/src/ops.h index 886d75cd5..843945690 100644 --- a/lib/src/ops.h +++ b/lib/src/ops.h @@ -554,65 +554,6 @@ typedef struct hicn_ops_s */ int (*set_last_data) (const hicn_packet_buffer_t *pkbuf, size_t pos); - /** - * @brief Returns the packet TTL - * @param [in] pkbuf - hICN packet buffer - * @param [in, out] pos - Current position in the sequence of headers while - * @param [out] hops - Pointer to the variable receiving the TTL value - * @return hICN error code - */ - int (*get_ttl) (const hicn_packet_buffer_t *pkbuf, size_t pos, u8 *hops); - - /** - * @brief Returns the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [in, out] pos - Current position in the sequence of headers while - * @param [out] hops - The TTL value to set - * @return hICN error code - */ - int (*set_ttl) (const hicn_packet_buffer_t *pkbuf, size_t pos, u8 hops); - - /** - * @brief Returns the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [in, out] pos - Current position in the sequence of headers while - * @param [out] port - Pointer to the variable that will receive the port - * number - * @return hICN error code - */ - int (*get_src_port) (const hicn_packet_buffer_t *pkbuf, size_t pos, - u16 *port); - - /** - * @brief Sets the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [in, out] pos - Current position in the sequence of headers while - * @param [out] port - The port number to set - * @return hICN error code - */ - int (*set_src_port) (const hicn_packet_buffer_t *pkbuf, size_t pos, - u16 port); - - /** - * @brief Returns the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [in, out] pos - Current position in the sequence of headers while - * @param [out] port - Pointer to the variable that will receive the port - * number - * @return hICN error code - */ - int (*get_dst_port) (const hicn_packet_buffer_t *pkbuf, size_t pos, - u16 *port); - - /** - * @brief Sets the packet source port - * @param [in] pkbuf - hICN packet buffer - * @param [in, out] pos - Current position in the sequence of headers while - * @param [out] port - The port number to set - * @return hICN error code - */ - int (*set_dst_port) (const hicn_packet_buffer_t *pkbuf, size_t pos, - u16 port); } hicn_ops_t; #define DECLARE_HICN_OPS(protocol, len) \ @@ -667,12 +608,6 @@ typedef struct hicn_ops_s ATTR_INIT (set_signature_size, protocol##_set_signature_size), \ ATTR_INIT (get_signature_padding, protocol##_get_signature_padding), \ ATTR_INIT (is_last_data, protocol##_is_last_data), \ - ATTR_INIT (get_ttl, protocol##_get_ttl), \ - ATTR_INIT (set_ttl, protocol##_set_ttl), \ - ATTR_INIT (get_src_port, protocol##_get_src_port), \ - ATTR_INIT (set_src_port, protocol##_set_src_port), \ - ATTR_INIT (get_dst_port, protocol##_get_dst_port), \ - ATTR_INIT (set_dst_port, protocol##_set_dst_port), \ } /** @@ -681,10 +616,14 @@ typedef struct hicn_ops_s */ extern const hicn_ops_t *const hicn_ops_vft[]; -#define PROT(pkbuf, pos) \ - ((pos < (HICN_FORMAT_LEN - 1)) ? \ - hicn_packet_get_format (pkbuf).as_u8[(pos) + 1] : \ - IPPROTO_NONE) +static inline uint8_t +PROT (const hicn_packet_buffer_t *pkbuf, int pos) +{ + if (pos < -1 || pos >= HICN_PACKET_FORMAT_SIZE - 1) + return IPPROTO_NONE; + hicn_packet_format_t format = hicn_packet_get_format (pkbuf); + return HICN_PACKET_FORMAT_GET (format, pos + 1); +} #define CALL_CHILD(method, pkbuf, pos, ...) \ hicn_ops_vft[PROT (pkbuf, (pos))]->method (pkbuf, (pos) + 1, ##__VA_ARGS__); @@ -1029,48 +968,6 @@ extern const hicn_ops_t *const hicn_ops_vft[]; return HICN_LIB_ERROR_##error; \ } -#define DECLARE_get_ttl(protocol, error) \ - int protocol##_get_ttl (const hicn_packet_buffer_t *pkbuf, size_t pos, \ - u8 *hops) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_ttl(protocol, error) \ - int protocol##_set_ttl (const hicn_packet_buffer_t *pkbuf, size_t pos, \ - u8 hops) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_src_port(protocol, error) \ - int protocol##_get_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, \ - u16 *port) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_src_port(protocol, error) \ - int protocol##_set_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, \ - u16 port) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_dst_port(protocol, error) \ - int protocol##_get_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, \ - u16 *port) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_dst_port(protocol, error) \ - int protocol##_set_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, \ - u16 port) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - #endif /* HICN_OPS_H */ /* diff --git a/lib/src/packet.c b/lib/src/packet.c index f020e099c..bebfad23e 100644 --- a/lib/src/packet.c +++ b/lib/src/packet.c @@ -55,6 +55,15 @@ hicn_packet_get_type (const hicn_packet_buffer_t *pkbuf) } void +hicn_packet_initialize_type (hicn_packet_buffer_t *pkbuf, + hicn_packet_type_t type) +{ + assert (pkbuf->format != HICN_PACKET_FORMAT_NONE); + pkbuf->type = type; + CALL (set_type, pkbuf, type); +} + +void hicn_packet_set_type (hicn_packet_buffer_t *pkbuf, hicn_packet_type_t type) { pkbuf->type = type; @@ -84,7 +93,7 @@ hicn_packet_init_header (hicn_packet_buffer_t *pkbuf, { if (hicn_packet_is_undefined (pkbuf)) return HICN_LIB_ERROR_UNEXPECTED; - if (hicn_packet_get_format (pkbuf).as_u32 == HICN_PACKET_FORMAT_NONE.as_u32) + if (hicn_packet_get_format (pkbuf) == HICN_PACKET_FORMAT_NONE) return HICN_LIB_ERROR_UNEXPECTED; if (!pkbuf_get_header (pkbuf)) return HICN_LIB_ERROR_UNEXPECTED; @@ -124,8 +133,6 @@ hicn_packet_analyze (hicn_packet_buffer_t *pkbuf) size_t signature_size; int rc; - hicn_packet_format_t *format = &pkbuf->format; - /* Bootstrap: assume IP packet, and get version from header */ switch (HICN_IP_VERSION (pkbuf_get_header (pkbuf))) { @@ -135,14 +142,17 @@ hicn_packet_analyze (hicn_packet_buffer_t *pkbuf) case 6: protocol = IPPROTO_IPV6; break; + case 9: + protocol = IPPROTO_ENCAP; // new + break; default: goto ERR; } - format->as_u32 = 0; - for (unsigned i = 0; i < HICN_FORMAT_LEN; i++) + hicn_packet_format_t *format = &pkbuf->format; + for (unsigned i = 0; i < HICN_PACKET_FORMAT_SIZE; i++) { - format->as_u8[i] = protocol; + HICN_PACKET_FORMAT_SET (*format, i, protocol); /* Next protocol + increment offset */ switch (protocol) @@ -257,7 +267,7 @@ hicn_packet_analyze (hicn_packet_buffer_t *pkbuf) return HICN_LIB_ERROR_NONE; ERR: - *format = HICN_PACKET_FORMAT_NONE; + pkbuf->format = HICN_PACKET_FORMAT_NONE; pkbuf->type = HICN_PACKET_TYPE_UNDEFINED; return HICN_LIB_ERROR_UNEXPECTED; } @@ -346,10 +356,9 @@ hicn_packet_get_header_length_from_format (hicn_packet_format_t format, size_t *header_length) { *header_length = 0; - for (unsigned i = 0; i < HICN_FORMAT_LEN; i++) - { - *header_length += hicn_ops_vft[format.as_u8[i]]->header_len; - } + HICN_PACKET_FORMAT_ENUMERATE (format, i, protocol, { + *header_length += hicn_ops_vft[protocol]->header_len; + }); return HICN_LIB_ERROR_NONE; } @@ -385,11 +394,10 @@ int hicn_packet_compare (const hicn_packet_buffer_t *pkbuf1, const hicn_packet_buffer_t *pkbuf2) { - hicn_packet_format_t format1 = hicn_packet_get_format (pkbuf1); hicn_packet_format_t format2 = hicn_packet_get_format (pkbuf2); - if (format1.as_u32 != format2.as_u32) + if (format1 != format2) return HICN_LIB_ERROR_UNEXPECTED; size_t len1 = hicn_packet_get_len (pkbuf1); @@ -563,7 +571,7 @@ hicn_packet_save_header (const hicn_packet_buffer_t *pkbuf, u8 *header, size_t *header_len, bool copy_ah) { hicn_packet_format_t format = hicn_packet_get_format (pkbuf); - if (copy_ah || !_is_ah (format)) + if (copy_ah || !HICN_PACKET_FORMAT_IS_AH (format)) { int rc = hicn_packet_get_header_len (pkbuf, header_len); if (HICN_LIB_IS_ERROR (rc)) @@ -773,42 +781,6 @@ hicn_packet_get_signature (const hicn_packet_buffer_t *pkbuf, } int -hicn_packet_get_ttl (const hicn_packet_buffer_t *pkbuf, u8 *hops) -{ - return CALL (get_ttl, pkbuf, hops); -} - -int -hicn_packet_set_ttl (const hicn_packet_buffer_t *pkbuf, u8 hops) -{ - return CALL (set_ttl, pkbuf, hops); -} - -int -hicn_packet_get_src_port (const hicn_packet_buffer_t *pkbuf, u16 *port) -{ - return CALL (get_src_port, pkbuf, port); -} - -int -hicn_packet_set_src_port (const hicn_packet_buffer_t *pkbuf, u16 port) -{ - return CALL (set_src_port, pkbuf, port); -} - -int -hicn_packet_get_dst_port (const hicn_packet_buffer_t *pkbuf, u16 *port) -{ - return CALL (get_dst_port, pkbuf, port); -} - -int -hicn_packet_set_dst_port (const hicn_packet_buffer_t *pkbuf, u16 port) -{ - return CALL (set_dst_port, pkbuf, port); -} - -int hicn_interest_rewrite (const hicn_packet_buffer_t *pkbuf, const hicn_ip_address_t *addr_new, hicn_ip_address_t *addr_old) diff --git a/lib/src/protocol/ah.c b/lib/src/protocol/ah.c index 645b0482b..ea028d559 100644 --- a/lib/src/protocol/ah.c +++ b/lib/src/protocol/ah.c @@ -50,12 +50,6 @@ DECLARE_get_payload_type (ah, UNEXPECTED); DECLARE_set_payload_type (ah, UNEXPECTED); DECLARE_is_last_data (ah, UNEXPECTED); DECLARE_set_last_data (ah, UNEXPECTED); -DECLARE_get_ttl (ah, UNEXPECTED); -DECLARE_set_ttl (ah, UNEXPECTED); -DECLARE_get_src_port (ah, UNEXPECTED); -DECLARE_set_src_port (ah, UNEXPECTED); -DECLARE_get_dst_port (ah, UNEXPECTED); -DECLARE_set_dst_port (ah, UNEXPECTED); int ah_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) diff --git a/lib/src/protocol/icmp.c b/lib/src/protocol/icmp.c index 71417a4e0..5ee70f088 100644 --- a/lib/src/protocol/icmp.c +++ b/lib/src/protocol/icmp.c @@ -44,12 +44,6 @@ DECLARE_get_signature (icmp, UNEXPECTED); DECLARE_has_signature (icmp, UNEXPECTED); DECLARE_is_last_data (icmp, UNEXPECTED); DECLARE_set_last_data (icmp, UNEXPECTED); -DECLARE_get_ttl (icmp, UNEXPECTED); -DECLARE_set_ttl (icmp, UNEXPECTED); -DECLARE_get_src_port (icmp, UNEXPECTED); -DECLARE_set_src_port (icmp, UNEXPECTED); -DECLARE_get_dst_port (icmp, UNEXPECTED); -DECLARE_set_dst_port (icmp, UNEXPECTED); int icmp_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) diff --git a/lib/src/protocol/ipv4.c b/lib/src/protocol/ipv4.c index a13728d04..daa5a706a 100644 --- a/lib/src/protocol/ipv4.c +++ b/lib/src/protocol/ipv4.c @@ -60,7 +60,7 @@ ipv4_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) .id = htons (IPV4_DEFAULT_ID), .frag_off = htons (IPV4_DEFAULT_FRAG_OFF), .ttl = HICN_DEFAULT_TTL, - .protocol = format.as_u8[pos + 1], + .protocol = HICN_PACKET_FORMAT_GET (format, pos + 1), .csum = 0, .saddr.as_u32 = 0, .daddr.as_u32 = 0, @@ -530,50 +530,6 @@ ipv4_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) return CALL_CHILD (set_last_data, pkbuf, pos); } -int -ipv4_get_ttl (const hicn_packet_buffer_t *pkbuf, size_t pos, u8 *hops) -{ - _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); - - *hops = ipv4->ttl; - - return HICN_LIB_ERROR_NONE; -} - -int -ipv4_set_ttl (const hicn_packet_buffer_t *pkbuf, size_t pos, u8 hops) -{ - _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); - - ipv4->ttl = hops; - - return HICN_LIB_ERROR_NONE; -} - -int -ipv4_get_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *port) -{ - return CALL_CHILD (get_src_port, pkbuf, pos, port); -} - -int -ipv4_set_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 port) -{ - return CALL_CHILD (set_src_port, pkbuf, pos, port); -} - -int -ipv4_get_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *port) -{ - return CALL_CHILD (get_dst_port, pkbuf, pos, port); -} - -int -ipv4_set_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 port) -{ - return CALL_CHILD (set_dst_port, pkbuf, pos, port); -} - DECLARE_HICN_OPS (ipv4, IPV4_HDRLEN); /* diff --git a/lib/src/protocol/ipv6.c b/lib/src/protocol/ipv6.c index aec521afc..c5fa643aa 100644 --- a/lib/src/protocol/ipv6.c +++ b/lib/src/protocol/ipv6.c @@ -53,7 +53,7 @@ ipv6_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) (IPV6_DEFAULT_TRAFFIC_CLASS << 20) | (IPV6_DEFAULT_FLOW_LABEL & 0xfffff)), .len = htons(header_len - IPV6_HDRLEN), - .nxt = format.as_u8[pos + 1], + .nxt = HICN_PACKET_FORMAT_GET(format, pos+1), .hlim = HICN_DEFAULT_TTL, }; /* clang-format on */ @@ -468,51 +468,6 @@ ipv6_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) return CALL_CHILD (set_last_data, pkbuf, pos); } -int -ipv6_get_ttl (const hicn_packet_buffer_t *pkbuf, size_t pos, u8 *hops) -{ - _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); - - *hops = ipv6->hlim; - - return HICN_LIB_ERROR_NONE; -} - -int -ipv6_set_ttl (const hicn_packet_buffer_t *pkbuf, size_t pos, u8 hops) -{ - - _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); - - ipv6->hlim = hops; - - return HICN_LIB_ERROR_NONE; -} - -int -ipv6_get_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *port) -{ - return CALL_CHILD (get_src_port, pkbuf, pos, port); -} - -int -ipv6_set_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 port) -{ - return CALL_CHILD (set_src_port, pkbuf, pos, port); -} - -int -ipv6_get_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *port) -{ - return CALL_CHILD (get_dst_port, pkbuf, pos, port); -} - -int -ipv6_set_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 port) -{ - return CALL_CHILD (set_dst_port, pkbuf, pos, port); -} - DECLARE_HICN_OPS (ipv6, IPV6_HDRLEN); /* diff --git a/lib/src/protocol/new.c b/lib/src/protocol/new.c index 5308e8c0a..e86ce4753 100644 --- a/lib/src/protocol/new.c +++ b/lib/src/protocol/new.c @@ -24,13 +24,6 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" -DECLARE_get_ttl (new, UNEXPECTED); -DECLARE_set_ttl (new, UNEXPECTED); -DECLARE_get_src_port (new, UNEXPECTED); -DECLARE_set_src_port (new, UNEXPECTED); -DECLARE_get_dst_port (new, UNEXPECTED); -DECLARE_set_dst_port (new, UNEXPECTED); - int new_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) { @@ -45,8 +38,9 @@ new_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) memset (new, 0, sizeof (_new_header_t)); _set_new_header_version (new); - uint8_t ah_flag = - format.as_u8[pos + 1] == IPPROTO_AH ? HICN_NEW_FLAG_SIG : 0; + uint8_t ah_flag = (HICN_PACKET_FORMAT_GET (format, pos + 1) == IPPROTO_AH) ? + HICN_NEW_FLAG_SIG : + 0; new->flags |= ah_flag; return CALL_CHILD (init_packet_header, pkbuf, pos); @@ -128,6 +122,8 @@ new_get_interest_name (const hicn_packet_buffer_t *pkbuf, size_t pos, return HICN_LIB_ERROR_NONE; } +// XXX never called and thus packet is never initialized as interest +// DECLARE_set_interest_name (udp, UNEXPECTED); int new_set_interest_name (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_name_t *name) diff --git a/lib/src/protocol/tcp.c b/lib/src/protocol/tcp.c index 822bd3e0c..e09495ecd 100644 --- a/lib/src/protocol/tcp.c +++ b/lib/src/protocol/tcp.c @@ -46,8 +46,6 @@ DECLARE_set_data_locator (tcp, UNEXPECTED); DECLARE_get_data_name (tcp, UNEXPECTED); DECLARE_set_data_name (tcp, UNEXPECTED); DECLARE_set_payload_len (tcp, UNEXPECTED); -DECLARE_get_ttl (tcp, UNEXPECTED); -DECLARE_set_ttl (tcp, UNEXPECTED); int tcp_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *old_val, u16 *new_val, @@ -115,7 +113,8 @@ tcp_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) .urg_ptr = 65000, }; - uint8_t ah_flag = ((format.as_u8[pos + 1] == IPPROTO_AH) ? AH_FLAG : 0); + uint8_t ah_flag = + (HICN_PACKET_FORMAT_GET (format, pos + 1) == IPPROTO_AH) ? AH_FLAG : 0; tcp->flags |= ah_flag; @@ -590,38 +589,6 @@ tcp_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) return HICN_LIB_ERROR_NONE; } -int -tcp_get_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *port) -{ - _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); - *port = ntohs (tcp->sport); - return HICN_LIB_ERROR_NONE; -} - -int -tcp_set_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 port) -{ - _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); - tcp->sport = htons (port); - return HICN_LIB_ERROR_NONE; -} - -int -tcp_get_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *port) -{ - _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); - *port = ntohs (tcp->dport); - return HICN_LIB_ERROR_NONE; -} - -int -tcp_set_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 port) -{ - _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); - tcp->dport = htons (port); - return HICN_LIB_ERROR_NONE; -} - DECLARE_HICN_OPS (tcp, TCP_HDRLEN); /* diff --git a/lib/src/protocol/udp.c b/lib/src/protocol/udp.c index ff2355b0c..56de0065f 100644 --- a/lib/src/protocol/udp.c +++ b/lib/src/protocol/udp.c @@ -30,8 +30,6 @@ DECLARE_set_data_locator (udp, UNEXPECTED); DECLARE_get_data_name (udp, UNEXPECTED); DECLARE_set_data_name (udp, UNEXPECTED); DECLARE_set_payload_len (udp, UNEXPECTED); -DECLARE_get_ttl (udp, UNEXPECTED); -DECLARE_set_ttl (udp, UNEXPECTED); int udp_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) @@ -299,42 +297,6 @@ udp_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) return CALL_CHILD (set_last_data, pkbuf, pos); } -int -udp_get_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *port) -{ - _udp_header_t *udp = pkbuf_get_udp (pkbuf); - - *port = udp->src_port; - return HICN_LIB_ERROR_NONE; -} - -int -udp_set_src_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 port) -{ - _udp_header_t *udp = pkbuf_get_udp (pkbuf); - - udp->src_port = port; - return HICN_LIB_ERROR_NONE; -} - -int -udp_get_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *port) -{ - _udp_header_t *udp = pkbuf_get_udp (pkbuf); - - *port = udp->dst_port; - return HICN_LIB_ERROR_NONE; -} - -int -udp_set_dst_port (const hicn_packet_buffer_t *pkbuf, size_t pos, u16 port) -{ - _udp_header_t *udp = pkbuf_get_udp (pkbuf); - - udp->dst_port = port; - return HICN_LIB_ERROR_NONE; -} - DECLARE_HICN_OPS (udp, UDP_HDRLEN); /* diff --git a/lib/src/test/test_name.cc b/lib/src/test/test_name.cc index cd9ff2d4d..0cf160f70 100644 --- a/lib/src/test/test_name.cc +++ b/lib/src/test/test_name.cc @@ -395,4 +395,7 @@ TEST_F (PrefixTest, PrefixLPM) HICN_PREFIX (b009, "b009::/64"); EXPECT_EQ (hicn_prefix_lpm (&b007, &b009), (uint32_t) 12); + + HICN_PREFIX (pfx, "1122:3344:5566:7788:9900:aabb:ccdd:eeff/128"); + EXPECT_EQ (hicn_prefix_lpm (&pfx, &pfx), (uint32_t) 128); } diff --git a/lib/src/test/test_udp_header.cc b/lib/src/test/test_udp_header.cc index 2853ee31b..2856c8ebf 100644 --- a/lib/src/test/test_udp_header.cc +++ b/lib/src/test/test_udp_header.cc @@ -125,7 +125,7 @@ protected: TEST_F (UdpHeaderTest, GetFormat) { hicn_packet_format_t format = hicn_packet_get_format (&pkbuf_); - EXPECT_EQ (format.as_u32, HICN_PACKET_FORMAT_IPV6_UDP.as_u32); + EXPECT_EQ (format, HICN_PACKET_FORMAT_IPV6_UDP); } TEST_F (UdpHeaderAHTest, GetFormat) @@ -134,7 +134,7 @@ TEST_F (UdpHeaderAHTest, GetFormat) hicn_packet_format_t format = hicn_packet_get_format (&pkbuf_); // Check it corresponds to the new header format - EXPECT_EQ (format.as_u32, HICN_PACKET_FORMAT_IPV6_UDP_AH.as_u32); + EXPECT_EQ (format, HICN_PACKET_FORMAT_IPV6_UDP_AH); } #if 0 |