Add the packet_injector tool to test the packet injection effect

This commit is contained in:
luwenpeng
2024-05-11 18:58:36 +08:00
parent 7f1e7a23de
commit cda77c6f6f
17 changed files with 851 additions and 337 deletions

View File

@@ -60,5 +60,6 @@ add_subdirectory(vendor)
add_subdirectory(deps)
add_subdirectory(src)
add_subdirectory(script)
add_subdirectory(test)
install(DIRECTORY DESTINATION log)

View File

@@ -7,11 +7,13 @@ extern "C"
#endif
#include <stdint.h>
#include "stellar/session.h"
uint16_t stellar_get_current_thread_index();
// return 0: success, -1: failed
int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inject_dir, uint8_t flags);
// return inject packet length, return 0 if failed
int stellar_inject_tcp_rst(const struct session *sess, enum flow_direction inject_dir);
int stellar_inject_tcp_fin(const struct session *sess, enum flow_direction inject_dir);
int stellar_inject_payload(const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len);
int stellar_inject_ctrl_msg(const struct session *sess, const struct sid_list *sids, const char *msg, uint16_t len);

View File

@@ -471,8 +471,6 @@ int session_to_json(struct session *sess, char *buff, int size)
enum flow_direction dir[] = {FLOW_DIRECTION_C2S, FLOW_DIRECTION_S2C};
for (int i = 0; i < 2; i++)
{
used += snprintf(buff + used, size - used, "\"%s_first_packet\":\"%p\",", str[i], session_get_first_packet(sess, dir[i]));
// raw packets
used += snprintf(buff + used, size - used, "\"%s_raw_packets_received\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_RAW_PACKETS_RECEIVED));
used += snprintf(buff + used, size - used, "\"%s_raw_bytes_received\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_RAW_BYTES_RECEIVED));
@@ -500,7 +498,7 @@ int session_to_json(struct session *sess, char *buff, int size)
used += snprintf(buff + used, size - used, "\"%s_control_bytes_transmitted\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_CONTROL_BYTES_TRANSMITTED));
used += snprintf(buff + used, size - used, "\"%s_control_packets_dropped\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_CONTROL_PACKETS_DROPPED));
used += snprintf(buff + used, size - used, "\"%s_control_bytes_dropped\":%" PRIu64 "", str[i], session_get_stat(sess, dir[i], STAT_CONTROL_BYTES_DROPPED));
used += snprintf(buff + used, size - used, "\"%s_control_bytes_dropped\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_CONTROL_BYTES_DROPPED));
if (session_get_type(sess) == SESSION_TYPE_TCP)
{
@@ -538,6 +536,7 @@ int session_to_json(struct session *sess, char *buff, int size)
used += snprintf(buff + used, size - used, "\"%s_tcp_payloads_released\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PAYLOADS_RELEASED));
}
used += snprintf(buff + used, size - used, "\"%s_first_packet\":\"%p\"", str[i], session_get_first_packet(sess, dir[i]));
if (i == 0)
{
used += snprintf(buff + used, size - used, ",");

View File

@@ -285,7 +285,8 @@ static void tcp_update(struct session_manager *mgr, struct session *sess, enum f
uint8_t flags = tcp_hdr_get_flags(hdr);
uint16_t len = tcp_layer->pld_len;
half->flags |= flags;
half->flags = flags;
half->history |= flags;
half->seq = tcp_hdr_get_seq(hdr);
half->ack = tcp_hdr_get_ack(hdr);
half->len = tcp_layer->pld_len;
@@ -771,12 +772,12 @@ static int session_manager_update_tcp_session(struct session_manager *mgr, struc
case SESSION_STATE_CLOSING:
if (flags & TH_FIN)
{
timeout = (peer->flags & TH_FIN) ? mgr->opts.tcp_time_wait_timeout : mgr->opts.tcp_half_closed_timeout;
timeout = (peer->history & TH_FIN) ? mgr->opts.tcp_time_wait_timeout : mgr->opts.tcp_half_closed_timeout;
}
else if (flags & TH_RST)
{
// if fin is received, the expected sequence number should be increased by 1
uint32_t expected = (peer->flags & TH_FIN) ? peer->ack + 1 : peer->ack;
uint32_t expected = (peer->history & TH_FIN) ? peer->ack + 1 : peer->ack;
timeout = (expected == curr->seq) ? mgr->opts.tcp_time_wait_timeout : mgr->opts.tcp_unverified_rst_timeout;
}
else

View File

@@ -30,6 +30,7 @@ struct tcp_half
uint32_t ack;
uint16_t len;
uint8_t flags;
uint8_t history;
};
/*

View File

@@ -1,9 +1,6 @@
add_library(core config.cpp stat.cpp stellar.cpp inject.cpp)
target_link_libraries(core timestamp plugin_manager session_manager ip_reassembly packet_io)
target_link_libraries(core pthread fieldstat4 toml)
target_link_libraries(core timestamp plugin_manager session_manager ip_reassembly packet_io pthread fieldstat4 toml)
add_executable(stellar main.cpp)
target_link_libraries(stellar core)
install(TARGETS stellar RUNTIME DESTINATION bin COMPONENT Program)
add_subdirectory(test)
target_link_libraries(stellar core plugin_manager)
install(TARGETS stellar RUNTIME DESTINATION bin COMPONENT Program)

View File

@@ -1,5 +1,7 @@
#include <time.h>
#include <errno.h>
#include <assert.h>
#include "tcp_utils.h"
#include "udp_utils.h"
#include "ipv4_utils.h"
@@ -12,6 +14,13 @@
#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__)
struct tcp_fingerprint
{
uint16_t ipid;
uint8_t ttl;
uint16_t win;
};
static uint16_t checksum(const char *data, uint16_t len)
{
uint32_t sum = 0;
@@ -155,7 +164,7 @@ static void update_ip6_hdr(struct ip6_hdr *ip6hdr, int trim)
ipv6_hdr_set_payload_len(ip6hdr, len - trim);
}
static inline void calc_tcp_seq_and_ack(const struct session *sess, enum flow_direction inject_dir, uint32_t *seq, uint32_t *ack)
static inline void calc_tcp_seq_ack(const struct session *sess, enum flow_direction inject_dir, uint32_t *seq, uint32_t *ack)
{
/*
* +--------+ current packet +---------+ C2S RST +--------+
@@ -173,6 +182,9 @@ static inline void calc_tcp_seq_and_ack(const struct session *sess, enum flow_di
* 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_flow_direction(sess);
@@ -185,36 +197,26 @@ static inline void calc_tcp_seq_and_ack(const struct session *sess, enum flow_di
else
{
*seq = tcp_curr_half->ack;
*ack = tcp_curr_half->seq + tcp_curr_half->len;
*ack = tcp_curr_half->seq + tcp_curr_half->len + (tcp_curr_half->flags & TH_SYN ? 1 : 0);
}
}
static inline void calc_ipid_ttl_win(uint16_t *ipid, uint8_t *ttl, uint16_t *win)
static inline void calc_tcp_fingerprint(struct tcp_fingerprint *finger)
{
#define RANGE(rand, start, end) (start + rand % (end - start + 1)) // [start, end]
struct timespec curtime;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &curtime);
uint64_t random = (0x013579ABCDEF ^ (uint64_t)curtime.tv_nsec);
*ipid = (uint16_t)(RANGE(random, 32767, 65535));
*ttl = (uint8_t)(RANGE(random, 48, 120));
*win = (uint16_t)(RANGE(random, 1000, 1460));
finger->ipid = (uint16_t)(RANGE(random, 32767, 65535));
finger->ttl = (uint8_t)(RANGE(random, 48, 120));
finger->win = (uint16_t)(RANGE(random, 1000, 1460));
}
/******************************************************************************
* Public API
******************************************************************************/
struct fingerprint
{
uint16_t ipid;
uint8_t ttl;
uint16_t win;
};
// return packet length
static int build_tcp_packet(const struct packet *first, const struct fingerprint *finger,
uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, char *tcp_pld, int pld_len, char *pkt_buff, int buff_size)
int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t ip_ttl,
uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, uint16_t tcp_win,
const char *tcp_pld, int pld_len, char *pkt_buff, int buff_size)
{
int trim = 0;
struct tcphdr *tcphdr;
@@ -244,12 +246,12 @@ static int build_tcp_packet(const struct packet *first, const struct fingerprint
{
case LAYER_TYPE_TCP:
trim = curr->hdr_len + curr->pld_len - sizeof(struct tcphdr) + pld_len;
if (len + trim > buff_size)
if (len - trim > buff_size)
{
return -ENOMEM;
}
tcphdr = (struct tcphdr *)(pkt_buff + curr->hdr_offset);
update_tcp_hdr(tcphdr, tcp_seq, tcp_ack, finger->win, TH_RST | TH_ACK);
update_tcp_hdr(tcphdr, tcp_seq, tcp_ack, tcp_win, tcp_flags);
if (pld_len)
{
memcpy(pkt_buff + curr->hdr_offset + sizeof(struct tcphdr), tcp_pld, pld_len);
@@ -270,9 +272,9 @@ static int build_tcp_packet(const struct packet *first, const struct fingerprint
if (last->type == LAYER_TYPE_UDP)
{
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset);
udphdr->uh_sum = checksum_v4(udphdr, len - trim - last->hdr_offset + pld_len, IPPROTO_UDP, &iphdr->ip_src, &iphdr->ip_dst);
udphdr->uh_sum = checksum_v4(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &iphdr->ip_src, &iphdr->ip_dst);
}
update_ip4_hdr(iphdr, finger->ipid, finger->ttl, trim);
update_ip4_hdr(iphdr, ip_id, ip_ttl, trim);
break;
case LAYER_TYPE_IPV6:
ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset);
@@ -285,7 +287,7 @@ static int build_tcp_packet(const struct packet *first, const struct fingerprint
if (last->type == LAYER_TYPE_UDP)
{
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset);
udphdr->uh_sum = checksum_v6(udphdr, len - trim - last->hdr_offset + pld_len, IPPROTO_UDP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
udphdr->uh_sum = checksum_v6(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
}
update_ip6_hdr(ip6hdr, trim);
break;
@@ -301,39 +303,120 @@ static int build_tcp_packet(const struct packet *first, const struct fingerprint
return len - trim;
}
// return 0: success, -1: failed
int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inject_dir, uint8_t flags)
// return packet length
int build_udp_packet(const struct packet *first, const char *udp_pld, int pld_len, char *pkt_buff, int buff_size)
{
int trim = 0;
struct udphdr *udphdr;
struct ip *iphdr;
struct ip6_hdr *ip6hdr;
struct packet_layer *curr;
struct packet_layer *last;
int len = packet_get_len(first);
int8_t layers = packet_get_layers_number(first);
if ((udp_pld == NULL && pld_len > 0) || (udp_pld != NULL && pld_len <= 0))
{
return -EINVAL;
}
if (len > buff_size)
{
return -ENOMEM;
}
memcpy(pkt_buff, packet_get_data(first), len);
for (int8_t i = layers - 1; i >= 0; i--)
{
curr = (struct packet_layer *)packet_get_layer(first, i);
switch (curr->type)
{
case LAYER_TYPE_UDP:
trim = curr->hdr_len + curr->pld_len - sizeof(struct udphdr) + pld_len;
if (len - trim > buff_size)
{
return -ENOMEM;
}
udphdr = (struct udphdr *)(pkt_buff + curr->hdr_offset);
update_udp_hdr(udphdr, trim);
if (pld_len)
{
memcpy(pkt_buff + curr->hdr_offset + sizeof(struct udphdr), udp_pld, pld_len);
}
break;
case LAYER_TYPE_IPV4:
iphdr = (struct ip *)(pkt_buff + curr->hdr_offset);
last = (struct packet_layer *)packet_get_layer(first, i + 1);
if (last->type == LAYER_TYPE_UDP)
{
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset);
udphdr->uh_sum = checksum_v4(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &iphdr->ip_src, &iphdr->ip_dst);
}
update_ip4_hdr(iphdr, 0, 0, trim);
break;
case LAYER_TYPE_IPV6:
ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset);
last = (struct packet_layer *)packet_get_layer(first, i + 1);
if (last->type == LAYER_TYPE_UDP)
{
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset);
udphdr->uh_sum = checksum_v6(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
}
update_ip6_hdr(ip6hdr, trim);
break;
case LAYER_TYPE_GRE:
return -EPROTONOSUPPORT;
// TODO
break;
default:
break;
}
}
return len - trim;
}
int inject_tcp_packet(const struct session *sess, enum flow_direction inject_dir, uint8_t tcp_flags, const char *payload, uint16_t 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)
uint16_t thr_idx = stellar_get_current_thread_index();
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 is not a TCP session, cannot inject TCP RST", session_get_id(sess));
return -1;
INJECT_PACKET_LOG_ERROR("session %ld is not a TCP session, cannot inject TCP packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d)",
session_get_id(sess), TCP_FLAGS_LOG_VALUE(tcp_flags), 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 has no %s first packet, cannot inject TCP RST", session_get_id(sess), flow_direction_to_str(inject_dir));
return -1;
INJECT_PACKET_LOG_ERROR("session %ld has no %s first packet, cannot inject TCP packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d)",
session_get_id(sess), flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), len);
return 0;
}
struct fingerprint finger = {0};
struct tcp_fingerprint finger = {0};
uint32_t tcp_seq = 0;
uint32_t tcp_ack = 0;
char buff[4096] = {0};
calc_tcp_seq_and_ack(sess, inject_dir, &tcp_seq, &tcp_ack);
calc_ipid_ttl_win(&finger.ipid, &finger.ttl, &finger.win);
calc_tcp_seq_ack(sess, inject_dir, &tcp_seq, &tcp_ack);
calc_tcp_fingerprint(&finger);
int len = build_tcp_packet(pkt, &finger, tcp_seq, tcp_ack, flags, NULL, 0, buff, sizeof(buff));
if (len <= 0)
int pkt_len = build_tcp_packet(pkt, finger.ipid, finger.ttl, tcp_seq, tcp_ack, tcp_flags, finger.win, payload, len, buff, sizeof(buff));
if (pkt_len <= 0)
{
INJECT_PACKET_LOG_ERROR("session %ld build TCP %s RST packet failed, %s", session_get_id(sess), flow_direction_to_str(inject_dir), strerror(len));
INJECT_PACKET_LOG_ERROR("session %ld build TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) failed, %s",
session_get_id(sess), flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), len, strerror(len));
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return -1;
return 0;
}
struct inject_packet_meta meta = {0};
@@ -342,31 +425,112 @@ int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inj
session_get_sid_list(sess, inject_dir, &meta.sids);
struct packet inj_pkt;
packet_parse(&inj_pkt, buff, len);
packet_parse(&inj_pkt, buff, pkt_len);
packet_set_origin(&inj_pkt, PACKET_ORIGIN_USERSTACK);
packet_set_origin_ctx(&inj_pkt, &meta);
if (packet_io_inject(runtime->packet_io, thr_idx, &inj_pkt, 1) == 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, len);
return 0;
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_BYTES_SUCCESS, pkt_len);
INJECT_PACKE_LOG_DEBUG("session %ld inject TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) success",
session_get_id(sess), flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), len);
return pkt_len;
}
else
{
INJECT_PACKET_LOG_ERROR("session %ld inject TCP %s RST packet failed, packet I/O nospace", session_get_id(sess), flow_direction_to_str(inject_dir));
INJECT_PACKET_LOG_ERROR("session %ld inject TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) failed, packet I/O nospace",
session_get_id(sess), flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), len);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return -1;
return 0;
}
}
int inject_udp_packet(const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len)
{
uint16_t thr_idx = stellar_get_current_thread_index();
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 is not a UDP session, cannot inject UDP packet (payload len:%d)",
session_get_id(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 has no %s first packet, cannot inject UDP packet (payload len:%d)",
session_get_id(sess), flow_direction_to_str(inject_dir), len);
return 0;
}
char buff[4096] = {0};
int pkt_len = build_udp_packet(pkt, payload, len, buff, sizeof(buff));
if (pkt_len <= 0)
{
INJECT_PACKET_LOG_ERROR("session %ld build UDP %s packet (payload len:%d) failed, %s",
session_get_id(sess), flow_direction_to_str(inject_dir), len, strerror(len));
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return 0;
}
struct inject_packet_meta meta = {0};
meta.session_id = session_get_id(sess);
session_get_route_ctx(sess, inject_dir, &meta.route);
session_get_sid_list(sess, inject_dir, &meta.sids);
struct packet inj_pkt;
packet_parse(&inj_pkt, buff, pkt_len);
packet_set_origin(&inj_pkt, PACKET_ORIGIN_USERSTACK);
packet_set_origin_ctx(&inj_pkt, &meta);
if (packet_io_inject(runtime->packet_io, thr_idx, &inj_pkt, 1) == 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 inject UDP %s packet (payload len:%d) success",
session_get_id(sess), flow_direction_to_str(inject_dir), len);
return pkt_len;
}
else
{
INJECT_PACKET_LOG_ERROR("session %ld inject UDP %s packet (payload len:%d) failed, packet I/O nospace",
session_get_id(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(const struct session *sess, enum flow_direction inject_dir)
{
return inject_tcp_packet(sess, inject_dir, TH_RST | TH_ACK, NULL, 0);
}
int stellar_inject_tcp_fin(const struct session *sess, enum flow_direction inject_dir)
{
return inject_tcp_packet(sess, inject_dir, TH_FIN | TH_ACK, NULL, 0);
}
int stellar_inject_payload(const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len)
{
// TODO
return -1;
switch (session_get_type(sess))
{
case SESSION_TYPE_TCP:
return inject_tcp_packet(sess, inject_dir, TH_ACK, payload, len);
case SESSION_TYPE_UDP:
return inject_udp_packet(sess, inject_dir, payload, len);
default:
return 0;
}
}
int stellar_inject_ctrl_msg(const struct session *sess, const struct sid_list *sids, const char *msg, uint16_t len)
{
// TODO
return -1;
}
return 0;
}

21
src/stellar/inject_priv.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef _INJECT_PRIV_H
#define _INJECT_PRIV_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/stellar.h"
// return packet length
int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t ip_ttl,
uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, uint16_t tcp_win,
const char *tcp_pld, int pld_len, char *pkt_buff, int buff_size);
int build_udp_packet(const struct packet *first, const char *udp_pld, int pld_len, char *pkt_buff, int buff_size);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,5 +0,0 @@
add_executable(gtest_inject_packet gtest_inject_packet.cpp ../config.cpp ../stat.cpp ../stellar.cpp ../inject.cpp)
target_link_libraries(gtest_inject_packet timestamp session_manager ip_reassembly packet_io pthread fieldstat4 toml gtest)
file(COPY ./conf/log.toml DESTINATION ./conf/)
file(COPY ./conf/stellar.toml DESTINATION ./conf/)

View File

@@ -1,272 +0,0 @@
#include <gtest/gtest.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <netinet/tcp.h>
#include "logo.h"
#include "config.h"
#include "timestamp.h"
#include "id_generator.h"
#include "stellar_priv.h"
#include "session_priv.h"
#define GMOCK_PLUGIN_MANAGER_LOG_ERROR(format, ...) LOG_ERROR("gmock plugin manager", format, ##__VA_ARGS__)
#define GMOCK_PLUGIN_MANAGER_LOG_DEBUG(format, ...) LOG_DEBUG("gomck plugin manager", format, ##__VA_ARGS__)
static void inject_tcp_rst(struct session *sess)
{
uint64_t session_num = sess->mgr_stat->total_nr_tcp_sess_used;
switch (session_num)
{
case 1:
// recv SYN packet
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) == 1 &&
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) == 0)
{
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == -1);
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv SYN packet",
session_get_id(sess), session_get_tuple_str(sess));
}
break;
case 2:
// recv SYN-ACK packet
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) == 1 &&
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) == 1)
{
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == 0);
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv SYN-ACK packet",
session_get_id(sess), session_get_tuple_str(sess));
}
break;
case 3:
// recv sub-ACK packet
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) == 2 &&
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) == 1)
{
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == 0);
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv sub-ACK packet",
session_get_id(sess), session_get_tuple_str(sess));
}
break;
case 4:
// recv C2S Payload
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_INJECTED_PACKETS_SUCCESS) == 0 &&
session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_TCP_SEGMENTS_RECEIVED) == 1)
{
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == 0);
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv C2S first payload packet",
session_get_id(sess), session_get_tuple_str(sess));
}
break;
case 5:
// recv S2C Payload
if (session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_INJECTED_PACKETS_SUCCESS) == 0 &&
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_TCP_SEGMENTS_RECEIVED) == 1)
{
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == 0);
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv S2C first payload packet",
session_get_id(sess), session_get_tuple_str(sess));
}
break;
default:
break;
}
}
/******************************************************************************
* gmock plugin manager
******************************************************************************/
struct plugin_manager
{
};
void *plugin_manager_new_ctx(struct session *sess)
{
return sess;
}
void plugin_manager_free_ctx(void *ctx)
{
struct session *sess = (struct session *)ctx;
char buff[4096] = {0};
session_to_json(sess, buff, sizeof(buff));
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> session: %s", buff);
}
struct plugin_manager *plugin_manager_new(void)
{
static struct plugin_manager mgr;
return &mgr;
}
void plugin_manager_free(struct plugin_manager *mgr)
{
}
void plugin_manager_dispatch_session(struct plugin_manager *mgr, struct session *sess, struct packet *pkt)
{
struct tcp_segment *seg;
enum session_type type = session_get_type(sess);
uint16_t thr_idx = stellar_get_current_thread_index();
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> thread [%d] plugin dispatch session: %lu %s, type: %s, state: %s", thr_idx,
session_get_id(sess), session_get_tuple_str(sess), session_type_to_str(type), session_state_to_str(session_get_state(sess)));
if (packet_is_ctrl(pkt))
{
}
else
{
switch (type)
{
case SESSION_TYPE_TCP:
while ((seg = session_get_tcp_segment(sess)) != NULL)
{
session_free_tcp_segment(sess, seg);
}
inject_tcp_rst(sess);
break;
case SESSION_TYPE_UDP:
break;
default:
assert(0);
break;
}
}
}
void plugin_manager_dispatch_packet(struct plugin_manager *mgr, struct packet *pkt)
{
}
/******************************************************************************
* gmock main
******************************************************************************/
static const char *log_config_file = "./conf/log.toml";
static const char *stellar_config_file = "./conf/stellar.toml";
static void signal_handler(int signo)
{
if (signo == SIGINT)
{
STELLAR_LOG_STATE("SIGINT received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGQUIT)
{
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGTERM)
{
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGHUP)
{
STELLAR_LOG_STATE("SIGHUP received, reload log level !!!");
log_reload_level(log_config_file);
}
}
int main(int argc, char **argv)
{
timestamp_update();
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
if (log_init(log_config_file) != 0)
{
STELLAR_LOG_ERROR("unable to init log");
goto error_out;
}
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", __stellar_version, logo_str);
if (stellar_load_config(stellar_config_file, config) != 0)
{
STELLAR_LOG_ERROR("unable to load config file");
goto error_out;
}
stellar_print_config(config);
STELLAR_LOG_DEBUG("sizeof(struct session) = %lu bytes", sizeof(struct session));
if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0)
{
STELLAR_LOG_ERROR("unable to init id generator");
goto error_out;
}
runtime->stat = stellar_stat_new(config->io_opts.nr_threads);
if (runtime->stat == NULL)
{
STELLAR_LOG_ERROR("unable to create stellar stat");
goto error_out;
}
runtime->plug_mgr = plugin_manager_new();
if (runtime->plug_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create plugin manager");
goto error_out;
}
runtime->packet_io = packet_io_new(&config->io_opts);
if (runtime->packet_io == NULL)
{
STELLAR_LOG_ERROR("unable to create packet io");
goto error_out;
}
if (stellar_thread_init(runtime, config) != 0)
{
STELLAR_LOG_ERROR("unable to init thread context");
goto error_out;
}
if (stellar_thread_run(runtime, config) != 0)
{
STELLAR_LOG_ERROR("unable to create worker thread");
goto error_out;
}
runtime->stat_last_output_ts = timestamp_get_msec();
while (!ATOMIC_READ(&runtime->need_exit))
{
timestamp_update();
if (timestamp_get_msec() - runtime->stat_last_output_ts > 2000)
{
runtime->stat_last_output_ts = timestamp_get_msec();
stellar_stat_output(runtime->stat);
}
usleep(1000); // 1ms
}
error_out:
stellar_thread_join(runtime, config);
stellar_thread_clean(runtime, config);
packet_io_free(runtime->packet_io);
plugin_manager_free(runtime->plug_mgr);
stellar_stat_free(runtime->stat);
STELLAR_LOG_STATE("stellar exit !!!\n");
log_free();
return 0;
}

15
test/CMakeLists.txt Normal file
View File

@@ -0,0 +1,15 @@
add_executable(gtest_build_tcp_packet gtest_build_tcp_packet.cpp)
target_link_libraries(gtest_build_tcp_packet core gtest)
add_executable(gtest_build_udp_packet gtest_build_udp_packet.cpp)
target_link_libraries(gtest_build_udp_packet core gtest)
include(GoogleTest)
gtest_discover_tests(gtest_build_tcp_packet)
gtest_discover_tests(gtest_build_udp_packet)
add_executable(packet_injector packet_injector.cpp)
target_link_libraries(packet_injector core gtest)
file(COPY ./conf/log.toml DESTINATION ./conf/)
file(COPY ./conf/stellar.toml DESTINATION ./conf/)

32
test/README.md Normal file
View File

@@ -0,0 +1,32 @@
# Inject Packet
## 捕包
``` shell
/opt/tsg/mrzcpd/bin/mrpdump -- --pdump "dev_name=nf_0_fw,queue=*" --bpf-rule="host 192.0.2.110 and port 80" --dumpfile-path=/home/admin/fw.pcap
tcpdump -i virtio_dign_c host 192.0.2.110 and port 80 -n -v -w virtio_dign_c.pcap
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
kubectl -n tsg-os-system exec -it dign-client-9h8rm -c dign-client sh
curl -v http://http.badssl.selftest.gdnt-cloud.website --resolve "http.badssl.selftest.gdnt-cloud.website:80:192.0.2.110"
```

View File

@@ -0,0 +1,63 @@
#include <gtest/gtest.h>
#include "inject_priv.h"
/******************************************************************************
* BUILD_TCP_PACKET
******************************************************************************/
// IPv4
TEST(BUILD_IPV4_BASED_TCP_PACKET, WITH_RST)
{
// TODO
}
TEST(BUILD_IPV4_BASED_TCP_PACKET, WITH_FIN)
{
// TODO
}
TEST(BUILD_IPV4_BASED_TCP_PACKET, WITH_PAYLOD)
{
// TODO
}
// IPv6
TEST(BUILD_IPV6_BASED_TCP_PACKET, WITH_RST)
{
// TODO
}
TEST(BUILD_IPV6_BASED_TCP_PACKET, WITH_FIN)
{
// TODO
}
TEST(BUILD_IPV6_BASED_TCP_PACKET, WITH_PAYLOD)
{
// TODO
}
// IPv4 + IPv6
TEST(BUILD_IPV4_IPV6_BASED_TCP_PACKET, WITH_RST)
{
// TODO
}
// IPv6 + IPv4
TEST(BUILD_IPV6_IPV4_BASED_TCP_PACKET, WITH_RST)
{
// TODO
}
// GRE
TEST(BUILD_GRE_BASED_TCP_PACKET, WITH_RST)
{
// TODO
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,53 @@
#include <gtest/gtest.h>
#include "inject_priv.h"
/******************************************************************************
* BUILD_UDP_PACKET
******************************************************************************/
// IPv4
TEST(BUILD_IPV4_BASED_UDP_PACKET, NO_PAYLOD)
{
// TODO
}
TEST(BUILD_IPV4_BASED_UDP_PACKET, WITH_PAYLOD)
{
// TODO
}
// IPv6
TEST(BUILD_IPV6_BASED_UDP_PACKET, NO_PAYLOD)
{
// TODO
}
TEST(BUILD_IPV6_BASED_UDP_PACKET, WITH_PAYLOD)
{
// TODO
}
// IPv4 + IPv6
TEST(BUILD_IPV4_IPV6_BASED_UDP_PACKET, WITH_PAYLOD)
{
// TODO
}
// IPv6 + IPv4
TEST(BUILD_IPV6_IPV4_BASED_UDP_PACKET, WITH_PAYLOD)
{
// TODO
}
// GRE
TEST(BUILD_GRE_BASED_UDP_PACKET, WITH_PAYLOD)
{
// TODO
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

442
test/packet_injector.cpp Normal file
View File

@@ -0,0 +1,442 @@
#include <gtest/gtest.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <netinet/tcp.h>
#include "logo.h"
#include "config.h"
#include "timestamp.h"
#include "id_generator.h"
#include "stellar_priv.h"
#include "session_priv.h"
#include "inject_priv.h"
#include "stellar/tuple.h"
#define MOCK_PLUGIN_LOG_DEBUG(format, ...) LOG_DEBUG("mock plugin", format, ##__VA_ARGS__)
/******************************************************************************
* gmock plugin manager
******************************************************************************/
enum condition
{
AFTER_RECV_C2S_N_PACKET = 1,
AFTER_RECV_S2C_N_PACKET = 2,
};
enum inject_type
{
INJECT_TYPE_TCP_RST = 1,
INJECT_TYPE_TCP_FIN = 2,
INJECT_TYPE_TCP_PAYLOAD = 3,
INJECT_TYPE_UDP_PAYLOAD = 4,
INJECT_TYPE_CTRL_MSG = 5,
};
struct inject_rule
{
int ip_type;
struct in_addr v4; /* network order */
struct in6_addr v6; /* network order */
int port; /* network order */
enum inject_type inject_type;
enum condition count_dir;
uint64_t count_num;
} rule;
static void inject_packet_plugin(struct session *sess, struct packet *pkt, struct inject_rule *rule)
{
const struct tuple6 *tuple = session_get_tuple(sess);
if (rule->ip_type == 4 && memcmp(&tuple->src_addr.v4, &rule->v4, sizeof(struct in_addr)) && memcmp(&tuple->dst_addr.v4, &rule->v4, sizeof(struct in_addr)))
{
return;
}
if (rule->ip_type == 6 && memcmp(&tuple->src_addr.v6, &rule->v6, sizeof(struct in6_addr)) && memcmp(&tuple->dst_addr.v6, &rule->v6, sizeof(struct in6_addr)))
{
return;
}
if (rule->port != 0 && tuple->src_port != rule->port && tuple->dst_port != rule->port)
{
return;
}
if (rule->count_dir == AFTER_RECV_C2S_N_PACKET && session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) != rule->count_num)
{
return;
}
if (rule->count_dir == AFTER_RECV_S2C_N_PACKET && session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) != rule->count_num)
{
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;
}
switch (rule->inject_type)
{
case INJECT_TYPE_TCP_RST:
packet_set_action(pkt, PACKET_ACTION_DROP);
EXPECT_TRUE(stellar_inject_tcp_rst(sess, FLOW_DIRECTION_C2S) > 0);
EXPECT_TRUE(stellar_inject_tcp_rst(sess, FLOW_DIRECTION_S2C) > 0);
break;
case INJECT_TYPE_TCP_FIN:
packet_set_action(pkt, PACKET_ACTION_DROP);
EXPECT_TRUE(stellar_inject_tcp_fin(sess, FLOW_DIRECTION_C2S) > 0);
EXPECT_TRUE(stellar_inject_tcp_fin(sess, FLOW_DIRECTION_S2C) > 0);
break;
case INJECT_TYPE_TCP_PAYLOAD:
case INJECT_TYPE_UDP_PAYLOAD:
packet_set_action(pkt, PACKET_ACTION_DROP);
EXPECT_TRUE(stellar_inject_payload(sess, FLOW_DIRECTION_C2S, "Hello Server", 12) > 0);
EXPECT_TRUE(stellar_inject_payload(sess, FLOW_DIRECTION_S2C, "Hello Client", 12) > 0);
break;
case INJECT_TYPE_CTRL_MSG:
// TOOD
break;
default:
break;
}
}
/******************************************************************************
* mock plugin manager
******************************************************************************/
struct plugin_manager
{
};
void *plugin_manager_new_ctx(struct session *sess)
{
return sess;
}
void plugin_manager_free_ctx(void *ctx)
{
struct session *sess = (struct session *)ctx;
char buff[4096] = {0};
session_to_json(sess, buff, sizeof(buff));
MOCK_PLUGIN_LOG_DEBUG("=> session: %s", buff);
}
struct plugin_manager *plugin_manager_new(void)
{
static struct plugin_manager mgr;
return &mgr;
}
void plugin_manager_free(struct plugin_manager *mgr)
{
}
void plugin_manager_dispatch_session(struct plugin_manager *mgr, struct session *sess, struct packet *pkt)
{
struct tcp_segment *seg;
enum session_type type = session_get_type(sess);
uint16_t thr_idx = stellar_get_current_thread_index();
MOCK_PLUGIN_LOG_DEBUG("=> thread: %d, session: %lu %s, type: %s, state: %s, c2s packet received: %lu, s2c packet received: %lu", thr_idx,
session_get_id(sess), session_get_tuple_str(sess),
session_type_to_str(type), session_state_to_str(session_get_state(sess)),
session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED),
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED));
if (packet_is_ctrl(pkt))
{
}
else
{
switch (type)
{
case SESSION_TYPE_TCP:
while ((seg = session_get_tcp_segment(sess)) != NULL)
{
session_free_tcp_segment(sess, seg);
}
break;
case SESSION_TYPE_UDP:
break;
default:
assert(0);
break;
}
inject_packet_plugin(sess, pkt, &rule);
}
}
void plugin_manager_dispatch_packet(struct plugin_manager *mgr, struct packet *pkt)
{
}
/******************************************************************************
* main
******************************************************************************/
// curl -v http://http.badssl.selftest.gdnt-cloud.website --resolve "http.badssl.selftest.gdnt-cloud.website:80:192.0.2.110"
static const char *log_config_file = "./conf/log.toml";
static const char *stellar_config_file = "./conf/stellar.toml";
static void signal_handler(int signo)
{
if (signo == SIGINT)
{
STELLAR_LOG_STATE("SIGINT received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGQUIT)
{
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGTERM)
{
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGHUP)
{
STELLAR_LOG_STATE("SIGHUP received, reload log level !!!");
log_reload_level(log_config_file);
}
}
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, 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_cmdline(int argc, char **argv, struct inject_rule *rule)
{
memset(rule, 0, sizeof(struct inject_rule));
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->count_num = atoi(optarg);
break;
default:
usage(argv[0]);
break;
}
}
if (host)
{
if (inet_pton(AF_INET, host, &rule->v4) != 1)
{
if (inet_pton(AF_INET6, host, &rule->v6) != 1)
{
printf("unable to convert host %s to IPv4 / IPv6\n", host);
return -1;
}
else
{
rule->ip_type = 6;
}
}
else
{
rule->ip_type = 4;
}
}
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, "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->count_dir = AFTER_RECV_C2S_N_PACKET;
}
else if (strcmp(condition, "s2c-packet") == 0)
{
rule->count_dir = AFTER_RECV_S2C_N_PACKET;
}
else
{
usage(argv[0]);
printf("invalid condition\n");
return -1;
}
if (rule->count_num <= 0)
{
usage(argv[0]);
printf("invalid count\n");
return -1;
}
printf("load inject rule:\n");
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->count_num);
return 0;
}
int main(int argc, char **argv)
{
if (parse_cmdline(argc, argv, &rule) != 0)
{
return -1;
}
timestamp_update();
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
if (log_init(log_config_file) != 0)
{
STELLAR_LOG_ERROR("unable to init log");
goto error_out;
}
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", __stellar_version, logo_str);
if (stellar_load_config(stellar_config_file, config) != 0)
{
STELLAR_LOG_ERROR("unable to load config file");
goto error_out;
}
stellar_print_config(config);
STELLAR_LOG_DEBUG("sizeof(struct session) = %lu bytes", sizeof(struct session));
if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0)
{
STELLAR_LOG_ERROR("unable to init id generator");
goto error_out;
}
runtime->stat = stellar_stat_new(config->io_opts.nr_threads);
if (runtime->stat == NULL)
{
STELLAR_LOG_ERROR("unable to create stellar stat");
goto error_out;
}
runtime->plug_mgr = plugin_manager_new();
if (runtime->plug_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create plugin manager");
goto error_out;
}
runtime->packet_io = packet_io_new(&config->io_opts);
if (runtime->packet_io == NULL)
{
STELLAR_LOG_ERROR("unable to create packet io");
goto error_out;
}
if (stellar_thread_init(runtime, config) != 0)
{
STELLAR_LOG_ERROR("unable to init thread context");
goto error_out;
}
if (stellar_thread_run(runtime, config) != 0)
{
STELLAR_LOG_ERROR("unable to create worker thread");
goto error_out;
}
runtime->stat_last_output_ts = timestamp_get_msec();
while (!ATOMIC_READ(&runtime->need_exit))
{
timestamp_update();
if (timestamp_get_msec() - runtime->stat_last_output_ts > 2000)
{
runtime->stat_last_output_ts = timestamp_get_msec();
stellar_stat_output(runtime->stat);
}
usleep(1000); // 1ms
}
error_out:
stellar_thread_join(runtime, config);
stellar_thread_clean(runtime, config);
packet_io_free(runtime->packet_io);
plugin_manager_free(runtime->plug_mgr);
stellar_stat_free(runtime->stat);
STELLAR_LOG_STATE("stellar exit !!!\n");
log_free();
return 0;
}