Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,10 @@

import Foundation

public enum RelayFDState: Equatable, Sendable {
case idle
case readable
case drainThenStop
case stop
}

public func relayFDState(_ revents: Int16) -> RelayFDState {
if revents & Int16(POLLERR | POLLNVAL) != 0 { return .stop }
if revents & Int16(POLLHUP) != 0 { return .drainThenStop }
if revents & Int16(POLLIN) != 0 { return .readable }
return .idle
}

/// Outcome of waiting for a specific direction on the SSH transport. Distinct from
/// `RelayFDState`, which classifies a read-side poll where "no bits set" means idle.
/// Here "no bits set" means the wait expired without the requested direction becoming
/// ready, which must not be mistaken for readiness.
public enum TransportPollOutcome: Equatable, Sendable {
case ready
case timedOut
case hangup
}

public func transportPollOutcome(revents: Int16, requestedEvents: Int16) -> TransportPollOutcome {
if revents & Int16(POLLERR | POLLNVAL | POLLHUP) != 0 { return .hangup }
if revents & requestedEvents != 0 { return .ready }
return .timedOut
}

/// Waits for libssh2's requested block directions to become ready on the transport.
/// When libssh2 reports no direction, there is nothing to wait on, so this yields
/// briefly instead of returning immediately, which would let a caller retry in a
/// zero-yield spin.
// Waits for libssh2's requested block directions to become ready on the transport.
// When libssh2 reports no direction, there is nothing to wait on, so this yields
// briefly instead of returning immediately, which would let a caller retry in a
// zero-yield spin.
public func pollReady(fd: Int32, directions: RelayDirections, timeoutMs: Int32) -> Bool {
var events: Int16 = 0
if directions.contains(.inbound) { events |= Int16(POLLIN) }
Expand All @@ -54,3 +24,18 @@ public func pollReady(fd: Int32, directions: RelayDirections, timeoutMs: Int32)
}

private let directionlessYieldMicroseconds: UInt32 = 1_000

public enum TransportRoom: Equatable, Sendable {
case room
case full
case gone
}

// A socket polls writable once its send low-water mark is free (2048 bytes unless set), which holds a small
// packet whole. A reset or closed socket reports a hangup or an error instead.
public func transportRoom(_ descriptor: Int32) -> TransportRoom {
var pollFD = pollfd(fd: descriptor, events: Int16(POLLOUT), revents: 0)
guard poll(&pollFD, 1, 0) == 1 else { return .full }
if pollFD.revents & Int16(POLLERR | POLLHUP | POLLNVAL) != 0 { return .gone }
return pollFD.revents & Int16(POLLOUT) != 0 ? .room : .full
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// SSHChannelIO.swift
// TableProSSHTransport
//

import Foundation

public struct RelayDirections: OptionSet, Sendable {
public let rawValue: Int32

public init(rawValue: Int32) {
self.rawValue = rawValue
}

public static let inbound = RelayDirections(rawValue: 1 << 0)
public static let outbound = RelayDirections(rawValue: 1 << 1)
}

public enum ChannelReadResult: Equatable, Sendable {
case bytes(Int)
case wouldBlock
case closed
// The session is gone, not only this channel: the relay ends .transportHangup so the owner marks the tunnel dead now, not at the next keep-alive.
case transportFailed
}

public enum ChannelWriteResult: Equatable, Sendable {
case bytes(Int)
case wouldBlock
case closed
case transportFailed
}

// One channel of a session, called only on its `SSHSessionPump`'s queue.
public protocol SSHChannelIO: Sendable {
func read(into buffer: UnsafeMutablePointer<CChar>, count: Int) -> ChannelReadResult
func write(_ buffer: UnsafePointer<CChar>, count: Int) -> ChannelWriteResult
func blockDirections() -> RelayDirections
// Data or an end of file another call on the session already took off the transport for this channel.
func hasQueuedInput() -> Bool
// Room left in the server's window for this channel's data. A `write` that answered `.wouldBlock` with
// room has a packet on its way that only another `write` on this channel finishes; without room it waits
// for the server to open the window.
func hasSendWindow() -> Bool
}

public enum RelayTermination: Equatable, Sendable {
case localClosed
case transportHangup
case channelClosed
case cancelled
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import Foundation
import os

// Cancelling a Swift task does not cancel the GCD block that runs a hop's `SSHChannelRelay`, so
// teardown wakes its descriptors and waits here for its last libssh2 call before freeing the channel
// or session. The stop action and the relay's final close share one lock, so a late stop cannot
// `shutdown` a descriptor number the kernel already handed to someone else.
// A hop's relay runs on its session's queue, outside any Swift task, so teardown stops it and waits
// here for its last libssh2 call before freeing the channel or session. The stop action and the
// finish share one lock, so a stop that comes after the finish does nothing.
public final class SSHJumpRelayFence: @unchecked Sendable {
private struct State {
var stopAction: (@Sendable () -> Void)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public final class SSHListenerAcceptor: @unchecked Sendable {
}
}
let acceptedAt = Date()
// Darwin hands the listener's O_NONBLOCK to the accepted socket, and the relays block on it.
// Darwin hands the listener's O_NONBLOCK to the accepted socket, and the Remote SQLite admission read blocks on it.
let flags = fcntl(descriptor, F_GETFL, 0)
if flags >= 0 {
_ = fcntl(descriptor, F_SETFL, flags & ~O_NONBLOCK)
Expand Down
Loading
Loading