From d7f1a590922569705c95347e7b65c109973474ba Mon Sep 17 00:00:00 2001 From: Santiago Alessandri Date: Tue, 23 Aug 2011 09:44:42 -0300 Subject: [PATCH] Finished getters and setters for Beacon in IEEE802.11. Added a new constructor --- include/ieee802-11.h | 338 ++++++++++++++++++++++++++++++++++++++++--- include/utils.h | 87 +++++------ src/ieee802-11.cpp | 23 ++- 3 files changed, 387 insertions(+), 61 deletions(-) diff --git a/include/ieee802-11.h b/include/ieee802-11.h index 217c9e4..4088535 100644 --- a/include/ieee802-11.h +++ b/include/ieee802-11.h @@ -574,24 +574,60 @@ namespace Tins { protected: ManagementFrame(); + ManagementFrame(const std::string& iface, const uint8_t* dst_hw_addr, const uint8_t* src_hw_addr) throw (std::runtime_error); struct CapabilityInformation { - unsigned int ess:1; - unsigned int ibss:1; - unsigned int cf_poll:1; - unsigned int cf_poll_req:1; - unsigned int privacy:1; - unsigned int short_preamble:1; - unsigned int pbcc:1; - unsigned int chanel_agility:1; - unsigned int spectrum_mgmt:1; - unsigned int qos:1; - unsigned int sst:1; - unsigned int apsd:1; - unsigned int reserved:1; - unsigned int dsss_ofdm:1; - unsigned int delayed_block_ack:1; - unsigned int immediate_block_ack:1; + unsigned int _ess:1; + unsigned int _ibss:1; + unsigned int _cf_poll:1; + unsigned int _cf_poll_req:1; + unsigned int _privacy:1; + unsigned int _short_preamble:1; + unsigned int _pbcc:1; + unsigned int _channel_agility:1; + unsigned int _spectrum_mgmt:1; + unsigned int _qos:1; + unsigned int _sst:1; + unsigned int _apsd:1; + unsigned int _reserved:1; + unsigned int _dsss_ofdm:1; + unsigned int _delayed_block_ack:1; + unsigned int _immediate_block_ack:1; + + inline bool ess() const { return this->_ess; } + inline bool ibss() const { return this->_ibss; } + inline bool cf_poll() const { return this->_cf_poll; } + inline bool cf_poll_req() const { return this->_cf_poll_req; } + inline bool privacy() const { return this->_privacy; } + inline bool short_preamble() const { return this->_short_preamble; } + inline bool pbcc() const { return this->_pbcc; } + inline bool channel_agility() const { return this->_channel_agility; } + inline bool spectrum_mgmt() const { return this->_spectrum_mgmt; } + inline bool qos() const { return this->_qos; } + inline bool sst() const { return this->_sst; } + inline bool apsd() const { return this->_apsd; } + inline bool reserved() const { return this->_reserved; } + inline bool dsss_ofdm() const { return this->_dsss_ofdm; } + inline bool delayed_block_ack() const { return this->_delayed_block_ack; } + inline bool immediate_block_ack() const { return this->_immediate_block_ack; } + + void ess(bool new_value) { this->_ess = new_value; } + void ibss(bool new_value) { this->_ibss = new_value; } + void cf_poll(bool new_value) { this->_cf_poll = new_value; } + void cf_poll_req(bool new_value) { this->_cf_poll_req = new_value; } + void privacy(bool new_value) { this->_privacy = new_value; } + void short_preamble(bool new_value) { this->_short_preamble = new_value; } + void pbcc(bool new_value) { this->_pbcc = new_value; } + void channel_agility(bool new_value) { this->_channel_agility = new_value; } + void spectrum_mgmt(bool new_value) { this->_spectrum_mgmt = new_value; } + void qos(bool new_value) { this->_qos = new_value; } + void sst(bool new_value) { this->_sst = new_value; } + void apsd(bool new_value) { this->_apsd = new_value; } + void reserved(bool new_value) { this->_reserved = new_value; } + void dsss_ofdm(bool new_value) { this->_dsss_ofdm = new_value; } + void delayed_block_ack(bool new_value) { this->_delayed_block_ack = new_value; } + void immediate_block_ack(bool new_value) { this->_immediate_block_ack = new_value; } + } __attribute__((__packed__)); private: @@ -613,24 +649,288 @@ namespace Tins { */ IEEE802_11_Beacon(); + /** + * \brief Constructor for creating a 802.11 Beacon. + * + * Constructor that builds a 802.11 Beacon 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 src_hw_addr uint8_t array of 6 bytes containing the source's MAC(optional). + */ + IEEE802_11_Beacon(const std::string& iface, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0) throw (std::runtime_error); + + /** + * \brief Getter for the timestamp field. + * + * \return Timestamp value in an uint64_t. + */ + inline uint64_t timestamp() const { return this->_body.timestamp; } + + /** + * \brief Getter for the interval field. + * + * \return Timestamp value in an uint64_t. + */ + inline uint16_t interval() const { return Utils::net_to_host_s(this->_body.interval); } + + /** + * \brief Getter for the ess flag. + * + * \return Bool indicating the flag's value. + */ + inline bool ess() const { return this->_body.capability.ess(); } + + /** + * \brief Getter for the ibss flag. + * + * \return Bool indicating the flag's value. + */ + inline bool ibss() const { return this->_body.capability.ibss(); } + + /** + * \brief Getter for the cf_poll flag. + * + * \return Bool indicating the flag's value. + */ + inline bool cf_poll() const { return this->_body.capability.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->_body.capability.cf_poll_req(); } + + /** + * \brief Getter for the privacy flag. + * + * \return Bool indicating the flag's value. + */ + inline bool privacy() const { return this->_body.capability.privacy(); } + + /** + * \brief Getter for the short_preamble flag. + * + * \return Bool indicating the flag's value. + */ + inline bool short_preamble() const { return this->_body.capability.short_preamble(); } + + /** + * \brief Getter for the pbcc flag. + * + * \return Bool indicating the flag's value. + */ + inline bool pbcc() const { return this->_body.capability.pbcc(); } + + /** + * \brief Getter for the channel_agility flag. + * + * \return Bool indicating the flag's value. + */ + inline bool channel_agility() const { return this->_body.capability.channel_agility(); } + + /** + * \brief Getter for the spectrum_mgmt flag. + * + * \return Bool indicating the flag's value. + */ + inline bool spectrum_mgmt() const { return this->_body.capability.spectrum_mgmt(); } + + /** + * \brief Getter for the qos flag. + * + * \return Bool indicating the flag's value. + */ + inline bool qos() const { return this->_body.capability.qos(); } + + /** + * \brief Getter for the sst flag. + * + * \return Bool indicating the flag's value. + */ + inline bool sst() const { return this->_body.capability.sst(); } + + /** + * \brief Getter for the apsd flag. + * + * \return Bool indicating the flag's value. + */ + inline bool apsd() const { return this->_body.capability.apsd(); } + + /** + * \brief Getter for the reserved flag. + * + * \return Bool indicating the flag's value. + */ + inline bool reserved() const { return this->_body.capability.reserved(); } + + /** + * \brief Getter for the dsss_ofdm flag. + * + * \return Bool indicating the flag's value. + */ + inline bool dsss_ofdm() const { return this->_body.capability.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->_body.capability.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->_body.capability.immediate_block_ack(); } + + /** + * \brief Setter for the ess flag. + * + * \param new_value bool indicating the flag's new value. + */ + void ess(bool new_value) { this->_body.capability.ess(new_value); } + + /** + * \brief Setter for the ibss flag. + * + * \param new_value bool indicating the flag's new value. + */ + void ibss(bool new_value) { this->_body.capability.ibss(new_value); } + + /** + * \brief Setter for the cf_poll flag. + * + * \param new_value bool indicating the flag's new value. + */ + void cf_poll(bool new_value) { this->_body.capability.cf_poll(new_value); } + + /** + * \brief Setter for the cf_poll_req flag. + * + * \param new_value bool indicating the flag's new value. + */ + void cf_poll_req(bool new_value) { this->_body.capability.cf_poll_req(new_value); } + + /** + * \brief Setter for the privacy flag. + * + * \param new_value bool indicating the flag's new value. + */ + void privacy(bool new_value) { this->_body.capability.privacy(new_value); } + + /** + * \brief Setter for the short_preamble flag. + * + * \param new_value bool indicating the flag's new value. + */ + void short_preamble(bool new_value) { this->_body.capability.short_preamble(new_value); } + + /** + * \brief Setter for the pbcc flag. + * + * \param new_value bool indicating the flag's new value. + */ + void pbcc(bool new_value) { this->_body.capability.pbcc(new_value); } + + /** + * \brief Setter for the channel_agility flag. + * + * \param new_value bool indicating the flag's new value. + */ + void channel_agility(bool new_value) { this->_body.capability.channel_agility(new_value); } + + /** + * \brief Setter for the spectrum_mgmt flag. + * + * \param new_value bool indicating the flag's new value. + */ + void spectrum_mgmt(bool new_value) { this->_body.capability.spectrum_mgmt(new_value); } + + /** + * \brief Setter for the qos flag. + * + * \param new_value bool indicating the flag's new value. + */ + void qos(bool new_value) { this->_body.capability.qos(new_value); } + + /** + * \brief Setter for the sst flag. + * + * \param new_value bool indicating the flag's new value. + */ + void sst(bool new_value) { this->_body.capability.sst(new_value); } + + /** + * \brief Setter for the apsd flag. + * + * \param new_value bool indicating the flag's new value. + */ + void apsd(bool new_value) { this->_body.capability.apsd(new_value); } + + /** + * \brief Setter for the reserved flag. + * + * \param new_value bool indicating the flag's new value. + */ + void reserved(bool new_value) { this->_body.capability.reserved(new_value); } + + /** + * \brief Setter for the dsss_ofdm flag. + * + * \param new_value bool indicating the flag's new value. + */ + void dsss_ofdm(bool new_value) { this->_body.capability.dsss_ofdm(new_value); } + + /** + * \brief Setter for the delayed_block_ack flag. + * + * \param new_value bool indicating the flag's new value. + */ + void delayed_block_ack(bool new_value) { this->_body.capability.delayed_block_ack(new_value); } + + /** + * \brief Setter for the immediate_block_ack flag. + * + * \param new_value bool indicating the flag's new value. + */ + void immediate_block_ack(bool new_value) { this->_body.capability.immediate_block_ack(new_value); } + + /** + * \brief Setter for the timestamp field. + * + * \param new_timestamp uint64_t with the timestamp to set. + */ + void timestamp(uint64_t new_timestamp); + + /** + * \brief Setter for the interval field. + * + * \param new_interval uint16_t with the interval to set. + */ + void interval(uint16_t new_interval); + /** * \brief Helper method to set the essid. * \param new_essid The essid to be set. */ void essid(const std::string &new_essid); - + /** * \brief Helper method to set the supported rates. * \param new_rates A list of rates to be set. */ void rates(const std::list &new_rates); - + /** * \brief Helper method to set the current channel. * \param new_channel The new channel to be set. */ void channel(uint8_t new_channel); - + /** * \brief Returns the frame's header length. * diff --git a/include/utils.h b/include/utils.h index 41da69e..4ab9bed 100644 --- a/include/utils.h +++ b/include/utils.h @@ -33,53 +33,53 @@ namespace Tins { /** \brief Network utils namespace. - * + * * This namespace provides utils to convert between integer IP addresses * and dotted notation strings, hw addresses, "net to host" integer * conversions, interface listing, etc. */ namespace Utils { /** \brief Convert a dotted-ip-notation string to an integer. - * + * * \param ip A dotted ip notation string - */ + */ uint32_t ip_to_int(const std::string &ip); - + /** \brief Convert an integer ip to a dotted-ip-notation string. - * + * * \param ip An integer ip. - */ + */ std::string ip_to_string(uint32_t ip); - + /** \brief Converts a hardware address string into a byte array. - * + * * The hardware address must be formatted using the notation 'HH:HH:HH:HH:HH:HH'. * Where H is a hexadecimal character(0-9, a-f). - * + * * \param hw_addr The harware address string. * \param array The output buffer. It must be at least 6 bytes long. */ bool hwaddr_to_byte(const std::string &hw_addr, uint8_t *array); - + /** \brief Converts a byte array representing a hardware address * into a string. - * + * * The input buffer must be at least 6 bytes long. * \param array The input buffer. - */ + */ std::string hwaddr_to_string(const uint8_t *array); /** \brief Resolves a domain name and returns its corresponding ip address. - * + * * If an ip address is given, its integer representation is returned. * Otherwise, the domain name is resolved and its ip address is returned. - * + * * \param to_resolve The domain name/ip address to resolve. - */ + */ uint32_t resolve_ip(const std::string &to_resolve); /** \brief Resolves the hardware address for a given ip. - * + * * \param iface The interface in which the packet will be sent. * \param ip The ip to resolve, in integer format. * \param buffer The buffer in which the host's hardware address will be stored. @@ -88,27 +88,27 @@ namespace Tins { * false otherwise. */ bool resolve_hwaddr(const std::string &iface, uint32_t ip, uint8_t *buffer, PacketSender *sender); - + /** \brief List all network interfaces. - * + * * Returns a set of strings, each of them representing the name * of a network interface. These names can be used as the input * interface for Utils::interface_ip, Utils::interface_hwaddr, etc. - */ + */ std::set network_interfaces(); - - /** + + /** * \brief Lookup the ip address of the given interface. - * + * * If the lookup fails, false will be returned, true otherwise. * \param iface The interface from which to extract the ip address. * \param ip The ip address found will be returned in this param. */ bool interface_ip(const std::string &iface, uint32_t &ip); - - /** + + /** * \brief Lookup the hardware address of the given interface. - * + * * If the lookup fails, false will be returned, true otherwise. * \param iface The interface from which to extract the hardware address. * \param buffer The hw address will be stored in this buffer. It must @@ -116,28 +116,36 @@ namespace Tins { */ bool interface_hwaddr(const std::string &iface, uint8_t *buffer); - /** + /** * \brief Lookup the interface identifier. - * + * * If the lookup fails, false will be returned, true otherwise. * \param iface The interface from which to extract the identifier. * \param id The interface id will be returned in this parameter. */ bool interface_id(const std::string &iface, uint32_t &id); - + /** * \brief Finds the gateway interface matching the given ip. - * + * * This function find the interface which would be the gateway * when sending a packet to the given ip. * \param ip The ip of the interface we are looking for. * \return The interface's name. */ std::string interface_from_ip(uint32_t ip); - - /** + + /** \brief Convert 16 bit integer into network byte order. + * + * \param data The data to convert. + */ + inline uint16_t net_to_host_s(uint16_t data) { + return ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8); + } + + /** * \brief Convert 32 bit integer into network byte order. - * + * * \param data The data to convert. */ inline uint32_t net_to_host_l(uint32_t data) { @@ -145,24 +153,21 @@ namespace Tins { ((data & 0x0000ff00) << 8) | ((data & 0x000000ff) << 24)); } - /** \brief Convert 16 bit integer into network byte order. - * - * \param data The data to convert. - */ - inline uint16_t net_to_host_s(uint16_t data) { - return ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8); + inline uint64_t net_to_host_ll(uint64_t data) { + return (((uint64_t)(net_to_host_l((uint32_t)((data << 32) >> 32))) << 32) | + (net_to_host_l(((uint32_t)(data >> 32))))); } /** \brief Returns the 32 bit crc of the given buffer. - * + * * \param data The input buffer. * \param data_size The size of the input buffer. */ uint32_t crc32(uint8_t* data, uint32_t data_size); - + /** \brief Generic function to iterate through interface and collect * data. - * + * * The parameter is applied to every interface found, allowing * the object to collect data from them. * \param functor An instance of an class which implements operator(struct ifaddrs*). diff --git a/src/ieee802-11.cpp b/src/ieee802-11.cpp index d3c9556..29e207a 100644 --- a/src/ieee802-11.cpp +++ b/src/ieee802-11.cpp @@ -191,7 +191,7 @@ void Tins::IEEE802_11::write_serialization(uint8_t *buffer, uint32_t total_sz, c buffer += 6; total_sz -= 6; } - + uint32_t child_len = write_fixed_parameters(buffer, total_sz - sizeof(ieee80211_header) - _options_size); buffer += child_len; assert(total_sz > child_len + _options_size); @@ -211,11 +211,32 @@ Tins::ManagementFrame::ManagementFrame() : IEEE802_11() { this->type(IEEE802_11::MANAGEMENT); } +Tins::ManagementFrame::ManagementFrame(const std::string& iface, + const uint8_t* dst_hw_addr, + const uint8_t* src_hw_addr) throw (std::runtime_error) : IEEE802_11(iface, dst_hw_addr, src_hw_addr) { + this->type(IEEE802_11::MANAGEMENT); +} + Tins::IEEE802_11_Beacon::IEEE802_11_Beacon() : ManagementFrame() { this->subtype(IEEE802_11::BEACON); memset(&_body, 0, sizeof(_body)); } +Tins::IEEE802_11_Beacon::IEEE802_11_Beacon(const std::string& iface, + const uint8_t* dst_hw_addr, + const uint8_t* src_hw_addr) throw (std::runtime_error) : ManagementFrame(iface, dst_hw_addr, src_hw_addr){ + this->subtype(IEEE802_11::BEACON); + memset(&_body, 0, sizeof(_body)); +} + +void Tins::IEEE802_11_Beacon::timestamp(uint64_t new_timestamp) { + this->_body.timestamp = new_timestamp; +} + +void Tins::IEEE802_11_Beacon::interval(uint16_t new_interval) { + this->_body.interval = Utils::net_to_host_s(new_interval); +} + void Tins::IEEE802_11_Beacon::essid(const std::string &new_essid) { add_tagged_option(IEEE802_11::SSID, new_essid.size(), (const uint8_t*)new_essid.c_str()); }