1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-30 13:34:27 +01:00

Removed the useless PDU::flag member. Added a PDU concatenation operator.

This commit is contained in:
Matias Fontanini
2012-10-07 18:51:06 -03:00
parent da60d99f98
commit 153bcecc35
24 changed files with 169 additions and 89 deletions

View File

@@ -92,7 +92,8 @@ namespace Tins {
EAPOL,
RC4EAPOL,
RSNEAPOL,
DNS
DNS,
LOOPBACK
};
/** \brief PDU constructor
@@ -101,7 +102,7 @@ namespace Tins {
* \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(PDU *next_pdu = 0);
/** \brief PDU destructor.
*
@@ -126,12 +127,6 @@ namespace Tins {
*/
uint32_t size() const;
/**
* \brief Getter for this PDU's type flag identifier.
* \return The type flag identifier.
*/
uint32_t flag() const { return _flag; }
/**
* \brief Getter for the inner PDU.
* \return The current inner PDU. Might be 0.
@@ -153,10 +148,6 @@ namespace Tins {
*/
PDU *release_inner_pdu();
/** \brief Sets the flag identifier.
*/
void flag(uint32_t new_flag);
/**
* \brief Sets the child PDU.
*
@@ -310,20 +301,47 @@ namespace Tins {
* \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, const PDU *parent) = 0;
/**
* \brief Generic clone pdu method.
*/
template<class T>
T *do_clone() const {
T *new_pdu = new T(*static_cast<const T*>(this));
//new_pdu->copy_inner_pdu(*this);
return new_pdu;
}
private:
uint32_t _flag;
PDU *_inner_pdu;
};
/**
* \brief Concatenation operator.
*
* This operator concatenates several PDUs. A copy of the right
* operand is set at the end of the left one's inner PDU chain.
* This means that:
*
* IP some_ip = IP("127.0.0.1") / TCP(12, 13) / RawPDU("bleh");
*
* Works as expected, meaning the output PDU will look like the
* following:
*
* IP - TCP - RawPDU
*
* \param lop The left operand, which will be the one modified.
* \param rop The right operand, the one which will be appended
* to lop.
*/
template<typename T>
T &operator/= (T &lop, const PDU &rop) {
PDU *last = &lop;
while(last->inner_pdu())
last = last->inner_pdu();
last->inner_pdu(rop.clone());
return lop;
}
/**
* \brief Concatenation operator.
*
* \sa operator/=
*/
template<typename T>
T operator/ (T lop, const PDU &rop) {
lop /= rop;
return lop;
}
};
#endif // TINS_PDU_H

View File

@@ -23,6 +23,7 @@
#define TINS_RAWPDU_H
#include <vector>
#include <string>
#include "pdu.h"
namespace Tins {
@@ -53,6 +54,13 @@ namespace Tins {
* \param size The size of the payload.
*/
RawPDU(const uint8_t *pload, uint32_t size);
/**
* \brief Creates an instance of RawPDU from an input string.
*
* \param data The content of the payload.
*/
RawPDU(const std::string &data);
/**
* \brief Setter for the payload field

View File

@@ -31,6 +31,7 @@
#include "pdu.h"
#include "ethernetII.h"
#include "radiotap.h"
#include "loopback.h"
namespace Tins {
/**
@@ -118,11 +119,11 @@ namespace Tins {
struct LoopData {
pcap_t *handle;
Functor c_handler;
bool wired;
int iface_type;
LoopData(pcap_t *_handle, const Functor _handler,
bool is_wired)
: handle(_handle), c_handler(_handler), wired(is_wired) { }
int if_type)
: handle(_handle), c_handler(_handler), iface_type(if_type) { }
};
BaseSniffer(const BaseSniffer&);
@@ -139,7 +140,7 @@ namespace Tins {
pcap_t *handle;
bpf_u_int32 mask;
bpf_program actual_filter;
bool wired;
int iface_type;
};
/**
@@ -184,7 +185,7 @@ namespace Tins {
template<class Functor>
void Tins::BaseSniffer::sniff_loop(Functor function, uint32_t max_packets) {
LoopData<Functor> data(handle, function, wired);
LoopData<Functor> data(handle, function, iface_type);
pcap_loop(handle, max_packets, &BaseSniffer::callback_handler<Functor>, (u_char*)&data);
}
@@ -200,14 +201,13 @@ namespace Tins {
std::auto_ptr<PDU> pdu;
LoopData<Functor> *data = reinterpret_cast<LoopData<Functor>*>(args);
bool ret_val(false);
/*if(data->wired)
ret_val = data->c_handler(Tins::EthernetII((const uint8_t*)packet, header->caplen));
else
pdu.reset(new Tins::RadioTap((const uint8_t*)packet, header->caplen));*/
if(data->wired)
if(data->iface_type == DLT_EN10MB)
ret_val = call_functor<Tins::EthernetII>(data, packet, header->caplen);
else
else if(data->iface_type == DLT_IEEE802_11_RADIO)
ret_val = call_functor<Tins::RadioTap>(data, packet, header->caplen);
else if(data->iface_type == DLT_NULL)
ret_val = call_functor<Tins::Loopback>(data, packet, header->caplen);
if(!ret_val)
pcap_breakloop(data->handle);
}