linux - 为什么 gethostbyaddr 不适用于 www.google.com

标签 linux network-programming ubuntu-10.04

/*  As usual, make the appropriate includes and declare the variables.  */

#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    char *host, **names, **addrs;
    struct hostent *hostinfo;

/*  Set the host in question to the argument supplied with the getname call,
    or default to the user's machine.  */

    if(argc == 1) {
        char myname[256];
        gethostname(myname, 255);
        host = myname;
    }
    else
        host = argv[1];

/*  Make the call to gethostbyname and report an error if no information is found.  */

    hostinfo = gethostbyname(host);
    if(!hostinfo) {
        fprintf(stderr, "cannot get info for host: %s\n", host);
        exit(1);
    }

/*  Display the hostname and any aliases it may have.  */

    printf("results for host %s:\n", host);
    printf("Name: %s\n", hostinfo -> h_name);
    printf("Aliases:");
    names = hostinfo -> h_aliases;
    while(*names) {
        printf(" %s", *names);
        names++;
    }
    printf("\n");

/*  Warn and exit if the host in question isn't an IP host.  */

    if(hostinfo -> h_addrtype != AF_INET) {
        fprintf(stderr, "not an IP host!\n");
        exit(1);
    }

/*  Otherwise, display the IP address(es).  */

    addrs = hostinfo -> h_addr_list;
    while(*addrs) {
        char *ip_str = inet_ntoa(*(struct in_addr *)*addrs); 

        printf(" %s", ip_str);

    /* <BEGIN> Get the host name by IP address */
    struct in_addr addr = {0};
    if (!inet_aton(ip_str, &addr)) {
        printf("Cannot parse IP %s\n", ip_str);
        exit(1);
    }

      struct hostent *remoteHost = gethostbyaddr((void*)&addr, sizeof(addr), AF_INET);
    if (remoteHost == NULL) {
      printf("\nInvalid remoteHost\n");
      exit(1);
    }

    printf("\nOfficial name: %s\n", remoteHost->h_name);
    char **pAlias;
    for(pAlias=remoteHost->h_aliases; *pAlias != 0; pAlias++) {
      printf("\tAlternate name: %s\n", *pAlias);
    }

        /* <End> */
        addrs++;
    }
    printf("\n");
    exit(0);
}

/* www.yahoo 测试运行 */

user@ubuntu:~/Documents/C$ ./getname2way www.yahoo.com
results for host www.yahoo.com:
Name: any-fp.wa1.b.yahoo.com
Aliases: www.yahoo.com fp.wg1.b.yahoo.com
 209.191.122.70
Official name: ir1.fp.vip.mud.yahoo.com

/* www.google.com 测试运行 */

user@ubuntu:~/Documents/C$ ./getname2way www.google.com
results for host www.google.com:
Name: www.l.google.com
Aliases: www.google.com
 74.125.225.83
Invalid remoteHost

为什么代码不适用于 www.google.com?

最佳答案

IP地址74.125.225.83没有反向PTR记录。您始终可以使用 nslookupdig 在命令行上进行测试。

can't find 74.125.225.83: Non-existent domain

http://en.wikipedia.org/wiki/Reverse_DNS_lookup

关于linux - 为什么 gethostbyaddr 不适用于 www.google.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6718869/

相关文章:

c++ - IO 完成端口和套接字 WSARecv()

c - Valgrind 的输出缺少基本值

在 Ubuntu 上安装期间出现 ruby​​gems 1.3.7 TypeError(在 'merge' 中)

ubuntu - 我不会在 ubuntu 32 位版本上安装 voltdb

javascript - 尝试在 Linux 上使用 Node 运行 hello.js 脚本

qt - Qt5 如何检查 url 是否可用?

linux - Nano - 禁用保存确认?

c - 写入h_addr_list

ruby -/proc目录脚本

linux - 将标准输出中的数字插入标准输出中的字符串