Re: MAC address for Ethernet interface

Sean Estabrooks <[email protected]> Fri, 12 Dec 2003 04:37:27 -0500
Newsgroups gmane.linux.redhat.devel
Message-ID <[email protected]>
On 12 Dec 2003 08:31:32 -0000
"anil  garrepally" <[email protected]> wrote:

> Hi,
> 
> Is there any system call available for getting the MAC address of
> ethernet interface. I need it in my program.
> 
> Anil.

Sure,

You can make a "SIOCGIFHWADDR" ioctl call on an open socket.
Here's a small example _without_ proper error checking:

#include <sys/socket.h>
#include <linux/sockios.h>
#include <linux/if.h>
main()
{
     int sd, i;
    struct ifreq buff;

    sd = socket(PF_INET,SOCK_DGRAM,0);
    strncpy(buff.ifr_name, "eth0", IFNAMSIZ);
    ioctl(sd,SIOCGIFHWADDR,&buff);

    for (i=0; i < 6; i++)
      printf("%02X:", (unsigned char)buff.ifr_hwaddr.sa_data[i]);
    printf("\n");
}
 
Regards,
Sean