简体   繁体   English

为什么我的程序不适用于不同的Linux发行版?

[英]Why doesn't my program work with different Linux distros?

I'm working on a packet sniffer. 我正在研究数据包嗅探器。 The big problem is that my code works perfectly only under Backtrack 5 R3, but it doesn't work under other distributions! 最大的问题是我的代码只能在Backtrack 5 R3下完美运行,但它在其他发行版下不起作用! In fact, on Ubuntu 12.10 and ArchLinux, when the sniffer gets the first packet, I experience a segmentation fault (I get "segmentation fault core dumped"). 事实上,在Ubuntu 12.10和ArchLinux上,当嗅探器获得第一个数据包时,我遇到了分段错误(我得到“分段故障核心转储”)。 At first, I thought the fault lay with the libraries or the compiler, but after some tests, I think I can exclude them! 起初,我认为错误在于库或编译器,但经过一些测试后,我想我可以排除它们! This is the situation: 情况就是这样:

  • Backtrack 5 R3 uses gcc 4.4.3 and libpcap 1.0.0 Backtrack 5 R3使用gcc 4.4.3和libpcap 1.0.0
  • Ubuntu 12.10 uses Gcc 4.7.2 and Libpcap 1.3.0 Ubuntu 12.10使用Gcc 4.7.2和Libpcap 1.3.0
  • ArchLinux the same as Ubuntu ArchLinux和Ubuntu一样

So I tried to downgrade on Arch to gcc 4.4.3 e libpcap 1.0.0, but I get the same error. 所以我尝试将Arch降级为gcc 4.4.3 e libpcap 1.0.0,但我得到了同样的错误。 I have some warnings while compiling the code, but nothing really important, and however it works perfectly under backtrack! 编译代码时我有一些警告,但没有什么真正重要的,但它在回溯下运行完美! That's the big mystery. 这是个大谜。

Here's the code which cause the problem: 这是导致问题的代码:

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));


}

I suspect your program is crashing because ip_header->source_addr_ip points to memory that you're not allowed to access. 我怀疑你的程序崩溃了,因为ip_header-> source_addr_ip指向你不允许访问的内存。 You should be able to use GDB to determine whether this is the case. 您应该能够使用GDB来确定是否是这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM