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! 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"). 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
- Ubuntu 12.10 uses Gcc 4.7.2 and Libpcap 1.3.0
- ArchLinux the same as Ubuntu
So I tried to downgrade on Arch to gcc 4.4.3 e libpcap 1.0.0, but I get the same error. 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));
}