The true meaning of network layering

Complex programs must be layered, which is a requirement of program design. For example, complex e-commerce will be divided into database layer, cache layer, Compose layer, Controller layer and access layer, with each layer focusing on its own tasks.

When a network packet passes through a network port, and you see it, first see if you want to invite it in and deal with it. Some network ports are configured with promiscuous mode, and everything that passes through is taken in.

After bringing it in, it will be handed over to a program to process. So, you call process_layer2(buffer). Of course, this is a fake function. But you understand the meaning and know that there must be such a function. So what does this function do? From the Buffer, remove the second-layer header and take a look at what operations should be performed based on the content in the header.

Assuming that you find that the MAC address of this packet matches yours, it means that it was sent to you, so you need to call process_layer3(buffer). At this time, there is often no second-level header in the Buffer, because it has been removed during the processing of the previous function, or the starting offset has been moved. In this function, take off the three-layer header to see if it is sent to you or if you want it to be forwarded.

How to judge? If the IP address is not your own, it should be forwarded; if the IP address is your own, it should be sent to you. According to the mark in the IP header, remove the three-layer header and proceed to the next layer of processing. Should we call process_tcp(buffer) or process_udp(buffer)?

Assuming that this address is TCP, process_tcp(buffer) will be called. At this time, there is no Layer 3 header in the Buffer, so you need to check the Layer 4 header to see if it is an initiation, a response, or a normal data packet, which will then be processed by different logic. If it is an initiation or response, a reply packet may be sent next; if it is a normal data packet, it needs to be handed over to the upper layer. To whom? Is there a process_http(buffer) function?

No, if you are a network packet processing program, you do not need to have process_http(buffer), but should leave it to the application for processing. Which application should I give it to? There is a port number in the layer 4 header, and different applications listen to different port numbers. If you find that the browser application is listening on this port, then you can just send it to the browser. As for how the browser handles it, it has nothing to do with you.

The browser naturally parses the HTML and displays the page. The computer owner was very happy to see the page and clicked the mouse. The mouse click is captured by the browser. The browser knows that it is about to initiate another HTTP request, so it uses the port number to send the request to you.

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

Then call send_layer3(buffer). The Buffer already contains HTTP headers and content, as well as TCP headers. Add an IP header to this function and record the source IP address and destination IP address.

Then call send_layer2(buffer). The Buffer already contains HTTP headers and contents, TCP headers, and IP headers. In this function, you need to add a MAC header and record the source MAC address. What you get is the MAC address of the machine and the MAC address of the target. However, it depends on whether you know it at present. If you know it, just add it directly; if you don't know it, you have to go through a certain protocol processing process to find the MAC address. You have to fill in one anyway, you can’t leave it blank.

Everything is ready, as long as the content in the Buffer is complete, it can be sent from the network port, and your task as a program is over.

As long as the package is running on the network, it is complete. There can be a lower level without an upper level, but it is absolutely impossible to have an upper level without a lower level. Therefore, for the TCP protocol, whether it is a three-way handshake or a retry, as long as you want to send out the packet, you must have an IP layer and a MAC layer, otherwise it will not be sent out .

What is a layer 2 device? Just take off the MAC header and see if it is discarded, forwarded, or kept. So what is three-layer equipment? After taking off the MAC header, take off the IP header to see if it is discarded, forwarded, or kept.

This article is a study note for Day 2 in September. The content comes from Geek Time's "Internet Protocol". This course is recommended.

Guess you like

Origin blog.csdn.net/key_3_feng/article/details/132642692