remove tuple.h from include/stellar

This commit is contained in:
luwenpeng
2024-06-07 15:17:35 +08:00
parent 4c0ad823d4
commit 10528bcfd3
13 changed files with 227 additions and 103 deletions

View File

@@ -6,7 +6,13 @@ extern "C"
{
#endif
#include "stellar/tuple.h"
#include <string.h>
#include <arpa/inet.h>
#define __FAVOR_BSD 1
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
enum layer_type
{
@@ -59,28 +65,6 @@ struct packet_layer
uint16_t pld_len; // payload length
};
// 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);
const struct packet_layer *packet_get_innermost_layer(const struct packet *pkt, enum layer_type type);
const struct packet_layer *packet_get_outermost_layer(const struct packet *pkt, enum layer_type type);
/******************************************************************************
* Utils
******************************************************************************/
#define MAX_SID_NUM 8
struct sid_list
{
@@ -117,29 +101,6 @@ void packet_set_action(struct packet *pkt, enum packet_action action);
enum packet_action packet_get_action(const struct packet *pkt);
/*
******************************************************************************
* Example: getting the innermost TCP layer
******************************************************************************
*
* |<--------------------------- pkt->data_len -------------------------->|
* +----------+------+-----+-------+------+---------------+---------------+
* | Ethernet | IPv4 | UDP | GTP-U | IPv4 | TCP | Payload |
* +----------+------+-----+-------+------+---------------+---------------+
* ^ ^ ^
* | | |
* |<------------ hdr_offset ------------>|<-- hdr_len -->|<-- pld_len -->|
* | | |
* | | +-- pld_ptr
* | +-- hdr_ptr
* +-- data_ptr
*
* const struct packet_layer *tcp_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_TCP);
* const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
* uint16_t src_port = ntohs(hdr->th_sport);
* uint16_t dst_port = ntohs(hdr->th_dport);
* uint32_t seq = ntohl(hdr->th_seq);
* uint32_t ack = ntohl(hdr->th_ack);
*
******************************************************************************
* Example: foreach layer in packet
******************************************************************************
@@ -160,6 +121,168 @@ enum packet_action packet_get_action(const struct packet *pkt);
* }
*/
struct address
{
uint8_t family; // AF_INET or AF_INET6
union
{
struct in_addr v4; /* network order */
struct in6_addr v6; /* network order */
} data;
};
static inline int packet_get_addr(const struct packet *pkt, struct address *src_addr, struct address *dst_addr)
{
const struct ip *ip4_hdr = NULL;
const struct ip6_hdr *ip6_hdr = NULL;
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_IPV4)
{
ip4_hdr = (const struct ip *)layer->hdr_ptr;
if (src_addr != NULL)
{
src_addr->family = AF_INET;
src_addr->data.v4.s_addr = ip4_hdr->ip_src.s_addr;
}
if (dst_addr != NULL)
{
dst_addr->family = AF_INET;
dst_addr->data.v4.s_addr = ip4_hdr->ip_dst.s_addr;
}
return 0;
}
if (layer->type & LAYER_TYPE_IPV6)
{
ip6_hdr = (const struct ip6_hdr *)layer->hdr_ptr;
if (src_addr != NULL)
{
src_addr->family = AF_INET6;
src_addr->data.v6 = ip6_hdr->ip6_src;
}
if (dst_addr != NULL)
{
dst_addr->family = AF_INET6;
dst_addr->data.v6 = ip6_hdr->ip6_dst;
}
return 0;
}
}
return -1;
}
static inline int packet_get_port(const struct packet *pkt, uint16_t *src_port, uint16_t *dst_port)
{
const struct tcphdr *tcp_hdr = NULL;
const struct udphdr *udp_hdr = NULL;
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_TCP)
{
tcp_hdr = (const struct tcphdr *)layer->hdr_ptr;
src_port != NULL ? *src_port = tcp_hdr->th_sport : 0;
dst_port != NULL ? *dst_port = tcp_hdr->th_dport : 0;
return 0;
}
if (layer->type & LAYER_TYPE_UDP)
{
udp_hdr = (const struct udphdr *)layer->hdr_ptr;
src_port != NULL ? *src_port = udp_hdr->uh_sport : 0;
dst_port != NULL ? *dst_port = udp_hdr->uh_dport : 0;
return 0;
}
}
return -1;
}
static inline int packet_get_ip_hdr(const struct packet *pkt, struct ip *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_IPV4)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct ip));
}
return 0;
}
}
return -1;
}
static inline int packet_get_ip6_hdr(const struct packet *pkt, struct ip6_hdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_IPV6)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct ip6_hdr));
}
return 0;
}
}
return -1;
}
static inline int packet_get_tcp_hdr(const struct packet *pkt, struct tcphdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_TCP)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct tcphdr));
}
return 0;
}
}
return -1;
}
static inline int packet_get_udp_hdr(const struct packet *pkt, struct udphdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type & LAYER_TYPE_UDP)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct udphdr));
}
return 0;
}
}
return -1;
}
#ifdef __cplusplus
}
#endif