LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-13-2002, 06:55 AM   #1
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Rep: Reputation: 15
Question Linux: Determining MAC address from C


What calls do I use to get the MAC address on a Linux box in a C command line program? (Don't want to parse ifconfig's output)
Is there an API call?
 
Old 12-17-2002, 10:48 PM   #2
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
Here ya go, enjoy!

Code:
/*
 * gethwaddr.c
 *
 * Demonstrates retrieving hardware address of adapter using ioctl()
 *
 * Author: Ben Menking <bmenking@highstream.net>
 *
 */
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>

int main( int argc, char *argv[] )
{
	int s;
	struct ifreq buffer;

	s = socket(PF_INET, SOCK_DGRAM, 0);

	memset(&buffer, 0x00, sizeof(buffer));
	
	strcpy(buffer.ifr_name, "eth0");

	ioctl(s, SIOCGIFHWADDR, &buffer);

	close(s);
	
	for( s = 0; s < 6; s++ )
	{
		printf("%.2X ", (unsigned char)buffer.ifr_hwaddr.sa_data[s]);
	}

	printf("\n");

	return 0;
}
 
1 members found this post helpful.
Old 03-13-2003, 08:20 AM   #3
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Original Poster
Rep: Reputation: 15
Smile

Never occurred to me to sy thank you...
Thanks, Ben, works like a charm!

Now I have a new question: can I use a similar technique to extract the IP address, or is it even more simple?

Not that I am lazy, but I still havent got the C and C++ library in KDevelop 2.1.3 working and cannot look up any api functions...
 
Old 03-13-2003, 10:41 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
...there's always man, info, apropos
 
Old 03-13-2003, 10:59 AM   #5
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
Your welcome!

Look at SIOCGIFADDR for getting ip address. You can also look at /usr/include/bits/ioctls.h to see more options you can get and set. Also look in /usr/include/linux/if.h for the other structs you can use to get more information (like struct ifreq).

ioctl() is confusing but there's alot of really cool stuff you can do.

Enjoy!
 
Old 03-14-2003, 02:12 AM   #6
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Original Poster
Rep: Reputation: 15
Ben,

Hmm. I did try ioctl(s, SIOCGIFADDR, &buffer) after the one to get the MAC address (after saving ifr_hwaddr.sa_data, of course), but ifr_addr.sa_data contains the MAC address, as well, and not the ip. Do I have to zero the contents of buffer before every ioctl call?

Mad_C
 
Old 03-14-2003, 04:08 PM   #7
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
Oh boy. It's no wonder you can't get the ip address. I figured this out running strace on the binary. The second call to ioctl() fails with a E_BAD_DESCRIPTOR, ie the file descriptor <s> is bad.

The problem occurs when I use <s> in the for loop. It overwrites the socket descriptor. The fix? Add another int variable and use that in the for loop. Your code should work properly.
 
Old 03-17-2003, 01:39 AM   #8
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Original Poster
Rep: Reputation: 15
<Snicker> I shoulda have seen that myself, really, but apprently I am so insecure about moving in the new (for me, that is) Linux world that I can't see the obvious.. My biggest problem is definitely having no concise reference of the tools available with Linux (strace?).
Just as people say: "Linux is only free if your time has no value"

Thanx again,

Mad_C
 
Old 03-17-2003, 07:50 AM   #9
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
Yeah, I know what you mean. Most of the things I know on Linux I've found by accident or brute force :-)

strace is a wonderful debugging utility because it will show you the system calls and signals of a running program. That's how I originally found out about ioctl() and all it's data.

# strace /usr/sbin/ifconfig eth0

Shows you all the system calls like ioctl() ifconfig makes. Try it on other binaries.. probably one of the best ways to learn system calls on linux.
 
Old 02-11-2005, 04:27 AM   #10
bensky
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Rep: Reputation: 0
I know you can fake the MAC-address on linux with ifconfig.

So is there a way to get the real MAC-address in C although the
user might have changed the MAC address with ifconfig or any other tool?

Thanks for any help on this... Ben
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting MAC Address with C++ (Linux) silentangel Programming 7 11-03-2012 02:36 PM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
DHCP Server MAC Address found, IP address not assigned wmburke Linux - Wireless Networking 17 11-17-2004 10:33 AM
MAC address cloning in Linux sb73542 Linux - Security 1 08-02-2003 07:38 PM
determining ppp ip address using perl Smerk Linux - Software 4 01-17-2003 07:12 AM


All times are GMT -5. The time now is 09:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration