TSG-7784 PacketAdapter支持CI自动构建RPM; 修改代码结构
This commit is contained in:
56
common/src/decode_gtp.c
Normal file
56
common/src/decode_gtp.c
Normal 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
68
common/src/decode_ipv4.c
Normal 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
55
common/src/decode_ipv6.c
Normal 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
52
common/src/decode_tcp.c
Normal 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
48
common/src/decode_udp.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user