diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2021-12-09 21:26:48 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2021-12-09 21:26:48 +0100 |
commit | 1cef5fc2744d2428f28976c7adb0f476d8bc3bcf (patch) | |
tree | 89633b5f3778865e0f9348d37442f63ccec11811 /src | |
parent | d9ddd8746ca83f361ec7ffb9f7146f6a23553b93 (diff) |
Network: ignore packets sent by ourselves
Diffstat (limited to 'src')
-rw-r--r-- | src/network.cpp | 17 | ||||
-rw-r--r-- | src/network.h | 2 |
2 files changed, 16 insertions, 3 deletions
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<LocalState> & 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<uint8_t> buf, decrypted, *current; unique_lock<mutex> 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) diff --git a/src/network.h b/src/network.h index b1ecf86..a59a1bc 100644 --- a/src/network.h +++ b/src/network.h @@ -147,6 +147,7 @@ struct Server::Priv void doListen(); void doAnnounce(); + bool isSelfAddress(const sockaddr_in & paddr); Peer & getPeer(const sockaddr_in & paddr); void handlePacket(Peer &, const TransportHeader &, ReplyBuilder &); @@ -173,6 +174,7 @@ struct Server::Priv vector<weak_ptr<WaitingRef>> waiting; int sock; + vector<in_addr> localAddresses; vector<in_addr> bcastAddresses; // Start destruction with finalizing services |