Skip to content

Commit 6903f9b

Browse files
authored
Merge pull request #179 from neacsu:feature/callback_ios
Complete interface for nym device configuration
2 parents 1053674 + c24c966 commit 6903f9b

5 files changed

Lines changed: 81 additions & 10 deletions

File tree

nym-vpn-lib/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ pub enum Error {
171171

172172
#[error("{0}")]
173173
TalpidCoreMpsc(#[from] talpid_core::mpsc::Error),
174+
175+
#[cfg(target_os = "ios")]
176+
#[error("{0}")]
177+
UniffiError(#[from] crate::platform::error::FFIError),
174178
}
175179

176180
// Result type based on our error type

nym-vpn-lib/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub use nym_task::{
4545
};
4646

4747
#[cfg(target_os = "ios")]
48-
use crate::platform::ios::{initVPN, OSTunProvider, VPNConfig, WgConfig};
48+
use crate::platform::ios::{initVPN, NymConfig, OSTunProvider, VPNConfig, WgConfig};
4949
#[cfg(target_os = "macos")]
5050
use crate::platform::macos::{initVPN, VPNConfig, WgConfig};
5151
#[cfg(any(target_os = "macos", target_os = "android", target_os = "ios"))]
@@ -233,7 +233,13 @@ impl NymVpn {
233233
mixnet_client.gateway_ws_fd().await,
234234
);
235235
debug!("Routing config: {}", routing_config);
236-
let mixnet_tun_dev = routing::setup_routing(route_manager, routing_config).await?;
236+
let mixnet_tun_dev = routing::setup_routing(
237+
route_manager,
238+
routing_config,
239+
#[cfg(target_os = "ios")]
240+
self.ios_tun_provider.clone(),
241+
)
242+
.await?;
237243

238244
info!("Setting up mixnet processor");
239245
let processor_config = mixnet_processor::Config::new(*exit_router);

nym-vpn-lib/src/nym_vpn_lib_ios.udl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ dictionary WgConfig {
5656
u16 mtu;
5757
};
5858

59+
dictionary NymConfig {
60+
Ipv4Addr ipv4_addr;
61+
Ipv6Addr ipv6_addr;
62+
u16 mtu;
63+
IpAddr? entry_mixnet_gateway_ip;
64+
};
65+
5966
dictionary VPNConfig {
6067
Url api_url;
6168
Url explorer_url;
@@ -84,7 +91,7 @@ interface OSTunProvider {
8491
[Throws=FFIError]
8592
void configure_wg(WgConfig config);
8693
[Throws=FFIError]
87-
void configure_nym();
94+
i32 configure_nym(NymConfig config);
8895
};
8996

9097
namespace nym_vpn_lib_ios {

nym-vpn-lib/src/platform/ios.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
use super::*;
55
use crate::gateway_client::{EntryPoint, ExitPoint};
6+
use crate::routing::RoutingConfig;
67
use crate::NymVpn;
78
use error::FFIError;
89
use log::warn;
910
use oslog::OsLogger;
1011
use std::fmt::Debug;
11-
use std::net::{Ipv4Addr, Ipv6Addr};
12+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
13+
use std::os::fd::RawFd;
1214
use talpid_types::net::wireguard::{PeerConfig, TunnelConfig};
1315
use url::Url;
1416

@@ -50,6 +52,30 @@ impl From<talpid_wireguard::config::Config> for WgConfig {
5052
}
5153
}
5254

55+
#[derive(Clone)]
56+
pub struct NymConfig {
57+
pub ipv4_addr: Ipv4Addr,
58+
pub ipv6_addr: Ipv6Addr,
59+
pub mtu: u16,
60+
pub entry_mixnet_gateway_ip: Option<IpAddr>,
61+
}
62+
63+
impl From<RoutingConfig> for NymConfig {
64+
fn from(value: RoutingConfig) -> Self {
65+
let entry_mixnet_gateway_ip = if value.enable_wireguard() {
66+
Some(value.entry_mixnet_gateway_ip())
67+
} else {
68+
None
69+
};
70+
NymConfig {
71+
ipv4_addr: value.tun_ips().ipv4,
72+
ipv6_addr: value.tun_ips().ipv6,
73+
mtu: value.mtu(),
74+
entry_mixnet_gateway_ip,
75+
}
76+
}
77+
}
78+
5379
pub struct VPNConfig {
5480
pub api_url: Url,
5581
pub explorer_url: Url,
@@ -60,7 +86,7 @@ pub struct VPNConfig {
6086

6187
pub trait OSTunProvider: Send + Sync + Debug {
6288
fn configure_wg(&self, config: WgConfig) -> Result<(), FFIError>;
63-
fn configure_nym(&self) -> Result<(), FFIError>;
89+
fn configure_nym(&self, config: NymConfig) -> Result<RawFd, FFIError>;
6490
}
6591

6692
#[allow(non_snake_case)]

nym-vpn-lib/src/routing.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ use crate::NymVpn;
2626

2727
const DEFAULT_TUN_MTU: u16 = 1500;
2828

29+
#[derive(Clone)]
2930
pub struct RoutingConfig {
3031
pub(crate) mixnet_tun_config: tun2::Configuration,
31-
// In case we need it, as it's not read-accessible in the tun2 config
32+
// In case we need them, as they're not read-accessible in the tun2 config
3233
pub(crate) tun_ips: IpPair,
34+
pub(crate) mtu: u16,
35+
3336
pub(crate) entry_mixnet_gateway_ip: IpAddr,
3437
pub(crate) lan_gateway_ip: LanGatewayIp,
3538
pub(crate) tunnel_gateway_ip: TunnelGatewayIp,
@@ -45,9 +48,10 @@ impl Display for RoutingConfig {
4548
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
4649
write!(
4750
f,
48-
"mixnet_tun_config: {:?}\ntun_ips: {:?}\nentry_mixnet_gateway_ip: {:?}\nlan_gateway_ip: {:?}\ntunnel_gateway_ip: {:?}\nenable_wireguard: {:?}\ndisable_routing: {:?}",
51+
"mixnet_tun_config: {:?}\ntun_ips: {:?}\nmtu: {}\nentry_mixnet_gateway_ip: {:?}\nlan_gateway_ip: {:?}\ntunnel_gateway_ip: {:?}\nenable_wireguard: {:?}\ndisable_routing: {:?}",
4952
self.mixnet_tun_config,
5053
self.tun_ips,
54+
self.mtu,
5155
self.entry_mixnet_gateway_ip,
5256
self.lan_gateway_ip,
5357
self.tunnel_gateway_ip,
@@ -68,14 +72,16 @@ impl RoutingConfig {
6872
) -> Self {
6973
debug!("TUN device IPs: {}", tun_ips);
7074
let mut mixnet_tun_config = tun2::Configuration::default();
75+
let mtu = vpn.nym_mtu.unwrap_or(DEFAULT_TUN_MTU);
7176
// only IPv4 is supported by tun2 for now
7277
mixnet_tun_config.address(tun_ips.ipv4);
73-
mixnet_tun_config.mtu(vpn.nym_mtu.unwrap_or(DEFAULT_TUN_MTU));
78+
mixnet_tun_config.mtu(mtu);
7479
mixnet_tun_config.up();
7580

7681
Self {
7782
mixnet_tun_config,
7883
tun_ips,
84+
mtu,
7985
entry_mixnet_gateway_ip,
8086
lan_gateway_ip,
8187
tunnel_gateway_ip,
@@ -91,9 +97,21 @@ impl RoutingConfig {
9197
pub fn tun_ips(&self) -> IpPair {
9298
self.tun_ips
9399
}
100+
101+
pub fn mtu(&self) -> u16 {
102+
self.mtu
103+
}
104+
105+
pub fn entry_mixnet_gateway_ip(&self) -> IpAddr {
106+
self.entry_mixnet_gateway_ip
107+
}
108+
109+
pub fn enable_wireguard(&self) -> bool {
110+
self.enable_wireguard
111+
}
94112
}
95113

96-
#[derive(Debug)]
114+
#[derive(Clone, Debug)]
97115
pub struct TunnelGatewayIp {
98116
pub ipv4: Ipv4Addr,
99117
pub ipv6: Option<Ipv6Addr>,
@@ -123,7 +141,7 @@ impl std::fmt::Display for TunnelGatewayIp {
123141
}
124142
}
125143

126-
#[derive(Debug)]
144+
#[derive(Clone, Debug)]
127145
pub struct LanGatewayIp(pub Interface);
128146

129147
impl LanGatewayIp {
@@ -189,6 +207,9 @@ fn replace_default_prefixes(network: IpNetwork) -> Vec<IpNetwork> {
189207
pub async fn setup_routing(
190208
route_manager: &mut RouteManager,
191209
config: RoutingConfig,
210+
#[cfg(target_os = "ios")] ios_tun_provider: std::sync::Arc<
211+
dyn crate::platform::ios::OSTunProvider,
212+
>,
192213
) -> Result<tun2::AsyncDevice> {
193214
debug!("Creating tun device");
194215
let mixnet_tun_config = config.mixnet_tun_config.clone();
@@ -215,6 +236,13 @@ pub async fn setup_routing(
215236
mixnet_tun_config.raw_fd(fd);
216237
mixnet_tun_config
217238
};
239+
#[cfg(target_os = "ios")]
240+
let mixnet_tun_config = {
241+
let fd = ios_tun_provider.configure_nym(config.clone().into())?;
242+
let mut mixnet_tun_config = mixnet_tun_config.clone();
243+
mixnet_tun_config.raw_fd(fd);
244+
mixnet_tun_config
245+
};
218246
let dev = tun2::create_as_async(&mixnet_tun_config)
219247
.tap_err(|err| error!("Failed to create tun device: {}", err))?;
220248
let device_name = dev.as_ref().tun_name().unwrap().to_string();

0 commit comments

Comments
 (0)