ec_inet_structures.h
上传用户:nilegod
上传日期:2007-01-08
资源大小:220k
文件大小:5k
源码类别:

网络截获/分析

开发平台:

C/C++

  1. #include <sys/socket.h>
  2. #ifdef LINUX
  3.    #include <features.h>         /* for the glibc version number */
  4.    #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
  5.       #include <netpacket/packet.h>
  6.       #include <net/ethernet.h>     /* the L2 protocols */
  7.    #else
  8.       #include <asm/types.h>
  9.       #include <linux/if_packet.h>
  10.       #include <linux/if_ether.h>   /* The L2 protocols */
  11.       #ifdef HAVE_NET_ETHERNET_H
  12.          #include <net/ethernet.h>
  13.       #endif
  14.    #endif
  15. #endif
  16. #ifndef OPENBSD
  17.    #include <net/if_arp.h>
  18. #endif
  19. #include <sys/types.h>
  20. #include <netinet/in_systm.h>
  21. #include <net/if.h>
  22. #include <netdb.h>
  23. #include <netinet/tcp.h>
  24. #include <netinet/in.h>
  25. #include <netinet/ip.h>
  26. #include <netinet/ip_icmp.h>
  27. #include <arpa/inet.h>
  28. typedef struct {
  29.    char dest_mac[6];          // dest ethernet address
  30.    char source_mac[6];        // source ethernet address
  31.    u_short type;              // type of packet
  32. } ETH_header;
  33. typedef struct {
  34.    u_short hw_type;           // hardware type
  35.    u_short proto_type;        // protocol type
  36.    char ha_len;               // hardware address len
  37.    char pa_len;               // protocol address len
  38.    u_short opcode;            // arp opcode
  39.    char source_add[6];        // source mac
  40.    char source_ip[4];         // source ip
  41.    char dest_add[6];          // dest mac
  42.    char dest_ip[4];           // dest ip
  43. } ARP_header;
  44. typedef struct {
  45. #ifdef HAVE_MACHINE_LIL
  46.    u_char h_len:4;            // header len
  47.    u_char version:4;          // ip version
  48. #endif
  49. #ifdef HAVE_MACHINE_BIG
  50.    u_char version:4;          // ip version
  51.    u_char h_len:4;            // header len
  52. #endif
  53.    u_char tos;                // type of service
  54.    u_short t_len;             // total len
  55.    u_short ident;             // ip ident
  56.    u_short frag_and_flags;    // fragments and flags
  57.    u_char  ttl;               // time to live
  58.    u_char proto;              // transport protocol
  59.    u_short checksum;          // IP checksum
  60.    u_long source_ip;          // source ip
  61.    u_long dest_ip;            // destination ip
  62. } IP_header;
  63. typedef struct {
  64.    u_char type;               // icmp type
  65.    u_char code;               // type sub code
  66.    u_short checksum;          // ones complement checksum of struct
  67.    union
  68.    {
  69.        struct
  70.        {
  71.            u_short id;        // ident
  72.            u_short seq;       // sequence number
  73.        } echo;
  74.        u_long gateway;        // gateway address
  75.        struct
  76.        {
  77.            u_short unused;
  78.            u_short mtu;
  79.        } frag;                // path mtu discovery
  80.     } un;
  81. } ICMP_header;
  82. typedef struct
  83. {
  84.    u_short source;            // source port
  85.    u_short dest;              // destination port
  86.    u_long seq;                // sequence number
  87.    u_long ack_seq;            // acknowledgement number
  88. #ifdef HAVE_MACHINE_LIL
  89.    u_char unused:4;           // unused
  90.    u_char doff:4;             // data offset
  91. #endif
  92. #ifdef HAVE_MACHINE_BIG
  93.    u_char doff:4;             // data offset
  94.    u_char unused:4;           // unused
  95. #endif
  96.    u_char  flags;             // tcp flags
  97.    u_short window;            // window
  98.    u_short checksum;          // checksum
  99.    u_short urg_ptr;           // urgent pointer
  100. } TCP_header;
  101. typedef struct
  102. {
  103.    u_short source;             // source port
  104.    u_short dest;               // destination port
  105.    u_short len;                // packet length
  106.    u_short checksum;           // checksum
  107. } UDP_header;
  108. #define MAX_PACKET_SIZE      0xffff
  109. #define ETH_HEADER           0xe     // Etherner header:     14 bytes
  110. #define ARP_HEADER           0x1c    // ARP header:          28 bytes
  111. #define IP_HEADER            0x14    // IP header:           20 bytes
  112. #define ICMP_HEADER          0x8     // ICMP header:          8 bytes
  113. #define TCP_HEADER           0x14    // TCP header:          20 bytes
  114. #define UDP_HEADER           0x8     // UDP header:           8 bytes
  115. #define TH_FIN  0x01    // TCP Flags
  116. #define TH_SYN  0x02
  117. #define TH_RST  0x04
  118. #define TH_PSH  0x08
  119. #define TH_ACK  0x10
  120. #define TH_URG  0x20
  121. #ifndef IP_OFFMASK
  122. #define IP_OFFMASK 0x1fff
  123. #endif
  124. #ifndef SOL_PACKET      // glibc 2.1 bug
  125. #define SOL_PACKET 263
  126. #endif
  127. #ifndef ETH_P_IP
  128. #define ETH_P_IP 0x0800
  129. #endif
  130. #ifndef ETH_P_ARP
  131. #define ETH_P_ARP 0x0806
  132. #endif
  133. #ifndef MSG_TRUNC
  134. #define MSG_TRUNC 0x20
  135. #endif
  136. #ifndef ETHER_ADDR_LEN
  137. #define ETHER_ADDR_LEN 6
  138. #endif
  139. #ifndef PACKET_HOST
  140. #define PACKET_HOST 0
  141. #endif
  142. #ifndef ICMP_DEST_UNREACH
  143. #define ICMP_DEST_UNREACH 3
  144. #endif
  145. #ifndef ICMP_SOURCE_QUENCH
  146. #define ICMP_SOURCE_QUENCH 4
  147. #endif
  148. #ifdef HAVE_MACHINE_LIL
  149.    #define ptohs(x) *(u_short *)(x)
  150.    #define ptohl(x) *(u_long *)(x)
  151. #endif
  152. #ifdef HAVE_MACHINE_BIG
  153.    #define ptohs(x) ( (u_short)                       
  154.                       ((u_short)*((u_char *)p+1)<<8|  
  155.                       (u_short)*((u_char *)p+0)<<0)   
  156.                     )
  157.    #define ptohl(x) ( (u_int32_t)*((u_char *)p+3)<<24|  
  158.                       (u_int32_t)*((u_char *)p+2)<<16|  
  159.                       (u_int32_t)*((u_char *)p+1)<<8|   
  160.                       (u_int32_t)*((u_char *)p+0)<<0    
  161.                     )
  162. #endif
  163. /* EOF */