add timeouts to connection/secio
This commit is contained in:
parent
107e71203d
commit
c6561b8851
|
@ -16,23 +16,27 @@ import peerinfo,
|
||||||
vbuffer
|
vbuffer
|
||||||
|
|
||||||
const DefaultReadSize*: uint = 64 * 1024
|
const DefaultReadSize*: uint = 64 * 1024
|
||||||
|
const DefaultRWTimeout*: Duration = 2.minutes
|
||||||
|
|
||||||
type
|
type
|
||||||
Connection* = ref object of LPStream
|
Connection* = ref object of LPStream
|
||||||
peerInfo*: PeerInfo
|
peerInfo*: PeerInfo
|
||||||
stream*: LPStream
|
stream*: LPStream
|
||||||
observedAddrs*: Multiaddress
|
observedAddrs*: Multiaddress
|
||||||
|
timeout*: Duration
|
||||||
|
|
||||||
InvalidVarintException = object of LPStreamError
|
InvalidVarintException = object of LPStreamError
|
||||||
|
|
||||||
proc newInvalidVarintException*(): ref InvalidVarintException =
|
proc newInvalidVarintException*(): ref InvalidVarintException =
|
||||||
newException(InvalidVarintException, "unable to prase varint")
|
newException(InvalidVarintException, "unable to prase varint")
|
||||||
|
|
||||||
proc newConnection*(stream: LPStream): Connection =
|
proc newConnection*(stream: LPStream,
|
||||||
|
timeout: Duration = DefaultRWTimeout): Connection =
|
||||||
## create a new Connection for the specified async reader/writer
|
## create a new Connection for the specified async reader/writer
|
||||||
new result
|
new result
|
||||||
result.stream = stream
|
result.stream = stream
|
||||||
result.closeEvent = newAsyncEvent()
|
result.closeEvent = newAsyncEvent()
|
||||||
|
result.timeout = timeout
|
||||||
|
|
||||||
# bind stream's close event to connection's close
|
# bind stream's close event to connection's close
|
||||||
# to ensure correct close propagation
|
# to ensure correct close propagation
|
||||||
|
@ -45,50 +49,50 @@ proc newConnection*(stream: LPStream): Connection =
|
||||||
asyncCheck this.close()
|
asyncCheck this.close()
|
||||||
|
|
||||||
method read*(s: Connection, n = -1): Future[seq[byte]] {.gcsafe.} =
|
method read*(s: Connection, n = -1): Future[seq[byte]] {.gcsafe.} =
|
||||||
s.stream.read(n)
|
wait(s.stream.read(n), s.timeout)
|
||||||
|
|
||||||
method readExactly*(s: Connection,
|
method readExactly*(s: Connection,
|
||||||
pbytes: pointer,
|
pbytes: pointer,
|
||||||
nbytes: int):
|
nbytes: int):
|
||||||
Future[void] {.gcsafe.} =
|
Future[void] {.gcsafe.} =
|
||||||
s.stream.readExactly(pbytes, nbytes)
|
wait(s.stream.readExactly(pbytes, nbytes), s.timeout)
|
||||||
|
|
||||||
method readLine*(s: Connection,
|
method readLine*(s: Connection,
|
||||||
limit = 0,
|
limit = 0,
|
||||||
sep = "\r\n"):
|
sep = "\r\n"):
|
||||||
Future[string] {.gcsafe.} =
|
Future[string] {.gcsafe.} =
|
||||||
s.stream.readLine(limit, sep)
|
wait(s.stream.readLine(limit, sep), s.timeout)
|
||||||
|
|
||||||
method readOnce*(s: Connection,
|
method readOnce*(s: Connection,
|
||||||
pbytes: pointer,
|
pbytes: pointer,
|
||||||
nbytes: int):
|
nbytes: int):
|
||||||
Future[int] {.gcsafe.} =
|
Future[int] {.gcsafe.} =
|
||||||
s.stream.readOnce(pbytes, nbytes)
|
wait(s.stream.readOnce(pbytes, nbytes), s.timeout)
|
||||||
|
|
||||||
method readUntil*(s: Connection,
|
method readUntil*(s: Connection,
|
||||||
pbytes: pointer,
|
pbytes: pointer,
|
||||||
nbytes: int,
|
nbytes: int,
|
||||||
sep: seq[byte]):
|
sep: seq[byte]):
|
||||||
Future[int] {.gcsafe.} =
|
Future[int] {.gcsafe.} =
|
||||||
s.stream.readUntil(pbytes, nbytes, sep)
|
wait(s.stream.readUntil(pbytes, nbytes, sep), s.timeout)
|
||||||
|
|
||||||
method write*(s: Connection,
|
method write*(s: Connection,
|
||||||
pbytes: pointer,
|
pbytes: pointer,
|
||||||
nbytes: int):
|
nbytes: int):
|
||||||
Future[void] {.gcsafe.} =
|
Future[void] {.gcsafe.} =
|
||||||
s.stream.write(pbytes, nbytes)
|
wait(s.stream.write(pbytes, nbytes), s.timeout)
|
||||||
|
|
||||||
method write*(s: Connection,
|
method write*(s: Connection,
|
||||||
msg: string,
|
msg: string,
|
||||||
msglen = -1):
|
msglen = -1):
|
||||||
Future[void] {.gcsafe.} =
|
Future[void] {.gcsafe.} =
|
||||||
s.stream.write(msg, msglen)
|
wait(s.stream.write(msg, msglen), s.timeout)
|
||||||
|
|
||||||
method write*(s: Connection,
|
method write*(s: Connection,
|
||||||
msg: seq[byte],
|
msg: seq[byte],
|
||||||
msglen = -1):
|
msglen = -1):
|
||||||
Future[void] {.gcsafe.} =
|
Future[void] {.gcsafe.} =
|
||||||
s.stream.write(msg, msglen)
|
wait(s.stream.write(msg, msglen), s.timeout)
|
||||||
|
|
||||||
method closed*(s: Connection): bool =
|
method closed*(s: Connection): bool =
|
||||||
if isNil(s.stream):
|
if isNil(s.stream):
|
||||||
|
|
|
@ -31,6 +31,7 @@ const
|
||||||
SecioExchanges = "P-256,P-384,P-521"
|
SecioExchanges = "P-256,P-384,P-521"
|
||||||
SecioCiphers = "TwofishCTR,AES-256,AES-128"
|
SecioCiphers = "TwofishCTR,AES-256,AES-128"
|
||||||
SecioHashes = "SHA256,SHA512"
|
SecioHashes = "SHA256,SHA512"
|
||||||
|
SecioRWTimeout = 2.minutes
|
||||||
|
|
||||||
type
|
type
|
||||||
Secio = ref object of Secure
|
Secio = ref object of Secure
|
||||||
|
@ -233,6 +234,7 @@ proc newSecureConnection(conn: Connection,
|
||||||
new result
|
new result
|
||||||
|
|
||||||
result.stream = conn
|
result.stream = conn
|
||||||
|
result.timeout = SecioRWTimeout
|
||||||
result.closeEvent = newAsyncEvent()
|
result.closeEvent = newAsyncEvent()
|
||||||
|
|
||||||
let i0 = if order < 0: 1 else: 0
|
let i0 = if order < 0: 1 else: 0
|
||||||
|
@ -331,7 +333,6 @@ proc handshake(s: Secio, conn: Connection): Future[SecureConnection] {.async.} =
|
||||||
remotePeerId = PeerID.init(remotePubkey)
|
remotePeerId = PeerID.init(remotePubkey)
|
||||||
|
|
||||||
# TODO: PeerID check against supplied PeerID
|
# TODO: PeerID check against supplied PeerID
|
||||||
|
|
||||||
let order = getOrder(remoteBytesPubkey, localNonce, localBytesPubkey,
|
let order = getOrder(remoteBytesPubkey, localNonce, localBytesPubkey,
|
||||||
remoteNonce)
|
remoteNonce)
|
||||||
trace "Remote proposal", schemes = remoteExchanges, ciphers = remoteCiphers,
|
trace "Remote proposal", schemes = remoteExchanges, ciphers = remoteCiphers,
|
||||||
|
@ -451,7 +452,7 @@ method init(s: Secio) {.gcsafe.} =
|
||||||
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
|
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
|
||||||
trace "handling connection"
|
trace "handling connection"
|
||||||
try:
|
try:
|
||||||
discard await s.handleConn(conn)
|
asyncCheck s.handleConn(conn)
|
||||||
trace "connection secured"
|
trace "connection secured"
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
if not conn.closed():
|
if not conn.closed():
|
||||||
|
|
Loading…
Reference in New Issue