C++Builder Developer's Journal

Is there a way to get MAC addresses using INDY

Is there a way to get MAC addresses using INDY

Posted: Fri Mar 23, 2018 11:24 pm

by quintin

I need to get the MAC addresses for nodes on a LAN so that I can set up devices remotely. I'm using c++ builder and have found a way on MSDN, but I'm sure someone will want to run it on their macbook! Is there an easy way to do this using Indy10? I have run several searches through the 4100+ pages of the Indy manual but nothing has jumped out at me. Same with google.

Thanks in advanceQuintin

Re: Is there a way to get MAC addresses using INDY

Posted: Mon Mar 26, 2018 11:47 am

by rlebeau

quintin wrote:I need to get the MAC addresses for nodes on a LAN so that I can set up devices remotely.

What way is that exactly? SendARP() (which resolves the MAC address of a given IP address)?

quintin wrote:Is there an easy way to do this using Indy10?

Not natively, no. There is nothing in socket APIs that deal with MAC addresses.

For IPv4, you would have to drop all the way down to the ethernet layer and manually send ARP requests and receive their responses. You can't do that with Indy. However, on Windows at least, there are GetIpNetTable() and GetIpNetTable2() functions in the Win32 API to retrieve Windows' ARP table containing known IPs and MACs.

For IPv6, you might be able to use Indy's TIdIcmpClient component to implement NDP (Neighbor Discovery Protocol) via ICMPv6.

Re: Is there a way to get MAC addresses using INDY

The project is for a holiday display that will be set-up and taken down annually. The cards that I’m testing have a default IP address and must be set manually. So the only way to tell them apart is from the MAC address, and I was asked to make it as “non techie” friendly as possible.

I found GetIPNetTable() on MSDN and have a function that steps through the table that gives the current IP and corresponding MAC address:

My plan is to write a setup routine that will query the table and attempt to configure the cards as they are plugged into the network. I just wanted to make sure I hadn’t missed anything that would be more portable using INDY.

Finally I’d like to say I owe all of my networking ignorance to those of you who work on the INDY project! Although I haven’t worked on that many projects that needed INDY, I’ve been able to do it without knowing the difference between a Winsock and an argyle! So Thanks!

Re: Is there a way to get MAC addresses using INDY

Posted: Wed Mar 28, 2018 11:54 am

by rlebeau

quintin wrote:The project is for a holiday display that will be set-up and taken down annually. The cards that I’m testing have a default IP address and must be set manually. So the only way to tell them apart is from the MAC address, and I was asked to make it as “non techie” friendly as possible.

That doesn't make much sense. If you have multiple cards on the same network, they need unique IP addresses. That is enough to differentiate them. I would be surprised if they didn't support obtaining an IP address automatically from a DNS server on the local network. That would make them more plug-n-play.

Are you wanting to get the MAC addresses so that you can then configure the IPs from within your software?

quintin wrote:I found GetIPNetTable() on MSDN and have a function that steps through the table that gives the current IP and corresponding MAC address

You are not not doing any error handling when calling GetIpNetTable(). And you are not allocating the IP array correctly, either. GetIpNetTable() returns the number of *bytes* needed for the array, not the number of *elements*, like you are currently treating it.

quintin wrote:My plan is to write a setup routine that will query the table and attempt to configure the cards as they are plugged into the network. I just wanted to make sure I hadn’t missed anything that would be more portable using INDY.

Nope, you didn't.

Re: Is there a way to get MAC addresses using INDY

Posted: Thu Mar 29, 2018 2:19 am

by quintin

rlebeau wrote:That doesn't make much sense. If you have multiple cards on the same network, they need unique IP addresses. That is enough to differentiate them. I would be surprised if they didn't support obtaining an IP address automatically from a DNS server on the local network. That would make them more plug-n-play.

Sorry, I should have explained this better. The cards are fairly basic (inexpensive) and have a default static IP address, and there is nothing in the documentation about obtaining an IP automatically. I have only tried it on 2 networks but it has shown up at its default IP in both instances. But, even if it could get an IP from the DNS server I still need the MAC address to identify the card and its location in the display.

rlebeau wrote:Are you wanting to get the MAC addresses so that you can then configure the IPs from within your software?

Yes, the software will assign an IP and set a couple of other parameters on the card based on the MAC address.

rlebeau wrote:You are not not doing any error handling when calling GetIpNetTable(). And you are not allocating the IP array correctly, either. GetIpNetTable() returns the number of *bytes* needed for the array, not the number of *elements*, like you are currently treating it.

This is my initial test code so I didn’t worry about error handling, I included it here because although it seemed to work I wasn’t confident that I was doing it right, which I was not. So thank you for correcting the code it gives me a good place to start!