当前位置: 技术问答>linux和unix
使用libpcap写抓包程序运行出现段错误,求解答。
来源: 互联网 发布时间:2017-04-09
本文导语: #include #include #include 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;...
#include
#include
#include
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:%sn",net_interface);
printf("the packet length is:%dn",protocol_header.len);
pcap_close(pcap_handle);
}
很简单的一段程序,到底哪里有问题?
运行时提示:段错误 (核心已转储)
#include
#include
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:%sn",net_interface);
printf("the packet length is:%dn",protocol_header.len);
pcap_close(pcap_handle);
}
很简单的一段程序,到底哪里有问题?
运行时提示:段错误 (核心已转储)
|
用root权限运行编译后的可执行文件