Solved

Retriving Ethernet Adapter's Address through programming in C

Posted on 2000-03-16
4
205 Views
Last Modified: 2010-03-18
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.
0
Comment
Question by:abhishekpatel
  • 3
4 Comments
 
LVL 2

Expert Comment

by:edskee
ID: 2624408
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! :)
0
 

Expert Comment

by:complaw
ID: 2630089
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.
0
 

Accepted Solution

by:
complaw earned 30 total points
ID: 2631536
Checked my references last night.  gethostbyname() and kin don't help with the hardware address of the cards.

Ioctl can get the information for you, but it's driver dependent so you might have to write separate code for every different ethernet card you have.

Looks the the most portable answer is to read /proc/net/arp and parse it for the hardware address.

Of course, this will only work for Linux/Unix clients.  Windows is a different ball of wax, but let me know if you need hardware addresses in Windows as well.

0
 

Expert Comment

by:complaw
ID: 2637205
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.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
*STABLE* and free Linux Firewall distribution 6 81
AIX    Volume group Auto ON/OFF question 2 90
Guacamole and browser performance 1 64
How to Edit Files in Linux 6 99
I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now