Skip to content

Instantly share code, notes, and snippets.

@liscio liscio/copy_mac_address.m Secret
Created Oct 21, 2019

Embed
What would you like to do?
#import <IOKit/IOKitLib.h>
#import <Foundation/Foundation.h>
// Returns a CFData object, containing the computer's GUID.
CFDataRef copy_mac_address(void)
{
kern_return_t kernResult;
mach_port_t master_port;
CFMutableDictionaryRef matchingDict;
io_iterator_t iterator;
io_object_t service;
CFDataRef macAddress = nil;
kernResult = IOMasterPort(MACH_PORT_NULL, &master_port);
if (kernResult != KERN_SUCCESS) {
printf("IOMasterPort returned %d\n", kernResult);
return nil;
}
matchingDict = IOBSDNameMatching(master_port, 0, "en0");
if (!matchingDict) {
printf("IOBSDNameMatching returned empty dictionary\n");
return nil;
}
kernResult = IOServiceGetMatchingServices(master_port, matchingDict, &iterator);
if (kernResult != KERN_SUCCESS) {
printf("IOServiceGetMatchingServices returned %d\n", kernResult);
return nil;
}
while((service = IOIteratorNext(iterator)) != 0) {
io_object_t parentService;
kernResult = IORegistryEntryGetParentEntry(service, kIOServicePlane,
&parentService);
if (kernResult == KERN_SUCCESS) {
if (macAddress) CFRelease(macAddress);
macAddress = (CFDataRef) IORegistryEntryCreateCFProperty(parentService,
CFSTR("IOMACAddress"), kCFAllocatorDefault, 0);
IOObjectRelease(parentService);
} else {
printf("IORegistryEntryGetParentEntry returned %d\n", kernResult);
}
IOObjectRelease(service);
}
IOObjectRelease(iterator);
return macAddress;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.