mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Solved conflicts.
This commit is contained in:
@@ -164,12 +164,11 @@ namespace Tins {
|
|||||||
static const uint8_t DEFAULT_TTL;
|
static const uint8_t DEFAULT_TTL;
|
||||||
|
|
||||||
void init_ip_fields();
|
void init_ip_fields();
|
||||||
void write_serialization(uint8_t *buffer, uint32_t total_sz);
|
void write_serialization(uint8_t *buffer, uint32_t total_sz, PDU *parent);
|
||||||
|
|
||||||
iphdr _ip;
|
iphdr _ip;
|
||||||
std::vector<IpOption> _ip_options;
|
std::vector<IpOption> _ip_options;
|
||||||
uint32_t _options_size;
|
uint32_t _options_size, _padded_options_size;
|
||||||
uint32_t _padded_options_size;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -30,37 +30,95 @@ namespace Tins {
|
|||||||
|
|
||||||
class PacketSender;
|
class PacketSender;
|
||||||
|
|
||||||
|
/** \brief PDU is the base class for protocol data units.
|
||||||
|
*
|
||||||
|
* Every PDU implementation must inherit this one. PDUs can be serialized,
|
||||||
|
* therefore allowing a PacketSender to send them through sockets. PDUs
|
||||||
|
* are created upwards: upper layers will be children of the lower ones.
|
||||||
|
* Each PDU must provide its flag identifier. This will be most likely added
|
||||||
|
* to its parent's data, hence it should be a valid identifier. For example,
|
||||||
|
* IP should provide IPPROTO_IP.
|
||||||
|
*/
|
||||||
class PDU {
|
class PDU {
|
||||||
public:
|
public:
|
||||||
|
/** \brief PDU constructor
|
||||||
|
*
|
||||||
|
* Must be called by subclasses in their constructors.
|
||||||
|
* \param flag The flag identifier for the subclass' PDU.
|
||||||
|
* \param next_pdu The child PDU. Can be obviated.
|
||||||
|
*/
|
||||||
PDU(uint32_t flag, PDU *next_pdu = 0);
|
PDU(uint32_t flag, PDU *next_pdu = 0);
|
||||||
virtual ~PDU();
|
virtual ~PDU();
|
||||||
|
|
||||||
/* This PDU's header size only. */
|
/** \brief The header's size
|
||||||
|
*/
|
||||||
virtual uint32_t header_size() const = 0;
|
virtual uint32_t header_size() const = 0;
|
||||||
/* This PDU's trailer size only. Defaults to 0. */
|
|
||||||
|
/** \brief Trailer's size.
|
||||||
|
*
|
||||||
|
* Some protocols require a trailer(like Ethernet). This defaults to 0.
|
||||||
|
*/
|
||||||
virtual uint32_t trailer_size() const { return 0; }
|
virtual uint32_t trailer_size() const { return 0; }
|
||||||
/* The size of the whole chain of PDUs, including this one. */
|
|
||||||
inline uint32_t size() const;
|
/** \brief The whole chain of PDU's size, including this one.
|
||||||
|
*
|
||||||
|
* Returns the sum of this and all children PDUs' size.
|
||||||
|
*/
|
||||||
|
uint32_t size() const;
|
||||||
|
|
||||||
|
/** \brief This PDU's type flag identifier.
|
||||||
|
*
|
||||||
|
*/
|
||||||
inline uint32_t flag() const { return _flag; }
|
inline uint32_t flag() const { return _flag; }
|
||||||
|
|
||||||
|
/** \brief The child PDU.
|
||||||
|
*/
|
||||||
inline const PDU *inner_pdu() const { return _inner_pdu; }
|
inline const PDU *inner_pdu() const { return _inner_pdu; }
|
||||||
|
|
||||||
|
/** \brief Sets the flag identifier.
|
||||||
|
*/
|
||||||
void flag(uint32_t new_flag);
|
void flag(uint32_t new_flag);
|
||||||
/* When setting a new inner_pdu, the instance takes
|
|
||||||
* ownership of the object, therefore deleting it when
|
/** \brief Sets the child PDU.
|
||||||
* it's no longer required. */
|
*
|
||||||
|
* \param next_pdu The new child PDU.
|
||||||
|
* When setting a new inner_pdu, the instance takesownership of
|
||||||
|
* the object, therefore deleting it when it's no longer required.
|
||||||
|
*/
|
||||||
void inner_pdu(PDU *next_pdu);
|
void inner_pdu(PDU *next_pdu);
|
||||||
|
|
||||||
/* Serializes the whole chain of PDU's, including this one. */
|
|
||||||
|
/** \brief Serializes the whole chain of PDU's, including this one.
|
||||||
|
*
|
||||||
|
* \param sz The size of the buffer must be returned through this parameter.
|
||||||
|
* The buffer returned must be deleted by the user using
|
||||||
|
* operator delete[].
|
||||||
|
*/
|
||||||
uint8_t *serialize(uint32_t &sz);
|
uint8_t *serialize(uint32_t &sz);
|
||||||
|
|
||||||
/* */
|
/** \brief Send the stack of PDUs through a PacketSender.
|
||||||
virtual bool send(PacketSender* sender) {return false;}
|
*
|
||||||
|
* This method will be called only for the PDU on the bottom of the stack,
|
||||||
|
* therefore it should only implement this method if it can be sent.
|
||||||
|
* PacketSender implements specific methods to send packets which start
|
||||||
|
* on every valid TCP/IP stack layer; this should only be a proxy for
|
||||||
|
* those methods.
|
||||||
|
* \param sender The PacketSender which will send the packet.
|
||||||
|
*/
|
||||||
|
virtual bool send(PacketSender* sender) { return false; }
|
||||||
protected:
|
protected:
|
||||||
/* Serialize this PDU storing the result in buffer. */
|
/* Serialize this PDU storing the result in buffer. */
|
||||||
void serialize(uint8_t *buffer, uint32_t total_sz);
|
void serialize(uint8_t *buffer, uint32_t total_sz, PDU *parent);
|
||||||
|
|
||||||
/* Each PDU's own implementation of serialization. */
|
/** \brief Serialices this TCP PDU.
|
||||||
virtual void write_serialization(uint8_t *buffer, uint32_t total_sz) = 0;
|
*
|
||||||
|
* Each PDU must override this method and implement it's own
|
||||||
|
* serialization.
|
||||||
|
* \param buffer The buffer in which the PDU will be serialized.
|
||||||
|
* \param total_sz The size available in the buffer.
|
||||||
|
* \param parent The PDU that's one level below this one on the stack. Might be 0.
|
||||||
|
*/
|
||||||
|
virtual void write_serialization(uint8_t *buffer, uint32_t total_sz, PDU *parent) = 0;
|
||||||
private:
|
private:
|
||||||
uint32_t _flag;
|
uint32_t _flag;
|
||||||
PDU *_inner_pdu;
|
PDU *_inner_pdu;
|
||||||
|
|||||||
@@ -186,12 +186,6 @@ namespace Tins {
|
|||||||
* payload and options size.
|
* payload and options size.
|
||||||
*/
|
*/
|
||||||
uint32_t header_size() const;
|
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:
|
private:
|
||||||
struct tcphdr {
|
struct tcphdr {
|
||||||
uint16_t sport;
|
uint16_t sport;
|
||||||
@@ -240,7 +234,15 @@ namespace Tins {
|
|||||||
|
|
||||||
static const uint16_t DEFAULT_WINDOW;
|
static const uint16_t DEFAULT_WINDOW;
|
||||||
|
|
||||||
uint16_t do_checksum(uint8_t *start, uint8_t *end) 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.
|
||||||
|
* \param parent The PDU that's one level below this one on the stack.
|
||||||
|
*/
|
||||||
|
void write_serialization(uint8_t *buffer, uint32_t total_sz, PDU *parent);
|
||||||
|
|
||||||
|
uint32_t do_checksum(uint8_t *start, uint8_t *end) const;
|
||||||
|
uint32_t pseudoheader_checksum(uint32_t source_ip, uint32_t dest_ip) const;
|
||||||
|
|
||||||
tcphdr _tcp;
|
tcphdr _tcp;
|
||||||
std::vector<TCPOption> _options;
|
std::vector<TCPOption> _options;
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ void Tins::IP::init_ip_fields() {
|
|||||||
_ip.ihl = sizeof(iphdr) / sizeof(uint32_t);
|
_ip.ihl = sizeof(iphdr) / sizeof(uint32_t);
|
||||||
_ip.ttl = DEFAULT_TTL;
|
_ip.ttl = DEFAULT_TTL;
|
||||||
_options_size = 0;
|
_options_size = 0;
|
||||||
|
_padded_options_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setters */
|
/* Setters */
|
||||||
@@ -164,7 +165,7 @@ bool Tins::IP::send(PacketSender* sender) {
|
|||||||
return sender->send_l3(this, (const struct sockaddr*)&link_addr, sizeof(link_addr));
|
return sender->send_l3(this, (const struct sockaddr*)&link_addr, sizeof(link_addr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz) {
|
void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, PDU *) {
|
||||||
uint32_t my_sz = header_size();
|
uint32_t my_sz = header_size();
|
||||||
uint32_t new_flag;
|
uint32_t new_flag;
|
||||||
assert(total_sz >= my_sz);
|
assert(total_sz >= my_sz);
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ bool Tins::PacketSender::send_l3(PDU *pdu, const struct sockaddr* link_addr, uin
|
|||||||
int sock = _sockets[IP_SOCKET];
|
int sock = _sockets[IP_SOCKET];
|
||||||
uint8_t *buffer = pdu->serialize(sz);
|
uint8_t *buffer = pdu->serialize(sz);
|
||||||
ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_link_addr) != -1);
|
ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_link_addr) != -1);
|
||||||
|
std::cout << "Ret_val: " << ret_val << "\n";
|
||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,16 +52,16 @@ void Tins::PDU::inner_pdu(PDU *next_pdu) {
|
|||||||
uint8_t *Tins::PDU::serialize(uint32_t &sz) {
|
uint8_t *Tins::PDU::serialize(uint32_t &sz) {
|
||||||
sz = size();
|
sz = size();
|
||||||
uint8_t *buffer = new uint8_t[sz];
|
uint8_t *buffer = new uint8_t[sz];
|
||||||
serialize(buffer, sz);
|
serialize(buffer, sz, 0);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tins::PDU::serialize(uint8_t *buffer, uint32_t total_sz) {
|
void Tins::PDU::serialize(uint8_t *buffer, uint32_t total_sz, PDU *parent) {
|
||||||
uint32_t sz = header_size() + trailer_size();
|
uint32_t sz = header_size() + trailer_size();
|
||||||
/* Must not happen... */
|
/* Must not happen... */
|
||||||
assert(total_sz >= sz);
|
assert(total_sz >= sz);
|
||||||
if(_inner_pdu)
|
if(_inner_pdu)
|
||||||
_inner_pdu->serialize(buffer + header_size(), total_sz - sz);
|
_inner_pdu->serialize(buffer + header_size(), total_sz - sz, this);
|
||||||
write_serialization(buffer, total_sz);
|
write_serialization(buffer, total_sz, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
42
src/tcp.cpp
42
src/tcp.cpp
@@ -21,10 +21,12 @@
|
|||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <iostream> //borrame
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#endif
|
#endif
|
||||||
#include "tcp.h"
|
#include "tcp.h"
|
||||||
|
#include "ip.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -126,10 +128,28 @@ void Tins::TCP::add_option(Options tcp_option, uint8_t length, uint8_t *data) {
|
|||||||
_total_options_size = (padding) ? _options_size - padding + 4 : _options_size;
|
_total_options_size = (padding) ? _options_size - padding + 4 : _options_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Tins::TCP::do_checksum(uint8_t *start, uint8_t *end) const {
|
uint32_t Tins::TCP::do_checksum(uint8_t *start, uint8_t *end) const {
|
||||||
unsigned checksum(0);
|
uint32_t checksum(0);
|
||||||
while(start < end)
|
uint16_t *ptr = (uint16_t*)start, *last = (uint16_t*)end, padding(0);
|
||||||
checksum += *(start++);
|
if(((end - start) & 1) == 1) {
|
||||||
|
last = (uint16_t*)end - 1;
|
||||||
|
padding = *(end - 1) << 8;
|
||||||
|
}
|
||||||
|
while(ptr < last)
|
||||||
|
checksum += Utils::net_to_host_s(*(ptr++));
|
||||||
|
return checksum + padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t Tins::TCP::pseudoheader_checksum(uint32_t source_ip, uint32_t dest_ip) const {
|
||||||
|
uint32_t checksum(0), len(header_size());
|
||||||
|
source_ip = Utils::net_to_host_l(source_ip);
|
||||||
|
dest_ip = Utils::net_to_host_l(dest_ip);
|
||||||
|
uint16_t *ptr = (uint16_t*)&source_ip;
|
||||||
|
|
||||||
|
checksum += *ptr + ptr[1];
|
||||||
|
ptr = (uint16_t*)&dest_ip;
|
||||||
|
checksum += *ptr + ptr[1];
|
||||||
|
checksum += IPPROTO_TCP + len;
|
||||||
return checksum;
|
return checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +157,7 @@ uint32_t Tins::TCP::header_size() const {
|
|||||||
return sizeof(tcphdr) + _payload_size + _total_options_size;
|
return sizeof(tcphdr) + _payload_size + _total_options_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tins::TCP::write_serialization(uint8_t *buffer, uint32_t total_sz) {
|
void Tins::TCP::write_serialization(uint8_t *buffer, uint32_t total_sz, PDU *parent) {
|
||||||
assert(total_sz >= header_size());
|
assert(total_sz >= header_size());
|
||||||
uint8_t *tcp_start = buffer;
|
uint8_t *tcp_start = buffer;
|
||||||
buffer += sizeof(tcphdr);
|
buffer += sizeof(tcphdr);
|
||||||
@@ -154,8 +174,16 @@ void Tins::TCP::write_serialization(uint8_t *buffer, uint32_t total_sz) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
memcpy(buffer, _payload, _payload_size);
|
memcpy(buffer, _payload, _payload_size);
|
||||||
if(!_tcp.check)
|
buffer += _payload_size;
|
||||||
_tcp.check = Utils::net_to_host_s(do_checksum(tcp_start + sizeof(tcphdr), buffer));
|
IP *ip_packet = dynamic_cast<IP*>(parent);
|
||||||
|
if(ip_packet) {
|
||||||
|
_tcp.check = 0;
|
||||||
|
uint32_t checksum = pseudoheader_checksum(ip_packet->source_address(), ip_packet->dest_address()) +
|
||||||
|
do_checksum(tcp_start + sizeof(tcphdr), buffer) + do_checksum((uint8_t*)&_tcp, ((uint8_t*)&_tcp) + sizeof(tcphdr));
|
||||||
|
while (checksum >> 16)
|
||||||
|
checksum = (checksum & 0xffff)+(checksum >> 16);
|
||||||
|
_tcp.check = Utils::net_to_host_s(~checksum);
|
||||||
|
}
|
||||||
memcpy(tcp_start, &_tcp, sizeof(tcphdr));
|
memcpy(tcp_start, &_tcp, sizeof(tcphdr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user