diff options
| author | luwenpeng <[email protected]> | 2024-06-07 15:17:35 +0800 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2024-06-07 16:50:21 +0800 |
| commit | 10528bcfd3b6cce87fd4c913899ee41b39f1b943 (patch) | |
| tree | 6599a019b5399062a21350fb129ec7617b0f0748 /include | |
| parent | 4c0ad823d488cc5aaf6470453ca0a2fca3f72ece (diff) | |
remove tuple.h from include/stellar
Diffstat (limited to 'include')
| -rw-r--r-- | include/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | include/stellar/packet.h | 215 | ||||
| -rw-r--r-- | include/stellar/session.h | 4 | ||||
| -rw-r--r-- | include/stellar/tuple.h | 89 |
4 files changed, 169 insertions, 140 deletions
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index ac87277..91b7269 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -1,5 +1,4 @@ install(FILES stellar/utils.h DESTINATION include/stellar/ COMPONENT LIBRARIES) -install(FILES stellar/tuple.h DESTINATION include/stellar/ COMPONENT LIBRARIES) install(FILES stellar/packet.h DESTINATION include/stellar/ COMPONENT LIBRARIES) install(FILES stellar/session.h DESTINATION include/stellar/ COMPONENT LIBRARIES) install(FILES stellar/stellar.h DESTINATION include/stellar/ COMPONENT LIBRARIES) diff --git a/include/stellar/packet.h b/include/stellar/packet.h index b867b6f..02060d0 100644 --- a/include/stellar/packet.h +++ b/include/stellar/packet.h @@ -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 { @@ -118,29 +102,6 @@ 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 diff --git a/include/stellar/session.h b/include/stellar/session.h index 182677a..959ab50 100644 --- a/include/stellar/session.h +++ b/include/stellar/session.h @@ -139,10 +139,6 @@ enum closing_reason session_get_closing_reason(const struct session *sess); enum session_direction session_get_direction(const struct session *sess); enum flow_direction session_get_current_flow_direction(const struct session *sess); const struct packet *session_get_first_packet(const struct session *sess, enum flow_direction dir); - -const struct tuple6 *session_get_tuple6(const struct session *sess); -enum flow_direction session_get_tuple6_direction(const struct session *sess); - uint64_t session_get_id(const struct session *sess); uint64_t session_get_timestamp(const struct session *sess, enum session_timestamp type); uint64_t session_get_stat(const struct session *sess, enum flow_direction dir, enum session_stat stat); diff --git a/include/stellar/tuple.h b/include/stellar/tuple.h deleted file mode 100644 index 8c9b2ff..0000000 --- a/include/stellar/tuple.h +++ /dev/null @@ -1,89 +0,0 @@ -#ifndef _TUPLE_PUB_H -#define _TUPLE_PUB_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include <arpa/inet.h> - -enum ip_type -{ - IP_TYPE_V4, - IP_TYPE_V6, -}; - -union ip_address -{ - struct in_addr v4; /* network order */ - struct in6_addr v6; /* network order */ -}; - -struct tuple2 -{ - enum ip_type ip_type; - union ip_address src_addr; /* network order */ - union ip_address dst_addr; /* network order */ -}; - -struct tuple4 -{ - enum ip_type ip_type; - union ip_address src_addr; /* network order */ - union ip_address dst_addr; /* network order */ - - in_port_t src_port; /* network order */ - in_port_t dst_port; /* network order */ -}; - -struct tuple5 -{ - enum ip_type ip_type; - union ip_address src_addr; /* network order */ - union ip_address dst_addr; /* network order */ - - in_port_t src_port; /* network order */ - in_port_t dst_port; /* network order */ - - uint16_t ip_proto; /* network order */ -}; - -struct tuple6 -{ - enum ip_type ip_type; - union ip_address src_addr; /* network order */ - union ip_address dst_addr; /* network order */ - - uint16_t src_port; /* network order */ - uint16_t dst_port; /* network order */ - - uint16_t ip_proto; /* network order */ - uint64_t domain; -}; - -uint32_t tuple2_hash(const struct tuple2 *tuple); -uint32_t tuple4_hash(const struct tuple4 *tuple); -uint32_t tuple5_hash(const struct tuple5 *tuple); -uint32_t tuple6_hash(const struct tuple6 *tuple); - -int tuple2_cmp(const struct tuple2 *tuple_a, const struct tuple2 *tuple_b); -int tuple4_cmp(const struct tuple4 *tuple_a, const struct tuple4 *tuple_b); -int tuple5_cmp(const struct tuple5 *tuple_a, const struct tuple5 *tuple_b); -int tuple6_cmp(const struct tuple6 *tuple_a, const struct tuple6 *tuple_b); - -void tuple2_reverse(const struct tuple2 *in, struct tuple2 *out); -void tuple4_reverse(const struct tuple4 *in, struct tuple4 *out); -void tuple5_reverse(const struct tuple5 *in, struct tuple5 *out); -void tuple6_reverse(const struct tuple6 *in, struct tuple6 *out); - -void tuple2_to_str(const struct tuple2 *tuple, char *buf, uint32_t size); -void tuple4_to_str(const struct tuple4 *tuple, char *buf, uint32_t size); -void tuple5_to_str(const struct tuple5 *tuple, char *buf, uint32_t size); -void tuple6_to_str(const struct tuple6 *tuple, char *buf, uint32_t size); - -#ifdef __cplusplus -} -#endif - -#endif |
