1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 20:44:26 +01:00

EthernetII, IEEE802_3 and Dot11(and subclasses) now use NetworkInterface and HWAddress.

This commit is contained in:
Matias Fontanini
2012-08-09 12:00:29 -03:00
parent 29c5a696e5
commit 91af0f9cc2
17 changed files with 278 additions and 600 deletions

View File

@@ -29,6 +29,7 @@
#include "ipaddress.h"
#include "utils.h"
#include "hwaddress.h"
#include "network_interface.h"
namespace Tins {
@@ -59,7 +60,8 @@ namespace Tins {
* ARP requests and replies can be constructed easily using
* ARP::make_arp_request/reply static functions.
*/
ARP(IPv4Address target_ip = 0, IPv4Address sender_ip = 0,
ARP(IPv4Address target_ip = IPv4Address(),
IPv4Address sender_ip = IPv4Address(),
const hwaddress_type &target_hw = hwaddress_type(),
const hwaddress_type &sender_hw = hwaddress_type());
@@ -223,7 +225,7 @@ namespace Tins {
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Request.
*/
static PDU* make_arp_request(const std::string& iface, IPv4Address target,
static PDU* make_arp_request(const NetworkInterface& iface, IPv4Address target,
IPv4Address sender, const hwaddress_type &hw_snd = hwaddress_type());
/**
@@ -239,7 +241,7 @@ namespace Tins {
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Replay.
*/
static PDU* make_arp_reply(const std::string& iface, IPv4Address target,
static PDU* make_arp_reply(const NetworkInterface& iface, IPv4Address target,
IPv4Address sender, const hwaddress_type &hw_tgt = hwaddress_type(),
const hwaddress_type &hw_snd = hwaddress_type());

File diff suppressed because it is too large Load Diff

View File

@@ -28,6 +28,7 @@
#include "pdu.h"
#include "utils.h"
#include "hwaddress.h"
#include "network_interface.h"
namespace Tins {
@@ -64,24 +65,7 @@ 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);
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
*
* Constructor that builds an ethernet PDU taking the interface index,
* destination's and source's MAC.
*
* \param iface_index const uint32_t with the interface's index from where to send the packet.
* \param dst_hw_addr uint8_t array of 6 bytes containing the destination's MAC(optional).
* \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,
EthernetII(const NetworkInterface& iface = NetworkInterface(),
const address_type &dst_hw_addr = address_type(),
const address_type &src_hw_addr = address_type(),
PDU* child = 0);
@@ -114,7 +98,7 @@ namespace Tins {
*
* \return Returns the interface's index as an uint32_t.
*/
uint32_t iface() const { return _iface_index; }
const NetworkInterface &iface() const { return _iface; }
/**
* \brief Getter for the payload_type
@@ -138,19 +122,12 @@ namespace Tins {
*/
void src_addr(const address_type &new_src_mac);
/**
* \brief Setter for the interface.
*
* \param new_iface_index uint32_t containing the new interface index.
*/
void iface(uint32_t new_iface_index);
/**
* \brief Setter for the interface.
*
* \param new_iface string reference containing the new interface name.
*/
void iface(const std::string& new_iface) throw (std::runtime_error);
void iface(const NetworkInterface& new_iface);
/**
* \brief Setter for the payload type.
@@ -223,7 +200,7 @@ namespace Tins {
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
ethhdr _eth;
uint32_t _iface_index;
NetworkInterface _iface;
};
};

View File

@@ -40,7 +40,7 @@ public:
static const size_t address_size = n;
HWAddress() {
std::fill(buffer, buffer + address_size, storage_type());
std::fill(begin(), end(), storage_type());
}
HWAddress(const storage_type* ptr) {
@@ -51,6 +51,10 @@ public:
convert(address, buffer);
}
HWAddress(const char *address) {
convert(address, buffer);
}
HWAddress& operator=(const std::string &address) {
convert(address, buffer);
}

View File

@@ -27,6 +27,8 @@
#include "pdu.h"
#include "utils.h"
#include "hwaddress.h"
#include "network_interface.h"
namespace Tins {
@@ -35,6 +37,11 @@ namespace Tins {
*/
class IEEE802_3 : public PDU {
public:
/**
* \brief The address type.
*/
typedef HWAddress<6> address_type;
/**
* \brief This PDU's flag.
*/
@@ -45,11 +52,6 @@ namespace Tins {
*/
static const uint8_t* BROADCAST;
/**
* \brief IEEE802_3 hardware address size.
*/
static const unsigned ADDR_SIZE = 6;
/**
* \brief Constructor for creating an IEEE802_3 PDU
*
@@ -61,20 +63,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).
*/
IEEE802_3(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);
/**
* \brief Constructor for creating an IEEE802_3 PDU
*
* Constructor that builds an IEEE802_3 PDU taking the interface index,
* destination's and source's MAC.
*
* \param iface_index const uint32_t with the interface's index from where to send the packet.
* \param dst_hw_addr uint8_t array of 6 bytes containing the destination's MAC(optional).
* \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).
*/
IEEE802_3(uint32_t iface_index, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0, PDU* child = 0);
IEEE802_3(const NetworkInterface& iface,
const address_type &dst_hw_addr = address_type(),
const address_type &src_hw_addr = address_type(),
PDU* child = 0);
/**
* \brief Constructor which creates an IEEE802_3 object from a buffer and adds all identifiable
@@ -90,27 +82,27 @@ namespace Tins {
*
* \return Returns the destination's mac address as a constant uint8_t pointer.
*/
inline 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.
*/
inline const uint8_t* src_addr() const { return _eth.src_mac; }
address_type src_addr() const { return _eth.src_mac; }
/**
* \brief Getter for the interface.
*
* \return Returns the interface's index as an uint32_t.
*/
inline uint32_t iface() const { return this->_iface_index; }
const NetworkInterface &iface() const { return this->_iface; }
/**
* \brief Getter for the length field.
* \return The length field value.
*/
inline uint16_t length() const { return Utils::net_to_host_s(_eth.length); };
uint16_t length() const { return Utils::net_to_host_s(_eth.length); };
/* Setters */
@@ -119,21 +111,21 @@ 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.
*
* \param new_iface_index uint32_t containing the new interface index.
*/
void iface(uint32_t new_iface_index);
void iface(const NetworkInterface &new_iface_index);
/**
* \brief Setter for the interface.
@@ -195,27 +187,26 @@ namespace Tins {
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
/**
* \brief Clones this PDU.
*
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const;
PDU *clone_pdu() const {
return do_clone_pdu<IEEE802_3>();
}
private:
/**
* Struct that represents the Ethernet II header
*/
struct ethhdr {
uint8_t dst_mac[ADDR_SIZE];
uint8_t src_mac[ADDR_SIZE];
uint8_t dst_mac[address_type::address_size];
uint8_t src_mac[address_type::address_size];
uint16_t length;
} __attribute__((__packed__));
void copy_fields(const IEEE802_3 *other);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
ethhdr _eth;
uint32_t _iface_index;
NetworkInterface _iface;
};
};

View File

@@ -139,7 +139,7 @@ namespace Tins {
* \param ip_src The source ip address(optional).
* \param child pointer to a PDU which will be set as the inner_pdu for the packet being constructed(optional).
*/
IP(IPv4Address ip_dst, IPv4Address ip_src = 0, PDU *child = 0);
IP(IPv4Address ip_dst, IPv4Address ip_src = IPv4Address(), PDU *child = 0);
/**
* \brief Constructor which creates an IP object from a buffer and adds all identifiable

View File

@@ -31,6 +31,7 @@ namespace Tins {
public:
IPv4Address(uint32_t ip = 0);
IPv4Address(const std::string &ip);
IPv4Address(const char *ip);
IPv4Address &operator=(uint32_t ip);
IPv4Address &operator=(const std::string &ip);

View File

@@ -52,6 +52,11 @@ public:
address_type hw_addr;
};
/**
* \brief Default constructor.
*/
NetworkInterface();
/**
* \brief Constructor from std::string.
*
@@ -59,6 +64,13 @@ public:
*/
NetworkInterface(const std::string &name);
/**
* \brief Constructor to allow implicit conversions from const char*.
*
* \param name The name of the interface this object will abstract.
*/
NetworkInterface(const char *name);
/**
* \brief Constructs a NetworkInterface from an ip address.
*
@@ -94,6 +106,16 @@ public:
*/
Info addresses() const;
/**
* \brief Tests whether this is a valid interface;
*
* An interface will not be valid iff it was created using the
* default constructor.
*/
operator bool() const {
return iface_id != 0;
}
/**
* \brief Compares this interface for equality.
*
@@ -112,6 +134,8 @@ public:
return !(*this == rhs);
}
private:
id_type resolve_index(const char *name);
id_type iface_id;
};
}

View File

@@ -24,6 +24,7 @@
#include <stdexcept>
#include "pdu.h"
#include "network_interface.h"
namespace Tins {
@@ -94,20 +95,13 @@ namespace Tins {
PADDING = 32,
FAILED_FCS = 64
};
/**
* \brief Creates an instance of RadioTap.
* \param iface The name of the interface in which to send this PDU.
* \param child The child PDU.(optional)
*/
RadioTap(const std::string &iface, PDU *child = 0) throw (std::runtime_error);
/**
* \brief Creates an instance of RadioTap.
* \param iface_index The index of the interface in which to send this PDU.
* \param iface The interface in which to send this PDU.
* \param child The child PDU.(optional)
*/
RadioTap(uint32_t iface_index, PDU *child = 0);
RadioTap(const NetworkInterface &iface, PDU *child = 0);
/**
* \brief Constructor which creates a RadioTap object from a buffer and adds all
@@ -314,7 +308,8 @@ namespace Tins {
radiotap_hdr _radio;
uint32_t _iface_index, _options_size;
NetworkInterface _iface;
uint32_t _options_size;
// present fields...
uint64_t _tsft;
uint8_t _flags, _rate;