Skip to content

Commit

Permalink
Add Multiaddr with range checks
Browse files Browse the repository at this point in the history
Added MultiaddrRange, which implements range checks for Multiaddr, when using IP4
with TCP. This enables specifying a range of IP4 with TCP addresses.
  • Loading branch information
hansieodendaal committed Sep 12, 2024
1 parent d69fd66 commit 46c0a23
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 26 deletions.
1 change: 0 additions & 1 deletion base_layer/contacts/tests/contacts_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ pub fn setup_contacts_service<T: ContactsBackend + 'static>(
auto_request: true,
..Default::default()
},
excluded_dial_addresses: vec![],
..Default::default()
},
allow_test_addresses: true,
Expand Down
3 changes: 2 additions & 1 deletion base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ use tari_common_types::{
};
use tari_comms::{
multiaddr::Multiaddr,
net_address::IP4_TCP_TEST_ADDR_RANGE,
peer_manager::{NodeIdentity, PeerQuery},
transports::MemoryTransport,
types::CommsPublicKey,
Expand Down Expand Up @@ -5327,7 +5328,7 @@ pub unsafe extern "C" fn comms_config_create(
minimum_desired_tcpv4_node_ratio: 0.0,
..Default::default()
},
excluded_dial_addresses: vec![],
excluded_dial_addresses: vec![IP4_TCP_TEST_ADDR_RANGE.parse().expect("valid address range")],
..Default::default()
},
allow_test_addresses: true,
Expand Down
3 changes: 2 additions & 1 deletion comms/core/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::{
connection_manager::{ConnectionManagerConfig, ConnectionManagerRequester},
connectivity::{ConnectivityConfig, ConnectivityRequester},
multiaddr::Multiaddr,
net_address::MultiaddrRange,
peer_manager::{NodeIdentity, PeerManager},
peer_validator::PeerValidatorConfig,
protocol::{NodeNetworkInfo, ProtocolExtensions},
Expand Down Expand Up @@ -242,7 +243,7 @@ impl CommsBuilder {
self
}

pub fn with_excluded_dial_addresses(mut self, excluded_addresses: Vec<Multiaddr>) -> Self {
pub fn with_excluded_dial_addresses(mut self, excluded_addresses: Vec<MultiaddrRange>) -> Self {
self.connection_manager_config.excluded_dial_addresses = excluded_addresses;
self
}
Expand Down
6 changes: 3 additions & 3 deletions comms/core/src/connection_manager/dialer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use crate::{
},
multiaddr::Multiaddr,
multiplexing::Yamux,
net_address::PeerAddressSource,
net_address::{MultiaddrRange, PeerAddressSource},
noise::{NoiseConfig, NoiseSocket},
peer_manager::{NodeId, NodeIdentity, Peer, PeerManager},
protocol::ProtocolId,
Expand Down Expand Up @@ -557,7 +557,7 @@ where
noise_config: &NoiseConfig,
transport: &TTransport,
network_byte: u8,
excluded_dial_addresses: Vec<Multiaddr>,
excluded_dial_addresses: Vec<MultiaddrRange>,
) -> (
DialState,
Result<(NoiseSocket<TTransport::Output>, Multiaddr), ConnectionManagerError>,
Expand All @@ -568,7 +568,7 @@ where
.clone()
.into_vec()
.iter()
.filter(|&a| !excluded_dial_addresses.iter().any(|excluded| a == excluded))
.filter(|&a| !excluded_dial_addresses.iter().any(|excluded| excluded.contains(a)))
.cloned()
.collect::<Vec<_>>();
if addresses.is_empty() {
Expand Down
3 changes: 2 additions & 1 deletion comms/core/src/connection_manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use crate::{
backoff::Backoff,
connection_manager::ConnectionId,
multiplexing::Substream,
net_address::MultiaddrRange,
noise::NoiseConfig,
peer_manager::{NodeId, NodeIdentity, PeerManagerError},
peer_validator::PeerValidatorConfig,
Expand Down Expand Up @@ -134,7 +135,7 @@ pub struct ConnectionManagerConfig {
/// Peer validation configuration. See [PeerValidatorConfig]
pub peer_validation_config: PeerValidatorConfig,
/// Addresses that should never be dialed
pub excluded_dial_addresses: Vec<Multiaddr>,
pub excluded_dial_addresses: Vec<MultiaddrRange>,
}

impl Default for ConnectionManagerConfig {
Expand Down
2 changes: 1 addition & 1 deletion comms/core/src/connection_manager/tests/listener_dialer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ async fn excluded_yes() {
let (request_tx, request_rx) = mpsc::channel(1);
let peer_manager2 = build_peer_manager();
let connection_manager_config = ConnectionManagerConfig {
excluded_dial_addresses: vec![address.clone()],
excluded_dial_addresses: vec![address.to_string().parse().unwrap()],
..Default::default()
};
let mut dialer = Dialer::new(
Expand Down
3 changes: 3 additions & 0 deletions comms/core/src/net_address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ pub use multiaddr_with_stats::{MultiaddrWithStats, PeerAddressSource};

mod mutliaddresses_with_stats;
pub use mutliaddresses_with_stats::MultiaddressesWithStats;

mod multiaddr_range;
pub use multiaddr_range::{MultiaddrRange, IP4_TCP_TEST_ADDR_RANGE};
Loading

0 comments on commit 46c0a23

Please sign in to comment.