Skip to content

Commit 839cf56

Browse files
committed
Simplify route manager handle
1 parent 517de3a commit 839cf56

File tree

17 files changed

+183
-316
lines changed

17 files changed

+183
-316
lines changed

talpid-core/src/dns/linux/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use self::{
88
systemd_resolved::SystemdResolved,
99
};
1010
use std::{env, fmt, net::IpAddr};
11-
use talpid_routing::RouteManagerHandle;
11+
use talpid_routing::RouteManager;
1212

1313
pub type Result<T> = std::result::Result<T, Error>;
1414

@@ -37,15 +37,15 @@ pub enum Error {
3737
}
3838

3939
pub struct DnsMonitor {
40-
route_manager: RouteManagerHandle,
40+
route_manager: RouteManager,
4141
handle: tokio::runtime::Handle,
4242
inner: Option<DnsMonitorHolder>,
4343
}
4444

4545
impl super::DnsMonitorT for DnsMonitor {
4646
type Error = Error;
4747

48-
fn new(handle: tokio::runtime::Handle, route_manager: RouteManagerHandle) -> Result<Self> {
48+
fn new(handle: tokio::runtime::Handle, route_manager: RouteManager) -> Result<Self> {
4949
Ok(DnsMonitor {
5050
route_manager,
5151
handle,
@@ -129,7 +129,7 @@ impl DnsMonitorHolder {
129129
fn set(
130130
&mut self,
131131
handle: &tokio::runtime::Handle,
132-
route_manager: &RouteManagerHandle,
132+
route_manager: &RouteManager,
133133
interface: &str,
134134
servers: &[IpAddr],
135135
) -> Result<()> {

talpid-core/src/dns/linux/systemd_resolved.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::linux::{iface_index, IfaceIndexLookupError};
22
use std::net::IpAddr;
33
use talpid_dbus::systemd_resolved::{AsyncHandle, SystemdResolved as DbusInterface};
4-
use talpid_routing::RouteManagerHandle;
4+
use talpid_routing::RouteManager;
55
use talpid_types::ErrorExt;
66

77
pub(crate) use talpid_dbus::systemd_resolved::Error as SystemdDbusError;
@@ -36,7 +36,7 @@ impl SystemdResolved {
3636

3737
pub async fn set_dns(
3838
&mut self,
39-
_route_manager: RouteManagerHandle,
39+
_route_manager: RouteManager,
4040
interface_name: &str,
4141
servers: &[IpAddr],
4242
) -> Result<()> {

talpid-core/src/dns/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::net::IpAddr;
22
#[cfg(target_os = "linux")]
3-
use talpid_routing::RouteManagerHandle;
3+
use talpid_routing::RouteManager;
44

55
#[cfg(target_os = "macos")]
66
use {
@@ -38,7 +38,7 @@ impl DnsMonitor {
3838
/// Returns a new `DnsMonitor` that can set and monitor the system DNS.
3939
pub fn new(
4040
#[cfg(target_os = "linux")] handle: tokio::runtime::Handle,
41-
#[cfg(target_os = "linux")] route_manager: RouteManagerHandle,
41+
#[cfg(target_os = "linux")] route_manager: RouteManager,
4242
#[cfg(target_os = "macos")] tx: Weak<UnboundedSender<TunnelCommand>>,
4343
) -> Result<Self, Error> {
4444
Ok(DnsMonitor {
@@ -94,7 +94,7 @@ trait DnsMonitorT: Sized {
9494

9595
fn new(
9696
#[cfg(target_os = "linux")] handle: tokio::runtime::Handle,
97-
#[cfg(target_os = "linux")] route_manager: RouteManagerHandle,
97+
#[cfg(target_os = "linux")] route_manager: RouteManager,
9898
#[cfg(target_os = "macos")] tx: Weak<UnboundedSender<TunnelCommand>>,
9999
) -> Result<Self, Self::Error>;
100100

talpid-core/src/offline/linux.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
net::{IpAddr, Ipv4Addr, Ipv6Addr},
44
sync::Arc,
55
};
6-
use talpid_routing::RouteManagerHandle;
6+
use talpid_routing::RouteManager;
77
use talpid_types::{net::Connectivity, ErrorExt};
88

99
pub type Result<T> = std::result::Result<T, Error>;
@@ -15,7 +15,7 @@ pub enum Error {
1515
}
1616

1717
pub struct MonitorHandle {
18-
route_manager: RouteManagerHandle,
18+
route_manager: RouteManager,
1919
fwmark: Option<u32>,
2020
_notify_tx: Arc<UnboundedSender<Connectivity>>,
2121
}
@@ -42,7 +42,7 @@ impl MonitorHandle {
4242

4343
pub async fn spawn_monitor(
4444
notify_tx: UnboundedSender<Connectivity>,
45-
route_manager: RouteManagerHandle,
45+
route_manager: RouteManager,
4646
fwmark: Option<u32>,
4747
) -> Result<MonitorHandle> {
4848
let mut is_offline = public_ip_unreachable(&route_manager, fwmark).await?;
@@ -86,10 +86,7 @@ pub async fn spawn_monitor(
8686
Ok(monitor_handle)
8787
}
8888

89-
async fn public_ip_unreachable(
90-
handle: &RouteManagerHandle,
91-
fwmark: Option<u32>,
92-
) -> Result<Connectivity> {
89+
async fn public_ip_unreachable(handle: &RouteManager, fwmark: Option<u32>) -> Result<Connectivity> {
9390
let route_exists = |destination| async move {
9491
handle
9592
.get_destination_route(destination, fwmark)

talpid-core/src/offline/macos.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! that the app gets stuck in an offline state, blocking all internet access and preventing the
33
//! user from connecting to a relay.
44
//!
5-
//! See [RouteManagerHandle::default_route_listener].
5+
//! See [RouteManager::default_route_listener].
66
//!
77
//! This offline monitor synthesizes an offline state between network switches and before coming
88
//! online from an offline state. This is done to work around issues with DNS being blocked due
@@ -17,7 +17,7 @@ use std::{
1717
sync::{Arc, Mutex},
1818
time::Duration,
1919
};
20-
use talpid_routing::{DefaultRouteEvent, RouteManagerHandle};
20+
use talpid_routing::{DefaultRouteEvent, RouteManager};
2121
use talpid_types::net::Connectivity;
2222

2323
const SYNTHETIC_OFFLINE_DURATION: Duration = Duration::from_secs(1);
@@ -65,7 +65,7 @@ impl ConnectivityInner {
6565

6666
pub async fn spawn_monitor(
6767
notify_tx: UnboundedSender<Connectivity>,
68-
route_manager_handle: RouteManagerHandle,
68+
route_manager_handle: RouteManager,
6969
) -> Result<MonitorHandle, Error> {
7070
let notify_tx = Arc::new(notify_tx);
7171

talpid-core/src/offline/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use futures::channel::mpsc::UnboundedSender;
22
use once_cell::sync::Lazy;
33
#[cfg(not(target_os = "android"))]
4-
use talpid_routing::RouteManagerHandle;
4+
use talpid_routing::RouteManager;
55
#[cfg(target_os = "android")]
66
use talpid_types::android::AndroidContext;
77
use talpid_types::net::Connectivity;
@@ -45,7 +45,7 @@ impl MonitorHandle {
4545
#[cfg(not(target_os = "android"))]
4646
pub async fn spawn_monitor(
4747
sender: UnboundedSender<Connectivity>,
48-
route_manager: RouteManagerHandle,
48+
route_manager: RouteManager,
4949
#[cfg(target_os = "linux")] fwmark: Option<u32>,
5050
) -> Result<MonitorHandle, Error> {
5151
let monitor = if *FORCE_DISABLE_OFFLINE_MONITOR {

talpid-core/src/offline/windows.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
sync::{Arc, Weak},
77
time::Duration,
88
};
9-
use talpid_routing::{get_best_default_route, CallbackHandle, EventType, RouteManagerHandle};
9+
use talpid_routing::{get_best_default_route, CallbackHandle, EventType, RouteManager};
1010
use talpid_types::{net::Connectivity, ErrorExt};
1111
use talpid_windows::net::AddressFamily;
1212

@@ -29,7 +29,7 @@ unsafe impl Send for BroadcastListener {}
2929
impl BroadcastListener {
3030
pub async fn start(
3131
notify_tx: UnboundedSender<Connectivity>,
32-
route_manager_handle: RouteManagerHandle,
32+
route_manager_handle: RouteManager,
3333
mut power_mgmt_rx: PowerManagementListener,
3434
) -> Result<Self, Error> {
3535
let notify_tx = Arc::new(notify_tx);
@@ -107,7 +107,7 @@ impl BroadcastListener {
107107
/// until after `WinNet_DeactivateConnectivityMonitor` has been called.
108108
async fn setup_network_connectivity_listener(
109109
system_state: Arc<Mutex<SystemState>>,
110-
route_manager_handle: RouteManagerHandle,
110+
route_manager_handle: RouteManager,
111111
) -> Result<CallbackHandle, Error> {
112112
let change_handle = route_manager_handle
113113
.add_default_route_change_callback(Box::new(move |event, addr_family| {
@@ -202,7 +202,7 @@ pub type MonitorHandle = BroadcastListener;
202202

203203
pub async fn spawn_monitor(
204204
sender: UnboundedSender<Connectivity>,
205-
route_manager_handle: RouteManagerHandle,
205+
route_manager_handle: RouteManager,
206206
) -> Result<MonitorHandle, Error> {
207207
let power_mgmt_rx = crate::window::PowerManagementListener::new();
208208
BroadcastListener::start(sender, route_manager_handle, power_mgmt_rx).await

talpid-core/src/split_tunnel/windows/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::{
1818
},
1919
time::Duration,
2020
};
21-
use talpid_routing::{get_best_default_route, CallbackHandle, EventType, RouteManagerHandle};
21+
use talpid_routing::{get_best_default_route, CallbackHandle, EventType, RouteManager};
2222
use talpid_types::{split_tunnel::ExcludedProcess, tunnel::ErrorStateCause, ErrorExt};
2323
use talpid_windows::{
2424
io::Overlapped,
@@ -112,7 +112,7 @@ pub struct SplitTunnel {
112112
_route_change_callback: Option<CallbackHandle>,
113113
daemon_tx: Weak<mpsc::UnboundedSender<TunnelCommand>>,
114114
async_path_update_in_progress: Arc<AtomicBool>,
115-
route_manager: RouteManagerHandle,
115+
route_manager: RouteManager,
116116
}
117117

118118
enum Request {
@@ -166,7 +166,7 @@ impl SplitTunnel {
166166
resource_dir: PathBuf,
167167
daemon_tx: Weak<mpsc::UnboundedSender<TunnelCommand>>,
168168
volume_update_rx: mpsc::UnboundedReceiver<()>,
169-
route_manager: RouteManagerHandle,
169+
route_manager: RouteManager,
170170
) -> Result<Self, Error> {
171171
let excluded_processes = Arc::new(RwLock::new(HashMap::new()));
172172

talpid-core/src/tunnel/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::logging;
33
use futures::channel::oneshot;
44
use std::path;
55
#[cfg(not(target_os = "android"))]
6-
use talpid_routing::RouteManagerHandle;
6+
use talpid_routing::RouteManager;
77
pub use talpid_tunnel::{TunnelArgs, TunnelEvent, TunnelMetadata};
88
#[cfg(not(target_os = "android"))]
99
use talpid_types::net::openvpn as openvpn_types;
@@ -227,7 +227,7 @@ impl TunnelMonitor {
227227
resource_dir: &path::Path,
228228
on_event: L,
229229
tunnel_close_rx: oneshot::Receiver<()>,
230-
route_manager: RouteManagerHandle,
230+
route_manager: RouteManager,
231231
) -> Result<Self>
232232
where
233233
L: (Fn(TunnelEvent) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>)

talpid-core/src/tunnel_state_machine/connecting_state.rs

+3-22
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ impl ConnectingState {
6060
if shared_values.connectivity.is_offline() {
6161
// FIXME: Temporary: Nudge route manager to update the default interface
6262
#[cfg(target_os = "macos")]
63-
if let Ok(handle) = shared_values.route_manager.handle() {
63+
{
6464
log::debug!("Poking route manager to update default routes");
65-
let _ = handle.refresh_routes();
65+
let _ = shared_values.route_manager.refresh_routes();
6666
}
6767
return ErrorState::enter(shared_values, ErrorStateCause::IsOffline);
6868
}
@@ -202,7 +202,7 @@ impl ConnectingState {
202202
})
203203
};
204204

205-
let route_manager_handle = route_manager.handle();
205+
let route_manager_handle = route_manager.clone();
206206
let log_dir = log_dir.clone();
207207
let resource_dir = resource_dir.to_path_buf();
208208

@@ -214,25 +214,6 @@ impl ConnectingState {
214214
tokio::task::spawn_blocking(move || {
215215
let start = Instant::now();
216216

217-
let route_manager_handle = match route_manager_handle {
218-
Ok(handle) => handle,
219-
Err(error) => {
220-
if tunnel_close_event_tx
221-
.send(Some(ErrorStateCause::StartTunnelError))
222-
.is_err()
223-
{
224-
log::warn!(
225-
"Tunnel state machine stopped before receiving tunnel closed event"
226-
);
227-
}
228-
log::error!(
229-
"{}",
230-
error.display_chain_with_msg("Failed to obtain route monitor handle")
231-
);
232-
return;
233-
}
234-
};
235-
236217
let args = TunnelArgs {
237218
runtime,
238219
resource_dir: &resource_dir,

talpid-core/src/tunnel_state_machine/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl TunnelStateMachine {
269269
#[cfg(target_os = "macos")]
270270
let filtering_resolver = crate::resolver::start_resolver().await?;
271271

272-
let route_manager = RouteManager::new(
272+
let route_manager = RouteManager::spawn(
273273
#[cfg(target_os = "linux")]
274274
args.linux_ids.fwmark,
275275
#[cfg(target_os = "linux")]
@@ -284,9 +284,7 @@ impl TunnelStateMachine {
284284
args.resource_dir.clone(),
285285
args.command_tx.clone(),
286286
volume_update_rx,
287-
route_manager
288-
.handle()
289-
.map_err(Error::InitRouteManagerError)?,
287+
route_manager.clone(),
290288
)
291289
.map_err(Error::InitSplitTunneling)?;
292290

@@ -308,9 +306,7 @@ impl TunnelStateMachine {
308306
#[cfg(target_os = "linux")]
309307
runtime.clone(),
310308
#[cfg(target_os = "linux")]
311-
route_manager
312-
.handle()
313-
.map_err(Error::InitRouteManagerError)?,
309+
route_manager.clone(),
314310
#[cfg(target_os = "macos")]
315311
args.command_tx.clone(),
316312
)
@@ -331,7 +327,7 @@ impl TunnelStateMachine {
331327
let offline_monitor = offline::spawn_monitor(
332328
offline_tx,
333329
#[cfg(not(target_os = "android"))]
334-
route_manager.handle()?,
330+
route_manager.clone(),
335331
#[cfg(target_os = "linux")]
336332
Some(args.linux_ids.fwmark),
337333
#[cfg(target_os = "android")]

talpid-openvpn/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl OpenVpnMonitor<OpenVpnCommand> {
249249
params: &openvpn::TunnelParameters,
250250
log_path: Option<PathBuf>,
251251
resource_dir: &Path,
252-
route_manager: talpid_routing::RouteManagerHandle,
252+
route_manager: talpid_routing::RouteManager,
253253
) -> Result<Self>
254254
where
255255
L: (Fn(TunnelEvent) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>)
@@ -817,7 +817,7 @@ mod event_server {
817817
pub proxy_auth_file_path: Option<super::PathBuf>,
818818
pub abort_server_tx: triggered::Trigger,
819819
pub proxy: Option<CustomProxy>,
820-
pub route_manager_handle: talpid_routing::RouteManagerHandle,
820+
pub route_manager_handle: talpid_routing::RouteManager,
821821
#[cfg(target_os = "linux")]
822822
pub ipv6_enabled: bool,
823823
}

talpid-routing/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use netlink_packet_route::rtnl::constants::RT_TABLE_MAIN;
2525
#[cfg(target_os = "macos")]
2626
pub use imp::{imp::RouteError, DefaultRouteEvent, PlatformError};
2727

28-
pub use imp::{Error, RouteManager, RouteManagerHandle};
28+
pub use imp::{Error, RouteManager};
2929

3030
/// A network route with a specific network node, destination and an optional metric.
3131
#[derive(Debug, Hash, Eq, PartialEq, Clone)]

0 commit comments

Comments
 (0)