Skip to content

Commit 6b3704e

Browse files
authored
feat: add option to disable route setup on macOS (#112)
1 parent f2021b8 commit 6b3704e

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/platform/macos/device.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ impl Device {
172172
config
173173
.netmask
174174
.unwrap_or(IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0))),
175+
config.platform_config.disable_routing,
175176
)?;
176177

177178
Ok(device)
@@ -192,7 +193,13 @@ impl Device {
192193
}
193194

194195
/// Set the IPv4 alias of the device.
195-
fn set_alias(&mut self, addr: IpAddr, broadaddr: IpAddr, mask: IpAddr) -> Result<()> {
196+
fn set_alias(
197+
&mut self,
198+
addr: IpAddr,
199+
broadaddr: IpAddr,
200+
mask: IpAddr,
201+
disable_routing: bool,
202+
) -> Result<()> {
196203
let IpAddr::V4(addr) = addr else {
197204
unimplemented!("do not support IPv6 yet")
198205
};
@@ -219,13 +226,15 @@ impl Device {
219226
if let Err(err) = siocaifaddr(ctl.as_raw_fd(), &req) {
220227
return Err(std::io::Error::from(err).into());
221228
}
222-
let route = Route {
223-
addr,
224-
netmask: mask,
225-
dest: broadaddr,
226-
};
227-
if let Err(e) = self.set_route(route) {
228-
log::warn!("{e:?}");
229+
if !disable_routing {
230+
let route = Route {
231+
addr,
232+
netmask: mask,
233+
dest: broadaddr,
234+
};
235+
if let Err(e) = self.set_route(route) {
236+
log::warn!("{e:?}");
237+
}
229238
}
230239
Ok(())
231240
}

src/platform/macos/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ use crate::error::Result;
2626
#[derive(Copy, Clone, Debug)]
2727
pub struct PlatformConfig {
2828
pub(crate) packet_information: bool,
29+
pub(crate) disable_routing: bool,
2930
}
3031

3132
impl Default for PlatformConfig {
3233
fn default() -> Self {
3334
PlatformConfig {
3435
packet_information: true, // default is true in macOS
36+
disable_routing: false,
3537
}
3638
}
3739
}
@@ -56,6 +58,12 @@ impl PlatformConfig {
5658
self.packet_information = value;
5759
self
5860
}
61+
62+
/// Do not setup route for utun interface automatically
63+
pub fn disable_routing(&mut self, value: bool) -> &mut Self {
64+
self.disable_routing = value;
65+
self
66+
}
5967
}
6068

6169
/// Create a TUN device with the given name.

0 commit comments

Comments
 (0)