rename dupkt -> dupkt_filter

This commit is contained in:
luwenpeng
2024-01-11 14:36:14 +08:00
parent d44eda2c86
commit 76d5fb36bb
6 changed files with 3 additions and 1 deletions

View File

@@ -0,0 +1,12 @@
###############################################################################
# dupkt_filter
###############################################################################
add_library(dupkt_filter dupkt_filter.cpp)
target_include_directories(dupkt_filter PUBLIC ${CMAKE_SOURCE_DIR}/src/dupkt)
target_include_directories(dupkt_filter PUBLIC ${CMAKE_SOURCE_DIR}/src/packet)
target_include_directories(dupkt_filter PUBLIC ${CMAKE_SOURCE_DIR}/src/timestamp)
target_include_directories(dupkt_filter PUBLIC ${CMAKE_SOURCE_DIR}/deps/dablooms)
target_link_libraries(dupkt_filter packet timestamp dablooms)
add_subdirectory(test)

View File

@@ -0,0 +1,145 @@
#include <string.h>
#include "timestamp.h"
#include "dablooms.h"
#include "tcp_helpers.h"
#include "ipv4_helpers.h"
#include "dupkt_filter.h"
struct packet_identify
{
// TCP
uint32_t seq;
uint32_t ack;
uint16_t sport;
uint16_t dport;
uint16_t l4_checksum;
// IPv4
uint16_t ip_id;
uint32_t ip_src;
uint32_t ip_dst;
} __attribute__((__packed__));
struct dupkt_filter
{
uint8_t enable;
unsigned int capacity;
double error_rate;
int timeout_s;
struct expiry_dablooms_handle *handle;
};
// return 0: success
// reutrn -1: error
static inline int packet_get_identify(const struct packet *packet, struct packet_identify *key)
{
const struct layer_record *ipv4_layer = packet_get_innermost_layer(packet, LAYER_TYPE_IPV4);
if (ipv4_layer == NULL)
{
return -1;
}
const struct layer_record *tcp_layer = packet_get_innermost_layer(packet, LAYER_TYPE_TCP);
if (tcp_layer == NULL)
{
return -1;
}
memset(key, 0, sizeof(struct packet_identify));
const struct ip *iphdr = (const struct ip *)ipv4_layer->hdr_ptr;
key->ip_id = ipv4_hdr_get_ipid(iphdr);
key->ip_src = ipv4_hdr_get_src(iphdr);
key->ip_dst = ipv4_hdr_get_dst(iphdr);
const struct tcphdr *tcphdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
key->seq = tcp_hdr_get_seq(tcphdr);
key->ack = tcp_hdr_get_ack(tcphdr);
key->sport = tcp_hdr_get_sport(tcphdr);
key->dport = tcp_hdr_get_dport(tcphdr);
key->l4_checksum = tcp_hdr_get_checksum(tcphdr);
return 0;
}
struct dupkt_filter *dupkt_filter_create(uint8_t enable, unsigned int capacity, double error_rate, int timeout_s)
{
struct dupkt_filter *filter = (struct dupkt_filter *)calloc(1, sizeof(struct dupkt_filter));
if (filter == NULL)
{
return NULL;
}
filter->enable = enable;
filter->capacity = capacity;
filter->error_rate = error_rate;
filter->timeout_s = timeout_s;
if (filter->enable == 0)
{
return filter;
}
filter->handle = expiry_dablooms_init(filter->capacity, filter->error_rate, timestamp_get_sec(), filter->timeout_s);
if (filter->handle == NULL)
{
free(filter);
return NULL;
}
return filter;
}
void dupkt_filter_destroy(struct dupkt_filter *filter)
{
if (filter)
{
if (filter->handle)
{
expiry_dablooms_destroy(filter->handle);
filter->handle = NULL;
}
free(filter);
filter = NULL;
}
}
// return 1: found
// reutrn 0: no found
int dupkt_filter_search(struct dupkt_filter *filter, const struct packet *packet)
{
if (filter->enable == 0)
{
return 0;
}
struct packet_identify identify;
if (packet_get_identify(packet, &identify) == -1)
{
return 0;
}
if (expiry_dablooms_search(filter->handle, (const char *)&identify, sizeof(struct packet_identify), timestamp_get_sec()) == 1)
{
return 1;
}
return 0;
}
void dupkt_filter_add(struct dupkt_filter *filter, const struct packet *packet)
{
if (filter->enable == 0)
{
return;
}
struct packet_identify identify;
if (packet_get_identify(packet, &identify) == -1)
{
return;
}
expiry_dablooms_add(filter->handle, (const char *)&identify, sizeof(struct packet_identify), timestamp_get_sec());
}

View File

@@ -0,0 +1,25 @@
#ifndef _DUPKT_FILTER_H
#define _DUPKT_FILTER_H
#ifdef __cpluscplus
extern "C"
{
#endif
// Duplicated Packet Filter for IPv4-based TCP Packet
#include "packet.h"
struct dupkt_filter *dupkt_filter_create(uint8_t enable, unsigned int capacity, double error_rate, int timeout_s);
void dupkt_filter_destroy(struct dupkt_filter *filter);
// return 1: found
// reutrn 0: no found
int dupkt_filter_search(struct dupkt_filter *filter, const struct packet *packet);
void dupkt_filter_add(struct dupkt_filter *filter, const struct packet *packet);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -0,0 +1,9 @@
###############################################################################
# gtest
###############################################################################
add_executable(gtest_dupkt_filter gtest_dupkt_filter.cpp)
target_link_libraries(gtest_dupkt_filter dupkt_filter gtest)
include(GoogleTest)
gtest_discover_tests(gtest_dupkt_filter)

View File

@@ -0,0 +1,117 @@
#include <gtest/gtest.h>
#include "dupkt_filter.h"
#include "timestamp.h"
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:ipv6:tcp]
******************************************************************************
*
* Frame 1: 106 bytes on wire (848 bits), 106 bytes captured (848 bits)
* Ethernet II, Src: JuniperN_45:88:29 (2c:6b:f5:45:88:29), Dst: JuniperN_2a:a2:00 (5c:5e:ab:2a:a2:00)
* Destination: JuniperN_2a:a2:00 (5c:5e:ab:2a:a2:00)
* Source: JuniperN_45:88:29 (2c:6b:f5:45:88:29)
* Type: IPv4 (0x0800)
* Internet Protocol Version 4, Src: 210.77.88.163, Dst: 59.66.4.50
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* Total Length: 92
* Identification: 0x0b4d (2893)
* 000. .... = Flags: 0x0
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 59
* Protocol: IPv6 (41)
* Header Checksum: 0x09c8 [validation disabled]
* [Header checksum status: Unverified]
* Source Address: 210.77.88.163
* Destination Address: 59.66.4.50
* Internet Protocol Version 6, Src: 2001:da8:200:900e:200:5efe:d24d:58a3, Dst: 2600:140e:6::1702:1058
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 32
* Next Header: TCP (6)
* Hop Limit: 64
* Source Address: 2001:da8:200:900e:200:5efe:d24d:58a3
* Destination Address: 2600:140e:6::1702:1058
* [Source ISATAP IPv4: 210.77.88.163]
* Transmission Control Protocol, Src Port: 52556, Dst Port: 80, Seq: 0, Len: 0
* Source Port: 52556
* Destination Port: 80
* [Stream index: 0]
* [Conversation completeness: Complete, WITH_DATA (31)]
* [TCP Segment Len: 0]
* Sequence Number: 0 (relative sequence number)
* Sequence Number (raw): 2172673142
* [Next Sequence Number: 1 (relative sequence number)]
* Acknowledgment Number: 0
* Acknowledgment number (raw): 0
* 1000 .... = Header Length: 32 bytes (8)
* Flags: 0x002 (SYN)
* Window: 8192
* [Calculated window size: 8192]
* Checksum: 0xf757 [unverified]
* [Checksum Status: Unverified]
* Urgent Pointer: 0
* Options: (12 bytes), Maximum segment size, No-Operation (NOP), Window scale, No-Operation (NOP), No-Operation (NOP), SACK permitted
* [Timestamps]
*/
unsigned char data[] = {
0x5c, 0x5e, 0xab, 0x2a, 0xa2, 0x00, 0x2c, 0x6b, 0xf5, 0x45, 0x88, 0x29, 0x08, 0x00, 0x45, 0x00, 0x00, 0x5c, 0x0b, 0x4d, 0x00, 0x00, 0x3b, 0x29, 0x09, 0xc8,
0xd2, 0x4d, 0x58, 0xa3, 0x3b, 0x42, 0x04, 0x32, 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x20, 0x01, 0x0d, 0xa8, 0x02, 0x00, 0x90, 0x0e, 0x02, 0x00,
0x5e, 0xfe, 0xd2, 0x4d, 0x58, 0xa3, 0x26, 0x00, 0x14, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x02, 0x10, 0x58, 0xcd, 0x4c, 0x00, 0x50,
0x81, 0x80, 0x5c, 0x76, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x20, 0x00, 0xf7, 0x57, 0x00, 0x00, 0x02, 0x04, 0x04, 0xc4, 0x01, 0x03, 0x03, 0x08, 0x01, 0x01,
0x04, 0x02};
struct config
{
uint8_t enable;
unsigned int capacity;
double error_rate;
int timeout_s;
} config = {
.enable = 1,
.capacity = 1000000,
.error_rate = 0.00001,
.timeout_s = 10,
};
TEST(DUPKT_FILTER, TEST)
{
timestamp_update();
struct packet pkt;
packet_parse(&pkt, (const char *)data, sizeof(data));
struct dupkt_filter *filter = dupkt_filter_create(config.enable, config.capacity, config.error_rate, config.timeout_s);
EXPECT_TRUE(filter != nullptr);
EXPECT_TRUE(dupkt_filter_search(filter, &pkt) == 0); // no found
dupkt_filter_add(filter, &pkt); // add
for (int i = 0; i < 12; i++)
{
timestamp_update();
if (i < config.timeout_s)
{
EXPECT_TRUE(dupkt_filter_search(filter, &pkt) == 1); // found
}
else
{
EXPECT_TRUE(dupkt_filter_search(filter, &pkt) == 0); // no found
}
sleep(1);
printf("sleep[%02d] 1s\n", i);
}
dupkt_filter_destroy(filter);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}