2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
## `Ping <https://docs.libp2p.io/concepts/protocols/#ping>`_ protocol implementation
|
2021-06-08 16:53:45 +00:00
|
|
|
|
2022-08-03 11:33:19 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-01-05 15:27:33 +00:00
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
import chronos, chronicles
|
|
|
|
import bearssl/[rand, hash]
|
2021-06-08 16:53:45 +00:00
|
|
|
import ../protobuf/minprotobuf,
|
|
|
|
../peerinfo,
|
|
|
|
../stream/connection,
|
|
|
|
../peerid,
|
|
|
|
../crypto/crypto,
|
|
|
|
../multiaddress,
|
|
|
|
../protocols/protocol,
|
2022-07-01 18:19:57 +00:00
|
|
|
../utility,
|
2021-06-08 16:53:45 +00:00
|
|
|
../errors
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
export chronicles, rand, connection
|
|
|
|
|
2021-06-08 16:53:45 +00:00
|
|
|
logScope:
|
|
|
|
topics = "libp2p ping"
|
|
|
|
|
|
|
|
const
|
|
|
|
PingCodec* = "/ipfs/ping/1.0.0"
|
|
|
|
PingSize = 32
|
|
|
|
|
|
|
|
type
|
|
|
|
PingError* = object of LPError
|
2022-07-01 18:19:57 +00:00
|
|
|
WrongPingAckError* = object of PingError
|
2021-06-08 16:53:45 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
PingHandler* {.public.} = proc (
|
2021-09-08 09:07:46 +00:00
|
|
|
peer: PeerId):
|
2021-06-08 16:53:45 +00:00
|
|
|
Future[void]
|
|
|
|
{.gcsafe, raises: [Defect].}
|
|
|
|
|
|
|
|
Ping* = ref object of LPProtocol
|
|
|
|
pingHandler*: PingHandler
|
2022-06-16 08:08:52 +00:00
|
|
|
rng: ref HmacDrbgContext
|
2021-06-08 16:53:45 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc new*(T: typedesc[Ping], handler: PingHandler = nil, rng: ref HmacDrbgContext = newRng()): T {.public.} =
|
2021-06-08 16:53:45 +00:00
|
|
|
let ping = Ping(pinghandler: handler, rng: rng)
|
|
|
|
ping.init()
|
|
|
|
ping
|
|
|
|
|
|
|
|
method init*(p: Ping) =
|
|
|
|
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
|
|
|
|
try:
|
|
|
|
trace "handling ping", conn
|
|
|
|
var buf: array[PingSize, byte]
|
|
|
|
await conn.readExactly(addr buf[0], PingSize)
|
|
|
|
trace "echoing ping", conn
|
2021-10-25 08:26:32 +00:00
|
|
|
await conn.write(@buf)
|
2021-06-08 16:53:45 +00:00
|
|
|
if not isNil(p.pingHandler):
|
2021-09-08 09:07:46 +00:00
|
|
|
await p.pingHandler(conn.peerId)
|
2021-06-08 16:53:45 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception in ping handler", exc = exc.msg, conn
|
|
|
|
|
|
|
|
p.handler = handle
|
|
|
|
p.codec = PingCodec
|
|
|
|
|
|
|
|
proc ping*(
|
|
|
|
p: Ping,
|
|
|
|
conn: Connection,
|
2022-07-01 18:19:57 +00:00
|
|
|
): Future[Duration] {.async, gcsafe, public.} =
|
|
|
|
## Sends ping to `conn`, returns the delay
|
2021-06-08 16:53:45 +00:00
|
|
|
|
|
|
|
trace "initiating ping", conn
|
|
|
|
|
|
|
|
var
|
|
|
|
randomBuf: array[PingSize, byte]
|
|
|
|
resultBuf: array[PingSize, byte]
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
hmacDrbgGenerate(p.rng[], randomBuf)
|
2021-06-08 16:53:45 +00:00
|
|
|
|
|
|
|
let startTime = Moment.now()
|
|
|
|
|
|
|
|
trace "sending ping", conn
|
2021-10-25 08:26:32 +00:00
|
|
|
await conn.write(@randomBuf)
|
2021-06-08 16:53:45 +00:00
|
|
|
|
|
|
|
await conn.readExactly(addr resultBuf[0], PingSize)
|
|
|
|
|
|
|
|
let responseDur = Moment.now() - startTime
|
|
|
|
|
|
|
|
trace "got ping response", conn, responseDur
|
|
|
|
|
|
|
|
for i in 0..<randomBuf.len:
|
|
|
|
if randomBuf[i] != resultBuf[i]:
|
|
|
|
raise newException(WrongPingAckError, "Incorrect ping data from peer!")
|
|
|
|
|
|
|
|
trace "valid ping response", conn
|
|
|
|
return responseDur
|