Home > How To > MAC for specific Adapter on Linux

MAC for specific Adapter on Linux

Today was working on getting a unique information for a system. And MAC would be enough for my requirement. (Need not go for a UID). Below is the code to find MAC for a specific Adapter. “eth0” for my case.
Only different thing here is the structure ifreq which I used for the first time.



Here is the struct information.

struct ifreq {

    char    ifr_name[IFNAMSIZ];/* Interface name */
    union {
            struct sockaddr ifr_addr;
            struct sockaddr ifr_dstaddr;
            struct sockaddr ifr_broadaddr;
            struct sockaddr ifr_netmask;
            struct sockaddr ifr_hwaddr;
            short   ifr_flags;
            int     ifr_ifindex;
            int     ifr_metric;
            int     ifr_mtu;
            struct ifmapifr_map;
            char    ifr_slave[IFNAMSIZ];
            char    ifr_newname[IFNAMSIZ];
            char *  ifr_data;
    };
};
struct ifconf { 
    int ifc_len;    /* size of buffer */
    union {            
            char *  ifc_buf; /* buffer address */ 
            struct ifreq *ifc_req; /* array of structures */
    };  
};

You can more information about this structure here below. 




#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

int main()
{
    int socketDescriptor;
    struct ifreq mac_info;
    /*UDP Socket created here*/
    socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
    mac_info.ifr_addr.sa_family = AF_INET;

    /*Information for eth0*/
    strncpy(mac_info.ifr_name, “eth0”, IFNAMSIZ-1);
    ioctl(socketDescriptor, SIOCGIFHWADDR, &mac_info);

    /*Closing Socket*/
    close(socketDescriptor);

    /*Lets Print this information*/
    printf(“%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n”,
         (unsigned char)mac_info.ifr_hwaddr.sa_data[0],
         (unsigned char)mac_info.ifr_hwaddr.sa_data[1],
         (unsigned char)mac_info.ifr_hwaddr.sa_data[2],
         (unsigned char)mac_info.ifr_hwaddr.sa_data[3],
         (unsigned char)mac_info.ifr_hwaddr.sa_data[4],
         (unsigned char)mac_info.ifr_hwaddr.sa_data[5]);
    return 0;
}

OUTPUT:
 
ahmed@ahmed-work-horse:~/rnd$ gcc testMac.c 
ahmed@ahmed-work-horse:~/rnd$ ./a.out 
00:12:64:12:7f:0a
ahmed@ahmed-work-horse:~/rnd$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:12:64:12:7f:0a  
          inet addr:172.16.2.15  Bcast:172.16.2.255  Mask:255.255.255.0
          inet6 addr: fe80::225:64ff:fe82:7f0a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3158057 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2005005 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2865103780 (2.8 GB)  TX bytes:200486870 (200.4 MB)
          Interrupt:42 Base address:0x4000 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:41143 errors:0 dropped:0 overruns:0 frame:0
          TX packets:41143 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:10216832 (10.2 MB)  TX bytes:10216832 (10.2 MB)
vmnet1    Link encap:Ethernet  HWaddr 00:50:12:c0:00:01  
          inet addr:172.16.45.1  Bcast:172.16.45.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fec0:1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1028 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
vmnet8    Link encap:Ethernet  HWaddr 00:50:12:c0:00:08  
          inet addr:172.16.167.1  Bcast:172.16.167.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fec0:8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1377 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1033 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
ahmed@ahmed-work-horse:~/rnd$
Advertisements
Categories: How To
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: