Link to home
Start Free TrialLog in
Avatar of abhishekpatel
abhishekpatel

asked on

Retriving Ethernet Adapter's Address through programming in C

how to retrive the Ethernet Adapter's Address(Not IP addr) of a particular
machine through programming in C under Linux.
   Basically i have made a client/server model in which i want the Ehernet
Adapter's addrs of the client which is connecting to my server rather then
IP addr.
Avatar of edskee
edskee

You want a 'C' command to get the MAC address of your adapter? I don't know of any. This is kind of 'hacky' but it will work. Dunno how often it needs to run, etc, but it's a quick and dirty way to do it:

Have your program run a shell command: ifconfig, and redirect the output to a file. (system("ifconfig > testfile"))

Open/read in that file, parse it for the hardware address...
sscanf(filepointer, "HWaddr %s", charstring)

Then unlink testfile. Yes, yes, I know, it's hacky as hell, and a nasty way to do it, but it's the method I use when I need to get something I know a shell command can do, but dont know the C command for it. :)

Only left this as a comment, didnt wanna go so far as to actually answer it, because I know this is a huge hack, and generally bad programming, but I figured I'd offer it as a last ditch suggestion. :) If you use it, accept it as an answer, I could use the points. Need that 10k point tshirt! :)
Just an initial idea until I check a reference at home.  All references are based on RedHat Linux.

You should be able to 'see' the MAC address for the network card in the client by reading /proc/net/arp.  It lists the IP address as well as the hardware address.  Your client should be able to access this as a regular file (just DON'T write to it!!). Then you'd just have to parse it.

I did notice that with DHCP there is a slight delay after booting before /proc/net/arp returned information.

I can't remember if the MAC address is available through calls like gethostbyname(), but I'll check tonight.

Good luck.
ASKER CERTIFIED SOLUTION
Avatar of complaw
complaw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just an initial idea until I check a reference at home.  All references are based on RedHat Linux.

You should be able to 'see' the MAC address for the network card in the client by reading /proc/net/arp.  It lists the IP address as well as the hardware address.  Your client should be able to access this as a regular file (just DON'T write to it!!). Then you'd just have to parse it.

I did notice that with DHCP there is a slight delay after booting before /proc/net/arp returned information.

I can't remember if the MAC address is available through calls like gethostbyname(), but I'll check tonight.

Good luck.