Skip to content

Commit 64622f4

Browse files
committed
Fixes
1 parent 3a67a6c commit 64622f4

File tree

11 files changed

+110
-40
lines changed

11 files changed

+110
-40
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ConnectivityListener(
6666
_isConnected =
6767
connectivityManager
6868
.hasInternetConnectivity(resolver)
69-
.onEach { notifyConnectivityChange(it.ipv4, it.ipv6) }
69+
.onEach { notifyConnectivityChange(it.hasIpV4(), it.hasIpV6()) }
7070
.stateIn(
7171
scope + Dispatchers.IO,
7272
SharingStarted.Eagerly,
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,44 @@
11
package net.mullvad.talpid.model
22

3+
import android.net.LinkAddress
4+
import java.net.Inet4Address
5+
import java.net.Inet6Address
6+
37
sealed class Connectivity {
4-
data class Status(val ipv4: Boolean, val ipv6: Boolean) : Connectivity()
8+
data class Online(val ipAvailability: IpAvailability) : Connectivity()
9+
10+
data object Offline : Connectivity()
511

612
// Required by jni
713
data object PresumeOnline : Connectivity()
14+
15+
fun hasIpV4() =
16+
when (this) {
17+
is Online ->
18+
ipAvailability == IpAvailability.Ipv4 || ipAvailability == IpAvailability.All
19+
else -> false
20+
}
21+
22+
fun hasIpV6() =
23+
when (this) {
24+
is Online ->
25+
ipAvailability == IpAvailability.Ipv6 || ipAvailability == IpAvailability.All
26+
else -> false
27+
}
28+
29+
companion object {
30+
fun fromIpAvailability(ipv4: Boolean, ipv6: Boolean) =
31+
when {
32+
ipv4 && ipv6 -> Online(IpAvailability.All)
33+
ipv4 -> Online(IpAvailability.Ipv4)
34+
ipv6 -> Online(IpAvailability.Ipv6)
35+
else -> Offline
36+
}
37+
38+
fun fromLinkAddresses(linkAddresses: List<LinkAddress>): Connectivity {
39+
val ipv4 = linkAddresses.any { it.address is Inet4Address }
40+
val ipv6 = linkAddresses.any { it.address is Inet6Address }
41+
return fromIpAvailability(ipv4, ipv6)
42+
}
43+
}
844
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.mullvad.talpid.model
2+
3+
enum class IpAvailability {
4+
Ipv4,
5+
Ipv6,
6+
All,
7+
}

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

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import android.net.LinkProperties
66
import android.net.Network
77
import android.net.NetworkCapabilities
88
import co.touchlab.kermit.Logger
9-
import java.net.Inet4Address
10-
import java.net.Inet6Address
119
import kotlin.time.Duration.Companion.milliseconds
1210
import kotlinx.coroutines.FlowPreview
1311
import kotlinx.coroutines.channels.awaitClose
@@ -137,7 +135,7 @@ internal fun ConnectivityManager.activeRawNetworkState(): RawNetworkState? =
137135
@OptIn(FlowPreview::class)
138136
fun ConnectivityManager.hasInternetConnectivity(
139137
resolver: UnderlyingConnectivityStatusResolver
140-
): Flow<Connectivity.Status> =
138+
): Flow<Connectivity> =
141139
this.defaultRawNetworkStateFlow()
142140
.debounce(CONNECTIVITY_DEBOUNCE)
143141
.map { resolveConnectivityStatus(it, resolver) }
@@ -146,7 +144,7 @@ fun ConnectivityManager.hasInternetConnectivity(
146144
internal fun resolveConnectivityStatus(
147145
currentRawNetworkState: RawNetworkState?,
148146
resolver: UnderlyingConnectivityStatusResolver,
149-
): Connectivity.Status =
147+
): Connectivity =
150148
if (currentRawNetworkState.isVpn()) {
151149
// If the default network is a VPN we need to use a socket to check
152150
// the underlying network
@@ -158,10 +156,7 @@ internal fun resolveConnectivityStatus(
158156
}
159157

160158
private fun RawNetworkState?.toConnectivityStatus() =
161-
Connectivity.Status(
162-
ipv4 = this?.linkProperties?.linkAddresses?.any { it.address is Inet4Address } == true,
163-
ipv6 = this?.linkProperties?.linkAddresses?.any { it.address is Inet6Address } == true,
164-
)
159+
Connectivity.fromLinkAddresses(this?.linkProperties?.linkAddresses ?: emptyList())
165160

166161
private fun RawNetworkState?.isVpn(): Boolean =
167162
this?.networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) == false

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import net.mullvad.talpid.model.Connectivity
1414
class UnderlyingConnectivityStatusResolver(
1515
private val protect: (socket: DatagramSocket) -> Boolean
1616
) {
17-
fun currentStatus(): Connectivity.Status =
18-
Connectivity.Status(ipv4 = hasIPv4(), ipv6 = hasIPv6())
17+
fun currentStatus(): Connectivity =
18+
Connectivity.fromIpAvailability(ipv4 = hasIPv4(), ipv6 = hasIPv6())
1919

2020
private fun hasIPv4(): Boolean =
2121
hasIpVersion(Inet4Address.getByName(PUBLIC_IPV4_ADDRESS), protect)

mullvad-jni/src/classes.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub const CLASSES: &[&str] = &[
1818
"net/mullvad/talpid/ConnectivityListener",
1919
"net/mullvad/talpid/TalpidVpnService",
2020
"net/mullvad/mullvadvpn/lib/endpoint/ApiEndpointOverride",
21-
"net/mullvad/talpid/model/Connectivity$Status",
21+
"net/mullvad/talpid/model/Connectivity$Online",
22+
"net/mullvad/talpid/model/Connectivity$Offline",
2223
"net/mullvad/talpid/model/Connectivity$PresumeOnline",
24+
"net/mullvad/talpid/model/IpAvailability",
2325
];

talpid-core/src/connectivity_listener.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,7 @@ pub extern "system" fn Java_net_mullvad_talpid_ConnectivityListener_notifyConnec
180180
let is_ipv6 = JNI_TRUE == is_ipv6;
181181

182182
if tx
183-
.unbounded_send(Connectivity::Status {
184-
ipv4: is_ipv4,
185-
ipv6: is_ipv6,
186-
})
183+
.unbounded_send(Connectivity::new(is_ipv4, is_ipv6))
187184
.is_err()
188185
{
189186
log::warn!("Failed to send offline change event");

talpid-core/src/offline/macos.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ struct ConnectivityInner {
5252

5353
impl ConnectivityInner {
5454
fn into_connectivity(self) -> Connectivity {
55-
Connectivity::Status {
56-
ipv4: self.ipv4,
57-
ipv6: self.ipv6,
58-
}
55+
Connectivity::new(self.ipv4, self.ipv6)
5956
}
6057

6158
fn is_online(&self) -> bool {

talpid-core/src/tunnel_state_machine/connecting_state.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use futures::{FutureExt, StreamExt};
99
use talpid_routing::RouteManagerHandle;
1010
use talpid_tunnel::tun_provider::TunProvider;
1111
use talpid_tunnel::{EventHook, TunnelArgs, TunnelEvent, TunnelMetadata};
12-
use talpid_types::net::{AllowedClients, AllowedEndpoint, AllowedTunnelTraffic, TunnelParameters};
12+
use talpid_types::net::{
13+
AllowedClients, AllowedEndpoint, AllowedTunnelTraffic, Connectivity, TunnelParameters,
14+
};
1315
use talpid_types::tunnel::{ErrorStateCause, FirewallPolicyError};
1416
use talpid_types::ErrorExt;
1517

@@ -73,7 +75,7 @@ impl ConnectingState {
7375
});
7476
}
7577

76-
if shared_values.connectivity.is_offline() {
78+
if shared_values.connectivity == Connectivity::Offline {
7779
// FIXME: Temporary: Nudge route manager to update the default interface
7880
#[cfg(target_os = "macos")]
7981
{

talpid-types/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[cfg(target_os = "android")]
22
pub mod android;
33
pub mod net;
4+
pub use net::IpAvailability;
45
pub mod tunnel;
56

67
#[cfg(target_os = "linux")]

talpid-types/src/net/mod.rs

+50-17
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,10 @@ pub fn all_of_the_internet() -> Vec<ipnetwork::IpNetwork> {
570570
#[cfg_attr(target_os = "android", derive(FromJava))]
571571
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.talpid.model"))]
572572
pub enum Connectivity {
573-
Status {
574-
/// Whether IPv4 connectivity seems to be available on the host.
575-
ipv4: bool,
576-
/// Whether IPv6 connectivity seems to be available on the host.
577-
ipv6: bool,
573+
Online {
574+
ip_availability: IpAvailability,
578575
},
576+
Offline,
579577
/// On/offline status could not be verified, but we have no particular
580578
/// reason to believe that the host is offline.
581579
PresumeOnline,
@@ -590,29 +588,64 @@ impl Connectivity {
590588
/// If no IP4 nor IPv6 routes exist, we have no way of reaching the internet
591589
/// so we consider ourselves offline.
592590
pub fn is_offline(&self) -> bool {
593-
matches!(
594-
self,
595-
Connectivity::Status {
596-
ipv4: false,
597-
ipv6: false
598-
}
599-
)
591+
matches!(self, Connectivity::Offline)
600592
}
601593

602594
/// Whether IPv4 connectivity seems to be available on the host.
603595
///
604596
/// If IPv4 status is unknown, `true` is returned.
605597
pub fn has_ipv4(&self) -> bool {
606-
matches!(
607-
self,
608-
Connectivity::Status { ipv4: true, .. } | Connectivity::PresumeOnline
609-
)
598+
match self {
599+
Connectivity::Offline => false,
600+
Connectivity::PresumeOnline => true,
601+
Connectivity::Online { ip_availability } => ip_availability.has_ipv4(),
602+
}
610603
}
611604

612605
/// Whether IPv6 connectivity seems to be available on the host.
613606
///
614607
/// If IPv6 status is unknown, `false` is returned.
615608
pub fn has_ipv6(&self) -> bool {
616-
matches!(self, Connectivity::Status { ipv6: true, .. })
609+
match self {
610+
Connectivity::Offline | Connectivity::PresumeOnline => false,
611+
Connectivity::Online { ip_availability } => ip_availability.has_ipv6(),
612+
}
613+
}
614+
615+
pub fn new(ipv4: bool, ipv6: bool) -> Connectivity {
616+
if ipv4 && ipv6 {
617+
Connectivity::Online {
618+
ip_availability: IpAvailability::All,
619+
}
620+
} else if ipv4 {
621+
Connectivity::Online {
622+
ip_availability: IpAvailability::Ipv4,
623+
}
624+
} else if ipv6 {
625+
Connectivity::Online {
626+
ip_availability: IpAvailability::Ipv6,
627+
}
628+
} else {
629+
Connectivity::Offline
630+
}
631+
}
632+
}
633+
634+
#[cfg_attr(target_os = "android", derive(FromJava))]
635+
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.talpid.model"))]
636+
#[derive(Clone, Debug, PartialEq, Eq, Copy)]
637+
pub enum IpAvailability {
638+
Ipv4,
639+
Ipv6,
640+
All,
641+
}
642+
643+
impl IpAvailability {
644+
pub fn has_ipv4(&self) -> bool {
645+
self.clone() == IpAvailability::Ipv4 || self.clone() == IpAvailability::All
646+
}
647+
648+
pub fn has_ipv6(&self) -> bool {
649+
self.clone() == IpAvailability::Ipv6 || self.clone() == IpAvailability::All
617650
}
618651
}

0 commit comments

Comments
 (0)