3

I have following script:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

const char *ip="190.162.1.2";

int main(int argc, const char * argv[])
{
    in_addr host_addr;
    hostent *addr=0;
    hostent *host=0;
    inet_pton(AF_INET, ip, &host_addr);
    addr=gethostbyaddr(&host_addr, sizeof(host_addr), AF_INET);
    printf("gethostbyaddr(%s):\n",ip);
    if(!addr)
        herror("Unable to resolve host by address");
    else {
        printf(" OK\n");
        host=gethostbyname(addr->h_name); //use the hostname we got previously
        printf("gethostbyname(%s):\n",addr->h_name);
        if(!host){
            herror(" Unable to resolve host by name"); //gets here, why?
            return 0;
        } else {
            printf(" IP addresses of %s: \n",addr->h_name);
            in_addr **addr_list;
            addr_list = (in_addr **)host->h_addr_list;
            for(int i = 0; addr_list[i] != NULL; i++) {
                printf("%s \n", inet_ntoa(*addr_list[i]));
            }
        }
    }
    return 0;
}

And I wonder that why does gethostbyname fail with hostname from previously successful gethostbyaddr. Could anyone explain me why?

Progress:

gethostbyaddr(190.162.1.2):
 OK
gethostbyname(pc-2-1-162-190.cm.vtr.net):
 Unable to resolve host by name: Unknown host

But it works with other IP addresses like 173.194.34.129 (google.com) etc.

1 Answers1

0

You may have an error in the local configuration of your DNS server. DNS has mappings in both directions (IP to host name and host name to IP); looks like the latter is missing.

Get your system administrator to check it out.

Does it show the IP address if you ping the host using the hostname from the command line?

noz
  • 1,863
  • 13
  • 14
  • ping pc-2-1-162-190.cm.vtr.net => unknown host + nah, it won't be error in just in my local config, when I try to look the host up on any online service, result is same ... http://www.dnswatch.info, http://www.hcidata.info/host2ip.htm, http://hostnametoip.com, ... –  Mar 27 '14 at 20:12
  • Ping isn't the right tool, use nslookup. nslookup 190.162.1.2 works, nslookup pc-2-1-162-190.cm.vtr.net doesn't. So you are correct, pc-2-1-162-190.cm.vtr.net is not in DNS. – Ciclamino Mar 27 '14 at 20:13