为什么我的程序不适用于不同的Linux发行版?
问题描述:
我正在使用数据包嗅探器。 最大的问题是我的代码只能在Backtrack 5 R3下完美工作,但在其他发行版中无法使用!事实上,在Ubuntu 12.10和ArchLinux上,当嗅探器获得第一个数据包时,我遇到了分段错误(我得到“分段错误核心转储”)。起初,我认为错误在于图书馆或编译器,但经过一些测试后,我想我可以排除它们!情况是这样的:为什么我的程序不适用于不同的Linux发行版?
- 回溯5 R3使用的gcc 4.4.3和1.0.0的libpcap
- Ubuntu的12.10使用的gcc 4.7.2和1.3.0 Libpcap的
- ArchLinux的相同的Ubuntu
于是,我就降级拱与gcc 4.4.3èlibpcap的1.0.0,但我得到了同样的错误。 编译代码时我有一些警告,但没有什么非常重要的,但它在回溯轨道下完美工作!这是一个很大的谜团。
这里是造成该问题的代码:
void packet_dump(unsigned char *arguments, const struct pcap_pkthdr *pcap_data, const unsigned char *packet) {
int packet_data_len, tcp_header_size=0, total_header_size;
unsigned char *packet_data;
const unsigned char *ip_src_dest;
const struct header_ip *ip_header;
//Calculate the value of variables
ip_src_dest = (packet+LUNGHEZZA_INTESTAZIONE_ETH);
ip_header = (const struct header_ip *)ip_src_dest;
total_header_size = LUNGHEZZA_INTESTAZIONE_ETH+sizeof(struct header_ip)+tcp_header_size;
packet_data = (unsigned char *)packet + total_header_size;
packet_data_len = pcap_data->len - total_header_size;
//THIS CAUSE THE PROBLEM (Solved removing inet_ntoa and converting it manually)
printf("[ %s ] ============> ", inet_ntoa(ip_header->source_addr_ip));
printf("[ %s ] \n", inet_ntoa(ip_header->destination_addr_ip));
}
您是否调试了分段错误? GDB是你的朋友。 – Mark
*编译代码时我有一些警告,但没有什么真正重要的* - 你肯定*关于这个吗? –
正如@马克说做一个gdb来看看那里的段错误发生 –