mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 04:54:28 +01:00
Fixed some bugs. Added documentation.
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
#include <string>
|
||||
#include <iterator>
|
||||
#include <pcap.h>
|
||||
#include "utils.h"
|
||||
|
||||
namespace Tins {
|
||||
class PDU;
|
||||
@@ -82,27 +83,10 @@ public:
|
||||
*/
|
||||
template<typename ForwardIterator>
|
||||
void write(ForwardIterator start, ForwardIterator end) {
|
||||
typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
|
||||
typedef derefer<value_type> deref_type;
|
||||
|
||||
while(start != end)
|
||||
write(deref_type::deref(*start++));
|
||||
write(Utils::dereference_until_pdu(*start++));
|
||||
}
|
||||
private:
|
||||
template<typename T>
|
||||
struct derefer {
|
||||
static T &deref(T &value) {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct derefer<T*> {
|
||||
static T &deref(T *value) {
|
||||
return *value;
|
||||
}
|
||||
};
|
||||
|
||||
// You shall not copy
|
||||
PacketWriter(const PacketWriter&);
|
||||
PacketWriter& operator=(const PacketWriter&);
|
||||
|
||||
@@ -66,26 +66,22 @@ public:
|
||||
/**
|
||||
* Default constructs the cached PDU.
|
||||
*/
|
||||
PDUCacher() {}
|
||||
PDUCacher() : cached_size() {}
|
||||
|
||||
/**
|
||||
* Constructor from a cached_type.
|
||||
* \param pdu The PDU to be copy constructed.
|
||||
*/
|
||||
PDUCacher(const cached_type &pdu) : cached(pdu) {}
|
||||
PDUCacher(const cached_type &pdu) : cached(pdu),
|
||||
cached_size() {}
|
||||
|
||||
/**
|
||||
* Forwards the call to the cached PDU. \sa PDU::header_size.
|
||||
*/
|
||||
uint32_t header_size() const {
|
||||
return cached.header_size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards the call to the cached PDU. \sa PDU::trailer_size.
|
||||
*/
|
||||
uint32_t trailer_size() const {
|
||||
return cached.trailer_size();
|
||||
if(cached_serialization.empty())
|
||||
cached_size = cached.size();
|
||||
return cached_size;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,8 +94,8 @@ public:
|
||||
/**
|
||||
* Forwards the call to the cached PDU. \sa PDU::send.
|
||||
*/
|
||||
bool send(PacketSender &sender) {
|
||||
return cached.send(sender);
|
||||
void send(PacketSender &sender) {
|
||||
cached.send(sender);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,13 +134,15 @@ public:
|
||||
}
|
||||
private:
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
if(cached_serialization.size() != total_sz)
|
||||
if(cached_serialization.size() != total_sz) {
|
||||
cached_serialization = cached.serialize();
|
||||
}
|
||||
std::copy(cached_serialization.begin(), cached_serialization.end(), buffer);
|
||||
}
|
||||
|
||||
cached_type cached;
|
||||
PDU::serialization_type cached_serialization;
|
||||
mutable uint32_t cached_size;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,20 @@
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Tins {
|
||||
class value_too_large : public std::exception {
|
||||
public:
|
||||
const char *what() const throw() {
|
||||
return "Value is too large";
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* \class small_uint
|
||||
* \brief Represents a field of <i>n</i> bits.
|
||||
*
|
||||
* This finds the best integral type of at least <i>n</i> bits and
|
||||
* uses it to store the wrapped value.
|
||||
*/
|
||||
template<size_t n>
|
||||
class small_uint {
|
||||
private:
|
||||
@@ -74,24 +88,38 @@ private:
|
||||
static const uint64_t value = 1;
|
||||
};
|
||||
public:
|
||||
class value_to_large : public std::exception {
|
||||
public:
|
||||
const char *what() const throw() {
|
||||
return "Value is too large";
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The type used to store the value.
|
||||
*/
|
||||
typedef typename best_type<n>::type repr_type;
|
||||
|
||||
/**
|
||||
* The maximum value this class can hold.
|
||||
*/
|
||||
static const repr_type max_value = power<2, n>::value - 1;
|
||||
|
||||
/**
|
||||
* Value initializes the value.
|
||||
*/
|
||||
small_uint() : value() {}
|
||||
|
||||
/**
|
||||
* \brief Copy constructs the stored value.
|
||||
*
|
||||
* This throws a value_too_large exception if the value provided
|
||||
* is larger than max_value.
|
||||
*
|
||||
* \param val The parameter from which to copy construct.
|
||||
*/
|
||||
small_uint(repr_type val) {
|
||||
if(val > max_value)
|
||||
throw value_to_large();
|
||||
throw value_too_large();
|
||||
value = val;
|
||||
}
|
||||
|
||||
/**
|
||||
* User defined conversion to repr_type.
|
||||
*/
|
||||
operator repr_type() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include <stdint.h>
|
||||
#include "sniffer.h"
|
||||
#include "tcp.h"
|
||||
#include "utils.h"
|
||||
#include "ip.h"
|
||||
#include "ip_address.h"
|
||||
|
||||
@@ -297,37 +298,6 @@ private:
|
||||
EndFunctor end_fun;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct is_pdu {
|
||||
template <typename U>
|
||||
static char test(typename U::PDUType*);
|
||||
|
||||
template <typename U>
|
||||
static long test(...);
|
||||
|
||||
static const bool value = sizeof(test<T>(0)) == 1;
|
||||
};
|
||||
|
||||
template<bool, typename>
|
||||
struct enable_if {
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct enable_if<true, T> {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
static PDU& recursive_dereference(PDU &pdu) {
|
||||
return pdu;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static typename enable_if<!is_pdu<T>::value, PDU&>::type
|
||||
recursive_dereference(T &value) {
|
||||
return recursive_dereference(*value);
|
||||
}
|
||||
|
||||
void clear_state() {
|
||||
sessions.clear();
|
||||
last_identifier = 0;
|
||||
@@ -355,7 +325,7 @@ void TCPStreamFollower::follow_streams(ForwardIterator start, ForwardIterator en
|
||||
{
|
||||
clear_state();
|
||||
while(start != end) {
|
||||
if(!callback(recursive_dereference(start), data_fun, end_fun))
|
||||
if(!callback(Utils::dereference_until_pdu(start), data_fun, end_fun))
|
||||
return;
|
||||
start++;
|
||||
}
|
||||
|
||||
@@ -54,5 +54,6 @@
|
||||
#include "tcp_stream.h"
|
||||
#include "crypto.h"
|
||||
#include "pdu_cacher.h"
|
||||
#include "rsn_information.h"
|
||||
|
||||
#endif // TINS_TINS_H
|
||||
|
||||
@@ -228,9 +228,57 @@ namespace Tins {
|
||||
}
|
||||
#endif // WIN32
|
||||
|
||||
/**
|
||||
* \cond
|
||||
*/
|
||||
namespace Internals {
|
||||
void skip_line(std::istream &input);
|
||||
bool from_hex(const std::string &str, uint32_t &result);
|
||||
|
||||
template<bool, typename>
|
||||
struct enable_if {
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct enable_if<true, T> {
|
||||
typedef T type;
|
||||
};
|
||||
}
|
||||
/**
|
||||
* \endcond
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
struct is_pdu {
|
||||
template <typename U>
|
||||
static char test(typename U::PDUType*);
|
||||
|
||||
template <typename U>
|
||||
static long test(...);
|
||||
|
||||
static const bool value = sizeof(test<T>(0)) == 1;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the argument.
|
||||
*/
|
||||
inline PDU& dereference_until_pdu(PDU &pdu) {
|
||||
return pdu;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Dereferences the parameter until a PDU is found.
|
||||
*
|
||||
* This function dereferences the parameter until a PDU object
|
||||
* is found. When it's found, it is returned.
|
||||
*
|
||||
* \param value The parameter to be dereferenced.
|
||||
*/
|
||||
template<typename T>
|
||||
inline typename Internals::enable_if<!is_pdu<T>::value, PDU&>::type
|
||||
dereference_until_pdu(T &value) {
|
||||
return dereference_until_pdu(*value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user