Deprecate mac address manipulation functions in favor or MacAddress class.

PiperOrigin-RevId: 752952529
This commit is contained in:
Francis Tsui
2025-04-29 17:59:57 -07:00
committed by Copybara-Service
parent 011993b4d3
commit 467e64c812
5 changed files with 67 additions and 77 deletions
+1 -1
View File
@@ -66,6 +66,7 @@ cc_library(
"//connections:partners",
],
deps = [
":mac_address",
"//proto:connections_enums_cc_proto",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
@@ -351,7 +352,6 @@ cc_library(
name = "mac_address",
srcs = ["mac_address.cc"],
hdrs = ["mac_address.h"],
compatible_with = ["//buildenv/target:non_prod"],
visibility = [
"//:__subpackages__",
],
+46 -55
View File
@@ -14,88 +14,79 @@
#include "internal/platform/bluetooth_utils.h"
#include <algorithm>
#include <cstdint>
#include <string>
#include "absl/strings/ascii.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/mac_address.h"
namespace nearby {
namespace {
uint64_t ByteArrayToUint64(const ByteArray& byte_array) {
if (byte_array.size() != BluetoothUtils::kBluetoothMacAddressLength) {
return 0;
}
uint64_t result = 0;
for (int i = 0; i < byte_array.size(); i++) {
result <<= 8;
result |= ((static_cast<uint64_t>(byte_array.data()[i])) & 0xFF);
}
return result;
}
ByteArray Uint64AddressToByteArray(uint64_t address) {
ByteArray address_bytes(BluetoothUtils::kBluetoothMacAddressLength);
address_bytes.data()[0] = (address >> 40) & 0xFF;
address_bytes.data()[1] = (address >> 32) & 0xFF;
address_bytes.data()[2] = (address >> 24) & 0xFF;
address_bytes.data()[3] = (address >> 16) & 0xFF;
address_bytes.data()[4] = (address >> 8) & 0xFF;
address_bytes.data()[5] = address & 0xFF;
return address_bytes;
}
} // namespace
std::string BluetoothUtils::ToString(const ByteArray& bluetooth_mac_address) {
std::string colon_delimited_string;
if (bluetooth_mac_address.size() != kBluetoothMacAddressLength)
return colon_delimited_string;
if (IsBluetoothMacAddressUnset(bluetooth_mac_address))
return colon_delimited_string;
for (auto byte : std::string(bluetooth_mac_address)) {
if (!colon_delimited_string.empty())
absl::StrAppend(&colon_delimited_string, ":");
absl::StrAppend(&colon_delimited_string, absl::StrFormat("%02X", byte));
MacAddress mac_address;
if (!MacAddress::FromUint64(ByteArrayToUint64(bluetooth_mac_address),
mac_address) ||
!mac_address.IsSet()) {
return "";
}
return colon_delimited_string;
return mac_address.ToString();
}
ByteArray BluetoothUtils::FromString(absl::string_view bluetooth_mac_address) {
std::string bt_mac_address(bluetooth_mac_address);
bt_mac_address.erase(
std::remove(bt_mac_address.begin(), bt_mac_address.end(), ':'),
bt_mac_address.end());
if (bt_mac_address.length() != kBluetoothMacAddressLength * 2 ||
!std::all_of(bt_mac_address.begin(), bt_mac_address.end(),
absl::ascii_isxdigit)) {
MacAddress mac_address;
if (!MacAddress::FromString(bluetooth_mac_address, mac_address) ||
!mac_address.IsSet()) {
return ByteArray();
}
auto bt_mac_address_string(absl::HexStringToBytes(bt_mac_address));
auto bt_mac_address_bytes =
ByteArray(bt_mac_address_string.data(), bt_mac_address_string.size());
if (IsBluetoothMacAddressUnset(bt_mac_address_bytes)) {
return ByteArray();
}
return bt_mac_address_bytes;
return Uint64AddressToByteArray(mac_address.address());
}
bool BluetoothUtils::IsBluetoothMacAddressUnset(
const ByteArray& bluetooth_mac_address_bytes) {
for (int i = 0; i < bluetooth_mac_address_bytes.size(); i++) {
if (bluetooth_mac_address_bytes.data()[i] != 0) {
return false;
}
}
return true;
return ByteArrayToUint64(bluetooth_mac_address_bytes) == 0;
}
std::string BluetoothUtils::FromNumber(std::uint64_t address) {
// Gets byte `index` from `address`.
auto b = [&](int index) {
return (std::uint8_t)(address >> (56 - 8 * index));
};
return absl::StrFormat("%02X:%02X:%02X:%02X:%02X:%02X", b(2), b(3), b(4),
b(5), b(6), b(7));
MacAddress mac_address;
if (!MacAddress::FromUint64(address, mac_address) || !mac_address.IsSet()) {
return "";
}
return mac_address.ToString();
}
std::uint64_t BluetoothUtils::ToNumber(absl::string_view address) {
ByteArray binary = FromString(address);
if (binary.size() != kBluetoothMacAddressLength) {
MacAddress mac_address;
if (!MacAddress::FromString(address, mac_address)) {
return 0;
}
std::uint64_t result = 0;
for (char ch : binary.AsStringView()) {
result <<= 8;
result |= static_cast<uint8_t>(ch);
}
return result;
return mac_address.address();
}
} // namespace nearby
+6
View File
@@ -18,6 +18,7 @@
#include <cstdint>
#include <string>
#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
@@ -30,22 +31,27 @@ class BluetoothUtils {
// Converts a Bluetooth MAC address from byte array to String format. Returns
// empty if input byte array is not of correct format.
// e.g. {-84, 55, 67, -68, -87, 40} -> "AC:37:43:BC:A9:28".
ABSL_DEPRECATED("Use MacAddress class instead.")
static std::string ToString(const ByteArray& bluetooth_mac_address);
// Converts a Bluetooth MAC address from String format to byte array. Returns
// empty if input string is not of correct format.
// e.g. "AC:37:43:BC:A9:28" -> {-84, 55, 67, -68, -87, 40}.
ABSL_DEPRECATED("Use MacAddress class instead.")
static ByteArray FromString(absl::string_view bluetooth_mac_address);
// Converts a MAC address from binary to canonical format.
// Example: 0xF1F2F3F4F5F6 -> "F1:F2:F3:F4:F5:F6"
ABSL_DEPRECATED("Use MacAddress class instead.")
static std::string FromNumber(std::uint64_t address);
// Converts a MAC address from canonical format to binary
// Example: "F1:F2:F3:F4:F5:F6" ->0xF1F2F3F4F5F6
ABSL_DEPRECATED("Use MacAddress class instead.")
static std::uint64_t ToNumber(absl::string_view address);
// Checks if a Bluetooth MAC address is zero for every byte.
ABSL_DEPRECATED("Use MacAddress class instead.")
static bool IsBluetoothMacAddressUnset(
const ByteArray& bluetooth_mac_address);
};
@@ -18,26 +18,20 @@
#include <winsock2.h>
// Standard C/C++ headers
#include <codecvt>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <locale>
#include <stdexcept>
#include <string>
#include <vector>
// Third party headers
#include "absl/strings/ascii.h"
#include "absl/strings/str_format.h"
// Nearby connections headers
#include "absl/strings/string_view.h"
#include "internal/platform/bluetooth_utils.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/crypto.h"
#include "internal/platform/implementation/windows/string_utils.h"
#include "internal/platform/logging.h"
#include "internal/platform/mac_address.h"
#include "internal/platform/uuid.h"
#include "winrt/Windows.Foundation.Collections.h"
#include "winrt/Windows.Networking.Connectivity.h"
@@ -55,24 +49,19 @@ using ::winrt::Windows::Networking::Connectivity::NetworkTypes;
} // namespace
std::string uint64_to_mac_address_string(uint64_t bluetoothAddress) {
std::string buffer = absl::StrFormat(
"%02llx:%02llx:%02llx:%02llx:%02llx:%02llx", bluetoothAddress >> 40,
(bluetoothAddress >> 32) & 0xff, (bluetoothAddress >> 24) & 0xff,
(bluetoothAddress >> 16) & 0xff, (bluetoothAddress >> 8) & 0xff,
bluetoothAddress & 0xff);
return absl::AsciiStrToUpper(buffer);
MacAddress mac_address;
if (!MacAddress::FromUint64(bluetoothAddress, mac_address)) {
return "";
}
return mac_address.ToString();
}
uint64_t mac_address_string_to_uint64(absl::string_view mac_address) {
ByteArray mac_address_array = BluetoothUtils::FromString(mac_address);
uint64_t mac_address_uint64 = 0;
for (int i = 0; i < mac_address_array.size(); i++) {
mac_address_uint64 <<= 8;
mac_address_uint64 |= static_cast<uint8_t>(
static_cast<unsigned char>(*(mac_address_array.data() + i)));
MacAddress address;
if (!MacAddress::FromString(mac_address, address)) {
return 0;
}
return mac_address_uint64;
return address.address();
}
std::string ipaddr_4bytes_to_dotdecimal_string(
@@ -22,6 +22,7 @@
#include <string>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/uuid.h"
@@ -33,7 +34,10 @@ namespace windows {
using winrt::Windows::Foundation::IInspectable;
ABSL_DEPRECATED("Use MacAddress class instead.")
std::string uint64_to_mac_address_string(uint64_t bluetoothAddress);
ABSL_DEPRECATED("Use MacAddress class instead.")
uint64_t mac_address_string_to_uint64(absl::string_view mac_address);
std::string ipaddr_4bytes_to_dotdecimal_string(absl::string_view ipaddr_4bytes);