inet_ntoa not returning correct values

Discussion in 'C Programming' started by satish.burnwal@gmail.com, Jan 12, 2009.

  1. Guest

    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
    , Jan 12, 2009
    #1
    1. Advertising

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

    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+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-----
    Falcon Kirtaran, Jan 12, 2009
    #2
    1. Advertising

  3. On Jan 12, 9:28 am, 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
    David Resnick, Jan 12, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anthony Roberts

    question about inet_ntoa(3)

    Anthony Roberts, Apr 10, 2004, in forum: C Programming
    Replies:
    3
    Views:
    354
    Keith Thompson
    Apr 11, 2004
  2. Replies:
    5
    Views:
    322
    Keith Thompson
    Nov 9, 2006
  3. Franz Bettag

    inet_aton / inet_ntoa

    Franz Bettag, Mar 15, 2006, in forum: Ruby
    Replies:
    3
    Views:
    124
    Franz Bettag
    Mar 16, 2006
  4. Rick Fiorentino

    inet_addr inet_ntoa

    Rick Fiorentino, May 30, 2007, in forum: Ruby
    Replies:
    2
    Views:
    112
    Brian Candler
    May 30, 2007
  5. Replies:
    3
    Views:
    93
    Evertjan.
    Apr 7, 2006
Loading...

Share This Page