TCP首部字段有必选字段20字节,另外有40字节的可选字段
必选字段如下:
- 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时,表明此报文段的发送端的数据已发送完毕,并要求释放运输连接。
备注:
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]
对应如下:
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和防止序号反转