diff options
| author | 卢文朋 <[email protected]> | 2021-09-13 02:12:29 +0000 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2021-09-15 16:58:01 +0800 |
| commit | ba21a53bb7c8bc5e70eae278b2fd238c2bcd1337 (patch) | |
| tree | f15571a0b8c096532be99e8a6858370c047a4b86 /common | |
| parent | aa887fd382a4eb1b981280183a780649230d71e9 (diff) | |
TSG-7784 PacketAdapter支持CI自动构建RPM; 修改代码结构
Diffstat (limited to 'common')
| -rw-r--r-- | common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | common/include/decode_gtp.h | 36 | ||||
| -rw-r--r-- | common/include/decode_ipv4.h | 55 | ||||
| -rw-r--r-- | common/include/decode_ipv6.h | 58 | ||||
| -rw-r--r-- | common/include/decode_tcp.h | 46 | ||||
| -rw-r--r-- | common/include/decode_udp.h | 40 | ||||
| -rw-r--r-- | common/include/public.h | 41 | ||||
| -rw-r--r-- | common/src/decode_gtp.c | 56 | ||||
| -rw-r--r-- | common/src/decode_ipv4.c | 68 | ||||
| -rw-r--r-- | common/src/decode_ipv6.c | 55 | ||||
| -rw-r--r-- | common/src/decode_tcp.c | 52 | ||||
| -rw-r--r-- | common/src/decode_udp.c | 48 |
12 files changed, 557 insertions, 0 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt new file mode 100644 index 0000000..c9d83f3 --- /dev/null +++ b/common/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(common src/decode_ipv4.c src/decode_ipv6.c src/decode_tcp.c src/decode_udp.c src/decode_gtp.c) +target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
\ No newline at end of file diff --git a/common/include/decode_gtp.h b/common/include/decode_gtp.h new file mode 100644 index 0000000..87d9fd9 --- /dev/null +++ b/common/include/decode_gtp.h @@ -0,0 +1,36 @@ +#ifndef _DECODE_GTP_H +#define _DECODE_GTP_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include "public.h" + + /* According to 3GPP TS 29.060. */ + typedef struct gtp1_header_s + { + uint8_t flags; + uint8_t type; + uint16_t length; + uint32_t tid; + } __attribute__((packed)) gtp1_header_t; + + typedef struct gtp_info_s + { + gtp1_header_t *hdr; + uint8_t *payload; + + uint32_t hdr_len; + uint32_t payload_len; + } gtp_info_t; + + int decode_gtp(gtp_info_t *packet, const uint8_t *data, uint32_t len); + void dump_gtp_info(uint32_t pkt_id, gtp_info_t *packet); + +#ifdef __cpluscplus +} +#endif + +#endif
\ No newline at end of file diff --git a/common/include/decode_ipv4.h b/common/include/decode_ipv4.h new file mode 100644 index 0000000..f3e3fa8 --- /dev/null +++ b/common/include/decode_ipv4.h @@ -0,0 +1,55 @@ +#ifndef _DECODE_IPV4_H +#define _DECODE_IPV4_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include "public.h" + +#define IPV4_HEADER_LEN 20 + + typedef struct ipv4_header_s + { + uint8_t ip_verhl; // version & header length + uint8_t ip_tos; + uint16_t ip_len; + uint16_t ip_id; + uint16_t ip_off; + uint8_t ip_ttl; + uint8_t ip_proto; + uint16_t ip_csum; + union + { + struct + { + struct in_addr ip_src; + struct in_addr ip_dst; + } ip4_un1; + uint16_t ip_addrs[4]; + } ip4_hdrun1; + } __attribute__((__packed__)) ipv4_header_t; + + typedef struct ipv4_info_s + { + char src_addr[INET_ADDRSTRLEN]; + char dst_addr[INET_ADDRSTRLEN]; + + ipv4_header_t *hdr; + uint8_t *payload; + uint8_t next_protocol; + + uint32_t hdr_len; + uint32_t opts_len; + uint32_t payload_len; + } ipv4_info_t; + + int decode_ipv4(ipv4_info_t *packet, const uint8_t *data, uint32_t len); + void dump_ipv4_info(uint32_t pkt_id, ipv4_info_t *packet); + +#ifdef __cpluscplus +} +#endif + +#endif
\ No newline at end of file diff --git a/common/include/decode_ipv6.h b/common/include/decode_ipv6.h new file mode 100644 index 0000000..9bb0d36 --- /dev/null +++ b/common/include/decode_ipv6.h @@ -0,0 +1,58 @@ +#ifndef _DECODE_IPV6_H +#define _DECODE_IPV6_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include "public.h" + +#define IPV6_HEADER_LEN 40 + + typedef struct ipv6_header_s + { + union + { + struct ip6_un1_ + { + uint32_t ip6_un1_flow; /* 20 bits of flow-ID */ + uint16_t ip6_un1_plen; /* payload length */ + uint8_t ip6_un1_nxt; /* next header */ + uint8_t ip6_un1_hlim; /* hop limit */ + } ip6_un1; + uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits class */ + } ip6_hdrun; + + union + { + struct + { + uint32_t ip6_src[4]; + uint32_t ip6_dst[4]; + } ip6_un2; + uint16_t ip6_addrs[16]; + } ip6_hdrun2; + } __attribute__((__packed__)) ipv6_header_t; + + typedef struct ipv6_info_s + { + char src_addr[INET6_ADDRSTRLEN]; + char dst_addr[INET6_ADDRSTRLEN]; + + ipv6_header_t *hdr; + uint8_t *payload; + uint8_t next_protocol; + + uint32_t hdr_len; + uint32_t payload_len; + } ipv6_info_t; + + int decode_ipv6(ipv6_info_t *packet, const uint8_t *data, uint32_t len); + void dump_ipv6_info(uint32_t pkt_id, ipv6_info_t *packet); + +#ifdef __cpluscplus +} +#endif + +#endif
\ No newline at end of file diff --git a/common/include/decode_tcp.h b/common/include/decode_tcp.h new file mode 100644 index 0000000..27cc14b --- /dev/null +++ b/common/include/decode_tcp.h @@ -0,0 +1,46 @@ +#ifndef _DECODE_TCP_H +#define _DECODE_TCP_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include "public.h" + +#define TCP_HEADER_LEN 20 + + typedef struct tcp_header_s + { + uint16_t th_sport; /**< source port */ + uint16_t th_dport; /**< destination port */ + uint32_t th_seq; /**< sequence number */ + uint32_t th_ack; /**< acknowledgement number */ + uint8_t th_offx2; /**< offset and reserved */ + uint8_t th_flags; /**< pkt flags */ + uint16_t th_win; /**< pkt window */ + uint16_t th_sum; /**< checksum */ + uint16_t th_urp; /**< urgent pointer */ + } __attribute__((__packed__)) tcp_header_t; + + typedef struct tcp_info_s + { + uint16_t src_port; + uint16_t dst_port; + + tcp_header_t *hdr; + uint8_t *payload; + + uint32_t opt_len; + uint32_t hdr_len; + uint32_t payload_len; + } tcp_info_t; + + int decode_tcp(tcp_info_t *packet, const uint8_t *data, uint32_t len); + void dump_tcp_info(uint32_t pkt_id, tcp_info_t *packet); + +#ifdef __cpluscplus +} +#endif + +#endif
\ No newline at end of file diff --git a/common/include/decode_udp.h b/common/include/decode_udp.h new file mode 100644 index 0000000..7f8ba45 --- /dev/null +++ b/common/include/decode_udp.h @@ -0,0 +1,40 @@ +#ifndef _DECODE_UDP_H +#define _DECODE_UDP_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include "public.h" + +#define UDP_HEADER_LEN 8 + + typedef struct udp_header_s + { + uint16_t udp_src_port; + uint16_t udp_dst_port; + uint16_t udp_len; + uint16_t udp_sum; + } __attribute__((__packed__)) udp_header_t; + + typedef struct udp_info_s + { + uint16_t src_port; + uint16_t dst_port; + + udp_header_t *hdr; + uint8_t *payload; + + uint32_t hdr_len; + uint32_t payload_len; + } udp_info_t; + + int decode_udp(udp_info_t *packet, const uint8_t *data, uint32_t len); + void dump_udp_info(uint32_t pkt_id, udp_info_t *packet); + +#ifdef __cpluscplus +} +#endif + +#endif
\ No newline at end of file diff --git a/common/include/public.h b/common/include/public.h new file mode 100644 index 0000000..60f3048 --- /dev/null +++ b/common/include/public.h @@ -0,0 +1,41 @@ +#ifndef _PUBLIC_H +#define _PUBLIC_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <arpa/inet.h> + +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define IP_GET_RAW_VER(raw_pkt) ((((raw_pkt)[0] & 0xf0) >> 4)) + +#define PRINT_FILE_INFO 0 + +#if (PRINT_FILE_INFO) +#define LOG_DEBUG(format, ...) \ + fprintf(stdout, "[%s-%s()-%05d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); + +#define LOG_ERROR(format, ...) \ + fprintf(stderr, "[%s-%s()-%05d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); +#else +#define LOG_DEBUG(format, ...) \ + fprintf(stdout, format "\n", ##__VA_ARGS__); + +#define LOG_ERROR(format, ...) \ + fprintf(stderr, format "\n", ##__VA_ARGS__); +#endif + +#ifdef __cpluscplus +} +#endif + +#endif
\ No newline at end of file diff --git a/common/src/decode_gtp.c b/common/src/decode_gtp.c new file mode 100644 index 0000000..92dc528 --- /dev/null +++ b/common/src/decode_gtp.c @@ -0,0 +1,56 @@ +#include "decode_gtp.h" + +#define GTP_TPDU 255 +#define GTP1U_PORT 2152 +#define GTP1_F_MASK 0x07 + +#define GTP1_GET_TYPE(gtp1_hdr) ((gtp1_hdr)->type) +#define GTP1_GET_FLAGS(gtp1_hdr) ((gtp1_hdr)->flags >> 5) +#define GTP1_GET_HLEN(gtp1_hdr) (((gtp1_hdr)->flags & GTP1_F_MASK) > 0 ? 12 : 8) + +enum gtp_version_e +{ + GTP_V0 = 0, + GTP_V1, +}; + +int decode_gtp(gtp_info_t *packet, const uint8_t *data, uint32_t len) +{ + if (len < sizeof(gtp1_header_t)) + { + LOG_ERROR("Parser GTP Header: packet length too small %d", len); + return -1; + } + + packet->hdr = (gtp1_header_t *)data; + if (GTP1_GET_FLAGS(packet->hdr) != GTP_V1) + { + LOG_ERROR("Parser GTP Header: invalid gtp flags %d", GTP1_GET_FLAGS(packet->hdr)); + return -1; + } + + if (GTP1_GET_TYPE(packet->hdr) != GTP_TPDU) + { + LOG_ERROR("Parser GTP Header: invalid gtp type %d", GTP1_GET_TYPE(packet->hdr)); + return -1; + } + + /* From 29.060: "This field shall be present if and only if any one or + * more of the S, PN and E flags are set.". + * + * If any of the bit is set, then the remaining ones also have to be set. + */ + packet->hdr_len = GTP1_GET_HLEN(packet->hdr); + packet->payload = (uint8_t *)data + packet->hdr_len; + packet->payload_len = len - packet->hdr_len; + + return 0; +} + +void dump_gtp_info(uint32_t pkt_id, gtp_info_t *packet) +{ + LOG_DEBUG("id: %u, gtp_info: {hdr_len: %u, data_len: %u}", + pkt_id, + packet->hdr_len, + packet->payload_len); +}
\ No newline at end of file diff --git a/common/src/decode_ipv4.c b/common/src/decode_ipv4.c new file mode 100644 index 0000000..8b98975 --- /dev/null +++ b/common/src/decode_ipv4.c @@ -0,0 +1,68 @@ +#include "decode_ipv4.h" + +#define IPV4_GET_HLEN(ip4_hdr) (((ip4_hdr)->ip_verhl & 0x0f) << 2) +#define IPV4_GET_IPPROTO(ip4_hdr) ((ip4_hdr)->ip_proto) +#define IPV4_GET_IPLEN(ip4_hdr) ((uint16_t)ntohs((ip4_hdr)->ip_len)) +#define IPV4_GET_SRC_ADDR(ip4_hdr) ((ip4_hdr)->ip4_hdrun1.ip4_un1.ip_src) +#define IPV4_GET_DST_ADDR(ip4_hdr) ((ip4_hdr)->ip4_hdrun1.ip4_un1.ip_dst) + +int decode_ipv4(ipv4_info_t *packet, const uint8_t *data, uint32_t len) +{ + // 检查包长是否大于 IPv4 header + if (len < IPV4_HEADER_LEN) + { + LOG_ERROR("Parser IPv4 Header: packet length too small %d", len); + return -1; + } + + // 检查 IPv4 header version + if (IP_GET_RAW_VER(data) != 4) + { + LOG_ERROR("Parser IPv4 Header: invalid IP version %d", IP_GET_RAW_VER(data)); + return -1; + } + + packet->hdr = (ipv4_header_t *)data; + // 检查 IPv4 header length + if (IPV4_GET_HLEN(packet->hdr) < IPV4_HEADER_LEN) + { + LOG_ERROR("Parser IPv4 Header: invalid IP header length %d", IPV4_GET_HLEN(packet->hdr)); + return -1; + } + + // 检查 IPv4 header total length + if (IPV4_GET_IPLEN(packet->hdr) < IPV4_GET_HLEN(packet->hdr)) + { + LOG_ERROR("Parser IPv4 Header: invalid IP header total length %d", IPV4_GET_IPLEN(packet->hdr)); + return -1; + } + + // 检查是否 IP 分片 + if (len < IPV4_GET_IPLEN(packet->hdr)) + { + LOG_ERROR("Parser IPv4 Header: trunc packet"); + return -1; + } + + inet_ntop(AF_INET, &IPV4_GET_SRC_ADDR(packet->hdr), packet->src_addr, sizeof(packet->src_addr)); + inet_ntop(AF_INET, &IPV4_GET_DST_ADDR(packet->hdr), packet->dst_addr, sizeof(packet->dst_addr)); + + packet->next_protocol = IPV4_GET_IPPROTO(packet->hdr); + packet->hdr_len = IPV4_GET_HLEN(packet->hdr); + packet->opts_len = packet->hdr_len - IPV4_HEADER_LEN; + packet->payload_len = len - packet->hdr_len; + packet->payload = (uint8_t *)data + packet->hdr_len; + + return 0; +} + +void dump_ipv4_info(uint32_t pkt_id, ipv4_info_t *packet) +{ + LOG_DEBUG("id: %u, ipv4_info: {src_addr: %s, dst_addr: %s, hdr_len: %u, opt_len: %u, data_len: %u}", + pkt_id, + packet->src_addr, + packet->dst_addr, + packet->hdr_len, + packet->opts_len, + packet->payload_len); +}
\ No newline at end of file diff --git a/common/src/decode_ipv6.c b/common/src/decode_ipv6.c new file mode 100644 index 0000000..a583d54 --- /dev/null +++ b/common/src/decode_ipv6.c @@ -0,0 +1,55 @@ +#include "decode_ipv6.h" + +#define IPV6_GET_PLEN(ip6_hdr) ((uint16_t)ntohs((ip6_hdr)->ip6_hdrun.ip6_un1.ip6_un1_plen)) +#define IPV6_GET_NH(ip6_hdr) ((ip6_hdr)->ip6_hdrun.ip6_un1.ip6_un1_nxt) +#define IPV6_GET_SRC_ADDR(ip6_hdr) ((ip6_hdr)->ip6_hdrun2.ip6_un2.ip6_src) +#define IPV6_GET_DST_ADDR(ip6_hdr) ((ip6_hdr)->ip6_hdrun2.ip6_un2.ip6_dst) + +int decode_ipv6(ipv6_info_t *packet, const uint8_t *data, uint32_t len) +{ + if (len < IPV6_HEADER_LEN) + { + LOG_ERROR("Parser IPv6 Header: packet length too small %d", len); + return -1; + } + + // 检查 IPv6 header version + if (IP_GET_RAW_VER(data) != 6) + { + LOG_ERROR("Parser IPv6 Header: invalid IP version %d", IP_GET_RAW_VER(data)); + return -1; + } + + packet->hdr = (ipv6_header_t *)data; + if (len < (IPV6_HEADER_LEN + IPV6_GET_PLEN(packet->hdr))) + { + LOG_ERROR("Parser IPv6 Header: trunc packet"); + return -1; + } + + if (len != (IPV6_HEADER_LEN + IPV6_GET_PLEN(packet->hdr))) + { + LOG_ERROR("Parser IPv6 Header: invalid payload length %d", IPV6_GET_PLEN(packet->hdr)); + return -1; + } + + inet_ntop(AF_INET6, &IPV6_GET_SRC_ADDR(packet->hdr), packet->src_addr, sizeof(packet->src_addr)); + inet_ntop(AF_INET6, &IPV6_GET_DST_ADDR(packet->hdr), packet->dst_addr, sizeof(packet->dst_addr)); + + packet->next_protocol = IPV6_GET_NH(packet->hdr); + packet->hdr_len = IPV6_HEADER_LEN; + packet->payload = (uint8_t *)data + packet->hdr_len; + packet->payload_len = len - packet->hdr_len; + + return 0; +} + +void dump_ipv6_info(uint32_t pkt_id, ipv6_info_t *packet) +{ + LOG_DEBUG("id: %u, ipv6_info: {src_addr: %s, dst_addr: %s, hdr_len: %u, data_len: %u}", + pkt_id, + packet->src_addr, + packet->dst_addr, + packet->hdr_len, + packet->payload_len); +}
\ No newline at end of file diff --git a/common/src/decode_tcp.c b/common/src/decode_tcp.c new file mode 100644 index 0000000..75a1ab8 --- /dev/null +++ b/common/src/decode_tcp.c @@ -0,0 +1,52 @@ +#include "decode_tcp.h" + +#define TCP_OPTLENMAX 40 + +#define TCP_GET_HLEN(tcp_hdr) ((((tcp_hdr)->th_offx2 & 0xf0) >> 4) << 2) +#define TCP_GET_SRC_PORT(tcp_hdr) ((uint16_t)ntohs((tcp_hdr)->th_sport)) +#define TCP_GET_DST_PORT(tcp_hdr) ((uint16_t)ntohs((tcp_hdr)->th_dport)) + +int decode_tcp(tcp_info_t *packet, const uint8_t *data, uint32_t len) +{ + if (len < TCP_HEADER_LEN) + { + LOG_ERROR("Parser TCP Header: packet length too small %d", len); + return -1; + } + + packet->hdr = (tcp_header_t *)data; + uint8_t hlen = TCP_GET_HLEN(packet->hdr); + if (len < hlen) + { + LOG_ERROR("Parser TCP Header: TCP packet too small %d", len); + return -1; + } + + uint8_t tcp_opt_len = hlen - TCP_HEADER_LEN; + if (tcp_opt_len > TCP_OPTLENMAX) + { + LOG_ERROR("Parser TCP Header: invalid opt length %d", tcp_opt_len); + return -1; + } + + packet->opt_len = tcp_opt_len; + packet->src_port = TCP_GET_SRC_PORT(packet->hdr); + packet->dst_port = TCP_GET_DST_PORT(packet->hdr); + + packet->hdr_len = hlen; + packet->payload = (uint8_t *)data + packet->hdr_len; + packet->payload_len = len - packet->hdr_len; + + return 0; +} + +void dump_tcp_info(uint32_t pkt_id, tcp_info_t *packet) +{ + LOG_DEBUG("id: %u, tcp_info: {src_port: %u, dst_port: %u, hdr_len: %u, opt_len: %u, data_len:%u}", + pkt_id, + packet->src_port, + packet->dst_port, + packet->hdr_len, + packet->opt_len, + packet->payload_len); +}
\ No newline at end of file diff --git a/common/src/decode_udp.c b/common/src/decode_udp.c new file mode 100644 index 0000000..bebe337 --- /dev/null +++ b/common/src/decode_udp.c @@ -0,0 +1,48 @@ +#include "decode_udp.h" + +#define UDP_GET_LEN(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_len)) +#define UDP_GET_SRC_PORT(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_src_port)) +#define UDP_GET_DST_PORT(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_dst_port)) + +int decode_udp(udp_info_t *packet, const uint8_t *data, uint32_t len) +{ + if (len < UDP_HEADER_LEN) + { + LOG_ERROR("Parser UDP Header: packet length too small %d", len); + return -1; + } + + packet->hdr = (udp_header_t *)data; + // 检查 UDP header len + if (len < UDP_GET_LEN(packet->hdr)) + { + LOG_ERROR("Parser UDP Header: UDP packet too small %d", len); + return -1; + } + + // 检查 UDP header len + if (len != UDP_GET_LEN(packet->hdr)) + { + LOG_ERROR("Parser UDP Header: invalid UDP header length %d", UDP_GET_LEN(packet->hdr)); + return -1; + } + + packet->src_port = UDP_GET_SRC_PORT(packet->hdr); + packet->dst_port = UDP_GET_DST_PORT(packet->hdr); + + packet->hdr_len = UDP_HEADER_LEN; + packet->payload = (uint8_t *)data + UDP_HEADER_LEN; + packet->payload_len = len - UDP_HEADER_LEN; + + return 0; +} + +void dump_udp_info(uint32_t pkt_id, udp_info_t *packet) +{ + LOG_DEBUG("id: %u, udp_info: {src_port: %u, dst_port: %u, hdr_len: %u, data_len: %u}", + pkt_id, + packet->src_port, + packet->dst_port, + packet->hdr_len, + packet->payload_len); +} |
