]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Start wire-up of logging and tunnel system.
authorJeroen Leenarts <jeroen.leenarts@gmail.com>
Sat, 7 Jul 2018 20:54:44 +0000 (22:54 +0200)
committerJeroen Leenarts <jeroen.leenarts@gmail.com>
Sat, 7 Jul 2018 20:54:44 +0000 (22:54 +0200)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Podfile.lock
WireGuard.xcodeproj/project.pbxproj
WireGuard/Log.swift [new file with mode: 0644]
WireGuardNetworkExtension/Log.swift [new file with mode: 0644]
WireGuardNetworkExtension/PacketTunnelProvider.swift
WireGuardNetworkExtension/WireGuardGoWrapper.m

index aaad4ec4bc7a8c7030cece77f2d4ad1ae6bbf2d8..e0cd145bdde12e5a70816dd883a5dfaaf5a23243 100644 (file)
@@ -63,4 +63,4 @@ SPEC CHECKSUMS:
 
 PODFILE CHECKSUM: 0d0e9b16451896d624a83e7c8701984fa3e606e0
 
-COCOAPODS: 1.5.2
+COCOAPODS: 1.5.3
index d856bf18e74f32cddae0bb9a81c831d72e2ef97f..694b167486815a86eca11cbacf78b04bc9490a25 100644 (file)
@@ -35,6 +35,8 @@
                4A8AABD820B6A79100B6D8C1 /* UITableView+WireGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8AABD720B6A79100B6D8C1 /* UITableView+WireGuard.swift */; };
                4AD095C820DC4190000E9CF5 /* libwg-go.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4AD0900120DC4171000E9CF5 /* libwg-go.a */; };
                4AD095CC20DC42CD000E9CF5 /* WireGuardGoWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AD095CB20DC42CD000E9CF5 /* WireGuardGoWrapper.m */; };
+               4AEAC32920F14B3B007B67AB /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AEAC32820F14B3B007B67AB /* Log.swift */; };
+               4AEAC32B20F14BA9007B67AB /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AEAC32A20F14BA9007B67AB /* Log.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
                4AD095C920DC42CD000E9CF5 /* WireGuardNetworkExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardNetworkExtension-Bridging-Header.h"; sourceTree = "<group>"; };
                4AD095CA20DC42CD000E9CF5 /* WireGuardGoWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WireGuardGoWrapper.h; sourceTree = "<group>"; };
                4AD095CB20DC42CD000E9CF5 /* WireGuardGoWrapper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WireGuardGoWrapper.m; sourceTree = "<group>"; };
+               4AEAC32820F14B3B007B67AB /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = "<group>"; };
+               4AEAC32A20F14BA9007B67AB /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = "<group>"; };
                861983CAE8FDC13BC83E7E04 /* Pods_WireGuard.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WireGuard.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 /* End PBXFileReference section */
 
                                4A4BACEC20B5F1C100F12B28 /* Assets.xcassets */,
                                4A4BACEE20B5F1C100F12B28 /* LaunchScreen.storyboard */,
                                4A4BACF120B5F1C100F12B28 /* Info.plist */,
+                               4AEAC32A20F14BA9007B67AB /* Log.swift */,
                        );
                        path = WireGuard;
                        sourceTree = "<group>";
                                4AD095CA20DC42CD000E9CF5 /* WireGuardGoWrapper.h */,
                                4AD095CB20DC42CD000E9CF5 /* WireGuardGoWrapper.m */,
                                4AD095C920DC42CD000E9CF5 /* WireGuardNetworkExtension-Bridging-Header.h */,
+                               4AEAC32820F14B3B007B67AB /* Log.swift */,
                        );
                        path = WireGuardNetworkExtension;
                        sourceTree = "<group>";
                        files = (
                                4A4BAD0C20B5F6AA00F12B28 /* AppCoordinator.swift in Sources */,
                                4A4BAD2220B6026900F12B28 /* Interface+CoreDataProperties.swift in Sources */,
+                               4AEAC32B20F14BA9007B67AB /* Log.swift in Sources */,
                                4A7F6EDE20B674CD00B260B7 /* Address+CoreDataProperties.swift in Sources */,
                                4A4BAD1320B5F82400F12B28 /* Identifyable.swift in Sources */,
                                4A4BAD1720B5F8DE00F12B28 /* WireGuard.xcdatamodeld in Sources */,
                        files = (
                                4AD095CC20DC42CD000E9CF5 /* WireGuardGoWrapper.m in Sources */,
                                4A61D82920D98CE2006C7A76 /* PacketTunnelProvider.swift in Sources */,
+                               4AEAC32920F14B3B007B67AB /* Log.swift in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                                CODE_SIGN_ENTITLEMENTS = WireGuardNetworkExtension/WireGuardNetworkExtension.entitlements;
                                CODE_SIGN_STYLE = Automatic;
                                DEVELOPMENT_TEAM = 67JZJ7TWU3;
+                               ENABLE_BITCODE = NO;
                                INFOPLIST_FILE = WireGuardNetworkExtension/Info.plist;
                                IPHONEOS_DEPLOYMENT_TARGET = 11.4;
                                LD_RUNPATH_SEARCH_PATHS = (
                                CODE_SIGN_ENTITLEMENTS = WireGuardNetworkExtension/WireGuardNetworkExtension.entitlements;
                                CODE_SIGN_STYLE = Automatic;
                                DEVELOPMENT_TEAM = 67JZJ7TWU3;
+                               ENABLE_BITCODE = NO;
                                INFOPLIST_FILE = WireGuardNetworkExtension/Info.plist;
                                IPHONEOS_DEPLOYMENT_TARGET = 11.4;
                                LD_RUNPATH_SEARCH_PATHS = (
diff --git a/WireGuard/Log.swift b/WireGuard/Log.swift
new file mode 100644 (file)
index 0000000..3f63fb6
--- /dev/null
@@ -0,0 +1,13 @@
+//
+//  Log.swift
+//  WireGuard
+//
+//  Created by Jeroen Leenarts on 07-07-18.
+//  Copyright © 2018 WireGuard. All rights reserved.
+//
+
+import os.log
+
+struct Log {
+    static var general = OSLog(subsystem: "com.wireguard.ios.WireGuard", category: "general")
+}
diff --git a/WireGuardNetworkExtension/Log.swift b/WireGuardNetworkExtension/Log.swift
new file mode 100644 (file)
index 0000000..358874c
--- /dev/null
@@ -0,0 +1,13 @@
+//
+//  Log.swift
+//  WireGuardNetworkExtension
+//
+//  Created by Jeroen Leenarts on 07-07-18.
+//  Copyright © 2018 WireGuard. All rights reserved.
+//
+
+import os.log
+
+struct Log {
+    static var general = OSLog(subsystem: "com.wireguard.ios.WireGuard.WireGuardNetworkExtension", category: "general")
+}
index 07fd8d3ac90528f5fe3f3064f49cbff646d964f6..14ddbb8ed18e9ab5f4093c0a86d361b5da8f67a4 100644 (file)
@@ -7,16 +7,31 @@
 //
 
 import NetworkExtension
+import os.log
 
 class PacketTunnelProvider: NEPacketTunnelProvider {
+    let wireGuardWrapper = WireGuardGoWrapper()
+
+    private let tunnelQueue = DispatchQueue(label: PacketTunnelProvider.description())
+
+    //TODO create a way to transfer config into extension
 
     override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
+        os_log("Starting tunnel", log: Log.general, type: .info)
         // Add code here to start the process of connecting the tunnel.
 
+        //TODO get a settings string in here.
+        tunnelQueue.sync {
+            wireGuardWrapper.turnOn(withInterfaceName: "TODO", settingsString: "TODO")
+        }
     }
 
     override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
+        os_log("Stopping tunnel", log: Log.general, type: .info)
         // Add code here to start the process of stopping the tunnel.
+        tunnelQueue.sync {
+            wireGuardWrapper.turnOff()
+        }
         completionHandler()
     }
 
@@ -27,12 +42,21 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
         }
     }
 
-    override func sleep(completionHandler: @escaping () -> Void) {
-        // Add code here to get ready to sleep.
-        completionHandler()
+    private func loopReadPackets(_ handler: @escaping ([Data]?, Error?) -> Void) {
+        packetFlow.readPackets { [weak self] (_, _) in
+            // TODO write packets into the tunnel
+            self?.loopReadPackets(handler)
+        }
+    }
+
+    func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
+        packetFlow.writePackets([packet], withProtocols: [AF_INET] as [NSNumber])
+        completionHandler?(nil)
     }
 
-    override func wake() {
-        // Add code here to wake up.
+    func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
+        let protocols = [Int32](repeating: AF_INET, count: packets.count) as [NSNumber]
+        packetFlow.writePackets(packets, withProtocols: protocols)
+        completionHandler?(nil)
     }
 }
index 4244fe91df616e062e327d5cc7962489e97aea1a..0b854f1e8c96b2a1bab98372d3e0d1d50e8f89fe 100644 (file)
@@ -8,12 +8,17 @@
 
 #import "WireGuardGoWrapper.h"
 
+#include <os/log.h>
 #include "wireguard.h"
 
 /// 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);
+/// Trampoline function
+static void do_log(int level, const char *tag, const char *msg);
+
+
 
 @interface WireGuardGoWrapper ()
 
@@ -26,6 +31,9 @@ static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len);
 
 - (void) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString
 {
+
+    wgSetLogger(do_log);
+
     const char * ifName = [interfaceName UTF8String];
     const char * settings = [settingsString UTF8String];
 
@@ -38,6 +46,16 @@ static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len);
     wgTurnOff(self.handle);
 }
 
++ (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;
+}
+
 @end
 
 static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
@@ -45,6 +63,7 @@ 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
     return wrapper.isClosed ? -1 : 0;
 }
 
@@ -54,3 +73,9 @@ static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len)
     printf("Writing from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len);
     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 %s: %s", level, tag, msg);
+}