如何使用winpcap获取HTTP数据包的uri?
可能重复:如何使用winpcap获取HTTP数据包的uri?,c,http,winpcap,C,Http,Winpcap,可能重复: 基于此,我可以获取所有传入的数据包 /* Callback function invoked by libpcap for every incoming packet */ void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) { struct tm *ltime; char timestr[16]; ip_header *ih
基于此,我可以获取所有传入的数据包
/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short sport,dport;
time_t local_tv_sec;
/* convert the timestamp to readable format */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
/* print timestamp and length of the packet */
printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
/* retireve the position of the ip header */
ih = (ip_header *) (pkt_data +
14); //length of ethernet header
/* retireve the position of the udp header */
ip_len = (ih->ver_ihl & 0xf) * 4;
uh = (udp_header *) ((u_char*)ih + ip_len);
/* convert from network byte order to host byte order */
sport = ntohs( uh->sport );
dport = ntohs( uh->dport );
/* print ip addresses and udp ports */
printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
}
但是如何在
数据包处理程序中提取URI信息呢?不是每个数据包都有URI
在http请求中,URI将在非常接近连接开始时传输,但后续数据包只是较大请求的一部分
要查找所请求的URI(以及所有数据),请查看pkt_数据您没有遵循最佳示例。您发布的URL是一个处理UDP数据包的示例,但HTTP是基于TCP的。通常(忽略连接:保持活动,非常短的第一个数据包等)。URI将是第一个传出TCP数据包第一行的第二个字(将字定义为空格分隔,将行定义为CR LF分隔)
由于它基于libpcap,是开源的,并且在这方面做得很好,您可以从那里开始查看。@abelenky,您能否详细说明如何查看pkt_数据
?pkt_数据的类型是“const u_char*”。我必须假设您知道如何检查无符号字符数组。如果没有其他问题,请尝试printf。您找到更好的示例了吗?正如@klatchko指出的,这段代码处理UDP数据包,并且不容易获取通过TCP传输的HTTP。请参阅我的答案。