summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/Prevent_Tracking.txt4
-rw-r--r--toxcore/DHT.c7
-rw-r--r--toxcore/DHT.h16
-rw-r--r--toxcore/onion_announce.c146
-rw-r--r--toxcore/onion_announce.h41
5 files changed, 207 insertions, 7 deletions
diff --git a/docs/Prevent_Tracking.txt b/docs/Prevent_Tracking.txt
index 08c3f673..e4d19edc 100644
--- a/docs/Prevent_Tracking.txt
+++ b/docs/Prevent_Tracking.txt
@@ -110,8 +110,8 @@ data to route request packet:
110Data sent to us: 110Data sent to us:
111announce response packet: 111announce response packet:
112[uint8_t packet id (132)][nonce] 112[uint8_t packet id (132)][nonce]
113encrypted with the DHT private key of Node D, our long term public key and the nonce:[[(32 bytes) ping_id][Node_Format * (maximum of 8)]] 113encrypted with the DHT private key of Node D, the public key in the request and the nonce:[[(32 bytes) ping_id][Node_Format * (maximum of 8)]]
114(if the ping id is zero, it means the client id we are searching for is stored on ) 114(if the ping id is zero, it means the information to reach the client id we are searching for is stored on this node)
115 115
116data to route response packet: 116data to route response packet:
117[uint8_t packet id (134)][nonce]encrypted with that temporary private key and the nonce:[data] 117[uint8_t packet id (134)][nonce]encrypted with that temporary private key and the nonce:[data]
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 935f0246..07500032 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -36,9 +36,6 @@
36#include "misc_tools.h" 36#include "misc_tools.h"
37#include "util.h" 37#include "util.h"
38 38
39/* The max number of nodes to send with send nodes. */
40#define MAX_SENT_NODES 8
41
42/* The timeout after which a node is discarded completely. */ 39/* The timeout after which a node is discarded completely. */
43#define KILL_NODE_TIMEOUT 300 40#define KILL_NODE_TIMEOUT 300
44 41
@@ -366,8 +363,8 @@ static int get_somewhat_close_nodes(DHT *dht, uint8_t *client_id, Node_format *n
366 return num_nodes; 363 return num_nodes;
367} 364}
368 365
369static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN, 366int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN,
370 uint8_t want_good) 367 uint8_t want_good)
371{ 368{
372 if (!dht->assoc) 369 if (!dht->assoc)
373 return get_somewhat_close_nodes(dht, client_id, nodes_list, sa_family, is_LAN, want_good); 370 return get_somewhat_close_nodes(dht, client_id, nodes_list, sa_family, is_LAN, want_good);
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 25a257e3..c42bceb8 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -35,6 +35,9 @@
35/* A list of the clients mathematically closest to ours. */ 35/* A list of the clients mathematically closest to ours. */
36#define LCLIENT_LIST 32 36#define LCLIENT_LIST 32
37 37
38/* The max number of nodes to send with send nodes. */
39#define MAX_SENT_NODES 8
40
38/* Maximum newly announced nodes to ping per TIME_TOPING seconds. */ 41/* Maximum newly announced nodes to ping per TIME_TOPING seconds. */
39#define MAX_TOPING 16 42#define MAX_TOPING 16
40 43
@@ -204,6 +207,19 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port);
204 */ 207 */
205int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2); 208int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2);
206 209
210/* Get the (maximum MAX_SENT_NODES) closest nodes to client_id we know
211 * and put them in nodes_list (must be MAX_SENT_NODES big).
212 *
213 * sa_family = family (IPv4 or IPv6)?
214 * is_LAN = return some LAN ips (true or false)
215 * want_good = do we want tested nodes or not? (TODO)
216 *
217 * return the number of nodes returned.
218 *
219 */
220int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN,
221 uint8_t want_good);
222
207/* Run this function at least a couple times per second (It's the main loop). */ 223/* Run this function at least a couple times per second (It's the main loop). */
208void do_DHT(DHT *dht); 224void do_DHT(DHT *dht);
209 225
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
new file mode 100644
index 00000000..b6f78430
--- /dev/null
+++ b/toxcore/onion_announce.c
@@ -0,0 +1,146 @@
1/*
2* onion_announce.c -- Implementation of the announce part of docs/Prevent_Tracking.txt
3*
4* Copyright (C) 2013 Tox project All Rights Reserved.
5*
6* This file is part of Tox.
7*
8* Tox is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by
10* the Free Software Foundation, either version 3 of the License, or
11* (at your option) any later version.
12*
13* Tox is distributed in the hope that it will be useful,
14* but WITHOUT ANY WARRANTY; without even the implied warranty of
15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16* GNU General Public License for more details.
17*
18* You should have received a copy of the GNU General Public License
19* along with Tox. If not, see <http://www.gnu.org/licenses/>.
20*
21*/
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include "onion_announce.h"
27#include "LAN_discovery.h"
28#include "util.h"
29
30#define PING_ID_SIZE crypto_hash_sha256_BYTES
31#define PING_ID_TIMEOUT 10
32
33#define ANNOUNCE_REQUEST_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES + ONION_RETURN_3)
34
35uint8_t ping_id_zero[PING_ID_SIZE];
36
37/* Generate a ping_id and put it in ping_id */
38static void generate_ping_id(Onion_Announce *onion_a, uint64_t time, uint8_t *public_key, uint8_t *ret,
39 uint8_t *ping_id)
40{
41 time /= PING_ID_TIMEOUT;
42 uint8_t data[crypto_secretbox_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES + ONION_RETURN_3];
43 memcpy(data, onion_a->secret_bytes, crypto_secretbox_KEYBYTES);
44 memcpy(data + crypto_secretbox_KEYBYTES, &time, sizeof(time));
45 memcpy(data + crypto_secretbox_KEYBYTES + sizeof(time), public_key, crypto_box_PUBLICKEYBYTES);
46 memcpy(data + crypto_secretbox_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES, ret, ONION_RETURN_3);
47 crypto_hash_sha256(ping_id, data, sizeof(data));
48}
49
50/* add entry to entries list
51 *
52 * return 0 if failure
53 * return 1 if added
54 */
55static int add_to_entries(Onion_Announce *onion_a, uint8_t *public_key, uint8_t *ret)
56{
57
58 return 0;
59}
60
61/* check if public key is in entries list
62 *
63 * return 0 if no
64 * return 1 if yes
65 */
66static int in_entries(Onion_Announce *onion_a, uint8_t *public_key)
67{
68
69 return 0;
70}
71
72static int handle_announce_request(void *object, IP_Port source, uint8_t *packet, uint32_t length)
73{
74 Onion_Announce *onion_a = object;
75
76 if (length != ANNOUNCE_REQUEST_SIZE)
77 return 1;
78
79 uint8_t plain[PING_ID_SIZE + crypto_box_PUBLICKEYBYTES];
80 int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_a->dht->self_secret_key, packet + 1,
81 packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
82 PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES, plain);
83
84 if ((uint32_t)len != sizeof(plain))
85 return 1;
86
87 uint8_t ping_id1[PING_ID_SIZE];
88 generate_ping_id(onion_a, unix_time(), packet + 1 + crypto_box_NONCEBYTES,
89 packet + (ANNOUNCE_REQUEST_SIZE - ONION_RETURN_3), ping_id1);
90
91 uint8_t ping_id2[PING_ID_SIZE];
92 generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet + 1 + crypto_box_NONCEBYTES,
93 packet + (ANNOUNCE_REQUEST_SIZE - ONION_RETURN_3), ping_id2);
94
95 int stored = 0;
96
97 if (memcmp(ping_id1, plain, PING_ID_SIZE) == 0 || memcmp(ping_id2, plain, PING_ID_SIZE) == 0) {
98 stored = add_to_entries(onion_a, packet + 1 + crypto_box_NONCEBYTES,
99 packet + (ANNOUNCE_REQUEST_SIZE - ONION_RETURN_3));
100 } else {
101 stored = in_entries(onion_a, plain + PING_ID_SIZE);
102 }
103
104 Node_format nodes_list[MAX_SENT_NODES];
105 uint32_t num_nodes = get_close_nodes(onion_a->dht, plain + PING_ID_SIZE, nodes_list, source.ip.family,
106 LAN_ip(source.ip) == 0, 1);
107
108 uint8_t nonce[crypto_box_NONCEBYTES];
109 new_nonce(nonce);
110
111 return 0;
112}
113
114static int handle_data_request(void *object, IP_Port source, uint8_t *packet, uint32_t length)
115{
116 Onion_Announce *onion_a = object;
117
118 return 0;
119}
120
121Onion_Announce *new_onion_announce(DHT *dht)
122{
123 if (dht == NULL)
124 return NULL;
125
126 Onion_Announce *onion_a = calloc(1, sizeof(Onion_Announce));
127
128 if (onion_a == NULL)
129 return NULL;
130
131 onion_a->dht = dht;
132 onion_a->net = dht->c->lossless_udp->net;
133 new_symmetric_key(onion_a->secret_bytes);
134
135 networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_announce_request, onion_a);
136 networking_registerhandler(onion_a->net, NET_PACKET_ONION_DATA_REQUEST, &handle_data_request, onion_a);
137
138 return onion_a;
139}
140
141void kill_onion_announce(Onion_Announce *onion_a)
142{
143 networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, NULL, NULL);
144 networking_registerhandler(onion_a->net, NET_PACKET_ONION_DATA_REQUEST, NULL, NULL);
145 free(onion_a);
146}
diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h
new file mode 100644
index 00000000..2b36d35c
--- /dev/null
+++ b/toxcore/onion_announce.h
@@ -0,0 +1,41 @@
1/*
2* onion_announce.h -- Implementation of the announce part of docs/Prevent_Tracking.txt
3*
4* Copyright (C) 2013 Tox project All Rights Reserved.
5*
6* This file is part of Tox.
7*
8* Tox is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by
10* the Free Software Foundation, either version 3 of the License, or
11* (at your option) any later version.
12*
13* Tox is distributed in the hope that it will be useful,
14* but WITHOUT ANY WARRANTY; without even the implied warranty of
15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16* GNU General Public License for more details.
17*
18* You should have received a copy of the GNU General Public License
19* along with Tox. If not, see <http://www.gnu.org/licenses/>.
20*
21*/
22
23#include "onion.h"
24
25#define ONION_ANNOUNCE_MAX_ENTRIES 32
26typedef struct {
27 DHT *dht;
28 Networking_Core *net;
29 struct {
30 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
31 uint8_t ret[ONION_RETURN_3];
32 } entries[ONION_ANNOUNCE_MAX_ENTRIES];
33 /* This is crypto_secretbox_KEYBYTES long just so we can use new_symmetric_key() to fill it */
34 uint8_t secret_bytes[crypto_secretbox_KEYBYTES];
35} Onion_Announce;
36
37
38
39Onion_Announce *new_onion_announce(DHT *dht);
40
41void kill_onion_announce(Onion_Announce *onion_a);