| 1885 | } |
| 1886 | |
| 1887 | bool CConnman::AddConnection(const std::string& address, ConnectionType conn_type, bool use_v2transport = false) |
| 1888 | { |
| 1889 | AssertLockNotHeld(m_nodes_mutex); |
| 1890 | AssertLockNotHeld(m_unused_i2p_sessions_mutex); |
| 1891 | std::optional<int> max_connections; |
| 1892 | switch (conn_type) { |
| 1893 | case ConnectionType::INBOUND: |
| 1894 | case ConnectionType::MANUAL: |
| 1895 | case ConnectionType::PRIVATE_BROADCAST: |
| 1896 | return false; |
| 1897 | case ConnectionType::OUTBOUND_FULL_RELAY: |
| 1898 | max_connections = m_max_outbound_full_relay; |
| 1899 | break; |
| 1900 | case ConnectionType::BLOCK_RELAY: |
| 1901 | max_connections = m_max_outbound_block_relay; |
| 1902 | break; |
| 1903 | // no limit for ADDR_FETCH because -seednode has no limit either |
| 1904 | case ConnectionType::ADDR_FETCH: |
| 1905 | break; |
| 1906 | // no limit for FEELER connections since they're short-lived |
| 1907 | case ConnectionType::FEELER: |
| 1908 | break; |
| 1909 | } // no default case, so the compiler can warn about missing cases |
| 1910 | |
| 1911 | // Count existing connections |
| 1912 | int existing_connections = WITH_LOCK(m_nodes_mutex, |
| 1913 | return std::count_if(m_nodes.begin(), m_nodes.end(), [conn_type](CNode* node) { return node->m_conn_type == conn_type; });); |
| 1914 | |
| 1915 | // Max connections of specified type already exist |
| 1916 | if (max_connections != std::nullopt && existing_connections >= max_connections) return false; |
| 1917 | |
| 1918 | // Max total outbound connections already exist |
| 1919 | CountingSemaphoreGrant<> grant(*semOutbound, true); |
| 1920 | if (!grant) return false; |
| 1921 | |
| 1922 | OpenNetworkConnection(/*addrConnect=*/CAddress{}, |
| 1923 | /*fCountFailure=*/false, |
| 1924 | /*grant_outbound=*/std::move(grant), |
| 1925 | /*pszDest=*/address.c_str(), |
| 1926 | /*conn_type=*/conn_type, |
| 1927 | /*use_v2transport=*/use_v2transport, |
| 1928 | /*proxy_override=*/std::nullopt); |
| 1929 | return true; |
| 1930 | } |
| 1931 | |
| 1932 | void CConnman::DisconnectNodes() |
| 1933 | { |
no test coverage detected