summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/onion_announce.c6
-rw-r--r--toxcore/onion_announce.h4
-rw-r--r--toxcore/onion_client.c55
-rw-r--r--toxcore/onion_client.h3
-rw-r--r--toxcore/ping_array.h2
5 files changed, 35 insertions, 35 deletions
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
index abe72486..e6489a67 100644
--- a/toxcore/onion_announce.c
+++ b/toxcore/onion_announce.c
@@ -50,14 +50,14 @@
50 * return 0 on success. 50 * return 0 on success.
51 */ 51 */
52int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format dest, uint8_t *public_key, 52int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format dest, uint8_t *public_key,
53 uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, uint8_t *sendback_data) 53 uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, uint64_t sendback_data)
54{ 54{
55 uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; 55 uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
56 memcpy(plain, ping_id, ONION_PING_ID_SIZE); 56 memcpy(plain, ping_id, ONION_PING_ID_SIZE);
57 memcpy(plain + ONION_PING_ID_SIZE, client_id, crypto_box_PUBLICKEYBYTES); 57 memcpy(plain + ONION_PING_ID_SIZE, client_id, crypto_box_PUBLICKEYBYTES);
58 memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES, data_public_key, crypto_box_PUBLICKEYBYTES); 58 memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES, data_public_key, crypto_box_PUBLICKEYBYTES);
59 memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES, sendback_data, 59 memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES, &sendback_data,
60 ONION_ANNOUNCE_SENDBACK_DATA_LENGTH); 60 sizeof(sendback_data));
61 uint8_t packet[ANNOUNCE_REQUEST_SIZE]; 61 uint8_t packet[ANNOUNCE_REQUEST_SIZE];
62 packet[0] = NET_PACKET_ANNOUNCE_REQUEST; 62 packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
63 random_nonce(packet + 1); 63 random_nonce(packet + 1);
diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h
index dff61e99..ea320998 100644
--- a/toxcore/onion_announce.h
+++ b/toxcore/onion_announce.h
@@ -29,7 +29,7 @@
29#define ONION_ANNOUNCE_TIMEOUT 300 29#define ONION_ANNOUNCE_TIMEOUT 300
30#define ONION_PING_ID_SIZE crypto_hash_sha256_BYTES 30#define ONION_PING_ID_SIZE crypto_hash_sha256_BYTES
31 31
32#define ONION_ANNOUNCE_SENDBACK_DATA_LENGTH (crypto_box_NONCEBYTES + sizeof(uint32_t) + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port) + crypto_box_MACBYTES) 32#define ONION_ANNOUNCE_SENDBACK_DATA_LENGTH (sizeof(uint64_t))
33 33
34#define ONION_ANNOUNCE_RESPONSE_MIN_SIZE (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES + 1 + ONION_PING_ID_SIZE + crypto_box_MACBYTES) 34#define ONION_ANNOUNCE_RESPONSE_MIN_SIZE (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES + 1 + ONION_PING_ID_SIZE + crypto_box_MACBYTES)
35#define ONION_ANNOUNCE_RESPONSE_MAX_SIZE (ONION_ANNOUNCE_RESPONSE_MIN_SIZE + sizeof(Node_format)*MAX_SENT_NODES) 35#define ONION_ANNOUNCE_RESPONSE_MAX_SIZE (ONION_ANNOUNCE_RESPONSE_MIN_SIZE + sizeof(Node_format)*MAX_SENT_NODES)
@@ -76,7 +76,7 @@ typedef struct {
76 * return 0 on success. 76 * return 0 on success.
77 */ 77 */
78int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format dest, uint8_t *public_key, 78int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format dest, uint8_t *public_key,
79 uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, uint8_t *sendback_data); 79 uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, uint64_t sendback_data);
80 80
81/* Create and send an onion data request packet. 81/* Create and send an onion data request packet.
82 * 82 *
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 0a238b5f..18221320 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -28,6 +28,9 @@
28#include "util.h" 28#include "util.h"
29#include "LAN_discovery.h" 29#include "LAN_discovery.h"
30 30
31/* defines for the array size and
32 timeout for onion announce packets. */
33#define ANNOUNCE_ARRAY_SIZE 256
31#define ANNOUNCE_TIMEOUT 10 34#define ANNOUNCE_TIMEOUT 10
32 35
33/* Create a new path or use an old suitable one (if pathnum is valid) 36/* Create a new path or use an old suitable one (if pathnum is valid)
@@ -105,20 +108,15 @@ static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, IP_Port s
105 * return 0 on success 108 * return 0 on success
106 * 109 *
107 */ 110 */
108static int new_sendback(Onion_Client *onion_c, uint32_t num, uint8_t *public_key, IP_Port ip_port, uint8_t *sendback) 111static int new_sendback(Onion_Client *onion_c, uint32_t num, uint8_t *public_key, IP_Port ip_port, uint64_t *sendback)
109{ 112{
110 uint8_t plain[sizeof(uint32_t) + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)]; 113 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)];
111 uint64_t time = unix_time(); 114 memcpy(data, &num, sizeof(uint32_t));
112 random_nonce(sendback); 115 memcpy(data + sizeof(uint32_t), public_key, crypto_box_PUBLICKEYBYTES);
113 memcpy(plain, &num, sizeof(uint32_t)); 116 memcpy(data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES, &ip_port, sizeof(IP_Port));
114 memcpy(plain + sizeof(uint32_t), &time, sizeof(uint64_t)); 117 *sendback = ping_array_add(&onion_c->announce_ping_array, data, sizeof(data));
115 memcpy(plain + sizeof(uint32_t) + sizeof(uint64_t), public_key, crypto_box_PUBLICKEYBYTES); 118
116 memcpy(plain + sizeof(uint32_t) + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES, &ip_port, sizeof(IP_Port)); 119 if (*sendback == 0)
117
118 int len = encrypt_data_symmetric(onion_c->secret_symmetric_key, sendback, plain, sizeof(plain),
119 sendback + crypto_box_NONCEBYTES);
120
121 if ((uint32_t)len + crypto_box_NONCEBYTES != ONION_ANNOUNCE_SENDBACK_DATA_LENGTH)
122 return -1; 120 return -1;
123 121
124 return 0; 122 return 0;
@@ -136,24 +134,17 @@ static int new_sendback(Onion_Client *onion_c, uint32_t num, uint8_t *public_key
136 */ 134 */
137static uint32_t check_sendback(Onion_Client *onion_c, uint8_t *sendback, uint8_t *ret_pubkey, IP_Port *ret_ip_port) 135static uint32_t check_sendback(Onion_Client *onion_c, uint8_t *sendback, uint8_t *ret_pubkey, IP_Port *ret_ip_port)
138{ 136{
139 uint8_t plain[sizeof(uint32_t) + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)]; 137 uint64_t sback;
140 int len = decrypt_data_symmetric(onion_c->secret_symmetric_key, sendback, sendback + crypto_box_NONCEBYTES, 138 memcpy(&sback, sendback, sizeof(uint64_t));
141 ONION_ANNOUNCE_SENDBACK_DATA_LENGTH - crypto_box_NONCEBYTES, plain); 139 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)];
142
143 if ((uint32_t)len != sizeof(plain))
144 return ~0;
145 140
146 uint64_t timestamp; 141 if (ping_array_check(data, sizeof(data), &onion_c->announce_ping_array, sback) != sizeof(data))
147 memcpy(&timestamp, plain + sizeof(uint32_t), sizeof(uint64_t));
148 uint64_t temp_time = unix_time();
149
150 if (timestamp + ANNOUNCE_TIMEOUT < temp_time || temp_time < timestamp)
151 return ~0; 142 return ~0;
152 143
153 memcpy(ret_pubkey, plain + sizeof(uint32_t) + sizeof(uint64_t), crypto_box_PUBLICKEYBYTES); 144 memcpy(ret_pubkey, data + sizeof(uint32_t), crypto_box_PUBLICKEYBYTES);
154 memcpy(ret_ip_port, plain + sizeof(uint32_t) + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port)); 145 memcpy(ret_ip_port, data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port));
155 uint32_t num; 146 uint32_t num;
156 memcpy(&num, plain, sizeof(uint32_t)); 147 memcpy(&num, data, sizeof(uint32_t));
157 return num; 148 return num;
158} 149}
159 150
@@ -163,9 +154,9 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
163 if (num > onion_c->num_friends) 154 if (num > onion_c->num_friends)
164 return -1; 155 return -1;
165 156
166 uint8_t sendback[ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; 157 uint64_t sendback;
167 158
168 if (new_sendback(onion_c, num, dest_pubkey, dest, sendback) == -1) 159 if (new_sendback(onion_c, num, dest_pubkey, dest, &sendback) == -1)
169 return -1; 160 return -1;
170 161
171 uint8_t zero_ping_id[ONION_PING_ID_SIZE] = {0}; 162 uint8_t zero_ping_id[ONION_PING_ID_SIZE] = {0};
@@ -1010,6 +1001,11 @@ Onion_Client *new_onion_client(Net_Crypto *c)
1010 if (onion_c == NULL) 1001 if (onion_c == NULL)
1011 return NULL; 1002 return NULL;
1012 1003
1004 if (ping_array_init(&onion_c->announce_ping_array, ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT) != 0) {
1005 free(onion_c);
1006 return NULL;
1007 }
1008
1013 onion_c->dht = c->dht; 1009 onion_c->dht = c->dht;
1014 onion_c->net = c->dht->net; 1010 onion_c->net = c->dht->net;
1015 onion_c->c = c; 1011 onion_c->c = c;
@@ -1028,6 +1024,7 @@ void kill_onion_client(Onion_Client *onion_c)
1028 if (onion_c == NULL) 1024 if (onion_c == NULL)
1029 return; 1025 return;
1030 1026
1027 ping_array_free_all(&onion_c->announce_ping_array);
1031 realloc_onion_friends(onion_c, 0); 1028 realloc_onion_friends(onion_c, 0);
1032 networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, NULL, NULL); 1029 networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, NULL, NULL);
1033 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL); 1030 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL);
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index 32d2989f..06909380 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -26,6 +26,7 @@
26 26
27#include "onion_announce.h" 27#include "onion_announce.h"
28#include "net_crypto.h" 28#include "net_crypto.h"
29#include "ping_array.h"
29 30
30#define MAX_ONION_CLIENTS 8 31#define MAX_ONION_CLIENTS 8
31#define ONION_NODE_PING_INTERVAL 30 32#define ONION_NODE_PING_INTERVAL 30
@@ -124,6 +125,8 @@ typedef struct {
124 uint32_t ping_nodes_sent_second; 125 uint32_t ping_nodes_sent_second;
125 126
126 Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; 127 Last_Pinged last_pinged[MAX_STORED_PINGED_NODES];
128
129 Ping_Array announce_ping_array;
127 uint8_t last_pinged_index; 130 uint8_t last_pinged_index;
128 struct { 131 struct {
129 oniondata_handler_callback function; 132 oniondata_handler_callback function;
diff --git a/toxcore/ping_array.h b/toxcore/ping_array.h
index b7fff1eb..c5811b16 100644
--- a/toxcore/ping_array.h
+++ b/toxcore/ping_array.h
@@ -53,7 +53,7 @@ uint64_t ping_array_add(Ping_Array *array, uint8_t *data, uint32_t length);
53/* Check if ping_id is valid and not timed out. 53/* Check if ping_id is valid and not timed out.
54 * 54 *
55 * On success, copies the data into data of length, 55 * On success, copies the data into data of length,
56 * 56 *
57 * return length of data copied on success. 57 * return length of data copied on success.
58 * return -1 on failure. 58 * return -1 on failure.
59 */ 59 */