0

I am currently working on a small project which captures packets and triggers an external application or script when a specific limit exceeds to handle following steps (which could be for example alerting, null routing etc).

I tried to create a really simple trigger with this code ("trigger" contains the path to binary or script):

char * trigger_complete;
sprintf(trigger_complete, "%s %u %u %s %s %Lf", trigger, data[II].count, data[II].proto, inet_ntoa(data[II].src_ip), inet_ntoa(data[II].dst_ip), rate);
system (trigger_complete);

On my Ubuntu 12.04.1 LTS it seems to work without issues, I tested with the "echo" application.

linux-gate.so.1 =>  (0xb779f000)
libpcap.so.0.8 => /usr/lib/i386-linux-gnu/libpcap.so.0.8 (0xb7752000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75a8000)
/lib/ld-linux.so.2 (0xb77a0000)

On a Debian Wheezy and an instance of "grml" I receive Segmentation Fault when executing the binary. (I verified that this issue is caused by the piece of code above by commenting it out and re-try.)

linux-vdso.so.1 =>  (0x00007fff875ff000)
libpcap.so.0.8 => /usr/lib/x86_64-linux-gnu/libpcap.so.0.8 (0x00007fa9c6048000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa9c5cbe000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa9c608d000)

The only difference I see is the architecture, the Ubuntu system is a 32 bit os while the Debian is a 64 bit os.

I am not sure if this is the issue, but it seems like it is.

Can anyone help me out with this?

Thank you in advance!

jay
  • 51
  • 7
  • 7
    ... might want to allocate some memory for `trigger_complete`! – Thomas Mar 10 '13 at 11:05
  • 1
    BTW: inet_ntoa() returns a pointer to a statically allocated buffer. Calling it twice inside printf()s arguments will probably cause the results to seem equal, because the second call overwrites the results of the first one. BTW: the real error is probably in the lines that you did not show. – wildplasser Mar 10 '13 at 11:11
  • Seems that using malloc() worked for me. I will have to check this further. Thanks so far. – jay Mar 10 '13 at 11:19
  • Your hint regarding inet_ntoa() is correct, I will have to fix this but this is another chapter ;) thank you! – jay Mar 10 '13 at 11:23
  • 1
    Yup that would have done it. A good tool to examine segfaults is "valgrind", by the way. – Victor Zamanian Mar 10 '13 at 11:23
  • valgrind is new to me, I will test it :) thanks – jay Mar 10 '13 at 11:32
  • In any case: snprintf() is your friend. – wildplasser Mar 10 '13 at 11:48

1 Answers1

0

Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.

... might want to allocate some memory for trigger_complete! – Thomas

Community
  • 1
  • 1
Armali
  • 18,255
  • 14
  • 57
  • 171