This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/src/packet/packet.h

205 lines
5.5 KiB
C
Raw Normal View History

2023-12-11 16:22:46 +08:00
#ifndef _PACKET_H
#define _PACKET_H
#ifdef __cpluscplus
extern "C"
{
#endif
2023-12-15 18:57:13 +08:00
#include <stdint.h>
#include <stdio.h>
#include "tuple.h"
2024-01-26 14:41:40 +08:00
#include "log.h"
2023-12-15 18:57:13 +08:00
#define PACKET_MAX_LAYERS 32
2024-01-26 14:41:40 +08:00
#define PACKET_LOG_ERROR(format, ...) LOG_ERROR("packet", format, ##__VA_ARGS__)
#define PACKET_LOG_DEBUG(format, ...) LOG_DEBUG("packet", format, ##__VA_ARGS__)
2023-12-15 18:57:13 +08:00
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,
};
enum packet_direction
{
PACKET_DIRECTION_I2E = 0,
PACKET_DIRECTION_E2I = 1,
};
enum packet_action
{
PACKET_ACTION_FORWARD = 0,
PACKET_ACTION_DROP = 1,
};
enum packet_type
{
PACKET_TYPE_DATA = 0,
PACKET_TYPE_CTRL = 1,
};
struct metadata
{
#define MAX_SID_NUM 8
#define MAX_ROUTE_LEN 64
struct
{
uint16_t list[MAX_SID_NUM];
uint16_t used;
} sid;
struct
{
char data[MAX_ROUTE_LEN];
uint16_t len;
} route;
void *user_data;
uint64_t domain;
uint64_t session_id;
enum packet_direction direction;
enum packet_action action;
enum packet_type type;
};
struct layer
2023-12-15 18:57:13 +08:00
{
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
};
struct packet
{
struct layer layers[PACKET_MAX_LAYERS];
struct layer *frag_layer; // fragment layer
2023-12-15 18:57:13 +08:00
int8_t layers_used;
int8_t layers_size;
const char *data_ptr;
uint16_t data_len;
bool need_free;
struct metadata meta;
2023-12-15 18:57:13 +08:00
};
// return innermost payload
const char *packet_parse(struct packet *pkt, const char *data, uint16_t len);
void packet_print(const struct packet *pkt);
2023-12-15 18:57:13 +08:00
// 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);
2023-12-15 18:57:13 +08:00
// 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);
2023-12-15 18:57:13 +08:00
// 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);
2023-12-15 18:57:13 +08:00
const struct layer *packet_get_innermost_layer(const struct packet *pkt, enum layer_type type);
const struct layer *packet_get_outermost_layer(const struct packet *pkt, enum layer_type type);
2023-12-15 18:57:13 +08:00
// direction 1: E2I
// direction 0: I2E
uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction);
// return 0: success
// return -1: failed
int packet_set_sid(struct packet *pkt, uint16_t *sid, int num);
// return number of sid
int packet_get_sid(const struct packet *pkt, uint16_t *sid, int size);
// return 0: success
// return -1: failed
int packet_prepend_sid(struct packet *pkt, uint16_t sid);
// return 0: success
// return -1: failed
int packet_append_sid(struct packet *pkt, uint16_t sid);
// return 0: success
// return -1: failed
int packet_set_route_ctx(struct packet *pkt, const char *route, int len);
// return len of route ctx
int packet_get_route_ctx(const struct packet *pkt, char *buff, int size);
void packet_set_user_data(struct packet *pkt, void *user_data);
void *packet_get_user_data(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_session_id(struct packet *pkt, uint64_t session_id);
uint64_t packet_get_session_id(const struct packet *pkt);
void packet_set_direction(struct packet *pkt, enum packet_direction direction);
enum packet_direction packet_get_direction(const struct packet *pkt);
void packet_set_action(struct packet *pkt, enum packet_action action);
enum packet_action packet_get_action(const struct packet *pkt);
void packet_set_type(struct packet *pkt, enum packet_type type);
enum packet_type packet_get_type(const struct packet *pkt);
struct packet *packet_new(uint16_t pkt_len);
void packet_free(struct packet *pkt);
struct packet *packet_dup(const struct packet *pkt);
const char *packet_get_data(const struct packet *pkt);
uint16_t packet_get_len(const struct packet *pkt);
bool packet_is_fragment(const struct packet *pkt);
2023-12-11 16:22:46 +08:00
#ifdef __cpluscplus
}
#endif
#endif