Refactored packet API to support struct layer (using union to contain different types of encapsulation headers)
This commit is contained in:
99
include/stellar/layer.h
Normal file
99
include/stellar/layer.h
Normal file
@@ -0,0 +1,99 @@
|
||||
#ifndef _LAYER_H
|
||||
#define _LAYER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#define __FAVOR_BSD 1
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/udp.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ip6.h>
|
||||
#include <netinet/icmp6.h>
|
||||
#include <netinet/ip_icmp.h>
|
||||
#include <linux/if_ether.h>
|
||||
#include <linux/mpls.h>
|
||||
|
||||
enum layer_proto
|
||||
{
|
||||
LAYER_PROTO_NONE = 0,
|
||||
|
||||
// L2 -- data link layer
|
||||
LAYER_PROTO_ETHER = 1,
|
||||
LAYER_PROTO_PWETH = 2,
|
||||
LAYER_PROTO_PPP = 3,
|
||||
LAYER_PROTO_L2TP = 4,
|
||||
|
||||
// L2 -- tunnel
|
||||
LAYER_PROTO_VLAN = 21,
|
||||
LAYER_PROTO_PPPOE = 22,
|
||||
LAYER_PROTO_MPLS = 23,
|
||||
|
||||
// L3 -- network layer
|
||||
LAYER_PROTO_IPV4 = 31,
|
||||
LAYER_PROTO_IPV6 = 32,
|
||||
LAYER_PROTO_IPAH = 33,
|
||||
|
||||
// L3 -- tunnel
|
||||
LAYER_PROTO_GRE = 41,
|
||||
|
||||
// L4 -- transport layer
|
||||
LAYER_PROTO_UDP = 51,
|
||||
LAYER_PROTO_TCP = 52,
|
||||
LAYER_PROTO_ICMP = 53,
|
||||
LAYER_PROTO_ICMP6 = 54,
|
||||
|
||||
// L4 -- tunnel
|
||||
LAYER_PROTO_VXLAN = 61,
|
||||
LAYER_PROTO_GTPV1_U = 62,
|
||||
};
|
||||
|
||||
struct layer
|
||||
{
|
||||
enum layer_proto proto;
|
||||
uint16_t payload_len;
|
||||
uint16_t header_len;
|
||||
union
|
||||
{
|
||||
struct ethhdr *eth;
|
||||
struct ip *ip4;
|
||||
struct ip6_hdr *ip6;
|
||||
struct tcphdr *tcp;
|
||||
struct udphdr *udp;
|
||||
struct icmphdr *icmp4;
|
||||
struct icmp6_hdr *icmp6;
|
||||
struct mpls_label *mpls;
|
||||
const char *raw;
|
||||
} header;
|
||||
const char *payload;
|
||||
};
|
||||
|
||||
int packet_get_layer_count(const struct packet *pkt);
|
||||
// return 0: success
|
||||
// return -1: failed
|
||||
int packet_get_layer(const struct packet *pkt, int idx, struct layer *out);
|
||||
|
||||
#define PACKET_FOREACH_LAYER_INORDER(pkt, layer) \
|
||||
for (int i = 0; i < packet_get_layer_count(pkt) && packet_get_layer(pkt, i, &layer) == 0; i++)
|
||||
|
||||
#define PACKET_FOREACH_LAYER_REVERSE(pkt, layer) \
|
||||
for (int i = packet_get_layer_count(pkt) - 1; i >= 0 && packet_get_layer(pkt, i, &layer) == 0; i--)
|
||||
|
||||
#define PACKET_GETALL_LAYERS(pkt, layers) \
|
||||
{ \
|
||||
int size = sizeof(layers) / sizeof(layers[0]); \
|
||||
int num = packet_get_layer_count(pkt); \
|
||||
if (num > size) \
|
||||
num = size; \
|
||||
for (int i = 0; i < num && packet_get_layer(pkt, i, &layers[i]) == 0; i++) \
|
||||
/* void */; \
|
||||
return num; \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user