TSG-7784 PacketAdapter支持CI自动构建RPM; 修改代码结构

This commit is contained in:
卢文朋
2021-09-13 02:12:29 +00:00
committed by luwenpeng
parent aa887fd382
commit ba21a53bb7
35 changed files with 2878 additions and 915 deletions

2
common/CMakeLists.txt Normal file
View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

41
common/include/public.h Normal file
View File

@@ -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

56
common/src/decode_gtp.c Normal file
View File

@@ -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);
}

68
common/src/decode_ipv4.c Normal file
View File

@@ -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);
}

55
common/src/decode_ipv6.c Normal file
View File

@@ -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);
}

52
common/src/decode_tcp.c Normal file
View File

@@ -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);
}

48
common/src/decode_udp.c Normal file
View File

@@ -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);
}