TCP/IP Detailed Explanation (1)

The TCP/IP protocol is the most basic protocol of the Internet, and it refers to the seven-layer OSI (Open System Interconnect, that is, open system interconnection) model to a certain extent. The OSI reference model is a network interconnection model released by the international organization ISO in 1985
. The purpose is to allow all companies to use a unified specification to control the network, so that all companies follow the same communication specifications, so that the network can be interconnected; the OSI model defines the seven-layer framework
of network interconnection (physical layer, data link layer, network Layer, transport layer, session layer, presentation layer, application layer), each layer implements its own functional protocol, and completes the interface communication with the adjacent layer.

In TCP/IPthe protocol, OSIthe 7 layers are simplified to 4 layers, which are link layer (also known as data link layer or network interface layer), network layer, transport layer, and application layer;

Application layer protocols: HTTP, Telnet, SMTP, FTP, DNSetc.; mainly for receiving messages from the transport layer, or writing data transmission to the transport layer;

Transport layer protocol: TCP, UDPetc.; mainly to achieve end-to-end data transmission, transmission services are divided into reliable ( TCP) and unreliable ( UDP), to provide end-to-end flow control, error control, QoS(Quality of Service);

Network layer protocols: IP, ICMP(Internet Control Message Protocol), IGMP (Internet Group Management Protocol, Internet Group Management Protocol), etc.; mainly for data packet transmission in the network;

Link layer protocol (also known as data link layer or network interface layer): ARP(address resolution protocol), RARP(reverse address resolution protocol), etc.; usually includes the device driver in the operating system or the corresponding network interface card in the computer, Together they deal with the details of the physical interface to the transmission medium (such as a cable or other physical device);

The role of each layer protocol

Physical layer: use MAC address to solve the problem of device identity;

The evolution history of communication tools: single or multiple network cables, hubs, switches, routers
Single or multiple network cables: two computers can be connected with one network cable to communicate. As for why they can communicate, this involves As for the knowledge of the operating system, if you are interested, you can learn about it IO, interrupts, buffers, and how the network card is converted into electrical signals, etc., or I will sort out this knowledge later.

Hub: As more and more computer devices are added, more and more network ports need to be opened on each computer, but the network ports opened by a computer are limited, and it is impossible to increase the network ports infinitely, so it appears A hub is an intermediary. Each computer's network cable is plugged into this hub, which is forwarded by this device, so that each device can communicate with each other. The communication mode of this hub is essentially the same as the communication mode of single or multiple network cables, except that the number of network ports of each computer is reduced.

A hub is just a computer that forwards electrical signals to all outlets (broadcasting) and does nothing to forward messages. For example, when A sends data to B, it only needs to provide the source MAC address and destination MAC address in the data packet header. When B receives the data packet, it only needs to judge whether it is sent to itself according to the destination MAC address in the data packet header. , and other devices, after receiving the data packet, also judge whether to send it to itself from the destination MAC address of the data packet header. If the destination MAC address is inconsistent with its own address, it discards the data packet MAC.

Although the hub simplifies the overall layout, the data sent from A to B will send data packets to all devices connected to the hub, which may cause data security issues and waste network resources.

So, if there is a device that can send data only to the B device?

Switch: In order to solve the problem of the hub, the message is sent only to MACthe device pointed to by the destination address, which is placed at the data link layer.
The switch maintains an MACaddress table inside, which records the MAC address of each device, and also records which port the device is connected to. Assuming that device A sends data to device B, when the data packet reaches the switch, the switch checks the internally maintained MAC address table and finds that the MAC address of device B is XXXX, which is on port 1, so the switch forwards the data packet from port 1 Send to the B device, the small-scale network formed by this transmission method, we call it Ethernet.

Of course, MACthe address table is empty when it is first established. When you send a message to device B, the message of device A is sent from port 1. At this time, the switch records the address and MACport number of device A in the internal mapping table of the switch. When MACWhen the switch cannot find the address and port number of device B in the internal mapping table, it will send this data packet to all ports, that is, to all devices. When only machine B receives the data and sends it to itself, then do A response is sent out, and the response code is sent from port 3 into the switch. At this time, the switch will MACupdate the MAC address and port of device B in the address table. As the machines on this network communicate, MACthe address tables in the switches are constantly being refined.

However, as the number of devices in the network increases, the number of MAC addresses that need to be maintained inside the switch also increases, and the number of ports of the switch will be insufficient.

At this time, the switch can no longer satisfy such a huge mapping relationship.

Router: As an independent MACdevice with an address, it can forward data packets once (note: each port of the router has an independent MACaddress); the router belongs to the network layer. Routers are specifically responsible for IPfinding addresses.

With a router, an address is naturally born IP, IPthe address length is 32 bits, and MACthe address length is 48 bits;

When device A and device C are not in the same subnet, device A will send the data packet to the router, and then the router will forward it;

However, how does device A know that it is not in the same subnet as device C, and how does A know that device is a router?

This is how subnets and gateways were born

The origin of the subnet:
A sends data to C, how does it know whether to forward it through the router? The answer is through the subnet.
The destination IP and source IP are in the same subnet, and they are forwarded directly through the switch; if
the destination IP and source IP are not in the same subnet, they are handed over to the router for processing;

Assuming that a certain subnet mask is defined as 255.255.255.0,
the source IP and destination IP are respectively ANDed with this subnet mask. If they are equal, they are in the same subnet. If they are not equal, they are in different subnets. For example, for 192.168.0.1, 192.168.0.xxx is considered to be in one subnet, otherwise it is in a different subnet.
For example:
Computer A: 192.168.0.1&255.255.255.0 = 192.168.0.0
Computer B: 192.168.0.3&255.255.255.0 = 192.168.0.0 Computer
C: 192.168.2.1&255.255.255.0 = 192.168. 2.0D
computer: 192.168.2.6&255.255.255.0 = 192.168.2.0
Then A and B are in the same subnet, C and D are in the same subnet, but A, C and D are not in the same subnet; the
default gateway (such as 192.168.0.254), when When A and C are not in the same subnet, A will send the packet to the default gateway under the same subnet. As for the device, A does not care, just send it.

The subnet mask actually indicates how many bits are the subnet segment, such as 192.168.0.0 (255.255.255.0), which can be abbreviated as 192.168.0.0/24;
insert image description here
when A transmits data to C, in addition to adding the data link layer In addition to the header of the network layer, a header of the network layer should be added;
the information sent by A to the router is as follows:
insert image description here
the information sent by the router to C is as follows:
insert image description here
As for why the sending process of the above process is like this, it is different from the subnet mentioned above It does matter, if you understand the concept of a subnet, you will understand the sending process from A to C;
as for how does the router know that the received data packet should go out from its own port?
The answer is that the router also maintains a routing table, which is the same as the MAC address table. Different from the MAC address, the routing table is not a one-to-one relationship;
insert image description here
because the subnet mask actually indicates how many digits represent the subnet segment, such as 192.168.0.0 (255.255.255.0) can also be abbreviated as 192.168. 0.0/24;
insert image description here
Therefore, the routing table shows that all 192.168.0.XXX are sent to port 0, and all 192.168.1.XXX are sent to port 1. The next hop in the above figure has no value temporarily, which can be ignored ;
In this way, the data we send from A to C can be sent normally;

However, there is another problem now. Sending data requires data link layer packets, and we only know the IP address. How is the data sent?

The answer is the ARP protocol. In the network layer, the MAC address of the corresponding device is found through the IP address. At the same time, computers A and C will cache an ARP cache table, recording the correspondence between the IP and the MAC address;

At the beginning, the ARP cache table is empty. In order to know the MAC address of C, A will broadcast an ARP request. After receiving the request, C will send its own MAC address and response code. Updated its own ARP cache table.

In this way, broadcast requests are continuously made ARP, and eventually the ARP cache table in the computer will be completely updated;

MACHow did the address table, routing table, and ARPcache table mentioned above come from?

MACAddress table: Each node in the Ethernet communicates continuously through the switch and is gradually improved; it is used to map MACaddresses and its port numbers;
routing table: routing algorithm + manual configuration is gradually improved; it is used to map IP addresses and its port numbers ;

ARPCache table: It is gradually improved through ARPprotocol broadcast requests; there are ARP cache tables in computers and routers, which are used for IPaddress-to- MACaddress mapping;

HTTPMessage transmission principle

When using TCP/IPnetwork communication, data packets are communicated with each other in hierarchical order. The sender goes down from the application layer, and the receiver goes up from the link layer. For the data from the client to the server, the data transmission sequence of each frame is: application layer –> transport layer –> network layer –> link layer –> link layer –> network layer –> transport layer –> application layer;
insert image description here
TCPProtocol message format
In TCP/IPthe protocol stack, IPthe protocol layer only cares about how to make data transmission across the boundary of the local network, not how the data is transmitted. The overall TCP/IPprotocol stack works together to solve how data can be smoothly transmitted to the destination through many point-to-point channels. A point-to-point path is called a hop. Through TCP/IPthe protocol stack, network members can establish mutual data paths on the basis of many "hops".
The TCP protocol at the transport layer provides a connection-oriented and reliable byte stream service, and its data frame format is shown in the figure below: The data frame
insert image description here
of the TCP protocol is roughly divided into the following parts:
Source port: The source port represents the packet The sending port occupies 16 bits. The combination of source port and IP address can identify the sending address of the message;
destination port: the destination port indicates the receiving port of the message, accounting for 16 bits. The combination of destination port and IP address can identify the receiving address of the message;
serial number: TCPin the byte stream sent by the sender during the transmission process, each byte of the data part of the transmitted message has its number. The serial number occupies 32 bits, that is, 4 bytes, and the sender needs to mark the serial number when sending data.

The sequence number SYNis related to the value of the control flag. Depending on SYNwhether the control flag is 1 or not, the meaning of the sequence number is different:
(1) When SYN=1, the current connection establishment stage, the sequence number at this time is the initial sequence number ISN, which is randomly generated by an algorithm Sequence number;
(2) When SYN= 0, at the beginning of data transmission, the sequence number of the first packet is ISN+1, and the sequence number of the following packet is the value of the previous packet SN+ TCPthe net number of bytes of the packet (excluding the TCP header ). For example, if the payload of a frame sent by the sender TCPis 16 bytes and the sequence number is 6, when the sender sends the next data packet, the sequence number should be set as: 16 + 6 = 22;

During data transmission, TCPthe protocol provides an ordered data stream to the upper layer through sequence numbers. The sending end can track the amount of data sent according to the sequence number, and the receiving end can identify duplicate packets according to the sequence number TCP, thereby TCPdiscarding the repeated packets. For out-of-sequence data packets, the receiving end can sort according to the sequence number.

Acknowledgment sequence number: identifies the sequence of bytes that the message receiver expects to receive. If the control bit is set ACK, the value of the acknowledgment sequence number represents the sequence code of a packet to be received, which points to the data packet to be received, that is, the sequence code of the next packet expected to be received; assuming that the sender sends 6
net The charge is 1024bytethat the data packet with the initial SN number of 1 is sent to the server. After receiving a packet, the server needs to reply a ACKresponse to confirm the data packet to the sender. The ACK value of the ACK response data packet is for each sender packet. The SN+packet payload indicates the number of bytes received by the server, and also indicates the SN sequence number of the next data packet expected to be received by the sender.
insert image description here
Only when the flag of the control flag ACKis 1, the confirmation sequence number in the data frame ACKis valid. TCPThe protocol stipulates that after the connection is established, the value of all sent messages ACKmust be 1, and the confirmation sequence numbers of all messages are valid. If it is a SYN type message, ACKthe flag is 0, and there is no confirmation sequence number.

Header length: This field occupies 4 bits and is used to indicate TCPthe header length of the message; its value indicates not the number of bytes, but the number (or multiple) of 32 bits contained in the header, or 4 words Sections, so the TCP header can have up to 60 bytes (4*15=60). The length of the header without any option field TCPis 20 bytes, so the length of the header is 5, which can be calculated by 20/4=5.

Reserved 6 is: the length of the field reserved after the header length is 6 bits, which is temporarily useless as a reserved field.
Control flags: Control Bits (Control Bits) have a total of 6 bits, and the specific flag bits are: URG、ACK、PSH、RST、SYN、FIN.
Window: The length is 16, a total of 2 bytes. This field is used for flow control. The unit of flow control is the number of bytes, indicating the number of bytes that the receiver wishes to receive at a time;

Checksum: length is 16 bits, a total of 2 bytes. Perform checksum calculation on the entire TCPmessage segment and data, and the receiving end is used to verify the received data packet;

Emergency pointer: the length is 16, a total of 2 bytes. It is an offset, which is SNadded to the serial number value to indicate the serial number of the last byte of urgent data;
the above is TCPthe field that must be required in the header of the protocol message, also known as the inherent field, with a length of 20 bytes, and the following is the TCPmessage Optional and padding items for , and data bits.

I will stop here for the time being. There are too many contents to finish at one time. I will continue to update next time.

Guess you like

Origin blog.csdn.net/shanniuliqingming/article/details/130517130