为什么在ping程序中内核没有去掉IP层
Why is the IP layer not removed by the kernel in the ping program
我正在研究标准的 ping 实现。这里创建了icmp结构,并填充了数据。IP层是内核添加的。然而,当我们使用函数 http://linux.die.net/man/2/recvfrom 收到一条消息时,我观察到它们首先解析 IP 数据包,然后解析 ICMp 数据包。为什么会这样。我引用的代码是在线提供的标准 ping 实现。
这是因为在原始套接字上接收 IPv4 数据包时总是包含 header。请注意 raw(7)
中的以下内容(强调我的):
The IPv4 layer generates an IP header when sending a packet unless the IP_HDRINCL
socket option is enabled on the socket. When it is enabled, the packet must contain an IP header. For receiving the IP header is always included in the packet.
由于始终包含 header 和 has variable length(对于 IPv4),因此必须对其进行解析以找出 ICMP 数据的起始位置。
至于 为什么 header 没有被删除(很抱歉,如果这是您唯一想知道的事情),我不知道。我的疯狂猜测是,有足够多的处理原始 IPv4 的程序想要查看 header 似乎不值得将剥离它作为一个选项。快速浏览一下,似乎 header 是 为 IPv6 剥离的。
标准ping
和ping6
顺便说一下来自iputils,其中ping_common.c、ping.c和ping6.c是最相关的源文件。
我正在研究标准的 ping 实现。这里创建了icmp结构,并填充了数据。IP层是内核添加的。然而,当我们使用函数 http://linux.die.net/man/2/recvfrom 收到一条消息时,我观察到它们首先解析 IP 数据包,然后解析 ICMp 数据包。为什么会这样。我引用的代码是在线提供的标准 ping 实现。
这是因为在原始套接字上接收 IPv4 数据包时总是包含 header。请注意 raw(7)
中的以下内容(强调我的):
The IPv4 layer generates an IP header when sending a packet unless the
IP_HDRINCL
socket option is enabled on the socket. When it is enabled, the packet must contain an IP header. For receiving the IP header is always included in the packet.
由于始终包含 header 和 has variable length(对于 IPv4),因此必须对其进行解析以找出 ICMP 数据的起始位置。
至于 为什么 header 没有被删除(很抱歉,如果这是您唯一想知道的事情),我不知道。我的疯狂猜测是,有足够多的处理原始 IPv4 的程序想要查看 header 似乎不值得将剥离它作为一个选项。快速浏览一下,似乎 header 是 为 IPv6 剥离的。
标准ping
和ping6
顺便说一下来自iputils,其中ping_common.c、ping.c和ping6.c是最相关的源文件。