title: What's wrong with gethostbyname?
body: <p>I am using this snippet of code I found in <a href="http://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/" rel="nofollow">http://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/</a> to perform dns lookups</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  int main(int argc, char *argv[ ]) {
      struct hostent *h;
  
      /* error check the command line */
      if(argc != 2) {
          fprintf(stderr, "Usage: %s hostname\n", argv[0]);
          exit(1);
      }
  
      /* get the host info */
      if((h=gethostbyname(argv[1])) == NULL) {
          herror("gethostbyname(): ");
          exit(1);
      }
      else     
          printf("Hostname: %s\n", h-&gt;h_name);
  
      printf("IP Address: %s\n", inet_ntoa(*((struct in_addr *)h-&gt;h_addr)));     
      return 0;
  }
  </code></pre>
  
  <p>I am facing a weird fact</p>
  
  <pre><code>./test www.google.com
  Hostname: www.l.google.com
  IP Address: 209.85.148.103
  </code></pre>
  
  <p>works fine, but if I try to resolve an incomplete IP address I get this</p>
  
  <pre><code>./test 10.1.1
  Hostname: 10.1.1
  IP Address: 10.1.0.1
  </code></pre>
  
  <p>I would expect an error like the following</p>
  
  <pre><code>./test www.google
  gethostbyname(): : Unknown host
  </code></pre>
  
  <p>but the program seems to work.</p>
  
  <p>Any idea why?</p>