From efdc474411f9304f0b85e62f753a0c76071cf6d3 Mon Sep 17 00:00:00 2001 From: Sergey Abramchuk Date: Thu, 9 Mar 2017 19:58:55 +0300 Subject: [PATCH] Fix possible memory leaks and decrease timeout for establishing tunnel --- OpenVPN Adapter/OpenVPNAdapter.mm | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/OpenVPN Adapter/OpenVPNAdapter.mm b/OpenVPN Adapter/OpenVPNAdapter.mm index a3fd300..90c1c23 100644 --- a/OpenVPN Adapter/OpenVPNAdapter.mm +++ b/OpenVPN Adapter/OpenVPNAdapter.mm @@ -204,9 +204,9 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData dispatch_semaphore_signal(sema); }]; - dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC); + dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC); if (dispatch_semaphore_wait(sema, timeout) != 0) { - NSLog(@"Tunnel configuration failed due to timeout"); + NSLog(@"Failed to establish tunnel in a reasonable time"); return -1; } @@ -337,8 +337,6 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData clientConfiguration.content = std::string([vpnConfiguration UTF8String]); clientConfiguration.connTimeout = 30; - self.vpnClient = new OpenVPNClient((__bridge void *)self); - ClientAPI::EvalConfig eval = self.vpnClient->eval_config(clientConfiguration); if (eval.error) { if (error) *error = [NSError errorWithDomain:OpenVPNAdapterErrorDomain code:OpenVPNErrorConfigurationFailure userInfo:@{ @@ -392,6 +390,16 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData OpenVPNClient::uninit_process(); self.tunConfiguration = nil; + + if (self.vpnSocket) { + CFSocketInvalidate(self.vpnSocket); + CFRelease(self.vpnSocket); + } + + if (self.tunSocket) { + CFSocketInvalidate(self.tunSocket); + CFRelease(self.tunSocket); + } }); } @@ -412,6 +420,8 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData _username = nil; _password = nil; _delegate = nil; + + self.vpnClient = new OpenVPNClient((__bridge void *)self); } return self; } @@ -464,16 +474,6 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData - (void)dealloc { delete self.vpnClient; - - if (self.vpnSocket) { - CFSocketInvalidate(self.vpnSocket); - CFRelease(self.vpnSocket); - } - - if (self.tunSocket) { - CFSocketInvalidate(self.tunSocket); - CFRelease(self.tunSocket); - } } @end