发送 ICMP ping

Sending ICMP ping

对于 ICMP ping 请求(回显请求),我需要获取目标的 MAC 地址吗?我正在尝试学习如何使用原始套接字在 C 中实现这一点,但无法理解如何获取目标的 MAC 地址。

感谢任何帮助。 谢谢。

[...] do I need to get the MAC address of the destination ?

不,在大多数情况下你不应该。

来自raw(7)的man-page(我的重点):

Raw sockets allow new IPv4 protocols to be implemented in user space. A raw socket receives or sends the raw datagram not including link level headers.

这只是合乎逻辑的,因为您可能 ping-ing 不在同一子网上的东西,在这种情况下 MAC 地址将是路由器的 MAC 地址.

为了证明这一点,here 的一些旧 BSD 版本的 ping 的源代码——我在 Google 上找到的第一个。请注意,它不会尝试查找目标 MAC 地址。

请注意,某些 UNIX ping 程序会执行各种有趣的操作。例如,某些版本允许您欺骗传出源 IP。一些版本包括 arping 功能。在这些情况下,他们将在较低级别(link 级别)注入东西,在这种情况下,您将需要原始 headers.