开发者

Ubuntu: gethostbyaddr Returns NULL with HOST_NOT_FOUND Error

My machine is Ubuntu Server 10.10.

I have the following code snippet:

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

I run it with:

./main 10.50.10.252

(10.50.10.252 is my eth0 IP address)

$ 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
          inet开发者_运维技巧6 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)

But the output is:

in_addr: 10.50.10.252
gethostbyaddr: Success
HOST_NOT_FOUND

I just don't know why HOST_NOT_FOUND, because I could use the internet, ssh, scp... from my machine.

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


For reverse DNS lookups, the behavior is controlled by /etc/host.conf. You need to specify hosts in there. You need to add this line in your /etc/host.conf:

order hosts,bind

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜