diff options
40 files changed, 904 insertions, 1004 deletions
diff --git a/include/stellar/packet.h b/include/stellar/packet.h index 0ceed05..8995986 100644 --- a/include/stellar/packet.h +++ b/include/stellar/packet.h @@ -36,6 +36,9 @@ uint16_t packet_get_raw_len(const struct packet *pkt); const char *packet_get_payload(const struct packet *pkt); uint16_t packet_get_payload_len(const struct packet *pkt); +struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len); +struct packet *imitate_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len); + #ifdef __cplusplus } #endif diff --git a/include/stellar/stellar.h b/include/stellar/stellar.h index 0f02132..3237455 100644 --- a/include/stellar/stellar.h +++ b/include/stellar/stellar.h @@ -49,15 +49,9 @@ typedef int plugin_on_polling_func(void *plugin_env); int stellar_polling_plugin_register(struct stellar *st, plugin_on_polling_func on_polling, void *plugin_env); uint16_t stellar_get_current_thread_index(); - -// return inject packet length, return 0 if failed -int stellar_inject_tcp_rst(struct stellar *st, const struct session *sess, enum flow_direction inject_dir); -int stellar_inject_tcp_fin(struct stellar *st, const struct session *sess, enum flow_direction inject_dir); -int stellar_inject_tcp_payload(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len); -int stellar_inject_udp_payload(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len); -int stellar_inject_ctrl_msg(struct stellar *st, const struct session *sess, const struct sids *sids, const char *msg, uint16_t len); - -int stellar_main(int argc, char **argv); +// only send user crafted packet, can't send packet which come from network +void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt); +int stellar_run(int argc, char **argv); #ifdef __cplusplus } diff --git a/src/id_generator/id_generator.cpp b/src/id_generator/id_generator.cpp index 4460735..0237a64 100644 --- a/src/id_generator/id_generator.cpp +++ b/src/id_generator/id_generator.cpp @@ -4,7 +4,7 @@ #include "log.h" #include "macro.h" #include "times.h" -#include "stellar_utils.h" +#include "stellar_core.h" #include "id_generator.h" #define ID_GENERATOR_LOG_ERROR(format, ...) LOG_ERROR("id generator", format, ##__VA_ARGS__) diff --git a/src/packet_io/dumpfile_io.cpp b/src/packet_io/dumpfile_io.cpp index a8b6687..3b76c75 100644 --- a/src/packet_io/dumpfile_io.cpp +++ b/src/packet_io/dumpfile_io.cpp @@ -19,8 +19,8 @@ #include "packet_parse.h" #include "lock_free_queue.h" -#define PACKET_IO_LOG_STATE(format, ...) LOG_STATE("dumpfile io", format, ##__VA_ARGS__) -#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("dumpfile io", format, ##__VA_ARGS__) +#define PACKET_IO_LOG_STATE(format, ...) LOG_STATE("dumpfile", format, ##__VA_ARGS__) +#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("dumpfile", format, ##__VA_ARGS__) #define MAX_PACKET_QUEUE_SIZE (4096 * 1000) diff --git a/src/packet_io/marsio_io.cpp b/src/packet_io/marsio_io.cpp index 3bbda38..012db3b 100644 --- a/src/packet_io/marsio_io.cpp +++ b/src/packet_io/marsio_io.cpp @@ -11,7 +11,7 @@ #include "packet_utils.h" #include "packet_parse.h" -#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("marsio io", format, ##__VA_ARGS__) +#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("marsio", format, ##__VA_ARGS__) struct marsio_io { diff --git a/src/plugin/plugin_manager.cpp b/src/plugin/plugin_manager.cpp index 2d0cd1d..0a31e5b 100644 --- a/src/plugin/plugin_manager.cpp +++ b/src/plugin/plugin_manager.cpp @@ -2,7 +2,7 @@ #include "plugin_manager.h" #include "session_utils.h" #include "packet_utils.h" -#include "stellar_utils.h" +#include "stellar_core.h" #include "stellar/utils.h" #include "stellar/session_exdata.h" #include "stellar/session_mq.h" diff --git a/src/session/session_def.h b/src/session/session_def.h index a611f18..e17796c 100644 --- a/src/session/session_def.h +++ b/src/session/session_def.h @@ -25,9 +25,6 @@ struct tcp_half struct tcp_segment in_order; // current packet in order segment uint32_t in_order_ref; // reference count of current packet in order segment - uint32_t inject_inc_seq_offset; // inject packet base on current packet increase seq offset - uint32_t inject_inc_ack_offset; // inject packet base on current packet increase ack offset - uint32_t seq; // current packet sequence number uint32_t ack; // current packet ack number uint16_t len; // current packet payload length diff --git a/src/session/session_manager.cpp b/src/session/session_manager.cpp index d9dd5e5..8a62712 100644 --- a/src/session/session_manager.cpp +++ b/src/session/session_manager.cpp @@ -298,8 +298,6 @@ static void tcp_update(struct session_manager *mgr, struct session *sess, enum f { half->isn = tcp_hdr_get_seq(hdr); } - half->inject_inc_ack_offset = 0; - half->inject_inc_seq_offset = 0; half->flags = flags; half->history |= flags; half->seq = tcp_hdr_get_seq(hdr); diff --git a/src/stellar/CMakeLists.txt b/src/stellar/CMakeLists.txt index 588a1e9..1185726 100644 --- a/src/stellar/CMakeLists.txt +++ b/src/stellar/CMakeLists.txt @@ -1,4 +1,4 @@ -set(SOURCE stellar_config.cpp stellar_stat.cpp stellar.cpp inject.cpp) +set(SOURCE stellar_config.cpp stellar_stat.cpp stellar_core.cpp) set(LIBRARY times plugin_manager session_manager ip_reassembly packet_io pthread fieldstat4 toml) add_library(stellar_core STATIC ${SOURCE}) diff --git a/src/stellar/inject.cpp b/src/stellar/inject.cpp deleted file mode 100644 index 2765dd4..0000000 --- a/src/stellar/inject.cpp +++ /dev/null @@ -1,227 +0,0 @@ -#include <errno.h> -#include <assert.h> - -#include "log.h" -#include "times.h" -#include "packet_io.h" -#include "packet_def.h" -#include "packet_build.h" -#include "packet_utils.h" -#include "session_def.h" -#include "session_utils.h" -#include "stellar_utils.h" -#include "tcp_reassembly.h" -#include "session_manager.h" - -#define INJECT_PACKET_LOG_ERROR(format, ...) LOG_ERROR("inject packet", format, ##__VA_ARGS__) -#define INJECT_PACKE_LOG_DEBUG(format, ...) LOG_DEBUG("inject packet", format, ##__VA_ARGS__) - -static inline void calc_tcp_seq_ack(const struct session *sess, enum flow_direction inject_dir, uint32_t *seq, uint32_t *ack, uint8_t flags, uint16_t len) -{ - /* - * +--------+ current packet +---------+ C2S RST +--------+ - * | |----------------->| |----------------->| | - * | Client | | Stellar | | Server | - * | |<-----------------| |<-----------------| | - * +--------+ S2C RST +---------+ +--------+ - * - * for example: current packet is C2S - * - * inject direction == current direction (inject C2S RST) - * seq = current_packet_seq - * ack = current_packet_ack - * - * inject direction != current direction (inject S2C RST) - * seq = current_packet_ack - * ack = current_packet_seq + current_packet_payload_len - * or if current packet is a SYN-ACK packet - * seq = current_packet_seq - * ack = current_packet_ack + current_packet_payload_len + 1 - */ - - enum flow_direction curr_dir = session_get_current_flow_direction(sess); - struct tcp_half *tcp_curr_half = (struct tcp_half *)&sess->tcp_halfs[curr_dir]; - if (inject_dir == curr_dir) - { - *seq = uint32_add(tcp_curr_half->seq, tcp_curr_half->inject_inc_seq_offset); - *ack = tcp_curr_half->ack; - - tcp_curr_half->inject_inc_seq_offset += len; - // inject RST packer after FIN packer, seq should be increased by 1 - tcp_curr_half->inject_inc_seq_offset += (flags & TH_FIN) ? 1 : 0; - } - else - { - *seq = uint32_add(tcp_curr_half->ack, tcp_curr_half->inject_inc_ack_offset); - *ack = uint32_add(tcp_curr_half->seq, tcp_curr_half->len + (tcp_curr_half->flags & TH_SYN ? 1 : 0)); - - tcp_curr_half->inject_inc_ack_offset += len; - // inject RST packer after FIN packer, ack should be increased by 1 - tcp_curr_half->inject_inc_ack_offset += (flags & TH_FIN) ? 1 : 0; - } -} - -// return 1 if success -// return 0 if failed -int stellar_send_packet(struct stellar *st, struct packet *pkt) -{ - uint64_t time_ms = stellar_get_monotonic_time_msec(); - uint16_t thr_idx = stellar_get_current_thread_index(); - struct packet_io *packet_io = stellar_get_packet_io(st); - struct session_manager *sess_mgr = stellar_get_session_manager(st); - session_manager_record_duplicated_packet(sess_mgr, pkt, time_ms); - - if (packet_get_origin_ctx(pkt)) - { - // TODO - assert(0); - packet_io_egress(packet_io, thr_idx, pkt, 1); - return 1; - } - else - { - return packet_io_inject(packet_io, thr_idx, pkt, 1); - } -} - -static int inject_tcp_packet(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len) -{ -#define TCP_FLAGS_LOG_FORMAT "URG:%d, ACK:%d, PSH:%d, RST:%d, SYN:%d, FIN:%d" -#define TCP_FLAGS_LOG_VALUE(flags) \ - (((flags) & TH_URG) ? 1 : 0), (((flags) & TH_ACK) ? 1 : 0), \ - (((flags) & TH_PUSH) ? 1 : 0), (((flags) & TH_RST) ? 1 : 0), \ - (((flags) & TH_SYN) ? 1 : 0), (((flags) & TH_FIN) ? 1 : 0) - - if (session_get_type(sess) != SESSION_TYPE_TCP) - { - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1); - INJECT_PACKET_LOG_ERROR("session %ld %s is not a TCP session, cannot inject TCP packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d)", - session_get_id(sess), session_get0_readable_addr(sess), - TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len); - return 0; - } - - const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir); - if (origin_pkt == NULL) - { - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1); - INJECT_PACKET_LOG_ERROR("session %ld %s has no %s first packet, cannot inject TCP packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d)", - session_get_id(sess), session_get0_readable_addr(sess), - flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len); - return 0; - } - - uint32_t tcp_seq = 0; - uint32_t tcp_ack = 0; - calc_tcp_seq_ack(sess, inject_dir, &tcp_seq, &tcp_ack, tcp_flags, tcp_payload_len); - - struct packet *new_pkt = imitate_tcp_packet(origin_pkt, tcp_seq, tcp_ack, tcp_flags, tcp_payload, tcp_payload_len); - if (new_pkt == NULL) - { - INJECT_PACKET_LOG_ERROR("session %ld %s build TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) failed", - session_get_id(sess), session_get0_readable_addr(sess), - flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len); - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1); - return 0; - } - - int pkt_len = packet_get_raw_len(new_pkt); - - if (stellar_send_packet(st, new_pkt) == 1) - { - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_SUCCESS, 1); - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_BYTES_SUCCESS, pkt_len); - INJECT_PACKE_LOG_DEBUG("session %ld %s inject TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) success", - session_get_id(sess), session_get0_readable_addr(sess), - flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len); - return pkt_len; - } - else - { - INJECT_PACKET_LOG_ERROR("session %ld %s inject TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) failed, packet I/O nospace", - session_get_id(sess), session_get0_readable_addr(sess), - flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len); - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1); - return 0; - } -} - -static int inject_udp_packet(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len) -{ - if (session_get_type(sess) != SESSION_TYPE_UDP) - { - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1); - INJECT_PACKET_LOG_ERROR("session %ld %s is not a UDP session, cannot inject UDP packet (payload len:%d)", - session_get_id(sess), session_get0_readable_addr(sess), len); - return 0; - } - - const struct packet *pkt = session_get_first_packet(sess, inject_dir); - if (pkt == NULL) - { - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1); - INJECT_PACKET_LOG_ERROR("session %ld %s has no %s first packet, cannot inject UDP packet (payload len:%d)", - session_get_id(sess), session_get0_readable_addr(sess), - flow_direction_to_str(inject_dir), len); - return 0; - } - - struct packet *new_pkt = imitate_udp_packet(pkt, payload, len); - if (new_pkt == 0) - { - INJECT_PACKET_LOG_ERROR("session %ld %s build UDP %s packet (payload len:%d) failed", - session_get_id(sess), session_get0_readable_addr(sess), - flow_direction_to_str(inject_dir), len); - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1); - return 0; - } - - int pkt_len = packet_get_raw_len(new_pkt); - if (stellar_send_packet(st, new_pkt) == 1) - { - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_SUCCESS, 1); - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_BYTES_SUCCESS, pkt_len); - INJECT_PACKE_LOG_DEBUG("session %ld %s inject UDP %s packet (payload len:%d) success", - session_get_id(sess), session_get0_readable_addr(sess), - flow_direction_to_str(inject_dir), len); - return pkt_len; - } - else - { - INJECT_PACKET_LOG_ERROR("session %ld %s inject UDP %s packet (payload len:%d) failed, packet I/O nospace", - session_get_id(sess), session_get0_readable_addr(sess), - flow_direction_to_str(inject_dir), len); - session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1); - return 0; - } -} - -/****************************************************************************** - * Public API - ******************************************************************************/ - -int stellar_inject_tcp_rst(struct stellar *st, const struct session *sess, enum flow_direction inject_dir) -{ - return inject_tcp_packet(st, sess, inject_dir, TH_RST | TH_ACK, NULL, 0); -} - -int stellar_inject_tcp_fin(struct stellar *st, const struct session *sess, enum flow_direction inject_dir) -{ - return inject_tcp_packet(st, sess, inject_dir, TH_FIN | TH_ACK, NULL, 0); -} - -int stellar_inject_tcp_payload(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len) -{ - return inject_tcp_packet(st, sess, inject_dir, TH_ACK, payload, len); -} - -int stellar_inject_udp_payload(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len) -{ - return inject_udp_packet(st, sess, inject_dir, payload, len); -} - -int stellar_inject_ctrl_msg(struct stellar *st, const struct session *sess, const struct sids *sids, const char *msg, uint16_t len) -{ - // TODO - return 0; -}
\ No newline at end of file diff --git a/src/stellar/main.cpp b/src/stellar/main.cpp index 48a0b79..c7e5b8f 100644 --- a/src/stellar/main.cpp +++ b/src/stellar/main.cpp @@ -1,6 +1,6 @@ -#include "stellar_utils.h" +#include "stellar_core.h" int main(int argc, char **argv) { - return stellar_main(argc, argv); + return stellar_run(argc, argv); }
\ No newline at end of file diff --git a/src/stellar/stellar.cpp b/src/stellar/stellar_core.cpp index f85377c..918b62e 100644 --- a/src/stellar/stellar.cpp +++ b/src/stellar/stellar_core.cpp @@ -4,20 +4,23 @@ #include <unistd.h> #include <string.h> #include <signal.h> +#include <stdlib.h> #include <pthread.h> #include <sys/prctl.h> #include "log.h" #include "logo.h" #include "times.h" +#include "packet_io.h" #include "packet_def.h" #include "packet_utils.h" #include "session_utils.h" #include "id_generator.h" #include "stellar_stat.h" -#include "stellar_utils.h" +#include "stellar_core.h" #include "stellar_config.h" #include "plugin_manager.h" +#include "session_manager.h" #define STELLAR_LOG_STATE(format, ...) LOG_STATE("stellar", format, ##__VA_ARGS__) #define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__) @@ -407,7 +410,7 @@ static void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_ } } -int stellar_main(int argc, char **argv) +int stellar_run(int argc, char **argv) { static struct stellar st = {0}; struct stellar_runtime *runtime = &st.runtime; @@ -536,3 +539,24 @@ uint16_t stellar_get_current_thread_index() { return __thread_id; } + +// only send user crafted packet, can't send packet which come from network +void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt) +{ + uint64_t time_ms = stellar_get_monotonic_time_msec(); + uint16_t thr_idx = stellar_get_current_thread_index(); + struct packet_io *packet_io = stellar_get_packet_io(st); + struct session_manager *sess_mgr = stellar_get_session_manager(st); + session_manager_record_duplicated_packet(sess_mgr, pkt, time_ms); + + if (packet_get_origin_ctx(pkt)) + { + // TODO + abort(); + packet_io_egress(packet_io, thr_idx, pkt, 1); + } + else + { + packet_io_inject(packet_io, thr_idx, pkt, 1); + } +}
\ No newline at end of file diff --git a/src/stellar/stellar_utils.h b/src/stellar/stellar_core.h index b646e7c..2291203 100644 --- a/src/stellar/stellar_utils.h +++ b/src/stellar/stellar_core.h @@ -13,7 +13,9 @@ 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); -int stellar_main(int argc, char **argv); +// only send user crafted packet, can't send packet which come from network +void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt); +int stellar_run(int argc, char **argv); #ifdef __cplusplus } diff --git a/src/stellar/version.map b/src/stellar/version.map index 8c3c2a8..1911222 100644 --- a/src/stellar/version.map +++ b/src/stellar/version.map @@ -14,6 +14,8 @@ global: packet_get_raw_len; packet_get_payload; packet_get_payload_len; + imitate_tcp_packet; + imitate_udp_packet; session_exdata_free; stellar_session_exdata_new_index; @@ -50,12 +52,8 @@ global: stellar_packet_plugin_register; stellar_polling_plugin_register; stellar_get_current_thread_index; - stellar_inject_tcp_rst; - stellar_inject_tcp_fin; - stellar_inject_tcp_payload; - stellar_inject_udp_payload; - stellar_inject_ctrl_msg; - stellar_main; + stellar_send_crafted_packet; + stellar_run; local: *; }; diff --git a/test/packet_inject/CMakeLists.txt b/test/packet_inject/CMakeLists.txt index d38d5fd..61fc165 100644 --- a/test/packet_inject/CMakeLists.txt +++ b/test/packet_inject/CMakeLists.txt @@ -1,18 +1,14 @@ -# build packet_injector -add_executable(packet_injector packet_inject_main.cpp packet_inject_plugin.cpp) -target_link_libraries(packet_injector "-rdynamic") -target_link_libraries(packet_injector stellar_core) - -# build libpacket_inject_plugin.so -add_library(packet_inject_plugin SHARED packet_inject_plugin.cpp) -target_include_directories(packet_inject_plugin PUBLIC ${CMAKE_SOURCE_DIR}/include/) -set_target_properties(packet_inject_plugin PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map") +# build libpacket_inject.so +add_library(packet_inject SHARED packet_inject.cpp) +target_link_libraries(packet_inject stellar_devel toml) +target_include_directories(packet_inject PUBLIC ${CMAKE_SOURCE_DIR}/include/) +set_target_properties(packet_inject PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map") # build gtest function(packet_inject_add_case EXEC_NAME) - add_executable(${EXEC_NAME} ${EXEC_NAME}.cpp packet_inject_main.cpp packet_inject_plugin.cpp packet_inject_test.cpp) + add_executable(${EXEC_NAME} ${EXEC_NAME}.cpp) target_link_libraries(${EXEC_NAME} "-rdynamic") - target_link_libraries(${EXEC_NAME} stellar_core gtest) + target_link_libraries(${EXEC_NAME} stellar_devel gtest) gtest_discover_tests(${EXEC_NAME}) endfunction() @@ -26,5 +22,4 @@ packet_inject_add_case(gtest_inject_tcp_payload_after_recv_s2c_first_payload) packet_inject_add_case(gtest_inject_tcp_payload_fin_rst_after_recv_c2s_first_payload) file(COPY ./conf/ DESTINATION ./conf/) -file(COPY ./pcap/ DESTINATION ./pcap/) -file(COPY ./plugin/ DESTINATION ./plugin/)
\ No newline at end of file +file(COPY ./pcap/ DESTINATION ./pcap/)
\ No newline at end of file diff --git a/test/packet_inject/README.md b/test/packet_inject/README.md index 2e2080d..d822a07 100644 --- a/test/packet_inject/README.md +++ b/test/packet_inject/README.md @@ -8,22 +8,6 @@ tcpdump -i virtio_dign_c host 192.0.2.110 and port 80 -n -v -w virtio_dign_c.pca tcpdump -i virtio_dign_s host 192.0.2.110 and port 80 -n -v -w virtio_dign_s.pcap ``` -## 运行 - -``` shell -./packet_injector -t tcp-rst -c c2s-packet -n 1 # After recv SYN -./packet_injector -t tcp-rst -c s2c-packet -n 1 # After recv SYN-ACK -./packet_injector -t tcp-rst -c c2s-packet -n 2 # After recv Sub-ACK -./packet_injector -t tcp-rst -c c2s-packet -n 3 # After recv First-Payload -``` - -``` shell -./packet_injector -t tcp-fin -c c2s-packet -n 1 # After recv SYN -./packet_injector -t tcp-fin -c s2c-packet -n 1 # After recv SYN-ACK -./packet_injector -t tcp-fin -c c2s-packet -n 2 # After recv Sub-ACK -./packet_injector -t tcp-fin -c c2s-packet -n 3 # After recv First-Payload -``` - ## 拨测 ``` shell @@ -33,12 +17,12 @@ curl -v http://http.badssl.selftest.gdnt-cloud.website --resolve "http.badssl.se ## 结果 -| -t | -c | -n | Note | result | -| ----------- | ----------- | ----------- | ---------------------------- | ----------- | -| tcp-rst | c2s-packet | 1 | After recv SYN | Failed | -| tcp-rst | s2c-packet | 1 | After recv SYN-ACK | Success | -| tcp-rst | c2s-packet | 2 | After recv Sub-ACK | Success | -| tcp-rst | c2s-packet | 3 | After recv C2S First-Payload | Success | -| tcp-rst | s2c-packet | 3 | After recv S2C First-payload | Success | -| tcp-payload | c2s-packet | 3 | After recv C2S First-Payload | Success | -| tcp-payload | s2c-packet | 3 | After recv S2C First-payload | Success |
\ No newline at end of file +| type | dir | pkts | note | result | +| ----------- | ---- | ---- | ---------------------------- | ----------- | +| TCP-RST | C2S | 1 | After recv SYN | Failed | +| TCP-RST | S2C | 1 | After recv SYN-ACK | Success | +| TCP-RST | C2S | 2 | After recv Sub-ACK | Success | +| TCP-RST | C2S | 3 | After recv C2S First-Payload | Success | +| TCP-RST | S2C | 3 | After recv S2C First-payload | Success | +| TCP-PAYLOAD | C2S | 3 | After recv C2S First-Payload | Success | +| TCP-PAYLOAD | S2C | 3 | After recv S2C First-payload | Success |
\ No newline at end of file diff --git a/test/packet_inject/conf/inject_ipv4_based_tcp_payload_after_recv_c2s_first_payload.toml b/test/packet_inject/conf/inject_ipv4_based_tcp_payload_after_recv_c2s_first_payload.toml new file mode 100644 index 0000000..3fd45b7 --- /dev/null +++ b/test/packet_inject/conf/inject_ipv4_based_tcp_payload_after_recv_c2s_first_payload.toml @@ -0,0 +1,7 @@ +# When C2S direction received 3 packets, inject a TCP packet with payload +[packet_inject] +filter_ip = any # eg: 2001:db8::1, 192.168.1.100, any +filter_port = 0 # eg: 80, 443 (0 for any) +filter_dir = C2S # eg: C2S, S2C +filter_pkts = 3 # can not be 0 +inject_type = TCP-PAYLOAD # eg: TCP-RST, TCP-FIN, TCP-PAYLOAD, TCP-PAYLOAD-FIN-RST, UDP-PAYLOAD, CTRL-MSG diff --git a/test/packet_inject/conf/inject_ipv4_based_tcp_payload_after_recv_s2c_first_payload.toml b/test/packet_inject/conf/inject_ipv4_based_tcp_payload_after_recv_s2c_first_payload.toml new file mode 100644 index 0000000..86858b3 --- /dev/null +++ b/test/packet_inject/conf/inject_ipv4_based_tcp_payload_after_recv_s2c_first_payload.toml @@ -0,0 +1,7 @@ +# When S2C direction received 3 packets, inject a TCP packet with payload +[packet_inject] +filter_ip = any # eg: 2001:db8::1, 192.168.1.100, any +filter_port = 0 # eg: 80, 443 (0 for any) +filter_dir = S2C # eg: C2S, S2C +filter_pkts = 3 # can not be 0 +inject_type = TCP-PAYLOAD # eg: TCP-RST, TCP-FIN, TCP-PAYLOAD, TCP-PAYLOAD-FIN-RST, UDP-PAYLOAD, CTRL-MSG diff --git a/test/packet_inject/conf/inject_ipv4_based_tcp_payload_fin_rst_after_recv_c2s_first_payload.toml b/test/packet_inject/conf/inject_ipv4_based_tcp_payload_fin_rst_after_recv_c2s_first_payload.toml new file mode 100644 index 0000000..6fa47ea --- /dev/null +++ b/test/packet_inject/conf/inject_ipv4_based_tcp_payload_fin_rst_after_recv_c2s_first_payload.toml @@ -0,0 +1,7 @@ +# When C2S direction received 3 packets, inject a TCP packet with payload, then a FIN packet, then a RST packet +[packet_inject] +filter_ip = any # eg: 2001:db8::1, 192.168.1.100, any +filter_port = 0 # eg: 80, 443 (0 for any) +filter_dir = C2S # eg: C2S, S2C +filter_pkts = 3 # can not be 0 +inject_type = TCP-PAYLOAD-FIN-RST # eg: TCP-RST, TCP-FIN, TCP-PAYLOAD, TCP-PAYLOAD-FIN-RST, UDP-PAYLOAD, CTRL-MSG diff --git a/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_c2s_first_payload.toml b/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_c2s_first_payload.toml new file mode 100644 index 0000000..5b5f58b --- /dev/null +++ b/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_c2s_first_payload.toml @@ -0,0 +1,7 @@ +# When C2S direction received 3 packets, inject a TCP RST packet +[packet_inject] +filter_ip = any # eg: 2001:db8::1, 192.168.1.100, any +filter_port = 0 # eg: 80, 443 (0 for any) +filter_dir = C2S # eg: C2S, S2C +filter_pkts = 3 # can not be 0 +inject_type = TCP-RST # eg: TCP-RST, TCP-FIN, TCP-PAYLOAD, TCP-PAYLOAD-FIN-RST, UDP-PAYLOAD, CTRL-MSG diff --git a/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_s2c_first_payload.toml b/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_s2c_first_payload.toml new file mode 100644 index 0000000..5c7115a --- /dev/null +++ b/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_s2c_first_payload.toml @@ -0,0 +1,7 @@ +# When S2C direction received 3 packets, inject a TCP RST packet +[packet_inject] +filter_ip = any # eg: 2001:db8::1, 192.168.1.100, any +filter_port = 0 # eg: 80, 443 (0 for any) +filter_dir = S2C # eg: C2S, S2C +filter_pkts = 3 # can not be 0 +inject_type = TCP-RST # eg: TCP-RST, TCP-FIN, TCP-PAYLOAD, TCP-PAYLOAD-FIN-RST, UDP-PAYLOAD, CTRL-MSG diff --git a/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_sub_ack.toml b/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_sub_ack.toml new file mode 100644 index 0000000..321bfbb --- /dev/null +++ b/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_sub_ack.toml @@ -0,0 +1,7 @@ +# When C2S direction received 2 packets, inject a TCP RST packet +[packet_inject] +filter_ip = any # eg: 2001:db8::1, 192.168.1.100, any +filter_port = 0 # eg: 80, 443 (0 for any) +filter_dir = C2S # eg: C2S, S2C +filter_pkts = 2 # can not be 0 +inject_type = TCP-RST # eg: TCP-RST, TCP-FIN, TCP-PAYLOAD, TCP-PAYLOAD-FIN-RST, UDP-PAYLOAD, CTRL-MSG diff --git a/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_syn_ack.toml b/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_syn_ack.toml new file mode 100644 index 0000000..839b17b --- /dev/null +++ b/test/packet_inject/conf/inject_ipv4_based_tcp_rst_after_recv_syn_ack.toml @@ -0,0 +1,7 @@ +# When S2C direction received 1 packets, inject a TCP RST packet +[packet_inject] +filter_ip = any # eg: 2001:db8::1, 192.168.1.100, any +filter_port = 0 # eg: 80, 443 (0 for any) +filter_dir = S2C # eg: C2S, S2C +filter_pkts = 1 # can not be 0 +inject_type = TCP-RST # eg: TCP-RST, TCP-FIN, TCP-PAYLOAD, TCP-PAYLOAD-FIN-RST, UDP-PAYLOAD, CTRL-MSG diff --git a/test/packet_inject/conf/spec.toml b/test/packet_inject/conf/spec.toml new file mode 100644 index 0000000..b51b154 --- /dev/null +++ b/test/packet_inject/conf/spec.toml @@ -0,0 +1,4 @@ +[[plugin]] +path = "./plugin/libpacket_inject.so" +init = "packet_inject_init" +exit = "packet_inject_exit" diff --git a/test/packet_inject/gtest_inject_tcp_payload_after_recv_c2s_first_payload.cpp b/test/packet_inject/gtest_inject_tcp_payload_after_recv_c2s_first_payload.cpp index 78253f3..cb3a5ef 100644 --- a/test/packet_inject/gtest_inject_tcp_payload_after_recv_c2s_first_payload.cpp +++ b/test/packet_inject/gtest_inject_tcp_payload_after_recv_c2s_first_payload.cpp @@ -4,26 +4,17 @@ TEST(INJECT_IPV4_BASED_TCP_PAYLOAD, AFTER_RECV_C2S_FIRST_PAYLOAD) { - char current_dir[1024] = {0}; + char curr_dir[1024] = {0}; char work_dir[2048] = {0}; - char input_dir[2048] = {0}; - getcwd(current_dir, sizeof(current_dir)); - snprintf(work_dir, sizeof(work_dir), "%s/%s", current_dir, "inject_ipv4_based_tcp_payload_after_recv_c2s_first_payload"); - snprintf(input_dir, sizeof(input_dir), "%s/%s", current_dir, "pcap/inject_ipv4_based_tcp_payload_after_recv_c2s_first_payload/test/"); + char pcap_dir[2048] = {0}; + getcwd(curr_dir, sizeof(curr_dir)); + snprintf(work_dir, sizeof(work_dir), "%s/%s", curr_dir, "INJECT_IPV4_BASED_TCP_PAYLOAD_AFTER_RECV_C2S_FIRST_PAYLOAD"); + snprintf(pcap_dir, sizeof(pcap_dir), "%s/%s", curr_dir, "pcap/inject_ipv4_based_tcp_payload_after_recv_c2s_first_payload/test/"); struct packet_inject_case test = { - // descriptor - .finish_clean_work_dir = 0, - .descriptor = "Inject IPv4 based TCP Payload after receiving C2S first payload packet.", .work_dir = work_dir, - - // prefix - .input_prefix = input_dir, - - // input pcap + .pcap_dir = pcap_dir, .input_pcap = "input.pcap", - - // compare .compares = { { .expect_pcap = "expect-192.0.2.110:80-192.0.2.212:54146-1.pcap", @@ -46,9 +37,7 @@ TEST(INJECT_IPV4_BASED_TCP_PAYLOAD, AFTER_RECV_C2S_FIRST_PAYLOAD) .inject_pcap = NULL, }, }, - - // packet injector command - .packet_injector_cmd = {"./packet_injector", "-t", "tcp-payload", "-c", "c2s-packet", "-n", "3"}, + .plugin_config_file = "inject_ipv4_based_tcp_payload_after_recv_c2s_first_payload.toml", .diff_skip_pattern = "-I frame.time -I frame.time_epoch -I ip.id -I ip.ttl -I ip.checksum -I tcp.checksum -I tcp.window_size", }; diff --git a/test/packet_inject/gtest_inject_tcp_payload_after_recv_s2c_first_payload.cpp b/test/packet_inject/gtest_inject_tcp_payload_after_recv_s2c_first_payload.cpp index 928edea..4268861 100644 --- a/test/packet_inject/gtest_inject_tcp_payload_after_recv_s2c_first_payload.cpp +++ b/test/packet_inject/gtest_inject_tcp_payload_after_recv_s2c_first_payload.cpp @@ -4,26 +4,17 @@ TEST(INJECT_IPV4_BASED_TCP_PAYLOAD, AFTER_RECV_S2C_FIRST_PAYLOAD) { - char current_dir[1024] = {0}; + char curr_dir[1024] = {0}; char work_dir[2048] = {0}; - char input_dir[2048] = {0}; - getcwd(current_dir, sizeof(current_dir)); - snprintf(work_dir, sizeof(work_dir), "%s/%s", current_dir, "inject_ipv4_based_tcp_payload_after_recv_s2c_first_payload"); - snprintf(input_dir, sizeof(input_dir), "%s/%s", current_dir, "pcap/inject_ipv4_based_tcp_payload_after_recv_s2c_first_payload/test/"); + char pcap_dir[2048] = {0}; + getcwd(curr_dir, sizeof(curr_dir)); + snprintf(work_dir, sizeof(work_dir), "%s/%s", curr_dir, "INJECT_IPV4_BASED_TCP_PAYLOAD_AFTER_RECV_S2C_FIRST_PAYLOAD"); + snprintf(pcap_dir, sizeof(pcap_dir), "%s/%s", curr_dir, "pcap/inject_ipv4_based_tcp_payload_after_recv_s2c_first_payload/test/"); struct packet_inject_case test = { - // descriptor - .finish_clean_work_dir = 0, - .descriptor = "Inject IPv4 based TCP Payload after receiving S2C first payload packet.", .work_dir = work_dir, - - // prefix - .input_prefix = input_dir, - - // input pcap + .pcap_dir = pcap_dir, .input_pcap = "input.pcap", - - // compare .compares = { { .expect_pcap = "expect-192.0.2.110:80-192.0.2.213:48322-1.pcap", @@ -46,9 +37,7 @@ TEST(INJECT_IPV4_BASED_TCP_PAYLOAD, AFTER_RECV_S2C_FIRST_PAYLOAD) .inject_pcap = NULL, }, }, - - // packet injector command - .packet_injector_cmd = {"./packet_injector", "-t", "tcp-payload", "-c", "s2c-packet", "-n", "3"}, + .plugin_config_file = "inject_ipv4_based_tcp_payload_after_recv_s2c_first_payload.toml", .diff_skip_pattern = "-I frame.time -I frame.time_epoch -I ip.id -I ip.ttl -I ip.checksum -I tcp.checksum -I tcp.window_size", }; diff --git a/test/packet_inject/gtest_inject_tcp_payload_fin_rst_after_recv_c2s_first_payload.cpp b/test/packet_inject/gtest_inject_tcp_payload_fin_rst_after_recv_c2s_first_payload.cpp index 18daf1f..18af087 100644 --- a/test/packet_inject/gtest_inject_tcp_payload_fin_rst_after_recv_c2s_first_payload.cpp +++ b/test/packet_inject/gtest_inject_tcp_payload_fin_rst_after_recv_c2s_first_payload.cpp @@ -4,26 +4,17 @@ TEST(INJECT_IPV4_BASED_TCP_PAYLOAD_FIN_RST, AFTER_RECV_C2S_FIRST_PAYLOAD) { - char current_dir[1024] = {0}; + char curr_dir[1024] = {0}; char work_dir[2048] = {0}; - char input_dir[2048] = {0}; - getcwd(current_dir, sizeof(current_dir)); - snprintf(work_dir, sizeof(work_dir), "%s/%s", current_dir, "inject_ipv4_based_tcp_payload_fin_rst_after_recv_c2s_first_payload"); - snprintf(input_dir, sizeof(input_dir), "%s/%s", current_dir, "pcap/inject_ipv4_based_tcp_payload_fin_rst_after_recv_c2s_first_payload/test/"); + char pcap_dir[2048] = {0}; + getcwd(curr_dir, sizeof(curr_dir)); + snprintf(work_dir, sizeof(work_dir), "%s/%s", curr_dir, "INJECT_IPV4_BASED_TCP_PAYLOAD_FIN_RST_AFTER_RECV_C2S_FIRST_PAYLOAD"); + snprintf(pcap_dir, sizeof(pcap_dir), "%s/%s", curr_dir, "pcap/inject_ipv4_based_tcp_payload_fin_rst_after_recv_c2s_first_payload/test/"); struct packet_inject_case test = { - // descriptor - .finish_clean_work_dir = 0, - .descriptor = "Inject IPv4 based TCP Payload & FIN & RST after receiving C2S first payload packet.", .work_dir = work_dir, - - // prefix - .input_prefix = input_dir, - - // input pcap + .pcap_dir = pcap_dir, .input_pcap = "input.pcap", - - // compare .compares = { { .expect_pcap = "expect-192.0.2.110:80-192.0.2.213:37296-1.pcap", @@ -54,9 +45,7 @@ TEST(INJECT_IPV4_BASED_TCP_PAYLOAD_FIN_RST, AFTER_RECV_C2S_FIRST_PAYLOAD) .inject_pcap = NULL, }, }, - - // packet injector command - .packet_injector_cmd = {"./packet_injector", "-t", "tcp-payload-fin-rst", "-c", "c2s-packet", "-n", "3"}, + .plugin_config_file = "inject_ipv4_based_tcp_payload_fin_rst_after_recv_c2s_first_payload.toml", .diff_skip_pattern = "-I frame.time -I frame.time_epoch -I ip.id -I ip.ttl -I ip.checksum -I tcp.checksum -I tcp.window_size", }; diff --git a/test/packet_inject/gtest_inject_tcp_rst_after_recv_c2s_first_payload.cpp b/test/packet_inject/gtest_inject_tcp_rst_after_recv_c2s_first_payload.cpp index 4d94235..22377a2 100644 --- a/test/packet_inject/gtest_inject_tcp_rst_after_recv_c2s_first_payload.cpp +++ b/test/packet_inject/gtest_inject_tcp_rst_after_recv_c2s_first_payload.cpp @@ -4,26 +4,17 @@ TEST(INJECT_IPV4_BASED_TCP_RST, AFTER_RECV_C2S_FIRST_PAYLOAD) { - char current_dir[1024] = {0}; + char curr_dir[1024] = {0}; char work_dir[2048] = {0}; - char input_dir[2048] = {0}; - getcwd(current_dir, sizeof(current_dir)); - snprintf(work_dir, sizeof(work_dir), "%s/%s", current_dir, "inject_ipv4_based_tcp_rst_after_recv_c2s_first_payload"); - snprintf(input_dir, sizeof(input_dir), "%s/%s", current_dir, "pcap/inject_ipv4_based_tcp_rst_after_recv_c2s_first_payload/test/"); + char pcap_dir[2048] = {0}; + getcwd(curr_dir, sizeof(curr_dir)); + snprintf(work_dir, sizeof(work_dir), "%s/%s", curr_dir, "INJECT_IPV4_BASED_TCP_RST_AFTER_RECV_C2S_FIRST_PAYLOAD"); + snprintf(pcap_dir, sizeof(pcap_dir), "%s/%s", curr_dir, "pcap/inject_ipv4_based_tcp_rst_after_recv_c2s_first_payload/test/"); struct packet_inject_case test = { - // descriptor - .finish_clean_work_dir = 0, - .descriptor = "Inject IPv4 based TCP RST after receiving C2S first payload packet.", .work_dir = work_dir, - - // prefix - .input_prefix = input_dir, - - // input pcap + .pcap_dir = pcap_dir, .input_pcap = "input.pcap", - - // compare .compares = { { .expect_pcap = "expect-192.0.2.211:35116-192.0.2.110:80-1.pcap", @@ -38,9 +29,7 @@ TEST(INJECT_IPV4_BASED_TCP_RST, AFTER_RECV_C2S_FIRST_PAYLOAD) .inject_pcap = NULL, }, }, - - // packet injector command - .packet_injector_cmd = {"./packet_injector", "-t", "tcp-rst", "-c", "c2s-packet", "-n", "3"}, + .plugin_config_file = "inject_ipv4_based_tcp_rst_after_recv_c2s_first_payload.toml", .diff_skip_pattern = "-I frame.time -I frame.time_epoch -I ip.id -I ip.ttl -I ip.checksum -I tcp.checksum -I tcp.window_size", }; diff --git a/test/packet_inject/gtest_inject_tcp_rst_after_recv_s2c_first_payload.cpp b/test/packet_inject/gtest_inject_tcp_rst_after_recv_s2c_first_payload.cpp index e8d6288..f02628d 100644 --- a/test/packet_inject/gtest_inject_tcp_rst_after_recv_s2c_first_payload.cpp +++ b/test/packet_inject/gtest_inject_tcp_rst_after_recv_s2c_first_payload.cpp @@ -4,26 +4,17 @@ TEST(INJECT_IPV4_BASED_TCP_RST, AFTER_RECV_S2C_FIRST_PAYLOAD) { - char current_dir[1024] = {0}; + char curr_dir[1024] = {0}; char work_dir[2048] = {0}; - char input_dir[2048] = {0}; - getcwd(current_dir, sizeof(current_dir)); - snprintf(work_dir, sizeof(work_dir), "%s/%s", current_dir, "inject_ipv4_based_tcp_rst_after_recv_s2c_first_payload"); - snprintf(input_dir, sizeof(input_dir), "%s/%s", current_dir, "pcap/inject_ipv4_based_tcp_rst_after_recv_s2c_first_payload/test/"); + char pcap_dir[2048] = {0}; + getcwd(curr_dir, sizeof(curr_dir)); + snprintf(work_dir, sizeof(work_dir), "%s/%s", curr_dir, "INJECT_IPV4_BASED_TCP_RST_AFTER_RECV_S2C_FIRST_PAYLOAD"); + snprintf(pcap_dir, sizeof(pcap_dir), "%s/%s", curr_dir, "pcap/inject_ipv4_based_tcp_rst_after_recv_s2c_first_payload/test/"); struct packet_inject_case test = { - // descriptor - .finish_clean_work_dir = 0, - .descriptor = "Inject IPv4 based TCP RST after receiving S2C first payload packet.", .work_dir = work_dir, - - // prefix - .input_prefix = input_dir, - - // input pcap + .pcap_dir = pcap_dir, .input_pcap = "input.pcap", - - // compare .compares = { { .expect_pcap = "expect-192.0.2.211:54408-192.0.2.110:80-1.pcap", @@ -38,9 +29,7 @@ TEST(INJECT_IPV4_BASED_TCP_RST, AFTER_RECV_S2C_FIRST_PAYLOAD) .inject_pcap = NULL, }, }, - - // packet injector command - .packet_injector_cmd = {"./packet_injector", "-t", "tcp-rst", "-c", "s2c-packet", "-n", "3"}, + .plugin_config_file = "inject_ipv4_based_tcp_rst_after_recv_s2c_first_payload.toml", .diff_skip_pattern = "-I frame.time -I frame.time_epoch -I ip.id -I ip.ttl -I ip.checksum -I tcp.checksum -I tcp.window_size", }; diff --git a/test/packet_inject/gtest_inject_tcp_rst_after_recv_sub_ack.cpp b/test/packet_inject/gtest_inject_tcp_rst_after_recv_sub_ack.cpp index f6439c4..0903d4c 100644 --- a/test/packet_inject/gtest_inject_tcp_rst_after_recv_sub_ack.cpp +++ b/test/packet_inject/gtest_inject_tcp_rst_after_recv_sub_ack.cpp @@ -4,26 +4,17 @@ TEST(INJECT_IPV4_BASED_TCP_RST, AFTER_RECV_SUB_ACK) { - char current_dir[1024] = {0}; + char curr_dir[1024] = {0}; char work_dir[2048] = {0}; - char input_dir[2048] = {0}; - getcwd(current_dir, sizeof(current_dir)); - snprintf(work_dir, sizeof(work_dir), "%s/%s", current_dir, "inject_ipv4_based_tcp_rst_after_recv_sub_ack"); - snprintf(input_dir, sizeof(input_dir), "%s/%s", current_dir, "pcap/inject_ipv4_based_tcp_rst_after_recv_sub_ack/test/"); + char pcap_dir[2048] = {0}; + getcwd(curr_dir, sizeof(curr_dir)); + snprintf(work_dir, sizeof(work_dir), "%s/%s", curr_dir, "INJECT_IPV4_BASED_TCP_RST_AFTER_RECV_SUB_ACK"); + snprintf(pcap_dir, sizeof(pcap_dir), "%s/%s", curr_dir, "pcap/inject_ipv4_based_tcp_rst_after_recv_sub_ack/test/"); struct packet_inject_case test = { - // descriptor - .finish_clean_work_dir = 0, - .descriptor = "Inject IPv4 based TCP RST after receiving SUB-ACK packet.", .work_dir = work_dir, - - // prefix - .input_prefix = input_dir, - - // input pcap + .pcap_dir = pcap_dir, .input_pcap = "input.pcap", - - // compare .compares = { { .expect_pcap = "expect-192.0.2.211:42242-192.0.2.110:80-1.pcap", @@ -38,9 +29,7 @@ TEST(INJECT_IPV4_BASED_TCP_RST, AFTER_RECV_SUB_ACK) .inject_pcap = NULL, }, }, - - // packet injector command - .packet_injector_cmd = {"./packet_injector", "-t", "tcp-rst", "-c", "c2s-packet", "-n", "2"}, + .plugin_config_file = "inject_ipv4_based_tcp_rst_after_recv_sub_ack.toml", .diff_skip_pattern = "-I frame.time -I frame.time_epoch -I ip.id -I ip.ttl -I ip.checksum -I tcp.checksum -I tcp.window_size", }; diff --git a/test/packet_inject/gtest_inject_tcp_rst_after_recv_syn_ack.cpp b/test/packet_inject/gtest_inject_tcp_rst_after_recv_syn_ack.cpp index f338b16..cac2be2 100644 --- a/test/packet_inject/gtest_inject_tcp_rst_after_recv_syn_ack.cpp +++ b/test/packet_inject/gtest_inject_tcp_rst_after_recv_syn_ack.cpp @@ -4,26 +4,17 @@ TEST(INJECT_IPV4_BASED_TCP_RST, AFTER_RECV_SYN_ACK) { - char current_dir[1024] = {0}; + char curr_dir[1024] = {0}; char work_dir[2048] = {0}; - char input_dir[2048] = {0}; - getcwd(current_dir, sizeof(current_dir)); - snprintf(work_dir, sizeof(work_dir), "%s/%s", current_dir, "inject_ipv4_based_tcp_rst_after_recv_syn_ack"); - snprintf(input_dir, sizeof(input_dir), "%s/%s", current_dir, "pcap/inject_ipv4_based_tcp_rst_after_recv_syn_ack/test/"); + char pcap_dir[2048] = {0}; + getcwd(curr_dir, sizeof(curr_dir)); + snprintf(work_dir, sizeof(work_dir), "%s/%s", curr_dir, "INJECT_IPV4_BASED_TCP_RST_AFTER_RECV_SYN_ACK"); + snprintf(pcap_dir, sizeof(pcap_dir), "%s/%s", curr_dir, "pcap/inject_ipv4_based_tcp_rst_after_recv_syn_ack/test/"); struct packet_inject_case test = { - // descriptor - .finish_clean_work_dir = 0, - .descriptor = "Inject IPv4 based TCP RST after receiving SYN-ACK packet.", .work_dir = work_dir, - - // prefix - .input_prefix = input_dir, - - // input pcap + .pcap_dir = pcap_dir, .input_pcap = "input.pcap", - - // compare .compares = { { .expect_pcap = "expect-192.0.2.211:59942-192.0.2.110:80-1.pcap", @@ -38,9 +29,7 @@ TEST(INJECT_IPV4_BASED_TCP_RST, AFTER_RECV_SYN_ACK) .inject_pcap = NULL, }, }, - - // packet injector command - .packet_injector_cmd = {"./packet_injector", "-t", "tcp-rst", "-c", "s2c-packet", "-n", "1"}, + .plugin_config_file = "inject_ipv4_based_tcp_rst_after_recv_syn_ack.toml", .diff_skip_pattern = "-I frame.time -I frame.time_epoch -I ip.id -I ip.ttl -I ip.checksum -I tcp.checksum -I tcp.window_size", }; diff --git a/test/packet_inject/packet_inject.cpp b/test/packet_inject/packet_inject.cpp new file mode 100644 index 0000000..7578bf5 --- /dev/null +++ b/test/packet_inject/packet_inject.cpp @@ -0,0 +1,568 @@ +#include <stdio.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <arpa/inet.h> + +#include "toml.h" +#include "stellar/layer.h" +#include "stellar/session_mq.h" + +#define LOG_ERR(fmt, ...) printf("ERROR [packet inject] " fmt, ##__VA_ARGS__) +#define LOG_INFO(fmt, ...) printf("INFO [packet inject] " fmt, ##__VA_ARGS__) + +/****************************************************************************** + * Config + ******************************************************************************/ + +enum inject_type +{ + INJECT_TCP_RST = 1, + INJECT_TCP_FIN = 2, + INJECT_TCP_PAYLOAD = 3, + INJECT_TCP_PAYLOAD_FIN_RST = 4, + INJECT_UDP_PAYLOAD = 5, + INJECT_CTRL_MSG = 6, +}; + +struct config +{ + int family; // AF_INET or AF_INET6 + union + { + struct sockaddr_in v4; + struct sockaddr_in6 v6; + } addr; + uint16_t port; + uint64_t number; // inject packet after (C2S/S2C) direction receiving n packets + enum inject_type type; + enum flow_direction direction; +}; + +static const char *inject_type_to_str(enum inject_type type) +{ + switch (type) + { + case INJECT_TCP_RST: + return "TCP-RST"; + case INJECT_TCP_FIN: + return "TCP-FIN"; + case INJECT_TCP_PAYLOAD: + return "TCP-PAYLOAD"; + case INJECT_TCP_PAYLOAD_FIN_RST: + return "TCP-PAYLOAD-FIN-RST"; + case INJECT_UDP_PAYLOAD: + return "UDP-PAYLOAD"; + case INJECT_CTRL_MSG: + return "CTRL-MSG"; + default: + return "UNKNOWN"; + } +} + +static int load_config(struct config *config, const char *file) +{ + int ret = -1; + char errbuf[200]; + const char *ptr; + FILE *fp = NULL; + toml_table_t *root = NULL; + toml_table_t *sub = NULL; + memset(config, 0, sizeof(struct config)); + + fp = fopen(file, "r"); + if (fp == NULL) + { + LOG_ERR("open config file %s failed, %s\n", file, strerror(errno)); + goto error_out; + } + + root = toml_parse_file(fp, errbuf, sizeof(errbuf)); + if (root == NULL) + { + LOG_ERR("parse config file %s failed, %s\n", file, errbuf); + goto error_out; + } + + sub = toml_table_in(root, "packet_inject"); + if (sub == NULL) + { + LOG_ERR("config file missing packet_inject section\n"); + goto error_out; + } + + ptr = toml_raw_in(sub, "filter_ip"); + if (ptr == NULL) + { + LOG_ERR("config file missing packet_inject->filter_ip\n"); + goto error_out; + } + if (strcmp(ptr, "any") == 0) + { + config->family = AF_UNSPEC; + } + else if (inet_pton(AF_INET, ptr, &config->addr.v4.sin_addr) == 1) + { + config->family = AF_INET; + } + else if (inet_pton(AF_INET6, ptr, &config->addr.v6.sin6_addr) == 1) + { + config->family = AF_INET6; + } + else + { + LOG_ERR("parse packet_inject->filter_ip failed, invalid ip address: %s\n", ptr); + goto error_out; + } + + ptr = toml_raw_in(sub, "filter_port"); + if (ptr == NULL) + { + LOG_ERR("config file missing packet_inject->filter_port\n"); + goto error_out; + } + config->port = atoi(ptr); + + ptr = toml_raw_in(sub, "filter_dir"); + if (ptr == NULL) + { + LOG_ERR("config file missing packet_inject->filter_dir\n"); + goto error_out; + } + if (strcmp(ptr, "C2S") == 0) + { + config->direction = FLOW_DIRECTION_C2S; + } + else if (strcmp(ptr, "S2C") == 0) + { + config->direction = FLOW_DIRECTION_S2C; + } + else + { + LOG_ERR("parse packet_inject->filter_dir failed, invalid direction: %s\n", ptr); + goto error_out; + } + + ptr = toml_raw_in(sub, "filter_pkts"); + if (ptr == NULL) + { + LOG_ERR("config file missing packet_inject->filter_pkts\n"); + goto error_out; + } + config->number = atoi(ptr); + if (config->number == 0) + { + LOG_ERR("parse packet_inject->filter_pkts failed, invalid number: %s\n", ptr); + goto error_out; + } + + ptr = toml_raw_in(sub, "inject_type"); + if (ptr == NULL) + { + LOG_ERR("config file missing packet_inject->inject_type\n"); + goto error_out; + } + if (strcmp(ptr, "TCP-RST") == 0) + { + config->type = INJECT_TCP_RST; + } + else if (strcmp(ptr, "TCP-FIN") == 0) + { + config->type = INJECT_TCP_FIN; + } + else if (strcmp(ptr, "TCP-PAYLOAD") == 0) + { + config->type = INJECT_TCP_PAYLOAD; + } + else if (strcmp(ptr, "TCP-PAYLOAD-FIN-RST") == 0) + { + config->type = INJECT_TCP_PAYLOAD_FIN_RST; + } + else if (strcmp(ptr, "UDP-PAYLOAD") == 0) + { + config->type = INJECT_UDP_PAYLOAD; + } + else if (strcmp(ptr, "CTRL-MSG") == 0) + { + config->type = INJECT_CTRL_MSG; + } + else + { + LOG_ERR("parse packet_inject->inject_type failed, invalid inject type: %s\n", ptr); + goto error_out; + } + + ret = 0; + +error_out: + if (root) + { + toml_free(root); + } + + if (fp) + { + fclose(fp); + } + + return ret; +} + +static void print_config(const struct config *config) +{ + char addr_str[INET6_ADDRSTRLEN] = {0}; + + switch (config->family) + { + case AF_INET: + inet_ntop(AF_INET, &config->addr.v4, addr_str, INET6_ADDRSTRLEN); + break; + case AF_INET6: + inet_ntop(AF_INET6, &config->addr.v6, addr_str, INET6_ADDRSTRLEN); + break; + default: + snprintf(addr_str, INET6_ADDRSTRLEN, "any"); + break; + } + + LOG_INFO("config->filter_ip : %s\n", addr_str); + LOG_INFO("config->filter_port : %d\n", config->port); + LOG_INFO("config->filter_dir : %s\n", config->direction == FLOW_DIRECTION_C2S ? "C2S" : "S2C"); + LOG_INFO("config->filter_pkts : %lu\n", config->number); + LOG_INFO("config->inject_type : %s\n", inject_type_to_str(config->type)); +} + +/****************************************************************************** + * Utils + ******************************************************************************/ + +struct packet_exdata +{ + enum flow_direction flow_dir; + + union + { + struct in_addr v4; + struct in6_addr v6; + } src_addr, dst_addr; + + uint16_t src_port; // host byte order + uint16_t dst_port; // host byte order + + uint16_t tcp_payload_len; + uint32_t tcp_seq; // host byte order + uint32_t tcp_ack; // host byte order + uint8_t tcp_flags; + + uint32_t inc_seq; + uint32_t inc_ack; +}; + +static inline void packet_exdata_init(const struct packet *pkt, enum flow_direction dir, struct packet_exdata *pkt_exdata) +{ + memset(pkt_exdata, 0, sizeof(struct packet_exdata)); + + pkt_exdata->flow_dir = dir; + + int get_inner_addr = 0; + struct layer layer; + PACKET_FOREACH_LAYER_REVERSE(pkt, layer) + { + switch (layer.proto) + { + case LAYER_PROTO_TCP: + pkt_exdata->src_port = ntohs(layer.hdr.tcp->th_sport); + pkt_exdata->dst_port = ntohs(layer.hdr.tcp->th_dport); + pkt_exdata->tcp_seq = ntohl(layer.hdr.tcp->th_seq); + pkt_exdata->tcp_ack = ntohl(layer.hdr.tcp->th_ack); + pkt_exdata->tcp_flags = layer.hdr.tcp->th_flags; + pkt_exdata->tcp_payload_len = packet_get_payload_len(pkt); + break; + case LAYER_PROTO_UDP: + pkt_exdata->src_port = ntohs(layer.hdr.udp->uh_sport); + pkt_exdata->dst_port = ntohs(layer.hdr.udp->uh_dport); + break; + case LAYER_PROTO_IPV4: + pkt_exdata->src_addr.v4 = layer.hdr.ip4->ip_src; + pkt_exdata->dst_addr.v4 = layer.hdr.ip4->ip_dst; + get_inner_addr = 1; + break; + case LAYER_PROTO_IPV6: + pkt_exdata->src_addr.v6 = layer.hdr.ip6->ip6_src; + pkt_exdata->dst_addr.v6 = layer.hdr.ip6->ip6_dst; + get_inner_addr = 1; + break; + default: + break; + } + + if (get_inner_addr) + { + break; + } + } +} + +static inline uint32_t uint32_add(uint32_t seq, uint32_t inc) +{ + if (seq > UINT32_MAX - inc) + { + seq = ((uint64_t)seq + (uint64_t)inc) % (4294967296); + } + else + { + seq += inc; + } + + return seq; +} + +static void imitate_and_send_udp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata, + enum flow_direction inject_dir, const char *udp_payload, uint16_t udp_payload_len) +{ + const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir); + if (origin_pkt == NULL) + { + LOG_ERR("imitate UDP packet failed, %s origin packet is NULL\n", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C"); + return; + } + + struct packet *imitate_pkt = imitate_udp_packet(origin_pkt, udp_payload, udp_payload_len); + if (imitate_pkt == NULL) + { + LOG_ERR("imitate UDP packet failed\n"); + return; + } + + stellar_send_crafted_packet(st, imitate_pkt); +} + +static void imitate_and_send_tcp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata, + enum flow_direction inject_dir, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len) +{ + uint32_t tcp_seq = 0; + uint32_t tcp_ack = 0; + + /* + * +--------+ current packet +---------+ C2S RST +--------+ + * | |----------------->| |----------------->| | + * | Client | | Stellar | | Server | + * | |<-----------------| |<-----------------| | + * +--------+ S2C RST +---------+ +--------+ + * + * for example: current packet is C2S + * + * inject direction == current direction (inject C2S RST) + * tcp_seq = current_packet_seq + * tcp_ack = current_packet_ack + * + * inject direction != current direction (inject S2C RST) + * tcp_seq = current_packet_ack + * tcp_ack = current_packet_seq + current_packet_payload_len + * or if current packet is a SYN-ACK packet + * tcp_seq = current_packet_seq + * tcp_ack = current_packet_ack + current_packet_payload_len + 1 + */ + + if (inject_dir == pkt_exdata->flow_dir) + { + tcp_seq = uint32_add(pkt_exdata->tcp_seq, pkt_exdata->inc_seq); + tcp_ack = pkt_exdata->tcp_ack; + + pkt_exdata->inc_seq += tcp_payload_len; + pkt_exdata->inc_seq += (tcp_flags & TH_FIN) ? 1 : 0; // inject RST packer after FIN packer, tcp_seq should be increased by 1 + } + else + { + tcp_seq = uint32_add(pkt_exdata->tcp_ack, pkt_exdata->inc_ack); + tcp_ack = uint32_add(pkt_exdata->tcp_seq, pkt_exdata->tcp_payload_len + (pkt_exdata->tcp_flags & TH_SYN ? 1 : 0)); + + pkt_exdata->inc_ack += tcp_payload_len; + pkt_exdata->inc_ack += (tcp_flags & TH_FIN) ? 1 : 0; // inject RST packer after FIN packer, ack should be increased by 1 + } + + const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir); + if (origin_pkt == NULL) + { + LOG_ERR("imitate TCP packet failed, %s origin packet is NULL\n", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C"); + return; + } + + struct packet *imitate_pkt = imitate_tcp_packet(origin_pkt, tcp_seq, tcp_ack, tcp_flags, tcp_payload, tcp_payload_len); + if (imitate_pkt == NULL) + { + LOG_ERR("imitate TCP packet failed\n"); + return; + } + + stellar_send_crafted_packet(st, imitate_pkt); +} + +/****************************************************************************** + * Core logic + ******************************************************************************/ + +struct plugin_ctx +{ + struct config config; + struct stellar *st; + int sess_plug_id; + int tcp_topic_id; + int udp_topic_id; +}; + +static void *on_sess_new(struct session *sess, void *plugin_ctx) +{ + // struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx; + LOG_INFO("handle session new: %s\n", session_get0_readable_addr(sess)); + return NULL; +} + +static void on_sess_free(struct session *sess, void *sess_ctx, void *plugin_ctx) +{ + // struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx; + LOG_INFO("handle session free: %s\n", session_get0_readable_addr(sess)); +} + +static void on_sess_msg(struct session *sess, int topic_id, const void *msg, void *sess_ctx, void *plugin_ctx) +{ + char buffer[1024] = {0}; + struct packet *pkt = (struct packet *)msg; + struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx; + struct stellar *st = ctx->st; + struct config *config = &ctx->config; + enum flow_direction flow_dir = session_get_current_flow_direction(sess); + LOG_INFO("handle session msg: %s (C2S received packets: %lu, S2C received packets: %lu)\n", + session_get0_readable_addr(sess), + session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED), + session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED)); + + struct packet_exdata pkt_exdata; + packet_exdata_init(pkt, flow_dir, &pkt_exdata); + + if (config->family == AF_INET && + memcmp(&config->addr.v4, &pkt_exdata.src_addr.v4, sizeof(struct in_addr)) != 0 && + memcmp(&config->addr.v4, &pkt_exdata.dst_addr.v4, sizeof(struct in_addr)) != 0) + { + return; + } + + if (config->family == AF_INET6 && + memcmp(&config->addr.v6, &pkt_exdata.src_addr.v6, sizeof(struct in6_addr)) != 0 && + memcmp(&config->addr.v6, &pkt_exdata.dst_addr.v6, sizeof(struct in6_addr)) != 0) + { + return; + } + + if (config->port && + pkt_exdata.src_port != config->port && + pkt_exdata.dst_port != config->port) + { + return; + } + + if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_INJECTED_PACKETS_SUCCESS) > 0 || + session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_INJECTED_PACKETS_SUCCESS) > 0) + { + return; + } + + if (config->direction == FLOW_DIRECTION_C2S && session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) != config->number) + { + return; + } + + if (config->direction == FLOW_DIRECTION_S2C && session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) != config->number) + { + return; + } + + switch (config->type) + { + case INJECT_TCP_RST: + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); + session_set_discard(sess); + break; + case INJECT_TCP_FIN: + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0); + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0); + session_set_discard(sess); + break; + case INJECT_TCP_PAYLOAD: + snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello"); + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server + session_set_discard(sess); + break; + case INJECT_TCP_PAYLOAD_FIN_RST: + snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello"); + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0); // inject FIN to client + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0); // inject FIN to server + imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server + session_set_discard(sess); + break; + case INJECT_UDP_PAYLOAD: + imitate_and_send_udp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, "Hello Server", 12); + imitate_and_send_udp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, "Hello Client", 12); + session_set_discard(sess); + break; + case INJECT_CTRL_MSG: + // TOOD + break; + default: + break; + } +} + +/****************************************************************************** + * Plugin API + ******************************************************************************/ + +extern "C" +{ + + void *packet_inject_init(struct stellar *st) + { + struct plugin_ctx *ctx = (struct plugin_ctx *)calloc(1, sizeof(struct plugin_ctx)); + if (ctx == NULL) + { + return NULL; + } + + if (load_config(&ctx->config, "./plugin/inject.toml") == -1) + { + LOG_ERR("load config failed\n"); + free(ctx); + return NULL; + } + print_config(&ctx->config); + + ctx->st = st; + ctx->sess_plug_id = stellar_session_plugin_register(st, on_sess_new, on_sess_free, ctx); + ctx->tcp_topic_id = stellar_session_mq_get_topic_id(st, TOPIC_TCP); + ctx->udp_topic_id = stellar_session_mq_get_topic_id(st, TOPIC_UDP); + + stellar_session_mq_subscribe(st, ctx->tcp_topic_id, on_sess_msg, ctx->sess_plug_id); + stellar_session_mq_subscribe(st, ctx->udp_topic_id, on_sess_msg, ctx->sess_plug_id); + + LOG_INFO("init\n"); + + return ctx; + } + + void packet_inject_exit(void *plugin_ctx) + { + struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx; + if (ctx) + { + LOG_INFO("exit\n"); + free(ctx); + } + } +}
\ No newline at end of file diff --git a/test/packet_inject/packet_inject_main.cpp b/test/packet_inject/packet_inject_main.cpp deleted file mode 100644 index 0b1f591..0000000 --- a/test/packet_inject/packet_inject_main.cpp +++ /dev/null @@ -1,166 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "stellar_utils.h" -#include "packet_inject_main.h" - -struct packet_inject_rule rule = {0}; - -static void usage(char *cmd) -{ - printf("Usage: %s [options]\n\n", cmd); - printf("Options:\n"); - printf(" -h <ip> Host IP address\n"); - printf(" -p <port> Port number\n"); - printf(" -t <type> Type of manipulation\n"); - printf(" Options: tcp-rst, tcp-fin, tcp-payload, tcp-payload-fin-rst, udp-payload, ctrl-msg\n"); - printf(" -c <condition> Condition for manipulation\n"); - printf(" Options: c2s-packet, s2c-packet\n"); - printf(" -n <number> Number of packets received before injecting action\n\n"); - printf("Example:\n"); - printf(" %s -h 192.168.1.100 -p 8080 -t tcp-payload -c c2s-packet -n 5\n", cmd); - printf(" %s -h 2001:db8::1 -p 8080 -t tcp-rst -c s2c-packet -n 10\n", cmd); - printf("\n"); -} - -static int parse_cmd(int argc, char **argv) -{ - int opt = 0; - const char *host = NULL; - const char *type = NULL; - const char *condition = NULL; - while ((opt = getopt(argc, argv, "h:p:t:c:n:")) != -1) - { - switch (opt) - { - case 'h': - host = optarg; - break; - case 'p': - rule.port = htons(atoi(optarg)); - break; - case 't': - type = optarg; - break; - case 'c': - condition = optarg; - break; - case 'n': - rule.number = atoi(optarg); - break; - default: - usage(argv[0]); - break; - } - } - - if (host) - { - if (inet_pton(AF_INET, host, &rule.addr4) != 1) - { - if (inet_pton(AF_INET6, host, &rule.addr6) != 1) - { - printf("unable to convert host %s to IPv4 / IPv6\n", host); - return -1; - } - else - { - rule.family = AF_INET6; - } - } - else - { - rule.family = AF_INET; - } - } - - if (type == NULL) - { - usage(argv[0]); - printf("invalid type\n"); - return -1; - } - else if (strcmp(type, "tcp-rst") == 0) - { - rule.inject_type = INJECT_TYPE_TCP_RST; - } - else if (strcmp(type, "tcp-fin") == 0) - { - rule.inject_type = INJECT_TYPE_TCP_FIN; - } - else if (strcmp(type, "tcp-payload") == 0) - { - rule.inject_type = INJECT_TYPE_TCP_PAYLOAD; - } - else if (strcmp(type, "tcp-payload-fin-rst") == 0) - { - rule.inject_type = INJECT_TYPE_TCP_PAYLOAD_FIN_RST; - } - else if (strcmp(type, "udp-payload") == 0) - { - rule.inject_type = INJECT_TYPE_UDP_PAYLOAD; - } - else if (strcmp(type, "ctrl-msg") == 0) - { - rule.inject_type = INJECT_TYPE_CTRL_MSG; - } - else - { - usage(argv[0]); - printf("invalid type\n"); - return -1; - } - - if (condition == NULL) - { - usage(argv[0]); - printf("invalid condition\n"); - return -1; - } - else if (strcmp(condition, "c2s-packet") == 0) - { - rule.direction = AFTER_RECV_C2S_N_PACKET; - } - else if (strcmp(condition, "s2c-packet") == 0) - { - rule.direction = AFTER_RECV_S2C_N_PACKET; - } - else - { - usage(argv[0]); - printf("invalid condition\n"); - return -1; - } - - if (rule.number <= 0) - { - usage(argv[0]); - printf("invalid count\n"); - return -1; - } - printf("%s load inject rule:\n", argv[0]); - printf(" host : %s\n", host); - printf(" port : %d\n", ntohs(rule.port)); - printf(" type : %s\n", type); - printf(" condition : %s\n", condition); - printf(" count : %lu\n\n", rule.number); - - return 0; -} - -int packet_inject_main(int argc, char **argv) -{ - if (parse_cmd(argc, argv) != 0) - { - return -1; - } - - return stellar_main(argc, argv); -} - -int __attribute__((weak)) main(int argc, char **argv) -{ - return packet_inject_main(argc, argv); -} diff --git a/test/packet_inject/packet_inject_main.h b/test/packet_inject/packet_inject_main.h deleted file mode 100644 index 7953ae9..0000000 --- a/test/packet_inject/packet_inject_main.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include <arpa/inet.h> -#include "stellar/packet.h" - -#define AFTER_RECV_C2S_N_PACKET 1 -#define AFTER_RECV_S2C_N_PACKET 2 - -enum packet_inject_type -{ - INJECT_TYPE_TCP_RST = 1, - INJECT_TYPE_TCP_FIN = 2, - INJECT_TYPE_TCP_PAYLOAD = 3, - INJECT_TYPE_TCP_PAYLOAD_FIN_RST = 4, - INJECT_TYPE_UDP_PAYLOAD = 5, - INJECT_TYPE_CTRL_MSG = 6, -}; - -struct packet_inject_rule -{ - int family; /* AF_INET or AF_INET6 */ - struct in_addr addr4; /* network order */ - struct in6_addr addr6; /* network order */ - uint16_t port; /* network order */ - - enum packet_inject_type inject_type; - - // inject packet after (C2S/S2C) receiving n packets - int direction; // AFTER_RECV_C2S_N_PACKET or AFTER_RECV_S2C_N_PACKET - uint64_t number; // n packets received -}; - -extern struct packet_inject_rule rule; - -int packet_inject_main(int argc, char **argv); - -#ifdef __cplusplus -} -#endif diff --git a/test/packet_inject/packet_inject_plugin.cpp b/test/packet_inject/packet_inject_plugin.cpp deleted file mode 100644 index 612069c..0000000 --- a/test/packet_inject/packet_inject_plugin.cpp +++ /dev/null @@ -1,177 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "stellar/layer.h" -#include "stellar/session_mq.h" -#include "packet_inject_main.h" - -struct packet_inject_plugin_ctx -{ - struct stellar *st; - int sess_plug_id; - int tcp_topic_id; - int udp_topic_id; - char name[64]; -}; - -static void *on_sess_new(struct session *sess, void *plugin_ctx) -{ - struct packet_inject_plugin_ctx *ctx = (struct packet_inject_plugin_ctx *)plugin_ctx; - printf("[%s] pluign handle session new: %s\n", ctx->name, session_get0_readable_addr(sess)); - return NULL; -} - -static void on_sess_free(struct session *sess, void *sess_ctx, void *plugin_ctx) -{ - struct packet_inject_plugin_ctx *ctx = (struct packet_inject_plugin_ctx *)plugin_ctx; - printf("[%s] pluign handle session free: %s\n", ctx->name, session_get0_readable_addr(sess)); -} - -static void on_sess_msg(struct session *sess, int topic_id, const void *msg, void *sess_ctx, void *plugin_ctx) -{ - struct packet_inject_plugin_ctx *ctx = (struct packet_inject_plugin_ctx *)plugin_ctx; - printf("[%s] pluign handle session msg: %s (C2S received packets: %lu, S2C received packets: %lu)\n", - ctx->name, session_get0_readable_addr(sess), - session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED), - session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED)); - - struct packet *pkt = (struct packet *)msg; - char buffer[1024] = {0}; - int is_ip_hit = 0; - int is_port_hit = 0; - struct layer layer; - PACKET_FOREACH_LAYER_REVERSE(pkt, layer) - { - switch (layer.proto) - { - case LAYER_PROTO_IPV4: - if (memcmp(&layer.hdr.ip4->ip_src, &rule.addr4, sizeof(struct in_addr)) == 0 || - memcmp(&layer.hdr.ip4->ip_dst, &rule.addr4, sizeof(struct in_addr)) == 0) - { - is_ip_hit = 1; - } - break; - case LAYER_PROTO_IPV6: - if (memcmp(&layer.hdr.ip6->ip6_src, &rule.addr6, sizeof(struct in6_addr)) == 0 || - memcmp(&layer.hdr.ip6->ip6_dst, &rule.addr6, sizeof(struct in6_addr)) == 0) - { - is_ip_hit = 1; - } - break; - case LAYER_PROTO_TCP: - if (layer.hdr.tcp->th_sport == rule.port || - layer.hdr.tcp->th_dport == rule.port) - { - is_port_hit = 1; - } - break; - case LAYER_PROTO_UDP: - if (layer.hdr.udp->uh_sport == rule.port || - layer.hdr.udp->uh_dport == rule.port) - { - is_port_hit = 1; - } - break; - default: - break; - } - } - - if (rule.family && !is_ip_hit) - { - return; - } - if (rule.port && !is_port_hit) - { - return; - } - if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_INJECTED_PACKETS_SUCCESS) > 0 || - session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_INJECTED_PACKETS_SUCCESS) > 0) - { - return; - } - if (rule.direction == AFTER_RECV_C2S_N_PACKET && session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) != rule.number) - { - return; - } - if (rule.direction == AFTER_RECV_S2C_N_PACKET && session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) != rule.number) - { - return; - } - switch (rule.inject_type) - { - case INJECT_TYPE_TCP_RST: - stellar_inject_tcp_rst(ctx->st, sess, FLOW_DIRECTION_C2S); - stellar_inject_tcp_rst(ctx->st, sess, FLOW_DIRECTION_S2C); - session_set_discard(sess); - break; - case INJECT_TYPE_TCP_FIN: - stellar_inject_tcp_fin(ctx->st, sess, FLOW_DIRECTION_C2S); - stellar_inject_tcp_fin(ctx->st, sess, FLOW_DIRECTION_S2C); - session_set_discard(sess); - break; - case INJECT_TYPE_TCP_PAYLOAD: - snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello"); - stellar_inject_tcp_payload(ctx->st, sess, FLOW_DIRECTION_S2C, buffer, strlen(buffer)); // inject payload to client - stellar_inject_tcp_payload(ctx->st, sess, FLOW_DIRECTION_S2C, "World\r\n", 7); // inject payload to client - stellar_inject_tcp_rst(ctx->st, sess, FLOW_DIRECTION_S2C); // inject RST to client - stellar_inject_tcp_rst(ctx->st, sess, FLOW_DIRECTION_C2S); // inject RST to server - session_set_discard(sess); - break; - case INJECT_TYPE_TCP_PAYLOAD_FIN_RST: - snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello"); - stellar_inject_tcp_payload(ctx->st, sess, FLOW_DIRECTION_S2C, buffer, strlen(buffer)); // inject payload to client - stellar_inject_tcp_payload(ctx->st, sess, FLOW_DIRECTION_S2C, "World\r\n", 7); // inject payload to client - stellar_inject_tcp_fin(ctx->st, sess, FLOW_DIRECTION_S2C); // inject FIN to client - stellar_inject_tcp_rst(ctx->st, sess, FLOW_DIRECTION_S2C); // inject RST to client - stellar_inject_tcp_fin(ctx->st, sess, FLOW_DIRECTION_C2S); // inject FIN to server - stellar_inject_tcp_rst(ctx->st, sess, FLOW_DIRECTION_C2S); // inject RST to server - session_set_discard(sess); - break; - case INJECT_TYPE_UDP_PAYLOAD: - stellar_inject_udp_payload(ctx->st, sess, FLOW_DIRECTION_C2S, "Hello Server", 12); - stellar_inject_udp_payload(ctx->st, sess, FLOW_DIRECTION_S2C, "Hello Client", 12); - session_set_discard(sess); - break; - case INJECT_TYPE_CTRL_MSG: - // TOOD - break; - default: - break; - } -} - -extern "C" -{ - void *packet_inject_plugin_init(struct stellar *st) - { - struct packet_inject_plugin_ctx *ctx = (struct packet_inject_plugin_ctx *)calloc(1, sizeof(struct packet_inject_plugin_ctx)); - if (ctx == NULL) - { - return NULL; - } - ctx->st = st; - ctx->sess_plug_id = stellar_session_plugin_register(st, on_sess_new, on_sess_free, ctx); - ctx->tcp_topic_id = stellar_session_mq_get_topic_id(st, TOPIC_TCP); - ctx->udp_topic_id = stellar_session_mq_get_topic_id(st, TOPIC_UDP); - snprintf(ctx->name, sizeof(ctx->name), "packet_inject"); - - stellar_session_mq_subscribe(st, ctx->tcp_topic_id, on_sess_msg, ctx->sess_plug_id); - stellar_session_mq_subscribe(st, ctx->udp_topic_id, on_sess_msg, ctx->sess_plug_id); - - printf("[%s] plugin init\n", ctx->name); - - return ctx; - } - - void packet_inject_plugin_exit(void *plugin_ctx) - { - struct packet_inject_plugin_ctx *ctx = (struct packet_inject_plugin_ctx *)plugin_ctx; - if (ctx) - { - printf("[%s] plugin exit\n", ctx->name); - free(ctx); - } - } -} diff --git a/test/packet_inject/packet_inject_test.cpp b/test/packet_inject/packet_inject_test.cpp deleted file mode 100644 index dfe5801..0000000 --- a/test/packet_inject/packet_inject_test.cpp +++ /dev/null @@ -1,181 +0,0 @@ -#include <stdarg.h> -#include <unistd.h> -#include <sys/stat.h> -#include <gtest/gtest.h> - -#include "packet_inject_test.h" -#include "packet_inject_main.h" - -static int args_len(const char **args) -{ - int i = 0; - while (args[i] != NULL) - { - i++; - } - return i; -} - -static void system_cmd(const char *cmd, ...) -{ - char buf[1024] = {0}; - va_list args; - - va_start(args, cmd); - vsnprintf(buf, sizeof(buf), cmd, args); - va_end(args); - - system(buf); -} - -static int replace_file_string(const char *file, const char *old_str, const char *new_str) -{ -#define BUFFER_SIZE 1024 - - FILE *in_fp = fopen(file, "r"); - if (in_fp == NULL) - { - printf("Open file %s failed, %s\n", file, strerror(errno)); - return -1; - } - - FILE *tmp_fp = tmpfile(); - if (tmp_fp == NULL) - { - printf("Create temporary file failed, %s\n", strerror(errno)); - fclose(in_fp); - return -1; - } - - size_t old_len = strlen(old_str); - size_t new_len = strlen(new_str); - char buff[BUFFER_SIZE]; - - while (fgets(buff, BUFFER_SIZE, in_fp)) - { - char *pos = buff; - if ((pos = strstr(pos, old_str))) - { - fwrite(buff, 1, pos - buff, tmp_fp); // Write characters before the old_str - fwrite(new_str, 1, new_len, tmp_fp); // Write the new_str - pos += old_len; // Move past the old_str - fwrite(pos, 1, strlen(pos), tmp_fp); // Write characters after the old_str - } - else - { - fputs(buff, tmp_fp); // Write the remaining part of the line - } - } - - fclose(in_fp); - fseek(tmp_fp, 0, SEEK_SET); - - FILE *out_fp = fopen(file, "w"); - if (out_fp == NULL) - { - printf("Open file %s for writing failed, %s\n", file, strerror(errno)); - fclose(tmp_fp); - return -1; - } - - while (fgets(buff, BUFFER_SIZE, tmp_fp)) - { - fputs(buff, out_fp); // Write the contents of the temporary file to the original file - } - - fclose(tmp_fp); - fclose(out_fp); - - return 0; -} - -static void expect_cmp_inject(const char *expect_pcap_file, const char *inject_pcap_file, const char *diff_skip_pattern, int idx) -{ - struct stat s; - char expect_pcap_json[1024] = {0}; - char inject_pcap_json[1024] = {0}; - char diff_json_txt[1024] = {0}; - - snprintf(expect_pcap_json, sizeof(expect_pcap_json), "expect_pcap_%d.json", idx); - snprintf(inject_pcap_json, sizeof(inject_pcap_json), "inject_pcap_%d.json", idx); - snprintf(diff_json_txt, sizeof(diff_json_txt), "json_diff_%d.txt", idx); - - stat(expect_pcap_file, &s); - EXPECT_TRUE(s.st_size > 0); - - stat(inject_pcap_file, &s); - EXPECT_TRUE(s.st_size > 0); - - printf("\033[32m tcpdump read expect pcap (%s) \033[0m\n", expect_pcap_file); - system_cmd("tcpdump -r %s", expect_pcap_file); - - printf("\033[32m tcpdump read inject pcap (%s) \033[0m\n", inject_pcap_file); - system_cmd("tcpdump -r %s", inject_pcap_file); - - system_cmd("tshark -r %s -T json | jq >> %s", expect_pcap_file, expect_pcap_json); - system_cmd("tshark -r %s -T json | jq >> %s", inject_pcap_file, inject_pcap_json); - - stat(expect_pcap_json, &s); - EXPECT_TRUE(s.st_size > 0); - - stat(inject_pcap_json, &s); - EXPECT_TRUE(s.st_size > 0); - - system_cmd("diff %s %s %s >> %s", diff_skip_pattern, expect_pcap_json, inject_pcap_json, diff_json_txt); - - stat(diff_json_txt, &s); - EXPECT_TRUE(s.st_size == 0); -} - -void packet_inject_test(struct packet_inject_case *test) -{ - printf("\033[32m ============================================= \033[0m\n"); - printf("\033[32mTest: %s\033[0m\n", test->descriptor); - printf("\033[32m ============================================= \033[0m\n"); - - // create directory - char dumpfile_dir[1024] = {0}; - snprintf(dumpfile_dir, sizeof(dumpfile_dir), "%s/input/", test->work_dir); - system_cmd("rm -rf %s", test->work_dir); - system_cmd("mkdir -p %s", dumpfile_dir); - system_cmd("mkdir -p %s/log/", test->work_dir); - - // copy file to work directory - for (int i = 0; i < MAX_COMPARISON; i++) - { - if (test->compares[i].expect_pcap) - { - system_cmd("cp %s/%s %s", test->input_prefix, test->compares[i].expect_pcap, test->work_dir); - } - } - system_cmd("cp %s/%s %s", test->input_prefix, test->input_pcap, dumpfile_dir); - system_cmd("cp -r conf %s/", test->work_dir); - system_cmd("cp -r plugin %s/", test->work_dir); - system_cmd("cp -r libpacket_inject_plugin.so %s/", test->work_dir); - - // run packet injector - char cwd[2048] = {0}; - char temp[2048] = {0}; - getcwd(cwd, sizeof(cwd)); - chdir(test->work_dir); - snprintf(temp, sizeof(temp), "dumpfile_dir = \"%s\"", dumpfile_dir); - EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "mode = marsio", "mode = dumpfile") == 0); - EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "dumpfile_dir = \"/tmp/dumpfile/\"", temp) == 0); - packet_inject_main(args_len(test->packet_injector_cmd), (char **)test->packet_injector_cmd); - - // compare pcap - for (int i = 0; i < MAX_COMPARISON; i++) - { - if (test->compares[i].expect_pcap && test->compares[i].inject_pcap) - { - expect_cmp_inject(test->compares[i].expect_pcap, test->compares[i].inject_pcap, test->diff_skip_pattern, i + 1); - } - } - - // clean work directory - if (test->finish_clean_work_dir) - { - system_cmd("rm -rf %s", test->work_dir); - } - chdir(cwd); -} diff --git a/test/packet_inject/packet_inject_test.h b/test/packet_inject/packet_inject_test.h index 0c5511b..f69680c 100644 --- a/test/packet_inject/packet_inject_test.h +++ b/test/packet_inject/packet_inject_test.h @@ -5,15 +5,21 @@ extern "C" { #endif +#include <stdarg.h> +#include <unistd.h> +#include <sys/stat.h> +#include <gtest/gtest.h> + +#include "stellar/stellar.h" + +#define BUFFER_SIZE 1024 #define MAX_COMPARISON 16 struct packet_inject_case { - int finish_clean_work_dir; - const char *descriptor; const char *work_dir; - const char *input_prefix; + const char *pcap_dir; const char *input_pcap; struct @@ -22,11 +28,165 @@ struct packet_inject_case const char *inject_pcap; } compares[MAX_COMPARISON]; - const char *packet_injector_cmd[16]; + const char *plugin_config_file; const char *diff_skip_pattern; }; -void packet_inject_test(struct packet_inject_case *test); +static inline void system_cmd(const char *cmd, ...) +{ + char buf[1024] = {0}; + va_list args; + + va_start(args, cmd); + vsnprintf(buf, sizeof(buf), cmd, args); + va_end(args); + + system(buf); +} + +static inline int replace_file_string(const char *file, const char *old_str, const char *new_str) +{ + FILE *in_fp = fopen(file, "r"); + if (in_fp == NULL) + { + printf("Open file %s failed, %s\n", file, strerror(errno)); + return -1; + } + + FILE *tmp_fp = tmpfile(); + if (tmp_fp == NULL) + { + printf("Create temporary file failed, %s\n", strerror(errno)); + fclose(in_fp); + return -1; + } + + size_t old_len = strlen(old_str); + size_t new_len = strlen(new_str); + char buff[BUFFER_SIZE]; + + while (fgets(buff, BUFFER_SIZE, in_fp)) + { + char *pos = buff; + if ((pos = strstr(pos, old_str))) + { + fwrite(buff, 1, pos - buff, tmp_fp); // Write characters before the old_str + fwrite(new_str, 1, new_len, tmp_fp); // Write the new_str + pos += old_len; // Move past the old_str + fwrite(pos, 1, strlen(pos), tmp_fp); // Write characters after the old_str + } + else + { + fputs(buff, tmp_fp); // Write the remaining part of the line + } + } + + fclose(in_fp); + fseek(tmp_fp, 0, SEEK_SET); + + FILE *out_fp = fopen(file, "w"); + if (out_fp == NULL) + { + printf("Open file %s for writing failed, %s\n", file, strerror(errno)); + fclose(tmp_fp); + return -1; + } + + while (fgets(buff, BUFFER_SIZE, tmp_fp)) + { + fputs(buff, out_fp); // Write the contents of the temporary file to the original file + } + + fclose(tmp_fp); + fclose(out_fp); + + return 0; +} + +static inline void expect_cmp_inject(const char *expect_pcap_file, const char *inject_pcap_file, const char *diff_skip_pattern, int idx) +{ + struct stat s; + char expect_pcap_json[1024] = {0}; + char inject_pcap_json[1024] = {0}; + char diff_json_txt[1024] = {0}; + + snprintf(expect_pcap_json, sizeof(expect_pcap_json), "expect_pcap_%d.json", idx); + snprintf(inject_pcap_json, sizeof(inject_pcap_json), "inject_pcap_%d.json", idx); + snprintf(diff_json_txt, sizeof(diff_json_txt), "json_diff_%d.txt", idx); + + stat(expect_pcap_file, &s); + EXPECT_TRUE(s.st_size > 0); + + stat(inject_pcap_file, &s); + EXPECT_TRUE(s.st_size > 0); + + printf("\033[32m tcpdump read expect pcap (%s) \033[0m\n", expect_pcap_file); + system_cmd("tcpdump -r %s", expect_pcap_file); + + printf("\033[32m tcpdump read inject pcap (%s) \033[0m\n", inject_pcap_file); + system_cmd("tcpdump -r %s", inject_pcap_file); + + system_cmd("tshark -r %s -T json | jq >> %s", expect_pcap_file, expect_pcap_json); + system_cmd("tshark -r %s -T json | jq >> %s", inject_pcap_file, inject_pcap_json); + + stat(expect_pcap_json, &s); + EXPECT_TRUE(s.st_size > 0); + + stat(inject_pcap_json, &s); + EXPECT_TRUE(s.st_size > 0); + + system_cmd("diff %s %s %s >> %s", diff_skip_pattern, expect_pcap_json, inject_pcap_json, diff_json_txt); + + stat(diff_json_txt, &s); + EXPECT_TRUE(s.st_size == 0); +} + +static inline void packet_inject_test(struct packet_inject_case *test) +{ + // create directory + char dumpfile_dir[1024] = {0}; + snprintf(dumpfile_dir, sizeof(dumpfile_dir), "%s/input/", test->work_dir); + system_cmd("rm -rf %s", test->work_dir); + system_cmd("mkdir -p %s/input/", test->work_dir); + system_cmd("mkdir -p %s/log/", test->work_dir); + system_cmd("mkdir -p %s/conf/", test->work_dir); + system_cmd("mkdir -p %s/plugin/", test->work_dir); + + // copy file + for (int i = 0; i < MAX_COMPARISON; i++) + { + if (test->compares[i].expect_pcap) + { + system_cmd("cp %s/%s %s", test->pcap_dir, test->compares[i].expect_pcap, test->work_dir); + } + } + system_cmd("cp %s/%s %s/input/", test->pcap_dir, test->input_pcap, test->work_dir); + system_cmd("cp conf/log.toml %s/conf/", test->work_dir); + system_cmd("cp conf/stellar.toml %s/conf/", test->work_dir); + system_cmd("cp conf/spec.toml %s/plugin/", test->work_dir); + system_cmd("cp conf/%s %s/plugin/inject.toml", test->plugin_config_file, test->work_dir); + system_cmd("cp libpacket_inject.so %s/plugin/", test->work_dir); + + // run + char cwd[2048] = {0}; + char temp[2048] = {0}; + getcwd(cwd, sizeof(cwd)); + chdir(test->work_dir); + snprintf(temp, sizeof(temp), "dumpfile_dir = \"%s\"", dumpfile_dir); + EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "mode = marsio", "mode = dumpfile") == 0); + EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "dumpfile_dir = \"/tmp/dumpfile/\"", temp) == 0); + stellar_run(0, NULL); + + // compare + for (int i = 0; i < MAX_COMPARISON; i++) + { + if (test->compares[i].expect_pcap && test->compares[i].inject_pcap) + { + expect_cmp_inject(test->compares[i].expect_pcap, test->compares[i].inject_pcap, test->diff_skip_pattern, i + 1); + } + } + chdir(cwd); +} #ifdef __cplusplus } diff --git a/test/packet_inject/plugin/spec.toml b/test/packet_inject/plugin/spec.toml deleted file mode 100644 index dec3483..0000000 --- a/test/packet_inject/plugin/spec.toml +++ /dev/null @@ -1,4 +0,0 @@ -[[plugin]] -path = "./libpacket_inject_plugin.so" -init = "packet_inject_plugin_init" -exit = "packet_inject_plugin_exit"
\ No newline at end of file diff --git a/test/packet_inject/version.map b/test/packet_inject/version.map index 1aa6729..2cdaaf9 100644 --- a/test/packet_inject/version.map +++ b/test/packet_inject/version.map @@ -1,7 +1,7 @@ -LIBPACKET_INJECT_PLUGIN { +LIBPACKET_INJECT { global: - packet_inject_plugin_init; - packet_inject_plugin_exit; + packet_inject_init; + packet_inject_exit; local: *; }; diff --git a/test/packet_parser/cmp_layers.sh b/test/packet_parser/cmp_layers.sh index 99a6486..923b818 100644 --- a/test/packet_parser/cmp_layers.sh +++ b/test/packet_parser/cmp_layers.sh @@ -2,8 +2,8 @@ input=$1 if [ -d "$input" ]; then - input_dir=$input - pcap_files=($(find ${input_dir} -type f -name "*.pcap")) + pcap_dir=$input + pcap_files=($(find ${pcap_dir} -type f -name "*.pcap")) elif [ -f "$input" ]; then input_file=$input pcap_files=($input_file) |
