Obtain the MAC address in C Code
-
Hi All,
As per the title can anyone assist with a method of obtaining the Onions MAC address using C code?
Kind Regards,
UFD
-
@UFD
One of the first Google results for "C linux get MAC": https://stackoverflow.com/a/6767517/5296568Either cross-compile the program or compile locally. No special flags needed
getmac.c
#include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> #include <stdio.h> #include <string.h> void mac_eth0(unsigned char MAC_str[13]) { #define HWADDR_len 6 int s,i; struct ifreq ifr; s = socket(AF_INET, SOCK_DGRAM, 0); strcpy(ifr.ifr_name, "br-wlan"); ioctl(s, SIOCGIFHWADDR, &ifr); for (i=0; i<HWADDR_len; i++) sprintf(&MAC_str[i*2],"%02X",((unsigned char*)ifr.ifr_hwaddr.sa_data)[i]); MAC_str[12]='\0'; } int main(int argc, char *argv[]) { unsigned char mac[13]; mac_eth0(mac); puts(mac); return 0; }
root@Omega-17FD:~# gcc -o getmac getmac.c root@Omega-17FD:~# ./getmac 40A36BC117FF
Notice how the printed MAC on the Onion does not match the output of the program. That's because the sticker is actually wrong - although my sticker reads
MAC: 40A36BC117FD
, the MAC returned byifconfig
is actually40:A3:6B:C1:17:FF
forbr-wlan0
and42:A3:6B:01:17:FD
forapcli0
.
-
@Maximilian-Gerhardt said in Obtain the MAC address in C Code:
Notice how the printed MAC on the Onion does not match the output of the program. That's because the sticker is actually wrong
The sticker probably isn't wrong. The board has two network interfaces, and as a result it has two MAC addresses.
They're related by a simple numerical substitution rule in one of the startup scripts.
is actually 40:A3:6B:C1:17:FF for br-wlan0
The above is the formally assigned address
and 42:A3:6B:01:17:FD for apcli0
Whiel that is a self-assigned address derived from the above via a substitution rule. Note the "2" in the first octet, which indicates it is a "locally administered" (vs unique-in-the-world) MAC address.
As for why the lowest octet in br-wlan0 is FF and not FD as assigned, that might be some sort of bug or odd decision in the startup code - seems like it wouldn't be a sequencing mistake at the factory, or the locally administered address would show it too.
If you poke around in the first few MTD partitions you can probably find what is actually saved in the flash chip.
-
Thanks @Maximilian-Gerhardt
I just changed "br-wlan" to "ra0" as this interface has a MAC value consistent with the sticker.