pcap pcap_loop 方法不回调什么原因?

2024-05-17 06:32

1. pcap pcap_loop 方法不回调什么原因?

原型 pcap_loop(pcap_t *p,int cnt,pcap_handler callback,u_char *user)
需要编写回调函数 回调函数原型pcap_callback(u_char* argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content)
回调函数的参数pcap_content 即是需要的内容
回调函数的参数pcap_pkthdr 是捕获到的数据包基本信息,包括时间,长度等信息
建议你下载pcap的文档看看

pcap pcap_loop 方法不回调什么原因?

2. winPcap 中pcap_compile函数的过滤规则

http://www.winpcap.org/docs/docs_40_2/html/group__wpcapfunc.html
http://www.winpcap.org/docs/docs_40_2/html/group__wpcapfunc.html#g363bdc6f6b39b4979ddcf15ecb830c5c
http://stackoverflow.com/questions/11448369/pcap-compile-expression
google一下应该有你要的。

3. Wincap设置过滤器,该如何解决

我按照Wincap官方的例子,设置我的抓包程序的过滤器。
BOOL CapturePacket::SetFilter(char * packet_filter)//设置过滤器{if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )//设置过滤器{AfxMessageBox(Unable to compil e the packet filter. Check the syntax.);
TRACE(Error reading the packets: %s\n, pcap_geterr(adhandle));//语法错误,&fcode为0
/* 释放设备列表 */
pcap_freealldevs(alldevs);//return -1;}if (pcap_setfilter(adhandle, &fcode)<0)//设置过滤器{AfxMessageBox(Error setting the filter.);
/* 释放设备列表 */
pcap_freealldevs(alldevs);return -1;}// pcap_loop(adhandle, 0, packet_handler, NULL);return 0;}pcap_compile函数执行返回小于0的数,就是没设置成功。于是我调试发现是&fcode为0。
就是开始定义struct bpf_program fcode;
然后pcap_compile,pcap_setfilter的参数调用啊!没其他地方调用,我问别人说,这个值要有值才行。
在pcap_compile(adhandle, &fcode, packet_filter, 1, netmask)中,fcode的使用方式
基本上就是 :struct bpf_program fcode;然后传给这个接口就好了,这个参数的值是由pcap_compile来写的
我觉得之所以返回值小于0,你可能需要检查:
(1)adhandle,这个是一个网卡;
(2)packet_filter是不是符合winpcap的语法规则
(3)netmask是否正确,拿不准的话可以用0xffffff
希望对你有用
------解决方案--------------------------------------------------------
int pcap_setfilter  (  pcap_t *   p,
struct bpf_program *   fp)Associate a filter to a capture.
pcap_setfilter() is used to specify a filter program. fp is a pointer to a bpf_program struct, usually the result of a call to pcap_compile(). -1 is returned on failure, in which case pcap_geterr() may be used to display the error text; 0 is returned on success.
------解决方案--------------------------------------------------------
应该肯定不是fcode的问题你看看是不是掩码没有设置正确

Wincap设置过滤器,该如何解决

4. SNORT中无法启动DAQ。提示ERROR: Can't set DAQ BPF filter to '–T' (hO)!

我是用centos搭建的snort,在最后运行时,提示:Can't set DAQ BPF filter to '–D',有遇到这样的情况吗?

5. python中re模块的compile函数应该怎么用?

Python通过re模块提供对正则表达式的支持。使用re的一般步骤是先使用re.compile()函数,将正则表达式的字符串形式编译为Pattern实例,然后使用Pattern实例处理文本并获得匹配结果(一个Match实例),最后使用Match实例获得信息,进行其他的操作。
举一个简单的例子,在寻找一个字符串中所有的英文字符:
import repattern = re.compile('[a-zA-Z]')result = pattern.findall('as3SiOPdj#@23awe')print result# ['a', 's', 'S', 'i', 'O', 'P', 'd', 'j', 'a', 'w', 'e']

python中re模块的compile函数应该怎么用?

6. 用winpcap的 pcap_compile过滤时, 如何只抓取 udp 和 tcp 包

建议你使用Wireshark,很方便的捕获TCP和UDP包 

参考下http://baike.baidu.com/view/696423.htm

里面有个捕获局域网内UDP报文的例子,希望对你有所帮助.

http://www.winpcap.org/devel.htm
里面有ibraries, include files, documentation and a complete set of example programs. 

Wireshark也是开源的,有精力的话还可以参考下它的源代码.

7. winpcap怎么设置过滤一个网段

设置过滤器要用到两个函数,一个是pcap_compile(),另一个是pcao_setfilter()。他们的函数原型如下所示:
int pcap_compile (pcap_t *p, struct bpf_program *fp, char *str, int optimize, bpf_u_int32 netmask)

      1、p是一个打开的网络设备的描述符。
      2、fp是一个指针,用来存储一个编译好的过滤码。我们需要提前设置 struct bpf_program fcode 在这里使用。
      3、str是字符串(以\0结尾),它指定一个字符串性质的过滤描述,通过这个函数把这个描述转换成可供内部使用的过滤码。
      4、optimize设置是否要进行优化。一般为1。
      5、netmask就是我们所打开的设备的netmask,不知道这个怎么用。一般用d->addresses->netmask这个就可以了。
      return: 如果出错的话,就会返回一个小于0的值。
 
      然后就是设置这个过滤器:
int pcap_setfilter (pcap_t *p, struct bpf_program *fp)


      1、p就是一个已经打开的设备的描述符。     
      2、fp是用上面的函数编译好的过滤码,现在把这个过滤码设置到系统中去。
 
      附上一个代码:
#define _CRT_SECURE_NO_WARNINGS

#include "pcap.h"

void packet_handler(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);

int main()
{
	pcap_t *cap_ins_des;
	pcap_if_t *alldevs;
	pcap_if_t *d;
	char source[PCAP_BUF_SIZE];
	char errbuf[PCAP_ERRBUF_SIZE];
	int i;
	u_int netmask;
	char packet_filter[] = "ip and udp";	// the filter
	struct bpf_program fcode;	// used in pcap_compile()

	/* set the source */
	if (pcap_createsrcstr(source, PCAP_SRC_IFLOCAL, NULL, NULL, NULL, errbuf) == -1) {
		printf("%s\n", errbuf);
		exit(-1);
	}
	printf("source: %s\n", source);

	/* find all devices */
	if (pcap_findalldevs_ex(source, NULL, &alldevs, errbuf) == -1) {
		printf("%s\n", errbuf);
		exit(-1);
	}

	/* choose one devices */
	d = alldevs;
	while (d != NULL) {
		printf("%s, %s\n", d->name, d->description);
		d = d->next;
	}
	scanf("%d", &i);
	d = alldevs;
	while (--i)
		d = d->next;
	printf("selected device: %s\n", d->name);

	/* open one device */ 
	cap_ins_des = pcap_open(d->name, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf);
	if (cap_ins_des == NULL) {
		printf("%s\n", errbuf);
		pcap_freealldevs(alldevs);
		exit(-1);
	}

	/* get the netmask, used at compiling the filter */
	if (d->addresses != NULL)
		netmask = ((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;	/*@#$%^&*!*/
	else
		netmask = 0xffffff;	/* 255.25.255.0 */

	// netmask = 0;

	/* compile the filter */
	if (pcap_compile(cap_ins_des, &fcode, packet_filter, 1, netmask) < 0) {
		printf("Error\n");
		pcap_freealldevs(alldevs);
		exit(-1);
	}

	/* set the filter */
	if (pcap_setfilter(cap_ins_des, &fcode) < 0) {
		printf("Error\n");
		pcap_freealldevs(alldevs);
		exit(-1);
	}

	pcap_freealldevs(alldevs);

	/* start the capture */
	pcap_loop(cap_ins_des, 30, packet_handler, NULL);

	return 0;
}

void packet_handler(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
{
	printf("in packet handler\n");
	return;
}

winpcap怎么设置过滤一个网段

8. linux的ubuntu14.04下的libpcap怎么测试

为了开发需要,我决定使用最新libpcap源码包安装。在Unix环境下安装libpcap库,需要
c编译器,flex,bison等,安装Ubuntu系统时,没有这些包。安装flex需要m4编译环境,否则会提示“GNU M4 is required”错误。


1.安装系统依赖包
sudo apt-get install gcc libc6-dev
sudo apt-get install m4 
sudo apt-get install flex bison


2.下载libpcap源码并安装
从官网http://www.tcpdump.org/下载最新的libpcap版本
cd /usr/local/src
wget http://www.tcpdump.org/release/libpcap-1.5.3.tar.gz
tar zxvf libpcap-1.5.3.tar.gz
cd libpcap-1.5.3
./configure
make
sudo make install


3.安装开发需要用到的依赖库
sudo apt-get install libpcap-dev


4.测试libpcap的小程序,命名为pcap_demo.c,以检验环境是否配置正确
#include 
#include 


int main(int argc, char *argv[])
{
pcap_t *handle;
/* Session handle */
char *dev;  /* The device to sniff on */
char errbuf[PCAP_ERRBUF_SIZE];
/* Error string */
struct bpf_program fp;
/* The compiled filter */
char filter_exp[] = "port 80";
/* The filter expression */
bpf_u_int32 mask;
/* Our netmask */
bpf_u_int32 net;
/* Our IP */
struct pcap_pkthdr header;
/* The header that pcap gives us */
const u_char *packet;
/* The actual packet */


/* Define the device */
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
/* Find the properties for the device */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
}
/* Open the session in promiscuous mode */
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}
/* Compile and apply the filter */
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
/* Grab a packet */
packet = pcap_next(handle, &header);
/* Print its length */
printf("Jacked a packet with length of [%d]\n", header.len);
/* And close the session */
pcap_close(handle);
return(0);
}
开始编译:
    gcc -g pcap_demo.c -o pcap_demo -lpcap  
开始执行
    ./pcap_demo


5.注意的问题
5.1.注意使用root用户来执行,或者对普通用户使用sudo来提升权限
    sudo pcap_demo 
5.2.对一些PCAP API函数要有全面地理解,并时刻更新文档,比如pcap_loop这个函数,下面是官网的man page地址
http://www.tcpdump.org/manpages/pcap.3pcap.html