I am trying to get the MAC addresses of my entire interface in OSX using C. The usual Linux methods for getting it do not work on BSD - from everything I've seen, you should get the interfaces and look for those that are of type AF_LINK. My problem is that LLADDR (sockaddr_dl) gives me a whole bunch of data (including my MAC) and I don't know what format the data is in. For instance; the following code is output:
Device: en1 link sdl_alen: 101 mac: 31: f8: 1e: DF: d 6: 22: 1d : 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: b0: 06: 10: 00: 01: 00: 00: 00: c0: 02: 10: 00: 01: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 40: 03: 10: 00: 01: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 03: 00: 6c: 6f: 30: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 00: 70: 03: 10: 00: 01: 00: 00: 00: e0: 02: 10: 00: 01: 00: 00:
My MAC is in bold. This seems to be the format all the time, but it would be much more convenient for me if I could put LLADDR (sockaddr_dl) on something. In net / if_dl.h, LLADDR is ignored as:
#define LLADDR(s) ((caddr_t)((s)->sdl_data + (s)->sdl_nlen))
which, as far as I can tell, suggests that the results are of type (void *) - no help.
Other posts like:
, Ethernet Mac OS X ioctl/SIOCGIFADDR/SIOCGIFCONF?
, , , , , - , sdl_alen 6.
int main() {
pcap_if_t *alldevs;
pcap_if_t *d;
pcap_addr_t *alladdrs;
pcap_addr_t *a;
struct sockaddr_dl* link;
char eb[PCAP_ERRBUF_SIZE];
char *addr_buf[40];
if (pcap_findalldevs(&alldevs, eb) == -1) {
printf("no devs found\n");
return(-1);
}
for (d = alldevs; d != NULL; d = d->next) {
printf("Device: %s\n", d->name);
alladdrs = d->addresses;
for (a = alladdrs; a != NULL; a = a->next) {
if(a->addr->sa_family == AF_LINK && a->addr->sa_data != NULL){
link = (struct sockaddr_dl*)a->addr->sa_data;
char mac[link->sdl_alen];
caddr_t macaddr = LLADDR(link);
memcpy(mac, LLADDR(link), link->sdl_alen);
printf("link sdl_alen: %i\n", link->sdl_alen);
int i;
printf("mac: ");
for(i = 0; i<link->sdl_alen; i++){
printf("%02x:", (unsigned char)mac[i]);
}
printf("\n");
}
}
}
}