mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Fix merge conflicts with master
Conflicts: src/pdu_option.cpp
This commit is contained in:
@@ -49,7 +49,7 @@ _build/lib_ directory.
|
||||
|
||||
### C++11 support
|
||||
|
||||
libtins is noticeable faster if you enable _C++11_ support. Therefore,
|
||||
libtins is noticeably faster if you enable _C++11_ support. Therefore,
|
||||
if your compiler supports this standard, then you should enable it.
|
||||
In order to do so, use the _LIBTINS_ENABLE_CXX11_ switch:
|
||||
|
||||
@@ -144,4 +144,4 @@ http://libtins.github.io/examples/
|
||||
## Contributing ##
|
||||
|
||||
If you want to report a bug or make a pull request, please have a look at
|
||||
the [contributing](CONTRIBUTING.md) file before doing so.
|
||||
the [contributing](CONTRIBUTING.md) file before doing so.
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
|
||||
#include <pcap.h>
|
||||
|
||||
|
||||
namespace Tins {
|
||||
class SnifferIterator;
|
||||
class SnifferConfiguration;
|
||||
@@ -72,7 +73,8 @@ public:
|
||||
* This constructor is available only in C++11.
|
||||
*/
|
||||
BaseSniffer(BaseSniffer &&rhs) TINS_NOEXCEPT
|
||||
: handle_(0), mask_(), extract_raw_(false) {
|
||||
: handle_(0), mask_(), extract_raw_(false),
|
||||
pcap_sniffing_method_(pcap_loop) {
|
||||
*this = std::move(rhs);
|
||||
}
|
||||
|
||||
@@ -85,6 +87,7 @@ public:
|
||||
swap(handle_, rhs.handle_);
|
||||
swap(mask_, rhs.mask_);
|
||||
swap(extract_raw_, rhs.extract_raw_);
|
||||
swap(pcap_sniffing_method_, rhs.pcap_sniffing_method_);
|
||||
return* this;
|
||||
}
|
||||
#endif
|
||||
@@ -237,6 +240,35 @@ public:
|
||||
*/
|
||||
void set_extract_raw_pdus(bool value);
|
||||
|
||||
/**
|
||||
* \brief function pointer for the sniffing method
|
||||
*
|
||||
* By default, libtins uses `pcap_loop` to sniff packets. With
|
||||
* `set_pcap_sniffing_method` it is possible to specify an alternative
|
||||
* sniffing method, for example `pcap_dispatch`, or a custom function.
|
||||
* This function pointer has the same interface as `pcap_loop` and
|
||||
* `pcap_dispatch`.
|
||||
*
|
||||
* \sa set_pcap_sniffing_method
|
||||
*/
|
||||
typedef int(*PcapSniffingMethod)(pcap_t*, int, pcap_handler, u_char*);
|
||||
|
||||
/**
|
||||
* \brief set sniffing method to either pcap_loop or pcap_dispatch.
|
||||
*
|
||||
* By default, packets are sniffed with `pcap_loop`, which only returns if
|
||||
* a packet is received, thus ignoring timeout expiration, if any is set.
|
||||
* With this method it is possible to pass an alternative sniffer function,
|
||||
* e.g. `pcap_dispatch`, that honors timeouts, or a custom function with
|
||||
* the same signature.
|
||||
*
|
||||
* See the relevant manual pages for pcap_loop and pcap_dispatch for more
|
||||
* information on their behavior
|
||||
*
|
||||
* \sa PcapSniffingMethod
|
||||
*/
|
||||
void set_pcap_sniffing_method(PcapSniffingMethod method);
|
||||
|
||||
/**
|
||||
* \brief Retrieves this sniffer's link type.
|
||||
*
|
||||
@@ -282,6 +314,7 @@ private:
|
||||
pcap_t* handle_;
|
||||
bpf_u_int32 mask_;
|
||||
bool extract_raw_;
|
||||
PcapSniffingMethod pcap_sniffing_method_;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -567,6 +600,12 @@ public:
|
||||
*/
|
||||
void set_filter(const std::string& filter);
|
||||
|
||||
/**
|
||||
* Sets the pcap sniffing method to use.
|
||||
* \param method The sniffing method to be used.
|
||||
*/
|
||||
void set_pcap_sniffing_method(BaseSniffer::PcapSniffingMethod method);
|
||||
|
||||
/**
|
||||
* Sets the rfmon option.
|
||||
* \param enabled The rfmon option value.
|
||||
@@ -608,6 +647,7 @@ protected:
|
||||
IMMEDIATE_MODE = 16,
|
||||
DIRECTION = 32,
|
||||
TIMESTAMP_PRECISION = 64,
|
||||
PCAP_SNIFFING_METHOD = 128,
|
||||
};
|
||||
|
||||
void configure_sniffer_pre_activation(Sniffer& sniffer) const;
|
||||
@@ -619,6 +659,7 @@ protected:
|
||||
unsigned snap_len_;
|
||||
unsigned buffer_size_;
|
||||
std::string filter_;
|
||||
BaseSniffer::PcapSniffingMethod pcap_sniffing_method_;
|
||||
unsigned timeout_;
|
||||
bool promisc_;
|
||||
bool rfmon_;
|
||||
|
||||
@@ -97,9 +97,7 @@ convert_vector(const uint8_t* ptr, uint32_t data_size, PDU::endian_type endian)
|
||||
while (input) {
|
||||
pair<T, U> data;
|
||||
data.first = input.read<T>();
|
||||
ptr += sizeof(T);
|
||||
data.second = input.read<U>();
|
||||
ptr += sizeof(U);
|
||||
if (endian == PDU::BE) {
|
||||
data.first = Endian::be_to_host(data.first);
|
||||
data.second = Endian::be_to_host(data.second);
|
||||
@@ -231,7 +229,6 @@ vector<IPv4Address> convert(const uint8_t* ptr, uint32_t data_size, PDU::endian_
|
||||
if (data_size % 4 != 0) {
|
||||
throw malformed_option();
|
||||
}
|
||||
|
||||
InputMemoryStream input(ptr, data_size);
|
||||
vector<IPv4Address> output(data_size / sizeof(uint32_t));
|
||||
vector<IPv4Address>::iterator it = output.begin();
|
||||
|
||||
@@ -212,7 +212,7 @@ PtrPacket BaseSniffer::next_packet() {
|
||||
// keep calling pcap_loop until a well-formed packet is found.
|
||||
while (data.pdu == 0 && data.packet_processed) {
|
||||
data.packet_processed = false;
|
||||
if (pcap_loop(handle_, 1, handler, (u_char*)&data) < 0) {
|
||||
if (pcap_sniffing_method_(handle_, 1, handler, (u_char*)&data) < 0) {
|
||||
return PtrPacket(0, Timestamp());
|
||||
}
|
||||
}
|
||||
@@ -223,6 +223,13 @@ void BaseSniffer::set_extract_raw_pdus(bool value) {
|
||||
extract_raw_ = value;
|
||||
}
|
||||
|
||||
void BaseSniffer::set_pcap_sniffing_method(PcapSniffingMethod method) {
|
||||
if (method == nullptr) {
|
||||
throw std::runtime_error("Sniffing method cannot be null");
|
||||
}
|
||||
pcap_sniffing_method_ = method;
|
||||
}
|
||||
|
||||
void BaseSniffer::stop_sniff() {
|
||||
pcap_breakloop(handle_);
|
||||
}
|
||||
@@ -419,8 +426,9 @@ const unsigned SnifferConfiguration::DEFAULT_SNAP_LEN = 65535;
|
||||
const unsigned SnifferConfiguration::DEFAULT_TIMEOUT = 1000;
|
||||
|
||||
SnifferConfiguration::SnifferConfiguration()
|
||||
: flags_(0), snap_len_(DEFAULT_SNAP_LEN), buffer_size_(0), timeout_(DEFAULT_TIMEOUT),
|
||||
promisc_(false), rfmon_(false), immediate_mode_(false), direction_(PCAP_D_INOUT),
|
||||
: flags_(0), snap_len_(DEFAULT_SNAP_LEN), buffer_size_(0),
|
||||
pcap_sniffing_method_(pcap_loop), timeout_(DEFAULT_TIMEOUT), promisc_(false),
|
||||
rfmon_(false), immediate_mode_(false), direction_(PCAP_D_INOUT),
|
||||
timestamp_precision_(0) {
|
||||
|
||||
}
|
||||
@@ -428,6 +436,7 @@ SnifferConfiguration::SnifferConfiguration()
|
||||
void SnifferConfiguration::configure_sniffer_pre_activation(Sniffer& sniffer) const {
|
||||
sniffer.set_snap_len(snap_len_);
|
||||
sniffer.set_timeout(timeout_);
|
||||
sniffer.set_pcap_sniffing_method(pcap_sniffing_method_);
|
||||
if ((flags_ & BUFFER_SIZE) != 0) {
|
||||
sniffer.set_buffer_size(buffer_size_);
|
||||
}
|
||||
@@ -488,6 +497,11 @@ void SnifferConfiguration::set_filter(const string& filter) {
|
||||
filter_ = filter;
|
||||
}
|
||||
|
||||
void SnifferConfiguration::set_pcap_sniffing_method(BaseSniffer::PcapSniffingMethod method) {
|
||||
flags_ |= PCAP_SNIFFING_METHOD;
|
||||
pcap_sniffing_method_ = method;
|
||||
}
|
||||
|
||||
void SnifferConfiguration::set_rfmon(bool enabled) {
|
||||
flags_ |= RFMON;
|
||||
rfmon_ = enabled;
|
||||
|
||||
Reference in New Issue
Block a user