LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-12-2010, 03:11 PM   #1
Ayalaskin
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
How to change MAC addres via C code.


Hi Guys.
I need a way to change the machine MAC address via a C code in linux.
Ive find a way to retrieve it but not to change it.

If someone could help, i will appreciate very much.

Thanks in advance.
 
Old 04-12-2010, 04:09 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Ayalaskin View Post
Hi Guys.
I need a way to change the machine MAC address via a C code in linux.
Ive find a way to retrieve it but not to change it.

If someone could help, i will appreciate very much.

Thanks in advance.
AFAIR MAC address can be changed using 'ifconfig', so a logical step would be to look into 'ifconfig' source.
 
Old 04-12-2010, 05:43 PM   #3
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Rep: Reputation: 3
I'll just assume you know MAC addresses are best left untouched, and that you have a valid reason for wanting to change it. Something like this should work:
Code:
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <string.h>
#include <errno.h>

int main(int argc, char **argv) {
	struct ifreq ifr;
	int s;

	s = socket(AF_INET, SOCK_DGRAM, 0);
	assert(s != -1);

	strcpy(ifr.ifr_name, "eth0");
	ifr.ifr_hwaddr.sa_data[0] = 0xDE;
	ifr.ifr_hwaddr.sa_data[1] = 0xAD;
	ifr.ifr_hwaddr.sa_data[2] = 0xBE;
	ifr.ifr_hwaddr.sa_data[3] = 0xEF;
	ifr.ifr_hwaddr.sa_data[4] = 0xCA;
	ifr.ifr_hwaddr.sa_data[5] = 0xFE;
	ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
	assert(ioctl(s, SIOCSIFHWADDR, &ifr) != -1);

	return EXIT_SUCCESS;
}
--
Oscar Endre Edvardsen
 
Old 04-13-2010, 12:00 AM   #4
Ayalaskin
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the tip, ioctl worked great, besides its hard reading documentation.

Its for a college work, the teacher asked us to do a program to manage MAC address.
 
Old 04-13-2010, 10:15 AM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by haikan View Post
I'll just assume you know MAC addresses are best left untouched, and that you have a valid reason for wanting to change it.
When someone asks such a simple question, you should always start with the assumption it is a homework assignment, until proven otherwise. If you must give help, give hints, not answers.
 
  


Reply



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
Can't change MAC addres - ndiswrapper bjeuk Linux - Wireless Networking 8 02-03-2010 05:15 PM
The mac addres of the access point of integrated adsl wifi router cavazziniangelo Linux - Wireless Networking 1 11-01-2007 06:45 AM
How to get my MAC Address using c code? analyze Programming 8 09-23-2007 10:54 AM
Mac Error Code 69 kalyan Linux - Networking 0 04-01-2005 06:48 PM
change MAC Cubiq Linux - Networking 5 08-22-2002 02:50 PM

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

All times are GMT -5. The time now is 06:19 AM.

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