?
Solved

Retriving Ethernet Adapter's Address through programming in C

Posted on 2000-03-16
4
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 120 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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