53 lines
1.5 KiB
C
53 lines
1.5 KiB
C
|
|
#ifndef __ETH_UDP_H__
|
||
|
|
#define __ETH_UDP_H__
|
||
|
|
|
||
|
|
#include "stdint.h"
|
||
|
|
|
||
|
|
#define UDP_CHECKSUM_ENABLE 1
|
||
|
|
|
||
|
|
struct udp_cfg_s {
|
||
|
|
char *mac_src;
|
||
|
|
char *mac_dst;
|
||
|
|
char *ip_src;
|
||
|
|
char *ip_dst;
|
||
|
|
char *port_src;
|
||
|
|
char *port_dst;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct udp_header_s {
|
||
|
|
uint8_t preamble[2]; // fixed 55 D5
|
||
|
|
/* MAC */
|
||
|
|
uint8_t mac_dst[6];
|
||
|
|
uint8_t mac_src[6];
|
||
|
|
uint8_t ip_type[2]; // 08 00 for IPv4 type
|
||
|
|
/* IP */
|
||
|
|
uint8_t ver_head_length; // 0x45 for IPv4 and head length is 5*4=20 bytes
|
||
|
|
uint8_t tos; // type of service, normal 0
|
||
|
|
uint8_t ip_total_length[2]; // 28 + user data length, e.g. 00 20 for 32 bytes
|
||
|
|
uint8_t id[2];
|
||
|
|
uint8_t offset[2];
|
||
|
|
uint8_t ttl; // time to live, normal 0x80
|
||
|
|
uint8_t prototal; // 0x11 for UDP prototal
|
||
|
|
uint8_t ip_checksum[2]; // normal 00 00
|
||
|
|
uint8_t ip_src[4]; // e.g. C0 A8 02 18 is for 192.168.2.24
|
||
|
|
uint8_t ip_dst[4]; // e.g. C0 A8 02 20 is for 192.168.2.32
|
||
|
|
/* UDP */
|
||
|
|
uint8_t port_src[2]; // e.g. 26 E5 is for 9957
|
||
|
|
uint8_t port_dst[2]; // e.g. 0E D3 is for 3795
|
||
|
|
uint8_t udp_total_length[2]; // 8 + user data length, e.g. 00 0C for 12 bytes
|
||
|
|
uint8_t udp_checksum[2];
|
||
|
|
};
|
||
|
|
|
||
|
|
#ifdef __cplusplus
|
||
|
|
extern "C" {
|
||
|
|
#endif
|
||
|
|
|
||
|
|
int udp_create_default(struct udp_cfg_s *cfg, struct udp_header_s *udp);
|
||
|
|
uint32_t *udp_copy_header(uint32_t *dst, struct udp_header_s *udp);
|
||
|
|
uint16_t udp_pack_data(struct udp_header_s *udp, uint16_t length);
|
||
|
|
|
||
|
|
#ifdef __cplusplus
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#endif /* __ETH_UDP_H__ */
|