diff --git a/src/eviction_filter/eviction_filter.cpp b/src/eviction_filter/eviction_filter.cpp index a54ed7b..845c4a4 100644 --- a/src/eviction_filter/eviction_filter.cpp +++ b/src/eviction_filter/eviction_filter.cpp @@ -4,7 +4,7 @@ #include "timestamp.h" #include "dablooms.h" -#include "udp_helpers.h" +#include "udp_utils.h" #include "ipv4_helpers.h" #include "ipv6_helpers.h" #include "eviction_filter.h" @@ -12,8 +12,8 @@ struct eviction_filter_key { // UDP - uint16_t src_port; /* network order */ - uint16_t dst_port; /* network order */ + uint16_t src_port; /* host order */ + uint16_t dst_port; /* host order */ // IPv4 or IPv6 union ip_address src_addr; /* network order */ @@ -77,8 +77,8 @@ static inline int packet_get_eviction_filter_key(const struct packet *packet, st } const struct udphdr *udphdr = (const struct udphdr *)l4_layer->hdr_ptr; - key->src_port = udp_hdr_get_net_order_sport(udphdr); - key->dst_port = udp_hdr_get_net_order_dport(udphdr); + key->src_port = udp_hdr_get_src_port(udphdr); + key->dst_port = udp_hdr_get_dst_port(udphdr); return 0; } diff --git a/src/packet/packet.cpp b/src/packet/packet.cpp index 7034120..c640a60 100644 --- a/src/packet/packet.cpp +++ b/src/packet/packet.cpp @@ -10,6 +10,7 @@ #include "uthash.h" #include "packet.h" +#include "udp_utils.h" #define likely(expr) __builtin_expect((expr), 1) #define unlikely(expr) __builtin_expect((expr), 0) @@ -865,6 +866,7 @@ static inline const char *parse_ipv4(struct packet *handler, const char *data, u if ((ntohs(hdr->ip_off) & IP_MF) || (ntohs(hdr->ip_off) & IP_OFFMASK)) { PACKET_LOG_DEBUG("ip is fragmented"); + handler->frag_layer = layer; return layer->pld_ptr; } @@ -888,6 +890,8 @@ static inline const char *parse_ipv6(struct packet *handler, const char *data, u uint8_t next_proto = ((struct ip6_hdr *)data)->ip6_nxt; SET_LAYER(handler, layer, LAYER_TYPE_IPV6, sizeof(struct ip6_hdr), data, len); + // TODO ipv6 fragment + // TESTED return parse_l4(handler, next_proto, layer->pld_ptr, layer->pld_len); } @@ -945,7 +949,7 @@ static inline const char *parse_udp(struct packet *handler, const char *data, ui struct udphdr *hdr = (struct udphdr *)data; SET_LAYER(handler, layer, LAYER_TYPE_UDP, sizeof(struct udphdr), data, len); - switch (ntohs(hdr->uh_dport)) + switch (udp_hdr_get_dst_port(hdr)) { // TESTED // VXLAN_DPORT @@ -1096,6 +1100,7 @@ static inline const char *parse_l4(struct packet *handler, uint8_t next_proto, c // return innermost payload const char *packet_parse(struct packet *handler, const char *data, uint16_t len) { + handler->frag_layer = NULL; handler->layers_used = 0; handler->layers_size = PACKET_MAX_LAYERS; handler->data_ptr = data; diff --git a/src/packet/packet.h b/src/packet/packet.h index 584c28a..ec61ac1 100644 --- a/src/packet/packet.h +++ b/src/packet/packet.h @@ -74,6 +74,7 @@ struct layer_record struct packet { struct layer_record layers[PACKET_MAX_LAYERS]; + struct layer_record *frag_layer; // fragment layer int8_t layers_used; int8_t layers_size; diff --git a/src/packet/test/CMakeLists.txt b/src/packet/test/CMakeLists.txt index 43de4df..798f514 100644 --- a/src/packet/test/CMakeLists.txt +++ b/src/packet/test/CMakeLists.txt @@ -5,8 +5,8 @@ add_executable(gtest_packet gtest_packet.cpp) target_link_libraries(gtest_packet packet gtest) -add_executable(gtest_udp_helpers gtest_udp_helpers.cpp) -target_link_libraries(gtest_udp_helpers packet gtest) +add_executable(gtest_udp_utils gtest_udp_utils.cpp) +target_link_libraries(gtest_udp_utils packet gtest) add_executable(gtest_tcp_helpers gtest_tcp_helpers.cpp) target_link_libraries(gtest_tcp_helpers packet gtest) @@ -22,7 +22,7 @@ target_link_libraries(gtest_packet_helpers packet gtest) include(GoogleTest) gtest_discover_tests(gtest_packet) -gtest_discover_tests(gtest_udp_helpers) +gtest_discover_tests(gtest_udp_utils) gtest_discover_tests(gtest_tcp_helpers) gtest_discover_tests(gtest_ipv4_helpers) gtest_discover_tests(gtest_ipv6_helpers) diff --git a/src/packet/test/gtest_udp_helpers.cpp b/src/packet/test/gtest_udp_utils.cpp similarity index 53% rename from src/packet/test/gtest_udp_helpers.cpp rename to src/packet/test/gtest_udp_utils.cpp index 000ec73..62dbc81 100644 --- a/src/packet/test/gtest_udp_helpers.cpp +++ b/src/packet/test/gtest_udp_utils.cpp @@ -1,6 +1,6 @@ #include -#include "udp_helpers.h" +#include "udp_utils.h" /* * User Datagram Protocol, Src Port: 4001, Dst Port: 8000 @@ -14,15 +14,28 @@ unsigned char data[] = {0x0f, 0xa1, 0x1f, 0x40, 0x00, 0x9b, 0x1e, 0x1e}; -TEST(UDP_HELPERS, TEST) +TEST(UDP_UTILS, GET) { const struct udphdr *hdr = (struct udphdr *)data; - EXPECT_TRUE(udp_hdr_get_host_order_sport(hdr) == 4001); - EXPECT_TRUE(udp_hdr_get_host_order_dport(hdr) == 8000); - EXPECT_TRUE(udp_hdr_get_len(hdr) == 155); + + EXPECT_TRUE(udp_hdr_get_src_port(hdr) == 4001); + EXPECT_TRUE(udp_hdr_get_dst_port(hdr) == 8000); + EXPECT_TRUE(udp_hdr_get_total_len(hdr) == 155); EXPECT_TRUE(udp_hdr_get_checksum(hdr) == 0x1e1e); } +TEST(UDP_UTILS, SET) +{ + char buff[8] = {0}; + struct udphdr *hdr = (struct udphdr *)buff; + + udp_hdr_set_src_port(hdr, 4001); + udp_hdr_set_dst_port(hdr, 8000); + udp_hdr_set_total_len(hdr, 155); + udp_hdr_set_checksum(hdr, 0x1e1e); + EXPECT_TRUE(memcmp(buff, data, 8) == 0); +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/src/packet/udp_helpers.h b/src/packet/udp_utils.h similarity index 50% rename from src/packet/udp_helpers.h rename to src/packet/udp_utils.h index 424d297..600f216 100644 --- a/src/packet/udp_helpers.h +++ b/src/packet/udp_utils.h @@ -1,5 +1,5 @@ -#ifndef _UDP_HELPERS_H -#define _UDP_HELPERS_H +#ifndef _UDP_UTILS_H +#define _UDP_UTILS_H #ifdef __cpluscplus extern "C" @@ -24,27 +24,21 @@ extern "C" * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ -static inline uint16_t udp_hdr_get_host_order_sport(const struct udphdr *hdr) +/****************************************************************************** + * get + ******************************************************************************/ + +static inline uint16_t udp_hdr_get_src_port(const struct udphdr *hdr) { return ntohs(hdr->uh_sport); } -static inline uint16_t udp_hdr_get_host_order_dport(const struct udphdr *hdr) +static inline uint16_t udp_hdr_get_dst_port(const struct udphdr *hdr) { return ntohs(hdr->uh_dport); } -static inline uint16_t udp_hdr_get_net_order_sport(const struct udphdr *hdr) -{ - return hdr->uh_sport; -} - -static inline uint16_t udp_hdr_get_net_order_dport(const struct udphdr *hdr) -{ - return hdr->uh_dport; -} - -static inline uint16_t udp_hdr_get_len(const struct udphdr *hdr) +static inline uint16_t udp_hdr_get_total_len(const struct udphdr *hdr) { return ntohs(hdr->uh_ulen); } @@ -54,6 +48,30 @@ static inline uint16_t udp_hdr_get_checksum(const struct udphdr *hdr) return ntohs(hdr->uh_sum); } +/****************************************************************************** + * set + ******************************************************************************/ + +static inline void udp_hdr_set_src_port(struct udphdr *hdr, uint16_t port) +{ + hdr->uh_sport = htons(port); +} + +static inline void udp_hdr_set_dst_port(struct udphdr *hdr, uint16_t port) +{ + hdr->uh_dport = htons(port); +} + +static inline void udp_hdr_set_total_len(struct udphdr *hdr, uint16_t len) +{ + hdr->uh_ulen = htons(len); +} + +static inline void udp_hdr_set_checksum(struct udphdr *hdr, uint16_t sum) +{ + hdr->uh_sum = htons(sum); +} + #ifdef __cpluscplus } #endif