3

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.

Novak007
  • 426
  • 1
  • 9
  • 23
  • 1
    recommandation: get the source code of `coreutils` and have a look at `ping`. https://www.gnu.org/software/coreutils/ – Jason Hu Feb 10 '15 at 18:26
  • 1
    @HuStmpHrrr: `ping` is not in [coreutils](http://en.wikipedia.org/wiki/GNU_Core_Utilities). – indiv Feb 10 '15 at 19:09

1 Answers1

3

[...] 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.

alk
  • 69,737
  • 10
  • 105
  • 255
abligh
  • 24,573
  • 4
  • 47
  • 84
  • I need to get the Mac address of the router which is the destination in this case (the next hop). – Novak007 Feb 10 '15 at 18:44
  • @Novak007, you shouldn't need to do that unless you are *not* using raw sockets, but using raw ethernet packets. If you are using raw sockets then the OS will work this out for you. – abligh Feb 10 '15 at 18:45
  • As the answer suggests the ARP would take care of the destination MAC address. If you are using raw sockets and the OS will use the resolved address destination MAC for you ethernet header – cmidi Feb 10 '15 at 19:01
  • It is different though if you want to create arp replies and responses in which case you would need to create an arp header. – cmidi Feb 10 '15 at 19:03
  • @cmidi indeed. Normal `ping` does not do that, but `arping` (or a ping incorporating `arping`) will. – abligh Feb 10 '15 at 20:22