MAC Address from OS X (C)

This may be a stupid question, and I apologize if it has already been considered here, but I was looking quite a bit without much luck. I am trying to get my hardware interface address in C and I am using OS X (x86-64). I know how to get it using ifconfig , but I want my program to load automatically for any computer, well, at least for OS X computers. I found another thread that posted this link , which pretty much does what i want (with some changes) but i cant do iokit link in ld (my gcc compiler). I tried adding the -lIOKit and -framework IOKit to the gcc command line, but still getting the same link errors. Here is a link to my code: header and source .

+7
source share
1 answer

This small program will work unchanged in OSX.

Code: (Alecs King credits from the freebsd list)

 #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <sys/sysctl.h> #include <net/if.h> #include <net/if_dl.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int mib[6], len; char *buf; unsigned char *ptr; struct if_msghdr *ifm; struct sockaddr_dl *sdl; if (argc != 2) { fprintf(stderr, "Usage: getmac <interface>\n"); return 1; } mib[0] = CTL_NET; mib[1] = AF_ROUTE; mib[2] = 0; mib[3] = AF_LINK; mib[4] = NET_RT_IFLIST; if ((mib[5] = if_nametoindex(argv[1])) == 0) { perror("if_nametoindex error"); exit(2); } if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { perror("sysctl 1 error"); exit(3); } if ((buf = malloc(len)) == NULL) { perror("malloc error"); exit(4); } if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { perror("sysctl 2 error"); exit(5); } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm + 1); ptr = (unsigned char *)LLADDR(sdl); printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)); return 0; } 

However, you must change int len; on size_t len;

+6
source

All Articles