Do not require NetworkExtension to know its own name

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-12-21 22:05:47 +01:00
parent f6af9d9ffb
commit accf60b82f
4 changed files with 18 additions and 27 deletions

View File

@ -8,8 +8,6 @@ class ErrorNotifier {
let activationAttemptId: String?
weak var tunnelProvider: NEPacketTunnelProvider?
var tunnelName: String?
init(activationAttemptId: String?, tunnelProvider: NEPacketTunnelProvider) {
self.activationAttemptId = activationAttemptId
self.tunnelProvider = tunnelProvider

View File

@ -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)
}

View File

@ -32,15 +32,14 @@ var loggerFunc unsafe.Pointer
var versionString *C.char
type CLogger struct {
level C.int
interfaceName string
level C.int
}
func (l *CLogger) Write(p []byte) (int, error) {
if uintptr(loggerFunc) == 0 {
return 0, errors.New("No logger initialized")
}
message := C.CString(l.interfaceName + ": " + string(p))
message := C.CString(string(p))
C.callLogger(loggerFunc, l.level, message)
C.free(unsafe.Pointer(message))
return len(p), nil
@ -75,17 +74,13 @@ func wgSetLogger(loggerFn uintptr) {
}
//export wgTurnOn
func wgTurnOn(ifnameRef string, settings string, tunFd int32) int32 {
interfaceName := string([]byte(ifnameRef))
func wgTurnOn(settings string, tunFd int32) int32 {
logger := &Logger{
Debug: log.New(&CLogger{level: 0, interfaceName: interfaceName}, "", 0),
Info: log.New(&CLogger{level: 1, interfaceName: interfaceName}, "", 0),
Error: log.New(&CLogger{level: 2, interfaceName: interfaceName}, "", 0),
Debug: log.New(&CLogger{level: 0}, "", 0),
Info: log.New(&CLogger{level: 1}, "", 0),
Error: log.New(&CLogger{level: 2}, "", 0),
}
logger.Debug.Println("Debug log enabled")
tun, _, err := tun.CreateTUNFromFD(int(tunFd))
if err != nil {
logger.Error.Println(err)

View File

@ -12,7 +12,7 @@
typedef struct { const char *p; size_t n; } gostring_t;
typedef void(*logger_fn_t)(int level, const char *msg);
extern void wgSetLogger(logger_fn_t logger_fn);
extern int wgTurnOn(gostring_t ifname, gostring_t settings, int32_t tun_fd);
extern int wgTurnOn(gostring_t settings, int32_t tun_fd);
extern void wgTurnOff(int handle);
extern int64_t wgSetConfig(int handle, gostring_t settings);
extern uint16_t wgGetListenPort(int handle);