【问题标题】:Ubuntu: gethostbyaddr Returns NULL with HOST_NOT_FOUND ErrorUbuntu:gethostbyaddr 返回 NULL 并出现 HOST_NOT_FOUND 错误
【发布时间】:2011-07-26 22:43:58
【问题描述】:

我的机器是 Ubuntu Server 10.10。

我有以下代码sn-p:

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

附: 猫 /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

【问题讨论】:

  • 尝试不同的 IP 地址是否有效?
  • 您的系统在 /etc/hosts 中有条目吗?您在 DNS 中有任何条目吗?您的 /etc/nsswitch.conf 是否设置为使用文件和 dns 作为主机?
  • 对 perror() 的调用没有帮助,因为它查看的是 errno,而不是 h_errno。 “成功”是一条红鲱鱼。
  • 是的,我同意“成功”是没有用的。
  • 如果我在 /etc/network/interfaces 中更改我的 IP 地址,它也不起作用

标签: c++ ubuntu ip host


【解决方案1】:

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

order hosts,bind

http://tldp.org/LDP/nag/node82.html

【讨论】:

  • 嗨 Jaidev,该行已经在 /etc/host.conf 中了。有什么想法吗?
  • 刚刚注意到您的 /etc/hosts 没有 10.50.10.252 的条目。那是错字吗? gethostbyaddr 需要该名称或名称服务器中的 PTR 条目。
  • 我们在 DHCP 本地 Intranet 中。 IP 地址是随机分配的。
  • 在这种情况下,DHCP 需要连接动态 DNS,以便在 DNS 服务器上更新 IP:your_hostname 映射。然后它可以正确响应您的 PTR 查询。类似debianadmin.com/…
  • 其他机器上的东西太奇怪了,一切都很好。
猜你喜欢
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多