Skip to content

Commit 26e40cb

Browse files
authored
discv5: NAT hole punching wire protocol (#225)
1 parent f8c7cad commit 26e40cb

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

discv5/discv5-theory.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ the distance to retrieve more nodes from adjacent k-buckets on `B`:
334334
Node `A` now sorts all received nodes by distance to the lookup target and proceeds by
335335
repeating the lookup procedure on another, closer node.
336336

337-
338337
[EIP-778]: ../enr.md
339338
[identity scheme]: ../enr.md#record-structure
340339
[handshake message packet]: ./discv5-wire.md#handshake-message-packet-flag--2

discv5/discv5-wire.md

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ the request.
4949

5050
## Packet Encoding
5151

52-
The protocol deals with three distinct kinds of packets:
52+
The protocol deals with four distinct kinds of packets:
5353

5454
- Ordinary message packets, which carry an encrypted/authenticated message.
55+
- Session message packets. These are identical to an ordinary message packets, but cannot
56+
trigger a handshake.
5557
- WHOAREYOU packets, which are sent when the recipient of an ordinary message packet
5658
cannot decrypt/authenticate the packet's message.
5759
- Handshake message packets, which are sent following WHOAREYOU. These packets establish a
@@ -115,6 +117,15 @@ For message packets, the `authdata` section is just the source node ID.
115117

116118
![message packet layout](./img/message-packet-layout.png)
117119

120+
### Session Message Packet (`flag = 3`)
121+
122+
The structure of this type is identical to an [ordinary message packet], apart from the
123+
different `flag` value.
124+
125+
Session packets are used to send a message that assumes an existing session. When a packet
126+
with this flag value is received and cannot be decrypted, the packet should be dropped,
127+
and not trigger a WHOAREYOU response.
128+
118129
### WHOAREYOU Packet (`flag = 1`)
119130

120131
In WHOAREYOU packets, the `authdata` section contains information for the identity
@@ -153,15 +164,25 @@ handshake packet.
153164

154165
![handshake packet layout](./img/handshake-packet-layout.png)
155166

156-
## Protocol Messages
167+
## Messages
168+
169+
Messages are the payloads of packets. A message is identified by its `message-type` and
170+
contain `message-data` specific to each type.
157171

158-
This section lists all defined messages which can be sent and received. The hexadecimal
159-
value in parentheses is the `message-type`.
172+
There are three classes of messages:
160173

161-
The first element of every `message-data` list is the request ID. `request-id` is an RLP
162-
byte array of length <= 8 bytes. For requests, this value is assigned by the requester.
163-
The recipient of a message must mirror the value in the `request-id` element of the
164-
response. The selection of appropriate values for request IDs is left to the implementation.
174+
- *Request* messages are sent as a [message packet] or [handshake message packet].
175+
- *Responses* are the answers to requests, and are sent using a [session message packet].
176+
- *Notifications* do not require a response, They are also sent as a [session message packet].
177+
178+
For request and response messages, the first element of every `message-data` list is the
179+
request ID. `request-id` is an RLP byte array of length <= 8 bytes. For requests, this
180+
value is assigned by the requester. The recipient of a message must mirror the value in
181+
the `request-id` element of the response. The selection of appropriate values for request
182+
IDs is left to the implementation.
183+
184+
All defined protocol messages are listed below. The hexadecimal value in parentheses is
185+
the `message-type`.
165186

166187
### PING Request (0x01)
167188

@@ -231,11 +252,39 @@ containing empty `response` data.
231252
TALKRESP is the response to TALKREQ. The `response` is a RLP byte array containing the
232253
response data.
233254

255+
### RELAYINIT Notification (0x07)
256+
257+
message-data = [initiator-enr, target-id, nonce]
258+
notification-type = 0x07
259+
target-id = 256-bit node ID of target
260+
nonce = uint96 -- nonce of timed out request
261+
262+
RELAYINIT is a notification sent from the initiator of a hole punch attempt to a relay.
263+
The sender sets the `initiator-enr` to its own ENR.
264+
265+
The relay looks up the ENR of `target-id` in its node table, and if it exists relays the
266+
`initiator-enr` and `nonce` to the target in a RELAYMSG notification.
267+
268+
### RELAYMSG Notification (0x08)
269+
270+
message-data = [initiator-enr, nonce]
271+
notification-type = 0x08
272+
nonce = uint96 -- nonce of timed out request
273+
274+
RELAYMSG is a notification from the relay in a hole punch attempt to the target. The
275+
receiver sends the `nonce` back to the initiator in a [WHOAREYOU packet] using the
276+
`initiator-enr` to address it.
277+
234278
## Test Vectors
235279

236280
A collection of test vectors for this specification can be found at
237281
[discv5 wire test vectors].
238282

283+
[ordinary message packet]: #ordinary-message-packet-flag--0
284+
[message packet]: #ordinary-message-packet-flag--0
285+
[WHOAREYOU packet]: #whoareyou-packet-flag--1
286+
[handshake message packet]: #handshake-message-packet-flag--2
287+
[session message packet]: #session-message-packet-flag--3
239288
[handshake section]: ./discv5-theory.md#handshake-steps
240289
[EIP-778]: ../enr.md
241290
[discv5 wire test vectors]: ./discv5-wire-test-vectors.md

0 commit comments

Comments
 (0)