summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c88
-rw-r--r--toxcore/DHT.h12
-rw-r--r--toxcore/crypto_core.h3
-rw-r--r--toxcore/network.c16
-rw-r--r--toxcore/network.h11
-rw-r--r--toxcore/onion.c36
-rw-r--r--toxcore/onion_client.c6
-rw-r--r--toxcore/onion_client.h2
8 files changed, 61 insertions, 113 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index d28c1ecf..deb71864 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -164,12 +164,17 @@ void to_net_family(IP *ip)
164 ip->family = TOX_AF_INET6; 164 ip->family = TOX_AF_INET6;
165} 165}
166 166
167void to_host_family(IP *ip) 167int to_host_family(IP *ip)
168{ 168{
169 if (ip->family == TOX_AF_INET) 169 if (ip->family == TOX_AF_INET) {
170 ip->family = AF_INET; 170 ip->family = AF_INET;
171 else if (ip->family == TOX_AF_INET6) 171 return 0;
172 } else if (ip->family == TOX_AF_INET6) {
172 ip->family = AF_INET6; 173 ip->family = AF_INET6;
174 return 0;
175 } else {
176 return -1;
177 }
173} 178}
174 179
175/* Pack number of nodes into data of maxlength length. 180/* Pack number of nodes into data of maxlength length.
@@ -2082,82 +2087,6 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
2082 return count; 2087 return count;
2083} 2088}
2084 2089
2085/* Put a random node from list of list_size in node. LAN_ok is 1 if LAN ips are ok, 0 if we don't want them. */
2086static int random_node_fromlist(Client_data *list, uint16_t list_size, Node_format *node, uint8_t LAN_ok)
2087{
2088 uint32_t i;
2089 uint32_t num_nodes = 0;
2090 Client_data *client_list[list_size * 2];
2091 IPPTsPng *assoc_list[list_size * 2];
2092
2093 for (i = 0; i < list_size; i++) {
2094 /* If node is not dead. */
2095 Client_data *client = &list[i];
2096 IPPTsPng *assoc;
2097 uint32_t a;
2098
2099 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) {
2100 /* If node is good. */
2101 if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) {
2102 if (!LAN_ok) {
2103 if (LAN_ip(assoc->ip_port.ip) == 0)
2104 continue;
2105 }
2106
2107 client_list[num_nodes] = client;
2108 assoc_list[num_nodes] = assoc;
2109 ++num_nodes;
2110 }
2111 }
2112 }
2113
2114 if (num_nodes == 0)
2115 return -1;
2116
2117 uint32_t rand_node = rand() % num_nodes;
2118 node->ip_port = assoc_list[rand_node]->ip_port;
2119 memcpy(node->client_id, client_list[rand_node]->client_id, CLIENT_ID_SIZE);
2120 return 0;
2121}
2122
2123/* Put up to max_num random nodes in nodes.
2124 *
2125 * return the number of nodes.
2126 *
2127 * NOTE:this is used to pick nodes for paths.
2128 *
2129 * TODO: remove the LAN stuff from this.
2130 */
2131uint16_t random_nodes_path(const DHT *dht, Node_format *nodes, uint16_t max_num)
2132{
2133 if (max_num == 0)
2134 return 0;
2135
2136 if (dht->num_friends == 0)
2137 return 0;
2138
2139 uint16_t count = 0;
2140 uint16_t list_size = 0;
2141 uint32_t i;
2142
2143 for (i = 0; i < max_num; ++i) {
2144 Client_data *list = NULL;
2145 uint16_t rand_num = rand() % (dht->num_friends);
2146 list = dht->friends_list[rand_num].client_list;
2147 list_size = MAX_FRIEND_CLIENTS;
2148
2149 uint8_t LAN_ok = 1;
2150
2151 if (count != 0 && LAN_ip(nodes[0].ip_port.ip) != 0)
2152 LAN_ok = 0;
2153
2154 if (random_node_fromlist(list, list_size, &nodes[count], LAN_ok) == 0)
2155 ++count;
2156 }
2157
2158 return count;
2159}
2160
2161void do_hardening(DHT *dht) 2090void do_hardening(DHT *dht)
2162{ 2091{
2163 uint32_t i; 2092 uint32_t i;
@@ -2331,7 +2260,6 @@ void kill_DHT(DHT *dht)
2331 kill_Assoc(dht->assoc); 2260 kill_Assoc(dht->assoc);
2332#endif 2261#endif
2333 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, NULL, NULL); 2262 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, NULL, NULL);
2334 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES, NULL, NULL);
2335 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL); 2263 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL);
2336 cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL); 2264 cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL);
2337 cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL); 2265 cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL);
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 35000c87..547f3887 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -65,7 +65,9 @@
65 65
66/* Functions to transfer ips safely across wire. */ 66/* Functions to transfer ips safely across wire. */
67void to_net_family(IP *ip); 67void to_net_family(IP *ip);
68void to_host_family(IP *ip); 68
69/* return 0 on success, -1 on failure. */
70int to_host_family(IP *ip);
69 71
70typedef struct { 72typedef struct {
71 IP_Port ip_port; 73 IP_Port ip_port;
@@ -324,14 +326,6 @@ int get_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes
324 */ 326 */
325uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num); 327uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num);
326 328
327/* Put up to max_num random nodes in nodes.
328 *
329 * return the number of nodes.
330 *
331 * NOTE:this is used to pick nodes for paths.
332 */
333uint16_t random_nodes_path(const DHT *dht, Node_format *nodes, uint16_t max_num);
334
335/* Run this function at least a couple times per second (It's the main loop). */ 329/* Run this function at least a couple times per second (It's the main loop). */
336void do_DHT(DHT *dht); 330void do_DHT(DHT *dht);
337 331
diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h
index fb66e724..58bdc2f3 100644
--- a/toxcore/crypto_core.h
+++ b/toxcore/crypto_core.h
@@ -123,9 +123,6 @@ void new_nonce(uint8_t *nonce);
123#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */ 123#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */
124#define CRYPTO_PACKET_HARDENING 48 /* Hardening crypto packet ID. */ 124#define CRYPTO_PACKET_HARDENING 48 /* Hardening crypto packet ID. */
125#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ 125#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */
126#define CRYPTO_PACKET_GROUP_CHAT_GET_NODES 48 /* Group chat get Nodes packet */
127#define CRYPTO_PACKET_GROUP_CHAT_SEND_NODES 49 /* Group chat send Nodes packet */
128#define CRYPTO_PACKET_GROUP_CHAT_BROADCAST 50 /* Group chat broadcast packet */
129 126
130/* Create a request to peer. 127/* Create a request to peer.
131 * send_public_key and send_secret_key are the pub/secret keys of the sender. 128 * send_public_key and send_secret_key are the pub/secret keys of the sender.
diff --git a/toxcore/network.c b/toxcore/network.c
index 5539de6b..7a528f4f 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -763,10 +763,15 @@ void ip_pack(uint8_t *data, const IP *source)
763 memcpy(data + 1, &source->ip6, SIZE_IP6); 763 memcpy(data + 1, &source->ip6, SIZE_IP6);
764} 764}
765 765
766void ip_unpack(IP *target, const uint8_t *data) 766/* return 0 on success, -1 on failure. */
767int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size)
767{ 768{
769 if (data_size < (1 + SIZE_IP6))
770 return -1;
771
768 target->family = data[0]; 772 target->family = data[0];
769 memcpy(&target->ip6, data + 1, SIZE_IP6); 773 memcpy(&target->ip6, data + 1, SIZE_IP6);
774 return 0;
770} 775}
771 776
772void ipport_pack(uint8_t *data, const IP_Port *source) 777void ipport_pack(uint8_t *data, const IP_Port *source)
@@ -775,10 +780,15 @@ void ipport_pack(uint8_t *data, const IP_Port *source)
775 memcpy(data + SIZE_IP, &source->port, SIZE_PORT); 780 memcpy(data + SIZE_IP, &source->port, SIZE_PORT);
776} 781}
777 782
778void ipport_unpack(IP_Port *target, const uint8_t *data) 783/* return 0 on success, -1 on failure. */
784int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size)
779{ 785{
780 ip_unpack(&target->ip, data); 786 if (data_size < (SIZE_IP + SIZE_PORT))
787 return -1;
788
789 ip_unpack(&target->ip, data, data_size);
781 memcpy(&target->port, data + SIZE_IP, SIZE_PORT); 790 memcpy(&target->port, data + SIZE_IP, SIZE_PORT);
791 return 0;
782} 792}
783 793
784/* ip_ntoa 794/* ip_ntoa
diff --git a/toxcore/network.h b/toxcore/network.h
index 5f9af490..49d51d20 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -97,7 +97,6 @@ typedef int sock_t;
97#define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ 97#define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */
98#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ 98#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */
99#define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ 99#define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */
100#define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */
101#define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */ 100#define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */
102#define NET_PACKET_COOKIE_REQUEST 24 /* Cookie request packet */ 101#define NET_PACKET_COOKIE_REQUEST 24 /* Cookie request packet */
103#define NET_PACKET_COOKIE_RESPONSE 25 /* Cookie response packet */ 102#define NET_PACKET_COOKIE_RESPONSE 25 /* Cookie response packet */
@@ -250,12 +249,14 @@ void ipport_copy(IP_Port *target, const IP_Port *source);
250 249
251/* packs IP into data, writes SIZE_IP bytes to data */ 250/* packs IP into data, writes SIZE_IP bytes to data */
252void ip_pack(uint8_t *data, const IP *source); 251void ip_pack(uint8_t *data, const IP *source);
253/* unpacks IP from data, reads SIZE_IP bytes from data */ 252/* unpacks IP from data, reads SIZE_IP bytes from data
254void ip_unpack(IP *target, const uint8_t *data); 253 return 0 on success, -1 on failure. */
254int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size);
255/* packs IP_Port into data, writes SIZE_IPPORT bytes to data */ 255/* packs IP_Port into data, writes SIZE_IPPORT bytes to data */
256void ipport_pack(uint8_t *data, const IP_Port *source); 256void ipport_pack(uint8_t *data, const IP_Port *source);
257/* unpacks IP_Port from data, reads SIZE_IPPORT bytes to data */ 257/* unpacks IP_Port from data, reads SIZE_IPPORT bytes to data
258void ipport_unpack(IP_Port *target, const uint8_t *data); 258 return 0 on success, -1 on failure. */
259int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size);
259 260
260/* 261/*
261 * addr_resolve(): 262 * addr_resolve():
diff --git a/toxcore/onion.c b/toxcore/onion.c
index 0982a2b0..b444e02a 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -293,8 +293,12 @@ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port
293 return 1; 293 return 1;
294 294
295 IP_Port send_to; 295 IP_Port send_to;
296 ipport_unpack(&send_to, plain); 296
297 to_host_family(&send_to.ip); 297 if (ipport_unpack(&send_to, plain, len) == -1)
298 return 1;
299
300 if (to_host_family(&send_to.ip) == -1)
301 return 1;
298 302
299 uint8_t ip_port[SIZE_IPPORT]; 303 uint8_t ip_port[SIZE_IPPORT];
300 ipport_pack(ip_port, &source); 304 ipport_pack(ip_port, &source);
@@ -342,8 +346,12 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui
342 return 1; 346 return 1;
343 347
344 IP_Port send_to; 348 IP_Port send_to;
345 ipport_unpack(&send_to, plain); 349
346 to_host_family(&send_to.ip); 350 if (ipport_unpack(&send_to, plain, len) == -1)
351 return 1;
352
353 if (to_host_family(&send_to.ip) == -1)
354 return 1;
347 355
348 uint8_t data[ONION_MAX_PACKET_SIZE]; 356 uint8_t data[ONION_MAX_PACKET_SIZE];
349 data[0] = NET_PACKET_ONION_SEND_2; 357 data[0] = NET_PACKET_ONION_SEND_2;
@@ -391,8 +399,12 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui
391 return 1; 399 return 1;
392 400
393 IP_Port send_to; 401 IP_Port send_to;
394 ipport_unpack(&send_to, plain); 402
395 to_host_family(&send_to.ip); 403 if (ipport_unpack(&send_to, plain, len) == -1)
404 return 1;
405
406 if (to_host_family(&send_to.ip) == -1)
407 return 1;
396 408
397 uint8_t data[ONION_MAX_PACKET_SIZE]; 409 uint8_t data[ONION_MAX_PACKET_SIZE];
398 memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT); 410 memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT);
@@ -437,7 +449,9 @@ static int handle_recv_3(void *object, IP_Port source, const uint8_t *packet, ui
437 return 1; 449 return 1;
438 450
439 IP_Port send_to; 451 IP_Port send_to;
440 ipport_unpack(&send_to, plain); 452
453 if (ipport_unpack(&send_to, plain, len) == -1)
454 return 1;
441 455
442 uint8_t data[ONION_MAX_PACKET_SIZE]; 456 uint8_t data[ONION_MAX_PACKET_SIZE];
443 data[0] = NET_PACKET_ONION_RECV_2; 457 data[0] = NET_PACKET_ONION_RECV_2;
@@ -471,7 +485,9 @@ static int handle_recv_2(void *object, IP_Port source, const uint8_t *packet, ui
471 return 1; 485 return 1;
472 486
473 IP_Port send_to; 487 IP_Port send_to;
474 ipport_unpack(&send_to, plain); 488
489 if (ipport_unpack(&send_to, plain, len) == -1)
490 return 1;
475 491
476 uint8_t data[ONION_MAX_PACKET_SIZE]; 492 uint8_t data[ONION_MAX_PACKET_SIZE];
477 data[0] = NET_PACKET_ONION_RECV_1; 493 data[0] = NET_PACKET_ONION_RECV_1;
@@ -505,7 +521,9 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui
505 return 1; 521 return 1;
506 522
507 IP_Port send_to; 523 IP_Port send_to;
508 ipport_unpack(&send_to, plain); 524
525 if (ipport_unpack(&send_to, plain, len) == -1)
526 return 1;
509 527
510 uint16_t data_len = length - (1 + RETURN_1); 528 uint16_t data_len = length - (1 + RETURN_1);
511 529
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 2e39762f..9153216d 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -133,8 +133,8 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
133 133
134 //if (DHT_non_lan_connected(onion_c->dht)) { 134 //if (DHT_non_lan_connected(onion_c->dht)) {
135 if (DHT_isconnected(onion_c->dht)) { 135 if (DHT_isconnected(onion_c->dht)) {
136 if (num_nodes < 3) 136 if (num_nodes == 0)
137 return random_nodes_path(onion_c->dht, nodes, max_num); 137 return 0;
138 138
139 for (i = 0; i < max_num; ++i) { 139 for (i = 0; i < max_num; ++i) {
140 nodes[i] = onion_c->path_nodes[rand() % num_nodes]; 140 nodes[i] = onion_c->path_nodes[rand() % num_nodes];
@@ -1190,7 +1190,7 @@ static void populate_path_nodes(Onion_Client *onion_c)
1190 1190
1191#define ANNOUNCE_FRIEND (ONION_NODE_PING_INTERVAL * 6) 1191#define ANNOUNCE_FRIEND (ONION_NODE_PING_INTERVAL * 6)
1192#define ANNOUNCE_FRIEND_BEGINNING 3 1192#define ANNOUNCE_FRIEND_BEGINNING 3
1193#define FRIEND_ONION_NODE_TIMEOUT (ONION_NODE_TIMEOUT * 4) 1193#define FRIEND_ONION_NODE_TIMEOUT (ONION_NODE_TIMEOUT * 6)
1194 1194
1195#define RUN_COUNT_FRIEND_ANNOUNCE_BEGINNING 17 1195#define RUN_COUNT_FRIEND_ANNOUNCE_BEGINNING 17
1196 1196
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index 7119a8bf..9a102891 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -30,7 +30,7 @@
30 30
31#define MAX_ONION_CLIENTS 8 31#define MAX_ONION_CLIENTS 8
32#define ONION_NODE_PING_INTERVAL 20 32#define ONION_NODE_PING_INTERVAL 20
33#define ONION_NODE_TIMEOUT (ONION_NODE_PING_INTERVAL * 4) 33#define ONION_NODE_TIMEOUT (ONION_NODE_PING_INTERVAL * 3)
34 34
35/* The interval in seconds at which to tell our friends where we are */ 35/* The interval in seconds at which to tell our friends where we are */
36#define ONION_FAKEID_INTERVAL 30 36#define ONION_FAKEID_INTERVAL 30