Network protocol architecture-02
/ * /
# define ETHER_ADDR_LEN 6 / / NIC physical address occupies 6 bytes
# define MAXDATA 10240
/ *
TCP header, IP header, UDP header, ARP packet, Ethernet packet, etc.
And all kinds of watches. Routing address table, address resolution protocol table, DNS table, etc.
, /
# define ETHERTYPE_IP 0x0800 / / IP Protocal
# define ETHERTYPE_ARP 0x0806 / / Address Resolution Protocal
# define ETHERTYPE_REVARP 0x0835 / / Reverse Address Resolution Protocal inverse address Resolution Protocol
/ * /
/ / ethernet
Typedef struct ether_header
{
U_char ether_ dhost[ETHER _ ADDR_LEN]
U_char ether_ shost[ETHER _ ADDR_LEN]
U_short ether_type
} ETH_HEADER
/ / arp
Typedef struct arphdr
{
U_short ar_hrd
U_short ar_pro
U_char ar_hln
U_char ar_pln
U_short ar_op
} ARP_HEADER
/ / IP header
Typedef struct ip
{
U_int ip_v:4; / / version (version)
U_int ip_hl:4; / / header length (header length)
U_char ip_tos
U_short ip_len
U_short ip_id
U_short ip_off
U_char ip_ttl
U_char ip_p
U_short ip_sum
Struct in_addr ip_src
Struct in_addr ip_dst
} IP_HEADER
/ / TCP header structure
Typedef struct tcphdr
{
U_short th_sport
U_short th_dport
U_int th_seq
U_int th_ack
U_int th_off:4
U_int th_x2:4
U_char th_flags
U_short th_win
U_short th_sum
U_short th_urp
} TCP_HEADER
# define TH_FIN 0x01
# define TH_SYN 0x02
# define TH_RST 0x04
# define TH_PUSH 0x08
# define TH_ACK 0x10
# define TH_URG 0x20
/ / UDP header structure * /
Typedef struct udphdr
{
U_short uh_sport
U_short uh_dport
U_short uh_ulen
U_short uh_sum
} UDP_HEADER
/ * header generated by ARP and ETHERNET * /
Typedef struct ether_arp
{
Struct arphdr ea_hdr
U_char arp_ sha[ETHER _ ADDR_LEN]
U_char arp_spa [4]
U_char arp_ tha[ETHER _ ADDR_LEN]
U_char arp_tpa [4]
} ETH_ARP
# define arp_hrd ea_hdr.ar_hrd
# define arp_pro ea_hdr.ar_pro
# define arp_hln ea_hdr.ar_hln
# define arp_pln ea_hdr.ar_pln
# define arp_op ea_hdr.ar_op
# define ARPHRD 1
/ / headers generated by tcp and ip
Typedef struct packet_tcp
{
Struct ip ip
Struct tcphdr tcp
U_char data [MAXDATA]
} TCP_IP
/ / headers generated by udp and ip
Typedef struct packet_udp
{
Struct ip ip
Struct udphdr udp
} UDP_IP
/ / various forms of ICMP
/ / icmpx,x==icmp_type
/ / icmp message (can reach the destination, response-request packet)
Struct icmp8
{
U_char icmp_type; / / type of message (message type)
U_char icmp_code; / / type sub code (message type subcode)
U_short icmp_cksum
U_short icmp_id
U_short icmp_seq
Char icmp_data [1]
}
/ / icmp message (can return to destination, response-response packet)
Struct icmp0
{
U_char icmp_type; / / type of message (message type)
U_char icmp_code; / / type sub code (message type subcode)
U_short icmp_cksum
U_short icmp_id
U_short icmp_seq
Char icmp_data [1]
}
/ / icmp message (destination unreachable)
Struct icmp3
{
U_char icmp_type; / / type of message (message type)
U_char icmp_code; / / type sub code (message type subcode), for example: 0 network reason can not reach, 1 host reason can not reach.
U_short icmp_cksum
U_short icmp_pmvoid
U_short icmp_nextmtu
Char icmp_data [1]
}
/ / icmp message (retransmission structure)
Struct icmp5
{
U_char icmp_type; / / type of message (message type)
U_char icmp_code; / / type sub code (message type subcode)
U_short icmp_cksum
Struct in_addr icmp_gwaddr
Char icmp_data [1]
}
Struct icmp11
{
U_char icmp_type; / / type of message (message type)
U_char icmp_code; / / type sub code (message type subcode)
U_short icmp_cksum
U_int icmp_void
Char icmp_data [1]
}