Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional refactoring of PacketTunnelActor State; mostly placing clearly subordinate types under appropriate namespaces #6070

Merged
merged 4 commits into from
Apr 8, 2024
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 @@ -95,7 +95,7 @@ extension PacketTunnelActor {
private func mapConnectionState(
_ connState: State.ConnectionData,
reason: BlockedStateReason,
priorState: StatePriorToBlockedState
priorState: State.BlockingData.PriorState
) -> State.BlockingData {
State.BlockingData(
reason: reason,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extension PacketTunnelActor {
- Parameter keyPolicy: a reference to key policy held either in connection state or blocked state struct.
- Returns: `true` when the policy was modified, otherwise `false`.
*/
private func setCurrentKeyPolicy(_ keyPolicy: inout KeyPolicy) {
private func setCurrentKeyPolicy(_ keyPolicy: inout State.KeyPolicy) {
if case .usePrior = keyPolicy {
keyPolicy = .useCurrent
}
Expand Down
12 changes: 11 additions & 1 deletion ios/PacketTunnelCore/Actor/PacketTunnelActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ public actor PacketTunnelActor {
// MARK: -

extension PacketTunnelActor {
/// Describes the reason for reconnection request.
enum ReconnectReason {
/// Initiated by user.
case userInitiated

/// Initiated by tunnel monitor due to loss of connectivity.
/// Actor will increment the connection attempt counter before picking next relay.
case connectionLoss
}

/**
Start the tunnel.

Expand Down Expand Up @@ -295,7 +305,7 @@ extension PacketTunnelActor {
settings: Settings,
reason: ReconnectReason
) throws -> State.ConnectionData? {
var keyPolicy: KeyPolicy = .useCurrent
var keyPolicy: State.KeyPolicy = .useCurrent
var networkReachability = defaultPathObserver.defaultPath?.networkReachability ?? .undetermined
var lastKeyRotation: Date?

Expand Down
11 changes: 8 additions & 3 deletions ios/PacketTunnelCore/Actor/State+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import MullvadTypes
import WireGuardKitTypes

extension State {
/// Target state the actor should transition into upon request to either start (connect) or reconnect.
enum TargetStateForReconnect {
case reconnecting, connecting
}

/// Returns the target state to which the actor state should transition when requested to reconnect.
/// It returns `nil` when reconnection is not supported such as when already `.disconnecting` or `.disconnected` states.
var targetStateForReconnect: TargetStateForReconnect? {
Expand Down Expand Up @@ -149,7 +154,7 @@ extension State {
}
}

extension KeyPolicy {
extension State.KeyPolicy {
func logFormat() -> String {
switch self {
case .useCurrent:
Expand All @@ -160,8 +165,8 @@ extension KeyPolicy {
}
}

extension KeyPolicy: Equatable {
static func == (lhs: KeyPolicy, rhs: KeyPolicy) -> Bool {
extension State.KeyPolicy: Equatable {
static func == (lhs: State.KeyPolicy, rhs: State.KeyPolicy) -> Bool {
switch (lhs, rhs) {
case (.useCurrent, .useCurrent): true
case let (.usePrior(priorA, _), .usePrior(priorB, _)): priorA == priorB
Expand Down
45 changes: 16 additions & 29 deletions ios/PacketTunnelCore/Actor/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ enum State: Equatable {
case error(BlockingData)
}

/// Policy describing what WG key to use for tunnel communication.
enum KeyPolicy {
/// Use current key stored in device data.
case useCurrent

/// Use prior key until timer fires.
case usePrior(_ priorKey: PrivateKey, _ timerTask: AutoCancellingTask)
}

/// Enum describing network availability.
public enum NetworkReachability: Equatable, Codable {
case undetermined, reachable, unreachable
}

protocol StateAssociatedData {
var currentKey: PrivateKey? { get set }
var keyPolicy: KeyPolicy { get set }
var keyPolicy: State.KeyPolicy { get set }
var networkReachability: NetworkReachability { get set }
var lastKeyRotation: Date? { get set }
}

extension State {
/// Policy describing what WG key to use for tunnel communication.
enum KeyPolicy {
/// Use current key stored in device data.
case useCurrent

/// Use prior key until timer fires.
case usePrior(_ priorKey: PrivateKey, _ timerTask: AutoCancellingTask)
}

/// Data associated with states that hold connection data.
struct ConnectionData: Equatable, StateAssociatedData {
/// Current selected relay.
Expand Down Expand Up @@ -173,7 +173,7 @@ extension State {
public var recoveryTask: AutoCancellingTask?

/// Prior state of the actor before entering blocked state
public var priorState: StatePriorToBlockedState
public var priorState: PriorState
}
}

Expand Down Expand Up @@ -214,14 +214,11 @@ public enum BlockedStateReason: String, Codable, Equatable {
case unknown
}

/// Legal states that can precede error state.
enum StatePriorToBlockedState: Equatable {
case initial, connecting, connected, reconnecting
}

/// Target state the actor should transition into upon request to either start (connect) or reconnect.
enum TargetStateForReconnect {
case reconnecting, connecting
extension State.BlockingData {
/// Legal states that can precede error state.
enum PriorState: Equatable {
case initial, connecting, connected, reconnecting
}
}

/// Describes which relay the tunnel should connect to next.
Expand All @@ -235,13 +232,3 @@ public enum NextRelay: Equatable, Codable {
/// Use pre-selected relay.
case preSelected(SelectedRelay)
}

/// Describes the reason for reconnection request.
enum ReconnectReason {
/// Initiated by user.
case userInitiated

/// Initiated by tunnel monitor due to loss of connectivity.
/// Actor will increment the connection attempt counter before picking next relay.
case connectionLoss
}
Loading