Interview Recitation Edition - Computer Network

computer network interview surprise

Part 1: Protocol layers and their service types

1. OSI seven-layer model

Interview high-frequency index: ★★★★★
insert image description here
① Application layer

The role of the application layer is to complete specific network functions through the interaction between applications. This layer protocol defines the interaction rules between application processes, and provides services for different network applications through different application layer protocols. For example, domain name system DNS, HTTP protocol supporting World Wide Web applications, SMTP protocol adopted by e-mail system, etc. The data unit exchanged at the application layer is called a message.
Protocols : HTTP, TFTP, FTP, NFS, WAIS, SMTP, Telnet, DNS, SNMP

② presentation layer

The role of the presentation layer is to enable the communicating application to interpret the meaning of the exchanged data. It is located in the sixth layer of the OSI reference model, providing services for the application layer upwards and receiving services from the session layer downwards. The services provided by this layer mainly include data compression, data encryption and data description. This frees applications from having to worry about differences in internal formats that are represented and stored across computers.
Protocol : TIFF, GIF, JPEG, PICT

③ session layer

The session layer is responsible for establishing, managing and terminating communication sessions between presentation layer entities. This layer provides demarcation and synchronization functions for data exchange, including methods for establishing checkpoints and recovery schemes.
Protocols : RPC, SQL, NFS, NetBIOS, names, AppleTalk

④ Transport layer

The main task of the transport layer is to provide services for communication between two host processes. Applications use this service to transmit application layer messages.
Protocol : TCP, UDP

⑤ Network layer

When data is transmitted between two computers, there is often more than one communication link, and the transmitted information may even pass through many communication subnets. The main task of the network layer is to select the appropriate inter-network routing and switching nodes to ensure the successful transmission of data. When sending data, the network layer encapsulates the message or user datagram generated by the transport layer into groups and packets and transmits them down to the data link layer. The protocol used in the network layer is the connectionless Internet Protocol (Internet Protocol) and many routing protocols, so we usually refer to this layer simply as the IP layer.
Protocols : IP, ICMP, ARP, RARP, RIP, IPX

⑥ Data Link Layer

The data link layer is between the physical layer and the network layer. The data transmission between two hosts is always transmitted on a link of a link, which requires the use of a special link layer protocol. When transmitting data between two adjacent nodes, the data link layer assembles the IP datagram delivered by the network layer into a frame, and transmits the frame on the link between two adjacent nodes. Each frame includes data and necessary control information. Through the control information, we can know the start and end bit position of a frame. In addition, it can also enable the receiving end to detect whether the received frame has any errors. If an error is found, the data link layer can simply discard the frame to avoid continuing to occupy Internet resources.
Protocols : FDDI, Frame Relay, HDLC, SLIP, PPP

⑦ Physical layer

The role of the physical layer is to realize the transparent transmission of bit streams between computer nodes, shielding the differences in specific transmission media and physical devices as much as possible. So that the data link layer above does not have to consider what the specific transmission medium of the network is. The main task of this layer is to determine some characteristics of the interface with the transmission medium (mechanical characteristics, electrical characteristics, functional characteristics, process characteristics).
Protocol : EIA/TIA-232, EIA/TIA-499, V.35, 802.3

2. TCP/IP Reference Model

Interview high frequency index: ★★★★★

​ The starting point when the OSI seven-layer model was proposed was based on standardization considerations, without considering specific market needs, making the model complex in structure and redundant in some functions, so there are not many systems that fully implement the OSI reference model. The TCP/IP reference model is directly oriented to market demand and is relatively easy to implement, so it has been widely used once it was proposed. The TCP/IP-based reference model divides the protocol into four layers, as shown in the figure above, which are: network access layer, Internet interconnection layer, transport layer, and application layer.

① Application layer

The TCP/IP model combines the functions of the session layer, presentation layer, and application layer in the OSI reference model into one application layer, and provides services for different applications through different application layer protocols. For example: FTP, Telnet, DNS, SMTP, etc.

② Transport layer

This layer corresponds to the transport layer of the OSI reference model, and provides the communication function from the source end to the peer host for the upper layer entity. The transport layer defines two main protocols: Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). Among them, the connection-oriented TCP protocol ensures the reliability of data transmission, and the connectionless UDP protocol can realize the simple and fast transmission of data packets.

③ Internet layer

The internetwork layer corresponds to the network layer of the OSI reference model, and is mainly responsible for communication between computers in the same or different networks. At the internetwork layer, the IP protocol provides an unreliable, connectionless datagram delivery service. The protocol implements two basic functions: addressing and segmentation. According to the destination address in the datagram header, the data is transmitted to the destination address, and IP is responsible for selecting the transmission route during this process. In addition to the IP protocol, the other two main protocols at this layer are the Internet Group Management Protocol (IGMP) and the Internet Control Message Protocol (ICMP).

④ Network access layer

The function of the network access layer corresponds to the physical layer and the data link layer in the OSI reference model, and it is responsible for monitoring the exchange of data between the host and the network. In fact, TCP/IP does not really describe the realization of this layer, but each network participating in the interconnection uses its own physical layer and data link layer protocol, and then connects with the network access layer of TCP/IP, so The specific implementation method will vary with different network types.

3. TCP/IP five-layer reference model

Interview high frequency index: ★★★☆☆

The protocol structure of the five-layer system is a protocol that combines the advantages of OSI and TCP/IP, including application layer, transport layer, network layer, data link layer and physical layer . The application layer corresponds to the upper three layers of OSI, and the lower four layers are the same as OSI. The architecture of the five-layer protocol is only designed to introduce the principle of the network, and the actual application is still the TCP/IP four-layer architecture.

4. Correspondence between OSI and TCP/IP protocols

Interview high frequency index: ★★★★☆

OSI seven-layer network model TCP/IP four-layer conceptual model TCP/IP four-layer conceptual model
Application layer (Application) application layer HTTP, TFTP, FTP, NFS, WAIS, SMTP, Telnet, DNS, SNMP
Presentation application layer TIFF, GIF, JPEG, PICT
Session layer (Session) application layer RPC, SQL, NFS, NetBIOS, names, AppleTalk
Transport layer (Transport) transport layer TCP, UDP
Network layer (Network) Network layer IP, ICMP, ARP, RARP, RIP, IPX
Data Link Layer (Data Link) data link layer FDDI, Frame Relay, HDLC, SLIP, PPP
Physical layer data link layer EIA/TIA-232, EIA/TIA-499, V.35, 802.3

5. How data is transmitted between layers [data encapsulation process]

Interview high frequency index: ★★★☆☆

On the sending host side, an application layer message is passed to the transport layer. In the simplest case, the transport layer receives the message and attaches additional information, which will be used by the transport layer at the receiving end. The application layer message and the transport layer header information together constitute the transport layer message segment. Additional information may include: information to allow the receiving transport layer to deliver the message up to the appropriate application, and error detection bit information. This information allows the receiving end to determine whether bits in the message have been changed en route. The transport layer transfers the segment to the network layer, and the network layer adds network layer header information such as source and destination system addresses to generate a network layer datagram. The datagram is then passed to the link layer. After the data link layer data packet is added with the MAC address of the sending end and the MAC address of the receiving end, it is encapsulated into a data frame. The data frame at the physical layer is encapsulated into a bit stream, and then transmitted through The medium is transferred to the peer.

Part II: Application Layer

1. What information does the HTTP header contain

Interview high frequency index: ★★★☆☆

An HTTP header is essentially a key-value pair that conveys additional important information. Mainly divided into: general header, request header, response header and entity header.

Universal header

protocol header illustrate example
Cache-Control Used to specify whether to use the cache mechanism in the current request/reply Cache-Control: no-store
Connection The connection type that the client (browser) wants to use preferentially Connection: keep-alive (Upgrade)
Date message creation time Date: Dec, 26 Dec 2015 17: 30: 00 GMT
Trailer It will realize which header fields are recorded after the message body, which can be used in HTTP/1.1 version chunked transfer encoding Trailer: Expiress
Transfer-Encoding Used to change message format Transfer-Encoding: chunked
Upgrade Ask the server to upgrade to a higher protocol version Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
Via Tell the server which proxy sent the request Via: 1.0 fred, 1.1 itbilu.com.com (Apache/1.1)
Warning A general warning that there may be errors in the entity content Warning: 199 Miscellaneous warning

2. The difference between Keep-Alive and non-Keep-Alive, does it affect server performance?

Interview high frequency index: ★★★☆☆

In the early HTTP/1.0, every time the browser initiates an HTTP request, it needs to create a new TCP connection with the server, and the server immediately disconnects the TCP connection after completing the request processing. The server does not track each client and does not record past requests. However, the process of creating and closing connections consumes resources and time. In order to reduce resource consumption and shorten response time, connections need to be reused. Persistent connections are used by default in HTTP/1.1 versions. The default connections of HTTP versions before this are non-persistent connections. If you want to maintain persistent connections on the old version of the HTTP protocol, you need to specify the value of the header field of the connection Use Keep-Alive to tell the other party not to close the request after the response is completed. We will continue to communicate with this request next time. We use a schematic diagram to show the difference between the two more vividly: for non-Keep=Alive, it must be for
insert image description here
each A requesting object establishes and maintains a brand new connection. For each such connection, both client and server allocate TCP buffers and variables, which imposes a severe burden on the server, because a Web server may serve hundreds of client requests at the same time. In the Keep-Alive mode, the server keeps the TCP connection open after responding, and subsequent request and response messages between the same client and server can be transmitted through the same connection. Even multiple Web pages on the same server can be sent over a single persistent TCP connection when sent from that server to the same client.

However, Keep-Alive is not without disadvantages. Keeping the TCP connection for a long time will easily lead to invalid occupation of system resources. If the Keep-Alive mode is not properly configured, it may cause more losses than the non-Keep-Alive mode. big. Therefore, we need to set the keep-alive timeout parameter correctly. When the TCP connection finishes transmitting the last HTTP response, the connection will keep keepalive_timeout seconds, and then start to close the connection.

3. What is the usage scenario of HTTP long connection and short connection

Interview high frequency index: ★★★☆☆

Long connection: It is mostly used for frequent operations, point-to-point communication, and a small number of client connections. Such as instant messaging, online games, etc.

Short connection: The HTTP service of a Web site with a large number of users generally uses a short connection. For example, large websites such as JD.com and Taobao usually have tens of millions or even hundreds of millions of clients. If long connections are used, a large amount of resources on the server will be ineffectively occupied, so short connections are generally used.

5. What HTTP methods know

Interview high frequency index: ★★★☆☆

HTTP/1.0 defines three request methods: GET, POST and HEAD methods.

HTTP/1.1 adds six request methods: OPTIONS, PUT, PATCH, DELETE, TRACE and CONNECT methods.

method describe
GET Request the specified page information and return specific content, usually only for reading data.
HEAD Similar to a GET request, except that there is no specific content in the returned response, which is used to obtain the header.
POST Submit data to a specified resource to process a request (such as submitting a form or uploading a file). Data is included in the request body. POST requests may result in the creation of new resources or changes to existing resources.
PUT Replace the specified resource, or add it if it doesn't exist.
DELETE Requests the server to delete the resource data identified by the URL.
CONNECT Use the server as a proxy and let the server access instead of the user.
OPTIONS Sending this method to the server will return the HTTP request methods supported by the specified resource.
TRACE Echo the request data received by the server, that is, the server returns the data received by itself, which is mainly used for testing and diagnosis.
PATCH Complementary to the PUT method for partial updates to known resources.

6. The difference between GET and POST

Interview high frequency index: ★★★★★

  • The data submitted by get will be placed after the URL, and the request parameters will be completely kept in the browser's records. Since the parameters are directly exposed in the URL, there may be security issues, so they are often used to obtain resource information. The post parameters are placed in the request body, and the parameters will not be retained. Compared with the get method, the post method is safer and is mainly used to modify resources on the server.
  • Get requests only support URL encoding, and post requests support multiple encoding formats.
  • get only supports parameters in ASCII character format, while the post method has no restrictions.
  • There is a limit to the size of the data submitted by get (the limit mentioned here is for the browser), but there is no limit to the data submitted by the post method.
  • The get method needs to use Request.QueryString to obtain the value of the variable, while the post method uses Request.Form to obtain it.
  • The get method produces one TCP packet, the post method produces two (not all browsers produce two).

7. What is the length limit of GET

Interview high frequency index: ★★★☆☆

The GET method in HTTP transfers data through the URL, and the URL itself does not limit the length of the data. The browser really limits the length of the GET. For example, the IE browser has a maximum limit of more than 2000 characters for the URL, which is about 2KB. About, browsers such as Chrome and FireFox can support more URL characters. Among them, the maximum length of URL in FireFox is limited to 65536 characters, and the maximum length of URL in Chrome browser is limited to 8182 characters. And this length is not only for the data part, but for the entire URL, in which different servers also affect the maximum length limit of the URL. So for specific browsers, the length limit of GET is different.

Since the request parameters of the POST method are in the request body, theoretically speaking, there is no size limit for the post method, but the real limitation is the processing capacity of the server handler.

8. How HTTP and HTTPs work [the process of establishing a connection]

Interview high frequency index: ★★★★☆

HTTP

HTTP (Hyper Text Transfer Protocol: Hypertext Transfer Protocol) is a simple request-response protocol used to pass messages between web browsers and web servers. HTTP uses TCP (rather than UDP) as its backing transport layer protocol. It works on port 80 of the TCP protocol by default. The HTTP client initiates a TCP connection with the server. Once the connection is established, the browser and the server process can access the TCP through the socket interface. The client sends HTTP request messages and receives HTTP response messages from the socket interface. Similarly, the server also receives HTTP request messages and sends HTTP response messages from the socket interface. Its communication content is sent in clear text without any data encryption. When the communication is over, the client and server close the connection.

HTTPS

HTTPS (Hyper Text Transfer Protocol over Secure Socket Layer) is a security-oriented HTTP protocol, which ensures the security of the transmission process through transmission encryption and identity authentication on the basis of HTTP. Its workflow is as follows:

① The client initiates an HTTPS request and connects to port 443 of the server. The information sent mainly includes the algorithm list and key length it supports;

② The server compares all the encryption algorithms it supports with the algorithm list of the client and selects a supported encryption algorithm, and then sends it to the client together with other key components.

③ The server sends a message containing a digital certificate to the client. The digital certificate includes information such as the issuing authority of the certificate, expiration time, and the public key of the server.

④ Finally, the server sends a completion message to inform the client that the first phase of SSL negotiation has been completed.

⑤ After the first SSL negotiation is completed, the client sends a response message, which contains a random password string generated by the client, called pre_master_secre, and the message is encrypted by the public key in the certificate.

⑥ Immediately afterwards, the client will send a message to remind the server that the subsequent messages are encrypted with pre_master_secre.

⑦ The client sends a finish message to the server. This handshake includes the overall check value of all messages since the first handshake. Whether the final negotiation is completed depends on whether the server can successfully decrypt it.

⑧ The server also sends a message with the same function as in step ⑥, which has been confirmed by the client, and finally sends a finish message to tell the client that it can correctly decrypt the message.

After the finish message exchange between the server and the client is completed, the SSL connection is established, and then the communication process is the same as that of HTTP. The only difference is that the HTTP communication process does not use plaintext transmission, but uses symmetric encryption. way, where the symmetric key has been negotiated during the establishment of SSL.

9. The difference between HTTPS and HTTP

Interview high frequency index: ★★★★☆

  • The HTTP protocol sends content in clear text, and the data is unencrypted, which has poor security.
  • The HTTPS data transmission process is encrypted and has better security.
  • HTTP and HTTPS use completely different connection methods and different ports. The former is port 80, and the latter is port 443.
  • The HTTPS protocol needs to apply for a certificate from a digital certification authority (Certificate Authority, CA), which generally requires a certain fee.
  • The HTTP page response is faster than HTTPS, mainly because HTTP uses 3-way handshake to establish a connection, and the client and server need to shake hands 3 times, while HTTPS needs to go through an SSL negotiation process in addition to the 3-way handshake of TCP.

10. HTTPS encryption method

Interview high frequency index: ★★★☆☆

HTTPS adopts a combination of symmetric encryption and asymmetric encryption. First, SSL/TLS protocol is used for encrypted transmission. In order to make up for the shortcomings of asymmetric encryption, HTTPS uses certificates to further strengthen the security of asymmetric encryption. Through asymmetric encryption, customers After the end and the server negotiate a symmetric key for communication and transmission, all subsequent information is encrypted and decrypted by the symmetric key to complete the entire HTTPS process.

11. Why does the client trust the third-party certificate

Interview high frequency index: ★★☆☆☆

Assuming that the middleman tampers with the original text of the certificate, since he does not have the private key of the CA organization, he cannot obtain the encrypted signature at this time, so he cannot tamper with the signature. After the client browser receives the certificate, it will find that the original text and the decrypted value of the signature are inconsistent, indicating that the certificate has been tampered with by an intermediary, and the certificate is not trustworthy, thus stopping the transmission of information to the server.

The above process shows that the certificate cannot be tampered with. Let us consider a more serious situation. For example, the middleman has obtained the certificate certified by the CA organization. It wants to steal the information sent by website A to the client, so it becomes the middleman and intercepts the certificate sent by A to the client, and then Replace it with your own certificate. At this time, the client browser receives the certificate that has been packaged by the middleman, but since the certificate contains the website information requested by the client, the client browser only needs to compare the domain name in the certificate with the domain name requested by itself You will know if the certificate has been dropped.

12. HTTP is a protocol that does not save state, how to save user state

Interview high frequency index: ★★★★☆

We know that if a particular client requests the same object twice in a short period of time, the server will not stop responding because it has just provided the object to the user, but will resend the object, just like The server has completely forgotten what it did not so long ago. Because an HTTP server does not keep any information about the client, we say that HTTP is a stateless protocol.

Usually there are two solutions:

① Session persistence based on Session

After the client sends an HTTP request to the server for the first time, the server will create a Session object and store the client's identity information in the form of a key-value pair, and then assign a session ID (SessionId) to the client. Save it in the client-side cookie, and then every time the browser sends an HTTP request, it will bring the SessionId in the cookie to the server, and the server can associate the previous state information with the session according to the session ID, so as to realize session retention.

Advantages: High security, because state information is saved on the server side.

Disadvantages: Since large websites often use distributed servers, the HTTP requests sent by the browser generally have to go through the load balancer before reaching the specific background server. If two HTTP requests from the same browser fall on different servers , Session-based methods cannot achieve session persistence.

[Solution: Using middleware, such as Redis, we store Session information in Redis so that each server can access the previous state information]

② Session persistence based on Cookie

When the server sends a response message, set the Set-Cookie field in the HTTP response header to store the status information of the client. The client parses out the field information in the HTTP response header, and creates different cookies according to its life cycle, so that every time the browser sends an HTTP request, it will bring the cookie field, so as to maintain the state. The main difference between cookie-based session persistence and session-based session persistence is that the former completely stores session state information in browser cookies.

Advantages: The server does not need to save state information, which reduces the storage pressure on the server and facilitates horizontal expansion of the server.

Disadvantage: This method is not secure, because the state information is stored on the client side, which means that confidential data cannot be saved in the session. In addition, the browser needs to send additional cookies to the server every time it initiates an HTTP request, which will take up more bandwidth.

Extension: What should I do if the cookie is disabled?

If the cookie is disabled, you can put the session identifier in the URL parameter by rewriting the URL, and you can also maintain the session.

13. Status code

Interview high frequency index: ★★★★☆

The HTTP status code consists of three decimal numbers, the first number defines the type of status code, and the last two do not play a role in classification. There are 5 types of HTTP status codes:

Classification Category description
1XX Indication message – Indicates that the request is being processed
2XX Success – Indicates that the request has been successfully processed
3XX Redirect – the request to complete requires additional action
4XX Client error – the request has a syntax error or cannot be fulfilled, the server cannot process the request
5XX Server-Side Error – The server encountered an error processing the request

14. The difference between HTTP/1.1 and HTTP/1.0

Interview high frequency index: ★★★☆☆

  • 错误通知的管理:HTTP/1.1 在 1.0 的基础上新增了 24 个错误状态响应码,例如 414 表示客户端请求中所包含的 URL 地址太长,以至于服务器无法处理;410 表示所请求的资源已经被永久删除。
  • 长连接:HTTP/1.0 默认浏览器和服务器之间保持短暂连接,浏览器的每次请求都需要与服务器建立一个 TCP 连接,服务器完成后立即断开 TCP 连接。HTTP/1.1 默认使用的是持久连接,其支持在同一个 TCP 请求中传送多个 HTTP 请求和响应。此之前的 HTTP 版本的默认连接都是使用非持久连接,如果想要在旧版本的 HTTP 协议上维持持久连接,则需要指定 Connection 的首部字段的值为 Keep-Alive

15. HTTP/1.X 和 HTTP/2.0 的区别

面试高频指数:★★★☆☆

  • 相比于 HTTP/1.X 的文本(字符串)传送, HTTP/2.0 采用二进制传送。客户端和服务器传输数据时把数据分成帧,帧组成了数据流,流具有流 ID 标识和优先级,通过优先级以及流依赖能够一定程度上解决关键请求被阻塞的问题。
  • HTTP/2.0 支持多路复用。因为流 ID 的存在, 通过同一个 HTTP 请求可以实现多个 HTTP 请求传输,客户端和服务器可以通过流 ID 来标识究竟是哪个流从而定位到是哪个 HTTP 请求。
  • HTTP/2.0 头部压缩。HTTP/2.0 通过 gzip 和 compress 压缩头部然后再发送,同时通信双方会维护一张头信息表,所有字段都记录在这张表中,在每次 HTTP 传输时只需要传头字段在表中的索引即可,大大减小了重传次数和数据量。
  • HTTP/2.0 支持服务器推送。服务器在客户端未经请求许可的情况下,可预先向客户端推送需要的内容,客户端在退出服务时可通过发送复位相关的请求来取消服务端的推送。

16. HTTP/3 了解吗

面试高频指数:★★☆☆☆

HTTP/2 存在的问题

我们知道,传统 Web 平台的数据传输都基于 TCP 协议,而 TCP 协议在创建连接之前不可避免的需要三次握手,如果需要提高数据交互的安全性,即增加传输层安全协议(TLS),还会增加更多的握手次数。 HTTP 从 1.0 到 2.0,其传输层都是基于 TCP 协议的。即使是带来巨大性能提升的 HTTP/2,也无法完全解决 TCP 协议存在的固有问题(慢启动,拥塞窗口尺寸的设置等)。此外,HTTP/2 多路复用只是减少了连接数,其队头的拥塞问题并没有完全解决,倘若 TCP 丢包率过大,则 HTTP/2 的表现将不如 HTTP/1.1。

QUIC 协议

QUIC(Quick UDP Internet Connections),直译为快速 UDP 网络连接,其主要目的是解决采用传输层 TCP 协议存在的问题,同时满足传输层和应用层对多连接、低延迟等的需求。该协议融合了 TCP, TLS, HTTP/2 等协议的特性,并基于 UDP传输。该协议带来的主要提升有:

  • 低延迟连接。当客户端第一次连接服务器时,QUIC 只需要 1 RTT(Round-Trid Time)延迟就可以建立安全可靠的连接(采用 TLS 1.3 版本),相比于 TCP + TLS 的 3 次 RTT 要更加快捷。之后,客户端可以在本地缓存加密的认证信息,当再次与服务器建立连接时可以实现 0 RTT 的连接建立延迟。
  • QUIC 复用了 HTTP/2 协议的多路复用功能,由于 QUIC 基于 UDP,所以也避免了 HTTP/2存在的队头阻塞问题。
  • 基于 UDP 协议的 QUIC 运行在用户域而不是系统内核,这使得 QUIC 协议可以快速的更新和部署,从而很好地解决了 TPC 协议部署及更新的困难。
  • QUIC 的报文是经过加密和认证的,除了少量的报文,其它所有的 QUIC 报文头部都经过了认证,报文主体经过了加密。只要有攻击者篡改 QUIC 报文,接收端都能及时发现。
  • 具有向前纠错机制,每个数据包携带了除了本身内容外的部分其他数据包的内容,使得在出现少量丢包的情况下,尽量地减少其它包的重传次数,其通过牺牲单个包所携带的有效数据大小换来更少的重传次数,这在丢包数量较小的场景下能够带来一定程度的性能提升。

HTTP/3

HTTP/3 是在 QUIC 基础上发展起来的,其底层使用 UDP 进行数据传输,上层仍然使用 HTTP/2。在 UDP 与 HTTP/2 之间存在一个 QUIC 层,其中 TLS 加密过程在该层进行处理。HTTP/3 主要有以下几个特点:

① 使用 UDP 作为传输层进行通信;

② 在 UDP 之上的 QUIC 协议保证了 HTTP/3 的安全性。QUIC 在建立连接的过程中就完成了 TLS 加密握手;

③ 建立连接快,正常只需要 1 RTT 即可建立连接。如果有缓存之前的会话信息,则直接验证和建立连接,此过程 0 RTT。建立连接时,也可以带有少量业务数据;

④ 不和具体底层连接绑定,QUIC 为每个连接的两端分别分配了一个唯一 ID,上层连接只认这对逻辑 ID。网络切换或者断连时,只需要继续发送数据包即可完成连接的建立;

⑤ 使用 QPACK 进行头部压缩,因为 在 HTTP/2 中的 HPACK 要求传输过程有序,这会导致队头阻塞,而 QPACK 不存在这个问题。

最后我们使用一张图来清晰的表示出 HTTP 协议的发展变化:

insert image description here

17. DNS 的作用和原理

面试高频指数:★★★★☆

DNS

DNS(Domain Name System)是域名系统的英文缩写,是一种组织成域层次结构的计算机和网络服务命名系统,用于 TCP/IP 网络。

DNS 的作用

通常我们有两种方式识别主机:通过主机名或者 IP 地址。人们喜欢便于记忆的主机名表示,而路由器则喜欢定长的、有着层次结构的 IP 地址。为了满足这些不同的偏好,我们就需要一种能够进行主机名到 IP 地址转换的目录服务,域名系统作为将域名和 IP 地址相互映射的一个分布式数据库,能够使人更方便地访问互联网。

DNS 域名解析原理

DNS 采用了分布式的设计方案,其域名空间采用一种树形的层次结构:
insert image description here
上图展示了 DNS 服务器的部分层次结构,从上到下依次为根域名服务器、顶级域名服务器和权威域名服务器。其实根域名服务器在因特网上有13个,大部分位于北美洲。第二层为顶级域服务器,这些服务器负责顶级域名(如 com、org、net、edu)和所有国家的顶级域名(如uk、fr、ca 和 jp)。在第三层为权威 DNS 服务器,因特网上具有公共可访问主机(例如 Web 服务器和邮件服务器)的每个组织机构必须提供公共可访问的 DNS 记录,这些记录由组织机构的权威 DNS 服务器负责保存,这些记录将这些主机的名称映射为 IP 地址。

除此之外,还有一类重要的 DNS 服务器,叫做本地 DNS 服务器。本地 DNS 服务器严格来说不在 DNS 服务器的层次结构中,但它对 DNS 层次结构是很重要的。一般来说,每个网络服务提供商(ISP) 都有一台本地 DNS 服务器。当主机与某个 ISP 相连时,该 ISP 提供一台主机的 IP 地址,该主机具有一台或多台其本地 DNS 服务器的 IP 地址。主机的本地 DNS 服务器通常和主机距离较近,当主机发起 DNS 请求时,该请求被发送到本地 DNS 服务器,它起着代理的作用,并将该请求转发到 DNS 服务器层次结构中。

我们以一个例子来了解 DNS 的工作原理,假设主机 A(IP 地址为 abc.xyz.edu) 想知道主机 B 的 IP 地址 (def.mn.edu),如下图所示,主机 A 首先向它的本地 DNS 服务器发送一个 DNS 查询报文。该查询报文含有被转换的主机名 def.mn.edu。本地 DNS 服务器将该报文转发到根 DNS 服务器,根 DNS 服务器注意到查询的 IP 地址前缀为 edu 后向本地 DNS 服务器返回负责 edu 的顶级域名服务器的 IP 地址列表。该本地 DNS 服务器则再次向这些 顶级域名服务器发送查询报文。该顶级域名服务器注意到 mn.edu 的前缀,并用权威域名服务器的 IP 地址进行响应。通常情况下,顶级域名服务器并不总是知道每台主机的权威 DNS 服务器的 IP 地址,而只知道中间的某个服务器,该中间 DNS 服务器依次能找到用于相应主机的 IP 地址,我们假设中间经历了权威服务器 ① 和 ②,最后找到了负责 def.mn.edu 的权威 DNS 服务器 ③,之后,本地 DNS 服务器直接向该服务器发送查询报文从而获得主机 B 的IP 地址。

insert image description here
在上图中,IP 地址的查询其实经历了两种查询方式,分别是递归查询和迭代查询。

拓展:域名解析查询的两种方式

  • 递归查询:如果主机所询问的本地域名服务器不知道被查询域名的 IP 地址,那么本地域名服务器就以 DNS 客户端的身份,向其他根域名服务器继续发出查询请求报文,即替主机继续查询,而不是让主机自己进行下一步查询,如上图步骤(1)和(10)。
  • 迭代查询:当根域名服务器收到本地域名服务器发出的迭代查询请求报文时,要么给出所要查询的 IP 地址,要么告诉本地服务器下一步应该找哪个域名服务器进行查询,然后让本地服务器进行后续的查询,如上图步骤(2)~(9)。

18. DNS 为什么用 UDP

面试高频指数:★★☆☆☆

更正确的答案是 DNS 既使用 TCP 又使用 UDP。

当进行区域传送(主域名服务器向辅助域名服务器传送变化的那部分数据)时会使用 TCP,因为数据同步传送的数据量比一个请求和应答的数据量要多,而 TCP 允许的报文长度更长,因此为了保证数据的正确性,会使用基于可靠连接的 TCP。

当客户端向 DNS 服务器查询域名 ( 域名解析) 的时候,一般返回的内容不会超过 UDP 报文的最大长度,即 512 字节。用 UDP 传输时,不需要经过 TCP 三次握手的过程,从而大大提高了响应速度,但这要求域名解析器和域名服务器都必须自己处理超时和重传从而保证可靠性。

19. 怎么实现 DNS 劫持

面试高频指数:★★★☆☆

DNS 劫持即域名劫持,是通过将原域名对应的 IP 地址进行替换从而使得用户访问到错误的网站或者使得用户无法正常访问网站的一种攻击方式。域名劫持往往只能在特定的网络范围内进行,范围外的 DNS 服务器能够返回正常的 IP 地址。攻击者可以冒充原域名所属机构,通过电子邮件的方式修改组织机构的域名注册信息,或者将域名转让给其它组织,并将新的域名信息保存在所指定的 DNS 服务器中,从而使得用户无法通过对原域名进行解析来访问目的网址。

具体实施步骤如下:

① 获取要劫持的域名信息:攻击者首先会访问域名查询站点查询要劫持的域名信息。

② 控制域名相应的 E-MAIL 账号:在获取到域名信息后,攻击者通过暴力破解或者专门的方法破解公司注册域名时使用的 E-mail 账号所对应的密码。更高级的攻击者甚至能够直接对 E-mail 进行信息窃取。

③ 修改注册信息:当攻击者破解了 E-MAIL 后,会利用相关的更改功能修改该域名的注册信息,包括域名拥有者信息,DNS 服务器信息等。

④ 使用 E-MAIL 收发确认函:在修改完注册信息后,攻击者在 E-mail 真正拥有者之前收到修改域名注册信息的相关确认信息,并回复确认修改文件,待网络公司恢复已成功修改信件后,攻击者便成功完成 DNS 劫持。

用户端的一些预防手段:

  • 直接通过 IP 地址访问网站,避开 DNS 劫持。
  • 由于域名劫持往往只能在特定的网络范围内进行,因此一些高级用户可以通过网络设置让 DNS 指向正常的域名服务器以实现对目的网址的正常访问,例如将计算机首选 DNS 服务器的地址固定为 8.8.8.8。

20. socket() 套接字有哪些

面试高频指数:★★★☆☆

套接字(Socket)是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象,网络进程通信的一端就是一个套接字,不同主机上的进程便是通过套接字发送报文来进行通信。例如 TCP 用主机的 IP 地址 + 端口号作为 TCP 连接的端点,这个端点就叫做套接字。

套接字主要有以下三种类型:

  • 流套接字(SOCK_STREAM):流套接字基于 TCP 传输协议,主要用于提供面向连接、可靠的数据传输服务。由于 TCP 协议的特点,使用流套接字进行通信时能够保证数据无差错、无重复传送,并按顺序接收,通信双方不需要在程序中进行相应的处理。
  • 数据报套接字(SOCK_DGRAM):和流套接字不同,数据报套接字基于 UDP 传输协议,对应于无连接的 UDP 服务应用。该服务并不能保证数据传输的可靠性,也无法保证对端能够顺序接收到数据。此外,通信两端不需建立长时间的连接关系,当 UDP 客户端发送一个数据给服务器后,其可以通过同一个套接字给另一个服务器发送数据。当用 UDP 套接字时,丢包等问题需要在程序中进行处理。
  • 原始套接字(SOCK_RAW):由于流套接字和数据报套接字只能读取 TCP 和 UDP 协议的数据,当需要传送非传输层数据包(例如 Ping 命令时用的 ICMP 协议数据包)或者遇到操作系统无法处理的数据包时,此时就需要建立原始套接字来发送。

21. URI(统一资源标识符)和 URL(统一资源定位符)之间的区别

面试高频指数:★★★☆☆

URL,即统一资源定位符 (Uniform Resource Locator ),URL 其实就是我们平时上网时输入的网址,它标识一个互联网资源,并指定对其进行操作或获取该资源的方法。例如 https://leetcode-cn.com/problemset/all/ 这个 URL,标识一个特定资源并表示该资源的某种形式是可以通过 HTTP 协议从相应位置获得。

从定义即可看出,URL 是 URI 的一个子集,两者都定义了资源是什么,而 URL 还定义了如何能访问到该资源。URI 是一种语义上的抽象概念,可以是绝对的,也可以是相对的,而URL则必须提供足够的信息来定位,是绝对的。简单地说,只要能唯一标识资源的就是 URI,在 URI 的基础上给出其资源的访问方式的就是 URL。

22. 为什么 fidder,charles 能抓到你的包【抓取数据包的过程】

面试高频指数:★★☆☆☆

假如我们需要抓取客户端的数据包,需要监控客户端与服务器交互之间的网络节点,监控其中任意一个网络节点(网卡),获取所有经过网卡中的数据,对这些数据按照网络协议进行解析,这就是抓包的基本原理。而中间的网络节点不受我们控制,是基本无法实现抓包的,因此只能在客户端与服务器之间进行抓包

① 当采用抓包工具抓取 HTTP 数据包时,过程较为简单:

  • 首先抓包工具会提出代理服务,客户端需要连接该代理;
  • 客户端发出 HTTP 请求时,会经过抓包工具的代理,抓包工具将请求的原文进行展示;
  • 抓包工具使用该原文将请求发送给服务器;
  • 服务器返回结果给抓包工具,抓包工具将返回结果进行展示;
  • 抓包工具将服务器返回的结果原样返回给客户端。

这里抓包工具相当于透明人,数据经过的时候它一只手接到数据,然后另一只手把数据传出去。

② 当抓取 HTTPS 数据包时:

  • 客户端连接抓包工具提供的代理服务,并安装抓包工具的根证书;
  • 客户端发出 HTTPS 请求,抓包工具模拟服务器与客户端进行 TLS 握手交换密钥等流程;
  • 抓包工具发送一个 HTTPS 请求给客户端请求的目标服务器,并与目标服务器进行 TLS 握手交换密钥等流程;
  • 客户端使用与抓包工具协定好的密钥加密数据后发送给抓包工具;
  • 抓包工具使用与客户端协定好的密钥解密数据,并将结果进行展示;
  • 抓包工具将解密后的客户端数据,使用与服务器协定好的密钥进行加密后发送给目标服务器;
  • 服务器解密数据后,做对应的逻辑处理,然后将返回结果使用与抓包工具协定好的密钥进行加密发送给抓包工具;
  • 抓包工具将服务器返回的结果,用与服务器协定好的密钥解密,并将结果进行展示;
  • 抓包工具将解密后的服务器返回数据,使用与客户端协定好的密钥进行加密后发送给客户端;
  • 客户端解密数据。

这个时候抓包工具对客户端来说相当于服务器,对服务器来说相当于客户端。在这个传输过程中,客户端会以为它就是目标服务器,服务器也会以为它就是请求发起的客户端。

23. 网页解析全过程【用户输入网址到显示对应页面的全过程】

面试高频指数:★★★★★
insert image description here
DNS 解析:当用户输入一个网址并按下回车键的时候,浏览器获得一个域名,而在实际通信过程中,我们需要的是一个 IP 地址,因此我们需要先把域名转换成相应 IP 地址。【具体细节参看问题 16,17】

TCP 连接:浏览器通过 DNS 获取到 Web 服务器真正的 IP 地址后,便向 Web 服务器发起 TCP 连接请求,通过 TCP 三次握手建立好连接后,浏览器便可以将 HTTP 请求数据发送给服务器了。【三次握手放在传输层详细讲解】

发送 HTTP 请求:浏览器向 Web 服务器发起一个 HTTP 请求,HTTP 协议是建立在 TCP 协议之上的应用层协议,其本质是在建立起的TCP连接中,按照HTTP协议标准发送一个索要网页的请求。在这一过程中,会涉及到负载均衡等操作。

拓展:什么是负载均衡?

负载均衡,英文名为 Load Balance,其含义是指将负载(工作任务)进行平衡、分摊到多个操作单元上进行运行,例如 FTP 服务器、Web 服务器、企业核心服务器和其他主要任务服务器等,从而协同完成工作任务。负载均衡建立在现有的网络之上,它提供了一种透明且廉价有效的方法扩展服务器和网络设备的带宽、增加吞吐量、加强网络处理能力并提高网络的灵活性和可用性。

负载均衡是分布式系统架构设计中必须考虑的因素之一,例如天猫、京东等大型用户网站中为了处理海量用户发起的请求,其往往采用分布式服务器,并通过引入反向代理等方式将用户请求均匀分发到每个服务器上,而这一过程所实现的就是负载均衡。

处理请求并返回:服务器获取到客户端的 HTTP 请求后,会根据 HTTP 请求中的内容来决定如何获取相应的文件,并将文件发送给浏览器。

浏览器渲染:浏览器根据响应开始显示页面,首先解析 HTML 文件构建 DOM 树,然后解析 CSS 文件构建渲染树,等到渲染树构建完成后,浏览器开始布局渲染树并将其绘制到屏幕上。

断开连接:客户端和服务器通过四次挥手终止 TCP 连接。【其中的细节放在传输层详细讲解】

  1. 浏览器查询DNS,获取域名对应的IP地址:具体过程包括浏览器搜索自身的DNS缓存、搜索操作系统的DNS缓存、读取本地的Host文件和向本地DNS服务器进行查询等。对于向本地DNS服务器进行查询,如果要查询的域名包含在本地配置区域资源中,则返回解析结果给客户机,完成域名解析(此解析具有权威性);如果要查询的域名不由本地DNS服务器区域解析,但该服务器已缓存了此网址映射关系,则调用这个IP地址映射,完成域名解析(此解析不具有权威性)。如果本地域名服务器并未缓存该网址映射关系,那么将根据其设置发起递归查询或者迭代查询;
  2. 浏览器获得域名对应的IP地址以后,浏览器向服务器请求建立链接,发起三次握手;TCP/IP链接建立起来后,浏览器向服务器发送HTTP请求;
  3. 服务器接收到这个请求,并根据路径参数映射到特定的请求处理器进行处理,并将处理结果及相应的视图返回给浏览器;
  4. 浏览器解析并渲染视图,若遇到对js文件、css文件及图片等静态资源的引用,则重复上述步骤并向服务器请求这些资源;
  5. 浏览器根据其请求到的资源、数据渲染页面,最终向用户呈现一个完整的页面。

第三部分 传输层

3.1 三次握手和四次挥手机制

面试高频指数:★★★★★

三次握手

insert image description here
三次握手是 TCP 连接的建立过程。在握手之前,主动打开连接的客户端结束 CLOSE 阶段,被动打开的服务器也结束 CLOSE 阶段,并进入 LISTEN 阶段。随后进入三次握手阶段:

① 首先客户端向服务器发送一个 SYN 包,并等待服务器确认,其中:

  • 标志位为 SYN,表示请求建立连接;
  • 序号为 Seq = x(x 一般为 1);
  • 随后客户端进入 SYN-SENT 阶段。

② 服务器接收到客户端发来的 SYN 包后,对该包进行确认后结束 LISTEN 阶段,并返回一段 TCP 报文,其中:

  • 标志位为 SYN 和 ACK,表示确认客户端的报文 Seq 序号有效,服务器能正常接收客户端发送的数据,并同意创建新连接;
  • 序号为 Seq = y;
  • 确认号为 Ack = x + 1,表示收到客户端的序号 Seq 并将其值加 1 作为自己确认号 Ack 的值,随后服务器端进入 SYN-RECV 阶段。

③ 客户端接收到发送的 SYN + ACK 包后,明确了从客户端到服务器的数据传输是正常的,从而结束 SYN-SENT 阶段。并返回最后一段报文。其中:

  • 标志位为 ACK,表示确认收到服务器端同意连接的信号;
  • 序号为 Seq = x + 1,表示收到服务器端的确认号 Ack,并将其值作为自己的序号值;
  • 确认号为 Ack= y + 1,表示收到服务器端序号 seq,并将其值加 1 作为自己的确认号 Ack 的值。
  • 随后客户端进入 ESTABLISHED。

当服务器端收到来自客户端确认收到服务器数据的报文后,得知从服务器到客户端的数据传输是正常的,从而结束 SYN-RECV 阶段,进入 ESTABLISHED 阶段,从而完成三次握手。

四次挥手:
insert image description here
四次挥手即 TCP 连接的释放,这里假设客户端主动释放连接。在挥手之前主动释放连接的客户端结束 ESTABLISHED 阶段,随后开始四次挥手:
① 首先客户端向服务器发送一段 TCP 报文表明其想要释放 TCP 连接,其中:

  • 标记位为 FIN,表示请求释放连接;
  • 序号为 Seq = u;
  • 随后客户端进入 FIN-WAIT-1 阶段,即半关闭阶段,并且停止向服务端发送通信数据。

② 服务器接收到客户端请求断开连接的 FIN 报文后,结束 ESTABLISHED 阶段,进入 CLOSE-WAIT 阶段并返回一段 TCP 报文,其中:

  • 标记位为 ACK,表示接收到客户端释放连接的请求;
  • 序号为 Seq = v;
  • 确认号为 Ack = u + 1,表示是在收到客户端报文的基础上,将其序号值加 1 作为本段报文确认号 Ack 的值;
  • 随后服务器开始准备释放服务器端到客户端方向上的连接。

客户端收到服务器发送过来的 TCP 报文后,确认服务器已经收到了客户端连接释放的请求,随后客户端结束 FIN-WAIT-1 阶段,进入 FIN-WAIT-2 阶段。

③ 服务器端在发出 ACK 确认报文后,服务器端会将遗留的待传数据传送给客户端,待传输完成后即经过 CLOSE-WAIT 阶段,便做好了释放服务器端到客户端的连接准备,再次向客户端发出一段 TCP 报文,其中:

  • 标记位为 FIN 和 ACK,表示已经准备好释放连接了;
  • 序号为 Seq = w;
  • 确认号 Ack = u + 1,表示是在收到客户端报文的基础上,将其序号 Seq 的值加 1 作为本段报文确认号 Ack 的值。

随后服务器端结束 CLOSE-WAIT 阶段,进入 LAST-ACK 阶段。并且停止向客户端发送数据。

④ 客户端收到从服务器发来的 TCP 报文,确认了服务器已经做好释放连接的准备,于是结束 FIN-WAIT-2 阶段,进入 TIME-WAIT 阶段,并向服务器发送一段报文,其中:

  • 标记位为 ACK,表示接收到服务器准备好释放连接的信号;
  • 序号为 Seq= u + 1,表示是在已收到服务器报文的基础上,将其确认号 Ack 值作为本段序号的值;
  • 确认号为 Ack= w + 1,表示是在收到了服务器报文的基础上,将其序号 Seq 的值作为本段报文确认号的值。

随后客户端开始在 TIME-WAIT 阶段等待 2 MSL。服务器端收到从客户端发出的 TCP 报文之后结束 LAST-ACK 阶段,进入 CLOSED 阶段。由此正式确认关闭服务器端到客户端方向上的连接。客户端等待完 2 MSL 之后,结束 TIME-WAIT 阶段,进入 CLOSED 阶段,由此完成「四次挥手」。

3.2 如果三次握手的时候每次握手信息对方没有收到会怎么样

面试高频指数:★★★★☆

  • 若第一次握手服务器未接收到客户端请求建立连接的数据包时,服务器不会进行任何相应的动作,而客户端由于在一段时间内没有收到服务器发来的确认报文,因此会等待一段时间后重新发送 SYN 同步报文,若仍然没有回应,则重复上述过程直到发送次数超过最大重传次数限制后,建立连接的系统调用会返回 -1。
  • 若第二次握手客户端未接收到服务器回应的 ACK 报文时,客户端会采取第一次握手失败时的动作,这里不再重复,而服务器端此时将阻塞在 accept() 系统调用处等待 client 再次发送 ACK 报文。
  • 若第三次握手服务器未接收到客户端发送过来的 ACK 报文,同样会采取类似于客户端的超时重传机制,若重传次数超过限制后仍然没有回应,则 accep() 系统调用返回 -1,服务器端连接建立失败。但此时客户端认为自己已经连接成功了,因此开始向服务器端发送数据,但是服务器端的 accept() 系统调用已返回,此时没有在监听状态。因此服务器端接收到来自客户端发送来的数据时会发送 RST 报文给客户端,消除客户端单方面建立连接的状态。

3.3 为什么要进行三次握手?两次握手可以吗?

面试高频指数:★★★★★

三次握手的主要目的是确认自己和对方的发送和接收都是正常的,从而保证了双方能够进行可靠通信。若采用两次握手,当第二次握手后就建立连接的话,此时客户端知道服务器能够正常接收到自己发送的数据,而服务器并不知道客户端是否能够收到自己发送的数据。

网络往往是非理想状态的(存在丢包和延迟),当客户端发起创建连接的请求时,如果服务器直接创建了这个连接并返回包含 SYN、ACK 和 Seq 等内容的数据包给客户端,这个数据包因为网络传输的原因丢失了,丢失之后客户端就一直接收不到返回的数据包。由于客户端可能设置了一个超时时间,一段时间后就关闭了连接建立的请求,再重新发起新的请求,而服务器端是不知道的,如果没有第三次握手告诉服务器客户端能否收到服务器传输的数据的话,服务器端的端口就会一直开着,等到客户端因超时重新发出请求时,服务器就会重新开启一个端口连接。长此以往, 这样的端口越来越多,就会造成服务器开销的浪费。

3.4 第 2 次握手传回了 ACK,为什么还要传回 SYN

面试高频指数:★★★★☆

ACK 是为了告诉客户端发来的数据已经接收无误,而传回 SYN 是为了告诉客户端,服务端收到的消息确实是客户端发送的消息。

3.5 为什么要四次挥手?

面试高频指数:★★★★☆

释放 TCP 连接时之所以需要四次挥手,是因为 FIN 释放连接报文和 ACK 确认接收报文是分别在两次握手中传输的。 当主动方在数据传送结束后发出连接释放的通知,由于被动方可能还有必要的数据要处理,所以会先返回 ACK 确认收到报文。当被动方也没有数据再发送的时候,则发出连接释放通知,对方确认后才完全关闭TCP连接。

举个例子:A 和 B 打电话,通话即将结束后,A 说“我没啥要说的了”,B回答“我知道了”,但是 B 可能还会有要说的话,A 不能要求 B 跟着自己的节奏结束通话,于是 B 可能又巴拉巴拉说了一通,最后 B 说“我说完了”,A 回答“知道了”,这样通话才算结束。

3.6 CLOSE-WAIT 和 TIME-WAIT 的状态和意义

面试高频指数:★★★★★

在服务器收到客户端关闭连接的请求并告诉客户端自己已经成功收到了该请求之后,服务器进入了 CLOSE-WAIT 状态,然而此时有可能服务端还有一些数据没有传输完成,因此不能立即关闭连接,而 CLOSE-WAIT 状态就是为了保证服务器在关闭连接之前将待发送的数据发送完成。

TIME-WAIT 发生在第四次挥手,当客户端向服务端发送 ACK 确认报文后进入该状态,若取消该状态,即客户端在收到服务端的 FIN 报文后立即关闭连接,此时服务端相应的端口并没有关闭,若客户端在相同的端口立即建立新的连接,则有可能接收到上一次连接中残留的数据包,可能会导致不可预料的异常出现。除此之外,假设客户端最后一次发送的 ACK 包在传输的时候丢失了,由于 TCP 协议的超时重传机制,服务端将重发 FIN 报文,若客户端并没有维持 TIME-WAIT 状态而直接关闭的话,当收到服务端重新发送的 FIN 包时,客户端就会用 RST 包来响应服务端,这将会使得对方认为是有错误发生,然而其实只是正常的关闭连接过程,并没有出现异常情况。

3.7 TIME_WAIT 状态会导致什么问题,怎么解决

面试高频指数:★★★☆☆

我们考虑高并发短连接的业务场景,在高并发短连接的 TCP 服务器上,当服务器处理完请求后主动请求关闭连接,这样服务器上会有大量的连接处于 TIME_WAIT 状态,服务器维护每一个连接需要一个 socket,也就是每个连接会占用一个文件描述符,而文件描述符的使用是有上限的,如果持续高并发,会导致一些正常的 连接失败。

解决方案:修改配置或设置 SO_REUSEADDR 套接字,使得服务器处于 TIME-WAIT 状态下的端口能够快速回收和重用。

3.8 TIME-WAIT 为什么是 2MSL

面试高频指数:★★★★☆

当客户端发出最后的 ACK 确认报文时,并不能确定服务器端能够收到该段报文。所以客户端在发送完 ACK 确认报文之后,会设置一个时长为 2 MSL 的计时器。MSL(Maximum Segment Lifetime),指一段 TCP 报文在传输过程中的最大生命周期。2 MSL 即是服务器端发出 FIN 报文和客户端发出的 ACK 确认报文所能保持有效的最大时长。

若服务器在 1 MSL 内没有收到客户端发出的 ACK 确认报文,再次向客户端发出 FIN 报文。如果客户端在 2 MSL 内收到了服务器再次发来的 FIN 报文,说明服务器由于一些原因并没有收到客户端发出的 ACK 确认报文。客户端将再次向服务器发出 ACK 确认报文,并重新开始 2 MSL 的计时。

若客户端在 2MSL 内没有再次收到服务器发送的 FIN 报文,则说明服务器正常接收到客户端 ACK 确认报文,客户端可以进入 CLOSE 阶段,即完成四次挥手。

所以客户端要经历 2 MSL 时长的 TIME-WAIT 阶段,为的是确认服务器能否接收到客户端发出的 ACK 确认报文。

3.9 有很多 TIME-WAIT 状态如何解决

面试高频指数:★★★☆☆

服务器可以设置 SO_REUSEADDR 套接字选项来通知内核,如果端口被占用,但 TCP 连接位于 TIME_WAIT 状态时可以重用端口。如果你的服务器程序停止后想立即重启,而新的套接字依旧希望使用同一端口,此时 SO_REUSEADDR 选项就可以避免 TIME-WAIT 状态。

也可以采用长连接的方式减少 TCP 的连接与断开,在长连接的业务中往往不需要考虑 TIME-WAIT 状态,但其实在长连接的业务中并发量一般不会太高。

3.10 有很多 CLOSE-WAIT 怎么解决

面试高频指数:★★★☆☆

  • 首先检查是不是自己的代码问题(看是否服务端程序忘记关闭连接),如果是,则修改代码。
  • 调整系统参数,包括句柄相关参数和 TCP/IP 的参数,一般一个 CLOSE_WAIT 会维持至少 2 个小时的时间,我们可以通过调整参数来缩短这个时间。

3.11 TCP 和 UDP 的区别

面试高频指数:★★★★★

类型 是否面向连接 传输可靠性 传输形式 传输效率 所需资源 应用场景 首部字节
TCP 可靠 字节流 文件传输、邮件传输 20~60
UDP 不可靠 数据报文段 即时通讯、域名转换 8个字节

3.13 TCP 是如何保证可靠性的

面试高频指数:★★★★☆

  • 数据分块:应用数据被分割成 TCP 认为最适合发送的数据块。
  • 序列号和确认应答:TCP 给发送的每一个包进行编号,在传输的过程中,每次接收方收到数据后,都会对传输方进行确认应答,即发送 ACK 报文,这个 ACK 报文当中带有对应的确认序列号,告诉发送方成功接收了哪些数据以及下一次的数据从哪里开始发。除此之外,接收方可以根据序列号对数据包进行排序,把有序数据传送给应用层,并丢弃重复的数据。
  • 校验和: TCP 将保持它首部和数据部分的检验和。这是一个端到端的检验和,目的是检测数据在传输过程中的任何变化。如果收到报文段的检验和有差错,TCP 将丢弃这个报文段并且不确认收到此报文段。
  • 流量控制: TCP 连接的双方都有一个固定大小的缓冲空间,发送方发送的数据量不能超过接收端缓冲区的大小。当接收方来不及处理发送方的数据,会提示发送方降低发送的速率,防止产生丢包。TCP 通过滑动窗口协议来支持流量控制机制。
  • 拥塞控制: 当网络某个节点发生拥塞时,减少数据的发送。
  • ARQ协议: 也是为了实现可靠传输的,它的基本原理就是每发完一个分组就停止发送,等待对方确认。在收到确认后再发下一个分组。
  • 超时重传: 当 TCP 发出一个报文段后,它启动一个定时器,等待目的端确认收到这个报文段。如果超过某个时间还没有收到确认,将重发这个报文段。

3.18 TCP 流量控制与拥塞控制

面试高频指数:★★★★☆

  • 流量控制

所谓流量控制就是让发送方的发送速率不要太快,让接收方来得及接收。如果接收方来不及接收发送方发送的数据,那么就会有分组丢失。在 TCP 中利用可边长的滑动窗口机制可以很方便的在 TCP 连接上实现对发送方的流量控制。主要的方式是接收方返回的 ACK 中会包含自己的接收窗口大小,以控制发送方此次发送的数据量大小(发送窗口大小)。

  • 拥塞控制

在实际的网络通信系统中,除了发送方和接收方外,还有路由器,交换机等复杂的网络传输线路,此时就需要拥塞控制。拥塞控制是作用于网络的,它是防止过多的数据注入到网络中,避免出现网络负载过大的情况。常用的解决方法有:慢开始和拥塞避免、快重传和快恢复。

  • 拥塞控制和流量控制的区别

拥塞控制往往是一种全局的,防止过多的数据注入到网络之中,而TCP连接的端点只要不能收到对方的确认信息,猜想在网络中发生了拥塞,但并不知道发生在何处,因此,流量控制往往指点对点通信量的控制,是端到端的问题。

3.14 UDP 为什么是不可靠的?bind 和 connect 对于 UDP 的作用是什么

面试高频指数:★★★☆☆

UDP 只有一个 socket 接收缓冲区,没有 socket 发送缓冲区,即只要有数据就发,不管对方是否可以正确接收。而在对方的 socket 接收缓冲区满了之后,新来的数据报无法进入到 socket 接受缓冲区,此数据报就会被丢弃,因此 UDP 不能保证数据能够到达目的地,此外,UDP 也没有流量控制和重传机制,故UDP的数据传输是不可靠的。

和 TCP 建立连接时采用三次握手不同,UDP 中调用 connect 只是把对端的 IP 和 端口号记录下来,并且 UDP 可多多次调用 connect 来指定一个新的 IP 和端口号,或者断开旧的 IP 和端口号(通过设置 connect 函数的第二个参数)。和普通的 UDP 相比,调用 connect 的 UDP 会提升效率,并且在高并发服务中会增加系统稳定性。

当 UDP 的发送端调用 bind 函数时,就会将这个套接字指定一个端口,若不调用 bind 函数,系统内核会随机分配一个端口给该套接字。当手动绑定时,能够避免内核来执行这一操作,从而在一定程度上提高性能。

3.15 TCP 超时重传的原理

面试高频指数:★★★☆☆

发送方在发送一次数据后就开启一个定时器,在一定时间内如果没有得到发送数据包的 ACK 报文,那么就重新发送数据,在达到一定次数还没有成功的话就放弃重传并发送一个复位信号。其中超时时间的计算是超时的核心,而定时时间的确定往往需要进行适当的权衡,因为当定时时间过长会造成网络利用率不高,定时太短会造成多次重传,使得网络阻塞。在 TCP 连接过程中,会参考当前的网络状况从而找到一个合适的超时时间。

3.16 TCP 的停止等待协议是什么

面试高频指数:★★★☆☆

停止等待协议是为了实现 TCP 可靠传输而提出的一种相对简单的协议,该协议指的是发送方每发完一组数据后,直到收到接收方的确认信号才继续发送下一组数据。我们通过四种情形来帮助理解停等协议是如何实现可靠传输的:

insert image description here
① 无差错传输

如上述左图所示,A 发送分组 Msg 1,发完就暂停发送,直到收到接收方确认收到 Msg 1 的报文后,继续发送 Msg 2,以此类推,该情形是通信中的一种理想状态。

② 出现差错

如上述右图所示,发送方发送的报文出现差错导致接收方不能正确接收数据,出现差错的情况主要分为两种:

  • 发送方发送的 Msg 1 在中途丢失了,接收方完全没收到数据。
  • 接收方收到 Msg 1 后检测出现了差错,直接丢弃 Msg 1。

上面两种情形,接收方都不会回任何消息给发送方,此时就会触发超时传输机制,即发送方在等待一段时间后仍然没有收到接收方的确认,就认为刚才发送的数据丢失了,因此重传前面发送过的数据。

insert image description here
③ 确认丢失

当接收方回应的 Msg 1 确认报文在传输过程中丢失,发送方无法接收到确认报文。于是发送方等待一段时间后重传 Msg 1,接收方将收到重复的 Msg1 数据包,此时接收方会丢弃掉这个重复报文并向发送方再次发送 Msg1 的确认报文。

④ 确认迟到

当接收方回应的 Msg 1 确认报文由于网络各种原因导致发送方没有及时收到,此时发送方在超时重传机制的作用下再次发送了 Msg 数据包,接收方此时进行和确认丢失情形下相同的动作(丢弃重复的数据包并再次发送 Msg 1 确认报文)。发送方此时收到了接收方的确认数据包,于是继续进行数据发送。过了一段时间后,发送方收到了迟到的 Msg 1 确认包会直接丢弃。

上述四种情形即停止等待协议中所出现的所有可能情况。

3.17 TCP 最大连接数限制

面试高频指数:★★☆☆☆

  • Client 最大 TCP 连接数

client 在每次发起 TCP 连接请求时,如果自己并不指定端口的话,系统会随机选择一个本地端口(local port),该端口是独占的,不能和其他 TCP 连接共享。TCP 端口的数据类型是 unsigned short,因此本地端口个数最大只有 65536,除了端口 0不能使用外,其他端口在空闲时都可以正常使用,这样可用端口最多有 65535 个。

  • Server最大 TCP 连接数

server 通常固定在某个本地端口上监听,等待 client 的连接请求。不考虑地址重用(Unix 的 SO_REUSEADDR 选项)的情况下,即使 server 端有多个 IP,本地监听端口也是独占的,因此 server 端 TCP 连接 4 元组中只有客户端的 IP 地址和端口号是可变的,因此最大 TCP 连接为客户端 IP 数 × 客户端 port 数,对 IPV4,在不考虑 IP 地址分类的情况下,最大 TCP 连接数约为 2 的 32 次方(IP 数)× 2 的 16 次方(port 数),也就是 server 端单机最大 TCP 连接数约为 2 的 48 次方。

然而上面给出的是只是理论上的单机最大连接数,在实际环境中,受到明文规定(一些 IP 地址和端口具有特殊含义,没有对外开放)、机器资源、操作系统等的限制,特别是 sever 端,其最大并发 TCP 连接数远不能达到理论上限。对 server 端,通过增加内存、修改最大文件描述符个数等参数,单机最大并发 TCP 连接数超过 10 万 是没问题的。

3.19 TCP滑动窗口

滑动窗口作用: 每个TCP/IP主机支持全双工数据传输,因此TCP有两个滑动窗口:一个用于接收数据,另一个用于发送数据。

作用1:流量控制: 发送的字节不能超过窗口的大小。窗口的大小是变动的,应用程序根据自身性能状况(如内存不足)实时通知tcp协议栈要缩小窗口的大小。窗口大小一般为一个16bit位字段。

作用2:保证TCP可靠(确认重传):tcp是双工的,所以任意一端都维护着一个发送窗口和接收窗口。发送窗口只有收到对端对于本段发送窗口内字节的ACK确认,才会移动发送窗口的左边界;接收窗口只有在前面所有的段都确认的情况下才会移动左边界。当在前面还有字节未接收但收到后面字节的情况下,窗口不会移动,并不对后续字节确认。以此确保对端会对这些数据重传。

3.19 如果接收方滑动窗口满了,发送方会怎么做

面试高频指数:★★★★☆

基于 TCP 流量控制中的滑动窗口协议,我们知道接收方返回给发送方的 ACK 包中会包含自己的接收窗口大小,若接收窗口已满,此时接收方返回给发送方的接收窗口大小为 0,此时发送方会等待接收方发送的窗口大小直到变为非 0 为止,然而,接收方回应的 ACK 包是存在丢失的可能的,为了防止双方一直等待而出现死锁情况,此时就需要坚持计时器来辅助发送方周期性地向接收方查询,以便发现窗口是否变大【坚持计时器参考问题】,当发现窗口大小变为非零时,发送方便继续发送数据。

3.20 TCP 拥塞控制采用的四种算法

面试高频指数:★★★☆☆

  • 慢开始

当发送方开始发送数据时,由于一开始不知道网络负荷情况,如果立即将大量的数据字节传输到网络中,那么就有可能引起网络拥塞。一个较好的方法是在一开始发送少量的数据先探测一下网络状况,即由小到大的增大发送窗口(拥塞窗口 cwnd)。慢开始的慢指的是初始时令 cwnd为 1,即一开始发送一个报文段。如果收到确认,则 cwnd = 2,之后每收到一个确认报文,就令 cwnd = cwnd* 2。

但是,为了防止拥塞窗口增长过大而引起网络拥塞,另外设置了一个慢开始门限 ssthresh。

① 当 cwnd < ssthresh 时,使用上述的慢开始算法;

② 当 cwnd > ssthresh 时,停止使用慢开始,转而使用拥塞避免算法;

③ 当 cwnd == ssthresh 时,两者均可。

  • 拥塞避免

拥塞控制是为了让拥塞窗口 cwnd 缓慢地增大,即每经过一个往返时间 RTT (往返时间定义为发送方发送数据到收到确认报文所经历的时间)就把发送方的 cwnd 值加 1,通过让 cwnd 线性增长,防止很快就遇到网络拥塞状态。

当网络拥塞发生时,让新的慢开始门限值变为发生拥塞时候的值的一半,并将拥塞窗口置为 1 ,然后再次重复两种算法(慢开始和拥塞避免),这时一瞬间会将网络中的数据量大量降低。

  • 快重传

快重传算法要求接收方每收到一个失序的报文就立即发送重复确认,而不要等到自己发送数据时才捎带进行确认,假定发送方发送了 Msg 1 ~ Msg 4 这 4 个报文,已知接收方收到了 Msg 1,Msg 3 和 Msg 4 报文,此时因为接收到收到了失序的数据包,按照快重传的约定,接收方应立即向发送方发送 Msg 1 的重复确认。 于是在接收方收到 Msg 4 报文的时候,向发送方发送的仍然是 Msg 1 的重复确认。这样,发送方就收到了 3 次 Msg 1 的重复确认,于是立即重传对方未收到的 Msg 报文。由于发送方尽早重传未被确认的报文段,因此,快重传算法可以提高网络的吞吐量。

  • 快恢复

快恢复算法是和快重传算法配合使用的,该算法主要有以下两个要点:

① 当发送方连续收到三个重复确认,执行乘法减小,慢开始门限 ssthresh 值减半;

② 由于发送方可能认为网络现在没有拥塞,因此与慢开始不同,把 cwnd 值设置为 ssthresh 减半之后的值,然后执行拥塞避免算法,线性增大 cwnd。

3.21 TCP 粘包问题

面试高频指数:★★★☆☆

为什么会发生TCP粘包和拆包?

① 发送方写入的数据大于套接字缓冲区的大小,此时将发生拆包。

② 发送方写入的数据小于套接字缓冲区大小,由于 TCP 默认使用 Nagle 算法,只有当收到一个确认后,才将分组发送给对端,当发送方收集了多个较小的分组,就会一起发送给对端,这将会发生粘包。

③ 进行 MSS (最大报文长度)大小的 TCP 分段,当 TCP 报文的数据部分大于 MSS 的时候将发生拆包。

④ 发送方发送的数据太快,接收方处理数据的速度赶不上发送端的速度,将发生粘包。

常见解决方法

① 在消息的头部添加消息长度字段,服务端获取消息头的时候解析消息长度,然后向后读取相应长度的内容。

② 固定消息数据的长度,服务端每次读取既定长度的内容作为一条完整消息,当消息不够长时,空位补上固定字符。但是该方法会浪费网络资源。

③ 设置消息边界,也可以理解为分隔符,服务端从数据流中按消息边界分离出消息内容,一般使用换行符。

什么时候需要处理粘包问题?

当接收端同时收到多个分组,并且这些分组之间毫无关系时,需要处理粘包;而当多个分组属于同一数据的不同部分时,并不需要处理粘包问题。

3.22 TCP 报文包含哪些信息

面试高频指数:★★☆☆☆

  • 源端口和目的端口号:它用于多路复用/分解来自或送往上层应用的数据,其和 IP 数据报中的源 IP 与目的 IP 地址一同确定一条 TCP 连接。

  • 序号和确认号字段:序号是本报文段发送的数据部分中第一个字节的编号,在 TCP 传送的流中,每一个字节一个序号。例如一个报文段的序号为 100,此报文段数据部分共有 100 个字节,则下一个报文段的序号为200。序号确保了 TCP 传输的有序性。确认号,即 ACK,指明下一个想要收到的字节序号,发送 ACK时表明当前序号之前的所有数据已经正确接收。这两个字段的主要目的是保证数据可靠传输。

  • 首部长度:该字段指示了以 32 比特的字为单位的 TCP 的首部长度。其中固定字段长度为 20 字节,由于首部长度可能含有可选项内容,因此 TCP 报头的长度是不确定的,20 字节是 TCP 首部的最小长度。

  • 保留:为将来用于新的用途而保留。

  • 控制位:URG 表示紧急指针标志,该位为 1 时表示紧急指针有效,为 0 则忽略;ACK 为确认序号标志,即相应报文段包括一个对已被成功接收报文段的确认;PSH 为 push 标志,当该位为 1 时,则指示接收方在接收到该报文段以后,应尽快将这个报文段交给应用程序,而不是在缓冲区排队; RST 为重置连接标志,当出现错误连接时,使用此标志来拒绝非法的请求;SYN 为同步序号,在连接的建立过程中使用,例如三次握手时,发送方发送 SYN 包表示请求建立连接;FIN 为 finish 标志,用于释放连接,为 1 时表示发送方已经没有数据发送了,即关闭本方数据流。

  • 接收窗口:主要用于 TCP 流量控制。该字段用来告诉发送方其窗口(缓冲区)大小,以此控制发送速率,从而达到流量控制的目的。

  • 校验和:奇偶校验,此校验和是对整个 TCP 报文段,包括 TCP 头部和数据部分。该校验和是一个端到端的校验和,由发送端计算和存储,并由接收端进行验证,主要目的是检验数据是否发生改动,若检测出差错,接收方会丢弃该 TCP 报文。

  • 紧急数据指针:紧急数据用于告知紧急数据所在的位置,在URG标志位为 1 时才有效。当紧急数据存在时,TCP 必须通知接收方的上层实体,接收方会对紧急模式采取相应的处理。

  • 选项:该字段一般为空,可根据首部长度进行推算。主要有以下作用:
    ① TCP 连接初始化时,通信双方确认最大报文长度。
    ② 在高速数据传输时,可使用该选项协商窗口扩大因子。
    ③ 作为时间戳时,提供一个 较为精准的 RTT,主要为了更好的实现 TCP 流量控制协议。

  • 数据:TCP 报文中的数据部分也是可选的,例如在 TCP 三次握手和四次挥手过程中,通信双方交换的报文只包含头部信息,数据部分为空,只有当连接成功建立后,TCP 包才真正携带数据。

3.23 SYN FLOOD 是什么

面试高频指数:★★★☆☆

SYN Flood 是种典型的 DoS(拒绝服务)攻击,其目的是通过消耗服务器所有可用资源使服务器无法用于处理合法请求。通过重复发送初始连接请求(SYN)数据包,攻击者能够压倒目标服务器上的所有可用端口,导致目标设备根本不响应合法请求。

3.24 为什么服务端易受到 SYN 攻击

面试高频指数:★★★☆☆

insert image description here

SYN泛洪攻击,其实这个攻击主要利用的就是TCP三次握手机制的缺陷。

A(攻击者)发送TCP SYN,SYN是TCP三次握手中的第一个数据包,而当这个服务器返回ACK以后,A不再进行确认,那这个连接就处在了一个挂起的状态,也就是半连接的意思,那么服务器收不到再确认的一个消息,还会重复发送ACK给A。这样一来就会更加浪费服务器的资源。A就对服务器发送非法大量的这种TCP连接,由于每一个都没法完成握手的机制,所以它就会消耗服务器的内存最后可能导致服务器死机,就无法正常工作了。更进一步说,如果这些半连接的握手请求是恶意程序发出,并且持续不断,那么就会导致服务端较长时间内丧失服务功能。这种攻击方式就称为SYN泛洪攻击。

优化方式:

  • 缩短SYN Timeout时间 ,使得主机尽快释放半连接的占用或者采用SYN cookie设置
  • 记录IP,若连续受到某个IP的重复SYN报文,从这个IP地址来的包会被一概丢弃。

3.26 TCP如何实现长连接

  1. 应用层的心跳机制
    通过应用程序自身发送心跳包去检测连接是否正常,基本方法就是在服务器端设置一个Timer事件,在一定时间段内向客户端一个心跳数据包,若在一定时间内没有收到客户端的回应,那么就会认为客户端掉线;同样客户端在一定时间段内没有收到服务器的心跳包,则判断与服务器端连接断开。
  2. TCP的keepalive机制
    其实跟心跳机制大同小异,无论是客户端还是服务器端,只要有一方开启Keepalive功能后,就会在特定的时间段内发送心跳包,对端在收到心跳包后进行回复,表示自己在线。默认的Keepalive超时时间为2小时,探测次数为5次,但超时时间可以手动设置成合理的时间段。

第四部分、网络层

4.1 IP 协议的定义和作用

面试高频指数:★★★☆☆

IP 协议(Internet Protocol)又称互联网协议,是支持网间互联的数据包协议。该协议工作在网络层,主要目的就是为了提高网络的可扩展性,和传输层 TCP 相比,IP 协议提供一种无连接/不可靠、尽力而为的数据包传输服务,其与TCP协议(传输控制协议)一起构成了TCP/IP 协议族的核心。IP 协议主要有以下几个作用:

  • 寻址和路由:在IP 数据包中会携带源 IP 地址和目的 IP 地址来标识该数据包的源主机和目的主机。IP 数据报在传输过程中,每个中间节点(IP 网关、路由器)只根据网络地址进行转发,如果中间节点是路由器,则路由器会根据路由表选择合适的路径。IP 协议根据路由选择协议提供的路由信息对 IP 数据报进行转发,直至抵达目的主机。
  • 分段与重组:IP 数据包在传输过程中可能会经过不同的网络,在不同的网络中数据包的最大长度限制是不同的,IP 协议通过给每个 IP 数据包分配一个标识符以及分段与组装的相关信息,使得数据包在不同的网络中能够传输,被分段后的 IP 数据报可以独立地在网络中进行转发,在到达目的主机后由目的主机完成重组工作,恢复出原来的 IP 数据包。

4.2 域名和 IP 的关系,一个 IP 可以对应多个域名吗

面试高频指数:★★☆☆☆

IP 在同一个网络中是唯一的,用来标识每一个网络上的设备,其相当于一个人的身份证号;域名在同一个网络中也是唯一的,就像一个人的名字,绰号。假如你有多个不同的绰号,你的朋友可以用其中任何一个绰号叫你,但你的身份证号码却是唯一的。由此我们可以看出一个域名只能对应一个 IP 地址,是一对一的关系;而一个 IP 却可以对应多个域名,是一对多的关系。

4.3 IPV4 地址不够如何解决

面试高频指数:★★★☆☆

  • DHCP:动态主机配置协议。动态分配 IP 地址,只给接入网络的设备分配IP地址,因此同一个 MAC 地址的设备,每次接入互联网时,得到的IP地址不一定是相同的,该协议使得空闲的 IP 地址可以得到充分利用。
  • CIDR:无类别域间路由。CIDR 消除了传统的 A 类、B 类、C 类地址以及划分子网的概念,因而更加有效的分配 IPv4 的地址空间,但无法从根本上解决地址耗尽问题。
  • NAT:网络地址转换协议。我们知道属于不同局域网的主机可以使用相同的 IP 地址,从而一定程度上缓解了 IP 资源枯竭的问题。然而主机在局域网中使用的 IP 地址是不能在公网中使用的,当局域网主机想要与公网进行通信时, NAT 方法可以将该主机 IP 地址转换成全球 IP 地址。该协议能够有效解决 IP 地址不足的问题。
  • IPv6 :作为接替 IPv4 的下一代互联网协议,其可以实现 2 的 128 次方个地址,而这个数量级,即使是给地球上每一颗沙子都分配一个IP地址,该协议能够从根本上解决 IPv4 地址不够用的问题。

4.4 路由器的分组转发流程

面试高频指数:★★★☆☆

① 从 IP 数据包中提取出目的主机的 IP 地址,找到其所在的网络;

② 判断目的 IP 地址所在的网络是否与本路由器直接相连,如果是,则不需要经过其它路由器直接交付,否则执行 ③;

③ 检查路由表中是否有目的 IP 地址的特定主机路由。如果有,则按照路由表传送到下一跳路由器中,否则执行 ④;

④ 逐条检查路由表,若找到匹配路由,则按照路由表转发到下一跳路由器中,否则执行步骤 ⑤;

⑤ 若路由表中设置有默认路由,则按照默认路由转发到默认路由器中,否则执行步骤 ⑥;

⑥ 无法找到合适路由,向源主机报错。

4.5 路由器和交换机的区别

面试高频指数:★★★★☆

  • 交换机:交换机用于局域网,利用主机的物理地址(MAC 地址)确定数据转发的目的地址,它工作与数据链路层。
  • 路由器:路由器通过数据包中的目的 IP 地址识别不同的网络从而确定数据转发的目的地址,网络号是唯一的。路由器根据路由选择协议和路由表信息从而确定数据的转发路径,直到到达目的网络,它工作于网络层。

4.6 ICMP 协议概念/作用

面试高频指数:★★☆☆☆

ICMP(Internet Control Message Protocol)是因特网控制报文协议,主要是实现 IP 协议中未实现的部分功能,是一种网络层协议。该协议并不传输数据,只传输控制信息来辅助网络层通信。其主要的功能是验证网络是否畅通(确认接收方是否成功接收到 IP 数据包)以及辅助 IP 协议实现可靠传输(若发生 IP 丢包,ICMP 会通知发送方 IP 数据包被丢弃的原因,之后发送方会进行相应的处理)。

4.7 ICMP 的应用

面试高频指数:★☆☆☆☆

  • Ping

Ping(Packet Internet Groper),即因特网包探测器,是一种工作在网络层的服务命令,主要用于测试网络连接量。本地主机通过向目的主机发送 ICMP Echo 请求报文,目的主机收到之后会发送 Echo 响应报文,Ping 会根据时间和成功响应的次数估算出数据包往返时间以及丢包率从而推断网络是否通常、运行是否正常等。

  • TraceRoute

TraceRoute 是 ICMP 的另一个应用,其主要用来跟踪一个分组从源点耗费最少 TTL 到达目的地的路径。TraceRoute 通过逐渐增大 TTL 值并重复发送数据报来实现其功能,首先,TraceRoute 会发送一个 TTL 为 1 的 IP 数据报到目的地,当路径上的第一个路由器收到这个数据报时,它将 TTL 的值减 1,此时 TTL = 0,所以路由器会将这个数据报丢掉,并返回一个差错报告报文,之后源主机会接着发送一个 TTL 为 2 的数据报,并重复此过程,直到数据报能够刚好到达目的主机。此时 TTL = 0,因此目的主机要向源主机发送 ICMP 终点不可达差错报告报文,之后源主机便知道了到达目的主机所经过的路由器 IP 地址以及到达每个路由器的往返时间。

4.8 两台电脑连起来后 ping 不通,你觉得可能存在哪些问题?

面试高频指数:★★★☆☆

  • 首先看网络是否连接正常,检查网卡驱动是否正确安装。
  • 局域网设置问题,检查 IP 地址是否设置正确。
  • 看是否被防火墙阻拦(有些设置中防火墙会对 ICMP 报文进行过滤),如果是的话,尝试关闭防火墙 。
  • 看是否被第三方软件拦截。
  • 两台设备间的网络延迟是否过大(例如路由设置不合理),导致 ICMP 报文无法在规定的时间内收到。

4.9 ARP 地址解析协议的原理和地址解析过程

面试高频指数:★★★★☆

ARP(Address Resolution Protocol)是地址解析协议的缩写,该协议提供根据 IP 地址获取物理地址的功能,它工作在第二层,是一个数据链路层协议,其在本层和物理层进行联系,同时向上层提供服务。当通过以太网发送 IP 数据包时,需要先封装 32 位的 IP 地址和 48位 MAC 地址。在局域网中两台主机进行通信时需要依靠各自的物理地址进行标识,但由于发送方只知道目标 IP 地址,不知道其 MAC 地址,因此需要使用地址解析协议。 ARP 协议的解析过程如下:

① 首先,每个主机都会在自己的 ARP 缓冲区中建立一个 ARP 列表,以表示 IP 地址和 MAC 地址之间的对应关系;

② 当源主机要发送数据时,首先检查 ARP 列表中是否有 IP 地址对应的目的主机 MAC 地址,如果存在,则可以直接发送数据,否则就向同一子网的所有主机发送 ARP 数据包。该数据包包括的内容有源主机的 IP 地址和 MAC 地址,以及目的主机的 IP 地址。

③ 当本网络中的所有主机收到该 ARP 数据包时,首先检查数据包中的 目的 主机IP 地址是否是自己的 IP 地址,如果不是,则忽略该数据包,如果是,则首先从数据包中取出源主机的 IP 和 MAC 地址写入到 ARP 列表中,如果已经存在,则覆盖,然后将自己的 MAC 地址写入 ARP 响应包中,告诉源主机自己是它想要找的 MAC 地址。

④ 源主机收到 ARP 响应包后。将目的主机的 IP 和 MAC 地址写入 ARP 列表,并利用此信息发送数据。如果源主机一直没有收到 ARP 响应数据包,表示 ARP 查询失败。

4.10 网络地址转换 NAT

面试高频指数:★★☆☆☆

NAT(Network Address Translation),即网络地址转换,它是一种把内部私有网络地址翻译成公有网络 IP 地址的技术。该技术不仅能解决 IP 地址不足的问题,而且还能隐藏和保护网络内部主机,从而避免来自外部网络的攻击。

NAT 的实现方式主要有三种:

  • 静态转换:内部私有 IP 地址和公有 IP 地址是一对一的关系,并且不会发生改变。通过静态转换,可以实现外部网络对内部网络特定设备的访问,这种方式原理简单,但当某一共有 IP 地址被占用时,跟这个 IP 绑定的内部主机将无法访问 Internet。
  • 动态转换:采用动态转换的方式时,私有 IP 地址每次转化成的公有 IP 地址是不唯一的。当私有 IP 地址被授权访问 Internet 时会被随机转换成一个合法的公有 IP 地址。当 ISP 通过的合法 IP 地址数量略少于网络内部计算机数量时,可以采用这种方式。
  • 端口多路复用:该方式将外出数据包的源端口进行端口转换,通过端口多路复用的方式,实现内部网络所有主机共享一个合法的外部 IP 地址进行 Internet 访问,从而最大限度地节约 IP 地址资源。同时,该方案可以隐藏内部网络中的主机,从而有效避免来自 Internet 的攻击。

4.11 运输层协议和网络层协议的区别

面试高频指数:★★★☆☆

网络层协议负责提供主机间的逻辑通信;运输层协议负责提供进程间的逻辑通信。

第五部分:数据链路层

5.1 MAC 地址和 IP 地址分别有什么作用

面试高频指数:★★★☆☆

  • MAC 地址是数据链路层和物理层使用的地址,是写在网卡上的物理地址。MAC 地址用来定义网络设备的位置。
  • IP 地址是网络层和以上各层使用的地址,是一种逻辑地址。IP 地址用来区别网络上的计算机。

5.2 为什么有了 MAC 地址还需要 IP 地址

面试高频指数:★★★★☆

  • 如果我们只使用 MAC 地址进行寻址的话,我们需要路由器记住每个 MAC
    地址属于哪一个子网,不然每一次路由器收到数据包时都要满世界寻找目的 MAC 地址。而我们知道 MAC 地址的长度为 48 位,也就是说最多总共有 2 的 48 次方个 MAC 地址,这就意味着每个路由器需要 256 T 的内存,这显然是不现实的。

和 MAC 地址不同,IP 地址是和地域相关的,在一个子网中的设备,我们给其分配的 IP 地址前缀都是一样的,这样路由器就能根据 IP 地址的前缀知道这个设备属于哪个子网,剩下的寻址就交给子网内部实现,从而大大减少了路由器所需要的内存。

5.3 为什么有了 IP 地址还需要 MAC 地址

面试高频指数:★★★★☆

只有当设备连入网络时,才能根据他进入了哪个子网来为其分配 IP 地址,在设备还没有 IP 地址的时候或者在分配 IP 地址的过程中,我们需要 MAC 地址来区分不同的设备。

5.4 私网地址和公网地址之间进行转换:同一个局域网内的两个私网地址,经过转换之后外面看到的一样吗

面试高频指数:★★★☆☆

当采用静态或者动态转换时,由于一个私网 IP 地址对应一个公网地址,因此经过转换之后的公网 IP 地址是不同的;而采用端口复用方式的话,在一个子网中的所有地址都采用一个公网地址,但是使用的端口是不同的。

第六部分:物理层

6.1 物理层主要做什么事情

面试高频指数:★★☆☆☆

作为 OSI 参考模型最低的一层,物理层是整个开放系统的基础,该层利用传输介质为通信的两端建立、管理和释放物理连接,实现比特流的透明传输。物理层考虑的是怎样才能在连接各种计算机的传输媒体上传输数据比特流,其尽可能地屏蔽掉不同种类传输媒体和通信手段的差异,使物理层上面的数据链路层感觉不到这些差异,这样就可以使数据链路层只考虑完成本层的协议和服务,而不必考虑网络的具体传输媒体和通信手段是什么。

6.2 主机之间的通信方式

面试高频指数:★★☆☆☆

  • 单工通信:也叫单向通信,发送方和接收方是固定的,消息只能单向传输。例如采集气象数据、家庭电费,网费等数据收集系统,或者打印机等应用主要采用单工通信。
  • 半双工通信:也叫双向交替通信,通信双方都可以发送消息,但同一时刻同一信道只允许单方向发送数据。例如传统的对讲机使用的就是半双工通信。
  • 全双工通信:也叫双向同时通信,全双工通信允许通信双方同时在两个方向是传输,其要求通信双方都具有独立的发送和接收数据的能力。例如平时我们打电话,自己说话的同时也能听到对面的声音。

第七部分:计算机网络中的安全

7.1 安全攻击有哪些

面试高频指数:★★☆☆☆

网络安全攻击主要分为被动攻击和主动攻击两类:

  • 被动攻击:攻击者窃听和监听数据传输,从而获取到传输的数据信息,被动攻击主要有两种形式:消息内容泄露攻击和流量分析攻击。由于攻击者并没有修改数据,使得这种攻击类型是很难被检测到的。
  • 主动攻击:攻击者修改传输的数据流或者故意添加错误的数据流,例如假冒用户身份从而得到一些权限,进行权限攻击,除此之外,还有重放、改写和拒绝服务等主动攻击的方式。

7.2 ARP 攻击

面试高频指数:★★★☆☆

在 ARP 的解析过程中,局域网上的任何一台主机如果接收到一个 ARP 应答报文,并不会去检测这个报文的真实性,而是直接记入自己的 ARP 缓存表中。并且这个 ARP 表是可以被更改的,当表中的某一列长时间不适使用,就会被删除。ARP 攻击就是利用了这一点,攻击者疯狂发送 ARP 报文,其源 MAC 地址为攻击者的 MAC 地址,而源 IP 地址为被攻击者的 IP 地址。通过不断发送这些伪造的 ARP 报文,让网络内部的所有主机和网关的 ARP 表中被攻击者的 IP 地址所对应的 MAC 地址为攻击者的 MAC 地址。这样所有发送给被攻击者的信息都会发送到攻击者的主机上,从而产生 ARP 欺骗。通常可以把 ARP 欺骗分为以下几种:

  • 洪泛攻击

攻击者恶意向局域网中的网关、路由器和交换机等发送大量 ARP 报文,设备的 CPU 忙于处理 ARP 协议,而导致难以响应正常的服务请求。其表现通常为:网络中断或者网速很慢。

  • 欺骗主机

这种攻击方式也叫仿冒网关攻击。攻击者通过 ARP 欺骗使得网络内部被攻击主机发送给网关的信息实际上都发送给了攻击者,主机更新的 ARP 表中对应的 MAC 地址为攻击者的 MAC。当用户主机向网关发送重要信息使,该攻击方式使得用户的数据存在被窃取的风险。

  • 欺骗网关

该攻击方式和欺骗主机的攻击方式类似,不过这种攻击的欺骗对象是局域网的网关,当局域网中的主机向网关发送数据时,网关会把数据发送给攻击者,这样攻击者就会源源不断地获得局域网中用户的信息。该攻击方式同样会造成用户数据外泄。

  • 中间人攻击

攻击者同时欺骗网关和主机,局域网的网关和主机发送的数据最后都会到达攻击者这边。这样,网关和用户的数据就会泄露。

  • IP 地址冲突

攻击者对局域网中的主机进行扫描,然后根据物理主机的 MAC 地址进行攻击,导致局域网内的主机产生 IP 冲突,使得用户的网络无法正常使用。

7.3 对称加密和非对称的区别,非对称加密有哪些

面试高频指数:★★★★☆

  • 加密和解密的过程不同:对称加密和解密过程使用同一个密钥;非对称加密中加密和解密采用公钥和私钥两个密钥,一般使用公钥进行加密,使用私钥进行解密。
  • 加密和解密的速度不同:对称加密和解密速度较快,当数据量比较大时适合使用;非对称加密和解密时间较长,速度相对较慢,适合少量数据传输的场景。
  • 传输的安全性不同:采用对称加密方式进行通信时,收发双方在数据传送前需要协定好密钥,而这个密钥还有可能被第三方窃听到的,一旦密钥泄漏,之后的通信就完全暴漏给攻击者了;非对称加密采用公钥加密和私钥解密的方式,其中私钥是基于不同的算法生成的随机数,公钥可以通过私钥通过一定的算法推导得出,并且私钥到公钥的推导过程是不可逆的,也就是说公钥无法反推导出私钥,即使攻击者窃听到传输的公钥,也无法正确解出数据,所以安全性较高。

常见的非对称加密算法主要有:RSA、Elgamal、背包算法、Rabin、D-H 算法等等。

7.4 RSA 和 AES 算法有什么区别

面试高频指数:★★★☆☆

  • RSA

Using asymmetric encryption, the public key is used for encryption and the private key for decryption. The length of the private key is generally longer. In addition, due to the need for operations such as exponentiation and modulus of large numbers, the operation speed is relatively slow, and it is not suitable for encrypting large amounts of data files.

  • AES

The symmetric encryption method is adopted, the longest key length is only 256 bits, the encryption and decryption speed is fast, and it is easy to realize by hardware. Due to the symmetric encryption, both communicating parties need to know the encryption key before data transmission.

Based on the characteristics of the above two algorithms, generally use RSA to transmit the key to the other party, and then use AES for encrypted communication.

7.5 What are DDoS and how to prevent it

Interview high frequency index: ★★★☆☆

DDoS is a distributed denial of service attack, which means that multiple attackers in different locations launch attacks on one or several targets at the same time, or an attacker controls multiple machines in different locations and uses these machines to attack victims simultaneously. attack. Compared with a single DoS attack, DDoS uses hundreds or thousands of hosts that have been invaded and added attack processes to launch network attacks together.

There are two main forms of DDoS attacks: traffic attacks and resource exhaustion attacks. The former is mainly aimed at network bandwidth. The attacker and the victim host launch a large number of attacks at the same time, causing the network bandwidth to be blocked, thus submerging legitimate network data packets; the latter mainly attacks the server. A large number of attack packets will exhaust server resources or CPU Too busy with kernel applications to provide network services.

Common DDos attacks mainly include: TCP flood attack (SYN Flood), radiation attack (DrDos), CC attack (HTTP Flood), etc.

For traffic attacks in DDoS, the most direct method is to increase the bandwidth. In theory, as long as the bandwidth is larger than the attack traffic, this method is very costly. Under the premise of sufficient network bandwidth, we should try to improve the configuration of hardware facilities such as routers, network cards, and switches.

For resource exhaustion attacks, we can upgrade the host server hardware, so that the server can effectively resist massive SYN attack packets under the premise that the network bandwidth is guaranteed. We can also install a professional anti-DDoS firewall to fight traffic-type attacks such as SYN Flood. In addition, load balancing, CDN and other technologies can effectively fight against DDoS attacks

Guess you like

Origin blog.csdn.net/luanfenlian0992/article/details/120007956