Skip to content
Bitcoin Roots

v29.4-roots.1 · Commit 3: [PATCH 03/13] policy: restore conservative relay and network controls

src/net.h

Before: src/net.h · After: src/net.h

File metadata
index e025b20bcdefb31cd92272825370d90d8a6f4ba8..6146a4f94739af109081a194ad585d619a566f62 100644

Download original patch · Release notes

Diff rows 1–214 of 214. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/net.h CHANGED
@@ -31,6 +31,7 @@
31
  #include <util/check.h>
32
  #include <util/sock.h>
33
  #include <util/threadinterrupt.h>
 
34
 
35
  #include <atomic>
36
  #include <condition_variable>
@@ -96,6 +97,9 @@ static constexpr bool DEFAULT_V2_TRANSPORT{true};
96
 
97
  typedef int64_t NodeId;
98
 
 
 
 
99
  struct AddedNodeParams {
100
  std::string m_added_node;
101
  bool m_use_v2transport;
@@ -148,7 +152,7 @@ enum
148
  LOCAL_NONE, // unknown
149
  LOCAL_IF, // address a local interface listens on
150
  LOCAL_BIND, // address explicit bound to
151
- LOCAL_MAPPED, // address reported by PCP
152
  LOCAL_MANUAL, // address explicitly specified (-externalip=)
153
 
154
  LOCAL_MAX
@@ -220,6 +224,10 @@ public:
220
  TransportProtocolType m_transport_type;
221
  /** BIP324 session id string in hex, if any. */
222
  std::string m_session_id;
 
 
 
 
223
  };
224
 
225
 
@@ -664,6 +672,8 @@ struct CNodeOptions
664
  NetPermissionFlags permission_flags = NetPermissionFlags::None;
665
  std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
666
  bool prefer_evict = false;
 
 
667
  size_t recv_flood_size{DEFAULT_MAXRECEIVEBUFFER * 1000};
668
  bool use_v2transport = false;
669
  };
@@ -721,6 +731,7 @@ public:
721
  */
722
  std::string cleanSubVer GUARDED_BY(m_subver_mutex){};
723
  const bool m_prefer_evict{false}; // This peer is preferred for eviction.
 
724
  bool HasPermission(NetPermissionFlags permission) const {
725
  return NetPermissions::HasFlag(m_permission_flags, permission);
726
  }
@@ -736,6 +747,10 @@ public:
736
  std::atomic_bool fPauseRecv{false};
737
  std::atomic_bool fPauseSend{false};
738
 
 
 
 
 
739
  const ConnectionType m_conn_type;
740
 
741
  /** Move all messages from the received queue to the processing queue. */
@@ -887,6 +902,7 @@ public:
887
  const std::string& addrNameIn,
888
  ConnectionType conn_type_in,
889
  bool inbound_onion,
 
890
  CNodeOptions&& node_opts = {});
891
  CNode(const CNode&) = delete;
892
  CNode& operator=(const CNode&) = delete;
@@ -945,6 +961,25 @@ public:
945
 
946
  void CopyStats(CNodeStats& stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);
947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
948
  std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }
949
 
950
  /**
@@ -969,6 +1004,9 @@ public:
969
  m_min_ping_time = std::min(m_min_ping_time.load(), ping_time);
970
  }
971
 
 
 
 
972
  private:
973
  const NodeId id;
974
  const uint64_t nLocalHostNonce;
@@ -1069,6 +1107,7 @@ public:
1069
  std::vector<NetWhitebindPermissions> vWhiteBinds;
1070
  std::vector<CService> vBinds;
1071
  std::vector<CService> onion_binds;
 
1072
  /// True if the user did not specify -bind= or -whitebind= and thus
1073
  /// we should bind on `0.0.0.0` (IPv4) and `::` (IPv6).
1074
  bool bind_on_any;
@@ -1078,6 +1117,8 @@ public:
1078
  bool m_i2p_accept_incoming;
1079
  bool whitelist_forcerelay = DEFAULT_WHITELISTFORCERELAY;
1080
  bool whitelist_relay = DEFAULT_WHITELISTRELAY;
 
 
1081
  };
1082
 
1083
  void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_total_bytes_sent_mutex)
@@ -1112,11 +1153,18 @@ public:
1112
  m_added_node_params.push_back({added_node, use_v2transport});
1113
  }
1114
  }
 
1115
  m_onion_binds = connOptions.onion_binds;
 
1116
  whitelist_forcerelay = connOptions.whitelist_forcerelay;
1117
  whitelist_relay = connOptions.whitelist_relay;
 
 
1118
  }
1119
 
 
 
 
1120
  CConnman(uint64_t seed0, uint64_t seed1, AddrMan& addrman, const NetGroupManager& netgroupman,
1121
  const CChainParams& params, bool network_active = true);
1122
 
@@ -1125,9 +1173,10 @@ public:
1125
  bool Start(CScheduler& scheduler, const Options& options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex, !m_added_nodes_mutex, !m_addr_fetches_mutex, !mutexMsgProc);
1126
 
1127
  void StopThreads();
1128
- void StopNodes();
1129
- void Stop()
1130
  {
 
1131
  StopThreads();
1132
  StopNodes();
1133
  };
@@ -1268,10 +1317,13 @@ public:
1268
  void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
1269
 
1270
  /** Return true if we should disconnect the peer for failing an inactivity check. */
1271
- bool ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const;
1272
 
1273
  bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex);
1274
 
 
 
 
1275
  private:
1276
  struct ListenSocket {
1277
  public:
@@ -1318,7 +1370,7 @@ private:
1318
  void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
1319
  void NotifyNumConnectionsChanged();
1320
  /** Return true if the peer is inactive and should be disconnected. */
1321
- bool InactivityCheck(const CNode& node) const;
1322
 
1323
  /**
1324
  * Generate a collection of sockets to check for IO readiness.
@@ -1362,7 +1414,14 @@ private:
1362
  */
1363
  bool AlreadyConnectedToAddress(const CAddress& addr);
1364
 
1365
- bool AttemptToEvictConnection();
 
 
 
 
 
 
 
1366
  CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1367
  void AddWhitelistPermissionFlags(NetPermissionFlags& flags, std::optional<CNetAddr> addr, const std::vector<NetWhitelistPermissions>& ranges) const;
1368
 
@@ -1573,11 +1632,14 @@ private:
1573
  */
1574
  std::atomic_bool m_start_extra_block_relay_peers{false};
1575
 
 
 
1576
  /**
1577
  * A vector of -bind=<address>:<port>=onion arguments each of which is
1578
  * an address and port that are designated for incoming Tor connections.
1579
  */
1580
  std::vector<CService> m_onion_binds;
 
1581
 
1582
  /**
1583
  * flag for adding 'forcerelay' permission to whitelisted inbound
@@ -1591,6 +1653,18 @@ private:
1591
  */
1592
  bool whitelist_relay;
1593
 
 
 
 
 
 
 
 
 
 
 
 
 
1594
  /**
1595
  * Mutex protecting m_i2p_sam_sessions.
1596
  */
 
31
  #include <util/check.h>
32
  #include <util/sock.h>
33
  #include <util/threadinterrupt.h>
34
+ #include <util/time.h>
35
 
36
  #include <atomic>
37
  #include <condition_variable>
 
97
 
98
  typedef int64_t NodeId;
99
 
100
+ /** Get the score of a local address. */
101
+ int GetnScore(const CService& addr);
102
+
103
  struct AddedNodeParams {
104
  std::string m_added_node;
105
  bool m_use_v2transport;
 
152
  LOCAL_NONE, // unknown
153
  LOCAL_IF, // address a local interface listens on
154
  LOCAL_BIND, // address explicit bound to
155
+ LOCAL_MAPPED, // address reported by UPnP or PCP
156
  LOCAL_MANUAL, // address explicitly specified (-externalip=)
157
 
158
  LOCAL_MAX
 
224
  TransportProtocolType m_transport_type;
225
  /** BIP324 session id string in hex, if any. */
226
  std::string m_session_id;
227
+ /** whether this peer forced its connection by evicting another */
228
+ bool m_forced_inbound;
229
+ /** CPU time spent processing messages to/from the peer. */
230
+ std::chrono::nanoseconds m_cpu_time;
231
  };
232
 
233
 
 
672
  NetPermissionFlags permission_flags = NetPermissionFlags::None;
673
  std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
674
  bool prefer_evict = false;
675
+ // True if ForceInbound connection required evicting a peer
676
+ bool forced_inbound{false};
677
  size_t recv_flood_size{DEFAULT_MAXRECEIVEBUFFER * 1000};
678
  bool use_v2transport = false;
679
  };
 
731
  */
732
  std::string cleanSubVer GUARDED_BY(m_subver_mutex){};
733
  const bool m_prefer_evict{false}; // This peer is preferred for eviction.
734
+ const bool m_forced_inbound{false}; // This peer forced an inbound connection
735
  bool HasPermission(NetPermissionFlags permission) const {
736
  return NetPermissions::HasFlag(m_permission_flags, permission);
737
  }
 
747
  std::atomic_bool fPauseRecv{false};
748
  std::atomic_bool fPauseSend{false};
749
 
750
+ /** Network key used to prevent fingerprinting our node across networks.
751
+ * Influenced by the network and the bind address (+ bind port for inbounds) */
752
+ const uint64_t m_network_key;
753
+
754
  const ConnectionType m_conn_type;
755
 
756
  /** Move all messages from the received queue to the processing queue. */
 
902
  const std::string& addrNameIn,
903
  ConnectionType conn_type_in,
904
  bool inbound_onion,
905
+ uint64_t network_key,
906
  CNodeOptions&& node_opts = {});
907
  CNode(const CNode&) = delete;
908
  CNode& operator=(const CNode&) = delete;
 
961
 
962
  void CopyStats(CNodeStats& stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);
963
 
964
+ bool PunishInvalidBlocks() const
965
+ {
966
+ if (HasPermission(NetPermissionFlags::NoBan)) {
967
+ return false;
968
+ }
969
+ switch (m_conn_type) {
970
+ case ConnectionType::INBOUND:
971
+ case ConnectionType::MANUAL:
972
+ case ConnectionType::FEELER:
973
+ return false;
974
+ case ConnectionType::OUTBOUND_FULL_RELAY:
975
+ case ConnectionType::BLOCK_RELAY:
976
+ case ConnectionType::ADDR_FETCH:
977
+ return true;
978
+ } // no default case, so the compiler can warn about missing cases
979
+
980
+ assert(false);
981
+ }
982
+
983
  std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }
984
 
985
  /**
 
1004
  m_min_ping_time = std::min(m_min_ping_time.load(), ping_time);
1005
  }
1006
 
1007
+ /** CPU time spent processing messages to/from the peer. */
1008
+ std::atomic<std::chrono::nanoseconds> m_cpu_time;
1009
+
1010
  private:
1011
  const NodeId id;
1012
  const uint64_t nLocalHostNonce;
 
1107
  std::vector<NetWhitebindPermissions> vWhiteBinds;
1108
  std::vector<CService> vBinds;
1109
  std::vector<CService> onion_binds;
1110
+ bool listenonion{false};
1111
  /// True if the user did not specify -bind= or -whitebind= and thus
1112
  /// we should bind on `0.0.0.0` (IPv4) and `::` (IPv6).
1113
  bool bind_on_any;
 
1117
  bool m_i2p_accept_incoming;
1118
  bool whitelist_forcerelay = DEFAULT_WHITELISTFORCERELAY;
1119
  bool whitelist_relay = DEFAULT_WHITELISTRELAY;
1120
+ bool m_capture_messages = false;
1121
+ bool disable_v1conn_clearnet = false;
1122
  };
1123
 
1124
  void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_total_bytes_sent_mutex)
 
1153
  m_added_node_params.push_back({added_node, use_v2transport});
1154
  }
1155
  }
1156
+ m_normal_binds = connOptions.vBinds;
1157
  m_onion_binds = connOptions.onion_binds;
1158
+ m_listenonion = connOptions.listenonion;
1159
  whitelist_forcerelay = connOptions.whitelist_forcerelay;
1160
  whitelist_relay = connOptions.whitelist_relay;
1161
+ m_capture_messages = connOptions.m_capture_messages;
1162
+ disable_v1conn_clearnet = connOptions.disable_v1conn_clearnet;
1163
  }
1164
 
1165
+ // test only
1166
+ void SetCaptureMessages(bool cap) { m_capture_messages = cap; }
1167
+
1168
  CConnman(uint64_t seed0, uint64_t seed1, AddrMan& addrman, const NetGroupManager& netgroupman,
1169
  const CChainParams& params, bool network_active = true);
1170
 
 
1173
  bool Start(CScheduler& scheduler, const Options& options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex, !m_added_nodes_mutex, !m_addr_fetches_mutex, !mutexMsgProc);
1174
 
1175
  void StopThreads();
1176
+ void StopNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex);
1177
+ void Stop() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex)
1178
  {
1179
+ AssertLockNotHeld(m_reconnections_mutex);
1180
  StopThreads();
1181
  StopNodes();
1182
  };
 
1317
  void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
1318
 
1319
  /** Return true if we should disconnect the peer for failing an inactivity check. */
1320
+ bool ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const;
1321
 
1322
  bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex);
1323
 
1324
+ /* Returns true if outbound v1 connections need to be disabled on IPV4/IPV6 network. */
1325
+ bool DisableV1OnClearnet(Network net) const;
1326
+
1327
  private:
1328
  struct ListenSocket {
1329
  public:
 
1370
  void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
1371
  void NotifyNumConnectionsChanged();
1372
  /** Return true if the peer is inactive and should be disconnected. */
1373
+ bool InactivityCheck(const CNode& node, std::chrono::microseconds now) const;
1374
 
1375
  /**
1376
  * Generate a collection of sockets to check for IO readiness.
 
1414
  */
1415
  bool AlreadyConnectedToAddress(const CAddress& addr);
1416
 
1417
+ /**
1418
+ * Attempt to disconnect a connected peer.
1419
+ * Used to make room for new inbound connections, returns true if successful.
1420
+ * @param[in] force Try to evict a random inbound ban-able peer if
1421
+ * all connections are otherwise protected.
1422
+ */
1423
+ bool AttemptToEvictConnection(bool force);
1424
+
1425
  CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1426
  void AddWhitelistPermissionFlags(NetPermissionFlags& flags, std::optional<CNetAddr> addr, const std::vector<NetWhitelistPermissions>& ranges) const;
1427
 
 
1632
  */
1633
  std::atomic_bool m_start_extra_block_relay_peers{false};
1634
 
1635
+ std::vector<CService> m_normal_binds;
1636
+
1637
  /**
1638
  * A vector of -bind=<address>:<port>=onion arguments each of which is
1639
  * an address and port that are designated for incoming Tor connections.
1640
  */
1641
  std::vector<CService> m_onion_binds;
1642
+ bool m_listenonion;
1643
 
1644
  /**
1645
  * flag for adding 'forcerelay' permission to whitelisted inbound
 
1653
  */
1654
  bool whitelist_relay;
1655
 
1656
+ /**
1657
+ * flag for whether messages are captured
1658
+ */
1659
+ bool m_capture_messages{false};
1660
+
1661
+ /**
1662
+ * option for disabling outbound v1 connections on IPV4 and IPV6.
1663
+ * outbound connections on IPV4/IPV6 need to be v2 connections.
1664
+ * outbound connections on Tor/I2P/CJDNS can be v1 or v2 connections.
1665
+ */
1666
+ bool disable_v1conn_clearnet;
1667
+
1668
  /**
1669
  * Mutex protecting m_i2p_sam_sessions.
1670
  */