• 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   return base::StringPrintf("xx:xx:xx:xx:%02x:%02x", address[4], address[5]);
55 }
56 
ToArray() const57 std::array<uint8_t, RawAddress::kLength> RawAddress::ToArray() const {
58   std::array<uint8_t, kLength> mac;
59   std::copy(std::begin(address), std::end(address), std::begin(mac));
60   return mac;
61 }
62 
FromString(const std::string & from,RawAddress & to)63 bool RawAddress::FromString(const std::string& from, RawAddress& to) {
64   RawAddress new_addr;
65   if (from.length() != 17) return false;
66 
67   std::vector<std::string> byte_tokens =
68       base::SplitString(from, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
69 
70   if (byte_tokens.size() != 6) return false;
71 
72   for (int i = 0; i < 6; i++) {
73     const auto& token = byte_tokens[i];
74 
75     if (token.length() != 2) return false;
76 
77     char* temp = nullptr;
78     new_addr.address[i] = strtol(token.c_str(), &temp, 16);
79     if (*temp != '\0') return false;
80   }
81 
82   to = new_addr;
83   return true;
84 }
85 
FromOctets(const uint8_t * from)86 size_t RawAddress::FromOctets(const uint8_t* from) {
87   std::copy(from, from + kLength, address);
88   return kLength;
89 };
90 
IsValidAddress(const std::string & address)91 bool RawAddress::IsValidAddress(const std::string& address) {
92   RawAddress tmp;
93   return RawAddress::FromString(address, tmp);
94 }
95