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 <stdint.h>
22 
23 #include <algorithm>
24 #include <array>
25 #include <iomanip>
26 #include <sstream>
27 #include <vector>
28 
29 static_assert(sizeof(RawAddress) == 6, "RawAddress must be 6 bytes long!");
30 
31 const RawAddress RawAddress::kAny{{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
32 const RawAddress RawAddress::kEmpty{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
33 
RawAddress(const uint8_t (& addr)[6])34 RawAddress::RawAddress(const uint8_t (&addr)[6]) {
35   std::copy(addr, addr + kLength, address);
36 }
37 
RawAddress(const std::array<uint8_t,kLength> mac)38 RawAddress::RawAddress(const std::array<uint8_t, kLength> mac) {
39   std::copy(mac.begin(), mac.end(), address);
40 }
41 
ToString() const42 std::string RawAddress::ToString() const { return ToColonSepHexString(); }
43 
ToColonSepHexString() const44 std::string RawAddress::ToColonSepHexString() const {
45   std::stringstream addr;
46   addr << std::hex << std::setfill('0');
47   for (size_t i = 0; i < 6; i++) {
48     addr << std::setw(2) << +address[i];
49     if (i != 5) {
50       addr << ":";
51     }
52   }
53   return addr.str();
54 }
55 
ToStringForLogging() const56 std::string RawAddress::ToStringForLogging() const {
57   return ToColonSepHexString();
58 }
59 
ToRedactedStringForLogging() const60 std::string RawAddress::ToRedactedStringForLogging() const {
61   if (*this == RawAddress::kAny || *this == RawAddress::kEmpty) {
62     return ToStringForLogging();
63   }
64   std::stringstream addr;
65   addr << std::hex << std::setfill('0');
66   addr << "xx:xx:xx:xx:";
67   addr << std::setw(2) << +address[4] << ":";
68   addr << std::setw(2) << +address[5];
69   return addr.str();
70 }
71 
ToArray() const72 std::array<uint8_t, RawAddress::kLength> RawAddress::ToArray() const {
73   std::array<uint8_t, kLength> mac;
74   std::copy(std::begin(address), std::end(address), std::begin(mac));
75   return mac;
76 }
77 
FromString(const std::string & from,RawAddress & to)78 bool RawAddress::FromString(const std::string& from, RawAddress& to) {
79   RawAddress new_addr;
80   if (from.length() != 17) return false;
81 
82   std::istringstream stream(from);
83   std::string token;
84   int index = 0;
85   while (getline(stream, token, ':')) {
86     if (index >= 6) {
87       return false;
88     }
89 
90     if (token.length() != 2) {
91       return false;
92     }
93 
94     char* temp = nullptr;
95     new_addr.address[index] = std::strtol(token.c_str(), &temp, 16);
96     if (temp == token.c_str()) {
97       // string token is empty or has wrong format
98       return false;
99     }
100     if (temp != (token.c_str() + token.size())) {
101       // cannot parse whole string
102       return false;
103     }
104 
105     index++;
106   }
107 
108   if (index != 6) {
109     return false;
110   }
111 
112   to = new_addr;
113   return true;
114 }
115 
FromOctets(const uint8_t * from)116 size_t RawAddress::FromOctets(const uint8_t* from) {
117   std::copy(from, from + kLength, address);
118   return kLength;
119 };
120 
IsValidAddress(const std::string & address)121 bool RawAddress::IsValidAddress(const std::string& address) {
122   RawAddress tmp;
123   return RawAddress::FromString(address, tmp);
124 }
125