The OSI seven-layer model

OSI model is a layered design concept that divides a complex network communication model into seven layers, with each layer receiving specific services from the next layer and providing specific services from the next layer. Each layer of the sender will add header information to the data, and each layer of the receiver will verify and dismantle the header information to obtain data. The responsibilities of each layer are clear.

Application layer:

The application program runs on the application layer, and the communication between different application programs between hosts is carried out by application process, and the communication of application process is completed by exchanging “messages” on the application layer of two end systems. Packets are sent from the application layer to the operating system kernel through the socket socket, and then to the peer application layer through the socket socket. Therefore, the application layer is oriented to specific applications to carry out network transmission.

The presentation layer:

In the process of host application interaction, different models and software will inevitably lead to different forms of data expression. The presentation layer translates the sender’s specific data format into a “network common data format”, and then the receiver can successfully receive the data and convert it into the data format it needs.

The session layer:

Determines which connections are used for data transfer, when connections are established and disconnected, the order of data transfer, and also the establishment of checkpoints and data recovery capabilities. The layers below the session layer are the ones that actually transfer the data.

Transport layer:

The transport layer extends the IP interaction between two end systems to the application process interaction between the IP on the two end systems. In the Internet, the sender receives the “message” from the application layer, and the transport layer generates a “message segment” at the head and sends it to the network layer. The receiving end receives the packet segment from the network layer, disassembs the destination port number in the packet segment, directs the destination port number to the corresponding process socket, and delivers the data to the application process running on the host to complete the logical communication between different host processes.

Network layer:

The network layer provides logical communication between different hosts. In the Internet, the sender according to the target IP address to determine the position of the target host in the network, and then send to undertake transport layer segments, add IP encapsulation head into “data”, and then to the neighboring router sends a datagram (bottom after a switch), of course, after each router and local area network (LAN), finally arrived at the receiving end. The receiving end obtains the datagram from the router, verifies that the IP address passes, extracts the packet segment of the transport layer and forwards it to the transport layer.

Data link layer:

Data is sent to the peer host based on the MAC address of the target, and data is processed on devices connected through physical media. In the Internet, the network layer of the datagram to move to the data link layer, data link layer to byte stream packets encapsulated into bits “frame”, and then based on the MAC address of the target host, along the link between the end-to-end, through switches pass the datagram to the next node (host or router), the next node data link layer in the paper the data to the network layer.

The physical layer:

The binary bit data is converted into voltage and optical signals and transmitted to the peer physical layer through the communication media.

Why the TCP/IP four-tier model?

The Internet simplified the OSI seven-tier model to four tiers in order to make TCP/IP protocol clusters more flexible. We leave the presentation layer, data format transformation and data transfer connection management in the session layer to the developer, giving them the flexibility to switch according to the business scenario. Less important services, for example, do not use connection management.

What happens when you enter an address in the browser address bar?

① After you enter the domain name, the browser performs DNS resolution on the domain name to obtain the destination IP address

Turn the DNS

② Check whether the request is cached in the CDN. If yes, return the request directly.

(3) The HTTPS request is used for TLS handshake, and subsequent data transmission is ensured by the session key.

Turn the HTTPS

(4) Add HTTP headers to the message body to form HTTP packets, and then send the packets to the transport layer of the operating system through sockets.

⑤ The transport layer generally uses TCP. The transport layer checks whether a long connection has been established with the destination IP address and destination port. If not, the transport layer establishes a TCP connection through three-way handshake. Then, the source port and destination port (URL input) are assembled into a TCP header, which is then spliced into HTTP request packets to generate TCP packet segments and sent to the network layer of the operating system.

⑥ The network layer protocol is mainly IP protocol. The network layer assembles the source IP address and target IP address (cached in the operating system after domain name resolution) into an IP header, splices the IP header into a TCP packet segment, generates a datagram, and sends the packet to the MAC layer of the operating system.

⑦ The MAC layer adds the MAC header for the datagram, and then broadcasts its OWN IP address through the APR protocol to get the MAC address of the network adapter (MAC network adapter is the first hop router), and then sends the datagram to the MAC network adapter through the switch. The MAC network adapter gets the datagram with the target IP address, hops between lans and routers through OSPF, and finally reaches the last-hop router, which is followed by the LAN where the target machine is located. Then the router obtains the MAC address of the destination server according to the ARP protocol, and the network packet reaches the destination server through the switch according to the MAC address.

8 The target server receives the packet.

  • The data link layer of the operating system first verifies that the MAC header is correct, retrieves the MAC header, and sends the network packet to the network layer of the operating system. The network layer verifies that the IP header is correct, removes the IP header and sends the network packet to the transport layer. After receiving the packet, the transport layer sends an ACK packet to the source machine to ensure reliable transmission of the TCP connection. Then remove the TCP header of the packet, get the port number 80, and find the process that is listening on this port.

  • The Nginx proxy server listens for the request and forwards the request to the corresponding host.

  • Typically, the Tomcat application server receives the request and hands it off to an internal thread pool for processing. The Tomcat thread parses the Request packets based on HTTP and encapsulates the result into a Request object. The Request object is then processed by multiple nodes in the responsibility chain, and the last node in the responsibility chain generates a Servlet instance and encapsulates the Request object. The filterChain filter chain is then triggered, all filters are fetched from the Servlet context, the servletName is matched with the filter and placed into the filterChain. Each filter in the filterChain is then executed to read the request body of the parse servlet instance. After completion, the servlet instance’s service() method is called, doGet() and doPost() are selected according to the request method type to perform business logic processing, and the response object is generated.

⑨ The response object is returned from the original Tomcat route to the source host, and the corresponding page is rendered.