Skip to content

Commit

Permalink
add a timeout during noise handshake (#294)
Browse files Browse the repository at this point in the history
* add a timeout during noise handshake

* noise hs timeout into const
  • Loading branch information
sinkingsugar authored Aug 4, 2020
1 parent 504e044 commit 5cd9354
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions libp2p/protocols/secure/noise.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const
NoiseSize = 32
MaxPlainSize = int(uint16.high - NoiseSize - ChaChaPolyTag.len)

HandshakeTimeout = 1.minutes

type
KeyPair = object
privateKey: Curve25519Key
Expand Down Expand Up @@ -265,14 +267,14 @@ template read_s: untyped =

proc receiveHSMessage(sconn: Connection): Future[seq[byte]] {.async.} =
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
trace "receiveHSMessage", size
if size == 0:
return

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

proc sendHSMessage(sconn: Connection; buf: openArray[byte]): Future[void] =
Expand Down

0 comments on commit 5cd9354

Please sign in to comment.