mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 05:24:26 +01:00
HWAddress class is working. EthernetII now uses this classes instead of raw uint8_t*
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
|
||||
#include "pdu.h"
|
||||
#include "utils.h"
|
||||
#include "hwaddress.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
@@ -35,6 +36,8 @@ namespace Tins {
|
||||
*/
|
||||
class EthernetII : public PDU {
|
||||
public:
|
||||
typedef HWAddress<6> address_type;
|
||||
|
||||
/**
|
||||
* \brief This PDU's flag.
|
||||
*/
|
||||
@@ -61,7 +64,11 @@ namespace Tins {
|
||||
* \param src_hw_addr uint8_t array of 6 bytes containing the source's MAC(optional).
|
||||
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
|
||||
*/
|
||||
EthernetII(const std::string& iface, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0, PDU* child = 0) throw (std::runtime_error);
|
||||
//EthernetII(const std::string& iface, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0, PDU* child = 0);
|
||||
EthernetII(const std::string& iface,
|
||||
const address_type &dst_hw_addr = address_type(),
|
||||
const address_type &src_hw_addr = address_type(),
|
||||
PDU* child = 0);
|
||||
|
||||
/**
|
||||
* \brief Constructor for creating an ethernet PDU
|
||||
@@ -74,7 +81,10 @@ namespace Tins {
|
||||
* \param src_hw_addr uint8_t array of 6 bytes containing the source's MAC(optional).
|
||||
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
|
||||
*/
|
||||
EthernetII(uint32_t iface_index, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0, PDU* child = 0);
|
||||
EthernetII(uint32_t iface_index,
|
||||
const address_type &dst_hw_addr = address_type(),
|
||||
const address_type &src_hw_addr = address_type(),
|
||||
PDU* child = 0);
|
||||
|
||||
/**
|
||||
* \brief Constructor which creates an EthernetII object from a buffer and adds all identifiable
|
||||
@@ -90,14 +100,14 @@ namespace Tins {
|
||||
*
|
||||
* \return Returns the destination's mac address as a constant uint8_t pointer.
|
||||
*/
|
||||
const uint8_t* dst_addr() const { return _eth.dst_mac; }
|
||||
address_type dst_addr() const { return _eth.dst_mac; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the source's mac address.
|
||||
*
|
||||
* \return Returns the source's mac address as a constant uint8_t pointer.
|
||||
*/
|
||||
const uint8_t* src_addr() const { return _eth.src_mac; }
|
||||
address_type src_addr() const { return _eth.src_mac; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the interface.
|
||||
@@ -119,14 +129,14 @@ namespace Tins {
|
||||
*
|
||||
* \param new_dst_mac uint8_t array of 6 bytes containing the new destination's MAC.
|
||||
*/
|
||||
void dst_addr(const uint8_t* new_dst_mac);
|
||||
void dst_addr(const address_type &new_dst_mac);
|
||||
|
||||
/**
|
||||
* \brief Setter for the source's MAC.
|
||||
*
|
||||
* \param new_src_mac uint8_t array of 6 bytes containing the new source's MAC.
|
||||
*/
|
||||
void src_addr(const uint8_t* new_src_mac);
|
||||
void src_addr(const address_type &new_src_mac);
|
||||
|
||||
/**
|
||||
* \brief Setter for the interface.
|
||||
|
||||
@@ -24,26 +24,37 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdexcept>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <tins/utils.h> //borrame
|
||||
#include <sstream>
|
||||
|
||||
namespace Tins {
|
||||
template<size_t n>
|
||||
template<size_t n, typename Storage = uint8_t>
|
||||
class HWAddress {
|
||||
public:
|
||||
typedef uint8_t element_type;
|
||||
typedef element_type* iterator;
|
||||
typedef const element_type* const_iterator;
|
||||
typedef Storage storage_type;
|
||||
typedef storage_type* iterator;
|
||||
typedef const storage_type* const_iterator;
|
||||
static const size_t address_size = n;
|
||||
|
||||
HWAddress() {
|
||||
std::fill(buffer, buffer + address_size, element_type());
|
||||
std::fill(buffer, buffer + address_size, storage_type());
|
||||
}
|
||||
|
||||
HWAddress(const storage_type* ptr) {
|
||||
std::copy(ptr, ptr + address_size, buffer);
|
||||
}
|
||||
|
||||
HWAddress(const std::string &address) {
|
||||
convert(address, buffer);
|
||||
}
|
||||
|
||||
HWAddress& operator=(const std::string &address) {
|
||||
convert(address, buffer);
|
||||
}
|
||||
|
||||
iterator begin() {
|
||||
return buffer;
|
||||
}
|
||||
@@ -60,6 +71,14 @@ public:
|
||||
return buffer + address_size;
|
||||
}
|
||||
|
||||
bool operator==(const HWAddress &rhs) const {
|
||||
return std::equal(begin(), end(), rhs.begin());
|
||||
}
|
||||
|
||||
bool operator!=(const HWAddress &rhs) const {
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const HWAddress &addr) {
|
||||
std::transform(
|
||||
addr.buffer,
|
||||
@@ -73,7 +92,7 @@ private:
|
||||
template<typename OutputIterator>
|
||||
static void convert(const std::string &hw_addr, OutputIterator output);
|
||||
|
||||
static std::string to_string(element_type element) {
|
||||
static std::string to_string(storage_type element) {
|
||||
std::ostringstream oss;
|
||||
oss << std::hex;
|
||||
if(element < 0x10)
|
||||
@@ -82,17 +101,19 @@ private:
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
element_type buffer[n];
|
||||
storage_type buffer[n];
|
||||
};
|
||||
|
||||
template<size_t n>
|
||||
template<size_t n, typename Storage>
|
||||
template<typename OutputIterator>
|
||||
void HWAddress<n>::convert(const std::string &hw_addr, OutputIterator output) {
|
||||
void HWAddress<n, Storage>::convert(const std::string &hw_addr,
|
||||
OutputIterator output)
|
||||
{
|
||||
unsigned i(0);
|
||||
element_type tmp;
|
||||
storage_type tmp;
|
||||
while(i < hw_addr.size()) {
|
||||
const unsigned end = i+2;
|
||||
tmp = element_type();
|
||||
tmp = storage_type();
|
||||
while(i < end) {
|
||||
if(hw_addr[i] >= 'a' && hw_addr[i] <= 'f')
|
||||
tmp = (tmp << 4) | (hw_addr[i] - 'a' + 10);
|
||||
|
||||
Reference in New Issue
Block a user