mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
EthernetII, IEEE802_3 and Dot11(and subclasses) now use NetworkInterface and HWAddress.
This commit is contained in:
@@ -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());
|
||||
|
||||
|
||||
402
include/dot11.h
402
include/dot11.h
File diff suppressed because it is too large
Load Diff
@@ -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;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -128,7 +128,7 @@ PDU *ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
|
||||
return cloned;
|
||||
}
|
||||
|
||||
PDU* ARP::make_arp_request(const std::string& iface, IPv4Address target,
|
||||
PDU* ARP::make_arp_request(const NetworkInterface& iface, IPv4Address target,
|
||||
IPv4Address sender, const hwaddress_type &hw_snd)
|
||||
{
|
||||
/* Create ARP packet and set its attributes */
|
||||
@@ -142,7 +142,7 @@ PDU* ARP::make_arp_request(const std::string& iface, IPv4Address target,
|
||||
return new EthernetII(iface, EthernetII::BROADCAST, hw_snd, arp);
|
||||
}
|
||||
|
||||
PDU* ARP::make_arp_reply(const string& iface, IPv4Address target,
|
||||
PDU* ARP::make_arp_reply(const NetworkInterface& iface, IPv4Address target,
|
||||
IPv4Address sender, const hwaddress_type &hw_tgt,
|
||||
const hwaddress_type &hw_snd)
|
||||
{
|
||||
|
||||
157
src/dot11.cpp
157
src/dot11.cpp
@@ -49,7 +49,7 @@ Tins::Dot11::Dot11(const address_type &dst_hw_addr, PDU* child)
|
||||
addr1(dst_hw_addr);
|
||||
}
|
||||
|
||||
Tins::Dot11::Dot11(const std::string& iface,
|
||||
Tins::Dot11::Dot11(const NetworkInterface &iface,
|
||||
const address_type &dst_hw_addr, PDU* child)
|
||||
: PDU(ETHERTYPE_IP, child), _options_size(0)
|
||||
{
|
||||
@@ -58,16 +58,6 @@ Tins::Dot11::Dot11(const std::string& iface,
|
||||
this->iface(iface);
|
||||
}
|
||||
|
||||
|
||||
Tins::Dot11::Dot11(uint32_t iface_index,
|
||||
const address_type &dst_hw_addr, PDU* child)
|
||||
: PDU(ETHERTYPE_IP, child), _options_size(0)
|
||||
{
|
||||
memset(&_header, 0, sizeof(ieee80211_header));
|
||||
addr1(dst_hw_addr);
|
||||
this->iface(iface_index);
|
||||
}
|
||||
|
||||
Tins::Dot11::Dot11(const ieee80211_header *header_ptr)
|
||||
: PDU(ETHERTYPE_IP)
|
||||
{
|
||||
@@ -180,18 +170,11 @@ void Tins::Dot11::duration_id(uint16_t new_duration_id) {
|
||||
}
|
||||
|
||||
void Tins::Dot11::addr1(const address_type &new_addr1) {
|
||||
//memcpy(this->_header.addr1, new_addr1, 6);
|
||||
std::copy(new_addr1.begin(), new_addr1.end(), _header.addr1);
|
||||
}
|
||||
|
||||
void Tins::Dot11::iface(uint32_t new_iface_index) {
|
||||
this->_iface_index = new_iface_index;
|
||||
}
|
||||
|
||||
void Tins::Dot11::iface(const std::string& new_iface) {
|
||||
if (!Tins::Utils::interface_id(new_iface, this->_iface_index)) {
|
||||
throw std::runtime_error("Invalid interface name!");
|
||||
}
|
||||
void Tins::Dot11::iface(const NetworkInterface &new_iface) {
|
||||
this->_iface = new_iface;
|
||||
}
|
||||
|
||||
uint32_t Tins::Dot11::header_size() const {
|
||||
@@ -207,7 +190,7 @@ bool Tins::Dot11::send(PacketSender* sender) {
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = this->_iface_index;
|
||||
addr.sll_ifindex = this->_iface.id();
|
||||
memcpy(&(addr.sll_addr), this->_header.addr1, 6);
|
||||
|
||||
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
@@ -275,7 +258,7 @@ Tins::PDU *Tins::Dot11::from_bytes(const uint8_t *buffer, uint32_t total_sz) {
|
||||
|
||||
void Tins::Dot11::copy_80211_fields(const Dot11 *other) {
|
||||
std::memcpy(&_header, &other->_header, sizeof(_header));
|
||||
_iface_index = other->_iface_index;
|
||||
_iface = other->_iface;
|
||||
_options_size = other->_options_size;
|
||||
for(std::list<Dot11Option>::const_iterator it = other->_options.begin(); it != other->_options.end(); ++it)
|
||||
_options.push_back(Dot11Option(it->option, it->length, it->value));
|
||||
@@ -301,7 +284,7 @@ Tins::Dot11ManagementFrame::Dot11ManagementFrame(
|
||||
addr2(src_hw_addr);
|
||||
}
|
||||
|
||||
Tins::Dot11ManagementFrame::Dot11ManagementFrame(const std::string &iface,
|
||||
Tins::Dot11ManagementFrame::Dot11ManagementFrame(const NetworkInterface &iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11(iface, dst_hw_addr)
|
||||
{
|
||||
@@ -327,12 +310,10 @@ uint32_t Tins::Dot11ManagementFrame::header_size() const {
|
||||
}
|
||||
|
||||
void Tins::Dot11ManagementFrame::addr2(const address_type &new_addr2) {
|
||||
//memcpy(this->_ext_header.addr2, new_addr2, 6);
|
||||
std::copy(new_addr2.begin(), new_addr2.end(), _ext_header.addr2);
|
||||
}
|
||||
|
||||
void Tins::Dot11ManagementFrame::addr3(const address_type &new_addr3) {
|
||||
//memcpy(this->_ext_header.addr3, new_addr3, 6);
|
||||
std::copy(new_addr3.begin(), new_addr3.end(), _ext_header.addr3);
|
||||
}
|
||||
|
||||
@@ -628,7 +609,7 @@ Tins::Dot11Beacon::Dot11Beacon(const address_type &dst_hw_addr,
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Tins::Dot11Beacon::Dot11Beacon(const std::string& iface,
|
||||
Tins::Dot11Beacon::Dot11Beacon(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -840,7 +821,7 @@ Tins::Dot11Disassoc::Dot11Disassoc(const address_type &dst_hw_addr,
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Tins::Dot11Disassoc::Dot11Disassoc(const std::string& iface,
|
||||
Tins::Dot11Disassoc::Dot11Disassoc(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr){
|
||||
this->subtype(Dot11::DISASSOC);
|
||||
@@ -892,7 +873,7 @@ Tins::Dot11AssocRequest::Dot11AssocRequest(const address_type &dst_hw_addr,
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Tins::Dot11AssocRequest::Dot11AssocRequest(const std::string& iface,
|
||||
Tins::Dot11AssocRequest::Dot11AssocRequest(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -973,7 +954,7 @@ Tins::Dot11AssocResponse::Dot11AssocResponse(const address_type &dst_hw_addr,
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Tins::Dot11AssocResponse::Dot11AssocResponse(const std::string& iface,
|
||||
Tins::Dot11AssocResponse::Dot11AssocResponse(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -1044,7 +1025,7 @@ Tins::Dot11ReAssocRequest::Dot11ReAssocRequest(const address_type &dst_hw_addr,
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Tins::Dot11ReAssocRequest::Dot11ReAssocRequest(const std::string& iface,
|
||||
Tins::Dot11ReAssocRequest::Dot11ReAssocRequest(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -1131,7 +1112,7 @@ Tins::Dot11ReAssocResponse::Dot11ReAssocResponse(const address_type &dst_hw_addr
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Tins::Dot11ReAssocResponse::Dot11ReAssocResponse(const std::string& iface,
|
||||
Tins::Dot11ReAssocResponse::Dot11ReAssocResponse(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -1200,7 +1181,7 @@ Tins::Dot11ProbeRequest::Dot11ProbeRequest(const address_type &dst_hw_addr,
|
||||
this->subtype(Dot11::PROBE_REQ);
|
||||
}
|
||||
|
||||
Tins::Dot11ProbeRequest::Dot11ProbeRequest(const std::string& iface,
|
||||
Tins::Dot11ProbeRequest::Dot11ProbeRequest(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -1246,7 +1227,7 @@ Tins::Dot11ProbeResponse::Dot11ProbeResponse(const address_type &dst_hw_addr,
|
||||
memset(&this->_body, 0, sizeof(this->_body));
|
||||
}
|
||||
|
||||
Tins::Dot11ProbeResponse::Dot11ProbeResponse(const std::string& iface,
|
||||
Tins::Dot11ProbeResponse::Dot11ProbeResponse(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -1395,7 +1376,7 @@ Tins::Dot11Authentication::Dot11Authentication(const address_type &dst_hw_addr,
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Tins::Dot11Authentication::Dot11Authentication(const std::string& iface,
|
||||
Tins::Dot11Authentication::Dot11Authentication(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -1462,7 +1443,7 @@ Tins::Dot11Deauthentication::Dot11Deauthentication(const address_type &dst_hw_ad
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Tins::Dot11Deauthentication::Dot11Deauthentication(const std::string& iface,
|
||||
Tins::Dot11Deauthentication::Dot11Deauthentication(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr)
|
||||
: Dot11ManagementFrame(iface, dst_hw_addr, src_hw_addr)
|
||||
{
|
||||
@@ -1529,14 +1510,6 @@ Tins::Dot11Data::Dot11Data(const uint8_t *buffer, uint32_t total_sz)
|
||||
inner_pdu(new Tins::SNAP(buffer, total_sz));
|
||||
}
|
||||
|
||||
Tins::Dot11Data::Dot11Data(uint32_t iface_index, const address_type &dst_hw_addr,
|
||||
const address_type &src_hw_addr, PDU* child)
|
||||
: Dot11(iface_index, dst_hw_addr, child)
|
||||
{
|
||||
type(Dot11::DATA);
|
||||
addr2(src_hw_addr);
|
||||
}
|
||||
|
||||
Tins::Dot11Data::Dot11Data(const address_type &dst_hw_addr,
|
||||
const address_type &src_hw_addr, PDU* child)
|
||||
: Dot11(dst_hw_addr, child)
|
||||
@@ -1546,7 +1519,7 @@ Tins::Dot11Data::Dot11Data(const address_type &dst_hw_addr,
|
||||
}
|
||||
|
||||
|
||||
Tins::Dot11Data::Dot11Data(const std::string &iface,
|
||||
Tins::Dot11Data::Dot11Data(const NetworkInterface &iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
||||
PDU* child)
|
||||
: Dot11(iface, dst_hw_addr, child)
|
||||
@@ -1569,12 +1542,10 @@ uint32_t Tins::Dot11Data::header_size() const {
|
||||
}
|
||||
|
||||
void Tins::Dot11Data::addr2(const address_type &new_addr2) {
|
||||
//memcpy(this->_ext_header.addr2, new_addr2, 6);
|
||||
std::copy(new_addr2.begin(), new_addr2.end(), _ext_header.addr2);
|
||||
}
|
||||
|
||||
void Tins::Dot11Data::addr3(const address_type &new_addr3) {
|
||||
//memcpy(this->_ext_header.addr3, new_addr3, 6);
|
||||
std::copy(new_addr3.begin(), new_addr3.end(), _ext_header.addr3);
|
||||
}
|
||||
|
||||
@@ -1617,7 +1588,7 @@ Tins::Dot11QoSData::Dot11QoSData(const address_type &dst_hw_addr,
|
||||
|
||||
}
|
||||
|
||||
Tins::Dot11QoSData::Dot11QoSData(const std::string& iface,
|
||||
Tins::Dot11QoSData::Dot11QoSData(const NetworkInterface &iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
||||
PDU* child)
|
||||
: Dot11Data(iface, dst_hw_addr, src_hw_addr, child)
|
||||
@@ -1626,15 +1597,6 @@ Tins::Dot11QoSData::Dot11QoSData(const std::string& iface,
|
||||
this->_qos_control = 0;
|
||||
}
|
||||
|
||||
Tins::Dot11QoSData::Dot11QoSData(uint32_t iface_index,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
||||
PDU* child)
|
||||
: Dot11Data(iface_index, dst_hw_addr, src_hw_addr, child)
|
||||
{
|
||||
this->subtype(Dot11::QOS_DATA_DATA);
|
||||
this->_qos_control = 0;
|
||||
}
|
||||
|
||||
Tins::Dot11QoSData::Dot11QoSData(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11Data(buffer, std::min(data_frame_size(), total_sz)) {
|
||||
uint32_t sz = data_frame_size();
|
||||
@@ -1693,20 +1655,13 @@ Tins::Dot11Control::Dot11Control(const address_type &dst_addr, PDU* child)
|
||||
type(CONTROL);
|
||||
}
|
||||
|
||||
Tins::Dot11Control::Dot11Control(const std::string& iface,
|
||||
Tins::Dot11Control::Dot11Control(const NetworkInterface &iface,
|
||||
const address_type &dst_addr, PDU* child)
|
||||
: Dot11(iface, dst_addr, child)
|
||||
{
|
||||
type(CONTROL);
|
||||
}
|
||||
|
||||
Tins::Dot11Control::Dot11Control(uint32_t iface_index,
|
||||
const address_type &dst_addr, PDU* child)
|
||||
: Dot11(iface_index, dst_addr, child)
|
||||
{
|
||||
type(CONTROL);
|
||||
}
|
||||
|
||||
Tins::Dot11Control::Dot11Control(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11(buffer, total_sz) {
|
||||
|
||||
@@ -1720,20 +1675,13 @@ Tins::Dot11ControlTA::Dot11ControlTA(const address_type &dst_addr,
|
||||
target_addr(target_address);
|
||||
}
|
||||
|
||||
Tins::Dot11ControlTA::Dot11ControlTA(const std::string& iface,
|
||||
Tins::Dot11ControlTA::Dot11ControlTA(const NetworkInterface &iface,
|
||||
const address_type &dst_addr, const address_type &target_address, PDU* child)
|
||||
: Dot11Control(iface, dst_addr, child)
|
||||
{
|
||||
target_addr(target_address);
|
||||
}
|
||||
|
||||
Tins::Dot11ControlTA::Dot11ControlTA(uint32_t iface_index,
|
||||
const address_type &dst_addr, const address_type &target_address, PDU* child)
|
||||
: Dot11Control(iface_index, dst_addr, child)
|
||||
{
|
||||
target_addr(target_address);
|
||||
}
|
||||
|
||||
Tins::Dot11ControlTA::Dot11ControlTA(const uint8_t *buffer, uint32_t total_sz) : Dot11Control(buffer, total_sz) {
|
||||
buffer += sizeof(ieee80211_header);
|
||||
total_sz -= sizeof(ieee80211_header);
|
||||
@@ -1753,7 +1701,6 @@ uint32_t Tins::Dot11ControlTA::write_ext_header(uint8_t *buffer, uint32_t total_
|
||||
}
|
||||
|
||||
void Tins::Dot11ControlTA::target_addr(const address_type &addr) {
|
||||
//std::memcpy(_taddr, addr, sizeof(_taddr));
|
||||
std::copy(addr.begin(), addr.end(), _taddr);
|
||||
}
|
||||
|
||||
@@ -1765,20 +1712,13 @@ Tins::Dot11RTS::Dot11RTS(const address_type &dst_addr ,
|
||||
subtype(RTS);
|
||||
}
|
||||
|
||||
Tins::Dot11RTS::Dot11RTS(const std::string& iface, const address_type &dst_addr,
|
||||
Tins::Dot11RTS::Dot11RTS(const NetworkInterface &iface, const address_type &dst_addr,
|
||||
const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(RTS);
|
||||
}
|
||||
|
||||
Tins::Dot11RTS::Dot11RTS(uint32_t iface_index, const address_type &dst_addr,
|
||||
const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface_index, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(RTS);
|
||||
}
|
||||
|
||||
Tins::Dot11RTS::Dot11RTS(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11ControlTA(buffer, total_sz) {
|
||||
|
||||
@@ -1799,20 +1739,13 @@ Tins::Dot11PSPoll::Dot11PSPoll(const address_type &dst_addr,
|
||||
subtype(PS);
|
||||
}
|
||||
|
||||
Tins::Dot11PSPoll::Dot11PSPoll(const std::string& iface,
|
||||
Tins::Dot11PSPoll::Dot11PSPoll(const NetworkInterface &iface,
|
||||
const address_type &dst_addr, const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(PS);
|
||||
}
|
||||
|
||||
Tins::Dot11PSPoll::Dot11PSPoll(uint32_t iface_index, const address_type &dst_addr,
|
||||
const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface_index, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(PS);
|
||||
}
|
||||
|
||||
Tins::Dot11PSPoll::Dot11PSPoll(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11ControlTA(buffer, total_sz) {
|
||||
|
||||
@@ -1833,20 +1766,13 @@ Tins::Dot11CFEnd::Dot11CFEnd(const address_type &dst_addr,
|
||||
subtype(CF_END);
|
||||
}
|
||||
|
||||
Tins::Dot11CFEnd::Dot11CFEnd(const std::string& iface,
|
||||
Tins::Dot11CFEnd::Dot11CFEnd(const NetworkInterface &iface,
|
||||
const address_type &dst_addr, const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(CF_END);
|
||||
}
|
||||
|
||||
Tins::Dot11CFEnd::Dot11CFEnd(uint32_t iface_index, const address_type &dst_addr,
|
||||
const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface_index, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(CF_END);
|
||||
}
|
||||
|
||||
Tins::Dot11CFEnd::Dot11CFEnd(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11ControlTA(buffer, total_sz) {
|
||||
|
||||
@@ -1867,20 +1793,13 @@ Tins::Dot11EndCFAck::Dot11EndCFAck(const address_type &dst_addr,
|
||||
subtype(CF_END_ACK);
|
||||
}
|
||||
|
||||
Tins::Dot11EndCFAck::Dot11EndCFAck(const std::string& iface,
|
||||
Tins::Dot11EndCFAck::Dot11EndCFAck(const NetworkInterface &iface,
|
||||
const address_type &dst_addr, const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(CF_END_ACK);
|
||||
}
|
||||
|
||||
Tins::Dot11EndCFAck::Dot11EndCFAck(uint32_t iface_index,
|
||||
const address_type &dst_addr, const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface_index, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(CF_END_ACK);
|
||||
}
|
||||
|
||||
Tins::Dot11EndCFAck::Dot11EndCFAck(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11ControlTA(buffer, total_sz) {
|
||||
|
||||
@@ -1900,20 +1819,13 @@ Tins::Dot11Ack::Dot11Ack(const address_type &dst_addr, PDU* child)
|
||||
subtype(ACK);
|
||||
}
|
||||
|
||||
Tins::Dot11Ack::Dot11Ack(const std::string& iface,
|
||||
Tins::Dot11Ack::Dot11Ack(const NetworkInterface &iface,
|
||||
const address_type &dst_addr, PDU* child)
|
||||
: Dot11Control(iface, dst_addr, child)
|
||||
{
|
||||
subtype(ACK);
|
||||
}
|
||||
|
||||
Tins::Dot11Ack::Dot11Ack(uint32_t iface_index,
|
||||
const address_type &dst_addr, PDU* child)
|
||||
: Dot11Control(iface_index, dst_addr, child)
|
||||
{
|
||||
subtype(ACK);
|
||||
}
|
||||
|
||||
Tins::Dot11Ack::Dot11Ack(const uint8_t *buffer, uint32_t total_sz) : Dot11Control(buffer, total_sz) {
|
||||
|
||||
}
|
||||
@@ -1933,20 +1845,13 @@ Tins::Dot11BlockAckRequest::Dot11BlockAckRequest(
|
||||
init_block_ack();
|
||||
}
|
||||
|
||||
Tins::Dot11BlockAckRequest::Dot11BlockAckRequest(const std::string& iface,
|
||||
Tins::Dot11BlockAckRequest::Dot11BlockAckRequest(const NetworkInterface &iface,
|
||||
const address_type &dst_addr, const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface, dst_addr, target_addr, child)
|
||||
{
|
||||
init_block_ack();
|
||||
}
|
||||
|
||||
Tins::Dot11BlockAckRequest::Dot11BlockAckRequest(uint32_t iface_index,
|
||||
const address_type &dst_addr, const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface_index, dst_addr, target_addr, child)
|
||||
{
|
||||
init_block_ack();
|
||||
}
|
||||
|
||||
Tins::Dot11BlockAckRequest::Dot11BlockAckRequest(const uint8_t *buffer, uint32_t total_sz) : Dot11ControlTA(buffer, total_sz) {
|
||||
uint32_t padding = controlta_size();
|
||||
buffer += padding;
|
||||
@@ -2000,7 +1905,7 @@ Tins::Dot11BlockAck::Dot11BlockAck(const address_type &dst_addr,
|
||||
std::memset(_bitmap, 0, sizeof(_bitmap));
|
||||
}
|
||||
|
||||
Tins::Dot11BlockAck::Dot11BlockAck(const std::string& iface,
|
||||
Tins::Dot11BlockAck::Dot11BlockAck(const NetworkInterface &iface,
|
||||
const address_type &dst_addr, const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface, dst_addr, target_addr, child)
|
||||
{
|
||||
@@ -2008,14 +1913,6 @@ Tins::Dot11BlockAck::Dot11BlockAck(const std::string& iface,
|
||||
std::memset(_bitmap, 0, sizeof(_bitmap));
|
||||
}
|
||||
|
||||
Tins::Dot11BlockAck::Dot11BlockAck(uint32_t iface_index,
|
||||
const address_type &dst_addr, const address_type &target_addr, PDU* child)
|
||||
: Dot11ControlTA(iface_index, dst_addr, target_addr, child)
|
||||
{
|
||||
subtype(BLOCK_ACK);
|
||||
std::memset(_bitmap, 0, sizeof(_bitmap));
|
||||
}
|
||||
|
||||
Tins::Dot11BlockAck::Dot11BlockAck(const uint8_t *buffer, uint32_t total_sz) : Dot11ControlTA(buffer, total_sz) {
|
||||
uint32_t padding = controlta_size();
|
||||
buffer += padding;
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
const uint8_t* Tins::EthernetII::BROADCAST = (const uint8_t*)"\xff\xff\xff\xff\xff\xff";
|
||||
const uint32_t Tins::EthernetII::ADDR_SIZE;
|
||||
|
||||
Tins::EthernetII::EthernetII(const std::string& iface,
|
||||
Tins::EthernetII::EthernetII(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
||||
PDU* child)
|
||||
: PDU(ETHERTYPE_IP, child)
|
||||
@@ -49,18 +49,6 @@ Tins::EthernetII::EthernetII(const std::string& iface,
|
||||
|
||||
}
|
||||
|
||||
Tins::EthernetII::EthernetII(uint32_t iface_index,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
||||
PDU* child)
|
||||
: PDU(ETHERTYPE_IP, child)
|
||||
{
|
||||
memset(&_eth, 0, sizeof(ethhdr));
|
||||
dst_addr(dst_hw_addr);
|
||||
src_addr(src_hw_addr);
|
||||
iface(iface_index);
|
||||
_eth.payload_type = 0;
|
||||
}
|
||||
|
||||
Tins::EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(ETHERTYPE_IP)
|
||||
{
|
||||
@@ -92,14 +80,8 @@ void Tins::EthernetII::src_addr(const address_type &new_src_mac) {
|
||||
std::copy(new_src_mac.begin(), new_src_mac.end(), _eth.src_mac);
|
||||
}
|
||||
|
||||
void Tins::EthernetII::iface(uint32_t new_iface_index) {
|
||||
_iface_index = new_iface_index;
|
||||
}
|
||||
|
||||
void Tins::EthernetII::iface(const std::string& new_iface) throw (std::runtime_error) {
|
||||
if (!Tins::Utils::interface_id(new_iface, this->_iface_index)) {
|
||||
throw std::runtime_error("Invalid interface name!");
|
||||
}
|
||||
void Tins::EthernetII::iface(const NetworkInterface& new_iface) {
|
||||
_iface = new_iface;
|
||||
}
|
||||
|
||||
void Tins::EthernetII::payload_type(uint16_t new_payload_type) {
|
||||
@@ -118,7 +100,7 @@ bool Tins::EthernetII::send(PacketSender* sender) {
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = ADDR_SIZE;
|
||||
addr.sll_ifindex = _iface_index;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
|
||||
|
||||
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
@@ -164,7 +146,7 @@ Tins::PDU *Tins::EthernetII::recv_response(PacketSender *sender) {
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = ADDR_SIZE;
|
||||
addr.sll_ifindex = _iface_index;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
|
||||
|
||||
return sender->recv_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#ifndef WIN32
|
||||
#include <net/ethernet.h>
|
||||
#include <netpacket/packet.h>
|
||||
@@ -31,29 +32,20 @@
|
||||
#include "utils.h"
|
||||
|
||||
const uint8_t* Tins::IEEE802_3::BROADCAST = (const uint8_t*)"\xff\xff\xff\xff\xff\xff";
|
||||
const uint32_t Tins::IEEE802_3::ADDR_SIZE;
|
||||
|
||||
Tins::IEEE802_3::IEEE802_3(const std::string& iface, const uint8_t* dst_hw_addr, const uint8_t* src_hw_addr, PDU* child) throw (std::runtime_error) : PDU(ETHERTYPE_IP, child) {
|
||||
Tins::IEEE802_3::IEEE802_3(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
||||
PDU* child)
|
||||
: PDU(ETHERTYPE_IP, child)
|
||||
{
|
||||
memset(&_eth, 0, sizeof(ethhdr));
|
||||
if(dst_hw_addr)
|
||||
this->dst_addr(dst_hw_addr);
|
||||
if(src_hw_addr)
|
||||
this->src_addr(src_hw_addr);
|
||||
this->dst_addr(dst_hw_addr);
|
||||
this->src_addr(src_hw_addr);
|
||||
this->iface(iface);
|
||||
this->_eth.length = 0;
|
||||
|
||||
}
|
||||
|
||||
Tins::IEEE802_3::IEEE802_3(uint32_t iface_index, const uint8_t* dst_hw_addr, const uint8_t* src_hw_addr, PDU* child) : PDU(ETHERTYPE_IP, child) {
|
||||
memset(&_eth, 0, sizeof(ethhdr));
|
||||
if(dst_hw_addr)
|
||||
this->dst_addr(dst_hw_addr);
|
||||
if(src_hw_addr)
|
||||
this->src_addr(src_hw_addr);
|
||||
this->iface(iface_index);
|
||||
this->_eth.length = 0;
|
||||
}
|
||||
|
||||
Tins::IEEE802_3::IEEE802_3(const uint8_t *buffer, uint32_t total_sz) : PDU(ETHERTYPE_IP) {
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
throw std::runtime_error("Not enough size for an ethernetII header in the buffer.");
|
||||
@@ -67,22 +59,16 @@ Tins::IEEE802_3::IEEE802_3(const uint8_t *buffer, uint32_t total_sz) : PDU(ETHER
|
||||
}
|
||||
}
|
||||
|
||||
void Tins::IEEE802_3::dst_addr(const uint8_t* new_dst_mac) {
|
||||
memcpy(_eth.dst_mac, new_dst_mac, sizeof(_eth.dst_mac));
|
||||
void Tins::IEEE802_3::dst_addr(const address_type &new_dst_mac) {
|
||||
std::copy(new_dst_mac.begin(), new_dst_mac.end(), _eth.dst_mac);
|
||||
}
|
||||
|
||||
void Tins::IEEE802_3::src_addr(const uint8_t* new_src_mac) {
|
||||
memcpy(_eth.src_mac, new_src_mac, sizeof(_eth.src_mac));
|
||||
void Tins::IEEE802_3::src_addr(const address_type &new_src_mac) {
|
||||
std::copy(new_src_mac.begin(), new_src_mac.end(), _eth.src_mac);
|
||||
}
|
||||
|
||||
void Tins::IEEE802_3::iface(uint32_t new_iface_index) {
|
||||
_iface_index = new_iface_index;
|
||||
}
|
||||
|
||||
void Tins::IEEE802_3::iface(const std::string& new_iface) throw (std::runtime_error) {
|
||||
if (!Tins::Utils::interface_id(new_iface, this->_iface_index)) {
|
||||
throw std::runtime_error("Invalid interface name!");
|
||||
}
|
||||
void Tins::IEEE802_3::iface(const NetworkInterface &new_iface) {
|
||||
_iface = new_iface;
|
||||
}
|
||||
|
||||
void Tins::IEEE802_3::length(uint16_t new_length) {
|
||||
@@ -100,9 +86,9 @@ bool Tins::IEEE802_3::send(PacketSender* sender) {
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = ADDR_SIZE;
|
||||
addr.sll_ifindex = _iface_index;
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
|
||||
addr.sll_halen = address_type::address_size;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, sizeof(_eth.dst_mac));
|
||||
|
||||
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
}
|
||||
@@ -111,7 +97,7 @@ bool Tins::IEEE802_3::matches_response(uint8_t *ptr, uint32_t total_sz) {
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
return false;
|
||||
ethhdr *eth_ptr = (ethhdr*)ptr;
|
||||
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, ADDR_SIZE)) {
|
||||
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, sizeof(_eth.src_mac))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -137,9 +123,9 @@ Tins::PDU *Tins::IEEE802_3::recv_response(PacketSender *sender) {
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_802_3);
|
||||
addr.sll_halen = ADDR_SIZE;
|
||||
addr.sll_ifindex = _iface_index;
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
|
||||
addr.sll_halen = address_type::address_size;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, sizeof(_eth.dst_mac));
|
||||
|
||||
return sender->recv_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
}
|
||||
@@ -156,14 +142,3 @@ Tins::PDU *Tins::IEEE802_3::clone_packet(const uint8_t *ptr, uint32_t total_sz)
|
||||
cloned->inner_pdu(child);
|
||||
return cloned;
|
||||
}
|
||||
|
||||
void Tins::IEEE802_3::copy_fields(const IEEE802_3 *other) {
|
||||
memcpy(&_eth, &other->_eth, sizeof(_eth));
|
||||
_iface_index = other->_iface_index;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::IEEE802_3::clone_pdu() const {
|
||||
IEEE802_3 *new_pdu = new IEEE802_3(_iface_index);
|
||||
new_pdu->copy_fields(this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
@@ -29,11 +29,16 @@ IPv4Address::IPv4Address(uint32_t ip) : ip_addr(ip) {
|
||||
|
||||
}
|
||||
|
||||
IPv4Address::IPv4Address(const std::string &ip) :
|
||||
ip_addr(Utils::ip_to_int(ip)) {
|
||||
IPv4Address::IPv4Address(const std::string &ip)
|
||||
: ip_addr(Utils::ip_to_int(ip)) {
|
||||
|
||||
}
|
||||
|
||||
IPv4Address::IPv4Address(const char *ip)
|
||||
: ip_addr(Utils::ip_to_int(ip)) {
|
||||
|
||||
}
|
||||
|
||||
IPv4Address &IPv4Address::operator=(uint32_t ip) {
|
||||
ip_addr = ip;
|
||||
return *this;
|
||||
|
||||
@@ -61,17 +61,23 @@ struct InterfaceInfoCollector {
|
||||
/** \endcond */
|
||||
|
||||
namespace Tins {
|
||||
NetworkInterface::NetworkInterface() : iface_id(0) {
|
||||
|
||||
}
|
||||
|
||||
NetworkInterface::NetworkInterface(const std::string &name) {
|
||||
iface_id = if_nametoindex(name.c_str());
|
||||
if(!iface_id)
|
||||
throw std::runtime_error("Invalid interface error");
|
||||
iface_id = resolve_index(name.c_str());
|
||||
}
|
||||
|
||||
NetworkInterface::NetworkInterface(const char *name) {
|
||||
iface_id = resolve_index(name);
|
||||
}
|
||||
|
||||
NetworkInterface::NetworkInterface(IPv4Address ip) : iface_id(0) {
|
||||
typedef std::vector<Utils::RouteEntry> entries_type;
|
||||
|
||||
if(ip == "127.0.0.1")
|
||||
iface_id = if_nametoindex("lo");
|
||||
iface_id = resolve_index("lo");
|
||||
else {
|
||||
entries_type entries;
|
||||
uint32_t ip_int = ip;
|
||||
@@ -103,5 +109,12 @@ NetworkInterface::Info NetworkInterface::addresses() const {
|
||||
throw std::runtime_error("Error looking up interface address");
|
||||
return info;
|
||||
}
|
||||
|
||||
NetworkInterface::id_type NetworkInterface::resolve_index(const char *name) {
|
||||
id_type id = if_nametoindex(name);
|
||||
if(!id)
|
||||
throw std::runtime_error("Invalid interface error");
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,14 +30,9 @@
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
Tins::RadioTap::RadioTap(const std::string &iface, PDU *child) throw (std::runtime_error) : PDU(0xff, child), _options_size(0) {
|
||||
if(!Utils::interface_id(iface, _iface_index))
|
||||
throw std::runtime_error("Invalid interface name!");
|
||||
std::memset(&_radio, 0, sizeof(_radio));
|
||||
init();
|
||||
}
|
||||
|
||||
Tins::RadioTap::RadioTap(uint32_t iface_index, PDU *child) : PDU(0xff, child), _iface_index(iface_index) {
|
||||
Tins::RadioTap::RadioTap(const NetworkInterface &iface, PDU *child)
|
||||
: PDU(0xff, child), _iface(iface), _options_size(0)
|
||||
{
|
||||
std::memset(&_radio, 0, sizeof(_radio));
|
||||
init();
|
||||
}
|
||||
@@ -205,7 +200,7 @@ bool Tins::RadioTap::send(PacketSender* sender) {
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = _iface_index;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
|
||||
Tins::Dot11 *wlan = dynamic_cast<Tins::Dot11*>(inner_pdu());
|
||||
if(wlan) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "ethernetII.h"
|
||||
#include "utils.h"
|
||||
#include "network_interface.h"
|
||||
|
||||
using namespace Tins;
|
||||
|
||||
@@ -13,8 +14,8 @@ public:
|
||||
static address_type s_addr;
|
||||
static address_type d_addr;
|
||||
static address_type empty_addr;
|
||||
static const NetworkInterface iface;
|
||||
static const uint16_t p_type;
|
||||
static const uint32_t iface;
|
||||
|
||||
void test_equals(const EthernetII ð1, const EthernetII ð2);
|
||||
};
|
||||
@@ -32,9 +33,9 @@ address_type EthernetIITest::d_addr("aa:bb:cc:dd:ee:ff");
|
||||
|
||||
address_type EthernetIITest::empty_addr;
|
||||
|
||||
const uint16_t EthernetIITest::p_type = 0xd0ab;
|
||||
const NetworkInterface EthernetIITest::iface("lo");
|
||||
|
||||
const uint32_t EthernetIITest::iface = 0x12345678;
|
||||
const uint16_t EthernetIITest::p_type = 0xd0ab;
|
||||
|
||||
void EthernetIITest::test_equals(const EthernetII ð1, const EthernetII ð2) {
|
||||
EXPECT_EQ(eth1.dst_addr(), eth2.dst_addr());
|
||||
@@ -45,8 +46,8 @@ void EthernetIITest::test_equals(const EthernetII ð1, const EthernetII ð2)
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, DefaultConstructor) {
|
||||
EthernetII eth(0);
|
||||
EXPECT_EQ(eth.iface(), 0);
|
||||
EthernetII eth(iface);
|
||||
EXPECT_EQ(eth.iface(), iface);
|
||||
EXPECT_EQ(eth.dst_addr(), empty_addr);
|
||||
EXPECT_EQ(eth.src_addr(), empty_addr);
|
||||
EXPECT_EQ(eth.payload_type(), 0);
|
||||
@@ -56,57 +57,51 @@ TEST_F(EthernetIITest, DefaultConstructor) {
|
||||
|
||||
TEST_F(EthernetIITest, CopyConstructor) {
|
||||
EthernetII eth1(expected_packet, sizeof(expected_packet));
|
||||
eth1.iface(0);
|
||||
EthernetII eth2(eth1);
|
||||
test_equals(eth1, eth2);
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, CopyAssignmentOperator) {
|
||||
EthernetII eth1(expected_packet, sizeof(expected_packet));
|
||||
eth1.iface(0);
|
||||
EthernetII eth2(0);
|
||||
EthernetII eth2;
|
||||
eth2 = eth1;
|
||||
test_equals(eth1, eth2);
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, NestedCopy) {
|
||||
EthernetII *nested = new EthernetII(expected_packet, sizeof(expected_packet));
|
||||
nested->iface(0);
|
||||
EthernetII eth1(expected_packet, sizeof(expected_packet));
|
||||
eth1.iface(0);
|
||||
eth1.inner_pdu(nested);
|
||||
EthernetII eth2(eth1);
|
||||
test_equals(eth1, eth2);
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, SourceAddress) {
|
||||
EthernetII eth(0);
|
||||
EthernetII eth;
|
||||
eth.src_addr(s_addr);
|
||||
ASSERT_EQ(eth.src_addr(), s_addr);
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, DestinationAddress) {
|
||||
EthernetII eth(0);
|
||||
EthernetII eth;
|
||||
eth.dst_addr(d_addr);
|
||||
ASSERT_EQ(eth.dst_addr(), d_addr);
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, PayloadType) {
|
||||
|
||||
EthernetII eth(0);
|
||||
EthernetII eth;
|
||||
eth.payload_type(p_type);
|
||||
ASSERT_EQ(eth.payload_type(), p_type);
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, Interface) {
|
||||
|
||||
EthernetII eth(0);
|
||||
EthernetII eth;
|
||||
eth.iface(iface);
|
||||
ASSERT_EQ(eth.iface(), iface);
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, CompleteConstructor) {
|
||||
EthernetII* eth2 = new EthernetII(0);
|
||||
EthernetII* eth2 = new EthernetII();
|
||||
EthernetII eth(iface, d_addr, s_addr, eth2);
|
||||
EXPECT_EQ(eth.dst_addr(), d_addr);
|
||||
EXPECT_EQ(eth.src_addr(), s_addr);
|
||||
@@ -116,7 +111,7 @@ TEST_F(EthernetIITest, CompleteConstructor) {
|
||||
}
|
||||
|
||||
TEST_F(EthernetIITest, Serialize) {
|
||||
EthernetII eth(0, d_addr, s_addr);
|
||||
EthernetII eth(iface, d_addr, s_addr);
|
||||
eth.payload_type(p_type);
|
||||
uint32_t sz;
|
||||
uint8_t *serialized = eth.serialize(sz);
|
||||
|
||||
Reference in New Issue
Block a user