rename handshakeInProgress to keyexchangeInProgress

Handshake is also a name of a message, which makes previous
name less clear.

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2023-09-12 11:33:17 +02:00
parent 03c363e18d
commit 4cf0ac30d0
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
1 changed files with 22 additions and 11 deletions

View File

@ -26,7 +26,7 @@ type
bindAddress*: Address ## UDP binding address
transp: DatagramTransport
pendingRequests: Table[AESGCMNonce, PendingRequest]
handshakeInProgress: HashSet[NodeId]
keyexchangeInProgress: HashSet[NodeId]
pendingRequestsByNode: Table[NodeId, seq[seq[byte]]]
codec*: Codec
rng: ref HmacDrbgContext
@ -84,17 +84,21 @@ proc sendMessage*(t: Transport, toNode: Node, message: seq[byte]) =
t.registerRequest(toNode, message, nonce)
t.send(toNode, data)
else:
if not (toNode.id in t.handshakeInProgress):
trace "Send message: send random", myport = t.bindAddress.port , dstId = toNode
# we don't have an encryption key for this target, so we should initiate keyexchange
if not (toNode.id in t.keyexchangeInProgress):
trace "Send message: send random to trigger Whoareyou", myport = t.bindAddress.port , dstId = toNode
t.registerRequest(toNode, message, nonce)
t.send(toNode, data)
t.handshakeInProgress.incl(toNode.id)
t.keyexchangeInProgress.incl(toNode.id)
trace "keyexchangeInProgress added", myport = t.bindAddress.port , dstId = toNode
sleepAsync(responseTimeout).addCallback() do(data: pointer):
t.handshakeInProgress.excl(toNode.id)
t.keyexchangeInProgress.excl(toNode.id)
trace "keyexchangeInProgress removed (timeout)", myport = t.bindAddress.port , dstId = toNode
else:
# delay sending this message until handshake, have to reencode once keys are clear
# delay sending this message until whoareyou is received and handshake is sent
# have to reencode once keys are clear
t.pendingRequestsByNode.mgetOrPut(toNode.id, newSeq[seq[byte]]()).add(message)
trace "Send message: Node with this id already has ongoing handshake, delaying packet",
trace "Send message: Node with this id already has ongoing keyexchage, delaying packet",
myport = t.bindAddress.port , dstId = toNode, qlen=t.pendingRequestsByNode[toNode.id].len
proc sendWhoareyou(t: Transport, toId: NodeId, a: Address,
@ -149,6 +153,12 @@ proc receive*(t: Transport, a: Address, packet: openArray[byte]) =
else:
trace "Not decryptable message packet received", myport = t.bindAddress.port,
srcId = packet.srcId, address = a
# If we already have a keyexchange in progress, we have a case of simultaneous cross-connect.
# We could try to decide here which should go on, but since we are on top of UDP, a more robust
# choice is to answer here and resolve conflicts in the next stage (reception of Whoareyou), or
# even later (reception of Handshake).
if packet.srcId in t.keyexchangeInProgress:
trace "cross-connect detected, still sending Whoareyou"
t.sendWhoareyou(packet.srcId, a, packet.requestNonce,
t.client.getNode(packet.srcId))
@ -170,10 +180,11 @@ proc receive*(t: Transport, a: Address, packet: openArray[byte]) =
toNode.pubkey
).expect("Valid handshake packet to encode")
trace "Send handshake message packet", dstId = toNode.id, address
trace "Send handshake message packet", myport = t.bindAddress.port, dstId = toNode.id, address
t.send(toNode, data)
# handshake ready, we can send queued packets
t.handshakeInProgress.excl(toNode.id)
# keyexchange ready, we can send queued packets
t.keyexchangeInProgress.excl(toNode.id)
trace "keyexchangeInProgress removed (finished)", myport = t.bindAddress.port, dstId = toNode.id, address
discard t.sendPending(toNode)
else:
@ -197,7 +208,7 @@ proc receive*(t: Transport, a: Address, packet: openArray[byte]) =
if t.client.addNode(node):
trace "Added new node to routing table after handshake", node, tablesize=t.client.nodesDiscovered()
# handshake finished, TODO: should this be inside the if above?
t.handshakeInProgress.excl(node.id)
t.keyexchangeInProgress.excl(node.id)
discard t.sendPending(node)
else:
trace "Packet decoding error", myport = t.bindAddress.port, error = decoded.error, address = a