Ubuntu:gethostbyaddr 返回 NULL 并出现 HOST_NOT_FOUND 错误
我的机器是 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 ...我的机器。
附: 猫 /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
猫 /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
猫 /etc/host.conf
# The "order" line is only used by old versions of the C library.
order hosts,bind
multi on
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
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)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于反向 DNS 查找,行为由 /etc/host.conf 控制。您需要在那里指定主机。您需要在 /etc/host.conf 中添加此行:
请参阅 http://tldp.org /LDP/nag/node82.html。
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:
See http://tldp.org/LDP/nag/node82.html.