remove tuple.h from include/stellar

This commit is contained in:
luwenpeng
2024-06-07 15:17:35 +08:00
parent 4c0ad823d4
commit 10528bcfd3
13 changed files with 227 additions and 103 deletions

View File

@@ -1,5 +1,4 @@
install(FILES stellar/utils.h DESTINATION include/stellar/ COMPONENT LIBRARIES) install(FILES stellar/utils.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/tuple.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/packet.h DESTINATION include/stellar/ COMPONENT LIBRARIES) install(FILES stellar/packet.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/session.h DESTINATION include/stellar/ COMPONENT LIBRARIES) install(FILES stellar/session.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/stellar.h DESTINATION include/stellar/ COMPONENT LIBRARIES) install(FILES stellar/stellar.h DESTINATION include/stellar/ COMPONENT LIBRARIES)

View File

@@ -6,7 +6,13 @@ extern "C"
{ {
#endif #endif
#include "stellar/tuple.h" #include <string.h>
#include <arpa/inet.h>
#define __FAVOR_BSD 1
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
enum layer_type enum layer_type
{ {
@@ -59,28 +65,6 @@ struct packet_layer
uint16_t pld_len; // payload length uint16_t pld_len; // payload length
}; };
// return 0: found
// return -1: not found
int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
const struct packet_layer *packet_get_innermost_layer(const struct packet *pkt, enum layer_type type);
const struct packet_layer *packet_get_outermost_layer(const struct packet *pkt, enum layer_type type);
/******************************************************************************
* Utils
******************************************************************************/
#define MAX_SID_NUM 8 #define MAX_SID_NUM 8
struct sid_list struct sid_list
{ {
@@ -117,29 +101,6 @@ void packet_set_action(struct packet *pkt, enum packet_action action);
enum packet_action packet_get_action(const struct packet *pkt); enum packet_action packet_get_action(const struct packet *pkt);
/* /*
******************************************************************************
* Example: getting the innermost TCP layer
******************************************************************************
*
* |<--------------------------- pkt->data_len -------------------------->|
* +----------+------+-----+-------+------+---------------+---------------+
* | Ethernet | IPv4 | UDP | GTP-U | IPv4 | TCP | Payload |
* +----------+------+-----+-------+------+---------------+---------------+
* ^ ^ ^
* | | |
* |<------------ hdr_offset ------------>|<-- hdr_len -->|<-- pld_len -->|
* | | |
* | | +-- pld_ptr
* | +-- hdr_ptr
* +-- data_ptr
*
* const struct packet_layer *tcp_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_TCP);
* const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
* uint16_t src_port = ntohs(hdr->th_sport);
* uint16_t dst_port = ntohs(hdr->th_dport);
* uint32_t seq = ntohl(hdr->th_seq);
* uint32_t ack = ntohl(hdr->th_ack);
*
****************************************************************************** ******************************************************************************
* Example: foreach layer in packet * Example: foreach layer in packet
****************************************************************************** ******************************************************************************
@@ -160,6 +121,168 @@ enum packet_action packet_get_action(const struct packet *pkt);
* } * }
*/ */
struct address
{
uint8_t family; // AF_INET or AF_INET6
union
{
struct in_addr v4; /* network order */
struct in6_addr v6; /* network order */
} data;
};
static inline int packet_get_addr(const struct packet *pkt, struct address *src_addr, struct address *dst_addr)
{
const struct ip *ip4_hdr = NULL;
const struct ip6_hdr *ip6_hdr = NULL;
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_IPV4)
{
ip4_hdr = (const struct ip *)layer->hdr_ptr;
if (src_addr != NULL)
{
src_addr->family = AF_INET;
src_addr->data.v4.s_addr = ip4_hdr->ip_src.s_addr;
}
if (dst_addr != NULL)
{
dst_addr->family = AF_INET;
dst_addr->data.v4.s_addr = ip4_hdr->ip_dst.s_addr;
}
return 0;
}
if (layer->type & LAYER_TYPE_IPV6)
{
ip6_hdr = (const struct ip6_hdr *)layer->hdr_ptr;
if (src_addr != NULL)
{
src_addr->family = AF_INET6;
src_addr->data.v6 = ip6_hdr->ip6_src;
}
if (dst_addr != NULL)
{
dst_addr->family = AF_INET6;
dst_addr->data.v6 = ip6_hdr->ip6_dst;
}
return 0;
}
}
return -1;
}
static inline int packet_get_port(const struct packet *pkt, uint16_t *src_port, uint16_t *dst_port)
{
const struct tcphdr *tcp_hdr = NULL;
const struct udphdr *udp_hdr = NULL;
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_TCP)
{
tcp_hdr = (const struct tcphdr *)layer->hdr_ptr;
src_port != NULL ? *src_port = tcp_hdr->th_sport : 0;
dst_port != NULL ? *dst_port = tcp_hdr->th_dport : 0;
return 0;
}
if (layer->type & LAYER_TYPE_UDP)
{
udp_hdr = (const struct udphdr *)layer->hdr_ptr;
src_port != NULL ? *src_port = udp_hdr->uh_sport : 0;
dst_port != NULL ? *dst_port = udp_hdr->uh_dport : 0;
return 0;
}
}
return -1;
}
static inline int packet_get_ip_hdr(const struct packet *pkt, struct ip *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_IPV4)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct ip));
}
return 0;
}
}
return -1;
}
static inline int packet_get_ip6_hdr(const struct packet *pkt, struct ip6_hdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_IPV6)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct ip6_hdr));
}
return 0;
}
}
return -1;
}
static inline int packet_get_tcp_hdr(const struct packet *pkt, struct tcphdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_TCP)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct tcphdr));
}
return 0;
}
}
return -1;
}
static inline int packet_get_udp_hdr(const struct packet *pkt, struct udphdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_UDP)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct udphdr));
}
return 0;
}
}
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -139,10 +139,6 @@ enum closing_reason session_get_closing_reason(const struct session *sess);
enum session_direction session_get_direction(const struct session *sess); enum session_direction session_get_direction(const struct session *sess);
enum flow_direction session_get_current_flow_direction(const struct session *sess); enum flow_direction session_get_current_flow_direction(const struct session *sess);
const struct packet *session_get_first_packet(const struct session *sess, enum flow_direction dir); const struct packet *session_get_first_packet(const struct session *sess, enum flow_direction dir);
const struct tuple6 *session_get_tuple6(const struct session *sess);
enum flow_direction session_get_tuple6_direction(const struct session *sess);
uint64_t session_get_id(const struct session *sess); uint64_t session_get_id(const struct session *sess);
uint64_t session_get_timestamp(const struct session *sess, enum session_timestamp type); uint64_t session_get_timestamp(const struct session *sess, enum session_timestamp type);
uint64_t session_get_stat(const struct session *sess, enum flow_direction dir, enum session_stat stat); uint64_t session_get_stat(const struct session *sess, enum flow_direction dir, enum session_stat stat);

View File

@@ -7,7 +7,7 @@ extern "C"
#endif #endif
#include "log.h" #include "log.h"
#include "stellar/tuple.h" #include "tuple.h"
#define EVICTED_SESSION_FILTER_LOG_ERROR(format, ...) LOG_ERROR("evicted session filter", format, ##__VA_ARGS__) #define EVICTED_SESSION_FILTER_LOG_ERROR(format, ...) LOG_ERROR("evicted session filter", format, ##__VA_ARGS__)

View File

@@ -10,7 +10,7 @@ extern "C"
#include <stdio.h> #include <stdio.h>
#include "log.h" #include "log.h"
#include "stellar/tuple.h" #include "tuple.h"
#include "stellar/packet.h" #include "stellar/packet.h"
#define PACKET_MAX_LAYERS 32 #define PACKET_MAX_LAYERS 32
@@ -60,6 +60,24 @@ void packet_print_str(const struct packet *pkt);
// direction 0: I2E // direction 0: I2E
uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction); uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
const struct packet_layer *packet_get_innermost_layer(const struct packet *pkt, enum layer_type type);
const struct packet_layer *packet_get_outermost_layer(const struct packet *pkt, enum layer_type type);
/****************************************************************************** /******************************************************************************
* Utils * Utils
******************************************************************************/ ******************************************************************************/

View File

@@ -10,7 +10,7 @@ extern "C"
#include "packet_priv.h" #include "packet_priv.h"
#include "timeout.h" #include "timeout.h"
#include "uthash.h" #include "uthash.h"
#include "stellar/tuple.h" #include "tuple.h"
#include "stellar/session.h" #include "stellar/session.h"
#include "tcp_reassembly.h" #include "tcp_reassembly.h"
#include "session_manager.h" #include "session_manager.h"
@@ -84,7 +84,10 @@ void session_init(struct session *sess);
void session_set_id(struct session *sess, uint64_t id); void session_set_id(struct session *sess, uint64_t id);
void session_set_tuple(struct session *sess, const struct tuple6 *key); void session_set_tuple(struct session *sess, const struct tuple6 *key);
const struct tuple6 *session_get_tuple6(const struct session *sess);
void session_set_tuple_direction(struct session *sess, enum flow_direction dir); void session_set_tuple_direction(struct session *sess, enum flow_direction dir);
enum flow_direction session_get_tuple6_direction(const struct session *sess);
void session_set_direction(struct session *sess, enum session_direction dir); void session_set_direction(struct session *sess, enum session_direction dir);
void session_set_current_flow_direction(struct session *sess, enum flow_direction dir); void session_set_current_flow_direction(struct session *sess, enum flow_direction dir);

View File

@@ -1,13 +1,5 @@
LIBSTELLAR_DEVEL { LIBSTELLAR_DEVEL {
global: global:
packet_get_innermost_tuple2;
packet_get_outermost_tuple2;
packet_get_innermost_tuple4;
packet_get_outermost_tuple4;
packet_get_innermost_tuple6;
packet_get_outermost_tuple6;
packet_get_innermost_layer;
packet_get_outermost_layer;
packet_get_direction; packet_get_direction;
packet_get_session_id; packet_get_session_id;
packet_prepend_sid_list; packet_prepend_sid_list;
@@ -19,6 +11,12 @@ global:
packet_get_payload_len; packet_get_payload_len;
packet_set_action; packet_set_action;
packet_get_action; packet_get_action;
packet_get_addr;
packet_get_port;
packet_get_ip_hdr;
packet_get_ip6_hdr;
packet_get_tcp_hdr;
packet_get_udp_hdr;
session_exdata_free; session_exdata_free;
stellar_session_exdata_new_index; stellar_session_exdata_new_index;
@@ -44,8 +42,6 @@ global:
session_get_direction; session_get_direction;
session_get_current_flow_direction; session_get_current_flow_direction;
session_get_first_packet; session_get_first_packet;
session_get_tuple6;
session_get_tuple6_direction;
session_get_id; session_get_id;
session_get_timestamp; session_get_timestamp;
session_get_stat; session_get_stat;
@@ -64,22 +60,5 @@ global:
stellar_inject_ctrl_msg; stellar_inject_ctrl_msg;
stellar_main; stellar_main;
tuple2_hash;
tuple4_hash;
tuple5_hash;
tuple6_hash;
tuple2_cmp;
tuple4_cmp;
tuple5_cmp;
tuple6_cmp;
tuple2_reverse;
tuple4_reverse;
tuple5_reverse;
tuple6_reverse;
tuple2_to_str;
tuple4_to_str;
tuple5_to_str;
tuple6_to_str;
local: *; local: *;
}; };

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "stellar/tuple.h" #include "tuple.h"
TEST(TUPLE, TUPLE2) TEST(TUPLE, TUPLE2)
{ {

View File

@@ -1,7 +1,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "stellar/tuple.h" #include "tuple.h"
#include "crc32_hash.h" #include "crc32_hash.h"
uint32_t tuple2_hash(const struct tuple2 *tuple) uint32_t tuple2_hash(const struct tuple2 *tuple)

View File

@@ -1,5 +1,5 @@
#ifndef _TUPLE_PUB_H #ifndef _TUPLE_H
#define _TUPLE_PUB_H #define _TUPLE_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"

View File

@@ -58,21 +58,21 @@ static int parse_cmd(int argc, char **argv)
if (host) if (host)
{ {
if (inet_pton(AF_INET, host, &rule.v4) != 1) if (inet_pton(AF_INET, host, &rule.addr.data.v4) != 1)
{ {
if (inet_pton(AF_INET6, host, &rule.v6) != 1) if (inet_pton(AF_INET6, host, &rule.addr.data.v6) != 1)
{ {
printf("unable to convert host %s to IPv4 / IPv6\n", host); printf("unable to convert host %s to IPv4 / IPv6\n", host);
return -1; return -1;
} }
else else
{ {
rule.ip_type = 6; rule.addr.family = AF_INET6;
} }
} }
else else
{ {
rule.ip_type = 4; rule.addr.family = AF_INET;
} }
} }

View File

@@ -7,6 +7,7 @@ extern "C"
#endif #endif
#include <arpa/inet.h> #include <arpa/inet.h>
#include "stellar/packet.h"
#define AFTER_RECV_C2S_N_PACKET 1 #define AFTER_RECV_C2S_N_PACKET 1
#define AFTER_RECV_S2C_N_PACKET 2 #define AFTER_RECV_S2C_N_PACKET 2
@@ -23,10 +24,8 @@ enum packet_inject_type
struct packet_inject_rule struct packet_inject_rule
{ {
int ip_type; struct address addr; /* network order */
struct in_addr v4; /* network order */ uint16_t port; /* network order */
struct in6_addr v6; /* network order */
int port; /* network order */
enum packet_inject_type inject_type; enum packet_inject_type inject_type;

View File

@@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "stellar/tuple.h" #include "stellar/packet.h"
#include "stellar/session_mq.h" #include "stellar/session_mq.h"
#include "packet_inject_main.h" #include "packet_inject_main.h"
@@ -36,22 +36,29 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi
session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED), session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED),
session_get_stat(sess, FLOW_DIRECTION_S2C, 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}; char buffer[1024] = {0};
// struct packet *pkt = (struct packet *)msg; uint16_t src_port = 0;
const struct tuple6 *tuple = session_get_tuple6(sess); uint16_t dst_port = 0;
if (rule.ip_type == 4 && struct address src_addr = {0};
memcmp(&tuple->src_addr.v4, &rule.v4, sizeof(struct in_addr)) && struct address dst_addr = {0};
memcmp(&tuple->dst_addr.v4, &rule.v4, sizeof(struct in_addr)))
packet_get_addr(pkt, &src_addr, &dst_addr);
packet_get_port(pkt, &src_port, &dst_port);
if (rule.addr.family == AF_INET &&
memcmp(&src_addr.data.v4, &rule.addr.data.v4, sizeof(struct in_addr)) &&
memcmp(&dst_addr.data.v4, &rule.addr.data.v4, sizeof(struct in_addr)))
{ {
return; return;
} }
if (rule.ip_type == 6 && if (rule.addr.family == AF_INET6 &&
memcmp(&tuple->src_addr.v6, &rule.v6, sizeof(struct in6_addr)) && memcmp(&src_addr.data.v6, &rule.addr.data.v6, sizeof(struct in6_addr)) &&
memcmp(&tuple->dst_addr.v6, &rule.v6, sizeof(struct in6_addr))) memcmp(&dst_addr.data.v6, &rule.addr.data.v6, sizeof(struct in6_addr)))
{ {
return; return;
} }
if (rule.port != 0 && tuple->src_port != rule.port && tuple->dst_port != rule.port) if (rule.port != 0 && src_port != rule.port && dst_port != rule.port)
{ {
return; return;
} }