当前位置: 技术问答>linux和unix
我自己写得libpcap抓包,用libnet发送程序,但是编译通过以后,无法运行,错误为 段错误
来源: 互联网 发布时间:2017-05-19
本文导语: 我自己写得libpcap抓包,用libnet发送程序,但是编译通过以后,无法运行,错误为 段错误,我自己也按照网上说段错误的调试了,但是还是不知道怎么修改。 下面是源代码以及错误提示: #include #include #include #...
我自己写得libpcap抓包,用libnet发送程序,但是编译通过以后,无法运行,错误为 段错误,我自己也按照网上说段错误的调试了,但是还是不知道怎么修改。
下面是源代码以及错误提示:
#include
#include
#include
#include
#include
#include
struct ether_header
{
u_int8_t ether_dhost[6];
u_int8_t ether_shost[6];
u_int16_t ether_type;
};
void ethernet_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)
{
int packet_size;
char error_buffer[LIBNET_ERRBUF_SIZE];
char *dev1_ = "eth9";
char *dev2_ = "eth8";
libnet_t *l;
static int k;
char *data;
u_int8_t len;
int ethernet_protocol_tag = 0;
u_short ethernet_type;
struct ether_header *ethernet_protocol;
u_int8_t *hardware_destination;
u_int8_t *hardware_source;
len = packet_header->len;
data = packet_content;
int i;
for(i=0;iether_type);
printf("%04xn", ethernet_type);
hardware_source = ethernet_protocol->ether_shost;
hardware_destination = ethernet_protocol->ether_dhost;
printf("Mac Source Address is : n");
printf("%02x:%02x:%02x:%02x:%02x:%02xn", *hardware_source, *(hardware_source + 1), *(hardware_source + 2), *(hardware_source + 3),
*(hardware_source + 4), *(hardware_source + 5));
printf("Mac Destination Address is : n");
printf("%02x:%02x:%02x:%02x:%02x:%02xn", *hardware_destination, *(hardware_destination + 1), *(hardware_destination + 2), *(hardware_destination+ 3),
*(hardware_destination + 4), *(hardware_destination + 5));
if(k%2==0)
l = libnet_init(LIBNET_LINK, dev1_, error_buffer);
else
l = libnet_init(LIBNET_LINK, dev2_, error_buffer);
k++;
ethernet_protocol_tag = libnet_build_ethernet(
hardware_destination,
hardware_source,
ethernet_type,
data,
len-14,
l,
ethernet_protocol_tag );
packet_size = libnet_write(l);
printf("鍙戦€佺殑瀛楄妭鏁颁负%dn", packet_size);
libnet_destroy(l);
}
void main()
{
pcap_t *pcap_handle;
int j = 0;
char error_content[PCAP_ERRBUF_SIZE];
char *dev1 = "eth8";
char *dev2 = "eth9";
while(1)
{
if(j%2==0)
{
pcap_handle = pcap_open_live(dev1, BUFSIZ, 1, 0, error_content);
if(pcap_handle = NULL)
{
fprintf(stderr,"could not open device %s;%s",dev1,error_content);
return;
}
pcap_loop(pcap_handle, 1, ethernet_protocol_packet_callback, NULL);
pcap_close(pcap_handle);
}
else
{
pcap_handle = pcap_open_live(dev2, BUFSIZ, 1, 0, error_content);
if(pcap_handle = NULL)
{
fprintf(stderr,"could not open device %s;%s",dev2,error_content);
return;
}
pcap_loop(pcap_handle, 1, ethernet_protocol_packet_callback, NULL);
pcap_close(pcap_handle);
}
j++;
}
错误提示为:Program received signal SIGSEGV, Segmentation fault.
0x0014d282 in pcap_loop () from /usr/lib/libpcap.so.0.8
我自己设置断点以后确定错误在执行 pcap_loop () 时候产生,在这个回调函数 void ethernet_protocol_packet_callback 中无法执行,就是提示上面的错误。
请大神看看到底是哪里的错误呢? 谢谢了
下面是源代码以及错误提示:
#include
#include
#include
#include
#include
#include
struct ether_header
{
u_int8_t ether_dhost[6];
u_int8_t ether_shost[6];
u_int16_t ether_type;
};
void ethernet_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)
{
int packet_size;
char error_buffer[LIBNET_ERRBUF_SIZE];
char *dev1_ = "eth9";
char *dev2_ = "eth8";
libnet_t *l;
static int k;
char *data;
u_int8_t len;
int ethernet_protocol_tag = 0;
u_short ethernet_type;
struct ether_header *ethernet_protocol;
u_int8_t *hardware_destination;
u_int8_t *hardware_source;
len = packet_header->len;
data = packet_content;
int i;
for(i=0;iether_type);
printf("%04xn", ethernet_type);
hardware_source = ethernet_protocol->ether_shost;
hardware_destination = ethernet_protocol->ether_dhost;
printf("Mac Source Address is : n");
printf("%02x:%02x:%02x:%02x:%02x:%02xn", *hardware_source, *(hardware_source + 1), *(hardware_source + 2), *(hardware_source + 3),
*(hardware_source + 4), *(hardware_source + 5));
printf("Mac Destination Address is : n");
printf("%02x:%02x:%02x:%02x:%02x:%02xn", *hardware_destination, *(hardware_destination + 1), *(hardware_destination + 2), *(hardware_destination+ 3),
*(hardware_destination + 4), *(hardware_destination + 5));
if(k%2==0)
l = libnet_init(LIBNET_LINK, dev1_, error_buffer);
else
l = libnet_init(LIBNET_LINK, dev2_, error_buffer);
k++;
ethernet_protocol_tag = libnet_build_ethernet(
hardware_destination,
hardware_source,
ethernet_type,
data,
len-14,
l,
ethernet_protocol_tag );
packet_size = libnet_write(l);
printf("鍙戦€佺殑瀛楄妭鏁颁负%dn", packet_size);
libnet_destroy(l);
}
void main()
{
pcap_t *pcap_handle;
int j = 0;
char error_content[PCAP_ERRBUF_SIZE];
char *dev1 = "eth8";
char *dev2 = "eth9";
while(1)
{
if(j%2==0)
{
pcap_handle = pcap_open_live(dev1, BUFSIZ, 1, 0, error_content);
if(pcap_handle = NULL)
{
fprintf(stderr,"could not open device %s;%s",dev1,error_content);
return;
}
pcap_loop(pcap_handle, 1, ethernet_protocol_packet_callback, NULL);
pcap_close(pcap_handle);
}
else
{
pcap_handle = pcap_open_live(dev2, BUFSIZ, 1, 0, error_content);
if(pcap_handle = NULL)
{
fprintf(stderr,"could not open device %s;%s",dev2,error_content);
return;
}
pcap_loop(pcap_handle, 1, ethernet_protocol_packet_callback, NULL);
pcap_close(pcap_handle);
}
j++;
}
错误提示为:Program received signal SIGSEGV, Segmentation fault.
0x0014d282 in pcap_loop () from /usr/lib/libpcap.so.0.8
我自己设置断点以后确定错误在执行 pcap_loop () 时候产生,在这个回调函数 void ethernet_protocol_packet_callback 中无法执行,就是提示上面的错误。
请大神看看到底是哪里的错误呢? 谢谢了
|
pcap_loop(pcap_handle, 1, ethernet_protocol_packet_callback, NULL); 应该是这个地方 打开错误后,后面接着又来关闭,产生的
|
一般这种问题是越界