Răsfoiți Sursa

Fix: lookup active interface as needed

mirokuratczyk 3 ani în urmă
părinte
comite
16941809c4

+ 4 - 2
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/Network/NetworkID.h

@@ -29,8 +29,10 @@ NS_ASSUME_NONNULL_BEGIN
 ///
 /// See network ID requirements here:
 /// https://godoc.org/github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon#NetworkIDGetter
-/// @param reachability ReachabilityProtocol implementer used to determine network ID on iOS >=12.
-/// @param currentNetworkStatus Used to determine network ID on iOS <12.
+/// @param reachability ReachabilityProtocol implementer used to determine active interface on iOS >=12 when
+/// currentNetworkStatus is NetworkReachabilityReachableViaWired.
+/// @param currentNetworkStatus Used to determine network ID and, on iOS <12, to determine the active interface when
+/// currentNetworkStatus is NetworkReachabilityReachableViaWired.
 /// @param outWarn If non-nil, then a non-fatal error occurred while determining the network ID and a valid network ID will still be returned.
 + (NSString *)getNetworkIDWithReachability:(id<ReachabilityProtocol>)reachability
                    andCurrentNetworkStatus:(NetworkReachability)currentNetworkStatus

+ 26 - 49
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/Network/NetworkID.m

@@ -23,21 +23,17 @@
 #import <CoreTelephony/CTCarrier.h>
 #import <SystemConfiguration/CaptiveNetwork.h>
 
-NSString *kNetworkIDUnknown = @"UNKNOWN";
-
 @implementation NetworkID
 
-/// Internal helper function. See comment in header for `getNetworkIDWithReachability:andCurrentNetworkStatus:warning:`.
-/// @param networkReachability Network reachability status.
-/// @param defaultActiveInterfaceName Interface associated with the default route on the device.
-/// @param outWarn If non-nil, then a non-fatal error occurred while determining the network ID and a valid network ID will still be returned.
-+ (NSString * _Nonnull)getNetworkID:(NetworkReachability)networkReachability
-         defaultActiveInterfaceName:(NSString*)defaultActiveInterfaceName
-                            warning:(NSError *_Nullable *_Nonnull)outWarn {
+// See comment in header.
++ (NSString *)getNetworkIDWithReachability:(id<ReachabilityProtocol>)reachability
+                   andCurrentNetworkStatus:(NetworkReachability)currentNetworkStatus
+                                   warning:(NSError *_Nullable *_Nonnull)outWarn {
+
     *outWarn = nil;
 
-    NSMutableString *networkID = [NSMutableString stringWithString:kNetworkIDUnknown];
-    if (networkReachability == NetworkReachabilityReachableViaWiFi) {
+    NSMutableString *networkID = [NSMutableString stringWithString:@"UNKNOWN"];
+    if (currentNetworkStatus == NetworkReachabilityReachableViaWiFi) {
         [networkID setString:@"WIFI"];
         NSArray *networkInterfaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();
         for (NSString *networkInterfaceName in networkInterfaceNames) {
@@ -46,7 +42,7 @@ NSString *kNetworkIDUnknown = @"UNKNOWN";
                 [networkID appendFormat:@"-%@", networkInterfaceInfo[(__bridge NSString*)kCNNetworkInfoKeyBSSID]];
             }
         }
-    } else if (networkReachability == NetworkReachabilityReachableViaCellular) {
+    } else if (currentNetworkStatus == NetworkReachabilityReachableViaCellular) {
         [networkID setString:@"MOBILE"];
         CTTelephonyNetworkInfo *telephonyNetworkinfo = [[CTTelephonyNetworkInfo alloc] init];
         CTCarrier *cellularProvider = [telephonyNetworkinfo subscriberCellularProvider];
@@ -55,11 +51,25 @@ NSString *kNetworkIDUnknown = @"UNKNOWN";
             NSString *mnc = [cellularProvider mobileNetworkCode];
             [networkID appendFormat:@"-%@-%@", mcc, mnc];
         }
-    } else if (networkReachability == NetworkReachabilityReachableViaWired) {
+    } else if (currentNetworkStatus == NetworkReachabilityReachableViaWired) {
         [networkID setString:@"WIRED"];
-        if (defaultActiveInterfaceName != NULL) {
+
+        NSError *err;
+        NSString *activeInterface =
+            [NetworkInterface getActiveInterfaceWithReachability:reachability
+                                         andCurrentNetworkStatus:currentNetworkStatus
+                                                           error:&err];
+        if (err != nil) {
+            NSString *localizedDescription = [NSString stringWithFormat:@"error getting active interface %@", err.localizedDescription];
+            *outWarn = [[NSError alloc] initWithDomain:@"iOSLibrary"
+                                                  code:1
+                                              userInfo:@{NSLocalizedDescriptionKey:localizedDescription}];
+            return networkID;
+        }
+
+        if (activeInterface != nil) {
             NSError *err;
-            NSString *interfaceAddress = [NetworkInterface getInterfaceAddress:defaultActiveInterfaceName
+            NSString *interfaceAddress = [NetworkInterface getInterfaceAddress:activeInterface
                                                                          error:&err];
             if (err != nil) {
                 NSString *localizedDescription =
@@ -70,43 +80,10 @@ NSString *kNetworkIDUnknown = @"UNKNOWN";
                 [networkID appendFormat:@"-%@", interfaceAddress];
             }
         }
-    } else if (networkReachability == NetworkReachabilityReachableViaLoopback) {
+    } else if (currentNetworkStatus == NetworkReachabilityReachableViaLoopback) {
         [networkID setString:@"LOOPBACK"];
     }
     return networkID;
 }
 
-// See comment in header.
-+ (NSString *)getNetworkIDWithReachability:(id<ReachabilityProtocol>)reachability
-                   andCurrentNetworkStatus:(NetworkReachability)currentNetworkStatus
-                                   warning:(NSError *_Nullable *_Nonnull)outWarn {
-    *outWarn = nil;
-
-    NSError *err;
-    NSString *activeInterface =
-        [NetworkInterface getActiveInterfaceWithReachability:reachability
-                                     andCurrentNetworkStatus:currentNetworkStatus
-                                                       error:&err];
-    if (err != nil) {
-        NSString *localizedDescription = [NSString stringWithFormat:@"error getting active interface %@", err.localizedDescription];
-        *outWarn = [[NSError alloc] initWithDomain:@"iOSLibrary"
-                                              code:1
-                                          userInfo:@{NSLocalizedDescriptionKey:localizedDescription}];
-        return kNetworkIDUnknown;
-    }
-
-    NSError *warn;
-    NSString *networkID = [NetworkID getNetworkID:currentNetworkStatus
-                       defaultActiveInterfaceName:activeInterface
-                                          warning:&warn];
-    if (warn != nil) {
-        NSString *localizedDescription = [NSString stringWithFormat:@"error getting network ID: %@", warn.localizedDescription];
-        *outWarn = [[NSError alloc] initWithDomain:@"iOSLibrary"
-                                              code:1
-                                          userInfo:@{NSLocalizedDescriptionKey:localizedDescription}];
-    }
-
-    return networkID;
-}
-
 @end

+ 1 - 1
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/Network/NetworkInterface.m

@@ -80,7 +80,7 @@
                                               (socklen_t)interface->ifa_addr->sa_len,
                                               addr,
                                               (socklen_t)NI_MAXHOST,
-                                              nil,
+                                              NULL,
                                               (socklen_t)0,
                                               NI_NUMERICHOST);
                         if (ret != 0) {