Unix Programming - inet_addr and inet_aton api not working

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2004 > inet_addr and inet_aton api not working





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author inet_addr and inet_aton api not working
buzz

2004-03-15, 6:35 pm

Folks,
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(yes i am trying this on a linux box, this is general sockets api
question):
======
$ 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);
}
---------------
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



those who know me have no need of my name

2004-03-15, 9:34 pm

in comp.unix.programmer i read:

>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.


it treats them as valid because they *are* valid -- you just aren't used to
seeing them. 192.1 means 192.0.0.1 and 192.168.0 means 192.0.168.0. if
you really want to be shocked try 0xc0.1 and 0300.0250.0 (which are the
same addresses). if you want only 4-part decimal addresses then use
inet_pton, otherwise check the string yourself after inet_aton returns
success.

> internetAddRet = inet_addr(goodAdd);
> if(internetAddRet == 0)
> printf("good address(%s) is seen as bad by inet_addr, why??\n",
>goodAdd);


inet_addr returns (in_addr_t)(-1) on error, not 0. (since that's a valid
ip address it's not suitable for use in general.)

>bad address(192.168.0.) is seen as good by inet_addr,why ?? translated
>32bit address=4294967295


4294967295 is probably (unsigned int)(in_addr_t)(-1) on your system, i.e.,
inet_addr returned an error indication.

--
a signature
buzz

2004-03-16, 1:35 am

I was under the impression that there had to be 3 dots in the string
representation passed to these functions for it to get past the
first stage of validation and then it would check for values greater the
255.

Now my question based on the previous response in the thread is:
what is the algorith it uses when less the 3 dots are passed in the string
value ?
The previous post indicates the following translations foe the two cases:
192.1 means 192.0.0.1 and 192.168.0 means 192.0.168.0.
does it take the first octet as is passed in and then fills in the missing
octets with 0 between the remaining octets that are passed in ?

I spent few hours trying to get the code to fail before posting to the
newsgroup. Now i feel that i can move on to my own validation of the strings
passed by the user and guide appropriatly.
thanks for the responses.
"Marc Rochkind" <rochkind@basepath.com> wrote in message
news:519a013.0403152019.3ce569b@posting.google.com...
> "buzz" <<$remove!>buzzbell2@yahoo.com</$remove!>> wrote in message

news:<FVq5c.5680$rQ.4847@lakeread04>...
string.[color=darkred]
>
> [snip]
>
> What do you mean by "bad?" 192.168.0 and 192.1 aren't bad on their
> face, and those formats (2 dots, 1 dot) are specified as OK by the doc
> for inet_addr (Single UNIX Specification,
> http://www.unix.org/version3). Or, do you mean that the addresses are
> OK in format, but are bad in the sense that they lead nowhere? If
> that's what you mean, then you're asking too much of these functions.
> They just do arithmetic.
>
> --
> Marc Rochkind
> "Advanced UNIX Programming" (publishing in April 2004)
> www.basepath.com/aup



Barry Margolin

2004-03-16, 2:35 am

In article <w4x5c.6559$rQ.4119@lakeread04>,
"buzz" <<$remove!>buzzbell2@yahoo.com</$remove!>> wrote:
> Now my question based on the previous response in the thread is:
> what is the algorith it uses when less the 3 dots are passed in the string
> value ?
> The previous post indicates the following translations foe the two cases:
> 192.1 means 192.0.0.1 and 192.168.0 means 192.0.168.0.
> does it take the first octet as is passed in and then fills in the missing
> octets with 0 between the remaining octets that are passed in ?


Yes, that's the basic idea. I think it's a holdover from the old
classful addressing days, and the fact that dotted quads were not
standardized immediately. If you had class A network 10, you could
enter addresses like 10.1, 10.500, and 10.589835, which are equivalent
to 10.0.0.1, 10.0.1.244, and 10.9.0.11, respectively. The part before
the dot is the network number, the part after is the host number.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Marc Rochkind

2004-03-16, 4:35 am

"buzz" <<$remove!>buzzbell2@yahoo.com</$remove!>> wrote in message news:<FVq5c.5680$rQ.4847@lakeread04>...
> Folks,
> 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.
>


[snip]

What do you mean by "bad?" 192.168.0 and 192.1 aren't bad on their
face, and those formats (2 dots, 1 dot) are specified as OK by the doc
for inet_addr (Single UNIX Specification,
http://www.unix.org/version3). Or, do you mean that the addresses are
OK in format, but are bad in the sense that they lead nowhere? If
that's what you mean, then you're asking too much of these functions.
They just do arithmetic.

--
Marc Rochkind
"Advanced UNIX Programming" (publishing in April 2004)
www.basepath.com/aup
buzz

2004-03-16, 8:36 am

Barry ,
thanks for the followup. Since we are dealing with the new generation of
users who are most likely to see the dotted notaion being used with 4 dots
(without much thought to the network/host value based on class etc), one
has to hand hold these users as they input data from the
command(console)based applications. GUI applications can be designed with
the appropriate fields for each octet etc.
thanks.

"Barry Margolin" <barmar@alum.mit.edu> wrote in message
news:barmar-B3B568.01424816032004@comcast.ash.giganews.com...
> In article <w4x5c.6559$rQ.4119@lakeread04>,
> "buzz" <<$remove!>buzzbell2@yahoo.com</$remove!>> wrote:
string[color=darkred]
cases:[color=darkred]
missing[color=darkred]
>
> Yes, that's the basic idea. I think it's a holdover from the old
> classful addressing days, and the fact that dotted quads were not
> standardized immediately. If you had class A network 10, you could
> enter addresses like 10.1, 10.500, and 10.589835, which are equivalent
> to 10.0.0.1, 10.0.1.244, and 10.9.0.11, respectively. The part before
> the dot is the network number, the part after is the host number.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***



Barry Margolin

2004-03-16, 12:35 pm

In article <MIC5c.6600$rQ.4873@lakeread04>,
"buzz" <<$remove!>buzzbell2@yahoo.com</$remove!>> wrote:

> thanks for the followup. Since we are dealing with the new generation of
> users who are most likely to see the dotted notaion being used with 4 dots
> (without much thought to the network/host value based on class etc), one
> has to hand hold these users as they input data from the
> command(console)based applications. GUI applications can be designed with
> the appropriate fields for each octet etc.


You're certainly free to do your own syntax checking. Just don't expect
the old routines to do it for you, because they have some ancient
backward compatibility rules built in.

Also watch out for octets that begin with 0 -- the standard routines
treat these as octal rather than decimal. So 10.0.0.010 is equivalent
to 10.0.0.8, not 10.0.0.10.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com