1 /******************************************************************************
2  *
3  *  Copyright 2019 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include "hci/address.h"
20 
21 #include <algorithm>
22 #include <cstdint>
23 #include <cstdio>
24 #include <iomanip>
25 #include <sstream>
26 
27 #include "common/strings.h"
28 
29 namespace bluetooth {
30 namespace hci {
31 
32 const Address Address::kAny{{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
33 const Address Address::kEmpty{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
34 
35 // Address cannot initialize member variables as it is a POD type
36 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Address(const uint8_t (& addr)[6])37 Address::Address(const uint8_t (&addr)[6]) { std::copy(addr, addr + kLength, data()); }
38 
Address(std::initializer_list<uint8_t> l)39 Address::Address(std::initializer_list<uint8_t> l) {
40   std::copy(l.begin(), std::min(l.begin() + kLength, l.end()), data());
41 }
42 
_ToMaskedColonSepHexString(int bytes_to_mask) const43 std::string Address::_ToMaskedColonSepHexString(int bytes_to_mask) const {
44   std::stringstream ss;
45   int count = 0;
46   for (auto it = address.rbegin(); it != address.rend(); it++) {
47     if (count++ < bytes_to_mask) {
48       ss << "xx";
49     } else {
50       ss << std::nouppercase << std::hex << std::setw(2) << std::setfill('0') << +*it;
51     }
52     if (std::next(it) != address.rend()) {
53       ss << ':';
54     }
55   }
56   return ss.str();
57 }
58 
ToString() const59 std::string Address::ToString() const { return _ToMaskedColonSepHexString(0); }
60 
ToColonSepHexString() const61 std::string Address::ToColonSepHexString() const { return _ToMaskedColonSepHexString(0); }
62 
ToStringForLogging() const63 std::string Address::ToStringForLogging() const { return _ToMaskedColonSepHexString(0); }
64 
ToRedactedStringForLogging() const65 std::string Address::ToRedactedStringForLogging() const { return _ToMaskedColonSepHexString(4); }
66 
ToLegacyConfigString() const67 std::string Address::ToLegacyConfigString() const { return ToString(); }
68 
FromLegacyConfigString(const std::string & str)69 std::optional<Address> Address::FromLegacyConfigString(const std::string& str) {
70   return FromString(str);
71 }
72 
FromString(const std::string & from)73 std::optional<Address> Address::FromString(const std::string& from) {
74   if (from.length() != 17) {
75     return std::nullopt;
76   }
77 
78   Address addr{};
79   std::istringstream stream(from);
80   std::string token;
81   int index = 0;
82   while (getline(stream, token, ':')) {
83     if (index >= 6) {
84       return std::nullopt;
85     }
86 
87     if (token.length() != 2) {
88       return std::nullopt;
89     }
90 
91     char* temp = nullptr;
92     addr.address.at(5 - index) = std::strtol(token.c_str(), &temp, 16);
93     if (temp == token.c_str()) {
94       // string token is empty or has wrong format
95       return std::nullopt;
96     }
97     if (temp != (token.c_str() + token.size())) {
98       // cannot parse whole string
99       return std::nullopt;
100     }
101 
102     index++;
103   }
104 
105   if (index != 6) {
106     return std::nullopt;
107   }
108 
109   return addr;
110 }
111 
FromString(const std::string & from,Address & to)112 bool Address::FromString(const std::string& from, Address& to) {
113   auto addr = FromString(from);
114   if (!addr) {
115     to = {};
116     return false;
117   }
118   to = std::move(*addr);
119   return true;
120 }
121 
FromOctets(const uint8_t * from)122 size_t Address::FromOctets(const uint8_t* from) {
123   std::copy(from, from + kLength, data());
124   return kLength;
125 }
126 
IsValidAddress(const std::string & address)127 bool Address::IsValidAddress(const std::string& address) {
128   return Address::FromString(address).has_value();
129 }
130 
131 }  // namespace hci
132 }  // namespace bluetooth
133