summaryrefslogtreecommitdiff
path: root/common/src/decode_udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/src/decode_udp.c')
-rw-r--r--common/src/decode_udp.c48
1 files changed, 48 insertions, 0 deletions
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);
+}