常用TCP-IP网络包头部定义C/C++版
#pragma pack(push, 1)
typedef struct mac_hdr {
uint8_t dst_mac[6]; //destination MAC address shall be defined first.
uint8_t src_mac[6];
uint16_t type;
}mac_hdr;
typedef struct ip_hdr {
#ifdef __LITTLE_ENDIAN_BIFIELD
uint8_t len : 4;
uint8_t ver : 4;
#else
uint8_t ver : 4;
uint8_t len : 4;
#endif
uint8_t tos;
uint16_t total_len;
uint16_t id;
uint16_t flags;
uint8_t ttl;
uint8_t protocol;
uint16_t chksum;
uint32_t src_addr;
uint32_t dst_addr;
}ip_hdr;
typedef struct udp_hdr {
uint16_t src_port;
uint16_t dst_port;
uint16_t len;
uint16_t sum;
}udp_hdr;
typedef struct tcp_hdr {
uint16_t src_port;
uint16_t dst_port;
uint32_t seq;
uint32_t ack_seq;
#ifdef __LITTLE_ENDIAN_BIFIELD
uint8_t res1 : 4;
uint8_t doff : 4;
uint8_t fin : 1;
uint8_t syn : 1;
uint8_t rst : 1;
uint8_t psh : 1;
uint8_t ack : 1;
uint8_t urg : 1;
uint8_t ece : 1;
uint8_t cwr : 1;
#else
uint8_t doff : 4;
uint8_t res1 : 4;
uint8_t cwr : 1;
uint8_t ece : 1;
uint8_t urg : 1;
uint8_t ack : 1;
uint8_t psh : 1;
uint8_t rst : 1;
uint8_t syn : 1;
uint8_t fin : 1;
#endif
uint16_t win;
uint16_t sum;
uint16_t urp;
}tcp_hdr;
typedef struct icmp_hdr {
uint8_t type;
uint8_t code;
uint16_t sum;
uint16_t id;
uint16_t seq;
uint16_t timestamp;
}icmp_hdr;
#pragma pack(pop)