常见tcp/ip协议结构定义

#ifndef _PROTO_H_
#define _PROTO_H_

/**//*
* TCP/IP 协议类型
*/
#define IPPROTO_IP         0              // IP
#define IPPROTO_ICMP       0x01              // ICMP
#define IPPROTO_TCP        0x06              // TCP
#define IPPROTO_UDP        0x11             // 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)

/* 4 bytes IP address */
typedef struct ip_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;

/* EThernet header 14bytes */
typedef struct ethernet_header
{
u_char src_mac[6];
u_char dest_mac[6];
u_short eth_type;
}ethernet_header;


/*ARP header */
typedef struct arp_header  
{
u_short hardware_type; //    硬件地址类型,以太网中为ARPHRD_ETHER               
u_short procotol_type; // 协议地址类型,ETHERTYPE_IP                            
u_char   addr_len;      //    硬件地址长度,MAC地址的长度为6                      
u_char   procotol_len; //    协议地址长度,IP地址的长度为4                       
u_short option;        //    ARP操作代码,ARPOP_REQUEST为请求,ARPOP_REPLY为响应
u_char   src_addr[6];       源MAC地址                                             
ip_address   src_ip;    //    源IP地址                                            
u_char   dest_addr[6];      目的MAC地址                                           
ip_address   dest_ip;   //    目的IP地址                                          
}arp_header;

/*IPv4 header 20bytes */
typedef struct ip_header
{
u_char ver_ihl;   // Version (4 bits) + Internet header length (4 bits)
u_char tos;    // Type of service
u_short tlen;    // Total length
u_short identification; // Identification
u_short flags_fo;   // Flags (3 bits) + Fragment offset (13 bits)
u_char ttl;    // Time to live
u_char proto;    // Protocol
u_short crc;    // Header checksum
ip_address saddr;   // Source address
ip_address daddr;   // Destination address
u_int op_pad;    // Option + Padding
}ip_header;

/* UDP header*/
typedef struct udp_header
{
u_short sport;    // Source port
u_short dport;    // Destination port
u_short len;    // Datagram length
u_short crc;    // Checksum
}udp_header;

/**//*
*20字节的TCP头
*/
typedef struct _TCPHDR   
{
    u_short    srceport;            // 16位源端口号
    u_short    dstport;            // 16位目的端口号
    ULONG    seq;                // 32位序列号
    ULONG    ack;                // 32位确认号
    u_char    dataoffset;            // 高4位表示数据偏移
    u_char    flags;                // 6位标志位
    //FIN - 0x01
    //SYN - 0x02
    //RST - 0x04
    //PSH - 0x08
    //ACK - 0x10
    //URG - 0x20
    //ACE - 0x40
    //CWR - 0x80

    u_short    window;                // 16位窗口大小
    u_short    checksum;            // 16位校验和
    u_short    urgptr;                // 16位紧急数据偏移量
} TCPHDR, *PTCPHDR;

/**//*
*伪TCP头,计算校验和时使用
*/
typedef struct _PSDTCPHDR
{
    ULONG    saddr;
    ULONG    daddr;
    char    mbz;
    char    ptcl;
    u_short    tcpl;
} PSDTCPHDR, *PPSDTCPHDR;

/**//*
*8字节的UDP头
*/
typedef struct _UDPHDR
{
    u_short    srcport;            // 源端口号       
    u_short    dstport;            // 目的端口号       
    u_short    len;                // 封包长度
    u_short    checksum;            // 校验和
} UDPHDR, *PUDPHDR;

/**//*
*伪UDP头,计算校验和时使用
*/
typedef struct _PSDUDPHDR
{
    ULONG    saddr;
    ULONG    daddr;
    char    mbz;
    char    ptcl;
    u_short    udpl;
} PSDUDPHDR, *PPSDUDPHDR;

/**//*
*12字节的ICMP头
*/
typedef struct _ICMPHDR
{
    u_char   type;                //类型
    u_char   code;                //代码
    u_short checksum;            //校验和
    u_short id;                    //标识符
    u_short sequence;            //序列号
    ULONG   timestamp;            //时间戳
} ICMPHDR, *PICMPHDR;

/**//*
*6字节的PPPOE头+2字节协议
*/
typedef struct _PPPOEHDR
{
    u_char    ver_type;            //版本+类型 一般为0x11
    u_char    code;                //编码
    u_short    sessionid;            //session id
    u_short    len;                //长度
    u_short    protocol;            //协议
} PPPOEHDR, *PPPPOEHDR;

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

/**//*
* dns查询包,query
*/
typedef struct _DNSQUERY
{
    /**//*u_char *dname;*/    //查询的域名,这是一个大小在0到63之间的字符串
    /**//*该域名的获取方法如下:
    * 长度:udp包总长度-sizeof(UDPHDR)-sizeof(DNSHDR)-sizeof(DNSQUERY)
    * 内容在dns头后面
    */
    u_short    type;            //查询类型,大约有20个不同的类型
    u_short    classes;        //查询类,通常是A类既查询IP地址
} DNSQUERY, *PDNSQUERY;

/**//*
* dns响应包
*/
typedef struct _DNSRESPONSE
{
    u_short    name;        // 查询的域名
    u_short    type;        // 查询的类型
    u_short    classes;    // 类型码
    u_int    ttl;        // 生存时间
    u_short    length;        // 资源数据长度
    u_int    addr;        // 资源数据
} DNSRESPONSE, *PDNRESPONSE;

#pragma pack(pop)

#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值