Network protocol structure
/ / i386 is little_endian.
# ifndef LITTLE_ENDIAN
# define LITTLE_ENDIAN (1) / / BYTE ORDER
# else
# error Redefine LITTLE_ORDER
# endif
/ / Mac header with a total length of 14 bytes
Typedef struct _ eth_hdr
{
Unsigned char dstmac [6]; / / destination mac address
Unsigned char srcmac [6]; / / Source mac address
Unsigned short eth_type; / / Ethernet type
} eth_hdr
/ / IP header with a total length of 20 bytes
Typedef struct _ ip_hdr
{
# if LITTLE_ENDIAN
Unsigned char ihl:4; / / header length
Unsigned char version:4, / / version
# else
Unsigned char version:4, / / version
Unsigned char ihl:4; / / header length
# endif
Unsigned char tos; / / Service type
Unsigned short tot_len; / / Total length
Unsigned short id; / / Flag
Unsigned short frag_off; / / fragment offset
Unsigned char ttl; / / time to live
Unsigned char protocol; / / protocol
Unsigned short chk_sum; / / Inspection and
Struct in_addr srcaddr; / / Source IP address
Struct in_addr dstaddr; / / destination IP address
} ip_hdr
/ / TCP header with a total length of 20 bytes
Typedef struct _ tcp_hdr
{
Unsigned short src_port; / / Source port number
Unsigned short dst_port; / / destination port number
Unsigned int seq_no; / / Serial number
Unsigned int ack_no; / / confirmation number
# if LITTLE_ENDIAN
Unsigned char reserved_1:4; / / preserves 4 of the 6-bit header length
Unsigned char thl:4; / / tcp header length
Unsigned char flag:6; / / 6-bit flag
Unsigned char reseverd_2:2; / / keep 2 of the 6 bits
# else
Unsigned char thl:4; / / tcp header length
Unsigned char reserved_1:4; / / preserves 4 of the 6-bit header length
Unsigned char reseverd_2:2; / / keep 2 of the 6 bits
Unsigned char flag:6; / / 6-bit flag
# endif
Unsigned short wnd_size; / / 16-bit window size
Unsigned short chk_sum; / / 16-bit TCP test and
Unsigned short urgt_p; / / 16 is an emergency pointer
} tcp_hdr
/ / UDP header with a total length of 8 bytes
Typedef struct _ udp_hdr
{
Unsigned short src_port; / / remote port number
Unsigned short dst_port; / / destination port number
Unsigned short uhl; / / udp header length
Unsigned short chk_sum; / / 16-bit udp test and
} udp_hdr
/ / ICMP header with a total length of 4 bytes
Typedef struct _ icmp_hdr
{
Unsigned char icmp_type; / / type
Unsigned char code; / / Code
Unsigned short chk_sum; / / 16-bit test sum
} icmp_hdr