Linux gethostbyname 有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6677077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:05:09  来源:igfitidea点击:

What's wrong with gethostbyname?

clinuxgethostbyname

提问by Ottavio Campana

I am using this snippet of code I found in http://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/to perform dns lookups

我正在使用我在http://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/ 中找到的这段代码来执行 dns 查找

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

int main(int argc, char *argv[ ]) {
    struct hostent *h;

    /* error check the command line */
    if(argc != 2) {
        fprintf(stderr, "Usage: %s hostname\n", argv[0]);
        exit(1);
    }

    /* get the host info */
    if((h=gethostbyname(argv[1])) == NULL) {
        herror("gethostbyname(): ");
        exit(1);
    }
    else     
        printf("Hostname: %s\n", h->h_name);

    printf("IP Address: %s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));     
    return 0;
}

I am facing a weird fact

我正面临一个奇怪的事实

./test www.google.com
Hostname: www.l.google.com
IP Address: 209.85.148.103

works fine, but if I try to resolve an incomplete IP address I get this

工作正常,但如果我尝试解析不完整的 IP 地址,我会得到这个

./test 10.1.1
Hostname: 10.1.1
IP Address: 10.1.0.1

I would expect an error like the following

我预计会出现如下错误

./test www.google
gethostbyname(): : Unknown host

but the program seems to work.

但该程序似乎有效。

Any idea why?

知道为什么吗?

采纳答案by Igor Korkhov

It is not a bug but rather a feature of inet_aton() function:

这不是 bug,而是 inet_aton() 函数的一个特性:

DESCRIPTION

The inet_aton() function converts the specified string, in the Internet standard dot notation, to a network address, and stores the address in the structure provided.

Values specified using dot notation take one of the following forms:

a.b.c.dWhen four parts are specified, each is interpreted as a byte of data and assigned, from left to right, to the four bytes of an internet address.

a.b.cWhen a three-part address is specified, the last part is interpreted as a 16-bit quantity and placed in the rightmost two bytes of the network address. This makes the three-part address format convenient for specifying Class B network addresses as 128.net.host.

描述

inet_aton() 函数将使用 Internet 标准点表示法的指定字符串转换为网络地址,并将地址存储在提供的结构中。

使用点表示法指定的值采用以下形式之一:

abcd当指定了四个部分时,每个部分都被解释为一个数据字节,并从左到右分配给 Internet 地址的四个字节。

abc当指定了三部分地址时,最后一部分被解释为一个 16 位的数量并放置在网络地址最右边的两个字节中。这使得三部分地址格式便于将 B 类网络地址指定为 128.net.host。

You can read more about this there, for example.

例如,您可以在那里阅读有关此内容的更多信息。

回答by Sander De Dycker

POSIX.2004says :

POSIX.2004说:

The name argument of gethostbyname() shall be a node name; the behavior of gethostbyname() when passed a numeric address string is unspecified. For IPv4, a numeric address string shall be in the dotted-decimal notation described in inet_addr().

gethostbyname() 的 name 参数应为节点名称;未指定传递数字地址字符串时 gethostbyname() 的行为。对于 IPv4,数字地址字符串应采用 inet_addr() 中描述的点分十进制表示法。

So, when looking at it from the POSIX point of view, you cannot expect anything when passing it an IP address.

因此,当从 POSIX 的角度来看它时,向它传递 IP 地址时您不能指望任何东西。

On my system, the man pagesays this :

在我的系统上,手册页是这样说的:

If name is an IPv4 or IPv6 address, no lookup is performed and gethostbyname() simply copies name into the h_name field and its struct in_addr equivalent into the h_addr_list[0] field of the returned hostent structure.

如果 name 是 IPv4 或 IPv6 地址,则不执行查找,gethostbyname() 只是将 name 复制到 h_name 字段中,并将其 struct in_addr 等效项复制到返回的 hostent 结构的 h_addr_list[0] 字段中。

It does not say anything about what happens if you pass it an incomplete IP address, so anything could happen, including the behavior you observed.

它没有说明如果您向它传递一个不完整的 IP 地址会发生什么,因此任何事情都可能发生,包括您观察到的行为。

For more information on how gethostbynameis implemented on your system, you can check the documentation for the function and/or the source code (if available).

有关如何gethostbyname在您的系统上实现的更多信息,您可以查看函数和/或源代码(如果可用)的文档。