Ubuntu:gethostbyaddr返回NULL并带有HOST_NOT_FOUND错误

时间:2011-07-26 22:43:59

标签: c++ ubuntu ip host

我的机器是Ubuntu Server 10.10。

我有以下代码段:

#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    struct in_addr addr;
    struct hostent *he;
    /* printf ("sizeof struct in_addr: %i\n", sizeof(struct in_addr)); */
    printf("in_addr: %s\n", argv[1]);
    addr.s_addr = inet_addr(argv[1]);
    if (addr.s_addr == 0 || addr.s_addr == -1)
    {
        perror("inet_addr");
        return 1;
    }
    /* else */
    he = gethostbyaddr(&addr, 4, AF_INET);
    if (he == NULL)
    {
        perror("gethostbyaddr");
        switch (h_errno)
        {
            case HOST_NOT_FOUND:
                printf("HOST_NOT_FOUND\n");
                break;
            case NO_ADDRESS:
                printf("NO_ADDRESS\n");
                break;
                /* case NO_DATA: print ("NO_DATA\n"); break; */
            case NO_RECOVERY:
                printf("NO_RECOVERY\n");
                break;
            case TRY_AGAIN:
                printf("TRY_AGAIN\n");
                break;
        }
        return 1;
    }
    /* else */
    printf("%s\n", he->h_name);
    return 0;
}

我用:

运行它
./main 10.50.10.252

(10.50.10.252是我的eth0 IP地址)

$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:65:0d:27
          inet addr:10.50.10.252  Bcast:10.50.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe65:d27/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1225069 errors:0 dropped:0 overruns:0 frame:0
          TX packets:980849 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:695573851 (695.5 MB)  TX bytes:658044535 (658.0 MB)
          Interrupt:19 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:783434 errors:0 dropped:0 overruns:0 frame:0
          TX packets:783434 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:701737148 (701.7 MB)  TX bytes:701737148 (701.7 MB)

但输出是:

in_addr: 10.50.10.252
gethostbyaddr: Success
HOST_NOT_FOUND

我只是不知道为什么HOST_NOT_FOUND,因为我可以在我的机器上使用互联网,ssh,scp ....

P.S .: cat / etc / hosts

127.0.0.1       localhost
127.0.1.1       ubuntu.localdomain      ubuntu

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

cat /etc/nsswitch.conf

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat
group:          compat
shadow:         compat

hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

cat /etc/host.conf

# The "order" line is only used by old versions of the C library.
order hosts,bind
multi on

1 个答案:

答案 0 :(得分:0)

对于反向DNS查找,行为由/etc/host.conf控制。您需要在那里指定主机。您需要在/etc/host.conf中添加以下行:

order hosts,bind

请参阅http://tldp.org/LDP/nag/node82.html