2018-06-22 06:23:39 +00:00
|
|
|
//
|
|
|
|
// WireGuardGoWrapper.m
|
|
|
|
// WireGuardNetworkExtension
|
|
|
|
//
|
|
|
|
// Created by Jeroen Leenarts on 21-06-18.
|
2018-07-15 09:55:41 +00:00
|
|
|
// Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All rights reserved.
|
2018-06-22 06:23:39 +00:00
|
|
|
//
|
|
|
|
|
2018-07-07 20:54:44 +00:00
|
|
|
#include <os/log.h>
|
2018-08-03 20:24:41 +00:00
|
|
|
|
2018-06-22 06:23:39 +00:00
|
|
|
#include "wireguard.h"
|
2018-08-03 20:24:41 +00:00
|
|
|
#import "WireGuardGoWrapper.h"
|
2018-06-22 06:23:39 +00:00
|
|
|
|
|
|
|
/// Trampoline function
|
|
|
|
static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len);
|
|
|
|
/// Trampoline function
|
|
|
|
static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len);
|
2018-07-07 20:54:44 +00:00
|
|
|
/// Trampoline function
|
|
|
|
static void do_log(int level, const char *tag, const char *msg);
|
|
|
|
|
|
|
|
|
2018-06-22 06:23:39 +00:00
|
|
|
|
|
|
|
@interface WireGuardGoWrapper ()
|
|
|
|
|
|
|
|
@property (nonatomic, assign) int handle;
|
|
|
|
@property (nonatomic, assign) BOOL isClosed;
|
2018-08-03 20:24:41 +00:00
|
|
|
@property (nonatomic, strong) NSMutableArray<NSData *> *packets;
|
|
|
|
@property (nonatomic, strong) NSMutableArray<NSNumber *> *protocols;
|
|
|
|
|
|
|
|
@property (nonatomic, strong) NSCondition *condition;
|
2018-06-22 06:23:39 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation WireGuardGoWrapper
|
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
self.condition = [NSCondition new];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString
|
2018-06-22 06:23:39 +00:00
|
|
|
{
|
2018-08-05 06:05:59 +00:00
|
|
|
os_log([WireGuardGoWrapper log], "WireGuard Go Version %{public}s", wgVersion());
|
2018-07-07 20:54:44 +00:00
|
|
|
|
|
|
|
wgSetLogger(do_log);
|
|
|
|
|
2018-06-22 06:23:39 +00:00
|
|
|
const char * ifName = [interfaceName UTF8String];
|
|
|
|
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));
|
2018-08-03 20:24:41 +00:00
|
|
|
|
|
|
|
return self.handle > 0;
|
2018-06-22 06:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) turnOff
|
|
|
|
{
|
|
|
|
self.isClosed = YES;
|
|
|
|
wgTurnOff(self.handle);
|
|
|
|
}
|
|
|
|
|
2018-08-05 06:05:59 +00:00
|
|
|
+ (NSString *)versionWireGuardGo {
|
|
|
|
return [NSString stringWithUTF8String:wgVersion()];
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:54:44 +00:00
|
|
|
+ (os_log_t)log {
|
|
|
|
static os_log_t subLog = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
subLog = os_log_create("com.wireguard.ios.WireGuard.WireGuardNetworkExtension", "WireGuard-Go");
|
|
|
|
});
|
|
|
|
|
|
|
|
return subLog;
|
|
|
|
}
|
|
|
|
|
2018-06-22 06:23:39 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx;
|
2018-08-05 13:58:52 +00:00
|
|
|
if (wrapper.isClosed) return -1;
|
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
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];
|
|
|
|
|
2018-08-05 13:58:52 +00:00
|
|
|
NSUInteger packetLength = [packet length];
|
|
|
|
if (packetLength > len) {
|
|
|
|
// The packet will be dropped when we end up here.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(buf, [packet bytes], packetLength);
|
2018-08-03 20:24:41 +00:00
|
|
|
|
2018-08-05 13:58:52 +00:00
|
|
|
return packetLength;
|
2018-06-22 06:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx;
|
2018-08-03 20:24:41 +00:00
|
|
|
//TODO: determine IPv4 or IPv6 status.
|
|
|
|
NSData *packet = [[NSData alloc] initWithBytes:buf length:len];
|
|
|
|
[wrapper.packetFlow writePackets:@[packet] withProtocols:@[@AF_INET]];
|
2018-06-22 06:23:39 +00:00
|
|
|
return len;
|
|
|
|
}
|
2018-07-07 20:54:44 +00:00
|
|
|
|
|
|
|
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.
|
2018-08-04 20:47:50 +00:00
|
|
|
os_log([WireGuardGoWrapper log], "Log level %d for %{public}s: %{public}s", level, tag, msg);
|
2018-07-07 20:54:44 +00:00
|
|
|
}
|