4

I try to use getaddrinfo function (http://man7.org/linux/man-pages/man3/getaddrinfo.3.html) 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.

bool resolve_host (string hostname, in_addr* address) {
    cout << "hostname  = " << hostname << endl;
    addrinfo *res {};

    addrinfo hints {0, AF_INET, 0, 0, 0, nullptr, nullptr, nullptr};

    auto result = getaddrinfo (hostname.c_str (), nullptr, &hints, &res);
    if (!result) { 
        memcpy (address, &((struct sockaddr_in*) res->ai_addr)->sin_addr, sizeof (in_addr));
        freeaddrinfo (res);

        cout << "resolved addr = " << inet_ntoa (*address) << endl << endl;

        return true;
    }
    else {
        return false;
    }
}

With the usage like this:

in_addr addr {};
resolve_host ("www.google.pl", &addr);      // 1
resolve_host ("linux-1smb", &addr);         // 2
resolve_host ("192.168.100.100", &addr);    // 3
resolve_host ("192.168.100.1001", &addr);   // 4

resolve_host ("wrong_name", &addr);         // 5

Output looks like this:

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

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.

Could you tell me where do I have a mistake (in code, or understanding how getaddrinfo works)?

Content of the /etc/hosts is:

#
# 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 

What's worth to be mentioned is, if I change hints, to this:

addrinfo hints {0, AF_UNSPEC, 0, 0, 0, nullptr, nullptr, nullptr};

which is (as far as I know - from documentation) equals to:

getaddrinfo (hostname.c_str (), nullptr, nullptr, &res);

The result of fourth call is:

hostname  = 192.168.100.1001
resolved addr = 217.74.65.145

Which is the same IP address like in case of call 5.

Possible solution:

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.

Thanks

Artur Pyszczuk
  • 1,920
  • 1
  • 16
  • 23
  • What's the contents of your etc/hosts file? – BlamKiwi Jan 25 '15 at 10:22
  • 3
    What does a lookup using dig/host/whatever yield? Maybe your nameserver (like that of many ISPs) spits out the address of a webpage with an error message instead of returning a proper error? BTW: Your code blindly assumes that it receives a `struct sockaddr_in`. It could be a `struct sockaddr_in6` if you enable IPv6. Also, there could be multiple addresses be returned. – Ulrich Eckhardt Jan 25 '15 at 10:53
  • 217.74.65.145 is interia.pl server. try whois on it – 4pie0 Jan 25 '15 at 11:23

0 Answers0