6 #if defined(HAVE_CONFIG_H) 44 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS 53 #include <unordered_map> 131 m_addr_fetches.push_back(strDest);
137 for (
const std::string& bind_arg :
gArgs.
GetArgs(
"-bind")) {
138 constexpr uint16_t dummy_port = 0;
140 const std::optional<CService> bind_addr{
Lookup(bind_arg, dummy_port,
false)};
141 if (bind_addr.has_value() && bind_addr->GetPort() != dummy_port)
return bind_addr->GetPort();
146 for (
const std::string& whitebind_arg :
gArgs.
GetArgs(
"-whitebind")) {
163 if (!
fListen)
return std::nullopt;
165 std::optional<CService> addr;
167 int nBestReachability = -1;
170 for (
const auto& [local_addr, local_service_info] : mapLocalHost) {
178 const int nScore{local_service_info.nScore};
179 const int nReachability{local_addr.GetReachabilityFrom(peer.
addr)};
180 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) {
181 addr.emplace(
CService{local_addr, local_service_info.nPort});
182 nBestReachability = nReachability;
191 static std::vector<CAddress>
ConvertSeeds(
const std::vector<uint8_t> &vSeedsIn)
197 const auto one_week{7 * 24h};
198 std::vector<CAddress> vSeedsOut;
208 vSeedsOut.push_back(addr);
225 const auto it = mapLocalHost.find(addr);
226 return (it != mapLocalHost.end()) ? it->second.nScore : 0;
251 if (
node.IsInboundConn()) {
260 addrLocal.SetIP(
node.GetAddrLocal());
277 if (!addr.IsRoutable())
286 LogPrintf(
"AddLocal(%s,%i)\n", addr.ToStringAddrPort(), nScore);
290 const auto [it, is_newly_added] = mapLocalHost.emplace(addr,
LocalServiceInfo());
292 if (is_newly_added || nScore >= info.
nScore) {
293 info.
nScore = nScore + (is_newly_added ? 0 : 1);
294 info.
nPort = addr.GetPort();
310 mapLocalHost.erase(addr);
317 const auto it = mapLocalHost.find(addr);
318 if (it == mapLocalHost.end())
return false;
328 return mapLocalHost.count(addr) > 0;
334 for (
CNode* pnode : m_nodes) {
335 if (static_cast<CNetAddr>(pnode->addr) ==
ip) {
345 for (
CNode* pnode : m_nodes) {
346 if (pnode->m_addr_name == addrName) {
356 for (
CNode* pnode : m_nodes) {
357 if (static_cast<CService>(pnode->addr) == addr) {
372 for (
const CNode* pnode : m_nodes) {
373 if (!pnode->fSuccessfullyConnected && !pnode->IsInboundConn() && pnode->GetLocalNonce() ==
nonce)
383 struct sockaddr_storage sockaddr_bind;
384 socklen_t sockaddr_bind_len =
sizeof(sockaddr_bind);
385 if (!sock.
GetSockName((
struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) {
386 addr_bind.
SetSockAddr((
const struct sockaddr*)&sockaddr_bind);
398 if (pszDest ==
nullptr) {
406 LogPrintf(
"Failed to open new connection, already connected\n");
412 use_v2transport ?
"v2" :
"v1",
414 Ticks<HoursDouble>(pszDest ? 0h : Now<NodeSeconds>() - addrConnect.
nTime));
417 const uint16_t default_port{pszDest !=
nullptr ?
GetDefaultPort(pszDest) :
421 if (!resolved.empty()) {
433 LogPrintf(
"Failed to open new connection, already connected\n");
440 bool connected =
false;
441 std::unique_ptr<Sock> sock;
445 std::unique_ptr<i2p::sam::Session> i2p_transient_session;
449 bool proxyConnectionFailed =
false;
451 if (addrConnect.
IsI2P() && use_proxy) {
455 connected =
m_i2p_sam_session->Connect(addrConnect, conn, proxyConnectionFailed);
459 if (m_unused_i2p_sessions.empty()) {
460 i2p_transient_session =
463 i2p_transient_session.swap(m_unused_i2p_sessions.front());
464 m_unused_i2p_sessions.pop();
467 connected = i2p_transient_session->Connect(addrConnect, conn, proxyConnectionFailed);
471 m_unused_i2p_sessions.emplace(i2p_transient_session.release());
477 sock = std::move(conn.
sock);
480 }
else if (use_proxy) {
496 if (!proxyConnectionFailed) {
507 uint16_t port{default_port};
509 bool proxyConnectionFailed;
511 proxyConnectionFailed);
529 pszDest ? pszDest :
"",
535 .use_v2transport = use_v2transport,
553 m_i2p_sam_session.reset();
572 if (addrLocal.IsValid()) {
573 error(
"Addr local already set for node: %i. Refusing to change from %s to %s",
id, addrLocal.ToStringAddrPort(), addrLocalIn.
ToStringAddrPort());
575 addrLocal = addrLocalIn;
590 #define X(name) stats.name = name 614 X(mapSendBytesPerMsgType);
619 X(mapRecvBytesPerMsgType);
641 const auto time = GetTime<std::chrono::microseconds>();
643 m_last_recv = std::chrono::duration_cast<std::chrono::seconds>(time);
644 nRecvBytes += msg_bytes.
size();
645 while (msg_bytes.
size() > 0) {
654 bool reject_message{
false};
656 if (reject_message) {
665 auto i = mapRecvBytesPerMsgType.find(
msg.m_type);
666 if (i == mapRecvBytesPerMsgType.end()) {
669 assert(i != mapRecvBytesPerMsgType.end());
670 i->second +=
msg.m_raw_message_size;
683 m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
685 assert(std::size(
Params().MessageStart()) == std::size(m_magic_bytes));
701 unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.
size());
703 memcpy(&hdrbuf[nHdrPos], msg_bytes.
data(), nCopy);
714 catch (
const std::exception&) {
740 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
741 unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.
size());
743 if (vRecv.size() < nDataPos + nCopy) {
745 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
748 hasher.Write(msg_bytes.
first(nCopy));
749 memcpy(&vRecv[nDataPos], msg_bytes.
data(), nCopy);
759 if (data_hash.IsNull())
760 hasher.Finalize(data_hash);
768 reject_message =
false;
774 msg.m_type = hdr.GetCommand();
776 msg.m_message_size = hdr.nMessageSize;
786 LogPrint(
BCLog::NET,
"Header error: Wrong checksum (%s, %u bytes), expected %s was %s, peer=%d\n",
791 reject_message =
true;
792 }
else if (!hdr.IsCommandValid()) {
795 reject_message =
true;
808 if (m_sending_header || m_bytes_sent < m_message_to_send.data.size())
return false;
818 m_header_to_send.clear();
822 m_message_to_send = std::move(
msg);
823 m_sending_header =
true;
832 if (m_sending_header) {
833 return {
Span{m_header_to_send}.
subspan(m_bytes_sent),
836 have_next_message || !m_message_to_send.data.empty(),
837 m_message_to_send.m_type
840 return {
Span{m_message_to_send.
data}.subspan(m_bytes_sent),
844 m_message_to_send.m_type
853 m_bytes_sent += bytes_sent;
854 if (m_sending_header && m_bytes_sent == m_header_to_send.size()) {
856 m_sending_header =
false;
858 }
else if (!m_sending_header && m_bytes_sent == m_message_to_send.data.size()) {
870 return m_message_to_send.GetMemoryUsage();
880 const std::array<std::string, 33> V2_MESSAGE_IDS = {
919 std::unordered_map<std::string, uint8_t> m_map;
922 V2MessageMap() noexcept
924 for (
size_t i = 1; i < std::size(V2_MESSAGE_IDS); ++i) {
925 m_map.emplace(V2_MESSAGE_IDS[i], i);
929 std::optional<uint8_t> operator()(
const std::string& message_name)
const noexcept
931 auto it = m_map.find(message_name);
932 if (it == m_map.end())
return std::nullopt;
937 const V2MessageMap V2_MESSAGE_MAP;
939 CKey GenerateRandomKey() noexcept
946 std::vector<uint8_t> GenerateRandomGarbage() noexcept
948 std::vector<uint8_t>
ret;
961 Assume(m_send_buffer.empty());
965 std::copy(m_send_garbage.begin(), m_send_garbage.end(), m_send_buffer.begin() +
EllSwiftPubKey::size());
970 m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
971 m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
972 m_recv_state{initiating ?
RecvState::KEY : RecvState::KEY_MAYBE_V1},
973 m_send_garbage{std::move(garbage)},
974 m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
976 Assume(m_send_garbage.size() <= MAX_GARBAGE_LEN);
980 StartSendingHandshake();
985 V2Transport{nodeid, initiating, type_in, version_in, GenerateRandomKey(),
992 switch (m_recv_state) {
993 case RecvState::KEY_MAYBE_V1:
997 Assume(recv_state == RecvState::GARB_GARBTERM);
999 case RecvState::GARB_GARBTERM:
1003 Assume(recv_state == RecvState::APP);
1005 case RecvState::APP:
1006 Assume(recv_state == RecvState::APP_READY);
1008 case RecvState::APP_READY:
1009 Assume(recv_state == RecvState::APP);
1016 m_recv_state = recv_state;
1023 switch (m_send_state) {
1024 case SendState::MAYBE_V1:
1025 Assume(send_state == SendState::V1 || send_state == SendState::AWAITING_KEY);
1027 case SendState::AWAITING_KEY:
1028 Assume(send_state == SendState::READY);
1030 case SendState::READY:
1036 m_send_state = send_state;
1057 std::array<uint8_t, V1_PREFIX_LEN> v1_prefix = {0, 0, 0, 0,
'v',
'e',
'r',
's',
'i',
'o',
'n', 0, 0, 0, 0, 0};
1058 std::copy(std::begin(
Params().MessageStart()), std::end(
Params().MessageStart()), v1_prefix.begin());
1059 Assume(m_recv_buffer.size() <= v1_prefix.size());
1060 if (!std::equal(m_recv_buffer.begin(), m_recv_buffer.end(), v1_prefix.begin())) {
1067 }
else if (m_recv_buffer.size() == v1_prefix.size()) {
1074 Assume(feedback.empty());
1099 static constexpr std::array<uint8_t, 12> MATCH = {
'v',
'e',
'r',
's',
'i',
'o',
'n', 0, 0, 0, 0, 0};
1100 static constexpr
size_t OFFSET = std::tuple_size_v<MessageStartChars>;
1101 if (!
m_initiating && m_recv_buffer.size() >= OFFSET + MATCH.size()) {
1102 if (std::equal(MATCH.begin(), MATCH.end(), m_recv_buffer.begin() + OFFSET)) {
1120 m_recv_buffer.clear();
1154 m_recv_aad = std::move(m_recv_buffer);
1156 m_recv_buffer.clear();
1182 static constexpr
size_t MAX_CONTENTS_LEN =
1189 if (m_recv_len > MAX_CONTENTS_LEN) {
1197 m_recv_decode_buffer.resize(m_recv_len);
1216 switch (m_recv_state) {
1245 switch (m_recv_state) {
1295 static constexpr
size_t MAX_RESERVE_AHEAD = 256 * 1024;
1298 if (m_recv_state == RecvState::V1)
return m_v1_fallback.ReceivedBytes(msg_bytes);
1304 while (!msg_bytes.empty()) {
1306 size_t max_read = GetMaxBytesToProcess();
1309 if (m_recv_buffer.size() + std::min(msg_bytes.size(), max_read) > m_recv_buffer.capacity()) {
1310 switch (m_recv_state) {
1311 case RecvState::KEY_MAYBE_V1:
1313 case RecvState::GARB_GARBTERM:
1319 case RecvState::APP: {
1325 size_t alloc_add = std::min(max_read, msg_bytes.size() + MAX_RESERVE_AHEAD);
1326 m_recv_buffer.reserve(m_recv_buffer.size() + alloc_add);
1329 case RecvState::APP_READY:
1331 Assume(m_recv_buffer.empty());
1341 max_read = std::min(msg_bytes.size(), max_read);
1343 m_recv_buffer.insert(m_recv_buffer.end(),
UCharCast(msg_bytes.data()),
UCharCast(msg_bytes.data() + max_read));
1344 msg_bytes = msg_bytes.subspan(max_read);
1347 switch (m_recv_state) {
1348 case RecvState::KEY_MAYBE_V1:
1349 ProcessReceivedMaybeV1Bytes();
1350 if (m_recv_state == RecvState::V1)
return true;
1354 if (!ProcessReceivedKeyBytes())
return false;
1357 case RecvState::GARB_GARBTERM:
1358 if (!ProcessReceivedGarbageBytes())
return false;
1362 case RecvState::APP:
1363 if (!ProcessReceivedPacketBytes())
return false;
1366 case RecvState::APP_READY:
1383 if (contents.size() == 0)
return std::nullopt;
1384 uint8_t first_byte = contents[0];
1385 contents = contents.subspan(1);
1387 if (first_byte != 0) {
1389 if (first_byte < std::size(V2_MESSAGE_IDS)) {
1391 return V2_MESSAGE_IDS[first_byte];
1394 return std::nullopt;
1399 return std::nullopt;
1402 size_t msg_type_len{0};
1405 if (contents[msg_type_len] <
' ' || contents[msg_type_len] > 0x7F) {
1410 std::string
ret{
reinterpret_cast<const char*
>(contents.data()), msg_type_len};
1413 if (contents[msg_type_len] != 0)
return {};
1418 return {std::move(
ret)};
1425 if (m_recv_state == RecvState::V1)
return m_v1_fallback.GetReceivedMessage(time, reject_message);
1427 Assume(m_recv_state == RecvState::APP_READY);
1429 auto msg_type = GetMessageType(contents);
1435 reject_message =
false;
1436 msg.m_type = std::move(*msg_type);
1438 msg.m_message_size = contents.size();
1439 msg.m_recv.resize(contents.size());
1440 std::copy(contents.begin(), contents.end(),
UCharCast(
msg.m_recv.data()));
1442 LogPrint(
BCLog::NET,
"V2 transport error: invalid message type (%u bytes contents), peer=%d\n", m_recv_decode_buffer.size(), m_nodeid);
1443 reject_message =
true;
1446 SetReceiveState(RecvState::APP);
1455 if (m_send_state == SendState::V1)
return m_v1_fallback.SetMessageToSend(
msg);
1459 if (!(m_send_state == SendState::READY && m_send_buffer.empty()))
return false;
1461 std::vector<uint8_t> contents;
1462 auto short_message_id = V2_MESSAGE_MAP(
msg.m_type);
1463 if (short_message_id) {
1464 contents.resize(1 +
msg.data.size());
1465 contents[0] = *short_message_id;
1466 std::copy(
msg.data.begin(),
msg.data.end(), contents.begin() + 1);
1471 std::copy(
msg.m_type.begin(),
msg.m_type.end(), contents.data() + 1);
1477 m_send_type =
msg.m_type;
1487 if (m_send_state == SendState::V1)
return m_v1_fallback.GetBytesToSend(have_next_message);
1489 if (m_send_state == SendState::MAYBE_V1)
Assume(m_send_buffer.empty());
1490 Assume(m_send_pos <= m_send_buffer.size());
1495 have_next_message && m_send_state == SendState::READY,
1504 if (m_send_state == SendState::V1)
return m_v1_fallback.MarkBytesSent(bytes_sent);
1506 if (m_send_state == SendState::AWAITING_KEY && m_send_pos == 0 && bytes_sent > 0) {
1510 m_send_pos += bytes_sent;
1511 Assume(m_send_pos <= m_send_buffer.size());
1513 m_sent_v1_header_worth =
true;
1516 if (m_send_pos == m_send_buffer.size()) {
1533 if (!m_recv_buffer.empty())
return false;
1536 return m_sent_v1_header_worth;
1571 auto it =
node.vSendMsg.begin();
1572 size_t nSentSize = 0;
1573 bool data_left{
false};
1574 std::optional<bool> expected_more;
1577 if (it !=
node.vSendMsg.end()) {
1581 size_t memusage = it->GetMemoryUsage();
1582 if (
node.m_transport->SetMessageToSend(*it)) {
1588 const auto& [data, more, msg_type] =
node.m_transport->GetBytesToSend(it !=
node.vSendMsg.end());
1592 if (expected_more.has_value())
Assume(!data.empty() == *expected_more);
1593 expected_more = more;
1594 data_left = !data.empty();
1596 if (!data.empty()) {
1610 nBytes =
node.m_sock->Send(reinterpret_cast<const char*>(data.data()), data.size(),
flags);
1613 node.m_last_send = GetTime<std::chrono::seconds>();
1614 node.nSendBytes += nBytes;
1616 node.m_transport->MarkBytesSent(nBytes);
1618 if (!msg_type.empty()) {
1619 node.AccountForSentBytes(msg_type, nBytes);
1621 nSentSize += nBytes;
1622 if ((
size_t)nBytes != data.size()) {
1632 node.CloseSocketDisconnect();
1641 if (it ==
node.vSendMsg.end()) {
1644 node.vSendMsg.erase(
node.vSendMsg.begin(), it);
1645 return {nSentSize, data_left};
1658 std::vector<NodeEvictionCandidate> vEvictionCandidates;
1663 if (
node->fDisconnect)
1667 .m_connected =
node->m_connected,
1668 .m_min_ping_time =
node->m_min_ping_time,
1669 .m_last_block_time =
node->m_last_block_time,
1670 .m_last_tx_time =
node->m_last_tx_time,
1671 .fRelevantServices =
node->m_has_all_wanted_services,
1672 .m_relay_txs =
node->m_relays_txs.load(),
1673 .fBloomFilter =
node->m_bloom_filter_loaded.load(),
1674 .nKeyedNetGroup =
node->nKeyedNetGroup,
1675 .prefer_evict =
node->m_prefer_evict,
1676 .m_is_local =
node->addr.IsLocal(),
1677 .m_network =
node->ConnectedThroughNetwork(),
1679 .m_conn_type =
node->m_conn_type,
1681 vEvictionCandidates.push_back(candidate);
1684 const std::optional<NodeId> node_id_to_evict =
SelectNodeToEvict(std::move(vEvictionCandidates));
1685 if (!node_id_to_evict) {
1689 for (
CNode* pnode : m_nodes) {
1690 if (pnode->GetId() == *node_id_to_evict) {
1691 LogPrint(
BCLog::NET,
"selected %s connection for eviction peer=%d; disconnecting\n", pnode->ConnectionTypeAsString(), pnode->GetId());
1692 pnode->fDisconnect =
true;
1700 struct sockaddr_storage sockaddr;
1701 socklen_t len =
sizeof(sockaddr);
1702 auto sock = hListenSocket.
sock->Accept((
struct sockaddr*)&sockaddr, &len);
1713 if (!addr.
SetSockAddr((
const struct sockaddr*)&sockaddr)) {
1746 for (
const CNode* pnode : m_nodes) {
1747 if (pnode->IsInboundConn()) nInbound++;
1756 if (!sock->IsSelectable()) {
1764 if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on,
sizeof(on)) ==
SOCKET_ERROR) {
1765 LogPrint(
BCLog::NET,
"connection from %s: unable to set TCP_NODELAY, continuing anyway\n",
1785 if (nInbound >= nMaxInbound)
1789 LogPrint(
BCLog::NET,
"failed to find an eviction candidate - connection dropped (full)\n");
1805 const bool use_v2transport(nodeServices &
NODE_P2P_V2);
1818 .prefer_evict = discouraged,
1820 .use_v2transport = use_v2transport,
1823 m_msgproc->InitializeNode(*pnode, nodeServices);
1829 m_nodes.push_back(pnode);
1839 std::optional<int> max_connections;
1840 switch (conn_type) {
1860 return std::count_if(m_nodes.begin(), m_nodes.end(), [conn_type](
CNode*
node) {
return node->m_conn_type == conn_type; }););
1863 if (max_connections != std::nullopt && existing_connections >= max_connections)
return false;
1867 if (!grant)
return false;
1880 decltype(m_reconnections) reconnections_to_add;
1887 for (
CNode* pnode : m_nodes) {
1888 if (!pnode->fDisconnect) {
1890 pnode->fDisconnect =
true;
1896 std::vector<CNode*> nodes_copy = m_nodes;
1897 for (
CNode* pnode : nodes_copy)
1899 if (pnode->fDisconnect)
1902 m_nodes.erase(
remove(m_nodes.begin(), m_nodes.end(), pnode), m_nodes.end());
1907 if (pnode->m_transport->ShouldReconnectV1()) {
1908 reconnections_to_add.push_back({
1909 .addr_connect = pnode->addr,
1910 .grant = std::move(pnode->grantOutbound),
1911 .destination = pnode->m_dest,
1912 .conn_type = pnode->m_conn_type,
1913 .use_v2transport =
false});
1914 LogPrint(
BCLog::NET,
"retrying with v1 transport protocol for peer=%d\n", pnode->GetId());
1918 pnode->grantOutbound.Release();
1921 pnode->CloseSocketDisconnect();
1924 if (pnode->IsManualOrFullOutboundConn()) --m_network_conn_counts[pnode->addr.GetNetwork()];
1935 for (
CNode* pnode : nodes_disconnected_copy)
1938 if (pnode->GetRefCount() <= 0) {
1947 m_reconnections.splice(m_reconnections.end(), std::move(reconnections_to_add));
1956 nodes_size = m_nodes.size();
1975 const auto now{GetTime<std::chrono::seconds>()};
1976 const auto last_send{
node.m_last_send.load()};
1977 const auto last_recv{
node.m_last_recv.load()};
1981 if (last_recv.count() == 0 || last_send.count() == 0) {
1996 if (!
node.fSuccessfullyConnected) {
2009 events_per_sock.emplace(hListenSocket.sock,
Sock::Events{Sock::RECV});
2012 for (
CNode* pnode : nodes) {
2013 bool select_recv = !pnode->fPauseRecv;
2016 LOCK(pnode->cs_vSend);
2020 const auto& [to_send, more, _msg_type] = pnode->m_transport->GetBytesToSend(!pnode->vSendMsg.empty());
2021 select_send = !to_send.empty() || more;
2023 if (!select_recv && !select_send)
continue;
2025 LOCK(pnode->m_sock_mutex);
2026 if (pnode->m_sock) {
2028 events_per_sock.emplace(pnode->m_sock,
Sock::Events{event});
2032 return events_per_sock;
2051 if (events_per_sock.empty() || !events_per_sock.begin()->first->WaitMany(timeout, events_per_sock)) {
2068 for (
CNode* pnode : nodes) {
2075 bool recvSet =
false;
2076 bool sendSet =
false;
2077 bool errorSet =
false;
2079 LOCK(pnode->m_sock_mutex);
2080 if (!pnode->m_sock) {
2083 const auto it = events_per_sock.find(pnode->m_sock);
2084 if (it != events_per_sock.end()) {
2087 errorSet = it->second.occurred &
Sock::ERR;
2104 if (data_left) recvSet =
false;
2108 if (recvSet || errorSet)
2111 uint8_t pchBuf[0x10000];
2114 LOCK(pnode->m_sock_mutex);
2115 if (!pnode->m_sock) {
2118 nBytes = pnode->m_sock->Recv(pchBuf,
sizeof(pchBuf),
MSG_DONTWAIT);
2122 bool notify =
false;
2123 if (!pnode->ReceiveMsgBytes({pchBuf, (size_t)nBytes}, notify)) {
2124 pnode->CloseSocketDisconnect();
2128 pnode->MarkReceivedMsgsForProcessing();
2132 else if (nBytes == 0)
2135 if (!pnode->fDisconnect) {
2138 pnode->CloseSocketDisconnect();
2140 else if (nBytes < 0)
2146 if (!pnode->fDisconnect) {
2149 pnode->CloseSocketDisconnect();
2164 const auto it = events_per_sock.find(listen_socket.sock);
2165 if (it != events_per_sock.end() && it->second.occurred &
Sock::RECV) {
2187 fMsgProcWake =
true;
2196 Shuffle(seeds.begin(), seeds.end(), rng);
2197 int seeds_right_now = 0;
2202 seeds_right_now = seeds.size();
2207 seeds_right_now = seeds.size();
2224 for (
const std::string& seed : seeds) {
2225 if (seeds_right_now == 0) {
2229 LogPrintf(
"Waiting %d seconds before querying DNS seeds.\n", seeds_wait_time.count());
2230 std::chrono::seconds to_wait = seeds_wait_time;
2231 while (to_wait.count() > 0) {
2242 for (
const CNode* pnode : m_nodes) {
2243 if (pnode->fSuccessfullyConnected && pnode->IsFullOutboundConn()) ++nRelevant;
2246 if (nRelevant >= 2) {
2248 LogPrintf(
"%d addresses found from DNS seeds\n", found);
2249 LogPrintf(
"P2P peers available. Finished DNS seeding.\n");
2251 LogPrintf(
"P2P peers available. Skipped DNS seeding.\n");
2263 LogPrintf(
"Waiting for network to be reactivated before querying DNS seeds.\n");
2269 LogPrintf(
"Loading addresses from DNS seed %s\n", seed);
2275 std::vector<CAddress> vAdd;
2277 std::string host =
strprintf(
"x%x.%s", requiredServiceBits, seed);
2282 unsigned int nMaxIPs = 256;
2283 const auto addresses{
LookupHost(host, nMaxIPs,
true)};
2284 if (!addresses.empty()) {
2288 vAdd.push_back(addr);
2301 LogPrintf(
"%d addresses found from DNS seeds\n", found);
2306 const auto start{SteadyClock::now()};
2311 addrman.
Size(), Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
2317 std::string strDest;
2320 if (m_addr_fetches.empty())
2322 strDest = m_addr_fetches.front();
2323 m_addr_fetches.pop_front();
2340 LogPrint(
BCLog::NET,
"setting try another outbound peer=%s\n", flag ?
"true" :
"false");
2357 int full_outbound_peers = 0;
2360 for (
const CNode* pnode : m_nodes) {
2361 if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsFullOutboundConn()) {
2362 ++full_outbound_peers;
2371 int block_relay_peers = 0;
2374 for (
const CNode* pnode : m_nodes) {
2375 if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsBlockOnlyConn()) {
2376 ++block_relay_peers;
2385 std::unordered_set<Network> networks{};
2386 for (
int n = 0; n <
NET_MAX; n++) {
2390 networks.insert(net);
2399 return m_network_conn_counts[net] > 1;
2408 for (
const auto net : nets) {
2424 if (!connect.empty())
2426 for (int64_t nLoop = 0;; nLoop++)
2428 for (
const std::string& strAddr : connect)
2432 for (
int i = 0; i < 10 && i < nLoop; i++)
2444 auto start = GetTime<std::chrono::microseconds>();
2454 if (!add_fixed_seeds) {
2455 LogPrintf(
"Fixed seeds are disabled\n");
2472 if (add_fixed_seeds && !fixed_seed_networks.empty()) {
2477 bool add_fixed_seeds_now =
false;
2479 if (GetTime<std::chrono::seconds>() > start + std::chrono::minutes{1}) {
2480 add_fixed_seeds_now =
true;
2481 LogPrintf(
"Adding fixed seeds as 60 seconds have passed and addrman is empty for at least one reachable network\n");
2485 else if (!dnsseed && !use_seednodes) {
2487 if (m_added_node_params.empty()) {
2488 add_fixed_seeds_now =
true;
2489 LogPrintf(
"Adding fixed seeds as -dnsseed=0 (or IPv4/IPv6 connections are disabled via -onlynet) and neither -addnode nor -seednode are provided\n");
2493 if (add_fixed_seeds_now) {
2501 seed_addrs.erase(std::remove_if(seed_addrs.begin(), seed_addrs.end(),
2502 [&fixed_seed_networks](
const CAddress& addr) {
return fixed_seed_networks.count(addr.GetNetwork()) == 0; }),
2507 add_fixed_seeds =
false;
2508 LogPrintf(
"Added %d fixed seeds from reachable networks.\n", seed_addrs.size());
2518 int nOutboundFullRelay = 0;
2519 int nOutboundBlockRelay = 0;
2520 int outbound_privacy_network_peers = 0;
2521 std::set<std::vector<unsigned char>> outbound_ipv46_peer_netgroups;
2525 for (
const CNode* pnode : m_nodes) {
2526 if (pnode->IsFullOutboundConn()) nOutboundFullRelay++;
2527 if (pnode->IsBlockOnlyConn()) nOutboundBlockRelay++;
2530 switch (pnode->m_conn_type) {
2543 const CAddress address{pnode->addr};
2544 if (address.IsTor() || address.IsI2P() || address.IsCJDNS()) {
2552 ++outbound_privacy_network_peers;
2561 auto now = GetTime<std::chrono::microseconds>();
2562 bool anchor =
false;
2563 bool fFeeler =
false;
2564 std::optional<Network> preferred_net;
2610 }
else if (now > next_feeler) {
2616 now > next_extra_network_peer &&
2681 std::tie(addr, addr_last_try) =
addrman.
Select(
false, preferred_net);
2699 if (current_time - addr_last_try < 10min && nTries < 30) {
2722 preferred_net.has_value() ?
"network-specific " :
"",
2747 const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(
nMaxConnections - 1, 2)};
2750 OpenNetworkConnection(addrConnect, count_failures, std::move(grant),
nullptr, conn_type, use_v2transport);
2757 std::vector<CAddress>
ret;
2759 for (
const CNode* pnode : m_nodes) {
2760 if (pnode->IsBlockOnlyConn()) {
2761 ret.push_back(pnode->addr);
2770 std::vector<AddedNodeInfo>
ret;
2772 std::list<AddedNodeParams> lAddresses(0);
2775 ret.reserve(m_added_node_params.size());
2776 std::copy(m_added_node_params.cbegin(), m_added_node_params.cend(), std::back_inserter(lAddresses));
2781 std::map<CService, bool> mapConnected;
2782 std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
2785 for (
const CNode* pnode : m_nodes) {
2786 if (pnode->addr.IsValid()) {
2787 mapConnected[pnode->addr] = pnode->IsInboundConn();
2789 std::string addrName{pnode->m_addr_name};
2790 if (!addrName.empty()) {
2791 mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->IsInboundConn(),
static_cast<const CService&
>(pnode->addr));
2796 for (
const auto& addr : lAddresses) {
2801 auto it = mapConnected.find(service);
2802 if (it != mapConnected.end()) {
2803 addedNode.resolvedAddress = service;
2804 addedNode.fConnected =
true;
2805 addedNode.fInbound = it->second;
2809 auto it = mapConnectedByName.find(addr.m_added_node);
2810 if (it != mapConnectedByName.end()) {
2811 addedNode.resolvedAddress = it->second.second;
2812 addedNode.fConnected =
true;
2813 addedNode.fInbound = it->second.first;
2816 ret.emplace_back(std::move(addedNode));
2832 if (!info.fConnected) {
2869 bool banned_or_discouraged =
m_banman && (
m_banman->IsDiscouraged(addrConnect) ||
m_banman->IsBanned(addrConnect));
2873 }
else if (
FindNode(std::string(pszDest)))
2876 CNode* pnode =
ConnectNode(addrConnect, pszDest, fCountFailure, conn_type, use_v2transport);
2885 m_nodes.push_back(pnode);
2900 bool fMoreWork =
false;
2908 for (
CNode* pnode : snap.Nodes()) {
2909 if (pnode->fDisconnect)
2914 fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
2929 fMsgProcWake =
false;
2935 static constexpr
auto err_wait_begin = 1s;
2936 static constexpr
auto err_wait_cap = 5min;
2937 auto err_wait = err_wait_begin;
2939 bool advertising_listen_addr =
false;
2942 auto SleepOnFailure = [&]() {
2944 if (err_wait < err_wait_cap) {
2952 if (advertising_listen_addr && conn.
me.
IsValid()) {
2954 advertising_listen_addr =
false;
2960 if (!advertising_listen_addr) {
2962 advertising_listen_addr =
true;
2973 err_wait = err_wait_begin;
2982 struct sockaddr_storage sockaddr;
2983 socklen_t len =
sizeof(sockaddr);
2984 if (!addrBind.
GetSockAddr((
struct sockaddr*)&sockaddr, &len))
2991 std::unique_ptr<Sock> sock =
CreateSock(addrBind);
3015 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
3016 if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (
const char*)&nProtLevel,
sizeof(
int)) ==
SOCKET_ERROR) {
3023 if (sock->Bind(reinterpret_cast<struct sockaddr*>(&sockaddr), len) ==
SOCKET_ERROR) {
3053 char pszHostName[256] =
"";
3054 if (gethostname(pszHostName,
sizeof(pszHostName)) !=
SOCKET_ERROR)
3056 const std::vector<CNetAddr> addresses{
LookupHost(pszHostName, 0,
true)};
3057 for (
const CNetAddr& addr : addresses)
3060 LogPrintf(
"%s: %s - %s\n", __func__, pszHostName, addr.ToStringAddr());
3063 #elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS) 3065 struct ifaddrs* myaddrs;
3066 if (getifaddrs(&myaddrs) == 0)
3068 for (
struct ifaddrs* ifa = myaddrs; ifa !=
nullptr; ifa = ifa->ifa_next)
3070 if (ifa->ifa_addr ==
nullptr)
continue;
3071 if ((ifa->ifa_flags & IFF_UP) == 0)
continue;
3072 if (strcmp(ifa->ifa_name,
"lo") == 0)
continue;
3073 if (strcmp(ifa->ifa_name,
"lo0") == 0)
continue;
3074 if (ifa->ifa_addr->sa_family == AF_INET)
3076 struct sockaddr_in* s4 = (
struct sockaddr_in*)(ifa->ifa_addr);
3081 else if (ifa->ifa_addr->sa_family == AF_INET6)
3083 struct sockaddr_in6* s6 = (
struct sockaddr_in6*)(ifa->ifa_addr);
3089 freeifaddrs(myaddrs);
3096 LogPrintf(
"%s: %s\n", __func__, active);
3111 : addrman(addrman_in)
3112 , m_netgroupman{netgroupman}
3117 SetTryNewOutboundPeer(
false);
3119 Options connOptions;
3121 SetNetworkActive(network_active);
3126 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
3161 bool fBound =
false;
3162 for (
const auto& addrBind : options.
vBinds) {
3165 for (
const auto& addrBind : options.
vWhiteBinds) {
3168 for (
const auto& addr_bind : options.
onion_binds) {
3172 struct in_addr inaddr_any;
3173 inaddr_any.s_addr = htonl(INADDR_ANY);
3174 struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT;
3189 _(
"Failed to listen on any port. Use -listen=0 if you want this."),
3201 for (
const auto& strDest : connOptions.
vSeedNodes) {
3211 LogPrintf(
"%i block-relay-only anchors will be tried for connections.\n",
m_anchors.size());
3239 fMsgProcWake =
false;
3256 _(
"Cannot provide specific connections and have addrman find outgoing connections at the same time."),
3354 std::vector<CNode*> nodes;
3356 for (
CNode* pnode : nodes) {
3357 pnode->CloseSocketDisconnect();
3385 std::vector<CAddress> addresses =
addrman.
GetAddr(max_addresses, max_pct, network);
3387 addresses.erase(std::remove_if(addresses.begin(), addresses.end(),
3399 .Write(local_socket_bytes)
3404 const auto current_time = GetTime<std::chrono::microseconds>();
3441 for (
const auto& it : m_added_node_params) {
3445 m_added_node_params.push_back(add);
3452 for (
auto it = m_added_node_params.begin(); it != m_added_node_params.end(); ++it) {
3453 if (strNode == it->m_added_node) {
3454 m_added_node_params.erase(it);
3467 return (m_added_node_params.size() < 24
3468 && std::any_of(m_added_node_params.cbegin(), m_added_node_params.cend(),
3469 [&](
const auto& p) {
return p.m_added_node == addr_str || p.m_added_node == addr_port_str; }));
3476 return m_nodes.size();
3479 for (
const auto& pnode : m_nodes) {
3497 vstats.reserve(m_nodes.size());
3498 for (
CNode* pnode : m_nodes) {
3499 vstats.emplace_back();
3500 pnode->CopyStats(vstats.back());
3501 vstats.back().m_mapped_as =
GetMappedAS(pnode->addr);
3510 pnode->fDisconnect =
true;
3518 bool disconnected =
false;
3520 for (
CNode* pnode : m_nodes) {
3521 if (subnet.
Match(pnode->addr)) {
3523 pnode->fDisconnect =
true;
3524 disconnected =
true;
3527 return disconnected;
3538 for(
CNode* pnode : m_nodes) {
3539 if (
id == pnode->GetId()) {
3541 pnode->fDisconnect =
true;
3558 nTotalBytesSent += bytes;
3560 const auto now = GetTime<std::chrono::seconds>();
3564 nMaxOutboundCycleStartTime = now;
3565 nMaxOutboundTotalBytesSentInCycle = 0;
3568 nMaxOutboundTotalBytesSentInCycle += bytes;
3597 if (nMaxOutboundCycleStartTime.count() == 0)
3601 const auto now = GetTime<std::chrono::seconds>();
3602 return (cycleEndTime < now) ? 0s : cycleEndTime - now;
3612 if (historicalBlockServingLimit)
3645 return nTotalBytesSent;
3655 if (use_v2transport) {
3663 std::shared_ptr<Sock> sock,
3665 uint64_t nKeyedNetGroupIn,
3666 uint64_t nLocalHostNonceIn,
3668 const std::string& addrNameIn,
3673 m_permission_flags{node_opts.permission_flags},
3675 m_connected{GetTime<std::chrono::seconds>()},
3677 addrBind{addrBindIn},
3678 m_addr_name{addrNameIn.empty() ? addr.ToStringAddrPort() : addrNameIn},
3680 m_inbound_onion{inbound_onion},
3681 m_prefer_evict{node_opts.prefer_evict},
3682 nKeyedNetGroup{nKeyedNetGroupIn},
3683 m_conn_type{conn_type_in},
3685 nLocalHostNonce{nLocalHostNonceIn},
3686 m_recv_flood_size{node_opts.recv_flood_size},
3687 m_i2p_sam_session{std::move(node_opts.i2p_sam_session)}
3692 mapRecvBytesPerMsgType[
msg] = 0;
3706 size_t nSizeAdded = 0;
3710 nSizeAdded +=
msg.m_raw_message_size;
3714 m_msg_process_queue.splice(m_msg_process_queue.end(),
vRecvMsg);
3715 m_msg_process_queue_size += nSizeAdded;
3722 if (m_msg_process_queue.empty())
return std::nullopt;
3724 std::list<CNetMessage> msgs;
3726 msgs.splice(msgs.begin(), m_msg_process_queue, m_msg_process_queue.begin());
3727 m_msg_process_queue_size -= msgs.front().m_raw_message_size;
3730 return std::make_pair(std::move(msgs.front()), !m_msg_process_queue.empty());
3741 size_t nMessageSize =
msg.data.size();
3747 TRACE6(net, outbound_message,
3756 size_t nBytesSent = 0;
3761 const auto& [to_send, more, _msg_type] =
3763 const bool queue_was_empty{to_send.empty() && pnode->vSendMsg.empty()};
3766 pnode->m_send_memusage +=
msg.GetMemoryUsage();
3769 pnode->vSendMsg.push_back(std::move(
msg));
3778 if (queue_was_empty && more) {
3787 CNode* found =
nullptr;
3789 for (
auto&& pnode : m_nodes) {
3790 if(pnode->
GetId() == id) {
3816 decltype(m_reconnections) todo;
3819 if (m_reconnections.empty())
break;
3820 todo.splice(todo.end(), m_reconnections, m_reconnections.begin());
3823 auto& item = *todo.begin();
3830 std::move(item.grant),
3831 item.destination.empty() ? nullptr : item.destination.c_str(),
3833 item.use_v2transport);
3839 const std::string& msg_type,
3847 auto now = GetTime<std::chrono::microseconds>();
3851 std::replace(clean_addr.begin(), clean_addr.end(),
':',
'_');
3856 fs::path path = base_path / (is_incoming ?
"msgs_recv.dat" :
"msgs_sent.dat");
3860 f <<
Span{msg_type};
3864 uint32_t size = data.
size();
3869 std::function<void(
const CAddress& addr,
3870 const std::string& msg_type,
const std::vector< std::string > & DNSSeeds() const
Return the list of hostnames to look up for DNS seeds.
const char * GETCFILTERS
getcfilters requests compact filters for a range of blocks.
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Wrapper that overrides the GetParams() function of a stream (and hides GetVersion/GetType).
bool RemoveAddedNode(const std::string &node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
std::vector< CService > vBinds
std::atomic< bool > flagInterruptMsgProc
std::vector< unsigned char > GetGroup(const CNetAddr &address) const
Get the canonical identifier of the network group for address.
Span< const std::byte > GetSendGarbageTerminator() const noexcept
Get the Garbage Terminator to send.
std::vector< CAddress > m_addrs_response_cache
static const int MAX_BLOCK_RELAY_ONLY_CONNECTIONS
Maximum number of block-relay-only outgoing connections.
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
uint64_t CalculateKeyedNetGroup(const CAddress &ad) const
Mutex m_send_mutex
Lock for sending state.
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
size_t Size(std::optional< Network > net=std::nullopt, std::optional< bool > in_new=std::nullopt) const
Return size information about addrman.
static constexpr auto FEELER_SLEEP_WINDOW
const uint256 & GetMessageHash() const EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
void RandAddEvent(const uint32_t event_info) noexcept
Gathers entropy from the low bits of the time at which events occur.
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
std::atomic_bool fPauseSend
AddrFetch connections are short lived connections used to solicit addresses from peers.
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).
BIP324Cipher m_cipher
Cipher state.
bool m_i2p_accept_incoming
const char * BLOCKTXN
Contains a BlockTransactions.
void ThreadDNSAddressSeed() EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex
std::atomic< bool > fNetworkActive
bool ProcessReceivedKeyBytes() noexcept EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex
Process bytes in m_recv_buffer, while in KEY state.
size_t GetSendMemoryUsage() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Return the memory usage of this transport attributable to buffered data to send.
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
ServiceFlags
nServices flags
std::optional< std::pair< CNetMessage, bool > > PollMessage() EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex)
Poll the next message from the processing queue of this connection.
bool AddedNodesContain(const CAddress &addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
Info GetInfo() const noexcept override
Retrieve information about this transport.
#define LogPrint(category,...)
Inbound connections are those initiated by a peer.
FILE * fopen(const fs::path &p, const char *mode)
A set of addresses that represent the hash of a string or FQDN.
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
Dummy value to indicate the number of NET_* constants.
constexpr C * end() const noexcept
void SocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex
Check connected and listening sockets for IO readiness and process them accordingly.
Unencrypted, plaintext protocol.
NetPermissionFlags permission_flags
bool Bind(const CService &addr, unsigned int flags, NetPermissionFlags permissions)
bool OutboundTargetReached(bool historicalBlockServingLimit) const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
check if the outbound target is reached if param historicalBlockServingLimit is set true...
Feeler connections are short-lived connections made to check that a node is alive.
Mutex m_total_bytes_sent_mutex
std::shared_ptr< Sock > sock
std::list< CNode * > m_nodes_disconnected
void ser_writedata64(Stream &s, uint64_t obj)
void MarkBytesSent(size_t bytes_sent) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Report how many bytes returned by the last GetBytesToSend() have been sent.
bool SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
Splits socket address string into host string and port value.
bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut)
static constexpr size_t MAX_UNUSED_I2P_SESSIONS_SIZE
Cap on the size of m_unused_i2p_sessions, to ensure it does not unexpectedly use too much memory...
void SetReceiveState(RecvState recv_state) noexcept EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
Change the receive state.
CService GetLocalAddress(const CNode &peer)
const std::chrono::seconds m_connected
Unix epoch time at peer connection.
BytesToSend GetBytesToSend(bool have_next_message) const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Get bytes to send on the wire, if any, along with other information about it.
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
static void ClearFlag(NetPermissionFlags &flags, NetPermissionFlags f)
ClearFlag is only called with f == NetPermissionFlags::Implicit.
bool SetMessageToSend(CSerializedNetMsg &msg) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Set the next message to send.
const std::string NET_MESSAGE_TYPE_OTHER
std::vector< unsigned char > data
static constexpr size_t size()
uint256 GetRandHash() noexcept
Nothing (this transport is using v1 fallback).
const ConnectionType m_conn_type
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
uint64_t GetTotalBytesRecv() const
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
std::unordered_set< Network > GetReachableEmptyNetworks() const
Return reachable networks for which we have no addresses in addrman and therefore may require loadin...
constexpr auto GetRandMillis
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
bool GetProxy(enum Network net, Proxy &proxyInfoOut)
bool ConnectThroughProxy(const Proxy &proxy, const std::string &strDest, uint16_t port, const Sock &sock, int nTimeout, bool &outProxyConnectionFailed)
Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 p...
void ser_writedata32(Stream &s, uint32_t obj)
static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME
The default timeframe for -maxuploadtarget.
constexpr std::size_t size() const noexcept
bool AlreadyConnectedToAddress(const CAddress &addr)
Determine whether we're already connected to a given address, in order to avoid initiating duplicate ...
void AddWhitelistPermissionFlags(NetPermissionFlags &flags, const CNetAddr &addr) const
static constexpr SerParams V2_NETWORK
const char * CFHEADERS
cfheaders is a response to a getcfheaders request containing a filter header and a vector of filter h...
RAII-style semaphore lock.
void Interrupt() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc)
CService me
Our I2P address.
bool BindListenPort(const CService &bindAddr, bilingual_str &strError, NetPermissionFlags permissions)
static constexpr uint32_t MAX_GARBAGE_LEN
bool DumpPeerAddresses(const ArgsManager &args, const AddrMan &addr)
static const unsigned char VERSION[]
void Initialize(const EllSwiftPubKey &their_pubkey, bool initiator, bool self_decrypt=false) noexcept
Initialize when the other side's public key is received.
const NodeId m_nodeid
NodeId (for debug logging).
void Discover()
Look up IP addresses from all interfaces on the machine and add them to the list of local addresses t...
bool ProcessReceivedPacketBytes() noexcept EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
Process bytes in m_recv_buffer, while in VERSION/APP state.
unsigned DecryptLength(Span< const std::byte > input) noexcept
Decrypt the length of a packet.
bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
bool IsConnectedThroughPrivacyNet() const
Whether this peer connected through a privacy network.
void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex
std::chrono::seconds GetMaxOutboundTimeLeftInCycle_() const EXCLUSIVE_LOCKS_REQUIRED(m_total_bytes_sent_mutex)
returns the time left in the current max outbound cycle in case of no limit, it will always return 0 ...
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
void SetTryNewOutboundPeer(bool flag)
CService MaybeFlipIPv6toCJDNS(const CService &service)
If an IPv6 address belongs to the address range used by the CJDNS network and the CJDNS network is re...
size_t GetMemoryUsage() const noexcept
Compute total memory usage of this object (own memory + any dynamic memory).
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
void RecordBytesSent(uint64_t bytes) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
bool IsPrivacyNet() const
Whether this object is a privacy network.
static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE
bool SeenLocal(const CService &addr)
vote for a local address
An established connection with another peer.
Network GetNetClass() const
size_t GetMaxBytesToProcess() noexcept EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
Determine how many received bytes can be processed in one go (not allowed in V1 state).
m_max_outbound_block_relay
CService LookupNumeric(const std::string &name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
Resolve a service string with a numeric IP to its first corresponding service.
static std::optional< CService > GetLocal(const CNode &peer)
const char * CFILTER
cfilter is a response to a getcfilters request containing a single compact filter.
V1Transport m_v1_fallback
Encapsulate a V1Transport to fall back to.
static std::vector< CAddress > ConvertSeeds(const std::vector< uint8_t > &vSeedsIn)
Convert the serialized seeds into usable address objects.
These are the default connections that we use to connect with the network.
const char * PONG
The pong message replies to a ping message, proving to the pinging node that the ponging node is stil...
std::vector< CAddress > GetAddresses(size_t max_addresses, size_t max_pct, std::optional< Network > network) const
Return all or many randomly selected addresses, optionally by network.
void GetNodeStats(std::vector< CNodeStats > &vstats) const
Non-refcounted RAII wrapper for FILE*.
bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex)
#define WSAGetLastError()
static constexpr std::chrono::minutes TIMEOUT_INTERVAL
Time after which to disconnect, after waiting for a ping response (or inactivity).
std::list< CNetMessage > vRecvMsg
bool GetTryNewOutboundPeer() const
void DumpAnchors(const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
void NotifyNumConnectionsChanged()
enum Network GetNetwork() const
BytesToSend GetBytesToSend(bool have_next_message) const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Get bytes to send on the wire, if any, along with other information about it.
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions...
const char * GETCFCHECKPT
getcfcheckpt requests evenly spaced compact filter headers, enabling parallelized download and valida...
std::string ToStringAddrPort() const
SendState
State type that controls the sender side.
std::map< uint64_t, CachedAddrResponse > m_addr_response_caches
Addr responses stored in different caches per (network, local socket) prevent cross-network node iden...
void Init(const Options &connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex
uint64_t GetMaxOutboundTarget() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
void ProcessAddrFetch() EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
static bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Stochastic address manager.
void AddSocketPermissionFlags(NetPermissionFlags &flags) const
bool ForNode(NodeId id, std::function< bool(CNode *pnode)> func)
uint16_t GetDefaultPort() const
bool ReceivedMessageComplete() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Returns true if the current message is complete (so GetReceivedMessage can be called).
constexpr unsigned char * begin()
std::string ToStringAddr() const
bool ReceiveMsgBytes(Span< const uint8_t > msg_bytes, bool &complete) EXCLUSIVE_LOCKS_REQUIRED(!cs_vRecv)
Receive bytes from the buffer and deserialize them into messages.
Mutex m_unused_i2p_sessions_mutex
Mutex protecting m_i2p_sam_sessions.
bool DisconnectNode(const std::string &node)
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD
std::function< void(const CAddress &addr, const std::string &msg_type, Span< const unsigned char > data, bool is_incoming)> CaptureMessage
Defaults to CaptureMessageToFile(), but can be overridden by unit tests.
int GetExtraBlockRelayCount() const
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
TransportProtocolType m_transport_type
Transport protocol type.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
void RecordBytesRecv(uint64_t bytes)
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
std::chrono::steady_clock Clock
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
static constexpr auto EXTRA_NETWORK_PEER_INTERVAL
Frequency to attempt extra connections to reachable networks we're not connected to yet...
std::thread threadI2PAcceptIncoming
std::tuple< Span< const uint8_t >, bool, const std::string &> BytesToSend
Return type for GetBytesToSend, consisting of:
bool CompleteInternal() const noexcept EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
bool IsBadPort(uint16_t port)
Determine if a port is "bad" from the perspective of attempting to connect to a node on that port...
void SetAddrLocal(const CService &addrLocalIn) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_local_mutex)
May not be called more than once.
Span< const std::byte > GetSessionID() const noexcept
Get the Session ID.
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
std::optional< CService > GetLocalAddrForPeer(CNode &node)
Returns a local address that we should advertise to this peer.
std::optional< uint256 > session_id
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty=0s)
Attempt to add one or more addresses to addrman's new table.
static constexpr uint16_t I2P_SAM31_PORT
SAM 3.1 and earlier do not support specifying ports and force the port to 0.
std::chrono::microseconds m_cache_entry_expiration
static bool NodeFullyConnected(const CNode *pnode)
unsigned int nPrevNodeCount
static constexpr unsigned GARBAGE_TERMINATOR_LEN
static const uint64_t RANDOMIZER_ID_NETGROUP
bool InactivityCheck(const CNode &node) const
Return true if the peer is inactive and should be disconnected.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::condition_variable condMsgProc
std::atomic_bool m_start_extra_block_relay_peers
flag for initiating extra block-relay-only peer connections.
const std::unique_ptr< Transport > m_transport
Transport serializer/deserializer.
bool ConnectSocketDirectly(const CService &addrConnect, const Sock &sock, int nTimeout, bool manual_connection)
Try to connect to the specified service on the specified socket.
std::vector< AddedNodeInfo > GetAddedNodeInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
bool IsManualOrFullOutboundConn() const
const EllSwiftPubKey & GetOurPubKey() const noexcept
Retrieve our public key.
Mutex m_added_nodes_mutex
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
We open manual connections to addresses that users explicitly requested via the addnode RPC or the -a...
CNode * ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex)
const uint64_t nSeed0
SipHasher seeds for deterministic randomness.
std::chrono::microseconds GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval)
Return a timestamp in the future sampled from an exponential distribution (https://en.wikipedia.org/wiki/Exponential_distribution).
bool ShouldReconnectV1() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex
Whether upon disconnections, a reconnect with V1 is warranted.
static std::unique_ptr< Transport > MakeTransport(NodeId id, bool use_v2transport, bool inbound) noexcept
V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept
Construct a V2 transport with securely generated random keys.
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
static bool HasAllDesirableServiceFlags(ServiceFlags services)
A shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services)...
std::thread threadOpenAddedConnections
std::string ConnectionTypeAsString(ConnectionType conn_type)
Convert ConnectionType enum to a string value.
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
void PerformReconnections() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex
Attempt reconnections, if m_reconnections non-empty.
std::vector< CAddress > ReadAnchors(const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
void StartExtraBlockRelayPeers()
Double ended buffer combining vector and stream-like interfaces.
CNode * FindNode(const CNetAddr &ip)
const char * ADDRV2
The addrv2 message relays connection information for peers on the network just like the addr message...
ServiceFlags GetLocalServices() const
Used to convey which local services we are offering peers during node connection. ...
bilingual_str _(const char *psz)
Translation function.
bool AddLocal(const CService &addr_, int nScore)
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
void fillrand(Span< std::byte > output)
Fill a byte Span with random bytes.
void SocketHandlerListening(const Sock::EventsPerSock &events_per_sock)
Accept incoming connections, one from each read-ready listening socket.
TransportProtocolType transport_type
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE
Number of DNS seeds to query when the number of connections is low.
A combination of a network address (CNetAddr) and a (TCP) port.
Transport protocol agnostic message container.
std::vector< std::string > vSeedNodes
std::vector< std::string > m_specified_outgoing
void OpenNetworkConnection(const CAddress &addrConnect, bool fCountFailure, CSemaphoreGrant &&grant_outbound, const char *strDest, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex)
void SetSendState(SendState send_state) noexcept EXCLUSIVE_LOCKS_REQUIRED(m_send_mutex)
Change the send state.
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
std::unique_ptr< CSemaphore > semOutbound
std::thread threadMessageHandler
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as connection attempted to.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
bool AddConnection(const std::string &address, ConnectionType conn_type) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex)
Attempts to open a connection.
An ElligatorSwift-encoded public key.
int GetExtraFullOutboundCount() const
A CService with information about it as peer.
std::vector< CAddress > GetCurrentBlockRelayOnlyConns() const
Return vector of current BLOCK_RELAY peers.
static int GetnScore(const CService &addr)
const std::vector< std::string > & getAllNetMessageTypes()
static constexpr bool DEFAULT_FIXEDSEEDS
void ClearShrink(V &v) noexcept
Clear a vector (or std::deque) and release its allocated memory.
uint32_t GetMappedAS(const CNetAddr &address) const
Get the autonomous system on the BGP path to address.
static bool IsPeerAddrLocalGood(CNode *pnode)
#define LogPrintLevel(category, level,...)
static CService ip(uint32_t i)
Do not call AddLocal() for our special addresses, e.g., for incoming Tor connections, to prevent gossiping them over the network.
int readData(Span< const uint8_t > msg_bytes) EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
std::atomic_bool m_try_another_outbound_peer
flag for deciding to connect to an extra outbound peer, in excess of m_max_outbound_full_relay This t...
bool ReceivedBytes(Span< const uint8_t > &msg_bytes) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex
Feed wire bytes to the transport.
(Responder only) either v2 public key or v1 header.
bool InitBinds(const Options &options)
void SetNetworkActive(bool active)
#define WAIT_LOCK(cs, name)
static constexpr bool DEFAULT_DNSSEED
const CChainParams & m_params
Mutex m_recv_mutex
Lock for receive state.
const bool m_initiating
Whether we are the initiator side.
static void CaptureMessageToFile(const CAddress &addr, const std::string &msg_type, Span< const unsigned char > data, bool is_incoming)
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
std::atomic< bool > m_bip152_highbandwidth_to
const NetPermissionFlags m_permission_flags
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
NodeSeconds nTime
Always included in serialization. The behavior is unspecified if the value is not representable as ui...
CSipHasher GetDeterministicRandomizer(uint64_t id) const
Get a unique deterministic randomizer.
bool SetMessageToSend(CSerializedNetMsg &msg) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Set the next message to send.
static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS
const bool m_inbound_onion
Whether this peer is an inbound onion, i.e. connected via our Tor onion service.
void ProcessReceivedMaybeV1Bytes() noexcept EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex
Process bytes in m_recv_buffer, while in KEY_MAYBE_V1 state.
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
const char * BLOCK
The block message transmits a single serialized block.
std::atomic_bool fDisconnect
void CreateNodeFromAcceptedSocket(std::unique_ptr< Sock > &&sock, NetPermissionFlags permission_flags, const CAddress &addr_bind, const CAddress &addr)
Create a CNode object from a socket that has just been accepted and add the node to the m_nodes membe...
std::string strSubVersion
Subversion as sent to the P2P network in version messages.
void AddAddrFetch(const std::string &strDest) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex)
const char * FEEFILTER
The feefilter message tells the receiving peer not to inv us any txs which do not meet the specified ...
const char * GETCFHEADERS
getcfheaders requests a compact filter header and the filter hashes for a range of blocks...
std::atomic< std::chrono::seconds > m_last_tx_time
UNIX epoch time of the last transaction received from this peer that we had not yet seen (e...
NetPermissionFlags m_flags
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const
Return all or many randomly selected addresses, optionally by network.
static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS
Maximum number of block-relay-only anchor connections.
Waiting for the other side's public key.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define Assume(val)
Assume is the identity function.
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
RecursiveMutex m_nodes_mutex
const size_t m_recv_flood_size
static bool MayHaveUsefulAddressDB(ServiceFlags services)
Checks if a peer with the given service flags may be capable of having a robust address-storage DB...
bool Decrypt(Span< const std::byte > input, Span< const std::byte > aad, bool &ignore, Span< std::byte > contents) noexcept
Decrypt a packet.
constexpr int64_t count_seconds(std::chrono::seconds t)
void DeleteNode(CNode *pnode)
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS
How long to delay before querying DNS seeds.
static constexpr Event ERR
Ignored if passed to Wait(), but could be set in the occurred events if an exceptional condition has ...
std::unique_ptr< i2p::sam::Session > m_i2p_sam_session
I2P SAM session.
bool CheckIncomingNonce(uint64_t nonce)
void CloseSocketDisconnect() EXCLUSIVE_LOCKS_REQUIRED(!m_sock_mutex)
void ThreadOpenConnections(std::vector< std::string > connect) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
std::pair< CAddress, NodeSeconds > Select(bool new_only=false, std::optional< Network > network=std::nullopt) const
Choose an address to connect to.
void ThreadOpenAddedConnections() EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex
bool Match(const CNetAddr &addr) const
Info GetInfo() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Retrieve information about this transport.
Mutex m_addr_fetches_mutex
size_t GetSendMemoryUsage() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Return the memory usage of this transport attributable to buffered data to send.
void StartSendingHandshake() noexcept EXCLUSIVE_LOCKS_REQUIRED(m_send_mutex)
Put our public key + garbage in the send buffer.
std::atomic< std::chrono::seconds > m_last_send
std::atomic< NodeId > nLastNodeId
constexpr C * begin() const noexcept
bool GetNameProxy(Proxy &nameProxyOut)
Garbage and garbage terminator.
std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
std::vector< CService > onion_binds
static time_point now() noexcept
Return current system time or mocked time, if set.
MessageStartChars m_magic_bytes
size_t GetNodeCount(ConnectionDirection) const
std::vector< CService > Lookup(const std::string &name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
void ThreadSocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict...
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
ServiceFlags nServices
Serialized as uint64_t in V1, and as CompactSize in V2.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
static const bool DEFAULT_WHITELISTFORCERELAY
Default for -whitelistforcerelay.
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
std::string ToString() const
void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc)
void PushMessage(CNode *pnode, CSerializedNetMsg &&msg) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
static const uint64_t SELECT_TIMEOUT_MILLISECONDS
std::thread threadOpenConnections
bool AttemptToEvictConnection()
Try to find a connection to evict when the node is full.
uint64_t GetTotalBytesSent() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
uint32_t GetMappedAS(const CNetAddr &addr) const
static std::optional< std::string > GetMessageType(Span< const uint8_t > &contents) noexcept
Given a packet's contents, find the message type (if valid), and strip it from contents.
bool ProcessReceivedGarbageBytes() noexcept EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
Process bytes in m_recv_buffer, while in GARB_GARBTERM state.
constexpr C * data() const noexcept
bool error(const char *fmt, const Args &... args)
const CChainParams & Params()
Return the currently selected parameters.
CSemaphoreGrant grantOutbound
void SocketHandlerConnected(const std::vector< CNode *> &nodes, const Sock::EventsPerSock &events_per_sock) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex
Do the read/write for connected sockets that are ready for IO.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
GlobalMutex g_maplocalhost_mutex
const MessageStartChars & MessageStart() const
std::atomic< std::chrono::seconds > m_last_recv
bool fAddressesInitialized
static CAddress GetBindAddress(const Sock &sock)
Get the bind address for a socket as CAddress.
std::thread threadDNSAddressSeed
bool MaybePickPreferredNetwork(std::optional< Network > &network)
Search for a "preferred" network, a reachable network to which we currently don't have any OUTBOUND_F...
#define TRACE6(context, event, a, b, c, d, e, f)
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
bool Start(CScheduler &scheduler, const Options &options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex
Span< const std::byte > MakeByteSpan(V &&v) noexcept
std::vector< ListenSocket > vhListenSocket
std::chrono::seconds GetMaxOutboundTimeframe() const
static const uint64_t RANDOMIZER_ID_ADDRCACHE
bool ReceivedBytes(Span< const uint8_t > &msg_bytes) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Feed wire bytes to the transport.
CService GetAddrLocal() const EXCLUSIVE_LOCKS_REQUIRED(!m_addr_local_mutex)
static uint32_t ReadLE32(const unsigned char *ptr)
std::atomic< int64_t > nTimeOffset
CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool &reject_message) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Retrieve a completed message from transport.
const char * GETDATA
The getdata message requests one or more data objects from another node.
static constexpr bool DEFAULT_FORCEDNSSEED
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
std::atomic_bool fSuccessfullyConnected
fSuccessfullyConnected is set to true on receiving VERACK from the peer.
static constexpr uint64_t MAX_SIZE
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size...
#define AssertLockNotHeld(cs)
std::string m_session_id
BIP324 session id string in hex, if any.
static constexpr auto FEELER_INTERVAL
Run the feeler connection loop once every 2 minutes.
RecvState
State type that defines the current contents of the receive buffer and/or how the next received bytes...
void Encrypt(Span< const std::byte > contents, Span< const std::byte > aad, bool ignore, Span< std::byte > output) noexcept
Encrypt a packet.
std::atomic< int > nVersion
Sock::EventsPerSock GenerateWaitSockets(Span< CNode *const > nodes)
Generate a collection of sockets to check for IO readiness.
bool AddNode(const AddedNodeParams &add) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
Nothing (an application packet is available for GetMessage()).
void MarkReceivedMsgsForProcessing() EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex)
Move all messages from the received queue to the processing queue.
void InterruptSocks5(bool interrupt)
static constexpr auto EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL
Run the extra block-relay-only connection loop once every 5 minutes.
std::string ConnectionTypeAsString() const
ConnectionType
Different types of connections to a peer.
bool ShouldRunInactivityChecks(const CNode &node, std::chrono::seconds now) const
Return true if we should disconnect the peer for failing an inactivity check.
const char * CFCHECKPT
cfcheckpt is a response to a getcfcheckpt request containing a vector of evenly spaced filter headers...
CConnman(uint64_t seed0, uint64_t seed1, AddrMan &addrman, const NetGroupManager &netgroupman, const CChainParams ¶ms, bool network_active=true)
static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL
unsigned char * UCharCast(char *c)
Mutex m_msg_process_queue_mutex
std::atomic< bool > m_bip152_highbandwidth_from
void ThreadI2PAcceptIncoming()
const std::vector< uint8_t > & FixedSeeds() const
RAII helper class that manages a socket and closes it automatically when it goes out of scope...
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE
The maximum allowed size for a serialized block, in bytes (only for buffer size limits) ...
Tp rand_uniform_delay(const Tp &time, typename Tp::duration range)
Return the time point advanced by a uniform random duration.
CNode(NodeId id, std::shared_ptr< Sock > sock, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string &addrNameIn, ConnectionType conn_type_in, bool inbound_onion, CNodeOptions &&node_opts={})
Mutex m_reconnections_mutex
Mutex protecting m_reconnections.
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
std::pair< size_t, bool > SocketSendData(CNode &node) const EXCLUSIVE_LOCKS_REQUIRED(node.cs_vSend)
(Try to) send data from node's vSendMsg.
static const bool DEFAULT_WHITELISTRELAY
Default for -whitelistrelay.
CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool &reject_message) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Retrieve a completed message from transport.
RAII helper to atomically create a copy of m_nodes and add a reference to each of the nodes...
static const int MAX_OUTBOUND_FULL_RELAY_CONNECTIONS
Maximum number of automatic outgoing nodes over which we'll relay everything (blocks, tx, addrs, etc)
BindFlags
Used to pass flags to the Bind() function.
void ThreadMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc)
An encapsulated private key.
void AcceptConnection(const ListenSocket &hListenSocket)
A Span is an object that can refer to a contiguous sequence of objects.
const char * TX
The tx message transmits a single transaction.
static bool TryParse(const std::string &str, NetWhitebindPermissions &output, bilingual_str &error)
Different type to mark Mutex at global scope.
std::string GetNetworkName(enum Network net)
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Information about a peer.
static CNetCleanup instance_of_cnetcleanup
Span< const std::byte > GetReceiveGarbageTerminator() const noexcept
Get the expected Garbage Terminator to receive.
std::atomic< std::chrono::seconds > m_last_block_time
UNIX epoch time of the last block received from this peer that we had not yet seen (e...
bool SetSockAddr(const struct sockaddr *paddr)
std::thread threadSocketHandler
std::optional< NodeId > SelectNodeToEvict(std::vector< NodeEvictionCandidate > &&vEvictionCandidates)
Select an inbound peer to evict after filtering out (protecting) peers having distinct, difficult-to-forge characteristics.
Simple class for background tasks that should be run periodically or once "after a while"...
std::atomic< std::chrono::microseconds > m_last_ping_time
Last measured round-trip time.
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
std::map< CNetAddr, LocalServiceInfo > mapLocalHost GUARDED_BY(g_maplocalhost_mutex)
Network ConnectedThroughNetwork() const
Get network the peer connected through.
bool Good(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
Mark an address record as accessible and attempt to move it to addrman's tried table.
std::vector< CAddress > m_anchors
Addresses that were saved during the previous clean shutdown.
static path u8path(const std::string &utf8_str)
virtual int GetSockName(sockaddr *name, socklen_t *name_len) const
getsockname(2) wrapper.
static constexpr unsigned EXPANSION
std::atomic< std::chrono::microseconds > m_min_ping_time
Lowest measured round-trip time.
const NetGroupManager & m_netgroupman
std::atomic< uint64_t > nTotalBytesRecv
Auxiliary requested/occurred events to wait for in WaitMany().
const std::string m_addr_name
std::atomic_bool fPauseRecv
std::unique_ptr< CSemaphore > semAddnode
std::unique_ptr< Sock > sock
Connected socket.
Span(T *, EndOrSize) -> Span< T >
CONSTEXPR_IF_NOT_DEBUG Span< C > last(std::size_t count) const noexcept
CThreadInterrupt interruptNet
This is signaled when network activity should cease.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool ReceivedMessageComplete() const override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Returns true if the current message is complete (so GetReceivedMessage can be called).
static constexpr std::array< std::byte, 0 > VERSION_CONTENTS
Contents of the version packet to send.
bool IsInboundConn() const
bool m_use_addrman_outgoing
std::vector< NetWhitebindPermissions > vWhiteBinds
static constexpr unsigned LENGTH_LEN
uint16_t GetDefaultPort(Network net) const
void scheduleEvery(Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Repeat f until the scheduler is stopped.
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
int readHeader(Span< const uint8_t > msg_bytes) EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
std::unique_ptr< i2p::sam::Session > i2p_sam_session
bool bind_on_any
True if the user did not specify -bind= or -whitebind= and thus we should bind on 0...
Chrono::duration rand_uniform_duration(typename Chrono::duration range) noexcept
Generate a uniform random duration in the range from 0 (inclusive) to range (exclusive).
void MarkBytesSent(size_t bytes_sent) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Report how many bytes returned by the last GetBytesToSend() have been sent.
const char *const ANCHORS_DATABASE_FILENAME
Anchor IP address database file name.
uint64_t GetOutboundTargetBytesLeft() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
response the bytes left in the current max outbound cycle in case of no limit, it will always respons...
m_max_outbound_full_relay
void Reset() EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex)
Cache responses to addr requests to minimize privacy leak.
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
std::vector< unsigned char > GetAddrBytes() const
void RemoveLocal(const CService &addr)
This transport is using v1 fallback.
We use block-relay-only connections to help prevent against partition attacks.
bool IsLocal(const CService &addr)
check whether a given address is potentially local
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
void CopyStats(CNodeStats &stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex
ReachableNets g_reachable_nets
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
static constexpr size_t V1_PREFIX_LEN
The length of the V1 prefix to match bytes initially received by responders with to determine if thei...
void TraceThread(std::string_view thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
Addresses from these networks are not publicly routable on the global Internet.
V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept