详解TCP报文结构

Last Updated: 2023-09-15 05:22:30 Friday

-- TOC --

TCP报头的前4个字节(source port and destination port)与UDP相同!

TCP Header Format, from RFC793

0                   1                   2                   3   
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |           |U|A|P|R|S|F|                               |
| Offset| Reserved  |R|C|S|S|Y|I|            Window             |
|       |           |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum            |         Urgent Pointer        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             data                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                        TCP Header Format

        Note that one tick mark represents one bit position.

                            Figure 3.

序列号(Sequence Number):在建立连接时由计算机生成的随机数作为其初始值,通过 SYN 包传给接收端主机,每发送一次数据,就「累加」一次该「数据字节数」的大小。这个字段用来解决网络包乱序问题。

确认应答号(Acknowledge Number):指下一次「期望」收到的数据的起始序列号,发送端收到这个确认应答以后可以认为在这个序号以前的数据都已经被正常接收。用来解决丢包的问题。

Data Offset:这4个bit现在都被解释成Header Length,4字节的倍数!因此TCP Header最长为15*4=60bytes(如果没有Options,TCP头刚好20bytes,因此Options+padding区域最长40bytes。UDP报头长度固定,所以没有这个字段。)。

控制位:

The URG flag is used to send data on a second channel of a TCP connection. It doesn't make sense to set it unless you're also sending data. The data will be kept in a separate buffer on the receiving end, the program is signaled that there's urgent data available, and it reads using a special flag to the recv system call.

AFAIK (As Far As I Know), the only protocol that ever used it is FTP, where you set the URG flag if you wanted to send a command during a transfer. It would be presumed that the server was otherwise busy sending data and not listening for new commands, but by setting the URG flag the server was interrupted by the special signal.

新版TCP的报文头:

from RFC3168

0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|               |               | C | E | U | A | P | R | S | F |
| Header Length |    Reserved   | W | C | R | C | S | S | Y | I |
|               |               | R | E | G | K | H | T | N | N |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

Figure 4: The new definition of bytes 13 and 14 of the TCP
        Header.

窗口字段(Window):占16比特,此字段用来进行通信双方流量控制,单位为字节数。

TCP校验和字段(Checksum):占16比特。计算范围包括整个TCP报文段,即TCP头部和TCP数据,还有一个伪IP头参与计算。(UDP的checksum计算与TCP类似)

Urgent Pointer:URG置位后,这个字段才有效。

Options:

Padding:确保header部分是4字节的倍数。

本文链接:https://cs.pynote.net/net/tcp/202302271/

-- EOF --

-- MORE --