1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-26 20:01:35 +01:00

BaseSniffer is now iterable.

This commit is contained in:
Matias Fontanini
2013-07-24 23:32:33 -03:00
parent fb8fb92ee6
commit 21b80a7370
2 changed files with 101 additions and 0 deletions

View File

@@ -36,6 +36,7 @@
#include <string>
#include <memory>
#include <stdexcept>
#include <iterator>
#include "pdu.h"
#include "ethernetII.h"
#include "radiotap.h"
@@ -48,6 +49,8 @@
#include "exceptions.h"
namespace Tins {
class SnifferIterator;
/**
* \class BaseSniffer
* \brief Base class for sniffers.
@@ -60,6 +63,11 @@ namespace Tins {
*/
class BaseSniffer {
public:
/**
* The iterator type.
*/
typedef SnifferIterator iterator;
#if TINS_IS_CXX11
/**
* \brief Move constructor.
@@ -178,6 +186,16 @@ namespace Tins {
* \brief Gets the file descriptor associated with the sniffer.
*/
int get_fd();
/**
* Retrieves an iterator to the next packet in this sniffer.
*/
iterator begin();
/**
* Retrieves an end iterator.
*/
iterator end();
protected:
/**
* Default constructor.
@@ -353,6 +371,81 @@ namespace Tins {
{
return HandlerProxy<T>(ptr, function);
}
/**
* \brief Iterates over packets sniffed by a BaseSniffer.
*/
class SnifferIterator : public std::iterator<std::forward_iterator_tag, PDU> {
public:
/**
* Constructs a SnifferIterator.
* \param sniffer The sniffer to iterate.
*/
SnifferIterator(BaseSniffer *sniffer = 0)
: sniffer(sniffer)
{
if(sniffer)
advance();
}
/**
* Advances the iterator.
*/
SnifferIterator& operator++() {
advance();
return *this;
}
/**
* Advances the iterator.
*/
SnifferIterator operator++(int) {
SnifferIterator other(*this);
advance();
return other;
}
/**
* Dereferences the iterator.
* \return references to the current packet.
*/
PDU &operator*() {
return *pkt.pdu();
}
/**
* Dereferences the iterator.
* \return pointer to the current packet.
*/
PDU *operator->() {
return &(**this);
}
/**
* Compares this iterator for equality.
* \param rhs The iterator to be compared to.
*/
bool operator==(const SnifferIterator &rhs) {
return sniffer == rhs.sniffer;
}
/**
* Compares this iterator for in-equality.
* \param rhs The iterator to be compared to.
*/
bool operator!=(const SnifferIterator &rhs) {
return !(*this == rhs);
}
private:
void advance() {
pkt = sniffer->next_packet();
if(!pkt)
sniffer = 0;
}
BaseSniffer *sniffer;
Packet pkt;
};
}
#endif // TINS_SNIFFER_H

View File

@@ -101,6 +101,14 @@ int BaseSniffer::get_fd() {
return pcap_get_selectable_fd(handle);
}
BaseSniffer::iterator BaseSniffer::begin() {
return iterator(this);
}
BaseSniffer::iterator BaseSniffer::end() {
return iterator(0);
}
bool BaseSniffer::set_filter(const std::string &filter) {
if(actual_filter.bf_insns)
pcap_freecode(&actual_filter);