🔊 This article is available at ⭐ cs-wiki (Gitee Recommended project, 0.9K Star). Welcome to star ~ 😊


The foreword 0.

In order to ensure the normal access to the URL, the domain name resolution Protocol (DNS) in fact behind a lot of efforts, this article will thoroughly explain the PRINCIPLE of DNS protocol, understand the address we contact every day in the end how to work.

1. What is DNS protocol

Before learning the DNS protocol, let’s distinguish between domain names and IP addresses:

  • IP address: a long number that uniquely identifies a computer on a network
  • The domain nameNetwork domain, also known as domain, is a string of dot separated names on the Internet a computer or computer group name, used to identify the location of the computer during data transmission (sometimes refers to the geographical location) for examplewww.baidu.com

I don’t know if there are any students to confuse the concept of domain name and url, can be understood in this way, the url contains the domain name. For example: www.gitee.com/veal98 is a web address and www.gitee.com is a domain name

Because THE IP address is not easy to remember and can not display the Name and nature of the address organization, people designed the Domain Name, and through the Domain Name resolution protocol (DNS, Domain Name System) to map the Domain Name and IP address to each other, so that people can more easily access the Internet. Instead of having to remember the number of IP addresses that can be read directly by the machine. Forward lookup is the process of mapping a domain name to an IP address, and reverse lookup is the process of mapping an IP address to a domain name.

💡 DNS can be transmitted using UDP or TCP, and the port number is 53. But in most cases DNS uses UDP for transmission.

2. Domain name description

❓ Then who will regulate and manage the domain name? It can’t just be written, can it?

The Internet Corporation for Assigned Names and Numbers (ICANN) is an organization based in California. ICANN manages the operation of the world’s domain name system.

A domain name has a hierarchical structure. From top to bottom, the domain name is the root domain name, the top level domain name (TLD), the second level domain name, and the third level domain name.

① Top-level domain name

Let’s start with top-level domains (TLD), the highest level of domain name. Simply put, it’s the last part of the url. For example, the top-level domain name for the website www.baidu.com is.com. One of ICANN’s main tasks is to determine which strings can be considered top-level domains. As of July 2015, there were 1,058 top-level domains, which can be roughly divided into two categories:

  • One kind isGeneric top-level domain(gTLD) for example.com,.net,.edu,.org,.xxxWait, there are 700 of them.
  • Another kind isNational top-level domain(ccTLD), for different countries and regions, such as.cn(China),.io(British Indian Ocean Territory),.cc(Cocos Islands).tv(Tuvalu) and more than 300.

Of course, ICANN itself will not manage these top-level domains, because there is no way to manage them. If you think about it, there are more than 1,000 top-level domains, and there are many wholesalers under each top-level domain, it would be too much trouble to manage every one of them. ICANN’s policy is to find a host for each top-level domain name, and the host is responsible for all matters related to that domain name. ICANN is only in contact with the host, which makes it easier to manage. For example, the China Internet Network Information Center (CNNIC) is the host of the.cn national top-level domain, which determines the policies for the.cn domain.

② Second-level domain name

Second Level domains (SLD) have a different meaning below generic top-level domains or national top-level domains:

  • Secondary domain name under GTLDS: Generally refers to the online name that the domain name registrant chooses to use, such asyahoo.comBusiness organizations often use their own trademarks, trade names or other business logos as their online names, e.gbaidu.com)
  • Second level domain under the national top-level domain: Generally refers to similar to the generic top-level domain name to indicate the registrant class and function of the mark. For example, in the.com.cnIn the domain name structure,.comThis time is placed in the country’s top-level domain name.cnUnder the second level domain, denotes Chinese commercial organizations, and so on.

A third-level domain name is a domain name in the form of www.baidu.com. It can be regarded as a subdomain name of a second-level domain name. The domain name contains two. For the owner/user of the domain name, the third level domain name is an adjunct to the second level domain name without a separate fee. A level-3 domain name cannot even be called a domain name, but is generally called a level-2 directory under a domain name.

(3) the root domain name

❓ So where is the root domain? Isn’t the root domain at the top level in the hierarchy? Why is it not seen in the domain name?

Because ICANN manages all top-level domains, it is the highest level of domain name node, known as the root domain. In some cases, www.xxx.com is written www.xxx.com. That is, there is an extra point at the end. This point is the root domain name.

In theory, all domain name queries must first look at the root domain, because only the root domain can tell you which server is managing a top-level domain. Indeed, ICANN maintains a list (the root domain list) of top-level domains and their hosts.

For example, if I want to access abc.xyz, I also have to go to the root domain list, which tells me that the.xyz domain is hosted by CentralNic. The root list also states that.Google is hosted by Google,.Apple is hosted by Apple, and so on.

Since the root domain name list rarely changes and most DNS services provide a cache for it, the root domain name is actually not looked up that often.

3. Details of the DNS server

A DOMAIN name server (DNS) is a host and software that manages domain names. It can manage information related to the domain in which it resides. The layers that a DNS server is responsible for are called zones. Each layer of the domain has a DNS server:

  • Root DNS server
  • Top-level Domain name servers
  • Permission Domain name Server

Here’s a picture that makes sense:

In addition to the above three DNS servers, there is another important DNS server that is not in the DNS hierarchy: the local domain name server. What are these four servers used for? 👇

① Root DNS server

As mentioned above, ICANN maintains a list of root domain names that contain top-level domain names and their hosts. The official name of the root domain name list is the DNS root zone. The server that stores the DNS root zone files. It is called the ROOT name server. The root DNS server holds the addresses of all top-level DNS servers

The early DNS query result was a 512-byte UDP packet. This package can hold up to 13 server addresses, so there are 13 root DNS servers worldwide, numbered from a.root-servers.net to m.root-servers.net. Ten are in the United States, with one each in the Netherlands, Sweden and Japan.

As mentioned earlier, all domain name queries must theoretically look up the root domain name first, so generally all DNS servers register a cache of the root DNS IP address for sending requests to it if necessary.

② Top-level DOMAIN name servers

In the same way that a root DNS server manages top-level domains, a TOP-LEVEL DNS server is clearly used to manage all secondary domain names registered under that top-level domain, recording the IP addresses of those secondary domain names.

③ Permission Domain name server

According to the above logic, permission should be registered domain name server in the secondary domain name under all three/four domain name, but it is not the case, if a secondary domain name or a three/four domain name corresponds to a domain name server, the domain name server will be a lot of number, we need to use the ways to solve this problem. A domain name server is the domain server responsible for managing a “zone”.

❓ What is an area? How do you divide it?

Regions and domains are actually different, and regions can be divided in many different ways. Taking Baidu as an example, we assume that there are three third-level domain names fanyi.baidu.com, ai.baidu.com and tieba.baidu.com. We can partition fanyi.baidu.com and tieba.baidu.com on the baidu.com domain name server, and ai.baidu.com on the ai.baidu.com domain name server. Baidu.com domain name server and AI.baidu.com domain name server have the same permission status, and the specific partition is determined by Baidu according to the number of domain names and visits.

Let me draw a picture to visualize it:

④ Local DNS server

In addition to the above three TYPES of DNS servers, there is another type of DNS server that is not in the DNS hierarchy, but is very important: the local Domain Name Server (also known as authoritative Domain Name server). A local DNS server is the default DNS server used by a computer for DNS resolution. It is the preferred DNS server and alternate DNS server configured on a computer. Common local DNS services include telecom, Intercom, Google, And Alibaba.

Each Internet service provider or a university, or even departments within a university, can have a local DNS server. When a host sends a DNS query request, the query request packet is sent to the local DNS server of the host. The local DNS server manages the resolution and mapping of local domain names and queries the upper-layer DNS server.

How does the local DNS server forward the query request to the upper-layer DNS server?

4. DNS query mode

There are two methods for querying DNS information:

  • Recursive query
  • Iterative query

Recursion is that if the receiver of the request does not know what is requested, the receiver will act as the requester and make the request until it gets what is needed, and then return it to the original requester.

👍 Colloquially, in A recursive query, if A requests B, then B, as the recipient of the request, must give A the desired answer; An iterative query means that if receiver B does not have the exact content that requester A needs, receiver B will tell requester A how to get it, but will not make the request itself.

In general, queries between DNS servers use iterative queries to avoid overloading the root DNS server. 👇 can be well understood by the following two pictures

1) Recursive query:

2) Iterative query:

5. Domain name cache

The above explanation is the DNS query request process between DNS servers, but in fact, every time there are countless Internet users to access the Internet, then every time to access the local DNS server to obtain the IP address is obviously not practical. The solution is to use a cache to store the mapping between domain names and IP addresses.

DNS records are cached locally in two ways: browser cache and operating system cache.

1) Browser cache: After obtaining the actual IP address of the website domain name, the browser will cache it to reduce the loss of network requests. Each browser has a fixed DNS cache time, such as Chrome’s expiration time is 1 minute, during which no DNS requests will be made again

2) Operating system cache: The operating system cache is actually the hosts files configured by the user. For example, the hosts file under Windows10 is stored in C:\Windows\System32\drivers\etc\hosts

On Windows, the DNS cache service is enabled by default. The service name is DNSClient. Some common domain names can be cached.

Run the ipconfig/displaydns command to view the domain names cached on your computer.

If no match is found, the system continues to query the operating system cache. Finally, the local DNS server recursively searches for the domain name records and returns the result. The query between the host and the local DNS server is a recursive query, that is, the host requests the local DNS server, so the local DNS server as the recipient of the request must give the host the desired answer.

6. Complete the domain name resolution process

OK, we mentioned above between DNS server query request process and domain name cache combined, is a complete DNS protocol for domain name resolution process. Here we take forward resolution as an example (domain names are resolved into IP addresses) :

1) First search the DNS cache of the browser and maintain a table corresponding to the domain name and IP address in the cache;

2) If no match is found, the system continues to search the DNS cache of the operating system.

3) If no match is found, the operating system sends the domain name to the local DNS server. The local DNS server queries its DNS cache and returns the result if the search succeeds. (Note: the search between the host and the local DNS server is recursive.)

4) If the DNS cache of the local DNS server does not match the DNS cache of the local DNS server, the local DNS server queries the upper-layer DNS server in the following ways (note: the query between the local DNS server and other DNS servers is iterative to prevent excessive pressure on the root DNS server) :

  • First, the local DNS server sends a request to the root DNS server. The root DNS server is the highest level. It does not directly indicate the IP address of the domain name, but returns the IP address of the top-level DNS server
  • After the local DNS server obtains the IP address of the TOP-LEVEL DNS server, it sends a request to obtain the IP address of the domain name server
  • The local DNS server sends requests to the DNS server based on the IP address of the domain name server and finally obtains the IP address corresponding to the domain name

4) The local DNS server returns the IP address to the operating system and caches the IP address itself

5) The operating system returns the IP address to the browser and caches the IP address itself

6) At this point, the browser gets the IP address corresponding to the domain name and caches the IP address

Intuitive understanding with the following figure:

| flying veal 🎉 pay close attention to the public, get updates immediately

  • The blogger, a first-year graduate student at Southeast University, uses his spare time to run an official account “Flying Veal”, which was first opened on December 29, 2020/12/29. Focus on sharing computer foundation (data structure + algorithm + computer network + database + operating system + Linux), Java foundation and interview guide related original technology good article. The purpose of this public account is to let you can quickly grasp the key knowledge, targeted. I hope you can support me and grow with veal 😃