怎么在ip数据包中获取tcp数据
文存储在u_char ** ptk_data中,通过winpcap截获的报文是完整的tcpip报文,里面包括了物理层,网络层,传输层的封装,要将报文解析首先要定义头的格式
//Mac帧头 占14个字节
typedef struct tag_mac_header
{
u_char dadd[6];//6个字节 目标地址
u_char sadd[6];//6个字节 源地址
u_short mac_type;//2个字节 类型
}mac_header;
//IP地址 4字节
typedef struct tag_ip_address
{
u_char byte1; //sizeof(u_char...全部
文存储在u_char ** ptk_data中,通过winpcap截获的报文是完整的tcpip报文,里面包括了物理层,网络层,传输层的封装,要将报文解析首先要定义头的格式
//Mac帧头 占14个字节
typedef struct tag_mac_header
{
u_char dadd[6];//6个字节 目标地址
u_char sadd[6];//6个字节 源地址
u_short mac_type;//2个字节 类型
}mac_header;
//IP地址 4字节
typedef struct tag_ip_address
{
u_char byte1; //sizeof(u_char)==1 u_char占一个字节(8位)
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;
//定义IP头 IP数据包=IP头+TCP数据段(或UDP数据段)
typedef struct tag_ip_header
{
u_char ver_ihl; // u_char 8位 版本(4位) + 头长(4位)
u_char tos;//TOS 服务类型
u_short tlen;//包总长 u_short占两个字节
u_short identification;//标识
u_short flags_fo;//标志位Flags(3位)+分段偏移(13位)
u_char ttl;//TTL
u_char proto;//Protocol
u_short crc;//校验和
ip_address saddr;//源地址
ip_address daddr;//目的地址
u_int op_pad;//选项+填充字段 u_int占4个字节 32位
//接下来是数据段,不属于IP头部
}ip_header;
//定义UDP头
typedef struct tag_udp_header
{
u_short sport;//源端口 16位
u_short dport;//目的端口 16位
u_short len;//数据报长度 16位
u_short crc;//校验和 16位
}udp_header;
//定义TCP头
typedef struct tag_tcp_header
{
u_short sport;//源端口地址 16位
u_short dport;//目的端口地址 16位
u_int32_t sn;//序列号 32位
u_int32_t an;//确认号 32位
u_int16_t other;//16位 其中 头长度4位+保留6位+UPG1+ACK1+PSH1+RST1+SYN1+FIN1
u_int16_t window_size;//窗口大小 16位
u_int16_t check_sum;//校验和 16位
u_int16_t urgent_pointer;//紧急指针 16位
u_int32_t option;//选项0或32位
}tcp_header;
//定义ICMP
typedef struct tag_icmp_header
{
u_char type;//8位 类型
u_char identifer; //8位 代码
u_char sequence;//序列号 8位
u_char chksum;//8位校验和
}icmp_header;
根据这些格式把报文一套,IP地址在ip_address结构体中(是网络序的,别忘了转成主机序),端口号在传输层协议中Udp_Header和Tcp_Header中
另外,站长团上有产品团购,便宜有保证。
收起