| 239 | } |
| 240 | |
| 241 | std::optional<CService> GetLocalAddrForPeer(CNode& node) |
| 242 | { |
| 243 | CService addrLocal{GetLocalAddress(node)}; |
| 244 | // If discovery is enabled, sometimes give our peer the address it |
| 245 | // tells us that it sees us as in case it has a better idea of our |
| 246 | // address than we do. |
| 247 | FastRandomContext rng; |
| 248 | if (IsPeerAddrLocalGood(&node) && (!addrLocal.IsRoutable() || |
| 249 | rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0)) |
| 250 | { |
| 251 | if (node.IsInboundConn()) { |
| 252 | // For inbound connections, assume both the address and the port |
| 253 | // as seen from the peer. |
| 254 | addrLocal = CService{node.GetAddrLocal()}; |
| 255 | } else { |
| 256 | // For outbound connections, assume just the address as seen from |
| 257 | // the peer and leave the port in `addrLocal` as returned by |
| 258 | // `GetLocalAddress()` above. The peer has no way to observe our |
| 259 | // listening port when we have initiated the connection. |
| 260 | addrLocal.SetIP(node.GetAddrLocal()); |
| 261 | } |
| 262 | } |
| 263 | if (addrLocal.IsRoutable()) { |
| 264 | LogDebug(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToStringAddrPort(), node.GetId()); |
| 265 | return addrLocal; |
| 266 | } |
| 267 | // Address is unroutable. Don't advertise. |
| 268 | return std::nullopt; |
| 269 | } |
| 270 | |
| 271 | void ClearLocal() |
| 272 | { |