BlakeFoster / Arduino-Ping

ICMP ping library for the Arduino
58 stars 44 forks source link

Is there any way to receive a MAC address? #2

Closed powtac closed 10 years ago

powtac commented 10 years ago

... with the W5100 Ethernet Shield?

BlakeFoster commented 10 years ago

The answer, if there is one, would be in the W5100 datasheet. The W5100 Arduino library is largely undocumented, and it's that datasheet that told me what I needed to know to write this library.

Based on a quick skim of the information there, it looks like the DHAR register in the W5100 may tell you the MAC address of incoming packets. I haven't tried it though. Also, this will only work for a device either directly connected to the Arduino, or connected via an Ethernet hub. If you go through a router, this won't work, because you'll get the router's MAC address instead of the originating device's MAC address.

If you need to know the MAC address of a device that's not on the same link, you would need some sort of software running on the device that would report the MAC address to you. But knowing the MAC address of a device that's not on the same link isn't all that useful.

May I ask why you need to know the MAC address of another device, as opposed to the IP address? There may be a better way to do whatever it is that you're trying to accomplish.

powtac commented 10 years ago

Thanks a lot for your answer! The goal is to identify devices over time to make some kind of statistics in a network. Like what iPhones connected to the LAN (Wifi on the same network). ... I'm afraid that when the IP changes the stats will be broken. The goal is identify the devices...

BlakeFoster commented 10 years ago

If we're just talking about devices on a single home network, you should be able to get their MAC addresses, because a home "router" is really just a glorified switch. Thus all computers connected to it are peers, and they will use link-layer protocols for communicating with each other.

The moment you go through a gateway (i.e. an IP router), though, you're pretty much out of luck without the support of software running on the device, because packets destined for the outside world will have the router's MAC address rather than the MAC address of the destination network interface. But again, this should not be a problem for a single network.

If you have multiple networks connected by a router, you would probably need a device monitoring MAC addresses on each one, possibly forwarding the information to one "master" device.

It's easy enough to find out if an IP address is on the same network. The arp command in Linux will look up an IP address in the system's ARP cache to determine the MAC address. For example, if I look up another computer on my network:

bfdesktop@ubuntu:~$ arp 192.168.10.108
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.10.108           ether   18:3d:a2:7c:f8:38   C                     eth0

If I try one of Google's servers:

bfdesktop@ubuntu:~$ arp 74.125.228.81
74.125.228.81 (74.125.228.81) -- no entry

As you can see, I got back a MAC address for the system on my network, but nothing for Google. That's because Google isn't on my network, so we can't determine its MAC address.

powtac commented 10 years ago

@PcTim found a way to receive the MAC address within the ICMPPing.cpp! See https://github.com/powtac/Autarc_LAN_User_Stats/commit/2fc37726e7a4d97f396e1d8051d228ce69848b60#diff-9e75f20f0e356d755d4e5c31b97006c6R122 And a change in ICMPPing.h https://github.com/powtac/Autarc_LAN_User_Stats/commit/2fc37726e7a4d97f396e1d8051d228ce69848b60#diff-377ba02311ce4338f1cece6bd85fdd28R110. Then the MAC address is available via

for(int part = 0; part < 6; part++) {
       mac[part] = echoReply.MACAddressSocket[part];
}