diff --git a/include/arp.h b/include/arp.h index 6bddb83..c587940 100644 --- a/include/arp.h +++ b/include/arp.h @@ -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()); diff --git a/include/dot11.h b/include/dot11.h index bbb5ee6..41a86ce 100644 --- a/include/dot11.h +++ b/include/dot11.h @@ -29,6 +29,7 @@ #include "pdu.h" #include "utils.h" +#include "network_interface.h" #include "hwaddress.h" namespace Tins { @@ -211,20 +212,6 @@ namespace Tins { Dot11(const address_type &dst_hw_addr = address_type(), PDU* child = 0); - /** - * \brief Constructor for creating a 802.11 PDU - * - * Constructor that builds a 802.11 PDU taking the interface name, - * destination's and source's MAC. - * - * \param iface string containing the interface's name from where to send the packet. - * \param dst_hw_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11(const std::string& iface, - const address_type &dst_hw_addr = address_type(), - PDU* child = 0); - /** * \brief Constructor for creating an 802.11 PDU * @@ -235,7 +222,7 @@ namespace Tins { * \param dst_hw_addr uint8_t array of 6 bytes containing the destination's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11(uint32_t iface_index, + Dot11(const NetworkInterface &iface, const address_type &dst_hw_addr = address_type(), PDU* child = 0); @@ -269,91 +256,91 @@ namespace Tins { * * \return The protocol version in an uint8_t. */ - inline uint8_t protocol() const { return this->_header.control.protocol; } + uint8_t protocol() const { return this->_header.control.protocol; } /** * \brief Getter for the 802.11 frame's type. * * \return The type of the 802.11 frame in an uint8_t. */ - inline uint8_t type() const { return this->_header.control.type; } + uint8_t type() const { return this->_header.control.type; } /** * \brief Getter for the 802.11 frame's subtype. * * \return The subtype of the 802.11 frame in an uint8_t. */ - inline uint8_t subtype() const { return this->_header.control.subtype; } + uint8_t subtype() const { return this->_header.control.subtype; } /** * \brief Getter for the 802.11 frame's "To DS" bit. * * \return Boolean indicating if the "To DS" bit is set. */ - inline bool to_ds() const { return this->_header.control.to_ds; } + bool to_ds() const { return this->_header.control.to_ds; } /** * \brief Getter for the 802.11 frame's "From DS" bit. * * \return Boolean indicating if the "From DS" bit is set. */ - inline bool from_ds() const { return this->_header.control.from_ds; } + bool from_ds() const { return this->_header.control.from_ds; } /** * \brief Getter for the 802.11 frame's "More Frag" bit. * * \return Boolean indicating if the "More Frag" bit is set. */ - inline bool more_frag() const { return this->_header.control.more_frag; } + bool more_frag() const { return this->_header.control.more_frag; } /** * \brief Getter for the 802.11 frame's "Retry" bit. * * \return Boolean indicating if the "Retry" bit is set. */ - inline bool retry() const { return this->_header.control.retry; } + bool retry() const { return this->_header.control.retry; } /** * \brief Getter for the 802.11 frame's "Power Management" bit. * * \return Boolean indicating if the "Power Management" bit is set. */ - inline bool power_mgmt() const { return this->_header.control.power_mgmt; } + bool power_mgmt() const { return this->_header.control.power_mgmt; } /** * \brief Getter for the 802.11 frame's "WEP" bit. * * \return Boolean indicating if the "WEP" bit is set. */ - inline bool wep() const { return this->_header.control.wep; } + bool wep() const { return this->_header.control.wep; } /** * \brief Getter for the 802.11 frame's "Order" bit. * * \return Boolean indicating if the "Order" bit is set. */ - inline bool order() const { return this->_header.control.order; } + bool order() const { return this->_header.control.order; } /** * \brief Getter for the duration/id field. * * \return The value of the duration/id field in an uint16_t. */ - inline uint16_t duration_id() const { return this->_header.duration_id; } + uint16_t duration_id() const { return this->_header.duration_id; } /** * \brief Getter for the first address. * * \return The first address as a constant uint8_t pointer. */ - inline address_type addr1() const { return this->_header.addr1; } + address_type addr1() const { return this->_header.addr1; } /** * \brief Getter for the interface. * * \return 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 Setter for the protocol version. @@ -444,14 +431,7 @@ namespace Tins { * * \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); + void iface(const NetworkInterface &new_ifac); /* Virtual methods */ /** @@ -555,7 +535,8 @@ namespace Tins { ieee80211_header _header; - uint32_t _iface_index, _options_size; + NetworkInterface _iface; + uint32_t _options_size; std::list _options; }; @@ -632,25 +613,25 @@ namespace Tins { * \brief Getter for the group suite field. * \return The group suite field. */ - inline CypherSuites group_suite() const { return _group_suite; } + CypherSuites group_suite() const { return _group_suite; } /** * \brief Getter for the version field. * \return The version field. */ - inline uint16_t version() const { return _version; } + uint16_t version() const { return _version; } /** * \brief Getter for the pairwise cypher suite list. * \return A list of pairwise cypher suites. */ - inline const std::list &pairwise_cyphers() const { return _pairwise_cyphers; } + const std::list &pairwise_cyphers() const { return _pairwise_cyphers; } /** * \brief Getter for the akm suite list. * \return A list of akm suites. */ - inline const std::list &akm_cyphers() const { return _akm_cyphers; } + const std::list &akm_cyphers() const { return _akm_cyphers; } /** * \brief Serializes this object. @@ -735,112 +716,112 @@ namespace Tins { * * \return Bool indicating the flag's value. */ - inline bool ess() const { return this->_ess; } + bool ess() const { return this->_ess; } /** * \brief Getter for the ibss flag. * * \return Bool indicating the flag's value. */ - inline bool ibss() const { return this->_ibss; } + bool ibss() const { return this->_ibss; } /** * \brief Getter for the cf_poll flag. * * \return Bool indicating the flag's value. */ - inline bool cf_poll() const { return this->_cf_poll; } + bool cf_poll() const { return this->_cf_poll; } /** * \brief Getter for the cf_poll_req flag. * * \return Bool indicating the flag's value. */ - inline bool cf_poll_req() const { return this->_cf_poll_req; } + bool cf_poll_req() const { return this->_cf_poll_req; } /** * \brief Getter for the privacy flag. * * \return Bool indicating the flag's value. */ - inline bool privacy() const { return this->_privacy; } + bool privacy() const { return this->_privacy; } /** * \brief Getter for the short_preamble flag. * * \return Bool indicating the flag's value. */ - inline bool short_preamble() const { return this->_short_preamble; } + bool short_preamble() const { return this->_short_preamble; } /** * \brief Getter for the pbcc flag. * * \return Bool indicating the flag's value. */ - inline bool pbcc() const { return this->_pbcc; } + bool pbcc() const { return this->_pbcc; } /** * \brief Getter for the channel_agility flag. * * \return Bool indicating the flag's value. */ - inline bool channel_agility() const { return this->_channel_agility; } + bool channel_agility() const { return this->_channel_agility; } /** * \brief Getter for the spectrum_mgmt flag. * * \return Bool indicating the flag's value. */ - inline bool spectrum_mgmt() const { return this->_spectrum_mgmt; } + bool spectrum_mgmt() const { return this->_spectrum_mgmt; } /** * \brief Getter for the qos flag. * * \return Bool indicating the flag's value. */ - inline bool qos() const { return this->_qos; } + bool qos() const { return this->_qos; } /** * \brief Getter for the sst flag. * * \return Bool indicating the flag's value. */ - inline bool sst() const { return this->_sst; } + bool sst() const { return this->_sst; } /** * \brief Getter for the apsd flag. * * \return Bool indicating the flag's value. */ - inline bool apsd() const { return this->_apsd; } + bool apsd() const { return this->_apsd; } /** * \brief Getter for the reserved flag. * * \return Bool indicating the flag's value. */ - inline bool reserved() const { return this->_reserved; } + bool reserved() const { return this->_reserved; } /** * \brief Getter for the dsss_ofdm flag. * * \return Bool indicating the flag's value. */ - inline bool dsss_ofdm() const { return this->_dsss_ofdm; } + bool dsss_ofdm() const { return this->_dsss_ofdm; } /** * \brief Getter for the delayed_block_ack flag. * * \return Bool indicating the flag's value. */ - inline bool delayed_block_ack() const { return this->_delayed_block_ack; } + bool delayed_block_ack() const { return this->_delayed_block_ack; } /** * \brief Getter for the immediate_block_ack flag. * * \return Bool indicating the flag's value. */ - inline bool immediate_block_ack() const { return this->_immediate_block_ack; } + bool immediate_block_ack() const { return this->_immediate_block_ack; } /** * \brief Setter for the ess flag. @@ -961,35 +942,35 @@ namespace Tins { * * \return The second address as a constant uint8_t pointer. */ - inline address_type addr2() const { return this->_ext_header.addr2; } + address_type addr2() const { return this->_ext_header.addr2; } /** * \brief Getter for the third address. * * \return The third address as a constant uint8_t pointer. */ - inline address_type addr3() const { return this->_ext_header.addr3; } + address_type addr3() const { return this->_ext_header.addr3; } /** * \brief Getter for the fragment number. * * \return The fragment number as an uint8_t. */ - inline uint8_t frag_num() const { return this->_ext_header.seq_control.frag_number; } + uint8_t frag_num() const { return this->_ext_header.seq_control.frag_number; } /** * \brief Getter for the sequence number. * * \return The sequence number as an uint16_t. */ - inline uint16_t seq_num() const { return this->_ext_header.seq_control.seq_number; } + uint16_t seq_num() const { return this->_ext_header.seq_control.seq_number; } /** * \brief Getter for the fourth address. * * \return The fourth address as a constant uint8_t pointer. */ - inline const uint8_t* addr4() const { return this->_addr4; } + const uint8_t* addr4() const { return this->_addr4; } /** * \brief Setter for the second address. @@ -1072,7 +1053,7 @@ namespace Tins { */ Dot11ManagementFrame(const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); - Dot11ManagementFrame(const std::string &iface, + Dot11ManagementFrame(const NetworkInterface &iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); Dot11ManagementFrame(const uint8_t *buffer, uint32_t total_sz); @@ -1326,7 +1307,7 @@ namespace Tins { * \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). */ - Dot11Beacon(const std::string& iface, + Dot11Beacon(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -1344,28 +1325,28 @@ namespace Tins { * * \return Timestamp value in an uint64_t. */ - inline uint64_t timestamp() const { return this->_body.timestamp; } + uint64_t timestamp() const { return this->_body.timestamp; } /** * \brief Getter for the interval field. * * \return Timestamp value in an uint16_t. */ - inline uint16_t interval() const { return this->_body.interval; } + uint16_t interval() const { return this->_body.interval; } /** * \brief Getter for the Capabilities Information. * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline const CapabilityInformation& capabilities() const { return this->_body.capability;} + const CapabilityInformation& capabilities() const { return this->_body.capability;} /** * \brief Getter for the Capabilities Information. * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline CapabilityInformation& capabilities() { return this->_body.capability;} + CapabilityInformation& capabilities() { return this->_body.capability;} /** * \brief Setter for the timestamp field. @@ -1642,7 +1623,7 @@ namespace Tins { * \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). */ - Dot11Disassoc(const std::string& iface, + Dot11Disassoc(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -1660,7 +1641,7 @@ namespace Tins { * * \return uint16_t with the reason code. */ - inline uint16_t reason_code() const { return this->_body.reason_code; } + uint16_t reason_code() const { return this->_body.reason_code; } /** * \brief Setter for the reason code. @@ -1738,7 +1719,7 @@ namespace Tins { * \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). */ - Dot11AssocRequest(const std::string& iface, + Dot11AssocRequest(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -1756,21 +1737,21 @@ namespace Tins { * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline const CapabilityInformation& capabilities() const { return this->_body.capability;} + const CapabilityInformation& capabilities() const { return this->_body.capability;} /** * \brief Getter for the Capabilities Information. * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline CapabilityInformation& capabilities() { return this->_body.capability;} + CapabilityInformation& capabilities() { return this->_body.capability;} /** * \brief Getter for the listen interval. * * \return The listen interval in an uint16_t. */ - inline uint16_t listen_interval() const { return this->_body.listen_interval; } + uint16_t listen_interval() const { return this->_body.listen_interval; } /** * \brief Setter for the listen interval. @@ -1899,7 +1880,7 @@ namespace Tins { * \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). */ - Dot11AssocResponse(const std::string& iface, + Dot11AssocResponse(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -1917,28 +1898,28 @@ namespace Tins { * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline const CapabilityInformation& capabilities() const { return this->_body.capability;} + const CapabilityInformation& capabilities() const { return this->_body.capability;} /** * \brief Getter for the Capabilities Information. * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline CapabilityInformation& capabilities() { return this->_body.capability;} + CapabilityInformation& capabilities() { return this->_body.capability;} /** * \brief Getter for the status code. * * \return The status code in an uint16_t. */ - inline uint16_t status_code() const { return this->_body.status_code; } + uint16_t status_code() const { return this->_body.status_code; } /** * \brief Getter for the AID field. * * \return The AID field value in an uint16_t. */ - inline uint16_t aid() const { return this->_body.aid; } + uint16_t aid() const { return this->_body.aid; } /** * \brief Setter for the status code. @@ -2049,7 +2030,7 @@ namespace Tins { * \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). */ - Dot11ReAssocRequest(const std::string& iface, + Dot11ReAssocRequest(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -2067,28 +2048,28 @@ namespace Tins { * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline const CapabilityInformation& capabilities() const { return this->_body.capability;} + const CapabilityInformation& capabilities() const { return this->_body.capability;} /** * \brief Getter for the Capabilities Information. * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline CapabilityInformation& capabilities() { return this->_body.capability;} + CapabilityInformation& capabilities() { return this->_body.capability;} /** * \brief Getter for the listen interval. * * \return The listen interval in an uint16_t. */ - inline uint16_t listen_interval() const { return this->_body.listen_interval; } + uint16_t listen_interval() const { return this->_body.listen_interval; } /** * \brief Getter for the current ap field. * * \return The current ap in an array of 6 uint8_t. */ - inline const uint8_t* current_ap() const { return this->_body.current_ap; } + const uint8_t* current_ap() const { return this->_body.current_ap; } /** * \brief Setter for the listen interval. @@ -2225,7 +2206,7 @@ namespace Tins { * \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). */ - Dot11ReAssocResponse(const std::string& iface, + Dot11ReAssocResponse(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -2243,28 +2224,28 @@ namespace Tins { * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline const CapabilityInformation& capabilities() const { return this->_body.capability;} + const CapabilityInformation& capabilities() const { return this->_body.capability;} /** * \brief Getter for the Capabilities Information. * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline CapabilityInformation& capabilities() { return this->_body.capability;} + CapabilityInformation& capabilities() { return this->_body.capability;} /** * \brief Getter for the status code. * * \return The status code in an uint16_t. */ - inline uint16_t status_code() const { return this->_body.status_code; } + uint16_t status_code() const { return this->_body.status_code; } /** * \brief Getter for the AID field. * * \return The AID field value in an uint16_t. */ - inline uint16_t aid() const { return this->_body.aid; } + uint16_t aid() const { return this->_body.aid; } /** * \brief Setter for the status code. @@ -2375,7 +2356,7 @@ namespace Tins { * \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). */ - Dot11Authentication(const std::string& iface, + Dot11Authentication(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -2393,21 +2374,21 @@ namespace Tins { * * \return The authentication algorithm number in an uint16_t. */ - inline uint16_t auth_algorithm() const {return this->_body.auth_algorithm; } + uint16_t auth_algorithm() const {return this->_body.auth_algorithm; } /** * \brief Getter for the Authetication Sequence Number. * * \return The authentication sequence number in an uint16_t. */ - inline uint16_t auth_seq_number() const {return this->_body.auth_seq_number; } + uint16_t auth_seq_number() const {return this->_body.auth_seq_number; } /** * \brief Getter for the status code. * * \return The status code in an uint16_t. */ - inline uint16_t status_code() const { return this->_body.status_code; } + uint16_t status_code() const { return this->_body.status_code; } /** * \brief Setter for the Authetication Algorithm Number. @@ -2510,7 +2491,7 @@ namespace Tins { * \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). */ - Dot11Deauthentication(const std::string& iface, + Dot11Deauthentication(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = 0); @@ -2528,7 +2509,7 @@ namespace Tins { * * \return uint16_t with the reason code. */ - inline uint16_t reason_code() const { return this->_body.reason_code; } + uint16_t reason_code() const { return this->_body.reason_code; } /** * \brief Setter for the reason code. @@ -2606,7 +2587,7 @@ namespace Tins { * \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). */ - Dot11ProbeRequest(const std::string& iface, + Dot11ProbeRequest(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -2701,7 +2682,7 @@ namespace Tins { * \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). */ - Dot11ProbeResponse(const std::string& iface, + Dot11ProbeResponse(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type()); @@ -2719,28 +2700,28 @@ namespace Tins { * * \return Timestamp value in an uint64_t. */ - inline uint64_t timestamp() const { return this->_body.timestamp; } + uint64_t timestamp() const { return this->_body.timestamp; } /** * \brief Getter for the interval field. * * \return Timestamp value in an uint16_t. */ - inline uint16_t interval() const { return this->_body.interval; } + uint16_t interval() const { return this->_body.interval; } /** * \brief Getter for the Capabilities Information. * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline const CapabilityInformation& capabilities() const { return this->_body.capability;} + const CapabilityInformation& capabilities() const { return this->_body.capability;} /** * \brief Getter for the Capabilities Information. * * \return CapabilityInformation Structure in a CapabilityInformation&. */ - inline CapabilityInformation& capabilities() { return this->_body.capability;} + CapabilityInformation& capabilities() { return this->_body.capability;} /** * \brief Setter for the timestamp field. @@ -2966,60 +2947,55 @@ namespace Tins { * \brief This PDU's flag. */ static const PDU::PDUType pdu_flag = PDU::DOT11_DATA; - + + Dot11Data(const address_type &dst_hw_addr = address_type(), + const address_type &src_hw_addr = address_type(), + PDU* child = 0); + + Dot11Data(const NetworkInterface &iface, const address_type &dst_hw_addr, + const address_type &src_hw_addr, PDU* child = 0); + /** * \brief Constructor which creates a Dot11Data object from a buffer and adds all identifiable * PDUs found in the buffer as children of this one. * \param buffer The buffer from which this PDU will be constructed. * \param total_sz The total size of the buffer. */ - Dot11Data(uint32_t iface_index, - const address_type &dst_hw_addr = address_type(), - const address_type &src_hw_addr = address_type(), - PDU* child = 0); - - Dot11Data(const address_type &dst_hw_addr = address_type(), - const address_type &src_hw_addr = address_type(), - PDU* child = 0); - - Dot11Data(const std::string &iface, const address_type &dst_hw_addr, - const address_type &src_hw_addr, PDU* child = 0); - Dot11Data(const uint8_t *buffer, uint32_t total_sz); /** * \brief Getter for the second address. * * \return The second address as a constant uint8_t pointer. */ - inline address_type addr2() const { return this->_ext_header.addr2; } + address_type addr2() const { return this->_ext_header.addr2; } /** * \brief Getter for the third address. * * \return The third address as a constant uint8_t pointer. */ - inline address_type addr3() const { return this->_ext_header.addr3; } + address_type addr3() const { return this->_ext_header.addr3; } /** * \brief Getter for the fragment number. * * \return The fragment number as an uint8_t. */ - inline uint8_t frag_num() const { return this->_ext_header.seq_control.frag_number; } + uint8_t frag_num() const { return this->_ext_header.seq_control.frag_number; } /** * \brief Getter for the sequence number. * * \return The sequence number as an uint16_t. */ - inline uint16_t seq_num() const { return this->_ext_header.seq_control.seq_number; } + uint16_t seq_num() const { return this->_ext_header.seq_control.seq_number; } /** * \brief Getter for the fourth address. * * \return The fourth address as a constant uint8_t pointer. */ - inline const uint8_t* addr4() const { return this->_addr4; } + const uint8_t* addr4() const { return this->_addr4; } /** * \brief Setter for the second address. @@ -3125,9 +3101,9 @@ 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 802.11 PDU (optional). */ - Dot11QoSData(const address_type &dst_hw_addr = 0, - const address_type &src_hw_addr = 0, - PDU* child = 0); + Dot11QoSData(const address_type &dst_hw_addr = address_type(), + const address_type &src_hw_addr = address_type(), + PDU* child = 0); /** * \brief Constructor for creating a 802.11 QoS Data PDU @@ -3140,23 +3116,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 802.11 PDU (optional). */ - Dot11QoSData(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 802.11 QoS Data PDU - * - * Constructor that builds an 802.11 QoS Data 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 802.11 PDU (optional). - */ - Dot11QoSData(uint32_t iface_index, + Dot11QoSData(const NetworkInterface& iface, const address_type &dst_hw_addr = address_type(), const address_type &src_hw_addr = address_type(), PDU* child = 0); @@ -3184,7 +3144,7 @@ namespace Tins { * * \return The value of the qos_control field in an uint16_t. */ - inline uint16_t qos_control() const { return this->_qos_control; } + uint16_t qos_control() const { return this->_qos_control; } /** * \brief Setter for the qos_control field. @@ -3261,21 +3221,7 @@ namespace Tins { * \param dst_addr uint8_t array of 6 bytes containing the destination's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11Control(const std::string& iface, - const address_type &dst_addr = address_type(), - PDU* child = 0); - - /** - * \brief Constructor for creating an 802.11 control frame PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11Control(uint32_t iface_index, + Dot11Control(const NetworkInterface& iface, const address_type &dst_addr = address_type(), PDU* child = 0); @@ -3333,23 +3279,7 @@ namespace Tins { * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11ControlTA(const std::string& iface, - const address_type &dst_addr = address_type(), - const address_type &target_addr = address_type(), - PDU* child = 0); - - /** - * \brief Constructor for creating an 802.11 control frame TA PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11ControlTA(uint32_t iface_index, + Dot11ControlTA(const NetworkInterface& iface, const address_type &dst_addr = address_type(), const address_type &target_addr = address_type(), PDU* child = 0); @@ -3365,7 +3295,7 @@ namespace Tins { /** * \brief Getter for the target address field. */ - inline address_type target_addr() const { return _taddr; } + address_type target_addr() const { return _taddr; } /** * \brief Setter for the target address field. @@ -3423,27 +3353,11 @@ namespace Tins { * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11RTS(const std::string& iface, + Dot11RTS(const NetworkInterface& iface, const address_type &dst_addr = address_type(), const address_type &target_addr = address_type(), PDU* child = 0); - - /** - * \brief Constructor for creating an 802.11 RTS frame PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11RTS(uint32_t iface_index, - const address_type &dst_hw_addr = address_type(), - const address_type &target_addr = address_type(), - PDU* child = 0); - + /** * \brief Constructor which creates an 802.11 RTS frame object from a buffer and * adds all identifiable PDUs found in the buffer as children of this one. @@ -3506,23 +3420,7 @@ namespace Tins { * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11PSPoll(const std::string& iface, - const address_type &dst_addr = address_type(), - const address_type &target_addr = address_type(), - PDU* child = 0); - - /** - * \brief Constructor for creating an 802.11 PS-Poll frame PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11PSPoll(uint32_t iface_index, + Dot11PSPoll(const NetworkInterface& iface, const address_type &dst_addr = address_type(), const address_type &target_addr = address_type(), PDU* child = 0); @@ -3589,27 +3487,11 @@ namespace Tins { * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11CFEnd(const std::string& iface, + Dot11CFEnd(const NetworkInterface& iface, const address_type &dst_addr = address_type(), const address_type &target_addr = address_type(), PDU* child = 0); - - /** - * \brief Constructor for creating an 802.11 CF-End frame PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11CFEnd(uint32_t iface_index, - const address_type &dst_addr = address_type(), - const address_type &target_addr = address_type(), - PDU* child = 0); - + /** * \brief Constructor which creates an 802.11 CF-End frame object from a buffer and * adds all identifiable PDUs found in the buffer as children of this one. @@ -3670,26 +3552,11 @@ namespace Tins { * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11EndCFAck(const std::string& iface, + Dot11EndCFAck(const NetworkInterface& iface, const address_type &dst_addr = address_type(), const address_type &target_addr = address_type(), PDU* child = 0); - /** - * \brief Constructor for creating an 802.11 End-CF-Ack frame PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11EndCFAck(uint32_t iface_index, - const address_type &dst_addr = address_type(), - const address_type &target_addr = address_type(), - PDU* child = 0); - /** * \brief Constructor which creates an 802.11 End-CF-Ack frame object from a buffer and * adds all identifiable PDUs found in the buffer as children of this one. @@ -3748,22 +3615,7 @@ namespace Tins { * \param dst_addr uint8_t array of 6 bytes containing the destination's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11Ack(const std::string& iface, - const address_type &dst_addr = address_type(), - PDU* child = 0); - - /** - * \brief Constructor for creating an 802.11 Ack frame PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11Ack(uint32_t iface_index, + Dot11Ack(const NetworkInterface& iface, const address_type &dst_addr = address_type(), PDU* child = 0); @@ -3830,22 +3682,7 @@ namespace Tins { * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11BlockAckRequest(const std::string& iface, - const address_type &dst_addr = address_type(), - const address_type &target_addr = address_type(), - PDU* child = 0); - - /** - * \brief Constructor for creating an 802.11 Block Ack request frame PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11BlockAckRequest(uint32_t iface_index, + Dot11BlockAckRequest(const NetworkInterface& iface, const address_type &dst_addr = address_type(), const address_type &target_addr = address_type(), PDU* child = 0); @@ -3965,22 +3802,7 @@ namespace Tins { * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). * \param child PDU* with the PDU contained by the 802.11 PDU (optional). */ - Dot11BlockAck(const std::string& iface, - const address_type &dst_addr = address_type(), - const address_type &target_addr = address_type(), - PDU* child = 0); - - /** - * \brief Constructor for creating an 802.11 Block Ack frame PDU - * - * Constructor that builds an 802.11 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_addr uint8_t array of 6 bytes containing the destination's MAC(optional). - * \param target_addr uint8_t array of 6 bytes containing the source's MAC(optional). - * \param child PDU* with the PDU contained by the 802.11 PDU (optional). - */ - Dot11BlockAck(uint32_t iface_index, + Dot11BlockAck(const NetworkInterface& iface, const address_type &dst_addr = address_type(), const address_type &target_addr = address_type(), PDU* child = 0); diff --git a/include/ethernetII.h b/include/ethernetII.h index efb4a04..8515e22 100644 --- a/include/ethernetII.h +++ b/include/ethernetII.h @@ -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; }; }; diff --git a/include/hwaddress.h b/include/hwaddress.h index 18efff3..0dbb882 100644 --- a/include/hwaddress.h +++ b/include/hwaddress.h @@ -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); } diff --git a/include/ieee802-3.h b/include/ieee802-3.h index 50ef53f..e2e84aa 100644 --- a/include/ieee802-3.h +++ b/include/ieee802-3.h @@ -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(); + } 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; }; }; diff --git a/include/ip.h b/include/ip.h index 8537c49..10445eb 100644 --- a/include/ip.h +++ b/include/ip.h @@ -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 diff --git a/include/ipaddress.h b/include/ipaddress.h index a45a694..858d011 100644 --- a/include/ipaddress.h +++ b/include/ipaddress.h @@ -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); diff --git a/include/network_interface.h b/include/network_interface.h index ceda418..c05ed12 100644 --- a/include/network_interface.h +++ b/include/network_interface.h @@ -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; }; } diff --git a/include/radiotap.h b/include/radiotap.h index 75ff49e..70d4e0e 100644 --- a/include/radiotap.h +++ b/include/radiotap.h @@ -24,6 +24,7 @@ #include #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; diff --git a/src/arp.cpp b/src/arp.cpp index f4449b6..7cb0dcc 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -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) { diff --git a/src/dot11.cpp b/src/dot11.cpp index 5518860..c465fec 100644 --- a/src/dot11.cpp +++ b/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::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; diff --git a/src/ethernetII.cpp b/src/ethernetII.cpp index c3dc724..58dd1bd 100644 --- a/src/ethernetII.cpp +++ b/src/ethernetII.cpp @@ -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)); diff --git a/src/ieee802-3.cpp b/src/ieee802-3.cpp index 3eb4baf..4afa3c6 100644 --- a/src/ieee802-3.cpp +++ b/src/ieee802-3.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #ifndef WIN32 #include #include @@ -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; -} diff --git a/src/ipaddress.cpp b/src/ipaddress.cpp index 4e20895..25a5f00 100644 --- a/src/ipaddress.cpp +++ b/src/ipaddress.cpp @@ -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; diff --git a/src/network_interface.cpp b/src/network_interface.cpp index cd1cb34..1f68f97 100644 --- a/src/network_interface.cpp +++ b/src/network_interface.cpp @@ -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 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; +} } diff --git a/src/radiotap.cpp b/src/radiotap.cpp index fe0bcc8..f157f5f 100644 --- a/src/radiotap.cpp +++ b/src/radiotap.cpp @@ -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(inner_pdu()); if(wlan) { diff --git a/tests/src/ethernetII_test.cpp b/tests/src/ethernetII_test.cpp index 16734a5..55d6b21 100644 --- a/tests/src/ethernetII_test.cpp +++ b/tests/src/ethernetII_test.cpp @@ -2,6 +2,7 @@ #include #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);