Computer network process detailed

First, why should a hierarchical network?

  I even have not heard of the network hierarchy, but you asked me why stratification, to find out how the network is hierarchical, with a picture about the network hierarchy, and each layer of the network protocol used:

 Why hierarchical network, because whenever complex procedures are required layered, understand the concept of a computer network, a good starting point is this: Imagine a network packet is a piece of Buffer, or a piece of memory, there is a format, at the same time , imagine that he is a program processing network packets, but this program can be run on a computer, you can run on the server, you can run on the switch, you can run on the router, you think they have a lot of network port, from get into the mouth of a package to a network, handle it with their own procedures, sent from another network port out complex procedures to be layered, this is the program design.

Second, the program is how it works?

There is a picture image of the program tells about the process of work in the column:

 Text Analysis:

(1) When a network packet from the network port time passes, you see, first of all take a look at whether or not to please come in, a process. Some network port configured in promiscuous mode, all passes, all to get up.

After (2) and came back, we should give a program to deal with, so you call process_layer2 (buffer) , of course, this is a fake function, but, in fact, there is such a real function, its role is in the buffer take off the second floor of the head, look, do what the head according to the contents inside.

(3)假设你发现这个包的MAC地址跟你的相符,那就是发给你的,于是需要调用process_layer3(buffer),这个时候Buffer中就没有二层的头了,因为已经在上一个函数在处理过程中拿掉了,或者将开始的偏移量移动了一下,在这个函数里面,摘掉三层的头,看看是发给自己的,还是希望转发出去的,如何判断呢?如果IP地址不是自己的,那就应该转发出去,如果IP地址是自己的,那就是发给自己的,根据IP头里面的标示,拿掉三层的头,进行下一层处理,到底是调用process_tcp(buffer)?还是调用process_udp(buffer)

(4)假设这个地址是TCP的,则会调用process_tcp(buffer),这时候Buffer里面没有三层的头,就需要查看四层的头,看这是一个发起,还是一个应答,又或者是一个正常的数据包,然后分别由不同的逻辑进行处理,如果是发起或者是应答,接下来可能要发送一个回复包;如果是一个正常的数据包,就需要交给上层了,交给应用去处理了,在四层的头里面,有端口号,不同的应用监听不同的端口号,如果发现浏览器在监听这个端口,那就发给浏览器处理就好了,至于浏览器怎么处理,就与你无关了。

(5)浏览器解析HTML,显示出页面来,电脑的主人,看到页面,点击鼠标,点击鼠标的动作被浏览器捕获,发起另一个HTTP 请求了,于是使用端口号,将请求发送给你(你现在是处理buffer数据包的程序)

(6)你应该调用send_tcp(buffer),不用说,Buffer里面就是HTTP请求的内容,这个请求里面加一个TCP的头,记录下源端口号,浏览器会给你目的端口号,一般是80端口。

(7)然后调用send_layer3(buffer),Buffer里面已经有了HTTP的头和内容,以及TCP的头,在这个函数里面加一个IP的头,记录下源IP地址以及目标IP地址

(8)然后调用send_layer2(buffer),Buffer里面已经有了HTTP的头和内容,TCP的头,以及IP的头。这个函数里面要加一个MAC的头,记录下源MAC地址,得到的是本机器的MAC地址和目标MAC地址,这个是必须要添加的MAC地址。

这样,只要Buffer里面的内容完整,就可以从网口发出去了,你作为一个程序的任务就告一段落了。

三、层与层之间有什么关系?

划重点:只要是在网络上跑的包,都是完整的,可以有下层没上层,绝对不可能没有下层有上层。

所以,对于TCP协议来说,三次握手也好,重试也好,只要想发出去的包,就要有IP层和MAC层,不要是发不出去的

如果一个HTTP的包跑在网络上,它一定是完整的,无论经过哪些设备,它都是完整的,所谓的二层设备、三层设备,都是这些设备上跑的程序不同而已。一个HTTP协议的包经过一个二层设备,二层设备收进去的是整个网络包。这里面HTTP、TCP、IP、MAC都有,什么叫二层设备啊,就是只把MAC头摘下来,看看到底是丢弃、转发、还是自己留着。什么是三层设备啊,就是把MAC头摘下来,再把IP头摘下来,看看到底是丢弃、转发、还是自己留着。

 

发布了85 篇原创文章 · 获赞 200 · 访问量 30万+

Guess you like

Origin blog.csdn.net/l_liangkk/article/details/103930636