C 为什么';t gethostbyname()在不存在的主机名上失败?
我正试图从主机名中获取IPv4地址,所以我想我可以使用它。这工作得很好,但有一个问题:将不存在的主机名传递给它时,它不会失败 下面是一些代码:C 为什么';t gethostbyname()在不存在的主机名上失败?,c,sockets,dns,C,Sockets,Dns,我正试图从主机名中获取IPv4地址,所以我想我可以使用它。这工作得很好,但有一个问题:将不存在的主机名传递给它时,它不会失败 下面是一些代码: #include <netdb.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { printf("%p\n", gethostbyname("fkldsjflkasdjflkajsdflkjasdf.com"
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
printf("%p\n", gethostbyname("fkldsjflkasdjflkajsdflkjasdf.com"));
return 0;
}
为什么它不是空的
*编辑*
以下是转储结果的更完整示例:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
struct hostent *hp = gethostbyname("fkldsjflkasdjflkajsdflkjasdf.com");
printf("%s %d %d %p\n", hp->h_name, hp->h_addrtype, hp->h_length, hp->h_addr_list);
printf("%s\n", inet_ntoa(*((struct in_addr *) hp->h_addr)));
return 0;
}
正如您所看到的,我甚至可以为不存在的主机名获取IP地址!不知道这是怎么可能的…
h\u errno
包含什么?它应该是HOST\u未找到
仅在发生内部错误时返回null,即无法为结构分配内存这看起来像是您使用的DNS服务器的问题。当我运行代码时,它不会返回任何有用的内容 鉴于whois查询返回的IP地址
62.138.238.45
包括描述www.t-online.de
,我假设您使用的是德国电信(t-online)提供的互联网接入,并且您使用的是t-online提供的默认名称服务器。对于不存在的域,此名称服务器不会返回NXDOMAIN,而是提供一个IP地址,这会导致“有用”错误,即播发
通过在此IP上手动与HTTP服务器通话并使用主机
标题中的相关域进行的简短测试显示:
$ telnet 62.138.238.45 80
GET / HTTP/1.0
Host: fkldsjflkasdjflkajsdflkjasdf.com
<newline>
$telnet 62.138.238.45 80
GET/HTTP/1.0
主持人:fkldsjflkasdjflkajsdflkjasdf.com
这导致:
HTTP/1.1 302 Found
Date: Sun, 30 Dec 2018 19:42:04 GMT
Server: Apache/2.4.7 (Ubuntu)
Location: http://navigationshilfe1.t-online.de/dnserror?url=fkldsjflkasdjflkajsdflkjasdf.com/
Content-Length: 267
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://navigationshilfe1.t-online.de/dnserror?url=fkldsjflkasdjflkajsdflkjasdf.com/">here</a>.</p>
</body></html>
找到HTTP/1.1 302
日期:2018年12月30日星期日19:42:04 GMT
服务器:Apache/2.4.7(Ubuntu)
地点:http://navigationshilfe1.t-online.de/dnserror?url=fkldsjflkasdjflkajsdflkjasdf.com/
内容长度:267
连接:关闭
内容类型:text/html;字符集=iso-8859-1
302发现
建立
文档已移动
有关此“服务”的更多信息以及如何禁用它的信息,请参阅(德语)。那么,返回的结构包含什么?请参阅我的编辑…这可能不是问题。许多ISP倾向于“劫持”dns请求,并将您发送到不存在域名的广告页面。
$ telnet 62.138.238.45 80
GET / HTTP/1.0
Host: fkldsjflkasdjflkajsdflkjasdf.com
<newline>
HTTP/1.1 302 Found
Date: Sun, 30 Dec 2018 19:42:04 GMT
Server: Apache/2.4.7 (Ubuntu)
Location: http://navigationshilfe1.t-online.de/dnserror?url=fkldsjflkasdjflkajsdflkjasdf.com/
Content-Length: 267
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://navigationshilfe1.t-online.de/dnserror?url=fkldsjflkasdjflkajsdflkjasdf.com/">here</a>.</p>
</body></html>