当然,我只是想了解原理,如果有源码就更好了,请发到117712#sina*com,立马给分,如果没有讲讲原理也行,我先说说我现在的进展,我需要获取到tcp发送端口和接收端口,目前从网上下出来一个tcp_header的结构体,总是不正确(获取到的发送端口和接收端口不正确),这里我给出代码给各位高手看看,struct ip_header //IP协议数据结构
{
u_int8_t ip_hlen :4, //首部长度
ip_version:4; //IP版本
u_int8_t ip_tos; //服务类型
u_int16_t ip_length; //总长
u_int16_t ip_id; //标识
u_int16_t ip_offset:13,ip_flag:3; //片偏移|标志
u_int8_t ip_ttl; //生存时间
u_int8_t ip_protocol; //协议
u_int16_t ip_sum; //首部校检和
u_int8_t ip_src_address[4]; //来源地址
u_int8_t ip_dst_address[4]; //目的地址
u_int32_t ip_op_pad; // 选项与填充(Option + Padding)
};//------------------------------------------------------------------
struct tcp_header //TCP协议数据结构
{
u_int16_t tcp_src_port; //源端口
u_int16_t tcp_dst_port; //目的端口
u_int32_t tcp_sn; //序列号
u_int32_t tcp_ack; //确认号
u_int8_t tcp_reserved4:4, //保留字段低4bit
tcp_hlen:4; //tcp头部长度
u_int8_t tcp_ctrl_fin:1, //终止连接位
tcp_ctrl_syn:1, //同步序列位
tcp_ctrl_rst:1, //复位连接位
tcp_ctrl_psh:1, //PUSH请求位
tcp_ctrl_ack:1, //确认字段有效位
tcp_ctrl_urg:1, //紧急指针有效位
tcp_reserved2:2; //保留字段高2bit
u_int16_t tcp_wnd_size; //窗口大小
u_int16_t tcp_sum; //校检和
u_int16_t tcp_urgent_ptr; //紧急指针
u_int32_t tcp_op_pad; //填充
};// 上面是定义 // 取得IP头
m_ip = (struct ip_header *)(data+14); // 取得Tcp头
tcp_header *tcpHeader = (struct tcp_header*)(data+14+20);
CString aabb;
aabb.Format("Come: %d To: %d", tcpHeader->tcp_src_port, tcpHeader->tcp_dst_port);
AfxMessageBox(aabb);// 输出的端口和sniffer上显示的始终对不上,请各位大牛帮忙研究一下,谢谢,能给出能运行的代码最好了,分不是问题!
{
u_int8_t ip_hlen :4, //首部长度
ip_version:4; //IP版本
u_int8_t ip_tos; //服务类型
u_int16_t ip_length; //总长
u_int16_t ip_id; //标识
u_int16_t ip_offset:13,ip_flag:3; //片偏移|标志
u_int8_t ip_ttl; //生存时间
u_int8_t ip_protocol; //协议
u_int16_t ip_sum; //首部校检和
u_int8_t ip_src_address[4]; //来源地址
u_int8_t ip_dst_address[4]; //目的地址
u_int32_t ip_op_pad; // 选项与填充(Option + Padding)
};//------------------------------------------------------------------
struct tcp_header //TCP协议数据结构
{
u_int16_t tcp_src_port; //源端口
u_int16_t tcp_dst_port; //目的端口
u_int32_t tcp_sn; //序列号
u_int32_t tcp_ack; //确认号
u_int8_t tcp_reserved4:4, //保留字段低4bit
tcp_hlen:4; //tcp头部长度
u_int8_t tcp_ctrl_fin:1, //终止连接位
tcp_ctrl_syn:1, //同步序列位
tcp_ctrl_rst:1, //复位连接位
tcp_ctrl_psh:1, //PUSH请求位
tcp_ctrl_ack:1, //确认字段有效位
tcp_ctrl_urg:1, //紧急指针有效位
tcp_reserved2:2; //保留字段高2bit
u_int16_t tcp_wnd_size; //窗口大小
u_int16_t tcp_sum; //校检和
u_int16_t tcp_urgent_ptr; //紧急指针
u_int32_t tcp_op_pad; //填充
};// 上面是定义 // 取得IP头
m_ip = (struct ip_header *)(data+14); // 取得Tcp头
tcp_header *tcpHeader = (struct tcp_header*)(data+14+20);
CString aabb;
aabb.Format("Come: %d To: %d", tcpHeader->tcp_src_port, tcpHeader->tcp_dst_port);
AfxMessageBox(aabb);// 输出的端口和sniffer上显示的始终对不上,请各位大牛帮忙研究一下,谢谢,能给出能运行的代码最好了,分不是问题!
改成
aabb.Format("Come: %d To: %d", htons(tcpHeader->tcp_src_port), htons(tcpHeader->tcp_dst_port));htons使用前需要
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
谢谢,你是我的救命草!再次感谢!