add pdusniffer
This commit is contained in:
67
src/PduSniffer.cpp
Normal file
67
src/PduSniffer.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* File: PduSniffer.cpp
|
||||
* Author: dev
|
||||
*
|
||||
* Created on 29. September 2015, 21:29
|
||||
*/
|
||||
|
||||
#include "PduSniffer.h"
|
||||
|
||||
namespace otonat {
|
||||
|
||||
PduSniffer::PduSniffer(NatMap * map) {
|
||||
this->map = map;
|
||||
this->isRunnig = false;
|
||||
config.set_promisc_mode(true);
|
||||
config.set_immediate_mode(true);
|
||||
}
|
||||
|
||||
PduSniffer::PduSniffer(const PduSniffer& orig) : snifferList(orig.snifferList), config(orig.config) {
|
||||
this->map = orig.map;
|
||||
this->isRunnig = orig.isRunnig;
|
||||
}
|
||||
|
||||
PduSniffer::~PduSniffer() {
|
||||
Stop();
|
||||
for (Tins::Sniffer * sniffer : snifferList) {
|
||||
delete sniffer;
|
||||
}
|
||||
|
||||
this->snifferList.clear();
|
||||
}
|
||||
|
||||
PduSniffer& PduSniffer::operator=(const PduSniffer& rhs) {
|
||||
if (this == &rhs) return *this; // handle self assignment
|
||||
|
||||
this->map = rhs.map;
|
||||
this->isRunnig = rhs.isRunnig;
|
||||
this->config = rhs.config;
|
||||
this->snifferList = rhs.snifferList;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool PduSniffer::sniffPdu(const Tins::PDU& pdu) {
|
||||
this->map->pushPduToIncommingPduQueue(pdu.clone());
|
||||
return this->isRunnig;
|
||||
}
|
||||
|
||||
void PduSniffer::Start() {
|
||||
this->isRunnig = true;
|
||||
}
|
||||
|
||||
void PduSniffer::Stop() {
|
||||
this->isRunnig = false;
|
||||
}
|
||||
|
||||
void PduSniffer::SniffInterface(const Tins::NetworkInterface & interface) {
|
||||
Start();
|
||||
Tins::Sniffer * sniffer = new Tins::Sniffer(interface.name(), config);
|
||||
sniffer->sniff_loop(std::bind(&PduSniffer::sniffPdu, this, std::placeholders::_1));
|
||||
this->snifferList.push_back(sniffer);
|
||||
}
|
||||
|
||||
std::thread * PduSniffer::SniffInterfaceInNewThread(const Tins::NetworkInterface& interface){
|
||||
std::thread * newThread = new std::thread(std::bind(&PduSniffer::SniffInterface, this, interface));
|
||||
return newThread;
|
||||
}
|
||||
}
|
||||
40
src/PduSniffer.h
Normal file
40
src/PduSniffer.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* File: PduSniffer.h
|
||||
* Author: dev
|
||||
*
|
||||
* Created on 29. September 2015, 21:29
|
||||
*/
|
||||
|
||||
#ifndef PDUSNIFFER_H
|
||||
#define PDUSNIFFER_H
|
||||
|
||||
#include "map/natmap.h"
|
||||
#include <tins/tins.h>
|
||||
#include <thread>
|
||||
|
||||
namespace otonat {
|
||||
|
||||
class PduSniffer {
|
||||
public:
|
||||
typedef std::vector<Tins::Sniffer *> SnifferList;
|
||||
|
||||
PduSniffer(NatMap * map);
|
||||
PduSniffer(const PduSniffer& orig);
|
||||
PduSniffer& operator=(const PduSniffer& rhs);
|
||||
virtual ~PduSniffer();
|
||||
void SniffInterface(const Tins::NetworkInterface & interface);
|
||||
std::thread * SniffInterfaceInNewThread(const Tins::NetworkInterface & interface);
|
||||
void Start();
|
||||
void Stop();
|
||||
private:
|
||||
SnifferList snifferList;
|
||||
NatMap * map;
|
||||
Tins::SnifferConfiguration config;
|
||||
bool isRunnig;
|
||||
bool sniffPdu(const Tins::PDU &pdu);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* PDUSNIFFER_H */
|
||||
|
||||
Reference in New Issue
Block a user