summaryrefslogtreecommitdiff
path: root/src/network.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/network.cpp')
-rw-r--r--src/network.cpp17
1 files changed, 14 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)