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