Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > inet_ntoa not returning correct values

Reply
Thread Tools

inet_ntoa not returning correct values

 
 
satish.burnwal@gmail.com
Guest
Posts: n/a
 
      01-12-2009
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
 
Reply With Quote
 
 
 
 
Falcon Kirtaran
Guest
Posts: n/a
 
      01-12-2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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


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+p4no2kxiOMGRXfvsUV7KrqpC3JB5 3HFDyTImL+CiIL
KmkFrgHNtmB/J9X2K2/SrV7UEsWHbh+wn5lvGL8l+vK68jLLvSLgMv99A2MevKj0
BiLfZ6DUFSLGwOoBZudKmmt7DwauqI2YZWj9mDweuEmfaH6ABf QYg9lgvTOHWZfj
MbseefSU8R3A7SVJ5quyb2GpEf65AGU+9d7FDrj3JkIA27UZtf KNrZypBfNF4XlR
lWB9tXYN1+8QBIBdkal2HwquXKbVyX0SiyIVCybih+8qw04bik rOkDnR5xiFauRu
FG4cydhRm1E6CpdVI2y0okYfTF6BXuKWy2iwU13EdTqLZ15MP5 K8QT6fYuWPV7W3
xXp/+WOsfBy1aQJNSF2hAOUx+kSd+3XwLPEZ0q5034RDI3P+AzvseE M3uwVJRCIt
Os6iqxpvZe0vhpsjHP+GVyn6hncmQJVqeCoJ6/dw1mCnsQiYz+PHP/GQr59oMMtq
f16EYX/qRHHaXdDw5Pcx
=qTz8
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      01-12-2009
On Jan 12, 9:28*am, (E-Mail Removed) wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inet_addr inet_ntoa Rick Fiorentino Ruby 2 05-30-2007 08:18 PM
about inet_ntoa(ptParams->pServerHandle->sockAddrClient.sin_addr) dharmdeep@gmail.com C Programming 5 11-09-2006 05:43 AM
JavaScript getMonth and getDay not returning correct values maflatoun@gmail.com Javascript 3 04-07-2006 01:46 PM
inet_aton / inet_ntoa Franz Bettag Ruby 3 03-16-2006 09:12 AM
question about inet_ntoa(3) Anthony Roberts C Programming 3 04-10-2004 11:19 PM



Advertisments