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/include/stellar/layer.h
2024-06-19 15:06:14 +08:00

92 lines
2.3 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#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_GTP = 62,
};
struct layer
{
enum layer_proto proto;
uint16_t hdr_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;
char *raw;
} hdr;
};
int packet_get_layer_count(const struct packet *pkt);
// return 0: success 
// return -1: failed
int packet_get_layer_by_idx(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_by_idx(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_by_idx(pkt, i, &layer) == 0; i--)
#define PACKET_GETALL_LAYERS(pkt, layers) \
{ \
int num = MIN(packet_get_layer_count(pkt), (sizeof(layers) / sizeof(layers[0]))); \
for (int i = 0; i < num && packet_get_layer_by_idx(pkt, i, &layers[i]) == 0; i++) \
/* void */; \
return num; \
}
#ifdef __cplusplus
}
#endif