refactor: rename the directory infra/packet_parser to infra/paket_manager

This commit is contained in:
luwenpeng
2024-09-13 16:08:07 +08:00
parent 06c498409f
commit 173a6ced61
42 changed files with 42 additions and 42 deletions

View File

@@ -0,0 +1,15 @@
add_library(packet_manager
packet_utils.c
packet_parser.c
packet_builder.c
packet_filter.c
packet_dump.c
checksum.c)
target_include_directories(packet_manager PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(packet_manager PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash)
target_include_directories(packet_manager PUBLIC ${CMAKE_SOURCE_DIR}/deps/logger)
target_include_directories(packet_manager PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_include_directories(packet_manager PUBLIC ${CMAKE_SOURCE_DIR}/infra)
target_link_libraries(packet_manager tuple logger dablooms)
add_subdirectory(test)

View File

@@ -0,0 +1,104 @@
#include "checksum.h"
uint16_t checksum(const void *data, int len)
{
uint16_t *ptr = (uint16_t *)data;
uint32_t sum = 0;
while (len > 1)
{
sum += *ptr++;
len -= 2;
}
if (len == 1)
{
sum += *(uint8_t *)ptr;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
return (uint16_t)~sum;
}
uint16_t checksum_v4(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in_addr *src_addr, struct in_addr *dst_addr)
{
uint16_t *ip_src = (uint16_t *)src_addr;
uint16_t *ip_dst = (uint16_t *)dst_addr;
const uint16_t *buffer = (u_int16_t *)l4_hdr_ptr;
uint32_t sum = 0;
size_t len = l4_total_len;
while (len > 1)
{
sum += *buffer++;
if (sum & 0x80000000)
{
sum = (sum & 0xFFFF) + (sum >> 16);
}
len -= 2;
}
if (len & 1)
{
sum += *((uint8_t *)buffer);
}
sum += *(ip_src++);
sum += *ip_src;
sum += *(ip_dst++);
sum += *ip_dst;
sum += htons(l4_proto);
sum += htons(l4_total_len);
while (sum >> 16)
{
sum = (sum & 0xFFFF) + (sum >> 16);
}
return ((uint16_t)(~sum));
}
uint16_t checksum_v6(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in6_addr *src_addr, struct in6_addr *dst_addr)
{
uint16_t *ip_src = (uint16_t *)src_addr;
uint16_t *ip_dst = (uint16_t *)dst_addr;
const uint16_t *buffer = (u_int16_t *)l4_hdr_ptr;
uint32_t sum = 0;
size_t len = l4_total_len;
while (len > 1)
{
sum += *buffer++;
if (sum & 0x80000000)
{
sum = (sum & 0xFFFF) + (sum >> 16);
}
len -= 2;
}
if (len & 1)
{
sum += *((uint8_t *)buffer);
}
for (int i = 0; i < 8; i++)
{
sum += *ip_src;
ip_src++;
}
for (int i = 0; i < 8; i++)
{
sum += *ip_dst;
ip_dst++;
}
sum += htons(l4_proto);
sum += htons(l4_total_len);
while (sum >> 16)
{
sum = (sum & 0xFFFF) + (sum >> 16);
}
return ((uint16_t)(~sum));
}

View File

@@ -0,0 +1,17 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#include <arpa/inet.h>
uint16_t checksum(const void *data, int len);
uint16_t checksum_v4(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in_addr *src_addr, struct in_addr *dst_addr);
uint16_t checksum_v6(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in6_addr *src_addr, struct in6_addr *dst_addr);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,414 @@
#include <time.h>
#include "checksum.h"
#include "log_private.h"
#include "packet_helper.h"
#include "packet_parser.h"
#include "packet_private.h"
#define PACKET_CRAFT_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "packet craft", format, ##__VA_ARGS__)
#define PACKET_CRAFT_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet craft", format, ##__VA_ARGS__)
struct fingerprint
{
// TODO
uint16_t ip_id;
uint8_t ip_ttl;
uint16_t tcp_win;
};
static uint8_t append_fingerprint = 0;
/******************************************************************************
* Private API
******************************************************************************/
static inline void calc_packet_fingerprint(struct fingerprint *finger)
{
if (append_fingerprint)
{
#define RANGE(rand, start, end) (start + rand % (end - start + 1)) // [start, end]
struct timespec time;
clock_gettime(CLOCK_MONOTONIC_COARSE, &time);
uint64_t random = 0x013579ABCDEF ^ time.tv_nsec;
finger->ip_id = (uint16_t)(RANGE(random, 32767, 65535));
finger->ip_ttl = (uint8_t)(RANGE(random, 48, 120));
finger->tcp_win = (uint16_t)(RANGE(random, 1000, 1460));
}
else
{
finger->ip_id = 0;
finger->ip_ttl = 0;
finger->tcp_win = 0;
}
}
static void update_udp_hdr(struct udphdr *udp, int trim_len)
{
uint16_t total = udp_hdr_get_total_len(udp);
udp_hdr_set_total_len(udp, total - trim_len);
udp_hdr_set_checksum(udp, 0);
}
static void update_ip4_hdr(struct ip *ip, uint16_t ipid, uint8_t ttl, int trim_len)
{
int hdr_len = ip4_hdr_get_hdr_len(ip);
uint16_t total = ip4_hdr_get_total_len(ip);
ip4_hdr_set_total_len(ip, total - trim_len);
if (ipid)
{
ip4_hdr_set_ipid(ip, ipid);
}
if (ttl)
{
ip4_hdr_set_ttl(ip, ttl);
}
ip->ip_sum = 0;
ip->ip_sum = checksum((const void *)ip, hdr_len);
}
static void update_ip6_hdr(struct ip6_hdr *ip6, int trim_len)
{
uint16_t len = ip6_hdr_get_payload_len(ip6);
ip6_hdr_set_payload_len(ip6, len - trim_len);
}
static void update_gtp1_hdr(struct gtp1_hdr *gtp, int trim_len)
{
uint16_t msg_len = gtp1_hdr_get_msg_len(gtp);
gtp1_hdr_set_msg_len(gtp, msg_len - trim_len);
if (gtp1_hdr_get_seq_flag(gtp) && gtp1_hdr_get_seq(gtp))
{
PACKET_CRAFT_LOG_ERROR("build packets may be dropped by intermediate devices, the GTPv1 layer requires a sequence number");
}
}
static void update_gtp2_hdr(struct gtp2_hdr *gtp, int trim_len)
{
uint16_t msg_len = gtp2_hdr_get_msg_len(gtp);
gtp2_hdr_set_msg_len(gtp, msg_len - trim_len);
if (gtp2_hdr_get_seq(gtp))
{
PACKET_CRAFT_LOG_ERROR("build packets may be dropped by intermediate devices, the GTPv2 layer requires a sequence number");
}
}
static void update_gre1_hdr(struct gre1_hdr *gre, int trim_len)
{
uint16_t payload_len = gre1_hdr_get_payload_length(gre);
gre1_hdr_set_payload_length(gre, payload_len - trim_len);
}
// L2 -- data link layer
// LAYER_PROTO_ETHER: // SKIP
// LAYER_PROTO_PWETH: // SKIP
// LAYER_PROTO_PPP: // SKIP
// LAYER_PROTO_L2TP: // TODO ???
// L2 -- tunnel
// LAYER_PROTO_VLAN: // SKIP
// LAYER_PROTO_PPPOE: // TODO ????
// LAYER_PROTO_MPLS: // SKIP
// L3 -- network layer
// LAYER_PROTO_IPV4: // DONE
// LAYER_PROTO_IPV6: // DONE
// LAYER_PROTO_IPAH: // TODO ????
// L3 -- tunnel
// LAYER_PROTO_GRE: // DONE
// L4 -- transport layer
// LAYER_PROTO_UDP: // DONE
// LAYER_PROTO_TCP: // DONE
// LAYER_PROTO_ICMP:
// LAYER_PROTO_ICMP6:
// L4 -- tunnel
// LAYER_PROTO_VXLAN: // SKIP
// LAYER_PROTO_GTP_U: // DONE
// LAYER_PROTO_GTP_C:
static void calculate_length_and_checksum(const struct packet *origin_pkt, int layer_count, char *new_pkt_data, uint16_t new_pkt_len, int trim_len)
{
uint8_t version = 0;
uint16_t sum = 0;
char *curr_hdr_ptr = NULL;
char *last_hdr_ptr = NULL;
struct tcphdr *tcp = NULL;
struct udphdr *udp = NULL;
struct ip *ip4 = NULL;
struct ip6_hdr *ip6 = NULL;
struct gtp1_hdr *gtp1 = NULL;
struct gtp2_hdr *gtp2 = NULL;
struct gre0_hdr *gre0 = NULL;
struct gre1_hdr *gre1 = NULL;
struct layer_private *curr_layer = NULL;
struct layer_private *last_layer = NULL;
struct fingerprint finger = {};
calc_packet_fingerprint(&finger);
for (int i = layer_count - 1; i >= 0; i--)
{
curr_layer = (struct layer_private *)packet_get_layer(origin_pkt, i);
last_layer = (struct layer_private *)packet_get_layer(origin_pkt, i + 1);
curr_hdr_ptr = new_pkt_data + curr_layer->hdr_offset;
last_hdr_ptr = last_layer ? new_pkt_data + last_layer->hdr_offset : NULL;
switch (curr_layer->proto)
{
case LAYER_PROTO_TCP:
tcp = (struct tcphdr *)curr_hdr_ptr;
if (finger.tcp_win)
{
tcp_hdr_set_window(tcp, finger.tcp_win);
}
tcp_hdr_set_checksum(tcp, 0);
break;
case LAYER_PROTO_UDP:
udp = (struct udphdr *)curr_hdr_ptr;
update_udp_hdr(udp, trim_len);
break;
case LAYER_PROTO_IPV4:
ip4 = (struct ip *)curr_hdr_ptr;
if (last_layer && last_layer->proto == LAYER_PROTO_TCP)
{
tcp = (struct tcphdr *)last_hdr_ptr;
tcp->th_sum = checksum_v4(tcp, new_pkt_len - last_layer->hdr_offset, IPPROTO_TCP, &ip4->ip_src, &ip4->ip_dst);
}
if (last_layer && last_layer->proto == LAYER_PROTO_UDP)
{
udp = (struct udphdr *)last_hdr_ptr;
udp->uh_sum = checksum_v4(udp, new_pkt_len - last_layer->hdr_offset, IPPROTO_UDP, &ip4->ip_src, &ip4->ip_dst);
}
update_ip4_hdr(ip4, finger.ip_id, finger.ip_ttl, trim_len);
break;
case LAYER_PROTO_IPV6:
ip6 = (struct ip6_hdr *)curr_hdr_ptr;
if (last_layer && last_layer->proto == LAYER_PROTO_TCP)
{
tcp = (struct tcphdr *)last_hdr_ptr;
tcp->th_sum = checksum_v6(tcp, new_pkt_len - last_layer->hdr_offset, IPPROTO_TCP, &ip6->ip6_src, &ip6->ip6_dst);
}
if (last_layer && last_layer->proto == LAYER_PROTO_UDP)
{
udp = (struct udphdr *)last_hdr_ptr;
udp->uh_sum = checksum_v6(udp, new_pkt_len - last_layer->hdr_offset, IPPROTO_UDP, &ip6->ip6_src, &ip6->ip6_dst);
}
update_ip6_hdr(ip6, trim_len);
break;
case LAYER_PROTO_GTP_C: /* fall through */
case LAYER_PROTO_GTP_U:
version = peek_gtp_version(curr_hdr_ptr, curr_layer->hdr_len);
if (version == 1)
{
gtp1 = (struct gtp1_hdr *)curr_hdr_ptr;
update_gtp1_hdr(gtp1, trim_len);
}
if (version == 2)
{
gtp2 = (struct gtp2_hdr *)curr_hdr_ptr;
update_gtp2_hdr(gtp2, trim_len);
}
break;
case LAYER_PROTO_GRE:
version = peek_gre_version(curr_hdr_ptr, curr_layer->hdr_len);
if (version == 0)
{
gre0 = (struct gre0_hdr *)curr_hdr_ptr;
if (gre0_hdr_get_checksum_flag(gre0))
{
gre0_hdr_set_checksum(gre0, ntohs(0));
sum = checksum((const void *)curr_hdr_ptr, new_pkt_len - curr_layer->hdr_offset);
gre0_hdr_set_checksum(gre0, ntohs(sum));
}
}
if (version == 1)
{
gre1 = (struct gre1_hdr *)curr_hdr_ptr;
update_gre1_hdr(gre1, trim_len);
}
break;
default:
break;
}
}
}
/******************************************************************************
* Public API
******************************************************************************/
void append_fingerprint_to_build_packet()
{
append_fingerprint = 1;
}
/*
* tcp_seq: the sequence number of the new TCP packet (in host byte order)
* tcp_ack: the acknowledgment number of the new TCP packet (in host byte order)
* tcp_options_len: the length of the options (must be a multiple of 4)
*/
struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags,
const char *tcp_options, uint16_t tcp_options_len,
const char *tcp_payload, uint16_t tcp_payload_len)
{
// check arguments
if (origin_pkt == NULL ||
(tcp_options == NULL && tcp_options_len != 0) || (tcp_options != NULL && tcp_options_len == 0) ||
(tcp_payload == NULL && tcp_payload_len != 0) || (tcp_payload != NULL && tcp_payload_len == 0) ||
(tcp_options_len && tcp_options_len % 4 != 0))
{
PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, invalid arguments");
return NULL;
}
// check the innermost layer of the original packet
int layer_count = packet_get_layer_count(origin_pkt);
const struct layer_private *tcp_layer = packet_get_layer(origin_pkt, layer_count - 1);
if (tcp_layer == NULL || tcp_layer->proto != LAYER_PROTO_TCP)
{
PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, the innermost layer of the original packet is not TCP");
return NULL;
}
// calculate the new packet length
int trim_len = tcp_layer->hdr_len + tcp_layer->pld_len - tcp_options_len - tcp_payload_len - sizeof(struct tcphdr);
uint16_t new_pkt_len = origin_pkt->data_len - origin_pkt->trim_len - trim_len;
struct packet *new_pkt = packet_new(new_pkt_len);
if (new_pkt == NULL)
{
PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, no space to allocate new packet");
return NULL;
}
// copy the data to the new packet
char *new_pkt_data = (char *)packet_get_raw_data(new_pkt);
memcpy(new_pkt_data, packet_get_raw_data(origin_pkt), tcp_layer->hdr_offset + sizeof(struct tcphdr));
if (tcp_options_len)
{
memcpy(new_pkt_data + tcp_layer->hdr_offset + sizeof(struct tcphdr), tcp_options, tcp_options_len);
}
memcpy(new_pkt_data + tcp_layer->hdr_offset + sizeof(struct tcphdr) + tcp_options_len, tcp_payload, tcp_payload_len);
struct tcphdr *hdr = (struct tcphdr *)(new_pkt_data + tcp_layer->hdr_offset);
tcp_hdr_set_seq(hdr, tcp_seq);
tcp_hdr_set_ack(hdr, tcp_ack);
tcp_hdr_set_flags(hdr, tcp_flags);
tcp_hdr_set_hdr_len(hdr, sizeof(struct tcphdr) + tcp_options_len);
calculate_length_and_checksum(origin_pkt, layer_count, new_pkt_data, new_pkt_len, trim_len);
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
new_pkt->meta.origin_ctx = NULL;
return new_pkt;
}
struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len)
{
// check arguments
if (origin_pkt == NULL || (udp_payload == NULL && udp_payload_len != 0) || (udp_payload != NULL && udp_payload_len == 0))
{
PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, invalid arguments");
return NULL;
}
// check the innermost layer of the original packet
int layer_count = packet_get_layer_count(origin_pkt);
const struct layer_private *udp_layer = packet_get_layer(origin_pkt, layer_count - 1);
if (udp_layer == NULL || udp_layer->proto != LAYER_PROTO_UDP)
{
PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, the innermost layer of the original packet is not UDP");
return NULL;
}
// calculate the new packet length
int trim_len = udp_layer->hdr_len + udp_layer->pld_len - udp_payload_len - sizeof(struct udphdr);
uint16_t new_pkt_len = origin_pkt->data_len - origin_pkt->trim_len - trim_len;
struct packet *new_pkt = packet_new(new_pkt_len);
if (new_pkt == NULL)
{
PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, no space to allocate new packet");
return NULL;
}
// copy the data to the new packet
char *new_pkt_data = (char *)packet_get_raw_data(new_pkt);
memcpy(new_pkt_data, packet_get_raw_data(origin_pkt), udp_layer->hdr_offset + sizeof(struct udphdr));
memcpy(new_pkt_data + udp_layer->hdr_offset + sizeof(struct udphdr), udp_payload, udp_payload_len);
calculate_length_and_checksum(origin_pkt, layer_count, new_pkt_data, new_pkt_len, trim_len);
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
new_pkt->meta.origin_ctx = NULL;
return new_pkt;
}
struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len)
{
if (origin_pkt == NULL || (l3_payload == NULL && l3_payload_len != 0) || (l3_payload != NULL && l3_payload_len == 0))
{
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, invalid arguments");
return NULL;
}
int i = 0;
int layers = packet_get_layer_count(origin_pkt);
const struct layer_private *l3_layer = NULL;
for (i = layers - 1; i >= 0; i--)
{
l3_layer = packet_get_layer(origin_pkt, i);
if (l3_layer->proto == LAYER_PROTO_IPV4 || l3_layer->proto == LAYER_PROTO_IPV6)
{
break;
}
else
{
l3_layer = NULL;
}
}
if (l3_layer == NULL)
{
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, the original packet does not contain an IP layer");
return NULL;
}
// calculate the new packet length
// trim IPv4 options
// trim IPv6 extension headers
int l3_hdr_len = l3_layer->proto == LAYER_PROTO_IPV4 ? sizeof(struct ip) : sizeof(struct ip6_hdr);
int trim_len = l3_layer->hdr_len + l3_layer->pld_len - l3_payload_len - l3_hdr_len;
uint16_t new_pkt_len = origin_pkt->data_len - origin_pkt->trim_len - trim_len;
struct packet *new_pkt = packet_new(new_pkt_len);
if (new_pkt == NULL)
{
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, no space to allocate new packet");
return NULL;
}
// copy the data to the new packet
char *new_pkt_data = (char *)packet_get_raw_data(new_pkt);
memcpy(new_pkt_data, packet_get_raw_data(origin_pkt), l3_layer->hdr_offset + l3_hdr_len);
if (l3_payload)
{
memcpy(new_pkt_data + l3_layer->hdr_offset + l3_hdr_len, l3_payload, l3_payload_len);
}
// update ip_proto
if (l3_layer->proto == LAYER_PROTO_IPV4)
{
struct ip *ip4 = (struct ip *)(new_pkt_data + l3_layer->hdr_offset);
ip4_hdr_set_protocol(ip4, ip_proto);
}
else
{
struct ip6_hdr *ip6 = (struct ip6_hdr *)(new_pkt_data + l3_layer->hdr_offset);
ip6_hdr_set_next_header(ip6, ip_proto);
}
calculate_length_and_checksum(origin_pkt, i + 1, new_pkt_data, new_pkt_len, trim_len);
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
new_pkt->meta.origin_ctx = NULL;
return new_pkt;
}

View File

@@ -0,0 +1,175 @@
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include "utils.h"
#include "log_private.h"
#include "packet_dump.h"
#include "packet_parser.h"
#include "packet_helper.h"
#include "packet_private.h"
#define PACKET_DUMP_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet dump", format, ##__VA_ARGS__)
struct pcap_pkt_hdr
{
unsigned int tv_sec; // time stamp
unsigned int tv_usec; // time stamp
unsigned int caplen; // length of portion present
unsigned int len; // length this packet (off wire)
};
struct pcap_file_hdr
{
unsigned int magic;
unsigned short version_major;
unsigned short version_minor;
unsigned int thiszone; // gmt to local correction
unsigned int sigfigs; // accuracy of timestamps
unsigned int snaplen; // max length saved portion of each pkt
unsigned int linktype; // data link type (LINKTYPE_*)
};
// return 0: success
// return -1: failed
int packet_dump_pcap(const struct packet *pkt, const char *file)
{
const char *data = packet_get_raw_data(pkt);
uint16_t len = packet_get_raw_len(pkt);
struct pcap_pkt_hdr pkt_hdr = {};
struct pcap_file_hdr file_hdr = {
.magic = 0xA1B2C3D4,
.version_major = 0x0002,
.version_minor = 0x0004,
.thiszone = 0,
.sigfigs = 0,
.snaplen = 0xFFFF,
.linktype = 1};
if (file == NULL || data == NULL || len == 0)
{
PACKET_DUMP_LOG_ERROR("invalid parameter, file: %p, data: %p, len: %d", file, data, len);
return -1;
}
FILE *fp = fopen(file, "w+");
if (fp == NULL)
{
PACKET_DUMP_LOG_ERROR("fopen %s failed, %s", file, strerror(errno));
return -1;
}
struct timeval ts = {};
gettimeofday(&ts, NULL);
pkt_hdr.tv_sec = ts.tv_sec;
pkt_hdr.tv_usec = ts.tv_usec;
pkt_hdr.caplen = len;
pkt_hdr.len = len;
fwrite(&file_hdr, sizeof(struct pcap_file_hdr), 1, fp);
fwrite(&pkt_hdr, sizeof(struct pcap_pkt_hdr), 1, fp);
fwrite(data, 1, len, fp);
fflush(fp);
fclose(fp);
return 0;
}
void packet_dump_hex(const struct packet *pkt, int fd)
{
uint16_t len = packet_get_raw_len(pkt);
const char *data = packet_get_raw_data(pkt);
hexdump_to_fd(fd, 0, data, len);
}
int packet_dump_str(const struct packet *pkt, char *buff, int size)
{
if (pkt == NULL)
{
return 0;
}
int old = 0;
int used = 0;
memset(buff, 0, size);
used += snprintf(buff + used, size - used, "packet: %p, data_ptr: %p, data_len: %u, trim_len: %u, layers_used: %d, layers_size: %d\n",
pkt, pkt->data_ptr, pkt->data_len, pkt->trim_len,
pkt->layers_used, pkt->layers_size);
for (uint8_t i = 0; i < pkt->layers_used; i++)
{
const struct layer_private *layer = &pkt->layers[i];
used += snprintf(buff + used, size - used, "layer[%u]: %p, proto: %s, header: {offset: %u, ptr: %p, len: %u}, payload: {ptr: %p, len: %u}\n",
i, layer, layer_proto_to_str(layer->proto), layer->hdr_offset,
layer->hdr_ptr, layer->hdr_len, layer->pld_ptr, layer->pld_len);
old = used;
switch (layer->proto)
{
case LAYER_PROTO_ETHER:
used += eth_hdr_to_str((const struct ethhdr *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_PWETH:
break;
case LAYER_PROTO_PPP:
break;
case LAYER_PROTO_L2TP:
used += l2tp_hdr_to_str((const struct l2tp_hdr *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_VLAN:
used += vlan_hdr_to_str((const struct vlan_hdr *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_PPPOE:
break;
case LAYER_PROTO_MPLS:
used += mpls_label_to_str((const struct mpls_label *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_IPV4:
used += ip4_hdr_to_str((const struct ip *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_IPV6:
used += ip6_hdr_to_str((const struct ip6_hdr *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_IPAH:
break;
case LAYER_PROTO_GRE:
used += gre_hdr_to_str(layer->hdr_ptr, layer->hdr_len, buff + used, size - used);
break;
case LAYER_PROTO_UDP:
used += udp_hdr_to_str((const struct udphdr *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_TCP:
used += tcp_hdr_to_str((const struct tcphdr *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_ICMP:
break;
case LAYER_PROTO_ICMP6:
break;
case LAYER_PROTO_VXLAN:
used += vxlan_hdr_to_str((const struct vxlan_hdr *)layer->hdr_ptr, buff + used, size - used);
break;
case LAYER_PROTO_GTP_C:
case LAYER_PROTO_GTP_U:
used += gtp_hdr_to_str(layer->hdr_ptr, layer->hdr_len, buff + used, size - used);
break;
default:
break;
}
if (old != used)
{
used += snprintf(buff + used, size - used, "\n");
}
}
return used;
}
void packet_print(const struct packet *pkt)
{
char buff[4096] = {};
packet_dump_str(pkt, buff, sizeof(buff));
printf("%s\n", buff);
}

View File

@@ -0,0 +1,20 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
struct packet;
// return 0: success
// return -1: failed
int packet_dump_pcap(const struct packet *pkt, const char *file);
void packet_dump_hex(const struct packet *pkt, int fd);
// return number of bytes written
int packet_dump_str(const struct packet *pkt, char *buff, int size);
void packet_print(const struct packet *pkt);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,131 @@
#include "dablooms.h"
#include "packet_filter.h"
#include "packet_private.h"
struct packet_key
{
// TCP or UDP
uint32_t seq; // network order
uint32_t ack; // network order
uint16_t src_port; // network order
uint16_t dst_port; // network order
uint16_t l4_checksum; // network order
// IPv4
uint16_t ip_id; // network order
uint32_t src_addr; // network order
uint32_t dst_addr; // network order
} __attribute__((__packed__));
struct packet_filter
{
struct expiry_dablooms_handle *dablooms;
};
/******************************************************************************
* Private API
******************************************************************************/
// return 0: success
// reutrn -1: error
static inline int packet_key_get(const struct packet *packet, struct packet_key *key)
{
const struct layer_private *ip_layer = packet_get_innermost_layer(packet, LAYER_PROTO_IPV4);
const struct layer_private *tcp_layer = packet_get_innermost_layer(packet, LAYER_PROTO_TCP);
const struct layer_private *udp_layer = packet_get_innermost_layer(packet, LAYER_PROTO_UDP);
if (ip_layer == NULL || (tcp_layer == NULL && udp_layer == NULL))
{
return -1;
}
memset(key, 0, sizeof(struct packet_key));
const struct ip *iphdr = (const struct ip *)ip_layer->hdr_ptr;
key->ip_id = iphdr->ip_id;
key->src_addr = iphdr->ip_src.s_addr;
key->dst_addr = iphdr->ip_dst.s_addr;
if (tcp_layer)
{
const struct tcphdr *tcphdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
key->seq = tcphdr->th_seq;
key->ack = tcphdr->th_ack;
key->src_port = tcphdr->th_sport;
key->dst_port = tcphdr->th_dport;
key->l4_checksum = tcphdr->th_sum;
}
else
{
const struct udphdr *udphdr = (const struct udphdr *)udp_layer->hdr_ptr;
key->src_port = udphdr->uh_sport;
key->dst_port = udphdr->uh_dport;
key->l4_checksum = udphdr->uh_sum;
}
return 0;
}
/******************************************************************************
* Public API
******************************************************************************/
struct packet_filter *packet_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now)
{
struct packet_filter *filter = (struct packet_filter *)calloc(1, sizeof(struct packet_filter));
if (filter == NULL)
{
return NULL;
}
filter->dablooms = expiry_dablooms_new(capacity, error_rate, now, timeout);
if (filter->dablooms == NULL)
{
free(filter);
return NULL;
}
return filter;
}
void packet_filter_free(struct packet_filter *filter)
{
if (filter)
{
if (filter->dablooms)
{
expiry_dablooms_free(filter->dablooms);
filter->dablooms = NULL;
}
free(filter);
filter = NULL;
}
}
// return 1: found
// reutrn 0: no found
int packet_filter_lookup(struct packet_filter *filter, const struct packet *packet, uint64_t now)
{
struct packet_key key;
if (packet_key_get(packet, &key) == -1)
{
return 0;
}
if (expiry_dablooms_search(filter->dablooms, (const char *)&key, sizeof(struct packet_key), now) == 1)
{
return 1;
}
return 0;
}
void packet_filter_add(struct packet_filter *filter, const struct packet *packet, uint64_t now)
{
struct packet_key key;
if (packet_key_get(packet, &key) == -1)
{
return;
}
expiry_dablooms_add(filter->dablooms, (const char *)&key, sizeof(struct packet_key), now);
}

View File

@@ -0,0 +1,22 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
struct packet;
// Duplicated Packet Filter for IPv4 Packet
struct packet_filter;
struct packet_filter *packet_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now);
void packet_filter_free(struct packet_filter *filter);
// return 1: found
// reutrn 0: no found
int packet_filter_lookup(struct packet_filter *filter, const struct packet *pkt, uint64_t now);
void packet_filter_add(struct packet_filter *filter, const struct packet *pkt, uint64_t now);
#ifdef __cplusplus
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,980 @@
#include <string.h>
#include <stdlib.h>
#include <netinet/icmp6.h>
#include <netinet/ip_icmp.h>
#include <linux/ppp_defs.h>
#include "log_private.h"
#include "packet_helper.h"
#include "packet_private.h"
#include "packet_parser.h"
#define PACKET_PARSE_LOG_DEBUG(format, ...) // STELLAR_LOG_DEBUG(__thread_local_logger, "packet parse", format, ##__VA_ARGS__)
#define PACKET_PARSE_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet parse", format, ##__VA_ARGS__)
#define PACKET_PARSE_LOG_WARN(format, ...) STELLAR_LOG_WARN(__thread_local_logger, "packet parse", format, ##__VA_ARGS__)
#define PACKET_LOG_DATA_INSUFFICIENCY(pkt, layer) \
{ \
PACKET_PARSE_LOG_WARN("pkt: %p, layer: %s, data insufficiency", \
(pkt), layer_proto_to_str(layer)); \
}
#define PACKET_LOG_UNSUPPORT_PROTO(pkt, layer, next_proto) \
{ \
PACKET_PARSE_LOG_WARN("pkt: %p, layer: %s, unsupport next proto: %d", \
(pkt), layer_proto_to_str(layer), (next_proto)); \
}
#define PACKET_LOG_UNSUPPORT_ETHPROTO(pkt, next_proto) \
{ \
PACKET_PARSE_LOG_WARN("pkt: %p, layer: L3, unsupport next proto: %d %s", \
(pkt), (next_proto), eth_proto_to_str(next_proto)); \
}
#define PACKET_LOG_UNSUPPORT_IPPROTO(pkt, next_proto) \
{ \
PACKET_PARSE_LOG_WARN("pkt: %p, layer: L4, unsupport next proto: %d %s", \
(pkt), (next_proto), ip_proto_to_str(next_proto)); \
}
/******************************************************************************
* Static API
******************************************************************************/
static inline struct layer_private *get_free_layer(struct packet *pkt);
// 数据链路层
static inline const char *parse_ether(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_pweth(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_ppp(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_l2tpv2_over_udp(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_l2tpv3_over_udp(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_l2tpv3_over_ip(struct packet *pkt, const char *data, uint16_t len);
// 数据链路层 -- 隧道
static inline const char *parse_vlan(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_pppoe_ses(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_mpls(struct packet *pkt, const char *data, uint16_t len);
// 网络层
static inline const char *parse_ipv4(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_ipv6(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_auth(struct packet *pkt, const char *data, uint16_t len);
// 网络层 -- 隧道
static inline const char *parse_gre(struct packet *pkt, const char *data, uint16_t len);
// 传输层
static inline const char *parse_udp(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_tcp(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_icmp(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_icmp6(struct packet *pkt, const char *data, uint16_t len);
// 传输层 -- 隧道
static inline const char *parse_vxlan(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_gtp_u(struct packet *pkt, const char *data, uint16_t len);
static inline const char *parse_gtp_c(struct packet *pkt, const char *data, uint16_t len);
// L3/L4
static inline const char *parse_l3(struct packet *pkt, uint16_t next_proto, const char *data, uint16_t len);
static inline const char *parse_l4(struct packet *pkt, uint8_t next_proto, const char *data, uint16_t len);
/******************************************************************************
* Private API -- Utils
******************************************************************************/
static inline struct layer_private *get_free_layer(struct packet *pkt)
{
if (pkt->layers_used >= pkt->layers_size)
{
return NULL;
}
return &pkt->layers[pkt->layers_used];
}
#define SET_LAYER(_pkt, _layer, _proto, _hdr_len, _data, _len, _trim) \
{ \
(_layer)->proto = (_proto); \
(_layer)->hdr_offset = (_pkt)->data_len - (_pkt)->trim_len - (_len); \
(_layer)->hdr_ptr = (_data); \
(_layer)->hdr_len = (_hdr_len); \
(_layer)->pld_ptr = (_data) + (_hdr_len); \
(_layer)->pld_len = (_len) - (_hdr_len) - (_trim); \
(_pkt)->trim_len += (_trim); \
(_pkt)->layers_used++; \
PACKET_PARSE_LOG_DEBUG("layer[%d/%d]: %s, header: {offset: %d, ptr: %p, len: %d}, payload: {ptr: %p, len: %d}", \
(_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); \
}
/******************************************************************************
* Private API -- Parses
******************************************************************************/
static inline const char *parse_ether(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < sizeof(struct ethhdr)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_ETHER);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
uint16_t next_proto = eth_hdr_get_proto((const struct ethhdr *)data);
SET_LAYER(pkt, layer, LAYER_PROTO_ETHER, sizeof(struct ethhdr), data, len, 0);
return parse_l3(pkt, next_proto, layer->pld_ptr, layer->pld_len);
}
static inline const char *parse_pweth(struct packet *pkt, const char *data, uint16_t len)
{
/*
* PW Ethernet Control Word
* 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |0 0 0 0| Reserved | Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* Reference: https://tools.ietf.org/html/rfc4448
*/
if (unlikely(len < 4))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_PWETH);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_PWETH, 4, data, len, 0);
return parse_ether(pkt, layer->pld_ptr, layer->pld_len);
}
static inline int is_ppp_proto(uint16_t proto)
{
// /usr/include/linux/ppp_defs.h.html
switch (proto)
{
case PPP_IP: /* Internet Protocol */
case PPP_AT: /* AppleTalk Protocol */
case PPP_IPX: /* IPX protocol */
case PPP_VJC_COMP: /* VJ compressed TCP */
case PPP_VJC_UNCOMP: /* VJ uncompressed TCP */
case PPP_MP: /* Multilink protocol */
case PPP_IPV6: /* Internet Protocol Version 6 */
case PPP_COMPFRAG: /* fragment compressed below bundle */
case PPP_COMP: /* compressed packet */
case PPP_MPLS_UC: /* Multi Protocol Label Switching - Unicast */
case PPP_MPLS_MC: /* Multi Protocol Label Switching - Multicast */
case PPP_IPCP: /* IP Control Protocol */
case PPP_ATCP: /* AppleTalk Control Protocol */
case PPP_IPXCP: /* IPX Control Protocol */
case PPP_IPV6CP: /* IPv6 Control Protocol */
case PPP_CCPFRAG: /* CCP at link level (below MP bundle) */
// case PPP_CCP: /* Compression Control Protocol */ (same as PPP_CCPFRAG)
case PPP_MPLSCP: /* MPLS Control Protocol */
case PPP_LCP: /* Link Control Protocol */
case PPP_PAP: /* Password Authentication Protocol */
case PPP_LQR: /* Link Quality Report protocol */
case PPP_CHAP: /* Cryptographic Handshake Auth. Protocol */
case PPP_CBCP: /* Callback Control Protocol */
return 1;
default:
return 0;
}
}
static inline const char *parse_ppp(struct packet *pkt, const char *data, uint16_t len)
{
/*
* https://datatracker.ietf.org/doc/html/rfc1661#section-2
* +----------+-------------+---------+
* | Protocol | Information | Padding |
* | 8/16 bits| * | * |
* +----------+-------------+---------+
*
* https://datatracker.ietf.org/doc/html/rfc1331#section-3.1
* +----------+----------+----------+----------+------------
* | Flag | Address | Control | Protocol | Information
* | 01111110 | 11111111 | 00000011 | 16 bits | *
* +----------+----------+----------+----------+------------
* ---+----------+----------+-----------------
* | FCS | Flag | Inter-frame Fill
* | 16 bits | 01111110 | or next Address
* ---+----------+----------+-----------------
*/
if (unlikely(len < 4))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_PPP);
return data;
}
uint16_t hdr_len = 0;
uint16_t next_proto = 0;
struct layer_private *layer = NULL;
// ppp header 1 byte
next_proto = *((uint8_t *)data);
if (is_ppp_proto(next_proto))
{
hdr_len = 1;
goto success;
}
// ppp header 2 bytes
next_proto = ntohs(*((uint16_t *)data));
if (is_ppp_proto(next_proto))
{
hdr_len = 2;
goto success;
}
// ppp header 4 bytes
next_proto = ntohs(*((uint16_t *)data + 1));
hdr_len = 4;
success:
layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_PPP, hdr_len, data, len, 0);
switch (next_proto)
{
case PPP_IP:
return parse_ipv4(pkt, layer->pld_ptr, layer->pld_len);
case PPP_IPV6:
return parse_ipv6(pkt, layer->pld_ptr, layer->pld_len);
default:
PACKET_LOG_UNSUPPORT_PROTO(pkt, LAYER_PROTO_PPP, next_proto);
return layer->pld_ptr;
}
}
static inline const char *parse_l2tpv2_over_udp(struct packet *pkt, const char *data, uint16_t len)
{
uint16_t hdr_len = calc_udp_l2tpv2_hdr_len(data, len);
if (unlikely(hdr_len == 0 || hdr_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_L2TP);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_L2TP, hdr_len, data, len, 0);
// control message
if (l2tp_hdr_get_type((const struct l2tp_hdr *)data))
{
return layer->pld_ptr;
}
// data message
else
{
return parse_ppp(pkt, layer->pld_ptr, layer->pld_len);
}
}
static inline const char *parse_l2tpv3_over_udp(struct packet *pkt, const char *data, uint16_t len)
{
uint16_t hdr_len = calc_udp_l2tpv3_hdr_len(data, len);
if (unlikely(hdr_len == 0 || hdr_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_L2TP);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_L2TP, hdr_len, data, len, 0);
// control message
if (l2tp_hdr_get_type((const struct l2tp_hdr *)data))
{
return layer->pld_ptr;
}
// data message
else
{
// TOOD
return layer->pld_ptr;
}
}
static inline const char *parse_l2tpv3_over_ip(struct packet *pkt, const char *data, uint16_t len)
{
uint16_t hdr_len = calc_ip_l2tpv3_hdr_len(data, len);
if (unlikely(hdr_len == 0 || hdr_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_L2TP);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_L2TP, hdr_len, data, len, 0);
// data message
if (ntohl(*((uint32_t *)data)))
{
// TOOD
return layer->pld_ptr;
}
// control message
else
{
return layer->pld_ptr;
}
}
static inline const char *parse_vlan(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < sizeof(struct vlan_hdr)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_VLAN);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
uint16_t next_proto = vlan_hdr_get_ethertype((const struct vlan_hdr *)data);
SET_LAYER(pkt, layer, LAYER_PROTO_VLAN, sizeof(struct vlan_hdr), data, len, 0);
return parse_l3(pkt, next_proto, layer->pld_ptr, layer->pld_len);
}
static inline const char *parse_pppoe_ses(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < 6))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_PPPOE);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_PPPOE, 6, data, len, 0);
return parse_ppp(pkt, layer->pld_ptr, layer->pld_len);
}
static inline const char *parse_mpls(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < 4))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_MPLS);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
if (mpls_label_get_bos((const struct mpls_label *)data))
{
SET_LAYER(pkt, layer, LAYER_PROTO_MPLS, 4, data, len, 0);
if (layer->pld_len == 0)
{
return layer->pld_ptr;
}
uint8_t next_proto = layer->pld_ptr[0] >> 4;
switch (next_proto)
{
case 0:
// the first four digits of the PW Ethernet control word must be "00000", but the first four digits of Ethernet may also be "0000"
if (layer->pld_len >= sizeof(struct ethhdr) && is_eth_proto(eth_hdr_get_proto((const struct ethhdr *)layer->pld_ptr)))
{
return parse_ether(pkt, layer->pld_ptr, layer->pld_len);
}
else
{
return parse_pweth(pkt, layer->pld_ptr, layer->pld_len);
}
case 4:
return parse_ipv4(pkt, layer->pld_ptr, layer->pld_len);
case 6:
return parse_ipv6(pkt, layer->pld_ptr, layer->pld_len);
default:
return parse_ether(pkt, layer->pld_ptr, layer->pld_len);
}
}
else
{
SET_LAYER(pkt, layer, LAYER_PROTO_MPLS, 4, data, len, 0);
return parse_mpls(pkt, layer->pld_ptr, layer->pld_len);
}
}
static inline const char *parse_ipv4(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < sizeof(struct ip)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPV4);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
const struct ip *hdr = (const struct ip *)data;
uint8_t version = ip4_hdr_get_version(hdr);
if (unlikely(version != 4))
{
PACKET_PARSE_LOG_ERROR("packet %p ipv4 version %d != 4", pkt, version);
return data;
}
uint16_t hdr_len = ip4_hdr_get_hdr_len(hdr);
if (unlikely(hdr_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPV4);
return data;
}
uint16_t total_len = ip4_hdr_get_total_len(hdr);
if (unlikely(total_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPV4);
return data;
}
if (unlikely(total_len < hdr_len))
{
PACKET_PARSE_LOG_ERROR("packet %p ip total_len %d < hdr_len %d", pkt, total_len, hdr_len);
return data;
}
uint16_t trim_len = len - total_len;
SET_LAYER(pkt, layer, LAYER_PROTO_IPV4, hdr_len, data, len, trim_len);
// ip fragmented
if (ip4_hdr_get_mf_flag(hdr) || ip4_hdr_get_frag_offset(hdr))
{
PACKET_PARSE_LOG_DEBUG("packet %p ip layer %p is fragmented", pkt, layer);
pkt->frag_layer = layer;
return layer->pld_ptr;
}
uint8_t next_proto = ip4_hdr_get_proto(hdr);
return parse_l4(pkt, next_proto, layer->pld_ptr, layer->pld_len);
}
static inline const char *parse_ipv6(struct packet *pkt, const char *data, uint16_t len)
{
/*
* IP6 Extension Headers
*
* Internet Protocol, Version 6 (IPv6) : https://datatracker.ietf.org/doc/html/rfc2460
* IP Encapsulating Security Payload (ESP) : https://datatracker.ietf.org/doc/html/rfc2406
* IP Authentication Header : https://datatracker.ietf.org/doc/html/rfc4302
*
* skip next header
* #define IPPROTO_HOPOPTS 0 // IP6 hop-by-hop options
* #define IPPROTO_ROUTING 43 // IP6 routing header
* #define IPPROTO_AH 51 // IP6 Auth Header
* #define IPPROTO_DSTOPTS 60 // IP6 destination option
*
* not skip next header
* #define IPPROTO_FRAGMENT 44 // IP6 fragmentation header
* #define IPPROTO_ESP 50 // IP6 Encap Sec. Payload
* #define IPPROTO_NONE 59 // IP6 no next header
*/
if (unlikely(len < sizeof(struct ip6_hdr)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPV6);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
const struct ip6_hdr *hdr = (const struct ip6_hdr *)data;
uint8_t version = ip6_hdr_get_version(hdr);
if (unlikely(version != 6))
{
PACKET_PARSE_LOG_ERROR("packet %p ipv6 version %d != 6", pkt, version);
return data;
}
uint16_t pld_len = ip6_hdr_get_payload_len(hdr);
if (unlikely(pld_len + sizeof(struct ip6_hdr) > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPV6);
return data;
}
uint8_t next_proto = ip6_hdr_get_next_header(hdr);
uint16_t hdr_len = sizeof(struct ip6_hdr);
uint16_t trim_len = len - pld_len - sizeof(struct ip6_hdr);
const char *next_hdr_ptr = data + hdr_len;
while (next_proto == IPPROTO_HOPOPTS || next_proto == IPPROTO_ROUTING || next_proto == IPPROTO_AH || next_proto == IPPROTO_DSTOPTS)
{
if (unlikely(pld_len < 2))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPV6);
return data;
}
struct ip6_ext *ext = (struct ip6_ext *)next_hdr_ptr;
uint16_t skip_len = 0;
if (next_proto == IPPROTO_AH)
{
/*
* https://datatracker.ietf.org/doc/html/rfc4302#section-2
* For IPv6, the total length of the header must be a multiple of 8-octet units.
* (Note that although IPv6 [DH98] characterizes AH as an extension header,
* its length is measured in 32-bit words, not the 64-bit words used by other IPv6 extension headers.)
*/
skip_len = ext->ip6e_len * 4 + 8;
}
else
{
skip_len = ext->ip6e_len * 8 + 8;
}
if (unlikely(skip_len > pld_len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPV6);
return data;
}
hdr_len += skip_len;
pld_len -= skip_len;
next_hdr_ptr += skip_len;
next_proto = ext->ip6e_nxt;
}
SET_LAYER(pkt, layer, LAYER_PROTO_IPV6, hdr_len, data, len, trim_len);
// ipv6 fragment
if (next_proto == IPPROTO_FRAGMENT)
{
PACKET_PARSE_LOG_DEBUG("packet %p ipv6 layer %p is fragmented", pkt, layer);
pkt->frag_layer = layer;
return layer->pld_ptr;
}
return parse_l4(pkt, next_proto, layer->pld_ptr, layer->pld_len);
}
static inline const char *parse_auth(struct packet *pkt, const char *data, uint16_t len)
{
/*
* https://datatracker.ietf.org/doc/html/rfc4302#section-2
* For IPv4: AH not IPv4 option, as an single layer
* For IPv6: AH as IPv6 extension header
*
* AH 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Next Header | Payload Len | RESERVED |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Security Parameters Index (SPI) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Sequence Number Field |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* + Integrity Check Value-ICV (variable) |
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
if (unlikely(len < 12))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPAH);
return data;
}
/*
* https://datatracker.ietf.org/doc/html/rfc4302#section-2
* For IPv4, This 8-bit field specifies the length of AH in 32-bit words (4-byte units), minus "2".
* Thus, for example, if an integrity algorithm yields a 96-bit authentication value,
* this length field will be "4" (3 32-bit word fixed fields plus 3 32-bit words for the ICV, minus 2).
*/
uint8_t next_proto = ((const uint8_t *)data)[0];
uint16_t hdr_len = ((const uint8_t *)data)[1] * 4 + 8;
if (unlikely(len < hdr_len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_IPAH);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_IPAH, hdr_len, data, len, 0);
return parse_l4(pkt, next_proto, layer->pld_ptr, layer->pld_len);
}
static inline const char *parse_gre(struct packet *pkt, const char *data, uint16_t len)
{
uint16_t hdr_len = calc_gre_hdr_len(data, len);
if (unlikely(hdr_len == 0 || hdr_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_GRE);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
uint16_t next_proto = peek_gre_proto(data, len);
SET_LAYER(pkt, layer, LAYER_PROTO_GRE, hdr_len, data, len, 0);
return parse_l3(pkt, next_proto, layer->pld_ptr, layer->pld_len);
}
static inline const char *parse_udp(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < sizeof(struct udphdr)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_UDP);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
const struct udphdr *udp_hdr = (struct udphdr *)data;
SET_LAYER(pkt, layer, LAYER_PROTO_UDP, sizeof(struct udphdr), data, len, 0);
uint16_t src_port = udp_hdr_get_src_port(udp_hdr);
uint16_t dst_port = udp_hdr_get_dst_port(udp_hdr);
if (dst_port == 4789)
{
// VXLAN_DPORT 4789
return parse_vxlan(pkt, layer->pld_ptr, layer->pld_len);
}
if (dst_port == 2152 || src_port == 2152)
{
// only GTPv1-U, no GTPv2-U
return parse_gtp_u(pkt, layer->pld_ptr, layer->pld_len);
}
if (dst_port == 2123 || src_port == 2123)
{
// GTPv1-C or GTPv2-C
return parse_gtp_c(pkt, layer->pld_ptr, layer->pld_len);
}
if (dst_port == 1701 || src_port == 1701)
{
// L2TP_DPORT 1701
if (unlikely(layer->pld_len < 8))
{
return layer->pld_ptr;
}
switch (l2tp_hdr_get_ver((const struct l2tp_hdr *)layer->pld_ptr))
{
case 2:
return parse_l2tpv2_over_udp(pkt, layer->pld_ptr, layer->pld_len);
case 3:
return parse_l2tpv3_over_udp(pkt, layer->pld_ptr, layer->pld_len);
default:
return layer->pld_ptr;
}
}
if (dst_port == 3544 || src_port == 3544)
{
// Teredo IPv6 tunneling 3544
if (unlikely(layer->pld_len < sizeof(struct ip6_hdr)))
{
return layer->pld_ptr;
}
const struct ip6_hdr *ipv6_hdr = (const struct ip6_hdr *)layer->pld_ptr;
if (ip6_hdr_get_version(ipv6_hdr) != 6)
{
return layer->pld_ptr;
}
return parse_ipv6(pkt, layer->pld_ptr, layer->pld_len);
}
return layer->pld_ptr;
}
static inline const char *parse_tcp(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < sizeof(struct tcphdr)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_TCP);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
uint16_t hdr_len = tcp_hdr_get_hdr_len((const struct tcphdr *)data);
if (unlikely(hdr_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_TCP);
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_TCP, hdr_len, data, len, 0);
return layer->pld_ptr;
}
static inline const char *parse_icmp(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < sizeof(struct icmphdr)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_ICMP);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_ICMP, sizeof(struct icmphdr), data, len, 0);
return layer->pld_ptr;
}
static inline const char *parse_icmp6(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < sizeof(struct icmp6_hdr)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_ICMP6);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_ICMP6, sizeof(struct icmp6_hdr), data, len, 0);
return layer->pld_ptr;
}
static inline const char *parse_vxlan(struct packet *pkt, const char *data, uint16_t len)
{
if (unlikely(len < sizeof(struct vxlan_hdr)))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_VXLAN);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_VXLAN, sizeof(struct vxlan_hdr), data, len, 0);
return parse_ether(pkt, layer->pld_ptr, layer->pld_len);
}
static inline const char *parse_gtp_u(struct packet *pkt, const char *data, uint16_t len)
{
// only GTPv1-U, no GTPv2-U
uint8_t version = peek_gtp_version(data, len);
if (unlikely(version != 1))
{
return data;
}
uint16_t hdr_len = calc_gtp_hdr_len(data, len);
if (unlikely(hdr_len == 0 || hdr_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_GTP_U);
return data;
}
uint8_t next_proto = (((const uint8_t *)(data + hdr_len))[0]) >> 4;
if (next_proto != 4 && next_proto != 6)
{
// next_proto is not IPv4 or IPv6, this is not a normal GTP-U packet, fallback to UDP
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_GTP_U, hdr_len, data, len, 0);
switch (next_proto)
{
case 4:
return parse_ipv4(pkt, layer->pld_ptr, layer->pld_len);
case 6:
return parse_ipv6(pkt, layer->pld_ptr, layer->pld_len);
default:
PACKET_LOG_UNSUPPORT_PROTO(pkt, LAYER_PROTO_GTP_U, next_proto);
return layer->pld_ptr;
}
}
static inline const char *parse_gtp_c(struct packet *pkt, const char *data, uint16_t len)
{
// GTPv1-C or GTPv2-C
uint16_t hdr_len = calc_gtp_hdr_len(data, len);
if (unlikely(hdr_len == 0 || hdr_len > len))
{
PACKET_LOG_DATA_INSUFFICIENCY(pkt, LAYER_PROTO_GTP_C);
return data;
}
struct layer_private *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
}
SET_LAYER(pkt, layer, LAYER_PROTO_GTP_C, hdr_len, data, len, 0);
return layer->pld_ptr;
}
static inline const char *parse_l3(struct packet *pkt, uint16_t next_proto, const char *data, uint16_t len)
{
switch (next_proto)
{
case ETH_P_8021Q:
case ETH_P_8021AD:
return parse_vlan(pkt, data, len);
case ETH_P_IP:
return parse_ipv4(pkt, data, len);
case ETH_P_IPV6:
return parse_ipv6(pkt, data, len);
case ETH_P_PPP_SES:
return parse_pppoe_ses(pkt, data, len);
case ETH_P_MPLS_UC:
return parse_mpls(pkt, data, len);
case 0x880b:
return parse_ppp(pkt, data, len);
default:
PACKET_LOG_UNSUPPORT_ETHPROTO(pkt, next_proto);
return data;
}
}
static inline const char *parse_l4(struct packet *pkt, uint8_t next_proto, const char *data, uint16_t len)
{
switch (next_proto)
{
case IPPROTO_AH:
return parse_auth(pkt, data, len);
case IPPROTO_TCP:
return parse_tcp(pkt, data, len);
case IPPROTO_UDP:
return parse_udp(pkt, data, len);
case IPPROTO_IPIP:
return parse_ipv4(pkt, data, len);
case IPPROTO_IPV6:
return parse_ipv6(pkt, data, len);
case IPPROTO_GRE:
return parse_gre(pkt, data, len);
case IPPROTO_ICMP:
return parse_icmp(pkt, data, len);
case IPPROTO_ICMPV6:
return parse_icmp6(pkt, data, len);
case 115:
// L2TP
return parse_l2tpv3_over_ip(pkt, data, len);
default:
PACKET_LOG_UNSUPPORT_IPPROTO(pkt, next_proto);
return data;
}
}
/******************************************************************************
* Public API
******************************************************************************/
// return innermost payload
const char *packet_parse(struct packet *pkt, const char *data, uint16_t len)
{
pkt->frag_layer = NULL;
pkt->layers_used = 0;
pkt->layers_size = PACKET_MAX_LAYERS;
pkt->data_ptr = data;
pkt->data_len = len;
pkt->trim_len = 0;
return parse_ether(pkt, data, len);
}
const char *layer_proto_to_str(enum layer_proto proto)
{
switch (proto)
{
case LAYER_PROTO_ETHER:
return "ETH";
case LAYER_PROTO_PWETH:
return "PWETH";
case LAYER_PROTO_PPP:
return "PPP";
case LAYER_PROTO_L2TP:
return "L2TP";
case LAYER_PROTO_VLAN:
return "VLAN";
case LAYER_PROTO_PPPOE:
return "PPPOE";
case LAYER_PROTO_MPLS:
return "MPLS";
case LAYER_PROTO_IPV4:
return "IPV4";
case LAYER_PROTO_IPV6:
return "IPV6";
case LAYER_PROTO_IPAH:
return "IPAH";
case LAYER_PROTO_GRE:
return "GRE";
case LAYER_PROTO_UDP:
return "UDP";
case LAYER_PROTO_TCP:
return "TCP";
case LAYER_PROTO_ICMP:
return "ICMP";
case LAYER_PROTO_ICMP6:
return "ICMP6";
case LAYER_PROTO_VXLAN:
return "VXLAN";
case LAYER_PROTO_GTP_C:
return "GTP-C";
case LAYER_PROTO_GTP_U:
return "GTP-U";
default:
return "UNKNOWN";
}
}

View File

@@ -0,0 +1,15 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/packet.h"
const char *packet_parse(struct packet *pkt, const char *data, uint16_t len);
const char *layer_proto_to_str(enum layer_proto proto);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,149 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "tuple.h"
#include "stellar/packet.h"
#define PACKET_MAX_LAYERS 32
#define MAX_ROUTE_CTX 64
struct route_ctx
{
char data[MAX_ROUTE_CTX];
int used;
};
struct metadata
{
struct route_ctx route_ctx;
struct sids sids;
uint64_t session_id;
uint64_t domain;
uint16_t link_id;
int is_ctrl;
enum packet_direction direction;
enum packet_action action;
struct timeval tv;
const void *origin_ctx;
};
struct layer_private
{
enum layer_proto proto;
uint16_t hdr_len; // header length
const char *hdr_ptr; // header pointer
uint16_t pld_len; // payload length
const char *pld_ptr; // payload pointer
uint16_t hdr_offset; // header offset from data_ptr
};
struct packet
{
void *user_data;
struct layer_private layers[PACKET_MAX_LAYERS];
struct layer_private *frag_layer; // fragment layer
int8_t layers_used;
int8_t layers_size;
int8_t need_free;
const char *data_ptr;
uint16_t data_len;
uint16_t trim_len; // trim eth padding
struct metadata meta;
};
enum packet_load_balance_method
{
PKT_LDBC_METH_OUTERMOST_INT_IP = 1,
PKT_LDBC_METH_OUTERMOST_EXT_IP = 2,
PKT_LDBC_METH_OUTERMOST_INT_EXT_IP = 3,
PKT_LDBC_METH_INNERMOST_INT_IP = 4,
PKT_LDBC_METH_INNERMOST_EXT_IP = 5,
};
/******************************************************************************
* 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_origin_ctx(struct packet *pkt, void *ctx);
const void *packet_get_origin_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_set_session_id(struct packet *pkt, uint64_t id);
uint64_t packet_get_session_id(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_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);
void *packet_get_user_data(struct packet *pkt);
void packet_set_user_data(struct packet *pkt, void *data);
/******************************************************************************
* tuple uitls
******************************************************************************/
// return 0: found
// return -1: not found
int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
/******************************************************************************
* layer uitls
******************************************************************************/
const struct layer_private *packet_get_layer(const struct packet *pkt, int idx);
const struct layer_private *packet_get_innermost_layer(const struct packet *pkt, enum layer_proto proto);
const struct layer_private *packet_get_outermost_layer(const struct packet *pkt, enum layer_proto proto);
/******************************************************************************
* load balance uitls
******************************************************************************/
uint64_t packet_ldbc_hash(const struct packet *pkt, enum packet_load_balance_method method, enum packet_direction direction);
/******************************************************************************
* other uitls
******************************************************************************/
struct packet *packet_new(uint16_t pkt_len);
struct packet *packet_dup(const struct packet *pkt);
void packet_free(struct packet *pkt);
int packet_is_fragment(const struct packet *pkt);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,931 @@
#include "tuple.h"
#include "uthash.h"
#include "log_private.h"
#include "packet_helper.h"
#include "packet_private.h"
#define PACKET_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet", format, ##__VA_ARGS__)
/******************************************************************************
* metadata utils
******************************************************************************/
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx)
{
pkt->meta.route_ctx = *ctx;
}
const struct route_ctx *packet_get_route_ctx(const struct packet *pkt)
{
return &pkt->meta.route_ctx;
}
void packet_set_origin_ctx(struct packet *pkt, void *ctx)
{
pkt->meta.origin_ctx = ctx;
}
const void *packet_get_origin_ctx(const struct packet *pkt)
{
return pkt->meta.origin_ctx;
}
void packet_set_sids(struct packet *pkt, const struct sids *sids)
{
pkt->meta.sids = *sids;
}
const struct sids *packet_get_sids(const struct packet *pkt)
{
return &pkt->meta.sids;
}
void packet_prepend_sids(struct packet *pkt, const struct sids *sids)
{
if (pkt->meta.sids.used + sids->used > MAX_SIDS)
{
PACKET_LOG_ERROR("sids overflow");
return;
}
else
{
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;
}
}
void packet_set_session_id(struct packet *pkt, uint64_t id)
{
pkt->meta.session_id = id;
}
uint64_t packet_get_session_id(const struct packet *pkt)
{
return pkt->meta.session_id;
}
void packet_set_domain(struct packet *pkt, uint64_t domain)
{
pkt->meta.domain = domain;
}
uint64_t packet_get_domain(const struct packet *pkt)
{
return pkt->meta.domain;
}
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)
{
pkt->meta.action = action;
}
enum packet_action packet_get_action(const struct packet *pkt)
{
return pkt->meta.action;
}
void packet_set_timeval(struct packet *pkt, const struct timeval *tv)
{
pkt->meta.tv = *tv;
}
const struct timeval *packet_get_timeval(const struct packet *pkt)
{
return &pkt->meta.tv;
}
void packet_set_user_data(struct packet *pkt, void *data)
{
pkt->user_data = data;
}
void *packet_get_user_data(struct packet *pkt)
{
return pkt->user_data;
}
/******************************************************************************
* tuple uitls
******************************************************************************/
// return 0 : found
// return -1 : not found
int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
{
memset(tuple, 0, sizeof(struct tuple2));
const struct layer_private *layer = NULL;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{
layer = &pkt->layers[i];
if (layer->proto == LAYER_PROTO_IPV4)
{
const struct ip *ip4_hdr = (const struct ip *)layer->hdr_ptr;
tuple->addr_family = AF_INET;
tuple->src_addr.v4 = ip4_hdr_get_src_in_addr(ip4_hdr);
tuple->dst_addr.v4 = ip4_hdr_get_dst_in_addr(ip4_hdr);
return 0;
}
if (layer->proto == LAYER_PROTO_IPV6)
{
const struct ip6_hdr *ip6_hdr = (const struct ip6_hdr *)layer->hdr_ptr;
tuple->addr_family = AF_INET6;
tuple->src_addr.v6 = ip6_hdr_get_src_in6_addr(ip6_hdr);
tuple->dst_addr.v6 = ip6_hdr_get_dst_in6_addr(ip6_hdr);
return 0;
}
}
return -1;
}
// return 0 : found
// return -1 : not found
int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
{
memset(tuple, 0, sizeof(struct tuple2));
const struct layer_private *layer = NULL;
for (int8_t i = 0; i < pkt->layers_used; i++)
{
layer = &pkt->layers[i];
if (layer->proto == LAYER_PROTO_IPV4)
{
const struct ip *ip4_hdr = (const struct ip *)layer->hdr_ptr;
tuple->addr_family = AF_INET;
tuple->src_addr.v4 = ip4_hdr_get_src_in_addr(ip4_hdr);
tuple->dst_addr.v4 = ip4_hdr_get_dst_in_addr(ip4_hdr);
return 0;
}
if (layer->proto == LAYER_PROTO_IPV6)
{
const struct ip6_hdr *ip6_hdr = (const struct ip6_hdr *)layer->hdr_ptr;
tuple->addr_family = AF_INET6;
tuple->src_addr.v6 = ip6_hdr_get_src_in6_addr(ip6_hdr);
tuple->dst_addr.v6 = ip6_hdr_get_dst_in6_addr(ip6_hdr);
return 0;
}
}
return -1;
}
// return 0 : found
// return -1 : not found
int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
{
memset(tuple, 0, sizeof(struct tuple4));
const struct layer_private *layer_l3 = NULL;
const struct layer_private *layer_l4 = NULL;
const struct layer_private *layer = NULL;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{
layer = &pkt->layers[i];
// first get L4 layer
if (layer->proto == LAYER_PROTO_UDP)
{
const struct udphdr *udp_hdr = (const struct udphdr *)layer->hdr_ptr;
tuple->src_port = udp_hdr->uh_sport;
tuple->dst_port = udp_hdr->uh_dport;
layer_l4 = layer;
continue;
}
if (layer->proto == LAYER_PROTO_TCP)
{
const struct tcphdr *tcp_hdr = (const struct tcphdr *)layer->hdr_ptr;
tuple->src_port = tcp_hdr->th_sport;
tuple->dst_port = tcp_hdr->th_dport;
layer_l4 = layer;
continue;
}
// second get L3 layer
if (layer->proto == LAYER_PROTO_IPV4)
{
const struct ip *ip4_hdr = (const struct ip *)layer->hdr_ptr;
tuple->addr_family = AF_INET;
tuple->src_addr.v4 = ip4_hdr_get_src_in_addr(ip4_hdr);
tuple->dst_addr.v4 = ip4_hdr_get_dst_in_addr(ip4_hdr);
layer_l3 = layer;
break;
}
if (layer->proto == LAYER_PROTO_IPV6)
{
const struct ip6_hdr *ip6_hdr = (const struct ip6_hdr *)layer->hdr_ptr;
tuple->addr_family = AF_INET6;
tuple->src_addr.v6 = ip6_hdr_get_src_in6_addr(ip6_hdr);
tuple->dst_addr.v6 = ip6_hdr_get_dst_in6_addr(ip6_hdr);
layer_l3 = layer;
break;
}
}
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
{
return 0;
}
else
{
return -1;
}
}
// return 0 : found
// return -1 : not found
int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
{
memset(tuple, 0, sizeof(struct tuple4));
const struct layer_private *layer_l3 = NULL;
const struct layer_private *layer_l4 = NULL;
const struct layer_private *layer = NULL;
for (int8_t i = 0; i < pkt->layers_used; i++)
{
layer = &pkt->layers[i];
// first get L3 layer
if (layer->proto == LAYER_PROTO_IPV4)
{
const struct ip *ip4_hdr = (const struct ip *)layer->hdr_ptr;
tuple->addr_family = AF_INET;
tuple->src_addr.v4 = ip4_hdr_get_src_in_addr(ip4_hdr);
tuple->dst_addr.v4 = ip4_hdr_get_dst_in_addr(ip4_hdr);
layer_l3 = layer;
continue;
}
if (layer->proto == LAYER_PROTO_IPV6)
{
const struct ip6_hdr *ip6_hdr = (const struct ip6_hdr *)layer->hdr_ptr;
tuple->addr_family = AF_INET6;
tuple->src_addr.v6 = ip6_hdr_get_src_in6_addr(ip6_hdr);
tuple->dst_addr.v6 = ip6_hdr_get_dst_in6_addr(ip6_hdr);
layer_l3 = layer;
continue;
}
// second get L4 layer
if (layer->proto == LAYER_PROTO_UDP)
{
const struct udphdr *udp_hdr = (const struct udphdr *)layer->hdr_ptr;
tuple->src_port = udp_hdr->uh_sport;
tuple->dst_port = udp_hdr->uh_dport;
layer_l4 = layer;
break;
}
if (layer->proto == LAYER_PROTO_TCP)
{
const struct tcphdr *tcp_hdr = (const struct tcphdr *)layer->hdr_ptr;
tuple->src_port = tcp_hdr->th_sport;
tuple->dst_port = tcp_hdr->th_dport;
layer_l4 = layer;
break;
}
}
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
{
return 0;
}
else
{
return -1;
}
}
// return 0 : found
// return -1 : not found
int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
{
memset(tuple, 0, sizeof(struct tuple6));
const struct layer_private *layer_l3 = NULL;
const struct layer_private *layer_l4 = NULL;
const struct layer_private *layer = NULL;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{
layer = &pkt->layers[i];
// first get L4 layer
if (layer->proto == LAYER_PROTO_UDP)
{
const struct udphdr *udp_hdr = (const struct udphdr *)layer->hdr_ptr;
tuple->ip_proto = IPPROTO_UDP;
tuple->src_port = udp_hdr->uh_sport;
tuple->dst_port = udp_hdr->uh_dport;
layer_l4 = layer;
continue;
}
if (layer->proto == LAYER_PROTO_TCP)
{
const struct tcphdr *tcp_hdr = (const struct tcphdr *)layer->hdr_ptr;
tuple->ip_proto = IPPROTO_TCP;
tuple->src_port = tcp_hdr->th_sport;
tuple->dst_port = tcp_hdr->th_dport;
layer_l4 = layer;
continue;
}
// second get L3 layer
if (layer->proto == LAYER_PROTO_IPV4)
{
const struct ip *ip4_hdr = (const struct ip *)layer->hdr_ptr;
tuple->addr_family = AF_INET;
tuple->src_addr.v4 = ip4_hdr_get_src_in_addr(ip4_hdr);
tuple->dst_addr.v4 = ip4_hdr_get_dst_in_addr(ip4_hdr);
layer_l3 = layer;
break;
}
if (layer->proto == LAYER_PROTO_IPV6)
{
const struct ip6_hdr *ip6_hdr = (const struct ip6_hdr *)layer->hdr_ptr;
tuple->addr_family = AF_INET6;
tuple->src_addr.v6 = ip6_hdr_get_src_in6_addr(ip6_hdr);
tuple->dst_addr.v6 = ip6_hdr_get_dst_in6_addr(ip6_hdr);
layer_l3 = layer;
break;
}
}
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
{
tuple->domain = packet_get_domain(pkt);
return 0;
}
else
{
return -1;
}
}
// return 0 : found
// return -1 : not found
int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
{
memset(tuple, 0, sizeof(struct tuple6));
const struct layer_private *layer_l3 = NULL;
const struct layer_private *layer_l4 = NULL;
const struct layer_private *layer = NULL;
for (int8_t i = 0; i < pkt->layers_used; i++)
{
layer = &pkt->layers[i];
// first get L3 layer
if (layer->proto == LAYER_PROTO_IPV4)
{
const struct ip *ip4_hdr = (const struct ip *)layer->hdr_ptr;
tuple->addr_family = AF_INET;
tuple->src_addr.v4 = ip4_hdr_get_src_in_addr(ip4_hdr);
tuple->dst_addr.v4 = ip4_hdr_get_dst_in_addr(ip4_hdr);
layer_l3 = layer;
continue;
}
if (layer->proto == LAYER_PROTO_IPV6)
{
const struct ip6_hdr *ip6_hdr = (const struct ip6_hdr *)layer->hdr_ptr;
tuple->addr_family = AF_INET6;
tuple->src_addr.v6 = ip6_hdr_get_src_in6_addr(ip6_hdr);
tuple->dst_addr.v6 = ip6_hdr_get_dst_in6_addr(ip6_hdr);
layer_l3 = layer;
continue;
}
// second get L4 layer
if (layer->proto == LAYER_PROTO_UDP)
{
const struct udphdr *udp_hdr = (const struct udphdr *)layer->hdr_ptr;
tuple->ip_proto = IPPROTO_UDP;
tuple->src_port = udp_hdr->uh_sport;
tuple->dst_port = udp_hdr->uh_dport;
layer_l4 = layer;
break;
}
if (layer->proto == LAYER_PROTO_TCP)
{
const struct tcphdr *tcp_hdr = (const struct tcphdr *)layer->hdr_ptr;
tuple->ip_proto = IPPROTO_TCP;
tuple->src_port = tcp_hdr->th_sport;
tuple->dst_port = tcp_hdr->th_dport;
layer_l4 = layer;
break;
}
}
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
{
tuple->domain = packet_get_domain(pkt);
return 0;
}
else
{
return -1;
}
}
/******************************************************************************
* layer uitls
******************************************************************************/
int packet_get_layer_count(const struct packet *pkt)
{
return pkt->layers_used;
}
const struct layer *packet_get_layer_by_idx(const struct packet *pkt, int idx)
{
const struct layer_private *layer = packet_get_layer(pkt, idx);
if (layer == NULL)
{
return NULL;
}
else
{
return (const struct layer *)layer;
}
}
const struct layer_private *packet_get_layer(const struct packet *pkt, int idx)
{
if (idx < 0 || idx >= pkt->layers_used)
{
return NULL;
}
return &pkt->layers[idx];
}
const struct layer_private *packet_get_innermost_layer(const struct packet *pkt, enum layer_proto proto)
{
const struct layer_private *layer = NULL;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{
layer = &pkt->layers[i];
if (layer->proto == proto)
{
return layer;
}
}
return NULL;
}
const struct layer_private *packet_get_outermost_layer(const struct packet *pkt, enum layer_proto proto)
{
const struct layer_private *layer = NULL;
for (int8_t i = 0; i < pkt->layers_used; i++)
{
layer = &pkt->layers[i];
if (layer->proto == proto)
{
return layer;
}
}
return NULL;
}
/******************************************************************************
* tunnel uitls
******************************************************************************/
struct tunnel_detector
{
enum tunnel_type type;
int contain_layers;
int (*identify_func)(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2);
};
static int is_ipv4_tunnel(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2 __attribute__((unused)))
{
if (curr && curr->proto == LAYER_PROTO_IPV4 &&
next1 && (next1->proto == LAYER_PROTO_IPV4 || next1->proto == LAYER_PROTO_IPV6))
{
return 1;
}
return 0;
}
static int is_ipv6_tunnel(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2 __attribute__((unused)))
{
if (curr && curr->proto == LAYER_PROTO_IPV6 &&
next1 && (next1->proto == LAYER_PROTO_IPV4 || next1->proto == LAYER_PROTO_IPV6))
{
return 1;
}
return 0;
}
static int is_gre_tunnel(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2 __attribute__((unused)))
{
if (curr && (curr->proto == LAYER_PROTO_IPV4 || curr->proto == LAYER_PROTO_IPV6) &&
next1 && next1->proto == LAYER_PROTO_GRE)
{
return 1;
}
return 0;
}
static int is_gtp_tunnel(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2)
{
if (curr && (curr->proto == LAYER_PROTO_IPV4 || curr->proto == LAYER_PROTO_IPV6) &&
next1 && next1->proto == LAYER_PROTO_UDP &&
next2 && next2->proto == LAYER_PROTO_GTP_U)
{
return 1;
}
return 0;
}
static int is_vxlan_tunnel(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2)
{
if (curr && (curr->proto == LAYER_PROTO_IPV4 || curr->proto == LAYER_PROTO_IPV6) &&
next1 && next1->proto == LAYER_PROTO_UDP &&
next2 && next2->proto == LAYER_PROTO_VXLAN)
{
return 1;
}
return 0;
}
static int is_l2tp_tunnel(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2)
{
if (curr && (curr->proto == LAYER_PROTO_IPV4 || curr->proto == LAYER_PROTO_IPV6) &&
next1 && next1->proto == LAYER_PROTO_UDP &&
next2 && next2->proto == LAYER_PROTO_L2TP)
{
return 1;
}
return 0;
}
static int is_teredo_tunnel(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2)
{
if (curr && curr->proto == LAYER_PROTO_IPV4 &&
next1 && next1->proto == LAYER_PROTO_UDP &&
next2 && next2->proto == LAYER_PROTO_IPV6)
{
return 1;
}
return 0;
}
static struct tunnel_detector detectors[] = {
{TUNNEL_IPV4, 1, is_ipv4_tunnel},
{TUNNEL_IPV6, 1, is_ipv6_tunnel},
{TUNNEL_GRE, 2, is_gre_tunnel},
{TUNNEL_GTP, 3, is_gtp_tunnel},
{TUNNEL_VXLAN, 3, is_vxlan_tunnel},
{TUNNEL_L2TP, 3, is_l2tp_tunnel},
{TUNNEL_TEREDO, 2, is_teredo_tunnel},
};
// return index of detectors
static int detect_tunnel(const struct layer_private *curr, const struct layer_private *next1, const struct layer_private *next2)
{
for (int i = 0; i < (int)(sizeof(detectors) / sizeof(detectors[0])); i++)
{
if (detectors[i].identify_func(curr, next1, next2))
{
return i;
}
}
return -1;
}
int packet_get_tunnel_count(const struct packet *pkt)
{
int count = 0;
int used = packet_get_layer_count(pkt);
const struct layer_private *curr = NULL;
const struct layer_private *next1 = NULL;
const struct layer_private *next2 = NULL;
for (int i = 0; i < used; i++)
{
curr = packet_get_layer(pkt, i);
next1 = packet_get_layer(pkt, i + 1);
next2 = packet_get_layer(pkt, i + 2);
if (detect_tunnel(curr, next1, next2) >= 0)
{
count++;
}
}
return count;
}
// return 0: success 
// return -1: failed
int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *out)
{
int ret = -1;
int count = 0;
int used = packet_get_layer_count(pkt);
const struct layer_private *curr = NULL;
const struct layer_private *next1 = NULL;
const struct layer_private *next2 = NULL;
memset(out, 0, sizeof(struct tunnel));
for (int i = 0; i < used; i++)
{
curr = packet_get_layer(pkt, i);
next1 = packet_get_layer(pkt, i + 1);
next2 = packet_get_layer(pkt, i + 2);
ret = detect_tunnel(curr, next1, next2);
if (ret >= 0 && count++ == idx)
{
struct tunnel_detector *hit = &detectors[ret];
out->type = hit->type;
out->layer_count = hit->contain_layers;
if (out->layer_count >= 1)
out->layers[0] = (const struct layer *)curr;
if (out->layer_count >= 2)
out->layers[1] = (const struct layer *)next1;
if (out->layer_count >= 3)
out->layers[2] = (const struct layer *)next2;
return 0;
}
}
return -1;
}
/******************************************************************************
* load balance uitls
******************************************************************************/
uint64_t packet_ldbc_hash(const struct packet *pkt, enum packet_load_balance_method method, enum packet_direction direction)
{
uint64_t temp = 0;
uint64_t hash_value = 1;
int inner_addr_len = 0;
int outer_addr_len = 0;
const char *inner_src_addr = NULL;
const char *inner_dst_addr = NULL;
const char *outer_src_addr = NULL;
const char *outer_dst_addr = NULL;
struct tuple2 inner_addr;
struct tuple2 outer_addr;
if (pkt == NULL)
{
return hash_value;
}
if (packet_get_innermost_tuple2(pkt, &inner_addr) == -1)
{
return hash_value;
}
if (packet_get_outermost_tuple2(pkt, &outer_addr) == -1)
{
return hash_value;
}
if (inner_addr.addr_family == AF_INET)
{
inner_src_addr = (const char *)&inner_addr.src_addr.v4;
inner_dst_addr = (const char *)&inner_addr.dst_addr.v4;
inner_addr_len = sizeof(struct in_addr);
}
else
{
inner_src_addr = (const char *)&inner_addr.src_addr.v6;
inner_dst_addr = (const char *)&inner_addr.dst_addr.v6;
inner_addr_len = sizeof(struct in6_addr);
}
if (outer_addr.addr_family == AF_INET)
{
outer_src_addr = (const char *)&outer_addr.src_addr.v4;
outer_dst_addr = (const char *)&outer_addr.dst_addr.v4;
outer_addr_len = sizeof(struct in_addr);
}
else
{
outer_src_addr = (const char *)&outer_addr.src_addr.v6;
outer_dst_addr = (const char *)&outer_addr.dst_addr.v6;
outer_addr_len = sizeof(struct in6_addr);
}
switch (method)
{
case PKT_LDBC_METH_OUTERMOST_INT_IP:
if (direction == PACKET_DIRECTION_INCOMING)
{
// direction 1: E2I
HASH_VALUE(outer_dst_addr, outer_addr_len, hash_value);
}
else
{
// direction 0: I2E
HASH_VALUE(outer_src_addr, outer_addr_len, hash_value);
}
break;
case PKT_LDBC_METH_OUTERMOST_EXT_IP:
if (direction == PACKET_DIRECTION_INCOMING)
{
// direction 1: E2I
HASH_VALUE(outer_src_addr, outer_addr_len, hash_value);
}
else
{
// direction 0: I2E
HASH_VALUE(outer_dst_addr, outer_addr_len, hash_value);
}
break;
case PKT_LDBC_METH_OUTERMOST_INT_EXT_IP:
HASH_VALUE(outer_src_addr, outer_addr_len, hash_value);
HASH_VALUE(outer_dst_addr, outer_addr_len, temp);
hash_value = hash_value ^ temp;
break;
case PKT_LDBC_METH_INNERMOST_INT_IP:
if (direction == PACKET_DIRECTION_INCOMING)
{
// direction 1: E2I
HASH_VALUE(inner_dst_addr, inner_addr_len, hash_value);
}
else
{
// direction 0: I2E
HASH_VALUE(inner_src_addr, inner_addr_len, hash_value);
}
break;
case PKT_LDBC_METH_INNERMOST_EXT_IP:
if (direction == PACKET_DIRECTION_INCOMING)
{
// direction 1: E2I
HASH_VALUE(inner_src_addr, inner_addr_len, hash_value);
}
else
{
// direction 0: I2E
HASH_VALUE(inner_dst_addr, inner_addr_len, hash_value);
}
break;
default:
return hash_value;
}
return hash_value;
}
/******************************************************************************
* other uitls
******************************************************************************/
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 == NULL || pkt->layers_used == 0)
{
return NULL;
}
if (pkt->layers[pkt->layers_used - 1].pld_len)
{
return pkt->layers[pkt->layers_used - 1].pld_ptr;
}
else
{
return NULL;
}
}
uint16_t packet_get_payload_len(const struct packet *pkt)
{
if (pkt == NULL || pkt->layers_used == 0)
{
return 0;
}
return pkt->layers[pkt->layers_used - 1].pld_len;
}
struct packet *packet_new(uint16_t pkt_len)
{
struct packet *pkt = (struct packet *)calloc(1, sizeof(struct packet) + pkt_len);
if (pkt == NULL)
{
return NULL;
}
pkt->data_len = pkt_len;
pkt->data_ptr = (const char *)pkt + sizeof(struct packet);
pkt->need_free = 1;
return pkt;
}
struct packet *packet_dup(const struct packet *pkt)
{
if (pkt == NULL)
{
return NULL;
}
struct packet *dup_pkt = packet_new(pkt->data_len);
if (dup_pkt == NULL)
{
return NULL;
}
dup_pkt->layers_used = pkt->layers_used;
dup_pkt->layers_size = pkt->layers_size;
memcpy((char *)dup_pkt->data_ptr, pkt->data_ptr, pkt->data_len);
dup_pkt->data_len = pkt->data_len;
packet_set_action(dup_pkt, PACKET_ACTION_DROP);
for (int8_t i = 0; i < pkt->layers_used; i++)
{
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].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_len = pkt->layers[i].hdr_len;
dup_pkt->layers[i].pld_len = pkt->layers[i].pld_len;
}
// update frag_layer
if (pkt->frag_layer)
{
dup_pkt->frag_layer = &dup_pkt->layers[pkt->frag_layer - pkt->layers];
}
memcpy(&dup_pkt->meta, &pkt->meta, sizeof(struct metadata));
packet_set_origin_ctx(dup_pkt, (void *)NULL);
return dup_pkt;
}
void packet_free(struct packet *pkt)
{
if (pkt && pkt->need_free)
{
free((void *)pkt);
}
}
int packet_is_fragment(const struct packet *pkt)
{
return (pkt->frag_layer) ? 1 : 0;
}

View File

@@ -0,0 +1,77 @@
add_executable(gtest_tunnel gtest_tunnel.cpp)
target_link_libraries(gtest_tunnel packet_manager gtest)
add_executable(gtest_udp_utils gtest_udp_utils.cpp)
target_link_libraries(gtest_udp_utils packet_manager gtest)
add_executable(gtest_tcp_utils gtest_tcp_utils.cpp)
target_link_libraries(gtest_tcp_utils packet_manager gtest)
add_executable(gtest_ip4_utils gtest_ip4_utils.cpp)
target_link_libraries(gtest_ip4_utils packet_manager gtest)
add_executable(gtest_ip6_utils gtest_ip6_utils.cpp)
target_link_libraries(gtest_ip6_utils packet_manager gtest)
add_executable(gtest_mpls_utils gtest_mpls_utils.cpp)
target_link_libraries(gtest_mpls_utils packet_manager gtest)
add_executable(gtest_eth_utils gtest_eth_utils.cpp)
target_link_libraries(gtest_eth_utils packet_manager gtest)
add_executable(gtest_vlan_utils gtest_vlan_utils.cpp)
target_link_libraries(gtest_vlan_utils packet_manager gtest)
add_executable(gtest_vxlan_utils gtest_vxlan_utils.cpp)
target_link_libraries(gtest_vxlan_utils packet_manager gtest)
add_executable(gtest_gre0_utils gtest_gre0_utils.cpp)
target_link_libraries(gtest_gre0_utils packet_manager gtest)
add_executable(gtest_gre1_utils gtest_gre1_utils.cpp)
target_link_libraries(gtest_gre1_utils packet_manager gtest)
add_executable(gtest_l2tp_utils gtest_l2tp_utils.cpp)
target_link_libraries(gtest_l2tp_utils packet_manager gtest)
add_executable(gtest_gtp1_utils gtest_gtp1_utils.cpp)
target_link_libraries(gtest_gtp1_utils packet_manager gtest)
add_executable(gtest_gtp2_utils gtest_gtp2_utils.cpp)
target_link_libraries(gtest_gtp2_utils packet_manager gtest)
add_executable(gtest_packet_frag gtest_packet_frag.cpp)
target_link_libraries(gtest_packet_frag packet_manager gtest)
add_executable(gtest_packet_parser gtest_packet_parser.cpp)
target_link_libraries(gtest_packet_parser packet_manager gtest)
add_executable(gtest_packet_builder gtest_packet_builder.cpp)
target_link_libraries(gtest_packet_builder packet_manager gtest)
add_executable(gtest_packet_filter gtest_packet_filter.cpp)
target_link_libraries(gtest_packet_filter packet_manager gtest)
add_executable(gtest_packet_ldbc gtest_packet_ldbc.cpp)
target_link_libraries(gtest_packet_ldbc packet_manager gtest)
include(GoogleTest)
gtest_discover_tests(gtest_tunnel)
gtest_discover_tests(gtest_udp_utils)
gtest_discover_tests(gtest_tcp_utils)
gtest_discover_tests(gtest_ip4_utils)
gtest_discover_tests(gtest_ip6_utils)
gtest_discover_tests(gtest_mpls_utils)
gtest_discover_tests(gtest_eth_utils)
gtest_discover_tests(gtest_vlan_utils)
gtest_discover_tests(gtest_vxlan_utils)
gtest_discover_tests(gtest_gre0_utils)
gtest_discover_tests(gtest_gre1_utils)
gtest_discover_tests(gtest_l2tp_utils)
gtest_discover_tests(gtest_gtp1_utils)
gtest_discover_tests(gtest_gtp2_utils)
gtest_discover_tests(gtest_packet_frag)
gtest_discover_tests(gtest_packet_parser)
gtest_discover_tests(gtest_packet_builder)
gtest_discover_tests(gtest_packet_filter)
gtest_discover_tests(gtest_packet_ldbc)

View File

@@ -0,0 +1,49 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* Ethernet II, Src: 00:00:00_00:04:36 (00:00:00:00:04:36), Dst: 18:10:04:00:03:1f (18:10:04:00:03:1f)
* Destination: 18:10:04:00:03:1f (18:10:04:00:03:1f)
* Address: 18:10:04:00:03:1f (18:10:04:00:03:1f)
* .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
* .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
* Source: 00:00:00_00:04:36 (00:00:00:00:04:36)
* Address: 00:00:00_00:04:36 (00:00:00:00:04:36)
* .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
* .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
* Type: MPLS label switched packet (0x8847)
*/
unsigned char data[] = {
0x18, 0x10, 0x04, 0x00, 0x03, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x04, 0x36, 0x88, 0x47};
TEST(ETH_UTILS, GET)
{
const struct ethhdr *hdr = (struct ethhdr *)data;
char dest[18] = {0};
char source[18] = {0};
EXPECT_TRUE(eth_hdr_get_dest(hdr, dest, sizeof(dest)) == 17);
EXPECT_TRUE(eth_hdr_get_source(hdr, source, sizeof(source)) == 17);
EXPECT_TRUE(memcmp(dest, "18:10:04:00:03:1f", 17) == 0);
EXPECT_TRUE(memcmp(source, "00:00:00:00:04:36", 17) == 0);
EXPECT_TRUE(eth_hdr_get_proto(hdr) == ETH_P_MPLS_UC);
}
TEST(ETH_UTILS, SET)
{
char buff[14] = {0};
struct ethhdr *hdr = (struct ethhdr *)buff;
eth_hdr_set_dest(hdr, "18:10:04:00:03:1f");
eth_hdr_set_source(hdr, "00:00:00:00:04:36");
eth_hdr_set_proto(hdr, ETH_P_MPLS_UC);
EXPECT_TRUE(memcmp(buff, data, 14) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,210 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* Generic Routing Encapsulation (IP)
* Flags and Version: 0x0000
* 0... .... .... .... = Checksum Bit: No
* .0.. .... .... .... = Routing Bit: No
* ..0. .... .... .... = Key Bit: No
* ...0 .... .... .... = Sequence Number Bit: No
* .... 0... .... .... = Strict Source Route Bit: No
* .... .000 .... .... = Recursion control: 0
* .... .... 0000 0... = Flags (Reserved): 0
* .... .... .... .000 = Version: GRE (0)
* Protocol Type: IP (0x0800)
*/
unsigned char gre0_no_opt[] = {
0x00, 0x00, 0x08, 0x00};
TEST(GRE0_UTILS, GET_1)
{
const struct gre0_hdr *hdr = (struct gre0_hdr *)gre0_no_opt;
EXPECT_TRUE(gre0_hdr_get_flags(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_checksum_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_routing_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_key_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_seq_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_strict_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_version(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_proto(hdr) == 0x0800);
EXPECT_TRUE(gre0_hdr_get_checksum(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_offset(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_key(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_seq(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_routing_data(hdr) == NULL);
EXPECT_TRUE(gre0_hdr_get_routing_len(hdr) == 0);
EXPECT_TRUE(calc_gre0_hdr_len((const char *)gre0_no_opt, sizeof(gre0_no_opt)) == 4);
}
TEST(GRE0_UTILS, SET_1)
{
char buff[4] = {0};
struct gre0_hdr *hdr = (struct gre0_hdr *)buff;
gre0_hdr_set_flags(hdr, 0);
gre0_hdr_set_checksum_flag(hdr, 0);
gre0_hdr_set_routing_flag(hdr, 0);
gre0_hdr_set_key_flag(hdr, 0);
gre0_hdr_set_seq_flag(hdr, 0);
gre0_hdr_set_strict_flag(hdr, 0);
gre0_hdr_set_version(hdr, 0);
gre0_hdr_set_proto(hdr, 0x0800);
gre0_hdr_set_checksum(hdr, 0);
gre0_hdr_set_offset(hdr, 0);
gre0_hdr_set_key(hdr, 0);
gre0_hdr_set_seq(hdr, 0);
gre0_hdr_set_routing_data(hdr, NULL, 0);
EXPECT_TRUE(memcmp(buff, gre0_no_opt, 4) == 0);
}
/*
* Generic Routing Encapsulation (IP)
* Flags and Version: 0x2000
* 0... .... .... .... = Checksum Bit: No
* .0.. .... .... .... = Routing Bit: No
* ..1. .... .... .... = Key Bit: Yes
* ...0 .... .... .... = Sequence Number Bit: No
* .... 0... .... .... = Strict Source Route Bit: No
* .... .000 .... .... = Recursion control: 0
* .... .... 0000 0... = Flags (Reserved): 0
* .... .... .... .000 = Version: GRE (0)
* Protocol Type: IP (0x0800)
* Key: 0x00000384
*/
unsigned char gre0_opt_key[] = {0x20, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x84};
TEST(GRE0_UTILS, GET_2)
{
const struct gre0_hdr *hdr = (struct gre0_hdr *)gre0_opt_key;
EXPECT_TRUE(gre0_hdr_get_flags(hdr) == 0x2000);
EXPECT_TRUE(gre0_hdr_get_checksum_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_routing_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_key_flag(hdr) == 1);
EXPECT_TRUE(gre0_hdr_get_seq_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_strict_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_version(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_proto(hdr) == 0x0800);
EXPECT_TRUE(gre0_hdr_get_checksum(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_offset(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_key(hdr) == 0x00000384);
EXPECT_TRUE(gre0_hdr_get_seq(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_routing_data(hdr) == NULL);
EXPECT_TRUE(gre0_hdr_get_routing_len(hdr) == 0);
EXPECT_TRUE(calc_gre0_hdr_len((const char *)gre0_opt_key, sizeof(gre0_opt_key)) == 8);
}
TEST(GRE0_UTILS, SET_2)
{
char buff[8] = {0};
struct gre0_hdr *hdr = (struct gre0_hdr *)buff;
gre0_hdr_set_flags(hdr, 0x2000);
gre0_hdr_set_checksum_flag(hdr, 0);
gre0_hdr_set_routing_flag(hdr, 0);
gre0_hdr_set_key_flag(hdr, 1);
gre0_hdr_set_seq_flag(hdr, 0);
gre0_hdr_set_strict_flag(hdr, 0);
gre0_hdr_set_version(hdr, 0);
gre0_hdr_set_proto(hdr, 0x0800);
gre0_hdr_set_checksum(hdr, 0);
gre0_hdr_set_offset(hdr, 0);
gre0_hdr_set_key(hdr, 0x00000384);
gre0_hdr_set_seq(hdr, 0);
gre0_hdr_set_routing_data(hdr, NULL, 0);
EXPECT_TRUE(memcmp(buff, gre0_opt_key, 8) == 0);
}
/*
* Generic Routing Encapsulation (IP)
* Flags and Version: 0xc000
* 1... .... .... .... = Checksum Bit: Yes
* .1.. .... .... .... = Routing Bit: Yes
* ..0. .... .... .... = Key Bit: No
* ...0 .... .... .... = Sequence Number Bit: No
* .... 0... .... .... = Strict Source Route Bit: No
* .... .000 .... .... = Recursion control: 0
* .... .... 0000 0... = Flags (Reserved): 0
* .... .... .... .000 = Version: GRE (0)
* Protocol Type: IP (0x0800)
* Checksum: 0x0000 incorrect, should be 0xea95
* [Expert Info (Warning/Protocol): Incorrect GRE Checksum [should be 0xea95]]
* [Incorrect GRE Checksum [should be 0xea95]]
* [Severity level: Warning]
* [Group: Protocol]
* [Checksum Status: Bad]
* Offset: 44
* Routing
* Address Family: 2
* SRE Offset: 0
* SRE Length: 44
* Routing Information: 6c696e6b5f696e666f206c696e6b5f696e666f206c696e6b5f696e666f206c696e6b5f696e666f2000000000
* Routing
* Address Family: 0
* SRE Offset: 0
* SRE Length: 0
*/
unsigned char gre0_opt_chek_rout[] = {
0xc0, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x02, 0x00, 0x2c, 0x6c, 0x69, 0x6e, 0x6b, 0x5f, 0x69, 0x6e, 0x66, 0x6f, 0x20, 0x6c, 0x69, 0x6e, 0x6b,
0x5f, 0x69, 0x6e, 0x66, 0x6f, 0x20, 0x6c, 0x69, 0x6e, 0x6b, 0x5f, 0x69, 0x6e, 0x66, 0x6f, 0x20, 0x6c, 0x69, 0x6e, 0x6b, 0x5f, 0x69, 0x6e, 0x66, 0x6f, 0x20,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
TEST(GRE0_UTILS, GET_3)
{
const struct gre0_hdr *hdr = (struct gre0_hdr *)gre0_opt_chek_rout;
EXPECT_TRUE(gre0_hdr_get_flags(hdr) == 0xc000);
EXPECT_TRUE(gre0_hdr_get_checksum_flag(hdr) == 1);
EXPECT_TRUE(gre0_hdr_get_routing_flag(hdr) == 1);
EXPECT_TRUE(gre0_hdr_get_key_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_seq_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_strict_flag(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_version(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_proto(hdr) == 0x0800);
EXPECT_TRUE(gre0_hdr_get_checksum(hdr) == 0x0000);
EXPECT_TRUE(gre0_hdr_get_offset(hdr) == 44);
EXPECT_TRUE(gre0_hdr_get_key(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_seq(hdr) == 0);
EXPECT_TRUE(gre0_hdr_get_routing_data(hdr) == (const char *)gre0_opt_chek_rout + 8);
EXPECT_TRUE(gre0_hdr_get_routing_len(hdr) == 52);
EXPECT_TRUE(calc_gre0_hdr_len((const char *)gre0_opt_chek_rout, sizeof(gre0_opt_chek_rout)) == 60);
}
TEST(GRE0_UTILS, SET_3)
{
char buff[60] = {0};
struct gre0_hdr *hdr = (struct gre0_hdr *)buff;
gre0_hdr_set_flags(hdr, 0xc000);
gre0_hdr_set_checksum_flag(hdr, 1);
gre0_hdr_set_routing_flag(hdr, 1);
gre0_hdr_set_key_flag(hdr, 0);
gre0_hdr_set_seq_flag(hdr, 0);
gre0_hdr_set_strict_flag(hdr, 0);
gre0_hdr_set_version(hdr, 0);
gre0_hdr_set_proto(hdr, 0x0800);
gre0_hdr_set_checksum(hdr, 0x0000);
gre0_hdr_set_offset(hdr, 44);
gre0_hdr_set_key(hdr, 0);
gre0_hdr_set_seq(hdr, 0);
gre0_hdr_set_routing_data(hdr, (const char *)gre0_opt_chek_rout + 8, 52);
EXPECT_TRUE(memcmp(buff, gre0_opt_chek_rout, 60) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,66 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* Generic Routing Encapsulation (PPP)
* Flags and Version: 0x3081
* 0... .... .... .... = Checksum Bit: No
* .0.. .... .... .... = Routing Bit: No
* ..1. .... .... .... = Key Bit: Yes
* ...1 .... .... .... = Sequence Number Bit: Yes
* .... 0... .... .... = Strict Source Route Bit: No
* .... .000 .... .... = Recursion control: 0
* .... .... 1... .... = Acknowledgment: Yes
* .... .... .000 0... = Flags (Reserved): 0
* .... .... .... .001 = Version: Enhanced GRE (1)
* Protocol Type: PPP (0x880b)
* Payload Length: 103
* Call ID: 6016
* Sequence Number: 430001
* Acknowledgment Number: 539254
*/
unsigned char gre1_opt_seq_ack[] = {
0x30, 0x81, 0x88, 0x0b, 0x00, 0x67, 0x17, 0x80, 0x00, 0x06, 0x8f, 0xb1, 0x00, 0x08, 0x3a, 0x76};
TEST(GRE1_UTILS, GET_1)
{
const struct gre1_hdr *hdr = (struct gre1_hdr *)gre1_opt_seq_ack;
EXPECT_TRUE(gre1_hdr_get_flags(hdr) == 0x3081);
EXPECT_TRUE(gre1_hdr_get_seq_flag(hdr) == 1);
EXPECT_TRUE(gre1_hdr_get_ack_flag(hdr) == 1);
EXPECT_TRUE(gre1_hdr_get_version(hdr) == 1);
EXPECT_TRUE(gre1_hdr_get_proto(hdr) == 0x880b);
EXPECT_TRUE(gre1_hdr_get_payload_length(hdr) == 103);
EXPECT_TRUE(gre1_hdr_get_call_id(hdr) == 6016);
EXPECT_TRUE(gre1_hdr_get_seq(hdr) == 430001);
EXPECT_TRUE(gre1_hdr_get_ack(hdr) == 539254);
EXPECT_TRUE(calc_gre1_hdr_len((const char *)gre1_opt_seq_ack, sizeof(gre1_opt_seq_ack)) == 16);
}
TEST(GRE1_UTILS, SET_1)
{
char buff[16] = {0};
struct gre1_hdr *hdr = (struct gre1_hdr *)buff;
gre1_hdr_set_flags(hdr, 0x3081);
gre1_hdr_set_seq_flag(hdr, 1);
gre1_hdr_set_ack_flag(hdr, 1);
gre1_hdr_set_version(hdr, 1);
gre1_hdr_set_proto(hdr, 0x880b);
gre1_hdr_set_payload_length(hdr, 103);
gre1_hdr_set_call_id(hdr, 6016);
gre1_hdr_set_seq(hdr, 430001);
gre1_hdr_set_ack(hdr, 539254);
EXPECT_TRUE(memcmp(buff, gre1_opt_seq_ack, 16) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,318 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* User Datagram Protocol, Src Port: 2123, Dst Port: 2123
* GPRS Tunneling Protocol
* Flags: 0x32
* 001. .... = Version: GTP release 99 version (1)
* ...1 .... = Protocol type: GTP (1)
* .... 0... = Reserved: 0
* .... .0.. = Is Next Extension Header present?: No
* .... ..1. = Is Sequence Number present?: Yes
* .... ...0 = Is N-PDU number present?: No
* Message Type: Create PDP context request (0x10)
* Length: 151
* TEID: 0x00000000 (0)
* Sequence number: 0x00fe (254)
* IMSI: 27203
* [Association IMSI: 27203]
* Mobile Country Code (MCC): Ireland (272)
* Mobile Network Code (MNC): Eircom Ltd (03)
* Routing Area Identity
* Mobile Country Code (MCC): Ireland (272)
* Mobile Network Code (MNC): Eircom Ltd (03)
* Location Area Code (LAC): 65534
* Routing Area Code (RAC): 255
* Recovery: 93
* Selection mode: MS or network provided APN, subscribed verified
* .... ..00 = Selection mode: MS or network provided APN, subscribed verified (0)
* TEID Data I: 0x372f0000 (925827072)
* TEID Control Plane: 0x372f0000 (925827072)
* NSAPI: 5
* .... 0101 = NSAPI: 5
* End user address (IETF/IPv4)
* Length: 2
* PDP type organization: IETF (1)
* PDP type number: IPv4 (0x21)
* Access Point Name: mms.mymeteor.ie
* APN length: 16
* APN: mms.mymeteor.ie
* Protocol configuration options
* Length: 34
* [Link direction: MS to network (0)]
* 1... .... = Extension: True
* .... .000 = Configuration Protocol: PPP for use with IP PDP type or IP PDN type (0)
* Protocol or Container ID: Password Authentication Protocol (0xc023)
* Length: 0x0b (11)
* PPP Password Authentication Protocol
* Code: Authenticate-Request (1)
* Identifier: 0
* Length: 11
* Data
* Peer-ID-Length: 2
* Peer-ID: my
* Password-Length: 3
* Password: wap
* Protocol or Container ID: Internet Protocol Control Protocol (0x8021)
* Length: 0x10 (16)
* PPP IP Control Protocol
* Code: Configuration Request (1)
* Identifier: 0 (0x00)
* Length: 16
* Options: (12 bytes), Primary DNS Server IP Address, Secondary DNS Server IP Address
* Primary DNS Server IP Address
* Type: Primary DNS Server IP Address (129)
* Length: 6
* Primary DNS Address: 0.0.0.0
* Secondary DNS Server IP Address
* Type: Secondary DNS Server IP Address (131)
* Length: 6
* Secondary DNS Address: 0.0.0.0
* GSN address : 212.129.65.13
* GSN address length: 4
* GSN address IPv4: 212.129.65.13
* GSN address : 212.129.65.23
* GSN address length: 4
* GSN address IPv4: 212.129.65.23
* MS international PSTN/ISDN number
* Length: 7
* 1... .... = Extension: No Extension
* .001 .... = Nature of number: International Number (0x1)
* .... 0001 = Number plan: ISDN/Telephony Numbering (Rec ITU-T E.164) (0x1)
* E.164 number (MSISDN): 353800000000
* Country Code: Ireland (353)
* Quality of Service
* Length: 12
* Allocation/Retention priority: 2
* 00.. .... = Spare: 0
* ..10 0... = QoS delay: Delay class 4 (best effort) (4)
* .... .011 = QoS reliability: Unacknowledged GTP/LLC, Ack RLC, Protected data (3)
* 0110 .... = QoS peak: Up to 32 000 oct/s (6)
* .... 0... = Spare: 0
* .... .010 = QoS precedence: Normal priority (2)
* 000. .... = Spare: 0
* ...1 1111 = QoS mean: Best effort (31)
* 100. .... = Traffic class: Background class (4)
* ...1 0... = Delivery order: Without delivery order ('no') (2)
* .... .011 = Delivery of erroneous SDU: Erroneous SDUs are not delivered ('no') (3)
* Maximum SDU size: 1500 octets
* Maximum bit rate for uplink: 256 kbps
* Maximum bit rate for downlink: 256 kbps
* 0111 .... = Residual BER: 1/100 000 = 1x10^-5 (7)
* .... 0100 = SDU Error ratio: 1/10 000 = 1x10^-4 (4)
* 1111 10.. = Transfer delay: 4000 ms (62)
* .... ..11 = Traffic handling priority: Priority level 3 (3)
* Guaranteed bit rate for uplink: 0 kbps (255)
* Guaranteed bit rate for downlink: 0 kbps (255)
* RAT Type: GERAN
* Length: 1
* RAT Type: GERAN (2)
* IMEI(SV): 3598100185893512
* Length: 8
* IMEI(SV): 3598100185893512
* [Response In: 2]
*/
unsigned char gtp1_c[] = {
0x32, 0x10, 0x00, 0x97, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x02, 0x72, 0x02, 0xf3, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x72, 0xf2, 0x30, 0xff,
0xfe, 0xff, 0x0e, 0x5d, 0x0f, 0xfc, 0x10, 0x37, 0x2f, 0x00, 0x00, 0x11, 0x37, 0x2f, 0x00, 0x00, 0x14, 0x05, 0x80, 0x00, 0x02, 0xf1, 0x21, 0x83, 0x00, 0x10,
0x03, 0x6d, 0x6d, 0x73, 0x08, 0x6d, 0x79, 0x6d, 0x65, 0x74, 0x65, 0x6f, 0x72, 0x02, 0x69, 0x65, 0x84, 0x00, 0x22, 0x80, 0xc0, 0x23, 0x0b, 0x01, 0x00, 0x00,
0x0b, 0x02, 0x6d, 0x79, 0x03, 0x77, 0x61, 0x70, 0x80, 0x21, 0x10, 0x01, 0x00, 0x00, 0x10, 0x81, 0x06, 0x00, 0x00, 0x00, 0x00, 0x83, 0x06, 0x00, 0x00, 0x00,
0x00, 0x85, 0x00, 0x04, 0xd4, 0x81, 0x41, 0x0d, 0x85, 0x00, 0x04, 0xd4, 0x81, 0x41, 0x17, 0x86, 0x00, 0x07, 0x91, 0x53, 0x83, 0x00, 0x00, 0x00, 0x00, 0x87,
0x00, 0x0c, 0x02, 0x23, 0x62, 0x1f, 0x93, 0x96, 0x58, 0x58, 0x74, 0xfb, 0xff, 0xff, 0x97, 0x00, 0x01, 0x02, 0x9a, 0x00, 0x08, 0x53, 0x89, 0x01, 0x10, 0x58,
0x98, 0x53, 0x21};
TEST(GTP1_UTILS, C_GET)
{
const struct gtp1_hdr *hdr = (struct gtp1_hdr *)gtp1_c;
// GTP Fixed Header Length + GTPv1-C Optional Headers + GTPv1-C Extension Headers
EXPECT_TRUE(calc_gtp1_hdr_len((const char *)gtp1_c, sizeof(gtp1_c)) == 12);
EXPECT_TRUE(gtp1_hdr_get_flags(hdr) == 0x32);
EXPECT_TRUE(gtp1_hdr_get_version(hdr) == 1);
EXPECT_TRUE(gtp1_hdr_get_proto(hdr) == 1);
EXPECT_TRUE(gtp1_hdr_get_reserved(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_ext_flag(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_seq_flag(hdr) == 1);
EXPECT_TRUE(gtp1_hdr_get_npdu_flag(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_msg_type(hdr) == 0x10);
EXPECT_TRUE(gtp1_hdr_get_msg_len(hdr) == 151);
EXPECT_TRUE(gtp1_hdr_get_teid(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_seq(hdr) == 254);
EXPECT_TRUE(gtp1_hdr_get_npdu(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_next_ext_type(hdr) == 0);
char buff[1024] = {0};
gtp1_hdr_to_str(hdr, buff, sizeof(buff));
printf("%s\n", buff);
}
TEST(GTP1_UTILS, C_SET)
{
char buff[12] = {0};
struct gtp1_hdr *hdr = (struct gtp1_hdr *)buff;
gtp1_hdr_set_flags(hdr, 0x32);
gtp1_hdr_set_version(hdr, 1);
gtp1_hdr_set_proto(hdr, 1);
gtp1_hdr_set_reserved(hdr, 0);
gtp1_hdr_set_ext_flag(hdr, 0);
gtp1_hdr_set_seq_flag(hdr, 1);
gtp1_hdr_set_npdu_flag(hdr, 0);
gtp1_hdr_set_msg_type(hdr, 0x10);
gtp1_hdr_set_msg_len(hdr, 151);
gtp1_hdr_set_teid(hdr, 0);
gtp1_hdr_set_seq(hdr, 254);
gtp1_hdr_set_npdu(hdr, 0);
gtp1_hdr_set_next_ext_type(hdr, 0);
EXPECT_TRUE(memcmp(buff, gtp1_c, 12) == 0);
}
/*
* User Datagram Protocol, Src Port: 2152, Dst Port: 2152
* GPRS Tunneling Protocol
* Flags: 0x30
* 001. .... = Version: GTP release 99 version (1)
* ...1 .... = Protocol type: GTP (1)
* .... 0... = Reserved: 0
* .... .0.. = Is Next Extension Header present?: No
* .... ..0. = Is Sequence Number present?: No
* .... ...0 = Is N-PDU number present?: No
* Message Type: T-PDU (0xff)
* Length: 40
* TEID: 0x001e849a (2000026)
*/
unsigned char gtp1_u1[] = {
0x30, 0xff, 0x00, 0x28, 0x00, 0x1e, 0x84, 0x9a};
TEST(GTP1_UTILS, U1_GET)
{
const struct gtp1_hdr *hdr = (struct gtp1_hdr *)gtp1_u1;
// GTP Fixed Header Length + GTPv1-U Optional Headers + GTPv1-U Extension Headers
EXPECT_TRUE(calc_gtp1_hdr_len((const char *)gtp1_u1, sizeof(gtp1_u1)) == 8);
EXPECT_TRUE(gtp1_hdr_get_flags(hdr) == 0x30);
EXPECT_TRUE(gtp1_hdr_get_version(hdr) == 1);
EXPECT_TRUE(gtp1_hdr_get_proto(hdr) == 1);
EXPECT_TRUE(gtp1_hdr_get_reserved(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_ext_flag(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_seq_flag(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_npdu_flag(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_msg_type(hdr) == 0xff);
EXPECT_TRUE(gtp1_hdr_get_msg_len(hdr) == 40);
EXPECT_TRUE(gtp1_hdr_get_teid(hdr) == 2000026);
EXPECT_TRUE(gtp1_hdr_get_seq(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_npdu(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_next_ext_type(hdr) == 0);
char buff[1024] = {0};
gtp1_hdr_to_str(hdr, buff, sizeof(buff));
printf("%s\n", buff);
}
TEST(GTP1_UTILS, U1_SET)
{
char buff[8] = {0};
struct gtp1_hdr *hdr = (struct gtp1_hdr *)buff;
gtp1_hdr_set_flags(hdr, 0x30);
gtp1_hdr_set_version(hdr, 1);
gtp1_hdr_set_proto(hdr, 1);
gtp1_hdr_set_reserved(hdr, 0);
gtp1_hdr_set_ext_flag(hdr, 0);
gtp1_hdr_set_seq_flag(hdr, 0);
gtp1_hdr_set_npdu_flag(hdr, 0);
gtp1_hdr_set_msg_type(hdr, 0xff);
gtp1_hdr_set_msg_len(hdr, 40);
gtp1_hdr_set_teid(hdr, 2000026);
gtp1_hdr_set_seq(hdr, 0);
gtp1_hdr_set_npdu(hdr, 0);
gtp1_hdr_set_next_ext_type(hdr, 0);
EXPECT_TRUE(memcmp(buff, gtp1_u1, 8) == 0);
}
/*
* User Datagram Protocol, Src Port: 2152, Dst Port: 2152
* GPRS Tunneling Protocol
* Flags: 0x34
* 001. .... = Version: GTP release 99 version (1)
* ...1 .... = Protocol type: GTP (1)
* .... 0... = Reserved: 0
* .... .1.. = Is Next Extension Header present?: Yes
* .... ..0. = Is Sequence Number present?: No
* .... ...0 = Is N-PDU number present?: No
* Message Type: T-PDU (0xff)
* Length: 445
* TEID: 0x01447453 (21263443)
* Next extension header type: PDU Session container (0x85)
* Extension header (PDU Session container)
* Extension Header Length: 1
* PDU Session Container
* 0001 .... = PDU Type: UL PDU SESSION INFORMATION (1)
* .... 0000 = Spare: 0x0
* 00.. .... = Spare: 0x0
* ..00 0001 = QoS Flow Identifier (QFI): 1
* Next extension header type: No more extension headers (0x00)
*/
unsigned char gtp1_u2[] = {
0x34, 0xff, 0x01, 0xbd, 0x01, 0x44, 0x74, 0x53, 0x00, 0x00, 0x00, 0x85, 0x01, 0x10, 0x01, 0x00};
TEST(GTP1_UTILS, U2_GET)
{
const struct gtp1_hdr *hdr = (struct gtp1_hdr *)gtp1_u2;
// GTP Fixed Header Length + GTPv1-U Optional Headers + GTPv1-U Extension Headers
EXPECT_TRUE(calc_gtp1_hdr_len((const char *)gtp1_u2, sizeof(gtp1_u2)) == 16);
EXPECT_TRUE(gtp1_hdr_get_flags(hdr) == 0x34);
EXPECT_TRUE(gtp1_hdr_get_version(hdr) == 1);
EXPECT_TRUE(gtp1_hdr_get_proto(hdr) == 1);
EXPECT_TRUE(gtp1_hdr_get_reserved(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_ext_flag(hdr) == 1);
EXPECT_TRUE(gtp1_hdr_get_seq_flag(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_npdu_flag(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_msg_type(hdr) == 0xff);
EXPECT_TRUE(gtp1_hdr_get_msg_len(hdr) == 445);
EXPECT_TRUE(gtp1_hdr_get_teid(hdr) == 21263443);
EXPECT_TRUE(gtp1_hdr_get_seq(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_npdu(hdr) == 0);
EXPECT_TRUE(gtp1_hdr_get_next_ext_type(hdr) == 0x85);
char buff[1024] = {0};
gtp1_hdr_to_str(hdr, buff, sizeof(buff));
printf("%s\n", buff);
}
TEST(GTP1_UTILS, U2_SET)
{
char buff[16] = {0};
struct gtp1_hdr *hdr = (struct gtp1_hdr *)buff;
gtp1_hdr_set_flags(hdr, 0x34);
gtp1_hdr_set_version(hdr, 1);
gtp1_hdr_set_proto(hdr, 1);
gtp1_hdr_set_reserved(hdr, 0);
gtp1_hdr_set_ext_flag(hdr, 1);
gtp1_hdr_set_seq_flag(hdr, 0);
gtp1_hdr_set_npdu_flag(hdr, 0);
gtp1_hdr_set_msg_type(hdr, 0xff);
gtp1_hdr_set_msg_len(hdr, 445);
gtp1_hdr_set_teid(hdr, 21263443);
gtp1_hdr_set_seq(hdr, 0);
gtp1_hdr_set_npdu(hdr, 0);
gtp1_hdr_set_next_ext_type(hdr, 0x85);
// not compare the extension header
EXPECT_TRUE(memcmp(buff, gtp1_u2, 12) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,150 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* User Datagram Protocol, Src Port: 2123, Dst Port: 12491
* GPRS Tunneling Protocol V2
* Flags: 0x48
* 010. .... = Version: 2
* ...0 .... = Piggybacking flag (P): 0
* .... 1... = TEID flag (T): 1
* .... .0.. = Message Priority(MP): 0
* Message Type: Delete Session Response (37)
* Message Length: 19
* Tunnel Endpoint Identifier: 0x0e05cd4d (235261261)
* Sequence Number: 0x000ce7 (3303)
* Spare: 0
* Cause : Request accepted (16)
* IE Type: Cause (2)
* IE Length: 2
* 0000 .... = CR flag: 0
* .... 0000 = Instance: 0
* Cause: Request accepted (16)
* 0000 0... = Spare bit(s): 0
* .... .0.. = PCE (PDN Connection IE Error): False
* .... ..0. = BCE (Bearer Context IE Error): False
* .... ...0 = CS (Cause Source): Originated by node sending the message
* Recovery (Restart Counter) : 13
* IE Type: Recovery (Restart Counter) (3)
* IE Length: 1
* 0000 .... = CR flag: 0
* .... 0000 = Instance: 0
* Restart Counter: 13
*/
unsigned char gtp2_c_pkt1[] = {
0x48, 0x25, 0x00, 0x13, 0x0e, 0x05, 0xcd, 0x4d, 0x00, 0x0c, 0xe7, 0x00, 0x02, 0x00, 0x02, 0x00, 0x10, 0x00, 0x03, 0x00, 0x01, 0x00, 0x0d};
// have teid
TEST(GTP2_UTILS, C_GET1)
{
const struct gtp2_hdr *hdr = (struct gtp2_hdr *)gtp2_c_pkt1;
EXPECT_TRUE(calc_gtp2_hdr_len((const char *)gtp2_c_pkt1, sizeof(gtp2_c_pkt1)) == 12);
EXPECT_TRUE(gtp2_hdr_get_flags(hdr) == 0x48);
EXPECT_TRUE(gtp2_hdr_get_version(hdr) == 2);
EXPECT_TRUE(gtp2_hdr_get_piggyback_flag(hdr) == 0);
EXPECT_TRUE(gtp2_hdr_get_teid_flag(hdr) == 1);
EXPECT_TRUE(gtp2_hdr_get_spare_flag(hdr) == 0);
EXPECT_TRUE(gtp2_hdr_get_msg_type(hdr) == 37);
EXPECT_TRUE(gtp2_hdr_get_msg_len(hdr) == 19);
EXPECT_TRUE(gtp2_hdr_get_teid(hdr) == 0x0e05cd4d);
EXPECT_TRUE(gtp2_hdr_get_seq(hdr) == 0x000ce7);
EXPECT_TRUE(gtp2_hdr_get_spare(hdr) == 0);
char buff[1024] = {0};
gtp2_hdr_to_str(hdr, buff, sizeof(buff));
printf("%s\n", buff);
}
TEST(GTP2_UTILS, C_SET1)
{
char buff[12] = {0};
struct gtp2_hdr *hdr = (struct gtp2_hdr *)buff;
gtp2_hdr_set_flags(hdr, 0x48);
gtp2_hdr_set_version(hdr, 2);
gtp2_hdr_set_piggyback_flag(hdr, 0);
gtp2_hdr_set_teid_flag(hdr, 1);
gtp2_hdr_set_spare_flag(hdr, 0);
gtp2_hdr_set_msg_type(hdr, 37);
gtp2_hdr_set_msg_len(hdr, 19);
gtp2_hdr_set_teid(hdr, 0x0e05cd4d);
gtp2_hdr_set_seq(hdr, 0x000ce7);
gtp2_hdr_set_spare(hdr, 0);
EXPECT_TRUE(memcmp(buff, gtp2_c_pkt1, 12) == 0);
}
/*
* User Datagram Protocol, Src Port: 2123, Dst Port: 2123
* GPRS Tunneling Protocol V2
* Flags: 0x40
* 010. .... = Version: 2
* ...0 .... = Piggybacking flag (P): 0
* .... 0... = TEID flag (T): 0
* .... .0.. = Message Priority(MP): 0
* Message Type: Echo Request (1)
* Message Length: 9
* Sequence Number: 0x0041d4 (16852)
* Spare: 0
* Recovery (Restart Counter) : 5
* IE Type: Recovery (Restart Counter) (3)
* IE Length: 1
* 0000 .... = CR flag: 0
* .... 0000 = Instance: 0
* Restart Counter: 5
*/
unsigned char gtp2_c_pkt2[] = {
0x40, 0x01, 0x00, 0x09, 0x00, 0x41, 0xd4, 0x00, 0x03, 0x00, 0x01, 0x00, 0x05};
// no teid
TEST(GTP2_UTILS, C_GET2)
{
const struct gtp2_hdr *hdr = (struct gtp2_hdr *)gtp2_c_pkt2;
EXPECT_TRUE(calc_gtp2_hdr_len((const char *)gtp2_c_pkt2, sizeof(gtp2_c_pkt2)) == 8);
EXPECT_TRUE(gtp2_hdr_get_flags(hdr) == 0x40);
EXPECT_TRUE(gtp2_hdr_get_version(hdr) == 2);
EXPECT_TRUE(gtp2_hdr_get_piggyback_flag(hdr) == 0);
EXPECT_TRUE(gtp2_hdr_get_teid_flag(hdr) == 0);
EXPECT_TRUE(gtp2_hdr_get_spare_flag(hdr) == 0);
EXPECT_TRUE(gtp2_hdr_get_msg_type(hdr) == 1);
EXPECT_TRUE(gtp2_hdr_get_msg_len(hdr) == 9);
EXPECT_TRUE(gtp2_hdr_get_teid(hdr) == 0);
EXPECT_TRUE(gtp2_hdr_get_seq(hdr) == 0x0041d4);
EXPECT_TRUE(gtp2_hdr_get_spare(hdr) == 0);
char buff[1024] = {0};
gtp2_hdr_to_str(hdr, buff, sizeof(buff));
printf("%s\n", buff);
}
TEST(GTP2_UTILS, C_SET2)
{
char buff[8] = {0};
struct gtp2_hdr *hdr = (struct gtp2_hdr *)buff;
gtp2_hdr_set_flags(hdr, 0x40);
gtp2_hdr_set_version(hdr, 2);
gtp2_hdr_set_piggyback_flag(hdr, 0);
gtp2_hdr_set_teid_flag(hdr, 0);
gtp2_hdr_set_spare_flag(hdr, 0);
gtp2_hdr_set_msg_type(hdr, 1);
gtp2_hdr_set_msg_len(hdr, 9);
gtp2_hdr_set_teid(hdr, 0);
gtp2_hdr_set_seq(hdr, 0x0041d4);
gtp2_hdr_set_spare(hdr, 0);
EXPECT_TRUE(memcmp(buff, gtp2_c_pkt2, 8) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,274 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/******************************************************************************
* more fragment
******************************************************************************/
/*
* Internet Protocol Version 4, Src: 192.168.36.103, Dst: 192.168.40.137
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 44
* Identification: 0xffff (65535)
* 001. .... = Flags: 0x1, More fragments
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..1. .... = More fragments: Set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 127
* Protocol: TCP (6)
* Header Checksum: 0x4d8b [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x4d8b]
* Source Address: 192.168.36.103
* Destination Address: 192.168.40.137
* [Reassembled IPv4 in frame: 5]
* Data (24 bytes)
*/
unsigned char data1[] = {0x45, 0x00, 0x00, 0x2c, 0xff, 0xff, 0x20, 0x00, 0x7f, 0x06, 0x4d, 0x8b, 0xc0, 0xa8, 0x24, 0x67, 0xc0, 0xa8, 0x28, 0x89};
TEST(IPV4_UTILS, GET1)
{
const struct ip *hdr = (struct ip *)data1;
EXPECT_TRUE(ip4_hdr_get_version(hdr) == 4);
EXPECT_TRUE(ip4_hdr_get_hdr_len(hdr) == 20);
EXPECT_TRUE(ip4_hdr_get_tos(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_total_len(hdr) == 44);
EXPECT_TRUE(ip4_hdr_get_ipid(hdr) == 65535);
EXPECT_TRUE(ip4_hdr_get_flags(hdr) == 1);
EXPECT_TRUE(ip4_hdr_get_rf_flag(hdr) == false);
EXPECT_TRUE(ip4_hdr_get_df_flag(hdr) == false);
EXPECT_TRUE(ip4_hdr_get_mf_flag(hdr) == true);
EXPECT_TRUE(ip4_hdr_get_frag_offset(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_ttl(hdr) == 127);
EXPECT_TRUE(ip4_hdr_get_proto(hdr) == 6);
EXPECT_TRUE(ip4_hdr_get_checksum(hdr) == 0x4d8b);
EXPECT_TRUE(ip4_hdr_get_src_addr(hdr) == 0xc0a82467);
EXPECT_TRUE(ip4_hdr_get_dst_addr(hdr) == 0xc0a82889);
EXPECT_TRUE(ip4_hdr_get_opt_len(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_opt_data(hdr) == NULL);
}
TEST(IPV4_UTILS, SET1)
{
char buff[20] = {0};
struct ip *hdr = (struct ip *)buff;
ip4_hdr_set_version(hdr, 4);
ip4_hdr_set_hdr_len(hdr, 20);
ip4_hdr_set_tos(hdr, 0);
ip4_hdr_set_total_len(hdr, 44);
ip4_hdr_set_ipid(hdr, 65535);
ip4_hdr_set_frag_offset(hdr, 0);
ip4_hdr_set_ttl(hdr, 127);
ip4_hdr_set_protocol(hdr, 6);
ip4_hdr_set_checksum(hdr, 0x4d8b);
ip4_hdr_set_src_addr(hdr, 0xc0a82467);
ip4_hdr_set_dst_addr(hdr, 0xc0a82889);
ip4_hdr_set_opt_len(hdr, 0);
ip4_hdr_set_opt_data(hdr, NULL);
ip4_hdr_set_flags(hdr, 1);
EXPECT_TRUE(memcmp(buff, data1, 20) == 0);
ip4_hdr_set_rf_flag(hdr, false);
ip4_hdr_set_df_flag(hdr, false);
ip4_hdr_set_mf_flag(hdr, true);
EXPECT_TRUE(memcmp(buff, data1, 20) == 0);
}
/******************************************************************************
* fragment offset
******************************************************************************/
/*
* Frame 5: 60 bytes on wire (480 bits), 60 bytes captured (480 bits)
* Ethernet II, Src: Fortinet_cc:87:22 (e8:1c:ba:cc:87:22), Dst: EvocInte_2f:35:b8 (00:22:46:2f:35:b8)
* Internet Protocol Version 4, Src: 192.168.36.103, Dst: 192.168.40.137
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 44
* Identification: 0xffff (65535)
* 000. .... = Flags: 0x0
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0011 = Fragment Offset: 24
* Time to Live: 127
* Protocol: TCP (6)
* Header Checksum: 0x6d88 [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x6d88]
* Source Address: 192.168.36.103
* Destination Address: 192.168.40.137
* [2 IPv4 Fragments (48 bytes): #4(24), #5(24)]
* [Frame: 4, payload: 0-23 (24 bytes)]
* [Frame: 5, payload: 24-47 (24 bytes)]
* [Fragment count: 2]
* [Reassembled IPv4 length: 48]
* [Reassembled IPv4 data: f4a5270f9107248703d518e75018ff005e9200003132330af4a5270f9107248b03d518e7…]
* Transmission Control Protocol, Src Port: 62629, Dst Port: 9999, Seq: 1, Ack: 1, Len: 28
* Data (28 bytes)
* Data: 3132330af4a5270f9107248b03d518e75018ff00301600006162630a
* [Length: 28]
*/
unsigned char data2[] = {0x45, 0x00, 0x00, 0x2c, 0xff, 0xff, 0x00, 0x03, 0x7f, 0x06, 0x6d, 0x88, 0xc0, 0xa8, 0x24, 0x67, 0xc0, 0xa8, 0x28, 0x89};
TEST(IPV4_UTILS, GET2)
{
const struct ip *hdr = (struct ip *)data2;
EXPECT_TRUE(ip4_hdr_get_version(hdr) == 4);
EXPECT_TRUE(ip4_hdr_get_hdr_len(hdr) == 20);
EXPECT_TRUE(ip4_hdr_get_tos(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_total_len(hdr) == 44);
EXPECT_TRUE(ip4_hdr_get_ipid(hdr) == 65535);
EXPECT_TRUE(ip4_hdr_get_flags(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_rf_flag(hdr) == false);
EXPECT_TRUE(ip4_hdr_get_df_flag(hdr) == false);
EXPECT_TRUE(ip4_hdr_get_mf_flag(hdr) == false);
EXPECT_TRUE(ip4_hdr_get_frag_offset(hdr) == 24);
EXPECT_TRUE(ip4_hdr_get_ttl(hdr) == 127);
EXPECT_TRUE(ip4_hdr_get_proto(hdr) == 6);
EXPECT_TRUE(ip4_hdr_get_checksum(hdr) == 0x6d88);
EXPECT_TRUE(ip4_hdr_get_src_addr(hdr) == 0xc0a82467);
EXPECT_TRUE(ip4_hdr_get_dst_addr(hdr) == 0xc0a82889);
EXPECT_TRUE(ip4_hdr_get_opt_len(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_opt_data(hdr) == NULL);
}
TEST(IPV4_UTILS, SET2)
{
char buff[20] = {0};
struct ip *hdr = (struct ip *)buff;
ip4_hdr_set_version(hdr, 4);
ip4_hdr_set_hdr_len(hdr, 20);
ip4_hdr_set_tos(hdr, 0);
ip4_hdr_set_total_len(hdr, 44);
ip4_hdr_set_ipid(hdr, 65535);
ip4_hdr_set_frag_offset(hdr, 24);
ip4_hdr_set_ttl(hdr, 127);
ip4_hdr_set_protocol(hdr, 6);
ip4_hdr_set_checksum(hdr, 0x6d88);
ip4_hdr_set_src_addr(hdr, 0xc0a82467);
ip4_hdr_set_dst_addr(hdr, 0xc0a82889);
ip4_hdr_set_opt_len(hdr, 0);
ip4_hdr_set_opt_data(hdr, NULL);
ip4_hdr_set_flags(hdr, 0);
EXPECT_TRUE(memcmp(buff, data2, 20) == 0);
ip4_hdr_set_rf_flag(hdr, false);
ip4_hdr_set_df_flag(hdr, false);
ip4_hdr_set_mf_flag(hdr, false);
EXPECT_TRUE(memcmp(buff, data2, 20) == 0);
}
/******************************************************************************
* options
******************************************************************************/
/*
* Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
* 0100 .... = Version: 4
* .... 1111 = Header Length: 60 bytes (15)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 124
* Identification: 0x0000 (0)
* 010. .... = Flags: 0x2, Don't fragment
* 0... .... = Reserved bit: Not set
* .1.. .... = Don't fragment: Set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 64
* Protocol: ICMP (1)
* Header Checksum: 0xfd30 [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0xfd30]
* Source Address: 127.0.0.1
* Destination Address: 127.0.0.1
* Options: (40 bytes), Commercial Security
* IP Option - Commercial Security (40 bytes)
* Type: 134
* 1... .... = Copy on fragmentation: Yes
* .00. .... = Class: Control (0)
* ...0 0110 = Number: Commercial IP security option (6)
* Length: 40
* DOI: 1
* Tag Type: Restrictive Category Bitmap (1)
* Sensitivity Level: 1
* Categories: 0,2,4,5,6,239
*/
unsigned char data3[] = {
0x4f, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x40, 0x00, 0x40, 0x01, 0xfd, 0x30, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x01, 0x86, 0x28, 0x00, 0x00, 0x00, 0x01,
0x01, 0x22, 0x00, 0x01, 0xae, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
TEST(IPV4_UTILS, GET3)
{
const struct ip *hdr = (struct ip *)data3;
EXPECT_TRUE(ip4_hdr_get_version(hdr) == 4);
EXPECT_TRUE(ip4_hdr_get_hdr_len(hdr) == 60);
EXPECT_TRUE(ip4_hdr_get_tos(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_total_len(hdr) == 124);
EXPECT_TRUE(ip4_hdr_get_ipid(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_flags(hdr) == 2);
EXPECT_TRUE(ip4_hdr_get_rf_flag(hdr) == false);
EXPECT_TRUE(ip4_hdr_get_df_flag(hdr) == true);
EXPECT_TRUE(ip4_hdr_get_mf_flag(hdr) == false);
EXPECT_TRUE(ip4_hdr_get_frag_offset(hdr) == 0);
EXPECT_TRUE(ip4_hdr_get_ttl(hdr) == 64);
EXPECT_TRUE(ip4_hdr_get_proto(hdr) == 1);
EXPECT_TRUE(ip4_hdr_get_checksum(hdr) == 0xfd30);
EXPECT_TRUE(ip4_hdr_get_src_addr(hdr) == 0x7f000001);
EXPECT_TRUE(ip4_hdr_get_dst_addr(hdr) == 0x7f000001);
EXPECT_TRUE(ip4_hdr_get_opt_len(hdr) == 40);
EXPECT_TRUE(ip4_hdr_get_opt_data(hdr) == (const char *)(data3 + 20));
}
TEST(IPV4_UTILS, SET3)
{
char buff[60] = {0};
struct ip *hdr = (struct ip *)buff;
ip4_hdr_set_version(hdr, 4);
ip4_hdr_set_hdr_len(hdr, 60);
ip4_hdr_set_tos(hdr, 0);
ip4_hdr_set_total_len(hdr, 124);
ip4_hdr_set_ipid(hdr, 0);
ip4_hdr_set_frag_offset(hdr, 0);
ip4_hdr_set_ttl(hdr, 64);
ip4_hdr_set_protocol(hdr, 1);
ip4_hdr_set_checksum(hdr, 0xfd30);
ip4_hdr_set_src_addr(hdr, 0x7f000001);
ip4_hdr_set_dst_addr(hdr, 0x7f000001);
ip4_hdr_set_opt_len(hdr, 40);
ip4_hdr_set_opt_data(hdr, (const char *)(data3 + 20));
ip4_hdr_set_flags(hdr, 2);
EXPECT_TRUE(memcmp(buff, data3, 60) == 0);
ip4_hdr_set_rf_flag(hdr, false);
ip4_hdr_set_df_flag(hdr, true);
ip4_hdr_set_mf_flag(hdr, false);
EXPECT_TRUE(memcmp(buff, data3, 60) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,104 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* Internet Protocol Version 6, Src: fe80::250:56ff:fe69:dc00, Dst: ff02::1:2
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 00.. .... .... .... .... .... = Differentiated Services Codepoint: Default (0)
* .... .... ..00 .... .... .... .... .... = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 60
* Next Header: UDP (17)
* Hop Limit: 1
* Source Address: fe80::250:56ff:fe69:dc00
* Destination Address: ff02::1:2
* [Source SLAAC MAC: VMware_69:dc:00 (00:50:56:69:dc:00)]
* */
unsigned char data[] = {
0x60, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x11, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x50, 0x56, 0xff, 0xfe, 0x69, 0xdc, 0x00, 0xff, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02};
TEST(IPV6_UTILS, GET)
{
char src_str[INET6_ADDRSTRLEN];
char dst_str[INET6_ADDRSTRLEN];
const struct ip6_hdr *hdr = (struct ip6_hdr *)data;
EXPECT_TRUE(ip6_hdr_get_version(hdr) == 6);
EXPECT_TRUE(ip6_hdr_get_traffic_class(hdr) == 0);
EXPECT_TRUE(ip6_hdr_get_flow_label(hdr) == 0);
EXPECT_TRUE(ip6_hdr_get_payload_len(hdr) == 60);
EXPECT_TRUE(ip6_hdr_get_next_header(hdr) == 17);
EXPECT_TRUE(ip6_hdr_get_hop_limit(hdr) == 1);
struct in6_addr src_addr = ip6_hdr_get_src_in6_addr(hdr);
struct in6_addr dst_addr = ip6_hdr_get_dst_in6_addr(hdr);
inet_ntop(AF_INET6, &src_addr, src_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &dst_addr, dst_str, INET6_ADDRSTRLEN);
EXPECT_TRUE(strcmp(src_str, "fe80::250:56ff:fe69:dc00") == 0);
EXPECT_TRUE(strcmp(dst_str, "ff02::1:2") == 0);
}
TEST(IPV6_UTILS, SET)
{
char buff[40] = {0};
struct in6_addr src_addr;
struct in6_addr dst_addr;
struct ip6_hdr *hdr = (struct ip6_hdr *)buff;
inet_pton(AF_INET6, "fe80::250:56ff:fe69:dc00", &src_addr);
inet_pton(AF_INET6, "ff02::1:2", &dst_addr);
ip6_hdr_set_version(hdr, 6);
ip6_hdr_set_traffic_class(hdr, 0);
ip6_hdr_set_flow_label(hdr, 0);
ip6_hdr_set_payload_len(hdr, 60);
ip6_hdr_set_next_header(hdr, 17);
ip6_hdr_set_hop_limit(hdr, 1);
ip6_hdr_set_src_in6_addr(hdr, src_addr);
ip6_hdr_set_dst_in6_addr(hdr, dst_addr);
EXPECT_TRUE(memcmp(buff, data, 40) == 0);
}
/*
* Fragment Header for IPv6
* Next header: UDP (17)
* Reserved octet: 0x00
* 0000 0101 1010 1... = Offset: 181 (1448 bytes)
* .... .... .... .00. = Reserved bits: 0
* .... .... .... ...1 = More Fragments: Yes
* Identification: 0xf88eb466
*/
unsigned char frag[] = {
0x11, 0x00, 0x05, 0xa9, 0xf8, 0x8e, 0xb4, 0x66};
TEST(IPV6_FRAG_HDR, GET)
{
const struct ip6_frag *hdr = (struct ip6_frag *)frag;
EXPECT_TRUE(ipv6_frag_get_next_header(hdr) == 17);
EXPECT_TRUE(ipv6_frag_get_offset(hdr) == 1448);
EXPECT_TRUE(ipv6_frag_get_more(hdr) == 1);
EXPECT_TRUE(ipv6_frag_get_ident(hdr) == 0xf88eb466);
}
TEST(IPV6_FRAG_HDR, SET)
{
char buff[8] = {0};
struct ip6_frag *hdr = (struct ip6_frag *)buff;
ipv6_frag_set_next_header(hdr, 17);
ipv6_frag_set_offset(hdr, 1448);
ipv6_frag_set_more(hdr, 1);
ipv6_frag_set_ident(hdr, 0xf88eb466);
EXPECT_TRUE(memcmp(buff, frag, 8) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,196 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* Layer 2 Tunneling Protocol
* Flags: 0xc802, Type: Control Message, Length Bit, Sequence Bit
* 1... .... .... .... = Type: Control Message (1)
* .1.. .... .... .... = Length Bit: Length field is present
* .... 1... .... .... = Sequence Bit: Ns and Nr fields are present
* .... ..0. .... .... = Offset bit: Offset size field is not present
* .... ...0 .... .... = Priority: No priority
* .... .... .... 0010 = Version: 2
* Length: 105
* Tunnel ID: 0
* Session ID: 0
* Ns: 0
* Nr: 0
* Control Message AVP
* 1... .... .... .... = Mandatory: True
* .0.. .... .... .... = Hidden: False
* .... ..00 0000 1000 = Length: 8
* Vendor ID: Reserved (0)
* AVP Type: Control Message (0)
* Message Type: Start_Control_Request (1)
* Protocol Version AVP
* 1... .... .... .... = Mandatory: True
* .0.. .... .... .... = Hidden: False
* .... ..00 0000 1000 = Length: 8
* Vendor ID: Reserved (0)
* AVP Type: Protocol Version (2)
* Version: 1
* Revision: 0
* Framing Capabilities AVP
* 1... .... .... .... = Mandatory: True
* .0.. .... .... .... = Hidden: False
* .... ..00 0000 1010 = Length: 10
* Vendor ID: Reserved (0)
* AVP Type: Framing Capabilities (3)
* .... .... .... .... .... .... .... ..0. = Async Framing Supported: False
* .... .... .... .... .... .... .... ...1 = Sync Framing Supported: True
* Bearer Capabilities AVP
* 1... .... .... .... = Mandatory: True
* .0.. .... .... .... = Hidden: False
* .... ..00 0000 1010 = Length: 10
* Vendor ID: Reserved (0)
* AVP Type: Bearer Capabilities (4)
* .... .... .... .... .... .... .... ..0. = Analog Access Supported: False
* .... .... .... .... .... .... .... ...0 = Digital Access Supported: False
* Firmware Revision AVP
* 0... .... .... .... = Mandatory: False
* .0.. .... .... .... = Hidden: False
* .... ..00 0000 1000 = Length: 8
* Vendor ID: Reserved (0)
* AVP Type: Firmware Revision (6)
* Firmware Revision: 1537 (0x0601)
* Host Name AVP
* 1... .... .... .... = Mandatory: True
* .0.. .... .... .... = Hidden: False
* .... ..00 0001 0010 = Length: 18
* Vendor ID: Reserved (0)
* AVP Type: Host Name (7)
* Host Name: IIE-SM-THINK
* Vendor Name AVP
* 0... .... .... .... = Mandatory: False
* .0.. .... .... .... = Hidden: False
* .... ..00 0000 1111 = Length: 15
* Vendor ID: Reserved (0)
* AVP Type: Vendor Name (8)
* Vendor Name: Microsoft
* Assigned Tunnel ID AVP
* 1... .... .... .... = Mandatory: True
* .0.. .... .... .... = Hidden: False
* .... ..00 0000 1000 = Length: 8
* Vendor ID: Reserved (0)
* AVP Type: Assigned Tunnel ID (9)
* Assigned Tunnel ID: 1
* Receive Window Size AVP
* 1... .... .... .... = Mandatory: True
* .0.. .... .... .... = Hidden: False
* .... ..00 0000 1000 = Length: 8
* Vendor ID: Reserved (0)
* AVP Type: Receive Window Size (10)
* Receive Window Size: 8
*/
unsigned char v2_over_udp_ctrl_msg[] = {
0xc8, 0x02, 0x00, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
0x01, 0x00, 0x80, 0x0a, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x80, 0x0a, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
0x00, 0x06, 0x06, 0x01, 0x80, 0x12, 0x00, 0x00, 0x00, 0x07, 0x49, 0x49, 0x45, 0x2d, 0x53, 0x4d, 0x2d, 0x54, 0x48, 0x49, 0x4e, 0x4b, 0x00, 0x0f, 0x00, 0x00,
0x00, 0x08, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x80, 0x08, 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x80, 0x08, 0x00, 0x00, 0x00, 0x0a, 0x00,
0x08};
TEST(L2TPV2_OVER_UDP_UTILS, CRTL_MSG)
{
const struct l2tp_hdr *hdr = (struct l2tp_hdr *)v2_over_udp_ctrl_msg;
EXPECT_TRUE(l2tp_hdr_get_ver(hdr) == 2);
EXPECT_TRUE(l2tp_hdr_get_type(hdr) == 1);
EXPECT_TRUE(calc_udp_l2tpv2_hdr_len((const char *)v2_over_udp_ctrl_msg, sizeof(v2_over_udp_ctrl_msg)) == 105);
}
/*
* Layer 2 Tunneling Protocol
* Flags: 0x4002, Type: Data Message, Length Bit
* 0... .... .... .... = Type: Data Message (0)
* .1.. .... .... .... = Length Bit: Length field is present
* .... 0... .... .... = Sequence Bit: Ns and Nr fields are not present
* .... ..0. .... .... = Offset bit: Offset size field is not present
* .... ...0 .... .... = Priority: No priority
* .... .... .... 0010 = Version: 2
* Length: 78
* Tunnel ID: 28998
* Session ID: 2
*/
unsigned char v2_over_udp_data_msg[] = {
0x40, 0x02, 0x00, 0x4e, 0x71, 0x46, 0x00, 0x02};
TEST(L2TPV2_OVER_UDP_UTILS, DATA_MSG)
{
const struct l2tp_hdr *hdr = (struct l2tp_hdr *)v2_over_udp_data_msg;
EXPECT_TRUE(l2tp_hdr_get_ver(hdr) == 2);
EXPECT_TRUE(l2tp_hdr_get_type(hdr) == 0);
EXPECT_TRUE(calc_udp_l2tpv2_hdr_len((const char *)v2_over_udp_data_msg, sizeof(v2_over_udp_data_msg)) == 8);
}
/*
* TODO
*/
unsigned char v3_over_udp_ctrl_msg[] = {};
TEST(L2TPV3_OVER_UDP_UTILS, CRTL_MSG)
{
// TODO
}
/*
* Layer 2 Tunneling Protocol version 3
* Flags: 0x0003, Type: Data Message
* 0... .... .... .... = Type: Data Message (0)
* .0.. .... .... .... = Length Bit: Length field is not present
* .... 0... .... .... = Sequence Bit: Ns and Nr fields are not present
* .... .... .... 0011 = Version: 3
* Reserved: 0x0000
* Session ID: 0x00000fa0
* [Pseudowire Type: Unknown (0)]
* Cookie: 00000000
*/
unsigned char v3_over_udp_data_msg[] = {
0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xa0, 0x00, 0x00, 0x00, 0x00};
TEST(L2TPV3_OVER_UDP_UTILS, DATA_MSG)
{
const struct l2tp_hdr *hdr = (struct l2tp_hdr *)v3_over_udp_data_msg;
EXPECT_TRUE(l2tp_hdr_get_ver(hdr) == 3);
EXPECT_TRUE(l2tp_hdr_get_type(hdr) == 0);
EXPECT_TRUE(calc_udp_l2tpv3_hdr_len((const char *)v3_over_udp_data_msg, sizeof(v3_over_udp_data_msg)) == 12);
}
/*
* TODO
*/
unsigned char v3_over_ip_ctrl_msg[] = {};
TEST(L2TPV3_OVER_IP_UTILS, CRTL_MSG)
{
// TODO
}
/*
* Layer 2 Tunneling Protocol version 3
* Session ID: 0x00009652
* [Pseudowire Type: Unknown (0)]
* Cookie: ca031078
*/
unsigned char v3_over_ip_data_msg[] = {
0x00, 0x00, 0x96, 0x52, 0xca, 0x03, 0x10, 0x78};
TEST(L2TPV3_OVER_IP_UTILS, DATA_MSG)
{
EXPECT_TRUE(ntohl(*((uint32_t *)v3_over_ip_data_msg)) != 0); // data message
EXPECT_TRUE(calc_ip_l2tpv3_hdr_len((const char *)v3_over_ip_data_msg, sizeof(v3_over_ip_data_msg)) == 8);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,42 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* MultiProtocol Label Switching Header, Label: 779408, Exp: 0, S: 0, TTL: 255
* 1011 1110 0100 1001 0000 .... .... .... = MPLS Label: 779408 (0xbe490)
* .... .... .... .... .... 000. .... .... = MPLS Experimental Bits: 0
* .... .... .... .... .... ...0 .... .... = MPLS Bottom Of Label Stack: 0
* .... .... .... .... .... .... 1111 1111 = MPLS TTL: 255
*/
unsigned char data[] = {
0xbe, 0x49, 0x00, 0xff};
TEST(MPLS_UTILS, GET)
{
const struct mpls_label *hdr = (struct mpls_label *)data;
EXPECT_TRUE(mpls_label_get_label(hdr) == 0xbe490);
EXPECT_TRUE(mpls_label_get_tc(hdr) == 0);
EXPECT_TRUE(mpls_label_get_bos(hdr) == 0);
EXPECT_TRUE(mpls_label_get_ttl(hdr) == 255);
}
TEST(MPLS_UTILS, SET)
{
char buff[4] = {0};
struct mpls_label *hdr = (struct mpls_label *)buff;
mpls_label_set_label(hdr, 0xbe490);
mpls_label_set_tc(hdr, 0);
mpls_label_set_bos(hdr, 0);
mpls_label_set_ttl(hdr, 255);
EXPECT_TRUE(memcmp(buff, data, 4) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,97 @@
#include <gtest/gtest.h>
#include "tuple.h"
#include "packet_private.h"
#include "packet_parser.h"
#include "packet_filter.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};
TEST(DUPLICATED_PACKET_FILTER, TEST)
{
struct packet pkt;
uint32_t capacity = 1000000;
uint32_t timeout = 2;
double error_rate = 0.00001;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)data, sizeof(data));
struct packet_filter *filter = packet_filter_new(capacity, timeout, error_rate, 1);
EXPECT_TRUE(filter != nullptr);
EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 1) == 0); // no found
packet_filter_add(filter, &pkt, 1); // add
EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 1) == 1); // found
EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 2) == 1); // found
EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 3) == 0); // not found
EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 4) == 0); // not found
packet_filter_free(filter);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,315 @@
#include <gtest/gtest.h>
#include "packet_private.h"
#include "packet_parser.h"
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:data]
******************************************************************************
*
* Frame 4: 60 bytes on wire (480 bits), 60 bytes captured (480 bits)
* Ethernet II, Src: Fortinet_cc:87:22 (e8:1c:ba:cc:87:22), Dst: EvocInte_2f:35:b8 (00:22:46:2f:35:b8)
* Destination: EvocInte_2f:35:b8 (00:22:46:2f:35:b8)
* Source: Fortinet_cc:87:22 (e8:1c:ba:cc:87:22)
* Type: IPv4 (0x0800)
* Padding: 0000
* Internet Protocol Version 4, Src: 192.168.36.103, Dst: 192.168.40.137
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 44
* Identification: 0xffff (65535)
* 001. .... = Flags: 0x1, More fragments
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..1. .... = More fragments: Set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 127
* Protocol: TCP (6)
* Header Checksum: 0x4d8b [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x4d8b]
* Source Address: 192.168.36.103
* Destination Address: 192.168.40.137
* [Reassembled IPv4 in frame: 5]
* Data (24 bytes)
* Data: f4a5270f9107248703d518e75018ff005e9200003132330a
* [Length: 24]
*/
unsigned char data1[] = {
0x00, 0x22, 0x46, 0x2f, 0x35, 0xb8, 0xe8, 0x1c, 0xba, 0xcc, 0x87, 0x22, 0x08, 0x00, 0x45, 0x00, 0x00, 0x2c, 0xff, 0xff, 0x20, 0x00, 0x7f, 0x06, 0x4d, 0x8b,
0xc0, 0xa8, 0x24, 0x67, 0xc0, 0xa8, 0x28, 0x89, 0xf4, 0xa5, 0x27, 0x0f, 0x91, 0x07, 0x24, 0x87, 0x03, 0xd5, 0x18, 0xe7, 0x50, 0x18, 0xff, 0x00, 0x5e, 0x92,
0x00, 0x00, 0x31, 0x32, 0x33, 0x0a, 0x00, 0x00};
#if 1
TEST(PACKET_FRAG, IPV4_FRAGMENT)
{
struct packet handler;
memset(&handler, 0, sizeof(handler));
packet_parse(&handler, (const char *)data1, sizeof(data1));
EXPECT_TRUE(packet_is_fragment(&handler) == true);
struct packet *dup = packet_dup(&handler);
EXPECT_TRUE(dup != NULL);
EXPECT_TRUE(packet_is_fragment(dup) == true);
packet_free(dup);
}
#endif
/******************************************************************************
* [Protocols in frame: eth:ethertype:ipv6:ipv6.fraghdr:data]
******************************************************************************
*
* Frame 5: 1510 bytes on wire (12080 bits), 1510 bytes captured (12080 bits)
* Ethernet II, Src: Apple_c0:61:b6 (68:5b:35:c0:61:b6), Dst: Dell_94:65:38 (00:1d:09:94:65:38)
* Destination: Dell_94:65:38 (00:1d:09:94:65:38)
* Source: Apple_c0:61:b6 (68:5b:35:c0:61:b6)
* Type: IPv6 (0x86dd)
* Internet Protocol Version 6, Src: 2607:f010:3f9::1001, Dst: 2607:f010:3f9::11:0
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 00.. .... .... .... .... .... = Differentiated Services Codepoint: Default (0)
* .... .... ..00 .... .... .... .... .... = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* .... 0010 0001 0010 1000 1001 = Flow Label: 0x21289
* Payload Length: 1456
* Next Header: Fragment Header for IPv6 (44)
* Hop Limit: 64
* Source Address: 2607:f010:3f9::1001
* Destination Address: 2607:f010:3f9::11:0
* Fragment Header for IPv6
* Next header: UDP (17)
* Reserved octet: 0x00
* 0000 1011 0101 0... = Offset: 362 (2896 bytes)
* .... .... .... .00. = Reserved bits: 0
* .... .... .... ...1 = More Fragments: Yes
* Identification: 0xf88eb466
* [Reassembled IPv6 in frame: 6]
* Data (1448 bytes)
* Data: 686868686868686868686868686868686868686868686868686868686868686868686868…
* [Length: 1448]
*/
unsigned char data2[] = {
0x00, 0x1d, 0x09, 0x94, 0x65, 0x38, 0x68, 0x5b, 0x35, 0xc0, 0x61, 0xb6, 0x86, 0xdd, 0x60, 0x02, 0x12, 0x89, 0x05, 0xb0, 0x2c, 0x40, 0x26, 0x07, 0xf0, 0x10,
0x03, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0x26, 0x07, 0xf0, 0x10, 0x03, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
0x00, 0x00, 0x11, 0x00, 0x0b, 0x51, 0xf8, 0x8e, 0xb4, 0x66, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
0x68, 0x68};
#if 1
TEST(PACKET_FRAG, IPV6_FRAGMENT)
{
struct packet handler;
memset(&handler, 0, sizeof(handler));
packet_parse(&handler, (const char *)data2, sizeof(data2));
EXPECT_TRUE(packet_is_fragment(&handler) == true);
struct packet *dup = packet_dup(&handler);
EXPECT_TRUE(dup != NULL);
EXPECT_TRUE(packet_is_fragment(dup) == true);
packet_free(dup);
}
#endif
/******************************************************************************
* [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 data3[] = {
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};
#if 1
TEST(PACKET_FRAG, IPV4_IPV6_NOT_FRAGMENT)
{
struct packet handler;
memset(&handler, 0, sizeof(handler));
packet_parse(&handler, (const char *)data3, sizeof(data3));
EXPECT_TRUE(packet_is_fragment(&handler) == false);
struct packet *dup = packet_dup(&handler);
EXPECT_TRUE(dup != NULL);
EXPECT_TRUE(packet_is_fragment(dup) == false);
packet_free(dup);
}
#endif
/******************************************************************************
* [Protocols in frame: eth:ethertype:ipv6:ipv6:udp:data]
******************************************************************************
*
* Frame 1: 106 bytes on wire (848 bits), 106 bytes captured (848 bits)
* Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: Broadcast (ff:ff:ff:ff:ff:ff)
* Destination: Broadcast (ff:ff:ff:ff:ff:ff)
* Source: 00:00:00_00:00:00 (00:00:00:00:00:00)
* Type: IPv6 (0x86dd)
* Internet Protocol Version 6, Src: 2001:4f8:4:7:2e0:81ff:fe52:ffff, Dst: 2001:4f8:4:7:2e0:81ff:fe52:9a6b
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 52
* Next Header: IPv6 (41)
* Hop Limit: 64
* Source Address: 2001:4f8:4:7:2e0:81ff:fe52:ffff
* Destination Address: 2001:4f8:4:7:2e0:81ff:fe52:9a6b
* [Source SLAAC MAC: TyanComp_52:ff:ff (00:e0:81:52:ff:ff)]
* [Destination SLAAC MAC: TyanComp_52:9a:6b (00:e0:81:52:9a:6b)]
* Internet Protocol Version 6, Src: dead::beef, Dst: cafe::babe
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 12
* Next Header: UDP (17)
* Hop Limit: 64
* Source Address: dead::beef
* Destination Address: cafe::babe
* User Datagram Protocol, Src Port: 30000, Dst Port: 13000
* Source Port: 30000
* Destination Port: 13000
* Length: 12
* Checksum: 0x83d2 [unverified]
* [Checksum Status: Unverified]
* [Stream index: 0]
* [Timestamps]
* UDP payload (4 bytes)
* Data (4 bytes)
*/
unsigned char data4[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x34, 0x29, 0x40, 0x20, 0x01, 0x04, 0xf8,
0x00, 0x04, 0x00, 0x07, 0x02, 0xe0, 0x81, 0xff, 0xfe, 0x52, 0xff, 0xff, 0x20, 0x01, 0x04, 0xf8, 0x00, 0x04, 0x00, 0x07, 0x02, 0xe0, 0x81, 0xff, 0xfe, 0x52,
0x9a, 0x6b, 0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x11, 0x40, 0xde, 0xad, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbe, 0xef,
0xca, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, 0xbe, 0x75, 0x30, 0x32, 0xc8, 0x00, 0x0c, 0x83, 0xd2, 0x58, 0x58,
0x58, 0x58};
#if 1
TEST(PACKET_FRAG, IPV6_IPV6_NOT_FRAGMENT)
{
struct packet handler;
memset(&handler, 0, sizeof(handler));
packet_parse(&handler, (const char *)data4, sizeof(data4));
EXPECT_TRUE(packet_is_fragment(&handler) == false);
struct packet *dup = packet_dup(&handler);
EXPECT_TRUE(dup != NULL);
EXPECT_TRUE(packet_is_fragment(dup) == false);
packet_free(dup);
}
#endif
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,96 @@
#include <gtest/gtest.h>
#include <arpa/inet.h>
#include "packet_private.h"
#include "packet_parser.h"
#include "packet_dump.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};
#if 1
TEST(PACKET_LDBC, HASH_VALUE)
{
struct packet handler;
memset(&handler, 0, sizeof(handler));
const char *payload = packet_parse(&handler, (const char *)data, sizeof(data));
EXPECT_TRUE(payload != nullptr);
EXPECT_TRUE((char *)payload - (char *)&data == 106);
packet_print(&handler);
// buffer: "2001:da8:200:900e:200:5efe:d24d:58a3 0 2600:140e:6::1702:1058 0"
// buffer: "210.77.88.163 0 59.66.4.50 0"
EXPECT_TRUE(packet_ldbc_hash(&handler, PKT_LDBC_METH_OUTERMOST_INT_IP, PACKET_DIRECTION_INCOMING) == packet_ldbc_hash(&handler, PKT_LDBC_METH_OUTERMOST_EXT_IP, PACKET_DIRECTION_OUTGOING));
EXPECT_TRUE(packet_ldbc_hash(&handler, PKT_LDBC_METH_OUTERMOST_EXT_IP, PACKET_DIRECTION_INCOMING) == packet_ldbc_hash(&handler, PKT_LDBC_METH_OUTERMOST_INT_IP, PACKET_DIRECTION_OUTGOING));
EXPECT_TRUE(packet_ldbc_hash(&handler, PKT_LDBC_METH_OUTERMOST_INT_EXT_IP, PACKET_DIRECTION_INCOMING) == packet_ldbc_hash(&handler, PKT_LDBC_METH_OUTERMOST_INT_EXT_IP, PACKET_DIRECTION_OUTGOING));
EXPECT_TRUE(packet_ldbc_hash(&handler, PKT_LDBC_METH_INNERMOST_INT_IP, PACKET_DIRECTION_INCOMING) == packet_ldbc_hash(&handler, PKT_LDBC_METH_INNERMOST_EXT_IP, PACKET_DIRECTION_OUTGOING));
}
#endif
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,148 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* Transmission Control Protocol, Src Port: 55555, Dst Port: 40856, Seq: 0, Ack: 1, Len: 0
* Source Port: 55555
* Destination Port: 40856
* [Stream index: 0]
* [Conversation completeness: Complete, WITH_DATA (31)]
* [TCP Segment Len: 0]
* Sequence Number: 0 (relative sequence number)
* Sequence Number (raw): 3965699644
* [Next Sequence Number: 1 (relative sequence number)]
* Acknowledgment Number: 1 (relative ack number)
* Acknowledgment number (raw): 991053714
* 1010 .... = Header Length: 40 bytes (10)
* Flags: 0x012 (SYN, ACK)
* 000. .... .... = Reserved: Not set
* ...0 .... .... = Accurate ECN: Not set
* .... 0... .... = Congestion Window Reduced: Not set
* .... .0.. .... = ECN-Echo: Not set
* .... ..0. .... = Urgent: Not set
* .... ...1 .... = Acknowledgment: Set
* .... .... 0... = Push: Not set
* .... .... .0.. = Reset: Not set
* .... .... ..1. = Syn: Set
* [Expert Info (Chat/Sequence): Connection establish acknowledge (SYN+ACK): server port 55555]
* [Connection establish acknowledge (SYN+ACK): server port 55555]
* [Severity level: Chat]
* [Group: Sequence]
* .... .... ...0 = Fin: Not set
* [TCP Flags: ·······A··S·]
* Window: 43690
* [Calculated window size: 43690]
* Checksum: 0xfe30 incorrect, should be 0x65c9(maybe caused by "TCP checksum offload"?)
* [Expert Info (Error/Checksum): Bad checksum [should be 0x65c9]]
* [Bad checksum [should be 0x65c9]]
* [Severity level: Error]
* [Group: Checksum]
* [Checksum Status: Bad]
* [Calculated Checksum: 0x65c9]
* Urgent Pointer: 0
* Options: (20 bytes), Maximum segment size, SACK permitted, Timestamps, No-Operation (NOP), Window scale
* TCP Option - Maximum segment size: 65495 bytes
* Kind: Maximum Segment Size (2)
* Length: 4
* MSS Value: 65495
* TCP Option - SACK permitted
* Kind: SACK Permitted (4)
* Length: 2
* TCP Option - Timestamps
* Kind: Time Stamp Option (8)
* Length: 10
* Timestamp value: 2767168460: TSval 2767168460, TSecr 2767168460
* Timestamp echo reply: 2767168460
* TCP Option - No-Operation (NOP)
* Kind: No-Operation (1)
* TCP Option - Window scale: 7 (multiply by 128)
* Kind: Window Scale (3)
* Length: 3
* Shift count: 7
* [Multiplier: 128]
* [Timestamps]
* [Time since first frame in this TCP stream: 475471172.552028000 seconds]
* [Time since previous frame in this TCP stream: 475471172.552028000 seconds]
* [SEQ/ACK analysis]
* [This is an ACK to the segment in frame: 1]
* [The RTT to ACK the segment was: 475471172.552028000 seconds]
* [iRTT: 0.000039000 seconds]
*/
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,
0x08, 0x0a, 0xa4, 0xef, 0xa3, 0xcc, 0xa4, 0xef, 0xa3, 0xcc, 0x01, 0x03, 0x03, 0x07};
TEST(TCP_UTILS, GET)
{
const struct tcphdr *hdr = (struct tcphdr *)data;
EXPECT_TRUE(tcp_hdr_get_src_port(hdr) == 55555);
EXPECT_TRUE(tcp_hdr_get_dst_port(hdr) == 40856);
EXPECT_TRUE(tcp_hdr_get_seq(hdr) == 3965699644);
EXPECT_TRUE(tcp_hdr_get_ack(hdr) == 991053714);
EXPECT_TRUE(tcp_hdr_get_hdr_len(hdr) == 40);
EXPECT_TRUE(tcp_hdr_get_flags(hdr) == 0x012);
EXPECT_TRUE(tcp_hdr_get_urg_flag(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_ack_flag(hdr) == true);
EXPECT_TRUE(tcp_hdr_get_push_flag(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_rst_flag(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_syn_flag(hdr) == true);
EXPECT_TRUE(tcp_hdr_get_fin_flag(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_window(hdr) == 43690);
EXPECT_TRUE(tcp_hdr_get_checksum(hdr) == 0xfe30);
EXPECT_TRUE(tcp_hdr_get_urg_ptr(hdr) == 0);
EXPECT_TRUE(tcp_hdr_get_opt_len(hdr) == 20);
EXPECT_TRUE(tcp_hdr_get_opt_data(hdr) == (const char *)(data + 20));
}
TEST(TCP_UTILS, SET1)
{
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_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, (const char *)(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, (const char *)(data + 20));
EXPECT_TRUE(memcmp(buff, data, 40) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,719 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
#include "packet_private.h"
#include "packet_parser.h"
#include "packet_dump.h"
/******************************************************************************
* [Protocols in frame: eth:ethertype:vlan:ethertype:vlan:ethertype:ip:ip:udp:data]
******************************************************************************
*
* Frame 1: 170 bytes on wire (1360 bits), 170 bytes captured (1360 bits)
* Ethernet II, Src: HuaweiTe_3b:b3:9a (a4:c6:4f:3b:b3:9a), Dst: 00:00:00_00:00:04 (00:00:00:00:00:04)
* Destination: 00:00:00_00:00:04 (00:00:00:00:00:04)
* Source: HuaweiTe_3b:b3:9a (a4:c6:4f:3b:b3:9a)
* Type: 802.1Q Virtual LAN (0x8100)
* 802.1Q Virtual LAN, PRI: 3, DEI: 0, ID: 1624
* 011. .... .... .... = Priority: Critical Applications (3)
* ...0 .... .... .... = DEI: Ineligible
* .... 0110 0101 1000 = ID: 1624
* Type: 802.1Q Virtual LAN (0x8100)
* 802.1Q Virtual LAN, PRI: 3, DEI: 0, ID: 505
* 011. .... .... .... = Priority: Critical Applications (3)
* ...0 .... .... .... = DEI: Ineligible
* .... 0001 1111 1001 = ID: 505
* Type: IPv4 (0x0800)
* Internet Protocol Version 4, Src: 69.67.35.146, Dst: 41.202.46.110
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0xb8 (DSCP: EF PHB, ECN: Not-ECT)
* Total Length: 148
* Identification: 0xe858 (59480)
* 000. .... = Flags: 0x0
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 255
* Protocol: IPIP (4)
* Header Checksum: 0x1148 [validation disabled]
* [Header checksum status: Unverified]
* Source Address: 69.67.35.146
* Destination Address: 41.202.46.110
* Internet Protocol Version 4, Src: 10.10.100.25, Dst: 10.10.101.2
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0xb8 (DSCP: EF PHB, ECN: Not-ECT)
* Total Length: 128
* Identification: 0x0001 (1)
* 000. .... = Flags: 0x0
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 254
* Protocol: UDP (17)
* Header Checksum: 0xde84 [validation disabled]
* [Header checksum status: Unverified]
* Source Address: 10.10.100.25
* Destination Address: 10.10.101.2
* User Datagram Protocol, Src Port: 62367, Dst Port: 17000
* Source Port: 62367
* Destination Port: 17000
* Length: 108
* Checksum: 0x4b9a [unverified]
* [Checksum Status: Unverified]
* [Stream index: 0]
* [Timestamps]
* [Time since first frame: 0.000000000 seconds]
* [Time since previous frame: 0.000000000 seconds]
* UDP payload (100 bytes)
* Data (100 bytes)
*/
unsigned char data1[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xa4, 0xc6, 0x4f, 0x3b, 0xb3, 0x9a, 0x81, 0x00, 0x66, 0x58, 0x81, 0x00, 0x61, 0xf9, 0x08, 0x00, 0x45, 0xb8, 0x00, 0x94,
0xe8, 0x58, 0x00, 0x00, 0xff, 0x04, 0x11, 0x48, 0x45, 0x43, 0x23, 0x92, 0x29, 0xca, 0x2e, 0x6e, 0x45, 0xb8, 0x00, 0x80, 0x00, 0x01, 0x00, 0x00, 0xfe, 0x11,
0xde, 0x84, 0x0a, 0x0a, 0x64, 0x19, 0x0a, 0x0a, 0x65, 0x02, 0xf3, 0x9f, 0x42, 0x68, 0x00, 0x6c, 0x4b, 0x9a, 0x00, 0x02, 0x00, 0x00, 0x04, 0x73, 0x6c, 0x10,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd};
/******************************************************************************
* [Protocols in frame: eth:ethertype:vlan:ethertype:ipv6:ip:gre:ppp:ip:udp:dns]
******************************************************************************
*
* Frame 1: 272 bytes on wire (2176 bits), 272 bytes captured (2176 bits)
* Ethernet II, Src: Cisco_e6:82:c4 (00:19:06:e6:82:c4), Dst: 10:01:00:00:61:3d (10:01:00:00:61:3d)
* Destination: 10:01:00:00:61:3d (10:01:00:00:61:3d)
* Source: Cisco_e6:82:c4 (00:19:06:e6:82:c4)
* Type: 802.1Q Virtual LAN (0x8100)
* 802.1Q Virtual LAN, PRI: 0, DEI: 0, ID: 100
* 000. .... .... .... = Priority: Best Effort (default) (0)
* ...0 .... .... .... = DEI: Ineligible
* .... 0000 0110 0100 = ID: 100
* Type: IPv6 (0x86dd)
* Internet Protocol Version 6, Src: 2607:fcd0:100:2300::b108:2a6b, Dst: 2402:f000:1:8e01::5555
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 214
* Next Header: IPIP (4)
* Hop Limit: 57
* Source Address: 2607:fcd0:100:2300::b108:2a6b
* Destination Address: 2402:f000:1:8e01::5555
* Internet Protocol Version 4, Src: 192.52.166.154, Dst: 16.0.0.200
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* Total Length: 214
* Identification: 0x842f (33839)
* 010. .... = Flags: 0x2, Don't fragment
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 64
* Protocol: Generic Routing Encapsulation (47)
* Header Checksum: 0x3e33 [validation disabled]
* [Header checksum status: Unverified]
* Source Address: 192.52.166.154
* Destination Address: 16.0.0.200
* Generic Routing Encapsulation (PPP)
* Flags and Version: 0x3081
* Protocol Type: PPP (0x880b)
* Payload Length: 178
* Call ID: 17
* Sequence Number: 538640
* Acknowledgment Number: 429725
* Point-to-Point Protocol
* Address: 0xff
* Control: 0x03
* Protocol: Internet Protocol version 4 (0x0021)
* Internet Protocol Version 4, Src: 8.8.8.8, Dst: 172.16.44.3
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* Total Length: 174
* Identification: 0x2f9c (12188)
* 000. .... = Flags: 0x0
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 50
* Protocol: UDP (17)
* Header Checksum: 0x7080 [validation disabled]
* [Header checksum status: Unverified]
* Source Address: 8.8.8.8
* Destination Address: 172.16.44.3
* User Datagram Protocol, Src Port: 53, Dst Port: 9879
* Source Port: 53
* Destination Port: 9879
* Length: 154
* Checksum: 0x45d9 [unverified]
* [Checksum Status: Unverified]
* [Stream index: 0]
* [Timestamps]
* UDP payload (146 bytes)
* Domain Name System (response)
*/
unsigned char data2[] = {
0x10, 0x01, 0x00, 0x00, 0x61, 0x3d, 0x00, 0x19, 0x06, 0xe6, 0x82, 0xc4, 0x81, 0x00, 0x00, 0x64, 0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0xd6, 0x04, 0x39,
0x26, 0x07, 0xfc, 0xd0, 0x01, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb1, 0x08, 0x2a, 0x6b, 0x24, 0x02, 0xf0, 0x00, 0x00, 0x01, 0x8e, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0x45, 0x00, 0x00, 0xd6, 0x84, 0x2f, 0x40, 0x00, 0x40, 0x2f, 0x3e, 0x33, 0xc0, 0x34, 0xa6, 0x9a, 0x10, 0x00, 0x00, 0xc8,
0x30, 0x81, 0x88, 0x0b, 0x00, 0xb2, 0x00, 0x11, 0x00, 0x08, 0x38, 0x10, 0x00, 0x06, 0x8e, 0x9d, 0xff, 0x03, 0x00, 0x21, 0x45, 0x00, 0x00, 0xae, 0x2f, 0x9c,
0x00, 0x00, 0x32, 0x11, 0x70, 0x80, 0x08, 0x08, 0x08, 0x08, 0xac, 0x10, 0x2c, 0x03, 0x00, 0x35, 0x26, 0x97, 0x00, 0x9a, 0x45, 0xd9, 0xb4, 0xe2, 0x81, 0x83,
0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x35, 0x78, 0x71, 0x74, 0x2d, 0x64, 0x65, 0x74, 0x65, 0x63, 0x74, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x32, 0x2d,
0x37, 0x38, 0x63, 0x30, 0x36, 0x64, 0x63, 0x37, 0x2d, 0x30, 0x34, 0x61, 0x37, 0x2d, 0x34, 0x38, 0x35, 0x33, 0x2d, 0x38, 0x34, 0x38, 0x33, 0x2d, 0x61, 0x35,
0x36, 0x32, 0x38, 0x39, 0x37, 0x36, 0x65, 0x32, 0x33, 0x33, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x02, 0xf0, 0x00, 0x40,
0x01, 0x61, 0x0c, 0x72, 0x6f, 0x6f, 0x74, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x73, 0x03, 0x6e, 0x65, 0x74, 0x00, 0x05, 0x6e, 0x73, 0x74, 0x6c, 0x64,
0x0c, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67, 0x6e, 0x2d, 0x67, 0x72, 0x73, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x78, 0x0d, 0x09, 0x09, 0x00, 0x00, 0x07, 0x08,
0x00, 0x00, 0x03, 0x84, 0x00, 0x09, 0x3a, 0x80, 0x00, 0x01, 0x51, 0x80};
/******************************************************************************
* [Protocols in frame: eth:ethertype:ipv6:udp:gtp:ipv6:tcp:ja3:tls]
******************************************************************************
*
* Frame 1: 1442 bytes on wire (11536 bits), 1442 bytes captured (11536 bits)
* Ethernet II, Src: zte_0e:f5:40 (74:4a:a4:0e:f5:40), Dst: HuaweiTe_40:e9:c2 (ac:b3:b5:40:e9:c2)
* Destination: HuaweiTe_40:e9:c2 (ac:b3:b5:40:e9:c2)
* Source: zte_0e:f5:40 (74:4a:a4:0e:f5:40)
* Type: IPv6 (0x86dd)
* Internet Protocol Version 6, Src: 2409:8034:4040:5300::105, Dst: 2409:8034:4025::60:61
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 1388
* Next Header: UDP (17)
* Hop Limit: 127
* Source Address: 2409:8034:4040:5300::105
* Destination Address: 2409:8034:4025::60:61
* User Datagram Protocol, Src Port: 2152, Dst Port: 2152
* Source Port: 2152
* Destination Port: 2152
* Length: 1388
* Checksum: 0xeb00 [unverified]
* [Checksum Status: Unverified]
* [Stream index: 0]
* [Timestamps]
* UDP payload (1380 bytes)
* GPRS Tunneling Protocol
* Flags: 0x30
* Message Type: T-PDU (0xff)
* Length: 1372
* TEID: 0x024c3cbd (38550717)
* Internet Protocol Version 6, Src: 2409:8c34:4400:700:0:4:0:3, Dst: 2409:8934:5082:2100:ecad:e0e4:530a:c269
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 1332
* Next Header: TCP (6)
* Hop Limit: 56
* Source Address: 2409:8c34:4400:700:0:4:0:3
* Destination Address: 2409:8934:5082:2100:ecad:e0e4:530a:c269
* Transmission Control Protocol, Src Port: 443, Dst Port: 46582, Seq: 1, Ack: 1, Len: 1312
* Source Port: 443
* Destination Port: 46582
* [Stream index: 0]
* [Conversation completeness: Incomplete (8)]
* [TCP Segment Len: 1312]
* Sequence Number: 1 (relative sequence number)
* Sequence Number (raw): 2198097831
* [Next Sequence Number: 1313 (relative sequence number)]
* Acknowledgment Number: 1 (relative ack number)
* Acknowledgment number (raw): 2264498872
* 0101 .... = Header Length: 20 bytes (5)
* Flags: 0x010 (ACK)
* Window: 529
* [Calculated window size: 529]
* [Window size scaling factor: -1 (unknown)]
* Checksum: 0x2c4b [unverified]
* [Checksum Status: Unverified]
* Urgent Pointer: 0
* [Timestamps]
* [SEQ/ACK analysis]
* TCP payload (1312 bytes)
* Transport Layer Security
*/
unsigned char data3[] = {
0xac, 0xb3, 0xb5, 0x40, 0xe9, 0xc2, 0x74, 0x4a, 0xa4, 0x0e, 0xf5, 0x40, 0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x05, 0x6c, 0x11, 0x7f, 0x24, 0x09, 0x80, 0x34,
0x40, 0x40, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x05, 0x24, 0x09, 0x80, 0x34, 0x40, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60,
0x00, 0x61, 0x08, 0x68, 0x08, 0x68, 0x05, 0x6c, 0xeb, 0x00, 0x30, 0xff, 0x05, 0x5c, 0x02, 0x4c, 0x3c, 0xbd, 0x60, 0x00, 0x00, 0x00, 0x05, 0x34, 0x06, 0x38,
0x24, 0x09, 0x8c, 0x34, 0x44, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x24, 0x09, 0x89, 0x34, 0x50, 0x82, 0x21, 0x00, 0xec, 0xad,
0xe0, 0xe4, 0x53, 0x0a, 0xc2, 0x69, 0x01, 0xbb, 0xb5, 0xf6, 0x83, 0x04, 0x4f, 0xa7, 0x86, 0xf9, 0x82, 0xb8, 0x50, 0x10, 0x02, 0x11, 0x2c, 0x4b, 0x00, 0x00,
0x17, 0x03, 0x03, 0x3c, 0x8c, 0x87, 0xa0, 0x99, 0x23, 0x5b, 0x53, 0x4a, 0x12, 0x1b, 0xf8, 0xba, 0xe8, 0x83, 0xc2, 0x95, 0xda, 0xb8, 0xea, 0x5b, 0xdc, 0x84,
0x61, 0xa9, 0x86, 0x7e, 0x43, 0xc7, 0x31, 0x44, 0x6e, 0x11, 0xc1, 0x30, 0x21, 0x03, 0xb4, 0x21, 0x4a, 0xee, 0xc9, 0x2e, 0x14, 0xd2, 0x98, 0x63, 0x12, 0xfe,
0x79, 0x58, 0xb3, 0x18, 0xa6, 0x8d, 0x0c, 0x62, 0x67, 0x51, 0xef, 0x02, 0x5a, 0xa8, 0xb3, 0x82, 0x1f, 0xe4, 0x51, 0xba, 0xde, 0xee, 0x83, 0x9c, 0x4e, 0xac,
0x4d, 0xa2, 0xb7, 0x6a, 0x82, 0xe7, 0xbb, 0x00, 0xf7, 0x5a, 0xe7, 0x02, 0x71, 0x7e, 0x7d, 0x6f, 0xf2, 0xe5, 0x47, 0xd0, 0xba, 0x3c, 0x51, 0x09, 0x95, 0xcd,
0xf6, 0xc9, 0x8b, 0x6f, 0xb0, 0x39, 0x11, 0x0d, 0xe9, 0x0d, 0x4d, 0x29, 0xd4, 0xcb, 0x87, 0xba, 0x11, 0xfa, 0x0d, 0x0b, 0x82, 0x95, 0xa5, 0x84, 0x94, 0x48,
0xa2, 0xee, 0xa4, 0xb7, 0xb6, 0x76, 0x13, 0x4d, 0x18, 0x42, 0x91, 0x77, 0xad, 0x82, 0x38, 0xee, 0x34, 0x1c, 0xb7, 0xf6, 0x39, 0xdc, 0xa4, 0x23, 0xa1, 0x7c,
0xa5, 0x0b, 0x7e, 0x4c, 0x8b, 0x81, 0x31, 0x48, 0xea, 0xf4, 0x18, 0x37, 0x09, 0x0a, 0x53, 0x13, 0x05, 0x90, 0x26, 0x10, 0x69, 0xb2, 0xa3, 0x36, 0xbc, 0xa5,
0x83, 0xd8, 0x16, 0x77, 0x98, 0xc8, 0x21, 0x38, 0xd9, 0x88, 0x0c, 0xa7, 0x16, 0x97, 0x4e, 0x20, 0x6d, 0x68, 0xda, 0x1b, 0x3b, 0x4a, 0x62, 0xe0, 0x36, 0x0d,
0xbf, 0x30, 0x71, 0xb1, 0xe9, 0xbe, 0x47, 0x77, 0x99, 0xb9, 0xe6, 0x26, 0xab, 0x81, 0x2e, 0x46, 0xf1, 0x1b, 0x1e, 0xfb, 0xd7, 0x81, 0x60, 0x21, 0x4a, 0x71,
0x85, 0xf7, 0x9c, 0x9c, 0xd4, 0x1c, 0x52, 0xc4, 0x3d, 0x8d, 0x72, 0xf6, 0x7c, 0xd3, 0x58, 0x79, 0x0d, 0x78, 0xd7, 0x7c, 0x29, 0x2b, 0xc3, 0x96, 0x1d, 0xc7,
0x96, 0x50, 0x42, 0xd7, 0xda, 0xeb, 0x29, 0x8e, 0x2a, 0x72, 0x23, 0x57, 0x0f, 0x6f, 0x37, 0x35, 0xb2, 0x42, 0x76, 0x78, 0xbf, 0xbf, 0x8c, 0x3f, 0x31, 0xa2,
0x51, 0xec, 0x9e, 0x0d, 0xfd, 0xf2, 0xaf, 0x71, 0xa0, 0x4f, 0xa9, 0xf6, 0x19, 0xcf, 0x3e, 0x4b, 0xc8, 0xaa, 0x38, 0x06, 0xa1, 0x15, 0xde, 0xde, 0xef, 0x9b,
0x25, 0xa3, 0xcc, 0x47, 0xca, 0x29, 0x30, 0x65, 0x5f, 0xc1, 0x8b, 0x12, 0x63, 0x79, 0xcd, 0x57, 0x4d, 0x99, 0xc0, 0xcd, 0xbe, 0x62, 0xcb, 0xc3, 0xf2, 0x6b,
0x0b, 0x40, 0xc5, 0xee, 0x79, 0x0a, 0xa4, 0x75, 0x56, 0xe7, 0xe7, 0xf2, 0xfd, 0xe0, 0x72, 0x78, 0x04, 0xa2, 0x50, 0x31, 0x09, 0x8b, 0x57, 0xc3, 0x85, 0x4e,
0xc4, 0xae, 0xde, 0x8a, 0xfa, 0xf6, 0x31, 0x06, 0xd2, 0x07, 0x25, 0x40, 0xce, 0x0d, 0xfd, 0x26, 0x98, 0x41, 0xa3, 0xa9, 0xa2, 0x8d, 0x8b, 0x7f, 0x6d, 0x63,
0x87, 0x7e, 0x75, 0x2f, 0x78, 0xc9, 0xd5, 0x04, 0xb2, 0x4f, 0xc9, 0x94, 0xa7, 0x7f, 0xbc, 0x75, 0x7b, 0xb6, 0xfb, 0x2c, 0x46, 0xf6, 0xde, 0x36, 0x31, 0x2a,
0x32, 0x1d, 0x7f, 0x30, 0x9e, 0x4a, 0x84, 0x69, 0x66, 0xac, 0xef, 0xbe, 0xb3, 0x83, 0x8c, 0xb8, 0x30, 0xd2, 0x3f, 0xcf, 0xb5, 0xbb, 0x65, 0xaa, 0xe7, 0x6b,
0x74, 0x48, 0x2c, 0xb2, 0x72, 0x2b, 0x78, 0xaf, 0xd0, 0x71, 0x04, 0xa9, 0xb4, 0x65, 0xd9, 0xfc, 0x74, 0x23, 0xff, 0x89, 0xc1, 0x16, 0x23, 0xac, 0x59, 0x16,
0x89, 0x41, 0xc3, 0xdb, 0xdb, 0x5b, 0x9a, 0x3d, 0x08, 0xc4, 0x12, 0x28, 0xf8, 0x10, 0xa5, 0xad, 0xc6, 0x81, 0xc0, 0x61, 0x48, 0xba, 0x9d, 0xef, 0xc7, 0xf8,
0xad, 0x9a, 0xbd, 0x87, 0xfa, 0x7f, 0xa2, 0x4e, 0x4d, 0xe0, 0x19, 0xd5, 0x47, 0xc7, 0xd0, 0xfb, 0x00, 0x7b, 0xbf, 0x17, 0x80, 0xfe, 0xf5, 0x27, 0xec, 0x94,
0x44, 0x3d, 0x4a, 0x34, 0x49, 0x60, 0xb4, 0x8d, 0x71, 0x6d, 0x9c, 0xf4, 0x4c, 0x33, 0xa9, 0x49, 0x58, 0x58, 0x6f, 0xe1, 0xd1, 0x7d, 0x36, 0x51, 0xf4, 0xd8,
0x0d, 0x0b, 0xfc, 0xeb, 0xae, 0x58, 0x06, 0x08, 0xbf, 0x67, 0x07, 0x28, 0x7e, 0x68, 0x65, 0x79, 0x86, 0xfb, 0x43, 0x0f, 0x0a, 0xef, 0xd0, 0x97, 0x33, 0x10,
0x7a, 0x20, 0xe8, 0x22, 0xe5, 0xdc, 0x0c, 0xa2, 0xa5, 0x50, 0x1b, 0x08, 0x15, 0xc2, 0xec, 0xd2, 0x06, 0x25, 0xd0, 0x3b, 0xfd, 0xe3, 0xa2, 0x6f, 0x41, 0x15,
0x6d, 0x9f, 0x5f, 0xc4, 0x07, 0x5c, 0x99, 0x63, 0xd9, 0xd7, 0xdc, 0x90, 0xc9, 0x8f, 0x3a, 0x4b, 0x6a, 0x84, 0xe8, 0x3c, 0xc7, 0x71, 0x50, 0x71, 0x86, 0x71,
0x7d, 0x54, 0x84, 0x7b, 0xb7, 0xca, 0xd5, 0x42, 0xaf, 0x88, 0xa5, 0xae, 0xa4, 0x9c, 0xfd, 0x71, 0x71, 0x0f, 0x67, 0xaa, 0x1b, 0x61, 0xd7, 0xf4, 0x50, 0x21,
0x9d, 0x80, 0x6e, 0x54, 0xcd, 0xb6, 0xb9, 0x02, 0x3e, 0x59, 0x50, 0xff, 0xf2, 0xda, 0x21, 0x5c, 0x50, 0x6d, 0x64, 0x8c, 0x33, 0x75, 0x2a, 0xa4, 0x56, 0xb3,
0xa8, 0xdb, 0xba, 0xbe, 0x52, 0xd4, 0xe5, 0x29, 0x68, 0xe2, 0x6b, 0x94, 0x6b, 0xb3, 0x90, 0x63, 0x91, 0x1a, 0x95, 0xb5, 0xd7, 0x10, 0x1b, 0xd9, 0x93, 0x4f,
0x33, 0xb6, 0x6a, 0x4e, 0xcd, 0x40, 0x9d, 0x47, 0x76, 0x3e, 0x4b, 0xc7, 0x2f, 0x16, 0x96, 0x64, 0x9d, 0x4e, 0x8c, 0xfb, 0x0f, 0xd2, 0xec, 0x6c, 0xba, 0xf2,
0x9c, 0xca, 0xd2, 0x3e, 0x64, 0x37, 0x32, 0x20, 0xd7, 0x4c, 0xb0, 0xe7, 0xd3, 0x75, 0x51, 0x3a, 0x94, 0xc1, 0xdf, 0x1c, 0xb3, 0x10, 0xd5, 0x1e, 0xcf, 0x7c,
0xb7, 0xab, 0x4a, 0x93, 0xf0, 0x78, 0x58, 0x28, 0x63, 0x10, 0xee, 0xb0, 0xd6, 0x14, 0x81, 0x47, 0xeb, 0x2e, 0xc8, 0x6e, 0x33, 0x7e, 0xf3, 0x2d, 0xc8, 0xdb,
0x29, 0x0c, 0x80, 0xe4, 0x2f, 0x10, 0x07, 0x8e, 0x08, 0x86, 0x97, 0x1b, 0x39, 0x98, 0x39, 0x06, 0xb3, 0x85, 0x53, 0xb7, 0xbb, 0x65, 0x65, 0x85, 0x0e, 0x0a,
0x7d, 0x29, 0x3d, 0x3f, 0x52, 0xc2, 0x7b, 0x2b, 0x30, 0x94, 0x99, 0x6a, 0x4b, 0xad, 0xe9, 0xec, 0xcb, 0xcd, 0xae, 0x97, 0x45, 0x54, 0xd5, 0x00, 0x5e, 0xd8,
0xac, 0xeb, 0x99, 0xdc, 0x58, 0x0b, 0x01, 0xeb, 0x32, 0x22, 0xc4, 0xec, 0x4f, 0xd2, 0x15, 0x03, 0x30, 0x88, 0xc7, 0x28, 0xaf, 0x78, 0xf5, 0x38, 0x84, 0x3b,
0x3b, 0xe9, 0x29, 0x71, 0x50, 0xa3, 0x07, 0x49, 0x3b, 0xc6, 0x97, 0xc6, 0xf9, 0x53, 0x95, 0x51, 0x65, 0x7e, 0xd7, 0xd4, 0xe8, 0x76, 0x6a, 0x6d, 0x37, 0x6b,
0xa5, 0x59, 0xaa, 0x14, 0x18, 0x8c, 0x8d, 0x65, 0x78, 0x67, 0xfb, 0x60, 0x56, 0xab, 0x04, 0xa0, 0xc2, 0x93, 0x46, 0xf1, 0x2b, 0x0d, 0x3b, 0x38, 0x62, 0x62,
0x5e, 0xc8, 0x30, 0xf9, 0x45, 0x28, 0x6f, 0xa1, 0xb1, 0x88, 0xf1, 0x2b, 0x3b, 0xf8, 0xae, 0x91, 0x52, 0xc3, 0x72, 0x86, 0xe4, 0xec, 0xc3, 0x54, 0x86, 0xbf,
0x8f, 0x33, 0xb1, 0x0f, 0x42, 0xc5, 0x9c, 0xb8, 0xc2, 0x67, 0x8b, 0xac, 0x78, 0xd7, 0x63, 0xab, 0x05, 0xc6, 0x6c, 0x37, 0xa1, 0x28, 0xef, 0x95, 0xc9, 0xf5,
0x12, 0x38, 0x54, 0x34, 0x2e, 0x03, 0x6a, 0xaa, 0xa9, 0x97, 0x72, 0x22, 0x9f, 0x20, 0xec, 0x9e, 0x29, 0x09, 0xd8, 0x38, 0xd1, 0x86, 0x82, 0x99, 0xbd, 0x2a,
0x03, 0xe9, 0x3d, 0xbd, 0xea, 0xc5, 0x8b, 0xb0, 0x4c, 0x8b, 0x7e, 0x78, 0x08, 0xef, 0x39, 0xa8, 0xb4, 0x47, 0xce, 0x44, 0xc3, 0x3f, 0x52, 0xe4, 0xbd, 0x9e,
0xf6, 0xed, 0x6f, 0x6c, 0x05, 0x19, 0xa6, 0x0a, 0x1e, 0x48, 0xe3, 0x9b, 0x91, 0x61, 0xef, 0xf5, 0x91, 0x39, 0x70, 0x44, 0x1c, 0x08, 0x2e, 0x2c, 0x6c, 0x27,
0xb9, 0x0e, 0xcc, 0x74, 0x69, 0xa5, 0xf8, 0x19, 0xd6, 0xbf, 0x57, 0x6c, 0x9a, 0x91, 0x74, 0xfd, 0xc2, 0x31, 0x32, 0x12, 0x06, 0xa3, 0x69, 0x71, 0xda, 0x40,
0xa1, 0xf3, 0xb5, 0x9a, 0x43, 0xcc, 0xb4, 0x3c, 0x16, 0x40, 0x65, 0x2b, 0x02, 0xac, 0x5c, 0xae, 0xd6, 0x34, 0x34, 0xe3, 0x69, 0x76, 0x2c, 0xa8, 0xdd, 0x04,
0x92, 0xa6, 0x7a, 0xc0, 0x87, 0x70, 0x8b, 0x85, 0xba, 0x5d, 0xbb, 0x62, 0x70, 0xcc, 0x1f, 0x21, 0x2c, 0x7e, 0xc3, 0x77, 0xcf, 0x23, 0x22, 0xf4, 0x16, 0x8e,
0xf1, 0x3d, 0xdc, 0x33, 0x99, 0x5e, 0xaa, 0xa2, 0x50, 0x68, 0xde, 0x03, 0x44, 0xbb, 0xc7, 0x16, 0x2a, 0xf2, 0x08, 0xeb, 0x3d, 0x12, 0x6d, 0xcb, 0x2a, 0xaf,
0xb4, 0x79, 0xdb, 0x74, 0x5e, 0x54, 0x89, 0x73, 0x0c, 0x48, 0x9c, 0x03, 0x33, 0xd2, 0x92, 0x22, 0xdb, 0x3a, 0xa0, 0x8c, 0xe2, 0x30, 0x6f, 0x39, 0xe4, 0xa9,
0x24, 0x04, 0xbb, 0x85, 0x7d, 0x62, 0xc5, 0xa9, 0x98, 0x92, 0xef, 0xc6, 0xc8, 0xd1, 0x81, 0xad, 0x95, 0x40, 0x27, 0x09, 0xc7, 0x43, 0xcd, 0xb6, 0x94, 0xfc,
0x1c, 0x7d, 0x1c, 0xd3, 0x47, 0xfe, 0x62, 0x9c, 0xfa, 0xeb, 0xfc, 0x02, 0x2e, 0x48, 0x62, 0xcf, 0x63, 0xdb, 0x63, 0xd9, 0x21, 0x86, 0xe8, 0x96, 0x54, 0xeb,
0x6a, 0xa8, 0x78, 0x3c, 0x5b, 0xb6, 0xde, 0xa9, 0x04, 0x48, 0x63, 0xb2, 0x10, 0x02, 0x6a, 0x7f, 0x6d, 0xc8, 0x04, 0xdd, 0x99, 0x25, 0x08, 0xff, 0x80, 0x11,
0x53, 0xfb, 0x7a, 0x07, 0x39, 0xd9, 0x97, 0xca, 0xf0, 0xa7, 0x46, 0x9c, 0xc2, 0xae, 0x2e, 0x05, 0x62, 0xa0, 0xd5, 0x5d, 0x17, 0x0e, 0x5c, 0x7e, 0x9a, 0xb2,
0xb7, 0x9d, 0xd4, 0x4f, 0xe3, 0xac, 0x64, 0xdb, 0x6f, 0x1d, 0xdf, 0xd8, 0x41, 0xd7, 0xd9, 0x50, 0x55, 0x30, 0xeb, 0x4b, 0x19, 0xce, 0x78, 0x1f, 0xa8, 0x1e,
0x87, 0x9c, 0x8f, 0x93, 0x97, 0xd4, 0xa2, 0x28, 0x2c, 0x79, 0x22, 0xc8};
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:udp:vxlan:eth:ethertype:ip:udp:dns]
******************************************************************************
*
* Frame 1: 124 bytes on wire (992 bits), 124 bytes captured (992 bits)
* Ethernet II, Src: zte_6c:fa:43 (00:1e:73:6c:fa:43), Dst: Shanghai_0d:0a (e4:95:6e:20:0d:0a)
* Destination: Shanghai_0d:0a (e4:95:6e:20:0d:0a)
* Source: zte_6c:fa:43 (00:1e:73:6c:fa:43)
* Type: IPv4 (0x0800)
* Internet Protocol Version 4, Src: 10.1.1.1, Dst: 192.168.1.10
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* Total Length: 110
* Identification: 0x0000 (0)
* 000. .... = Flags: 0x0
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 254
* Protocol: UDP (17)
* Header Checksum: 0xefca [validation disabled]
* [Header checksum status: Unverified]
* Source Address: 10.1.1.1
* Destination Address: 192.168.1.10
* User Datagram Protocol, Src Port: 50709, Dst Port: 4789
* Source Port: 50709
* Destination Port: 4789
* Length: 90
* Checksum: 0x0000 [zero-value ignored]
* [Stream index: 0]
* [Timestamps]
* UDP payload (82 bytes)
* Virtual eXtensible Local Area Network
* Flags: 0x0800, VXLAN Network ID (VNI)
* Group Policy ID: 0
* VXLAN Network Identifier (VNI): 458755
* Reserved: 0
* Ethernet II, Src: WistronI_18:18:41 (3c:97:0e:18:18:41), Dst: DawningI_13:70:7a (e8:61:1f:13:70:7a)
* Destination: DawningI_13:70:7a (e8:61:1f:13:70:7a)
* Source: WistronI_18:18:41 (3c:97:0e:18:18:41)
* Type: IPv4 (0x0800)
* Internet Protocol Version 4, Src: 192.168.11.193, Dst: 114.114.114.114
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* Total Length: 60
* Identification: 0x0cb6 (3254)
* 000. .... = Flags: 0x0
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 64
* Protocol: UDP (17)
* Header Checksum: 0xbcad [validation disabled]
* [Header checksum status: Unverified]
* Source Address: 192.168.11.193
* Destination Address: 114.114.114.114
* User Datagram Protocol, Src Port: 65290, Dst Port: 53
* Source Port: 65290
* Destination Port: 53
* Length: 40
* Checksum: 0x39e4 [unverified]
* [Checksum Status: Unverified]
* [Stream index: 1]
* [Timestamps]
* UDP payload (32 bytes)
* Domain Name System (query)
*/
unsigned char data4[] = {
0xe4, 0x95, 0x6e, 0x20, 0x0d, 0x0a, 0x00, 0x1e, 0x73, 0x6c, 0xfa, 0x43, 0x08, 0x00, 0x45, 0x00, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x11, 0xef, 0xca,
0x0a, 0x01, 0x01, 0x01, 0xc0, 0xa8, 0x01, 0x0a, 0xc6, 0x15, 0x12, 0xb5, 0x00, 0x5a, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x07, 0x00, 0x03, 0x00, 0xe8, 0x61,
0x1f, 0x13, 0x70, 0x7a, 0x3c, 0x97, 0x0e, 0x18, 0x18, 0x41, 0x08, 0x00, 0x45, 0x00, 0x00, 0x3c, 0x0c, 0xb6, 0x00, 0x00, 0x40, 0x11, 0xbc, 0xad, 0xc0, 0xa8,
0x0b, 0xc1, 0x72, 0x72, 0x72, 0x72, 0xff, 0x0a, 0x00, 0x35, 0x00, 0x28, 0x39, 0xe4, 0x86, 0x84, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01};
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:udp:l2tp:ppp:ip:udp:nbns]
******************************************************************************
*
* Frame 1: 150 bytes on wire (1200 bits), 150 bytes captured (1200 bits)
* Ethernet II, Src: LCFCElectron_43:38:37 (28:d2:44:43:38:37), Dst: c0:00:14:8c:00:00 (c0:00:14:8c:00:00)
* Destination: c0:00:14:8c:00:00 (c0:00:14:8c:00:00)
* Source: LCFCElectron_43:38:37 (28:d2:44:43:38:37)
* Type: IPv4 (0x0800)
* Internet Protocol Version 4, Src: 172.16.0.100, Dst: 172.16.0.254
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 136
* Identification: 0x06ca (1738)
* 000. .... = Flags: 0x0
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 128
* Protocol: UDP (17)
* Header Checksum: 0xda18 [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0xda18]
* Source Address: 172.16.0.100
* Destination Address: 172.16.0.254
* User Datagram Protocol, Src Port: 1701, Dst Port: 1701
* Source Port: 1701
* Destination Port: 1701
* Length: 116
* Checksum: 0x962f [correct]
* [Calculated Checksum: 0x962f]
* [Checksum Status: Good]
* [Stream index: 0]
* [Timestamps]
* [Time since first frame: 0.000000000 seconds]
* [Time since previous frame: 0.000000000 seconds]
* UDP payload (108 bytes)
* Layer 2 Tunneling Protocol
* Flags: 0x4002, Type: Data Message, Length Bit
* 0... .... .... .... = Type: Data Message (0)
* .1.. .... .... .... = Length Bit: Length field is present
* .... 0... .... .... = Sequence Bit: Ns and Nr fields are not present
* .... ..0. .... .... = Offset bit: Offset size field is not present
* .... ...0 .... .... = Priority: No priority
* .... .... .... 0010 = Version: 2
* Length: 108
* Tunnel ID: 28998
* Session ID: 2
* Point-to-Point Protocol
* Address: 0xff
* Control: 0x03
* Protocol: Internet Protocol version 4 (0x0021)
* Internet Protocol Version 4, Src: 172.16.2.100, Dst: 255.255.255.255
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 96
* Identification: 0x0004 (4)
* 000. .... = Flags: 0x0
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 128
* Protocol: UDP (17)
* Header Checksum: 0x8c15 [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x8c15]
* Source Address: 172.16.2.100
* Destination Address: 255.255.255.255
* User Datagram Protocol, Src Port: 137, Dst Port: 137
* Source Port: 137
* Destination Port: 137
* Length: 76
* Checksum: 0xba80 [correct]
* [Calculated Checksum: 0xba80]
* [Checksum Status: Good]
* [Stream index: 1]
* [Timestamps]
* [Time since first frame: 0.000000000 seconds]
* [Time since previous frame: 0.000000000 seconds]
* UDP payload (68 bytes)
* NetBIOS Name Service
*/
unsigned char data5[] = {
0xc0, 0x00, 0x14, 0x8c, 0x00, 0x00, 0x28, 0xd2, 0x44, 0x43, 0x38, 0x37, 0x08, 0x00, 0x45, 0x00, 0x00, 0x88, 0x06, 0xca, 0x00, 0x00, 0x80, 0x11, 0xda, 0x18,
0xac, 0x10, 0x00, 0x64, 0xac, 0x10, 0x00, 0xfe, 0x06, 0xa5, 0x06, 0xa5, 0x00, 0x74, 0x96, 0x2f, 0x40, 0x02, 0x00, 0x6c, 0x71, 0x46, 0x00, 0x02, 0xff, 0x03,
0x00, 0x21, 0x45, 0x00, 0x00, 0x60, 0x00, 0x04, 0x00, 0x00, 0x80, 0x11, 0x8c, 0x15, 0xac, 0x10, 0x02, 0x64, 0xff, 0xff, 0xff, 0xff, 0x00, 0x89, 0x00, 0x89,
0x00, 0x4c, 0xba, 0x80, 0xc6, 0x46, 0x29, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x20, 0x45, 0x4a, 0x45, 0x4a, 0x45, 0x46, 0x43, 0x4e, 0x46,
0x44, 0x45, 0x4e, 0x43, 0x4e, 0x46, 0x45, 0x45, 0x49, 0x45, 0x4a, 0x45, 0x4f, 0x45, 0x4c, 0x43, 0x41, 0x43, 0x41, 0x43, 0x41, 0x41, 0x41, 0x00, 0x00, 0x20,
0x00, 0x01, 0xc0, 0x0c, 0x00, 0x20, 0x00, 0x01, 0x00, 0x04, 0x93, 0xe0, 0x00, 0x06, 0x00, 0x00, 0xac, 0x10, 0x02, 0x64};
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:udp:teredo:ipv6:udp:data]
******************************************************************************
*
* Frame 1: 108 bytes on wire (864 bits), 108 bytes captured (864 bits)
* Ethernet II, Src: Dell_c4:5b:ea (bc:30:5b:c4:5b:ea), Dst: Dell_3e:34:9c (b8:ac:6f:3e:34:9c)
* Destination: Dell_3e:34:9c (b8:ac:6f:3e:34:9c)
* Source: Dell_c4:5b:ea (bc:30:5b:c4:5b:ea)
* Type: IPv4 (0x0800)
* Internet Protocol Version 4, Src: 193.0.0.3, Dst: 193.0.0.1
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 94
* Identification: 0x62a0 (25248)
* 000. .... = Flags: 0x0
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 64
* Protocol: UDP (17)
* Header Checksum: 0x95ea [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x95ea]
* Source Address: 193.0.0.3
* Destination Address: 193.0.0.1
* User Datagram Protocol, Src Port: 45802, Dst Port: 3544
* Source Port: 45802
* Destination Port: 3544
* Length: 74
* Checksum: 0x4b23 [correct]
* [Calculated Checksum: 0x4b23]
* [Checksum Status: Good]
* [Stream index: 0]
* [Timestamps]
* [Time since first frame: 0.000000000 seconds]
* [Time since previous frame: 0.000000000 seconds]
* UDP payload (66 bytes)
* Teredo IPv6 over UDP tunneling
* Internet Protocol Version 6, Src: 2002:0:c100:1:24ba:4d15:3eff:fffc, Dst: 2001:db8:1::1
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 00.. .... .... .... .... .... = Differentiated Services Codepoint: Default (0)
* .... .... ..00 .... .... .... .... .... = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 26
* Next Header: UDP (17)
* Hop Limit: 128
* Source Address: 2002:0:c100:1:24ba:4d15:3eff:fffc
* Destination Address: 2001:db8:1::1
* [Source 6to4 Gateway IPv4: 0.0.193.0]
* [Source 6to4 SLA ID: 1]
* User Datagram Protocol, Src Port: 32768, Dst Port: 20480
* Source Port: 32768
* Destination Port: 20480
* Length: 26
* Checksum: 0xf017 [correct]
* [Calculated Checksum: 0xf017]
* [Checksum Status: Good]
* [Stream index: 1]
* [Timestamps]
* [Time since first frame: 0.000000000 seconds]
* [Time since previous frame: 0.000000000 seconds]
* UDP payload (18 bytes)
* Data (18 bytes)
* Data: 4fd54034712d3f014d3180b082c007d0e76c
* [Length: 18]
*/
unsigned char data6[] = {
0xb8, 0xac, 0x6f, 0x3e, 0x34, 0x9c, 0xbc, 0x30, 0x5b, 0xc4, 0x5b, 0xea, 0x08, 0x00, 0x45, 0x00, 0x00, 0x5e, 0x62, 0xa0, 0x00, 0x00, 0x40, 0x11, 0x95, 0xea,
0xc1, 0x00, 0x00, 0x03, 0xc1, 0x00, 0x00, 0x01, 0xb2, 0xea, 0x0d, 0xd8, 0x00, 0x4a, 0x4b, 0x23, 0x60, 0x00, 0x00, 0x00, 0x00, 0x1a, 0x11, 0x80, 0x20, 0x02,
0x00, 0x00, 0xc1, 0x00, 0x00, 0x01, 0x24, 0xba, 0x4d, 0x15, 0x3e, 0xff, 0xff, 0xfc, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x50, 0x00, 0x00, 0x1a, 0xf0, 0x17, 0x4f, 0xd5, 0x40, 0x34, 0x71, 0x2d, 0x3f, 0x01, 0x4d, 0x31, 0x80, 0xb0, 0x82, 0xc0,
0x07, 0xd0, 0xe7, 0x6c};
TEST(TUNNEL, IPV4)
{
struct packet pkt;
struct tunnel out;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)data1, sizeof(data1));
packet_print(&pkt);
EXPECT_TRUE(packet_get_tunnel_count(&pkt) == 1);
// IPv4 tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 0, &out) == 0);
EXPECT_TRUE(out.type == TUNNEL_IPV4);
EXPECT_TRUE(out.layer_count == 1);
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
// No tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
}
TEST(TUNNEL, IPV6)
{
// TEST ON GRE
}
TEST(TUNNEL, GRE)
{
struct packet pkt;
struct tunnel out;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)data2, sizeof(data2));
packet_print(&pkt);
EXPECT_TRUE(packet_get_tunnel_count(&pkt) == 2);
// IPv6 tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 0, &out) == 0);
EXPECT_TRUE(out.type == TUNNEL_IPV6);
EXPECT_TRUE(out.layer_count == 1);
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV6);
EXPECT_TRUE(out.layers[0]->hdr_len == 40);
// GRE tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == 0);
EXPECT_TRUE(out.type == TUNNEL_GRE);
EXPECT_TRUE(out.layer_count == 2);
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_GRE);
EXPECT_TRUE(out.layers[1]->hdr_len == 16);
// No tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 2, &out) == -1);
}
TEST(TUNNEL, GTP)
{
struct packet pkt;
struct tunnel out;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)data3, sizeof(data3));
packet_print(&pkt);
EXPECT_TRUE(packet_get_tunnel_count(&pkt) == 1);
// GTP tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 0, &out) == 0);
EXPECT_TRUE(out.type == TUNNEL_GTP);
EXPECT_TRUE(out.layer_count == 3);
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV6);
EXPECT_TRUE(out.layers[0]->hdr_len == 40);
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP);
EXPECT_TRUE(out.layers[1]->hdr_len == 8);
EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_GTP_U);
EXPECT_TRUE(out.layers[2]->hdr_len == 8);
// No tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
}
TEST(TUNNEL, VXLAN)
{
struct packet pkt;
struct tunnel out;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)data4, sizeof(data4));
packet_print(&pkt);
EXPECT_TRUE(packet_get_tunnel_count(&pkt) == 1);
// VXLAN tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 0, &out) == 0);
EXPECT_TRUE(out.type == TUNNEL_VXLAN);
EXPECT_TRUE(out.layer_count == 3);
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP);
EXPECT_TRUE(out.layers[1]->hdr_len == 8);
EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_VXLAN);
EXPECT_TRUE(out.layers[2]->hdr_len == 8);
// No tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
}
TEST(TUNNEL, L2TP)
{
struct packet pkt;
struct tunnel out;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)data5, sizeof(data5));
packet_print(&pkt);
EXPECT_TRUE(packet_get_tunnel_count(&pkt) == 1);
// L2TP tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 0, &out) == 0);
EXPECT_TRUE(out.type == TUNNEL_L2TP);
EXPECT_TRUE(out.layer_count == 3);
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP);
EXPECT_TRUE(out.layers[1]->hdr_len == 8);
EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_L2TP);
EXPECT_TRUE(out.layers[2]->hdr_len == 8);
// No tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
}
TEST(TUNNEL, TEREDO)
{
struct packet pkt;
struct tunnel out;
memset(&pkt, 0, sizeof(pkt));
packet_parse(&pkt, (const char *)data6, sizeof(data6));
packet_print(&pkt);
EXPECT_TRUE(packet_get_tunnel_count(&pkt) == 1);
// IPv4 tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 0, &out) == 0);
EXPECT_TRUE(out.type == TUNNEL_TEREDO);
EXPECT_TRUE(out.layer_count == 2);
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP);
EXPECT_TRUE(out.layers[1]->hdr_len == 8);
// No tunnel
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,43 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* User Datagram Protocol, Src Port: 4001, Dst Port: 8000
* Source Port: 4001
* Destination Port: 8000
* Length: 155
* Checksum: 0x1e1e [correct]
* [Calculated Checksum: 0x1e1e]
* [Checksum Status: Good]
*/
unsigned char data[] = {0x0f, 0xa1, 0x1f, 0x40, 0x00, 0x9b, 0x1e, 0x1e};
TEST(UDP_UTILS, GET)
{
const struct udphdr *hdr = (struct udphdr *)data;
EXPECT_TRUE(udp_hdr_get_src_port(hdr) == 4001);
EXPECT_TRUE(udp_hdr_get_dst_port(hdr) == 8000);
EXPECT_TRUE(udp_hdr_get_total_len(hdr) == 155);
EXPECT_TRUE(udp_hdr_get_checksum(hdr) == 0x1e1e);
}
TEST(UDP_UTILS, SET)
{
char buff[8] = {0};
struct udphdr *hdr = (struct udphdr *)buff;
udp_hdr_set_src_port(hdr, 4001);
udp_hdr_set_dst_port(hdr, 8000);
udp_hdr_set_total_len(hdr, 155);
udp_hdr_set_checksum(hdr, 0x1e1e);
EXPECT_TRUE(memcmp(buff, data, 8) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,42 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* IEEE 802.1ad, ID: 1
* 000. .... .... .... = Priority: 0
* ...0 .... .... .... = DEI: 0
* .... 0000 0000 0001 = ID: 1
* Type: 802.1Q Virtual LAN (0x8100)
*/
unsigned char data[] = {
0x00, 0x01, 0x81, 0x00};
TEST(VLAN_UTILS, GET)
{
const struct vlan_hdr *hdr = (struct vlan_hdr *)data;
EXPECT_TRUE(vlan_hdr_get_priority(hdr) == 0);
EXPECT_TRUE(vlan_hdr_get_dei(hdr) == 0);
EXPECT_TRUE(vlan_hdr_get_vid(hdr) == 1);
EXPECT_TRUE(vlan_hdr_get_ethertype(hdr) == 0x8100);
}
TEST(VLAN_UTILS, SET)
{
char buff[4] = {0};
struct vlan_hdr *hdr = (struct vlan_hdr *)buff;
vlan_hdr_set_priority(hdr, 0);
vlan_hdr_set_dei(hdr, 0);
vlan_hdr_set_vid(hdr, 1);
vlan_hdr_set_ethertype(hdr, 0x8100);
EXPECT_TRUE(memcmp(buff, data, 4) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,43 @@
#include <gtest/gtest.h>
#include "packet_helper.h"
/*
* Virtual eXtensible Local Area Network
* Flags: 0x0800, VXLAN Network ID (VNI)
* 0... .... .... .... = GBP Extension: Not defined
* .... 1... .... .... = VXLAN Network ID (VNI): True
* .... .... .0.. .... = Don't Learn: False
* .... .... .... 0... = Policy Applied: False
* .000 .000 0.00 .000 = Reserved(R): 0x0000
* Group Policy ID: 0
* VXLAN Network Identifier (VNI): 461829
* Reserved: 0
*/
unsigned char data[] = {
0x08, 0x00, 0x00, 0x00, 0x07, 0x0c, 0x05, 0x00};
TEST(VXLAN_UTILS, GET)
{
const struct vxlan_hdr *hdr = (struct vxlan_hdr *)data;
EXPECT_TRUE(vxlan_hdr_get_flags(hdr) == 0x08);
EXPECT_TRUE(vxlan_hdr_get_vni(hdr) == 461829);
}
TEST(VXLAN_UTILS, SET)
{
char buff[8] = {0};
struct vxlan_hdr *hdr = (struct vxlan_hdr *)buff;
vxlan_hdr_set_flags(hdr, 0x08);
vxlan_hdr_set_vni(hdr, 461829);
EXPECT_TRUE(memcmp(buff, data, 8) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}