LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to get my MAC Address using c code? (https://www.linuxquestions.org/questions/programming-9/how-to-get-my-mac-address-using-c-code-586385/)

analyze 09-21-2007 12:39 PM

How to get my MAC Address using c code?
 
I'm trying to develop program that can get MAC Address by using c language, anyone can guide me or give example of code....Thank you!!!

Gethyn 09-21-2007 01:05 PM

I take it that a system call to /sbin/ifconfig isn't allowed? It certainly wouldn't be very portable or make nice code, but it would be fairly easy to implement.

analyze 09-21-2007 01:46 PM

i have to implement like a software to automatic send MAC Address to database, so i have to use c code to get the MAC Address.

theNbomr 09-21-2007 01:57 PM

Cribbed from the Sourceforge BACnet for Linux package:
Code:

/* LQanalyze.c: get MAC for specified interface
*/
#include <stdio.h>              /* Standard I/O */
#include <stdlib.h>            /* Standard Library */
#include <errno.h>              /* Error number and related */


#define ENUMS
#include <sys/socket.h>
#include <net/route.h>
#include <net/if.h>
#include <features.h>          /* for the glibc version number */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>      /* the L2 protocols */
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>    /* The L2 protocols */
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <netdb.h>


int main( int argc, char * argv[] ){

unsigned char  mac[IFHWADDRLEN];
int i;
    get_local_hwaddr( argv[1], mac );
    for( i = 0; i < IFHWADDRLEN; i++ ){
        printf( "%02X:", (unsigned int)(mac[i]) );
    }

}


int get_local_hwaddr(const char *ifname, unsigned char *mac)
{
    struct ifreq ifr;
    int fd;
    int rv;                    // return value - error value from df or ioctl call

    /* determine the local MAC address */
    strcpy(ifr.ifr_name, ifname);
    fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (fd < 0)
        rv = fd;
    else {
        rv = ioctl(fd, SIOCGIFHWADDR, &ifr);
        if (rv >= 0)            /* worked okay */
            memcpy(mac, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
    }

    return rv;
}

Code:

LQanalyze eth0
00:01:02:47:F4:54

--- rod.

analyze 09-21-2007 02:14 PM

Thank you!! so much...

salasi 09-21-2007 02:35 PM

What if there are - say - two ethernet cards in the machine? Would that be a problem to you? Or maybe you can be sure that there will only ever be one.

osor 09-21-2007 04:24 PM

Quote:

Originally Posted by salasi (Post 2899484)
What if there are - say - two ethernet cards in the machine? Would that be a problem to you?

How is this a problem? You just provide the other interface’s name to the nice little program. The only would-be problem here is portability, which I don’t see any way of resolving without a “cross-platform” library which uses platform-specific code behind-the-scenes (perhaps pcap).

analyze 09-23-2007 08:50 AM

I have another problem that i also write code for get the IP Address and the result is 127.0.0.1 T_T seem like it take the IP Address from local loop back, so i can't get MAC Address too. Could anyone help how to get eth0 instead of lo.

analyze@ubuntu:~/Desktop$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:16:D3:A4:F6:79
inet addr:192.168.1.33 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::216:d3ff:fea4:f679/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:499 errors:0 dropped:0 overruns:0 frame:0
TX packets:416 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:229934 (224.5 KiB) TX bytes:61790 (60.3 KiB)

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:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:300 (300.0 b) TX bytes:300 (300.0 b)

osor 09-23-2007 10:54 AM

Quote:

Originally Posted by analyze (Post 2901001)
I have another problem that i also write code for get the IP Address and the result is 127.0.0.1

Well, in order for us to help you, we must know what you did. If you want to go the ioctl() way, just use SIOCGIFADDR. You might also try using netlink sockets if you want to be more sophisticated.


All times are GMT -5. The time now is 07:39 PM.