Skip to content

Commit 7973189

Browse files
committed
Move error type to new mod
1 parent c31ad3f commit 7973189

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

talpid-wireguard/src/lib.rs

+32-32
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::borrow::Cow;
1414
use std::env;
1515
use std::{
1616
convert::Infallible,
17-
io,
1817
net::IpAddr,
1918
path::Path,
2019
pin::Pin,
@@ -68,33 +67,6 @@ type EventCallback = Box<dyn (Fn(TunnelEvent) -> BoxFuture<'static, ()>) + Send
6867
#[derive(err_derive::Error, Debug)]
6968
#[error(no_from)]
7069
pub enum Error {
71-
/// Failed to set up routing.
72-
#[error(display = "Failed to setup routing")]
73-
SetupRoutingError(#[error(source)] talpid_routing::Error),
74-
75-
/// Failed to set MTU
76-
#[error(display = "Failed to detect MTU because every ping was dropped.")]
77-
MtuDetectionAllDropped,
78-
79-
/// Failed to set MTU
80-
#[error(display = "Failed to detect MTU because of unexpected ping error.")]
81-
MtuDetectionPingError(#[error(source)] surge_ping::SurgeError),
82-
83-
/// Failed to set MTU
84-
#[error(
85-
display = "Failed to detect MTU because of an IO error when setting up the ping socket."
86-
)]
87-
MtuDetectionSetupSocket(#[error(source)] io::Error),
88-
89-
/// Failed to set MTU
90-
#[cfg(target_os = "macos")]
91-
#[error(display = "Failed to set buffer size")]
92-
MtuSetBufferSize(#[error(source)] nix::Error),
93-
94-
/// Failed to set MTU on the active tunnel
95-
#[error(display = "Failed to set MTU on the active tunnel")]
96-
SetMtu(#[error(source)] io::Error),
97-
9870
/// Tunnel timed out
9971
#[error(display = "Tunnel timed out")]
10072
TimeoutError,
@@ -103,6 +75,10 @@ pub enum Error {
10375
#[error(display = "Tunnel failed")]
10476
TunnelError(#[error(source)] TunnelError),
10577

78+
/// Failed to set up routing.
79+
#[error(display = "Failed to setup routing")]
80+
SetupRoutingError(#[error(source)] talpid_routing::Error),
81+
10682
/// Failed to create tunnel obfuscator
10783
#[error(display = "Failed to create tunnel obfuscator")]
10884
CreateObfuscatorError(#[error(source)] ObfuscationError),
@@ -994,15 +970,39 @@ impl WireguardMonitor {
994970

995971
#[cfg(not(target_os = "android"))]
996972
mod mtu_detection {
997-
use std::{net::IpAddr, time::Duration};
973+
use std::{io, net::IpAddr, time::Duration};
998974

999975
use futures::{future, stream::FuturesUnordered, TryStreamExt};
1000976
use surge_ping::{Client, Config, PingIdentifier, PingSequence, SurgeError};
1001977
use talpid_tunnel::{ICMP_HEADER_SIZE, IPV4_HEADER_SIZE, MIN_IPV4_MTU};
1002978
use tokio_stream::StreamExt;
1003979

1004-
use super::Error;
980+
#[derive(err_derive::Error, Debug)]
981+
#[error(no_from)]
982+
pub enum Error {
983+
/// Failed to set MTU on the active tunnel
984+
#[error(display = "Failed to set MTU on the active tunnel")]
985+
SetMtu(#[error(source)] io::Error),
986+
987+
/// Failed to set MTU
988+
#[error(display = "Failed to detect MTU because every ping was dropped.")]
989+
MtuDetectionAllDropped,
1005990

991+
/// Failed to set MTU
992+
#[error(display = "Failed to detect MTU because of unexpected ping error.")]
993+
MtuDetectionPing(#[error(source)] surge_ping::SurgeError),
994+
995+
/// Failed to set MTU
996+
#[error(
997+
display = "Failed to detect MTU because of an IO error when setting up the ping socket."
998+
)]
999+
MtuDetectionSetupSocket(#[error(source)] io::Error),
1000+
1001+
/// Failed to set MTU
1002+
#[cfg(target_os = "macos")]
1003+
#[error(display = "Failed to set buffer size")]
1004+
MtuSetBufferSize(#[error(source)] nix::Error),
1005+
}
10061006
/// Verify that the current MTU doesn't cause dropped packets, otherwise lower it to the
10071007
/// largest value which doesn't.
10081008
///
@@ -1117,15 +1117,15 @@ mod mtu_detection {
11171117
// If the first ping we get back timed out, then all of them did
11181118
SurgeError::Timeout { .. } => Error::MtuDetectionAllDropped,
11191119
// Unexpected error type
1120-
e => Error::MtuDetectionPingError(e),
1120+
e => Error::MtuDetectionPing(e),
11211121
})?;
11221122

11231123
ping_stream
11241124
.timeout(PING_OFFSET_TIMEOUT) // Start a new, shorter, timeout
11251125
.map_while(|res| res.ok()) // Stop waiting for pings after this timeout
11261126
.try_fold(first_ping_size, |acc, mtu| future::ready(Ok(acc.max(mtu)))) // Get largest ping
11271127
.await
1128-
.map_err(Error::MtuDetectionPingError)
1128+
.map_err(Error::MtuDetectionPing)
11291129
}
11301130

11311131
/// Creates a linear spacing of MTU values with the given step size. Always includes the given

0 commit comments

Comments
 (0)