aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuardNetworkExtension/WireGuardGoWrapper.m
diff options
context:
space:
mode:
Diffstat (limited to 'WireGuardNetworkExtension/WireGuardGoWrapper.m')
-rw-r--r--WireGuardNetworkExtension/WireGuardGoWrapper.m48
1 files changed, 41 insertions, 7 deletions
diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.m b/WireGuardNetworkExtension/WireGuardGoWrapper.m
index f15e34e..a24a239 100644
--- a/WireGuardNetworkExtension/WireGuardGoWrapper.m
+++ b/WireGuardNetworkExtension/WireGuardGoWrapper.m
@@ -6,10 +6,10 @@
// Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All rights reserved.
//
-#import "WireGuardGoWrapper.h"
-
#include <os/log.h>
+
#include "wireguard.h"
+#import "WireGuardGoWrapper.h"
/// Trampoline function
static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len);
@@ -24,12 +24,25 @@ static void do_log(int level, const char *tag, const char *msg);
@property (nonatomic, assign) int handle;
@property (nonatomic, assign) BOOL isClosed;
+@property (nonatomic, strong) NSMutableArray<NSData *> *packets;
+@property (nonatomic, strong) NSMutableArray<NSNumber *> *protocols;
+
+@property (nonatomic, strong) NSCondition *condition;
@end
@implementation WireGuardGoWrapper
-- (void) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString
+- (instancetype)init
+{
+ self = [super init];
+ if (self) {
+ self.condition = [NSCondition new];
+ }
+ return self;
+}
+
+- (BOOL) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString
{
wgSetLogger(do_log);
@@ -38,6 +51,8 @@ static void do_log(int level, const char *tag, const char *msg);
const char * settings = [settingsString UTF8String];
self.handle = wgTurnOn((gostring_t){ .p = ifName, .n = interfaceName.length }, (gostring_t){ .p = settings, .n = settingsString.length }, do_read, do_write, (__bridge void *)(self));
+
+ return self.handle > 0;
}
- (void) turnOff
@@ -61,16 +76,35 @@ 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)
{
WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx;
- printf("Reading from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len);
- sleep(1);
- // TODO received data from tunnel, write to Packetflow
+ if (wrapper.packets.count == 0) {
+
+ [wrapper.packetFlow readPacketsWithCompletionHandler:^(NSArray<NSData *> * _Nonnull packets, NSArray<NSNumber *> * _Nonnull protocols) {
+ [wrapper.packets addObjectsFromArray:packets];
+ [wrapper.protocols addObjectsFromArray:protocols];
+ // TODO make sure that the completion handler and the do_read are not performed on the same thread.
+ [wrapper.condition signal];
+ }];
+ [wrapper.condition wait];
+ }
+
+ NSData *packet = [wrapper.packets objectAtIndex:0];
+// NSNumber *protocol = [wrapper.protocols objectAtIndex:0];
+ [wrapper.packets removeObjectAtIndex:0];
+ [wrapper.protocols removeObjectAtIndex:0];
+
+ len = [packet length];
+ buf = (Byte*)malloc(len);
+ memcpy(buf, [packet bytes], len);
+
return wrapper.isClosed ? -1 : 0;
}
static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len)
{
WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx;
- printf("Writing from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len);
+ //TODO: determine IPv4 or IPv6 status.
+ NSData *packet = [[NSData alloc] initWithBytes:buf length:len];
+ [wrapper.packetFlow writePackets:@[packet] withProtocols:@[@AF_INET]];
return len;
}