Solved

Retriving Ethernet Adapter's Address through programming in C

Posted on 2000-03-16
4
202 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

7 Experts available now in Live!

Get 1:1 Help Now