LinuxQuestions.org
Help answer threads with 0 replies.
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 01-15-2004, 06:04 AM   #1
ofada
Member
 
Registered: Jan 2004
Posts: 32

Rep: Reputation: 15
find MAC ADDRESS program C


I want to find my MAC ADDRESS in C language.
There is the 'detect' library but it doesnt detect hardware address.
Thank you for your help
bye
 
Old 01-15-2004, 08:13 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
ok, ive done a little detective work on ifconfig and from what i can see you want to use the SIOCGIFHWADDR ioctl on an open network socket, it takes a pointer to a struct ifreq which is defined in include/linux/if.h it might be worth looking at the source to ifconfig because this is just an estimated guess from the output of strace and some greps.

post back if you need an example program and i'll try to write one.

<edit>

what was wrong with the post from Hiper in Linux Hardware? http://www.linuxquestions.org/questi...hreadid=135016

please dont double post, if you feel the answer wasnt good enough then post back or if you want your thread moved click the report to moderator link and ask them to move to another forum

</edit>

Last edited by kev82; 01-15-2004 at 08:32 AM.
 
Old 01-15-2004, 01:40 PM   #3
ofada
Member
 
Registered: Jan 2004
Posts: 32

Original Poster
Rep: Reputation: 15
If you can post me an example (i dont care if it doenst work properly) it will be cool.
Thanks!
 
Old 01-15-2004, 01:50 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
Code:
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <stdio.h>
#include <linux/if.h>

int main()
{
	int sd,i;
	struct ifreq ir;

	sd=socket(PF_INET, SOCK_STREAM, 0);
	memset(&ir, 0, sizeof(ir));

	strcpy(ir.ifr_name, "eth0");
	ioctl(sd, SIOCGIFHWADDR, &ir);

	close(sd);

	for(i=0;i<6;i++) {
		printf("%.2hhx:", ir.ifr_hwaddr.sa_data[i]);
	}

	return 0;
}
<edit>
there is another ioctl you could use to get the ip of the interface but i'll leave that to you
</edit>

Last edited by kev82; 01-15-2004 at 02:01 PM.
 
Old 01-15-2004, 02:27 PM   #5
ofada
Member
 
Registered: Jan 2004
Posts: 32

Original Poster
Rep: Reputation: 15
thank you I will try this.
Bye
 
  


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
How to find an IP address from the MAC address of a remote machine ? jitz Linux - General 3 01-03-2006 08:55 AM
How to find IP address of a machine if I know their MAC Address dysenteryduke Linux - Networking 13 09-12-2005 11:21 AM
How to find MAC address of a specified IP address ? longtongfish Programming 1 07-12-2005 04:26 PM
how to find IP if I have MAC address bojo Linux - Networking 3 11-29-2003 11:10 PM
how to find out my MAC address gexiaofei Linux - Software 9 08-18-2003 01:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration