111 lines
3.0 KiB
C
111 lines
3.0 KiB
C
#ifndef _PACKET_H
|
|
#define _PACKET_H
|
|
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include "tuple.h"
|
|
|
|
enum layer_type
|
|
{
|
|
// 数据链路层
|
|
LAYER_TYPE_ETHER = 1 << 0,
|
|
LAYER_TYPE_PPP = 1 << 1,
|
|
LAYER_TYPE_HDLC = 1 << 2,
|
|
LAYER_TYPE_L2 = (LAYER_TYPE_ETHER | LAYER_TYPE_PPP | LAYER_TYPE_HDLC),
|
|
|
|
// 数据链路层 -- 隧道
|
|
LAYER_TYPE_VLAN = 1 << 3,
|
|
LAYER_TYPE_PPPOE = 1 << 4,
|
|
LAYER_TYPE_MPLS = 1 << 5,
|
|
LAYER_TYPE_L2_TUN = (LAYER_TYPE_VLAN | LAYER_TYPE_PPPOE | LAYER_TYPE_MPLS),
|
|
|
|
// 网络层
|
|
LAYER_TYPE_IPV4 = 1 << 6,
|
|
LAYER_TYPE_IPV6 = 1 << 7,
|
|
LAYER_TYPE_L3 = (LAYER_TYPE_IPV4 | LAYER_TYPE_IPV6),
|
|
|
|
// 网络层 -- 隧道
|
|
LAYER_TYPE_GRE = 1 << 8,
|
|
LAYER_TYPE_L3_TUN = (LAYER_TYPE_GRE),
|
|
|
|
// 传输层
|
|
LAYER_TYPE_UDP = 1 << 9,
|
|
LAYER_TYPE_TCP = 1 << 10,
|
|
LAYER_TYPE_L4 = (LAYER_TYPE_UDP | LAYER_TYPE_TCP),
|
|
|
|
// 传输层 -- 隧道
|
|
LAYER_TYPE_VXLAN = 1 << 11,
|
|
LAYER_TYPE_GTPV1_U = 1 << 12,
|
|
|
|
// ALL
|
|
LAYER_TYPE_ALL = (LAYER_TYPE_L2 | LAYER_TYPE_L2_TUN | LAYER_TYPE_L3 | LAYER_TYPE_L3_TUN | LAYER_TYPE_L4 | LAYER_TYPE_VXLAN | LAYER_TYPE_GTPV1_U),
|
|
};
|
|
|
|
enum ldbc_method
|
|
{
|
|
LDBC_METHOD_HASH_INT_IP = 1,
|
|
LDBC_METHOD_HASH_EXT_IP = 2,
|
|
LDBC_METHOD_HASH_INT_IP_AND_EXT_IP = 3,
|
|
LDBC_METHOD_HASH_INNERMOST_INT_IP = 4,
|
|
LDBC_METHOD_HASH_INNERMOST_EXT_IP = 5,
|
|
};
|
|
|
|
struct layer_record
|
|
{
|
|
enum layer_type type;
|
|
const char *hdr_ptr; // header pointer
|
|
const char *pld_ptr; // payload pointer
|
|
uint16_t hdr_offset; // header offset from data_ptr
|
|
uint16_t hdr_len; // header length
|
|
uint16_t pld_len; // payload length
|
|
};
|
|
|
|
#define PACKET_MAX_LAYERS 32
|
|
struct packet
|
|
{
|
|
struct layer_record layers[PACKET_MAX_LAYERS];
|
|
int8_t layers_used;
|
|
int8_t layers_size;
|
|
|
|
const char *data_ptr;
|
|
uint16_t data_len;
|
|
};
|
|
|
|
#define PACKET_LOG_ERROR(format, ...) void(0)
|
|
#ifndef PACKET_LOG_ERROR
|
|
#define PACKET_LOG_ERROR(format, ...) \
|
|
fprintf(stderr, "ERROR " format "\n", ##__VA_ARGS__);
|
|
#endif
|
|
|
|
// return innermost payload
|
|
const char *packet_parse(struct packet *handler, const char *data, uint16_t len);
|
|
void packet_print(const struct packet *handler);
|
|
|
|
// return 0 : found
|
|
// return -1 : not found
|
|
int packet_get_innermost_four_tuple(const struct packet *handler, struct four_tuple *tuple);
|
|
int packet_get_outermost_four_tuple(const struct packet *handler, struct four_tuple *tuple);
|
|
|
|
// return 0 : found
|
|
// return -1 : not found
|
|
int packet_get_innermost_two_tuple(const struct packet *handler, struct two_tuple *tuple);
|
|
int packet_get_outermost_two_tuple(const struct packet *handler, struct two_tuple *tuple);
|
|
|
|
const struct layer_record *packet_get_innermost_layer(const struct packet *handler, enum layer_type type);
|
|
const struct layer_record *packet_get_outermost_layer(const struct packet *handler, enum layer_type type);
|
|
|
|
// direction 1: E2I
|
|
// direction 0: I2E
|
|
uint64_t packet_get_hash(const struct packet *handler, enum ldbc_method method, int direction);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|