Make sure the reference is not nil to packet flow. Thanks go out to "The Eskimo".

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jeroen Leenarts 2018-08-14 21:40:20 +02:00
parent fc72697d82
commit 335907309c
3 changed files with 11 additions and 3 deletions

View File

@ -41,7 +41,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
//TODO: Hardcoded values for addresses //TODO: Hardcoded values for addresses
let ipv4Settings = NEIPv4Settings(addresses: ["10.50.10.171"], subnetMasks: ["255.255.224.0"]) let ipv4Settings = NEIPv4Settings(addresses: ["10.50.10.171"], subnetMasks: ["255.255.224.0"])
//TODO: Hardcoded values for allowed ips //TODO: Hardcoded values for allowed ips
ipv4Settings.includedRoutes = [NEIPv4Route(destinationAddress: "0.0.0.0", subnetMask: "0.0.0.0")] ipv4Settings.includedRoutes = [NEIPv4Route.default()]
ipv4Settings.excludedRoutes = endpoints.split(separator: ",").compactMap { $0.split(separator: ":").first}.map {NEIPv4Route(destinationAddress: String($0), subnetMask: "255.255.255.255")} ipv4Settings.excludedRoutes = endpoints.split(separator: ",").compactMap { $0.split(separator: ":").first}.map {NEIPv4Route(destinationAddress: String($0), subnetMask: "255.255.255.255")}
//TODO IPv6 settings //TODO IPv6 settings
@ -58,12 +58,14 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
} }
setTunnelNetworkSettings(newSettings) { [weak self](error) in setTunnelNetworkSettings(newSettings) { [weak self](error) in
completionHandler(error) self?.wireGuardWrapper.packetFlow = self?.packetFlow
self?.wireGuardWrapper.configured = true self?.wireGuardWrapper.configured = true
self?.wireGuardWrapper.startReadingPackets() self?.wireGuardWrapper.startReadingPackets()
completionHandler(error)
} }
} else { } else {
self.wireGuardWrapper.packetFlow = self.packetFlow
completionHandler(PacketTunnelProviderError.tunnelSetupFailed) completionHandler(PacketTunnelProviderError.tunnelSetupFailed)
wireGuardWrapper.configured = false wireGuardWrapper.configured = false
} }

View File

@ -11,7 +11,7 @@
@interface WireGuardGoWrapper : NSObject @interface WireGuardGoWrapper : NSObject
@property (nonatomic, weak) NEPacketTunnelFlow *packetFlow; @property (nonatomic, strong) NEPacketTunnelFlow *packetFlow;
@property (nonatomic, assign) BOOL configured; @property (nonatomic, assign) BOOL configured;
- (BOOL) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString; - (BOOL) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString;

View File

@ -83,12 +83,14 @@ static void do_log(int level, const char *tag, const char *msg);
os_log_debug([WireGuardGoWrapper log], "readPackets - read call - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); os_log_debug([WireGuardGoWrapper log], "readPackets - read call - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
[self.packetFlow readPacketsWithCompletionHandler:^(NSArray<NSData *> * _Nonnull packets, NSArray<NSNumber *> * _Nonnull protocols) { [self.packetFlow readPacketsWithCompletionHandler:^(NSArray<NSData *> * _Nonnull packets, NSArray<NSNumber *> * _Nonnull protocols) {
[self.condition lock];
@synchronized(self.packets) { @synchronized(self.packets) {
[self.packets addObjectsFromArray:packets]; [self.packets addObjectsFromArray:packets];
[self.protocols addObjectsFromArray:protocols]; [self.protocols addObjectsFromArray:protocols];
} }
os_log_debug([WireGuardGoWrapper log], "readPackets - signal - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); os_log_debug([WireGuardGoWrapper log], "readPackets - signal - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
[self.condition signal]; [self.condition signal];
[self.condition unlock];
[self readPackets]; [self readPackets];
}]; }];
}); });
@ -126,6 +128,7 @@ static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
NSData * __block packet = nil; NSData * __block packet = nil;
// NSNumber *protocol = nil; // NSNumber *protocol = nil;
dispatch_sync(wrapper.dispatchQueue, ^{ dispatch_sync(wrapper.dispatchQueue, ^{
[wrapper.condition lock];
@synchronized(wrapper.packets) { @synchronized(wrapper.packets) {
if (wrapper.packets.count == 0) { if (wrapper.packets.count == 0) {
os_log_debug([WireGuardGoWrapper log], "do_read - no packet - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); os_log_debug([WireGuardGoWrapper log], "do_read - no packet - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
@ -143,7 +146,10 @@ static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
if (packet == nil) { if (packet == nil) {
os_log_debug([WireGuardGoWrapper log], "do_read - wait - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); os_log_debug([WireGuardGoWrapper log], "do_read - wait - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
[wrapper.condition wait]; [wrapper.condition wait];
[wrapper.condition unlock];
return 0; return 0;
} else {
[wrapper.condition unlock];
} }
NSUInteger packetLength = [packet length]; NSUInteger packetLength = [packet length];