几个常用且重要的网络数据结构

来源:互联网 发布:磁性自瞄源码 原理 编辑:程序博客网 时间:2024/04/29 22:38

 当接收到原始数据包后,为了不用指针定位所需要的字段,LINUX其实定义好了几个常用的结构。暂时先记录下来。

1,IP数据包的分析: #include <netinet/ip.h>

struct ip
  {
#if __BYTE_ORDER == __LITTLE_ENDIAN
    unsigned int ip_hl:4; /* header length */
    unsigned int ip_v:4; /* version */
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
    unsigned int ip_v:4; /* version */
    unsigned int ip_hl:4; /* header length */
#endif
    u_int8_t ip_tos; /* type of service */
    u_short ip_len; /* total length */
    u_short ip_id; /* identification */
    u_short ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
    u_int8_t ip_ttl; /* time to live */
    u_int8_t ip_p; /* protocol */
    u_short ip_sum; /* checksum */
    struct in_addr ip_src, ip_dst; /* source and dest address */
  }; 


2,ICMP 数据包的分析:#include <netinet/ip_icmp.h>

struct icmp 
{
    char icmp_type;
    char icmp_code;
    unsigned short icmp_cksum;
    unsigned short icmp_id;
    unsigned short icmp_seq;
    char icmp_data[20];
};


TCP数据包分析: #include <netinet/tcp.h>

 struct tcphdr
  {
     u_int16_t source;
      u_int16_t dest;
      u_int32_t seq;
      u_int32_t ack_seq;
  #  if __BYTE_ORDER == __LITTLE_ENDIAN
      u_int16_t res1:4;
     u_int16_t doff:4;
     u_int16_t fin:1;
     u_int16_t syn:1;
     u_int16_t rst:1;
     u_int16_t psh:1;
     u_int16_t ack:1;
     u_int16_t urg:1;
     u_int16_t res2:2;
 #  elif __BYTE_ORDER == __BIG_ENDIAN
     u_int16_t doff:4;
     u_int16_t res1:4;
     u_int16_t res2:2;
     u_int16_t urg:1;
     u_int16_t ack:1;
     u_int16_t psh:1;
     u_int16_t rst:1;
     u_int16_t syn:1;
     u_int16_t fin:1;
 #  else
 #   error "Adjust your <bits/endian.h> defines"
 #  endif
     u_int16_t window;
     u_int16_t check;
     u_int16_t urg_ptr;
 };


UDP数据包分析: #include<netinet/udp.h>

struct udphdr
  {
    u_int16_t source;
    u_int16_t dest;
    u_int16_t len;
    u_int16_t check;
  };



0 0