31 #ifndef TINS_SNIFFER_H 32 #define TINS_SNIFFER_H 38 #include <tins/packet.h> 39 #include <tins/cxxstd.h> 40 #include <tins/macros.h> 41 #include <tins/exceptions.h> 42 #include <tins/detail/type_traits.h> 50 class SnifferIterator;
51 class SnifferConfiguration;
63 class TINS_API BaseSniffer {
68 typedef SnifferIterator iterator;
75 BaseSniffer(BaseSniffer &&rhs) TINS_NOEXCEPT
76 : handle_(0), mask_(), extract_raw_(false),
77 pcap_sniffing_method_(pcap_loop) {
78 *
this = std::move(rhs);
85 BaseSniffer& operator=(BaseSniffer &&rhs) TINS_NOEXCEPT {
87 swap(handle_, rhs.handle_);
88 swap(mask_, rhs.mask_);
89 swap(extract_raw_, rhs.extract_raw_);
90 swap(pcap_sniffing_method_, rhs.pcap_sniffing_method_);
99 virtual ~BaseSniffer();
188 template <
typename Functor>
189 void sniff_loop(Functor
function, uint32_t max_packets = 0);
196 bool set_filter(
const std::string& filter);
217 bool set_direction(pcap_direction_t d);
225 void set_timeout(
int ms);
241 void set_extract_raw_pdus(
bool value);
254 typedef int(*PcapSniffingMethod)(pcap_t*, int, pcap_handler, u_char*);
270 void set_pcap_sniffing_method(PcapSniffingMethod method);
278 int link_type()
const;
293 pcap_t* get_pcap_handle();
298 const pcap_t* get_pcap_handle()
const;
305 void set_pcap_handle(pcap_t* pcap_handle);
307 void set_if_mask(bpf_u_int32 if_mask);
309 bpf_u_int32 get_if_mask()
const;
311 BaseSniffer(
const BaseSniffer&);
312 BaseSniffer& operator=(
const BaseSniffer&);
317 PcapSniffingMethod pcap_sniffing_method_;
324 class TINS_API Sniffer :
public BaseSniffer {
340 Sniffer(
const std::string& device);
353 Sniffer(
const std::string& device,
const SnifferConfiguration& configuration);
369 TINS_DEPRECATED(Sniffer(
const std::string& device,
unsigned max_packet_size,
370 bool promisc =
false,
const std::string& filter =
"",
bool rfmon =
false));
385 TINS_DEPRECATED(Sniffer(
const std::string& device, promisc_type promisc,
386 const std::string& filter =
"",
bool rfmon =
false));
389 friend class SnifferConfiguration;
391 void init(
const std::string& device,
const SnifferConfiguration& configuration);
392 void set_snap_len(
unsigned snap_len);
393 void set_buffer_size(
unsigned buffer_size);
394 void set_promisc_mode(
bool promisc_enabled);
395 void set_rfmon(
bool rfmon_enabled);
396 void set_immediate_mode(
bool enabled);
397 void set_timestamp_precision(
int value);
407 class TINS_API FileSniffer :
public BaseSniffer {
414 FileSniffer(
const std::string& file_name,
const SnifferConfiguration& configuration);
423 FileSniffer(
const std::string& file_name,
const std::string& filter =
"");
426 template <
typename T>
430 typedef bool (T::*fun_type)(PDU&) ;
432 HandlerProxy(ptr_type ptr, fun_type
function)
433 : object_(ptr), fun_(function) {}
435 bool operator()(PDU& pdu) {
436 return (object_->*fun_)(pdu);
443 template <
typename T>
444 HandlerProxy<T> make_sniffer_handler(T* ptr,
445 typename HandlerProxy<T>::fun_type
function) {
446 return HandlerProxy<T>(ptr,
function);
452 class SnifferIterator :
public std::iterator<std::forward_iterator_tag, Packet> {
458 SnifferIterator(BaseSniffer* sniffer = 0)
459 : sniffer_(sniffer) {
468 SnifferIterator& operator++() {
476 SnifferIterator operator++(
int) {
477 SnifferIterator other(*
this);
486 Packet& operator*() {
494 Packet* operator->() {
502 bool operator==(
const SnifferIterator& rhs)
const {
503 return sniffer_ == rhs.sniffer_;
510 bool operator!=(
const SnifferIterator& rhs)
const {
511 return !(*
this == rhs);
515 pkt_ = sniffer_->next_packet();
521 BaseSniffer* sniffer_;
558 class TINS_API SnifferConfiguration {
565 static const unsigned DEFAULT_SNAP_LEN;
572 static const unsigned DEFAULT_TIMEOUT;
577 SnifferConfiguration();
583 void set_snap_len(
unsigned snap_len);
589 void set_buffer_size(
unsigned buffer_size);
595 void set_promisc_mode(
bool enabled);
601 void set_filter(
const std::string& filter);
607 void set_pcap_sniffing_method(BaseSniffer::PcapSniffingMethod method);
613 void set_rfmon(
bool enabled);
619 void set_timeout(
unsigned timeout);
625 void set_direction(pcap_direction_t direction);
631 void set_immediate_mode(
bool enabled);
637 void set_timestamp_precision(
int value);
639 friend class Sniffer;
640 friend class FileSniffer;
649 TIMESTAMP_PRECISION = 64,
650 PCAP_SNIFFING_METHOD = 128,
653 void configure_sniffer_pre_activation(Sniffer& sniffer)
const;
654 void configure_sniffer_pre_activation(FileSniffer& sniffer)
const;
656 void configure_sniffer_post_activation(Sniffer& sniffer)
const;
660 unsigned buffer_size_;
662 BaseSniffer::PcapSniffingMethod pcap_sniffing_method_;
666 bool immediate_mode_;
667 pcap_direction_t direction_;
668 int timestamp_precision_;
671 template <
typename Functor>
672 void Tins::BaseSniffer::sniff_loop(Functor
function, uint32_t max_packets) {
673 for(iterator it = begin(); it != end(); ++it) {
676 #if TINS_IS_CXX11 && !defined(_MSC_VER) 677 if (!Tins::Internals::invoke_loop_cb(
function, *it)) {
681 if (!
function(*it->pdu())) {
686 catch(malformed_packet&) { }
687 catch(pdu_not_found&) { }
688 if (max_packets && --max_packets == 0) {
696 #endif // TINS_HAVE_PCAP 698 #endif // TINS_SNIFFER_H bool operator==(const PDUIteratorBase< Concrete > &lhs, const PDUIteratorBase< Concrete > &rhs)
Definition: pdu_iterator.h:108
The Tins namespace.
Definition: address_range.h:38
bool operator!=(const PDUIteratorBase< Concrete > &lhs, const PDUIteratorBase< Concrete > &rhs)
Definition: pdu_iterator.h:121
PacketWrapper< PDU *, Timestamp > PtrPacket
Thin wrapper over a PDU pointer and a Timestamp.
Definition: packet.h:54