#include "TinsNetworkInterfaceCard.h" #include #include #include #include #include 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(); snifferConfig->set_promisc_mode(true); snifferConfig->set_immediate_mode(true); sniffer = std::make_unique(networkInterface.name()); packetSender = std::make_unique(networkInterface); sendPduHashList = std::make_unique(); hashListMutex = std::make_unique(); } 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 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 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(); }