请问怎样从截获的数据包中取出目标IP地址?最好能有源程序,谢谢 用winpcap截获的数据包,请问怎样取出包中的目标IP地址?最好能提供源程序谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 照着testApp的例子,先PacketReceivePacket,然后可以得到IP头,照着IP结构分析如下:typedef struct _IP_HEADER{#if defined(__LITTLE_ENDIAN_BITFIELD) __u8 ihl:4, version:4;#elif defined (__BIG_ENDIAN_BITFIELD) __u8 version:4, ihl:4;#endif __u8 tos; __u16 tot_len; __u16 id; __u16 frag_off; __u8 ttl; __u8 protocol; __u16 check; __u32 saddr; __u32 daddr;} IP_HEADER;取得daddr即为包中的目标IP地址 定义一个ip报文头的结构:struct ip { unsigned int ip_hl:4; /* header length */ unsigned int ip_v:4; /* version */ u_char ip_tos; /* type of service */ u_short ip_len; /* total length */ u_short ip_id; /* identification */ u_short ip_off; /* fragment offset field */#define IP_RF 0x8000 /* reserved fragment flag */#define IP_DF 0x4000 /* dont fragment flag */#define IP_MF 0x2000 /* more fragments flag */#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ u_char ip_ttl; /* time to live */ u_char ip_p; /* protocol */ u_short ip_sum; /* checksum */ struct in_addr ip_src, ip_dst; /* source and dest address */ };用这个函数可以显示报文的ip地址:bp是用winpcap截到的报文,length是报文长度:voidprint_ip(unsigned char *bp, u_short length){ struct ip *iph; u_int ip_off, ip_hl, ip_len; iph = (struct ip *)bp; if (length < IP_H) { printf("truncated-ip %d", length); return; } ip_hl = iph->ip_hl * 4; //ip_len = ntohs(iph->ip_len); ip_len = iph->ip_len; if (length < ip_len) { printf("truncated-ip - %d bytes missing!", ip_len - length); return; } ip_off = ntohs(iph->ip_off); /* Handle first fragment. */ if ((ip_off & IP_OFFMASK) == 0) { switch (iph->ip_p) { default: printf("%s > %s:", inet_ntoa(iph->ip_src), inet_ntoa(iph->ip_dst)); printf(" ip-proto-%d %d", iph->ip_p, ip_len); break; } } /* Handle more frags. */ if (ip_off & (IP_MF|IP_OFFMASK)) { if (ip_off & IP_OFFMASK) printf("%s > %s:", inet_ntoa(iph->ip_src), inet_ntoa(iph->ip_dst.s_addr)); printf(" (frag %d:%d@%d%s)", ntohs(iph->ip_id), ip_len - ip_hl, (ip_off & IP_OFFMASK) << 3, (ip_off & IP_MF) ? "+" : ""); } /* Handle don't frags. */ else if (ip_off & IP_DF) printf(" (DF)"); if (iph->ip_tos) printf(" [tos 0x%x]", (int)iph->ip_tos); if (iph->ip_ttl <= 1) printf(" [ttl %d]", (int)iph->ip_ttl);} 比如包在const char * p中前面14是以太头。p[12]*0x100 + p[13] == 0x0800时是IP包然后UINT sip = *(UINT *)&p[12 + 14];UINT dip = *(UINT *)&p[16 + 14];找本《TCP/IP协议》看看就行了 使用GDI+中Image时返回OutOfMemory vc 界面 通过VC实现Excel获取权限对象后,保存Excel文档出错,请高手们指点,已经郁闷很久了~~~~ 关于定时器? 如何改变format后指数形式e后面的位数? 问:网络通信编程,选java还是c++? 请高手指点为何ADO连接Excel后无法再连接ACCESS!!! 如何在vc中运行绘图程序??? 如何在接口IDL定义中使用ADO记录集?急 一个Socket通讯的问题,请指教! 想靠实力通过MCSD考试的进来看看啊 初学vc++的弟兄,大家一起努力
typedef struct _IP_HEADER
{
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u8 ihl:4,
version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
__u8 version:4,
ihl:4;
#endif
__u8 tos;
__u16 tot_len;
__u16 id;
__u16 frag_off;
__u8 ttl;
__u8 protocol;
__u16 check;
__u32 saddr;
__u32 daddr;
} IP_HEADER;
取得daddr即为包中的目标IP地址
struct ip
{
unsigned int ip_hl:4; /* header length */
unsigned int ip_v:4; /* version */
u_char ip_tos; /* type of service */
u_short ip_len; /* total length */
u_short ip_id; /* identification */
u_short ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_char ip_ttl; /* time to live */
u_char ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src, ip_dst; /* source and dest address */
};用这个函数可以显示报文的ip地址:
bp是用winpcap截到的报文,length是报文长度:void
print_ip(unsigned char *bp, u_short length)
{
struct ip *iph;
u_int ip_off, ip_hl, ip_len; iph = (struct ip *)bp; if (length < IP_H) {
printf("truncated-ip %d", length);
return;
}
ip_hl = iph->ip_hl * 4;
//ip_len = ntohs(iph->ip_len);
ip_len = iph->ip_len; if (length < ip_len) {
printf("truncated-ip - %d bytes missing!", ip_len - length);
return;
}
ip_off = ntohs(iph->ip_off); /* Handle first fragment. */
if ((ip_off & IP_OFFMASK) == 0) {
switch (iph->ip_p) { default:
printf("%s > %s:", inet_ntoa(iph->ip_src),
inet_ntoa(iph->ip_dst));
printf(" ip-proto-%d %d", iph->ip_p, ip_len);
break;
}
}
/* Handle more frags. */
if (ip_off & (IP_MF|IP_OFFMASK)) {
if (ip_off & IP_OFFMASK)
printf("%s > %s:", inet_ntoa(iph->ip_src),
inet_ntoa(iph->ip_dst.s_addr));
printf(" (frag %d:%d@%d%s)", ntohs(iph->ip_id), ip_len - ip_hl,
(ip_off & IP_OFFMASK) << 3, (ip_off & IP_MF) ? "+" : "");
}
/* Handle don't frags. */
else if (ip_off & IP_DF) printf(" (DF)");
if (iph->ip_tos) printf(" [tos 0x%x]", (int)iph->ip_tos);
if (iph->ip_ttl <= 1) printf(" [ttl %d]", (int)iph->ip_ttl);
}
然后
UINT sip = *(UINT *)&p[12 + 14];
UINT dip = *(UINT *)&p[16 + 14];找本《TCP/IP协议》看看就行了