From 3ef85aae3881cf4a614c7fc9d09a25aa83ccd6f9 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Wed, 30 Jul 2014 23:01:03 -0300 Subject: [PATCH] Added BaseSniffer::set_extract_raw_pdus. --- include/sniffer.h | 17 +++++++++++++++++ src/sniffer.cpp | 11 +++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/sniffer.h b/include/sniffer.h index 5dbacf2..854dabd 100644 --- a/include/sniffer.h +++ b/include/sniffer.h @@ -200,6 +200,22 @@ namespace Tins { */ void set_timeout(int ms); + /** + * \brief Sets whether to extract RawPDUs or fully parsed packets. + * + * By default, packets will be parsed starting from link layer. + * However, if you're parsing a lot of traffic, then you might + * want to extract packets and push them into a queue, + * so a consumer can parse them when they're popped. + * + * This method allows doing that. If the parameter is true, + * then packets taken from this BaseSniffer will only contain + * a RawPDU which will have to entire contents of the packet. + * + * \param value Whether to extract RawPDUs or not. + */ + void set_extract_raw_pdus(bool value); + /** * \brief Retrieves this sniffer's link type. * @@ -239,6 +255,7 @@ namespace Tins { pcap_t *handle; bpf_u_int32 mask; + bool extract_raw; }; /** diff --git a/src/sniffer.cpp b/src/sniffer.cpp index 6a1fa60..d446f61 100644 --- a/src/sniffer.cpp +++ b/src/sniffer.cpp @@ -33,6 +33,7 @@ #include "ethernetII.h" #include "radiotap.h" #include "loopback.h" +#include "rawpdu.h" #include "dot3.h" #include "sll.h" #include "ppi.h" @@ -42,7 +43,7 @@ using std::runtime_error; namespace Tins { BaseSniffer::BaseSniffer() -: handle(0), mask(0) +: handle(0), mask(0), extract_raw(false) { } @@ -116,7 +117,9 @@ PtrPacket BaseSniffer::next_packet() { sniff_data data; const int iface_type = pcap_datalink(handle); pcap_handler handler = 0; - if(iface_type == DLT_EN10MB) + if(extract_raw) + handler = &sniff_loop_handler; + else if(iface_type == DLT_EN10MB) handler = sniff_loop_eth_handler; else if(iface_type == DLT_IEEE802_11_RADIO) { #ifdef HAVE_DOT11 @@ -149,6 +152,10 @@ PtrPacket BaseSniffer::next_packet() { return PtrPacket(data.pdu, data.tv); } +void BaseSniffer::set_extract_raw_pdus(bool value) { + extract_raw = value; +} + void BaseSniffer::stop_sniff() { pcap_breakloop(handle); }