Networking Forums

Networking Forums > Computer Networking > Linux Networking > Get the permanent mac address of a network card

Reply
Thread Tools Display Modes

Get the permanent mac address of a network card

 
 
cris
Guest
Posts: n/a

 
      08-28-2003, 07:47 PM
I want get the permanent mac address of a network card,
programmatically, in C language, for example with a ioctl o similar.

But the mac address is updateable via software with ifconfig or other
commands. In this case I want get the real mac address set in the
eprom of a network card by the factory, not the modified mac address.

Is this possible ?
If it is possible, have you a sample code for get this information in
c lang ?

Thanks in advance.
 
Reply With Quote
 
 
 
 
Floyd Davidson
Guest
Posts: n/a

 
      08-28-2003, 10:24 PM
(E-Mail Removed) (cris) wrote:
>I want get the permanent mac address of a network card,
>programmatically, in C language, for example with a ioctl o similar.
>
>But the mac address is updateable via software with ifconfig or other
>commands. In this case I want get the real mac address set in the
>eprom of a network card by the factory, not the modified mac address.
>
>Is this possible ?
>If it is possible, have you a sample code for get this information in
>c lang ?
>
>Thanks in advance.


I don't know if you can get at the hardwired MAC address or not.
The following program demonstrates how to get the address that the
interface is actually using.

#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <arpa/inet.h>

int main(void)
{
int sfd;
unsigned char *u;
struct ifreq ifr;
struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;

memset(&ifr, 0, sizeof ifr);

if (0 > (sfd = socket(AF_INET, SOCK_STREAM, 0))) {
perror("socket()");
exit(EXIT_FAILURE);
}

strcpy(ifr.ifr_name, "eth0");
sin->sin_family = AF_INET;

if (0 == ioctl(sfd, SIOCGIFADDR, &ifr)) {
printf("%s: %s\n", ifr.ifr_name, inet_ntoa(sin->sin_addr));
}

if (0 > ioctl(sfd, SIOCGIFHWADDR, &ifr)) {
return EXIT_FAILURE;
}

u = (unsigned char *) &ifr.ifr_addr.sa_data;

if (u[0] + u[1] + u[2] + u[3] + u[4] + u[5]) {
printf("HW Address: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
u[0], u[1], u[2], u[3], u[4], u[5]);
}

return EXIT_SUCCESS;
}


--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) (E-Mail Removed)
 
Reply With Quote
 
cris
Guest
Posts: n/a

 
      09-02-2003, 09:42 PM
Thank you very much, Floyd.

I have tried your code. But if i change the mac address via ifconfig I
get the running mac address and not the real mac address of the
network card.

Anyone know how get the real mac address of the network card ?

Thanks again, Floyd.


Floyd Davidson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) (cris) wrote:
> >I want get the permanent mac address of a network card,
> >programmatically, in C language, for example with a ioctl o similar.
> >
> >But the mac address is updateable via software with ifconfig or other
> >commands. In this case I want get the real mac address set in the
> >eprom of a network card by the factory, not the modified mac address.
> >
> >Is this possible ?
> >If it is possible, have you a sample code for get this information in
> >c lang ?
> >
> >Thanks in advance.

>
> I don't know if you can get at the hardwired MAC address or not.
> The following program demonstrates how to get the address that the
> interface is actually using.
>
> #define _BSD_SOURCE
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/ioctl.h>
> #include <sys/socket.h>
> #include <sys/types.h>
> #include <net/if.h>
> #include <arpa/inet.h>
>
> int main(void)
> {
> int sfd;
> unsigned char *u;
> struct ifreq ifr;
> struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
>
> memset(&ifr, 0, sizeof ifr);
>
> if (0 > (sfd = socket(AF_INET, SOCK_STREAM, 0))) {
> perror("socket()");
> exit(EXIT_FAILURE);
> }
>
> strcpy(ifr.ifr_name, "eth0");
> sin->sin_family = AF_INET;
>
> if (0 == ioctl(sfd, SIOCGIFADDR, &ifr)) {
> printf("%s: %s\n", ifr.ifr_name, inet_ntoa(sin->sin_addr));
> }
>
> if (0 > ioctl(sfd, SIOCGIFHWADDR, &ifr)) {
> return EXIT_FAILURE;
> }
>
> u = (unsigned char *) &ifr.ifr_addr.sa_data;
>
> if (u[0] + u[1] + u[2] + u[3] + u[4] + u[5]) {
> printf("HW Address: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
> u[0], u[1], u[2], u[3], u[4], u[5]);
> }
>
> return EXIT_SUCCESS;
> }

 
Reply With Quote
 
cris
Guest
Posts: n/a

 
      09-02-2003, 09:52 PM
Anyone does know how to verify, programmatically via c lang, if a mac
address in a redhat linux systen is a 'software' mac adddres (changed
in the system via ifconfig or similar) ?

Thanks in advance.
 
Reply With Quote
 
Peter T. Breuer
Guest
Posts: n/a

 
      09-03-2003, 04:27 PM
cris <(E-Mail Removed)> wrote:
> Anyone does know how to verify, programmatically via c lang, if a mac


Another person uses "programmatically"! I swear that is not an english
word! I haveonly ever seen french people use it. And now an italian.

> address in a redhat linux systen is a 'software' mac adddres (changed
> in the system via ifconfig or similar) ?


The mac address is always for real. You can ask the card what it is
(see all that rtnetlink stuff in the kernel and read source of
ifconfig). I don't know if you can find out what the default was.

Peter
 
Reply With Quote
 
Tauno Voipio
Guest
Posts: n/a

 
      09-03-2003, 08:03 PM

"cris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Thank you very much, Floyd.
>
> I have tried your code. But if i change the mac address via ifconfig I
> get the running mac address and not the real mac address of the
> network card.


The thing you are requesting is different for each type of interface chip
and NIC. On most cards, the on-card MAC address is stored on a small serial
non-volatile memory connected to the network interface chip. The actual
protocol needed to access the memory is heavily dependent on the interface
chip. For an example, get the data sheet of SMSC LAN91C96 from the company
website.

There is no universal ioctl() implemented on the NIC drivers to read the
on-board stored MAC.

What would you need the stored MAC for?

(Please note that the open-source community is quite sensitive to
host-identifier or copy-protection issues).

HTH

Tauno Voipio
tauno voipio @ iki fi



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make IP address permanent? Fred Linux Networking 5 06-14-2008 06:52 PM
How to limit DHCP server address assignment to a single network card? Eric Long Windows Networking 3 04-19-2007 03:23 PM
How do I make network mappings (mounts?) permanent Bob Macpherson Linux Networking 1 06-22-2005 10:24 AM
Cannot assign tcpip address on any network device or card.. Etop Udoh Windows Networking 4 03-20-2005 06:19 AM
Configure a Permanent IP Address for Network Devices tg Windows Networking 0 02-24-2004 08:32 PM



1 2 3 4 5 6 7 8 9 10 11