Writing in the front

We’re always talking about TCP/IP. HTTP headers; TCP header; IP header how how; MAC head blah blah blah. But then again, that’s what most of the questions about computer networks are about. So if you look at this, it’s really the best solution.

However, as a comprehensive development of the new code of moral, intellectual and physical American labor farmers, more understanding, more learning. It’s always true. (Not just a few more hair, afraid of what! Use bully at worst.

Today, we’re going to start with league of Legends and talk about how packets go from our crystal (my computer) to the enemy crystal (target computer).

Of course, if you don’t want to see these baffling content of friends, you can directly drag to the end of the article to see the summary ~

Open to

The preparatory work

First of all, generally speaking, from the application layer to the link layer, we wrap HTTP content, TCP content, IP content layer by layer. At the link layer, you need to encapsulate the MAC address.

In this case, we need to obtain the MAC address of the target IP through ARP. The first step is to determine whether the target IP address is in the same network segment as you.

A-ip check: The system uses its own (A-IP) subnet mask and the IP address of the SYSTEM are in the same network segment. The result is 1 only if both of the two digits are 1. Otherwise, the value is 0.

If it’s the same network segment, it’s very comfortable. Direct the LAN to yell out the MAC who knows this X-IP address. X-ip where the network port a look: Oh god, this is not called me? Dude, come in, come in, come in.

So the bag came in just fine. So what follows is the familiar TCP/IP reverse lookup process.

What if they’re not on the same network segment?

Let’s start with a picture:

Enter the world of alliances

Do you feel like you’re on the wrong set? No mistake, no mistake, so let me, using this diagram, explain how packets (heroes) will travel from our crystal (my computer) to the enemy crystal (target computer).

Let’s say we are random heroes, the game loads, heroes are created (from the application layer to the network layer, wrapped layer by layer) and come to our springs (the link layer).

The game is ready to begin. Yo, our hero is Raven. Suppose our Raven was born with one goal in mind: to break into the enemy crystal from the road.

But our Raven, she knows the target and nothing else! Which way is the enemy on the road? I’m new to this. I don’t know. The fountain merchant is a warmonger, well informed, go and ask him!

The fountain merchant saw raven’s target and gave raven a clear path: You go to our high tower.

We hit the high tower, saw Raven’s target said: don’t panic, although I don’t know how the enemy hit the crystal, but I know how we hit the front teeth. You go from me to him!

Raven has arrived on our way with the Rune blade in tow. When we hit the road and take a look at the target in front teeth, Raven’s target is enemy crystal! We hit the front teeth, a contemptuous smile: little girl, I advise you not to seek death.

Raven cold hum 1: old niang, heart yue member 666! This word, the day also clear, rain also stopped, the network seems to be very good.

And that’s how raven, left hand yuan, right hand big hat, killed her way to the enemy crystal.

Have a serious one

Parse some of the above in bold:

noun explain
R Vivian: I won’t go into that too much, our packets.
The target: That’s our target IP.
Spring businessman: It is understandable that the default gateway will handle our packets when the destination IP address is not in the same network segment as the source IP address. How it is handled will be discussed below.
Road tower, road front teeth: These two can be understood as the routers through which packets of data jump during network transmission.

To be honest, with the league example, the truth may not be very accurate, but the big picture can be explained. Now let’s talk about this process in earnest

In fact, this process can be fully explained by a graph:

Before the packet is sent, you need to calculate whether the destination IP address and the packet are on the same network segment. There are two possibilities: Yes or no. On the same network, use ARP to obtain the MAC address of the target, encapsulate the MAC address into a packet, and send the packet. If it is not the same network, you need to obtain the MAC address of the static gateway through ARP (also roar) and send the packet to it. As for how the static gateway finds the target address, it is the static gateway’s business.

Stop and talk about gateways and routers

The gateway

Static gateway: The static gateway is configured using DHCP when the operating system starts. The default IP address of the static gateway is 192.168.1.1.

A static gateway is a gateway, which is the key of the LAN to which it belongs.

The router

A router connects multiple gateways and forwards data packets to one gateway. Therefore, a router is the key to managing gateways.

So comparing a gateway to a router may not be very appropriate. They are not the same thing: if you compare the whole world of computer networks to the ancient world. So a gateway is like a checkpoint, and a router is like a city. A city can control multiple levels. Levels can control multiple paths.

If we want to go from Xuchang to Chang ‘an, then on this road, hu Lao pass and Han Gu pass can be called gateways, luoyang device is a router. Xuchang and Hulaoguan can be regarded as the same LAN.

Xuchang people want to go out from the LAN, must start from the Tiger prison (default gateway), and want to chang ‘an city, the tiger prison gateway knows to be forwarded to Luoyang city. As a router, Luoyang found the IP address of Chang ‘an through its own routing table, which needed to be forwarded to the gateway of Hangu Pass.

So the packet arrived in the LAN under the jurisdiction of Hangu guan, and finally arrived in Chang ‘an.

Our network world is connected by routers to one LAN, while gateways are responsible for their own LAN.

Continue the packet’s journey

The static gateway takes the packet and proceeds to calculate how to forward the packet based on the destination IP address. For example, the static gateway knows that the destination IP should have a router 4 to forward. It then rewraps the MAC address and sends the packet to Router 4. This may raise a question: how do routers know to whom an IP address is addressed on such a large network? This involves routing tables and routing protocols.

The routing table

From this table, the router knows which network port (gateway) the incoming packets should go through. This table is generated by the routing protocol.

Routing protocol

Routes are classified into static routes and dynamic routes. Static routing can be understood as setting the routing structure and jumping from place to place, but this is only suitable for simple problems in the network case.

So, in this section we’ll focus on dynamic routing. Dynamic routing The dynamic routing table can be generated according to the routing protocol algorithm, which changes with the network health.

Our Internet world is a complex and changeable environment, which can be viewed as a graph structure if abstracted. So now the problem for the routing algorithm becomes finding the shortest path to the target from the graph.

Here there are two common: distance vector routing algorithm, link state routing algorithm.

Dynamic Routing algorithm – Distance vector routing algorithm:

Based on the Bellman-Ford algorithm. In our data structure. Graph shortest path algorithm. The basic idea of the algorithm is that each router keeps a routing table, and each row contains two parts of information, one is to go to the destination router and exit from the line, and the other is the distance to the destination router.

Due to space reasons, this part of the content will not be expanded temporarily, if there is a small partner interested, you can go to understand yo.

Dynamic routing algorithm – Link state routing algorithm

It’s based on Dijkstra’s algorithm, which is also the algorithm in our data structure. Each router that starts up finds and calculates the distance from its neighbor and broadcasts this table to the entire network. Eventually each router will have the entire routing structure.

Due to space reasons, this part of the content will not be expanded temporarily, if there is a small partner interested, you can go to understand yo.

Continue the packet’s journey again

When our router owns the routing table, it is as if the router owns the whole world, so in the world of computer networks, any IP address that is routed to is accessible, just a few router hops.

Every time our packet arrives at a router, the router checks the destination IP address of the packet and configures it with the contents of its routing table to see which gateway it should go to.

If a gateway receives a packet and finds that it is not on its own network, it also forwards it to a structured router, repeating the process until it reaches its destination.

conclusion

When a data leaves a network card, there are two outlets. One is broadcast to a computer in the same network segment. Another way out is to send it to the configured default gateway. This gateway sends the corresponding router, which uses its routing table to determine the next gateway. The next gateway checks whether the packet is an IP address in its own LAN, and if not, sends it to a router that can forward it. Repeat the process over and over again. The computer broadcasts its LAN contents until the appropriate gateway receives the data.

The end of the

In fact, the content of this article is biased towards the process of sorting out, because the world of computer network itself is very large and complex knowledge, three words can not explain the complexity and wisdom of it (frankly speaking, even I can not). Therefore, this paper tends to explain the process in a simplified way.

If you are interested, you can learn more professional content by yourself

This public number!

Is the pit we stepped on all the way from graduating students to development, as well as the record of our step by step learning, if interested in friends can pay attention to it, refuelled together oh ~