An Overview of Computer Networks

foreword

When doing upper-layer application development, we can often call various network frameworks to realize the functions of network requests and data delivery. It is true that the network framework can simplify the structure of the code or reduce the amount of unnecessary code, but it is also easy to cause us to become unfamiliar with the underlying network principles. Familiarity with the principles of computer networks is the only way for every programmer to become a senior or even senior engineer. This article is an in-depth analysis and summary of computer networks.

The OSI seven-layer model is a relatively authoritative standard model, but it is rarely used because of its complex layering and impracticality. The TCP/IP four-layer model is a widely used model. In order to better grasp the knowledge of network layering, these two protocols are generally combined to form a five-layer protocol for learning.

The role of each layer of the network model and the protocol transmission unit of each layer

OSI seven-layer model:

  • Application layer: Network applications are implemented through the interaction between application processes, and the protocol transmission unit is the application protocol data unit (APDU, Application Protocol Data Unit).
  • Presentation layer: data format conversion, the protocol transmission unit is the presentation protocol data unit (PPDU, Presentation Protocol Data Unit).
  • Session layer: Realize session management in the process of data synchronization, and the protocol transmission unit is the session protocol data unit (SPDU, Session Protocol Data Unit).
  • Transport layer: realizes data transmission between ports, and the protocol transmission unit is a segment (Segment).
  • Network layer: realize the communication between the host and the host, and the protocol transmission unit is the data packet (Package).
  • Data link layer: The data packets sent by the network layer are packaged into frames to realize transmission between adjacent nodes of the link. The protocol transmission unit is a frame (Frame).
  • Physical layer: The bit stream is transmitted on the physical connection, and the protocol transmission unit is bit (Bit).

TCP/IP four-layer model:

  • application layer
  • transport layer
  • internet layer
  • network interface layer

Five-layer model:

  • application layer
  • transport layer
  • Network layer
  • data link layer
  • physical layer

Main Protocols in Different Layers

  • application layer

insert image description here

  • transport layer
    insert image description here

  • Network layer
    insert image description here

  • Data link layer
    PPP protocol (point-to-point protocol, a protocol designed for transmitting data packets between peer units)
    MAC protocol (media access control protocol)
    VLAN protocol (virtual local area network protocol)

  • Physical layer
    RJ45 (an interface protocol)
    IEEE802.2 (protocol defines transmission rate, working frequency band, etc.)

Introduction to important agreements

The HTTPS and HTTP protocols are very important protocols in computer networks. The previous article analyzed HTTP and HTTPS protocols in detail and introduced them in detail.

How does DNS find the corresponding IP address through the domain name?

  • Check whether there is an IP corresponding to the domain name in the local hosts file, and return it directly if there is one. (file cache)
  • Check the local DNS cache and return if there is one. (local system cache)
  • Connect to the network and query the DNS server cache. (The first time you visit a domain name, there must be no local cache, and you will definitely go to the dns server)
  • DNS server lookup recursively.

The detailed process of recursive search:

  • Ask for the root domain name (.com. with a dot at the end), get the top-level domain name ( .com )
  • Inquire about the top-level domain name and obtain the second-level domain name (.baidu.com)
  • Inquire about the second-level domain name and obtain the third-level domain name (.baijiahao.baidu.com)
  • Finally, return the ip address of .baijiahao.baidu.com to the user and cache it.

ARP execution process

  • Each host has an ARP list, which stores the Mac address corresponding to the IP address.
  • When the source host wants to send data, first judge whether there is a Mac address corresponding to the IP address in the ARP list, and if so, send the data directly. If not, the source host broadcasts to all hosts in this network segment. The broadcast data packet information includes: source host IP address, source host Mac address, and destination host IP address.
  • After receiving the broadcast, other hosts will first judge whether the target IP address of the source host matches its own IP address. If it doesn't match, ignore it. If they match, it will first update the IP address and Mac address of the source host to its own ARP list, and then send a unicast to inform the source host of the Mac address corresponding to the target IP address.
  • After the source host receives the unicast, it will update the Mac address to the ARP list, and then use the Mac address to send data. If the source host has not received a response, it means that the ARP query failed.

UDP

insert image description here

TCP

TCP segment structure
insert image description here

  • Source port: 2 bytes, the port number of the application sending the TCP connection request
  • Destination port: 2 bytes, the port number of the application receiving the TCP connection request
  • Serial number: 4 bytes, refers to the serial number. Because TCP is byte-oriented, the sequence number is the sequence number of each byte in the byte stream transmitted by the TCP connection. For example, the value of the sequence number field in the message segment is 1, and the data carried is 100 bytes in total, which means that the sequence number of the first byte of the message segment is 1, and the sequence number of the last byte is 100. The sequence number of the next segment starts from 101.
  • Acknowledgment number: The sequence number of the first data byte expected to be received in the next segment. Take the example of the sequence number above as an example. When B receives the message segment sent by A (the field value of the sequence number in the message segment is 1, and the data carried is 100 bytes in total), then the confirmation message segment sent by B to A The number is 101.
  • Data offset: occupies 4 bits, the maximum value is 60 bytes, which refers to the distance between the beginning of the TCP segment data and the beginning of the TCP, that is, the length of the header.
  • Reserved: 6 digits, reserved for future use.
  • Confirmation ACK: ACK is set to 1, and the confirmation number field is valid. ACK is 0, and the acknowledgment number field is invalid.
  • Synchronous SYN: Used to synchronize the serial number when the connection is established. When SYN=1 and ACK=0, it means to request to establish a connection; when SYN=1 and ACK=1, it means that the other party agrees to establish a connection.
  • Terminate FIN: used to release a connection.
  • Window: occupies 2 bytes, refers to the receiving window of the party sending this message segment, and its function is to tell the other party the amount of data that the receiving party currently allows the sender to send from the confirmation number of this message segment.
  • Urgent URG: URG is set to 1, indicating that there is urgent data in the segment, which needs to be processed first by the system.
  • Push PSH: The sender TCP sets PSH to 1, and immediately sends the segment. After receiving the segment with PSH=1, the receiver will reply as soon as possible, instead of waiting for the cache to fill up before replying.
  • Reset RST: When RST is 1, it indicates that there is a serious error in the TCP connection, and the connection needs to be released and re-established. It can also be used to reject illegal segments.
  • Checksum: Occupying 2 bytes, the function is to discover the changes of the header and data from the sending end to the receiving end.
  • Urgent pointer: occupying 2 bytes, indicating the position of the end of the urgent data in the message segment (since the urgent data is followed by ordinary data, the urgent pointer also indicates the number of bytes of the urgent data).
  • Option: Function: TCP is designed to adapt to the complex network environment and better serve the application layer, such as setting the maximum segment length. Variable length, up to 40 bytes
  • Padding: Make the TCP header length an integer multiple of 4 bytes.

MSS

  • Meaning: The maximum segment length refers to the maximum value of the data field in a TCP segment.
  • Function: Improve transmission efficiency. The MSS is low, for example, only 1 byte, while the header length of the network layer is at least 40 bytes, and the utilization rate is only 1/41, which is very low. If the MSS is very large, it will need to be fragmented at the network layer, and the overhead will also increase. Therefore, the MSS should be as large as possible, as long as it is not fragmented at the network layer.

TCP three-way handshake

  • The first handshake: the client sends a data packet requesting a connection to the server, waiting for the server to confirm;
  • The second handshake: the server receives the connection request from the client, but the server must also confirm whether the client can connect, so the server also sends a data packet requesting a connection to the client, waiting for the client's confirmation;
  • The third handshake: the client receives the data packet from the server and sends a confirmation packet to the server, and the two parties establish a connection.

TCP waved four times

  • The first wave: the client sends the completed data packet to the server, and the client enters the state of waiting for the end;
  • The second wave: the server receives the end data packet from the client, then sends a confirmation data packet, and enters the waiting state for closing;
  • The third wave: the server sends the end data packet to the client, and enters the final confirmation state;
  • The fourth wave: the client receives the end data packet and enters the end state. Before the end, it sends a confirmation data packet to the server. After receiving it, the server also enters the end state and completes four waves.

Three-way handshake reason

  • Guaranteed reliable data transmission.
  • If only one handshake is performed, after the client sends a connection request, it does not receive a response from the server, so it is impossible to determine whether the connection is successful.
  • If only two handshakes are performed, the server cannot know whether the client is connected successfully. To give an abnormal scenario, after the client sends a connection request, it will wait for the response from the server. If the client's SYN has not arrived at the server for a long time (the first time), at this time, after the client times out (the server has not received the confirmation connection packet for a long time), it will resend the connection again. The server receives it, and responds to the client, and the connection is established. But after the establishment, the first long-overdue SYN also arrives at the server. At this time, the server will think that this is a new connection (because there are only two handshakes, and the server does not know that the client has successfully connected), and will try again. Send an ACK to the client, which will of course be discarded by the client. But at this time, the server has already allocated resources for this connection, and the server will always maintain this resource, which will cause waste.

reason for four waves

After the server receives the end data packet from the client, the server may have unfinished operations and needs to end its own operations first, so there will be four waved hands.

flow control

When the data buffer at the receiving end is full and cannot process the data, it will send a "no longer receive" signal, and the sending end will stop sending until the sending end receives the "can continue sending" signal before sending data.

congestion control

  • Use the congestion window cwnd to control the sending window size;
  • If the packet times out, it is considered to be congested; otherwise, it is considered that the network is not congested when the confirmation is received;
  • Congestion is less (cwnd decreases), non-congested is more (cwnd increases);
  • When the network is unknown, cwnd starts from the minimum, and cwnd gradually increases after receiving the confirmation;
  • In order to improve efficiency, the growth rate is fast at the beginning, and the growth rate slows down after a certain stage.

fast retransmit

  • After the sender sends 4 data packets in succession——1, 2, 3, and 4, it receives confirmation data packets of 2, 3, and 4, but does not receive the confirmation data packet of 1.
  • The sender then sends a data packet of 1 to the server, without waiting for the confirmation data packet of 1 to arrive (shorter than the time set by the timer).

How to use TCP to achieve security and reliability

  • Timeout retransmission: After TCP sends a message segment, it will start a timer to confirm the timing of the confirmation information sent back by the destination, and retransmit after timeout.
  • Acknowledgment signal: When TCP receives a segment from TCP, it will send back an acknowledgment signal.
  • Checksum: TCP will always maintain the checksum of the header and data. If there is an error in the checksum of the received message segment, it will be discarded, hoping that the sender will retransmit after a timeout.
  • Reordering: Since the arrival of IP datagrams may be out of order, TCP will reorder the data and deliver it to the application layer in the correct order.
  • Discard duplicates: Since IP datagrams may be duplicated, TCP will discard duplicate data.
  • flow control

The difference between TCP-oriented byte stream and UDP packet-oriented

  • When sending data under TCP, the sender can send data multiple times in segments, and the receiver can receive data multiple times.

  • When UDP sends data, the sending end will send the data at one time, and the receiving end will receive it at one time.

    This is the working characteristic of TCP and UDP.

The execution process of the system after entering the domain name

  • DNS resolves domain names to IP addresses.
  • Obtain the MAC physical address corresponding to the IP address through ARP.
  • TCP establishes a connection through a three-way handshake.
  • After the connection is established, an HTTP request is sent to obtain the data returned by the server.
  • Disconnect with four hand waves.

Welcome to pay attention to the official account Duxiong Jun, share and communicate together.

Guess you like

Origin blog.csdn.net/qq_40796375/article/details/125029055