111 lines
2.8 KiB
C++
111 lines
2.8 KiB
C++
#include "TinsNetworkInterfaceCard.h"
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <tins/packet_sender.h>
|
|
#include <tins/network_interface.h>
|
|
#include <tins/sniffer.h>
|
|
|
|
ByteVectorHash TinsNetworkInterfaceCard::byteVectorHash;
|
|
|
|
TinsNetworkInterfaceCard::TinsNetworkInterfaceCard() : TinsNetworkInterfaceCard(Tins::NetworkInterface::default_interface())
|
|
{
|
|
}
|
|
|
|
TinsNetworkInterfaceCard::TinsNetworkInterfaceCard(const std::string& name) : TinsNetworkInterfaceCard(name.c_str())
|
|
{
|
|
|
|
}
|
|
|
|
TinsNetworkInterfaceCard::TinsNetworkInterfaceCard(const char* name) : TinsNetworkInterfaceCard(Tins::NetworkInterface(name))
|
|
{
|
|
|
|
}
|
|
|
|
TinsNetworkInterfaceCard::TinsNetworkInterfaceCard(const Tins::IPv4Address & ip) : TinsNetworkInterfaceCard(Tins::NetworkInterface(ip))
|
|
{
|
|
|
|
}
|
|
|
|
TinsNetworkInterfaceCard::TinsNetworkInterfaceCard(const Tins::IPv6Address & ipv6) : TinsNetworkInterfaceCard(Tins::NetworkInterface(ipv6))
|
|
{
|
|
|
|
}
|
|
|
|
TinsNetworkInterfaceCard::TinsNetworkInterfaceCard(const Tins::NetworkInterface & networkInterface)
|
|
{
|
|
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>();
|
|
}
|
|
|
|
TinsNetworkInterfaceCard::~TinsNetworkInterfaceCard()
|
|
{
|
|
}
|
|
|
|
bool TinsNetworkInterfaceCard::handle(const Tins::PDU &pdu, IPacketHandler *callBackHandler)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
void TinsNetworkInterfaceCard::sendPacket(const Tins::PDU &pdu)
|
|
{
|
|
Tins::PDU * clonePdu = pdu.clone();
|
|
if (clonePdu == nullptr){
|
|
return;
|
|
}
|
|
|
|
Tins::PDU & clonePduRef = *clonePdu;
|
|
Tins::PDU * phyLayerLessPdu = getPhyLessPduPtr(clonePduRef);
|
|
|
|
// layer3 only
|
|
if (phyLayerLessPdu != nullptr)
|
|
{
|
|
|
|
packetSender->send(clonePduRef);
|
|
}
|
|
|
|
delete clonePdu;
|
|
}
|
|
|
|
void TinsNetworkInterfaceCard::addPduToHashList(IN Tins::PDU &pdu)
|
|
{
|
|
std::lock_guard<std::mutex> lockGuard(*hashListMutex);
|
|
const ByteVector byteVector = pdu.serialize();
|
|
const std::size_t byteVectorHashValue = byteVectorHash(byteVector);
|
|
sendPduHashList->push_back(byteVectorHashValue);
|
|
}
|
|
|
|
bool TinsNetworkInterfaceCard::searchAndRemoveHashListItem(IN const std::size_t removedHash)
|
|
{
|
|
std::lock_guard<std::mutex> lockGuard(*hashListMutex);
|
|
const HashList::const_iterator start = sendPduHashList->cbegin();
|
|
const HashList::const_iterator end = sendPduHashList->cend();
|
|
const HashList::const_iterator findIt = std::find(start, end, removedHash);
|
|
if (findIt == end)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
sendPduHashList->erase(findIt);
|
|
return true;
|
|
}
|
|
|
|
void TinsNetworkInterfaceCard::startListen()
|
|
{
|
|
|
|
}
|
|
|
|
void TinsNetworkInterfaceCard::stopListen()
|
|
{
|
|
|
|
}
|
|
|
|
Tins::PDU * TinsNetworkInterfaceCard::getPhyLessPduPtr(IN const Tins::PDU & pdu) const
|
|
{
|
|
return pdu.inner_pdu();
|
|
}
|