diff --git a/include/tcp.h b/include/tcp.h index 78d1e32..aad83b0 100644 --- a/include/tcp.h +++ b/include/tcp.h @@ -30,10 +30,21 @@ #endif #include "pdu.h" + namespace Tins { + /** \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. + */ + class TCP : public PDU { public: + /** \brief TCP flags enum. + * + * These flags identify those supported by the TCP PDU. + */ enum Flags { FIN, SYN, @@ -45,38 +56,140 @@ namespace Tins { CWR }; + /** \brief TCP options enum. + * + * This enum identifies valid options supported by TCP PDU. + */ + enum Options { MSS = 2, TSOPT = 8 }; + /** \brief TCP constructor. + * + * Creates an instance of TCP. Destination and source port can + * be provided, otherwise both will be 0. + * \param dport Destination port. + * \param sport Source port. + * */ + TCP(uint16_t dport = 0, uint16_t sport = 0); + + /** \brief TCP destructor. + * + * Destructs the TCP instance. Does not free the payload. + * */ + ~TCP(); + /** \brief Returns the destination port. + */ inline uint16_t dport() const { return _tcp.dport; } + + /** \brief Returns the source port. + */ inline uint16_t sport() const { return _tcp.sport; } + + /** \brief Returns the sequence number. + */ inline uint32_t seq() const { return _tcp.seq; } + + /** \brief Returns the acknowledge number. + */ inline uint32_t ack_seq() const { return _tcp.ack_seq; } + + /** \brief Returns the window size. + */ inline uint16_t window() const { return _tcp.window; } + + /** \brief Returns the checksum. + */ inline uint16_t check() const { return _tcp.check; } + + /** \brief Returns the urgent pointer. + */ inline uint16_t urg_ptr() const { return _tcp.urg_ptr; } + /** \brief Set the destination port. + * \param new_dport New destination port. + */ void dport(uint16_t new_dport); + + /** \brief Set the source port. + * \param new_dport New source port. + */ void sport(uint16_t new_sport); + + /** \brief Set the sequence number. + * \param new_seq New sequence number. + */ void seq(uint32_t new_seq); + + /** \brief Set the acknowledge number. + * \param new_ack_seq New acknowledge number. + */ void ack_seq(uint32_t new_ack_seq); + + /** \brief Set the window size. + * \param new_window New window size. + */ void window(uint16_t new_window); + + /** \brief Set the checksum. + * \param new_check New checksum. + */ void check(uint16_t new_check); + + /** \brief Set the urgent pointer. + * \param new_urg_ptr New urgent pointer. + */ void urg_ptr(uint16_t new_urg_ptr); + + /** \brief Set the payload. + * + * Payload is NOT copied. Therefore, pointers provided as + * payloads must be freed manually by the user. + * \param new_payload New payload. + */ void payload(uint8_t *new_payload, uint32_t new_payload_size); + /** \brief Set maximum segment size. + * \param value 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. + */ 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. + */ 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. + */ void add_option(Options tcp_option, uint8_t length = 0, uint8_t *data = 0); /* Virtual methods */ + /** \brief Returns the header size. + * + * This metod overrides PDU::header_size. This size includes the + * payload and options size. + */ uint32_t header_size() const; + + /** \brief Serialices this TCP PDU. + * \param buffer The buffer in which the PDU will be serialized. + * \param total_sz The size available in the buffer. + */ void write_serialization(uint8_t *buffer, uint32_t total_sz); private: struct tcphdr { diff --git a/src/tcp.cpp b/src/tcp.cpp index 6360e03..a50713d 100644 --- a/src/tcp.cpp +++ b/src/tcp.cpp @@ -73,7 +73,6 @@ void Tins::TCP::urg_ptr(uint16_t new_urg_ptr) { } void Tins::TCP::payload(uint8_t *new_payload, uint32_t new_payload_size) { - delete[] _payload; _payload = new_payload; _payload_size = new_payload_size; }