Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Shut down on server-initiated HARD_RESET
Browse files Browse the repository at this point in the history
Session is stale and not recoverable (lame duck).
  • Loading branch information
keeshux committed Oct 24, 2018
1 parent 0b79ce4 commit b1a79d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion TunnelKit/Sources/AppExtension/TunnelKitProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ extension TunnelKitProvider {
}
} else if let se = error as? SessionError {
switch se {
case .negotiationTimeout, .pingTimeout:
case .negotiationTimeout, .pingTimeout, .staleSession:
return .timeout

case .badCredentials:
Expand Down
3 changes: 3 additions & 0 deletions TunnelKit/Sources/Core/SessionError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public enum SessionError: String, Error {

/// The server couldn't ping back before timeout.
case pingTimeout

/// The session reached a stale state and can't be recovered.
case staleSession
}

extension Error {
Expand Down
15 changes: 8 additions & 7 deletions TunnelKit/Sources/Core/SessionProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,6 @@ public class SessionProxy {
private func start() {
loopLink()
hardReset()

guard !keys.isEmpty else {
fatalError("Main loop must follow hard reset, keys are empty!")
}

loopNegotiation()
}

private func loopNegotiation() {
Expand Down Expand Up @@ -466,7 +460,10 @@ public class SessionProxy {
// deferStop(.shutdown, e)
// return
}
if (code == .softResetV1) && (negotiationKey.state != .softReset) {
if (code == .hardResetServerV2) && (negotiationKey.state != .hardReset) {
deferStop(.shutdown, SessionError.staleSession)
return
} else if (code == .softResetV1) && (negotiationKey.state != .softReset) {
softReset(isServerInitiated: true)
}

Expand Down Expand Up @@ -560,6 +557,10 @@ public class SessionProxy {

let payload = hardResetPayload() ?? Data()
negotiationKey.state = .hardReset
guard !keys.isEmpty else {
fatalError("Main loop must follow hard reset, keys are empty!")
}
loopNegotiation()
enqueueControlPackets(code: .hardResetClientV2, key: UInt8(negotiationKeyIdx), payload: payload)
}

Expand Down

0 comments on commit b1a79d6

Please sign in to comment.