Skip to content

Commit

Permalink
Merge pull request #10 from Labonte-LucPaul/issue-9
Browse files Browse the repository at this point in the history
Issue-9: Attempt to fix infinite loop back of packet
  • Loading branch information
Labonte-LucPaul authored Nov 15, 2022
2 parents 9531d8e + 3d8a133 commit d78f062
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
34 changes: 15 additions & 19 deletions binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,23 @@ void Binder::stop() {

void Binder::snifferThread(const std::string &listenerIface, const std::string &senderIface) {
logger->info("Starting bind thread {} -> {}", listenerIface, senderIface);
Tins::SnifferConfiguration cfg;
cfg.set_immediate_mode(true);
std::scoped_lock<std::mutex> vec(pktMutex);
auto sniffer = sniffers.emplace_back(std::make_shared<Tins::Sniffer>(listenerIface, cfg));
pktMutex.unlock();
Tins::SnifferConfiguration configuration;
configuration.set_immediate_mode(true);
configuration.set_promisc_mode(true);
configuration.set_direction(pcap_direction_t::PCAP_D_IN);
auto sniffer = std::make_shared<Tins::Sniffer>(listenerIface, configuration);
{
std::scoped_lock<std::mutex> sniffer_mutex(pktMutex);
sniffers.push_back(sniffer);
}
Tins::PacketSender sender((Tins::NetworkInterface(senderIface)));
sniffer->sniff_loop([this, &sender, &listenerIface, &senderIface](Tins::PDU &pdu) {
const auto tmp = pdu.serialize();
{
std::scoped_lock<std::mutex> lock(pktMutex);
if (lastPkt != tmp) {
lastPkt = tmp;
logger->debug("Sending packet {} -> {}", listenerIface, senderIface);
sender.send(pdu);
++stats[listenerIface].received;
++stats[senderIface].sending;
} else {
lastPkt.clear();
}
return run.load();
}
std::scoped_lock<std::mutex> lock(pktMutex);
logger->debug("Sending packet {} -> {}", listenerIface, senderIface);
sender.send(pdu);
++stats[listenerIface].received;
++stats[senderIface].sending;
return run.load();
});
sniffer->stop_sniff();
logger->info("Stopping bind {} -> {}", listenerIface, senderIface);
Expand Down
1 change: 0 additions & 1 deletion binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class Binder {

std::thread iface2Handler;
mutable std::mutex pktMutex;
Tins::PDU::serialization_type lastPkt{};

std::vector<std::shared_ptr<Tins::Sniffer>> sniffers{};
std::map<std::string, Stats> stats;
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

using namespace std::chrono_literals;

constexpr auto VERSION = "2.3.0";
constexpr auto VERSION = "2.3.1";

using Interfaces = std::vector<std::pair<std::string, std::string>>;

Expand Down

0 comments on commit d78f062

Please sign in to comment.