update TCP utils
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "timestamp.h"
|
#include "timestamp.h"
|
||||||
#include "dablooms.h"
|
#include "dablooms.h"
|
||||||
#include "tcp_helpers.h"
|
#include "tcp_utils.h"
|
||||||
#include "ipv4_helpers.h"
|
#include "ipv4_helpers.h"
|
||||||
#include "dupkt_filter.h"
|
#include "dupkt_filter.h"
|
||||||
|
|
||||||
@@ -11,8 +11,8 @@ struct dupkt_filter_key
|
|||||||
// TCP
|
// TCP
|
||||||
uint32_t seq;
|
uint32_t seq;
|
||||||
uint32_t ack;
|
uint32_t ack;
|
||||||
uint16_t src_port; /* network order */
|
uint16_t src_port; /* host order */
|
||||||
uint16_t dst_port; /* network order */
|
uint16_t dst_port; /* host order */
|
||||||
uint16_t l4_checksum;
|
uint16_t l4_checksum;
|
||||||
|
|
||||||
// IPv4
|
// IPv4
|
||||||
@@ -53,15 +53,15 @@ static inline int packet_get_dupkt_filter_key(const struct packet *packet, struc
|
|||||||
memset(key, 0, sizeof(struct dupkt_filter_key));
|
memset(key, 0, sizeof(struct dupkt_filter_key));
|
||||||
|
|
||||||
const struct ip *iphdr = (const struct ip *)ipv4_layer->hdr_ptr;
|
const struct ip *iphdr = (const struct ip *)ipv4_layer->hdr_ptr;
|
||||||
key->ip_id = ipv4_hdr_get_ipid(iphdr);
|
key->ip_id = ipv4_hdr_get_id(iphdr);
|
||||||
key->src_addr = ipv4_hdr_get_net_order_saddr(iphdr);
|
key->src_addr = ipv4_hdr_get_net_order_saddr(iphdr);
|
||||||
key->dst_addr = ipv4_hdr_get_net_order_daddr(iphdr);
|
key->dst_addr = ipv4_hdr_get_net_order_daddr(iphdr);
|
||||||
|
|
||||||
const struct tcphdr *tcphdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
|
const struct tcphdr *tcphdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
|
||||||
key->seq = tcp_hdr_get_seq(tcphdr);
|
key->seq = tcp_hdr_get_seq(tcphdr);
|
||||||
key->ack = tcp_hdr_get_ack(tcphdr);
|
key->ack = tcp_hdr_get_ack(tcphdr);
|
||||||
key->src_port = tcp_hdr_get_net_order_sport(tcphdr);
|
key->src_port = tcp_hdr_get_src_port(tcphdr);
|
||||||
key->dst_port = tcp_hdr_get_net_order_dport(tcphdr);
|
key->dst_port = tcp_hdr_get_dst_port(tcphdr);
|
||||||
key->l4_checksum = tcp_hdr_get_checksum(tcphdr);
|
key->l4_checksum = tcp_hdr_get_checksum(tcphdr);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include "uthash.h"
|
#include "uthash.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "udp_utils.h"
|
#include "udp_utils.h"
|
||||||
|
#include "tcp_utils.h"
|
||||||
|
|
||||||
#define likely(expr) __builtin_expect((expr), 1)
|
#define likely(expr) __builtin_expect((expr), 1)
|
||||||
#define unlikely(expr) __builtin_expect((expr), 0)
|
#define unlikely(expr) __builtin_expect((expr), 0)
|
||||||
@@ -977,7 +978,7 @@ static inline const char *parse_tcp(struct packet *handler, const char *data, ui
|
|||||||
{
|
{
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
uint16_t hdr_len = ((struct tcphdr *)data)->th_off << 2;
|
uint16_t hdr_len = tcp_hdr_get_hdr_len((struct tcphdr *)data);
|
||||||
SET_LAYER(handler, layer, LAYER_TYPE_TCP, hdr_len, data, len);
|
SET_LAYER(handler, layer, LAYER_TYPE_TCP, hdr_len, data, len);
|
||||||
|
|
||||||
return layer->pld_ptr;
|
return layer->pld_ptr;
|
||||||
|
|||||||
@@ -1,146 +0,0 @@
|
|||||||
#ifndef _TCP_HELPERS_H
|
|
||||||
#define _TCP_HELPERS_H
|
|
||||||
|
|
||||||
#ifdef __cpluscplus
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#define __FAVOR_BSD 1
|
|
||||||
#include <netinet/tcp.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* TCP Header Format
|
|
||||||
*
|
|
||||||
* 0 1 2 3
|
|
||||||
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
||||||
* | Source Port | Destination Port |
|
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
||||||
* | Sequence Number |
|
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
||||||
* | Acknowledgment Number |
|
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
||||||
* | Data | |U|A|P|R|S|F| |
|
|
||||||
* | Offset| Reserved |R|C|S|S|Y|I| Window |
|
|
||||||
* | | |G|K|H|T|N|N| |
|
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
||||||
* | Checksum | Urgent Pointer |
|
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
||||||
* | Options | Padding |
|
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
||||||
* | data |
|
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
||||||
*/
|
|
||||||
|
|
||||||
static inline uint16_t tcp_hdr_get_host_order_sport(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return ntohs(hdr->th_sport);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint16_t tcp_hdr_get_host_order_dport(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return ntohs(hdr->th_dport);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint16_t tcp_hdr_get_net_order_sport(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_sport;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint16_t tcp_hdr_get_net_order_dport(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_dport;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint32_t tcp_hdr_get_seq(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return ntohl(hdr->th_seq);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint32_t tcp_hdr_get_ack(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return ntohl(hdr->th_ack);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint8_t tcp_hdr_get_doff(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_off << 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint8_t tcp_hdr_get_flags(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool tcp_hdr_has_flag_urg(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_flags & TH_URG;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool tcp_hdr_has_flag_ack(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_flags & TH_ACK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool tcp_hdr_has_flag_psh(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_flags & TH_PUSH;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool tcp_hdr_has_flag_rst(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_flags & TH_RST;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool tcp_hdr_has_flag_syn(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_flags & TH_SYN;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool tcp_hdr_has_flag_fin(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return hdr->th_flags & TH_FIN;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint16_t tcp_hdr_get_window(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return ntohs(hdr->th_win);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint16_t tcp_hdr_get_checksum(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return ntohs(hdr->th_sum);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint16_t tcp_hdr_get_urg_ptr(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return ntohs(hdr->th_urp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint16_t tcp_hdr_get_opt_len(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return tcp_hdr_get_doff(hdr) - sizeof(struct tcphdr);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline const uint8_t *tcp_hdr_get_opt_ptr(const struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
return ((const uint8_t *)hdr) + sizeof(struct tcphdr);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void tcp_hdr_set_flags(struct tcphdr *hdr, uint8_t flags)
|
|
||||||
{
|
|
||||||
hdr->th_flags = flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void tcp_hdr_set_flag_rst(struct tcphdr *hdr)
|
|
||||||
{
|
|
||||||
hdr->th_flags |= TH_RST;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __cpluscplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
263
src/packet/tcp_utilis.h
Normal file
263
src/packet/tcp_utilis.h
Normal file
@@ -0,0 +1,263 @@
|
|||||||
|
#ifndef _TCP_UTILS_H
|
||||||
|
#define _TCP_UTILS_H
|
||||||
|
|
||||||
|
#ifdef __cpluscplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#define __FAVOR_BSD 1
|
||||||
|
#include <netinet/tcp.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TCP Header Format
|
||||||
|
*
|
||||||
|
* 0 1 2 3
|
||||||
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||||
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||||
|
* | Source Port | Destination Port |
|
||||||
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||||
|
* | Sequence Number |
|
||||||
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||||
|
* | Acknowledgment Number |
|
||||||
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||||
|
* | Data | |U|A|P|R|S|F| |
|
||||||
|
* | Offset| Reserved |R|C|S|S|Y|I| Window |
|
||||||
|
* | | |G|K|H|T|N|N| |
|
||||||
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||||
|
* | Checksum | Urgent Pointer |
|
||||||
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||||
|
* | Options | Padding |
|
||||||
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||||
|
* | data |
|
||||||
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||||
|
*/
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* get
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
static inline uint16_t tcp_hdr_get_src_port(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return ntohs(hdr->th_sport);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint16_t tcp_hdr_get_dst_port(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return ntohs(hdr->th_dport);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint32_t tcp_hdr_get_seq(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return ntohl(hdr->th_seq);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint32_t tcp_hdr_get_ack(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return ntohl(hdr->th_ack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t tcp_hdr_get_hdr_len(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return hdr->th_off << 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t tcp_hdr_get_flags(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return hdr->th_flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool tcp_hdr_get_urg_flag(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return hdr->th_flags & TH_URG;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool tcp_hdr_get_ack_flag(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return hdr->th_flags & TH_ACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool tcp_hdr_get_push_flag(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return hdr->th_flags & TH_PUSH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool tcp_hdr_get_rst_flag(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return hdr->th_flags & TH_RST;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool tcp_hdr_get_syn_flag(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return hdr->th_flags & TH_SYN;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool tcp_hdr_get_fin_flag(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return hdr->th_flags & TH_FIN;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint16_t tcp_hdr_get_window(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return ntohs(hdr->th_win);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint16_t tcp_hdr_get_checksum(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return ntohs(hdr->th_sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint16_t tcp_hdr_get_urg_ptr(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return ntohs(hdr->th_urp);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint16_t tcp_hdr_get_opt_len(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return tcp_hdr_get_hdr_len(hdr) - sizeof(struct tcphdr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline const char *tcp_hdr_get_opt_data(const struct tcphdr *hdr)
|
||||||
|
{
|
||||||
|
return ((const char *)hdr) + sizeof(struct tcphdr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* set
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_src_port(struct tcphdr *hdr, uint16_t port)
|
||||||
|
{
|
||||||
|
hdr->th_sport = htons(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_dst_port(struct tcphdr *hdr, uint16_t port)
|
||||||
|
{
|
||||||
|
hdr->th_dport = htons(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_seq(struct tcphdr *hdr, uint32_t seq)
|
||||||
|
{
|
||||||
|
hdr->th_seq = htonl(seq);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_ack(struct tcphdr *hdr, uint32_t ack)
|
||||||
|
{
|
||||||
|
hdr->th_ack = htonl(ack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_hdr_len(struct tcphdr *hdr, uint8_t offset)
|
||||||
|
{
|
||||||
|
hdr->th_off = offset >> 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_flags(struct tcphdr *hdr, uint8_t flags)
|
||||||
|
{
|
||||||
|
hdr->th_flags = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_urg_flag(struct tcphdr *hdr, bool flag)
|
||||||
|
{
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
hdr->th_flags |= TH_URG;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hdr->th_flags &= ~TH_URG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_ack_flag(struct tcphdr *hdr, bool flag)
|
||||||
|
{
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
hdr->th_flags |= TH_ACK;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hdr->th_flags &= ~TH_ACK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_push_flag(struct tcphdr *hdr, bool flag)
|
||||||
|
{
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
hdr->th_flags |= TH_PUSH;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hdr->th_flags &= ~TH_PUSH;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_rst_flag(struct tcphdr *hdr, bool flag)
|
||||||
|
{
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
hdr->th_flags |= TH_RST;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hdr->th_flags &= ~TH_RST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_syn_flag(struct tcphdr *hdr, bool flag)
|
||||||
|
{
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
hdr->th_flags |= TH_SYN;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hdr->th_flags &= ~TH_SYN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_fin_flag(struct tcphdr *hdr, bool flag)
|
||||||
|
{
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
hdr->th_flags |= TH_FIN;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hdr->th_flags &= ~TH_FIN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_window(struct tcphdr *hdr, uint16_t window)
|
||||||
|
{
|
||||||
|
hdr->th_win = htons(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_checksum(struct tcphdr *hdr, uint16_t checksum)
|
||||||
|
{
|
||||||
|
hdr->th_sum = htons(checksum);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_urg_ptr(struct tcphdr *hdr, uint16_t ptr)
|
||||||
|
{
|
||||||
|
hdr->th_urp = htons(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void tcp_hdr_set_opt_len(struct tcphdr *hdr, uint16_t len)
|
||||||
|
{
|
||||||
|
hdr->th_off = (sizeof(struct tcphdr) + len) >> 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// must be called after tcp_hdr_set_opt_len
|
||||||
|
static inline void tcp_hdr_set_opt_data(struct tcphdr *hdr, const char *ptr)
|
||||||
|
{
|
||||||
|
memcpy((char *)hdr + sizeof(struct tcphdr), ptr, tcp_hdr_get_opt_len(hdr));
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cpluscplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,8 +8,8 @@ target_link_libraries(gtest_packet packet gtest)
|
|||||||
add_executable(gtest_udp_utils gtest_udp_utils.cpp)
|
add_executable(gtest_udp_utils gtest_udp_utils.cpp)
|
||||||
target_link_libraries(gtest_udp_utils packet gtest)
|
target_link_libraries(gtest_udp_utils packet gtest)
|
||||||
|
|
||||||
add_executable(gtest_tcp_helpers gtest_tcp_helpers.cpp)
|
add_executable(gtest_tcp_utils gtest_tcp_utils.cpp)
|
||||||
target_link_libraries(gtest_tcp_helpers packet gtest)
|
target_link_libraries(gtest_tcp_utils packet gtest)
|
||||||
|
|
||||||
add_executable(gtest_ipv4_helpers gtest_ipv4_helpers.cpp)
|
add_executable(gtest_ipv4_helpers gtest_ipv4_helpers.cpp)
|
||||||
target_link_libraries(gtest_ipv4_helpers packet gtest)
|
target_link_libraries(gtest_ipv4_helpers packet gtest)
|
||||||
@@ -23,7 +23,7 @@ target_link_libraries(gtest_packet_helpers packet gtest)
|
|||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
gtest_discover_tests(gtest_packet)
|
gtest_discover_tests(gtest_packet)
|
||||||
gtest_discover_tests(gtest_udp_utils)
|
gtest_discover_tests(gtest_udp_utils)
|
||||||
gtest_discover_tests(gtest_tcp_helpers)
|
gtest_discover_tests(gtest_tcp_utils)
|
||||||
gtest_discover_tests(gtest_ipv4_helpers)
|
gtest_discover_tests(gtest_ipv4_helpers)
|
||||||
gtest_discover_tests(gtest_ipv6_helpers)
|
gtest_discover_tests(gtest_ipv6_helpers)
|
||||||
gtest_discover_tests(gtest_packet_helpers)
|
gtest_discover_tests(gtest_packet_helpers)
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include "tcp_helpers.h"
|
#include "tcp_utils.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Transmission Control Protocol, Src Port: 55555, Dst Port: 40856, Seq: 0, Ack: 1, Len: 0
|
* Transmission Control Protocol, Src Port: 55555, Dst Port: 40856, Seq: 0, Ack: 1, Len: 0
|
||||||
@@ -74,26 +74,71 @@ unsigned char data[] = {
|
|||||||
0xd9, 0x03, 0x9f, 0x98, 0xec, 0x5f, 0xc6, 0x3c, 0x3b, 0x12, 0x47, 0x92, 0xa0, 0x12, 0xaa, 0xaa, 0xfe, 0x30, 0x00, 0x00, 0x02, 0x04, 0xff, 0xd7, 0x04, 0x02,
|
0xd9, 0x03, 0x9f, 0x98, 0xec, 0x5f, 0xc6, 0x3c, 0x3b, 0x12, 0x47, 0x92, 0xa0, 0x12, 0xaa, 0xaa, 0xfe, 0x30, 0x00, 0x00, 0x02, 0x04, 0xff, 0xd7, 0x04, 0x02,
|
||||||
0x08, 0x0a, 0xa4, 0xef, 0xa3, 0xcc, 0xa4, 0xef, 0xa3, 0xcc, 0x01, 0x03, 0x03, 0x07};
|
0x08, 0x0a, 0xa4, 0xef, 0xa3, 0xcc, 0xa4, 0xef, 0xa3, 0xcc, 0x01, 0x03, 0x03, 0x07};
|
||||||
|
|
||||||
TEST(TCP_HELPERS, TEST)
|
TEST(TCP_UTILS, GET)
|
||||||
{
|
{
|
||||||
const struct tcphdr *hdr = (struct tcphdr *)data;
|
const struct tcphdr *hdr = (struct tcphdr *)data;
|
||||||
EXPECT_TRUE(tcp_hdr_get_host_order_sport(hdr) == 55555);
|
EXPECT_TRUE(tcp_hdr_get_src_port(hdr) == 55555);
|
||||||
EXPECT_TRUE(tcp_hdr_get_host_order_dport(hdr) == 40856);
|
EXPECT_TRUE(tcp_hdr_get_dst_port(hdr) == 40856);
|
||||||
EXPECT_TRUE(tcp_hdr_get_seq(hdr) == 3965699644);
|
EXPECT_TRUE(tcp_hdr_get_seq(hdr) == 3965699644);
|
||||||
EXPECT_TRUE(tcp_hdr_get_ack(hdr) == 991053714);
|
EXPECT_TRUE(tcp_hdr_get_ack(hdr) == 991053714);
|
||||||
EXPECT_TRUE(tcp_hdr_get_doff(hdr) == 40);
|
EXPECT_TRUE(tcp_hdr_get_hdr_len(hdr) == 40);
|
||||||
EXPECT_TRUE(tcp_hdr_get_flags(hdr) == 0x012);
|
EXPECT_TRUE(tcp_hdr_get_flags(hdr) == 0x012);
|
||||||
EXPECT_TRUE(tcp_hdr_has_flag_urg(hdr) == false);
|
EXPECT_TRUE(tcp_hdr_get_urg_flag(hdr) == false);
|
||||||
EXPECT_TRUE(tcp_hdr_has_flag_ack(hdr) == true);
|
EXPECT_TRUE(tcp_hdr_get_ack_flag(hdr) == true);
|
||||||
EXPECT_TRUE(tcp_hdr_has_flag_psh(hdr) == false);
|
EXPECT_TRUE(tcp_hdr_get_push_flag(hdr) == false);
|
||||||
EXPECT_TRUE(tcp_hdr_has_flag_rst(hdr) == false);
|
EXPECT_TRUE(tcp_hdr_get_rst_flag(hdr) == false);
|
||||||
EXPECT_TRUE(tcp_hdr_has_flag_syn(hdr) == true);
|
EXPECT_TRUE(tcp_hdr_get_syn_flag(hdr) == true);
|
||||||
EXPECT_TRUE(tcp_hdr_has_flag_fin(hdr) == false);
|
EXPECT_TRUE(tcp_hdr_get_fin_flag(hdr) == false);
|
||||||
EXPECT_TRUE(tcp_hdr_get_window(hdr) == 43690);
|
EXPECT_TRUE(tcp_hdr_get_window(hdr) == 43690);
|
||||||
EXPECT_TRUE(tcp_hdr_get_checksum(hdr) == 0xfe30);
|
EXPECT_TRUE(tcp_hdr_get_checksum(hdr) == 0xfe30);
|
||||||
EXPECT_TRUE(tcp_hdr_get_urg_ptr(hdr) == 0);
|
EXPECT_TRUE(tcp_hdr_get_urg_ptr(hdr) == 0);
|
||||||
EXPECT_TRUE(tcp_hdr_get_opt_len(hdr) == 20);
|
EXPECT_TRUE(tcp_hdr_get_opt_len(hdr) == 20);
|
||||||
EXPECT_TRUE(tcp_hdr_get_opt_ptr(hdr) == data + 20);
|
EXPECT_TRUE(tcp_hdr_get_opt_data(hdr) == data + 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TCP_UTILS, SET1)
|
||||||
|
{
|
||||||
|
char buff[40];
|
||||||
|
|
||||||
|
struct tcphdr *hdr = (struct tcphdr *)buff;
|
||||||
|
tcp_hdr_set_src_port(hdr, 55555);
|
||||||
|
tcp_hdr_set_dst_port(hdr, 40856);
|
||||||
|
tcp_hdr_set_seq(hdr, 3965699644);
|
||||||
|
tcp_hdr_set_ack(hdr, 991053714);
|
||||||
|
tcp_hdr_set_hdr_len(hdr, 40);
|
||||||
|
tcp_hdr_set_flags(hdr, 0x012);
|
||||||
|
tcp_hdr_set_window(hdr, 43690);
|
||||||
|
tcp_hdr_set_checksum(hdr, 0xfe30);
|
||||||
|
tcp_hdr_set_urg_ptr(hdr, 0);
|
||||||
|
tcp_hdr_set_opt_len(hdr, 20);
|
||||||
|
tcp_hdr_set_opt_data(hdr, data + 20);
|
||||||
|
|
||||||
|
EXPECT_TRUE(memcmp(buff, data, 40) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TCP_UTILS, SET2)
|
||||||
|
{
|
||||||
|
char buff[40] = {0};
|
||||||
|
|
||||||
|
struct tcphdr *hdr = (struct tcphdr *)buff;
|
||||||
|
tcp_hdr_set_src_port(hdr, 55555);
|
||||||
|
tcp_hdr_set_dst_port(hdr, 40856);
|
||||||
|
tcp_hdr_set_seq(hdr, 3965699644);
|
||||||
|
tcp_hdr_set_ack(hdr, 991053714);
|
||||||
|
tcp_hdr_set_hdr_len(hdr, 40);
|
||||||
|
tcp_hdr_set_urg_flag(hdr, false);
|
||||||
|
tcp_hdr_set_ack_flag(hdr, true);
|
||||||
|
tcp_hdr_set_push_flag(hdr, false);
|
||||||
|
tcp_hdr_set_rst_flag(hdr, false);
|
||||||
|
tcp_hdr_set_syn_flag(hdr, true);
|
||||||
|
tcp_hdr_set_fin_flag(hdr, false);
|
||||||
|
tcp_hdr_set_window(hdr, 43690);
|
||||||
|
tcp_hdr_set_checksum(hdr, 0xfe30);
|
||||||
|
tcp_hdr_set_urg_ptr(hdr, 0);
|
||||||
|
tcp_hdr_set_opt_len(hdr, 20);
|
||||||
|
tcp_hdr_set_opt_data(hdr, data + 20);
|
||||||
|
|
||||||
|
EXPECT_TRUE(memcmp(buff, data, 40) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
@@ -8,8 +8,8 @@
|
|||||||
#include "session_timer.h"
|
#include "session_timer.h"
|
||||||
#include "session_queue.h"
|
#include "session_queue.h"
|
||||||
#include "session_manager.h"
|
#include "session_manager.h"
|
||||||
#include "tcp_helpers.h"
|
#include "tcp_utils.h"
|
||||||
#include "udp_helpers.h"
|
#include "udp_utils.h"
|
||||||
#include "packet_helpers.h"
|
#include "packet_helpers.h"
|
||||||
#include "dupkt_filter.h"
|
#include "dupkt_filter.h"
|
||||||
#include "eviction_filter.h"
|
#include "eviction_filter.h"
|
||||||
@@ -390,13 +390,13 @@ static inline void session_update_tcp_state(struct session *sess, const struct l
|
|||||||
{
|
{
|
||||||
const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
|
const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
|
||||||
uint64_t state = session_get_tcp_state(sess);
|
uint64_t state = session_get_tcp_state(sess);
|
||||||
if (tcp_hdr_has_flag_syn(hdr))
|
if (tcp_hdr_get_syn_flag(hdr))
|
||||||
{
|
{
|
||||||
state |= (tcp_hdr_has_flag_ack(hdr) ? TCP_SYNACK_RECVED : TCP_SYN_RECVED);
|
state |= (tcp_hdr_get_ack_flag(hdr) ? TCP_SYNACK_RECVED : TCP_SYN_RECVED);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (tcp_hdr_has_flag_ack(hdr))
|
if (tcp_hdr_get_ack_flag(hdr))
|
||||||
{
|
{
|
||||||
if (curr_dir == SESSION_DIR_C2S)
|
if (curr_dir == SESSION_DIR_C2S)
|
||||||
{
|
{
|
||||||
@@ -408,7 +408,7 @@ static inline void session_update_tcp_state(struct session *sess, const struct l
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (tcp_hdr_has_flag_fin(hdr))
|
if (tcp_hdr_get_fin_flag(hdr))
|
||||||
{
|
{
|
||||||
if (curr_dir == SESSION_DIR_C2S)
|
if (curr_dir == SESSION_DIR_C2S)
|
||||||
{
|
{
|
||||||
@@ -419,7 +419,7 @@ static inline void session_update_tcp_state(struct session *sess, const struct l
|
|||||||
state |= TCP_S2C_FIN_RECVED;
|
state |= TCP_S2C_FIN_RECVED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (tcp_hdr_has_flag_rst(hdr))
|
if (tcp_hdr_get_rst_flag(hdr))
|
||||||
{
|
{
|
||||||
if (curr_dir == SESSION_DIR_C2S)
|
if (curr_dir == SESSION_DIR_C2S)
|
||||||
{
|
{
|
||||||
@@ -608,7 +608,7 @@ static inline void session_manager_update_session_base(struct session_manager *m
|
|||||||
|
|
||||||
static inline void session_manager_update_session_packet(struct session_manager *mgr, struct session *sess, const struct packet *pkt, enum session_dir curr_dir)
|
static inline void session_manager_update_session_packet(struct session_manager *mgr, struct session *sess, const struct packet *pkt, enum session_dir curr_dir)
|
||||||
{
|
{
|
||||||
uint64_t len = packet_get_raw_len(pkt);
|
uint64_t len = packet_get_len(pkt);
|
||||||
if (curr_dir == SESSION_DIR_C2S)
|
if (curr_dir == SESSION_DIR_C2S)
|
||||||
{
|
{
|
||||||
session_inc_c2s_metrics(sess, 1, len);
|
session_inc_c2s_metrics(sess, 1, len);
|
||||||
@@ -809,7 +809,7 @@ static inline struct session *session_manager_new_tcp_session(struct session_man
|
|||||||
}
|
}
|
||||||
|
|
||||||
const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
|
const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
|
||||||
if (!tcp_hdr_has_flag_syn(hdr))
|
if (!tcp_hdr_get_syn_flag(hdr))
|
||||||
{
|
{
|
||||||
mgr->npkts_hit_tcp_miss_sess++;
|
mgr->npkts_hit_tcp_miss_sess++;
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -841,7 +841,7 @@ static inline struct session *session_manager_new_tcp_session(struct session_man
|
|||||||
struct session *sess = session_pool_alloc(mgr->sess_pool);
|
struct session *sess = session_pool_alloc(mgr->sess_pool);
|
||||||
assert(sess);
|
assert(sess);
|
||||||
|
|
||||||
enum session_dir curr_dir = tcp_hdr_has_flag_ack(hdr) ? SESSION_DIR_S2C : SESSION_DIR_C2S;
|
enum session_dir curr_dir = tcp_hdr_get_ack_flag(hdr) ? SESSION_DIR_S2C : SESSION_DIR_C2S;
|
||||||
session_manager_update_session_base(mgr, sess, key, curr_dir);
|
session_manager_update_session_base(mgr, sess, key, curr_dir);
|
||||||
session_manager_update_session_packet(mgr, sess, pkt, curr_dir);
|
session_manager_update_session_packet(mgr, sess, pkt, curr_dir);
|
||||||
session_update_tcp_state(sess, tcp_layer, curr_dir);
|
session_update_tcp_state(sess, tcp_layer, curr_dir);
|
||||||
|
|||||||
Reference in New Issue
Block a user