Reputation: 1009
I'm working with a packet capture library and i've got it to print out packet IP addresses to the console using which is working fine:
printf(" From: %s\n", inet_ntoa(ip->ip_src));
printf(" To: %s\n", inet_ntoa(ip->ip_dst));
A few lines under that I call a method in a different .C
file using this passing the IP addresses and port number:
addpacket(0, inet_ntoa(ip->ip_src), inet_ntoa(ip->ip_dst), ntohs(tcp->th_dport));
The method is: void addpacket(int table, char *srcIP, char *dstIP, int port) {...}
But inside the addpacket
method I have this printing to the console:
printf("---------------------SRC:%s\n", srcIP);
printf("---------------------DST:%s\n", dstIP);
printf("---------------------PRT:%i\n", port);
And I get a result like this:
---------------------SRC:192.168.1.64
---------------------DST:192.168.1.64
---------------------PRT:60549
where the Source and Destination IP addresses are always the same. I can't find anywhere that i've mixed it up though. If I print this to the console in the original .C
file the correct IP addresses appear but something seems to be getting mixed up during the method call. Any ideas?
Upvotes: 0
Views: 35
Reputation: 179422
inet_ntoa
may use a static buffer to store the result, meaning that the second invocation of inet_ntoa
overwrites the result from the first invocation. Use inet_ntop
instead, which allows you to supply your own buffers.
Upvotes: 5