Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 1 | #include "aos/uuid.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 2 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 3 | #include <fcntl.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 6 | #include <array> |
| 7 | #include <random> |
| 8 | #include <string_view> |
| 9 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame^] | 10 | #include "gflags/gflags.h" |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 11 | #include "glog/logging.h" |
| 12 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame^] | 13 | DEFINE_string(boot_uuid, "", |
| 14 | "If set, override the boot UUID to have this value instead."); |
| 15 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 16 | namespace aos { |
| 17 | namespace { |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 18 | void ToHex(const uint8_t *val, char *result, size_t count) { |
| 19 | while (count > 0) { |
| 20 | int upper = ((*val) >> 4) & 0xf; |
| 21 | if (upper < 10) { |
| 22 | result[0] = upper + '0'; |
| 23 | } else { |
| 24 | result[0] = upper - 10 + 'a'; |
| 25 | } |
| 26 | |
| 27 | int lower = (*val) & 0xf; |
| 28 | if (lower < 10) { |
| 29 | result[1] = lower + '0'; |
| 30 | } else { |
| 31 | result[1] = lower - 10 + 'a'; |
| 32 | } |
| 33 | |
| 34 | ++val; |
| 35 | result += 2; |
| 36 | --count; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 39 | |
| 40 | void FromHex(const char *val, uint8_t *result, size_t count) { |
| 41 | while (count > 0) { |
| 42 | CHECK((val[0] >= '0' && val[0] <= '9') || (val[0] >= 'a' && val[0] <= 'f')) |
| 43 | << ": Invalid hex '" << val[0] << "'"; |
| 44 | CHECK((val[1] >= '0' && val[1] <= '9') || (val[1] >= 'a' && val[1] <= 'f')) |
| 45 | << ": Invalid hex '" << val[1] << "'"; |
| 46 | |
| 47 | uint8_t converted = 0; |
| 48 | if (val[0] < 'a') { |
| 49 | converted |= static_cast<uint8_t>(val[0] - '0') << 4; |
| 50 | } else { |
| 51 | converted |= (static_cast<uint8_t>(val[0] - 'a') + 0xa) << 4; |
| 52 | } |
| 53 | if (val[1] < 'a') { |
| 54 | converted |= static_cast<uint8_t>(val[1] - '0'); |
| 55 | } else { |
| 56 | converted |= (static_cast<uint8_t>(val[1] - 'a') + 0xa); |
| 57 | } |
| 58 | *result = converted; |
| 59 | |
| 60 | val += 2; |
| 61 | ++result; |
| 62 | --count; |
| 63 | } |
| 64 | } |
| 65 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 66 | } // namespace |
| 67 | |
| 68 | UUID UUID::Random() { |
| 69 | std::random_device rd; |
| 70 | std::mt19937 gen(rd()); |
| 71 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 72 | std::uniform_int_distribution<> dis(0, 255); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 73 | std::uniform_int_distribution<> dis2(8, 11); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 74 | UUID result; |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 75 | for (size_t i = 0; i < kDataSize; ++i) { |
| 76 | result.data_[i] = dis(gen); |
| 77 | } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 78 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 79 | // Mark the reserved bits in the data that this is a uuid4, a random UUID. |
| 80 | result.data_[6] = (result.data_[6] & 0x0f) | 0x40; |
| 81 | result.data_[8] = (result.data_[6] & 0x3f) | 0x80; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 82 | |
| 83 | return result; |
| 84 | } |
| 85 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 86 | std::string UUID::ToString() const { |
| 87 | std::string out; |
| 88 | out.resize(UUID::kStringSize); |
| 89 | CopyTo(out.data()); |
| 90 | return out; |
| 91 | } |
| 92 | |
| 93 | std::ostream &operator<<(std::ostream &os, const UUID &uuid) { |
| 94 | return os << uuid.ToString(); |
| 95 | } |
| 96 | |
| 97 | flatbuffers::Offset<flatbuffers::String> UUID::PackString( |
| 98 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 99 | std::array<char, kStringSize> data; |
| 100 | CopyTo(data.data()); |
| 101 | |
| 102 | return fbb->CreateString(data.data(), data.size()); |
| 103 | } |
| 104 | |
| 105 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> UUID::PackVector( |
| 106 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 107 | return fbb->CreateVector(data_.data(), data_.size()); |
| 108 | } |
| 109 | |
| 110 | void UUID::CopyTo(char *result) const { |
| 111 | ToHex(&data_[0], result, 4); |
| 112 | result[8] = '-'; |
| 113 | ToHex(&data_[4], result + 9, 2); |
| 114 | result[13] = '-'; |
| 115 | ToHex(&data_[6], result + 14, 2); |
| 116 | result[18] = '-'; |
| 117 | ToHex(&data_[8], result + 19, 2); |
| 118 | result[23] = '-'; |
| 119 | ToHex(&data_[10], result + 24, 6); |
| 120 | } |
| 121 | |
| 122 | UUID UUID::Zero() { |
| 123 | UUID result; |
| 124 | std::memset(result.data_.data(), 0, result.data_.size()); |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | UUID UUID::FromString(const flatbuffers::String *str) { |
| 129 | return FromString(str->string_view()); |
| 130 | } |
| 131 | |
| 132 | UUID UUID::FromVector(const flatbuffers::Vector<uint8_t> *data) { |
| 133 | CHECK(data != nullptr); |
| 134 | CHECK_EQ(data->size(), kDataSize); |
| 135 | |
| 136 | UUID result; |
| 137 | std::memcpy(result.data_.data(), data->Data(), kDataSize); |
| 138 | return result; |
| 139 | } |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 140 | |
| 141 | UUID UUID::FromString(std::string_view str) { |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 142 | CHECK_EQ(str.size(), kStringSize); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 143 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 144 | UUID result; |
| 145 | FromHex(str.data(), result.data_.data(), 4); |
| 146 | CHECK(str.data()[8] == '-' && str.data()[13] == '-' && |
| 147 | str.data()[18] == '-' && str.data()[23] == '-') |
| 148 | << ": Invalid uuid."; |
| 149 | FromHex(str.data() + 9, result.data_.data() + 4, 2); |
| 150 | FromHex(str.data() + 14, result.data_.data() + 6, 2); |
| 151 | FromHex(str.data() + 19, result.data_.data() + 8, 2); |
| 152 | FromHex(str.data() + 24, result.data_.data() + 10, 6); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 153 | return result; |
| 154 | } |
| 155 | |
| 156 | UUID UUID::BootUUID() { |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame^] | 157 | if (!FLAGS_boot_uuid.empty()) { |
| 158 | return UUID::FromString(FLAGS_boot_uuid); |
| 159 | } |
| 160 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 161 | int fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY); |
| 162 | PCHECK(fd != -1); |
| 163 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 164 | std::array<char, kStringSize> data; |
| 165 | CHECK_EQ(static_cast<ssize_t>(kStringSize), |
| 166 | read(fd, data.begin(), kStringSize)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 167 | close(fd); |
| 168 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 169 | return UUID::FromString(std::string_view(data.data(), data.size())); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 172 | } // namespace aos |