aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--WireGuardNetworkExtension/PacketTunnelProvider.swift1
-rw-r--r--WireGuardNetworkExtension/WireGuardGoWrapper.h2
-rw-r--r--WireGuardNetworkExtension/WireGuardGoWrapper.m64
3 files changed, 52 insertions, 15 deletions
diff --git a/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuardNetworkExtension/PacketTunnelProvider.swift
index b411355..ccbf120 100644
--- a/WireGuardNetworkExtension/PacketTunnelProvider.swift
+++ b/WireGuardNetworkExtension/PacketTunnelProvider.swift
@@ -60,6 +60,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
setTunnelNetworkSettings(newSettings) { [weak self](error) in
completionHandler(error)
self?.wireGuardWrapper.configured = true
+ self?.wireGuardWrapper.startReadingPackets()
}
} else {
diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.h b/WireGuardNetworkExtension/WireGuardGoWrapper.h
index 4c89bb6..21ee8b4 100644
--- a/WireGuardNetworkExtension/WireGuardGoWrapper.h
+++ b/WireGuardNetworkExtension/WireGuardGoWrapper.h
@@ -17,6 +17,8 @@
- (BOOL) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString;
- (void) turnOff;
+- (void) startReadingPackets;
+
+ (NSString *)versionWireGuardGo;
@end
diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.m b/WireGuardNetworkExtension/WireGuardGoWrapper.m
index 2f5a558..7afe255 100644
--- a/WireGuardNetworkExtension/WireGuardGoWrapper.m
+++ b/WireGuardNetworkExtension/WireGuardGoWrapper.m
@@ -26,6 +26,7 @@ static void do_log(int level, const char *tag, const char *msg);
@property (nonatomic, assign) BOOL isClosed;
@property (nonatomic, strong) NSMutableArray<NSData *> *packets;
@property (nonatomic, strong) NSMutableArray<NSNumber *> *protocols;
+@property (nonatomic, strong) dispatch_queue_t dispatchQueue;
@property (nonatomic, strong) NSCondition *condition;
@@ -37,9 +38,11 @@ static void do_log(int level, const char *tag, const char *msg);
{
self = [super init];
if (self) {
+ self.packets = [[NSMutableArray alloc]initWithCapacity:100];
self.handle = -1;
self.configured = false;
self.condition = [NSCondition new];
+ self.dispatchQueue = dispatch_queue_create("manager", NULL);
}
return self;
}
@@ -66,6 +69,31 @@ static void do_log(int level, const char *tag, const char *msg);
self.handle = -1;
}
+- (void) startReadingPackets {
+ [self readPackets];
+}
+
+- (void) readPackets {
+ dispatch_async(self.dispatchQueue, ^{
+ if (self.isClosed || self.handle < 0 || !self.configured ) {
+ [self readPackets];
+ return;
+ }
+
+ 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) {
+ @synchronized(self.packets) {
+ [self.packets addObjectsFromArray:packets];
+ [self.protocols addObjectsFromArray:protocols];
+ }
+ os_log_debug([WireGuardGoWrapper log], "readPackets - signal - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
+ [self.condition signal];
+ [self readPackets];
+ }];
+ });
+}
+
+ (NSString *)versionWireGuardGo {
return [NSString stringWithUTF8String:wgVersion()];
}
@@ -94,24 +122,30 @@ static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
return 0;
}
- if (!wrapper.packets.count) {
- [wrapper.packetFlow readPacketsWithCompletionHandler:^(NSArray<NSData *> * _Nonnull packets, NSArray<NSNumber *> * _Nonnull protocols) {
- [wrapper.condition signal];
- [wrapper.packets addObjectsFromArray:packets];
- [wrapper.protocols addObjectsFromArray:protocols];
- os_log_debug([WireGuardGoWrapper log], "do_read - signal - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
- [wrapper.condition signal];
- }];
+
+ NSData * __block packet = nil;
+// NSNumber *protocol = nil;
+ dispatch_sync(wrapper.dispatchQueue, ^{
+ @synchronized(wrapper.packets) {
+ if (wrapper.packets.count == 0) {
+ os_log_debug([WireGuardGoWrapper log], "do_read - no packet - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
+
+ return;
+ }
+
+ packet = [wrapper.packets objectAtIndex:0];
+ // protocol = [wrapper.protocols objectAtIndex:0];
+ [wrapper.packets removeObjectAtIndex:0];
+ [wrapper.protocols removeObjectAtIndex:0];
+ }
+ });
+
+ if (packet == nil) {
os_log_debug([WireGuardGoWrapper log], "do_read - wait - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread);
- while (!wrapper.packets.count)
- [wrapper.condition wait];
+ [wrapper.condition wait];
+ return 0;
}
- NSData *packet = [wrapper.packets objectAtIndex:0];
-// NSNumber *protocol = [wrapper.protocols objectAtIndex:0];
- [wrapper.packets removeObjectAtIndex:0];
- [wrapper.protocols removeObjectAtIndex:0];
-
NSUInteger packetLength = [packet length];
if (packetLength > len) {
// The packet will be dropped when we end up here.