leading zeroes in gethostbyname()

bridged with qdn.public.qnxrtp.porting
Post Reply
Guest

leading zeroes in gethostbyname()

Post by Guest » Wed Sep 19, 2001 2:38 pm

Hello,

I'm experiencing unexpected behaviour when calling gethostbyname()
on a string that contains a leading zero internet address.

for example "192.168.0.039"

The leading zeroes make it being interpreted as being octal
instead of hexadecimal.

i.e. the address string above comes back as 192.168.0.33
where 33 comes from (3 * 8) + 9
Is this a bug, or is this intended as such?

See code snippet below; compile using -lsocket

Regards, Leon Woestenberg

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int main()
{
int result;
char *address = "192.168.0.039";
struct sockaddr_in sock_addr = { 0, 0, 0 };
struct hostent *hostentry = gethostbyname(address);

if (hostentry != 0)
{
struct in_addr *ina;
printf("Obtained host entry for %s.\n", address);
sock_addr.sin_family = hostentry->h_addrtype;
sock_addr.sin_port = htons(2071);
sock_addr.sin_addr = *(struct in_addr *)hostentry->h_addr_list[0];

ina = (struct in_addr *)hostentry->h_addr_list[0];

printf("Official name: %s\n", hostentry->h_name);
printf("Address bytes (raw): %08x\n", *ina);
printf("Address: %s\n", inet_ntoa(*(struct in_addr *)hostentry->h_addr_list[0]));
}
return result;
}

Leon Woestenberg

Re: leading zeroes in gethostbyname()

Post by Leon Woestenberg » Wed Sep 19, 2001 2:57 pm

leon.woestenberg@axon.nl wrote:
Hello,

I'm experiencing unexpected behaviour when calling gethostbyname()
on a string that contains a leading zero internet address.

for example "192.168.0.039"
A search on groups.google.com learned me that each TCP/IP stack does this,
i.e. interpret 039 as being octal. ( even the 9!)

Although many RFC (even the Internet numbering RFC) gives examples where
leading zero addresses are interpreted as DECIMAL numbers.

Major contradiction. A fact I never learned in my years of programming
TCP/IP application until now that I'm programming a fool prool app.

Regards, Leon Woestenberg.

Post Reply

Return to “qdn.public.qnxrtp.porting”