aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuardNetworkExtension
diff options
context:
space:
mode:
authorEric Kuck <eric@bluelinelabs.com>2018-12-12 15:33:14 -0600
committerEric Kuck <eric@bluelinelabs.com>2018-12-12 15:33:14 -0600
commite4ac48bc75064c0e144020ded1ed877d226742c8 (patch)
treeffee70e6c957a717cc8a6b805e6cefa59ed05bc7 /WireGuard/WireGuardNetworkExtension
parentTons more swiftlint warnings fixed. Still a few remaining. (diff)
downloadwireguard-apple-e4ac48bc75064c0e144020ded1ed877d226742c8.tar.xz
wireguard-apple-e4ac48bc75064c0e144020ded1ed877d226742c8.zip
More linter warnings fixed, enabled more swiftlint rules, project cleanup
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to 'WireGuard/WireGuardNetworkExtension')
-rw-r--r--WireGuard/WireGuardNetworkExtension/DNSResolver.swift41
-rw-r--r--WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift2
-rw-r--r--WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift12
-rw-r--r--WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift2
4 files changed, 31 insertions, 26 deletions
diff --git a/WireGuard/WireGuardNetworkExtension/DNSResolver.swift b/WireGuard/WireGuardNetworkExtension/DNSResolver.swift
index 4ce89b2..57093c8 100644
--- a/WireGuard/WireGuardNetworkExtension/DNSResolver.swift
+++ b/WireGuard/WireGuardNetworkExtension/DNSResolver.swift
@@ -66,25 +66,12 @@ extension DNSResolver {
// Based on DNS resolution code by Jason Donenfeld <jason@zx2c4.com>
// in parse_endpoint() in src/tools/config.c in the WireGuard codebase
private static func resolveSync(endpoint: Endpoint) -> Endpoint? {
- var hints = addrinfo(
- ai_flags: 0,
- ai_family: AF_UNSPEC,
- ai_socktype: SOCK_DGRAM, // WireGuard is UDP-only
- ai_protocol: IPPROTO_UDP, // WireGuard is UDP-only
- ai_addrlen: 0,
- ai_canonname: nil,
- ai_addr: nil,
- ai_next: nil)
- var resultPointer = UnsafeMutablePointer<addrinfo>(OpaquePointer(bitPattern: 0))
switch endpoint.host {
case .name(let name, _):
+ var resultPointer = UnsafeMutablePointer<addrinfo>(OpaquePointer(bitPattern: 0))
+
// The endpoint is a hostname and needs DNS resolution
- let returnValue = getaddrinfo(
- name.cString(using: .utf8), // Hostname
- "\(endpoint.port)".cString(using: .utf8), // Port
- &hints,
- &resultPointer)
- if returnValue == 0 {
+ if addressInfo(for: name, port: endpoint.port, resultPointer: &resultPointer) == 0 {
// getaddrinfo succeeded
let ipv4Buffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(INET_ADDRSTRLEN))
let ipv6Buffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(INET6_ADDRSTRLEN))
@@ -115,9 +102,9 @@ extension DNSResolver {
ipv6Buffer.deallocate()
// We prefer an IPv4 address over an IPv6 address
if let ipv4AddressString = ipv4AddressString, let ipv4Address = IPv4Address(ipv4AddressString) {
- return Endpoint(host: NWEndpoint.Host.ipv4(ipv4Address), port: endpoint.port)
+ return Endpoint(host: .ipv4(ipv4Address), port: endpoint.port)
} else if let ipv6AddressString = ipv6AddressString, let ipv6Address = IPv6Address(ipv6AddressString) {
- return Endpoint(host: NWEndpoint.Host.ipv6(ipv6Address), port: endpoint.port)
+ return Endpoint(host: .ipv6(ipv6Address), port: endpoint.port)
} else {
return nil
}
@@ -130,4 +117,22 @@ extension DNSResolver {
return endpoint
}
}
+
+ private static func addressInfo(for name: String, port: NWEndpoint.Port, resultPointer: inout UnsafeMutablePointer<addrinfo>?) -> Int32 {
+ var hints = addrinfo(
+ ai_flags: 0,
+ ai_family: AF_UNSPEC,
+ ai_socktype: SOCK_DGRAM, // WireGuard is UDP-only
+ ai_protocol: IPPROTO_UDP, // WireGuard is UDP-only
+ ai_addrlen: 0,
+ ai_canonname: nil,
+ ai_addr: nil,
+ ai_next: nil)
+
+ return getaddrinfo(
+ name.cString(using: .utf8), // Hostname
+ "\(port)".cString(using: .utf8), // Port
+ &hints,
+ &resultPointer)
+ }
}
diff --git a/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift b/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift
index f06860a..163535a 100644
--- a/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift
+++ b/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift
@@ -20,6 +20,6 @@ class ErrorNotifier {
static func notify(_ error: PacketTunnelProviderError, from tunnelProvider: NEPacketTunnelProvider) {
guard let (title, message) = ErrorNotifier.errorMessage(for: error) else { return }
// displayMessage() is deprecated, but there's no better alternative to show the error to the user
- tunnelProvider.displayMessage("\(title): \(message)", completionHandler: { (_) in })
+ tunnelProvider.displayMessage("\(title): \(message)") { _ in }
}
}
diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
index 029d74b..b1571d5 100644
--- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
+++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
@@ -79,7 +79,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
// Bring up wireguard-go backend
- let fileDescriptor = packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32
+ let fileDescriptor = packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32 //swiftlint:disable:this force_cast
if fileDescriptor < 0 {
wg_log(.error, staticMessage: "Starting tunnel failed: Could not determine file descriptor")
ErrorNotifier.notify(PacketTunnelProviderError.couldNotStartWireGuard, from: self)
@@ -124,7 +124,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
// Apply network settings
let networkSettings: NEPacketTunnelNetworkSettings = packetTunnelSettingsGenerator.generateNetworkSettings()
- setTunnelNetworkSettings(networkSettings) { (error) in
+ setTunnelNetworkSettings(networkSettings) { error in
if let error = error {
wg_log(.error, staticMessage: "Starting tunnel failed: Error setting network settings.")
wg_log(.error, message: "Error from setTunnelNetworkSettings: \(error.localizedDescription)")
@@ -169,7 +169,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
// Setup WireGuard logger
- wgSetLogger { (level, msgCStr) in
+ wgSetLogger { level, msgCStr in
let logType: OSLogType
switch level {
case 0:
@@ -187,7 +187,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
private func connect(interfaceName: String, settings: String, fileDescriptor: Int32) -> Int32 {
- return withStringsAsGoStrings(interfaceName, settings) { (nameGoStr, settingsGoStr) -> Int32 in
+ return withStringsAsGoStrings(interfaceName, settings) { nameGoStr, settingsGoStr in
return wgTurnOn(nameGoStr, settingsGoStr, fileDescriptor)
}
}
@@ -206,9 +206,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
private func withStringsAsGoStrings<R>(_ str1: String, _ str2: String, closure: (gostring_t, gostring_t) -> R) -> R {
- return str1.withCString { (s1cStr) -> R in
+ return str1.withCString { s1cStr in
let gstr1 = gostring_t(p: s1cStr, n: str1.utf8.count)
- return str2.withCString { (s2cStr) -> R in
+ return str2.withCString { s2cStr in
let gstr2 = gostring_t(p: s2cStr, n: str2.utf8.count)
return closure(gstr1, gstr2)
}
diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift
index b9562a0..1e5ae8e 100644
--- a/WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift
+++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelSettingsGenerator.swift
@@ -70,7 +70,7 @@ class PacketTunnelSettingsGenerator {
* a valid IP address that will actually route over the Internet.
*/
var remoteAddress: String = "0.0.0.0"
- let endpointsCompact = resolvedEndpoints.compactMap({ $0 })
+ let endpointsCompact = resolvedEndpoints.compactMap { $0 }
if endpointsCompact.count == 1 {
switch endpointsCompact.first!.host {
case .ipv4(let address):