diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2021-04-13 15:11:20 +0200 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2022-06-20 14:51:48 +0200 |
commit | a8d6abba5ec4ae2a3375e9be0b739f298899eca2 (patch) | |
tree | a8cc4c86f40bffb2afa744fc6f74cfd5bc37b889 | |
parent | 748dbcd9f29dbe4110da8a06f08e3eefa95f5321 (diff) | |
download | bitcoin-a8d6abba5ec4ae2a3375e9be0b739f298899eca2.tar.xz |
net: change GetBindAddress() to take Sock argument
This avoids the direct call to `getsockname()` and allows mocking.
-rw-r--r-- | src/net.cpp | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/net.cpp b/src/net.cpp index d42f130af7..7499d4c72f 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -422,13 +422,13 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce) } /** Get the bind address for a socket as CAddress */ -static CAddress GetBindAddress(SOCKET sock) +static CAddress GetBindAddress(const Sock& sock) { CAddress addr_bind; struct sockaddr_storage sockaddr_bind; socklen_t sockaddr_bind_len = sizeof(sockaddr_bind); - if (sock != INVALID_SOCKET) { - if (!getsockname(sock, (struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) { + if (sock.Get() != INVALID_SOCKET) { + if (!sock.GetSockName((struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) { addr_bind.SetSockAddr((const struct sockaddr*)&sockaddr_bind); } else { LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "getsockname failed\n"); @@ -540,7 +540,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo NodeId id = GetNewNodeId(); uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize(); if (!addr_bind.IsValid()) { - addr_bind = GetBindAddress(sock->Get()); + addr_bind = GetBindAddress(*sock); } CNode* pnode = new CNode(id, nLocalServices, @@ -1154,7 +1154,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) { addr = CAddress{MaybeFlipIPv6toCJDNS(addr), NODE_NONE}; } - const CAddress addr_bind{MaybeFlipIPv6toCJDNS(GetBindAddress(sock->Get())), NODE_NONE}; + const CAddress addr_bind{MaybeFlipIPv6toCJDNS(GetBindAddress(*sock)), NODE_NONE}; NetPermissionFlags permissionFlags = NetPermissionFlags::None; hListenSocket.AddSocketPermissionFlags(permissionFlags); |