title: getaddrinfo resolves hostname incorrectly
body: <p>I try to use getaddrinfo function (<a href="http://man7.org/linux/man-pages/man3/getaddrinfo.3.html" rel="nofollow">http://man7.org/linux/man-pages/man3/getaddrinfo.3.html</a>) to get in_addr structure of IP address, but as far as I see, something is wrong, or I do something wrong. Consider following code snippet.</p>
  
  <pre><code>bool resolve_host (string hostname, in_addr* address) {
      cout &lt;&lt; "hostname  = " &lt;&lt; hostname &lt;&lt; endl;
      addrinfo *res {};
  
      addrinfo hints {0, AF_INET, 0, 0, 0, nullptr, nullptr, nullptr};
  
      auto result = getaddrinfo (hostname.c_str (), nullptr, &amp;hints, &amp;res);
      if (!result) { 
          memcpy (address, &amp;((struct sockaddr_in*) res-&gt;ai_addr)-&gt;sin_addr, sizeof (in_addr));
          freeaddrinfo (res);
  
          cout &lt;&lt; "resolved addr = " &lt;&lt; inet_ntoa (*address) &lt;&lt; endl &lt;&lt; endl;
  
          return true;
      }
      else {
          return false;
      }
  }
  </code></pre>
  
  <p>With the usage like this:</p>
  
  <pre><code>in_addr addr {};
  resolve_host ("www.google.pl", &amp;addr);      // 1
  resolve_host ("linux-1smb", &amp;addr);         // 2
  resolve_host ("192.168.100.100", &amp;addr);    // 3
  resolve_host ("192.168.100.1001", &amp;addr);   // 4
  
  resolve_host ("wrong_name", &amp;addr);         // 5
  </code></pre>
  
  <p>Output looks like this:</p>
  
  <pre><code>hostname  = www.google.pl
  resolved addr = 74.125.71.94
  
  hostname  = linux-1smb
  resolved addr = 192.168.0.101
  
  hostname  = 192.168.100.100
  resolved addr = 192.168.100.100
  
  hostname  = 192.168.100.1001
  hostname  = wrong_name
  resolved addr = 217.74.65.145
  </code></pre>
  
  <p>First four calls acts like intended. There is of course an assumption that linux-1smb is a known hostname (added to /etc/hosts). But the last (5) call shows that hostname "wrong_name" (which of course is not added to /etc/hosts) has an IP address 217.74.65.145.</p>
  
  <p>Could you tell me where do I have a mistake (in code, or understanding how getaddrinfo works)?</p>
  
  <p>Content of the /etc/hosts is:</p>
  
  <pre><code>#
  # hosts         This file describes a number of hostname-to-address
  #               mappings for the TCP/IP subsystem.  It is mostly
  #               used at boot time, when no name servers are running.
  #               On small systems, this file can be used instead of a
  #               "named" name server.
  # Syntax:
  #    
  # IP-Address  Full-Qualified-Hostname  Short-Hostname
  #
  
  127.0.0.1       localhost 
  
  # special IPv6 addresses
  ::1             localhost ipv6-localhost ipv6-loopback
  
  fe00::0         ipv6-localnet 
  
  ff00::0         ipv6-mcastprefix 
  ff02::1         ipv6-allnodes 
  ff02::2         ipv6-allrouters 
  ff02::3         ipv6-allhosts 
  192.168.0.101   linux-1smb 
  225.0.0.37      multi-test 
  </code></pre>
  
  <p>What's worth to be mentioned is, if I change hints, to this:</p>
  
  <pre><code>addrinfo hints {0, AF_UNSPEC, 0, 0, 0, nullptr, nullptr, nullptr};
  </code></pre>
  
  <p>which is (as far as I know - from documentation) equals to:</p>
  
  <pre><code>getaddrinfo (hostname.c_str (), nullptr, nullptr, &amp;res);
  </code></pre>
  
  <p>The result of fourth call is:</p>
  
  <pre><code>hostname  = 192.168.100.1001
  resolved addr = 217.74.65.145
  </code></pre>
  
  <p>Which is the same IP address like in case of call 5.</p>
  
  <p>Possible solution:</p>
  
  <p>I read in the comment some possible situation and I checked it. It turns out, that the address 217.74.65.145 is an address of my ISP, which basically forwards all unknown hostnames (from browser for example) into this address. I think, that's the reason why getaddrinfo returns this address. I still don't know how to solve this, but now I now why this happens.</p>
  
  <p>Thanks</p>