ping 发送 0 字节

ping 发送 0 字节

Linux(CentOS)中的ping命令是否可以发送0字节?在 Windows 中,可以使用尝试过的-l参数
命令来定义

  ping localhost -s 0
    PING localhost (127.0.0.1) 0(28) bytes of data.
    8 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64
    8 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64
    ^C
    --- localhost ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 999ms



man ping

-s packetsize
              Specifies  the  number of data bytes to be sent.  The default is
              56, which translates into 64 ICMP data bytes when combined  with
              the 8 bytes of ICMP header data.

Edit1:添加 ping 的 Windows 输出,以防万一有人需要它

ping 127.0.0.1  -l 0

Pinging 127.0.0.1 with 0 bytes of data:
Reply from 127.0.0.1: bytes=0 time<1ms TTL=128
Reply from 127.0.0.1: bytes=0 time<1ms TTL=128
Reply from 127.0.0.1: bytes=0 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

ping 127.0.0.1

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

答案1

在 Linux、Windows 或任何其他声称能够发送 ping 的平台上,ping 不能为 0 字节。数据包至少必须包含 IP 标头,并且格式正确的 no-trick-playing ping 还将包含 ICMP 标头,长度为 8 个字节。

Windows 可能在输出接收到的字节的方式上有所不同。 Linux 告诉您数据包的 ICMP 部分的大小(ICMP 标头加上任何存在的 ICMP 数据为 8 个字节)。 Windows 可能会打印 ICMP 有效负载数据字节数,这样虽然它告诉您“0”,但那 8 个 ICMP 标头字节仍然存在。真正拥有 0 ICMP 字节意味着您的数据包是原始 IP 标头,不再是 ICMP ping 请求。关键是,即使 Windows 告诉您 ping 数据包的长度为 0 字节,但事实并非如此。

ICMP 回显请求或回显应答数据包的最小大小为 28 字节:

  • 20字节IP头,
  • 4字节ICMP报头,
  • 4字节回显请求/回复头数据,
  • 0 字节 ICMP 有效负载数据。

在 linux 上 ping 时打印:

8 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64

这 8 个字节是 4 字节 ICMP 标头和 4 字节 ICMP 回显应答标头数据,反映 0 字节的 ICMP 有效负载数据大小。

答案2

您的第 2 层传输介质是什么?以太网?无论以太网帧携带什么内容,都不可能短于 64 字节。

因此,即使您可以发送有效负载为 0 字节的 ICMP ping,您的以太网卡也会将帧填充到 64 字节以进行传输。

@Casey 表明理论上的最小值是 28 字节,但以太网将添加 36 字节的填充,使其达到 64 字节总数。少了就是一个 Runt 数据包,而且太短了。

为什么是 64 字节?这是一个全新的问题,与比特的传输时间和冲突检测有关,并且以太网是 CSMA/CD 域而不是 CSMA/CA。

答案3

您必须区分标头大小和有效负载大小(数据)。 ICMP 数据包可以选择保存数据。标头的大小恒定为 64 位,即 8 个字节。有效负载可能会有所不同,甚至可能不存在。这意味着发送 0 字节,并在man-page中进行了准确描述ping

  -s packetsize
          Specifies the number of data bytes to be sent.  The default is 56, which translates into 64 ICMP data bytes when combined with the 8 bytes of ICMP header data.

当您通过将数据包大小设置为 0 来执行时,它会输出以下内容:

ping -s 0 192.168.172.51
PING 192.168.172.51 (192.168.172.51) 0(28) bytes of data.
8 bytes from 192.168.172.51: icmp_seq=1 ttl=63
8 bytes from 192.168.172.51: icmp_seq=2 ttl=63

正如您所看到的,它确实提到了 8 个字节 - 计算标头和有效负载。您可以使用任意数据包大小进行进一步测试,并看到总会多传输 8 个字节,因为这是附加标头的恒定大小。

同时tcpdump证明了这一点:

11:49:30.271160 IP 192.168.172.50 > 192.168.172.51: ICMP echo request, id 5472, seq 30, length 8

标头行中的 28 个额外字节 0(28) 来自 IP+ICMP 标头。

相关内容