Look not at what a man says, but at what he does

preface

Hello, everyone, I’m Qi 89. Today we are going to continue to talk about network communications. In previous articles

  1. Network communication generates HTTP messages
  2. IP address for network communication

As we mentioned, to communicate with the server, the client (usually a browser) needs to encapsulate the data information. The format is as follows.

Although the browser can parse web addresses and generate HTTP messages, it does not have the function of sending messages to the network. The client will entrust the operating system, call the corresponding protocol stack, and encapsulate data. And then send it.

And then we introduced some concepts of IP addresses. Thus, the IP address is the communication address of a network card in the network world, which is equivalent to our real world house number. That is to say, we can travel unbridled in the network, need to rely on IP address.

In real life, however, we don’t access a resource through some arcane IP address. Instead, access it by domain name. For example, when we query a key through Baidu, we do not use baidu’s long list of IP addresses, but directly enter www.baidu.com in the browser for information retrieval.

So, today we will talk about the network world, is how to achieve this process.

It’s getting late. Get down to business.

concise

  1. Querying an IP address using the DNS is called domain name resolution
  2. A Socket library is a collection of program components used to invoke network functions
  3. When querying an IP address based on a domain name, the browser uses a parser in the Socket library
  4. The client query message contains the followingThree kinds of information

    1. The domain name

    2. Class(IN)

    3. Record type
  5. The DNS server searches for the corresponding record in the mapping table between the domain name and IP address and returns the IP address
  6. In a domain name, the position to the right indicates a higher level
  7. On the Internet,comcnThere is also a first-order domain above, calledThe root domain
  8. The root domainIn the DNS servercom,cnInformation about the DNS server
  9. DNS server is a tree hierarchy from top to bottom for 1. Root DNS server 2. Top-level DOMAIN DNS server 3. Authoritative DNS server

1. Using DNS to query an IP address is called domain name resolution 2. The query message on the client contains the following types of information: Domain name /Class/ record type 3. The DNS server searches for the corresponding record in the mapping table between the domain name and IP address and returns the IP address 4. The DNS server of the root domain stores information about DNS servers such as COM and CN

The article summary

  1. The Socket library provides the function of querying IP addresses
  2. The parser issues queries to the DNS server
  3. This section describes how to work the DNS server
  4. Hierarchy of domain names
  5. Find the CORRESPONDING DNS server and obtain the IP address
  6. Load balancing

1. The Socket library provides the function of querying IP addresses

To query the IP address, simply ask the nearest DNS server, “What is the IP address of www. wl.com?” the DNS server will reply, “The IP address of this server is XXX.XXX.xxx.xxx.”

To send a query message to the DNS server, the DNS server sends a query message and receives the response message. For a DNS server, there must be a CORRESPONDING DNS client on our computer, and the part of the DNS client is called the DNS parser, or simply the parser.

Querying an IP address using the DNS is called domain name resolution

A parser is actually a program that is contained in the operating system’s Socket library. A library is a collection of generic program components. A Socket library is also a library that contains program components that allow other applications to call the network functions of the operating system, and the parser is one of the program components in this library.

A Socket library is a collection of program components used to invoke network functions


2. The parser sends a query to the DNS server

After the parser is invoked, the parser sends a query message to the DNS server, which then returns a response message. The response message contains the queried IP address, which the parser retrieves and writes to the memory address specified by the browser.

Next, when the browser sends a message to the Web server, it simply fetchs the IP address from that memory address and hands it to the operating system along with the HTTP request message.

When querying an IP address based on a domain name, the browser uses a parser in the Socket library

When the browser calls the parser, the control flow of the program moves inside the parser. When the part of the program that calls the parser is reached, the corresponding one-line program is executed, and the browser’s own work is paused (①, this step is obstructive). The parser in the Socket library then runs (②) to complete the application delegate operation. The originally running program enters the pause state, and the called program begins to run.

The parser then generates query messages to be sent to the DNS server. This process is similar to how a browser generates an HTTP request message to send to a Web server. According to the DNS specification, the parser generates a piece of data saying “Please tell me the IP address of XXXX” and sends it to the DNS server (③). Sending messages is not performed by the parser itself, but is delegated to the protocol stack within the operating system. The parser itself does not have the ability to send and receive data over the network.

The protocol stack performs the operation of sending the message, and then sends the message to the DNS server through the nic (④⑤). When the DNS server receives the query message, it queries the DNS server based on the query content in the message. Then, in line with the steps taken to send the message, the returned message follows the original route. (6) 7) end pet-name ruby).

Finally, the parser writes the fetched IP address to a memory address specified by the application.


3. Procedure of the DNS server

The basic job of a DNS server is to receive a query message from a client and return a response based on the message content.

The query message from the client contains three types of information.

type describe
The domain name Name of the server, mail server (the part after @ in the mail address)
Class The value of the ClassforeverIt stands for the InternetIN
Record type Represents domain name correspondenceWhat typeThe record of



A type ofA“: indicates that the domain name corresponds toThe IP address

A type ofMX“: indicates that the domain name corresponds to the mail server

A stands for Address /MX: Mail eXchange

The RECORD data corresponding to the preceding three types of information is stored on the DNS server.

For example, to query the IP address corresponding to the domain name www.wl.com, the client sends a query message to the DNS server containing the following information:

information
Domain name =www.wl.com
Class = IN
Record type = A

The DNS server then looks for the existing records that all match the domain name, Class, and record type.

When the record type is MX, the DNS server saves two types of information in the record, namely, the domain name and priority of the mail server.

The DNS server searches for the corresponding record in the mapping table between the domain name and IP address and returns the IP address

There are actually many other types.

  • By IP addressThe check of the domain namethePTRtype
  • Query domain name InformationThe aliastheCNAMEtype
  • Query the IP address of the DNS serverNStype
  • And the queryDomain name Attribute InformationtheSOAType etc.

4. Domain name hierarchy

There are numerous servers on the Internet. It is impossible to store all the information of these servers in one DNS server. Therefore, the DNS server is bound to fail to find the information to be queried.

The information needs to be distributed among multiple DNS servers, and these DNS servers relay with each other to find the information to be queried.

How is the information registered on the DNS server

First, all information in a DNS server is stored in a hierarchical structure by domain name. Domain names in DNS are separated by periods, such as www.wl.com, which represent boundaries between different layers.

In a domain name, the position to the right indicates a higher level. For example www.wl.com this domain name if according to the organization structure of the company, is probably “WWW of WL department of COM enterprise group” so. The part that corresponds to a level is called a domain. Thus, the next layer of the COM domain is the WL domain, and just below that is the WWW name.

This hierarchical domain name information is registered with the DNS server, and each domain is handled as a whole. In other words, the information of a domain is stored on the DNS server as a whole, and a domain cannot be separated into multiple DNS servers. (However, the relationship between DNS servers and domains is not always one-to-one, and one DNS server can hold information about multiple domains.)

Therefore, DNS servers also have a hierarchical structure like domain names, and the information of each domain is stored in the CORRESPONDING DNS server.


5. Search for the DNS server and obtain its IP address

The key here is how to find out which DNS server manages the information about the Web server we want to access.

First, the IP addresses of the DNS servers that manage the lower-level domains are registered with their upper-level DNS servers, and then the IP addresses of the upper-level DNS servers are registered with the higher-level DNS servers, and so on.

For example, the IP address of the DNS server that manages the domain bcnz.wl.com must be registered with the DNS server of the domain Wl.com, and the IP address of the DNS server of the domain wl.com must be registered with the DNS server of the domain COM. In this way, we can query the IP address of the lower-level DNS server through the upper-level DNS server and send query requests to the lower-level DNS server.

The com and CN domains (called top-level domains) are responsible for saving information about lower-level DNS servers. In the Internet, com and CN have one level of domain above them, called the root domain. The root domain does not have its own name like com and cn. Therefore, it is often omitted when writing domain names. To specify the root domain, use the following example: www.wl.com. This adds a period to the end of the domain name, and the last period represents the root domain.

The DNS server of the root domain stores information about DNS servers such as COM and CN

There is one more thing that needs to be done, which is to save the root domain DNS server information to all DNS servers on the Internet. If the client can find any DNS server, it can use it to find the root DNS server, and then locate a target DNS server in the lower layer.

There are only 13 IP addresses assigned to the root domain DNS server in the world, and these addresses rarely change.

DNS Resolution Process

  1. The computer client issues a DNS request asking what is the IP address of www.wl.com and sends it to the local DNS server. If configured using DHCP, the local DNS is automatically assigned by your Internet service provider (ISP), such as Telecom, mobile, etc. It is usually located in one of your ISP’s facilities.

  2. The local DNS receives a request from the client. Procedure Then, find the corresponding record information. If www.wl.com can be found, it simply returns the IP address. If not, the local DNS will ask its root DNS server. The root DNS server is the highest level, with 13 sets worldwide. It is not directly used for domain name resolution, but can point the way.

  3. The root DNS receives a request from the local DNS, finds the suffix is.com, and says: “www.wl.com, this domain name is managed by the.com zone, I’ll give you the address of its top-level domain name server, you can ask it.”

  4. The local DNS turns to the top-level domain server and asks, “Can you tell me the IP address of www.wl.com?” The top-level domain name server (TLDS) is the well-known tier 1 domain such as.com,.net and.org. It manages tier 2 domain names such as wl.com, so it provides a clearer direction.

  5. The TOP-LEVEL DNS server says, “I’ll give you the address of the authoritative DNS server responsible for the www.wl.com area, and you should be able to ask it.”

  6. The local DNS turns to the authoritative DNS server and asks, “What is the IP address of www.wl.com?” The authoritative DNS server of wl.com, which is the source of domain name resolution results. Why authority? Is my domain name I call the shots.

  7. The authoritative DNS server displays the corresponding IP address X.X.X.X to the local DNS server.

  8. The local DNS returns the IP address to the client, and the client establishes a connection with the target.

Caching speeds up DNS server responses

Sometimes you don’t need to start at the highest level of the root domain, because the DNS server has a caching function to remember previously queried domain names.

If the domain name and related information to be queried is already in the cache, the response can be returned directly, and subsequent queries can proceed down from the cached location. Caching can reduce the time it takes to query rather than looking up from the root domain every time.

One thing to note about this caching mechanism is that after the information is cached, the original registration information may change, and the information in the cache may be incorrect. Therefore, the information stored in the DNS server has a validity period. When the validity period expires, the data is deleted from the cache.


6. Load balancing

From the client’s point of view, this is a DNS recursive query process. Because the local DNS does everything for it, it just sits back and waits for the results. In this process, DNS can do another thing besides mapping names to IP addresses, which is load balancing.

DNS can perform internal load balancing first.

For example, if an application wants to access another application, if the IP address of the other application is configured, the access is one-to-one. But we can actually deploy multiple applications when the applications being accessed fail. But how does the application accessing it load balance among multiple applications? Just configure it as a domain name. During domain name resolution, you only need to configure a policy to return the first IP address this time and the second IP address next time to achieve load balancing.

DNS can also perform global load balancing.

To ensure high availability, our applications are often deployed in multiple computer rooms, each with its own IP address. When a user accesses a domain name, the IP address can poll to access multiple data centers. If a DATA center is down for some reason, you only need to delete the IP address of the data center from the DNS server to achieve high availability.

  1. When a client accesses object.wl.com, it needs to convert the domain name to an IP address for access, so it requests the local DNS resolver. (Step 1)

  2. The local DNS resolver first looks to see if the local cache has the record. If there is one, use it directly, because the above procedure is too complicated, and if you have to recurse every time, it will be too much trouble. (step 2)

  3. If there is no local cache, request the local DNS server. (Step 3)

  4. The local DNS server is usually deployed in your data center or your carrier’s network. The local DNS server also needs to check whether there is a local cache and return if there is, because it does not want to go through the above recursive process again. (step 4)

  5. 5 to 7. If no, the local DNS recursively searches for the top-level domain name server of.com from the root DNS server to the authoritative DNS server of wl.com. The authoritative DNS server returns the actual IP address to be accessed. (Step 5-7) For simple applications that do not need to perform global load balancing, the authoritative DNS server of Wl.com can directly resolve the domain name object.wl.com to one or more IP addresses, and then the client can use multiple IP addresses to perform simple polling. Simple Load balancing but complex applications, especially large-scale applications across regions and carriers, require a more complex Global Load balancing mechanism. Therefore, a dedicated device or Server for Global Server Load Balance (GSLB) is required to perform this task.

  6. The first-level GSLB knows the carrier of the user by looking at the carrier of the local DNS server that requested it. The local DNS server is told to request layer 2 GSLB via another alias, object.yd.wl.com, via CNAME, assuming the move

  7. The layer-2 GSLB knows the user’s geographical location by viewing the address of the local DNS Server that requests it. The GSLB then identifies the six Server Load Balancer (SLB) addresses in the Region closest to the user’s location. Return to the local DNS server


Afterword.

Sharing is an attitude. This article, mainly from How the Internet Is Connected, can be regarded as a record and summary of a self-learning process. Basically, it’s a list of what you think is important. At the same time, but also for everyone to save the time of mine clearance and trample pits. Of course, some points may not be well expressed due to the limitations of their own cognitive ability. If you want to see the original, “wall crack recommended” to see the original.

References:

  1. Interesting talk about network protocols
  2. How is the network connected

See all see here, that bother, move a little hand, a key three even wow