@@ -340,6 +340,22 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce)
340
340
return true ;
341
341
}
342
342
343
+ /* * Get the bind address for a socket as CAddress */
344
+ static CAddress GetBindAddress (SOCKET sock)
345
+ {
346
+ CAddress addr_bind;
347
+ struct sockaddr_storage sockaddr_bind;
348
+ socklen_t sockaddr_bind_len = sizeof (sockaddr_bind);
349
+ if (sock != INVALID_SOCKET) {
350
+ if (!getsockname (sock, (struct sockaddr *)&sockaddr_bind, &sockaddr_bind_len)) {
351
+ addr_bind.SetSockAddr ((const struct sockaddr *)&sockaddr_bind);
352
+ } else {
353
+ LogPrint (BCLog::NET, " Warning: getsockname failed\n " );
354
+ }
355
+ }
356
+ return addr_bind;
357
+ }
358
+
343
359
CNode* CConnman::ConnectNode (CAddress addrConnect, const char *pszDest, bool fCountFailure )
344
360
{
345
361
if (pszDest == NULL ) {
@@ -393,7 +409,8 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
393
409
// Add node
394
410
NodeId id = GetNewNodeId ();
395
411
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
396
- CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, pszDest ? pszDest : " " , false );
412
+ CAddress addr_bind = GetBindAddress (hSocket);
413
+ CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , false );
397
414
pnode->nServicesExpected = ServiceFlags (addrConnect.nServices & nRelevantServices);
398
415
pnode->AddRef ();
399
416
@@ -635,6 +652,7 @@ void CNode::copyStats(CNodeStats &stats)
635
652
stats.nodeid = this ->GetId ();
636
653
X (nServices);
637
654
X (addr);
655
+ X (addrBind);
638
656
{
639
657
LOCK (cs_filter);
640
658
X (fRelayTxes );
@@ -1036,9 +1054,11 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1036
1054
int nInbound = 0 ;
1037
1055
int nMaxInbound = nMaxConnections - (nMaxOutbound + nMaxFeeler);
1038
1056
1039
- if (hSocket != INVALID_SOCKET)
1040
- if (!addr.SetSockAddr ((const struct sockaddr *)&sockaddr))
1057
+ if (hSocket != INVALID_SOCKET) {
1058
+ if (!addr.SetSockAddr ((const struct sockaddr *)&sockaddr)) {
1041
1059
LogPrintf (" Warning: Unknown socket family\n " );
1060
+ }
1061
+ }
1042
1062
1043
1063
bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange (addr);
1044
1064
{
@@ -1092,8 +1112,9 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1092
1112
1093
1113
NodeId id = GetNewNodeId ();
1094
1114
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
1115
+ CAddress addr_bind = GetBindAddress (hSocket);
1095
1116
1096
- CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addr, CalculateKeyedNetGroup (addr), nonce, " " , true );
1117
+ CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addr, CalculateKeyedNetGroup (addr), nonce, addr_bind, " " , true );
1097
1118
pnode->AddRef ();
1098
1119
pnode->fWhitelisted = whitelisted;
1099
1120
GetNodeSignals ().InitializeNode (pnode, *this );
@@ -2639,9 +2660,10 @@ int CConnman::GetBestHeight() const
2639
2660
unsigned int CConnman::GetReceiveFloodSize () const { return nReceiveFloodSize; }
2640
2661
unsigned int CConnman::GetSendBufferSize () const { return nSendBufferMaxSize; }
2641
2662
2642
- CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string& addrNameIn, bool fInboundIn ) :
2663
+ CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string& addrNameIn, bool fInboundIn ) :
2643
2664
nTimeConnected(GetSystemTimeInSeconds()),
2644
2665
addr(addrIn),
2666
+ addrBind(addrBindIn),
2645
2667
fInbound(fInboundIn ),
2646
2668
nKeyedNetGroup(nKeyedNetGroupIn),
2647
2669
addrKnown(5000 , 0.001 ),
0 commit comments