2

I am looking for an easy way to convert a MAC address to the corresponding IP address in a local network. In my case, there are only two devices: a very normal PC (192.168.0.1) and a scientific instrument which has an arbitrary IP address (192.168.0.xxx) hard coded in its ROM. The PC and the instrument are directly connected over a UDP socket with a CAT5 cable.

I know the MAC address of the instrument, but please assume that its IP address is unknown. I would like to write a C/C++ application which talks with the instrument using a socket connection. But I need to know the IP address before opening a socket (WinSock on Windows, sys/socket on OS X and Linux).

Currently I use a very dirty way as shown below.

  1. Execute ping command ping 192.168.0.2 (NOTE: the instrument does not respond to ping)
  2. Repeat this from 192.168.0.2 to 192.168.0.255
  3. Execute arp -a to print a list of IP and MAC addresses
  4. Find the known MAC address and the corresponding IP address from the list

I would like to know how to retrieve the IP address in a more sophisticated way. It will be very nice if I can use the same method on Mac, Linux and Windows machines.

As far as I know, I have to broadcast a ARP packet to the network in order to retrieve a MAC address from a known IP address. But I could not find a way to get an IP address from a MAC address.

Akira Okumura
  • 1,816
  • 2
  • 20
  • 41
  • 2
    That's called RARP - reverse ARP. https://en.wikipedia.org/wiki/Reverse_Address_Resolution_Protocol – Violet Giraffe Feb 13 '14 at 21:45
  • 2
    @VioletGiraffe That's just an obsolete variant of providing dhcp functionallity - it's not going to help the op. One basically have to do what the OP already does, ping the entire range and search the ARP cache - unless the instrument provides a specific way to learn its IP address. – nos Feb 13 '14 at 22:05
  • @nos: what does DHCP have to do with it? RARP is exactly what the OP asked for. – Violet Giraffe Feb 13 '14 at 22:18
  • 2
    I think, RARP is used to get an IP address from a RARP server if a network device which has a MAC address does not have an IP address. I suppose this is why @nos wrote "dhcp functionality." But in my case, the network instrument has an IP address which is hard coded on its ROM. In addition, there is not a RARP server in my network. – Akira Okumura Feb 13 '14 at 22:28
  • @violet giraffe please read your own link before dumping it, it speaks about rarp being obsoleted by BOOTP/DHCP in the introduction already – KillianDS Feb 13 '14 at 22:52

4 Answers4

3

There's no good, generic solution for this as it is the reverse of intended behavior. Lower level protocols are not supposed to need to be aware of higher layer ones, so operations at the MAC layer don't have any good way of finding out about IP addresses. And then you get into the situation you're in now. So you can either employ a hack like the code you already have, or you can tackle this from a different direction. Is there any non-code way to determine the IP address of the device before hand? Such as setting it explicitly or putting it in a configuration file for your app. Alternatively, can you have the device send out spurious ARP requests? The PC should update its ARP cache based off of incoming requests as well as responses to requests it made.

Chris Ryding
  • 1,508
  • 11
  • 16
  • Thank you Chris. Your answer is what I expected unfortunately :-) The instrument I mentioned above is a custom made circuit which has an FPGA on it. As the FPGA generates an IP address at random for some reasons, what I know is only the MAC address. In reality, I (will) have hundreds of this device. So I wanted to avoid repeating my dirty hack hundreds of times. I will ask the developer of the FPGA code about the possibility of sending ARP requests. – Akira Okumura Feb 13 '14 at 23:09
  • 1
    I worked on a similar sort of problem a number of years ago. We had devices with user assigned addresses that would come and go on the network. What we did was to have each device listen on a specific UDP port for a particular character string. If it received that string, it responded with an identifier. That way we could blast out UDP packets to each address and then just passively collect responses to see what devices were live. That might be an option if you have a bunch of these things. – Chris Ryding Feb 13 '14 at 23:18
1

We had to do this a while back, but I don't think we got it working properly.

I don't have the API calls off-hand, but they're easy to find in the Windows API. That's what we used, so our solution wouldn't be portable to non-Windows systems.

In our case, we ran into the same hurdle--no easy translation. What we ended up having to do was get a list of all the NICs available, and then loop through each one trying to match our given MAC address against the MAC address obtained from the NIC structure.

Once we found a match, we looked up the IP address given to the NIC structure.

We kept on going to see if we found any other matches in order to log an error. It's a good thing we did, because I believe we did find it multiple times, and it wasn't due to a MAC address being cloned.

That's when we learned that this would be an even harder problem, and we decided to abandon the whole thing and stick to just IP addresses.

jia103
  • 1,116
  • 2
  • 13
  • 20
0

How about try the system command arp within c++

system("arp");

This gives you a IP-MAC translation table.

xb.
  • 1,617
  • 11
  • 16
  • Yes, but he has to populate that table first, which is what his listed code is trying to do. – Chris Ryding Feb 13 '14 at 22:47
  • @xb. In other words, I would like to know how to populate ARP cache without repeating ping or a similar command. – Akira Okumura Feb 13 '14 at 22:58
  • @AkiraOkumura `arp` gives me an IP-MAC table of currently connected machines without executing any `ping` command. I am running a Linux. – xb. Feb 13 '14 at 23:03
  • @xb. Thank you for your comments. The device I use is not ordinary. It does not send any packets until I send it a packet from the PC (192.168.0.1). That's why ARP cache is not populated before I send ping. I should have clarified on this. – Akira Okumura Feb 13 '14 at 23:31
  • @AkiraOkumura Good to beware of such type of system :) – xb. Feb 13 '14 at 23:47
  • @xb. no, there is no such thing as a connection on ip/Ether level. It just gives the arp entries it learned and cached for the past X minutes. For it to contain a specific device there either had to be some communication first (e.g. a ping), or the device would have to send a gratuitious ARP or an RA. – KillianDS Feb 14 '14 at 07:54
-1

few days ago I was also facing this issue but after many struggle I got its solution below How MAC to IP address converter tool works? This MAC address converter can convert MAC address to IPv4 IP Address and convert MAC address to IPv6 IP Address, these internet protocol Addresses are very common to use. It takes MAC Address as input string and generates a query against given MAC to IP address and MAC conversion option like to MAC to IPV6 or MAC to IPV4 or both for MAC address conversion together. After this MAC conversion you can also revert MAC to IP conversion changes by using IPv6 to IPv4. Query generates an output response according to selected options.If you insert any invalid input produces an invalid input message response