| 3277 | } |
| 3278 | |
| 3279 | void CConnman::ThreadPrivateBroadcast() |
| 3280 | { |
| 3281 | AssertLockNotHeld(m_nodes_mutex); |
| 3282 | AssertLockNotHeld(m_unused_i2p_sessions_mutex); |
| 3283 | |
| 3284 | size_t addrman_num_bad_addresses{0}; |
| 3285 | while (!m_interrupt_net->interrupted()) { |
| 3286 | |
| 3287 | if (!fNetworkActive) { |
| 3288 | m_interrupt_net->sleep_for(5s); |
| 3289 | continue; |
| 3290 | } |
| 3291 | |
| 3292 | CountingSemaphoreGrant<> conn_max_grant{m_private_broadcast.m_sem_conn_max}; // Would block if too many are opened. |
| 3293 | |
| 3294 | m_private_broadcast.NumToOpenWait(); |
| 3295 | |
| 3296 | if (m_interrupt_net->interrupted()) { |
| 3297 | break; |
| 3298 | } |
| 3299 | |
| 3300 | std::optional<Proxy> proxy; |
| 3301 | const std::optional<Network> net{m_private_broadcast.PickNetwork(proxy)}; |
| 3302 | if (!net.has_value()) { |
| 3303 | LogWarning("Unable to open -privatebroadcast connections: neither Tor nor I2P is reachable"); |
| 3304 | m_interrupt_net->sleep_for(5s); |
| 3305 | continue; |
| 3306 | } |
| 3307 | |
| 3308 | const auto [addr, _] = addrman.get().Select(/*new_only=*/false, {net.value()}); |
| 3309 | |
| 3310 | if (!addr.IsValid() || IsLocal(addr)) { |
| 3311 | ++addrman_num_bad_addresses; |
| 3312 | if (addrman_num_bad_addresses > 100) { |
| 3313 | LogDebug(BCLog::PRIVBROADCAST, "Connections needed but addrman keeps returning bad addresses, will retry"); |
| 3314 | m_interrupt_net->sleep_for(500ms); |
| 3315 | } |
| 3316 | continue; |
| 3317 | } |
| 3318 | addrman_num_bad_addresses = 0; |
| 3319 | |
| 3320 | auto target_str{addr.ToStringAddrPort()}; |
| 3321 | if (proxy.has_value()) { |
| 3322 | target_str += " through the proxy at " + proxy->ToString(); |
| 3323 | } |
| 3324 | |
| 3325 | const bool use_v2transport(addr.nServices & GetLocalServices() & NODE_P2P_V2); |
| 3326 | |
| 3327 | if (OpenNetworkConnection(addr, |
| 3328 | /*fCountFailure=*/true, |
| 3329 | std::move(conn_max_grant), |
| 3330 | /*pszDest=*/nullptr, |
| 3331 | ConnectionType::PRIVATE_BROADCAST, |
| 3332 | use_v2transport, |
| 3333 | proxy)) { |
| 3334 | const size_t remaining{m_private_broadcast.NumToOpenSub(1)}; |
| 3335 | LogDebug(BCLog::PRIVBROADCAST, "Socket connected to %s; remaining connections to open: %d", target_str, remaining); |
| 3336 | } else { |
nothing calls this directly
no test coverage detected