From 72018badff0496878dbcb3c71f0b73279bf918e5 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 16 May 2023 23:53:33 +0100 Subject: [PATCH] Codechange: Swap SocketList map key/value around. This map is used store socket and address together, and, other than checking that the address does not already have a socket, the data layout does not seem particularly important. However, as address is the key, technically it should not be modified, and address may self-modify itself during comparisons. --- src/network/core/address.cpp | 4 ++-- src/network/core/address.h | 2 +- src/network/core/tcp_listen.h | 6 +++--- src/network/core/udp.cpp | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index 9a2d3dc487..366c140e65 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -259,7 +259,7 @@ SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList * * of course totally unneeded ;) */ if (sockets != nullptr) { NetworkAddress address(runp->ai_addr, (int)runp->ai_addrlen); - if (sockets->Contains(address)) continue; + if (std::any_of(sockets->begin(), sockets->end(), [&address](const auto &p) { return p.second == address; })) continue; } sock = func(runp); if (sock == INVALID_SOCKET) continue; @@ -284,7 +284,7 @@ SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList * } NetworkAddress addr(runp->ai_addr, (int)runp->ai_addrlen); - (*sockets)[addr] = sock; + (*sockets)[sock] = addr; sock = INVALID_SOCKET; } freeaddrinfo (ai); diff --git a/src/network/core/address.h b/src/network/core/address.h index 7cf159fb5e..c82b42f556 100644 --- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -19,7 +19,7 @@ class NetworkAddress; typedef std::vector NetworkAddressList; ///< Type for a list of addresses. -typedef SmallMap SocketList; ///< Type for a mapping between address and socket. +typedef SmallMap SocketList; ///< Type for a mapping between address and socket. /** * Wrapper for (un)resolved network addresses; there's no reason to transform diff --git a/src/network/core/tcp_listen.h b/src/network/core/tcp_listen.h index 0c7b11df1f..4df240ae6a 100644 --- a/src/network/core/tcp_listen.h +++ b/src/network/core/tcp_listen.h @@ -114,7 +114,7 @@ public: /* take care of listener port */ for (auto &s : sockets) { - FD_SET(s.second, &read_fd); + FD_SET(s.first, &read_fd); } tv.tv_sec = tv.tv_usec = 0; // don't block at all. @@ -122,7 +122,7 @@ public: /* accept clients.. */ for (auto &s : sockets) { - if (FD_ISSET(s.second, &read_fd)) AcceptClient(s.second); + if (FD_ISSET(s.first, &read_fd)) AcceptClient(s.first); } /* read stuff from clients */ @@ -164,7 +164,7 @@ public: static void CloseListeners() { for (auto &s : sockets) { - closesocket(s.second); + closesocket(s.first); } sockets.clear(); Debug(net, 5, "[{}] Closed listeners", Tsocket::GetName()); diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp index f1f74bcf34..9f4a09e67c 100644 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -59,7 +59,7 @@ bool NetworkUDPSocketHandler::Listen() void NetworkUDPSocketHandler::CloseSocket() { for (auto &s : this->sockets) { - closesocket(s.second); + closesocket(s.first); } this->sockets.clear(); } @@ -81,20 +81,20 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool a NetworkAddress send(*recv); /* Not the same type */ - if (!send.IsFamily(s.first.GetAddress()->ss_family)) continue; + if (!send.IsFamily(s.second.GetAddress()->ss_family)) continue; p->PrepareToSend(); if (broadcast) { /* Enable broadcast */ unsigned long val = 1; - if (setsockopt(s.second, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val)) < 0) { + if (setsockopt(s.first, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val)) < 0) { Debug(net, 1, "Setting broadcast mode failed: {}", NetworkError::GetLast().AsString()); } } /* Send the buffer */ - ssize_t res = p->TransferOut(sendto, s.second, 0, (const struct sockaddr *)send.GetAddress(), send.GetAddressLength()); + ssize_t res = p->TransferOut(sendto, s.first, 0, (const struct sockaddr *)send.GetAddress(), send.GetAddressLength()); Debug(net, 7, "sendto({})", send.GetAddressAsString()); /* Check for any errors, but ignore it otherwise */ @@ -119,8 +119,8 @@ void NetworkUDPSocketHandler::ReceivePackets() socklen_t client_len = sizeof(client_addr); /* Try to receive anything */ - SetNonBlocking(s.second); // Some OSes seem to lose the non-blocking status of the socket - ssize_t nbytes = p.TransferIn(recvfrom, s.second, 0, (struct sockaddr *)&client_addr, &client_len); + SetNonBlocking(s.first); // Some OSes seem to lose the non-blocking status of the socket + ssize_t nbytes = p.TransferIn(recvfrom, s.first, 0, (struct sockaddr *)&client_addr, &client_len); /* Did we get the bytes for the base header of the packet? */ if (nbytes <= 0) break; // No data, i.e. no packet