TCP报文格式
簡介
TCP 是傳輸層協議,為應用程序提供了穩定可靠的數據傳輸。它是一個滑動窗口協議,提供了超時和重傳的處理。
TCP 是在兩個端點之間建立的全雙工虛擬連接。每個端點由 IP 地址和端口號定義。
數據以字節流的形式傳輸,字節流按段傳輸。窗口大小決定了在需要接收方確認之前可以發送的數據字節數。
TCP/IP 協議的模型
首先,我們 TCP/IP 協議棧是一個三層模型的結構,包括傳輸層、網絡層、數據鏈路層。在 TCP/IP 之上的是應用層,比較常用的就是 HTTP 協議。
在圖中,沒有被客戶端和服務端圈中的方格,代表的是數據報。
其中,粉色的部分是當前協議的請求頭,綠色的部分對當前協議而言就是數據部分。
舉例說明:
以 HTTP 協議為例,客戶端發送數據時,先封裝一層 HTTP Header + HTTP Data,然后交給傳輸層。
傳輸層協議把應用層傳遞過來的數據整體當做 Data,再加上了 TCP Header,封裝成數據報,然后交給網絡層。
網絡層再在傳輸層數據的基礎上,加上 IP Header,封裝后交給數據鏈路層。
數據鏈路層,將數據加上 MAC Header 之后,從客戶端傳送給服務端。
服務端接收到數據后,一層層剝離 Header 并且根據 Header 中的協議內容處理后,再將數據傳遞給上層進行操作。
數據傳遞到應用層后,由 HTTP 協議解析完成后,處理 Request 請求,并且生成相應的 Response,然后再反過來傳遞給客戶端。
最終客戶端(瀏覽器)中就會展示相應的網頁。
TCP 報文格式
二進制位報文格式圖:
Source Port. 16 bits. 發送端端口號
Destination Port. 16 bits. 接收端(目的地端)端口號
Sequence Number. 32 bits.
The sequence number of the first data byte in this segment. If the SYN bit is set, the sequence number is the initial sequence number and the first data byte is initial sequence number + 1.
序列號,當前段的第一個字節的序列號。如果設置了 SYN 同步標記(即 SYN=1),序列號的值是初始序列號。帶 SYN 同步標記的段的第一個字節的序列號=初始序列號+1。
初始序列號(ISN)是隨機的,網上很多圖片上的Seq=0,其實指的是相對序列號。
為什么初始序列號是隨機的,而不是固定的? 參考 初始化序列號(ISN)
Acknowledgment Number. 32 bits. 確認編號,回執號
If the ACK bit is set, this field contains the value of the next sequence number the sender of the segment is expecting to receive. Once a connection is established this is always sent.
如果設置了 ACK 確認標記(ACK=1),確認編號的值等于接收端希望收到的下一個字節流段的序列號,也等于接收端收到的由發送端發來的上一個字節流段的序列號 + 1。一旦客戶端和服務端的連接建立起來,就會一直發送確認編號。
Data Offset. 4 bits.
The number of 32-bit words in the TCP header. This indicates where the data begins. The length of the TCP header is always a multiple of 32 bits.
TCP Header 中, 32-bit words 的數量,也就是本節中的 TCP 二進制位報文格式圖的行數。這個字段表明了數據從什么地方開始。TCP 報頭的長度始終是 32 位的倍數。
reserved. 3 bits. 保留字段
Must be cleared to zero.
ECN, Explicit Congestion Notification. 3 bits. 顯式擁塞通知
Added in RFC 3168.
N, NS, Nonce Sum. 1 bit.
Added in RFC 3540. This is an optional field added to ECN intended to protect against accidental or malicious concealment of marked packets from the TCP sender.
這是添加到ECN的可選字段,用于防止來自TCP發送者的標記數據包的意外或惡意隱藏。
C, CWR. 1 bit.
A Congestion Window Reduced (CWR) flag in the TCP header so that the data sender can inform the data receiver that the congestion window has been reduced.
TCP報頭中的減少擁塞窗口(CWR)標志,以便數據發送者可以通知數據接收者擁塞窗口已經減少。
E, ECE, ECN-Echo. 1 bit.
An ECN-Echo (ECE) flag in the TCP header so that the data receiver can inform the data sender when a CE packet has been received.
TCP報頭中的ECN-Echo(ECE)標志,以便數據接收器可以在接收到CE數據包時通知數據發送者。
CE: Routers can instead set the Congestion Experienced (CE) codepoint in the IP header of packets from ECN-capable transports.
路由器可以在支持ECN的傳輸包的IP報頭中設置擁塞經歷(CE)碼位。
Control Bits. 6 bits. 控制位
U, URG. 1 bit.
Urgent pointer valid flag.
A, ACK. 1 bit.
Acknowledgment number valid flag.
P, PSH. 1 bit.
Push flag.
R, RST. 1 bit.
Reset connection flag.
重置連接標識
S, SYN. 1 bit.
Synchronize sequence numbers flag.
同步序列號標識
F, FIN. 1 bit.
End of data flag.
結束數據傳輸標識
Window. 16 bits, unsigned.
The number of data bytes beginning with the one indicated in the acknowledgment field which the sender of this segment is willing to accept.
此段的發送方愿意接受的以確認字段中指示的字節開始的數據字節數。
Checksum. 16 bits.
This is computed as the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header, the TCP header, and the data, padded as needed with zero bytes at the end to make a multiple of two bytes. The pseudo header contains the following fields:
這是作為IP頭,TCP頭和數據中的偽信息頭的補碼之和的16位補碼計算得出的,根據需要用最后的零字節填充以形成兩個字節的倍數 。 偽頭包含以下字段:
Urgent Pointer. 16 bits, unsigned.
If the URG bit is set, this field points to the sequence number of the last byte in a sequence of urgent data.
如果URG位置1,則此字段指向緊急數據序列中最后一個字節的序列號。
Options. 0 to 40 bytes.
Options occupy space at the end of the TCP header. All options are included in the checksum. An option may begin on any byte boundary. The TCP header must be padded with zeros to make the header length a multiple of 32 bits.
選項占據TCP報頭末尾的空間。 所有選項都包含在校驗和(checksum)中。選項可以在任何字節邊界上開始。TCP報頭必須用零填充,以使報頭長度為32位的倍數。
Data. Variable length. 數據部分
十六進制圖
每 1 個十六進制數位可以表示 4 位 bits 位。轉換結果如下
TCP 狀態集
參考文檔
www.networksorcery.com/enp/protocol/tcp.htm
擴展閱讀
最詳細的http協議、tcp/ip協議
TCP報文解析
總結
- 上一篇: CDS view里case - when
- 下一篇: 如何把SAP CRM产品主数据隐藏的ba