Scoop API

scoop:scoop.tls.asyncio:0.1.0

src/protocol.scoop

Selected production source · src/protocol.scoop

1package scoop.tls.asyncio
3import scoop.tls.*
4import scoop.tls.native.*
6internal fun advanceProtocol(state: TlsConnection): Boolean {
7 if (state.verificationBusy) return false
8 if (present(state.exclusive)) return advanceRequest(state, state.exclusive!!)
9 val read = state.reader
10 val write = state.writer
11 var changed = false
12 if (present(read) && read!!.waiting == RetryKind.Output) {
13 changed = advanceRequest(state, read!!)
14 if (read!!.waiting == RetryKind.Output || state.closing) return changed
15 }
16 if (present(write) && write!!.kind == RequestKind.Write) {
17 changed = advanceRequest(state, write!!) || changed
18 if (write!!.waiting != RetryKind.Finished || state.closing) return changed
19 }
20 if (present(read)) changed = advanceRequest(state, read!!) || changed
21 return changed
24private fun advanceRequest(state: TlsConnection, entry: TlsRequest): Boolean {
25 if (entry.selected) return false
26 val ready = when (entry.waiting) {
27 case Ready -> true
28 case Input -> state.inputEpoch != entry.lastInput
29 case Output -> state.outputEpoch != entry.lastOutput
30 case Verification -> false
31 case Finished -> false
32 }
33 if (!ready) return false
34 try {
35 val step = stepRequest(state, entry)
36 entry.progress = step.count
37 entry.lastInput = state.inputEpoch
38 entry.lastOutput = state.outputEpoch
39 entry.waiting = when (step.kind) {
40 case Done -> RetryKind.Finished
41 case WantRead -> RetryKind.Input
42 case WantWrite -> RetryKind.Output
43 case WantVerification -> RetryKind.Verification
44 case Closed -> {
45 if (entry.kind != RequestKind.Read)
46 throw TlsException("TLS protocol", TlsFailureKind.Protocol, 0UL, 0L, 0, "unexpected close_notify")
47 RetryKind.Finished
48 }
49 }
50 } catch (error: Throwable) { terminate(state, Some(entry), error) }
51 return true
54private fun stepRequest(state: TlsConnection, entry: TlsRequest): TlsStep {
55 @Unsafe { return when (entry.kind) {
56 case Handshake -> state.engine.handshake()
57 case Read -> state.engine.read(entry.borrow ?: throw IllegalStateException(Some("TLS read has no active borrow")))
58 case Write -> state.engine.write(entry.borrow ?: throw IllegalStateException(Some("TLS write has no active borrow")))
59 case Shutdown -> state.engine.shutdown()
60 case Flush -> throw IllegalStateException(Some("flush does not execute an SSL step"))
61 } }
64internal fun inputRequest(state: TlsConnection): TlsRequest? {
65 if (present(state.exclusive) && state.exclusive!!.waiting == RetryKind.Input) return state.exclusive
66 if (present(state.writer) && state.writer!!.waiting == RetryKind.Input) return state.writer
67 if (present(state.reader) && state.reader!!.waiting == RetryKind.Input) return state.reader
68 return None
71internal fun outputRequest(state: TlsConnection): TlsRequest {
72 if (present(state.exclusive)) return state.exclusive!!
73 if (present(state.writer)) return state.writer!!
74 return state.reader ?: throw IllegalStateException(Some("TLS output has no active request"))
77internal fun finishRequests(state: TlsConnection, actions: ArrayList<() -> Unit>): Boolean {
78 if (state.outputBusy || state.output.readableBytes != 0L || state.engine.pendingOutput != 0L) return false
79 var changed = false
80 for (entry in requests(state)) {
81 if (entry.waiting != RetryKind.Finished || entry.selected) continue
82 if (present(state.inputOwner) && state.inputOwner!! === entry) continue
83 if (entry.kind == RequestKind.Flush && !entry.flushed) continue
84 entry.selected = true
85 if (entry.kind == RequestKind.Shutdown) {
86 state.closeJob = entry.job
87 state.closing = true
88 return true
89 }
90 releaseRequest(state, entry, actions)
91 changed = true
92 }
93 return changed