Refactored packet API to support struct layer (using union to contain different types of encapsulation headers)

This commit is contained in:
luwenpeng
2024-06-14 19:24:27 +08:00
parent 1f78881cbb
commit de4c15f43c
47 changed files with 834 additions and 701 deletions

View File

@@ -1,4 +1,5 @@
install(FILES stellar/utils.h DESTINATION include/stellar/ COMPONENT LIBRARIES) install(FILES stellar/utils.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/layer.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)

99
include/stellar/layer.h Normal file
View File

@@ -0,0 +1,99 @@
#ifndef _LAYER_H
#define _LAYER_H
#ifdef __cplusplus
extern "C"
{
#endif
#define __FAVOR_BSD 1
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet/ip_icmp.h>
#include <linux/if_ether.h>
#include <linux/mpls.h>
enum layer_proto
{
LAYER_PROTO_NONE = 0,
// L2 -- data link layer
LAYER_PROTO_ETHER = 1,
LAYER_PROTO_PWETH = 2,
LAYER_PROTO_PPP = 3,
LAYER_PROTO_L2TP = 4,
// L2 -- tunnel
LAYER_PROTO_VLAN = 21,
LAYER_PROTO_PPPOE = 22,
LAYER_PROTO_MPLS = 23,
// L3 -- network layer
LAYER_PROTO_IPV4 = 31,
LAYER_PROTO_IPV6 = 32,
LAYER_PROTO_IPAH = 33,
// L3 -- tunnel
LAYER_PROTO_GRE = 41,
// L4 -- transport layer
LAYER_PROTO_UDP = 51,
LAYER_PROTO_TCP = 52,
LAYER_PROTO_ICMP = 53,
LAYER_PROTO_ICMP6 = 54,
// L4 -- tunnel
LAYER_PROTO_VXLAN = 61,
LAYER_PROTO_GTPV1_U = 62,
};
struct layer
{
enum layer_proto proto;
uint16_t payload_len;
uint16_t header_len;
union
{
struct ethhdr *eth;
struct ip *ip4;
struct ip6_hdr *ip6;
struct tcphdr *tcp;
struct udphdr *udp;
struct icmphdr *icmp4;
struct icmp6_hdr *icmp6;
struct mpls_label *mpls;
const char *raw;
} header;
const char *payload;
};
int packet_get_layer_count(const struct packet *pkt);
// return 0: success 
// return -1: failed
int packet_get_layer(const struct packet *pkt, int idx, struct layer *out);
#define PACKET_FOREACH_LAYER_INORDER(pkt, layer) \
for (int i = 0; i < packet_get_layer_count(pkt) && packet_get_layer(pkt, i, &layer) == 0; i++)
#define PACKET_FOREACH_LAYER_REVERSE(pkt, layer) \
for (int i = packet_get_layer_count(pkt) - 1; i >= 0 && packet_get_layer(pkt, i, &layer) == 0; i--)
#define PACKET_GETALL_LAYERS(pkt, layers) \
{ \
int size = sizeof(layers) / sizeof(layers[0]); \
int num = packet_get_layer_count(pkt); \
if (num > size) \
num = size; \
for (int i = 0; i < num && packet_get_layer(pkt, i, &layers[i]) == 0; i++) \
/* void */; \
return num; \
}
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,179 +1,44 @@
#ifndef _PACKET_PUB_H #ifndef _PACKET_H
#define _PACKET_PUB_H #define _PACKET_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
#include <string.h> #include <stdint.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_proto #define MAX_SIDS 8
struct sids
{ {
LAYER_PROTO_NONE = 0, uint16_t sid[MAX_SIDS];
// L2 -- data link layer
LAYER_PROTO_ETHER = 1,
LAYER_PROTO_PWETH = 2,
LAYER_PROTO_PPP = 3,
LAYER_PROTO_L2TP = 4,
// L2 -- tunnel
LAYER_PROTO_VLAN = 21,
LAYER_PROTO_PPPOE = 22,
LAYER_PROTO_MPLS = 23,
// L3 -- network layer
LAYER_PROTO_IPV4 = 31,
LAYER_PROTO_IPV6 = 32,
LAYER_PROTO_IPAH = 33,
// L3 -- tunnel
LAYER_PROTO_GRE = 41,
// L4 -- transport layer
LAYER_PROTO_UDP = 51,
LAYER_PROTO_TCP = 52,
LAYER_PROTO_ICMP = 53,
LAYER_PROTO_ICMP6 = 54,
// L4 -- tunnel
LAYER_PROTO_VXLAN = 61,
LAYER_PROTO_GTPV1_U = 62,
};
struct raw_layer
{
enum layer_proto type;
const char *hdr_ptr; // header pointer
const char *pld_ptr; // payload pointer
uint16_t hdr_offset; // header offset from data_ptr
uint16_t hdr_len; // header length
uint16_t pld_len; // payload length
};
#define MAX_SID_NUM 8
struct sid_list
{
uint16_t sid[MAX_SID_NUM];
int used; int used;
}; };
void packet_prepend_sids(struct packet *pkt, const struct sids *sids);
enum packet_direction enum packet_direction
{ {
PACKET_DIRECTION_OUTGOING = 0, // Internal -> External: 0 PACKET_DIRECTION_OUTGOING = 0, // Internal -> External: 0
PACKET_DIRECTION_INCOMING = 1, // External -> Internal: 1 PACKET_DIRECTION_INCOMING = 1, // External -> Internal: 1
}; };
enum packet_direction packet_get_direction(const struct packet *pkt);
enum packet_action enum packet_action
{ {
PACKET_ACTION_FORWARD = 0, PACKET_ACTION_FORWARD = 0, // must be zero
PACKET_ACTION_DROP = 1, PACKET_ACTION_DROP = 1,
PACKET_ACTION_DEFER = 2, // TODO
}; };
enum packet_direction packet_get_direction(const struct packet *pkt);
uint64_t packet_get_session_id(const struct packet *pkt);
void packet_prepend_sid_list(struct packet *pkt, const struct sid_list *list);
int packet_get_layer_count(const struct packet *pkt);
const struct raw_layer *packet_get_raw_layer(const struct packet *pkt, int idx);
const char *packet_get_data(const struct packet *pkt);
uint16_t packet_get_len(const struct packet *pkt);
const char *packet_get_payload(const struct packet *pkt);
uint16_t packet_get_payload_len(const struct packet *pkt);
void packet_set_action(struct packet *pkt, enum packet_action action); 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);
struct address uint64_t packet_get_session_id(const struct packet *pkt);
{
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 char *packet_get_raw_data(const struct packet *pkt);
{ uint16_t packet_get_raw_len(const struct packet *pkt);
const struct ip *ip4_hdr = NULL;
const struct ip6_hdr *ip6_hdr = NULL;
const struct raw_layer *layer = NULL;
int num = packet_get_layer_count(pkt);
for (int i = num - 1; i >= 0; i--)
{
layer = packet_get_raw_layer(pkt, i);
if (layer->type == LAYER_PROTO_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_PROTO_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; const char *packet_get_payload(const struct packet *pkt);
} uint16_t packet_get_payload_len(const struct packet *pkt);
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 raw_layer *layer = NULL;
int num = packet_get_layer_count(pkt);
for (int i = num - 1; i >= 0; i--)
{
layer = packet_get_raw_layer(pkt, i);
if (layer->type == LAYER_PROTO_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_PROTO_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;
}
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -133,7 +133,7 @@ enum session_type session_get_type(const struct session *sess);
enum session_state session_get_current_state(const struct session *sess); enum session_state session_get_current_state(const struct session *sess);
const struct packet *session_get0_current_packet(const struct session *sess); const struct packet *session_get0_current_packet(const struct session *sess);
const char *session_get0_current_payload(const struct session *sess, size_t *payload_len); const char *session_get0_current_payload(const struct session *sess, uint16_t *payload_len);
enum closing_reason session_get_closing_reason(const struct session *sess); 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);

View File

@@ -56,7 +56,7 @@ int stellar_inject_tcp_rst(struct stellar *st, const struct session *sess, enum
int stellar_inject_tcp_fin(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_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_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 sid_list *sids, const char *msg, 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); int stellar_main(int argc, char **argv);

View File

@@ -72,6 +72,7 @@ TEST(DUPLICATED_PACKET_FILTER, TEST)
uint32_t timeout = 2; uint32_t timeout = 2;
double error_rate = 0.00001; double error_rate = 0.00001;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)data, sizeof(data)); packet_parse(&pkt, (const char *)data, sizeof(data));
struct duplicated_packet_filter *filter = duplicated_packet_filter_new(capacity, timeout, error_rate, 1); struct duplicated_packet_filter *filter = duplicated_packet_filter_new(capacity, timeout, error_rate, 1);

View File

@@ -4,6 +4,7 @@
#include <assert.h> #include <assert.h>
#include "packet_priv.h" #include "packet_priv.h"
#include "packet_utils.h"
#include "crc32_hash.h" #include "crc32_hash.h"
#include "checksum.h" #include "checksum.h"
#include "ipv4_utils.h" #include "ipv4_utils.h"
@@ -302,7 +303,7 @@ static inline void ip_frag_hdr_init(struct ip_frag_hdr *hdr, const struct packet
{ {
struct raw_layer *layer = pkt->frag_layer; struct raw_layer *layer = pkt->frag_layer;
if (layer->type == LAYER_PROTO_IPV6) if (layer->proto == LAYER_PROTO_IPV6)
{ {
struct ip6_frag *frag_ext = ipv6_hdr_get_frag_ext((const struct ip6_hdr *)layer->hdr_ptr); struct ip6_frag *frag_ext = ipv6_hdr_get_frag_ext((const struct ip6_hdr *)layer->hdr_ptr);
hdr->next_proto = frag_ext->ip6f_nxt; hdr->next_proto = frag_ext->ip6f_nxt;
@@ -639,8 +640,8 @@ static struct packet *ip_frag_reassemble(struct ip_reassembly *assy, struct ip_f
return NULL; return NULL;
} }
char *ptr = (char *)packet_get_data(pkt); char *ptr = (char *)packet_get_raw_data(pkt);
char *end = ptr + packet_get_len(pkt); char *end = ptr + packet_get_raw_len(pkt);
// copy last frag // copy last frag
if (last->len > end - ptr) if (last->len > end - ptr)
@@ -729,7 +730,6 @@ static struct packet *ip_frag_reassemble(struct ip_reassembly *assy, struct ip_f
// create a new packet // create a new packet
packet_parse(pkt, ptr, packet_len); packet_parse(pkt, ptr, packet_len);
packet_set_origin_ctx(pkt, NULL);
return pkt; return pkt;
@@ -865,7 +865,7 @@ struct packet *ip_reassembly_packet(struct ip_reassembly *assy, const struct pac
return NULL; return NULL;
} }
if (layer->type == LAYER_PROTO_IPV4) if (layer->proto == LAYER_PROTO_IPV4)
{ {
pkt1 = ipv4_reassembly_packet(assy, pkt, now); pkt1 = ipv4_reassembly_packet(assy, pkt, now);
if (pkt1 && pkt1->frag_layer) if (pkt1 && pkt1->frag_layer)
@@ -877,7 +877,7 @@ struct packet *ip_reassembly_packet(struct ip_reassembly *assy, const struct pac
return pkt1; return pkt1;
} }
else if (layer->type == LAYER_PROTO_IPV6) else if (layer->proto == LAYER_PROTO_IPV6)
{ {
pkt1 = ipv6_reassembly_packet(assy, pkt, now); pkt1 = ipv6_reassembly_packet(assy, pkt, now);
if (pkt1 && pkt1->frag_layer) if (pkt1 && pkt1->frag_layer)

View File

@@ -215,6 +215,7 @@ TEST(IPV4_REASSEMBLE, PADDING_ORDER)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -226,6 +227,7 @@ TEST(IPV4_REASSEMBLE, PADDING_ORDER)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt); EXPECT_TRUE(new_pkt);
@@ -308,6 +310,7 @@ TEST(IPV4_REASSEMBLE, PADDING_UNORDER)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -319,6 +322,7 @@ TEST(IPV4_REASSEMBLE, PADDING_UNORDER)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt); EXPECT_TRUE(new_pkt);
@@ -400,6 +404,7 @@ TEST(IPV4_REASSEMBLE, EXPIRE)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -411,6 +416,7 @@ TEST(IPV4_REASSEMBLE, EXPIRE)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 2); new_pkt = ip_reassembly_packet(assy, &pkt, 2);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -452,6 +458,7 @@ TEST(IPV4_REASSEMBLE, DUP_FIRST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -463,6 +470,7 @@ TEST(IPV4_REASSEMBLE, DUP_FIRST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -474,6 +482,7 @@ TEST(IPV4_REASSEMBLE, DUP_FIRST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt); EXPECT_TRUE(new_pkt);
@@ -556,6 +565,7 @@ TEST(IPV4_REASSEMBLE, DUP_LAST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -567,6 +577,7 @@ TEST(IPV4_REASSEMBLE, DUP_LAST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -578,6 +589,7 @@ TEST(IPV4_REASSEMBLE, DUP_LAST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt); EXPECT_TRUE(new_pkt);
@@ -660,6 +672,7 @@ TEST(IPV4_REASSEMBLE, FULL)
char dup_frag[sizeof(frag1)] = {0}; char dup_frag[sizeof(frag1)] = {0};
memcpy(dup_frag, frag1, sizeof(frag1)); memcpy(dup_frag, frag1, sizeof(frag1));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)dup_frag, sizeof(dup_frag)); packet_parse(&pkt, (const char *)dup_frag, sizeof(dup_frag));
// flow1 // flow1

View File

@@ -626,6 +626,7 @@ TEST(IPV6_REASSEMBLE, NORMAL)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -637,6 +638,7 @@ TEST(IPV6_REASSEMBLE, NORMAL)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -648,6 +650,7 @@ TEST(IPV6_REASSEMBLE, NORMAL)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag3 // frag3
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag3, sizeof(frag3)); packet_parse(&pkt, (const char *)frag3, sizeof(frag3));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -659,6 +662,7 @@ TEST(IPV6_REASSEMBLE, NORMAL)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag4 // frag4
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag4, sizeof(frag4)); packet_parse(&pkt, (const char *)frag4, sizeof(frag4));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt); EXPECT_TRUE(new_pkt);
@@ -734,6 +738,7 @@ TEST(IPV6_REASSEMBLE, EXPIRE)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -745,6 +750,7 @@ TEST(IPV6_REASSEMBLE, EXPIRE)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 2); new_pkt = ip_reassembly_packet(assy, &pkt, 2);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -786,6 +792,7 @@ TEST(IPV6_REASSEMBLE, DUP_FIRST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -797,6 +804,7 @@ TEST(IPV6_REASSEMBLE, DUP_FIRST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -808,6 +816,7 @@ TEST(IPV6_REASSEMBLE, DUP_FIRST_FRAG)
0, 0, 0, 0, 1, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 1, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -819,6 +828,7 @@ TEST(IPV6_REASSEMBLE, DUP_FIRST_FRAG)
0, 0, 0, 0, 1, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 1, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag3 // frag3
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag3, sizeof(frag3)); packet_parse(&pkt, (const char *)frag3, sizeof(frag3));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -830,6 +840,7 @@ TEST(IPV6_REASSEMBLE, DUP_FIRST_FRAG)
0, 0, 0, 0, 1, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 1, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag4 // frag4
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag4, sizeof(frag4)); packet_parse(&pkt, (const char *)frag4, sizeof(frag4));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt); EXPECT_TRUE(new_pkt);
@@ -906,6 +917,7 @@ TEST(IPV6_REASSEMBLE, DUP_LAST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag4 // frag4
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag4, sizeof(frag4)); packet_parse(&pkt, (const char *)frag4, sizeof(frag4));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -917,6 +929,7 @@ TEST(IPV6_REASSEMBLE, DUP_LAST_FRAG)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag4 // frag4
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag4, sizeof(frag4)); packet_parse(&pkt, (const char *)frag4, sizeof(frag4));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -928,6 +941,7 @@ TEST(IPV6_REASSEMBLE, DUP_LAST_FRAG)
0, 0, 0, 0, 0, 1); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 1); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag3 // frag3
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag3, sizeof(frag3)); packet_parse(&pkt, (const char *)frag3, sizeof(frag3));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -939,6 +953,7 @@ TEST(IPV6_REASSEMBLE, DUP_LAST_FRAG)
0, 0, 0, 0, 0, 1); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 1); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -950,6 +965,7 @@ TEST(IPV6_REASSEMBLE, DUP_LAST_FRAG)
0, 0, 0, 0, 0, 1); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 1); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt); EXPECT_TRUE(new_pkt);
@@ -1027,6 +1043,7 @@ TEST(IPV6_REASSEMBLE, FULL)
char dup_frag[sizeof(frag1)] = {0}; char dup_frag[sizeof(frag1)] = {0};
memcpy(dup_frag, frag1, sizeof(frag1)); memcpy(dup_frag, frag1, sizeof(frag1));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)dup_frag, sizeof(dup_frag)); packet_parse(&pkt, (const char *)dup_frag, sizeof(dup_frag));
// flow1 // flow1
@@ -1095,6 +1112,7 @@ TEST(IPV6_REASSEMBLE, OVERLAP)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag1 // frag1
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag1, sizeof(frag1)); packet_parse(&pkt, (const char *)frag1, sizeof(frag1));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -1106,6 +1124,7 @@ TEST(IPV6_REASSEMBLE, OVERLAP)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag2 // frag2
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag2, sizeof(frag2)); packet_parse(&pkt, (const char *)frag2, sizeof(frag2));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);
@@ -1119,6 +1138,7 @@ TEST(IPV6_REASSEMBLE, OVERLAP)
// frag3 -- overwrite frag offset // frag3 -- overwrite frag offset
char dup_frag[sizeof(frag3)] = {0}; char dup_frag[sizeof(frag3)] = {0};
memcpy(dup_frag, frag3, sizeof(frag3)); memcpy(dup_frag, frag3, sizeof(frag3));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)dup_frag, sizeof(dup_frag)); packet_parse(&pkt, (const char *)dup_frag, sizeof(dup_frag));
packet_set_ipv6_frag_offset(&pkt, 2048); packet_set_ipv6_frag_offset(&pkt, 2048);
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
@@ -1131,6 +1151,7 @@ TEST(IPV6_REASSEMBLE, OVERLAP)
0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag 0, 0, 0, 0, 0, 0); // ip6: nospace, overlap, many frag, invalid length, dup first frag, dup last frag
// frag4 // frag4
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)frag4, sizeof(frag4)); packet_parse(&pkt, (const char *)frag4, sizeof(frag4));
new_pkt = ip_reassembly_packet(assy, &pkt, 1); new_pkt = ip_reassembly_packet(assy, &pkt, 1);
EXPECT_TRUE(new_pkt == NULL); EXPECT_TRUE(new_pkt == NULL);

View File

@@ -14,6 +14,7 @@ extern "C"
#include "ipv6_utils.h" #include "ipv6_utils.h"
#include "ip_reassembly.h" #include "ip_reassembly.h"
#include "packet_priv.h" #include "packet_priv.h"
#include "packet_utils.h"
static inline void packet_set_ipv4_src_addr(struct packet *pkt, uint32_t saddr) static inline void packet_set_ipv4_src_addr(struct packet *pkt, uint32_t saddr)
{ {

View File

@@ -1,7 +1,7 @@
add_library(packet packet.cpp packet_utils.cpp) add_library(packet packet.cpp packet_utils.cpp packet_layer.cpp)
target_include_directories(packet PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(packet PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash) target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash)
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/include) target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(packet tuple log mrzcpd) target_link_libraries(packet tuple log)
add_subdirectory(test) add_subdirectory(test)

View File

@@ -6,6 +6,7 @@
#include "uthash.h" #include "uthash.h"
#include "packet_priv.h" #include "packet_priv.h"
#include "packet_utils.h"
#include "eth_utils.h" #include "eth_utils.h"
#include "gre_utils.h" #include "gre_utils.h"
#include "udp_utils.h" #include "udp_utils.h"
@@ -49,11 +50,11 @@
******************************************************************************/ ******************************************************************************/
static inline const char *ldbc_method_to_str(enum ldbc_method method); static inline const char *ldbc_method_to_str(enum ldbc_method method);
static inline const char *layer_proto_to_str(enum layer_proto type); static inline const char *layer_proto_to_str(enum layer_proto proto);
static inline void set_tuple2(const char *data, enum layer_proto type, struct tuple2 *tuple); static inline void set_tuple2(const char *data, enum layer_proto proto, struct tuple2 *tuple);
static inline void set_tuple4(const char *data, enum layer_proto type, struct tuple4 *tuple); static inline void set_tuple4(const char *data, enum layer_proto proto, struct tuple4 *tuple);
static inline void set_tuple6(const char *data, enum layer_proto type, struct tuple6 *tuple, uint64_t domain); static inline void set_tuple6(const char *data, enum layer_proto proto, struct tuple6 *tuple, uint64_t domain);
static inline struct raw_layer *get_free_layer(struct packet *pkt); static inline struct raw_layer *get_free_layer(struct packet *pkt);
@@ -112,9 +113,9 @@ static inline const char *ldbc_method_to_str(enum ldbc_method method)
} }
} }
static inline const char *layer_proto_to_str(enum layer_proto type) static inline const char *layer_proto_to_str(enum layer_proto proto)
{ {
switch (type) switch (proto)
{ {
case LAYER_PROTO_ETHER: case LAYER_PROTO_ETHER:
return "ETH"; return "ETH";
@@ -155,12 +156,12 @@ static inline const char *layer_proto_to_str(enum layer_proto type)
} }
} }
static inline void set_tuple2(const char *data, enum layer_proto type, struct tuple2 *tuple) static inline void set_tuple2(const char *data, enum layer_proto proto, struct tuple2 *tuple)
{ {
const struct ip *ipv4 = NULL; const struct ip *ipv4 = NULL;
const struct ip6_hdr *ipv6 = NULL; const struct ip6_hdr *ipv6 = NULL;
switch (type) switch (proto)
{ {
case LAYER_PROTO_IPV4: case LAYER_PROTO_IPV4:
ipv4 = (const struct ip *)data; ipv4 = (const struct ip *)data;
@@ -179,14 +180,14 @@ static inline void set_tuple2(const char *data, enum layer_proto type, struct tu
} }
} }
static inline void set_tuple4(const char *data, enum layer_proto type, struct tuple4 *tuple) static inline void set_tuple4(const char *data, enum layer_proto proto, struct tuple4 *tuple)
{ {
const struct ip *ipv4 = NULL; const struct ip *ipv4 = NULL;
const struct ip6_hdr *ipv6 = NULL; const struct ip6_hdr *ipv6 = NULL;
const struct tcphdr *tcp = NULL; const struct tcphdr *tcp = NULL;
const struct udphdr *udp = NULL; const struct udphdr *udp = NULL;
switch (type) switch (proto)
{ {
case LAYER_PROTO_TCP: case LAYER_PROTO_TCP:
tcp = (const struct tcphdr *)data; tcp = (const struct tcphdr *)data;
@@ -215,7 +216,7 @@ static inline void set_tuple4(const char *data, enum layer_proto type, struct tu
} }
} }
static inline void set_tuple6(const char *data, enum layer_proto type, struct tuple6 *tuple, uint64_t domain) static inline void set_tuple6(const char *data, enum layer_proto proto, struct tuple6 *tuple, uint64_t domain)
{ {
const struct ip *ipv4 = NULL; const struct ip *ipv4 = NULL;
const struct ip6_hdr *ipv6 = NULL; const struct ip6_hdr *ipv6 = NULL;
@@ -224,7 +225,7 @@ static inline void set_tuple6(const char *data, enum layer_proto type, struct tu
tuple->domain = domain; tuple->domain = domain;
switch (type) switch (proto)
{ {
case LAYER_PROTO_TCP: case LAYER_PROTO_TCP:
tcp = (const struct tcphdr *)data; tcp = (const struct tcphdr *)data;
@@ -265,9 +266,9 @@ static inline struct raw_layer *get_free_layer(struct packet *pkt)
return &pkt->layers[pkt->layers_used]; return &pkt->layers[pkt->layers_used];
} }
#define SET_LAYER(_pkt, _layer, _type, _hdr_len, _data, _len, _trim) \ #define SET_LAYER(_pkt, _layer, _proto, _hdr_len, _data, _len, _trim) \
{ \ { \
(_layer)->type = (_type); \ (_layer)->proto = (_proto); \
(_layer)->hdr_offset = (_pkt)->data_len - (_pkt)->trim_len - (_len); \ (_layer)->hdr_offset = (_pkt)->data_len - (_pkt)->trim_len - (_len); \
(_layer)->hdr_ptr = (_data); \ (_layer)->hdr_ptr = (_data); \
(_layer)->hdr_len = (_hdr_len); \ (_layer)->hdr_len = (_hdr_len); \
@@ -276,7 +277,7 @@ static inline struct raw_layer *get_free_layer(struct packet *pkt)
(_pkt)->trim_len += (_trim); \ (_pkt)->trim_len += (_trim); \
(_pkt)->layers_used++; \ (_pkt)->layers_used++; \
PACKET_LOG_DEBUG("layer[%d/%d]: %s, hdr_offset: %d, hdr_ptr: %p, hdr_len: %d, pld_ptr: %p, pld_len: %d", \ PACKET_LOG_DEBUG("layer[%d/%d]: %s, hdr_offset: %d, hdr_ptr: %p, hdr_len: %d, pld_ptr: %p, pld_len: %d", \
(_pkt)->layers_used - 1, (_pkt)->layers_size, layer_proto_to_str((_type)), \ (_pkt)->layers_used - 1, (_pkt)->layers_size, layer_proto_to_str((_proto)), \
(_layer)->hdr_offset, (_layer)->hdr_ptr, (_layer)->hdr_len, (_layer)->pld_ptr, (_layer)->pld_len); \ (_layer)->hdr_offset, (_layer)->hdr_ptr, (_layer)->hdr_len, (_layer)->pld_ptr, (_layer)->pld_len); \
} }
@@ -1148,10 +1149,10 @@ void packet_print_str(const struct packet *pkt)
{ {
int used = 0; int used = 0;
const struct raw_layer *layer = &pkt->layers[i]; const struct raw_layer *layer = &pkt->layers[i];
printf(" layer[%u]: %p, type: %s, hdr_offset: %u, hdr_ptr: %p, hdr_len: %u, pld_ptr: %p, pld_len: %u\n", printf(" layer[%u]: %p, proto: %s, hdr_offset: %u, hdr_ptr: %p, hdr_len: %u, pld_ptr: %p, pld_len: %u\n",
i, layer, layer_proto_to_str(layer->type), layer->hdr_offset, i, layer, layer_proto_to_str(layer->proto), layer->hdr_offset,
layer->hdr_ptr, layer->hdr_len, layer->pld_ptr, layer->pld_len); layer->hdr_ptr, layer->hdr_len, layer->pld_ptr, layer->pld_len);
switch (layer->type) switch (layer->proto)
{ {
case LAYER_PROTO_ETHER: case LAYER_PROTO_ETHER:
used = eth_hdr_to_str((const struct ethhdr *)layer->hdr_ptr, buffer, sizeof(buffer)); used = eth_hdr_to_str((const struct ethhdr *)layer->hdr_ptr, buffer, sizeof(buffer));
@@ -1219,9 +1220,9 @@ int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
{ {
layer = &pkt->layers[i]; layer = &pkt->layers[i];
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6) if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
{ {
set_tuple2((const char *)pkt->data_ptr + layer->hdr_offset, layer->type, tuple); set_tuple2((const char *)pkt->data_ptr + layer->hdr_offset, layer->proto, tuple);
return 0; return 0;
} }
} }
@@ -1240,9 +1241,9 @@ int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
{ {
layer = &pkt->layers[i]; layer = &pkt->layers[i];
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6) if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
{ {
set_tuple2((const char *)pkt->data_ptr + layer->hdr_offset, layer->type, tuple); set_tuple2((const char *)pkt->data_ptr + layer->hdr_offset, layer->proto, tuple);
return 0; return 0;
} }
} }
@@ -1264,14 +1265,14 @@ int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
layer = &pkt->layers[i]; layer = &pkt->layers[i];
// first get L4 layer // first get L4 layer
if (layer->type == LAYER_PROTO_UDP || layer->type == LAYER_PROTO_TCP) if (layer->proto == LAYER_PROTO_UDP || layer->proto == LAYER_PROTO_TCP)
{ {
layer_l4 = layer; layer_l4 = layer;
continue; continue;
} }
// second get L3 layer // second get L3 layer
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6) if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
{ {
layer_l3 = layer; layer_l3 = layer;
break; break;
@@ -1280,8 +1281,8 @@ int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
if (layer_l3 && layer_l4) if (layer_l3 && layer_l4)
{ {
set_tuple4((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple); set_tuple4((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->proto, tuple);
set_tuple4((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple); set_tuple4((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->proto, tuple);
return 0; return 0;
} }
else else
@@ -1304,14 +1305,14 @@ int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
layer = &pkt->layers[i]; layer = &pkt->layers[i];
// first get L3 layer // first get L3 layer
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6) if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
{ {
layer_l3 = layer; layer_l3 = layer;
continue; continue;
} }
// second get L4 layer // second get L4 layer
if (layer->type == LAYER_PROTO_UDP || layer->type == LAYER_PROTO_TCP) if (layer->proto == LAYER_PROTO_UDP || layer->proto == LAYER_PROTO_TCP)
{ {
layer_l4 = layer; layer_l4 = layer;
break; break;
@@ -1320,8 +1321,8 @@ int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
if (layer_l3 && layer_l4) if (layer_l3 && layer_l4)
{ {
set_tuple4((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple); set_tuple4((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->proto, tuple);
set_tuple4((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple); set_tuple4((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->proto, tuple);
return 0; return 0;
} }
else else
@@ -1338,21 +1339,21 @@ int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
const struct raw_layer *layer_l3 = NULL; const struct raw_layer *layer_l3 = NULL;
const struct raw_layer *layer_l4 = NULL; const struct raw_layer *layer_l4 = NULL;
const struct raw_layer *layer = NULL; const struct raw_layer *layer = NULL;
uint64_t domain = packet_get_domain(pkt); uint64_t domain = packet_get_domain_id(pkt);
for (int8_t i = pkt->layers_used - 1; i >= 0; i--) for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{ {
layer = &pkt->layers[i]; layer = &pkt->layers[i];
// first get L4 layer // first get L4 layer
if (layer->type == LAYER_PROTO_UDP || layer->type == LAYER_PROTO_TCP) if (layer->proto == LAYER_PROTO_UDP || layer->proto == LAYER_PROTO_TCP)
{ {
layer_l4 = layer; layer_l4 = layer;
continue; continue;
} }
// second get L3 layer // second get L3 layer
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6) if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
{ {
layer_l3 = layer; layer_l3 = layer;
break; break;
@@ -1361,8 +1362,8 @@ int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1) if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
{ {
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple, domain); set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->proto, tuple, domain);
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple, domain); set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->proto, tuple, domain);
return 0; return 0;
} }
else else
@@ -1379,21 +1380,21 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
const struct raw_layer *layer_l3 = NULL; const struct raw_layer *layer_l3 = NULL;
const struct raw_layer *layer_l4 = NULL; const struct raw_layer *layer_l4 = NULL;
const struct raw_layer *layer = NULL; const struct raw_layer *layer = NULL;
uint64_t domain = packet_get_domain(pkt); uint64_t domain = packet_get_domain_id(pkt);
for (int8_t i = 0; i < pkt->layers_used; i++) for (int8_t i = 0; i < pkt->layers_used; i++)
{ {
layer = &pkt->layers[i]; layer = &pkt->layers[i];
// first get L3 layer // first get L3 layer
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6) if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
{ {
layer_l3 = layer; layer_l3 = layer;
continue; continue;
} }
// second get L4 layer // second get L4 layer
if (layer->type == LAYER_PROTO_UDP || layer->type == LAYER_PROTO_TCP) if (layer->proto == LAYER_PROTO_UDP || layer->proto == LAYER_PROTO_TCP)
{ {
layer_l4 = layer; layer_l4 = layer;
break; break;
@@ -1402,8 +1403,8 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1) if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
{ {
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple, domain); set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->proto, tuple, domain);
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple, domain); set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->proto, tuple, domain);
return 0; return 0;
} }
else else
@@ -1412,14 +1413,56 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
} }
} }
const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt, enum layer_proto type) const char *packet_get_raw_data(const struct packet *pkt)
{
return pkt->data_ptr;
}
uint16_t packet_get_raw_len(const struct packet *pkt)
{
return pkt->data_len;
}
const char *packet_get_payload(const struct packet *pkt)
{
if (pkt->layers_used == 0)
{
return NULL;
}
return pkt->layers[pkt->layers_used - 1].pld_ptr;
}
uint16_t packet_get_payload_len(const struct packet *pkt)
{
if (pkt->layers_used == 0)
{
return 0;
}
return pkt->layers[pkt->layers_used - 1].pld_len;
}
int packet_get_layer_count(const struct packet *pkt)
{
return pkt->layers_used;
}
const struct raw_layer *packet_get_raw_layer(const struct packet *pkt, int idx)
{
if (idx < 0 || idx >= pkt->layers_used)
{
return NULL;
}
return &pkt->layers[idx];
}
const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt, enum layer_proto proto)
{ {
const struct raw_layer *layer = NULL; const struct raw_layer *layer = NULL;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--) for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{ {
layer = &pkt->layers[i]; layer = &pkt->layers[i];
if (layer->type == type) if (layer->proto == proto)
{ {
return layer; return layer;
} }
@@ -1428,14 +1471,14 @@ const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt,
return NULL; return NULL;
} }
const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt, enum layer_proto type) const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt, enum layer_proto proto)
{ {
const struct raw_layer *layer = NULL; const struct raw_layer *layer = NULL;
for (int8_t i = 0; i < pkt->layers_used; i++) for (int8_t i = 0; i < pkt->layers_used; i++)
{ {
layer = &pkt->layers[i]; layer = &pkt->layers[i];
if (layer->type == type) if (layer->proto == proto)
{ {
return layer; return layer;
} }
@@ -1444,6 +1487,7 @@ const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt,
return NULL; return NULL;
} }
// TODO
// direction 1: E2I // direction 1: E2I
// 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)

View File

@@ -0,0 +1,23 @@
#include "packet_priv.h"
int packet_get_layer(const struct packet *pkt, int idx, struct layer *out)
{
if (pkt == NULL || out == NULL)
{
return -1;
}
if (idx < 0 || idx >= pkt->layers_used)
{
return -1;
}
const struct raw_layer *raw = &pkt->layers[idx];
out->proto = raw->proto;
out->header_len = raw->hdr_len;
out->payload_len = raw->pld_len;
out->header.raw = raw->hdr_ptr;
out->payload = raw->pld_ptr;
return 0;
}

View File

@@ -6,18 +6,15 @@ extern "C"
{ {
#endif #endif
#include <stdint.h>
#include <stdio.h>
#include "log.h" #include "log.h"
#include "tuple.h" #include "tuple.h"
#include "stellar/layer.h"
#include "stellar/packet.h" #include "stellar/packet.h"
#define PACKET_MAX_LAYERS 32 #define PACKET_MAX_LAYERS 32
#define PACKET_LOG_ERROR(format, ...) LOG_ERROR("packet", format, ##__VA_ARGS__) #define PACKET_LOG_ERROR(format, ...) LOG_ERROR("packet", format, ##__VA_ARGS__)
#define PACKET_LOG_WARN(format, ...) LOG_WARN("packet", format, ##__VA_ARGS__) #define PACKET_LOG_WARN(format, ...) LOG_WARN("packet", format, ##__VA_ARGS__)
#define PACKET_LOG_DEBUG(format, ...) void(0) #define PACKET_LOG_DEBUG(format, ...) void(0)
// #define PACKET_LOG_DEBUG(format, ...) LOG_DEBUG("packet", format, ##__VA_ARGS__)
enum ldbc_method enum ldbc_method
{ {
@@ -28,12 +25,36 @@ enum ldbc_method
LDBC_METHOD_HASH_INNERMOST_EXT_IP = 5, LDBC_METHOD_HASH_INNERMOST_EXT_IP = 5,
}; };
enum packet_origin #define MAX_ROUTE_CTX 64
struct route_ctx
{ {
PACKET_ORIGIN_MARSIO = 0x1, // packet data in mbuff (eg: packet I/O mrzcpd mode) char data[MAX_ROUTE_CTX];
PACKET_ORIGIN_DUMPFILE = 0x2, // packet data in pcap (eg: packet I/O dumpfile mode) int used;
PACKET_ORIGIN_USERSTACK = 0x3, // packet data in user stack (eg: inject packet) };
PACKET_ORIGIN_USERHEAP = 0x4, // packet data in user heap (eg: ip reassembly)
struct metadata
{
struct route_ctx route_ctx;
struct sids sids;
uint64_t session_id;
uint64_t domain_id;
uint16_t link_id;
int is_ctrl;
enum packet_direction direction;
enum packet_action action;
const void *origin_ctx;
};
struct raw_layer
{
enum layer_proto proto;
const char *hdr_ptr; // header pointer
const char *pld_ptr; // payload pointer
uint16_t hdr_offset; // header offset from data_ptr
uint16_t hdr_len; // header length
uint16_t pld_len; // payload length
}; };
struct packet struct packet
@@ -42,24 +63,19 @@ struct packet
struct raw_layer *frag_layer; // fragment layer struct raw_layer *frag_layer; // fragment layer
int8_t layers_used; int8_t layers_used;
int8_t layers_size; int8_t layers_size;
int8_t need_free;
const char *data_ptr; const char *data_ptr;
uint16_t data_len; uint16_t data_len;
uint16_t trim_len; // trim eth padding uint16_t trim_len; // trim eth padding
void *origin_ctx; // mbuff or pcap pointer struct metadata meta;
enum packet_action action;
enum packet_origin origin;
}; };
// return innermost payload // return innermost payload
const char *packet_parse(struct packet *pkt, const char *data, uint16_t len); const char *packet_parse(struct packet *pkt, const char *data, uint16_t len);
void packet_print_str(const struct packet *pkt); void packet_print_str(const struct packet *pkt);
// direction 1: E2I
// direction 0: I2E
uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction);
// return 0: found // return 0: found
// return -1: not found // return -1: not found
int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple); int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
@@ -75,46 +91,14 @@ int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple); int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple); int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
int packet_get_layer_count(const struct packet *pkt);
const struct raw_layer *packet_get_raw_layer(const struct packet *pkt, int idx);
const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt, enum layer_proto type); const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt, enum layer_proto type);
const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt, enum layer_proto type); const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt, enum layer_proto type);
/****************************************************************************** // direction 1: E2I
* Utils // direction 0: I2E
******************************************************************************/ uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction);
#define MAX_ROUTE_CTX 64
struct route_ctx
{
char data[MAX_ROUTE_CTX];
int used;
};
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx);
void packet_get_route_ctx(const struct packet *pkt, struct route_ctx *ctx);
void packet_set_origin(struct packet *pkt, enum packet_origin origin);
enum packet_origin packet_get_origin(const struct packet *pkt);
void packet_set_origin_ctx(struct packet *pkt, void *ctx);
void *packet_get_origin_ctx(const struct packet *pkt);
void packet_set_domain(struct packet *pkt, uint64_t domain);
uint64_t packet_get_domain(const struct packet *pkt);
void packet_set_ctrl(struct packet *pkt);
int packet_is_ctrl(const struct packet *pkt);
void packet_set_sid_list(struct packet *pkt, const struct sid_list *list);
void packet_get_sid_list(const struct packet *pkt, struct sid_list *list);
void packet_append_sid_list(struct packet *pkt, const struct sid_list *list);
struct packet *packet_new(uint16_t pkt_len);
void packet_free(struct packet *pkt);
struct packet *packet_dup(const struct packet *pkt);
void packet_set_session_id(struct packet *pkt, uint64_t sess_id);
void packet_set_direction(struct packet *pkt, enum packet_direction dir);
int packet_is_fragment(const struct packet *pkt);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -1,90 +1,127 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "udp_utils.h"
#include "tcp_utils.h"
#include "ipv4_utils.h"
#include "ipv6_utils.h"
#include "packet_priv.h" #include "packet_priv.h"
#include "marsio.h" #include "packet_utils.h"
/****************************************************************************** /******************************************************************************
* set and get from struct packet * metadata utils
******************************************************************************/ ******************************************************************************/
void packet_set_origin(struct packet *pkt, enum packet_origin origin) void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx)
{ {
pkt->origin = origin; pkt->meta.route_ctx = *ctx;
} }
enum packet_origin packet_get_origin(const struct packet *pkt) const struct route_ctx *packet_get_route_ctx(const struct packet *pkt)
{ {
return pkt->origin; return &pkt->meta.route_ctx;
} }
int packet_get_layer_count(const struct packet *pkt) void packet_set_sids(struct packet *pkt, const struct sids *sids)
{ {
return pkt->layers_used; pkt->meta.sids = *sids;
} }
const struct raw_layer *packet_get_raw_layer(const struct packet *pkt, int idx) const struct sids *packet_get_sids(const struct packet *pkt)
{ {
if (idx < 0 || idx >= pkt->layers_used) return &pkt->meta.sids;
}
void packet_prepend_sids(struct packet *pkt, const struct sids *sids)
{
if (pkt->meta.sids.used + sids->used > MAX_SIDS)
{ {
return NULL; PACKET_LOG_ERROR("sids overflow");
return;
} }
return &pkt->layers[idx]; else
}
const char *packet_get_data(const struct packet *pkt)
{
return pkt->data_ptr;
}
uint16_t packet_get_len(const struct packet *pkt)
{
return pkt->data_len;
}
const char *packet_get_payload(const struct packet *pkt)
{
if (pkt->layers_used == 0)
{ {
return NULL; for (int i = pkt->meta.sids.used - 1; i >= 0; i--)
{
pkt->meta.sids.sid[i + sids->used] = pkt->meta.sids.sid[i];
}
for (int i = 0; i < sids->used; i++)
{
pkt->meta.sids.sid[i] = sids->sid[i];
}
pkt->meta.sids.used += sids->used;
} }
return pkt->layers[pkt->layers_used - 1].pld_ptr;
} }
uint16_t packet_get_payload_len(const struct packet *pkt) void packet_set_session_id(struct packet *pkt, uint64_t id)
{ {
if (pkt->layers_used == 0) pkt->meta.session_id = id;
{ }
return 0;
} uint64_t packet_get_session_id(const struct packet *pkt)
return pkt->layers[pkt->layers_used - 1].pld_len; {
return pkt->meta.session_id;
}
void packet_set_domain_id(struct packet *pkt, uint64_t id)
{
pkt->meta.domain_id = id;
}
uint64_t packet_get_domain_id(const struct packet *pkt)
{
return pkt->meta.domain_id;
}
void packet_set_link_id(struct packet *pkt, uint16_t id)
{
pkt->meta.link_id = id;
}
uint16_t packet_get_link_id(const struct packet *pkt)
{
return pkt->meta.link_id;
}
void packet_set_ctrl(struct packet *pkt, uint8_t ctrl)
{
pkt->meta.is_ctrl = ctrl;
}
uint8_t packet_is_ctrl(const struct packet *pkt)
{
return pkt->meta.is_ctrl;
}
void packet_set_direction(struct packet *pkt, enum packet_direction dir)
{
pkt->meta.direction = dir;
}
enum packet_direction packet_get_direction(const struct packet *pkt)
{
return pkt->meta.direction;
} }
void packet_set_action(struct packet *pkt, enum packet_action action) void packet_set_action(struct packet *pkt, enum packet_action action)
{ {
pkt->action = action; pkt->meta.action = action;
} }
enum packet_action packet_get_action(const struct packet *pkt) enum packet_action packet_get_action(const struct packet *pkt)
{ {
return pkt->action; return pkt->meta.action;
} }
void packet_set_origin_ctx(struct packet *pkt, void *ctx) void packet_set_origin_ctx(struct packet *pkt, void *ctx)
{ {
pkt->origin_ctx = ctx; pkt->meta.origin_ctx = ctx;
} }
void *packet_get_origin_ctx(const struct packet *pkt) const void *packet_get_origin_ctx(const struct packet *pkt)
{ {
return pkt->origin_ctx; return pkt->meta.origin_ctx;
} }
/******************************************************************************
* other uitls
******************************************************************************/
int packet_is_fragment(const struct packet *pkt) int packet_is_fragment(const struct packet *pkt)
{ {
return (pkt->frag_layer) ? 1 : 0; return (pkt->frag_layer) ? 1 : 0;
@@ -99,19 +136,11 @@ struct packet *packet_new(uint16_t pkt_len)
} }
pkt->data_len = pkt_len; pkt->data_len = pkt_len;
pkt->data_ptr = (const char *)pkt + sizeof(struct packet); pkt->data_ptr = (const char *)pkt + sizeof(struct packet);
pkt->origin = PACKET_ORIGIN_USERHEAP; pkt->need_free = 1;
return pkt; return pkt;
} }
void packet_free(struct packet *pkt)
{
if (pkt && pkt->origin == PACKET_ORIGIN_USERHEAP)
{
free((void *)pkt);
}
}
struct packet *packet_dup(const struct packet *pkt) struct packet *packet_dup(const struct packet *pkt)
{ {
if (pkt == NULL) if (pkt == NULL)
@@ -130,14 +159,11 @@ struct packet *packet_dup(const struct packet *pkt)
memcpy((char *)dup_pkt->data_ptr, pkt->data_ptr, pkt->data_len); memcpy((char *)dup_pkt->data_ptr, pkt->data_ptr, pkt->data_len);
dup_pkt->data_len = pkt->data_len; dup_pkt->data_len = pkt->data_len;
packet_set_action(dup_pkt, PACKET_ACTION_DROP);
dup_pkt->origin_ctx = NULL;
dup_pkt->origin = PACKET_ORIGIN_USERHEAP;
dup_pkt->action = PACKET_ACTION_DROP;
for (int8_t i = 0; i < pkt->layers_used; i++) for (int8_t i = 0; i < pkt->layers_used; i++)
{ {
dup_pkt->layers[i].type = pkt->layers[i].type; dup_pkt->layers[i].proto = pkt->layers[i].proto;
dup_pkt->layers[i].hdr_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset; dup_pkt->layers[i].hdr_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset;
dup_pkt->layers[i].pld_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset + pkt->layers[i].hdr_len; dup_pkt->layers[i].pld_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset + pkt->layers[i].hdr_len;
dup_pkt->layers[i].hdr_offset = pkt->layers[i].hdr_offset; dup_pkt->layers[i].hdr_offset = pkt->layers[i].hdr_offset;
@@ -154,254 +180,10 @@ struct packet *packet_dup(const struct packet *pkt)
return dup_pkt; return dup_pkt;
} }
/****************************************************************************** void packet_free(struct packet *pkt)
* set and get from mbuff
******************************************************************************/
void packet_set_ctrl(struct packet *pkt)
{ {
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO) if (pkt && pkt->need_free)
{ {
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt); free((void *)pkt);
assert(mbuff != NULL);
marsio_buff_set_ctrlbuf(mbuff);
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to set ctrl");
}
}
int packet_is_ctrl(const struct packet *pkt)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
return marsio_buff_is_ctrlbuf(mbuff);
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to check ctrl");
return 0;
}
}
void packet_set_direction(struct packet *pkt, enum packet_direction dir)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DIR, &dir, sizeof(dir)) != 0)
{
PACKET_LOG_ERROR("failed to set direction");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to set direction");
}
}
enum packet_direction packet_get_direction(const struct packet *pkt)
{
enum packet_direction dir = PACKET_DIRECTION_INCOMING;
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DIR, &dir, sizeof(dir)) <= 0)
{
PACKET_LOG_ERROR("failed to get direction");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to get direction");
}
return dir;
}
void packet_set_session_id(struct packet *pkt, uint64_t sess_id)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
if (marsio_buff_set_metadata(mbuff, MR_BUFF_SESSION_ID, &sess_id, sizeof(sess_id)) != 0)
{
PACKET_LOG_ERROR("failed to set session id");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to set session id");
}
}
uint64_t packet_get_session_id(const struct packet *pkt)
{
uint64_t sess_id = 0;
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
if (marsio_buff_get_metadata(mbuff, MR_BUFF_SESSION_ID, &sess_id, sizeof(sess_id)) <= 0)
{
PACKET_LOG_ERROR("failed to get session id");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to get session id");
}
return sess_id;
}
void packet_set_domain(struct packet *pkt, uint64_t domain)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
// TODO
#if 0
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DOMAIN, &domain, sizeof(domain)) != 0)
{
PACKET_LOG_ERROR("failed to set domain");
}
#endif
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to set domain");
}
}
uint64_t packet_get_domain(const struct packet *pkt)
{
uint64_t domain = 0;
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
// TODO
#if 0
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DOMAIN, &domain, sizeof(domain)) <= 0)
{
PACKET_LOG_ERROR("failed to get domain");
}
#endif
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to get domain");
}
return domain;
}
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
if (marsio_buff_set_metadata(mbuff, MR_BUFF_ROUTE_CTX, (void *)ctx->data, ctx->used) != 0)
{
PACKET_LOG_ERROR("failed to set route ctx");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to set route ctx");
}
}
void packet_get_route_ctx(const struct packet *pkt, struct route_ctx *ctx)
{
ctx->used = 0;
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
ctx->used = marsio_buff_get_metadata(mbuff, MR_BUFF_ROUTE_CTX, ctx->data, sizeof(ctx->data));
if (ctx->used <= 0)
{
PACKET_LOG_ERROR("failed to get route ctx");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to get route ctx");
}
}
void packet_set_sid_list(struct packet *pkt, const struct sid_list *list)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
if (marsio_buff_set_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
{
PACKET_LOG_ERROR("failed to set sid list");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to set sid list");
}
}
void packet_get_sid_list(const struct packet *pkt, struct sid_list *list)
{
list->used = 0;
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
list->used = marsio_buff_get_sid_list(mbuff, (sid_t *)list->sid, sizeof(list->sid) / sizeof(list->sid[0]));
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to get sid list");
}
}
void packet_prepend_sid_list(struct packet *pkt, const struct sid_list *list)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
if (marsio_buff_prepend_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
{
PACKET_LOG_ERROR("failed to prepend sid list");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to prepend sid list");
}
}
void packet_append_sid_list(struct packet *pkt, const struct sid_list *list)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL);
if (marsio_buff_append_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
{
PACKET_LOG_ERROR("failed to append sid list");
}
}
else
{
PACKET_LOG_WARN("packet origin is not marsio, failed to append sid list");
} }
} }

57
src/packet/packet_utils.h Normal file
View File

@@ -0,0 +1,57 @@
#ifndef _PACKET_UTILS_H
#define _PACKET_UTILS_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#include "packet_priv.h"
/******************************************************************************
* metadata utils
******************************************************************************/
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx);
const struct route_ctx *packet_get_route_ctx(const struct packet *pkt);
void packet_set_sids(struct packet *pkt, const struct sids *sids);
const struct sids *packet_get_sids(const struct packet *pkt);
void packet_prepend_sids(struct packet *pkt, const struct sids *sids);
void packet_set_session_id(struct packet *pkt, uint64_t id);
uint64_t packet_get_session_id(const struct packet *pkt);
void packet_set_domain_id(struct packet *pkt, uint64_t id);
uint64_t packet_get_domain_id(const struct packet *pkt);
void packet_set_link_id(struct packet *pkt, uint16_t id);
uint16_t packet_get_link_id(const struct packet *pkt);
void packet_set_ctrl(struct packet *pkt, uint8_t ctrl);
uint8_t packet_is_ctrl(const struct packet *pkt);
void packet_set_direction(struct packet *pkt, enum packet_direction dir);
enum packet_direction packet_get_direction(const struct packet *pkt);
void packet_set_action(struct packet *pkt, enum packet_action action);
enum packet_action packet_get_action(const struct packet *pkt);
void packet_set_origin_ctx(struct packet *pkt, void *ctx);
const void *packet_get_origin_ctx(const struct packet *pkt);
/******************************************************************************
* other uitls
******************************************************************************/
int packet_is_fragment(const struct packet *pkt);
struct packet *packet_new(uint16_t pkt_len);
struct packet *packet_dup(const struct packet *pkt);
void packet_free(struct packet *pkt);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -79,6 +79,7 @@ TEST(PACKET, ETH_VLAN_VLAN_IP4_IP4_UDP)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data1, sizeof(data1)); const char *payload = packet_parse(&handler, (const char *)data1, sizeof(data1));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data1 == 70); EXPECT_TRUE((char *)payload - (char *)&data1 == 70);
@@ -282,6 +283,7 @@ TEST(PACKET, ETH_IP6_IP4_TCP_SSH)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data2, sizeof(data2)); const char *payload = packet_parse(&handler, (const char *)data2, sizeof(data2));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data2 == 94); EXPECT_TRUE((char *)payload - (char *)&data2 == 94);
@@ -474,6 +476,7 @@ TEST(PACKET, ETH_VLAN_IP6_IP4_GRE_PPP_IP4_UDP_DNS)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data3, sizeof(data3)); const char *payload = packet_parse(&handler, (const char *)data3, sizeof(data3));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data3 == 126); EXPECT_TRUE((char *)payload - (char *)&data3 == 126);
@@ -680,6 +683,7 @@ TEST(PACKET, ETH_IP4_IP6_TCP)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data4, sizeof(data4)); const char *payload = packet_parse(&handler, (const char *)data4, sizeof(data4));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data4 == 106); EXPECT_TRUE((char *)payload - (char *)&data4 == 106);
@@ -834,6 +838,7 @@ TEST(PACKET, ETH_IP6_IP6_UDP)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data5, sizeof(data5)); const char *payload = packet_parse(&handler, (const char *)data5, sizeof(data5));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data5 == 102); EXPECT_TRUE((char *)payload - (char *)&data5 == 102);
@@ -989,6 +994,7 @@ TEST(PACKET, ETH_MPLS_IP4_TCP)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data6, sizeof(data6)); const char *payload = packet_parse(&handler, (const char *)data6, sizeof(data6));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data6 == 70); EXPECT_TRUE((char *)payload - (char *)&data6 == 70);
@@ -1155,6 +1161,7 @@ TEST(PACKET, ETH_MPLS_MPLS_IP4_TCP)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data7, sizeof(data7)); const char *payload = packet_parse(&handler, (const char *)data7, sizeof(data7));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data7 == 66); EXPECT_TRUE((char *)payload - (char *)&data7 == 66);
@@ -1331,6 +1338,7 @@ TEST(PACKET, ETH_VLAN_PPPOE_IP4_TCP)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data8, sizeof(data8)); const char *payload = packet_parse(&handler, (const char *)data8, sizeof(data8));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data8 == 78); EXPECT_TRUE((char *)payload - (char *)&data8 == 78);
@@ -1583,6 +1591,7 @@ TEST(PACKET, ETH_IP6_UDP_GTP_IP6_TCP_TLS)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data9, sizeof(data9)); const char *payload = packet_parse(&handler, (const char *)data9, sizeof(data9));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data9 == 130); EXPECT_TRUE((char *)payload - (char *)&data9 == 130);
@@ -1836,6 +1845,7 @@ TEST(PACKET, ETH_IP6_UDP_GTP_IP4_TCP_TLS)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data10, sizeof(data10)); const char *payload = packet_parse(&handler, (const char *)data10, sizeof(data10));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data10 == 122); EXPECT_TRUE((char *)payload - (char *)&data10 == 122);
@@ -2037,6 +2047,7 @@ TEST(PACKET, ETH_IP4_UDP_VXLAN_ETH_IP4_UDP_DNS)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data11, sizeof(data11)); const char *payload = packet_parse(&handler, (const char *)data11, sizeof(data11));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data11 == 92); EXPECT_TRUE((char *)payload - (char *)&data11 == 92);
@@ -2193,6 +2204,7 @@ TEST(PACKET, ETH_MPLS_MPLS_PWETHCW_ETH_ARP)
{ {
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data12, sizeof(data12)); const char *payload = packet_parse(&handler, (const char *)data12, sizeof(data12));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data12 == 40); EXPECT_TRUE((char *)payload - (char *)&data12 == 40);
@@ -2329,6 +2341,7 @@ TEST(PACKET, ETH_IP4_ICMP)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data13, sizeof(data13)); const char *payload = packet_parse(&handler, (const char *)data13, sizeof(data13));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data13 == 14 + 20 + 8); EXPECT_TRUE((char *)payload - (char *)&data13 == 14 + 20 + 8);
@@ -2434,6 +2447,7 @@ TEST(PACKET, ETH_IP6_ICMP6)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data14, sizeof(data14)); const char *payload = packet_parse(&handler, (const char *)data14, sizeof(data14));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data14 == 14 + 40 + 8); EXPECT_TRUE((char *)payload - (char *)&data14 == 14 + 40 + 8);
@@ -2598,6 +2612,7 @@ TEST(PACKET, ETH_IP4_UDP_L2TPV2_PPP_IP4_UDP)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data15, sizeof(data15)); const char *payload = packet_parse(&handler, (const char *)data15, sizeof(data15));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data15 == 14 + 20 + 8 + 8 + 4 + 20 + 8); EXPECT_TRUE((char *)payload - (char *)&data15 == 14 + 20 + 8 + 8 + 4 + 20 + 8);
@@ -2762,6 +2777,7 @@ TEST(PACKET, ETH_IP4_TCP_PADDING)
char buffer[256]; char buffer[256];
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data16, sizeof(data16)); const char *payload = packet_parse(&handler, (const char *)data16, sizeof(data16));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data16 == 14 + 20 + 20); EXPECT_TRUE((char *)payload - (char *)&data16 == 14 + 20 + 20);
@@ -2826,6 +2842,7 @@ TEST(PACKET, HASH_VALUE)
{ {
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data4, sizeof(data4)); const char *payload = packet_parse(&handler, (const char *)data4, sizeof(data4));
EXPECT_TRUE(payload != nullptr); EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data4 == 106); EXPECT_TRUE((char *)payload - (char *)&data4 == 106);

View File

@@ -1,6 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "packet_priv.h" #include "packet_priv.h"
#include "packet_utils.h"
/****************************************************************************** /******************************************************************************
* [Protocols in frame: eth:ethertype:ip:data] * [Protocols in frame: eth:ethertype:ip:data]
@@ -47,6 +48,7 @@ unsigned char data1[] = {
TEST(PACKET_FRAG, IPV4_FRAGMENT) TEST(PACKET_FRAG, IPV4_FRAGMENT)
{ {
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
packet_parse(&handler, (const char *)data1, sizeof(data1)); packet_parse(&handler, (const char *)data1, sizeof(data1));
EXPECT_TRUE(packet_is_fragment(&handler) == true); EXPECT_TRUE(packet_is_fragment(&handler) == true);
@@ -155,6 +157,7 @@ unsigned char data2[] = {
TEST(PACKET_FRAG, IPV6_FRAGMENT) TEST(PACKET_FRAG, IPV6_FRAGMENT)
{ {
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
packet_parse(&handler, (const char *)data2, sizeof(data2)); packet_parse(&handler, (const char *)data2, sizeof(data2));
EXPECT_TRUE(packet_is_fragment(&handler) == true); EXPECT_TRUE(packet_is_fragment(&handler) == true);
@@ -231,6 +234,7 @@ unsigned char data3[] = {
TEST(PACKET_FRAG, IPV4_IPV6_NOT_FRAGMENT) TEST(PACKET_FRAG, IPV4_IPV6_NOT_FRAGMENT)
{ {
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
packet_parse(&handler, (const char *)data3, sizeof(data3)); packet_parse(&handler, (const char *)data3, sizeof(data3));
EXPECT_TRUE(packet_is_fragment(&handler) == false); EXPECT_TRUE(packet_is_fragment(&handler) == false);
@@ -293,6 +297,7 @@ unsigned char data4[] = {
TEST(PACKET_FRAG, IPV6_IPV6_NOT_FRAGMENT) TEST(PACKET_FRAG, IPV6_IPV6_NOT_FRAGMENT)
{ {
struct packet handler; struct packet handler;
memset(&handler, 0, sizeof(handler));
packet_parse(&handler, (const char *)data4, sizeof(data4)); packet_parse(&handler, (const char *)data4, sizeof(data4));
EXPECT_TRUE(packet_is_fragment(&handler) == false); EXPECT_TRUE(packet_is_fragment(&handler) == false);

View File

@@ -12,6 +12,7 @@
#include "macro.h" #include "macro.h"
#include "dumpfile_io.h" #include "dumpfile_io.h"
#include "packet_priv.h" #include "packet_priv.h"
#include "packet_utils.h"
#include "lock_free_queue.h" #include "lock_free_queue.h"
#define MAX_PACKET_QUEUE_SIZE (4096 * 1000) #define MAX_PACKET_QUEUE_SIZE (4096 * 1000)
@@ -81,7 +82,7 @@ static void save_packet(const char *work_dir, struct packet *pkt, uint64_t idx)
char src_addr[INET6_ADDRSTRLEN] = {0}; char src_addr[INET6_ADDRSTRLEN] = {0};
char dst_addr[INET6_ADDRSTRLEN] = {0}; char dst_addr[INET6_ADDRSTRLEN] = {0};
len = packet_get_len(pkt); len = packet_get_raw_len(pkt);
memset(&tuple, 0, sizeof(struct tuple6)); memset(&tuple, 0, sizeof(struct tuple6));
packet_get_innermost_tuple6(pkt, &tuple); packet_get_innermost_tuple6(pkt, &tuple);
@@ -109,7 +110,7 @@ static void save_packet(const char *work_dir, struct packet *pkt, uint64_t idx)
fwrite(&DEFAULT_PCAP_FILE_HDR, sizeof(DEFAULT_PCAP_FILE_HDR), 1, fp); fwrite(&DEFAULT_PCAP_FILE_HDR, sizeof(DEFAULT_PCAP_FILE_HDR), 1, fp);
fwrite(&pcap_hdr, sizeof(struct pcap_pkt_hdr), 1, fp); fwrite(&pcap_hdr, sizeof(struct pcap_pkt_hdr), 1, fp);
fwrite(packet_get_data(pkt), 1, len, fp); fwrite(packet_get_raw_data(pkt), 1, len, fp);
fflush(fp); fflush(fp);
fclose(fp); fclose(fp);
PACKET_IO_LOG_STATE("save packet to %s", file); PACKET_IO_LOG_STATE("save packet to %s", file);
@@ -200,6 +201,7 @@ static void pcap_packet_handler(u_char *user, const struct pcap_pkthdr *h, const
// calculate packet hash // calculate packet hash
struct packet pkt; struct packet pkt;
memset(&pkt, 0, sizeof(struct packet));
packet_parse(&pkt, pcap_pkt->data, pcap_pkt->len); packet_parse(&pkt, pcap_pkt->data, pcap_pkt->len);
uint64_t hash = packet_get_hash(&pkt, LDBC_METHOD_HASH_INT_IP_AND_EXT_IP, 0); uint64_t hash = packet_get_hash(&pkt, LDBC_METHOD_HASH_INT_IP_AND_EXT_IP, 0);
@@ -400,7 +402,6 @@ int dumpfile_io_ingress(struct dumpfile_io *handle, uint16_t thr_idx, struct pac
memset(pkt, 0, sizeof(struct packet)); memset(pkt, 0, sizeof(struct packet));
packet_parse(pkt, pcap_pkt->data, pcap_pkt->len); packet_parse(pkt, pcap_pkt->data, pcap_pkt->len);
packet_set_origin_ctx(pkt, pcap_pkt); packet_set_origin_ctx(pkt, pcap_pkt);
packet_set_origin(pkt, PACKET_ORIGIN_DUMPFILE);
packet_set_action(pkt, PACKET_ACTION_FORWARD); packet_set_action(pkt, PACKET_ACTION_FORWARD);
nr_parsed++; nr_parsed++;
} }
@@ -418,7 +419,7 @@ void dumpfile_io_egress(struct dumpfile_io *handle, uint16_t thr_idx, struct pac
for (int i = 0; i < nr_pkts; i++) for (int i = 0; i < nr_pkts; i++)
{ {
pkt = &pkts[i]; pkt = &pkts[i];
len = packet_get_len(pkt); len = packet_get_raw_len(pkt);
stat->dev_tx_pkts++; stat->dev_tx_pkts++;
stat->dev_tx_bytes += len; stat->dev_tx_bytes += len;
@@ -447,7 +448,7 @@ void dumpfile_io_drop(struct dumpfile_io *handle, uint16_t thr_idx, struct packe
if (pcap_pkt) if (pcap_pkt)
{ {
stat->drop_pkts++; stat->drop_pkts++;
stat->drop_bytes += packet_get_len(pkt); stat->drop_bytes += packet_get_raw_len(pkt);
free(pcap_pkt); free(pcap_pkt);
} }
packet_free(pkt); packet_free(pkt);
@@ -463,7 +464,7 @@ int dumpfile_io_inject(struct dumpfile_io *handle, uint16_t thr_idx, struct pack
for (int i = 0; i < nr_pkts; i++) for (int i = 0; i < nr_pkts; i++)
{ {
pkt = &pkts[i]; pkt = &pkts[i];
len = packet_get_len(pkt); len = packet_get_raw_len(pkt);
stat->inject_pkts++; stat->inject_pkts++;
stat->inject_bytes += len; stat->inject_bytes += len;

View File

@@ -7,6 +7,7 @@
#include "macro.h" #include "macro.h"
#include "marsio.h" #include "marsio.h"
#include "packet_priv.h" #include "packet_priv.h"
#include "packet_utils.h"
#include "marsio_io.h" #include "marsio_io.h"
struct marsio_io struct marsio_io
@@ -22,6 +23,131 @@ struct marsio_io
* Private API * Private API
******************************************************************************/ ******************************************************************************/
static void metadata_from_mbuff_to_packet(marsio_buff_t *mbuff, struct packet *pkt)
{
struct route_ctx route_ctx = {0};
struct sids sids = {0};
uint64_t session_id = {0};
uint64_t domain_id = {0};
uint16_t link_id = {0};
int is_ctrl = {0};
enum packet_direction direction = PACKET_DIRECTION_OUTGOING;
route_ctx.used = marsio_buff_get_metadata(mbuff, MR_BUFF_ROUTE_CTX, &route_ctx.data, sizeof(route_ctx.data));
if (route_ctx.used > 0)
{
packet_set_route_ctx(pkt, &route_ctx);
}
else
{
PACKET_IO_LOG_ERROR("failed to get route ctx");
}
sids.used = marsio_buff_get_sid_list(mbuff, sids.sid, sizeof(sids.sid) / sizeof(sids.sid[0]));
if (sids.used > 0)
{
packet_set_sids(pkt, &sids);
}
else
{
PACKET_IO_LOG_ERROR("failed to get sids");
}
if (marsio_buff_get_metadata(mbuff, MR_BUFF_SESSION_ID, &session_id, sizeof(session_id)) == sizeof(session_id))
{
packet_set_session_id(pkt, session_id);
}
else
{
PACKET_IO_LOG_ERROR("failed to get session id");
}
// TODO
#if 0
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DOMAIN_ID, &domain_id, sizeof(domain_id)) == sizeof(domain_id))
{
packet_set_domain_id(pkt, domain_id);
}
else
{
PACKET_IO_LOG_ERROR("failed to get domain id");
}
#endif
if (marsio_buff_get_metadata(mbuff, MR_BUFF_LINK_ID, &link_id, sizeof(link_id)) == sizeof(link_id))
{
packet_set_link_id(pkt, link_id);
}
else
{
PACKET_IO_LOG_ERROR("failed to get link id");
}
is_ctrl = marsio_buff_is_ctrlbuf(mbuff);
packet_set_ctrl(pkt, is_ctrl);
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DIR, &direction, sizeof(direction)) == sizeof(direction))
{
packet_set_direction(pkt, direction);
}
else
{
PACKET_IO_LOG_ERROR("failed to get direction");
}
packet_set_action(pkt, PACKET_ACTION_FORWARD);
packet_set_origin_ctx(pkt, mbuff);
}
static void metadata_from_packet_to_mbuff(struct packet *pkt, marsio_buff_t *mbuff)
{
const struct route_ctx *route_ctx = packet_get_route_ctx(pkt);
const struct sids *sids = packet_get_sids(pkt);
uint64_t session_id = packet_get_session_id(pkt);
uint64_t domain_id = packet_get_domain_id(pkt);
uint16_t link_id = packet_get_link_id(pkt);
int is_ctrl = packet_is_ctrl(pkt);
enum packet_direction direction = packet_get_direction(pkt);
if (marsio_buff_set_metadata(mbuff, MR_BUFF_ROUTE_CTX, (void *)route_ctx->data, route_ctx->used) != 0)
{
PACKET_IO_LOG_ERROR("failed to set route ctx");
}
if (marsio_buff_set_sid_list(mbuff, (sid_t *)sids->sid, sids->used) != 0)
{
PACKET_IO_LOG_ERROR("failed to set sids");
}
if (marsio_buff_set_metadata(mbuff, MR_BUFF_SESSION_ID, &session_id, sizeof(session_id)) != 0)
{
PACKET_IO_LOG_ERROR("failed to set session id");
}
// TODO
#if 0
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DOMAIN_ID, &domain_id, sizeof(domain_id)) != 0)
{
PACKET_IO_LOG_ERROR("failed to set domain id");
}
#endif
if (marsio_buff_set_metadata(mbuff, MR_BUFF_LINK_ID, &link_id, sizeof(link_id)) != 0)
{
PACKET_IO_LOG_ERROR("failed to set link id");
}
if (is_ctrl)
{
marsio_buff_set_ctrlbuf(mbuff);
}
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DIR, &direction, sizeof(direction)) != 0)
{
PACKET_IO_LOG_ERROR("failed to set direction");
}
}
static inline int is_keepalive_packet(const char *data, int len) static inline int is_keepalive_packet(const char *data, int len)
{ {
if (data == NULL || len < (int)(sizeof(struct ethhdr))) if (data == NULL || len < (int)(sizeof(struct ethhdr)))
@@ -170,14 +296,11 @@ int marsio_io_ingress(struct marsio_io *handle, uint16_t thr_idx, struct packet
pkt = &pkts[nr_parsed]; pkt = &pkts[nr_parsed];
memset(pkt, 0, sizeof(struct packet)); memset(pkt, 0, sizeof(struct packet));
packet_parse(pkt, data, len); packet_parse(pkt, data, len);
packet_set_origin_ctx(pkt, mbuff); metadata_from_mbuff_to_packet(mbuff, pkt);
packet_set_origin(pkt, PACKET_ORIGIN_MARSIO);
packet_set_action(pkt, PACKET_ACTION_FORWARD);
nr_parsed++; nr_parsed++;
if (marsio_buff_is_ctrlbuf(mbuff)) if (marsio_buff_is_ctrlbuf(mbuff))
{ {
packet_set_ctrl(pkt);
stat->ctrl_rx_pkts++; stat->ctrl_rx_pkts++;
stat->ctrl_rx_bytes += len; stat->ctrl_rx_bytes += len;
} }
@@ -201,13 +324,14 @@ void marsio_io_egress(struct marsio_io *handle, uint16_t thr_idx, struct packet
for (int i = 0; i < nr_pkts; i++) for (int i = 0; i < nr_pkts; i++)
{ {
pkt = &pkts[i]; pkt = &pkts[i];
len = packet_get_len(pkt); len = packet_get_raw_len(pkt);
stat->dev_tx_pkts++; stat->dev_tx_pkts++;
stat->dev_tx_bytes += len; stat->dev_tx_bytes += len;
mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt); mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
assert(mbuff != NULL); assert(mbuff != NULL);
metadata_from_packet_to_mbuff(pkt, mbuff);
if (marsio_buff_is_ctrlbuf(mbuff)) if (marsio_buff_is_ctrlbuf(mbuff))
{ {
@@ -238,7 +362,7 @@ void marsio_io_drop(struct marsio_io *handle, uint16_t thr_idx, struct packet *p
if (mbuff) if (mbuff)
{ {
stat->drop_pkts++; stat->drop_pkts++;
stat->drop_bytes += packet_get_len(pkt); stat->drop_bytes += packet_get_raw_len(pkt);
marsio_buff_free(handle->mr_ins, &mbuff, 1, 0, thr_idx); marsio_buff_free(handle->mr_ins, &mbuff, 1, 0, thr_idx);
} }
packet_free(pkt); packet_free(pkt);
@@ -253,12 +377,11 @@ int marsio_io_inject(struct marsio_io *handle, uint16_t thr_idx, struct packet *
struct packet *pkt; struct packet *pkt;
marsio_buff_t *mbuff; marsio_buff_t *mbuff;
struct io_stat *stat = &handle->stat[thr_idx]; struct io_stat *stat = &handle->stat[thr_idx];
struct inject_packet_meta *meta;
for (int i = 0; i < nr_pkts; i++) for (int i = 0; i < nr_pkts; i++)
{ {
pkt = &pkts[i]; pkt = &pkts[i];
len = packet_get_len(pkt); len = packet_get_raw_len(pkt);
if (marsio_buff_malloc_global(handle->mr_ins, &mbuff, 1, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY) < 0) if (marsio_buff_malloc_global(handle->mr_ins, &mbuff, 1, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY) < 0)
{ {
@@ -278,31 +401,9 @@ int marsio_io_inject(struct marsio_io *handle, uint16_t thr_idx, struct packet *
nr_inject++; nr_inject++;
ptr = marsio_buff_append(mbuff, len); ptr = marsio_buff_append(mbuff, len);
memcpy(ptr, packet_get_data(pkt), len); memcpy(ptr, packet_get_raw_data(pkt), len);
meta = (struct inject_packet_meta *)packet_get_origin_ctx(pkt); metadata_from_packet_to_mbuff(pkt, mbuff);
if (meta)
{
if (meta->route.used && marsio_buff_set_metadata(mbuff, MR_BUFF_ROUTE_CTX, meta->route.data, meta->route.used) != 0)
{
PACKET_IO_LOG_ERROR("unable to set route context for inject packet");
}
if (meta->sids.used && marsio_buff_set_sid_list(mbuff, meta->sids.sid, meta->sids.used) != 0)
{
PACKET_IO_LOG_ERROR("unable to set sid list for inject packet");
}
if (meta->session_id && marsio_buff_set_metadata(mbuff, MR_BUFF_SESSION_ID, &meta->session_id, sizeof(meta->session_id)) != 0)
{
PACKET_IO_LOG_ERROR("unable to set session id for inject packet");
}
if (meta->link_id)
{
// TODO
}
if (meta->is_ctrl)
{
marsio_buff_set_ctrlbuf(mbuff);
}
}
marsio_send_burst_with_options(handle->mr_path, thr_idx, &mbuff, 1, MARSIO_SEND_OPT_REHASH); marsio_send_burst_with_options(handle->mr_path, thr_idx, &mbuff, 1, MARSIO_SEND_OPT_REHASH);
packet_free(pkt); packet_free(pkt);
} }

View File

@@ -71,15 +71,6 @@ struct packet_io_options
uint16_t cpu_mask[MAX_THREAD_NUM]; uint16_t cpu_mask[MAX_THREAD_NUM];
}; };
struct inject_packet_meta
{
struct route_ctx route;
struct sid_list sids;
uint64_t session_id;
uint16_t link_id;
int is_ctrl;
};
struct packet_io; struct packet_io;
struct packet_io *packet_io_new(struct packet_io_options *opts); struct packet_io *packet_io_new(struct packet_io_options *opts);
void packet_io_free(struct packet_io *packet_io); void packet_io_free(struct packet_io *packet_io);

View File

@@ -146,19 +146,19 @@ uint64_t session_get_timestamp(const struct session *sess, enum session_timestam
return sess->timestamps[type]; return sess->timestamps[type];
} }
void session_clear_sid_list(struct session *sess, enum flow_direction dir) void session_clear_sids(struct session *sess, enum flow_direction dir)
{ {
memset(&sess->sids[dir], 0, sizeof(struct sid_list)); memset(&sess->sids[dir], 0, sizeof(struct sids));
} }
void session_set_sid_list(struct session *sess, enum flow_direction dir, const struct sid_list *list) void session_set_sids(struct session *sess, enum flow_direction dir, const struct sids *sids)
{ {
sess->sids[dir] = *list; sess->sids[dir] = *sids;
} }
void session_get_sid_list(const struct session *sess, enum flow_direction dir, struct sid_list *list) const struct sids *session_get_sids(const struct session *sess, enum flow_direction dir)
{ {
*list = sess->sids[dir]; return &sess->sids[dir];
} }
void session_clear_route_ctx(struct session *sess, enum flow_direction dir) void session_clear_route_ctx(struct session *sess, enum flow_direction dir)
@@ -171,9 +171,9 @@ void session_set_route_ctx(struct session *sess, enum flow_direction dir, const
sess->route_ctx[dir] = *ctx; sess->route_ctx[dir] = *ctx;
} }
void session_get_route_ctx(const struct session *sess, enum flow_direction dir, struct route_ctx *ctx) const struct route_ctx *session_get_route_ctx(const struct session *sess, enum flow_direction dir)
{ {
*ctx = sess->route_ctx[dir]; return &sess->route_ctx[dir];
} }
void session_set_first_packet(struct session *sess, enum flow_direction dir, const struct packet *pkt) void session_set_first_packet(struct session *sess, enum flow_direction dir, const struct packet *pkt)
@@ -196,7 +196,7 @@ const struct packet *session_get0_current_packet(const struct session *sess)
return sess->curr_pkt; return sess->curr_pkt;
} }
const char *session_get0_current_payload(const struct session *sess, size_t *payload_len) const char *session_get0_current_payload(const struct session *sess, uint16_t *payload_len)
{ {
const struct packet *pkt = session_get0_current_packet(sess); const struct packet *pkt = session_get0_current_packet(sess);
if (pkt) if (pkt)

View File

@@ -488,7 +488,7 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session
if (duplicated_packet_filter_lookup(mgr->dup_pkt_filter, pkt, now)) if (duplicated_packet_filter_lookup(mgr->dup_pkt_filter, pkt, now))
{ {
session_inc_stat(sess, dir, STAT_DUPLICATE_PACKETS_BYPASS, 1); session_inc_stat(sess, dir, STAT_DUPLICATE_PACKETS_BYPASS, 1);
session_inc_stat(sess, dir, STAT_DUPLICATE_BYTES_BYPASS, packet_get_len(pkt)); session_inc_stat(sess, dir, STAT_DUPLICATE_BYTES_BYPASS, packet_get_raw_len(pkt));
switch (session_get_type(sess)) switch (session_get_type(sess))
{ {
case SESSION_TYPE_TCP: case SESSION_TYPE_TCP:
@@ -572,18 +572,13 @@ static void session_update(struct session *sess, enum session_state next_state,
} }
session_inc_stat(sess, dir, STAT_RAW_PACKETS_RECEIVED, 1); session_inc_stat(sess, dir, STAT_RAW_PACKETS_RECEIVED, 1);
session_inc_stat(sess, dir, STAT_RAW_BYTES_RECEIVED, packet_get_len(pkt)); session_inc_stat(sess, dir, STAT_RAW_BYTES_RECEIVED, packet_get_raw_len(pkt));
if (!session_get_first_packet(sess, dir)) if (!session_get_first_packet(sess, dir))
{ {
struct route_ctx ctx = {0};
struct sid_list list = {0};
packet_get_route_ctx(pkt, &ctx);
packet_get_sid_list(pkt, &list);
session_set_first_packet(sess, dir, packet_dup(pkt)); session_set_first_packet(sess, dir, packet_dup(pkt));
session_set_route_ctx(sess, dir, &ctx); session_set_route_ctx(sess, dir, packet_get_route_ctx(pkt));
session_set_sid_list(sess, dir, &list); session_set_sids(sess, dir, packet_get_sids(pkt));
} }
session_set_current_packet(sess, pkt); session_set_current_packet(sess, pkt);
@@ -1044,8 +1039,8 @@ void session_manager_free_session(struct session_manager *mgr, struct session *s
session_set_first_packet(sess, FLOW_DIRECTION_S2C, NULL); session_set_first_packet(sess, FLOW_DIRECTION_S2C, NULL);
session_clear_route_ctx(sess, FLOW_DIRECTION_C2S); session_clear_route_ctx(sess, FLOW_DIRECTION_C2S);
session_clear_route_ctx(sess, FLOW_DIRECTION_S2C); session_clear_route_ctx(sess, FLOW_DIRECTION_S2C);
session_clear_sid_list(sess, FLOW_DIRECTION_C2S); session_clear_sids(sess, FLOW_DIRECTION_C2S);
session_clear_sid_list(sess, FLOW_DIRECTION_S2C); session_clear_sids(sess, FLOW_DIRECTION_S2C);
session_set_current_packet(sess, NULL); session_set_current_packet(sess, NULL);
session_set_current_flow_direction(sess, FLOW_DIRECTION_NONE); session_set_current_flow_direction(sess, FLOW_DIRECTION_NONE);
session_pool_push(mgr->sess_pool, sess); session_pool_push(mgr->sess_pool, sess);

View File

@@ -8,6 +8,7 @@ extern "C"
#include "list.h" #include "list.h"
#include "packet_priv.h" #include "packet_priv.h"
#include "packet_utils.h"
#include "timeout.h" #include "timeout.h"
#include "uthash.h" #include "uthash.h"
#include "tuple.h" #include "tuple.h"
@@ -62,7 +63,7 @@ struct session
UT_hash_handle hh3; UT_hash_handle hh3;
struct tuple6 tuple; struct tuple6 tuple;
char tuple_str[TUPLE6_STR_SIZE]; char tuple_str[TUPLE6_STR_SIZE];
struct sid_list sids[MAX_FLOW_DIRECTION]; struct sids sids[MAX_FLOW_DIRECTION];
struct route_ctx route_ctx[MAX_FLOW_DIRECTION]; struct route_ctx route_ctx[MAX_FLOW_DIRECTION];
const struct packet *first_pkt[MAX_FLOW_DIRECTION]; const struct packet *first_pkt[MAX_FLOW_DIRECTION];
const struct packet *curr_pkt; const struct packet *curr_pkt;
@@ -98,13 +99,13 @@ void session_set_closing_reason(struct session *sess, enum closing_reason reason
void session_inc_stat(struct session *sess, enum flow_direction dir, enum session_stat stat, uint64_t val); void session_inc_stat(struct session *sess, enum flow_direction dir, enum session_stat stat, uint64_t val);
void session_set_timestamp(struct session *sess, enum session_timestamp type, uint64_t value); void session_set_timestamp(struct session *sess, enum session_timestamp type, uint64_t value);
void session_clear_sid_list(struct session *sess, enum flow_direction dir); void session_clear_sids(struct session *sess, enum flow_direction dir);
void session_set_sid_list(struct session *sess, enum flow_direction dir, const struct sid_list *list); void session_set_sids(struct session *sess, enum flow_direction dir, const struct sids *sids);
void session_get_sid_list(const struct session *sess, enum flow_direction dir, struct sid_list *list); const struct sids *session_get_sids(const struct session *sess, enum flow_direction dir);
void session_clear_route_ctx(struct session *sess, enum flow_direction dir); void session_clear_route_ctx(struct session *sess, enum flow_direction dir);
void session_set_route_ctx(struct session *sess, enum flow_direction dir, const struct route_ctx *ctx); void session_set_route_ctx(struct session *sess, enum flow_direction dir, const struct route_ctx *ctx);
void session_get_route_ctx(const struct session *sess, enum flow_direction dir, struct route_ctx *ctx); const struct route_ctx *session_get_route_ctx(const struct session *sess, enum flow_direction dir);
void session_set_first_packet(struct session *sess, enum flow_direction dir, const struct packet *pkt); void session_set_first_packet(struct session *sess, enum flow_direction dir, const struct packet *pkt);
void session_set_current_packet(struct session *sess, const struct packet *pkt); void session_set_current_packet(struct session *sess, const struct packet *pkt);

View File

@@ -309,6 +309,7 @@ TEST(CASE, TCP_FAST_OPEN)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S fast open packet\n"); printf("\n=> Packet Parse: TCP C2S fast open packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_fast_open_pkt, sizeof(tcp_fast_open_pkt)); packet_parse(&pkt, (const char *)tcp_fast_open_pkt, sizeof(tcp_fast_open_pkt));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -66,6 +66,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYN_DUP)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -81,6 +82,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYN_DUP)
// C2S SYN dup Packet // C2S SYN dup Packet
printf("\n=> Packet Parse: TCP C2S SYN dup packet\n"); printf("\n=> Packet Parse: TCP C2S SYN dup packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -98,6 +100,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYN_DUP)
printf("\n=> Packet Parse: TCP C2S SYN retransmission packet\n"); printf("\n=> Packet Parse: TCP C2S SYN retransmission packet\n");
char syn_retransmission[1500] = {0}; char syn_retransmission[1500] = {0};
memcpy(syn_retransmission, tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); memcpy(syn_retransmission, tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)syn_retransmission, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
packet_set_ip_id(&pkt, 0x1234); packet_set_ip_id(&pkt, 0x1234);
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -129,6 +132,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYNACK_DUP)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -144,6 +148,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYNACK_DUP)
// S2C SYNACK dup Packet // S2C SYNACK dup Packet
printf("\n=> Packet Parse: TCP S2C SYNACK dup packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK dup packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -161,6 +166,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYNACK_DUP)
printf("\n=> Packet Parse: TCP S2C SYNACK retransmission packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK retransmission packet\n");
char synack_retransmission[1500] = {0}; char synack_retransmission[1500] = {0};
memcpy(synack_retransmission, tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); memcpy(synack_retransmission, tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)synack_retransmission, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)synack_retransmission, sizeof(tcp_pkt2_s2c_syn_ack));
packet_set_ip_id(&pkt, 0x1234); packet_set_ip_id(&pkt, 0x1234);
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -193,6 +199,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SKIP)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -209,6 +216,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SKIP)
// C2S SYN retransmission Packet // C2S SYN retransmission Packet
printf("\n=> Packet Parse: TCP C2S SYN retransmission packet\n"); printf("\n=> Packet Parse: TCP C2S SYN retransmission packet\n");
memcpy(syn_retransmission, tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); memcpy(syn_retransmission, tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)syn_retransmission, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
packet_set_ip_id(&pkt, 0x1234); packet_set_ip_id(&pkt, 0x1234);
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -226,6 +234,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SKIP)
// C2S SYN retransmission Packet // C2S SYN retransmission Packet
printf("\n=> Packet Parse: TCP C2S SYN retransmission packet\n"); printf("\n=> Packet Parse: TCP C2S SYN retransmission packet\n");
memcpy(syn_retransmission, tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); memcpy(syn_retransmission, tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)syn_retransmission, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
packet_set_ip_id(&pkt, 0x1235); packet_set_ip_id(&pkt, 0x1235);
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -242,6 +251,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SKIP)
// C2S SYN dup Packet // C2S SYN dup Packet
printf("\n=> Packet Parse: TCP C2S SYN dup packet\n"); printf("\n=> Packet Parse: TCP C2S SYN dup packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -275,6 +285,7 @@ TEST(TCP_DUPKT_FILTER_DISABLE, SYN_DUP)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -290,6 +301,7 @@ TEST(TCP_DUPKT_FILTER_DISABLE, SYN_DUP)
// C2S SYN dup Packet // C2S SYN dup Packet
printf("\n=> Packet Parse: TCP C2S SYN dup packet\n"); printf("\n=> Packet Parse: TCP C2S SYN dup packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -323,6 +335,7 @@ TEST(TCP_DUPKT_FILTER_DISABLE, SYNACK_DUP)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -338,6 +351,7 @@ TEST(TCP_DUPKT_FILTER_DISABLE, SYNACK_DUP)
// S2C SYNACK dup Packet // S2C SYNACK dup Packet
printf("\n=> Packet Parse: TCP S2C SYNACK dup packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK dup packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -67,6 +67,7 @@ TEST(TCP_OVERLOAD, EVICT_OLD_SESS)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -107,6 +108,7 @@ TEST(TCP_OVERLOAD, EVICT_NEW_SESS)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -68,6 +68,7 @@ TEST(UDP_OVERLOAD, EVICT_OLD_SESS)
// C2S REQ Packet // C2S REQ Packet
printf("\n=> Packet Parse: UDP C2S REQ packet\n"); printf("\n=> Packet Parse: UDP C2S REQ packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req)); packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -156,6 +157,7 @@ TEST(UDP_OVERLOAD, EVICT_NEW_SESS)
// C2S REQ Packet // C2S REQ Packet
printf("\n=> Packet Parse: UDP C2S REQ packet\n"); printf("\n=> Packet Parse: UDP C2S REQ packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req)); packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -72,6 +72,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_out_of_order_pkt1, sizeof(tcp_out_of_order_pkt1)); packet_parse(&pkt, (const char *)tcp_out_of_order_pkt1, sizeof(tcp_out_of_order_pkt1));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -86,6 +87,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER)
// C2S ACK Packet // C2S ACK Packet
printf("\n=> Packet Parse: TCP C2S ACK packet\n"); printf("\n=> Packet Parse: TCP C2S ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_out_of_order_pkt2, sizeof(tcp_out_of_order_pkt2)); packet_parse(&pkt, (const char *)tcp_out_of_order_pkt2, sizeof(tcp_out_of_order_pkt2));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -100,6 +102,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER)
// C2S Data Packet 2222 // C2S Data Packet 2222
printf("\n=> Packet Parse: TCP C2S Data packet\n"); printf("\n=> Packet Parse: TCP C2S Data packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_out_of_order_pkt3, sizeof(tcp_out_of_order_pkt3)); packet_parse(&pkt, (const char *)tcp_out_of_order_pkt3, sizeof(tcp_out_of_order_pkt3));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -114,6 +117,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER)
// C2S Data Packet 3333 // C2S Data Packet 3333
printf("\n=> Packet Parse: TCP C2S Data packet\n"); printf("\n=> Packet Parse: TCP C2S Data packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_out_of_order_pkt4, sizeof(tcp_out_of_order_pkt4)); packet_parse(&pkt, (const char *)tcp_out_of_order_pkt4, sizeof(tcp_out_of_order_pkt4));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -128,6 +132,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER)
// C2S Data Packet 4444 // C2S Data Packet 4444
printf("\n=> Packet Parse: TCP C2S Data packet\n"); printf("\n=> Packet Parse: TCP C2S Data packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_out_of_order_pkt5, sizeof(tcp_out_of_order_pkt5)); packet_parse(&pkt, (const char *)tcp_out_of_order_pkt5, sizeof(tcp_out_of_order_pkt5));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -142,6 +147,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER)
// C2S Data Packet 5555 // C2S Data Packet 5555
printf("\n=> Packet Parse: TCP C2S Data packet\n"); printf("\n=> Packet Parse: TCP C2S Data packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_out_of_order_pkt6, sizeof(tcp_out_of_order_pkt6)); packet_parse(&pkt, (const char *)tcp_out_of_order_pkt6, sizeof(tcp_out_of_order_pkt6));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -156,6 +162,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER)
// C2S Data Packet 1111 // C2S Data Packet 1111
printf("\n=> Packet Parse: TCP C2S Data packet\n"); printf("\n=> Packet Parse: TCP C2S Data packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_out_of_order_pkt7, sizeof(tcp_out_of_order_pkt7)); packet_parse(&pkt, (const char *)tcp_out_of_order_pkt7, sizeof(tcp_out_of_order_pkt7));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -256,6 +263,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, SEQ_WRAPAROUND)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_seq_wraparound_pkt1, sizeof(tcp_seq_wraparound_pkt1)); packet_parse(&pkt, (const char *)tcp_seq_wraparound_pkt1, sizeof(tcp_seq_wraparound_pkt1));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -270,6 +278,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, SEQ_WRAPAROUND)
// C2S ACK Packet // C2S ACK Packet
printf("\n=> Packet Parse: TCP C2S ACK packet\n"); printf("\n=> Packet Parse: TCP C2S ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_seq_wraparound_pkt2, sizeof(tcp_seq_wraparound_pkt2)); packet_parse(&pkt, (const char *)tcp_seq_wraparound_pkt2, sizeof(tcp_seq_wraparound_pkt2));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -284,6 +293,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, SEQ_WRAPAROUND)
// C2S Data Packet // C2S Data Packet
printf("\n=> Packet Parse: TCP C2S Data packet\n"); printf("\n=> Packet Parse: TCP C2S Data packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_seq_wraparound_pkt3, sizeof(tcp_seq_wraparound_pkt3)); packet_parse(&pkt, (const char *)tcp_seq_wraparound_pkt3, sizeof(tcp_seq_wraparound_pkt3));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -302,6 +312,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, SEQ_WRAPAROUND)
// C2S Data Packet // C2S Data Packet
printf("\n=> Packet Parse: TCP C2S Data packet\n"); printf("\n=> Packet Parse: TCP C2S Data packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_seq_wraparound_pkt4, sizeof(tcp_seq_wraparound_pkt4)); packet_parse(&pkt, (const char *)tcp_seq_wraparound_pkt4, sizeof(tcp_seq_wraparound_pkt4));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -53,6 +53,7 @@ static void build_active_tcp_session(struct session_manager *mgr, struct session
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -64,6 +65,7 @@ static void build_active_tcp_session(struct session_manager *mgr, struct session
// C2S DATA Packet // C2S DATA Packet
printf("\n=> Packet Parse: TCP C2S DATA packet\n"); printf("\n=> Packet Parse: TCP C2S DATA packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt4_c2s_http_req, sizeof(tcp_pkt4_c2s_http_req)); packet_parse(&pkt, (const char *)tcp_pkt4_c2s_http_req, sizeof(tcp_pkt4_c2s_http_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -96,6 +98,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_FIN_FIN)
// C2S FIN Packet // C2S FIN Packet
printf("\n=> Packet Parse: TCP C2S FIN packet\n"); printf("\n=> Packet Parse: TCP C2S FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin)); packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -107,6 +110,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_FIN_FIN)
// S2C FIN Packet // S2C FIN Packet
printf("\n=> Packet Parse: TCP S2C FIN packet\n"); printf("\n=> Packet Parse: TCP S2C FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin)); packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -189,6 +193,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_RST)
printf("\n=> Packet Parse: TCP C2S RST packet\n"); printf("\n=> Packet Parse: TCP C2S RST packet\n");
char tcp_pkt_c2s_rst[1500] = {0}; char tcp_pkt_c2s_rst[1500] = {0};
memcpy(tcp_pkt_c2s_rst, tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin)); memcpy(tcp_pkt_c2s_rst, tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt_c2s_rst, sizeof(tcp_pkt9_c2s_fin)); packet_parse(&pkt, (const char *)tcp_pkt_c2s_rst, sizeof(tcp_pkt9_c2s_fin));
const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_TCP); const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_TCP);
EXPECT_TRUE(tcp_layer); EXPECT_TRUE(tcp_layer);
@@ -276,6 +281,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_RST)
printf("\n=> Packet Parse: TCP S2C RST packet\n"); printf("\n=> Packet Parse: TCP S2C RST packet\n");
char tcp_pkt_s2c_rst[1500] = {0}; char tcp_pkt_s2c_rst[1500] = {0};
memcpy(tcp_pkt_s2c_rst, tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin)); memcpy(tcp_pkt_s2c_rst, tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt_s2c_rst, sizeof(tcp_pkt10_s2c_fin)); packet_parse(&pkt, (const char *)tcp_pkt_s2c_rst, sizeof(tcp_pkt10_s2c_fin));
const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_TCP); const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_TCP);
EXPECT_TRUE(tcp_layer); EXPECT_TRUE(tcp_layer);
@@ -409,6 +415,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_HALF_CLOSED_TIMEOUT)
// C2S FIN Packet // C2S FIN Packet
printf("\n=> Packet Parse: TCP C2S FIN packet\n"); printf("\n=> Packet Parse: TCP C2S FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin)); packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -489,6 +496,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_HALF_CLOSED_TIMEOUT)
// S2C FIN Packet // S2C FIN Packet
printf("\n=> Packet Parse: TCP S2C FIN packet\n"); printf("\n=> Packet Parse: TCP S2C FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin)); packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -66,6 +66,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -144,6 +145,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -222,6 +224,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -233,6 +236,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -311,6 +315,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK_ACK)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -322,6 +327,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK_ACK)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -333,6 +339,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK_ACK)
// C2S ACK Packet // C2S ACK Packet
printf("\n=> Packet Parse: TCP C2S ACK packet\n"); printf("\n=> Packet Parse: TCP C2S ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt3_c2s_ack, sizeof(tcp_pkt3_c2s_ack)); packet_parse(&pkt, (const char *)tcp_pkt3_c2s_ack, sizeof(tcp_pkt3_c2s_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -413,6 +420,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_RETRANSMISSION)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -426,6 +434,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_RETRANSMISSION)
printf("\n=> Packet Parse: TCP C2S SYN retransmission packet\n"); printf("\n=> Packet Parse: TCP C2S SYN retransmission packet\n");
char syn_retransmission[1500] = {0}; char syn_retransmission[1500] = {0};
memcpy(syn_retransmission, tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); memcpy(syn_retransmission, tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)syn_retransmission, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
const struct raw_layer *ipv4_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_IPV4); const struct raw_layer *ipv4_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_IPV4);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr; struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
@@ -509,6 +518,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK_RETRANSMISSION)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -522,6 +532,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK_RETRANSMISSION)
printf("\n=> Packet Parse: TCP S2C SYNACK retransmission packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK retransmission packet\n");
char tcp_pkt_s2c_synack_retransmission[1500] = {0}; char tcp_pkt_s2c_synack_retransmission[1500] = {0};
memcpy(tcp_pkt_s2c_synack_retransmission, tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); memcpy(tcp_pkt_s2c_synack_retransmission, tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt_s2c_synack_retransmission, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt_s2c_synack_retransmission, sizeof(tcp_pkt2_s2c_syn_ack));
const struct raw_layer *ipv4_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_IPV4); const struct raw_layer *ipv4_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_IPV4);
EXPECT_TRUE(ipv4_layer); EXPECT_TRUE(ipv4_layer);
@@ -604,6 +615,7 @@ TEST(TCP_INIT_TO_OPENING, BY_C2S_ASMMETRIC)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -615,6 +627,7 @@ TEST(TCP_INIT_TO_OPENING, BY_C2S_ASMMETRIC)
// C2S ACK Packet // C2S ACK Packet
printf("\n=> Packet Parse: TCP C2S ACK packet\n"); printf("\n=> Packet Parse: TCP C2S ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt3_c2s_ack, sizeof(tcp_pkt3_c2s_ack)); packet_parse(&pkt, (const char *)tcp_pkt3_c2s_ack, sizeof(tcp_pkt3_c2s_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -693,6 +706,7 @@ TEST(TCP_INIT_TO_OPENING, BY_S2C_ASMMETRIC)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -704,6 +718,7 @@ TEST(TCP_INIT_TO_OPENING, BY_S2C_ASMMETRIC)
// S2C ACK Packet // S2C ACK Packet
printf("\n=> Packet Parse: TCP S2C ACK packet\n"); printf("\n=> Packet Parse: TCP S2C ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt5_s2c_ack, sizeof(tcp_pkt5_s2c_ack)); packet_parse(&pkt, (const char *)tcp_pkt5_s2c_ack, sizeof(tcp_pkt5_s2c_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -61,6 +61,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -92,6 +93,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -123,6 +125,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// C2S ACK Packet // C2S ACK Packet
printf("\n=> Packet Parse: TCP C2S ACK packet\n"); printf("\n=> Packet Parse: TCP C2S ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt3_c2s_ack, sizeof(tcp_pkt3_c2s_ack)); packet_parse(&pkt, (const char *)tcp_pkt3_c2s_ack, sizeof(tcp_pkt3_c2s_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -154,6 +157,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// C2S REQ Packet // C2S REQ Packet
printf("\n=> Packet Parse: TCP C2S REQ packet\n"); printf("\n=> Packet Parse: TCP C2S REQ packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt4_c2s_http_req, sizeof(tcp_pkt4_c2s_http_req)); packet_parse(&pkt, (const char *)tcp_pkt4_c2s_http_req, sizeof(tcp_pkt4_c2s_http_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -185,6 +189,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// S2C ACK Packet // S2C ACK Packet
printf("\n=> Packet Parse: TCP S2C ACK packet\n"); printf("\n=> Packet Parse: TCP S2C ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt5_s2c_ack, sizeof(tcp_pkt5_s2c_ack)); packet_parse(&pkt, (const char *)tcp_pkt5_s2c_ack, sizeof(tcp_pkt5_s2c_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -216,6 +221,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// S2C HTTP Resp Packet1 // S2C HTTP Resp Packet1
printf("\n=> Packet Parse: TCP S2C Resp packet1\n"); printf("\n=> Packet Parse: TCP S2C Resp packet1\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt6_s2c_http_resq_1, sizeof(tcp_pkt6_s2c_http_resq_1)); packet_parse(&pkt, (const char *)tcp_pkt6_s2c_http_resq_1, sizeof(tcp_pkt6_s2c_http_resq_1));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -247,6 +253,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// S2C HTTP Resp Packet2 // S2C HTTP Resp Packet2
printf("\n=> Packet Parse: TCP S2C Resp packet2\n"); printf("\n=> Packet Parse: TCP S2C Resp packet2\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt7_s2c_http_resp_2, sizeof(tcp_pkt7_s2c_http_resp_2)); packet_parse(&pkt, (const char *)tcp_pkt7_s2c_http_resp_2, sizeof(tcp_pkt7_s2c_http_resp_2));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -278,6 +285,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// C2S ACK Packet // C2S ACK Packet
printf("\n=> Packet Parse: TCP C2S ACK packet\n"); printf("\n=> Packet Parse: TCP C2S ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt8_c2s_ack, sizeof(tcp_pkt8_c2s_ack)); packet_parse(&pkt, (const char *)tcp_pkt8_c2s_ack, sizeof(tcp_pkt8_c2s_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -309,6 +317,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// C2S FIN Packet // C2S FIN Packet
printf("\n=> Packet Parse: TCP C2S FIN packet\n"); printf("\n=> Packet Parse: TCP C2S FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin)); packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -340,6 +349,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// S2C FIN Packet // S2C FIN Packet
printf("\n=> Packet Parse: TCP S2C FIN packet\n"); printf("\n=> Packet Parse: TCP S2C FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin)); packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -371,6 +381,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST)
// C2S ACK Packet // C2S ACK Packet
printf("\n=> Packet Parse: TCP C2S ACK packet\n"); printf("\n=> Packet Parse: TCP C2S ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt11_c2s_ack, sizeof(tcp_pkt11_c2s_ack)); packet_parse(&pkt, (const char *)tcp_pkt11_c2s_ack, sizeof(tcp_pkt11_c2s_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -65,6 +65,7 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYN_C2S_DATA)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -76,6 +77,7 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYN_C2S_DATA)
// C2S DATA Packet // C2S DATA Packet
printf("\n=> Packet Parse: TCP C2S DATA packet\n"); printf("\n=> Packet Parse: TCP C2S DATA packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt4_c2s_http_req, sizeof(tcp_pkt4_c2s_http_req)); packet_parse(&pkt, (const char *)tcp_pkt4_c2s_http_req, sizeof(tcp_pkt4_c2s_http_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -154,6 +156,7 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYNACK_S2C_DATA)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -165,6 +168,7 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYNACK_S2C_DATA)
// S2C DATA Packet // S2C DATA Packet
printf("\n=> Packet Parse: TCP S2C DATA packet\n"); printf("\n=> Packet Parse: TCP S2C DATA packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt6_s2c_http_resq_1, sizeof(tcp_pkt6_s2c_http_resq_1)); packet_parse(&pkt, (const char *)tcp_pkt6_s2c_http_resq_1, sizeof(tcp_pkt6_s2c_http_resq_1));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -66,6 +66,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_FIN_FIN)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -77,6 +78,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_FIN_FIN)
// C2S FIN Packet // C2S FIN Packet
printf("\n=> Packet Parse: TCP C2S FIN packet\n"); printf("\n=> Packet Parse: TCP C2S FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin)); packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -88,6 +90,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_FIN_FIN)
// S2C FIN Packet // S2C FIN Packet
printf("\n=> Packet Parse: TCP S2C FIN packet\n"); printf("\n=> Packet Parse: TCP S2C FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin)); packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -165,6 +168,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_RST)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -178,6 +182,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_RST)
printf("\n=> Packet Parse: TCP C2S RST packet\n"); printf("\n=> Packet Parse: TCP C2S RST packet\n");
char tcp_pkt_c2s_rst[1500] = {0}; char tcp_pkt_c2s_rst[1500] = {0};
memcpy(tcp_pkt_c2s_rst, tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin)); memcpy(tcp_pkt_c2s_rst, tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt_c2s_rst, sizeof(tcp_pkt9_c2s_fin)); packet_parse(&pkt, (const char *)tcp_pkt_c2s_rst, sizeof(tcp_pkt9_c2s_fin));
const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_TCP); const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_TCP);
EXPECT_TRUE(tcp_layer); EXPECT_TRUE(tcp_layer);
@@ -260,6 +265,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_RST)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -273,6 +279,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_RST)
printf("\n=> Packet Parse: TCP S2C RST packet\n"); printf("\n=> Packet Parse: TCP S2C RST packet\n");
char tcp_pkt_s2c_rst[1500] = {0}; char tcp_pkt_s2c_rst[1500] = {0};
memcpy(tcp_pkt_s2c_rst, tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin)); memcpy(tcp_pkt_s2c_rst, tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin));
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt_s2c_rst, sizeof(tcp_pkt10_s2c_fin)); packet_parse(&pkt, (const char *)tcp_pkt_s2c_rst, sizeof(tcp_pkt10_s2c_fin));
const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_TCP); const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(&pkt, LAYER_PROTO_TCP);
EXPECT_TRUE(tcp_layer); EXPECT_TRUE(tcp_layer);
@@ -354,6 +361,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_INIT_TIMEOUT)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -412,6 +420,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_HANDSHAKE_TIMEOUT)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -423,6 +432,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_HANDSHAKE_TIMEOUT)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -501,6 +511,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_DATA_TIMEOUT)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -512,6 +523,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_DATA_TIMEOUT)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -523,6 +535,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_DATA_TIMEOUT)
// C2S ACK Packet // C2S ACK Packet
printf("\n=> Packet Parse: TCP C2S ACK packet\n"); printf("\n=> Packet Parse: TCP C2S ACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt3_c2s_ack, sizeof(tcp_pkt3_c2s_ack)); packet_parse(&pkt, (const char *)tcp_pkt3_c2s_ack, sizeof(tcp_pkt3_c2s_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -601,6 +614,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_HALF_FIN)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -612,6 +626,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_HALF_FIN)
// C2S FIN Packet // C2S FIN Packet
printf("\n=> Packet Parse: TCP C2S FIN packet\n"); printf("\n=> Packet Parse: TCP C2S FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin)); packet_parse(&pkt, (const char *)tcp_pkt9_c2s_fin, sizeof(tcp_pkt9_c2s_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -689,6 +704,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_HALF_FIN)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -700,6 +716,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_HALF_FIN)
// S2C FIN Packet // S2C FIN Packet
printf("\n=> Packet Parse: TCP S2C FIN packet\n"); printf("\n=> Packet Parse: TCP S2C FIN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin)); packet_parse(&pkt, (const char *)tcp_pkt10_s2c_fin, sizeof(tcp_pkt10_s2c_fin));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -61,6 +61,7 @@ TEST(UDP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING, TEST)
// C2S REQ Packet // C2S REQ Packet
printf("\n=> Packet Parse: UDP C2S REQ packet\n"); printf("\n=> Packet Parse: UDP C2S REQ packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req)); packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -91,6 +92,7 @@ TEST(UDP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING, TEST)
// S2C RESP Packet // S2C RESP Packet
printf("\n=> Packet Parse: UDP S2C RESP packet\n"); printf("\n=> Packet Parse: UDP S2C RESP packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt2_dns_resp, sizeof(udp_pkt2_dns_resp)); packet_parse(&pkt, (const char *)udp_pkt2_dns_resp, sizeof(udp_pkt2_dns_resp));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -66,6 +66,7 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_C2S)
// C2S REQ Packet // C2S REQ Packet
printf("\n=> Packet Parse: UDP C2S REQ packet\n"); printf("\n=> Packet Parse: UDP C2S REQ packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req)); packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -145,6 +146,7 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_S2C)
// S2C RESP Packet // S2C RESP Packet
printf("\n=> Packet Parse: UDP S2C RESP packet\n"); printf("\n=> Packet Parse: UDP S2C RESP packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt2_dns_resp, sizeof(udp_pkt2_dns_resp)); packet_parse(&pkt, (const char *)udp_pkt2_dns_resp, sizeof(udp_pkt2_dns_resp));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -58,6 +58,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_DATA)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -69,6 +70,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_DATA)
// C2S DATA Packet // C2S DATA Packet
printf("\n=> Packet Parse: TCP C2S DATA packet\n"); printf("\n=> Packet Parse: TCP C2S DATA packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt4_c2s_http_req, sizeof(tcp_pkt4_c2s_http_req)); packet_parse(&pkt, (const char *)tcp_pkt4_c2s_http_req, sizeof(tcp_pkt4_c2s_http_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -59,6 +59,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_HANDSHAKE)
// S2C SYNACK Packet // S2C SYNACK Packet
printf("\n=> Packet Parse: TCP S2C SYNACK packet\n"); printf("\n=> Packet Parse: TCP S2C SYNACK packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack)); packet_parse(&pkt, (const char *)tcp_pkt2_s2c_syn_ack, sizeof(tcp_pkt2_s2c_syn_ack));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -59,6 +59,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_INIT)
// C2S SYN Packet // C2S SYN Packet
printf("\n=> Packet Parse: TCP C2S SYN packet\n"); printf("\n=> Packet Parse: TCP C2S SYN packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn)); packet_parse(&pkt, (const char *)tcp_pkt1_c2s_syn, sizeof(tcp_pkt1_c2s_syn));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -58,6 +58,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA1)
// C2S REQ Packet // C2S REQ Packet
printf("\n=> Packet Parse: UDP C2S REQ packet\n"); printf("\n=> Packet Parse: UDP C2S REQ packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req)); packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -94,6 +95,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA2)
// C2S REQ Packet // C2S REQ Packet
printf("\n=> Packet Parse: UDP C2S REQ packet\n"); printf("\n=> Packet Parse: UDP C2S REQ packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req)); packet_parse(&pkt, (const char *)udp_pkt1_dns_req, sizeof(udp_pkt1_dns_req));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");
@@ -105,6 +107,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA2)
// S2C RESP Packet // S2C RESP Packet
printf("\n=> Packet Parse: UDP S2C RESP packet\n"); printf("\n=> Packet Parse: UDP S2C RESP packet\n");
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)udp_pkt2_dns_resp, sizeof(udp_pkt2_dns_resp)); packet_parse(&pkt, (const char *)udp_pkt2_dns_resp, sizeof(udp_pkt2_dns_resp));
printf("<= Packet Parse: done\n\n"); printf("<= Packet Parse: done\n\n");

View File

@@ -235,7 +235,7 @@ static int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t
struct ip6_hdr *ip6hdr; struct ip6_hdr *ip6hdr;
struct raw_layer *curr; struct raw_layer *curr;
struct raw_layer *last; struct raw_layer *last;
int len = packet_get_len(first); int len = packet_get_raw_len(first);
int layers = packet_get_layer_count(first); int layers = packet_get_layer_count(first);
if ((tcp_pld == NULL && pld_len > 0) || (tcp_pld != NULL && pld_len <= 0)) if ((tcp_pld == NULL && pld_len > 0) || (tcp_pld != NULL && pld_len <= 0))
@@ -248,11 +248,11 @@ static int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t
return -ENOMEM; return -ENOMEM;
} }
memcpy(pkt_buff, packet_get_data(first), len); memcpy(pkt_buff, packet_get_raw_data(first), len);
for (int i = layers - 1; i >= 0; i--) for (int i = layers - 1; i >= 0; i--)
{ {
curr = (struct raw_layer *)packet_get_raw_layer(first, i); curr = (struct raw_layer *)packet_get_raw_layer(first, i);
switch (curr->type) switch (curr->proto)
{ {
case LAYER_PROTO_TCP: case LAYER_PROTO_TCP:
trim = curr->hdr_len + curr->pld_len - sizeof(struct tcphdr) - pld_len; trim = curr->hdr_len + curr->pld_len - sizeof(struct tcphdr) - pld_len;
@@ -274,12 +274,12 @@ static int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t
case LAYER_PROTO_IPV4: case LAYER_PROTO_IPV4:
iphdr = (struct ip *)(pkt_buff + curr->hdr_offset); iphdr = (struct ip *)(pkt_buff + curr->hdr_offset);
last = (struct raw_layer *)packet_get_raw_layer(first, i + 1); last = (struct raw_layer *)packet_get_raw_layer(first, i + 1);
if (last->type == LAYER_PROTO_TCP) if (last->proto == LAYER_PROTO_TCP)
{ {
tcphdr = (struct tcphdr *)(pkt_buff + last->hdr_offset); tcphdr = (struct tcphdr *)(pkt_buff + last->hdr_offset);
tcphdr->th_sum = checksum_v4(tcphdr, len - trim - last->hdr_offset, IPPROTO_TCP, &iphdr->ip_src, &iphdr->ip_dst); tcphdr->th_sum = checksum_v4(tcphdr, len - trim - last->hdr_offset, IPPROTO_TCP, &iphdr->ip_src, &iphdr->ip_dst);
} }
if (last->type == LAYER_PROTO_UDP) if (last->proto == LAYER_PROTO_UDP)
{ {
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset); 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); udphdr->uh_sum = checksum_v4(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &iphdr->ip_src, &iphdr->ip_dst);
@@ -289,12 +289,12 @@ static int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t
case LAYER_PROTO_IPV6: case LAYER_PROTO_IPV6:
ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset); ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset);
last = (struct raw_layer *)packet_get_raw_layer(first, i + 1); last = (struct raw_layer *)packet_get_raw_layer(first, i + 1);
if (last->type == LAYER_PROTO_TCP) if (last->proto == LAYER_PROTO_TCP)
{ {
tcphdr = (struct tcphdr *)(pkt_buff + last->hdr_offset); tcphdr = (struct tcphdr *)(pkt_buff + last->hdr_offset);
tcphdr->th_sum = checksum_v6(tcphdr, len - trim - last->hdr_offset, IPPROTO_TCP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst); tcphdr->th_sum = checksum_v6(tcphdr, len - trim - last->hdr_offset, IPPROTO_TCP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
} }
if (last->type == LAYER_PROTO_UDP) if (last->proto == LAYER_PROTO_UDP)
{ {
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset); 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); udphdr->uh_sum = checksum_v6(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
@@ -322,7 +322,7 @@ static int build_udp_packet(const struct packet *first, const char *udp_pld, int
struct ip6_hdr *ip6hdr; struct ip6_hdr *ip6hdr;
struct raw_layer *curr; struct raw_layer *curr;
struct raw_layer *last; struct raw_layer *last;
int len = packet_get_len(first); int len = packet_get_raw_len(first);
int layers = packet_get_layer_count(first); int layers = packet_get_layer_count(first);
if ((udp_pld == NULL && pld_len > 0) || (udp_pld != NULL && pld_len <= 0)) if ((udp_pld == NULL && pld_len > 0) || (udp_pld != NULL && pld_len <= 0))
@@ -335,11 +335,11 @@ static int build_udp_packet(const struct packet *first, const char *udp_pld, int
return -ENOMEM; return -ENOMEM;
} }
memcpy(pkt_buff, packet_get_data(first), len); memcpy(pkt_buff, packet_get_raw_data(first), len);
for (int i = layers - 1; i >= 0; i--) for (int i = layers - 1; i >= 0; i--)
{ {
curr = (struct raw_layer *)packet_get_raw_layer(first, i); curr = (struct raw_layer *)packet_get_raw_layer(first, i);
switch (curr->type) switch (curr->proto)
{ {
case LAYER_PROTO_UDP: case LAYER_PROTO_UDP:
trim = curr->hdr_len + curr->pld_len - sizeof(struct udphdr) - pld_len; trim = curr->hdr_len + curr->pld_len - sizeof(struct udphdr) - pld_len;
@@ -357,7 +357,7 @@ static int build_udp_packet(const struct packet *first, const char *udp_pld, int
case LAYER_PROTO_IPV4: case LAYER_PROTO_IPV4:
iphdr = (struct ip *)(pkt_buff + curr->hdr_offset); iphdr = (struct ip *)(pkt_buff + curr->hdr_offset);
last = (struct raw_layer *)packet_get_raw_layer(first, i + 1); last = (struct raw_layer *)packet_get_raw_layer(first, i + 1);
if (last->type == LAYER_PROTO_UDP) if (last->proto == LAYER_PROTO_UDP)
{ {
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset); 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); udphdr->uh_sum = checksum_v4(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &iphdr->ip_src, &iphdr->ip_dst);
@@ -367,7 +367,7 @@ static int build_udp_packet(const struct packet *first, const char *udp_pld, int
case LAYER_PROTO_IPV6: case LAYER_PROTO_IPV6:
ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset); ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset);
last = (struct raw_layer *)packet_get_raw_layer(first, i + 1); last = (struct raw_layer *)packet_get_raw_layer(first, i + 1);
if (last->type == LAYER_PROTO_UDP) if (last->proto == LAYER_PROTO_UDP)
{ {
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset); 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); udphdr->uh_sum = checksum_v6(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
@@ -435,15 +435,12 @@ static int inject_tcp_packet(struct stellar *st, const struct session *sess, enu
return 0; 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; struct packet inj_pkt;
memset(&inj_pkt, 0, sizeof(inj_pkt));
packet_parse(&inj_pkt, buff, pkt_len); packet_parse(&inj_pkt, buff, pkt_len);
packet_set_origin(&inj_pkt, PACKET_ORIGIN_USERSTACK); packet_set_session_id(&inj_pkt, session_get_id(sess));
packet_set_origin_ctx(&inj_pkt, &meta); packet_set_sids(&inj_pkt, session_get_sids(sess, inject_dir));
packet_set_route_ctx(&inj_pkt, session_get_route_ctx(sess, inject_dir));
session_manager_record_duplicated_packet(sess_mgr, &inj_pkt, time_ms); session_manager_record_duplicated_packet(sess_mgr, &inj_pkt, time_ms);
if (packet_io_inject(packet_io, thr_idx, &inj_pkt, 1) == 1) if (packet_io_inject(packet_io, thr_idx, &inj_pkt, 1) == 1)
{ {
@@ -500,15 +497,12 @@ static int inject_udp_packet(struct stellar *st, const struct session *sess, enu
return 0; 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; struct packet inj_pkt;
memset(&inj_pkt, 0, sizeof(inj_pkt));
packet_parse(&inj_pkt, buff, pkt_len); packet_parse(&inj_pkt, buff, pkt_len);
packet_set_origin(&inj_pkt, PACKET_ORIGIN_USERSTACK); packet_set_session_id(&inj_pkt, session_get_id(sess));
packet_set_origin_ctx(&inj_pkt, &meta); packet_set_sids(&inj_pkt, session_get_sids(sess, inject_dir));
packet_set_route_ctx(&inj_pkt, session_get_route_ctx(sess, inject_dir));
session_manager_record_duplicated_packet(sess_mgr, &inj_pkt, time_ms); session_manager_record_duplicated_packet(sess_mgr, &inj_pkt, time_ms);
if (packet_io_inject(packet_io, thr_idx, &inj_pkt, 1) == 1) if (packet_io_inject(packet_io, thr_idx, &inj_pkt, 1) == 1)
{ {
@@ -553,7 +547,7 @@ int stellar_inject_udp_payload(struct stellar *st, const struct session *sess, e
return inject_udp_packet(st, sess, inject_dir, payload, 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 sid_list *sids, const char *msg, 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)
{ {
// TODO // TODO
return 0; return 0;

View File

@@ -57,15 +57,19 @@ static void update_session_stat(struct session *sess, struct packet *pkt)
enum flow_direction dir = session_get_current_flow_direction(sess); enum flow_direction dir = session_get_current_flow_direction(sess);
assert(dir != FLOW_DIRECTION_NONE); assert(dir != FLOW_DIRECTION_NONE);
int is_ctrl = packet_is_ctrl(pkt); int is_ctrl = packet_is_ctrl(pkt);
uint16_t len = packet_get_raw_len(pkt);
switch (packet_get_action(pkt)) switch (packet_get_action(pkt))
{ {
case PACKET_ACTION_DROP: case PACKET_ACTION_DROP:
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_DROPPED : STAT_RAW_PACKETS_DROPPED), 1); session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_DROPPED : STAT_RAW_PACKETS_DROPPED), 1);
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_DROPPED : STAT_RAW_BYTES_DROPPED), packet_get_len(pkt)); session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_DROPPED : STAT_RAW_BYTES_DROPPED), len);
break; break;
case PACKET_ACTION_FORWARD: case PACKET_ACTION_FORWARD:
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_TRANSMITTED : STAT_RAW_PACKETS_TRANSMITTED), 1); session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_TRANSMITTED : STAT_RAW_PACKETS_TRANSMITTED), 1);
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_TRANSMITTED : STAT_RAW_BYTES_TRANSMITTED), packet_get_len(pkt)); session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_TRANSMITTED : STAT_RAW_BYTES_TRANSMITTED), len);
break;
case PACKET_ACTION_DEFER:
// TODO
break; break;
default: default:
assert(0); assert(0);
@@ -222,8 +226,9 @@ static void *work_thread(void *arg)
packet_set_action(pkt, PACKET_ACTION_DROP); packet_set_action(pkt, PACKET_ACTION_DROP);
} }
update_session_stat(sess, pkt); update_session_stat(sess, pkt);
if (packet_get_action(pkt) == PACKET_ACTION_DROP) switch (packet_get_action(pkt))
{ {
case PACKET_ACTION_DROP:
if (pkt == defraged_pkt) if (pkt == defraged_pkt)
{ {
packet_io_drop(packet_io, thr_idx, &packets[i], 1); packet_io_drop(packet_io, thr_idx, &packets[i], 1);
@@ -233,9 +238,8 @@ static void *work_thread(void *arg)
{ {
packet_io_drop(packet_io, thr_idx, pkt, 1); packet_io_drop(packet_io, thr_idx, pkt, 1);
} }
} break;
else case PACKET_ACTION_FORWARD:
{
if (pkt == defraged_pkt) if (pkt == defraged_pkt)
{ {
packet_io_egress(packet_io, thr_idx, &packets[i], 1); packet_io_egress(packet_io, thr_idx, &packets[i], 1);
@@ -245,6 +249,18 @@ static void *work_thread(void *arg)
{ {
packet_io_egress(packet_io, thr_idx, pkt, 1); packet_io_egress(packet_io, thr_idx, pkt, 1);
} }
case PACKET_ACTION_DEFER:
if (pkt == defraged_pkt)
{
// TODO
// defer current packe: &packets[i], free defraged_pkt, update meta
packet_free(defraged_pkt);
}
else
{
// do nothing
}
break;
} }
} }

View File

@@ -2,17 +2,15 @@ LIBSTELLAR_DEVEL {
global: global:
packet_get_direction; packet_get_direction;
packet_get_session_id; packet_get_session_id;
packet_prepend_sid_list; packet_prepend_sids;
packet_get_layer_count; packet_get_layer_count;
packet_get_raw_layer; packet_get_layer;
packet_get_data; packet_get_raw_data;
packet_get_len; packet_get_raw_len;
packet_get_payload; packet_get_payload;
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;
session_exdata_free; session_exdata_free;
stellar_session_exdata_new_index; stellar_session_exdata_new_index;

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.addr.data.v4) != 1) if (inet_pton(AF_INET, host, &rule.addr4) != 1)
{ {
if (inet_pton(AF_INET6, host, &rule.addr.data.v6) != 1) if (inet_pton(AF_INET6, host, &rule.addr6) != 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.addr.family = AF_INET6; rule.family = AF_INET6;
} }
} }
else else
{ {
rule.addr.family = AF_INET; rule.family = AF_INET;
} }
} }

View File

@@ -24,7 +24,9 @@ enum packet_inject_type
struct packet_inject_rule struct packet_inject_rule
{ {
struct address addr; /* network order */ int family; /* AF_INET or AF_INET6 */
struct in_addr addr4; /* network order */
struct in6_addr addr6; /* network order */
uint16_t port; /* network order */ uint16_t 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/packet.h" #include "stellar/layer.h"
#include "stellar/session_mq.h" #include "stellar/session_mq.h"
#include "packet_inject_main.h" #include "packet_inject_main.h"
@@ -38,27 +38,51 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi
struct packet *pkt = (struct packet *)msg; struct packet *pkt = (struct packet *)msg;
char buffer[1024] = {0}; char buffer[1024] = {0};
uint16_t src_port = 0; int is_ip_hit = 0;
uint16_t dst_port = 0; int is_port_hit = 0;
struct address src_addr = {0}; struct layer layer;
struct address dst_addr = {0}; PACKET_FOREACH_LAYER_REVERSE(pkt, layer)
{
switch (layer.proto)
{
case LAYER_PROTO_IPV4:
if (memcmp(&layer.header.ip4->ip_src, &rule.addr4, sizeof(struct in_addr)) == 0 ||
memcmp(&layer.header.ip4->ip_dst, &rule.addr4, sizeof(struct in_addr)) == 0)
{
is_ip_hit = 1;
}
break;
case LAYER_PROTO_IPV6:
if (memcmp(&layer.header.ip6->ip6_src, &rule.addr6, sizeof(struct in6_addr)) == 0 ||
memcmp(&layer.header.ip6->ip6_dst, &rule.addr6, sizeof(struct in6_addr)) == 0)
{
is_ip_hit = 1;
}
break;
case LAYER_PROTO_TCP:
if (layer.header.tcp->th_sport == rule.port ||
layer.header.tcp->th_dport == rule.port)
{
is_port_hit = 1;
}
break;
case LAYER_PROTO_UDP:
if (layer.header.udp->uh_sport == rule.port ||
layer.header.udp->uh_dport == rule.port)
{
is_port_hit = 1;
}
break;
default:
break;
}
}
packet_get_addr(pkt, &src_addr, &dst_addr); if (rule.family && !is_ip_hit)
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.addr.family == AF_INET6 && if (rule.port && !is_port_hit)
memcmp(&src_addr.data.v6, &rule.addr.data.v6, sizeof(struct in6_addr)) &&
memcmp(&dst_addr.data.v6, &rule.addr.data.v6, sizeof(struct in6_addr)))
{
return;
}
if (rule.port != 0 && src_port != rule.port && dst_port != rule.port)
{ {
return; return;
} }

View File

@@ -95,7 +95,7 @@ static void packet_to_tshark_format(const struct packet *pkt, uint64_t idx)
memset(tmp_src_buff, 0, sizeof(tmp_src_buff)); memset(tmp_src_buff, 0, sizeof(tmp_src_buff));
memset(tmp_dst_buff, 0, sizeof(tmp_dst_buff)); memset(tmp_dst_buff, 0, sizeof(tmp_dst_buff));
const struct raw_layer *layer = packet_get_raw_layer(pkt, i); const struct raw_layer *layer = packet_get_raw_layer(pkt, i);
switch (layer->type) switch (layer->proto)
{ {
case LAYER_PROTO_ETHER: case LAYER_PROTO_ETHER:
buffer_push(&buff_proto, "eth:ethertype"); buffer_push(&buff_proto, "eth:ethertype");
@@ -232,6 +232,7 @@ static void packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_ch
struct options *opts = (struct options *)user; struct options *opts = (struct options *)user;
struct packet pkt; struct packet pkt;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)bytes, h->caplen); packet_parse(&pkt, (const char *)bytes, h->caplen);
number++; number++;