inet_ntoa not returning correct values

S

satish.burnwal

I am facing an issue in my app that is analysing network packets. I
see that inet_ntoa function (defined in <arpa/inet.h> is not printing
correctly.

When I print (I have ip pointer variable populated), it prints
correctly:
printf(log_msg, "From %lu to %lu", (ip->ip_src).s_addr, (ip-
ip_dest).s_addr);
//prints From 192.168.10.2 to 192.168.10.15

But when I print, it is printing src and dest ip same:
printf(log_msg, "From %s to %s", inet_ntoa(ip->ip_src), inet_ntoa(ip-
ip_dest));
//prints From 192.168.10.2 to 192.168.10.2

Anything wrong here ?

-Satish
 
F

Falcon Kirtaran

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I am facing an issue in my app that is analysing network packets. I
see that inet_ntoa function (defined in <arpa/inet.h> is not printing
correctly.

When I print (I have ip pointer variable populated), it prints
correctly:
printf(log_msg, "From %lu to %lu", (ip->ip_src).s_addr, (ip-
//prints From 192.168.10.2 to 192.168.10.15

But when I print, it is printing src and dest ip same:
printf(log_msg, "From %s to %s", inet_ntoa(ip->ip_src), inet_ntoa(ip-
//prints From 192.168.10.2 to 192.168.10.2

Anything wrong here ?

-Satish

Does that first comment specify what the call actually prints? The way
I understand those functions, the second call is correct and does what
you say, but the first one should not print dotted quads.

- --
- --Falcon Darkstar Kirtaran
- --
- --OpenPGP: (7902:4457) 9282:A431

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQIcBAEBAgAGBQJJa1VPAAoJEKmxP9YxEE4rfXgP/i2MzrMi19L+Baph5UPObNbT
if4j3fC9eMVRabJQ7fVKYHdtgtNxo/m+jlV44JoqK6y/CmFJT1bZsU9NI69fb0F/
oQdDnZ8JamMFKVziHnpLRHZLoTw7jh9SepgNmu3b8IPaXZA2i+xxBpW5AA2/Y51k
Gp+cV90B1ijykNNFfLhzI+p4no2kxiOMGRXfvsUV7KrqpC3JB53HFDyTImL+CiIL
KmkFrgHNtmB/J9X2K2/SrV7UEsWHbh+wn5lvGL8l+vK68jLLvSLgMv99A2MevKj0
BiLfZ6DUFSLGwOoBZudKmmt7DwauqI2YZWj9mDweuEmfaH6ABfQYg9lgvTOHWZfj
MbseefSU8R3A7SVJ5quyb2GpEf65AGU+9d7FDrj3JkIA27UZtfKNrZypBfNF4XlR
lWB9tXYN1+8QBIBdkal2HwquXKbVyX0SiyIVCybih+8qw04bikrOkDnR5xiFauRu
FG4cydhRm1E6CpdVI2y0okYfTF6BXuKWy2iwU13EdTqLZ15MP5K8QT6fYuWPV7W3
xXp/+WOsfBy1aQJNSF2hAOUx+kSd+3XwLPEZ0q5034RDI3P+AzvseEM3uwVJRCIt
Os6iqxpvZe0vhpsjHP+GVyn6hncmQJVqeCoJ6/dw1mCnsQiYz+PHP/GQr59oMMtq
f16EYX/qRHHaXdDw5Pcx
=qTz8
-----END PGP SIGNATURE-----
 
D

David Resnick

I am facing an issue in my app that is analysing network packets. I
see that inet_ntoa function (defined in <arpa/inet.h> is not printing
correctly.

When I print (I have ip pointer variable populated), it prints
correctly:
printf(log_msg, "From %lu to %lu", (ip->ip_src).s_addr, (ip->ip_dest).s_addr);

//prints From 192.168.10.2 to 192.168.10.15

But when I print, it is printing src and dest ip same:
printf(log_msg, "From %s to %s", inet_ntoa(ip->ip_src), inet_ntoa(ip->ip_dest));

//prints From 192.168.10.2 to 192.168.10.2

While details of inet_ntoa are not on topic in comp.lang.c (try
comp.unix.programmer), the problem here requires no detailed knowledge
of anything other than C. Where do you think inet_ntoa is putting the
string you are printing? You aren't providing a buffer, so it must be
providing it. You are calling the function twice (in an unspecified
order). The string printed will be that of the second call.

BTW, on my system at least the man page of inet_ntoa says all this
quite clearly.
And you might try "man inet_ntop" also, it solves this problem and
others for you.

-David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top