inet_addr and inet_aton not working on linux

inet_addr and inet_aton not working on linux

Post by buzz » Wed, 17 Mar 2004 08:15:51



I have a very simple program that uses the inet_addr and the inet_aton
functions to translate a dottoed ip address to a binary number
and I am not having much success.
inet_addr treats a bad ip(192.168.0) address string as a valid address
string
inet_aton treats another bad ip address (192.1) as a valid address string.

I have tried various experiments and don't seem to get the resulst that I
expect. I have also taken one "stevens" sample program and it too allows
some bad addresss to get throuh(it uses the same calls).

Please tell me what I am doing wrong or is it my enviroment  ?

Here are the details:
gcc info:
======
$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with:
../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/
share/info --enable-shared --enable-threads=posix --disable-checking --host=
i386-re
dhat-linux --with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

Source Code:
--------------
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char** argv)
{
   char ipentered[32];
   struct sockaddr_in   serveraddr;
   unsigned long internetAddRet=0;
   char goodAdd[]="192.168.0.255";
   char badAdd1[]="192.168.0.";
   char badAdd2[]="192.1";
   if (argc != 2)
   {
     printf("usage: tcpcli <IPaddress>\n");
     exit(1);
   }
   printf("results of static ip address:\n");
   internetAddRet = inet_addr(goodAdd);
   if(internetAddRet == 0)
       printf("good address(%s) is seen as bad by inet_addr, why??\n",
goodAdd);
   else
   {
     printf("good address(%s) is seen as good by inet_addr, translated 32bit
address=%u\n",goodAdd,internetAddRet);
   }
   if(inet_aton(goodAdd,&serveraddr.sin_addr) == 0)
     printf("good address(%s) is seen as bad by inet_aton, why??\n",
goodAdd);
   else
     printf("good address(%s) is seen as good by inet_atonr, translated
32bit address=%u\n",goodAdd,serveraddr.sin_addr);

    bzero(&serveraddr, sizeof(serveraddr));
    internetAddRet = inet_addr(badAdd1);
   if(internetAddRet == 0)
       printf("bad address(%s) is seen as bad by inet_addr\n", badAdd1);
   else
   {
     printf("bad address(%s) is seen as good by inet_addr,why ?? translated
32bit address=%u\n",badAdd1,internetAddRet);
   }
   if(inet_aton(badAdd1,&serveraddr.sin_addr) == 0)
     printf("bad address(%s) is seen as bad by inet_aton\n", badAdd1);
   else
     printf("bad address(%s) is seen as good by inet_aton why ??, translated
32bit address=%u\n",badAdd1,serveraddr.sin_addr);

 bzero(&serveraddr, sizeof(serveraddr));
    internetAddRet = inet_addr(badAdd2);
   if(internetAddRet == 0)
       printf("bad address(%s) is seen as bad by inet_addr\n", badAdd2);
   else
   {
     printf("bad address(%s) is seen as good by inet_addr,why ?? translated
32bit address=%u\n",badAdd2,internetAddRet);
   }
   if(inet_aton(badAdd2,&serveraddr.sin_addr) == 0)
     printf("bad address(%s) is seen as bad by inet_aton\n", badAdd2);
   else
     printf("bad address(%s) is seen as good by inet_aton why ??, translated
32bit address=%u\n",badAdd2,serveraddr.sin_addr);
   bzero(&serveraddr, sizeof(serveraddr));

   printf("now will look at the results passed by the user\n");
   //results don't change whether i pass the argv[1]  instead of copied
string
   strcpy(ipentered, argv[1]);
   internetAddRet = 0;
   internetAddRet = inet_addr(ipentered);
   if(internetAddRet == 0)
       printf("ip address(%s) is seen as badd by inet_addr\n", ipentered);
    else
    {
       printf("ip address (%s) is seen as good by inet translated
address=%u\n",ipentered,internetAddRet);
    }
   bzero(&serveraddr, sizeof(serveraddr));
   if(inet_aton(ipentered,&serveraddr.sin_addr) == 0)
     printf("ip address(%s) is seen as bad by inet_aton\n",ipentered);
   else
     printf("ip address(%s) is seen as good by inet_aton.translated
value=%u\n",ipentered,serveraddr.sin_addr);
   exit(0);

Quote:}

---------------
sample output:
./testapi1 192.168.0
results of static ip address:
good address(192.168.0.255) is seen as good by inet_addr, translated 32bit
address=
4278233280
good address(192.168.0.255) is seen as good by inet_atonr, translated 32bit
address
=4278233280
bad address(192.168.0.) is seen as good by inet_addr,why ?? translated 32bit
addres
s=4294967295
bad address(192.168.0.) is seen as bad by inet_aton
bad address(192.1) is seen as good by inet_addr,why ?? translated 32bit
address=167
77408
bad address(192.1) is seen as good by inet_aton why ??, translated 32bit
address=16
777408
now will look at the results passed by the user
ip address (192.168.0) is seen as good by inet translated address=43200
ip address(192.168.0) is seen as good by inet_aton.translated value=43200
 
 
 

inet_addr and inet_aton not working on linux

Post by Villy Krus » Wed, 17 Mar 2004 23:56:45


On Mon, 15 Mar 2004 18:15:51 -0500,

Quote:> I have a very simple program that uses the inet_addr and the inet_aton
> functions to translate a dottoed ip address to a binary number
> and I am not having much success.
> inet_addr treats a bad ip(192.168.0) address string as a valid address
> string
> inet_aton treats another bad ip address (192.1) as a valid address string.

These are actual valid string representation of IP numbers.

Try for example with these strings:
"192.168.0.5"
"192.168.5"
"3232235525"
"0xC0A80005"
"030052000005"

They all valid string representation of the same IP number.  
Also compare "192.168.0.10" and "192.168.0.010", and, no, they are not
the same IP number.  010 is and octal number with the value 8.

Villy

 
 
 

1. inet_aton() or inet_addr()?

Of inet_aton() or inet_addr(), which is more portable (i.e., POSIX
compliant)?  SUSv3 doesn't even list inet_aton(), but my man page
(on Linux.. *gasp*) says this about inet_addr():

        This (inet_addr) is an obsolete
        interface to inet_aton, described immediately above; it is
        obsolete  because -1 is a valid address (255.255.255.255),
        and inet_aton provides a cleaner  way  to  indicate  error
        return.

So I'm assuming that inet_addr() is the "best" way, since it's
in SUS... is this correct?

--
 josh(at)cc.gatech.edu  |  http://intmain.net:800

 522194 local keystrokes since last reboot (51 days ago)

2. Modemsettings for PPP (14.4,V42bis) ?????

3. Matrox Mystique ands X.

4. Serial Port Sample Code

5. Some Hardware that works with Linux and some that works not!

6. Problems compiling top

7. Linux and W98SE / Ping Linux from W98SE NOT working but can Ping W98SE from Linux

8. rc.conf file

9. Fujitsu-Siemens Keybird Wireless Office II does not work under Linux (works ok on other OSs)

10. Linux Tar Not Working If Not SU

11. inet_addr reentrancy?????? or MT safeness???

12. inet_addr on DG-UX - where?

13. inet_addr thread-safe??