diff options
author | irungentoo <irungentoo@gmail.com> | 2013-12-07 19:06:41 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-12-07 19:06:41 -0500 |
commit | 1c45e5993858a4fc0e85858975e3206a657b9f94 (patch) | |
tree | c79b883aeb3cfefba7f1a2d86cbe97fda887dd4c /docs/updates/DHT.md | |
parent | d078c5e9c2f1727502bce7db1b7de7e2fc4880ce (diff) |
Added/updated some basic docs and cleaned up some things.
Diffstat (limited to 'docs/updates/DHT.md')
-rw-r--r-- | docs/updates/DHT.md | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/docs/updates/DHT.md b/docs/updates/DHT.md new file mode 100644 index 00000000..79ec8c4b --- /dev/null +++ b/docs/updates/DHT.md | |||
@@ -0,0 +1,113 @@ | |||
1 | DHT protocol | ||
2 | ============ | ||
3 | |||
4 | Follows pretty much the principle of the torrent DHT: http://www.bittorrent.org/beps/bep_0005.html (READ IT) | ||
5 | |||
6 | But: | ||
7 | Vastly simplified packet format and encryption. | ||
8 | |||
9 | Boostrapping: | ||
10 | The first time you install the client we bootstrap it with a node on our servers.(bandwidth should not be a problem as the client only needs to be sent one reply.) | ||
11 | |||
12 | |||
13 | Basics | ||
14 | ------ | ||
15 | (All the numbers here are just guesses and are probably not optimal values) | ||
16 | |||
17 | client list: A list of node ids closest (mathematically see bittorrent doc) to ours matched with ip addresses + port number corresponding to that id and a timestamp containing the time or time since the client was successfully pinged. | ||
18 | |||
19 | "friends" list: A list containing the node_ids of all our "friends" or clients we want to connect to. | ||
20 | Also contains the ip addresses + port + node_ids + timestamp(of last ping like in the client list) of the 8 clients closest (mathematically see bittorrent doc) to each "friend" | ||
21 | |||
22 | One pinged lists: | ||
23 | -One for storing a list of ips along with their ping_ids and a timestamp for the ping requests | ||
24 | Entries in the pinged lists expire after 5 seconds. | ||
25 | If one of the lists becomes full, the expire rate reduces itself one second or the new ping takes the place of the oldest one. | ||
26 | |||
27 | |||
28 | Entries in client list and "friends" list expire after 300 seconds without ping response. | ||
29 | Each client stores a maximum of 32 entries in its client list. | ||
30 | Each client in the client list and "friends" list is pinged every 60 seconds. | ||
31 | Each client in the client list and "friends" list has a timestamp which denote the last time it was successfully pinged. | ||
32 | If the corresponding clients timestamp is more than 130 seconds old it is considered bad. | ||
33 | Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. | ||
34 | Send a get nodes request every 20 seconds to a random good node in the client list. | ||
35 | |||
36 | |||
37 | When a client receives any request from another | ||
38 | ----------------------------------------------- | ||
39 | -Respond to the request | ||
40 | -Ping request is replied to with with a ping response containing the same encrypted data | ||
41 | -Get nodes request is replied with a send nodes reply containing the same encrypted data and the good nodes from the client list and/or the "friends" list that are closest to the requested_node_id | ||
42 | |||
43 | -If the requesting client is not in the client list: | ||
44 | -If there are no bad clients in the list and the list is full: | ||
45 | -If the id of the other client is closer (mathematically see bittorrent doc) than at least one of the clients in the list or our "friends" list: | ||
46 | -Send a ping request to the client. | ||
47 | -if not forget about the client. | ||
48 | |||
49 | -If there are bad clients and/or the list isn't full: | ||
50 | -Send a ping request to the client | ||
51 | |||
52 | When a client receives a response | ||
53 | --------------------------------- | ||
54 | -Ping response | ||
55 | -If the node was previously pinged with a matching ping_id (check in the corresponding pinged list.) | ||
56 | -If the node is in the client list the matching client's timestamp is set to current time. | ||
57 | -If the node is in the "friends" list the matching client's timestamp is set to current time for every occurrence. | ||
58 | -If the node is not in the client list: | ||
59 | -If the list isn't full, add it to the list. | ||
60 | -If the list is full, the furthest away (mathematically see bittorrent doc) bad client is replaced by the new one. | ||
61 | -If the list is filled with good nodes replace the furthest client with it only if it is closer than the replaced node. | ||
62 | -for each friend in the "friends" list: | ||
63 | -If that friend's client list isn't full, add that client to it | ||
64 | -If that friend's client list contains bad clients, replace the furthest one with that client. | ||
65 | -If that friend's client list contains only good clients | ||
66 | -If the client is closer to the friend than one of the other clients, it replaces the farthest one | ||
67 | -If not, nothing happens. | ||
68 | |||
69 | -Send nodes | ||
70 | -If the ping_id matches what we sent previously (check in the corresponding pinged list.): | ||
71 | -Each node in the response is pinged. | ||
72 | |||
73 | |||
74 | |||
75 | |||
76 | |||
77 | Protocol | ||
78 | -------- | ||
79 | |||
80 | Node format: | ||
81 | ``` | ||
82 | [char array (node_id), length=32 bytes][ip (in network byte order), length=4 bytes][port (in network byte order), length=2 bytes][Padding , length=2 bytes] | ||
83 | ``` | ||
84 | see also: DHT.h (Node4_format struct) | ||
85 | |||
86 | IPv6 Node format: | ||
87 | see: DHT.h (Node_format struct) | ||
88 | |||
89 | Valid queries and Responses: | ||
90 | |||
91 | Ping(Request and response): | ||
92 | ``` | ||
93 | [byte with value: 00 for request, 01 for response][char array (client node_id), length=32 bytes][random 24 byte nonce][Encrypted with the nonce and private key of the sender: [random 8 byte (ping_id)]] | ||
94 | ``` | ||
95 | ping_id = a random integer, the response must contain the exact same number as the request | ||
96 | |||
97 | |||
98 | Get nodes (Request): | ||
99 | Packet contents: | ||
100 | ``` | ||
101 | [byte with value: 02][char array (client node_id), length=32 bytes][random 24 byte nonce][Encrypted with the nonce and private key of the sender:[char array: requested_node_id (node_id of which we want the ip), length=32 bytes][Encrypted data (must be sent back unmodified by in the response), length=NODES_ENCRYPTED_MESSAGE_LENGTH bytes]] | ||
102 | ``` | ||
103 | Valid replies: a send_nodes packet | ||
104 | |||
105 | Send_nodes (response (for ipv4 addresses)): | ||
106 | ``` | ||
107 | [byte with value: 03][char array (client node_id), length=32 bytes][random 24 byte nonce][Encrypted with the nonce and private key of the sender:[Nodes in node format, length=40 * (number of nodes (maximum of 8 nodes)) bytes][Encrypted data, length=NODES_ENCRYPTED_MESSAGE_LENGTH bytes]] | ||
108 | ``` | ||
109 | |||
110 | Send_nodes_IPv6 (response (for ipv6 addresses)): | ||
111 | ``` | ||
112 | [byte with value: 04][char array (client node_id), length=32 bytes][random 24 byte nonce][Encrypted with the nonce and private key of the sender:[Nodes in ipv6_node format, length=56 * (number of nodes (maximum of 8 nodes)) bytes][Encrypted data, length=NODES_ENCRYPTED_MESSAGE_LENGTH bytes]] | ||
113 | ``` | ||