/src/Fast-DDS/src/cpp/utils/Host.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | /* |
16 | | * Host.cpp |
17 | | * |
18 | | */ |
19 | | |
20 | | #include <utils/Host.hpp> |
21 | | |
22 | | #if defined(_WIN32) |
23 | | #include <WinSock2.h> // Avoid conflicts with WinSock of Windows.h |
24 | | #include <windows.h> |
25 | | #include <process.h> |
26 | | #elif defined(__APPLE__) |
27 | | #include <IOKit/IOKitLib.h> |
28 | | #else |
29 | | #include <unistd.h> |
30 | | #include <fcntl.h> |
31 | | #endif // if defined(_WIN32) |
32 | | |
33 | | |
34 | | namespace eprosima { |
35 | | |
36 | | fastcdr::string_255 Host::compute_machine_id() |
37 | 0 | { |
38 | | #ifdef _WIN32 |
39 | | char machine_id[255]; |
40 | | DWORD BufferSize = sizeof(machine_id); |
41 | | LONG res = RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGuid", RRF_RT_REG_SZ, |
42 | | NULL, machine_id, &BufferSize); |
43 | | if (res == 0) |
44 | | { |
45 | | return machine_id; |
46 | | } |
47 | | return ""; |
48 | | #elif defined(__APPLE__) |
49 | | io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMainPortDefault, "IOService:/"); |
50 | | if (!ioRegistryRoot) |
51 | | { |
52 | | return ""; |
53 | | } |
54 | | CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR( |
55 | | kIOPlatformUUIDKey), kCFAllocatorDefault, 0); |
56 | | IOObjectRelease(ioRegistryRoot); |
57 | | if (!uuidCf) |
58 | | { |
59 | | return ""; |
60 | | } |
61 | | |
62 | | char buf[255]; |
63 | | if (!CFStringGetCString(uuidCf, buf, sizeof(buf), kCFStringEncodingUTF8)) |
64 | | { |
65 | | return ""; |
66 | | } |
67 | | CFRelease(uuidCf); |
68 | | return buf; |
69 | | #elif defined(_POSIX_SOURCE) |
70 | | int fd = open("/etc/machine-id", O_RDONLY); |
71 | 0 | if (fd == -1) |
72 | 0 | { |
73 | 0 | return ""; |
74 | 0 | } |
75 | | |
76 | 0 | char buffer[33] = {0}; |
77 | 0 | ssize_t bytes_read = read(fd, buffer, 32); |
78 | 0 | close(fd); |
79 | |
|
80 | 0 | if (bytes_read < 32) |
81 | 0 | { |
82 | 0 | return ""; |
83 | 0 | } |
84 | | |
85 | 0 | return buffer; |
86 | | #else |
87 | | return ""; |
88 | | #endif // if defined(_WIN32) |
89 | 0 | } |
90 | | |
91 | | } // eprosima |