Skip to content

Commit

Permalink
fixup: recreateTun -> getTunForced
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Aug 12, 2024
1 parent ff8b2d3 commit 04a69c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,19 @@ open class TalpidVpnService : LifecycleVpnService() {
if (config == currentTunConfig && tunStatus != null && tunStatus.isOpen) {
return tunStatus
} else {
val newTunStatus = createTun(config)

currentTunConfig = config
activeTunStatus = newTunStatus

return newTunStatus
return getTunForced(config)
}
}
}

fun recreateTun() {
fun getTunForced(config: TunConfig): CreateTunResult {
synchronized(this) {
currentTunConfig?.let { config ->
activeTunStatus = createTun(config)
};
val newTunStatus = createTun(config)

currentTunConfig = config
activeTunStatus = newTunStatus

return newTunStatus
}
}

Expand Down
2 changes: 1 addition & 1 deletion talpid-core/src/tunnel_state_machine/connecting_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl ConnectingState {
shared_values.prepare_tun_config(false);
if retry_attempt > 0 && retry_attempt % MAX_ATTEMPTS_WITH_SAME_TUN == 0 {
if let Err(error) =
{ shared_values.tun_provider.lock().unwrap().recreate_tun() }
{ shared_values.tun_provider.lock().unwrap().get_tun_forced() }
{
log::error!(
"{}",
Expand Down
42 changes: 15 additions & 27 deletions talpid-tunnel/src/tun_provider/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,18 @@ impl AndroidTunProvider {

/// Retrieve a tunnel device with the provided configuration.
pub fn get_tun(&mut self) -> Result<VpnServiceTun, Error> {
self.get_tun_inner()
self.get_tun_inner("getTun")
}

/// Retrieve a tunnel device with the provided configuration.
fn get_tun_inner(&mut self) -> Result<VpnServiceTun, Error> {
let tun_fd = self.get_tun_fd()?;
/// Force recreation even if the tunnel config hasn't changed.
pub fn get_tun_forced(&mut self) -> Result<VpnServiceTun, Error> {
self.get_tun_inner("getTunForced")
}

/// Retrieve a tunnel device with the provided configuration.
fn get_tun_inner(&mut self, get_tun_func_name: &'static str) -> Result<VpnServiceTun, Error> {
let tun_fd = self.get_tun_fd(get_tun_func_name)?;

let jvm = unsafe { JavaVM::from_raw(self.jvm.get_java_vm_pointer()) }
.map_err(Error::CloneJavaVm)?;
Expand All @@ -103,43 +109,25 @@ impl AndroidTunProvider {
})
}

/// Open a tunnel device using the current configuration.
///
/// Will open a new tunnel if there is already an active tunnel. The previous tunnel will be
/// closed.
pub fn recreate_tun(&mut self) -> Result<(), Error> {
let result = self.call_method(
"recreateTun",
"()V",
JavaType::Primitive(Primitive::Void),
&[],
)?;

match result {
JValue::Void => Ok(()),
value => Err(Error::InvalidMethodResult(
"recreateTun",
format!("{:?}", value),
)),
}
}

fn get_tun_fd(&self) -> Result<RawFd, Error> {
fn get_tun_fd(&self, get_tun_func_name: &'static str) -> Result<RawFd, Error> {
let config = VpnServiceConfig::new(self.config.clone());

let env = self.env()?;
let java_config = config.into_java(&env);

let result = self.call_method(
"getTun",
get_tun_func_name,
"(Lnet/mullvad/talpid/model/TunConfig;)Lnet/mullvad/talpid/model/CreateTunResult;",
JavaType::Object("net/mullvad/talpid/model/CreateTunResult".to_owned()),
&[JValue::Object(java_config.as_obj())],
)?;

match result {
JValue::Object(result) => CreateTunResult::from_java(&env, result).into(),
value => Err(Error::InvalidMethodResult("getTun", format!("{:?}", value))),
value => Err(Error::InvalidMethodResult(
get_tun_func_name,
format!("{:?}", value),
)),
}
}

Expand Down

0 comments on commit 04a69c2

Please sign in to comment.