Skip to content

Commit 1c6696e

Browse files
committed
Fixes based on feedback
1 parent 9cf92d0 commit 1c6696e

File tree

7 files changed

+38
-45
lines changed

7 files changed

+38
-45
lines changed

android/lib/talpid/src/main/kotlin/net/mullvad/talpid/ConnectivityListener.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import kotlinx.coroutines.flow.onEach
2121
import kotlinx.coroutines.flow.scan
2222
import kotlinx.coroutines.flow.stateIn
2323
import kotlinx.coroutines.plus
24-
import net.mullvad.talpid.model.ConnectionStatus
24+
import net.mullvad.talpid.model.Connectivity
2525
import net.mullvad.talpid.util.IPAvailabilityUtils
2626
import net.mullvad.talpid.util.NetworkEvent
2727
import net.mullvad.talpid.util.defaultNetworkFlow
@@ -31,7 +31,7 @@ class ConnectivityListener(
3131
val connectivityManager: ConnectivityManager,
3232
val protect: (socket: DatagramSocket) -> Unit,
3333
) {
34-
private lateinit var _isConnected: StateFlow<ConnectionStatus>
34+
private lateinit var _isConnected: StateFlow<Connectivity>
3535
// Used by JNI
3636
val isConnected
3737
get() = _isConnected.value
@@ -50,23 +50,23 @@ class ConnectivityListener(
5050
linkPropertiesChanged: NetworkEvent,
5151
hasInternetCapability: Boolean ->
5252
if (hasInternetCapability) {
53-
ConnectionStatus(
53+
Connectivity.Status(
5454
IPAvailabilityUtils.isIPv4Available(protect = { protect(it) }),
5555
IPAvailabilityUtils.isIPv6Available(protect = { protect(it) }),
5656
)
5757
// If we have internet, but both IPv4 and IPv6 are not available, we
5858
// assume something is wrong and instead
5959
// will return both as available since this is the previous behavior.
60-
.takeUnless { !it.ipv4 && !it.ipv6 } ?: ConnectionStatus(true, true)
60+
.takeUnless { !it.ipv4 && !it.ipv6 } ?: Connectivity.Status(true, true)
6161
} else {
62-
ConnectionStatus(false, false)
62+
Connectivity.Status(false, false)
6363
}
6464
}
6565
.onEach { notifyConnectivityChange(it.ipv4, it.ipv6) }
6666
.stateIn(
6767
scope + Dispatchers.IO,
6868
SharingStarted.Eagerly,
69-
ConnectionStatus(false, false),
69+
Connectivity.Status(false, false),
7070
)
7171
}
7272

android/lib/talpid/src/main/kotlin/net/mullvad/talpid/model/ConnectionStatus.kt

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.mullvad.talpid.model
2+
3+
sealed class Connectivity {
4+
data class Status(val ipv4: Boolean, val ipv6: Boolean) : Connectivity()
5+
6+
data object PresumeOnline : Connectivity()
7+
}

android/lib/talpid/src/main/kotlin/net/mullvad/talpid/util/IPAvailabilityUtils.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ object IPAvailabilityUtils {
1414
isIPAvailable(InetAddress.getByName(PUBLIC_IPV6_ADDRESS), protect)
1515

1616
// Fake a connection to a public ip address using a UDP socket.
17+
// We don't care about the result of the connection, only that it is possible to create.
18+
// This is done this way since otherwise there is not way to check the availability of an ip
19+
// version on the underlying network if the VPN is turned on.
1720
// Since we are protecting the socket it will use the underlying network regardless
1821
// if the VPN is turned on or not.
1922
// If the ip version is not supported on the underlying network it will trigger a socket
20-
// exception. If not it should return the local ip address.
23+
// exception. Otherwise we assume it is available.
2124
private inline fun <reified T : InetAddress> isIPAvailable(
2225
ip: T,
2326
protect: (socket: DatagramSocket) -> Unit,
@@ -26,7 +29,7 @@ object IPAvailabilityUtils {
2629
return try {
2730
protect(socket)
2831
socket.connect(InetSocketAddress(ip, 1))
29-
socket.localAddress.hostAddress?.isNotEmpty() == true && socket.localAddress is T
32+
true
3033
} catch (_: SocketException) {
3134
Logger.e("Socket could not be set up")
3235
false

mullvad-jni/src/classes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ pub const CLASSES: &[&str] = &[
1616
"net/mullvad/talpid/ConnectivityListener",
1717
"net/mullvad/talpid/TalpidVpnService",
1818
"net/mullvad/mullvadvpn/lib/endpoint/ApiEndpointOverride",
19-
"net/mullvad/talpid/model/ConnectionStatus",
19+
"net/mullvad/talpid/model/Connectivity$Status",
20+
"net/mullvad/talpid/model/Connectivity$PresumeOnline",
2021
];

talpid-core/src/connectivity_listener.rs

+15-29
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl ConnectivityListener {
8282
};
8383

8484
Ok(ConnectivityListener {
85-
jvm: android_context.clone().jvm,
85+
jvm: android_context.jvm,
8686
android_listener,
8787
})
8888
}
@@ -98,18 +98,16 @@ impl ConnectivityListener {
9898

9999
/// Return the current offline/connectivity state
100100
pub fn connectivity(&self) -> Connectivity {
101-
self.get_is_connected()
102-
.map(|(ipv4, ipv6)| Connectivity::Status { ipv4, ipv6 })
103-
.unwrap_or_else(|error| {
104-
log::error!(
105-
"{}",
106-
error.display_chain_with_msg("Failed to check connectivity status")
107-
);
108-
Connectivity::PresumeOnline
109-
})
101+
self.get_is_connected().unwrap_or_else(|error| {
102+
log::error!(
103+
"{}",
104+
error.display_chain_with_msg("Failed to check connectivity status")
105+
);
106+
Connectivity::PresumeOnline
107+
})
110108
}
111109

112-
fn get_is_connected(&self) -> Result<(bool, bool), Error> {
110+
fn get_is_connected(&self) -> Result<Connectivity, Error> {
113111
let env = JnixEnv::from(
114112
self.jvm
115113
.attach_current_thread_as_daemon()
@@ -120,26 +118,14 @@ impl ConnectivityListener {
120118
.call_method(
121119
self.android_listener.as_obj(),
122120
"isConnected",
123-
"()Lnet/mullvad/talpid/model/ConnectionStatus;",
121+
"()Lnet/mullvad/talpid/model/Connectivity;",
124122
&[],
125123
)
126124
.expect("Missing isConnected")
127125
.l()
128126
.expect("isConnected is not an object");
129127

130-
let ipv4 = env
131-
.call_method(is_connected, "component1", "()Z", &[])
132-
.expect("Missing ConnectionStatus.ipv4")
133-
.z()
134-
.expect("ipv4 is not a boolean");
135-
136-
let ipv6 = env
137-
.call_method(is_connected, "component2", "()Z", &[])
138-
.expect("Missing ConnectionStatus.ipv6")
139-
.z()
140-
.expect("ipv6 is not a boolean");
141-
142-
Ok((ipv4, ipv6))
128+
Ok(Connectivity::from_java(&env, is_connected))
143129
}
144130

145131
/// Return the current DNS servers according to Android
@@ -183,13 +169,13 @@ pub extern "system" fn Java_net_mullvad_talpid_ConnectivityListener_notifyConnec
183169
return;
184170
};
185171

186-
let isIPv4 = JNI_TRUE == is_ipv4;
187-
let isIPv6 = JNI_TRUE == is_ipv6;
172+
let is_ipv4 = JNI_TRUE == is_ipv4;
173+
let is_ipv6 = JNI_TRUE == is_ipv6;
188174

189175
if tx
190176
.unbounded_send(Connectivity::Status {
191-
ipv4: isIPv4,
192-
ipv6: isIPv6,
177+
ipv4: is_ipv4,
178+
ipv6: is_ipv6,
193179
})
194180
.is_err()
195181
{

talpid-types/src/net/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
2+
use jnix::FromJava;
23
use obfuscation::ObfuscatorConfig;
34
use serde::{Deserialize, Serialize};
45
#[cfg(windows)]
@@ -564,7 +565,8 @@ pub fn all_of_the_internet() -> Vec<ipnetwork::IpNetwork> {
564565
///
565566
/// Information about the host's connectivity, such as the preesence of
566567
/// configured IPv4 and/or IPv6.
567-
#[derive(Debug, Clone, Copy, PartialEq)]
568+
#[derive(Debug, Clone, Copy, PartialEq, FromJava)]
569+
#[jnix(package = "net.mullvad.talpid.model")]
568570
pub enum Connectivity {
569571
Status {
570572
/// Whether IPv4 connectivity seems to be available on the host.

0 commit comments

Comments
 (0)