From 4cf0ac30d020aefb3f9cbf765b656a3599d68387 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Tue, 12 Sep 2023 11:33:17 +0200 Subject: [PATCH] rename handshakeInProgress to keyexchangeInProgress Handshake is also a name of a message, which makes previous name less clear. Signed-off-by: Csaba Kiraly --- .../private/eth/p2p/discoveryv5/transport.nim | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/libp2pdht/private/eth/p2p/discoveryv5/transport.nim b/libp2pdht/private/eth/p2p/discoveryv5/transport.nim index c8ba063..a7c2bc6 100644 --- a/libp2pdht/private/eth/p2p/discoveryv5/transport.nim +++ b/libp2pdht/private/eth/p2p/discoveryv5/transport.nim @@ -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