1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Add initial ACK tracking code

This commit is contained in:
Matias Fontanini
2016-02-13 20:24:15 -08:00
parent 48c068b84a
commit 116eb9f1c1
14 changed files with 578 additions and 19 deletions

View File

@@ -13,4 +13,7 @@
/* Use pcap_sendpacket to send l2 packets */
#cmakedefine HAVE_PACKET_SENDER_PCAP_SENDPACKET
/* Have TCP ACK tracking */
#cmakedefine HAVE_ACK_TRACKER
#endif // TINS_CONFIG_H

View File

@@ -200,6 +200,17 @@ public:
}
};
/**
* \brief Exception thrown when a feature has been disabled
* at compile time.
*/
class feature_disabled : public exception_base {
public:
const char* what() const throw() {
return "Feature disabled";
}
};
/**
* \brief Exception thrown when a payload is too large to fit
* into a PDUOption.

View File

@@ -177,6 +177,9 @@ bool decrement(HWAddress<n>& addr) {
return decrement_buffer(addr);
}
// Compares sequence numbers as defined by RFC 1982.
int seq_compare(uint32_t seq1, uint32_t seq2);
IPv4Address last_address_from_mask(IPv4Address addr, IPv4Address mask);
IPv6Address last_address_from_mask(IPv6Address addr, const IPv6Address& mask);
template<size_t n>

View File

@@ -0,0 +1,147 @@
/*
* Copyright (c) 2016, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef TINS_TCP_IP_ACK_TRACKER_H
#define TINS_TCP_IP_ACK_TRACKER_H
#include "../config.h"
#ifdef HAVE_ACK_TRACKER
#include <vector>
#include <boost/icl/interval_set.hpp>
namespace Tins {
class PDU;
namespace TCPIP {
/**
* \brief Represents an acknowledged segment range
*
* The interval represented by this range is a closed interval [first, last].
*/
class AckedRange {
public:
typedef boost::icl::discrete_interval<uint32_t> interval_type;
/**
* \brief Constructs an acked range
*
* \param first The first acked byte
* \param last The last acked byte (inclusive)
*/
AckedRange(uint32_t first, uint32_t last);
/**
* \brief Gets the next acked interval in this range
*
* If has_next() == false, then this returns an empty interval
*/
interval_type next();
/**
* Indicates whether there is still some non-consumed acked-interval in this
* range
*/
bool has_next() const;
/**
* Gets the first index acked by this range
*/
uint32_t first() const;
/**
* Gets the last index acked by this range
*/
uint32_t last() const;
private:
uint32_t first_;
uint32_t last_;
};
/**
* \brief Allows tracking acknowledged intervals in a TCP stream
*/
class AckTracker {
public:
/**
* The type used to store ACKed intervals
*/
typedef boost::icl::interval_set<uint32_t> interval_set_type;
/**
* Default constructor
*/
AckTracker();
/**
* \brief Construct an instance using some attributes
*
* \param intial_ack The initial ACK number to use
* \param use_sack Indicate whether to use Selective ACKs to track ACK numbers
*/
AckTracker(uint32_t initial_ack, bool use_sack = true);
/**
* \brief Process a packet
*/
void process_packet(const PDU& packet);
/**
* \brief Indicates whether Selective ACKs should be processed
*/
void use_sack();
/**
* Retrieves the current ACK number in this tracker
*/
uint32_t ack_number() const;
/**
* \brief Retrieves all acked intervals by Selective ACKs
*/
const interval_set_type& acked_intervals() const;
private:
void process_sack(const std::vector<uint32_t>& sack);
void cleanup_sacked_intervals(uint32_t old_ack, uint32_t new_ack);
interval_set_type acked_intervals_;
uint32_t ack_number_;
bool use_sack_;
};
} // TCPIP
} // Tins
#endif // HAVE_ACK_TRACKER
#endif // TINS_TCP_IP_ACK_TRACKER_H

View File

@@ -41,6 +41,7 @@
#include <functional>
#include <stdint.h>
#include "../macros.h"
#include "ack_tracker.h"
#include "../hw_address.h"
namespace Tins {
@@ -267,16 +268,31 @@ public:
* \brief Indicates whether this Flow supports selective acknowledgements
*/
bool sack_permitted() const;
/**
* \brief Enables tracking of ACK numbers
*
* This requires having the boost.icl library. If the library is not installed
* or ACK tracking was disabled when compiling the library, then this method
* will throw an exception.
*/
void enable_ack_tracking();
/**
* \brief Indicates whether ACK number tracking is enabled
*/
bool ack_tracking_enabled() const;
private:
// Compress all flags into just one struct using bitfields
struct flags {
flags() : ignore_data_packets(0), sack_permitted(0) {
flags() : ignore_data_packets(0), sack_permitted(0), ack_tracking(0) {
}
uint32_t is_v6:1,
ignore_data_packets:1,
sack_permitted:1;
sack_permitted:1,
ack_tracking:1;
};
void store_payload(uint32_t seq, payload_type payload);
@@ -295,6 +311,9 @@ private:
State state_;
int mss_;
flags flags_;
#ifdef HAVE_ACK_TRACKER
AckTracker ack_tracker_;
#endif // HAVE_ACK_TRACKER
};
} // TCPIP

View File

@@ -334,6 +334,18 @@ public:
* \param value The value to be set for this property
*/
void auto_cleanup_payloads(bool value);
/**
* Enables tracking of acknowledged segments
*
* \sa Flow::enable_ack_tracking
*/
void enable_ack_tracking();
/**
* \brief Indicates whether ACK number tracking is enabled for this stream
*/
bool ack_tracking_enabled() const;
private:
static Flow extract_client_flow(const PDU& packet);
static Flow extract_server_flow(const PDU& packet);