summaryrefslogtreecommitdiff
path: root/core/DHT.h
diff options
context:
space:
mode:
Diffstat (limited to 'core/DHT.h')
-rw-r--r--core/DHT.h193
1 files changed, 0 insertions, 193 deletions
diff --git a/core/DHT.h b/core/DHT.h
deleted file mode 100644
index 6295581b..00000000
--- a/core/DHT.h
+++ /dev/null
@@ -1,193 +0,0 @@
1/* DHT.h
2 *
3 * An implementation of the DHT as seen in http://wiki.tox.im/index.php/DHT
4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#ifndef DHT_H
25#define DHT_H
26
27#include "net_crypto.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33
34/* size of the client_id in bytes */
35#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
36
37/* maximum number of clients stored per friend. */
38#define MAX_FRIEND_CLIENTS 8
39
40/* A list of the clients mathematically closest to ours. */
41#define LCLIENT_LIST 32
42
43/* The list of ip ports along with the ping_id of what we sent them and a timestamp */
44#define LPING_ARRAY 256 //NOTE Deprecated (doesn't do anything)
45
46#define LSEND_NODES_ARRAY LPING_ARRAY/2
47
48/*Maximum newly announced nodes to ping per TIME_TOPING seconds*/
49#define MAX_TOPING 16
50
51typedef struct {
52 uint8_t client_id[CLIENT_ID_SIZE];
53 IP_Port ip_port;
54 uint64_t timestamp;
55 uint64_t last_pinged;
56
57 /* Returned by this node. Either our friend or us */
58 IP_Port ret_ip_port;
59 uint64_t ret_timestamp;
60} Client_data;
61
62/*----------------------------------------------------------------------------------*/
63
64typedef struct {
65 uint8_t client_id[CLIENT_ID_SIZE];
66 Client_data client_list[MAX_FRIEND_CLIENTS];
67
68 /* time at which the last get_nodes request was sent. */
69 uint64_t lastgetnode;
70
71 /* Symetric NAT hole punching stuff */
72
73 /* 1 if currently hole punching, otherwise 0 */
74 uint8_t hole_punching;
75 uint32_t punching_index;
76 uint64_t punching_timestamp;
77 uint64_t recvNATping_timestamp;
78 uint64_t NATping_id;
79 uint64_t NATping_timestamp;
80} DHT_Friend;
81
82typedef struct {
83 uint8_t client_id[CLIENT_ID_SIZE];
84 IP_Port ip_port;
85} Node_format;
86
87typedef struct {
88 IP_Port ip_port;
89 uint64_t ping_id;
90 uint64_t timestamp;
91} Pinged;
92
93/*----------------------------------------------------------------------------------*/
94typedef struct {
95 Net_Crypto *c;
96 Client_data close_clientlist[LCLIENT_LIST];
97 DHT_Friend *friends_list;
98 uint16_t num_friends;
99 Pinged send_nodes[LSEND_NODES_ARRAY];
100 Node_format toping[MAX_TOPING];
101 uint64_t last_toping;
102 uint64_t close_lastgetnodes;
103 void *ping;
104} DHT;
105/*----------------------------------------------------------------------------------*/
106
107Client_data *DHT_get_close_list(DHT *dht);
108
109/* Add a new friend to the friends list
110 client_id must be CLIENT_ID_SIZE bytes long.
111 returns 0 if success
112 returns 1 if failure (friends list is full) */
113int DHT_addfriend(DHT *dht, uint8_t *client_id);
114
115/* Delete a friend from the friends list
116 client_id must be CLIENT_ID_SIZE bytes long.
117 returns 0 if success
118 returns 1 if failure (client_id not in friends list) */
119int DHT_delfriend(DHT *dht, uint8_t *client_id);
120
121/* Get ip of friend
122 client_id must be CLIENT_ID_SIZE bytes long.
123 ip must be 4 bytes long.
124 port must be 2 bytes long.
125 returns ip if success
126 returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.)
127 returns ip of 1 if friend is not in list. */
128IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id);
129
130/* Run this function at least a couple times per second (It's the main loop) */
131void do_DHT(DHT *dht);
132
133/* Use this function to bootstrap the client
134 Sends a get nodes request to the given node with ip port and public_key */
135void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key);
136
137/* Add nodes to the toping list
138 all nodes in this list are pinged every TIME_TOPING seconds
139 and are then removed from the list.
140 if the list is full the nodes farthest from our client_id are replaced
141 the purpose of this list is to enable quick integration of new nodes into the
142 network while preventing amplification attacks.
143 return 0 if node was added
144 return -1 if node was not added */
145int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port);
146
147/* ROUTING FUNCTIONS */
148
149/* send the given packet to node with client_id
150 returns -1 if failure */
151int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length);
152
153/* Send the following packet to everyone who tells us they are connected to friend_id
154 returns the number of nodes it sent the packet to */
155int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length);
156
157/* NAT PUNCHING FUNCTIONS */
158
159/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
160 ip_portlist must be at least MAX_FRIEND_CLIENTS big
161 returns the number of ips returned
162 returns -1 if no such friend*/
163int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id);
164
165/* SAVE/LOAD functions */
166
167/* get the size of the DHT (for saving) */
168uint32_t DHT_size(DHT *dht);
169
170/* save the DHT in data where data is an array of size DHT_size() */
171void DHT_save(DHT *dht, uint8_t *data);
172
173/* init DHT */
174DHT *new_DHT(Net_Crypto *c);
175
176void kill_DHT(DHT *dht);
177
178/* load the DHT from data of size size;
179 return -1 if failure
180 return 0 if success */
181int DHT_load(DHT *dht, uint8_t *data, uint32_t size);
182
183/* returns 0 if we are not connected to the DHT
184 returns 1 if we are */
185int DHT_isconnected(DHT *dht);
186
187void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id);
188
189#ifdef __cplusplus
190}
191#endif
192
193#endif