有关tcp字段的详解

TCP首部包含20字节必选字段和40字节可选字段,涉及紧急比特URG、确认比特ACK、推送功能PSH、复位比特RST、同步比特SYN和终止比特FIN。这些标志位分别用于紧急数据传输、确认连接、数据推送、错误恢复、连接建立和连接释放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




TCP首部字段有必选字段20字节,另外有40字节的可选字段


必选字段如下:

源端口(Source Port),目标端口(Destination Port) 各2字节

源端口号,标识主机上发起传送的应用程序;目的端口标识主机上传送要到达的应用程序。源端和目的端的端口号,用于寻找发端和收端应用进程。这两个值加上I P首部中的源端I P地址和目的端I P地址唯一确定一个TCP连接。一个I P地址和一个端口号有时也称为一个插口(socket),插口对(socket pair)(包含客户I P地址、客户端口号、服务器 I P地址和服务器端口号的四元组 )可唯一确定互联网络中每个TCP连接的双方。IP+TCP端口唯一确定一个TCP连接。
  TCP协议通过使用"端口"来标识源端和目标端的应用进程。 端口号可以使用0到65535之间的任何数字。在收到服务请求时,操作系统动态地为客户端的应用程序分配端口号。在服务器端,每种服务在"众所周知的端 口"(Well-Know Port)为用户提供服务。


封包序号(Sequence Number) 4字节
由于 TCP 封包必须要带入 IP 封包当中,所以如果 TCP 数据太大时(大于 IP 封包的容许程度), 就得要进行分段。这个 Sequence Number 就是记录每个封包的序号,可以让收受端重新将 TCP 的数据组合起来。
序号字段的值则指的是本报文段所发送的数据的第一个字节的序号。

确认号(Acknowledge Number) 4字节
为了确认主机端确实有收到我们 client 端所送出的封包数据,我们 client 端当然希望能够收到主机方面的响应,那就是这个 Acknowledge Number 的用途了。 当 client 端收到这个确认码时,就能够确定之前传递的封包已经被正确的收下了。这个号是期望收到对方的下一个报文段的数据的第一个字节的序号。

数据偏移(Data Offset)4比特
和IP数据包头部一样,也有个Options字段,长度是不固定的,而为了要确认整个TCP封包大小,就需要这个标志来说明整个封包区段的起始位置。
这个字段指出TCP报文段的数据起始处距离 TCP报文段的起始处有多远。“数据偏移”的单位不是字节而是32bit字(4字节为计算单位)。
没有任何选项字段的TCP头部长度为20字节;最多可以有60字节的TCP头部。

保留字段(Reserved) 占6比特
保留为今后使用,但目前应置为0。 

状态控制码(Code,Control Flag)
标志位字段(U、A、P、R、S、F):占6比特。各 比特的含义如下:
  • URG:紧急比特(urgent),当URG=1时,表明紧急指针字段有效,代表该封包为紧急封包。它告诉系统此报文段中有紧急数据,应尽快传送(相当于高优先级的数据), 且上图中的 Urgent Pointer 字段也会被启用。
  • ACK:确认比特(Acknowledge)。只有当ACK=1时确认号字段才有效,代表这个封包为确认封包。当ACK=0时,确认号无效。
  • PSH:(Push function)若为1时,代表要求对方立即传送缓冲区内的其他对应封包,而无需等缓冲满了才送。
  • RST:复位比特(Reset) ,当RST=1时,表明TCP连接中出现严重差错(如由于主机崩溃或其他原因),必须释放连接,然后再重新建立运输连接。
  • SYN:同步比特(Synchronous),SYN置为1,就表示这是一个连接请求或连接接受报文,通常带有 SYN 标志的封包表示『主动』要连接到对方的意思。。
  • FIN:终止比特(Final),用来释放一个连接。当FIN=1时,表明此报文段的发送端的数据已发送完毕,并要求释放运输连接。

滑动窗口(Window) 占2字节
窗口字段用来控制对方发送的数据量,可以告知对方目前本身有的缓冲器容量(Receive Buffer) 还可以接收封包。当 Window=0 时,代表缓冲器已经额满,所以应该要暂停传输数据。单位为字节。TCP连接的一端根据设置的缓存空间大小确定自己的接收窗口大小,然后通知对方以确定对方的发送窗口的上限,这个值是本机期望一次接收的字节数。

TCP校验和(Checksum)  占2字节
当数据要由发送端送出前,会进行一个检验的动作,并将该动作的检验值标注在这个字段上; 而接收者收到这个封包之后,会再次的对封包进行验证,并且比对原发送的 Checksum 值是否相符,如果相符就接受,若不符就会假设该封包已经损毁,进而要求对方重新发送此封包!在计算检验和时,要在TCP报文段的前面加上12字节的伪首部。

紧急指针(Urgent Pointer)  占2字节
这个字段是在 Code 字段内的 URG = 1 时才会产生作用。可以告知紧急数据所在的位置(紧急指针指出在本报文段中的紧急数据的最后一个字节的序号)。

选项(Options) 长度可变。
TCP首部可以有多达40字节的可选信息,用于把附加信息传递给终点,或用来对齐其它选项。
目前此字段仅应用于表示接收端可以接收的最大数据区段容量,若此字段不使用, 表示可以使用任意数据区段的大小。 这个字段较少使用。
TCP首部的主要选项:
最大报文段长度MSS(Maximum Segment Size)是TCP报文段中的数据字段的最大长度。MSS告诉对方TCP:“我的缓存所能接收的报文段的数据字段的最大长度是MSS个字节。”

填充字段(Padding)
如同 IP 封包需要有固定的 32bits 表头一样, Options 由于字段为非固定, 所以也需要 Padding 字段来加以补齐才行。同样也是 32 bits 的整数。
这是为了使整个首部长度是4字节的整数倍。

备注:

1、MSS(Max Segment Size) 是TCP数据包每次能够传输的最大数据分段,其中并不包括TCP首部。而且MSS只出现在syn报文段中。一般来说,MSS的值在不分段的情况会越大越好,比如一个外出接口的MSS值是MTU减去IP和TCP首部长度。
  2、窗口大小是个动态的值,因为TCP是用的滑动窗口协议,传输数据的速率都是根据窗口大小来调整的。可以把窗口理解为一个缓存,而且窗口大小跟MSS是没有任何关系的。
  3、窗口是为了控制传输过程中的速度。而MSS只是控制TCP报文段大小。

Source port (16 bits)
Identifies the sending port
Destination port (16 bits)
Identifies the receiving port
Sequence number (32 bits)
Has a dual role:
  • If the SYN flag is set (1), then this is the initial sequence number. The sequence number of the actual first data byte and the acknowledged number in the corresponding ACK are then this sequence number plus 1.
  • If the SYN flag is clear (0), then this is the accumulated sequence number of the first data byte of this segment for the current session.
Acknowledgment number (32 bits)
If the  ACK flag is set then the value of this field is the next sequence number that the sender is expecting. This acknowledges receipt of all prior bytes (if any). The first  ACK sent by each end acknowledges the other end's initial sequence number itself, but no data.
Data offset (4 bits)
Specifies the size of the TCP header in 32-bit  words. The minimum size header is 5 words and the maximum is 15 words thus giving the minimum size of 20 bytes and maximum of 60 bytes, allowing for up to 40 bytes of options in the header. This field gets its name from the fact that it is also the offset from the start of the TCP segment to the actual data.
Reserved (3 bits)
For future use and should be set to zero
Flags (9 bits) (aka Control bits)
Contains 9 1-bit flags
  • NS (1 bit) – ECN-nonce concealment protection .
  • CWR (1 bit) – Congestion Window Reduced (CWR) flag is set by the sending host to indicate that it received a TCP segment with the ECE flag set and had responded in congestion control mechanism .
  • ECE (1 bit) – ECN-Echo has a dual role, depending on the value of the SYN flag. It indicates:
  • If the SYN flag is set (1), that the TCP peer is ECN capable.
  • If the SYN flag is clear (0), that a packet with Congestion Experienced flag set (ECN=11) in IP header received during normal transmission. This serves as an indication of network congestion (or impending congestion) to the TCP sender.
  • URG (1 bit) – indicates that the Urgent pointer field is significant
  • ACK (1 bit) – indicates that the Acknowledgment field is significant. All packets after the initial SYN packet sent by the client should have this flag set.
  • PSH (1 bit) – Push function. Asks to push the buffered data to the receiving application.
  • RST (1 bit) – Reset the connection
  • SYN (1 bit) – Synchronize sequence numbers. Only the first packet sent from each end should have this flag set. Some other flags and fields change meaning based on this flag, and some are only valid for when it is set, and others when it is clear.
  • FIN (1 bit) – Last package from sender.
Window size (16 bits)
The size of the  receive window, which specifies the number of window size units (by default, bytes) (beyond the segment identified by the sequence number in the acknowledgment field) that the sender of this segment is currently willing to receive 
Checksum (16 bits)
The 16-bit  checksum field is used for error-checking of the header and data
Urgent pointer (16 bits)
if the  URG flag is set, then this 16-bit field is an offset from the sequence number indicating the last urgent data byte


有关可选字段:

摘自wiki:

  • 0 (8 bits) – End of options list
  • 1 (8 bits) – No operation (NOP, Padding) This may be used to align option fields on 32-bit boundaries for better performance.
  • 2,4,SS (32 bits) – Maximum segment size (see maximum segment size[SYN]
  • 3,3,S (24 bits) – Window scale (see window scaling for details[SYN][6]
  • 4,2 (16 bits) – Selective Acknowledgement permitted. [SYN] (See selective acknowledgments for details)[7]
  • 5,N,BBBB,EEEE,... (variable bits, N is either 10, 18, 26, or 34)- Selective ACKnowledgement (SACK)[8] These first two bytes are followed by a list of 1–4 blocks being selectively acknowledged, specified as 32-bit begin/end pointers.
  • 8,10,TTTT,EEEE (80 bits)- Timestamp and echo of previous timestamp (see TCP timestamps for details)[9]
(The remaining options are historical, obsolete, experimental, not yet standardized, or unassigned)

对应如下:

1、TCP首部选项字段多达40B,记下一些常用的东西:

2、选项结束字段(EOP,0x00),占1B,一个报文段仅用一次。放在末尾用于填充,用途是说明:首部已经没有更多的消息,应用数据在下一个32位字开始处

3、无操作字段(NOP, 0x01),占1B,也用于填充,放在选项的开头



4、MSS(最大报文段长度),格式如下:种类(1B,值为2),长度(1B,值为4),数值(2B)

用于在连接开始时确定MSS的大小,如果没有确定,就用默认的(一般实现是536B)


5、窗口扩大因子window scale,格式如下:种类(1B,值为3),长度(1B,值为3),数值(1B)

新窗口值 = 首部窗口值 * 2的(扩大因子)次方(即窗口值大小要左移window scale 位)

当通信双方认为首部的窗口值还不够大的时候,在连接开始时用这个来定义更大的窗口。仅在连接开始时有效。一经定义,通信过程中无法更改。

6、允许SACK和SACK选项,略,以后补充

可看这里http://blog.youkuaiyun.com/jxh_123/article/details/27361321

http://blog.chinaunix.net/uid-13746440-id-3282565.html

7、时间截(应用测试RTT和防止序号绕回)

协议规定收端不需要响应每一个收到的数据报文,只需要收到N个报文后,向发端回复一个ack报文即可。
    这样的规定是为了提高通讯的效率,但是也引入了几个问题:
    A. 发端发出报文后,到底多久能够收到ack是不确定的。
    B. 万一ack报文丢失了,判断需要重发的timeout时间也很难确定。


这样时间戳就能计算RTT和防止序号反转

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值