diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.m b/WireGuardNetworkExtension/WireGuardGoWrapper.m index c4235b8..7631365 100644 --- a/WireGuardNetworkExtension/WireGuardGoWrapper.m +++ b/WireGuardNetworkExtension/WireGuardGoWrapper.m @@ -114,68 +114,74 @@ static void do_log(int level, const char *tag, const char *msg); static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len) { -// os_log_debug([WireGuardGoWrapper log], "do_read - start - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); - WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx; - if (wrapper.isClosed) return -1; + @autoreleasepool { + os_log_debug([WireGuardGoWrapper log], "do_read - start - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); + WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx; + if (wrapper.isClosed) return -1; - if (wrapper.handle < 0 || !wrapper.configured ) { -// os_log_debug([WireGuardGoWrapper log], "do_read - early - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); + if (wrapper.handle < 0 || !wrapper.configured ) { + os_log_debug([WireGuardGoWrapper log], "do_read - early - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); - return 0; - } - - - NSData * __block packet = nil; -// NSNumber *protocol = nil; - dispatch_sync(wrapper.dispatchQueue, ^{ - [wrapper.condition lock]; - @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]; + return 0; } - }); - if (packet == nil) { - os_log_debug([WireGuardGoWrapper log], "do_read - wait - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); - [wrapper.condition wait]; - [wrapper.condition unlock]; - return 0; - } else { - [wrapper.condition unlock]; - } - NSUInteger packetLength = [packet length]; - if (packetLength > len) { - // The packet will be dropped when we end up here. - os_log_debug([WireGuardGoWrapper log], "do_read - drop - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); - return 0; + NSData * __block packet = nil; +// NSNumber *protocol = nil; + dispatch_sync(wrapper.dispatchQueue, ^{ + [wrapper.condition lock]; + @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); + [wrapper.condition wait]; + [wrapper.condition unlock]; + return 0; + } else { + [wrapper.condition unlock]; + } + + NSUInteger packetLength = [packet length]; + if (packetLength > len) { + // The packet will be dropped when we end up here. + os_log_debug([WireGuardGoWrapper log], "do_read - drop - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); + return 0; + } + memcpy(buf, [packet bytes], packetLength); + os_log_debug([WireGuardGoWrapper log], "do_read - packet - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); + return packetLength; } - memcpy(buf, [packet bytes], packetLength); - os_log_debug([WireGuardGoWrapper log], "do_read - packet - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); - return packetLength; } static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len) { - os_log_debug([WireGuardGoWrapper log], "do_write - start"); + @autoreleasepool { + os_log_debug([WireGuardGoWrapper log], "do_write - start"); - WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx; - //TODO: determine IPv4 or IPv6 status. - NSData *packet = [[NSData alloc] initWithBytes:buf length:len]; - [wrapper.packetFlow writePackets:@[packet] withProtocols:@[@AF_INET]]; - return len; + WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx; + //TODO: determine IPv4 or IPv6 status. + NSData *packet = [[NSData alloc] initWithBytes:buf length:len]; + [wrapper.packetFlow writePackets:@[packet] withProtocols:@[@AF_INET]]; + return len; + } } static void do_log(int level, const char *tag, const char *msg) { - // TODO Get some details on the log level and distribute to matching log levels. - os_log([WireGuardGoWrapper log], "Log level %d for %{public}s: %{public}s", level, tag, msg); + @autoreleasepool { + // TODO Get some details on the log level and distribute to matching log levels. + os_log([WireGuardGoWrapper log], "Log level %d for %{public}s: %{public}s", level, tag, msg); + } }