From 1cef5fc2744d2428f28976c7adb0f476d8bc3bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Thu, 9 Dec 2021 21:26:48 +0100 Subject: Network: ignore packets sent by ourselves --- src/network.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/network.cpp') diff --git a/src/network.cpp b/src/network.cpp index 1e95571..1d38bdd 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -222,6 +222,7 @@ Server::Priv::Priv(const Head & local, const Identity & self, for (struct ifaddrs * ifa = addrs.get(); ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET && ifa->ifa_flags & IFF_BROADCAST) { + localAddresses.push_back(((sockaddr_in*)ifa->ifa_addr)->sin_addr); bcastAddresses.push_back(((sockaddr_in*)ifa->ifa_broadaddr)->sin_addr); } } @@ -278,7 +279,7 @@ void Server::Priv::doListen() vector buf, decrypted, *current; unique_lock lock(dataMutex); - while (!finish) { + for (; !finish; lock.lock()) { sockaddr_in paddr; lock.unlock(); @@ -292,6 +293,9 @@ void Server::Priv::doListen() break; buf.resize(ret); + if (isSelfAddress(paddr)) + continue; + auto & peer = getPeer(paddr); current = &buf; @@ -334,8 +338,6 @@ void Server::Priv::doListen() } else { std::cerr << "invalid packet\n"; } - - lock.lock(); } } @@ -369,6 +371,15 @@ void Server::Priv::doAnnounce() } } +bool Server::Priv::isSelfAddress(const sockaddr_in & paddr) +{ + for (const auto & in : localAddresses) + if (in.s_addr == paddr.sin_addr.s_addr && + ntohs(paddr.sin_port) == discoveryPort) + return true; + return false; +} + Server::Peer & Server::Priv::getPeer(const sockaddr_in & paddr) { for (auto & peer : peers) -- cgit v1.2.3