我正在编写一个pcaket嗅探器程序,使用libpcap在"wlan“接口上。我想过滤捕获的数据包,以便只处理信标帧。因此,我为此编写了以下代码:
const char *str = "wlan subtype beacon";
printf("debug stmt1\n");
struct bpf_program *fp;
printf("debug stmt2\n");
if((pcap_compile(pkt_handle, fp, str, 1, PCAP_NETMASK_UNKNOWN)==-1)
{
pcap_perror(pkt_handle, "Compile");
}
printf("debug stmt3\n"):但是在编译时,我在pcap_compile()语句上有一个分段错误:
debug stmt1
debug stmt2
Segmentation fault那么,有什么问题呢?
作业系统: Ubuntu 10.10
更新:
我在pcap_compile()语句之前移动了pcap_activate()语句。该程序工作良好,只捕获灯塔帧。但是,pcap_compile()似乎仍在返回-1,我在输出中得到以下语句:
Compile: 802.11 link-layer types supported only on 802.11有什么问题吗?我使用的是网具USB无线网卡。
Update2:
根据nos的建议,我做了以下修改:
struct bpf_program *fp = (struct bpf_program *)malloc(sizeof(struct bpf_program));但是,我还是得到了同样的信息:
Compile: 802.11 link-layer types supported only on 802.11你知道那条信息是什么意思吗?
更新3:
我还包括了以下代码,以确保pcap句柄指向正确的接口:
int *dlt_buf;
int n;
n = pcap_list_datalinks(pkt_handle, &dlt_buf);
printf("n = %d\n",n);
if(n == -1)
{
pcap_perror(pkt_handle, "Datalink_list");
}
else
{
printf("The list of datalinks supported are\n");
int i;
for(i=0; i<n; i++)
printf("%d\n",dlt_buf[i]);
const char *str1 = pcap_datalink_val_to_name(dlt_buf[0]);
const char *str2 = pcap_datalink_val_to_description(dlt_buf[0]);
printf("str1 = %s\n",str1);
printf("str2 = %s\n",str2);
pcap_free_datalinks(dlt_buf);
}这是我得到的输出:
n = 1
The list of datalinks supported are
127
str1 = IEEE802_11_RADIO
str2 = 802.11 plus radiotap header因此,我的pcap句柄指向正确的接口。但我还是收到了错误信息。
发布于 2012-07-12 17:17:40
如前所述,此次崩盘是因为fp没有指向什么东西。如果一个函数接受一个类型为“{某某} *”的参数,这并不意味着您需要甚至应该将一个类型为“{某某}*”的变量传递给它;您应该向它传递该类型的指针值。
例如,在这种情况下:
struct bpf_program *fp;
if((pcap_compile(pkt_handle, fp, str, 1, PCAP_NETMASK_UNKNOWN)==-1){
是错误的,而且
struct bpf_program pgm;
if((pcap_compile(pkt_handle, &pgm, str, 1, PCAP_NETMASK_UNKNOWN)==-1){
是对的。
至于在pcap_compile()之前调用pcap_activate(),这是不正确的。您必须在调用pcap_activate()之后调用它,否则pcap_t不具有链接层头类型,pcap_compile()也不知道它应该为其生成代码的链接层报头的类型。(我已经签入了libpcap的修复程序,以禁止在尚未激活的pcap_compile()上调用pcap_t。)
https://stackoverflow.com/questions/11448369
复制相似问题