IP包头结构定义以及结构体iphdr分析

Linux, linux
可以使用 http://blog.csdn.net/dreamintheworld/article/details/52096675 中的例程来实践通讯过程;
来看一下IP包, IP头结构的定义


这里写图片描述


这里写图片描述

包格式解析如图
这里写图片描述

数据对应
这里写图片描述

对应的结构体在 /usr/src/linux-2.6.19/include/linux/ip.h(或 /usr/src/linux-headers-3.5.0-18/include/linux/ip.h)

struct iphdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
    __u8    ihl:4,
        version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
    __u8    version:4,
        ihl:4;
#else
#error  "Please fix <asm/byteorder.h>"
#endif
    __u8    tos;
    __be16  tot_len;
    __be16  id;
    __be16  frag_off;
    __u8    ttl;
    __u8    protocol;
    __sum16 check;
    __be32  saddr;
    __be32  daddr;
    /*The options start here. */
};

看包情况
45(0100,0101) 第一个8位表示4位版本号+4位首部长度, 0100=Version为4, 0101=5*4=20(个字节长度)
00 (0000,0000)第二个8位表示服务类型
00 3C 第三四个, 16位标识
60 DF 表示16位标识符
40 00 表示3位标志+13位片偏移 (000=位标识, 1000000000000=位偏移)
40 表示8位生存时间
06 表示8位协议,这里表示TCP
62 1C 表示16位首部校验和
C0 A8 FB 2C 表示32位源IP地址
C0 A8 FB 42 表示32位目的IP地址
无选项
无数据

这个包TCP协议里面的第一次握手的情况,TCP握手基本就是无选项无数据,所以目的地址之后就没有数据了;

还有关于TCP头部和UDP头部的结构情况,放个图,分析情况基本一致,不做详细分析

TCP
这里写图片描述

//TCP报头结构体   
typedef struct tcphdr   
{  
    u_short th_sport;  
    u_short th_dport;  
    u_int th_seq;  
    u_int th_ack;  
    u_int th_off:4;  
    u_int th_x2:4;  
    u_char th_flags;  
    u_short th_win;  
    u_short th_sum;  
    u_short th_urp;  
}TCP_HEADER;  

UDP
这里写图片描述

//UDP报头结构体*/   
typedef struct udphdr   
{  
    u_short uh_sport;  
    u_short uh_dport;  
    u_short uh_ulen;  
    u_short uh_sum;  
}UDP_HEADER; 

参考
http://blog.csdn.net/mrwangwang/article/details/8537775
http://zoufengfu168.blog.163.com/blog/static/5461055200991333616451/
http://www.cnblogs.com/lancidie/archive/2013/05/16/3082378.html

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值