From 958edcc74a22aec0049de775e85563c600232214 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 2 Sep 2012 18:24:59 -0300 Subject: [PATCH] Added small_uint class. --- include/dot11.h | 92 +++++++----- include/eapol.h | 80 ++++------- include/ip.h | 9 +- include/pdu.h | 2 +- include/rawpdu.h | 71 ++++++---- include/tcp.h | 9 +- src/dot11.cpp | 71 +++++----- src/eapol.cpp | 205 +++++++++------------------ src/ip.cpp | 61 ++++---- src/rawpdu.cpp | 29 ++-- src/tcp.cpp | 6 +- src/utils.cpp | 2 +- tests/src/dot11/ack.cpp | 2 +- tests/src/dot11/assoc_request.cpp | 2 +- tests/src/dot11/assoc_response.cpp | 2 +- tests/src/dot11/authentication.cpp | 2 +- tests/src/dot11/beacon.cpp | 20 ++- tests/src/dot11/cfend.cpp | 2 +- tests/src/dot11/cfendack.cpp | 2 +- tests/src/dot11/data.cpp | 2 +- tests/src/dot11/deauthentication.cpp | 2 +- tests/src/dot11/disassoc.cpp | 2 +- tests/src/dot11/probe_request.cpp | 2 +- tests/src/dot11/probe_response.cpp | 2 +- tests/src/dot11/pspoll.cpp | 2 +- tests/src/dot11/reassoc_request.cpp | 2 +- tests/src/dot11/reassoc_response.cpp | 2 +- tests/src/dot11/rts.cpp | 2 +- 28 files changed, 326 insertions(+), 361 deletions(-) diff --git a/include/dot11.h b/include/dot11.h index 5934c5e..255e596 100644 --- a/include/dot11.h +++ b/include/dot11.h @@ -32,6 +32,7 @@ #include "utils.h" #include "network_interface.h" #include "hwaddress.h" +#include "small_uint.h" namespace Tins { @@ -218,70 +219,70 @@ namespace Tins { * * \return The protocol version in an uint8_t. */ - uint8_t protocol() const { return _header.control.protocol; } + small_uint<2> protocol() const { return _header.control.protocol; } /** * \brief Getter for the 802.11 frame's type. * * \return The type of the 802.11 frame in an uint8_t. */ - uint8_t type() const { return _header.control.type; } + small_uint<2> type() const { return _header.control.type; } /** * \brief Getter for the 802.11 frame's subtype. * * \return The subtype of the 802.11 frame in an uint8_t. */ - uint8_t subtype() const { return _header.control.subtype; } + small_uint<4> subtype() const { return _header.control.subtype; } /** * \brief Getter for the 802.11 frame's "To DS" bit. * * \return Boolean indicating if the "To DS" bit is set. */ - bool to_ds() const { return _header.control.to_ds; } + small_uint<1> to_ds() const { return _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. */ - bool from_ds() const { return _header.control.from_ds; } + small_uint<1> from_ds() const { return _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. */ - bool more_frag() const { return _header.control.more_frag; } + small_uint<1> more_frag() const { return _header.control.more_frag; } /** * \brief Getter for the 802.11 frame's "Retry" bit. * * \return Boolean indicating if the "Retry" bit is set. */ - bool retry() const { return _header.control.retry; } + small_uint<1> retry() const { return _header.control.retry; } /** * \brief Getter for the 802.11 frame's "Power Management" bit. * * \return Boolean indicating if the "Power Management" bit is set. */ - bool power_mgmt() const { return _header.control.power_mgmt; } + small_uint<1> power_mgmt() const { return _header.control.power_mgmt; } /** * \brief Getter for the 802.11 frame's "WEP" bit. * * \return Boolean indicating if the "WEP" bit is set. */ - bool wep() const { return _header.control.wep; } + small_uint<1> wep() const { return _header.control.wep; } /** * \brief Getter for the 802.11 frame's "Order" bit. * * \return Boolean indicating if the "Order" bit is set. */ - bool order() const { return _header.control.order; } + small_uint<1> order() const { return _header.control.order; } /** * \brief Getter for the duration/id field. @@ -309,70 +310,70 @@ namespace Tins { * * \param new_proto uint8_t with the new protocol version. */ - void protocol(uint8_t new_proto); + void protocol(small_uint<2> new_proto); /** * \brief Setter for the 802.11 frame's type. * * \param new_type uint8_t with the new type of the 802.11 frame. */ - void type(uint8_t new_type); + void type(small_uint<2> new_type); /** * \brief Setter for the 802.11 frame's subtype. * * \param new_subtype uint8_t with the new subtype of the 802.11 frame. */ - void subtype(uint8_t new_subtype); + void subtype(small_uint<4> new_subtype); /** * \brief Setter for the 802.11 frame's "To DS" bit. * * \param new_value bool indicating the new value of the flag. */ - void to_ds(bool new_value); + void to_ds(small_uint<1> new_value); /** * \brief Setter for the 802.11 frame's "From DS" bit. * * \param new_value bool indicating the new value of the flag. */ - void from_ds(bool new_value); + void from_ds(small_uint<1> new_value); /** * \brief Setter for the 802.11 frame's "More Frag" bit. * * \param new_value bool indicating the new value of the flag. */ - void more_frag(bool new_value); + void more_frag(small_uint<1> new_value); /** * \brief Setter for the 802.11 frame's "Retry" bit. * * \param new_value bool indicating the new value of the flag. */ - void retry(bool new_value); + void retry(small_uint<1> new_value); /** * \brief Setter for the 802.11 frame's "Power Management" bit. * * \param new_value bool indicating the new value of the flag. */ - void power_mgmt(bool new_value); + void power_mgmt(small_uint<1> new_value); /** * \brief Setter for the 802.11 frame's "WEP" bit. * * \param new_value bool indicating the new value of the flag. */ - void wep(bool new_value); + void wep(small_uint<1> new_value); /** * \brief Setter for the 802.11 frame's "Order" bit. * * \param new_value bool indicating the new value of the flag. */ - void order(bool new_value); + void order(small_uint<1> new_value); /** * \brief Setter for the duration/id field. @@ -531,6 +532,21 @@ namespace Tins { PSK = 0x02ac0f00 }; + /** + * The type used to store the cypher suites. + */ + typedef std::vector cyphers_type; + + /** + * The type used to store the AKM suites. + */ + typedef std::vector akm_type; + + /** + * The type returned on serialization. + */ + typedef std::vector serialization_type; + /** * \brief Creates an instance of RSNInformation. * @@ -587,33 +603,36 @@ namespace Tins { * \brief Getter for the version field. * \return The version field. */ - uint16_t version() const { return _version; } + uint16_t version() const { return Utils::le_to_host(_version); } + + /** + * \brief Getter for the capabilities field. + * \return The version field. + */ + uint16_t capabilities() const { return Utils::le_to_host(_capabilities); } /** * \brief Getter for the pairwise cypher suite list. * \return A list of pairwise cypher suites. */ - const std::list &pairwise_cyphers() const { return _pairwise_cyphers; } + const cyphers_type &pairwise_cyphers() const { return _pairwise_cyphers; } /** * \brief Getter for the akm suite list. * \return A list of akm suites. */ - const std::list &akm_cyphers() const { return _akm_cyphers; } + const akm_type &akm_cyphers() const { return _akm_cyphers; } /** * \brief Serializes this object. - * \param size Output parameter which will contain the size of - * the allocated buffer. - * \return The result of the serialization. This pointer should - * be free'd using operator delete[]. + * \return The result of the serialization. */ - uint8_t *serialize(uint32_t &size) const; + serialization_type serialize() const; private: uint16_t _version, _capabilities; CypherSuites _group_suite; - std::list _akm_cyphers; - std::list _pairwise_cyphers; + akm_type _akm_cyphers; + cyphers_type _pairwise_cyphers; }; /** @@ -1315,13 +1334,14 @@ namespace Tins { // Option searching helpers /** - * \brief Helper method to search for the RSN information of this PDU. - * - * This method fills the RSN information structure of this PDU. - * \param rsn A pointer in which the RSN information will be stored. - * \return True if the RSNInformation option has been set. + * \brief Helper method to search for this PDU's rsn information + * option. + * + * Throws a std::runtime_error if the option has not been set. + * + * \return std::string containing the ssid. */ - bool rsn_information(RSNInformation *rsn); + RSNInformation rsn_information(); /** * \brief Helper method to search for this PDU's ssid. diff --git a/include/eapol.h b/include/eapol.h index 4177fc3..b5be496 100644 --- a/include/eapol.h +++ b/include/eapol.h @@ -24,6 +24,7 @@ #include "pdu.h" +#include "small_uint.h" #include "utils.h" @@ -169,6 +170,11 @@ namespace Tins { */ class RC4EAPOL : public EAPOL { public: + /** + * The type used to store the key. + */ + typedef std::vector key_type; + /** * \brief This PDU's flag. */ @@ -186,23 +192,6 @@ namespace Tins { */ RC4EAPOL(const uint8_t *buffer, uint32_t total_sz); - /** - * \brief Copy constructor. - */ - RC4EAPOL(const RC4EAPOL &other); - - /** - * \brief Copy assignment operator. - */ - RC4EAPOL &operator= (const RC4EAPOL &other); - - /** - * \brief RC4EAPOL destructor - * - * Memory allocated for the key field is freed(if any). - */ - ~RC4EAPOL(); - /* Getters */ /** @@ -227,13 +216,13 @@ namespace Tins { * \brief Getter for the key flag field. * \return The key flag field. */ - uint8_t key_flag() const { return _header.key_flag; } + small_uint<1> key_flag() const { return _header.key_flag; } /** * \brief Getter for the key index field. * \return The key index field. */ - uint8_t key_index() const { return _header.key_index; } + small_uint<7> key_index() const { return _header.key_index; } /** * \brief Getter for the key signature field. @@ -245,7 +234,7 @@ namespace Tins { * \brief Getter for the key field. * \return The key field. */ - const uint8_t *key() const { return _key; } + const key_type &key() const { return _key; } /* Setters */ @@ -271,13 +260,13 @@ namespace Tins { * \brief Sets the key flag field. * \param new_key_flag The new key flag to be set. */ - void key_flag(bool new_key_flag); + void key_flag(small_uint<1> new_key_flag); /** * \brief Sets the key index field. * \param new_key_index The new key index to be set. */ - void key_index(uint8_t new_key_index); + void key_index(small_uint<7> new_key_index); /** * \brief Sets the key signature field. @@ -289,7 +278,7 @@ namespace Tins { * \brief Sets the key field. * \param new_key The new key to be set. */ - void key(const uint8_t *new_key, uint32_t sz); + void key(const key_type &new_key); /* Virtual method override. */ @@ -323,7 +312,9 @@ namespace Tins { * * \sa PDU::clone_pdu */ - PDU *clone_pdu() const; + RC4EAPOL *clone_pdu() const { + return new RC4EAPOL(*this); + } private: struct rc4hdr { uint16_t key_length; @@ -334,12 +325,10 @@ namespace Tins { uint8_t key_sign[16]; } __attribute__((__packed__)); - void copy_fields(const RC4EAPOL *other); void write_body(uint8_t *buffer, uint32_t total_sz); - uint8_t *_key; - uint32_t _key_size; + key_type _key; rc4hdr _header; }; @@ -349,6 +338,11 @@ namespace Tins { */ class RSNEAPOL : public EAPOL { public: + /** + * The type used to store the key. + */ + typedef std::vector key_type; + /** * \brief This PDU's flag. */ @@ -366,23 +360,6 @@ namespace Tins { */ RSNEAPOL(const uint8_t *buffer, uint32_t total_sz); - /** - * \brief Copy constructor. - */ - RSNEAPOL(const RSNEAPOL &other); - - /** - * \brief Copy assignment operator. - */ - RSNEAPOL &operator= (const RSNEAPOL &other); - - /** - * \brief Destructor. - * - * Memory allocated for the key field is freed(if any). - */ - ~RSNEAPOL(); - /* Getters */ /** @@ -437,7 +414,7 @@ namespace Tins { * \brief Getter for the key field. * \return The key field. */ - const uint8_t *key() const { return _key; } + const key_type &key() const { return _key; } /** * \brief Returns the header size. @@ -509,7 +486,7 @@ namespace Tins { * \brief Sets the key field. * \param new_key The new key to be set. */ - void key(const uint8_t *new_key, uint32_t sz); + void key(const key_type &new_key); /** * \brief Sets RSN information for this EAPOL PDU. @@ -541,7 +518,9 @@ namespace Tins { * * \sa PDU::clone_pdu */ - PDU *clone_pdu() const; + RSNEAPOL *clone_pdu() const { + return new RSNEAPOL(*this); + } private: struct rsnhdr { uint16_t key_mic:1, @@ -551,7 +530,7 @@ namespace Tins { encrypted:1, reserved:3, key_descriptor:3, - key_type:1, + key_t:1, key_index:2, install:1, key_ack:1; @@ -563,14 +542,11 @@ namespace Tins { uint16_t wpa_length; } __attribute__((__packed__)); - - void copy_fields(const RSNEAPOL *other); void write_body(uint8_t *buffer, uint32_t total_sz); rsnhdr _header; - uint8_t *_key; - uint32_t _key_size; + key_type _key; }; }; diff --git a/include/ip.h b/include/ip.h index d1295d1..0b6e248 100644 --- a/include/ip.h +++ b/include/ip.h @@ -29,6 +29,7 @@ #include #include #include "pdu.h" +#include "small_uint.h" #include "ipaddress.h" #include "utils.h" @@ -159,7 +160,7 @@ namespace Tins { * * \return The number of dwords the header occupies in an uin8_t. */ - uint8_t head_len() const { return this->_ip.ihl; } + small_uint<4> head_len() const { return this->_ip.ihl; } /** * \brief Getter for the type of service field. @@ -227,7 +228,7 @@ namespace Tins { * \brief Getter for the version field. * \return The version for this IP PDU. */ - uint8_t version() const { return _ip.version; } + small_uint<4> version() const { return _ip.version; } /* Setters */ @@ -236,7 +237,7 @@ namespace Tins { * * \param new_head_len uint8_t with the new header length. */ - void head_len(uint8_t new_head_len); + void head_len(small_uint<4> new_head_len); /** * \brief Setter for the type of service field. @@ -306,7 +307,7 @@ namespace Tins { * * \param ver The version field to be set. */ - void version(uint8_t ver); + void version(small_uint<4> ver); /** * \brief Sets an IP option. diff --git a/include/pdu.h b/include/pdu.h index d1e95da..5280b76 100644 --- a/include/pdu.h +++ b/include/pdu.h @@ -187,7 +187,7 @@ namespace Tins { * \param flag The flag which being searched. */ template - T *find_inner_pdu(PDUType type = T::pdu_flag) { + T *find_pdu(PDUType type = T::pdu_flag) { PDU *pdu = this; while(pdu) { if(pdu->pdu_type() == type) diff --git a/include/rawpdu.h b/include/rawpdu.h index 7e361a2..b13b057 100644 --- a/include/rawpdu.h +++ b/include/rawpdu.h @@ -22,10 +22,9 @@ #ifndef TINS_RAWPDU_H #define TINS_RAWPDU_H - +#include #include "pdu.h" - namespace Tins { /** \brief Represents a PDU which holds raw data. @@ -36,11 +35,17 @@ namespace Tins { class RawPDU : public PDU { public: /** - * \brief This PDU's flag. + * The type used to store the payload. + */ + typedef std::vector payload_type; + + /** + * This PDU's flag. */ static const PDU::PDUType pdu_flag = PDU::RAW; - /** \brief Creates an instance of RawPDU. + /** + * \brief Creates an instance of RawPDU. * * The payload is copied, therefore the original payload's memory * must be freed by the user. @@ -48,34 +53,52 @@ namespace Tins { * \param size The size of the payload. */ RawPDU(const uint8_t *pload, uint32_t size); - - /** \brief Creates an instance of RawPDU. - * - * The payload is not copied in this constructor, therefore - * it must be manually freed by the user. - * \param pload The payload which the RawPDU will contain. - * \param size The size of the payload. - */ - RawPDU(uint8_t *pload, uint32_t size); - /** \brief RawPDU destructor. - * - * Deletes the payload only if it was created setting the copy - * flag to true. + /** + * \brief Setter for the payload field + * \param pload The payload to be set. */ - ~RawPDU(); + void payload(const payload_type &pload); - /** \brief Getter for the payload. - * + /** + * \brief Setter for the payload field + * \param start The start of the new payload. + * \param end The end of the new payload. + */ + template + void payload(ForwardIterator start, ForwardIterator end) { + _payload.assign(start, end); + } + + /** + * \brief Const getter for the payload. * \return The RawPDU's payload. */ - uint8_t *payload() { return _payload; } + const payload_type &payload() const { return _payload; } - /** \brief Returns the header size. + /** + * \brief Non-const getter for the payload. + * \return The RawPDU's payload. + */ + payload_type &payload() { return _payload; } + + /** + * \brief Returns the header size. + * + * This returns the same as RawPDU::payload_size(). * * This metod overrides PDU::header_size. \sa PDU::header_size */ uint32_t header_size() const; + + /** + * \brief Returns the payload size. + * + * \return uint32_t containing the payload size. + */ + uint32_t payload_size() const { + return _payload.size(); + } /** * \brief Getter for the PDU's type. @@ -85,9 +108,7 @@ namespace Tins { private: void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent); - uint8_t *_payload; - uint32_t _payload_size; - bool _owns_payload; + payload_type _payload; }; }; diff --git a/include/tcp.h b/include/tcp.h index eace96d..6f87923 100644 --- a/include/tcp.h +++ b/include/tcp.h @@ -30,6 +30,7 @@ #include #endif #include "pdu.h" +#include "small_uint.h" #include "utils.h" @@ -190,7 +191,7 @@ namespace Tins { * * \return Data offset in an uint8_t. */ - uint8_t data_offset() const { return this->_tcp.doff; } + small_uint<4> data_offset() const { return this->_tcp.doff; } /** * \brief Getter for the option list. @@ -205,7 +206,7 @@ namespace Tins { * \param tcp_flag The polled flag. * \return The value of the flag. */ - uint8_t get_flag(Flags tcp_flag); + small_uint<1> get_flag(Flags tcp_flag); /* Setters */ @@ -263,7 +264,7 @@ namespace Tins { * * \param new_doff The new data offset pointer. */ - void data_offset(uint8_t new_doff); + void data_offset(small_uint<4> new_doff); /** * \brief Set the payload. @@ -368,7 +369,7 @@ namespace Tins { * \param tcp_flag The flag to be set. * \param value The new value for this flag. Must be 0 or 1. */ - void set_flag(Flags tcp_flag, uint8_t value); + void set_flag(Flags tcp_flag, small_uint<1> value); /** * \brief Adds a TCP option. diff --git a/src/dot11.cpp b/src/dot11.cpp index 69d692f..de77972 100644 --- a/src/dot11.cpp +++ b/src/dot11.cpp @@ -104,43 +104,43 @@ const Dot11::Dot11Option *Dot11::search_option(TaggedOption opt) const { return 0; } -void Dot11::protocol(uint8_t new_proto) { +void Dot11::protocol(small_uint<2> new_proto) { this->_header.control.protocol = new_proto; } -void Dot11::type(uint8_t new_type) { +void Dot11::type(small_uint<2> new_type) { this->_header.control.type = new_type; } -void Dot11::subtype(uint8_t new_subtype) { +void Dot11::subtype(small_uint<4> new_subtype) { this->_header.control.subtype = new_subtype; } -void Dot11::to_ds(bool new_value) { +void Dot11::to_ds(small_uint<1> new_value) { this->_header.control.to_ds = (new_value)? 1 : 0; } -void Dot11::from_ds(bool new_value) { +void Dot11::from_ds(small_uint<1> new_value) { this->_header.control.from_ds = (new_value)? 1 : 0; } -void Dot11::more_frag(bool new_value) { +void Dot11::more_frag(small_uint<1> new_value) { this->_header.control.more_frag = (new_value)? 1 : 0; } -void Dot11::retry(bool new_value) { +void Dot11::retry(small_uint<1> new_value) { this->_header.control.retry = (new_value)? 1 : 0; } -void Dot11::power_mgmt(bool new_value) { +void Dot11::power_mgmt(small_uint<1> new_value) { this->_header.control.power_mgmt = (new_value)? 1 : 0; } -void Dot11::wep(bool new_value) { +void Dot11::wep(small_uint<1> new_value) { this->_header.control.wep = (new_value)? 1 : 0; } -void Dot11::order(bool new_value) { +void Dot11::order(small_uint<1> new_value) { this->_header.control.order = (new_value)? 1 : 0; } @@ -338,10 +338,8 @@ void Dot11ManagementFrame::ssid(const std::string &new_ssid) { } void Dot11ManagementFrame::rsn_information(const RSNInformation& info) { - uint32_t size; - uint8_t *buffer = info.serialize(size); - add_tagged_option(RSN, size, buffer); - delete[] buffer; + RSNInformation::serialization_type buffer = info.serialize(); + add_tagged_option(RSN, buffer.size(), &buffer[0]); } uint8_t *Dot11ManagementFrame::serialize_rates(const rates_type &rates) { @@ -570,45 +568,47 @@ void Dot11ManagementFrame::challenge_text(const std::string &text) { // Getters -bool Dot11ManagementFrame::rsn_information(RSNInformation *rsn) { +RSNInformation Dot11ManagementFrame::rsn_information() { + const char *err_msg = "Malformed RSN information option"; const Dot11::Dot11Option *option = search_option(RSN); if(!option || option->data_size() < (sizeof(uint16_t) << 1) + sizeof(uint32_t)) - return false; + throw std::runtime_error("RSN information not set"); + RSNInformation rsn; const uint8_t *buffer = option->data_ptr(); uint32_t bytes_left = option->data_size(); - rsn->version(*(uint16_t*)buffer); + rsn.version(*(uint16_t*)buffer); buffer += sizeof(uint16_t); - rsn->group_suite((RSNInformation::CypherSuites)*(uint32_t*)buffer); + rsn.group_suite((RSNInformation::CypherSuites)*(uint32_t*)buffer); buffer += sizeof(uint32_t); bytes_left -= (sizeof(uint16_t) << 1) + sizeof(uint32_t); if(bytes_left < sizeof(uint16_t)) - return false; + throw std::runtime_error(err_msg); uint16_t count = *(uint16_t*)buffer; buffer += sizeof(uint16_t); if(count * sizeof(uint32_t) > bytes_left) - return false; + throw std::runtime_error(err_msg); bytes_left -= count * sizeof(uint32_t); while(count--) { - rsn->add_pairwise_cypher((RSNInformation::CypherSuites)*(uint32_t*)buffer); + rsn.add_pairwise_cypher((RSNInformation::CypherSuites)*(uint32_t*)buffer); buffer += sizeof(uint32_t); } if(bytes_left < sizeof(uint16_t)) - return false; + throw std::runtime_error(err_msg); count = *(uint16_t*)buffer; buffer += sizeof(uint16_t); bytes_left -= sizeof(uint16_t); if(count * sizeof(uint32_t) > bytes_left) - return false; + throw std::runtime_error(err_msg); bytes_left -= count * sizeof(uint32_t); while(count--) { - rsn->add_akm_cypher((RSNInformation::AKMSuites)*(uint32_t*)buffer); + rsn.add_akm_cypher((RSNInformation::AKMSuites)*(uint32_t*)buffer); buffer += sizeof(uint32_t); } if(bytes_left < sizeof(uint16_t)) - return false; - rsn->capabilities(*(uint16_t*)buffer); - return true; + throw std::runtime_error(err_msg); + rsn.capabilities(*(uint16_t*)buffer); + return rsn; } string Dot11ManagementFrame::ssid() const { @@ -1587,32 +1587,33 @@ void RSNInformation::group_suite(CypherSuites group) { } void RSNInformation::version(uint16_t ver) { - _version = ver; + _version = Utils::host_to_le(ver); } void RSNInformation::capabilities(uint16_t cap) { - _capabilities = cap; + _capabilities = Utils::host_to_le(cap); } -uint8_t *RSNInformation::serialize(uint32_t &size) const { - size = sizeof(_version) + sizeof(_capabilities) + sizeof(uint32_t); +RSNInformation::serialization_type RSNInformation::serialize() const { + uint32_t size = sizeof(_version) + sizeof(_capabilities) + sizeof(uint32_t); size += (sizeof(uint16_t) << 1); // 2 lists count. size += sizeof(uint32_t) * (_akm_cyphers.size() + _pairwise_cyphers.size()); - - uint8_t *buffer = new uint8_t[size], *ptr = buffer; + + serialization_type buffer(size); + serialization_type::value_type *ptr = &buffer[0]; *(uint16_t*)ptr = _version; ptr += sizeof(_version); *(uint32_t*)ptr = _group_suite; ptr += sizeof(uint32_t); *(uint16_t*)ptr = _pairwise_cyphers.size(); ptr += sizeof(uint16_t); - for(std::list::const_iterator it = _pairwise_cyphers.begin(); it != _pairwise_cyphers.end(); ++it) { + for(cyphers_type::const_iterator it = _pairwise_cyphers.begin(); it != _pairwise_cyphers.end(); ++it) { *(uint32_t*)ptr = *it; ptr += sizeof(uint32_t); } *(uint16_t*)ptr = _akm_cyphers.size(); ptr += sizeof(uint16_t); - for(std::list::const_iterator it = _akm_cyphers.begin(); it != _akm_cyphers.end(); ++it) { + for(akm_type::const_iterator it = _akm_cyphers.begin(); it != _akm_cyphers.end(); ++it) { *(uint32_t*)ptr = *it; ptr += sizeof(uint32_t); } diff --git a/src/eapol.cpp b/src/eapol.cpp index 31898fd..c46ef36 100644 --- a/src/eapol.cpp +++ b/src/eapol.cpp @@ -26,24 +26,25 @@ #include "dot11.h" -Tins::EAPOL::EAPOL(uint8_t packet_type, EAPOLTYPE type) : PDU(0xff) { +namespace Tins { +EAPOL::EAPOL(uint8_t packet_type, EAPOLTYPE type) : PDU(0xff) { std::memset(&_header, 0, sizeof(_header)); _header.version = 1; _header.packet_type = packet_type; _header.type = (uint8_t)type; } -Tins::EAPOL::EAPOL(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) { +EAPOL::EAPOL(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) { if(total_sz < sizeof(_header)) throw std::runtime_error("Not enough size for an EAPOL header in the buffer."); std::memcpy(&_header, buffer, sizeof(_header)); } -Tins::EAPOL::EAPOL(const EAPOL &other) : PDU(other) { +EAPOL::EAPOL(const EAPOL &other) : PDU(other) { copy_eapol_fields(&other); } -Tins::EAPOL *Tins::EAPOL::from_bytes(const uint8_t *buffer, uint32_t total_sz) { +EAPOL *EAPOL::from_bytes(const uint8_t *buffer, uint32_t total_sz) { if(total_sz < sizeof(eapolhdr)) throw std::runtime_error("Not enough size for an EAPOL header in the buffer."); const eapolhdr *ptr = (const eapolhdr*)buffer; @@ -59,23 +60,23 @@ Tins::EAPOL *Tins::EAPOL::from_bytes(const uint8_t *buffer, uint32_t total_sz) { return 0; } -void Tins::EAPOL::version(uint8_t new_version) { +void EAPOL::version(uint8_t new_version) { _header.version = new_version; } -void Tins::EAPOL::packet_type(uint8_t new_ptype) { +void EAPOL::packet_type(uint8_t new_ptype) { _header.packet_type = new_ptype; } -void Tins::EAPOL::length(uint16_t new_length) { +void EAPOL::length(uint16_t new_length) { _header.length = new_length; } -void Tins::EAPOL::type(uint8_t new_type) { +void EAPOL::type(uint8_t new_type) { _header.type = new_type; } -void Tins::EAPOL::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) { +void EAPOL::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) { uint32_t sz = header_size(); assert(total_sz >= sz); if(!_header.length) @@ -84,18 +85,22 @@ void Tins::EAPOL::write_serialization(uint8_t *buffer, uint32_t total_sz, const write_body(buffer + sizeof(_header), total_sz - sizeof(_header)); } -void Tins::EAPOL::copy_eapol_fields(const EAPOL *other) { +void EAPOL::copy_eapol_fields(const EAPOL *other) { std::memcpy(&_header, &other->_header, sizeof(_header)); } /* RC4EAPOL */ -Tins::RC4EAPOL::RC4EAPOL() : EAPOL(0x03, RC4), _key(0), _key_size(0) { +RC4EAPOL::RC4EAPOL() +: EAPOL(0x03, RC4) +{ std::memset(&_header, 0, sizeof(_header)); } -Tins::RC4EAPOL::RC4EAPOL(const uint8_t *buffer, uint32_t total_sz) : EAPOL(buffer, total_sz), _key_size(0) { +RC4EAPOL::RC4EAPOL(const uint8_t *buffer, uint32_t total_sz) +: EAPOL(buffer, total_sz) +{ buffer += sizeof(eapolhdr); total_sz -= sizeof(eapolhdr); if(total_sz < sizeof(_header)) @@ -103,101 +108,64 @@ Tins::RC4EAPOL::RC4EAPOL(const uint8_t *buffer, uint32_t total_sz) : EAPOL(buffe std::memcpy(&_header, buffer, sizeof(_header)); buffer += sizeof(_header); total_sz -= sizeof(_header); - if(total_sz == key_length()) { - _key = new uint8_t[total_sz]; - _key_size = total_sz; - std::memcpy(_key, buffer, total_sz); - } - else - _key = 0; + if(total_sz == key_length()) + _key.assign(buffer, buffer + total_sz); } -Tins::RC4EAPOL::RC4EAPOL(const RC4EAPOL &other) : EAPOL(other) { - copy_fields(&other); -} - -Tins::RC4EAPOL &Tins::RC4EAPOL::operator= (const RC4EAPOL &other) { - copy_fields(&other); - copy_inner_pdu(other); - return *this; -} - -Tins::RC4EAPOL::~RC4EAPOL() { - delete[] _key; -} - -void Tins::RC4EAPOL::key_length(uint16_t new_key_length) { +void RC4EAPOL::key_length(uint16_t new_key_length) { _header.key_length = Utils::host_to_be(new_key_length); } -void Tins::RC4EAPOL::replay_counter(uint16_t new_replay_counter) { +void RC4EAPOL::replay_counter(uint16_t new_replay_counter) { _header.replay_counter = Utils::host_to_be(new_replay_counter); } -void Tins::RC4EAPOL::key_iv(const uint8_t *new_key_iv) { +void RC4EAPOL::key_iv(const uint8_t *new_key_iv) { std::memcpy(_header.key_iv, new_key_iv, sizeof(_header.key_iv)); } -void Tins::RC4EAPOL::key_flag(bool new_key_flag) { +void RC4EAPOL::key_flag(small_uint<1> new_key_flag) { _header.key_flag = new_key_flag; } -void Tins::RC4EAPOL::key_index(uint8_t new_key_index) { +void RC4EAPOL::key_index(small_uint<7> new_key_index) { _header.key_index = new_key_index; } -void Tins::RC4EAPOL::key_sign(const uint8_t *new_key_sign) { +void RC4EAPOL::key_sign(const uint8_t *new_key_sign) { std::memcpy(_header.key_sign, new_key_sign, sizeof(_header.key_sign)); } -void Tins::RC4EAPOL::key(const uint8_t *new_key, uint32_t sz) { - delete[] _key; - _key = new uint8_t[sz]; - _key_size = sz; - std::memcpy(_key, new_key, sz); +void RC4EAPOL::key(const key_type &new_key) { + _key = new_key; } -uint32_t Tins::RC4EAPOL::header_size() const { - return sizeof(eapolhdr) + sizeof(_header) + _key_size; +uint32_t RC4EAPOL::header_size() const { + return sizeof(eapolhdr) + sizeof(_header) + _key.size(); } -void Tins::RC4EAPOL::write_body(uint8_t *buffer, uint32_t total_sz) { - uint32_t sz = sizeof(_header) + _key_size; +void RC4EAPOL::write_body(uint8_t *buffer, uint32_t total_sz) { + uint32_t sz = sizeof(_header) + _key.size(); assert(total_sz >= sz); - if(_key) - _header.key_length = Utils::host_to_be(_key_size); + if(_key.size()) + _header.key_length = Utils::host_to_be(_key.size()); std::memcpy(buffer, &_header, sizeof(_header)); buffer += sizeof(_header); - if(_key) - std::memcpy(buffer, _key, _key_size); -} - -Tins::PDU *Tins::RC4EAPOL::clone_pdu() const { - RC4EAPOL *new_pdu = new RC4EAPOL(); - new_pdu->copy_fields(this); - return new_pdu; -} - -void Tins::RC4EAPOL::copy_fields(const RC4EAPOL *other) { - copy_eapol_fields(other); - std::memcpy(&_header, &other->_header, sizeof(_header)); - _key_size = other->_key_size; - if(_key_size) { - _key = new uint8_t[_key_size]; - std::memcpy(_key, other->_key, _key_size); - } - else - _key = 0; + std::copy(_key.begin(), _key.end(), buffer); } /* RSNEAPOL */ -Tins::RSNEAPOL::RSNEAPOL() : EAPOL(0x03, RSN), _key(0), _key_size(0) { +RSNEAPOL::RSNEAPOL() +: EAPOL(0x03, RSN) +{ std::memset(&_header, 0, sizeof(_header)); } -Tins::RSNEAPOL::RSNEAPOL(const uint8_t *buffer, uint32_t total_sz) : EAPOL(0x03, RSN), _key_size(0) { +RSNEAPOL::RSNEAPOL(const uint8_t *buffer, uint32_t total_sz) +: EAPOL(0x03, RSN) +{ buffer += sizeof(eapolhdr); total_sz -= sizeof(eapolhdr); if(total_sz < sizeof(_header)) @@ -205,109 +173,68 @@ Tins::RSNEAPOL::RSNEAPOL(const uint8_t *buffer, uint32_t total_sz) : EAPOL(0x03, std::memcpy(&_header, buffer, sizeof(_header)); buffer += sizeof(_header); total_sz -= sizeof(_header); - if(total_sz == wpa_length()) { - _key = new uint8_t[total_sz]; - _key_size = total_sz; - std::memcpy(_key, buffer, total_sz); - } - else - _key = 0; + if(total_sz == wpa_length()) + _key.assign(buffer, buffer + total_sz); } -Tins::RSNEAPOL::RSNEAPOL(const RSNEAPOL &other) : EAPOL(other) { - copy_fields(&other); -} - -Tins::RSNEAPOL &Tins::RSNEAPOL::operator= (const RSNEAPOL &other) { - copy_fields(&other); - copy_inner_pdu(other); - return *this; -} - -Tins::RSNEAPOL::~RSNEAPOL() { - delete[] _key; -} - -void Tins::RSNEAPOL::RSNEAPOL::nonce(const uint8_t *new_nonce) { +void RSNEAPOL::RSNEAPOL::nonce(const uint8_t *new_nonce) { std::memcpy(_header.nonce, new_nonce, sizeof(_header.nonce)); } -void Tins::RSNEAPOL::rsc(uint64_t new_rsc) { +void RSNEAPOL::rsc(uint64_t new_rsc) { _header.rsc = Utils::host_to_be(new_rsc); } -void Tins::RSNEAPOL::id(uint64_t new_id) { +void RSNEAPOL::id(uint64_t new_id) { _header.id = Utils::host_to_be(new_id); } -void Tins::RSNEAPOL::mic(const uint8_t *new_mic) { +void RSNEAPOL::mic(const uint8_t *new_mic) { std::memcpy(_header.mic, new_mic, sizeof(_header.mic)); } -void Tins::RSNEAPOL::wpa_length(uint16_t new_wpa_length) { +void RSNEAPOL::wpa_length(uint16_t new_wpa_length) { _header.wpa_length = Utils::host_to_be(new_wpa_length); } -void Tins::RSNEAPOL::key(const uint8_t *new_key, uint32_t sz) { - delete[] _key; - _key = new uint8_t[sz]; - _key_size = sz; - _header.key_type = 0; - std::memcpy(_key, new_key, sz); +void RSNEAPOL::key(const key_type &new_key) { + _key = new_key; + _header.key_t = 0; } -void Tins::RSNEAPOL::rsn_information(const RSNInformation &rsn) { - _key = rsn.serialize(_key_size); - _header.key_type = 1; +void RSNEAPOL::rsn_information(const RSNInformation &rsn) { + _key = rsn.serialize(); + _header.key_t = 1; } -uint32_t Tins::RSNEAPOL::header_size() const { +uint32_t RSNEAPOL::header_size() const { uint32_t padding(0); - if(_header.key_type && _key_size) + if(_header.key_t && _key.size()) padding = 2; - return sizeof(eapolhdr) + sizeof(_header) + _key_size + padding; + return sizeof(eapolhdr) + sizeof(_header) + _key.size() + padding; } -void Tins::RSNEAPOL::write_body(uint8_t *buffer, uint32_t total_sz) { +void RSNEAPOL::write_body(uint8_t *buffer, uint32_t total_sz) { uint32_t sz = header_size() - sizeof(eapolhdr); assert(total_sz >= sz); - if(_key) { - if(!_header.key_type) { + if(_key.size()) { + if(!_header.key_t) { _header.key_length = Utils::host_to_be(32); - wpa_length(_key_size); + wpa_length(_key.size()); } - else if(_key_size) { + else if(_key.size()) { _header.key_length = 0; - wpa_length(_key_size + 2); + wpa_length(_key.size() + 2); } else wpa_length(0); } std::memcpy(buffer, &_header, sizeof(_header)); buffer += sizeof(_header); - if(_key) { - if(_header.key_type && _key_size) { - *(buffer++) = Dot11::RSN; - *(buffer++) = _key_size; - } - std::memcpy(buffer, _key, _key_size); + if(_header.key_t && _key.size()) { + *(buffer++) = Dot11::RSN; + *(buffer++) = _key.size(); } + std::copy(_key.begin(), _key.end(), buffer); } - -void Tins::RSNEAPOL::copy_fields(const RSNEAPOL *other) { - copy_eapol_fields(other); - std::memcpy(&_header, &other->_header, sizeof(_header)); - _key_size = other->_key_size; - if(_key_size) { - _key = new uint8_t[_key_size]; - std::memcpy(_key, other->_key, _key_size); - } - else - _key = 0; -} - -Tins::PDU *Tins::RSNEAPOL::clone_pdu() const { - RSNEAPOL *new_pdu = new RSNEAPOL(); - new_pdu->copy_fields(this); - return new_pdu; } diff --git a/src/ip.cpp b/src/ip.cpp index b05f6d4..5b8ae69 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -38,9 +38,11 @@ using namespace std; -const uint8_t Tins::IP::DEFAULT_TTL = 128; +namespace Tins { -Tins::IP::IP(address_type ip_dst, address_type ip_src, PDU *child) +const uint8_t IP::DEFAULT_TTL = 128; + +IP::IP(address_type ip_dst, address_type ip_src, PDU *child) : PDU(Constants::IP::PROTO_IP, child) { init_ip_fields(); @@ -48,7 +50,7 @@ Tins::IP::IP(address_type ip_dst, address_type ip_src, PDU *child) this->src_addr(ip_src); } -Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz) +IP::IP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PROTO_IP) { static const char *msg("Not enough size for an IP header in the buffer."); @@ -128,7 +130,7 @@ Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz) } } -void Tins::IP::init_ip_fields() { +void IP::init_ip_fields() { memset(&_ip, 0, sizeof(iphdr)); this->_ip.version = 4; this->ttl(DEFAULT_TTL); @@ -139,65 +141,65 @@ void Tins::IP::init_ip_fields() { /* Setters */ -void Tins::IP::tos(uint8_t new_tos) { +void IP::tos(uint8_t new_tos) { _ip.tos = new_tos; } -void Tins::IP::tot_len(uint16_t new_tot_len) { +void IP::tot_len(uint16_t new_tot_len) { _ip.tot_len = Utils::host_to_be(new_tot_len); } -void Tins::IP::id(uint16_t new_id) { +void IP::id(uint16_t new_id) { _ip.id = Utils::host_to_be(new_id); } -void Tins::IP::frag_off(uint16_t new_frag_off) { +void IP::frag_off(uint16_t new_frag_off) { _ip.frag_off = Utils::host_to_be(new_frag_off); } -void Tins::IP::ttl(uint8_t new_ttl) { +void IP::ttl(uint8_t new_ttl) { _ip.ttl = new_ttl; } -void Tins::IP::protocol(uint8_t new_protocol) { +void IP::protocol(uint8_t new_protocol) { _ip.protocol = new_protocol; } -void Tins::IP::check(uint16_t new_check) { +void IP::check(uint16_t new_check) { _ip.check = Utils::host_to_be(new_check); } -void Tins::IP::src_addr(address_type ip) { +void IP::src_addr(address_type ip) { _ip.saddr = ip; } -void Tins::IP::dst_addr(address_type ip) { +void IP::dst_addr(address_type ip) { _ip.daddr = ip; } -void Tins::IP::head_len(uint8_t new_head_len) { +void IP::head_len(small_uint<4> new_head_len) { _ip.ihl = new_head_len; } -void Tins::IP::version(uint8_t ver) { +void IP::version(small_uint<4> ver) { _ip.version = ver; } -void Tins::IP::set_eol_option() { +void IP::set_eol_option() { this->set_option(0, IP::CONTROL, IP::END); } -void Tins::IP::set_noop_option() { +void IP::set_noop_option() { this->set_option(0, IP::CONTROL, IP::NOOP); } -void Tins::IP::set_sec_option(const uint8_t* data, uint32_t data_len) { +void IP::set_sec_option(const uint8_t* data, uint32_t data_len) { this->set_option(1, IP::CONTROL, IP::SEC, data, data_len); } -void Tins::IP::set_option(uint8_t copied, +void IP::set_option(uint8_t copied, OptionClass op_class, Option number, const uint8_t* data, @@ -220,7 +222,7 @@ void Tins::IP::set_option(uint8_t copied, _padded_options_size = padding ? (_options_size - padding + 4) : _options_size; } -const Tins::IP::IPOption *Tins::IP::search_option(OptionClass opt_class, Option opt_number) const { +const IP::IPOption *IP::search_option(OptionClass opt_class, Option opt_number) const { for(std::list::const_iterator it = _ip_options.begin(); it != _ip_options.end(); ++it) { if(it->type.op_class == (uint8_t)opt_class && it->type.number == (uint8_t)opt_number) return &(*it); @@ -228,7 +230,7 @@ const Tins::IP::IPOption *Tins::IP::search_option(OptionClass opt_class, Option return 0; } -uint8_t* Tins::IP::IPOption::write(uint8_t* buffer) { +uint8_t* IP::IPOption::write(uint8_t* buffer) { memcpy(buffer, &type, 1); buffer += 1; if (!optional_data.empty()) { @@ -238,21 +240,21 @@ uint8_t* Tins::IP::IPOption::write(uint8_t* buffer) { return buffer; } -const uint8_t* Tins::IP::IPOption::data_ptr() const { +const uint8_t* IP::IPOption::data_ptr() const { return !optional_data.empty() ? (&optional_data[1]) : 0; } -uint8_t Tins::IP::IPOption::data_size() const { +uint8_t IP::IPOption::data_size() const { return !optional_data.empty() ? (optional_data.size() - 1) : 0; } /* Virtual method overriding. */ -uint32_t Tins::IP::header_size() const { +uint32_t IP::header_size() const { return sizeof(iphdr) + _padded_options_size; } -bool Tins::IP::send(PacketSender* sender) { +bool IP::send(PacketSender* sender) { struct sockaddr_in link_addr; PacketSender::SocketType type = PacketSender::IP_SOCKET; link_addr.sin_family = AF_INET; @@ -264,7 +266,7 @@ bool Tins::IP::send(PacketSender* sender) { return sender->send_l3(this, (struct sockaddr*)&link_addr, sizeof(link_addr), type); } -Tins::PDU *Tins::IP::recv_response(PacketSender *sender) { +PDU *IP::recv_response(PacketSender *sender) { struct sockaddr_in link_addr; PacketSender::SocketType type = PacketSender::IP_SOCKET; link_addr.sin_family = AF_INET; @@ -276,7 +278,7 @@ Tins::PDU *Tins::IP::recv_response(PacketSender *sender) { return sender->recv_l3(this, (struct sockaddr*)&link_addr, sizeof(link_addr), type); } -void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU* parent) { +void IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU* parent) { uint32_t my_sz = header_size(); assert(total_sz >= my_sz); if(inner_pdu()) { @@ -308,7 +310,7 @@ void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU } } -bool Tins::IP::matches_response(uint8_t *ptr, uint32_t total_sz) { +bool IP::matches_response(uint8_t *ptr, uint32_t total_sz) { if(total_sz < sizeof(iphdr)) return false; iphdr *ip_ptr = (iphdr*)ptr; @@ -319,7 +321,7 @@ bool Tins::IP::matches_response(uint8_t *ptr, uint32_t total_sz) { return false; } -Tins::PDU *Tins::IP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { +PDU *IP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { if(total_sz < sizeof(iphdr)) return 0; const iphdr *ip_ptr = (iphdr*)ptr; @@ -335,3 +337,4 @@ Tins::PDU *Tins::IP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { cloned->inner_pdu(child); return cloned; } +} diff --git a/src/rawpdu.cpp b/src/rawpdu.cpp index f2946e0..30b6fa4 100644 --- a/src/rawpdu.cpp +++ b/src/rawpdu.cpp @@ -20,30 +20,27 @@ */ #include -#include +#include #include "rawpdu.h" -Tins::RawPDU::RawPDU(const uint8_t *pload, uint32_t size) : PDU(255), _payload_size(size), _owns_payload(true) { - _payload = new uint8_t[size]; - std::memcpy(_payload, pload, size); -} - -Tins::RawPDU::RawPDU(uint8_t *pload, uint32_t size) : PDU(255), _payload(pload), _payload_size(size), _owns_payload(false) { +namespace Tins { +RawPDU::RawPDU(const uint8_t *pload, uint32_t size) +: PDU(255), _payload(pload, pload + size) +{ } -Tins::RawPDU::~RawPDU() { - if(_owns_payload) - delete[] _payload; +uint32_t RawPDU::header_size() const { + return _payload.size(); } -uint32_t Tins::RawPDU::header_size() const { - return _payload_size; +void RawPDU::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) { + assert(total_sz >= _payload.size()); + std::copy(_payload.begin(), _payload.end(), buffer); } -void Tins::RawPDU::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) { - assert(total_sz >= _payload_size); - std::memcpy(buffer, _payload, _payload_size); +void RawPDU::payload(const payload_type &pload) { + _payload = pload; +} } - diff --git a/src/tcp.cpp b/src/tcp.cpp index f46cf07..7cacd89 100644 --- a/src/tcp.cpp +++ b/src/tcp.cpp @@ -120,7 +120,7 @@ void TCP::payload(uint8_t *new_payload, uint32_t new_payload_size) { inner_pdu(new RawPDU(new_payload, new_payload_size)); } -void TCP::data_offset(uint8_t new_doff) { +void TCP::data_offset(small_uint<4> new_doff) { this->_tcp.doff = new_doff; } @@ -201,7 +201,7 @@ bool TCP::search_altchecksum_option(uint8_t *value) { return generic_search(ALTCHK, value); } -uint8_t TCP::get_flag(Flags tcp_flag) { +small_uint<1> TCP::get_flag(Flags tcp_flag) { switch(tcp_flag) { case FIN: return _tcp.fin; @@ -233,7 +233,7 @@ uint8_t TCP::get_flag(Flags tcp_flag) { }; } -void TCP::set_flag(Flags tcp_flag, uint8_t value) { +void TCP::set_flag(Flags tcp_flag, small_uint<1> value) { switch(tcp_flag) { case FIN: _tcp.fin = value; diff --git a/src/utils.cpp b/src/utils.cpp index 4725df1..11ee8df 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -122,7 +122,7 @@ bool Utils::resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip, std::auto_ptr packet(ARP::make_arp_request(iface, ip, info.ip_addr, info.hw_addr)); std::auto_ptr response(sender->send_recv(packet.get())); if(response.get()) { - ARP *arp_resp = response->find_inner_pdu(); + ARP *arp_resp = response->find_pdu(); if(arp_resp) *address = arp_resp->sender_hw_addr(); return arp_resp; diff --git a/tests/src/dot11/ack.cpp b/tests/src/dot11/ack.cpp index 6a121af..d191dc0 100644 --- a/tests/src/dot11/ack.cpp +++ b/tests/src/dot11/ack.cpp @@ -90,7 +90,7 @@ TEST_F(Dot11AckTest, ClonePDU) { TEST_F(Dot11AckTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11Ack *inner = dot11->find_inner_pdu(); + const Dot11Ack *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/assoc_request.cpp b/tests/src/dot11/assoc_request.cpp index 4f06b6e..d5440d5 100644 --- a/tests/src/dot11/assoc_request.cpp +++ b/tests/src/dot11/assoc_request.cpp @@ -80,7 +80,7 @@ TEST_F(Dot11AssocRequestTest, ClonePDU) { TEST_F(Dot11AssocRequestTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11AssocRequest *inner = dot11->find_inner_pdu(); + const Dot11AssocRequest *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/assoc_response.cpp b/tests/src/dot11/assoc_response.cpp index fde6694..e17bab5 100644 --- a/tests/src/dot11/assoc_response.cpp +++ b/tests/src/dot11/assoc_response.cpp @@ -89,7 +89,7 @@ TEST_F(Dot11AssocResponseTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); std::cout << (int)dot11->pdu_type() << std::endl; - const Dot11AssocResponse *inner = dot11->find_inner_pdu(); + const Dot11AssocResponse *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/authentication.cpp b/tests/src/dot11/authentication.cpp index cdf4fb9..7d69535 100644 --- a/tests/src/dot11/authentication.cpp +++ b/tests/src/dot11/authentication.cpp @@ -96,7 +96,7 @@ TEST_F(Dot11AuthenticationTest, ClonePDU) { TEST_F(Dot11AuthenticationTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11Authentication *inner = dot11->find_inner_pdu(); + const Dot11Authentication *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/beacon.cpp b/tests/src/dot11/beacon.cpp index 6b15d2f..b72aa4f 100644 --- a/tests/src/dot11/beacon.cpp +++ b/tests/src/dot11/beacon.cpp @@ -99,7 +99,7 @@ TEST_F(Dot11BeaconTest, CopyAssignmentOperator) { TEST_F(Dot11BeaconTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11Beacon *beacon = dot11->find_inner_pdu(); + const Dot11Beacon *beacon = dot11->find_pdu(); ASSERT_TRUE(beacon); test_equals_expected(*beacon); } @@ -355,6 +355,24 @@ TEST_F(Dot11BeaconTest, ChallengeText) { EXPECT_EQ(dot11.challenge_text(), "libtins ftw"); } +TEST_F(Dot11BeaconTest, RSNInformationTest) { + Dot11Beacon dot11; + RSNInformation rsn_info, found; + rsn_info.add_pairwise_cypher(RSNInformation::WEP_40); + rsn_info.add_akm_cypher(RSNInformation::PSK); + rsn_info.group_suite(RSNInformation::CCMP); + rsn_info.version(0x7283); + rsn_info.capabilities(0x18ad); + dot11.rsn_information(rsn_info); + found = dot11.rsn_information(); + + EXPECT_EQ(rsn_info.version(), found.version()); + EXPECT_EQ(rsn_info.capabilities(), found.capabilities()); + EXPECT_EQ(rsn_info.group_suite(), found.group_suite()); + EXPECT_EQ(rsn_info.pairwise_cyphers(), found.pairwise_cyphers()); + EXPECT_EQ(rsn_info.akm_cyphers(), found.akm_cyphers()); +} + TEST_F(Dot11BeaconTest, PCAPLoad1) { const uint8_t buffer[] = { diff --git a/tests/src/dot11/cfend.cpp b/tests/src/dot11/cfend.cpp index e026e76..b812dd2 100644 --- a/tests/src/dot11/cfend.cpp +++ b/tests/src/dot11/cfend.cpp @@ -67,7 +67,7 @@ TEST_F(Dot11CFEndTest, ClonePDU) { TEST_F(Dot11CFEndTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11CFEnd *inner = dot11->find_inner_pdu(); + const Dot11CFEnd *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/cfendack.cpp b/tests/src/dot11/cfendack.cpp index f6d6fc9..b7b6215 100644 --- a/tests/src/dot11/cfendack.cpp +++ b/tests/src/dot11/cfendack.cpp @@ -67,7 +67,7 @@ TEST_F(Dot11EndCFAckTest, ClonePDU) { TEST_F(Dot11EndCFAckTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11EndCFAck *inner = dot11->find_inner_pdu(); + const Dot11EndCFAck *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/data.cpp b/tests/src/dot11/data.cpp index 3e576c5..10e96f2 100644 --- a/tests/src/dot11/data.cpp +++ b/tests/src/dot11/data.cpp @@ -56,7 +56,7 @@ TEST_F(Dot11DataTest, ClonePDU) { TEST_F(Dot11DataTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11Data *inner = dot11->find_inner_pdu(); + const Dot11Data *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/deauthentication.cpp b/tests/src/dot11/deauthentication.cpp index 6c58a75..b6dfc8f 100644 --- a/tests/src/dot11/deauthentication.cpp +++ b/tests/src/dot11/deauthentication.cpp @@ -78,7 +78,7 @@ TEST_F(Dot11DeauthenticationTest, ClonePDU) { TEST_F(Dot11DeauthenticationTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11Deauthentication *inner = dot11->find_inner_pdu(); + const Dot11Deauthentication *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/disassoc.cpp b/tests/src/dot11/disassoc.cpp index 309f1b5..83caf66 100644 --- a/tests/src/dot11/disassoc.cpp +++ b/tests/src/dot11/disassoc.cpp @@ -78,7 +78,7 @@ TEST_F(Dot11DisassocTest, ClonePDU) { TEST_F(Dot11DisassocTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11Disassoc *inner = dot11->find_inner_pdu(); + const Dot11Disassoc *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/probe_request.cpp b/tests/src/dot11/probe_request.cpp index 7763ece..c2022eb 100644 --- a/tests/src/dot11/probe_request.cpp +++ b/tests/src/dot11/probe_request.cpp @@ -68,7 +68,7 @@ TEST_F(Dot11ProbeRequestTest, ClonePDU) { TEST_F(Dot11ProbeRequestTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11ProbeRequest *inner = dot11->find_inner_pdu(); + const Dot11ProbeRequest *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/probe_response.cpp b/tests/src/dot11/probe_response.cpp index 759c412..bbe7ad6 100644 --- a/tests/src/dot11/probe_response.cpp +++ b/tests/src/dot11/probe_response.cpp @@ -88,7 +88,7 @@ TEST_F(Dot11ProbeResponseTest, ClonePDU) { TEST_F(Dot11ProbeResponseTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11ProbeResponse *inner = dot11->find_inner_pdu(); + const Dot11ProbeResponse *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/pspoll.cpp b/tests/src/dot11/pspoll.cpp index adc534b..3f59932 100644 --- a/tests/src/dot11/pspoll.cpp +++ b/tests/src/dot11/pspoll.cpp @@ -67,7 +67,7 @@ TEST_F(Dot11PSPollTest, ClonePDU) { TEST_F(Dot11PSPollTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11PSPoll *inner = dot11->find_inner_pdu(); + const Dot11PSPoll *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/reassoc_request.cpp b/tests/src/dot11/reassoc_request.cpp index f8e693f..971ddad 100644 --- a/tests/src/dot11/reassoc_request.cpp +++ b/tests/src/dot11/reassoc_request.cpp @@ -87,7 +87,7 @@ TEST_F(Dot11ReAssocRequestTest, ClonePDU) { TEST_F(Dot11ReAssocRequestTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11ReAssocRequest *inner = dot11->find_inner_pdu(); + const Dot11ReAssocRequest *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/reassoc_response.cpp b/tests/src/dot11/reassoc_response.cpp index 2ea73b9..4fc2e9f 100644 --- a/tests/src/dot11/reassoc_response.cpp +++ b/tests/src/dot11/reassoc_response.cpp @@ -76,7 +76,7 @@ TEST_F(Dot11ReAssocResponseTest, ClonePDU) { TEST_F(Dot11ReAssocResponseTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11ReAssocResponse *inner = dot11->find_inner_pdu(); + const Dot11ReAssocResponse *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); } diff --git a/tests/src/dot11/rts.cpp b/tests/src/dot11/rts.cpp index 91f6994..c8b69b0 100644 --- a/tests/src/dot11/rts.cpp +++ b/tests/src/dot11/rts.cpp @@ -67,7 +67,7 @@ TEST_F(Dot11RTSTest, ClonePDU) { TEST_F(Dot11RTSTest, FromBytes) { std::auto_ptr dot11(Dot11::from_bytes(expected_packet, sizeof(expected_packet))); ASSERT_TRUE(dot11.get()); - const Dot11RTS *inner = dot11->find_inner_pdu(); + const Dot11RTS *inner = dot11->find_pdu(); ASSERT_TRUE(inner); test_equals_expected(*inner); }