ICMP 套接字(Linux)
IP协议下可以使用ICMP套接字吗?也许类似于:
socket(PF_INET,
我应该在
字段中放入什么?我看到了一些使用 SOCK_RAW
的示例,但这不会阻止操作系统处理 IP 协议吗?
还有一件事。由于协议不涉及端口,操作系统如何知道应该将 ICMP 数据报发送到哪个进程?
Is it possible to use ICMP sockets under the IP protocol? Maybe something like:
socket(PF_INET, <type>, IPPROTO_ICMP)?
What should I put in the <type>
field? I saw some examples using SOCK_RAW
, but won't that prevent the OS from doing his job handling the IP protocol?
And another thing. How can the OS know to which process he should send the ICMP datagrams, since there are no ports involved with the protocol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Linux 有一个特殊的 ICMP 套接字类型,您可以使用它:
这允许您仅发送 ICMP 回显请求 内核将对其进行特殊处理(匹配请求/响应,填写校验和)。
仅当设置了特殊 sysctl 时,此功能才有效。默认情况下,即使 root 也不能使用这种套接字。您指定可以访问它的用户组。要允许 root(组 0)使用 ICMP 套接字,请执行以下操作:
以下是一个示例程序,演示发送 ICMP 回显请求的基本用法:
请注意,如果发送的数据不符合要求,则内核将拒绝 sendto() 调用并使其失败没有足够的空间容纳正确的 ICMP 标头,并且 ICMP
type
必须为 8 (ICMP_ECHO),并且 ICMP 代码必须为 0。Linux have a special ICMP socket type you can use with:
This allows you to only send ICMP echo requests The kernel will handle it specially (match request/responses, fill in the checksum).
This only works if a special sysctl is set. By default not even root can use this kind of socket. You specify the user groups that can access it. To allow root (group 0) to use ICMP sockets, do:
Here is an example program to demonstrate the very basic usage of sending an ICMP echo request:
Note that the kernel will reject and fail the sendto() call if the data sent does not have room for a proper ICMP header, and the ICMP
type
must be 8 (ICMP_ECHO) and the ICMP code must be 0.是的,这是可能的,因为
ping
命令执行 ICMP。要找出所涉及的系统调用,您可以
strace
该命令(在 root 下)。您还可以浏览该命令的源代码,例如 Debian 的 ping
并且有 liboping 库可以帮助您...
Yes it is possible, since the
ping
command does ICMP.To find out the syscalls involved, you can
strace
that command (under root).You could also glance into that command's source code, e.g. Debian's ping
And there is the liboping library to help you...