Files
archived-libtailscale/swift/TailscaleKit/TailscaleError.swift
Jonathan Nobels 2948fe1136 swift: add NWConnection-inspired Swift 6 compatible Tailscale.framework
updates tailscale/tailscale#13937

Builds a TailscaleKit.framework which wraps libtailscale.a in an actor,
providing IncomingConnection, Listener, and OutgoingConnection
types which are more usable in pure async Swift code.  The
method signatures are intended to be similar in form to NWConnection.

We also provide an extension to URLSession so you can make URL
requests via the user-space proxy.

Adds a static library mirroring much of the test control utilities in go, minus
the dependency on testing, so that we can export the signatures and
link it to tests that cannot run in go.

Added functionality to get both the local interface IPs as well as
the remote IP of  incoming connections accepted by a listener.

Fixed a bug in the log writer so we append a newline.

This also updates to the latest go toolchain and tailscale version.

Signed-off-by: Jonathan Nobels <jonathan@tailscale.com>
2025-01-02 14:35:20 -05:00

51 lines
2.1 KiB
Swift

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
public enum TailscaleError: Error {
case badInterfaceHandle ///< The tailscale handle is bad.
case listenerClosed ///< The listener was closed and cannot accept new connections
case invalidTimeout ///< The provided listener timeout is invalid
case connectionClosed ///< The underlying connection is closed
case readFailed ///< Read failed
case shortWrite ///< Some data was not written to the connection
case invalidProxyAddress ///< Some data was not written to the connection
case invalidControlURL ///< The provided control URL is invalid
case cannotFetchIps(_ details: String? = nil) ///< The IPs for the Tailscale server could not be read
case posixError(_ err: POSIXError, _ details: String? = nil) ///< A posix error was thrown with the given err code and details
case unknownPosixError(_ err: Int32, _ details: String? = nil) ///< An unknown posix error occurred
case internalError(_ details: String? = nil) ///< A generic internal error occurred
/// Create a Tailscale error from an underlying posix error code
static func fromPosixErrCode(_ code: Int32, _ details: String? = nil) -> TailscaleError {
if code == -1 {
return .internalError(details)
}
if let code = POSIXErrorCode(rawValue: code){
return .posixError( POSIXError(code))
}
return unknownPosixError(code, details)
}
}
extension TailscaleHandle {
static let kMaxErrorMessageLength: Int = 256
/// Returns the last error message in the Tailscale server as a string.
/// Handles messages up to kMaxErrorMessageLength bytes only.
internal func getErrorMessage() -> String {
let buf = UnsafeMutablePointer<Int8>.allocate(capacity: Self.kMaxErrorMessageLength)
defer {
buf.deallocate()
}
let res = tailscale_errmsg(self, buf, 256)
if res != 0 {
return "Error fetch failure: \(res)"
}
return String(cString: buf)
}
}