This article is reprinted from the WeChat public account "Xiaocai Learns Programming", written by fasionchan. Please contact Xiaocai Learns Programming public account for reprinting this article. Since domain names are easier to remember than IP addresses, we usually use them to access network services. If a web application client wants to communicate with the server, it must first query the DNS server for the IP address corresponding to the domain name. For example, when a reader visits my website fasionchan.com, the browser needs to first query the website's IP address based on the domain name, and then communicate with the website's web server. So, how to implement domain name query through programming? This is an issue that cannot be avoided when developing network applications. We know that DNS servers and clients communicate using the DNS protocol: the client first sends a request message to the server, and the server encapsulates the query result into a response message and replies to the client. DNS can use UDP or TCP as the transport layer protocol, and the communication port number is 53. Assuming the client uses the UDP protocol, the steps for a domain name query are as follows:
If every network application needs to encapsulate DNS messages to implement domain name query, it would be too troublesome! For this purpose, the C library provides a series of tool functions. The application only needs to call these tool functions to complete the domain name query without operating the socket or encapsulating the DNS message. Sample ProgramThis program calls the C library function gethostbyname to query the domain name specified by the user in the command line parameter:
As the name implies, gethostbyname queries the address of the host based on the domain name, and the result is usually an IP address or IPv6 address. Please look at line 14 of the program, where the gethostbyname function is called with the domain name to be queried as a parameter; it returns a pointer to a hostent structure, which stores the domain name query result. Lines 15-33 check the domain name resolution result. A blank value indicates an error. If an error occurs, the error is handled according to the value of h_errno (see below for details). Lines 35-39 retrieve the query results from the hostent structure and print them to the screen. So, what does the gethostbyname library function do internally? The answer is not hard to guess. It helps us create a UDP socket, send a DNS request message, and receive and parse the reply message. Taking this program as an example, its execution flow (blue line) is roughly as follows: Domain name query library functionIn fact, the C library provides a series of utility functions for domain name query:
Take gethostbyname as an example. If the query is successful, it will return a hostent structure pointer, which stores the query result. If the query fails, it will return NULL and save the error in the h_errno global variable. Generally speaking, domain name query errors can be divided into the following situations: HOST_NOT_FOUND, indicating that the specified host does not exist, that is, the domain name does not exist; NO_DATA, indicating that there are other records for the domain name, but no address-related records (A or AAAA); NO_RECOVERY, an unrecoverable error occurred in the domain name server; TRY_AGAIN, a temporary error that can be recovered by retrying; When the domain name query fails, the caller must check the h_errno variable and handle it accordingly. limitationIn application scenarios such as web crawlers and Socks5 proxies, domain name queries are very frequent. At this time, directly using the gethostbyname series of library functions is likely to encounter performance bottlenecks. On the one hand, the gethostbyname library function creates a UDP socket to communicate with the DNS server every time it queries a domain name. This means that frequent domain name queries are inevitably accompanied by the creation and destruction of a large number of sockets, and the overhead can be imagined! On the other hand, the gethostbyname library function will block until the DNS server returns a result or the query times out, which will seriously restrict the concurrent processing capability of the system. Therefore, in high-frequency query scenarios, you cannot directly use library functions such as gethostbyname, and you must use some optimized asynchronous domain name resolution libraries. Further readinggethostbyname |
>>: 5G rumors are spreading, and this time it is India that is hurt
[[254871]] In today's mobile Internet era, mo...
5G is still on the way, and telecom operators are...
According to foreign media reports, the telecommu...
[[263546]] 5G has received great attention since ...
The development of 5G has now become another hot ...
First, we need to know why we need to evolve from...
1. Development History: 2. Wireless network class...
NetOps, also known as NetDevOps, is the practice ...
Author: Lu Yao Proofread by Yun Zhao Not long ago...
TMThosting has launched a 2021 Summer Sale event,...
The Internet is everywhere and deeply affects our...
Recently, Beijing Mobile officially launched the ...
When it comes to comparing SD-WAN vs. VPN service...
80VPS is an early-established Chinese hosting com...
[[286782]] As a new generation of mobile communic...