Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP/IP 详解卷一: 协议 #82

Open
magicdawn opened this issue Jun 3, 2016 · 9 comments
Open

TCP/IP 详解卷一: 协议 #82

magicdawn opened this issue Jun 3, 2016 · 9 comments
Labels

Comments

@magicdawn
Copy link
Owner

卷一: 协议

@magicdawn magicdawn added the Book label Jun 3, 2016
@magicdawn
Copy link
Owner Author

magicdawn commented Jun 3, 2016

ch17 TCP 协议

datagram

Bytes 用途
20 IP header
20 TCP header
... real data

TCP header

Bytes 用途
1-4 src-port(16 bit) + dest-port(16 bit)
5-8 sequence number(32 bit)
9-12 Acknowledgment sequence number(32 bit)
13-16 header-length(4 bit) + reserved(6 bit) + URG-ACK-PSH-RST-SYNC-FIN(6 bit) + window-size(16 bit)
17-20 checksum(16 bit) + urgent-pointer(16 bit)
  • src/dest 端口
  • sequence number 传输序号, 如果将流过的字节流每一个字节都给一个标识, 那么此处的传输标识是此 segment 的数据的 第一个字节代表的序号. 32bit
  • Acknowledgment sequence number 确认序号.
  • header-length: header长度, 在 data 之前还有一个 Option 区, 可选的. 因此 length 不一定为 20. TCP 限制 60- header
  • FLAGS:
    • URG: 标识 the urgent pointer is valid
    • ACK: 标识 the Acknowledgment number is valid
    • PSH: reciver 应该尽快将此数据传递给 application. see ch20.5
    • RST: seset the connection. see ch18.7
    • SYN: initiate the connection
    • FIN: the sender is finished sending data
  • window-size: 窗口大小 limit to 65535 bytes
  • checksum: 校验和 cover the TCP header & the TCP data
  • urgent-pointer: 紧急指针, 在 URG 标志位设置之后, 这个 pointer 表示必须加到上次收到的 URGENT data 的末尾, 以产生正确的 sequence number

TCP data

TCP header 中并没有包含 data length. payload length 由IP header 给出
https://ask.wireshark.org/questions/4178/how-do-i-determine-a-tcp-segments-length

@magicdawn
Copy link
Owner Author

magicdawn commented Jun 3, 2016

ch18 TCP 连接 & 终止

连接

package_type(seq num, ack seq num)
A为client / B为Server

  1. -> syn(seqA0, 0)
  2. <- ack-sync(seqB0, seqA0 + 1)
  3. -> ack(seqA0+1, seqB0 +1)

至此连接建立完成

下次发送的 seq num 还是 seqA0 + 1, 因为第 3 步的 ack 包是没有 payload

终止

image

TIME_WAIT

2MSL

image

  • MSL = Maximum Segment Lifetime = 任何报文段被丢弃前在网络内的最长时间.
  • RFC 793 指出, MSL = 2min
  • 常用实现值 30s / 1min / 2min

RST

无论何时, 一个报文段发往基准的连接出现错误, TCP都会发出一个复位报文. 基准连接指 src-ip-port 与 dest-ip-port 之间的连接.

@magicdawn
Copy link
Owner Author

magicdawn commented Jun 3, 2016

ch19 TCP 交互数据流

Nagle

在局域网上一些小分组(tinygram)通常不会引起麻烦, 因为局域网一般不会出现拥塞.
但在广域网上, 这些小分组会增加拥塞出现的可能.

该算法要求

  • 一个TCP连接上最多只有一个未被确认的未完成的小分组, 在该分组的确认到达前不能发送其他的分组.
  • 该算法收集小分组, 并在确认到来时以一个分组发出去
  • 该算法是自适应的: 确认到达的越快, 数据也就发送的越快.

禁用

  • 使用 TCP_NODELAY 来禁用 Nagle 算法.
  • 使用 socket.setNoDelay(true) in Node.js 禁用

@magicdawn
Copy link
Owner Author

ch20 TCP 成块数据流

@magicdawn
Copy link
Owner Author

ch21 TCP 超时 & 重传

@magicdawn
Copy link
Owner Author

ch22 TCP 坚持定时器

@magicdawn
Copy link
Owner Author

ch23 TCP 保活定时器

@magicdawn
Copy link
Owner Author

ch24 TCP 未来 & 性能

@magicdawn
Copy link
Owner Author

magicdawn commented Jun 3, 2016

tcpdump

http://man.linuxde.net/tcpdump

example

tcpdump host <target_host>

flags

flag 含义
-x 以 hex 显示包内容
-A 以 ASCII 显示包内容

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant