diff options
| author | luwenpeng <[email protected]> | 2024-08-16 14:15:03 +0800 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2024-08-16 14:15:03 +0800 |
| commit | 4ee08c136a628ff475ff0f613e2359997f1de2c3 (patch) | |
| tree | 51a08702157257a473e1ba30c39c079a1485d46f | |
| parent | ebf1dad62c05571595fa03e3bb647afe1f29ad2f (diff) | |
refactor: rename packet_craft.cpp/h to packet_builder.cpp/h
| -rw-r--r-- | include/stellar/packet.h | 6 | ||||
| -rw-r--r-- | include/stellar/stellar.h | 2 | ||||
| -rw-r--r-- | src/packet/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/packet/packet_builder.cpp (renamed from src/packet/packet_craft.cpp) | 16 | ||||
| -rw-r--r-- | src/packet/packet_builder.h | 14 | ||||
| -rw-r--r-- | src/packet/packet_craft.h | 25 | ||||
| -rw-r--r-- | src/packet/test/CMakeLists.txt | 10 | ||||
| -rw-r--r-- | src/packet/test/gtest_packet_builder.cpp (renamed from src/packet/test/gtest_packet_craft.cpp) | 44 | ||||
| -rw-r--r-- | src/stellar/stellar_core.cpp | 2 | ||||
| -rw-r--r-- | src/stellar/stellar_core.h | 3 | ||||
| -rw-r--r-- | src/stellar/version.map | 8 | ||||
| -rw-r--r-- | test/packet_inject/packet_inject.cpp | 8 |
12 files changed, 65 insertions, 75 deletions
diff --git a/include/stellar/packet.h b/include/stellar/packet.h index 2d760e9..d3132df 100644 --- a/include/stellar/packet.h +++ b/include/stellar/packet.h @@ -41,11 +41,11 @@ uint16_t packet_get_payload_len(const struct packet *pkt); * tcp_ack: the acknowledgment number of the new TCP packet (in host byte order) * tcp_options_len: the length of the options (must be a multiple of 4) */ -struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, +struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, const char *tcp_options, uint16_t tcp_options_len, const char *tcp_payload, uint16_t tcp_payload_len); -struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len); -struct packet *craft_l3_packet(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len); +struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len); +struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len); struct tcp_segment; const char *tcp_segment_get_data(const struct tcp_segment *seg); diff --git a/include/stellar/stellar.h b/include/stellar/stellar.h index 9af384c..6007da4 100644 --- a/include/stellar/stellar.h +++ b/include/stellar/stellar.h @@ -52,7 +52,7 @@ void stellar_emit_datapath_telemetry(struct packet *pkt, const char * module, c int stellar_get_worker_thread_num(struct stellar *st); uint16_t stellar_get_current_thread_index(); // only send user crafted packet, can't send packet which come from network -void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt); +void stellar_send_build_packet(struct stellar *st, struct packet *pkt); int stellar_run(int argc, char **argv); #ifdef __cplusplus diff --git a/src/packet/CMakeLists.txt b/src/packet/CMakeLists.txt index 29498a0..4368964 100644 --- a/src/packet/CMakeLists.txt +++ b/src/packet/CMakeLists.txt @@ -1,6 +1,6 @@ add_library(packet packet_parser.cpp - packet_craft.cpp + packet_builder.cpp packet_filter.cpp packet_dump.cpp packet_utils.cpp diff --git a/src/packet/packet_craft.cpp b/src/packet/packet_builder.cpp index b334d37..0aa9d58 100644 --- a/src/packet/packet_craft.cpp +++ b/src/packet/packet_builder.cpp @@ -12,7 +12,7 @@ #include "packet_utils.h" #include "packet_layer.h" #include "packet_parser.h" -#include "packet_craft.h" +#include "packet_builder.h" #define PACKET_CRAFT_LOG_DEBUG(format, ...) LOG_DEBUG("packet craft", format, ##__VA_ARGS__) #define PACKET_CRAFT_LOG_ERROR(format, ...) LOG_ERROR("packet craft", format, ##__VA_ARGS__) @@ -25,7 +25,7 @@ struct fingerprint uint16_t tcp_win; }; -static uint8_t append_sender_fingerprint = 0; +static uint8_t append_fingerprint = 0; /****************************************************************************** * Private API @@ -33,7 +33,7 @@ static uint8_t append_sender_fingerprint = 0; static inline void calc_packet_fingerprint(struct fingerprint *finger) { - if (append_sender_fingerprint) + if (append_fingerprint) { #define RANGE(rand, start, end) (start + rand % (end - start + 1)) // [start, end] struct timespec time; @@ -244,9 +244,9 @@ static void calculate_length_and_checksum(const struct packet *origin_pkt, int l * Public API ******************************************************************************/ -void append_sender_fingerprint_to_crafted_packet() +void append_fingerprint_to_build_packet() { - append_sender_fingerprint = 1; + append_fingerprint = 1; } /* @@ -254,7 +254,7 @@ void append_sender_fingerprint_to_crafted_packet() * tcp_ack: the acknowledgment number of the new TCP packet (in host byte order) * tcp_options_len: the length of the options (must be a multiple of 4) */ -struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, +struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, const char *tcp_options, uint16_t tcp_options_len, const char *tcp_payload, uint16_t tcp_payload_len) { @@ -310,7 +310,7 @@ struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_se return new_pkt; } -struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len) +struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len) { // check arguments if (origin_pkt == NULL || (udp_payload == NULL && udp_payload_len != 0) || (udp_payload != NULL && udp_payload_len == 0)) @@ -352,7 +352,7 @@ struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp return new_pkt; } -struct packet *craft_l3_packet(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len) +struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len) { if (origin_pkt == NULL || (l3_payload == NULL && l3_payload_len != 0) || (l3_payload != NULL && l3_payload_len == 0)) { diff --git a/src/packet/packet_builder.h b/src/packet/packet_builder.h new file mode 100644 index 0000000..643be2c --- /dev/null +++ b/src/packet/packet_builder.h @@ -0,0 +1,14 @@ +#pragma once + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include <stdint.h> + +void append_fingerprint_to_build_packet(); + +#ifdef __cplusplus +} +#endif diff --git a/src/packet/packet_craft.h b/src/packet/packet_craft.h deleted file mode 100644 index cfd7cf4..0000000 --- a/src/packet/packet_craft.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include <stdint.h> - -void append_sender_fingerprint_to_crafted_packet(); - -/* - * tcp_seq: the sequence number of the new TCP packet (in host byte order) - * tcp_ack: the acknowledgment number of the new TCP packet (in host byte order) - * tcp_options_len: the length of the options (must be a multiple of 4) - */ -struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, - const char *tcp_options, uint16_t tcp_options_len, - const char *tcp_payload, uint16_t tcp_payload_len); -struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len); -struct packet *craft_l3_packet(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len); - -#ifdef __cplusplus -} -#endif diff --git a/src/packet/test/CMakeLists.txt b/src/packet/test/CMakeLists.txt index b8acc77..a5e3a2f 100644 --- a/src/packet/test/CMakeLists.txt +++ b/src/packet/test/CMakeLists.txt @@ -46,8 +46,11 @@ target_link_libraries(gtest_packet_frag packet gtest) add_executable(gtest_packet_parser gtest_packet_parser.cpp) target_link_libraries(gtest_packet_parser packet gtest) -add_executable(gtest_packet_craft gtest_packet_craft.cpp) -target_link_libraries(gtest_packet_craft packet gtest) +add_executable(gtest_packet_builder gtest_packet_builder.cpp) +target_link_libraries(gtest_packet_builder packet gtest) + +add_executable(gtest_packet_filter gtest_packet_filter.cpp) +target_link_libraries(gtest_packet_filter packet gtest) add_executable(gtest_packet_ldbc gtest_packet_ldbc.cpp) target_link_libraries(gtest_packet_ldbc packet gtest) @@ -69,5 +72,6 @@ gtest_discover_tests(gtest_gtp1_utils) gtest_discover_tests(gtest_gtp2_utils) gtest_discover_tests(gtest_packet_frag) gtest_discover_tests(gtest_packet_parser) -gtest_discover_tests(gtest_packet_craft) +gtest_discover_tests(gtest_packet_builder) +gtest_discover_tests(gtest_packet_filter) gtest_discover_tests(gtest_packet_ldbc)
\ No newline at end of file diff --git a/src/packet/test/gtest_packet_craft.cpp b/src/packet/test/gtest_packet_builder.cpp index ec5fbd5..4adeb17 100644 --- a/src/packet/test/gtest_packet_craft.cpp +++ b/src/packet/test/gtest_packet_builder.cpp @@ -12,7 +12,7 @@ #include "packet_dump.h" #include "packet_layer.h" #include "packet_parser.h" -#include "packet_craft.h" +#include "packet_builder.h" #include "packet_utils.h" #define PRINT_GREEN(fmt, ...) printf("\033[0;32m" fmt "\033[0m\n", ##__VA_ARGS__) @@ -123,13 +123,13 @@ unsigned char data1[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; /* - * craft_tcp_packet() + * packet_build_tcp() * -> ETH->IPv4->TCP * -> with TCP options * -> with TCP payload */ #if 1 -TEST(PACKET_CRAFT_TCP, ETH_IP4_TCP) +TEST(PACKET_BUILD_TCP, ETH_IP4_TCP) { struct packet orig_pkt; memset(&orig_pkt, 0, sizeof(orig_pkt)); @@ -137,7 +137,7 @@ TEST(PACKET_CRAFT_TCP, ETH_IP4_TCP) PRINT_GREEN("origin packet:"); packet_dump_stdio(&orig_pkt); - struct packet *new_pkt = craft_tcp_packet(&orig_pkt, 1, 2, TH_ACK, (const char *)&ts_pad_opt, sizeof(ts_pad_opt), "Hello", 5); + struct packet *new_pkt = packet_build_tcp(&orig_pkt, 1, 2, TH_ACK, (const char *)&ts_pad_opt, sizeof(ts_pad_opt), "Hello", 5); EXPECT_TRUE(new_pkt != nullptr); PRINT_GREEN("new packet:"); packet_dump_stdio(new_pkt); @@ -261,12 +261,12 @@ unsigned char data2[] = { 0x04, 0x02}; /* - * craft_tcp_packet() + * packet_build_tcp() * -> ETH->IPv4->IPv6->TCP * -> with TCP payload */ #if 1 -TEST(PACKET_CRAFT_TCP, ETH_IP4_IP6_TCP) +TEST(PACKET_BUILD_TCP, ETH_IP4_IP6_TCP) { struct packet orig_pkt; memset(&orig_pkt, 0, sizeof(orig_pkt)); @@ -274,7 +274,7 @@ TEST(PACKET_CRAFT_TCP, ETH_IP4_IP6_TCP) PRINT_GREEN("origin packet:"); packet_dump_stdio(&orig_pkt); - struct packet *new_pkt = craft_tcp_packet(&orig_pkt, 1234, 2345, TH_ACK, NULL, 0, "Hello", 5); + struct packet *new_pkt = packet_build_tcp(&orig_pkt, 1234, 2345, TH_ACK, NULL, 0, "Hello", 5); EXPECT_TRUE(new_pkt != nullptr); PRINT_GREEN("new packet:"); packet_dump_stdio(new_pkt); @@ -477,12 +477,12 @@ unsigned char data3[] = { 0x4f, 0xe9, 0xf5, 0xf0, 0x61, 0x5d, 0x7f, 0xc4, 0xc4, 0xd1, 0x05, 0x54, 0x13, 0xdb}; /* - * craft_tcp_packet() + * packet_build_tcp() * -> ETH->IPv6->UDP->GTP->IPv4->TCP * -> with TCP payload */ #if 1 -TEST(PACKET_CRAFT_TCP, ETH_IP6_UDP_GTP_IP4_TCP) +TEST(PACKET_BUILD_TCP, ETH_IP6_UDP_GTP_IP4_TCP) { struct packet orig_pkt; memset(&orig_pkt, 0, sizeof(orig_pkt)); @@ -490,7 +490,7 @@ TEST(PACKET_CRAFT_TCP, ETH_IP6_UDP_GTP_IP4_TCP) PRINT_GREEN("origin packet:"); packet_dump_stdio(&orig_pkt); - struct packet *new_pkt = craft_tcp_packet(&orig_pkt, 1, 2, TH_ACK, NULL, 0, "Hello", 5); + struct packet *new_pkt = packet_build_tcp(&orig_pkt, 1, 2, TH_ACK, NULL, 0, "Hello", 5); EXPECT_TRUE(new_pkt != nullptr); PRINT_GREEN("new packet:"); packet_dump_stdio(new_pkt); @@ -697,13 +697,13 @@ unsigned char data4[] = { 0x00, 0x00, 0x00, 0x01, 0x30, 0x39, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02, 0x20, 0x00, 0x31, 0xa0, 0x00, 0x00}; /* - * craft_tcp_packet() + * packet_build_tcp() * -> ETH->IPv4->GRE->IPv6->TCP * -> with TCP payload * -> with GRE checksum */ #if 1 -TEST(PACKET_CRAFT_TCP, ETH_IP4_GRE_IP6_TCP) +TEST(PACKET_BUILD_TCP, ETH_IP4_GRE_IP6_TCP) { struct packet orig_pkt; memset(&orig_pkt, 0, sizeof(orig_pkt)); @@ -711,7 +711,7 @@ TEST(PACKET_CRAFT_TCP, ETH_IP4_GRE_IP6_TCP) PRINT_GREEN("origin packet:"); packet_dump_stdio(&orig_pkt); - struct packet *new_pkt = craft_tcp_packet(&orig_pkt, 1, 2, TH_ACK, NULL, 0, "Hello", 5); + struct packet *new_pkt = packet_build_tcp(&orig_pkt, 1, 2, TH_ACK, NULL, 0, "Hello", 5); EXPECT_TRUE(new_pkt != nullptr); PRINT_GREEN("new packet:"); packet_dump_stdio(new_pkt); @@ -924,13 +924,13 @@ unsigned char data5[] = { 0x33, 0x31, 0x34, 0x30, 0x65, 0x37, 0x36, 0x36, 0x37, 0x38, 0x00, 0x00, 0x1c, 0x00, 0x01}; /* - * craft_udp_packet() + * packet_build_udp() * -> ETH->VLAN->IPv6->IPv4->GRE->PPP->IPv4->UDP->DNS * -> with UDP payload * -> with GRE payload length */ #if 1 -TEST(PACKET_CRAFT_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS) +TEST(PACKET_BUILD_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS) { struct packet orig_pkt; memset(&orig_pkt, 0, sizeof(orig_pkt)); @@ -938,7 +938,7 @@ TEST(PACKET_CRAFT_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS) PRINT_GREEN("origin packet:"); packet_dump_stdio(&orig_pkt); - struct packet *new_pkt = craft_udp_packet(&orig_pkt, "Hello", 5); + struct packet *new_pkt = packet_build_udp(&orig_pkt, "Hello", 5); EXPECT_TRUE(new_pkt != nullptr); PRINT_GREEN("new packet:"); packet_dump_stdio(new_pkt); @@ -1027,12 +1027,12 @@ TEST(PACKET_CRAFT_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS) #endif /* - * craft_l3_packet() + * packet_build_l3() * -> ETH->IPv4->ICMP * -> ICMPv4 checkum not include the pseudo-header of IPv4 header */ #if 1 -TEST(PACKET_CRAFT_L3, ETH_IP4_ICMP) +TEST(PACKET_BUILD_L3, ETH_IP4_ICMP) { /* * Internet Control Message Protocol @@ -1066,7 +1066,7 @@ TEST(PACKET_CRAFT_L3, ETH_IP4_ICMP) icmp->checksum = 0; icmp->checksum = checksum(icmp, sizeof(icmp_resp)); - struct packet *new_pkt = craft_l3_packet(&orig_pkt, IPPROTO_ICMP, (const char *)icmp_resp, sizeof(icmp_resp)); + struct packet *new_pkt = packet_build_l3(&orig_pkt, IPPROTO_ICMP, (const char *)icmp_resp, sizeof(icmp_resp)); EXPECT_TRUE(new_pkt != nullptr); PRINT_GREEN("new packet:"); packet_dump_stdio(new_pkt); @@ -1116,12 +1116,12 @@ TEST(PACKET_CRAFT_L3, ETH_IP4_ICMP) #endif /* - * craft_l3_packet() + * packet_build_l3() * -> ETH->IPv4->IPv6->ICMPv6 * -> ICMPv6 checkum need include the pseudo-header of IPv6 header */ #if 1 -TEST(PACKET_CRAFT_L3, ETH_IP6_ICMP) +TEST(PACKET_BUILD_L3, ETH_IP6_ICMP) { /* * Internet Control Message Protocol v6 @@ -1161,7 +1161,7 @@ TEST(PACKET_CRAFT_L3, ETH_IP6_ICMP) } } - struct packet *new_pkt = craft_l3_packet(&orig_pkt, IPPROTO_ICMPV6, (const char *)icmp_resp, sizeof(icmp_resp)); + struct packet *new_pkt = packet_build_l3(&orig_pkt, IPPROTO_ICMPV6, (const char *)icmp_resp, sizeof(icmp_resp)); EXPECT_TRUE(new_pkt != nullptr); PRINT_GREEN("new packet:"); packet_dump_stdio(new_pkt); diff --git a/src/stellar/stellar_core.cpp b/src/stellar/stellar_core.cpp index 7ebf11f..3ce3380 100644 --- a/src/stellar/stellar_core.cpp +++ b/src/stellar/stellar_core.cpp @@ -611,7 +611,7 @@ uint16_t stellar_get_current_thread_index() } // only send user crafted packet, can't send packet which come from network -void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt) +void stellar_send_build_packet(struct stellar *st, struct packet *pkt) { uint16_t thr_idx = stellar_get_current_thread_index(); struct packet_io *packet_io = stellar_get_packet_io(st); diff --git a/src/stellar/stellar_core.h b/src/stellar/stellar_core.h index 788ef1d..3d9b5fb 100644 --- a/src/stellar/stellar_core.h +++ b/src/stellar/stellar_core.h @@ -13,9 +13,6 @@ struct plugin_manager_schema *stellar_get_plugin_manager(const struct stellar *s // TODO fix plugin manager, delete this function void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema *plug_mgr); -// only send user crafted packet, can't send packet which come from network -void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt); - #ifdef __cplusplus } #endif diff --git a/src/stellar/version.map b/src/stellar/version.map index 39855e1..ef5181c 100644 --- a/src/stellar/version.map +++ b/src/stellar/version.map @@ -14,9 +14,9 @@ global: packet_get_raw_len; packet_get_payload; packet_get_payload_len; - craft_tcp_packet; - craft_udp_packet; - craft_l3_packet; + packet_build_tcp; + packet_build_udp; + packet_build_l3; tcp_segment_get_data; tcp_segment_get_len; @@ -56,7 +56,7 @@ global: stellar_polling_plugin_register; stellar_get_current_thread_index; stellar_get_worker_thread_num; - stellar_send_crafted_packet; + stellar_send_build_packet; stellar_run; local: *; diff --git a/test/packet_inject/packet_inject.cpp b/test/packet_inject/packet_inject.cpp index 6087cae..c9fb4e3 100644 --- a/test/packet_inject/packet_inject.cpp +++ b/test/packet_inject/packet_inject.cpp @@ -332,14 +332,14 @@ static void craft_and_send_udp_packet(struct stellar *st, struct session *sess, return; } - struct packet *craft_pkt = craft_udp_packet(origin_pkt, udp_payload, udp_payload_len); + struct packet *craft_pkt = packet_build_udp(origin_pkt, udp_payload, udp_payload_len); if (craft_pkt == NULL) { LOG_ERR("craft UDP packet failed\n"); return; } - stellar_send_crafted_packet(st, craft_pkt); + stellar_send_build_packet(st, craft_pkt); } static void craft_and_send_tcp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata, @@ -393,14 +393,14 @@ static void craft_and_send_tcp_packet(struct stellar *st, struct session *sess, return; } - struct packet *craft_pkt = craft_tcp_packet(origin_pkt, tcp_seq, tcp_ack, tcp_flags, NULL, 0, tcp_payload, tcp_payload_len); + struct packet *craft_pkt = packet_build_tcp(origin_pkt, tcp_seq, tcp_ack, tcp_flags, NULL, 0, tcp_payload, tcp_payload_len); if (craft_pkt == NULL) { LOG_ERR("craft TCP packet failed\n"); return; } - stellar_send_crafted_packet(st, craft_pkt); + stellar_send_build_packet(st, craft_pkt); } /****************************************************************************** |
