1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 04:54:28 +01:00

Moved endianness change functions to endianness.h.

This commit is contained in:
Matias Fontanini
2012-09-03 23:58:43 -03:00
parent 279940c398
commit 832a79a1e1
39 changed files with 482 additions and 557 deletions

View File

@@ -27,7 +27,7 @@
#include <string>
#include "pdu.h"
#include "ipaddress.h"
#include "utils.h"
#include "endianness.h"
#include "hwaddress.h"
#include "network_interface.h"
@@ -115,14 +115,14 @@ namespace Tins {
*
* \return Returns the hardware address' format in an uint16_t.
*/
uint16_t hw_addr_format() const { return Utils::be_to_host(_arp.ar_hrd); }
uint16_t hw_addr_format() const { return Endian::be_to_host(_arp.ar_hrd); }
/**
* \brief Getter for the protocol address format.
*
* \return Returns the protocol address' format in an uint16_t.
*/
uint16_t prot_addr_format() const { return Utils::be_to_host(_arp.ar_pro); }
uint16_t prot_addr_format() const { return Endian::be_to_host(_arp.ar_pro); }
/**
* \brief Getter for the hardware address length.
@@ -143,7 +143,7 @@ namespace Tins {
*
* \return Returns the ARP opcode in an uint16_t.
*/
uint16_t opcode() const { return Utils::be_to_host(_arp.ar_op); }
uint16_t opcode() const { return Endian::be_to_host(_arp.ar_op); }
/** \brief Getter for the header size.
* \return Returns the ARP header size.

View File

@@ -26,7 +26,7 @@
#include <algorithm>
#include <vector>
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
#include "ipaddress.h"
#include "hwaddress.h"
@@ -114,18 +114,18 @@ namespace Tins {
* \brief Getter for the xid field.
* \return The xid field for this BootP PDU.
*/
uint32_t xid() const { return Utils::be_to_host(_bootp.xid); }
uint32_t xid() const { return Endian::be_to_host(_bootp.xid); }
/**
* \brief Getter for the secs field.
* \return The secs field for this BootP PDU.
*/
uint16_t secs() const { return Utils::be_to_host(_bootp.secs); }
uint16_t secs() const { return Endian::be_to_host(_bootp.secs); }
/** \brief Getter for the padding field.
* \return The padding field for this BootP PDU.
*/
uint16_t padding() const { return Utils::be_to_host(_bootp.padding); }
uint16_t padding() const { return Endian::be_to_host(_bootp.padding); }
/**
* \brief Getter for the ciaddr field.

View File

@@ -29,7 +29,8 @@
#include <string>
#include <map>
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
#include "ipaddress.h"
namespace Tins {
class DNS : public PDU {
@@ -175,7 +176,7 @@ namespace Tins {
*
* \return uint16_t containing the value of the id field.
*/
uint16_t id() const { return Utils::be_to_host(dns.id); }
uint16_t id() const { return Endian::be_to_host(dns.id); }
/**
* \brief Setter for the query response field.
@@ -258,28 +259,28 @@ namespace Tins {
*
* \return uint16_t containing the value of the questions field.
*/
uint16_t questions() const { return Utils::be_to_host(dns.questions); }
uint16_t questions() const { return Endian::be_to_host(dns.questions); }
/**
* \brief Setter for the answers field.
*
* \return uint16_t containing the value of the answers field.
*/
uint16_t answers() const { return Utils::be_to_host(dns.answers); }
uint16_t answers() const { return Endian::be_to_host(dns.answers); }
/**
* \brief Setter for the authority field.
*
* \return uint16_t containing the value of the authority field.
*/
uint16_t authority() const { return Utils::be_to_host(dns.authority); }
uint16_t authority() const { return Endian::be_to_host(dns.authority); }
/**
* \brief Setter for the additional field.
*
* \return uint16_t containing the value of the additional field.
*/
uint16_t additional() const { return Utils::be_to_host(dns.additional); }
uint16_t additional() const { return Endian::be_to_host(dns.additional); }
/**
* \brief Getter for the PDU's type.

View File

@@ -29,7 +29,7 @@
#include <utility>
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
#include "network_interface.h"
#include "hwaddress.h"
#include "small_uint.h"
@@ -290,7 +290,7 @@ namespace Tins {
*
* \return The value of the duration/id field in an uint16_t.
*/
uint16_t duration_id() const { return Utils::le_to_host(_header.duration_id); }
uint16_t duration_id() const { return Endian::le_to_host(_header.duration_id); }
/**
* \brief Getter for the first address.
@@ -979,7 +979,7 @@ namespace Tins {
*
* \return The sequence number as an uint16_t.
*/
uint16_t seq_num() const { return Utils::le_to_host(_ext_header.seq_control.seq_number); }
uint16_t seq_num() const { return Endian::le_to_host(_ext_header.seq_control.seq_number); }
/**
* \brief Getter for the fourth address.
@@ -1525,14 +1525,14 @@ namespace Tins {
*
* \return Timestamp value in an uint64_t.
*/
uint64_t timestamp() const { return Utils::le_to_host(_body.timestamp); }
uint64_t timestamp() const { return Endian::le_to_host(_body.timestamp); }
/**
* \brief Getter for the interval field.
*
* \return Timestamp value in an uint16_t.
*/
uint16_t interval() const { return Utils::le_to_host(_body.interval); }
uint16_t interval() const { return Endian::le_to_host(_body.interval); }
/**
* \brief Getter for the Capabilities Information structure.
@@ -1644,7 +1644,7 @@ namespace Tins {
*
* \return uint16_t with the reason code.
*/
uint16_t reason_code() const { return Utils::le_to_host(_body.reason_code); }
uint16_t reason_code() const { return Endian::le_to_host(_body.reason_code); }
/**
* \brief Setter for the reason code.
@@ -1747,7 +1747,7 @@ namespace Tins {
*
* \return The listen interval in an uint16_t.
*/
uint16_t listen_interval() const { return Utils::le_to_host(_body.listen_interval); }
uint16_t listen_interval() const { return Endian::le_to_host(_body.listen_interval); }
/**
* \brief Setter for the listen interval.
@@ -1851,14 +1851,14 @@ namespace Tins {
*
* \return The status code in an uint16_t.
*/
uint16_t status_code() const { return Utils::le_to_host(_body.status_code); }
uint16_t status_code() const { return Endian::le_to_host(_body.status_code); }
/**
* \brief Getter for the AID field.
*
* \return The AID field value in an uint16_t.
*/
uint16_t aid() const { return Utils::le_to_host(_body.aid); }
uint16_t aid() const { return Endian::le_to_host(_body.aid); }
/**
* \brief Setter for the status code.
@@ -1970,7 +1970,7 @@ namespace Tins {
*
* \return The listen interval in an uint16_t.
*/
uint16_t listen_interval() const { return Utils::le_to_host(_body.listen_interval); }
uint16_t listen_interval() const { return Endian::le_to_host(_body.listen_interval); }
/**
* \brief Getter for the current ap field.
@@ -2089,14 +2089,14 @@ namespace Tins {
*
* \return The status code in an uint16_t.
*/
uint16_t status_code() const { return Utils::le_to_host(_body.status_code); }
uint16_t status_code() const { return Endian::le_to_host(_body.status_code); }
/**
* \brief Getter for the AID field.
*
* \return The AID field value in an uint16_t.
*/
uint16_t aid() const { return Utils::le_to_host(_body.aid); }
uint16_t aid() const { return Endian::le_to_host(_body.aid); }
/**
* \brief Setter for the status code.
@@ -2194,21 +2194,21 @@ namespace Tins {
*
* \return The authentication algorithm number in an uint16_t.
*/
uint16_t auth_algorithm() const {return Utils::le_to_host(_body.auth_algorithm); }
uint16_t auth_algorithm() const {return Endian::le_to_host(_body.auth_algorithm); }
/**
* \brief Getter for the Authetication Sequence Number.
*
* \return The authentication sequence number in an uint16_t.
*/
uint16_t auth_seq_number() const {return Utils::le_to_host(_body.auth_seq_number); }
uint16_t auth_seq_number() const {return Endian::le_to_host(_body.auth_seq_number); }
/**
* \brief Getter for the status code.
*
* \return The status code in an uint16_t.
*/
uint16_t status_code() const { return Utils::le_to_host(_body.status_code); }
uint16_t status_code() const { return Endian::le_to_host(_body.status_code); }
/**
* \brief Setter for the Authetication Algorithm Number.
@@ -2314,7 +2314,7 @@ namespace Tins {
*
* \return uint16_t with the reason code.
*/
uint16_t reason_code() const { return Utils::le_to_host(_body.reason_code); }
uint16_t reason_code() const { return Endian::le_to_host(_body.reason_code); }
/**
* \brief Setter for the reason code.
@@ -2463,14 +2463,14 @@ namespace Tins {
*
* \return Timestamp value in an uint64_t.
*/
uint64_t timestamp() const { return Utils::le_to_host(_body.timestamp); }
uint64_t timestamp() const { return Endian::le_to_host(_body.timestamp); }
/**
* \brief Getter for the interval field.
*
* \return Timestamp value in an uint16_t.
*/
uint16_t interval() const { return Utils::le_to_host(_body.interval); }
uint16_t interval() const { return Endian::le_to_host(_body.interval); }
/**
* \brief Getter for the Capabilities Information.
@@ -2591,7 +2591,7 @@ namespace Tins {
*
* \return The sequence number as an uint16_t.
*/
uint16_t seq_num() const { return Utils::le_to_host(_ext_header.seq_control.seq_number); }
uint16_t seq_num() const { return Endian::le_to_host(_ext_header.seq_control.seq_number); }
/**
* \brief Getter for the fourth address.
@@ -2725,7 +2725,7 @@ namespace Tins {
*
* \return The value of the qos_control field in an uint16_t.
*/
uint16_t qos_control() const { return Utils::le_to_host(_qos_control); }
uint16_t qos_control() const { return Endian::le_to_host(_qos_control); }
/**
* \brief Setter for the qos_control field.
@@ -3196,13 +3196,13 @@ namespace Tins {
* \brief Getter for the bar control field.
* \return The bar control field.
*/
uint16_t bar_control() const { return Utils::le_to_host(_bar_control.tid); }
uint16_t bar_control() const { return Endian::le_to_host(_bar_control.tid); }
/**
* \brief Getter for the start sequence field.
* \return The bar start sequence.
*/
uint16_t start_sequence() const { return Utils::le_to_host(_start_sequence.seq); }
uint16_t start_sequence() const { return Endian::le_to_host(_start_sequence.seq); }
/**
* \brief Getter for the fragment number field.

View File

@@ -25,7 +25,7 @@
#include "pdu.h"
#include "small_uint.h"
#include "utils.h"
#include "endianness.h"
namespace Tins {
@@ -78,7 +78,7 @@ namespace Tins {
* \brief Getter for the length field.
* \return The length field.
*/
uint16_t length() const { return Utils::be_to_host(_header.length); }
uint16_t length() const { return Endian::be_to_host(_header.length); }
/**
* \brief Getter for the type field.
@@ -198,13 +198,13 @@ namespace Tins {
* \brief Getter for the key length field.
* \return The key length field.
*/
uint16_t key_length() const { return Utils::be_to_host(_header.key_length); }
uint16_t key_length() const { return Endian::be_to_host(_header.key_length); }
/**
* \brief Getter for the replay counter field.
* \return The replay counter field.
*/
uint64_t replay_counter() const { return Utils::be_to_host(_header.replay_counter); }
uint64_t replay_counter() const { return Endian::be_to_host(_header.replay_counter); }
/**
* \brief Getter for the key IV field.
@@ -366,13 +366,13 @@ namespace Tins {
* \brief Getter for the key length field.
* \return The key length field.
*/
uint16_t key_length() const { return Utils::be_to_host(_header.key_length); }
uint16_t key_length() const { return Endian::be_to_host(_header.key_length); }
/**
* \brief Getter for the replay counter field.
* \return The replay counter field.
*/
uint64_t replay_counter() const { return Utils::be_to_host(_header.replay_counter); }
uint64_t replay_counter() const { return Endian::be_to_host(_header.replay_counter); }
/**
* \brief Getter for the key IV field.
@@ -390,13 +390,13 @@ namespace Tins {
* \brief Getter for the rsc field.
* \return The rsc field.
*/
uint64_t rsc() const { return Utils::be_to_host(_header.rsc); }
uint64_t rsc() const { return Endian::be_to_host(_header.rsc); }
/**
* \brief Getter for the id field.
* \return The id field.
*/
uint64_t id() const { return Utils::be_to_host(_header.id); }
uint64_t id() const { return Endian::be_to_host(_header.id); }
/**
* \brief Getter for the mic field.
@@ -408,7 +408,7 @@ namespace Tins {
* \brief Getter for the wpa length field.
* \return The wpa length field.
*/
uint16_t wpa_length() const { return Utils::be_to_host(_header.wpa_length); }
uint16_t wpa_length() const { return Endian::be_to_host(_header.wpa_length); }
/**
* \brief Getter for the key field.

153
include/endianness.h Normal file
View File

@@ -0,0 +1,153 @@
/*
* libtins is a net packet wrapper library for crafting and
* interpreting sniffed packets.
*
* Copyright (C) 2011 Nasel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TINS_ENDIANNESS_H
#define TINS_ENDIANNESS_H
#include <stdint.h>
#ifndef WIN32
#include <endian.h>
#endif
#include "small_uint.h"
#define TINS_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
#define TINS_IS_BIG_ENDIAN (__BYTE_ORDER == __BIG_ENDIAN)
namespace Tins {
namespace Endian {
/**
* \brief Changes a 16-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint16_t change_endian(uint16_t data) {
return ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8);
}
/**
* \brief Changes a 32-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint32_t change_endian(uint32_t data) {
return (((data & 0xff000000) >> 24) | ((data & 0x00ff0000) >> 8) |
((data & 0x0000ff00) << 8) | ((data & 0x000000ff) << 24));
}
/**
* \brief Changes a 64-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint64_t change_endian(uint64_t data) {
return (((uint64_t)(change_endian((uint32_t)((data << 32) >> 32))) << 32) |
(change_endian(((uint32_t)(data >> 32)))));
}
#if TINS_IS_LITTLE_ENDIAN
/**
* \brief Convert any integral type to big endian.
*
* \param data The data to convert.
*/
template<typename T>
inline T host_to_be(T data) {
return change_endian(data);
}
/**
* \brief Convert any integral type to little endian.
*
* On little endian platforms, the parameter is simply returned.
*
* \param data The data to convert.
*/
template<typename T>
inline T host_to_le(T data) {
return data;
}
/**
* \brief Convert any big endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T be_to_host(T data) {
return change_endian(data);
}
/**
* \brief Convert any little endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T le_to_host(T data) {
return data;
}
#elif TINS_IS_BIG_ENDIAN
/**
* \brief Convert any integral type to big endian.
*
* \param data The data to convert.
*/
template<typename T>
inline T host_to_be(T data) {
return data;
}
/**
* \brief Convert any integral type to little endian.
*
* On little endian platforms, the parameter is simply returned.
*
* \param data The data to convert.
*/
template<typename T>
inline T host_to_le(T data) {
return change_endian(data);
}
/**
* \brief Convert any big endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T be_to_host(T data) {
return data;
}
/**
* \brief Convert any little endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T le_to_host(T data) {
return change_endian(data);
}
#endif
}
}
#endif // TINS_ENDIANNESS_H

View File

@@ -26,7 +26,7 @@
#include <stdexcept>
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
#include "hwaddress.h"
#include "network_interface.h"
@@ -109,7 +109,7 @@ namespace Tins {
* \brief Getter for the payload_type
* \return The payload type.
*/
uint16_t payload_type() const { return Utils::be_to_host(_eth.payload_type); };
uint16_t payload_type() const { return Endian::be_to_host(_eth.payload_type); };
/* Setters */

View File

@@ -24,7 +24,7 @@
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
namespace Tins {
@@ -230,28 +230,28 @@ namespace Tins {
*
* \return Returns the checksum as an unit16_t.
*/
uint16_t check() const { return Utils::be_to_host(this->_icmp.check); }
uint16_t check() const { return Endian::be_to_host(this->_icmp.check); }
/**
* \brief Getter for the echo id.
*
* \return Returns the echo id.
*/
uint16_t id() const { return Utils::be_to_host(_icmp.un.echo.id); }
uint16_t id() const { return Endian::be_to_host(_icmp.un.echo.id); }
/**
* \brief Getter for the echo sequence number.
*
* \return Returns the echo sequence number.
*/
uint16_t sequence() const { return Utils::be_to_host(_icmp.un.echo.sequence); }
uint16_t sequence() const { return Endian::be_to_host(_icmp.un.echo.sequence); }
/**
* \brief Getter for the gateway field.
*
* \return Returns the gateways in an unit32_t.
*/
uint32_t gateway() const { return Utils::be_to_host(this->_icmp.un.gateway); }
uint32_t gateway() const { return Endian::be_to_host(this->_icmp.un.gateway); }
/**
* \brief Getter for the pointer field.
@@ -265,7 +265,7 @@ namespace Tins {
*
* \return Returns the mtu value in an uint16_t.
*/
uint16_t mtu() const { return Utils::be_to_host(this->_icmp.un.frag.mtu); }
uint16_t mtu() const { return Endian::be_to_host(this->_icmp.un.frag.mtu); }
/**
* \brief Returns the header size.

View File

@@ -26,7 +26,7 @@
#include <stdexcept>
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
#include "hwaddress.h"
#include "network_interface.h"
@@ -102,7 +102,7 @@ namespace Tins {
* \brief Getter for the length field.
* \return The length field value.
*/
uint16_t length() const { return Utils::be_to_host(_eth.length); };
uint16_t length() const { return Endian::be_to_host(_eth.length); };
/* Setters */

View File

@@ -31,7 +31,7 @@
#include "pdu.h"
#include "small_uint.h"
#include "ipaddress.h"
#include "utils.h"
#include "endianness.h"
namespace Tins {
@@ -174,21 +174,21 @@ namespace Tins {
*
* \return The total length of this IP PDU.
*/
uint16_t tot_len() const { return Utils::be_to_host(_ip.tot_len); }
uint16_t tot_len() const { return Endian::be_to_host(_ip.tot_len); }
/**
* \brief Getter for the id field.
*
* \return The id for this IP PDU.
*/
uint16_t id() const { return Utils::be_to_host(_ip.id); }
uint16_t id() const { return Endian::be_to_host(_ip.id); }
/**
* \brief Getter for the fragment offset field.
*
* \return The fragment offset for this IP PDU.
*/
uint16_t frag_off() const { return Utils::be_to_host(_ip.frag_off); }
uint16_t frag_off() const { return Endian::be_to_host(_ip.frag_off); }
/**
* \brief Getter for the time to live field.
@@ -209,7 +209,7 @@ namespace Tins {
*
* \return The checksum for this IP PDU.
*/
uint16_t check() const { return Utils::be_to_host(_ip.check); }
uint16_t check() const { return Endian::be_to_host(_ip.check); }
/**
* \brief Getter for the source address field.

View File

@@ -26,7 +26,7 @@
#include <utility>
#include <stdint.h>
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
namespace Tins {

View File

@@ -24,6 +24,7 @@
#include <stdexcept>
#include "pdu.h"
#include "endianness.h"
#include "network_interface.h"
namespace Tins {
@@ -185,67 +186,67 @@ namespace Tins {
* \brief Getter for the version field.
* \return The version field.
*/
inline uint8_t version() const { return _radio.it_version; }
uint8_t version() const { return _radio.it_version; }
/**
* \brief Getter for the padding field.
* \return The padding field.
*/
inline uint8_t padding() const { return _radio.it_pad; }
uint8_t padding() const { return _radio.it_pad; }
/**
* \brief Getter for the length field.
* \return The length field.
*/
inline uint8_t length() const { return _radio.it_len; }
uint8_t length() const { return _radio.it_len; }
/**
* \brief Getter for the tsft field.
* \return The tsft field.
*/
inline uint64_t tsft() const { return _tsft; }
uint64_t tsft() const { return Endian::le_to_host(_tsft); }
/**
* \brief Getter for the flags field.
* \return The flags field.
*/
inline FrameFlags flags() const { return (FrameFlags)_flags; }
FrameFlags flags() const { return (FrameFlags)_flags; }
/**
* \brief Getter for the rate field.
* \return The rate field.
*/
inline uint8_t rate() const { return _rate; }
uint8_t rate() const { return _rate; }
/**
* \brief Getter for the channel frequency field.
* \return The channel frequency field.
*/
inline uint16_t channel_freq() const { return _channel_freq; }
uint16_t channel_freq() const { return Endian::le_to_host(_channel_freq); }
/**
* \brief Getter for the channel type field.
* \return The channel type field.
*/
inline uint16_t channel_type() const { return _channel_type; }
uint16_t channel_type() const { return Endian::le_to_host(_channel_type); }
/**
* \brief Getter for the dbm signal field.
* \return The dbm signal field.
*/
inline uint8_t dbm_signal() const { return _dbm_signal; }
uint8_t dbm_signal() const { return _dbm_signal; }
/**
* \brief Getter for the antenna field.
* \return The antenna field.
*/
inline uint8_t antenna() const { return _antenna; }
uint8_t antenna() const { return _antenna; }
/**
* \brief Getter for the rx flags field.
* \return The rx flags field.
*/
inline uint16_t rx_flags() const { return _rx_flags; }
uint16_t rx_flags() const { return Endian::le_to_host(_rx_flags); }
/**
* \brief Getter for the present bit fields.

View File

@@ -24,7 +24,7 @@
#include <stdint.h>
#include <vector>
#include "utils.h"
#include "endianness.h"
namespace Tins{
/**
@@ -72,6 +72,12 @@ namespace Tins{
*/
RSNInformation();
/**
* \brief Constructor from buffer.
*
* \param buffer The buffer from which this object will be constructed.
* \param total_sz The total size of the buffer.
*/
RSNInformation(const uint8_t *buffer, uint32_t total_sz);
/**
@@ -123,13 +129,13 @@ namespace Tins{
* \brief Getter for the version field.
* \return The version field.
*/
uint16_t version() const { return Utils::le_to_host(_version); }
uint16_t version() const { return Endian::le_to_host(_version); }
/**
* \brief Getter for the capabilities field.
* \return The version field.
*/
uint16_t capabilities() const { return Utils::le_to_host(_capabilities); }
uint16_t capabilities() const { return Endian::le_to_host(_capabilities); }
/**
* \brief Getter for the pairwise cypher suite list.

View File

@@ -19,13 +19,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TINS_IEEE8022_H
#define TINS_IEEE8022_H
#ifndef TINS_SNAP_H
#define TINS_SNAP_H
#include <stdint.h>
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
#include "small_uint.h"
namespace Tins {
@@ -58,35 +59,19 @@ namespace Tins {
*/
SNAP(const uint8_t *buffer, uint32_t total_sz);
/**
* \brief Copy constructor.
*/
SNAP(const SNAP &other);
/**
* \brief Copy assignment operator.
*/
SNAP &operator= (const SNAP &other);
/* Setters */
/**
* \brief Setter for the id field.
* \param new_id The new id to be set.
* \brief Setter for the control field.
* \param new_id The new control to be set.
*/
void id(uint8_t new_id);
/**
* \brief Setter for the poll field.
* \param new_poll The new poll to be set.
*/
void poll(uint8_t new_poll);
void control(uint8_t new_control);
/**
* \brief Setter for the org code field.
* \param new_org The new org code to be set.
*/
void org_code(uint32_t new_org);
void org_code(small_uint<24> new_org);
/**
* \brief Setter for the eth type field.
@@ -109,28 +94,28 @@ namespace Tins {
uint8_t ssap() const { return _snap.ssap; }
/**
* \brief Getter for the id field.
* \return The id field.
* \brief Getter for the control field.
* \return The control field.
*/
uint8_t id() const { return _snap.id; }
/**
* \brief Getter for the poll field.
* \return The poll field.
*/
uint8_t poll() const { return _snap.poll; }
uint8_t control() const { return _snap.control; }
/**
* \brief Getter for the org code field.
* \return The org code field.
*/
uint32_t org_code() const { return _snap.org_code; }
*/
small_uint<24> org_code() const {
#ifdef TINS_IS_LITTLE_ENDIAN
return Endian::be_to_host<uint32_t>(_snap.org_code << 8);
#else
return Endian::be_to_host(_snap.org_code);
#endif
}
/**
* \brief Getter for the eth type field.
* \return The eth field.
*/
uint16_t eth_type() const { return Utils::be_to_host(_snap.eth_type); }
uint16_t eth_type() const { return Endian::be_to_host(_snap.eth_type); }
/**
* \brief Returns the SNAP frame's header length.
@@ -151,28 +136,23 @@ namespace Tins {
*
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const;
SNAP *clone_pdu() const {
return new SNAP(*this);
}
private:
struct snaphdr {
uint8_t dsap;
uint8_t ssap;
#if TINS_IS_LITTLE_ENDIAN
uint32_t id:2,
reserved1:2,
poll:2,
reserved2:2,
org_code:24;
uint32_t control:8,
org_code:24;
#elif TINS_IS_BIG_ENDIAN
uint32_t reserved1:2,
poll:2,
reserved2:2,
id:2,
org_code:24;
uint32_t org_code:24,
control:8;
#endif
uint16_t eth_type;
} __attribute__((__packed__));
void copy_fields(const SNAP *other);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
snaphdr _snap;
@@ -180,4 +160,4 @@ namespace Tins {
};
#endif // TINS_IEEE8022_H
#endif // TINS_SNAP_H

View File

@@ -31,7 +31,7 @@
#endif
#include "pdu.h"
#include "small_uint.h"
#include "utils.h"
#include "endianness.h"
namespace Tins {
@@ -142,49 +142,49 @@ namespace Tins {
*
* \return The destination port in an uint16_t.
*/
uint16_t dport() const { return Utils::be_to_host(_tcp.dport); }
uint16_t dport() const { return Endian::be_to_host(_tcp.dport); }
/**
* \brief Getter for the source port field.
*
* \return The source port in an uint16_t.
*/
uint16_t sport() const { return Utils::be_to_host(_tcp.sport); }
uint16_t sport() const { return Endian::be_to_host(_tcp.sport); }
/**
* \brief Getter for the sequence number field.
*
* \return The sequence number in an uint32_t.
*/
uint32_t seq() const { return Utils::be_to_host(_tcp.seq); }
uint32_t seq() const { return Endian::be_to_host(_tcp.seq); }
/**
* \brief Getter for the acknowledge number field.
*
* \return The acknowledge number in an uint32_t.
*/
uint32_t ack_seq() const { return Utils::be_to_host(_tcp.ack_seq); }
uint32_t ack_seq() const { return Endian::be_to_host(_tcp.ack_seq); }
/**
* \brief Getter for the window size field.
*
* \return The window size in an uint32_t.
*/
uint16_t window() const { return Utils::be_to_host(_tcp.window); }
uint16_t window() const { return Endian::be_to_host(_tcp.window); }
/**
* \brief Getter for the checksum field.
*
* \return The checksum field in an uint16_t.
*/
uint16_t check() const { return Utils::be_to_host(_tcp.check); }
uint16_t check() const { return Endian::be_to_host(_tcp.check); }
/**
* \brief Getter for the urgent pointer field.
*
* \return The urgent pointer in an uint16_t.
*/
uint16_t urg_ptr() const { return Utils::be_to_host(_tcp.urg_ptr); }
uint16_t urg_ptr() const { return Endian::be_to_host(_tcp.urg_ptr); }
/**
* \brief Getter for the data offset field.

View File

@@ -19,12 +19,12 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __UDP_H
#define __UDP_H
#ifndef TINS_UDP_H
#define TINS_UDP_H
#include "pdu.h"
#include "utils.h"
#include "endianness.h"
namespace Tins {
@@ -63,19 +63,19 @@ namespace Tins {
* \brief Getter for the destination port.
* \return The datagram's destination port.
*/
uint16_t dport() const { return Utils::be_to_host(_udp.dport); }
uint16_t dport() const { return Endian::be_to_host(_udp.dport); }
/**
* \brief Getter for the source port.
* \return The datagram's source port.
*/
uint16_t sport() const { return Utils::be_to_host(_udp.sport); }
uint16_t sport() const { return Endian::be_to_host(_udp.sport); }
/**
* \brief Getter for the length of the datagram.
* \return The length of the datagram.
*/
uint16_t length() const { return Utils::be_to_host(_udp.len); }
uint16_t length() const { return Endian::be_to_host(_udp.len); }
/**
* \brief Set the destination port.
@@ -95,18 +95,6 @@ namespace Tins {
*/
void length(uint16_t new_len);
/** \brief Set the payload.
*
* Payload is NOT copied. Therefore, pointers provided as
* payloads must be freed manually by the user. This actually
* creates a RawPDU that holds the payload, and sets it as the
* inner_pdu. Therefore, if an inner_pdu was set previously,
* a call to UDP::payload will delete it.
* \param new_payload New payload.
* \param new_payload_size New payload's size
*/
void payload(uint8_t *new_payload, uint32_t new_payload_size);
/** \brief Returns the header size.
*
* This metod overrides PDU::header_size. This size includes the
@@ -124,7 +112,7 @@ namespace Tins {
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<UDP>();
return new UDP(*this);
}
private:
struct udphdr {
@@ -141,4 +129,4 @@ namespace Tins {
};
};
#endif
#endif // TINS_UDP_H

View File

@@ -19,8 +19,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __UTILS_H
#define __UTILS_H
#ifndef TINS_UTILS_H
#define TINS_UTILS_H
#include <stdexcept>
@@ -142,149 +142,6 @@ namespace Tins {
*/
template<class ForwardIterator>
void route_entries(ForwardIterator output);
/**
* \brief Changes a 16-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint16_t change_endian(uint16_t data) {
return ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8);
}
/**
* \brief Changes a 32-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint32_t change_endian(uint32_t data) {
return (((data & 0xff000000) >> 24) | ((data & 0x00ff0000) >> 8) |
((data & 0x0000ff00) << 8) | ((data & 0x000000ff) << 24));
}
/**
* \brief Changes a 64-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint64_t change_endian(uint64_t data) {
return (((uint64_t)(change_endian((uint32_t)((data << 32) >> 32))) << 32) |
(change_endian(((uint32_t)(data >> 32)))));
}
#if TINS_IS_LITTLE_ENDIAN
/**
* \brief Convert any integral type to big endian.
*
* \param data The data to convert.
*/
template<typename T>
inline T host_to_be(T data) {
return change_endian(data);
}
/**
* \brief Convert any integral type to little endian.
*
* On little endian platforms, the parameter is simply returned.
*
* \param data The data to convert.
*/
template<typename T>
inline T host_to_le(T data) {
return data;
}
/**
* \brief Convert any big endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T be_to_host(T data) {
return change_endian(data);
}
/**
* \brief Convert any little endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T le_to_host(T data) {
return data;
}
#elif TINS_IS_BIG_ENDIAN
/**
* \brief Convert any integral type to big endian.
*
* \param data The data to convert.
*/
template<typename T>
inline T host_to_be(T data) {
return data;
}
/**
* \brief Convert any integral type to little endian.
*
* On little endian platforms, the parameter is simply returned.
*
* \param data The data to convert.
*/
template<typename T>
inline T host_to_le(T data) {
return change_endian(data);
}
/**
* \brief Convert any big endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T be_to_host(T data) {
return data;
}
/**
* \brief Convert any little endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T le_to_host(T data) {
return change_endian(data);
}
#endif
/** \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) {
return (((data & 0xff000000) >> 24) | ((data & 0x00ff0000) >> 8) |
((data & 0x0000ff00) << 8) | ((data & 0x000000ff) << 24));
}
/**
* \brief Convert 64 bit integer into network byte order.
*
* \param data The data to convert.
*/
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.
*
@@ -371,4 +228,4 @@ void Tins::Utils::route_entries(ForwardIterator output) {
}
}
#endif
#endif // TINS_UTILS_H