Description
Description of DHT packets
1. Overview
PingRequest and NodesRequest provide method to interact with Tox Networks.
-
Purpose of packet
- PingRequest : check if the node is alive
- NodesRequest : get nodes closest to me and my friends.
- NatPingRequest : check if my friend is behind NAT, then punch hole
-
Data types used
- Kbucket : contains nodes closest to me
- Bucket : contains nodes closest to my friend, nodes to bootstrap
2. PingRequest
-
When DHT node receives PingRequest, it responds with PingResponse and sends PingRequest.
-
When DHT node receives NodesRequest, it responds with NodesResponse and sends PingRequest.
-
Before DHT node sends PingRequest, it checks if the node is addable to close list
- addable : send PingRequest
- not addable: don't send PingRequest
3. PingResponse
- When DHT node receives PingResponse, it checks if the node is addable to close list
- addable : add node to close list and friend's close list
- not addable : don't add node to close list
4. NodesRequest
There are two timers interval
- 60 seconds interval : to ping nodes in close list and friend's close list
- 20 seconds interval : to get nodes close to me and my friends
- 20 seconds interval timer expires only 5 times after start-up.
There are two case sending NodesRequest
- on two timers expire
- bootstrapping
BootStrapping is checking nodes whether it is alive or not.
The nodes to bootstrap are nodes in NodesResponse and if
- the node is addable to close list of my Public Key or
- the node is addable to close list of my friend's Public Key
When DHT node receives NodesRequest, it responds with NodesResponse.
NodesResponse contains maximum 4 nodes in close list and friend's close list.
Maximum 4 nodes are closest to requested Public Key.
5. NodesResponse
When DHT node receives NodesResponse, it unpack nodes in packet's payload.
Unpacked nodes are added to
- if addable to close list, add
- if addable to close list of my friend, add
- if addable to bootstrap list, add
- if addable to bootstrap list of my friend, add
6. Replacing order of Kbucket and Bucket
Nodes in close list have two status
- Good : node is alive and 'alive' is meaning that last responding time is within 182 seconds.
- Bad : node is not alive, which means last responding time exceeds 182 seconds.
The old node in close list is replaced by new node order by
- Bad and farthest
- Good and farther than new node
- if there is no bad or no (good and father than new node), then new node is not addable
7. NatPingRequest
If a friend is not connected with me and friend is online, at every 3 seconds
- send NatPingRequest to all entry of close_nodes of the friend
Close nodes of a friend have SocketAddrs, if
- more than half of ip addresses are same and
- last time of receiving NatPingResponse is within 6 seconds and
- do_hole_punching variable == 1
are true then starts hole punching.
When DHT node receives NatPingRequest, it checks if Receiver PK is same with my PK
- if same PK, find Sender's PK in my friend's list
- if found, responds with NatPingResponse
- if not found, do nothing
- if not same, search DHT node's close list to find same PK
- if found, send NatPingRequest to the node
- if not found, do nothing
8. NatPingResponse
When DHT node receives NatPingResponse, it checks if Receiver PK is same with my PK
- if same PK, find Sender's PK in my friend's list
- if found, check if ping_id is same with ping_id saved
- if same, set do_hole_punching variable to 1
- if not same, do nothing
- if not found, do nothing
- if found, check if ping_id is same with ping_id saved
- if not same, search DHT node's close list to find same PK
- if found, send NatPingResponse to the node
- if not found, do nothing