This series is mainly to summarize the basic computer knowledge related to the front-end and improve our knowledge system. This series is divided into the following parts (click the link to view directly).
- Computer Networks (This paper)
- The HTTP protocol
- Computer composition principle
- The operating system
- Compilation principle
Data structures and algorithms will be released in 2021.
An overview of the
A computer network is a collection of interconnected, autonomous computer systems. It focuses on how different computers communicate with each other.
In order to decouple the different functions of the complex communication system, the functions needed in the whole process need to be modular processing; In order to make each module reusable, it is necessary to establish a unified standard for each module.
Different modules here are the layers of computer networks, and standardization is the different protocols involved in communication. Let’s go over a few concepts before we get into the details.
- A protocol is a set of rules that two modules of the same layer communicate with
- Interface The connection point between adjacent modules at two layers
- Service the functions provided by the lower layer to the upper layer
Common layered models include osi reference model, TCP/IP model and corresponding protocols as follows (reference)
Note that the implementation details between layers are imperceptive, and each layer only needs to use the service interfaces provided by other layers to complete its own tasks and provide services externally. Other than that you can not care about.
Osi reference model is proposed by international Organization for Standardization (ISO), which is divided into seven layers: application layer, presentation layer, session layer, transport layer, network layer, data link layer and physical layer. Concrete implementation is relatively inefficient. The model is too idealized, so it is difficult and inefficient to implement. TCP/IP model is ARPA’s description of existing protocols, so it has become a de facto standard, which is divided into four layers: application layer, transmission layer, Internet layer and link layer. The description of link layer is too general. Because of the problems with both, the improved five-tier TCP/IP model that we are most exposed to today (most of the diagrams below are from MicrochipDeveloper) was created.
Five layer model each layer function
- The application layer is where applications that need to communicate, such as Web browsers, live and use the transport layer to send requests to remotely connect to hosts
- The transport layer uses TCP reliable connections and UDP fast connections to establish connections between applications on different Hosts. You can track the processes running on your application by using the port number. Use the network layer to access a network
- The network layer is responsible for creating cross-network packets and determining the source and destination addresses of a packet using IP addresses
- The data link layer is responsible for creating frames across the network that encapsulate packets and determine source and destination addresses using MAC addresses
- The physical layer is responsible for encoding and decoding the bits in the frame stream
Each layer adds a header, which contains the information needed for the change. The process starts with the application layer. Let’s take a web page as an example:
- Application Layer The application layer produces a message that asks the browser to make a request to download a Web page, which is then sent to the transport layer.
- The transport layer adds a TCP or UDP hader that contains the port address of the data source and destination address. Additional information, such as the packet serial number used by TCP, is also added to the header. If TCP is used, a Segment is generated, and if UDP is used, Datagram is generated, which is then sent to the network layer.
- The network layer adds the header containing the IP address of the source and destination, generates a packet, and sends it to the data link layer
- The data link layer adds the header containing the current and next hop MAC addresses, generates a frame, and sends the bit to the physical layer
The application layer
Contains protocols directly used by various applications. Users can use the software provided by the corresponding application to send messages. Common protocols include
DHCP
When the client detects a network, it automatically requests an IP address from the server, which usually runs in a router
DNS
You can obtain the IP address by domain name. A network needs IP (not domain or host) to generate a packet
HTTP
Is the most common protocol, discussed in detail in the next article,
NBNS
NetBios Name Service translates a local host name into a local IP address, similar to DNS.
SMTP
Used to send emails
Telnet
Bi-directional text communication using a terminal
The transport layer
The data is retrieved from the application layer, and a port number is added to each running process and TCP or UDP headers are added to the retrieved data.
Only hosts have transport and application layers. For example, routers have the following three layers. Function as follows
- Provides communication between processes, that is, end-to-end communication (the corresponding network layer provides point-to-point communication between hosts)
- Reuse and division, is for the application layer, the sender of the transmission layer no matter what application data will be uniformly transmitted to the next layer, this is multiplexing; The receiver’s transport layer then strips the header and sends the message to each process
- Error detection of packets (headers and data) (IP only detects IP headers)
- TCP and UDP are supported
Note that IP transport is unreliable. If TCP is used to ensure reliable communication at the current layer, UDP is not
addressing
The port number
The transport layer uses a port number for each process, which is equivalent to an IP address at the network layer and a MAC address at the link layer. Port number Length: 16 bits, including 65536 different port numbers (that is, 2^16). There are two types of port numbers
- There are two types of port numbers used by the server
- 0 to 1023 are designated as critical applications, such as
- 1024 to 49151, used by other servers
- The port number used by the client, which is dynamically selected only when the client is running, is also known as a temporary port
The socket
To mark a communication endpoint with a socket equal to “IP + port number” in network communications.
UDP protocol.
The User Datagram Protocol (UDP) adds only two features to IP datagrams: reuse and error checking. It has the following advantages
- No connection required, unreliable but fast
- It can be broadcast or multicast
- The connectionless state needs to be maintained and will not be blocked
- Low header overhead, 8B(TCP requires 20B)
UDP packets are processed in the unit of packets. The packets obtained from the upper layer are not merged or split. Only the UDP header is processed and transmitted to the next layer. The link layer has a Maximum Transmission Unit (MTU) limit on the length of data frames. If the data at the network layer exceeds this limit, the data must be fragmented and transmitted to the link layer, such as udp data. When TCP is used, TCP has a limit on THE MSS (Maxitum Segment Size). Generally, THE MSS is smaller than the MTU. Therefore, IP slice is not required after TCP slice.
The message format is as followsrespectively
- Source Port Source port
- Destination Port Destination port
- Length Indicates the total length of a UDP datagram. The minimum length is 8B of the header
- Header and Data checksun Checksun is used to check whether the datagram has errors
TCP protocol
Transfer Control Protocol (TCP) provides reliable transmission, traffic Control, and congestion Control
- Connection-oriented, reliable but slow
- One-to-one communication only
- Provides full duplex communication
The minimum length of the TCP packet header is 20B, including:
respectively
- Source Port Source Port
- Destination Port Destination Port
- Sequence Number (SEQ in connection management) Sequence Number, the Sequence Number of the first byte sent in the packet
- This Acknowledgment Number (ack in connection management) Acknowledgment Number, the Number of the first byte expected to be received next time
- Data Offset Indicates the length of the header
- Reserved Reserved field for future use
- Flags various flags, 0 or 1, where URG is urgent, ACK confirms when establishing a connection, PSH(push) delivers to the application as soon as possible without waiting for the cache to fill, RST(reset) reestablishes a connection, SYN indicates a connection request when connecting, and FIN (Finish) disconnects when the connection is disconnected
- Window Indicates the amount of data allowed to be sent by the peer
- Checksum check and
- Urgent Pointer is used in conjunction with URG to indicate the first byte of Urgent data
- Options currently has only one MSS for sharding.
- Padding Padding fields so that the entire header is a multiple of 4B
Connection management
TCP is a connection-oriented protocol. Each connection consists of three steps: establishing a connection, transmitting data, and releasing the connection. The connection process consists of three steps: three-way handshake
- The client sends a request packet with the SYN set to 1 and a random sequence number, seq= X, which carries no data but consumes a sequence number
- After receiving the request packet, if the server agrees to establish a connection, the server performs necessary Settings and sends an acknowledgement packet. Both SYN and ACK are set to 1, the random sequence number is seq= Y, and the acknowledgement number is the sequence number +1 of the request packet, that is, ACK = X +1. No data is carried, but a sequence number is consumed
- After receiving the acknowledgement packet, the client performs necessary Settings and confirms to the server that the ACK value is 1, seq= X +1, ACK = Y +1. It can carry data. If it does not carry data, the serial number is not consumed
Is full-duplex communication connection is established, the two sides can send data at any time, the other is the allocation of resources in the second and third handshake, so vulnerable to server the SYN flood attack, which disappeared after client has launched the first shake hands, the server will be constantly launch a second handshake, consumes server resources. The serial numbers for connections should be random, not identical, so that new connections can be created each time. A three-way handshake error occurs in case a connection request segment that has expired is suddenly sent to the server
Connection release
Any party involved in the connection can terminate the connection in four steps, namely four waves
- The client sends a release request when it intends to close the connection. FIN is set to 1 and SEQ is the sequence number of the last byte of transmitted data +1(suppose u). Consumes a sequence number and terminates sending data, turning off the sending function in the TCP connection while still receiving
- After receiving the release request packet, the server sends an ACKNOWLEDGEMENT packet with ACK value 1 (ACK = U +1) and SEQ (V). The one-way connection is closed at this point
At this point, the server can continue to send data until the end of the transmission, and then initiate a connection request in another direction 3. Set FIN to 1, ACK to 1, and SEQ to the calculated value. If no data is sent after the second wave, the value is V +1, denoted as W, ACK = U +1, and release request packet 4 is sent. Seq =u+1, ACK =w+1, etc. 2msl(Maximum Segment Lifetime)
Wait 2sml for the fourth wave to ensure that the server receives the acknowledgement packet. If the server does not receive the acknowledgement packet, the server will initiate the third wave after the timeout.
The network layer
When sending data, the layer adds a header containing the source and destination IP addresses and sends it to the data link layer. When data is received, for host, it is sent to the transport layer if the destination IP of the received data is the local address. The router selects a route based on the IP address and sends the route to the destination host or router. The router obtains the corresponding MAC address through ARP.
function
The functions of the network layer include heterogeneous network interconnection, routing and forwarding, and congestion control.
Network interconnection
The network layer, the data link layer and the physical layer are responsible for the actual communication of the upper layer data. The network layer is responsible for the exchange among multiple networks (whether the network is in one network according to the subnet mask), the data link layer is responsible for the data exchange within a network, and the physical layer is responsible for the actual bit stream transmission. There are many intermediate devices in the whole network
- Physical layer: Repeaters, Hubs are used to extend the transmission distance of signals
- Data link layer: Bridges, switches, hosts that connect to the same network
- Network layer: The Router connects different networks. The WAN and LAN represent the wide area network (WAN) and local area network (LAN) respectively, which are described in details in the data link layer
- Above the network layer: Gateway Refer to Hypertext Transfer Protocol for terms such as Gateway, proxy, and tunnel
Routing and Forwarding
, respectively,
- Routing tables are constructed, updated and maintained according to specific routing protocols
- Packet forwarding Forwarding table queries and forwards data streams
Congestion control
The phenomenon of network performance degradation caused by excessive grouping is called congestion. The control method is divided into preventive open-loop control and dynamic closed-loop control
IPV4
It is the IP version commonly used today
packet
The basic unit of IP transmission is packet. The header format is as follows. We focus on the source address and destination address represented by IPBecause the data link layer has a maximum transmission unit (MTU) limit, IP packets need to be fragmented below this limit
IPv4 address
An IP address is a 32-bit unique global identifier. It is divided into five types: ABCDE. Each IP address is divided into a host ID and a network ID.
NAT
Network Address Translation (NAT) translates an internal Network address into a public Network address, facilitating internal management and solving the problem of INSUFFICIENT IP addresses.
For network security, some IP addresses are classified as private IP addresses that can only be used by the LAN
Subnet division and subnet mask, CIDR
Two-tier IP addresses make the UTILIZATION of IP addresses low and inflexible. Therefore, the subnet NUMBER field is introduced, which is called subnet division. Therefore, an IP address is divided into three parts: network NUMBER, subnet number, and host number. In order to show how subnets are divided, we need to use a subnet mask, and we can see how the network is divided at three levels of IP. A subnet mask is a binary with the same length as an IP address, consisting of a string of 1s followed by zeros, where 1 corresponds to the network number and subnet number, and 0 represents the host number. Align an IP address with each bit of the corresponding subnet mask and perform operations respectively to obtain the network address of the corresponding subnet, for example
- IP 192.168.5.56
- The subnet mask is 255.255.255.0
- Network address 192.168.5.0
Classless inter-domain Routing (CIDR) further divides networks on the basis of ABC. This division divides IP into two parts: network prefix and host number, which are represented by CIDR, that is, the number of bits occupied by IP address/network prefix. Where the network prefix corresponds to the network number, such as the number of IP addresses represented by a CIDR tag, and the starting IP is
ARP,DHCP,ICMP
A MAC address is a concept at the data link layer and represents a physical address, which is described in the next section
When a router receives a packet, it obtains the IP address of the next router or host based on the destination IP address. Specific forwarding is implemented based on the MAC address. The source AND destination IP addresses remain the same each time, but the source AND destination MAC addresses vary with the actual source and destination IP addresses. The Address Resolution Protocol (ARP) is used to translate IP addresses to MAC addresses.Dynamic Host Configuration Protocol (DHCP) Is used by a router to dynamically generate an IP address within a subnet
Internet Control Message Protocol (ICMP) can be used by hosts or routers to report errors and exceptions. The two applications are ping and Traceroute, which are used to verify the connectivity of two hosts and trace the routes that packets pass through.
IPV6
The latest version of IP addresses addresses IP address exhaustion and other problems of IPV4. For details, see ipv6
Data link layer
The data link layer is used to transfer information among devices within a single network.
When sending data, encapsulate the header containing the source and destination MAC addresses into the packet received from the network layer, create a frame and send it to the physical layer. When receiving data, the host sends it to the network layer for further processing if the data contains the MAC address of the host.
For relay device (such as Bridges and switches), each port of these devices is a collision domain (collision domain, also called a network segment, each collision domain host thread or the physical relay equipment connection), a collision domain can be composed of multiple devices are linked together, they can communicate directly without the need for a forward with the aid of data link layer device. So if the received source MAC and destination MAC are in the same collision domain, they are discarded directly (because they can communicate directly), otherwise they are forwarded to the destination collision domain (another port on the relay device).
function
There are three types of services for the network layer
- No confirmed connectionless service
- There are confirmed connectionless services
- There are confirmed connection-oriented services
Link management
Manage the establishment, connection, and release of links for connection-oriented services
framing
After receiving data at the network layer, it is encapsulated into frames according to certain rules to solve problems such as frame demarcation, frame synchronization and transparent transmission. In the process of framing, the header and tail of the received data are added respectively, which can be used for frame demarcation and frame synchronization (indicating the start and end of the frame). In the data, the same bit combination with the frame boundary may be generated. In the process of framing, it is necessary to ensure that whatever bit combination exists can be transmitted at the link layer. This is the problem of transparent transmission
Flow control
Solve the problem of inconsistent speed between receiver and receiver
Error control
Due to problems such as channel noise, the frame may make errors during transmission, including dislocation and frame error. A dislocation is an error in some bits that requires automatic retransmission of the wrong frame. Frame error refers to the loss, repetition, or disorder of frames. Timer and numbering mechanisms are introduced in the data link layer to ensure correct transmission.
Media access control
The solution is to deal with the channel contention. IEEE802 standards divide the data Link layer into two sub-layers (Logical Link Control (LLC) and Media Access Control (MAC)).Figure source), where the MAC sub-layer is responsible for media access control.
When multiple devices share a channel, there are the following processing methods
Channel division
An original channel can be divided into several non-interfering sub-channels through channel division, and related methods include
- Frequency division multichannel FDM
- Time division multiplex TDM
- Wavedivision multichannel WDM
- Code division multiple CDM, such as CDMA is a specific one, namely code division multiple access
Random access
It does not adopt centralized control of the channel, but adopts random sending information. When multiple users send information at the same time, it will fail, and then it will be repeated in accordance with certain rules until no collision is passed, and relevant rules include
- ALOHA
- CSMA
- CSMA/CD
- CSMA/CA
Round-robin access media access control
In this control method, users should not be random, but allocate the communication right of each node in a circular way. When the channel is occupied, other nodes cannot send messages, such as token passing protocol
Local area network (LAN)
Local area Network (LAN) is a computer network in a small geographical area. The technical characteristics of a LAN are determined by three elements: topology, transmission medium and media access control mode. There are four types of common LAN topologies
- star
- ring
- bus
- Star and bus combined
Transmission media include twisted pair wire, copper wire and optical fiber, mainly twisted pair ring access control methods mainly include CSMA/CD, token bus and token ring, among which the first two are used for total linear, the last one for ring
The three special LAN topologies are
- Ethernet, logical topology bus, physical topology star or extended star
- Token ring (IEEE802.5), logical ring, physical star
- FDDI(IEEE802.8), logical ring, physical double ring
IEEE802 defines the data link layer and physical layer for the osi reference model of LAN, and divides the data link layer into two sub-layers, LLC and MAC. Because of its monopoly in the market, Ethernet is often used to represent a local area network (LAN), where the LLC layer is less useful than Ethernet, so many network cards only have the MAC layer.
Ethernet and IEEE802.3
There are minor differences between the standards of Ethernet and iEEE802.3, so iEEE802.3 lans can be called Ethernet. It logically uses a bus topology, all computers share a bus, information is sent in broadcast mode, using CSMA/CD media control
IEEE802.11
It’s a wireless LAN protocol
IEEE802.5
Token ring network
wan
Wide Area Network (WAN) is a long-distance network with a Wide range, which is the core part of Internet. The differences with LAN are as follows
The physical layer
The bit stream in the frame is transmitted by cable or wirelessly, and the relay devices in this layer (repeater and hub) are only used to extend the line.
The basic concept
Here are some concepts related to communication
- Data is an entity that transmits information. It is divided into digital data and analog data, and is represented in binary in stored procedures
- The form of signal data during transmission is divided into analog signal and digital signal
- The source, channel, and destination tables show where data is generated, sent, and received
- Encoded data for transmission is converted into signals, which are analog signals called modulation and digital signals called encoding
- Circuit switching, message switching and packet switching are three ways of data transmission
Transmission medium
It is classified into wired and wireless. For other information, see related introduction of data link layer
Scatter flowers at the end of this period