diff --git a/include/tcp.h b/include/tcp.h index aad83b0..748a4bc 100644 --- a/include/tcp.h +++ b/include/tcp.h @@ -117,7 +117,7 @@ namespace Tins { void dport(uint16_t new_dport); /** \brief Set the source port. - * \param new_dport New source port. + * \param new_sport New source port. */ void sport(uint16_t new_sport); @@ -151,6 +151,7 @@ namespace Tins { * Payload is NOT copied. Therefore, pointers provided as * payloads must be freed manually by the user. * \param new_payload New payload. + * \param new_payload_size New payload's size */ void payload(uint8_t *new_payload, uint32_t new_payload_size); diff --git a/include/udp.h b/include/udp.h new file mode 100644 index 0000000..6918029 --- /dev/null +++ b/include/udp.h @@ -0,0 +1,46 @@ +#ifndef __UDP_H +#define __UDP_H + + +#include "pdu.h" + +namespace Tins { + + /** \brief UDP represents the UDP PDU. + * + * UDP is the representation of the UDP PDU. Instances of this class + * must be sent over a level 3 PDU, this will otherwise fail. + */ + class UDP : public PDU { + public: + /** \brief UDP constructor. + * + * Creates an instance of UDP. Destination and source port can + * be provided, otherwise both will be 0. + * \param dport Destination port. + * \param sport Source port. + * */ + UDP(uint16_t sport = 0, uint16_t dport = 0); + + /** \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. + * \param new_payload_size New payload's size + */ + void payload(uint8_t *new_payload, uint32_t new_payload_size); + private: + struct udphdr { + uint16_t sport; + uint16_t dport; + uint16_t len; + uint16_t check; + } __attribute__((packed)); + + udphdr _udp; + uint8_t *payload; + }; +}; + +#endif diff --git a/src/ip.cpp b/src/ip.cpp index 9aecd3b..d4cf501 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -126,8 +126,6 @@ void Tins::IP::set_option(uint8_t copied, option.type.number = number; uint8_t* buffer(0); if (data_size) { - /* data must be a valid pointer */ - assert(data); buffer = new uint8_t[data_size]; memcpy(buffer, data, data_size); } diff --git a/src/tcp.cpp b/src/tcp.cpp index a50713d..8b7bba3 100644 --- a/src/tcp.cpp +++ b/src/tcp.cpp @@ -37,6 +37,7 @@ Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(IPPROTO_TCP), _payload(0), _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; } Tins::TCP::~TCP() { @@ -153,7 +154,8 @@ void Tins::TCP::write_serialization(uint8_t *buffer, uint32_t total_sz) { } memcpy(buffer, _payload, _payload_size); - _tcp.check = Utils::net_to_host_s(do_checksum(tcp_start + sizeof(tcphdr), buffer)); + if(!_tcp.check) + _tcp.check = Utils::net_to_host_s(do_checksum(tcp_start + sizeof(tcphdr), buffer)); memcpy(tcp_start, &_tcp, sizeof(tcphdr)); } diff --git a/src/udp.cpp b/src/udp.cpp new file mode 100644 index 0000000..e634811 --- /dev/null +++ b/src/udp.cpp @@ -0,0 +1,14 @@ +#ifndef WIN32 + #include +#endif +#include "udp.h" + +Tins::UDP::UDP(uint16_t sport, uint16_t dport) : PDU(IPPROTO_UDP), _payload(0) { + _udp.sport = sport; + _udp.dport = dport; +} + +void Tins::UDP::payload(uint8_t *new_payload, uint32_t new_payload_size) { + _payload = new_payload; + _udp.len = sizeof(udphdr) + new_payload_size; +}