Search code examples
linuxsocketsicmp

ICMP sockets (linux)


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?


Solution

  • 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...