Do you really understand the DNS protocol that is used every day at work? (There is a surprise at the end of the article, don't miss it)

♥ Foreword

When we often visit some websites, enter an address similar to www.baidu.com in the browser, then enter this address in the browser ---> Baidu server will return us this Baidu page, what is the process in the middle What kind?

With this question in mind, let's analyze the process together.

1. The relationship between domain name and IP

First of all, addresses like 'www.baidu.com' and 'www.sina.com.cn' are collectively called domain names. Then, after using the domain name to access, can the request reach the corresponding server directly?

no!

In this process, the domain name needs to be converted into an IP address. Only by searching through the IP address can the location of the server be found and the server receive the request.

Why do you need to perform this conversion operation between domain names and IPs? Let's answer two questions:

  1. So why don't we communicate directly with IP addresses?

    Because IP addresses are not convenient for users to remember, domain names are more convenient for users to remember and use. For example, www.baidu.com is the domain name of Baidu; www.sina.com.cn is the domain name of Sina, which is very easy to understand and remember;

  2. Then why not just use the domain name to find the server?

This is because the length of the domain name is not fixed, which is not convenient for computers to process; and the IP address is of fixed length, if it is an IPv4 address, it is 32 bits, and if it is an IPv6 address, it is 128 bits, so it is easier for computers to process it with a fixed length.

So, to sum up, the IP address is for the host, and the domain name is for the user. So we need to convert the domain name and IP address to each other.

2. Convert domain name to IP address

hosts file

At the beginning, people used the hosts file of the computer to convert the domain name and IP.

There is a host file under the path of our computer C:\Windows\System32\drivers\etc, which can save the corresponding relationship between domain name and IP, as shown in the following figure:

picture

If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the first interface automation testing tutorial on the entire network at station B. At the same time, the number of online users has reached 1,000, and there are notes to collect and various Lu Dashen Technical Exchange: 798478386      

 [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (the latest version of actual combat)_哔哩哔哩_bilibili [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (actual combat) The latest version) has a total of 200 videos, including: 1. [Interface Automation] The current market situation of software testing and the ability standards of testers. , 2. [Interface Automation] Fully skilled in the Requests library and the underlying method call logic, 3. [Interface Automation] interface automation combat and the application of regular expressions and JsonPath extractors, etc. For more exciting videos, please pay attention to the UP account. https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337&vd_source=488d25e59e6c5b111f7a1a1a16ecbe9a

The line framed in the figure maps the IP address 120.24.89.47 to the domain name www.tricy1.com. After the configuration is complete, even if the domain name www.tricy1.com is not a real registered domain name, you can still access the domain name www.tricy1.com on this computer to access the website corresponding to this IP address (lemon class test Pie Forum).

Initially, the hosts file is managed through the Internet Information Center. If a new computer wants to connect to the network, or a computer IP changes, it is necessary to apply for changing the hosts file at the Information Center. Other computers also need regular updates to be able to go online. But this is too much trouble. For easier operation, the DNS protocol appeared.

DNS protocol

DNS protocol, the full name of Domain Name System, is an application layer protocol, which is based on UDP or TCP protocol, and uses port 53 by default.

The client communicates through the UDP protocol by default, but because the wide area network is not suitable for transmitting large UDP packets, it is stipulated that when the packet length exceeds 512 bytes, it should be converted to use the TCP protocol for data transmission. Therefore, the DNS protocol is a rare application layer protocol that can use both the UDP protocol and the TCP protocol as the underlying protocol.

The function of this protocol is to convert human-readable domain names (eg, www.qq.com) into machine-readable IP addresses (eg, 119.147.15.13).

picture

Domain name hierarchy:

As shown in the figure above, the middle of the domain name is separated by dots. What does each dot represent?

We need to understand that the domain name is a hierarchical structure, and the domain name server is also a corresponding hierarchical structure, which are the root domain name, the top-level domain name system, the second-level domain name system, and the third-level domain name system, as shown in the following figure:

picture

 There are 13 root servers in the world. The Chinese names of these 13 root domain name servers are "A" to "M". Among them, 10 are set in the United States, and the other three are respectively set in the United Kingdom, Sweden, and Japan. One primary root server is placed in the United States, and the remaining 12 are secondary root servers.

For example, the hierarchical structure of the domain name www.baidu.com is as follows:

com: top-level domain name. Indicates that this is a corporate domain name.

baidu: Second-level domain name, refers to the company name.

www: Internet protocol (World Wide Web)

Domain names generally cannot exceed 5 levels, and the levels of domains become higher from left to right, and higher-level domains contain lower-level domains. The domain name is unique in the entire Internet. When the high-level sub-domain is the same, the low-level sub-domain is not allowed to be repeated.

With the domain name structure, there needs to be something to resolve the domain name, and that is the domain name server.

Domain names need to be resolved by domain name servers all over the world. Domain name servers are actually hosts with domain name systems installed.

DNS resolution process

picture

 As shown above, let's analyze the process of DNS resolution:

The user wants to access the Baidu server through the client browser - the domain name is www.baidu.com:

  1. When the user enters the www.baidu.com domain name in the browser, the default priority is to search the browser cache to see if the cache contains the IP address corresponding to the domain name

  2. When it cannot be found in the browser, it will check whether there is a corresponding IP address in the Hosts file in the system

  3. If there is no mapping of this domain name in hosts, look for the local DNS server;

  4. If not, search the root domain name server through the DNS server; the root domain name service returns the IP address of the top-level domain name server available for query;

  5. The top-level domain name server returns a DNS response message;

  6. After the host receives the response message, it can access the server normally; and save the result for the next use


The query methods of the DNS protocol are divided into the following two types:


Recursive query : The machine sends a query request to the local domain name server, and waits for the final result. If the local domain name server cannot resolve it, I will query other domain name servers as a DNS client until I get the final IP address and tell this machine

picture

Iterative query : the local domain name server queries the root domain name server, the root domain name server tells it where to query next, and then it checks again, each time it queries each server as a client.

picture

In layman's terms, recursion is to hand over one thing to others. If the thing is not finished, even if it has been done a lot, don't tell me the result. What I want is your final result, not the intermediate result; if you don't After finishing it, please find someone else to finish it.

Iteration is one thing I give you, tell me how much you can do, and then I will do the rest.

Guess you like

Origin blog.csdn.net/caixiangting/article/details/132212661