mirror of
https://github.com/mfontanini/libtins
synced 2026-01-28 04:34:27 +01:00
Created UDP PDU. Done some documentation on TCP.
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
46
include/udp.h
Normal file
46
include/udp.h
Normal file
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
14
src/udp.cpp
Normal file
14
src/udp.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef WIN32
|
||||
#include <netinet/in.h>
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user