1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 04:54:28 +01:00

Added HWAddress class.

This commit is contained in:
Matias Fontanini
2012-08-07 12:05:05 -03:00
parent 4af3d3c697
commit b8f39f1291
2 changed files with 155 additions and 0 deletions

117
include/hwaddress.h Normal file
View File

@@ -0,0 +1,117 @@
/*
* libtins is a net packet wrapper library for crafting and
* interpreting sniffed packets.
*
* Copyright (C) 2011 Nasel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TINS_HWADDRESS_H
#define TINS_HWADDRESS_H
#include <stdint.h>
#include <stdexcept>
#include <iostream>
#include <tins/utils.h> //borrame
namespace Tins {
template<size_t n>
class HWAddress {
public:
typedef uint8_t element_type;
typedef element_type* iterator;
typedef const element_type* const_iterator;
static const size_t address_size = n;
HWAddress() {
std::fill(buffer, buffer + address_size, element_type());
}
HWAddress(const std::string &address) {
convert(address, buffer);
}
iterator begin() {
return buffer;
}
const_iterator begin() const {
return buffer;
}
iterator end() {
return buffer + address_size;
}
const_iterator end() const {
return buffer + address_size;
}
friend std::ostream &operator<<(std::ostream &os, const HWAddress &addr) {
std::transform(
addr.buffer,
addr.buffer + HWAddress::address_size - 1,
std::ostream_iterator<std::string>(os, ":"),
&HWAddress::to_string
);
return os << to_string(addr.buffer[HWAddress::address_size-1]);
}
private:
template<typename OutputIterator>
static void convert(const std::string &hw_addr, OutputIterator output);
static std::string to_string(element_type element) {
std::ostringstream oss;
oss << std::hex;
if(element < 0x10)
oss << '0';
oss << (unsigned)element;
return oss.str();
}
element_type buffer[n];
};
template<size_t n>
template<typename OutputIterator>
void HWAddress<n>::convert(const std::string &hw_addr, OutputIterator output) {
unsigned i(0);
element_type tmp;
while(i < hw_addr.size()) {
const unsigned end = i+2;
tmp = element_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] >= '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;
if(i < hw_addr.size()) {
if(hw_addr[i] == ':')
i++;
else
throw std::runtime_error("Invalid separator");
}
}
}
}
#endif // TINS_HWADDRESS_H

38
tests/src/hwaddress.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <string>
#include <sstream>
#include <stdint.h>
#include "hwaddress.h"
using namespace Tins;
class HWAddressTest : public testing::Test {
public:
static const std::string address;
static const uint8_t *byte_address, *empty_addr;
};
const std::string HWAddressTest::address = "00:de:ad:be:ef:00";
const uint8_t *HWAddressTest::byte_address = (const uint8_t*)"\x00\xde\xad\xbe\xef\x00",
*HWAddressTest::empty_addr = (const uint8_t*)"\x00\x00\x00\x00\x00\x00";
TEST_F(HWAddressTest, DefaultConstructor) {
HWAddress<6> addr;
EXPECT_TRUE(std::equal(addr.begin(), addr.end(), empty_addr));
}
TEST_F(HWAddressTest, ConstructorFromAddress) {
HWAddress<6> addr(address);
EXPECT_TRUE(std::equal(addr.begin(), addr.end(), byte_address));
}
TEST_F(HWAddressTest, OutputOperator) {
HWAddress<6> addr(address);
std::ostringstream oss;
oss << addr;
EXPECT_EQ(oss.str(), address);
}