add a timeout during noise handshake (#294)

* add a timeout during noise handshake

* noise hs timeout into const
This commit is contained in:
Giovanni Petrantoni 2020-08-04 17:04:16 +09:00 committed by GitHub
parent 504e0444d3
commit 5cd93540fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 2 deletions

View File

@ -37,6 +37,8 @@ const
NoiseSize = 32 NoiseSize = 32
MaxPlainSize = int(uint16.high - NoiseSize - ChaChaPolyTag.len) MaxPlainSize = int(uint16.high - NoiseSize - ChaChaPolyTag.len)
HandshakeTimeout = 1.minutes
type type
KeyPair = object KeyPair = object
privateKey: Curve25519Key privateKey: Curve25519Key
@ -265,14 +267,14 @@ template read_s: untyped =
proc receiveHSMessage(sconn: Connection): Future[seq[byte]] {.async.} = proc receiveHSMessage(sconn: Connection): Future[seq[byte]] {.async.} =
var besize: array[2, byte] var besize: array[2, byte]
await sconn.readExactly(addr besize[0], besize.len) await sconn.readExactly(addr besize[0], besize.len).wait(HandshakeTimeout)
let size = uint16.fromBytesBE(besize).int let size = uint16.fromBytesBE(besize).int
trace "receiveHSMessage", size trace "receiveHSMessage", size
if size == 0: if size == 0:
return return
var buffer = newSeq[byte](size) var buffer = newSeq[byte](size)
await sconn.readExactly(addr buffer[0], buffer.len) await sconn.readExactly(addr buffer[0], buffer.len).wait(HandshakeTimeout)
return buffer return buffer
proc sendHSMessage(sconn: Connection; buf: openArray[byte]): Future[void] = proc sendHSMessage(sconn: Connection; buf: openArray[byte]): Future[void] =