2024-02-21 11:14:21 +08:00
|
|
|
#ifndef _UDP_UTILS_H
|
|
|
|
|
#define _UDP_UTILS_H
|
2023-12-21 20:03:43 +08:00
|
|
|
|
2024-04-21 11:30:41 +08:00
|
|
|
#ifdef __cplusplus
|
2023-12-21 20:03:43 +08:00
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#define __FAVOR_BSD 1
|
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
|
|
2024-01-03 09:57:06 +08:00
|
|
|
/*
|
|
|
|
|
* User Datagram Header Format
|
|
|
|
|
*
|
|
|
|
|
* 0 1 2 3
|
|
|
|
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
* | Source Port | Destination Port |
|
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
* | Length | Checksum |
|
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
* | Data |
|
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
*/
|
|
|
|
|
|
2024-02-21 11:14:21 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* get
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
static inline uint16_t udp_hdr_get_src_port(const struct udphdr *hdr)
|
2023-12-21 20:03:43 +08:00
|
|
|
{
|
|
|
|
|
return ntohs(hdr->uh_sport);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 11:14:21 +08:00
|
|
|
static inline uint16_t udp_hdr_get_dst_port(const struct udphdr *hdr)
|
2023-12-21 20:03:43 +08:00
|
|
|
{
|
|
|
|
|
return ntohs(hdr->uh_dport);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 11:14:21 +08:00
|
|
|
static inline uint16_t udp_hdr_get_total_len(const struct udphdr *hdr)
|
2024-01-11 16:46:33 +08:00
|
|
|
{
|
2024-02-21 11:14:21 +08:00
|
|
|
return ntohs(hdr->uh_ulen);
|
2024-01-11 16:46:33 +08:00
|
|
|
}
|
|
|
|
|
|
2024-02-21 11:14:21 +08:00
|
|
|
static inline uint16_t udp_hdr_get_checksum(const struct udphdr *hdr)
|
2024-01-11 16:46:33 +08:00
|
|
|
{
|
2024-02-21 11:14:21 +08:00
|
|
|
return ntohs(hdr->uh_sum);
|
2024-01-11 16:46:33 +08:00
|
|
|
}
|
|
|
|
|
|
2024-02-21 11:14:21 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* set
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
static inline void udp_hdr_set_src_port(struct udphdr *hdr, uint16_t port)
|
2024-01-03 09:57:06 +08:00
|
|
|
{
|
2024-02-21 11:14:21 +08:00
|
|
|
hdr->uh_sport = htons(port);
|
2024-01-03 09:57:06 +08:00
|
|
|
}
|
|
|
|
|
|
2024-02-21 11:14:21 +08:00
|
|
|
static inline void udp_hdr_set_dst_port(struct udphdr *hdr, uint16_t port)
|
2024-01-03 09:57:06 +08:00
|
|
|
{
|
2024-02-21 11:14:21 +08:00
|
|
|
hdr->uh_dport = htons(port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void udp_hdr_set_total_len(struct udphdr *hdr, uint16_t len)
|
|
|
|
|
{
|
|
|
|
|
hdr->uh_ulen = htons(len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void udp_hdr_set_checksum(struct udphdr *hdr, uint16_t sum)
|
|
|
|
|
{
|
|
|
|
|
hdr->uh_sum = htons(sum);
|
2024-01-03 09:57:06 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-21 11:30:41 +08:00
|
|
|
#ifdef __cplusplus
|
2023-12-21 20:03:43 +08:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|