Search code examples
cmacospingicmpraw-sockets

Sending ICMP ping


For ICMP ping request (echo request) do I need to get the MAC address of the destination ? I am trying to learn how to implement this in C using raw sockets but can not understand how to get the MAC address of the destination.

Any help is appreciated. Thanks.


Solution

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

    No you shouldn't, in most cases.

    From the man-page of raw(7) (my emphasis):

    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.

    This is only logical, because you might be ping-ing something not on the same subnet, in which case the MAC address would be the MAC address of the router.

    To prove the point, here's some source code for an old BSD version of ping - first one I found on Google. Note that it doesn't attempt to find the destination MAC address.

    Note that some UNIX ping programs do all sorts of fun stuff. Some versions for instance allow you to spoof the outgoing source IP. Some versions include arping functionality. In these cases they will be injecting things at a lower level (at the link level), in which case you will need raw headers.