Why are networks layered?

Why are networks layered? Because, you know, it’s a complicated procedure and it’s all layered.

A good way to understand the concepts in computer networks is to think of a network package as a Buffer, or a chunk of memory, that is formatted. Also, imagine that you are a program that handles network packets, and that the program can run on a computer, a server, a switch, or a router. Imagine that you have multiple network ports, take a network packet from one port, process it with your own program, and send it from another port.

Of course, the network package format is very complicated, and so is the program. ** Complex programs are layered, which is a programming requirement. ** For example, complex e-commerce may have a database layer, a cache layer, a Compose layer, a Controller layer, and an access layer, each focusing on its own layer.

How does the program work?

We can simply imagine how the “you” program works.

When a network packet passes through a network port, you see it, first check to see if you want to invite in and handle it. Some network ports are configured with promiscuous mode.

Once it’s brought in, it goes through a process. You then call process_layer2(buffer). Of course, this is a false function. But you know what it means, you know there must be a function. So what does this function do? From the Buffer, remove the layer of headers and see what should be done based on the contents of the headers.

If you find that the package’s MAC address matches yours, it was sent to you, and process_layer3(buffer) needs to be called. At this point, there is usually no layer 2 header in the Buffer because it was removed during the processing of the previous function, or the starting offset was moved. In this function, remove the three layers of headers to see if they are sent to you, or if you want to forward them.

How do you tell? If the IP address is not your own, you should forward it. If the IP address is your own, it’s sent to you. Process_tcp (buffer) or process_udp(buffer) should be called after removing the three layers of headers from the IP header.

Assuming the address is TCP, process_TCP (buffer) is called. When there is no layer 3 header in the Buffer, you need to look at the layer 4 header to see if it is an initiation, a reply, or a normal packet, which is then processed by different logic. If it is an initiate or reply, a reply packet may be sent next. If it is a normal packet, it needs to be passed to the upper layer. To whom? Is there a process_http(buffer) function?

No, if you’re a network package handler, you don’t need to have process_HTTP (buffer), you should hand it over to the application. To which application? Inside the top four layers are port numbers that different applications listen for. If you find that the browser application is listening to this port, you can send it to the browser. What the browser does with it is none of your business.

The browser, of course, parses the HTML and displays the page. The owner of the computer was very happy to see the page and clicked the mouse. Mouse clicks are captured by the browser. The browser knows it’s about to make another HTTP request and sends it to you using the port number.

You should call send_TCP (buffer). Needless to say, the Buffer contains the content of the HTTP request. This function adds a TCP header that records the source port number. The browser will give you the destination port number, usually port 80.

Send_layer3 (buffer) is then called. Buffers already contain HTTP headers and content, as well as TCP headers. Add an IP header to this function, recording the source IP address and destination IP address.

Send_layer2 (buffer) is then called. Buffers already contain HTTP headers and content, TCP headers, and IP headers. The MAC header is added to this function, and the source MAC address is recorded. The MAC address of this machine and the destination MAC address are obtained. However, this also depends on the current know not to know, know directly add; If you don’t know, you have to go through a certain protocol processing process to find the MAC address. I have to fill one in anyway. I can’t leave it blank.

Everything is ready, and once the contents of the Buffer are complete, it can be sent from the network port, and your job as a program is done.

Uncover the relationship between layers

Now that we know how to do this, let’s go back to the original puzzle.

The first is the layered metaphor. ** All metaphors that do not encapsulate meaning are inappropriate. ** General managers shake hands, do not need employees in the bar, general managers talk about what, do not need employees to participate in it, but the network world is not like this. What should be correct is that when general managers communicate with each other, the manager puts the general manager in his pocket, then the group leader puts the manager in his pocket, and the employees put the group leader in their pocket, just like a set of dolls. It’s not appropriate for employees to communicate directly without the general manager.

In real life, it is often an employee who says one sentence, the group leader adds two sentences, then the manager adds two sentences, and finally the general manager adds two more sentences. But in the online world, it should be the general manager who speaks, the manager who adds two sentences, the team leader who adds two sentences, and the employee who adds two sentences.

So what are the IP and MAC layers doing when TCP does a three-way handshake? Of course, TCP sends each message with the IP layer and the MAC layer. Because every time TCP sends a message, all the mechanisms at the IP and MAC layers run again. You only see TCP three handshakes, and in fact, the IP layer and the MAC layer have been working on this for a long time.

One thing to remember here: as long as the package is running on the network, it is complete. There can be a lower level without a higher level, but there can never be a higher level without a lower level.

Therefore, TCP protocol, no matter three handshake, retry, as long as you want to send packets, there must be IP layer and MAC layer, otherwise it is impossible to send out.

The question often asked is, I already know the IP address of the machine, so I just send it a message. Why do I need a MAC address? The key here is that you can’t send a message without a MAC address.

So if an HTTP packet is running on the network, it must be complete. No matter what devices the package passes through, it is intact.

The so-called two layer equipment, three layer equipment, are these devices run on the program is different. An HTTP packet passes through a Layer 2 device, which receives the entire network packet. There are HTTP, TCP, IP, MAC. What is called a layer 2 device, is just to remove the MAC head, see whether it is discarded, forward, or keep. What is a three-tier device? After the MAC header is removed, the IP header is removed to see whether to discard, forward, or keep.

summary

  • Always imagine yourself as a program dealing with network packets: how to get network packets, how to process them according to the rules, and how to send them;
  • Always bear in mind one principle: as long as the package is running on the network, it is complete. There can be a lower level without a higher level, but there can never be a higher level without a lower level.