/usr/include/tins/hw_address.h is in libtins-dev 3.4-2+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | /*
* Copyright (c) 2016, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef TINS_HWADDRESS_H
#define TINS_HWADDRESS_H
#include <stdint.h>
#include <stdexcept>
#include <iterator>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include "cxxstd.h"
namespace Tins {
/**
* \class HWAddress
* \brief Represents a hardware address.
*
* This class represents a hardware (MAC) address. It can
* be constructed from it's string representation and you can
* iterate over the bytes that compose it.
*
* For example:
*
* \code
* // Construct it from a string.
* HWAddress<6> address("00:01:fa:9e:1a:cd");
*
* // Iterate over its bytes.
* for(auto element : address) {
* // element will be each of the bytes(\x00, \x01, \xfa, etc)
* }
* \endcode
*/
template<size_t n, typename Storage = uint8_t>
class HWAddress {
public:
/**
* \brief The type of the elements stored in the hardware address.
*
* This is the same as the template parameter Storage.
*/
typedef Storage storage_type;
/**
* \brief The random access iterator type.
*/
typedef storage_type* iterator;
/**
* \brief Const iterator type.
*/
typedef const storage_type* const_iterator;
/**
* \brief Non-member constant indicating the amount of storage_type
* elements in this address.
*/
static const size_t address_size = n;
/**
* \brief The broadcast address.
*/
static const HWAddress<n, Storage> broadcast;
/**
* \brief Constructor from a const storage_type*.
*
* If no pointer or a null pointer is provided, the address is
* initialized to 00:00:00:00:00:00.
*
* This constructor is very usefull when passing zero initialized
* addresses as arguments to other functions. You can use a
* literal 0, which will be implicitly converted to the empty address.
*
* If a pointer is provided, address_size storage_type elements
* are copied from the pointer, into the internal address representation.
*
* \param ptr The pointer from which to construct this address.
*/
HWAddress(const storage_type* ptr = 0) {
if (ptr) {
std::copy(ptr, ptr + address_size, buffer_);
}
else {
std::fill(begin(), end(), storage_type());
}
}
/**
* \brief Constructs an address from a hex-notation address.
*
* This constructor will parse strings in the form:
*
* "00:01:da:fa:..."
*
* And initialize the internal representation accordingly.
*
* \param address The hex-notation address to be parsed.
*/
HWAddress(const std::string& address) {
convert(address, buffer_);
}
/**
* \brief Overload provided basically for string literals.
*
* This constructor takes a const char array of i elements in
* hex-notation. \sa HWAddress::HWAddress(const std::string& address)
*
* This is mostly used when providing string literals. If this where
* a const char*, then there would be an ambiguity when providing
* a null pointer.
*
* \param address The array of chars containing the hex-notation
* cstring to be parsed.
*/
template<size_t i>
HWAddress(const char (&address)[i]) {
convert(address, buffer_);
}
/**
* \brief Copy construct from a HWAddress of length i.
*
* If i is lower or equal than address_size, then i storage_type
* elements are copied, and the last (n - i) are initialized to
* the default storage_type value(0 most of the times).
*
* If i is larger than address_size, then only the first address_size
* elements are copied.
*
* \param rhs The HWAddress to be constructed from.
*/
template<size_t i>
HWAddress(const HWAddress<i>& rhs) {
// Fill extra bytes
std::fill(
// Copy as most as we can
std::copy(
rhs.begin(),
rhs.begin() + std::min(i, n),
begin()
),
end(),
0
);
}
/**
* \brief Retrieves an iterator pointing to the begining of the
* address.
*
* \return iterator.
*/
iterator begin() {
return buffer_;
}
/**
* \brief Retrieves a const iterator pointing to the begining of
* the address.
*
* \return const_iterator.
*/
const_iterator begin() const {
return buffer_;
}
/**
* \brief Retrieves an iterator pointing one-past-the-end of the
* address.
*
* \return iterator.
*/
iterator end() {
return buffer_ + address_size;
}
/**
* \brief Retrieves a const iterator pointing one-past-the-end of
* the address.
*
* \return const_iterator.
*/
const_iterator end() const {
return buffer_ + address_size;
}
/**
* \brief Compares this HWAddress for equality.
*
* \param rhs The HWAddress to be compared to.
*
* \return bool indicating whether addresses are equal.
*/
bool operator==(const HWAddress& rhs) const {
return std::equal(begin(), end(), rhs.begin());
}
/**
* \brief Compares this HWAddress for in-equality.
*
* \param rhs The HWAddress to be compared to.
*
* \return bool indicating whether addresses are distinct.
*/
bool operator!=(const HWAddress& rhs) const {
return !(*this == rhs);
}
/**
* \brief Compares this HWAddress for less-than inequality.
*
* \param rhs The HWAddress to be compared to.
*
* \return bool indicating whether this address is less-than rhs.
*/
bool operator<(const HWAddress& rhs) const {
return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
}
/**
* \brief Retrieves the size of this address.
*
* This effectively returns the address_size constant.
*/
const size_t size() const {
return address_size;
}
/**
* \brief Indicates whether this is a broadcast address.
*/
bool is_broadcast() const {
return* this == broadcast;
}
/**
* \brief Indicates whether this is a multicast address.
*/
bool is_multicast() const {
return (*begin() & 0x01);
}
/**
* \brief Indicates whether this is an unicast address.
*/
bool is_unicast() const {
return !is_broadcast() && !is_multicast();
}
/**
* \brief Convert this address to a hex-notation std::string address.
*
* \return std::string containing the hex-notation address.
*/
std::string to_string() const {
std::ostringstream oss;
oss <<* this;
return oss.str();
}
/**
* \brief Retrieves the i-th storage_type in this address.
*
* \param i The element to retrieve.
*/
storage_type operator[](size_t i) const {
return begin()[i];
}
/**
* \brief Writes this HWAddress in hex-notation to a std::ostream.
*
* \param os The stream in which to write the address.
* \param addr The parameter to be written.
* \return std::ostream& pointing to the os parameter.
*/
friend std::ostream& operator<<(std::ostream& os, const HWAddress& addr) {
std::transform(
addr.begin(),
addr.end() - 1,
std::ostream_iterator<std::string>(os, ":"),
&HWAddress::storage_to_string
);
return os << storage_to_string(addr.begin()[HWAddress::address_size - 1]);
}
/**
* \brief Helper function which copies the address into an output
* iterator.
*
* This is the same as:
*
* std::copy(begin(), end(), iter);
*
* But since some PDUs return a HWAddress<> by value, this function
* can be used to avoid temporaries.
*
* \param output The output iterator in which to store this address.
* \return OutputIterator pointing to one-past the last position
* written.
*/
template<typename OutputIterator>
OutputIterator copy(OutputIterator output) const {
for (const_iterator iter = begin(); iter != end(); ++iter) {
*output++ = *iter;
}
return output;
}
private:
template<typename OutputIterator>
static void convert(const std::string& hw_addr, OutputIterator output);
static HWAddress<n> make_broadcast_address() {
// Build a buffer made of n 0xff bytes
uint8_t buffer[n];
for (size_t i = 0; i < n; ++i) {
buffer[i] = 0xff;
}
return HWAddress<n>(buffer);
}
static std::string storage_to_string(storage_type element) {
std::ostringstream oss;
oss << std::hex;
if (element < 0x10) {
oss << '0';
}
oss << (unsigned)element;
return oss.str();
}
storage_type buffer_[n];
};
template<size_t n, typename Storage>
template<typename OutputIterator>
void HWAddress<n, Storage>::convert(const std::string& hw_addr,
OutputIterator output) {
unsigned i(0);
size_t count(0);
storage_type tmp;
while (i < hw_addr.size() && count < n) {
const unsigned end = i+2;
tmp = storage_type();
while (i < end) {
if (hw_addr[i] >= 'a' && hw_addr[i] <= 'f') {
tmp = (tmp << 4) | (hw_addr[i] - 'a' + 10);
}
else if (hw_addr[i] >= 'A' && hw_addr[i] <= 'F') {
tmp = (tmp << 4) | (hw_addr[i] - 'A' + 10);
}
else if (hw_addr[i] >= '0' && hw_addr[i] <= '9') {
tmp = (tmp << 4) | (hw_addr[i] - '0');
}
else if (hw_addr[i] == ':') {
break;
}
else {
throw std::runtime_error("Invalid byte found");
}
i++;
}
*(output++) = tmp;
count++;
if (i < hw_addr.size()) {
if (hw_addr[i] == ':') {
i++;
}
else {
throw std::runtime_error("Invalid separator");
}
}
}
while (count++ < n) {
*(output++) = storage_type();
}
}
template<size_t n, typename Storage>
const HWAddress<n, Storage> HWAddress<n, Storage>::broadcast = make_broadcast_address();
} // namespace Tins
#if TINS_IS_CXX11
namespace std {
// Specialization of std::hash for HWAddress
template<size_t n>
struct hash<Tins::HWAddress<n>> {
size_t operator()(const Tins::HWAddress<n>& addr) const {
return std::hash<std::string>()(addr.to_string());
}
};
} // namespace std
#endif // TINS_IS_CXX11
#endif // TINS_HWADDRESS_H
|