Browse Source

Fix: added mutex for initialDNSCache

Amir Khan 8 years ago
parent
commit
6a95a4edc4
1 changed files with 21 additions and 5 deletions
  1. 21 5
      MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m

+ 21 - 5
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m

@@ -62,6 +62,7 @@
 
 
     // This cache becomes void if internetReachabilityChanged is called.
     // This cache becomes void if internetReachabilityChanged is called.
     // Cache can be invalidated by setting it to nil.
     // Cache can be invalidated by setting it to nil.
+    NSLock *dnsCacheLock;
     NSArray<NSString *> *initialDNSCache;
     NSArray<NSString *> *initialDNSCache;
 }
 }
 
 
@@ -88,6 +89,7 @@
         self->secondaryGoogleDNS = GOOGLE_DNS_1;
         self->secondaryGoogleDNS = GOOGLE_DNS_1;
     }
     }
 
 
+    dnsCacheLock = [[NSLock alloc] init];
     self->initialDNSCache = [self getDNSServers];
     self->initialDNSCache = [self getDNSServers];
 
 
 
 
@@ -842,22 +844,34 @@
     // This function is only called when BindToDevice is used/supported.
     // This function is only called when BindToDevice is used/supported.
     // TODO: Implement correctly
     // TODO: Implement correctly
 
 
+    NSString *dns = nil;
+
+    [dnsCacheLock lock];
     if (self->initialDNSCache && [initialDNSCache count] > 0) {
     if (self->initialDNSCache && [initialDNSCache count] > 0) {
-        return self->initialDNSCache[0];
+        dns = self->initialDNSCache[0];
     } else {
     } else {
-        return self->primaryGoogleDNS;
+        dns = self->primaryGoogleDNS;
     }
     }
+    [dnsCacheLock unlock];
+
+    return dns;
 }
 }
 
 
 - (NSString *)getSecondaryDnsServer {
 - (NSString *)getSecondaryDnsServer {
     // This function is only called when BindToDevice is used/supported.
     // This function is only called when BindToDevice is used/supported.
     // TODO: Implement correctly
     // TODO: Implement correctly
 
 
-    if (self->initialDNSCache && [initialDNSCache count] > 1) {
-        return self->initialDNSCache[1];
+    NSString *dns = nil;
+
+    [dnsCacheLock lock];
+    if (self->initialDNSCache && [initialDNSCache count] > 0) {
+        dns = self->initialDNSCache[0];
     } else {
     } else {
-        return self->secondaryGoogleDNS;
+        dns = self->secondaryGoogleDNS;
     }
     }
+    [dnsCacheLock unlock];
+
+    return dns;
 }
 }
 
 
 - (long)hasNetworkConnectivity {
 - (long)hasNetworkConnectivity {
@@ -1051,7 +1065,9 @@
 
 
 - (void)internetReachabilityChanged:(NSNotification *)note {
 - (void)internetReachabilityChanged:(NSNotification *)note {
     // Invalidate initialDNSCache.
     // Invalidate initialDNSCache.
+    [dnsCacheLock lock];
     self->initialDNSCache = nil;
     self->initialDNSCache = nil;
+    [dnsCacheLock unlock];
 
 
     // If we lose network while connected, we're going to force a reconnect in
     // If we lose network while connected, we're going to force a reconnect in
     // order to trigger the waiting-for-network state. The reason we don't wait
     // order to trigger the waiting-for-network state. The reason we don't wait