aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-12-13 19:36:43 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-12-13 19:36:51 +0100
commita6912ca7a2d3201d6ddb44247674cfcb74623513 (patch)
treebc7f7bc6aa94610c6887ee80c0e6941b0624897f
parentTunnelsManager: Remove mentions of 'internal error' (diff)
downloadwireguard-apple-a6912ca7a2d3201d6ddb44247674cfcb74623513.tar.xz
wireguard-apple-a6912ca7a2d3201d6ddb44247674cfcb74623513.zip
Tidy up str to gostr conversion
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift24
1 files changed, 8 insertions, 16 deletions
diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
index 65400e01..8fb60b1d 100644
--- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
+++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
@@ -87,14 +87,10 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
if path.status == .satisfied {
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: wgGetListenPort(handle))
- let err = endpointString.withCString {
- wgSetConfig(handle, gostring_t(p: $0, n: endpointString.utf8.count))
- }
+ let err = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
if err == -EADDRINUSE {
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
- _ = endpointString.withCString {
- wgSetConfig(handle, gostring_t(p: $0, n: endpointString.utf8.count))
- }
+ _ = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
}
}
}
@@ -158,18 +154,14 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
private func connect(interfaceName: String, settings: String, fileDescriptor: Int32) -> Int32 {
- return withStringsAsGoStrings(interfaceName, settings) { nameGoStr, settingsGoStr in
- return wgTurnOn(nameGoStr, settingsGoStr, fileDescriptor)
- }
+ return withStringsAsGoStrings(interfaceName, settings) { return wgTurnOn($0.0, $0.1, fileDescriptor) }
}
}
-private func withStringsAsGoStrings<R>(_ str1: String, _ str2: String, closure: (gostring_t, gostring_t) -> R) -> R {
- return str1.withCString { s1cStr in
- let gstr1 = gostring_t(p: s1cStr, n: str1.utf8.count)
- return str2.withCString { s2cStr in
- let gstr2 = gostring_t(p: s2cStr, n: str2.utf8.count)
- return closure(gstr1, gstr2)
- }
+// swiftlint:disable large_tuple
+func withStringsAsGoStrings<R>(_ s1: String, _ s2: String? = nil, _ s3: String? = nil, _ s4: String? = nil, call: @escaping ((gostring_t, gostring_t, gostring_t, gostring_t)) -> R) -> R {
+ func helper(_ p1: UnsafePointer<Int8>?, _ p2: UnsafePointer<Int8>?, _ p3: UnsafePointer<Int8>?, _ p4: UnsafePointer<Int8>?) -> 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)))
}
+ return helper(s1, s2, s3, s4)
}