Introduction to TCP/IP protocol stack

When it comes to the TCP/IP protocol stack , we may be familiar with certain protocols, such as the HTTP protocol we use in the transmission of web page data, that is, hypertext data, and the TCP protocol that provides services for the HTTP protocol, but there are some protocols We are useful but may not know very clearly, such as ARP protocol, DHCP protocol and DNS protocol and so on.

Our article is to introduce to you some very important agreements, but everyone is not very clear about them.

The TCP/IP protocol has a five-layer structure, which is the application layer, transport layer, network layer, data link layer and physical layer from top to bottom. We focus on the above four layers.

The first one is the application layer. The application layer itself can be said to be the interface of the user's real application. There are many protocols in this layer, such as HTTP protocol, FTP protocol, DNS protocol, etc. Another situation in the application layer is that users develop their own programs and then write applications on their own programs, which is also possible. Under the application layer, there are two ways that are really responsible for data transmission, one is TCP and the other is UDP. The simplest and most fundamental difference between TCP and UDP is that TCP ensures the reliability of transmitted data, while UDP does not.

Then there is the network layer. Routers and switches are network layer devices. The network layer is mainly responsible for network communication, and does not ensure the reliability of data.

The data link layer relies on physical devices for data transmission. In Ethernet, the data link layer is the MAC controller.

The following figure is the encapsulation form of the TCP/IP protocol stack

It is very clear that TCP/IP is a layer. In terms of the user layer, it does not need to care about what each layer below does. It only needs to implement some standards of some application interface protocols in the upper layer application. application and design.

When my user data is sent to it through TFTP, the TFTP protocol stack will automatically add a TFTP Header. TFTP calls UDP for data transmission, so when it calls the UDP function, it will add a UDP Header. UDP The following is the IP layer, and an IP Header will be added to the IP layer, followed by the data link layer and the physical layer, and the Ethernet Header and Ethernet Trailer will be added.

In the software part of the TCP/IP protocol stack, that is, the application layer, the transport layer and the network layer, only the header packet is added, and only the data link layer will add the header packet and a tail packet.

ARP protocol

We first introduce the ARP protocol to you

The role of the ARP protocol is to match the IP address with the MAC address, which is a protocol between the IP layer and the data link layer.

When we do an application design, we only know the IP address. Here is an example to illustrate why the ARP protocol is needed.

We need a dynamic mapping to match the IP address 192.168.0.10 with the MAC address 02-00-00-00-00-00 so that they can find each other.

We illustrate with a simple model. We have a STM32 development board and a computer here

There are two ways to set the IP for the STM32 development board, one is statically set, which is 192.168.0.10; the other is assigned by the DHCP server, which is 192.168.0.x, and the MAC address is 02-00-00 -00-00-00. The IP address of the computer is 192.168.0.11 and the MAC address is Dell_8c:fb:d1.

First, the computer sends a broadcast packet, asking who knows 192.168.0.10, please tell 192.168.0.11 (that is, the computer itself). The reason for sending the broadcast packet is that I didn’t know who 192.168.0.10 was at the beginning, so only the owner of the network segment can send a broadcast packet. If 192.168.0.10 exists in the network segment, it will give feedback to the computer and tell the computer it It is 192.168.0.10, and tell the computer its MAC address.

Let's summarize the process of some ARP protocols

In the entire ARP protocol, when I get an IP address, I don’t know its MAC address, so I need to ask through ARP query, send a broadcast packet to find out what the MAC address corresponding to the IP address is, if the network If this IP address exists in the segment, it will respond and feed back the MAC address after receiving the broadcast packet.

ICMP protocol

The ICMP protocol is actually a protocol belonging to the network layer. Its working method is that I send a sequence, and the other party sends this sequence back. I check whether the sequence sent and received is the same to judge whether the transmission is normal. At the same time, ICMP also A lot of error messages are provided.

The ICMP protocol itself is for diagnosing and controlling problems at the IP layer, the most important of which is diagnosis. Through ICMP, it is possible to diagnose which network transmission problems occur at the IP layer.

ICMP includes two parts, one is interactive query information, and the other is error information. Some of these information may be sent by the user itself, or may be generated by the protocol stack itself.

There are many kinds of ICMP error messages, we choose two of the more common ones to talk about, one is that the destination is unreachable, and the other is timeout. The most likely reason for the timeout is that someone has already replied, but it just timed out. There are several reasons for the timeout, such as a bad network driver, a busy CPU at the time, and unsatisfactory network cable conditions. The reason that the destination is unreachable may be that the network is not established.

DHCP protocol

Before it, there was a protocol with a function very similar to it called the BOOTP protocol. The functions of these two protocols are almost the same, and the purpose is to request an IP address. DHCP is a protocol designed through the UDP client and server model. The purpose of this protocol is to obtain an IP address dynamically. The allocation and management of this address is managed by the DHCP server. We want to request a lease from the HCP server For the IP address, a DHCP client is also required. Through the exchange of information between the client and the server, a dynamic IP address that can be used can be obtained.

We use an example to illustrate the use of DHCP

First of all, we must set up a DHCP server on the computer. There are many softwares that have realized this point. The most useful one is Tftpd32. This software can set up a DHCP server. We set 192.168.0.11 as the DHCP server, the MAC address of the STM32 evaluation board is 02:00:00:00:00:00, and the IP address assigned to it is 192.168.0.2. Then why assign this address to it? When we set up the DHCP server, the starting address of IP allocation is 192.168.0.1. This address is generally applied to the gateway. The most common gateway is a router. When assigning an IP address to a new DHCP client, press In order. But as we said, DHCP is a protocol for renting IP, that is, it must be returned as soon as the lease expires. The general DHCP server provides the lease for six hours, that is, the DHCP lease will be performed after six hours. update. When the lease expires, if you want to renew the lease, the leased IP address may be the same as the original IP address. This is for the convenience of the client to perform some processing, and for better network management.

DNS protocol

The DNS protocol is an application layer protocol. The most interesting thing about this protocol is that it uses two protocols of the transport layer, both the TCP protocol and the UDP protocol.

We usually type the domain name in the browser. For example, if we want to query some content of the ST official website, we will type www.st.com . In fact, we want to find www.st.com, which is obtained by the router by querying the IP address of ST's official website, that is, we must convert www.st.com into an IP address, but it is very difficult for users to remember the IP address. So we need domain name resolution protocol to convert www.st.com into an IP address.

Let's take a look at the specific implementation of DNS. It asks the DNS server where www.st.com is. DNS is also a protocol designed based on the client-server model of UDP and TCP.

Reposted from: https://zhuanlan.zhihu.com/p/79252282

Guess you like

Origin blog.csdn.net/lingshengxiyou/article/details/130158286