From d61c1cd57fbd38a63834224de09b33fbcd95bca1 Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Sun, 20 Oct 2024 09:48:45 -0400 Subject: [PATCH] return actual NAK for uninitialized affiliations tables; use SO_REUSEADDR on InfluxDB to reduce the number of idiotic TIME_WAIT TCP sockets; --- src/fne/network/FNENetwork.cpp | 15 ++++++++++----- src/fne/network/influxdb/InfluxDB.h | 6 ++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/fne/network/FNENetwork.cpp b/src/fne/network/FNENetwork.cpp index 58855536..94ee5432 100644 --- a/src/fne/network/FNENetwork.cpp +++ b/src/fne/network/FNENetwork.cpp @@ -1070,7 +1070,8 @@ void* FNENetwork::threadedNetworkRx(void* arg) std::string ip = udp::Socket::address(req->address); lookups::AffiliationLookup* aff = network->m_peerAffiliations[peerId]; if (aff == nullptr) { - LogError(LOG_NET, "PEER %u (%s) has an invalid affiliations lookup? This shouldn't happen BUGBUG.", peerId, connection->identity().c_str()); + LogError(LOG_NET, "PEER %u (%s) has uninitialized affiliations lookup?", peerId, connection->identity().c_str()); + network->writePeerNAK(peerId, TAG_ANNOUNCE, NET_CONN_NAK_INVALID); } // validate peer (simple validation really) @@ -1093,7 +1094,8 @@ void* FNENetwork::threadedNetworkRx(void* arg) std::string ip = udp::Socket::address(req->address); lookups::AffiliationLookup* aff = network->m_peerAffiliations[peerId]; if (aff == nullptr) { - LogError(LOG_NET, "PEER %u (%s) has an invalid affiliations lookup? This shouldn't happen BUGBUG.", peerId, connection->identity().c_str()); + LogError(LOG_NET, "PEER %u (%s) has uninitialized affiliations lookup?", peerId, connection->identity().c_str()); + network->writePeerNAK(peerId, TAG_ANNOUNCE, NET_CONN_NAK_INVALID); } // validate peer (simple validation really) @@ -1114,7 +1116,8 @@ void* FNENetwork::threadedNetworkRx(void* arg) std::string ip = udp::Socket::address(req->address); lookups::AffiliationLookup* aff = network->m_peerAffiliations[peerId]; if (aff == nullptr) { - LogError(LOG_NET, "PEER %u (%s) has an invalid affiliations lookup? This shouldn't happen BUGBUG.", peerId, connection->identity().c_str()); + LogError(LOG_NET, "PEER %u (%s) has uninitialized affiliations lookup?", peerId, connection->identity().c_str()); + network->writePeerNAK(peerId, TAG_ANNOUNCE, NET_CONN_NAK_INVALID); } // validate peer (simple validation really) @@ -1135,7 +1138,8 @@ void* FNENetwork::threadedNetworkRx(void* arg) std::string ip = udp::Socket::address(req->address); lookups::AffiliationLookup* aff = network->m_peerAffiliations[peerId]; if (aff == nullptr) { - LogError(LOG_NET, "PEER %u (%s) has an invalid affiliations lookup? This shouldn't happen BUGBUG.", peerId, connection->identity().c_str()); + LogError(LOG_NET, "PEER %u (%s) has uninitialized affiliations lookup?", peerId, connection->identity().c_str()); + network->writePeerNAK(peerId, TAG_ANNOUNCE, NET_CONN_NAK_INVALID); } // validate peer (simple validation really) @@ -1159,7 +1163,8 @@ void* FNENetwork::threadedNetworkRx(void* arg) if (connection->connected() && connection->address() == ip) { lookups::AffiliationLookup* aff = network->m_peerAffiliations[peerId]; if (aff == nullptr) { - LogError(LOG_NET, "PEER %u (%s) has an invalid affiliations lookup? This shouldn't happen BUGBUG.", peerId, connection->identity().c_str()); + LogError(LOG_NET, "PEER %u (%s) has uninitialized affiliations lookup?", peerId, connection->identity().c_str()); + network->writePeerNAK(peerId, TAG_ANNOUNCE, NET_CONN_NAK_INVALID); } if (aff != nullptr) { diff --git a/src/fne/network/influxdb/InfluxDB.h b/src/fne/network/influxdb/InfluxDB.h index 0848e326..3868b208 100644 --- a/src/fne/network/influxdb/InfluxDB.h +++ b/src/fne/network/influxdb/InfluxDB.h @@ -189,6 +189,12 @@ namespace network return 1; } + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) { + LogError(LOG_NET, "Failed to connect to InfluxDB server, err: %d", errno); + closesocket(fd); + return 1; + } + // connect to the server ret = connect(fd, addr->ai_addr, addr->ai_addrlen); if (ret < 0) {