@@ -570,12 +570,10 @@ pub fn all_of_the_internet() -> Vec<ipnetwork::IpNetwork> {
570
570
#[ cfg_attr( target_os = "android" , derive( FromJava ) ) ]
571
571
#[ cfg_attr( target_os = "android" , jnix( package = "net.mullvad.talpid.model" ) ) ]
572
572
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 ,
578
575
} ,
576
+ Offline ,
579
577
/// On/offline status could not be verified, but we have no particular
580
578
/// reason to believe that the host is offline.
581
579
PresumeOnline ,
@@ -590,29 +588,64 @@ impl Connectivity {
590
588
/// If no IP4 nor IPv6 routes exist, we have no way of reaching the internet
591
589
/// so we consider ourselves offline.
592
590
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 )
600
592
}
601
593
602
594
/// Whether IPv4 connectivity seems to be available on the host.
603
595
///
604
596
/// If IPv4 status is unknown, `true` is returned.
605
597
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
+ }
610
603
}
611
604
612
605
/// Whether IPv6 connectivity seems to be available on the host.
613
606
///
614
607
/// If IPv6 status is unknown, `false` is returned.
615
608
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
617
650
}
618
651
}
0 commit comments