summaryrefslogtreecommitdiff
path: root/toxcore/DHT.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/DHT.h')
-rw-r--r--toxcore/DHT.h64
1 files changed, 47 insertions, 17 deletions
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 3d2722f8..41fe99c9 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -24,7 +24,8 @@
24#ifndef DHT_H 24#ifndef DHT_H
25#define DHT_H 25#define DHT_H
26 26
27#include "net_crypto.h" 27#include "crypto_core.h"
28#include "network.h"
28 29
29/* Size of the client_id in bytes. */ 30/* Size of the client_id in bytes. */
30#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES 31#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
@@ -36,7 +37,7 @@
36#define LCLIENT_LIST 32 37#define LCLIENT_LIST 32
37 38
38/* The max number of nodes to send with send nodes. */ 39/* The max number of nodes to send with send nodes. */
39#define MAX_SENT_NODES 8 40#define MAX_SENT_NODES 4
40 41
41/* Ping timeout in seconds */ 42/* Ping timeout in seconds */
42#define PING_TIMEOUT 3 43#define PING_TIMEOUT 3
@@ -52,6 +53,8 @@
52/* Redefinitions of variables for safe transfer over wire. */ 53/* Redefinitions of variables for safe transfer over wire. */
53#define TOX_AF_INET 2 54#define TOX_AF_INET 2
54#define TOX_AF_INET6 10 55#define TOX_AF_INET6 10
56#define TOX_TCP_INET 130
57#define TOX_TCP_INET6 138
55 58
56/* The number of "fake" friends to add (for optimization purposes and so our paths for the onion part are more random) */ 59/* The number of "fake" friends to add (for optimization purposes and so our paths for the onion part are more random) */
57#define DHT_FAKE_FRIEND_NUMBER 4 60#define DHT_FAKE_FRIEND_NUMBER 4
@@ -128,16 +131,30 @@ typedef struct {
128 NAT nat; 131 NAT nat;
129} DHT_Friend; 132} DHT_Friend;
130 133
131/* this must be kept even if IP_Port is expanded: wire compatibility */ 134typedef struct __attribute__ ((__packed__))
132typedef struct { 135{
133 uint8_t client_id[CLIENT_ID_SIZE];
134 IP4_Port ip_port;
135} Node4_format;
136
137typedef struct {
138 uint8_t client_id[CLIENT_ID_SIZE]; 136 uint8_t client_id[CLIENT_ID_SIZE];
139 IP_Port ip_port; 137 IP_Port ip_port;
140} Node_format; 138}
139Node_format;
140
141/* Pack number of nodes into data of maxlength length.
142 *
143 * return length of packed nodes on success.
144 * return -1 on failure.
145 */
146int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t number);
147
148/* Unpack data of length into nodes of size max_num_nodes.
149 * Put the length of the data processed in processed_data_len.
150 * tcp_enabled sets if TCP nodes are expected (true) or not (false).
151 *
152 * return number of unpacked nodes on success.
153 * return -1 on failure.
154 */
155int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data,
156 uint16_t length, uint8_t tcp_enabled);
157
141 158
142/*----------------------------------------------------------------------------------*/ 159/*----------------------------------------------------------------------------------*/
143/* struct to store some shared keys so we don't have to regenerate them for each request. */ 160/* struct to store some shared keys so we don't have to regenerate them for each request. */
@@ -155,8 +172,15 @@ typedef struct {
155 172
156/*----------------------------------------------------------------------------------*/ 173/*----------------------------------------------------------------------------------*/
157 174
175typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data,
176 uint32_t len);
177
178typedef struct {
179 cryptopacket_handler_callback function;
180 void *object;
181} Cryptopacket_Handles;
182
158typedef struct { 183typedef struct {
159 Net_Crypto *c;
160 Networking_Core *net; 184 Networking_Core *net;
161 185
162 Client_data close_clientlist[LCLIENT_LIST]; 186 Client_data close_clientlist[LCLIENT_LIST];
@@ -164,7 +188,7 @@ typedef struct {
164 uint32_t close_bootstrap_times; 188 uint32_t close_bootstrap_times;
165 189
166 /* Note: this key should not be/is not used to transmit any sensitive materials */ 190 /* Note: this key should not be/is not used to transmit any sensitive materials */
167 uint8_t secret_symmetric_key[crypto_secretbox_KEYBYTES]; 191 uint8_t secret_symmetric_key[crypto_box_KEYBYTES];
168 /* DHT keypair */ 192 /* DHT keypair */
169 uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 193 uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
170 uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 194 uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
@@ -180,6 +204,8 @@ typedef struct {
180 struct Assoc *assoc; 204 struct Assoc *assoc;
181#endif 205#endif
182 uint64_t last_run; 206 uint64_t last_run;
207
208 Cryptopacket_Handles cryptopackethandlers[256];
183} DHT; 209} DHT;
184/*----------------------------------------------------------------------------------*/ 210/*----------------------------------------------------------------------------------*/
185 211
@@ -191,12 +217,12 @@ typedef struct {
191 */ 217 */
192void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, uint8_t *secret_key, uint8_t *client_id); 218void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, uint8_t *secret_key, uint8_t *client_id);
193 219
194/* Copy shared_key to decrypt DHT packet from client_id into shared_key 220/* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key
195 * for packets that we recieve. 221 * for packets that we receive.
196 */ 222 */
197void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, uint8_t *client_id); 223void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, uint8_t *client_id);
198 224
199/* Copy shared_key to decrypt DHT packet from client_id into shared_key 225/* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key
200 * for packets that we send. 226 * for packets that we send.
201 */ 227 */
202void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, uint8_t *client_id); 228void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, uint8_t *client_id);
@@ -251,7 +277,7 @@ int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2);
251/* Get the (maximum MAX_SENT_NODES) closest nodes to client_id we know 277/* Get the (maximum MAX_SENT_NODES) closest nodes to client_id we know
252 * and put them in nodes_list (must be MAX_SENT_NODES big). 278 * and put them in nodes_list (must be MAX_SENT_NODES big).
253 * 279 *
254 * sa_family = family (IPv4 or IPv6)? 280 * sa_family = family (IPv4 or IPv6) (0 if we don't care)?
255 * is_LAN = return some LAN ips (true or false) 281 * is_LAN = return some LAN ips (true or false)
256 * want_good = do we want tested nodes or not? (TODO) 282 * want_good = do we want tested nodes or not? (TODO)
257 * 283 *
@@ -315,6 +341,10 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
315 */ 341 */
316int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); 342int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length);
317 343
344/* Function to handle crypto packets.
345 */
346void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_callback cb, void *object);
347
318/* NAT PUNCHING FUNCTIONS */ 348/* NAT PUNCHING FUNCTIONS */
319 349
320/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. 350/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist.
@@ -341,7 +371,7 @@ void DHT_save(DHT *dht, uint8_t *data);
341int DHT_load(DHT *dht, uint8_t *data, uint32_t length); 371int DHT_load(DHT *dht, uint8_t *data, uint32_t length);
342 372
343/* Initialize DHT. */ 373/* Initialize DHT. */
344DHT *new_DHT(Net_Crypto *c); 374DHT *new_DHT(Networking_Core *net);
345 375
346void kill_DHT(DHT *dht); 376void kill_DHT(DHT *dht);
347 377