Skip to content

Commit

Permalink
Detect reachability through catching the error
Browse files Browse the repository at this point in the history
  • Loading branch information
umgefahren committed Jun 14, 2024
1 parent 236b356 commit b0adbd7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion protocols/autonat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rand = "0.8"
rand_core = { version = "0.6", optional = true }
thiserror = { version = "1.0.52", optional = true }
void = { version = "1", optional = true }
libc = { version = "0.2", optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt", "sync"]}
Expand All @@ -43,7 +44,8 @@ libp2p-swarm = { workspace = true, features = ["macros"]}
[features]
default = ["v1", "v2"]
v1 = ["dep:libp2p-request-response"]
v2 = ["dep:bytes", "dep:either", "dep:futures-bounded", "dep:thiserror", "dep:void", "dep:rand_core"]
v2 = ["dep:bytes", "dep:either", "dep:futures-bounded", "dep:thiserror", "dep:void",
"dep:rand_core", "dep:libc"]

[lints]
workspace = true
Expand Down
31 changes: 27 additions & 4 deletions protocols/autonat/src/v2/server/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use std::{

use crate::v2::server::handler::dial_request::DialBackStatus;
use either::Either;
use libp2p_core::{transport::PortUse, Endpoint, Multiaddr};
use libp2p_core::{
transport::{PortUse, TransportError},
Endpoint, Multiaddr,
};
use libp2p_identity::PeerId;
use libp2p_swarm::dial_opts::PeerCondition;
use libp2p_swarm::{
dial_opts::DialOpts, dummy, ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure,
FromSwarm, NetworkBehaviour, ToSwarm,
};
use libp2p_swarm::{dial_opts::PeerCondition, DialError};
use rand_core::{OsRng, RngCore};

use crate::v2::server::handler::{
Expand Down Expand Up @@ -91,11 +94,25 @@ where
}

fn on_swarm_event(&mut self, event: FromSwarm) {
if let FromSwarm::DialFailure(DialFailure { connection_id, .. }) = event {
if let FromSwarm::DialFailure(DialFailure {
connection_id,
error,
..
}) = event
{
if let Some(DialBackCommand { back_channel, .. }) =
self.dialing_dial_back.remove(&connection_id)
{
let _ = back_channel.send(Err(DialBackStatus::DialErr));
let dial_back_status = if let DialError::Transport(errors) = error {
if errors.into_iter().any(|(_, e)| matches!(e, TransportError::Other(ie) if is_network_unreachable(ie))) {

Check failure on line 107 in protocols/autonat/src/v2/server/behaviour.rs

View workflow job for this annotation

GitHub Actions / clippy (1.78.0)

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `Vec`

Check failure on line 107 in protocols/autonat/src/v2/server/behaviour.rs

View workflow job for this annotation

GitHub Actions / clippy (beta)

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `Vec`
DialBackStatus::Unreachable
} else {
DialBackStatus::DialErr
}
} else {
DialBackStatus::DialErr
};
let _ = back_channel.send(Err(dial_back_status));
}
}
}
Expand Down Expand Up @@ -153,3 +170,9 @@ pub struct Event {
/// The result of the test.
pub result: Result<(), io::Error>,
}

fn is_network_unreachable(err: &io::Error) -> bool {
err.raw_os_error()
.map(|e| e == libc::ENETUNREACH)

Check failure on line 176 in protocols/autonat/src/v2/server/behaviour.rs

View workflow job for this annotation

GitHub Actions / Compile on wasm32-unknown-unknown

cannot find value `ENETUNREACH` in crate `libc`
.unwrap_or(false)
}
9 changes: 9 additions & 0 deletions protocols/autonat/src/v2/server/handler/dial_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub(crate) enum DialBackStatus {
DialErr,
/// Failure during dial back
DialBackErr,
/// Unreachable
Unreachable,
}

#[derive(Debug)]
Expand Down Expand Up @@ -185,6 +187,13 @@ impl From<HandleFail> for DialResponse {
dial_status: match result {
Err(DialBackStatus::DialErr) => DialStatus::E_DIAL_ERROR,
Err(DialBackStatus::DialBackErr) => DialStatus::E_DIAL_BACK_ERROR,
Err(DialBackStatus::Unreachable) => {
return Self {
status: ResponseStatus::E_DIAL_REFUSED,
addr_idx: 0,
dial_status: DialStatus::UNUSED,
}
}
Ok(()) => DialStatus::OK,
},
},
Expand Down

0 comments on commit b0adbd7

Please sign in to comment.