std::string generateHostUUID() { std::string hardware_uuid; #ifdef __APPLE__ // Use the hardware UUID available on OSX to identify this machine uuid_t id; // wait at most 5 seconds for gethostuuid to return const timespec wait = {5, 0}; if (gethostuuid(id, &wait) == 0) { char out[128] = {0}; uuid_unparse(id, out); hardware_uuid = std::string(out); } #elif WIN32 WmiRequest wmiUUIDReq("Select UUID from Win32_ComputerSystemProduct"); std::vector<WmiResultItem>& wmiUUIDResults = wmiUUIDReq.results(); if (wmiUUIDResults.size() != 0) { wmiUUIDResults[0].GetString("UUID", hardware_uuid); } #else readFile("/sys/class/dmi/id/product_uuid", hardware_uuid); #endif // We know at least Linux will append a newline. hardware_uuid.erase( std::remove(hardware_uuid.begin(), hardware_uuid.end(), '\n'), hardware_uuid.end()); boost::algorithm::trim(hardware_uuid); if (!hardware_uuid.empty()) { // Construct a new string to remove trailing nulls. return std::string(hardware_uuid.c_str()); } // Unable to get the hardware UUID, just return a new UUID return generateNewUUID(); }
__private_extern__ CFStringRef _CFGetHostUUIDString(void) { static CFStringRef __hostUUIDString = NULL; if (!__hostUUIDString) { CFUUIDBytes uuidBytes; int getuuidErr = 0; struct timespec timeout = {0, 0}; // Infinite timeout for gethostuuid() getuuidErr = gethostuuid((unsigned char *)&uuidBytes, &timeout); if (getuuidErr == -1) { // An error has occurred trying to get the host UUID string. There's nothing we can do here, so we should just return NULL. CFLog(kCFLogLevelWarning, CFSTR("_CFGetHostUUIDString: unable to determine UUID for host. Error: %d"), errno); return NULL; } CFUUIDRef uuidRef = CFUUIDCreateFromUUIDBytes(kCFAllocatorSystemDefault, uuidBytes); CFStringRef uuidAsString = CFUUIDCreateString(kCFAllocatorSystemDefault, uuidRef); if (!OSAtomicCompareAndSwapPtrBarrier(NULL, (void *)uuidAsString, (void *)&__hostUUIDString)) { CFRelease(uuidAsString); // someone else made the assignment, so just release the extra string. } CFRelease(uuidRef); } return __hostUUIDString; }
DNSServiceErrorType RegisterWorkstationService(MyDNSServiceState *ref, CFStringRef serviceName) { char name[64]; DNSServiceErrorType error = kDNSServiceErr_BadParam; if (CFStringGetCString(serviceName, name, sizeof(name), kCFStringEncodingUTF8)) { uuid_t compUUID; CFDataRef cfTXTRecord = NULL; struct timespec waitTime = { 0 }; if ( gethostuuid(compUUID, &waitTime) == 0 ) { CFMutableDictionaryRef txtRecordDict = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks ); uuid_string_t uuidstr; uuid_unparse_upper( compUUID, uuidstr ); CFStringRef cfUUID = CFStringCreateWithCString( kCFAllocatorDefault, uuidstr, kCFStringEncodingUTF8 ); CFDictionarySetValue( txtRecordDict, CFSTR("uuid"), cfUUID ); DbgLog( kLogInfo, "Workstation service - uuid = %s", uuidstr ); DSCFRelease( cfUUID ); cfTXTRecord = CFNetServiceCreateTXTDataWithDictionary( kCFAllocatorDefault, txtRecordDict ); DSCFRelease( txtRecordDict ); } else { DbgLog( kLogError, "Failed to get host UUID from gethostuuid()" ); } error = DNSServiceRegister(&ref->service, kDNSServiceFlagsNoAutoRename, kDNSServiceInterfaceIndexAny, name, kWorkstationType, NULL, NULL, htons(kWorkstationPort), (cfTXTRecord ? CFDataGetLength(cfTXTRecord) : 0), (cfTXTRecord ? CFDataGetBytePtr(cfTXTRecord) : NULL), RegisterWorkstationCallBack, (void *)ref); DSCFRelease( cfTXTRecord ); } if (kDNSServiceErr_NoError == error) { DbgLog( kLogNotice, "Registered Workstation service - %s.%s", name, kWorkstationType ); MyDNSServiceAddToRunLoop(ref); } return error; }
EXPORT BOOL NtSystemDebugControl(void) { uuid_t id; struct timespec wait; wait.tv_sec = 0; wait.tv_nsec = 0; int error; error = gethostuuid(id, &wait); return 0; }
static const char * get_host_uuid() { static uuid_string_t hostuuid = {}; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ struct timespec timeout = {30, 0}; uuid_t uuid = {}; if (gethostuuid(uuid, &timeout) == 0) { uuid_unparse(uuid, hostuuid); } else { secerror("failed to get host uuid"); } });
// We use this to get a deterministic id for the device hosting the BusObject void GetDeviceUUID(char *result) { uuid_t uuid; timespec wait; wait.tv_sec = 0; wait.tv_nsec = 0; if (gethostuuid(uuid, &wait) == 0) { int i, j; for (i = 0, j = 0; i < 16; ++i) { if (i == 4 || i == 6 || i == 8 || i == 10) { result[i * 2 + j++] = '-'; } sprintf(result + i * 2 + j, "%02x", uuid[i]); } result[i * 2 + j] = 0; } }
std::string generateHostUUID() { #ifdef __APPLE__ // Use the hardware UUID available on OSX to identify this machine uuid_t id; // wait at most 5 seconds for gethostuuid to return const timespec wait = {5, 0}; int result = gethostuuid(id, &wait); if (result == 0) { char out[128] = {0}; uuid_unparse(id, out); std::string uuid_string = std::string(out); boost::algorithm::trim(uuid_string); return uuid_string; } else { // Unable to get the hardware UUID, just return a new UUID return generateNewUUID(); } #else return generateNewUUID(); #endif }