一般的なtcp/ipプロトコル構造定義

5768 ワード

http://www.cppblog.com/aurain/archive/2009/12/03/102451.html 
#ifndef _PROTO_H_
#define _PROTO_H_

/**//*
* TCP/IP     
*/
#define IPPROTO_IP        0              // IP
#define IPPROTO_ICMP    1              // ICMP
#define IPPROTO_TCP        6              // TCP
#define IPPROTO_UDP        17             // UDP

/**//*
*     
*/
#define PORT_DNS        53                // DNS

/**//*
*     
*/
#define ETH_ALEN       6              //        
#define ETH_HLEN       14             //        
#define ETH_DATA_LEN   1500           //          
#define ETH_FRAME_LEN  1514           //      ,  +    

/**//**
 *       
**/
#pragma pack(push, 1)

/**//*
*14        
*/
typedef struct _ETHDR         
{
    UCHAR    eh_dst[ETH_ALEN];            //   MAC  
    UCHAR    eh_src[ETH_ALEN];            //  MAC  
    USHORT    eh_type;                    //       , IP(ETHERTYPE_IP)、ARP(ETHERTYPE_ARP) 
} ETHDR, *PETHDR;

/**//*
*28   ARP 
*/
typedef struct _ARPHDR    
{
    USHORT    ar_hrd;                //          ,     ARPHRD_ETHER
    USHORT    ar_pro;                //        ,ETHERTYPE_IP
    UCHAR    ar_hln;                //          ,MAC      6
    UCHAR    ar_pln;                //          ,IP      4
    USHORT    ar_op;                //    ARP    ,ARPOP_REQUEST   ,ARPOP_REPLY   
    UCHAR    ar_sha[ETH_ALEN];    //     MAC  
    ULONG    ar_sip;                //     IP  
    UCHAR    ar_tha[ETH_ALEN];    //      MAC  
    ULONG    ar_tip;                //      IP  
} ARPHDR, *PARPHDR;

/**//*
*20   IP 
*/
typedef struct _IPHDR        
{
    UCHAR    h_lenver;            //        (  4 )
    UCHAR    tos;                //      
    USHORT    total_len;            //      ,   IP    
    USHORT    ident;                //     ,             
    USHORT    frag_and_flags;        //   
    UCHAR    ttl;                //     ,  TTL
    UCHAR    protocol;            //   ,   TCP、UDP、ICMP 
    USHORT    checksum;            //    
    ULONG    saddr;                //  IP  
    ULONG    daddr;                //   IP  
} IPHDR, *PIPHDR; 

/**//*
*20   TCP 
*/
typedef struct _TCPHDR    
{
    USHORT    srceport;            // 16     
    USHORT    dstport;            // 16      
    ULONG    seq;                // 32    
    ULONG    ack;                // 32    
    UCHAR    dataoffset;            //  4       
    UCHAR    flags;                // 6    
    //FIN - 0x01
    //SYN - 0x02
    //RST - 0x04 
    //PSH - 0x08
    //ACK - 0x10
    //URG - 0x20
    //ACE - 0x40
    //CWR - 0x80

    USHORT    window;                // 16     
    USHORT    checksum;            // 16    
    USHORT    urgptr;                // 16         
} TCPHDR, *PTCPHDR;

/**//*
* TCP ,        
*/
typedef struct _PSDTCPHDR
{
    ULONG    saddr;
    ULONG    daddr;
    char    mbz;
    char    ptcl;
    USHORT    tcpl;
} PSDTCPHDR, *PPSDTCPHDR;

/**//*
*8   UDP 
*/
typedef struct _UDPHDR
{
    USHORT    srcport;            //             
    USHORT    dstport;            //              
    USHORT    len;                //     
    USHORT    checksum;            //    
} UDPHDR, *PUDPHDR;

/**//*
* UDP ,        
*/
typedef struct _PSDUDPHDR
{
    ULONG    saddr;
    ULONG    daddr;
    char    mbz;
    char    ptcl;
    USHORT    udpl;
} PSDUDPHDR, *PPSDUDPHDR;

/**//*
*12   ICMP 
*/
typedef struct _ICMPHDR
{
    UCHAR   type;                //  
    UCHAR   code;                //  
    USHORT  checksum;            //   
    USHORT  id;                    //   
    USHORT  sequence;            //   
    ULONG   timestamp;            //   
} ICMPHDR, *PICMPHDR;

/**//*
*6   PPPOE +2    
*/
typedef struct _PPPOEHDR
{
    UCHAR    ver_type;            //  +      0x11
    UCHAR    code;                //  
    USHORT    sessionid;            //session id
    USHORT    len;                //  
    USHORT    protocol;            //  
} PPPOEHDR, *PPPPOEHDR;

/**//*
* dns  
*/
typedef struct _DNSHDR 
{
    USHORT id;
    USHORT flags;
    USHORT quests;
    USHORT answers;
    USHORT author;
    USHORT addition;
} DNSHDR, *PDNSHDR;

/**//* 
* dns   ,query
*/
typedef struct _DNSQUERY
{
    /**//*UCHAR *dname;*/    //     ,       0 63      
    /**//*          :
    *   :udp    -sizeof(UDPHDR)-sizeof(DNSHDR)-sizeof(DNSQUERY)
    *    dns   
    */
    USHORT    type;            //    ,   20      
    USHORT    classes;        //   ,   A    IP  
} DNSQUERY, *PDNSQUERY;

/**//* 
* dns   
*/
typedef struct _DNSRESPONSE
{
    USHORT    name;        //      
    USHORT    type;        //      
    USHORT    classes;    //    
    UINT    ttl;        //     
    USHORT    length;        //       
    UINT    addr;        //     
} DNSRESPONSE, *PDNRESPONSE;

#pragma pack(pop)

#endif