summaryrefslogtreecommitdiff
path: root/common/src/decode_udp.c
blob: bebe33754746806bc317257336e313b6424bfee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
}