当前位置:文档之家› TCPIP协议头部结构体

TCPIP协议头部结构体

TCP/IP协议头部结构体网络协议结构体定义1.// i386 is little_endian.2.#ifndef LITTLE_ENDIAN3.#define LITTLE_ENDIAN (1) //BYTE ORDER4.#else5.#error Redefine LITTLE_ORDER6.#endif7.//Mac头部,总长度14字节8.typedef struct _eth_hdr9.{10. unsigned char dstmac[6]; //目标mac地址11. unsigned char srcmac[6]; //源mac地址12. unsigned short eth_type; //以太网类型13.}eth_hdr;14.//IP头部,总长度20字节15.typedef struct _ip_hdr16.{17. #if LITTLE_ENDIAN18. unsigned char ihl:4; //首部长度19. unsigned char version:4, //版本20. #else21. unsigned char version:4, //版本22. unsigned char ihl:4; //首部长度23. #endif24. unsigned char tos; //服务类型25. unsigned short tot_len; //总长度26. unsigned short id; //标志27. unsigned short frag_off; //分片偏移28. unsigned char ttl; //生存时间29. unsigned char protocol; //协议30. unsigned short chk_sum; //检验和31.struct in_addr srcaddr; //源IP地址32.struct in_addr dstaddr; //目的IP地址33.}ip_hdr;34.//TCP头部,总长度20字节35.typedef struct _tcp_hdr36.{37. unsigned short src_port; //源端口号38. unsigned short dst_port; //目的端口号39. unsigned int seq_no; //序列号40. unsigned int ack_no; //确认号41. #if LITTLE_ENDIAN42. unsigned char reserved_1:4; //保留6位中的4位首部长度43. unsigned char thl:4; //tcp头部长度44. unsigned char flag:6; //6位标志45. unsigned char reseverd_2:2; //保留6位中的2位46. #else47. unsigned char thl:4; //tcp头部长度48. unsigned char reserved_1:4; //保留6位中的4位首部长度49. unsigned char reseverd_2:2; //保留6位中的2位50. unsigned char flag:6; //6位标志51. #endif52. unsigned short wnd_size; //16位窗口大小53. unsigned short chk_sum; //16位TCP检验和54. unsigned short urgt_p; //16为紧急指针55.}tcp_hdr;56.//UDP头部,总长度8字节57.typedef struct _udp_hdr58.{59. unsigned short src_port; //远端口号60. unsigned short dst_port; //目的端口号61. unsigned short uhl; //udp头部长度62. unsigned short chk_sum; //16位udp检验和63.}udp_hdr;64.//ICMP头部,总长度4字节65.typedef struct _icmp_hdr66.{67. unsigned char icmp_type; //类型68. unsigned char code; //代码69. unsigned short chk_sum; //16位检验和70.}icmp_hdr;全面的网络协议结构体定义1./*********************************************/2.//计算机网络各种协议的结构3.#define ETHER_ADDR_LEN 6 //NIC物理地址占6字节4.#define MAXDATA 102405./*6.网络实验程序7.数据包中的TCP包头,IP包头,UDP包头,ARP包,Ethernet包等.8.以及各种表.路由寻址表,地址解析协议表DNS表等9.*/10.#define ETHERTYPE_IP 0x0800 //IP Protocal11.#define ETHERTYPE_ARP 0x0806 //Address Resolution Protocal12.#define ETHERTYPE_REVARP 0x0835 //Reverse Address Resolution Protocal 逆地址解析协议13./*********************************************/14.//ethernet15.typedef struct ether_header16.{17. u_char ether_dhost[ETHER_ADDR_LEN];18. u_char ether_shost[ETHER_ADDR_LEN];19. u_short ether_type;20.}ETH_HEADER;21./*********************************************/22.//ether_header eth;23./*********************************************/24.//arp25.typedef struct arphdr26.{27. u_short ar_hrd;28. u_short ar_pro;29. u_char ar_hln;30. u_char ar_pln;31. u_short ar_op;32.}ARP_HEADER;33./*********************************************/34./*********************************************/35.//IP报头36.typedef struct ip37.{38. u_int ip_v:4; //version(版本)39. u_int ip_hl:4; //header length(报头长度)40. u_char ip_tos;41. u_short ip_len;42. u_short ip_id;43. u_short ip_off;44. u_char ip_ttl;45. u_char ip_p;46. u_short ip_sum;47.struct in_addr ip_src;48.struct in_addr ip_dst;49.}IP_HEADER;50./*********************************************/51./*********************************************/52.//TCP报头结构体53.typedef struct tcphdr54.{55. u_short th_sport;56. u_short th_dport;57. u_int th_seq;58. u_int th_ack;59. u_int th_off:4;60. u_int th_x2:4;61. u_char th_flags;62. u_short th_win;63. u_short th_sum;64. u_short th_urp;65.}TCP_HEADER;66.#define TH_FIN 0x0167.#define TH_SYN 0x0268.#define TH_RST 0x0469.#define TH_PUSH 0x0870.#define TH_ACK 0x1071.#define TH_URG 0x2072./*********************************************/73./*********************************************/74.//UDP报头结构体*/75.typedef struct udphdr76.{77. u_short uh_sport;78. u_short uh_dport;79. u_short uh_ulen;80. u_short uh_sum;81.}UDP_HEADER;82./*********************************************/83.//=============================================84./*********************************************/85./*ARP与ETHERNET生成的报头*/86.typedef struct ether_arp87.{88.struct arphdr ea_hdr;89. u_char arp_sha[ETHER_ADDR_LEN];90. u_char arp_spa[4];91. u_char arp_tha[ETHER_ADDR_LEN];92. u_char arp_tpa[4];93.}ETH_ARP;94.#define arp_hrd ea_hdr.ar_hrd95.#define arp_pro ea_hdr.ar_pro96.#define arp_hln ea_hdr.ar_hln97.#define arp_pln ea_hdr.ar_pln98.#define arp_op ea_hdr.ar_op99.#define ARPHRD 1100./*********************************************/ 101./*********************************************/ 102.//tcp与ip生成的报头103.typedef struct packet_tcp104.{105.struct ip ip;106.struct tcphdr tcp;107. u_char data[MAXDATA];108.}TCP_IP;109./*********************************************/ 110./*********************************************/ 111.//udp与ip生成的报头112.typedef struct packet_udp113.{114.struct ip ip;115.struct udphdr udp;116.}UDP_IP;117./*********************************************/ 118./*********************************************/ 119.//ICMP的各种形式120.//icmpx,x==icmp_type;121.//icmp报文(能到达目的地,响应-请求包)122.struct icmp8123.{124. u_char icmp_type; //type of message(报文类型) 125. u_char icmp_code; //type sub code(报文类型子码) 126. u_short icmp_cksum;127. u_short icmp_id;128. u_short icmp_seq;129. char icmp_data[1];130.};131.//icmp报文(能返回目的地,响应-应答包)132.struct icmp0133.{134. u_char icmp_type; //type of message(报文类型)135. u_char icmp_code; //type sub code(报文类型子码) 136. u_short icmp_cksum;137. u_short icmp_id;138. u_short icmp_seq;139. char icmp_data[1];140.};141.//icmp报文(不能到达目的地)142.struct icmp3143.{144. u_char icmp_type; //type of message(报文类型) 145. u_char icmp_code; //type sub code(报文类型子码),例如:0网络原因不能到达,1主机原因不能到达...146. u_short icmp_cksum;147. u_short icmp_pmvoid;148. u_short icmp_nextmtu;149. char icmp_data[1];150.};151.//icmp报文(重发结构体)152.struct icmp5153.{154. u_char icmp_type; //type of message(报文类型) 155. u_char icmp_code; //type sub code(报文类型子码) 156. u_short icmp_cksum;157.struct in_addr icmp_gwaddr;158. char icmp_data[1];159.};160.struct icmp11161.{162. u_char icmp_type; //type of message(报文类型) 163. u_char icmp_code; //type sub code(报文类型子码) 164. u_short icmp_cksum;165. u_int icmp_void;166. char icmp_data[1];167.};========================================== ======================================IP协议IP协议(Internet Protocol)是网络层协议,用在因特网上,TCP,UDP,ICMP,IGMP数据都是按照IP数据格式发送得。

相关主题