aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-12-21 22:05:47 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-12-21 22:05:47 +0100
commitaccf60b82f29b3af930b372a2f401c299ceeb5c2 (patch)
treeab13c9a95ac167738c415f0732385564de42622f /WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
parentAll migration stuff moved to one gross file (diff)
downloadwireguard-apple-accf60b82f29b3af930b372a2f401c299ceeb5c2.tar.xz
wireguard-apple-accf60b82f29b3af930b372a2f401c299ceeb5c2.zip
Do not require NetworkExtension to know its own name
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to '')
-rw-r--r--WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift24
1 files changed, 11 insertions, 13 deletions
diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
index 27a42c5d..5e994c0c 100644
--- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
+++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
@@ -37,10 +37,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
configureLogger()
- let tunnelName = tunnelConfiguration.interface.name
- wg_log(.info, message: "Starting tunnel '\(tunnelName)' from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app"))
-
- errorNotifier.tunnelName = tunnelName
+ wg_log(.info, message: "Starting tunnel from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app"))
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
guard let resolvedEndpoints = DNSResolver.resolveSync(endpoints: endpoints) else {
@@ -67,7 +64,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
networkMonitor!.pathUpdateHandler = pathUpdate
networkMonitor!.start(queue: DispatchQueue(label: "NetworkMonitor"))
- let handle = withStringsAsGoStrings(tunnelConfiguration.interface.name, wireguardSettings) { return wgTurnOn($0.0, $0.1, fileDescriptor) }
+ let handle = wireguardSettings.withGoString { return wgTurnOn($0, fileDescriptor) }
if handle < 0 {
wg_log(.error, staticMessage: "Starting tunnel failed: Could not start WireGuard")
errorNotifier.notify(PacketTunnelProviderError.couldNotStartWireGuard)
@@ -131,19 +128,20 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
guard path.status == .satisfied else { return }
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: listenPort)
- let err = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
+ let err = endpointString.withGoString { return wgSetConfig(handle, $0) }
if err == -EADDRINUSE && listenPort != nil {
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
- _ = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
+ _ = endpointString.withGoString { return wgSetConfig(handle, $0) }
+
}
}
}
-// swiftlint:disable:next large_tuple identifier_name
-func withStringsAsGoStrings<R>(_ s1: String, _ s2: String? = nil, _ s3: String? = nil, _ s4: String? = nil, call: ((gostring_t, gostring_t, gostring_t, gostring_t)) -> R) -> R {
- // swiftlint:disable:next large_tuple identifier_name
- func helper(_ p1: UnsafePointer<Int8>?, _ p2: UnsafePointer<Int8>?, _ p3: UnsafePointer<Int8>?, _ p4: UnsafePointer<Int8>?, _ call: ((gostring_t, gostring_t, gostring_t, gostring_t)) -> R) -> R {
- return call((gostring_t(p: p1, n: s1.utf8.count), gostring_t(p: p2, n: s2?.utf8.count ?? 0), gostring_t(p: p3, n: s3?.utf8.count ?? 0), gostring_t(p: p4, n: s4?.utf8.count ?? 0)))
+extension String {
+ func withGoString<R>(_ call: (gostring_t) -> R) -> R {
+ func helper(_ pointer: UnsafePointer<Int8>?, _ call: (gostring_t) -> R) -> R {
+ return call(gostring_t(p: pointer, n: utf8.count))
+ }
+ return helper(self, call)
}
- return helper(s1, s2, s3, s4, call)
}