C语言 getaddrinfo() 如何进行 DNS 查找?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2157592/
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
How does getaddrinfo() do DNS lookup?
提问by n00b2000
getaddrinfo() is a function we need to use beforecreating a socket() or connect()ing, right? Then how does getaddrinfo communicate with DNS server in the first place?
getaddrinfo() 是我们在创建 socket() 或 connect()之前需要使用的函数,对吗?那么 getaddrinfo 是如何与 DNS 服务器通信的呢?
PS: Where can I see the complete source of getaddrinfo?
PS:哪里可以看到getaddrinfo的完整源码?
回答by mark4o
It is not necessary to call getaddrinfo()before creating a socket or connecting. It is used to translate a domain name, like stackoverflow.com, to an IP address like 69.59.196.211. If you know the IP address then you can connect directly to that address and there is no need to use getaddrinfo(). getaddrinfo()uses the DNS protocol to talk to your name servers, which are configured using their IP address.
getaddrinfo()在创建套接字或连接之前不需要调用。它用于将域名(如stackoverflow.com)转换为 IP 地址(如69.59.196.211 )。如果您知道 IP 地址,那么您可以直接连接到该地址,而无需使用getaddrinfo(). getaddrinfo()使用 DNS 协议与您的名称服务器通信,这些服务器使用其 IP 地址进行配置。
The glibc source code is here.
glibc 源代码在这里。
回答by Tyler McHenry
The short answer is "it asks the system", which in turn knows how to do DNS lookups and which servers to use.
简短的回答是“它询问系统”,后者又知道如何进行 DNS 查找以及使用哪些服务器。
getaddrinfo()is documented by the getaddrinfo(3)manual page, which means it's a C library function. It is also a POSIXfunction, so there is no canonical "source"; each standard C library of an operating system that conforms to POSIX will implement its own version. Either way the source to just that function is probably not too enlightening, since it would just call other functions and OS APIs, and you'd have to drill down pretty far to get to the actual DNS mechanism. You'd be better off reading documentation of the DNS protocol itself if you're interested in how that works.
getaddrinfo()由getaddrinfo(3)手册页记录,这意味着它是一个 C 库函数。它也是一个POSIX函数,所以没有规范的“来源”;符合 POSIX 的操作系统的每个标准 C 库都将实现自己的版本。无论哪种方式,该函数的来源可能都不太具有启发性,因为它只会调用其他函数和操作系统 API,并且您必须深入研究才能获得实际的 DNS 机制。如果您对 DNS 协议的工作原理感兴趣,最好阅读 DNS 协议本身的文档。
回答by Steve Emmerson
Does your Unix system have the file /etc/nsswitch.conf? If so, then the "hosts" entry gives the search order for resolving hostnames into IP addresses. Does your system have the file /etc/resolv.conf? If so, then it specifies what DNS servers to use.
你的 Unix 系统有 /etc/nsswitch.conf 文件吗?如果是,则“hosts”条目给出将主机名解析为 IP 地址的搜索顺序。你的系统有/etc/resolv.conf这个文件吗?如果是,则它指定要使用的 DNS 服务器。
As you can see, getaddrinfo() can do quite a bit (and can take a while)!
如您所见,getaddrinfo() 可以做很多事情(并且可能需要一段时间)!
回答by jstedfast
getaddrinfo()likely does make a connect()call behind the scenes, however it already knows the IP address of the DNS server it needs to connect to in order to query for the address of the host you are asking it to query for.
getaddrinfo()可能确实会connect()在幕后进行调用,但是它已经知道它需要连接的 DNS 服务器的 IP 地址,以便查询您要求它查询的主机的地址。
getaddrinfo()is only needed if you want to map "www.somehost.com" to an IP address, it's not needed as a primer to call connect().
getaddrinfo()仅当您想将“ www.somehost.com”映射到 IP 地址时才需要,它不需要作为调用的入门connect()。
You can probably find the complete source code for getaddrinfo()in glibc sources which you'd be able to find here (among other places).
您可能可以getaddrinfo()在 glibc 源中找到完整的源代码,您可以在此处(以及其他地方)找到这些源代码。
Hope that clarifies things for you.
希望能为你澄清事情。
回答by Zepplock
It is using DNS protocol (UDP) http://www.freesoft.org/CIE/Topics/77.htm
它使用 DNS 协议 (UDP) http://www.freesoft.org/CIE/Topics/77.htm

