c - What's wrong with gethostbyname? -



c - What's wrong with gethostbyname? -

i using snippet of code found in http://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/ perform dns lookups

#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main(int argc, char *argv[ ]) { struct hostent *h; /* error check command line */ if(argc != 2) { fprintf(stderr, "usage: %s hostname\n", argv[0]); exit(1); } /* host info */ if((h=gethostbyname(argv[1])) == null) { herror("gethostbyname(): "); exit(1); } else printf("hostname: %s\n", h->h_name); printf("ip address: %s\n", inet_ntoa(*((struct in_addr *)h->h_addr))); homecoming 0; }

i facing weird fact

./test www.google.com hostname: www.l.google.com ip address: 209.85.148.103

works fine, if seek resolve incomplete ip address this

./test 10.1.1 hostname: 10.1.1 ip address: 10.1.0.1

i expect error following

./test www.google gethostbyname(): : unknown host

but programme seems work.

any thought why?

it not bug rather feature of inet_aton() function:

description

the inet_aton() function converts specified string, in net standard dot notation, network address, , stores address in construction provided.

values specified using dot notation take 1 of next forms:

a.b.c.d when 4 parts specified, each interpreted byte of info , assigned, left right, 4 bytes of net address.

a.b.c when three-part address specified, lastly part interpreted 16-bit quantity , placed in rightmost 2 bytes of network address. makes three-part address format convenient specifying class b network addresses 128.net.host.

you can read more there, example.

c linux gethostbyname

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -