日期:2014-05-16  浏览次数:20696 次

使用libpcap写抓包程序运行出现段错误,求解答。
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
void main()
{
char error_content[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr protocol_header;
pcap_t *pcap_handle;
struct bpf_program bpf_filter;
char bpf_filter_string[]="";
const u_char *packet_content;
bpf_u_int32 net_mask;
bpf_u_int32 net_ip;
char *net_interface;
net_interface=pcap_lookupdev(error_content);
pcap_lookupnet(net_interface,&net_ip,&net_mask,error_content);
pcap_handle=pcap_open_live(net_interface,BUFSIZ,1,0,error_content);
pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_ip);
pcap_setfilter(pcap_handle,&bpf_filter);
packet_content=pcap_next(pcap_handle,&protocol_header);
printf("capture a packet from:%s\n",net_interface);
printf("the packet length is:%d\n",protocol_header.len);
pcap_close(pcap_handle);

}
很简单的一段程序,到底哪里有问题?
运行时提示:段错误 (核心已转储)

------解决方案--------------------
用root权限运行编译后的可执行文件