Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Keep idle established connections open in tunnel mode

Jackye2c4b00acb3d
diff --git a/VPhoneKit/VPhoneCoreKit/Network/VPhoneTCPForwarder.swift b/VPhoneKit/VPhoneCoreKit/Network/VPhoneTCPForwarder.swift
index 8b9c79fd..a4a4a62f 100644
--- a/VPhoneKit/VPhoneCoreKit/Network/VPhoneTCPForwarder.swift
+++ b/VPhoneKit/VPhoneCoreKit/Network/VPhoneTCPForwarder.swift
@@ -57,8 +57,16 @@ final class VPhoneTCPForwarder: @unchecked Sendable {
/// Same rationale as the UDP forwarder: UDP has no teardown, so a timer is
/// the only bound. TCP does have one, but a peer that vanishes mid-connection
- /// leaves a session behind until this fires.
+ /// leaves a session behind until this fires. Also how often the reaper runs.
+ ///
+ /// This bound is for connections that are opening or closing.
private static let idleTimeout: TimeInterval = 120
+ /// The bound for an established connection, RFC 5382's floor (REQ-5).
+ /// Silence there is normal: an ssh session with no keepalives sends
+ /// nothing while its user reads, and push and chat connections ping every
+ /// few minutes. Reaped at 120 s, such a connection was torn down under its
+ /// user with no RST to the guest.
+ private static let establishedIdleTimeout: TimeInterval = 2 * 3600 + 4 * 60
/// A ceiling, so a guest that opens sockets in a loop cannot exhaust the
/// host. Well above what a VM needs.
private static let maximumConnections = 256
@@ -834,9 +842,12 @@ final class VPhoneTCPForwarder: @unchecked Sendable {
// MARK: - Housekeeping
- private func reapIdle() {
- let now = Date()
- for (key, connection) in connections where now.timeIntervalSince(connection.lastActivity) >= Self.idleTimeout {
+ /// Internal, with `now`, so tests can run it as if time had passed.
+ func reapIdle(now: Date = Date()) {
+ dispatchPrecondition(condition: .onQueue(queue))
+ for (key, connection) in connections {
+ let limit = connection.state == .established ? Self.establishedIdleTimeout : Self.idleTimeout
+ guard now.timeIntervalSince(connection.lastActivity) >= limit else { continue }
close(connection)
connections[key] = nil
}
diff --git a/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneTCPForwarderTests.swift b/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneTCPForwarderTests.swift
index 7427848b..124a979c 100644
--- a/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneTCPForwarderTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneTCPForwarderTests.swift
@@ -129,6 +129,15 @@ struct VPhoneTCPForwarderTests {
queue.sync { forwarder.stop() }
}
+ /// Run the idle reaper as if `seconds` had passed with nothing sent.
+ func reap(after seconds: TimeInterval) {
+ queue.sync { forwarder.reapIdle(now: Date().addingTimeInterval(seconds)) }
+ }
+
+ var connectionCount: Int {
+ forwarder.connectionCount
+ }
+
/// Wait for something a background socket is responsible for.
///
/// Polls rather than sleeping a fixed time: the far end of these tests is
@@ -517,6 +526,53 @@ struct VPhoneTCPForwarderTests {
#expect(nudged, "nothing was ever sent twice, so a lost segment would stall forever")
}
+ // MARK: - Idle connections
+
+ /// An ssh session nobody types into sends nothing. Reaped after 120 s of
+ /// silence, it dropped two to four minutes after its user stopped typing.
+ @Test func `an established connection survives minutes of silence`() throws {
+ let server = try LoopbackServer { client, server in server.drain(client) }
+ defer { server.stop() }
+
+ let harness = Harness(guestPort: 51006, destinationPort: server.port)
+ harness.start()
+ defer { harness.stop() }
+
+ try handshake(harness)
+ #expect(harness.connectionCount == 1)
+
+ harness.reap(after: 30 * 60)
+ #expect(harness.connectionCount == 1, "an idle established connection was reaped after 30 minutes")
+
+ harness.reap(after: 3 * 3600)
+ #expect(harness.connectionCount == 0, "a connection silent for three hours was kept")
+ }
+
+ /// A handshake the guest never finished still goes after the short bound.
+ @Test func `a half-open connection is still reaped quickly`() throws {
+ let server = try LoopbackServer { client, server in server.drain(client) }
+ defer { server.stop() }
+
+ let harness = Harness(guestPort: 51007, destinationPort: server.port)
+ harness.start()
+ defer { harness.stop() }
+
+ harness.feed(VPhoneTCPSegment(
+ sourcePort: harness.guestPort,
+ destinationPort: server.port,
+ sequenceNumber: 1000,
+ acknowledgmentNumber: 0,
+ flags: VPhoneTCPFlags.syn,
+ windowSize: 65535,
+ maximumSegmentSize: 1460,
+ ))
+ try #require(harness.waitUntil("the SYN-ACK") { harness.ourISN != nil })
+ #expect(harness.connectionCount == 1)
+
+ harness.reap(after: 180)
+ #expect(harness.connectionCount == 0, "a handshake left unanswered for three minutes was kept")
+ }
+
// MARK: - Throughput
/// The send path should not be the limit. Not a benchmark -- a guard that a