Solved

Retriving Ethernet Adapter's Address through programming in C

Posted on 2000-03-16
4
212 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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