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