In the previous article, we described the “application layer” and “transport layer” in the computer five-layer model, and elaborated the relevant principles of the more complex TCP protocol. I believe that we must also have harvest, so this article will continue to learn the five-layer model.

The network layer

“Network layer” actually is a “forward” to solve the problem, by the legendary “IP” divides the network range, which I did not directly use cable together with you, I can also through the analysis of the IP you found out how responsible your gateway router, and through your gateway routing quote you transmit data.

This is what the “network layer” does, essentially solving the problem of two hosts communicating with each other that do not exist under the same subnetwork. And “IP protocol” and “how to resolve the ALGORITHM of IP” are the two most core content, we first look at the “IP protocol” related concepts.

IPv4, for example, uses 32 bits to describe an IP address. Therefore, in theory, IPv4 provides more than 4 billion IP addresses, which are expressed in dotted decimal notation.

For example, the IP address of 11000001 00100000 11011000 00001001 is 193.32.216.9.

Thus, we have solved the problem of IP addressing, but how to determine which subnetwork it belongs to from an IP address?

Introduced the noun “subnet mask”, which is formally expressed in 32-bit bits, like an IP address. The bits describing the network part are all 1, and the host number part in the sub-network is all 0.

For example: the subnet mask 11111111.11111111.11111111.00000000, written in a decimal is 255.255.255.0. It specifies that the first 24 bits of an IP address using the subnet mask are part of its subnetwork, and the last 8 bits are the number of the host corresponding to the IP address in the subnetwork.

Here’s an example:

IP address 172.16.254.1 corresponds to a subnet mask of 255.255.255.0. Therefore, you only need to perform AND to obtain the network part of the IP address.

Therefore, the network number of this IP address is 172.16.254.

Let’s look at a very important protocol that solves the problem of how a host that has just joined a subnetwork obtains its IP address. This protocol is called dynamic Host Configuration Protocol (DHCP).

DHCP

Generally speaking, there are two ways to configure the IP address of the host. One is for the administrator to manually specify an IP address. Of course, the cost is very high, and you cannot configure an IP address that has been allocated.

The other is our DHCP protocol, which allows the newly added host to automatically obtain an IP address and the relevant subnet mask and gateway address.

By default, routers isolate broadcast packets and do not send received broadcast packets from one subnet to another. When the DHCP server and the client are on different subnets, the router that acts as the default gateway of the client sends broadcast packets to the subnet where the DHCP server resides. This function is called DHCP Relay.

In other words, a subnetwork must have one DHCP server to assign IP addresses to the entire subnet. But it’s also a little “silly” to configure a separate DHCP server for each subnet.

Another solution is that the gateway on a network knows the location and IP address of the DHCP server that is responsible for the network, and the gateway route forwards the DHCP request and returns the response message. This is called DHCP relay.

Of course, today’s routers can actually act as DHCP servers themselves, providing dynamic address retrieval services for their subnets, so forwarding is often not as troublesome.

A complete DHCP request and response process looks like this:

The first step:

DHCP server discovery. The primary task in this phase is to locate the DHCP server on the network and send DHCP packets to destination port 67 based on UDP/IP.

Because the local host does not have AN IP address, source Address in the IP packet is 0.0.0.0 and Destination Address is 255.255.255.255.

In this way, when the packet is broadcast at the link layer, all hosts in the same subnetwork accept the packet, but only the DHCP server responds to the request.

So if the router itself is a DHCP server, it will go to the second step, otherwise the router will forward packets to the network where the DHCP server resides.

The second step:

Provided by DHCP server. The DHCP server, whether on the Internet or the gateway itself, responds with a Provide message after receiving a Discover message.

This document contains the recommended IP address, subnet mask, and IP address lease period.

Step 3:

DHCP requests. This is actually a selection phase, where the client host confirms the parameters recommended by the server, decides to use them, and still sends a request to the server in the form of a broadcast confirmation.

Step 4:

DHCP ACK. After receiving the confirmation request from the client host, the server actually allocates an IP address from the IP address pool and returns an ACK for the confirmation information from the client.

After that, the host got a usable IP address and finally joined the network.

In addition, there is a detail we do not know whether to pay attention to daily, that is, for the same subnetwork, THE IP address is basically the same, and there is no different IP because each time after the boot connected to the network.

This is a DHCP convention. When a host joins a sub-network for the first time, it obtains a new IP address from the DHCP server.

When the host is added to the network again, the host directly goes to step 3 of the DHCP request and sends the parameters used last time to the server to check whether the parameters are available. Usually the server will agree and assign an IP address as you request, which is why you use almost the same IP address every time.

After talking about DHCP dynamically obtaining IP addresses, we take a brief look at the basic format of IP datagrams and finally discuss the router selection algorithm to see how an IP datagram is forwarded by a router.

How each field or option is used is not discussed here for the moment. It is easy to understand by analyzing packet distribution and parsing.

The router

As a core device at the network layer, a router completes the complete path forwarding process from the destination IP address to the subnetwork where the destination IP address resides. Its internal structure is as follows:

Each port is directly connected to a device, where a “routing processor” resolves which output port data from an input port should be pushed to.

So, as you can see, the core of the router is the routing processor, the algorithm that drives the routing processor, which we call the routing algorithm. The algorithm is essentially solving the problem of which port to forward a datagram to when it enters the router’s memory.

A good “routing algorithm” should not only solve the problem of how to get to a destination, but also consider the fastest way to get there, which is to judge and selectively bypass congested network paths.

The whole routing algorithm is divided into two categories, global routing algorithm and distributed routing algorithm. One of the most typical implementations of the former is “link state routing algorithm”, and one of the most typical implementations of the latter is “distance vector algorithm”.

Instead of discussing the theory behind these two algorithms, we’ll focus on how routing is implemented on the Internet based on these two algorithms.

First of all, the Entire Internet is a large and complex system. AS a whole, it is divided into autonomous systems (AS). Each AS runs the same routing algorithm and uses BGP to exchange information among autonomous systems.

The entire Internet is roughly made up of these interconnections of autonomous systems, in which all routers run the same routing algorithm, “RIP” based on the distance vector or “OSPF” based on the link state.

As for the reason of splitting autonomous systems, you will understand after we introduce RIP or OSPF.

The RIP algorithm is as follows:

A simple autonomous system, let’s take this as an example to see how the whole RIP protocol works.

To begin with, router A’s forwarding table must look like this:

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to a subnet The next-hop routing hop B 1 q E x 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

Other routers are similar in that the first step is to establish a connection with the neighbor directly connected to them.

The second step is an ongoing process in which neighboring routers exchange information about their forwarding table every 30 seconds.

So after A switch, router A receives the forwarding table information from router B and router E, and the routing table is updated as follows:

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to a subnet The next hop routing hop E x B 1 q 1 2 p y B E 2 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

But there’s A detail here, subnetwork Y can be reached through a-B-Y, but it can also be reached through A-e-C-Y. As you might have guessed, a router will of course choose one of the shortest paths to update its forwarding table.

Therefore, the distance vector algorithm is essentially to ensure that all routers in an AUTONOMOUS system know the shortest path of a destination subnet through continuous information exchange.

The OSPF implementation looks like this:

Let’s do the same with the above example:

OSPF is implemented based on the link-state routing algorithm, so it is also a global routing algorithm. After the algorithm is run once, routing information on the entire network can be updated.

In essence, OSPF is a Dijstrasse shortest path problem, which updates the entire routing table through continuous iteration and calculation. Suppose our router A is running OSPF:

After the completion of the first iteration, it gets the path calculation of the subnetwork associated with routers B and E.

For the second time, the algorithm is re-run starting from B or E. Here, we assume that the algorithm is run starting from B, then the path of the subnetwork related to C is also updated into the routing forwarding table of A.

For the third time, starting from C, the same algorithm is run to obtain the sub-network path update related to D.

As D is the terminal route, there is no other directly connected route, so the algorithm does not continue and returns to E.

For the fourth time, with E as the origin, the algorithm is run to obtain the path of c-related subnetwork. If there is A shorter path, the forwarding table of A will be updated to the optimal path.

However, unlike RIP, OSPF converges fast and can complete tasks quickly. RIP, on the other hand, needs to exchange information continuously to meet requirements, and usually falls into a long period.

Of course, OSPF requires more CPU computing power and more memory storage space. RIP is applied to lower-level ISPs, while OSPF runs on higher-level ISPs.

So far, the basic content of the whole network layer is also introduced. In summary, the core task of the network layer is responsible for forwarding packets. How to forward packets to the network of the destination host involves THE IP protocol and divides the network by IP address and subnet mask. The router performs a routing algorithm to obtain the complete routing path of the destination subnetwork and distributes it.

The link layer

The network layer addresses packet forwarding to the destination network, that is, the gateway route that is forwarded to the destination network, while the link layer addresses packet broadcasting to individuals, that is, the destination host.

IP packets at the network layer are encapsulated as “Ethernet frames” at the link layer. The basic structure is as follows:

The lead code is used to synchronize the clock, which, as I understand it, is used to distinguish frames one by one. The source and destination addresses refer to “Mac addresses”, also known as physical addresses.

Mac address is uniquely identified by the manufacturer. Something like this:

34-E6-AD-17-A5-6B

The Mac address of any host in the world is different, unlike IP addresses that can be shared when not in use.

The next protocol that converts host IP addresses to Mac addresses is the ARP address resolution protocol.

ARP is similar to THE DNS protocol introduced at the application layer. You enter a domain name address and output an IP address, whereas ARP inputs an IP address and output a Mac address.

Every host in the network, including the router, has built-in ARP modules and ARP tables. When a packet reaches the link layer, the first thing to do is to query the ARP module of the host with the destination IP address of the packet as input. If the Mac address of the destination host can be obtained, encapsulate an Ethernet frame and send it to the physical layer.

If the LOCAL ARP table does not store the Mac address of the host with the destination IP address, you need to query the Mac address of other hosts on the same network.

The sender constructs a special ARP packet. The source Mac address is the sender’s Mac address, the destination Mac address is broadcast address 255.255.255.255, and the source and destination IP address are essentially a special Ethernet frame.

Then all hosts on the network will receive the ARP packet, and all they need to do is to split the IP address to see if it is the same as their own IP address. If so, they respond to an ARP packet and tell the sender their Mac address.

If not, it checks its OWN ARP module to see if it can help.

Eventually, the sender gets the desired destination Mac address and updates its OWN ARP table, then encapsulates a normal Ethernet frame and sends it.

The Ethernet uses the broadcast mode. Therefore, any host in the same subnetwork sends a packet, and all other hosts receive the packet. However, they match the destination Mac address to whether it is the host or not.

Ok, so far we also briefly introduced the link layer related content, about the physical layer, there is nothing to introduce, is 0, 1 electrical signal transmission.

On the whole OSI five layer model, we have also completed the study from top to bottom, the next article will be a complete look at “www.baidu.com”, the whole computer network happened after the story, in fact, a bit of title party, the last chapter will introduce the complete HTTP request process, forgive me!


All the code, images and files in this article are stored in the cloud on my GitHub:

(https://github.com/SingleYam/overview_java)

Welcome to wechat public number: jump on the code of Gorky, all articles will be synchronized in the public number.