TCP is a connection-oriented protocol. before sending data, any interaction must be completed based on the connection. This blog post discusses how to establish a connection. I open a port on the local machine, and telnet the port. after the connection, it will be disconnected, and tcpdump will be used to output the TCP data transmission. if you are not familiar with the TCP header, refer to Bo
TCP is a connection-oriented protocol. before sending data, any interaction must be completed based on the connection. This blog post discusses how to establish a connection.
I open a port on the local machine and telnet the port. after the connection, it will be disconnected and usedTcpdumpOutput TCP data transmission. if you are not familiar with the TCP header, refer to [TCP/IP: Understanding TCP]
I. test output
Call telnet
Telnet to 192.168.1.105 5210
The output is as follows:
[Root @ localhost/] # tcpdump-S tcp port 5210
Tcpdump: verbose output suppressed, use-v or-vv for full protocol decode
Listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
18:36:14. 762904 IP 192.168.1.102.20.88> 192.168.1.105.5210: S 313624018: 313624018 (0) win 8192
18:36:14. 773698 IP 192.168.1.105.5210> 192.168.1.102.20.88: S 4078776998: 4078776998 (0) ack 313624019 win 5840
18:36:14. 773902 IP 192.168.1.102.20.88> 192.168.1.105.5210:. ack 4078776999 win 16425
18:36:14. 764642 IP 192.168.1.105.5210> 192.168.1.102.20.88: P 4078776999: 4078777013 (14) ack 313624019 win 46
18:36:14. 765099 IP 192.168.1.105.5210> 192.168.1.102.20.88: F 4078777013: 4078777013 (0) ack 313624019 win 46
18:36:14. 765341 IP 192.168.1.102.20.88> 192.168.1.105.5210:. ack 4078777014 win 16421
18:36:14. 775762 IP 192.168.1.102.20.88> 192.168.1.105.5210: F 313624019: 313624019 (0) ack 4078777014 win 16421
18:36:14. 775802 IP 192.168.1.105.5210> 192.168.1.102.20.88:. ack 313624020 win 46
【
Note: In the tcpdump command above, the-S option is not mandatory, but only to output the specific ISN value, rather than the first ISN offset value randomly based on time. If the-S option is not available, the output will be in the following format:
[Root @ localhost/] # tcpdump tcp port 5210
Tcpdump: verbose output suppressed, use-v or-vv for full protocol decode
Listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
18:52:33. 996202 IP 192.168.1.102.49317> 192.168.1.105.5210: S 2455039660: 2455039660 (0) win 8192
18:52:34. 012040 IP 192.168.1.105.5210> 192.168.1.102.49317: S 828175062: 828175062 (0) ack 2455039661 win 5840
18:52:34. 012091 IP 192.168.1.102.49317> 192.168.1.105.5210:. ack 1 win 16425
18:52:33. 999654 IP 192.168.1.105.5210> 192.168.1.102.49317: P (14) ack 1 win 46
18:52:34. 000172 IP 192.168.1.105.5210> 192.168.1.102.49317: F (0) ack 1 win 46
18:52:34. 000432 IP 192.168.1.102.49317> 192.168.1.105.5210:. ack 16 win 16421
18:52:34. 011735 IP 192.168.1.102.49317> 192.168.1.105.5210: F (0) ack 16 win 16421
18:52:34. 011764 IP 192.168.1.105.5210> 192.168.1.102.49317:. ack 2 win 46
]
Because there is no data, these outputs only contain the first data of the TCP connection. the format starting from each output row is:
SOURCE> purpose: Flag
The source and destination are easy to understand, indicating the IP address and port. the flag indicates the six flag bits in the TCP header. the output above contains S, P, and F. Five signs may appear here, which are shown as follows:
S. SYN, used for synchronization.
F. FIN, used by the sender to send data.
R. Indicates RST. reset the connection.
P. PSH, send data to the receiving process as quickly as possible.
.. Indicates that the bits of the above four flags are 0.
2. data row analysis
As shown in the second line above
18:36:14. 773698 IP 192.168.1.105.5210> 192.168.1.102.20.88: S 4078776998: 4078776998 (0) ack 313624019 win 5840
The 4078776998: 4078776998 (0) field in the row indicates that the group sequence number is 4078776998, and the number of data bytes in the message is 0. Tcpdump shows that the format of this field is the starting sequence number: the hidden ending sequence number (number of data bytes). because the data byte is 0, the starting sequence number is equal to the ending sequence number. This field is not displayed in all output rows. it is only displayed in the following two cases:
The packet segment must contain at least one data byte.
SYN, FIN, and RST are set to 1.
Ack 313624019 indicates the sequence number. it is displayed only when the ACK flag bit of the header is set to 1. Because the second row is the data that the server sends to the client, it indicates the response from the server to the client. In fact, the first row contains the serial number 313624018, therefore, adding 1 as the response is also a process of three handshakes.
Win 5840 indicates the window size. There is no data exchange, and the window is a default window value.
Mss1460 indicates the maximum packet segment length option specified by the sender. the sender will not receive TCP packets exceeding this length. From the tcpdump results, we can see that both sides of the connection should announce their respective MSS. First, we simply think that both ends are directly connected, because the MSS setting size has a certain relationship with MTU, MTU and path selection are also more or less related.
III. connection process
Based on the above tcpdump output, we will discuss the process of establishing a connection. this is of course three steps:
IP 192.168.1.102.20.88> 192.168.1.105.5210: S. Indicates that the client sends a SYN segment, connects to the server port, and contains the initialization serial number (ISN, as shown in the preceding output as 313624018). This SYN segment is the first packet, both the window size and MSS value are reported.
IP 192.168.1.105.5210> 192.168.1.102.20.88: S. It indicates that the server sends back the SYN packet segment containing the initial serial number of the server as the response, and sets the serial number to the customer's ISN plus 1 to confirm the SYN packet of the client, here, a SYN occupies a sequence number and tells the window size and MSS value.
IP 192.168.1.102.000088> 192.168.1.105.5210:. ack 4078776999win16425. this is a client response to server serial number 4078776998. The Flag "." indicates that all four SFRP identifiers are 0. of course, the ACK flag is 1 at this time.
4. connection timeout
When a connection cannot be established, for example, TELNET a port that does not provide services, telnet will continuously send SYN numbers. In the following example, a port that does not provide services is connected, but the connection is not directly rejected. in this case, when the client reconnects to a certain number of times and reaches the specified timeout time, the connection times out, and the output is as follows:
19:02:19. 526955 IP 192.168.1.105.38890> 4.38.89.117.broad.nj.js.dynamic.163data.com.cn.8625: S 1446215798: 1446215798 (0) win 5840
19:02:22. 529755 IP 192.168.1.105.38890> 4.38.89.117.broad.nj.js.dynamic.163data.com.cn.8625: S 1446215798: 1446215798 (0) win 5840
19:02:28. 532807 IP 192.168.1.105.38890> 4.38.89.117.broad.nj.js.dynamic.163data.com.cn.8625: S 1446215798: 1446215798 (0) win 5840
19:02:40. 537427 IP 192.168.1.105.38890> 4.38.89.117.broad.nj.js.dynamic.163data.com.cn.8625: S 1446215798: 1446215798 (0) win 5840
19:03:04. 583216 IP 192.168.1.105.38890> 4.38.89.117.broad.nj.js.dynamic.163data.com.cn.8625: S 1446215798: 1446215798 (0) win 5840
19:03:53. 302771 IP 192.168.1.105.38890> 4.38.89.117.broad.nj.js.dynamic.163data.com.cn.8625: S 1446215798: 1446215798 (0) win 5840
From the above output, we can see that after the first connection fails to respond, another SYN packet is sent three seconds later, which is 6 seconds, 12 seconds, and 24 seconds, 48 seconds ......, the maximum number of resends is reached. the time interval varies with TCP implementation Machines. for example, if the connection times out in 48 seconds, some times out after 75 seconds.