MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / CreateNodeFromAcceptedSocket

Method CreateNodeFromAcceptedSocket

src/net.cpp:1775–1885  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1773}
1774
1775void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
1776 NetPermissionFlags permission_flags,
1777 const CService& addr_bind,
1778 const CService& addr)
1779{
1780 AssertLockNotHeld(m_nodes_mutex);
1781
1782 int nInbound = 0;
1783
1784 const bool inbound_onion = std::find(m_onion_binds.begin(), m_onion_binds.end(), addr_bind) != m_onion_binds.end();
1785
1786 // Tor inbound connections do not reveal the peer's actual network address.
1787 // Therefore do not apply address-based whitelist permissions to them.
1788 AddWhitelistPermissionFlags(permission_flags, inbound_onion ? std::optional<CNetAddr>{} : addr, vWhitelistedRangeIncoming);
1789
1790 {
1791 LOCK(m_nodes_mutex);
1792 for (const CNode* pnode : m_nodes) {
1793 if (pnode->IsInboundConn()) nInbound++;
1794 }
1795 }
1796
1797 if (!fNetworkActive) {
1798 LogDebug(BCLog::NET, "connection from %s dropped: not accepting new connections\n", addr.ToStringAddrPort());
1799 return;
1800 }
1801
1802 if (!sock->IsSelectable()) {
1803 LogInfo("connection from %s dropped: non-selectable socket\n", addr.ToStringAddrPort());
1804 return;
1805 }
1806
1807 // According to the internet TCP_NODELAY is not carried into accepted sockets
1808 // on all platforms. Set it again here just to be sure.
1809 const int on{1};
1810 if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
1811 LogDebug(BCLog::NET, "connection from %s: unable to set TCP_NODELAY, continuing anyway\n",
1812 addr.ToStringAddrPort());
1813 }
1814
1815 // Don't accept connections from banned peers.
1816 bool banned = m_banman && m_banman->IsBanned(addr);
1817 if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && banned)
1818 {
1819 LogDebug(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToStringAddrPort());
1820 return;
1821 }
1822
1823 // Only accept connections from discouraged peers if our inbound slots aren't (almost) full.
1824 bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
1825 if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= m_max_inbound && discouraged)
1826 {
1827 LogDebug(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToStringAddrPort());
1828 return;
1829 }
1830
1831 if (nInbound >= m_max_inbound)
1832 {

Callers

nothing calls this directly

Calls 15

RandAddEventFunction · 0.85
IsInboundConnMethod · 0.80
ToStringAddrPortMethod · 0.80
IsBannedMethod · 0.80
IsDiscouragedMethod · 0.80
GetNetClassMethod · 0.80
GetAddrBytesMethod · 0.80
GetPortMethod · 0.80
AddRefMethod · 0.80
GetIdMethod · 0.80

Tested by

no test coverage detected