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 *
+ 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;
}