環境: Apple MacBook Pro (以下會根據你的平台數據會有所不同)
$ ping 8.8.8.8 -s 8185 -c 1
PING 8.8.8.8 (8.8.8.8): 8185 data bytes
ping: sendto: Message too long
[-s packetsize]
[-c count]
$ ping 8.8.8.8 -s 8184 -c 1
PING 8.8.8.8 (8.8.8.8): 8184 data bytes
8192 bytes from 8.8.8.8: icmp_seq=0 ttl=121 time=57.464 ms
--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 57.464/57.464/57.464/0.000 ms
為什麼用ping來打size只能限制到8184?
1. 查看系統核心參數,因爲icmp是raw socket
$ sysctl -a | grep net.inet.raw
net.inet.raw.maxdgram: 8192
net.inet.raw.recvspace: 8192
8192代表Transport Layer socket的大小(限制)
接下來看如果傳輸層要傳送8192 bytes一次需要被切割幾個封包?
2. Ethernet的MTU為1500,即IP header裡面的Total length最大為1500
(IP header 20 + Data 1480)
所以8192 = 1480 * 5 + 792
==> 所以要六個封包才能送完 (No 27, 28, 29, 30, 31, 32)
3. 再由觀察wireshark抓封包得知
封包總Length (Ethernet header + IP header + Transport header + Message)
1514 * 5 + 826 = 8396 bytes
此案例為icmp所以封包格式為
Ethernet header + IP header + ICMP header + ICMP payload
8396 - (IP header 20 *6) - (Ethernet header 14 * 6) = 8192 bytes (比對吻合)