diff --git a/include/tcp.h b/include/tcp.h index d2d5d6d..14dc375 100644 --- a/include/tcp.h +++ b/include/tcp.h @@ -29,11 +29,13 @@ #include #endif #include "pdu.h" +#include "utils.h" namespace Tins { - /** \brief TCP represents the TCP PDU. + /** + * \brief TCP represents the TCP PDU. * * TCP is the representation of the TCP PDU. Instances of this class * must be sent over a level 3 PDU, this will otherwise fail. @@ -41,7 +43,8 @@ namespace Tins { class TCP : public PDU { public: - /** \brief TCP flags enum. + /** + * \brief TCP flags enum. * * These flags identify those supported by the TCP PDU. */ @@ -56,7 +59,8 @@ namespace Tins { CWR }; - /** \brief TCP options enum. + /** + * \brief TCP options enum. * * This enum identifies valid options supported by TCP PDU. */ @@ -66,7 +70,8 @@ namespace Tins { TSOPT = 8 }; - /** \brief TCP constructor. + /** + * \brief TCP constructor. * * Creates an instance of TCP. Destination and source port can * be provided, otherwise both will be 0. @@ -76,121 +81,185 @@ namespace Tins { TCP(uint16_t dport = 0, uint16_t sport = 0); - /** \brief TCP destructor. + /** + * \brief TCP destructor. * * Destructs the TCP instance. Does not free the payload. * */ ~TCP(); - /** \brief Returns the destination port. + /** + * \brief Getter for the destination port field. + * + * \return The destination port in an uint16_t. */ - inline uint16_t dport() const { return _tcp.dport; } + inline uint16_t dport() const { return Utils::net_to_host_s(_tcp.dport); } - /** \brief Returns the source port. + /** + * \brief Getter for the source port field. + * + * \return The source port in an uint16_t. */ - inline uint16_t sport() const { return _tcp.sport; } + inline uint16_t sport() const { return Utils::net_to_host_s(_tcp.sport); } - /** \brief Returns the sequence number. + /** + * \brief Getter for the sequence number field. + * + * \return The sequence number in an uint32_t. */ - inline uint32_t seq() const { return _tcp.seq; } + inline uint32_t seq() const { return Utils::net_to_host_l(_tcp.seq); } - /** \brief Returns the acknowledge number. + /** + * \brief Getter for the acknowledge number field. + * + * \return The acknowledge number in an uint32_t. */ - inline uint32_t ack_seq() const { return _tcp.ack_seq; } + inline uint32_t ack_seq() const { return Utils::net_to_host_l(_tcp.ack_seq); } - /** \brief Returns the window size. + /** + * \brief Getter for the window size field. + * + * \return The window size in an uint32_t. */ - inline uint16_t window() const { return _tcp.window; } + inline uint16_t window() const { return Utils::net_to_host_s(_tcp.window); } - /** \brief Returns the checksum. + /** + * \brief Getter for the checksum field. + * + * \return The checksum field in an uint16_t. */ - inline uint16_t check() const { return _tcp.check; } + inline uint16_t check() const { return Utils::net_to_host_s(_tcp.check); } - /** \brief Returns the urgent pointer. + /** + * \brief Getter for the urgent pointer field. + * + * \return The urgent pointer in an uint16_t. */ - inline uint16_t urg_ptr() const { return _tcp.urg_ptr; } + inline uint16_t urg_ptr() const { return Utils::net_to_host_s(_tcp.urg_ptr); } - /** \brief Set the destination port. - * \param new_dport New destination port. + /** + * \brief Getter for the data offset field. + * + * \return Data offset in an uint8_t. + */ + inline uint8_t data_offset() const { return this->_tcp.doff; } + + /** + * \brief Setter for the destination port field. + * + * \param new_dport uint16_t with the new destination port. */ void dport(uint16_t new_dport); - /** \brief Set the source port. - * \param new_sport New source port. + /** + * \brief Setter for the source port field. + * + * \param new_sport uint16_t with the new source port. */ void sport(uint16_t new_sport); - /** \brief Set the sequence number. - * \param new_seq New sequence number. + /** + * \brief Setter for the sequence number. + * + * \param new_seq uint32_t with the new sequence number. */ void seq(uint32_t new_seq); - /** \brief Set the acknowledge number. - * \param new_ack_seq New acknowledge number. + /** + * \brief Setter for the acknowledge number. + * + * \param new_ack_seq uint32_t with the new acknowledge number. */ void ack_seq(uint32_t new_ack_seq); - /** \brief Set the window size. - * \param new_window New window size. + /** + * \brief Setter for the window size. + * + * \param new_window uint16_t with the new window size. */ void window(uint16_t new_window); - /** \brief Set the checksum. - * \param new_check New checksum. + /** + * \brief Setter for the checksum field. + * + * \param new_check uint16_t with the new checksum. */ void check(uint16_t new_check); - /** \brief Set the urgent pointer. - * \param new_urg_ptr New urgent pointer. + /** + * \brief Setter for the urgent pointer field. + * + * \param new_urg_ptr uint16_t with the new urgent pointer. */ void urg_ptr(uint16_t new_urg_ptr); - /** \brief Set the payload. + /** + * \brief Setter for the data offset pointer field. + * + * \param new_doff uint8_t with the new data offset pointer. + */ + void data_offset(uint8_t new_doff); + + /** + * \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 TCP::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 Set maximum segment size. - * \param value New maximum segment size. + /** + * \brief Set the maximum segment size. + * + * \param value uint16_t with the new maximum segment size. */ void set_mss(uint16_t value); - /** \brief Set the timestamp. - * \param value Current value of the timestamp clock. - * \param reply Echo reply field. + /** + * \brief Set the timestamp. + * + * \param value uint32_t with the current value of the timestamp clock. + * \param reply uint32_t with the echo reply field. */ void set_timestamp(uint32_t value, uint32_t reply); - /** \brief Set a TCP flag value. - * \param tcp_flag Indicates which flag will be set. - * \param value New value for this flag. Must be 0 or 1. + /** + * \brief Set a TCP flag value. + * + * \param tcp_flag Flag which indicates the flag to be set. + * \param value uint8_t with the new value for this flag. Must be 0 or 1. */ void set_flag(Flags tcp_flag, uint8_t value); - /** \brief Adds a TCP option. - * \param tcp_option Indicates the option that will be set. - * \param length Length of this option. - * \param data This option's data. + /** + * \brief Adds a TCP option. + * + * \param tcp_option Options indicating the option to be set. + * \param length uint8_t with the length of this option(optional). + * \param data uint8_t* containing this option's data(optional). */ void add_option(Options tcp_option, uint8_t length = 0, uint8_t *data = 0); - /** \brief Returns the header size. + /** + * \brief Returns the header size. * * This metod overrides PDU::header_size. This size includes the - * payload and options size. \sa PDU::header_size + * payload and options size. + * + * \sa PDU::header_size */ uint32_t header_size() const; /** * \brief Getter for the PDU's type. + * * \sa PDU::pdu_type */ PDUType pdu_type() const { return PDU::TCP; } diff --git a/src/tcp.cpp b/src/tcp.cpp index e2e6d85..d0a2f46 100644 --- a/src/tcp.cpp +++ b/src/tcp.cpp @@ -34,11 +34,11 @@ const uint16_t Tins::TCP::DEFAULT_WINDOW = 32678; Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(IPPROTO_TCP), _options_size(0), _total_options_size(0) { std::memset(&_tcp, 0, sizeof(tcphdr)); - _tcp.dport = Utils::net_to_host_s(dport); - _tcp.sport = Utils::net_to_host_s(sport); - _tcp.doff = sizeof(tcphdr) / sizeof(uint32_t); - _tcp.window = Utils::net_to_host_s(DEFAULT_WINDOW); - _tcp.check = 0; + this->dport(dport); + this->sport(sport); + this->data_offset(sizeof(tcphdr) / sizeof(uint32_t)); + this->window(DEFAULT_WINDOW); + this->check(0); } Tins::TCP::~TCP() { @@ -55,29 +55,33 @@ void Tins::TCP::sport(uint16_t new_sport) { } void Tins::TCP::seq(uint32_t new_seq) { - _tcp.seq = new_seq; + _tcp.seq = Utils::net_to_host_l(new_seq); } void Tins::TCP::ack_seq(uint32_t new_ack_seq) { - _tcp.ack_seq = new_ack_seq; + _tcp.ack_seq = Utils::net_to_host_l(new_ack_seq); } void Tins::TCP::window(uint16_t new_window) { - _tcp.window = new_window; + _tcp.window = Utils::net_to_host_s(new_window); } void Tins::TCP::check(uint16_t new_check) { - _tcp.check = new_check; + _tcp.check = Utils::net_to_host_s(new_check); } void Tins::TCP::urg_ptr(uint16_t new_urg_ptr) { - _tcp.urg_ptr = new_urg_ptr; + _tcp.urg_ptr = Utils::net_to_host_s(new_urg_ptr); } void Tins::TCP::payload(uint8_t *new_payload, uint32_t new_payload_size) { inner_pdu(new RawPDU(new_payload, new_payload_size)); } +void Tins::TCP::data_offset(uint8_t new_doff) { + this->_tcp.doff = new_doff; +} + void Tins::TCP::set_mss(uint16_t value) { value = Utils::net_to_host_s(value); add_option(MSS, 2, (uint8_t*)&value);