49 lines
1.4 KiB
C
49 lines
1.4 KiB
C
#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;
|
|
}
|
|
|
|
int dump_udp_info(udp_info_t *packet, char *buff, size_t size)
|
|
{
|
|
return snprintf(buff, size,
|
|
"{\"src_port\":%u,\"dst_port\":%u,\"hdr_len\":%u,\"data_len\":%u}",
|
|
packet->src_port,
|
|
packet->dst_port,
|
|
packet->hdr_len,
|
|
packet->payload_len);
|
|
}
|