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 | |
parent | d9ddd8746ca83f361ec7ffb9f7146f6a23553b93 (diff) |
Network: ignore packets sent by ourselves
-rw-r--r-- | src/network.cpp | 17 | ||||
-rw-r--r-- | src/network.h | 2 | ||||
-rw-r--r-- | test/attach.test | 2 | ||||
-rw-r--r-- | test/discovery.test | 2 | ||||
-rw-r--r-- | test/sync.test | 2 |
5 files changed, 16 insertions, 9 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 diff --git a/test/attach.test b/test/attach.test index 0c97fb1..4e2238e 100644 --- a/test/attach.test +++ b/test/attach.test @@ -9,9 +9,7 @@ test: send "start-server" to p2 expect /local-identity Device1 Owner/ from p1 expect /local-identity Device2/ from p2 - expect /peer [0-9]+ 192.168.0.11:29665/ from p1 expect /peer [0-9]+ 192.168.0.12:29665/ from p1 - expect /peer [0-9]+ 192.168.0.12:29665/ from p2 expect /peer [0-9]+ 192.168.0.11:29665/ from p2 expect /peer [0-9]+ Device2 Device2/ from p1 expect /peer [0-9]+ Owner Device1/ from p2 diff --git a/test/discovery.test b/test/discovery.test index 26e8eaf..aba6b15 100644 --- a/test/discovery.test +++ b/test/discovery.test @@ -5,9 +5,7 @@ test: send "create-identity Device2" to p2 send "start-server" to p1 send "start-server" to p2 - expect /peer [0-9]+ 192.168.0.11:29665/ from p1 expect /peer [0-9]+ 192.168.0.12:29665/ from p1 - expect /peer [0-9]+ 192.168.0.12:29665/ from p2 expect /peer [0-9]+ 192.168.0.11:29665/ from p2 expect /peer [0-9]+ Device2 Device2/ from p1 expect /peer [0-9]+ Device1 Device1/ from p2 diff --git a/test/sync.test b/test/sync.test index 1b243c2..2480ce9 100644 --- a/test/sync.test +++ b/test/sync.test @@ -12,9 +12,7 @@ test: expect /local-identity Device1 Owner/ from p1 expect /local-identity Device2/ from p2 expect /shared-identity Owner/ from p1 - expect /peer [0-9]+ 192.168.0.11:29665/ from p1 expect /peer [0-9]+ 192.168.0.12:29665/ from p1 - expect /peer [0-9]+ 192.168.0.12:29665/ from p2 expect /peer [0-9]+ 192.168.0.11:29665/ from p2 expect /peer [0-9]+ Device2 Device2/ from p1 expect /peer [0-9]+ Owner Device1/ from p2 |