add test fo receive and sending packets

This commit is contained in:
stubbfel
2017-03-16 23:36:14 +01:00
parent 76e86f556a
commit 0facb2f57b
4 changed files with 179 additions and 9 deletions

View File

@@ -32,40 +32,67 @@ TinsNetworkInterfaceCard::TinsNetworkInterfaceCard(const Tins::IPv6Address & ipv
}
TinsNetworkInterfaceCard::TinsNetworkInterfaceCard(const Tins::NetworkInterface & networkInterface)
TinsNetworkInterfaceCard::TinsNetworkInterfaceCard(const Tins::NetworkInterface & networkInterface) : sniffNetworkInterfaceName(networkInterface.name())
{
snifferConfig = std::make_unique<Tins::SnifferConfiguration>();
snifferConfig->set_promisc_mode(true);
snifferConfig->set_immediate_mode(true);
sniffer = std::make_unique<Tins::Sniffer>(networkInterface.name());
packetSender = std::make_unique<Tins::PacketSender>(networkInterface);
sendPduHashList = std::make_unique<HashList>();
hashListMutex = std::make_unique<std::mutex>();
isSnifferRunning = false;
}
TinsNetworkInterfaceCard::~TinsNetworkInterfaceCard()
{
stopListen();
}
bool TinsNetworkInterfaceCard::handle(const Tins::PDU &pdu, IPacketHandler *callBackHandler)
{
return false;
if (!isSnifferRunning)
{
return false;
}
Tins::PDU * clonePdu = pdu.clone();
if (clonePdu == nullptr)
{
return true;
}
//was the pdu sent by me?
Tins::PDU * phyLayerLessPdu = getPhyLessPduPtr(*clonePdu);
const ByteVector byteVector = phyLayerLessPdu->serialize();
delete clonePdu;
const std::size_t byteVectorHashValue = byteVectorHash(byteVector);
if(searchAndRemoveHashListItem(byteVectorHashValue))
{
return true;
}
for (SPtrIPacketHandler handler: handlerList)
{
handler->handle(pdu, nullptr);
}
return true;
}
void TinsNetworkInterfaceCard::sendPacket(const Tins::PDU &pdu)
{
Tins::PDU * clonePdu = pdu.clone();
if (clonePdu == nullptr){
if (clonePdu == nullptr)
{
return;
}
Tins::PDU & clonePduRef = *clonePdu;
Tins::PDU * phyLayerLessPdu = getPhyLessPduPtr(clonePduRef);
// layer3 only
if (phyLayerLessPdu != nullptr)
{
addPduToHashList(*phyLayerLessPdu);
packetSender->send(clonePduRef);
}
@@ -74,9 +101,14 @@ void TinsNetworkInterfaceCard::sendPacket(const Tins::PDU &pdu)
void TinsNetworkInterfaceCard::addPduToHashList(IN Tins::PDU &pdu)
{
std::lock_guard<std::mutex> lockGuard(*hashListMutex);
if (!isSnifferRunning)
{
return;
}
const ByteVector byteVector = pdu.serialize();
const std::size_t byteVectorHashValue = byteVectorHash(byteVector);
std::lock_guard<std::mutex> lockGuard(*hashListMutex);
sendPduHashList->push_back(byteVectorHashValue);
}
@@ -97,12 +129,28 @@ bool TinsNetworkInterfaceCard::searchAndRemoveHashListItem(IN const std::size_t
void TinsNetworkInterfaceCard::startListen()
{
if (isSnifferRunning)
{
return;
}
UPtrSniffer sniffer = std::make_unique<Tins::Sniffer>(sniffNetworkInterfaceName, *snifferConfig);
isSnifferRunning = true;
sniffer->sniff_loop(make_sniffer_handler(this, &TinsNetworkInterfaceCard::tinsSnifferCallback));
}
void TinsNetworkInterfaceCard::stopListen()
{
if (!isSnifferRunning)
{
return;
}
isSnifferRunning = false;
Tins::EthernetII pkt = Tins::EthernetII();
packetSender->send(pkt);
std::lock_guard<std::mutex> lockGuard(*hashListMutex);
sendPduHashList->clear();
}
Tins::PDU * TinsNetworkInterfaceCard::getPhyLessPduPtr(IN Tins::PDU & pdu) const
@@ -115,3 +163,23 @@ Tins::PDU * TinsNetworkInterfaceCard::getPhyLessPduPtr(IN Tins::PDU & pdu) const
return getPhyLessPduPtr(*(ethPdu->inner_pdu()));
}
Tins::SnifferConfiguration & TinsNetworkInterfaceCard::getSnifferConfig() const
{
return *snifferConfig;
}
bool TinsNetworkInterfaceCard::tinsSnifferCallback(Tins::PDU &some_pdu)
{
return handle(some_pdu, nullptr);
}
SPtrIPacketHandlerList & TinsNetworkInterfaceCard::getHandlerList()
{
return handlerList;
}
bool TinsNetworkInterfaceCard::isSniffRunning() const
{
return isSnifferRunning;
}

View File

@@ -1,6 +1,7 @@
#ifndef TINSNETWORKINTERFACECARD_H
#define TINSNETWORKINTERFACECARD_H
#include <atomic>
#include "TinsNetworkInterfaceCard_t.h"
#include "INetworkInterfaceCard_t.h"
#include "INetworkInterfaceCard.h"
@@ -24,17 +25,24 @@ public:
virtual void startListen() override;
virtual void stopListen() override;
Tins::SnifferConfiguration & getSnifferConfig() const;
SPtrIPacketHandlerList & getHandlerList();
bool isSniffRunning() const;
private:
Tins::PDU * getPhyLessPduPtr(IN Tins::PDU & pdu) const;
bool tinsSnifferCallback(IN Tins::PDU & some_pdu);
Tins::PDU * getPhyLessPduPtr(IN Tins::PDU & pdu) const;
void addPduToHashList(IN Tins::PDU &pdu);
bool searchAndRemoveHashListItem(IN const std::size_t removedHash);
UPtrSniffer sniffer;
UPtrSnifferConfiguration snifferConfig;
UPtrPacketSender packetSender;
static ByteVectorHash byteVectorHash;
UPtrHashList sendPduHashList;
UPtrMutex hashListMutex;
std::atomic<bool> isSnifferRunning;
SPtrIPacketHandlerList handlerList;
std::string sniffNetworkInterfaceName;
};
#endif

View File

@@ -55,4 +55,8 @@ typedef std::unique_ptr<std::mutex> UPtrMutex;
typedef std::vector<size_t> HashList;
typedef std::shared_ptr<HashList> SPtrrHashList;
typedef std::unique_ptr<HashList> UPtrHashList;
class TinsNetworkInterfaceCard;
typedef std::shared_ptr<TinsNetworkInterfaceCard> SPtrTinsNetworkInterfaceCard;
#endif