Skip to content

Commit

Permalink
Merge pull request #82 from IdeasOnCanvas/enhancement/legacyIdentifier
Browse files Browse the repository at this point in the history
Make legacy-style primary MAC address retrieval available
  • Loading branch information
schwmi authored Aug 24, 2022
2 parents 912195c + 7046ce5 commit 74301b7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/IdeasOnCanvas/ASN1Decoder",
"state": {
"branch": null,
"revision": "6f36ef23becd7f9266ef6b026af4798996a1a8be",
"version": "1.8.1"
"revision": "1fa1e9c68c27cbed56e2a997605ae2e808cf4d98",
"version": "1.8.2"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import IOKit
extension AppReceiptValidator.Parameters.DeviceIdentifier {

/// On mac this is the primary network interface's MAC Adress as bytes
///
/// Note: this uses `getPrimaryNetworkMACAddress` but it may be advisable to try to use `getLegacyPrimaryNetworkMACAddress` in case of validation failure
static var installedDeviceIdentifierData: Data? {
return getPrimaryNetworkMACAddress()?.data
}
Expand All @@ -28,6 +30,49 @@ extension AppReceiptValidator.Parameters.DeviceIdentifier {
data.copyBytes(to: &address, count: address.count)


let addressString = address
.map { String(format: "%02x", $0) }
.joined(separator: ":")

return (data: Data(address), addressString: addressString)
}

/// Legacy Style of getting primary network MAC Address.
/// This is the way we retrieved the MAC Address before https://github.com/IdeasOnCanvas/AppReceiptValidator/pull/79
///
/// Finds the MAC Address of the primary network interface.
/// Original implementation https://gist.github.com/mminer/82975d3781e2f42fc644d7fbfbf4f905
///
/// - Returns: The MAC Address as Data and String representation
public static func getLegacyPrimaryNetworkMACAddress() -> (data: Data, addressString: String)? {
let matching = IOServiceMatching("IOEthernetInterface") as NSMutableDictionary
matching[kIOPropertyMatchKey] = ["IOPrimaryInterface": true]
var servicesIterator: io_iterator_t = 0
defer { IOObjectRelease(servicesIterator) }

guard IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &servicesIterator) == KERN_SUCCESS else { return nil }

var address: [UInt8] = [0, 0, 0, 0, 0, 0]
var service = IOIteratorNext(servicesIterator)

while service != 0 {
var controllerService: io_object_t = 0

defer {
IOObjectRelease(service)
IOObjectRelease(controllerService)
service = IOIteratorNext(servicesIterator)
}

guard IORegistryEntryGetParentEntry(service, "IOService", &controllerService) == KERN_SUCCESS else { continue }

let ref = IORegistryEntryCreateCFProperty(controllerService, "IOMACAddress" as CFString, kCFAllocatorDefault, 0)

guard let data = ref?.takeRetainedValue() as? Data else { continue }

data.copyBytes(to: &address, count: address.count)
}

let addressString = address
.map { String(format: "%02x", $0) }
.joined(separator: ":")
Expand Down

0 comments on commit 74301b7

Please sign in to comment.