• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2017 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 "raw_address.h"
20 
21 #include <base/strings/string_split.h>
22 #include <base/strings/stringprintf.h>
23 #include <stdint.h>
24 #include <algorithm>
25 #include <array>
26 #include <vector>
27 
28 static_assert(sizeof(RawAddress) == 6, "RawAddress must be 6 bytes long!");
29 
30 const RawAddress RawAddress::kAny{{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
31 const RawAddress RawAddress::kEmpty{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
32 
RawAddress(const uint8_t (& addr)[6])33 RawAddress::RawAddress(const uint8_t (&addr)[6]) {
34   std::copy(addr, addr + kLength, address);
35 }
36 
RawAddress(const std::array<uint8_t,kLength> mac)37 RawAddress::RawAddress(const std::array<uint8_t, kLength> mac) {
38   std::copy(mac.begin(), mac.end(), address);
39 }
40 
ToString() const41 std::string RawAddress::ToString() const { return ToColonSepHexString(); }
42 
ToColonSepHexString() const43 std::string RawAddress::ToColonSepHexString() const {
44   return base::StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", address[0],
45                             address[1], address[2], address[3], address[4],
46                             address[5]);
47 }
48 
ToStringForLogging() const49 std::string RawAddress::ToStringForLogging() const {
50   return ToColonSepHexString();
51 }
52 
ToRedactedStringForLogging() const53 std::string RawAddress::ToRedactedStringForLogging() const {
54   if (*this == RawAddress::kAny || *this == RawAddress::kEmpty) {
55     return ToStringForLogging();
56   }
57   return base::StringPrintf("xx:xx:xx:xx:%02x:%02x", address[4], address[5]);
58 }
59 
ToArray() const60 std::array<uint8_t, RawAddress::kLength> RawAddress::ToArray() const {
61   std::array<uint8_t, kLength> mac;
62   std::copy(std::begin(address), std::end(address), std::begin(mac));
63   return mac;
64 }
65 
FromString(const std::string & from,RawAddress & to)66 bool RawAddress::FromString(const std::string& from, RawAddress& to) {
67   RawAddress new_addr;
68   if (from.length() != 17) return false;
69 
70   std::vector<std::string> byte_tokens =
71       base::SplitString(from, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
72 
73   if (byte_tokens.size() != 6) return false;
74 
75   for (int i = 0; i < 6; i++) {
76     const auto& token = byte_tokens[i];
77 
78     if (token.length() != 2) return false;
79 
80     char* temp = nullptr;
81     new_addr.address[i] = strtol(token.c_str(), &temp, 16);
82     if (*temp != '\0') return false;
83   }
84 
85   to = new_addr;
86   return true;
87 }
88 
FromOctets(const uint8_t * from)89 size_t RawAddress::FromOctets(const uint8_t* from) {
90   std::copy(from, from + kLength, address);
91   return kLength;
92 };
93 
IsValidAddress(const std::string & address)94 bool RawAddress::IsValidAddress(const std::string& address) {
95   RawAddress tmp;
96   return RawAddress::FromString(address, tmp);
97 }
98