Networking Forums

Networking Forums > Computer Networking > Linux Networking > what's going on with inet_ntoa() ?

Reply
Thread Tools Display Modes

what's going on with inet_ntoa() ?

 
 
Paul
Guest
Posts: n/a

 
      03-21-2006, 02:47 PM
HI !

Preface: This is my first time writing a posting on the usenet. So if this
isn't the right place for this topic please tell me an alternative.


Currently I'm playing a little with raw sockets on linux. But I have a
strange problem.

Watch this simple c-code which compiles fine and runs:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>

#include <netinet/ether.h>
#include <net/ethernet.h>

#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>

#include <netinet/tcp.h>
#include <stdio.h>

int main( int argc , char** argv ) {

int packetsize = sizeof( struct ether_header ) + sizeof( struct iphdr )
+ sizeof( struct tcphdr );
char* packet = malloc( packetsize );

struct ether_header* eth = (struct ether_header*) packet;
struct iphdr* ip = (struct iphdr*) (packet + sizeof( struct
ether_header ) );
struct tcphdr* tcp = (struct tcphdr*) (packet + sizeof( struct
ether_header) + sizeof( struct iphdr ) );

int uid = getuid();
if( uid != 0 ) { printf("Your UID is %d. Please log in as root.\n",uid);
exit(1); }

int socket_fd = socket( AF_INET , SOCK_PACKET , htons(0x3) );
if( socket_fd < 0 ) { printf("Error, creating socket.\n"); exit(1); }

while(1) {
recv( socket_fd , packet , packetsize, 0 );
printf("Ethernet destination MAC address: %s\n" ,
ether_ntoa( (struct ether_addr*) eth ) );

struct in_addr* daddr = malloc( sizeof( struct in_addr ) );
daddr->s_addr =ip->daddr;

printf("Source: inet_ntoa( %d ) -> %s\nDest: inet_ntoa( %d ) ->
%s\n" , ip->saddr , inet_ntoa( *(struct in_addr*) &ip->saddr ) ,
ip->daddr , inet_ntoa( *(struct in_addr*) &ip->daddr ) );
}

} //end source code


Okay the line

printf("Source: inet_ntoa( %d ) -> %s\nDest: inet_ntoa( %d ) -> %s\n" ,
ip->saddr , inet_ntoa( *(struct in_addr*) &ip->saddr ) , ip->daddr ,
inet_ntoa( *(struct in_addr*) &ip->daddr ) );

produces a strange output. ip->daddr and ip->saddr are different, but after
appling the method inet_ntoa the results are identical. Here is a sample
output of my xterm (communication between my wlan card and my wlan router):

Ethernet destination MAC address: 0:3:c9:84:af:cd
Source: inet_ntoa( 184723648 ) -> 192.168.2.11
Dest: inet_ntoa( 16951488 ) -> 192.168.2.11
Ethernet destination MAC address: 0:c:f1:35:c2:53
Source: inet_ntoa( 16951488 ) -> 192.168.2.1
Dest: inet_ntoa( 184723648 ) -> 192.168.2.1

I really do not understand how this can happen. I've already read some man
pages and read the apropriate kernel header's source. I would be very
thankful if anybody could help. It's driving me crazy and I don't know
what's going on.

 
Reply With Quote
 
 
 
 
Giovanni
Guest
Posts: n/a

 
      03-21-2006, 03:17 PM
Paul wrote:

> I really do not understand how this can happen. I've already read some man
> pages and read the apropriate kernel header's source. I would be very
> thankful if anybody could help. It's driving me crazy and I don't know
> what's going on.


You did not read the right man pages :-(
inet_ntoa() documentation states:

The inet_ntoa() function converts the Internet host address 'in' given
in network byte order to a string in standard numbers-and-dots
notation. The string is returned in a statically allocated buffer,
which subsequent calls will overwrite.

Ciao
Giovanni
--
A computer is like an air conditioner,
it stops working when you open Windows.
Registered Linux user #337974 <http://counter.li.org/>
 
Reply With Quote
 
Paul
Guest
Posts: n/a

 
      03-21-2006, 04:02 PM
> The inet_ntoa() function converts the Internet host address 'in' given
> in network byte order to a string in standard numbers-and-dots
> notation. The string is returned in a statically allocated buffer,
> which subsequent calls will overwrite.


Acutally I do not understand how this solves my problem. I read this man
page and I understand, that a given address in nethwork byte order of
format struct in_addr is transformed into a dot-number string. Could you
please be a little bit more precisly ? Thanks.


 
Reply With Quote
 
Giovanni
Guest
Posts: n/a

 
      03-21-2006, 04:34 PM
Paul wrote:
>> The inet_ntoa() function converts the Internet host address 'in' given
>> in network byte order to a string in standard numbers-and-dots
>> notation. The string is returned in a statically allocated buffer,
>> which subsequent calls will overwrite.

>
> Acutally I do not understand how this solves my problem. I read this man
> page and I understand, that a given address in nethwork byte order of
> format struct in_addr is transformed into a dot-number string. Could you
> please be a little bit more precisly ? Thanks.
>
>


In a single statement like:
printf("Source: inet_ntoa( %d ) -> %s\nDest: inet_ntoa( %d ) -> %s\n",
ip->saddr, inet_ntoa(*(struct in_addr*)&ip->saddr ), ip->daddr,
inet_ntoa(*(struct in_addr*) &ip->daddr ));
you call inet_ntoa() twice. The first call fills the buffer with the
string corresponding to the 'ip->daddr' end returns the pointer to the
buffer. The second call fills the *same* buffer (it is a static buffer)
with the string corresponding to the 'ip->saddr' and returns the pointer
to the buffer. Unfortunately this destroys the results of the previous
call before it is used.

Try with two different statements:
printf("Source: inet_ntoa( %d ) -> %s\n",
ip->saddr, inet_ntoa(*(struct in_addr*)&ip->saddr));
printf("Dest: inet_ntoa( %d ) -> %s\n",
ip->daddr, inet_ntoa(*(struct in_addr*) &ip->daddr));
or better replace also the format specifier %d with %08x which gives a
better view for adresses.

Ciao
Giovanni
--
A computer is like an air conditioner,
it stops working when you open Windows.
Registered Linux user #337974 <http://counter.li.org/>
 
Reply With Quote
 
Paul
Guest
Posts: n/a

 
      03-21-2006, 05:24 PM
Oh man, thank you really much ! Now I understand what is meant by a static
buffer. And the program works fine. Thanks for help!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

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




1 2 3 4 5 6 7 8 9 10 11