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

Added small_uint class.

This commit is contained in:
Matias Fontanini
2012-09-02 18:24:59 -03:00
parent fbd6ef397b
commit 958edcc74a
28 changed files with 326 additions and 361 deletions

View File

@@ -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<CypherSuites> cyphers_type;
/**
* The type used to store the AKM suites.
*/
typedef std::vector<AKMSuites> akm_type;
/**
* The type returned on serialization.
*/
typedef std::vector<uint8_t> 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<CypherSuites> &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<AKMSuites> &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<AKMSuites> _akm_cyphers;
std::list<CypherSuites> _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.

View File

@@ -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<uint8_t> 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<uint8_t> 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;
};
};

View File

@@ -29,6 +29,7 @@
#include <utility>
#include <list>
#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.

View File

@@ -187,7 +187,7 @@ namespace Tins {
* \param flag The flag which being searched.
*/
template<class T>
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)

View File

@@ -22,10 +22,9 @@
#ifndef TINS_RAWPDU_H
#define TINS_RAWPDU_H
#include <vector>
#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<uint8_t> 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<typename ForwardIterator>
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;
};
};

View File

@@ -30,6 +30,7 @@
#include <endian.h>
#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.