summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-04-23 11:35:40 -0400
committerirungentoo <irungentoo@gmail.com>2014-04-23 11:35:40 -0400
commit384750af8cf9e339989ded452181e1238ed6e307 (patch)
treef3eb8ac28b07263fe4c8372db8a2f52230531133 /toxcore
parent1bfe15ee88844bdbd43052b4026202cf924ad6ca (diff)
Major cleanups.
Fixed circular dependency between DHT and net_crypto: DHT no longer depends on net_crypto. Moved the crypto request packets functions to crypto core and DHT. Cleaned up/added some defines that can be used to get the true maximum length of things like the friends request message. MAX_DATA_SIZE has been replaced in most places by more appropriate defines.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c67
-rw-r--r--toxcore/DHT.h15
-rw-r--r--toxcore/Messenger.c29
-rw-r--r--toxcore/Messenger.h6
-rw-r--r--toxcore/crypto_core.c71
-rw-r--r--toxcore/crypto_core.h31
-rw-r--r--toxcore/friend_requests.c8
-rw-r--r--toxcore/friend_requests.h5
-rw-r--r--toxcore/group_chats.c12
-rw-r--r--toxcore/net_crypto.c120
-rw-r--r--toxcore/net_crypto.h41
-rw-r--r--toxcore/onion.c40
-rw-r--r--toxcore/onion.h7
-rw-r--r--toxcore/onion_announce.c7
-rw-r--r--toxcore/onion_announce.h5
-rw-r--r--toxcore/onion_client.c47
-rw-r--r--toxcore/onion_client.h7
17 files changed, 275 insertions, 243 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 5b43cdb0..8b553581 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -1685,7 +1685,7 @@ int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id)
1685static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t type) 1685static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t type)
1686{ 1686{
1687 uint8_t data[sizeof(uint64_t) + 1]; 1687 uint8_t data[sizeof(uint64_t) + 1];
1688 uint8_t packet[MAX_DATA_SIZE]; 1688 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
1689 1689
1690 int num = 0; 1690 int num = 0;
1691 1691
@@ -1896,7 +1896,7 @@ static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8
1896 if (length > HARDREQ_DATA_SIZE - 1) 1896 if (length > HARDREQ_DATA_SIZE - 1)
1897 return -1; 1897 return -1;
1898 1898
1899 uint8_t packet[MAX_DATA_SIZE]; 1899 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
1900 uint8_t data[HARDREQ_DATA_SIZE] = {0}; 1900 uint8_t data[HARDREQ_DATA_SIZE] = {0};
1901 data[0] = type; 1901 data[0] = type;
1902 memcpy(data + 1, contents, length); 1902 memcpy(data + 1, contents, length);
@@ -1925,7 +1925,7 @@ static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *qu
1925 if (!ip_isset(&sendto->ip_port.ip)) 1925 if (!ip_isset(&sendto->ip_port.ip))
1926 return -1; 1926 return -1;
1927 1927
1928 uint8_t packet[MAX_DATA_SIZE]; 1928 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
1929 uint8_t data[1 + CLIENT_ID_SIZE + nodes_data_length]; 1929 uint8_t data[1 + CLIENT_ID_SIZE + nodes_data_length];
1930 data[0] = CHECK_TYPE_GETNODE_RES; 1930 data[0] = CHECK_TYPE_GETNODE_RES;
1931 memcpy(data + 1, queried_client_id, CLIENT_ID_SIZE); 1931 memcpy(data + 1, queried_client_id, CLIENT_ID_SIZE);
@@ -2242,12 +2242,54 @@ void do_hardening(DHT *dht)
2242 2242
2243/*----------------------------------------------------------------------------------*/ 2243/*----------------------------------------------------------------------------------*/
2244 2244
2245DHT *new_DHT(Net_Crypto *c) 2245void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_callback cb, void *object)
2246{
2247 dht->cryptopackethandlers[byte].function = cb;
2248 dht->cryptopackethandlers[byte].object = object;
2249}
2250
2251static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, uint32_t length)
2252{
2253 DHT *dht = object;
2254
2255 if (packet[0] == NET_PACKET_CRYPTO) {
2256 if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES ||
2257 length > MAX_CRYPTO_REQUEST_SIZE + crypto_box_MACBYTES)
2258 return 1;
2259
2260 if (memcmp(packet + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us.
2261 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
2262 uint8_t data[MAX_CRYPTO_REQUEST_SIZE];
2263 uint8_t number;
2264 int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, data, &number, packet, length);
2265
2266 if (len == -1 || len == 0)
2267 return 1;
2268
2269 if (!dht->cryptopackethandlers[number].function) return 1;
2270
2271 return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key,
2272 data, len);
2273
2274 } else { /* If request is not for us, try routing it. */
2275 int retval = route_packet(dht, packet + 1, packet, length);
2276
2277 if ((unsigned int)retval == length)
2278 return 0;
2279 }
2280 }
2281
2282 return 1;
2283}
2284
2285/*----------------------------------------------------------------------------------*/
2286
2287DHT *new_DHT(Networking_Core *net)
2246{ 2288{
2247 /* init time */ 2289 /* init time */
2248 unix_time_update(); 2290 unix_time_update();
2249 2291
2250 if (c == NULL) 2292 if (net == NULL)
2251 return NULL; 2293 return NULL;
2252 2294
2253 DHT *dht = calloc(1, sizeof(DHT)); 2295 DHT *dht = calloc(1, sizeof(DHT));
@@ -2255,8 +2297,7 @@ DHT *new_DHT(Net_Crypto *c)
2255 if (dht == NULL) 2297 if (dht == NULL)
2256 return NULL; 2298 return NULL;
2257 2299
2258 dht->c = c; 2300 dht->net = net;
2259 dht->net = c->lossless_udp->net;
2260 dht->ping = new_ping(dht); 2301 dht->ping = new_ping(dht);
2261 2302
2262 if (dht->ping == NULL) { 2303 if (dht->ping == NULL) {
@@ -2266,9 +2307,9 @@ DHT *new_DHT(Net_Crypto *c)
2266 2307
2267 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, &handle_getnodes, dht); 2308 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, &handle_getnodes, dht);
2268 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht); 2309 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht);
2269 init_cryptopackets(dht); 2310 networking_registerhandler(dht->net, NET_PACKET_CRYPTO, &cryptopacket_handle, dht);
2270 cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht); 2311 cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht);
2271 cryptopacket_registerhandler(c, CRYPTO_PACKET_HARDENING, &handle_hardening, dht); 2312 cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, &handle_hardening, dht);
2272 2313
2273 new_symmetric_key(dht->secret_symmetric_key); 2314 new_symmetric_key(dht->secret_symmetric_key);
2274 crypto_box_keypair(dht->self_public_key, dht->self_secret_key); 2315 crypto_box_keypair(dht->self_public_key, dht->self_secret_key);
@@ -2283,7 +2324,6 @@ DHT *new_DHT(Net_Crypto *c)
2283 DHT_addfriend(dht, random_key_bytes); 2324 DHT_addfriend(dht, random_key_bytes);
2284 } 2325 }
2285 2326
2286 c->dht = dht;
2287 return dht; 2327 return dht;
2288} 2328}
2289 2329
@@ -2316,9 +2356,8 @@ void kill_DHT(DHT *dht)
2316 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, NULL, NULL); 2356 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, NULL, NULL);
2317 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES, NULL, NULL); 2357 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES, NULL, NULL);
2318 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL); 2358 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL);
2319 cryptopacket_registerhandler(dht->c, CRYPTO_PACKET_NAT_PING, NULL, NULL); 2359 cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL);
2320 cryptopacket_registerhandler(dht->c, CRYPTO_PACKET_HARDENING, NULL, NULL); 2360 cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL);
2321 dht->c->dht = 0;
2322 kill_ping(dht->ping); 2361 kill_ping(dht->ping);
2323 free(dht->friends_list); 2362 free(dht->friends_list);
2324 free(dht); 2363 free(dht);
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 0c59dc81..c08b498c 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -24,7 +24,7 @@
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 28
29/* Size of the client_id in bytes. */ 29/* Size of the client_id in bytes. */
30#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES 30#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
@@ -168,8 +168,15 @@ typedef struct {
168 168
169/*----------------------------------------------------------------------------------*/ 169/*----------------------------------------------------------------------------------*/
170 170
171typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data,
172 uint32_t len);
173
174typedef struct {
175 cryptopacket_handler_callback function;
176 void *object;
177} Cryptopacket_Handles;
178
171typedef struct { 179typedef struct {
172 Net_Crypto *c;
173 Networking_Core *net; 180 Networking_Core *net;
174 181
175 Client_data close_clientlist[LCLIENT_LIST]; 182 Client_data close_clientlist[LCLIENT_LIST];
@@ -193,6 +200,8 @@ typedef struct {
193 struct Assoc *assoc; 200 struct Assoc *assoc;
194#endif 201#endif
195 uint64_t last_run; 202 uint64_t last_run;
203
204 Cryptopacket_Handles cryptopackethandlers[256];
196} DHT; 205} DHT;
197/*----------------------------------------------------------------------------------*/ 206/*----------------------------------------------------------------------------------*/
198 207
@@ -354,7 +363,7 @@ void DHT_save(DHT *dht, uint8_t *data);
354int DHT_load(DHT *dht, uint8_t *data, uint32_t length); 363int DHT_load(DHT *dht, uint8_t *data, uint32_t length);
355 364
356/* Initialize DHT. */ 365/* Initialize DHT. */
357DHT *new_DHT(Net_Crypto *c); 366DHT *new_DHT(Networking_Core *net);
358 367
359void kill_DHT(DHT *dht); 368void kill_DHT(DHT *dht);
360 369
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index f7d773dd..308cf14e 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -214,9 +214,7 @@ void getaddress(Messenger *m, uint8_t *address)
214 */ 214 */
215int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) 215int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
216{ 216{
217 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES 217 if (length > MAX_FRIEND_REQUEST_DATA_SIZE)
218 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
219 + crypto_box_ZEROBYTES))
220 return FAERR_TOOLONG; 218 return FAERR_TOOLONG;
221 219
222 uint8_t client_id[crypto_box_PUBLICKEYBYTES]; 220 uint8_t client_id[crypto_box_PUBLICKEYBYTES];
@@ -1757,26 +1755,26 @@ Messenger *new_messenger(uint8_t ipv6enabled)
1757 return NULL; 1755 return NULL;
1758 } 1756 }
1759 1757
1760 m->net_crypto = new_net_crypto(m->net); 1758 m->dht = new_DHT(m->net);
1761 1759
1762 if (m->net_crypto == NULL) { 1760 if (m->dht == NULL) {
1763 kill_networking(m->net); 1761 kill_networking(m->net);
1764 free(m); 1762 free(m);
1765 return NULL; 1763 return NULL;
1766 } 1764 }
1767 1765
1768 m->dht = new_DHT(m->net_crypto); 1766 m->net_crypto = new_net_crypto(m->dht);
1769 1767
1770 if (m->dht == NULL) { 1768 if (m->net_crypto == NULL) {
1771 kill_net_crypto(m->net_crypto);
1772 kill_networking(m->net); 1769 kill_networking(m->net);
1770 kill_DHT(m->dht);
1773 free(m); 1771 free(m);
1774 return NULL; 1772 return NULL;
1775 } 1773 }
1776 1774
1777 m->onion = new_onion(m->dht); 1775 m->onion = new_onion(m->dht);
1778 m->onion_a = new_onion_announce(m->dht); 1776 m->onion_a = new_onion_announce(m->dht);
1779 m->onion_c = new_onion_client(m->dht); 1777 m->onion_c = new_onion_client(m->net_crypto);
1780 1778
1781 if (!(m->onion && m->onion_a && m->onion_c)) { 1779 if (!(m->onion && m->onion_a && m->onion_c)) {
1782 kill_onion(m->onion); 1780 kill_onion(m->onion);
@@ -2409,10 +2407,12 @@ int wait_cleanup_messenger(Messenger *m, uint8_t *data)
2409#define MESSENGER_STATE_TYPE_STATUSMESSAGE 5 2407#define MESSENGER_STATE_TYPE_STATUSMESSAGE 5
2410#define MESSENGER_STATE_TYPE_STATUS 6 2408#define MESSENGER_STATE_TYPE_STATUS 6
2411 2409
2410#define SAVED_FRIEND_REQUEST_SIZE 1024
2411
2412struct SAVED_FRIEND { 2412struct SAVED_FRIEND {
2413 uint8_t status; 2413 uint8_t status;
2414 uint8_t client_id[CLIENT_ID_SIZE]; 2414 uint8_t client_id[CLIENT_ID_SIZE];
2415 uint8_t info[MAX_DATA_SIZE]; // the data that is sent during the friend requests we do. 2415 uint8_t info[SAVED_FRIEND_REQUEST_SIZE]; // the data that is sent during the friend requests we do.
2416 uint16_t info_size; // Length of the info. 2416 uint16_t info_size; // Length of the info.
2417 uint8_t name[MAX_NAME_LENGTH]; 2417 uint8_t name[MAX_NAME_LENGTH];
2418 uint16_t name_length; 2418 uint16_t name_length;
@@ -2428,7 +2428,7 @@ struct SAVED_FRIEND {
2428struct SAVED_FRIEND_OLD { 2428struct SAVED_FRIEND_OLD {
2429 uint8_t status; 2429 uint8_t status;
2430 uint8_t client_id[CLIENT_ID_SIZE]; 2430 uint8_t client_id[CLIENT_ID_SIZE];
2431 uint8_t info[MAX_DATA_SIZE]; 2431 uint8_t info[1024];
2432 uint16_t info_size; 2432 uint16_t info_size;
2433 uint8_t name[MAX_NAME_LENGTH]; 2433 uint8_t name[MAX_NAME_LENGTH];
2434 uint16_t name_length; 2434 uint16_t name_length;
@@ -2456,7 +2456,12 @@ static uint32_t friends_list_save(Messenger *m, uint8_t *data)
2456 memcpy(temp.client_id, m->friendlist[i].client_id, CLIENT_ID_SIZE); 2456 memcpy(temp.client_id, m->friendlist[i].client_id, CLIENT_ID_SIZE);
2457 2457
2458 if (temp.status < 3) { 2458 if (temp.status < 3) {
2459 memcpy(temp.info, m->friendlist[i].info, m->friendlist[i].info_size); 2459 if (m->friendlist[i].info_size > SAVED_FRIEND_REQUEST_SIZE) {
2460 memcpy(temp.info, m->friendlist[i].info, SAVED_FRIEND_REQUEST_SIZE);
2461 } else {
2462 memcpy(temp.info, m->friendlist[i].info, m->friendlist[i].info_size);
2463 }
2464
2460 temp.info_size = htons(m->friendlist[i].info_size); 2465 temp.info_size = htons(m->friendlist[i].info_size);
2461 temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam; 2466 temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam;
2462 } else { 2467 } else {
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 0ce96f44..3d42e911 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -44,9 +44,9 @@
44#define PACKET_ID_STATUSMESSAGE 49 44#define PACKET_ID_STATUSMESSAGE 49
45#define PACKET_ID_USERSTATUS 50 45#define PACKET_ID_USERSTATUS 50
46#define PACKET_ID_TYPING 51 46#define PACKET_ID_TYPING 51
47#define PACKET_ID_RECEIPT 65 47#define PACKET_ID_RECEIPT 63
48#define PACKET_ID_MESSAGE 64 48#define PACKET_ID_MESSAGE 64
49#define PACKET_ID_ACTION 63 49#define PACKET_ID_ACTION 65
50#define PACKET_ID_MSI 69 50#define PACKET_ID_MSI 69
51#define PACKET_ID_FILE_SENDREQUEST 80 51#define PACKET_ID_FILE_SENDREQUEST 80
52#define PACKET_ID_FILE_CONTROL 81 52#define PACKET_ID_FILE_CONTROL 81
@@ -134,7 +134,7 @@ typedef struct {
134 uint64_t friendrequest_lastsent; // Time at which the last friend request was sent. 134 uint64_t friendrequest_lastsent; // Time at which the last friend request was sent.
135 uint32_t friendrequest_timeout; // The timeout between successful friendrequest sending attempts. 135 uint32_t friendrequest_timeout; // The timeout between successful friendrequest sending attempts.
136 uint8_t status; // 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. 136 uint8_t status; // 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online.
137 uint8_t info[MAX_DATA_SIZE]; // the data that is sent during the friend requests we do. 137 uint8_t info[MAX_FRIEND_REQUEST_DATA_SIZE]; // the data that is sent during the friend requests we do.
138 uint8_t name[MAX_NAME_LENGTH]; 138 uint8_t name[MAX_NAME_LENGTH];
139 uint16_t name_length; 139 uint16_t name_length;
140 uint8_t name_sent; // 0 if we didn't send our name to this friend 1 if we have. 140 uint8_t name_sent; // 0 if we didn't send our name to this friend 1 if we have.
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c
index 22612442..59053bc4 100644
--- a/toxcore/crypto_core.c
+++ b/toxcore/crypto_core.c
@@ -166,4 +166,73 @@ void new_nonce(uint8_t *nonce)
166 166
167 increment_nonce(base_nonce); 167 increment_nonce(base_nonce);
168 memcpy(nonce, base_nonce, crypto_box_NONCEBYTES); 168 memcpy(nonce, base_nonce, crypto_box_NONCEBYTES);
169} \ No newline at end of file 169}
170
171/* Create a request to peer.
172 * send_public_key and send_secret_key are the pub/secret keys of the sender.
173 * recv_public_key is public key of reciever.
174 * packet must be an array of MAX_CRYPTO_REQUEST_SIZE big.
175 * Data represents the data we send with the request with length being the length of the data.
176 * request_id is the id of the request (32 = friend request, 254 = ping request).
177 *
178 * return -1 on failure.
179 * return the length of the created packet on success.
180 */
181int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
182 uint8_t *data, uint32_t length, uint8_t request_id)
183{
184 if (MAX_CRYPTO_REQUEST_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 +
185 crypto_box_MACBYTES)
186 return -1;
187
188 uint8_t nonce[crypto_box_NONCEBYTES];
189 uint8_t temp[MAX_CRYPTO_REQUEST_SIZE];
190 memcpy(temp + 1, data, length);
191 temp[0] = request_id;
192 new_nonce(nonce);
193 int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1,
194 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
195
196 if (len == -1)
197 return -1;
198
199 packet[0] = NET_PACKET_CRYPTO;
200 memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES);
201 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES);
202 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES);
203
204 return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES;
205}
206
207/* Puts the senders public key in the request in public_key, the data from the request
208 * in data if a friend or ping request was sent to us and returns the length of the data.
209 * packet is the request packet and length is its length.
210 *
211 * return -1 if not valid request.
212 */
213int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
214 uint8_t *request_id, uint8_t *packet, uint16_t length)
215{
216 if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES &&
217 length <= MAX_CRYPTO_REQUEST_SIZE) {
218 if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
219 memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
220 uint8_t nonce[crypto_box_NONCEBYTES];
221 uint8_t temp[MAX_CRYPTO_REQUEST_SIZE];
222 memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
223 int len1 = decrypt_data(public_key, self_secret_key, nonce,
224 packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
225 length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
226
227 if (len1 == -1 || len1 == 0)
228 return -1;
229
230 request_id[0] = temp[0];
231 --len1;
232 memcpy(data, temp + 1, len1);
233 return len1;
234 }
235 }
236
237 return -1;
238}
diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h
index 6a13b22b..a862635e 100644
--- a/toxcore/crypto_core.h
+++ b/toxcore/crypto_core.h
@@ -25,6 +25,7 @@
25 25
26#include "network.h" 26#include "network.h"
27 27
28
28/* return zero if the buffer contains only zeros. */ 29/* return zero if the buffer contains only zeros. */
29uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen); 30uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen);
30 31
@@ -83,4 +84,34 @@ void new_symmetric_key(uint8_t *key);
83/*Gives a nonce guaranteed to be different from previous ones.*/ 84/*Gives a nonce guaranteed to be different from previous ones.*/
84void new_nonce(uint8_t *nonce); 85void new_nonce(uint8_t *nonce);
85 86
87#define MAX_CRYPTO_REQUEST_SIZE 1024
88
89#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */
90#define CRYPTO_PACKET_HARDENING 48 /* Hardening crypto packet ID. */
91#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */
92#define CRYPTO_PACKET_GROUP_CHAT_GET_NODES 48 /* Group chat get Nodes packet */
93#define CRYPTO_PACKET_GROUP_CHAT_SEND_NODES 49 /* Group chat send Nodes packet */
94#define CRYPTO_PACKET_GROUP_CHAT_BROADCAST 50 /* Group chat broadcast packet */
95
96/* Create a request to peer.
97 * send_public_key and send_secret_key are the pub/secret keys of the sender.
98 * recv_public_key is public key of reciever.
99 * packet must be an array of MAX_CRYPTO_REQUEST_SIZE big.
100 * Data represents the data we send with the request with length being the length of the data.
101 * request_id is the id of the request (32 = friend request, 254 = ping request).
102 *
103 * return -1 on failure.
104 * return the length of the created packet on success.
105 */
106int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
107 uint8_t *data, uint32_t length, uint8_t request_id);
108
109/* puts the senders public key in the request in public_key, the data from the request
110 in data if a friend or ping request was sent to us and returns the length of the data.
111 packet is the request packet and length is its length
112 return -1 if not valid request. */
113int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
114 uint8_t *request_id, uint8_t *packet, uint16_t length);
115
116
86#endif 117#endif
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c
index c5cfa4b4..eb2a791c 100644
--- a/toxcore/friend_requests.c
+++ b/toxcore/friend_requests.c
@@ -37,10 +37,10 @@
37 */ 37 */
38int send_friendrequest(Onion_Client *onion_c, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length) 38int send_friendrequest(Onion_Client *onion_c, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length)
39{ 39{
40 if (length + sizeof(nospam_num) >= MAX_DATA_SIZE) 40 if (1 + sizeof(nospam_num) + length > ONION_CLIENT_MAX_DATA_SIZE || length == 0)
41 return -1; 41 return -1;
42 42
43 uint8_t temp[MAX_DATA_SIZE]; 43 uint8_t temp[1 + sizeof(nospam_num) + length];
44 temp[0] = CRYPTO_PACKET_FRIEND_REQ; 44 temp[0] = CRYPTO_PACKET_FRIEND_REQ;
45 memcpy(temp + 1, &nospam_num, sizeof(nospam_num)); 45 memcpy(temp + 1, &nospam_num, sizeof(nospam_num));
46 memcpy(temp + 1 + sizeof(nospam_num), data, length); 46 memcpy(temp + 1 + sizeof(nospam_num), data, length);
@@ -50,7 +50,7 @@ int send_friendrequest(Onion_Client *onion_c, uint8_t *public_key, uint32_t nosp
50 if (friend_num == -1) 50 if (friend_num == -1)
51 return -1; 51 return -1;
52 52
53 int num = send_onion_data(onion_c, friend_num, temp, 1 + sizeof(nospam_num) + length); 53 int num = send_onion_data(onion_c, friend_num, temp, sizeof(temp));
54 54
55 if (num <= 0) 55 if (num <= 0)
56 return -1; 56 return -1;
@@ -137,7 +137,7 @@ static int friendreq_handlepacket(void *object, uint8_t *source_pubkey, uint8_t
137{ 137{
138 Friend_Requests *fr = object; 138 Friend_Requests *fr = object;
139 139
140 if (length <= 1 + sizeof(fr->nospam) || length > MAX_DATA_SIZE) 140 if (length <= 1 + sizeof(fr->nospam) || length > ONION_CLIENT_MAX_DATA_SIZE)
141 return 1; 141 return 1;
142 142
143 ++packet; 143 ++packet;
diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h
index 8f25d4c1..429ffbad 100644
--- a/toxcore/friend_requests.h
+++ b/toxcore/friend_requests.h
@@ -26,6 +26,8 @@
26 26
27#include "onion_client.h" 27#include "onion_client.h"
28 28
29#define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - (1 + sizeof(uint32_t)))
30
29typedef struct { 31typedef struct {
30 uint32_t nospam; 32 uint32_t nospam;
31 void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *); 33 void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *);
@@ -46,7 +48,8 @@ typedef struct {
46} Friend_Requests; 48} Friend_Requests;
47 49
48/* Try to send a friendrequest to peer with public_key. 50/* Try to send a friendrequest to peer with public_key.
49 * data is the data in the request and length is the length. 51 * data is the data in the request and length is the length.
52 * Maximum length of data is MAX_FRIEND_REQUEST_DATA_SIZE.
50 */ 53 */
51int send_friendrequest(Onion_Client *onion_c, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length); 54int send_friendrequest(Onion_Client *onion_c, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length);
52/* Set and get the nospam variable used to prevent one type of friend request spam. */ 55/* Set and get the nospam variable used to prevent one type of friend request spam. */
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c
index ecc9cea7..fbe76d16 100644
--- a/toxcore/group_chats.c
+++ b/toxcore/group_chats.c
@@ -32,7 +32,7 @@
32#include "LAN_discovery.h" 32#include "LAN_discovery.h"
33#include "util.h" 33#include "util.h"
34 34
35#define GROUPCHAT_MAXDATA_LENGTH (MAX_DATA_SIZE - (1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES)) 35#define GROUPCHAT_MAXDATA_LENGTH (MAX_CRYPTO_REQUEST_SIZE - (1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES))
36#define GROUPCHAT_MAXPLAINDATA_LENGTH (GROUPCHAT_MAXDATA_LENGTH - crypto_box_MACBYTES) 36#define GROUPCHAT_MAXPLAINDATA_LENGTH (GROUPCHAT_MAXDATA_LENGTH - crypto_box_MACBYTES)
37 37
38#define GROUP_MAX_SENDNODES (GROUP_CLOSE_CONNECTIONS * 2) 38#define GROUP_MAX_SENDNODES (GROUP_CLOSE_CONNECTIONS * 2)
@@ -181,7 +181,7 @@ static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *publ
181 if (id_equal(chat->self_public_key, public_key)) 181 if (id_equal(chat->self_public_key, public_key))
182 return -1; 182 return -1;
183 183
184 uint8_t packet[MAX_DATA_SIZE]; 184 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
185 int len = create_request(chat->self_public_key, chat->self_secret_key, packet, public_key, data, length, request_id); 185 int len = create_request(chat->self_public_key, chat->self_secret_key, packet, public_key, data, length, request_id);
186 packet[0] = NET_PACKET_GROUP_CHATS; 186 packet[0] = NET_PACKET_GROUP_CHATS;
187 187
@@ -587,10 +587,10 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
587 587
588static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t message_id) 588static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t message_id)
589{ 589{
590 if (len + GROUP_DATA_MIN_SIZE > MAX_DATA_SIZE) /*NOTE: not the real maximum len.*/ 590 if (len + GROUP_DATA_MIN_SIZE > MAX_CRYPTO_REQUEST_SIZE) /*NOTE: not the real maximum len.*/
591 return 1; 591 return 1;
592 592
593 uint8_t packet[MAX_DATA_SIZE]; 593 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
594 ++chat->message_number; 594 ++chat->message_number;
595 595
596 if (chat->message_number == 0) 596 if (chat->message_number == 0)
@@ -616,11 +616,11 @@ static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t
616 616
617int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length) 617int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length)
618{ 618{
619 if (length > MAX_DATA_SIZE) 619 if (length > MAX_CRYPTO_REQUEST_SIZE)
620 return 1; 620 return 1;
621 621
622 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 622 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
623 uint8_t data[MAX_DATA_SIZE]; 623 uint8_t data[MAX_CRYPTO_REQUEST_SIZE];
624 uint8_t number; 624 uint8_t number;
625 int len = handle_request(chat->self_public_key, chat->self_secret_key, public_key, data, &number, packet, length); 625 int len = handle_request(chat->self_public_key, chat->self_secret_key, public_key, data, &number, packet, length);
626 626
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 3eb9ca4c..2136e09f 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -249,113 +249,6 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
249 return 1; 249 return 1;
250} 250}
251 251
252/* Create a request to peer.
253 * send_public_key and send_secret_key are the pub/secret keys of the sender.
254 * recv_public_key is public key of reciever.
255 * packet must be an array of MAX_DATA_SIZE big.
256 * Data represents the data we send with the request with length being the length of the data.
257 * request_id is the id of the request (32 = friend request, 254 = ping request).
258 *
259 * return -1 on failure.
260 * return the length of the created packet on success.
261 */
262int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
263 uint8_t *data, uint32_t length, uint8_t request_id)
264{
265 if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES)
266 return -1;
267
268 uint8_t nonce[crypto_box_NONCEBYTES];
269 uint8_t temp[MAX_DATA_SIZE];
270 memcpy(temp + 1, data, length);
271 temp[0] = request_id;
272 new_nonce(nonce);
273 int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1,
274 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
275
276 if (len == -1)
277 return -1;
278
279 packet[0] = NET_PACKET_CRYPTO;
280 memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES);
281 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES);
282 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES);
283
284 return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES;
285}
286
287/* Puts the senders public key in the request in public_key, the data from the request
288 * in data if a friend or ping request was sent to us and returns the length of the data.
289 * packet is the request packet and length is its length.
290 *
291 * return -1 if not valid request.
292 */
293int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
294 uint8_t *request_id, uint8_t *packet, uint16_t length)
295{
296 if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES &&
297 length <= MAX_DATA_SIZE) {
298 if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
299 memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
300 uint8_t nonce[crypto_box_NONCEBYTES];
301 uint8_t temp[MAX_DATA_SIZE];
302 memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
303 int len1 = decrypt_data(public_key, self_secret_key, nonce,
304 packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
305 length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
306
307 if (len1 == -1 || len1 == 0)
308 return -1;
309
310 request_id[0] = temp[0];
311 --len1;
312 memcpy(data, temp + 1, len1);
313 return len1;
314 }
315 }
316
317 return -1;
318}
319
320void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object)
321{
322 c->cryptopackethandlers[byte].function = cb;
323 c->cryptopackethandlers[byte].object = object;
324}
325
326static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, uint32_t length)
327{
328 DHT *dht = object;
329
330 if (packet[0] == NET_PACKET_CRYPTO) {
331 if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES ||
332 length > MAX_DATA_SIZE + crypto_box_MACBYTES)
333 return 1;
334
335 if (memcmp(packet + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us.
336 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
337 uint8_t data[MAX_DATA_SIZE];
338 uint8_t number;
339 int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, data, &number, packet, length);
340
341 if (len == -1 || len == 0)
342 return 1;
343
344 if (!dht->c->cryptopackethandlers[number].function) return 1;
345
346 return dht->c->cryptopackethandlers[number].function(dht->c->cryptopackethandlers[number].object, source, public_key,
347 data, len);
348
349 } else { /* If request is not for us, try routing it. */
350 int retval = route_packet(dht, packet + 1, packet, length);
351
352 if ((unsigned int)retval == length)
353 return 0;
354 }
355 }
356
357 return 1;
358}
359 252
360/* Send a crypto handshake packet containing an encrypted secret nonce and session public key 253/* Send a crypto handshake packet containing an encrypted secret nonce and session public key
361 * to peer with connection_id and public_key. 254 * to peer with connection_id and public_key.
@@ -774,11 +667,11 @@ static void receive_crypto(Net_Crypto *c)
774/* Run this to (re)initialize net_crypto. 667/* Run this to (re)initialize net_crypto.
775 * Sets all the global connection variables to their default values. 668 * Sets all the global connection variables to their default values.
776 */ 669 */
777Net_Crypto *new_net_crypto(Networking_Core *net) 670Net_Crypto *new_net_crypto(DHT *dht)
778{ 671{
779 unix_time_update(); 672 unix_time_update();
780 673
781 if (net == NULL) 674 if (dht == NULL)
782 return NULL; 675 return NULL;
783 676
784 Net_Crypto *temp = calloc(1, sizeof(Net_Crypto)); 677 Net_Crypto *temp = calloc(1, sizeof(Net_Crypto));
@@ -786,7 +679,8 @@ Net_Crypto *new_net_crypto(Networking_Core *net)
786 if (temp == NULL) 679 if (temp == NULL)
787 return NULL; 680 return NULL;
788 681
789 temp->lossless_udp = new_lossless_udp(net); 682 temp->dht = dht;
683 temp->lossless_udp = new_lossless_udp(dht->net);
790 684
791 if (temp->lossless_udp == NULL) { 685 if (temp->lossless_udp == NULL) {
792 free(temp); 686 free(temp);
@@ -798,12 +692,6 @@ Net_Crypto *new_net_crypto(Networking_Core *net)
798 return temp; 692 return temp;
799} 693}
800 694
801void init_cryptopackets(void *dht)
802{
803 DHT *s_dht = dht;
804 networking_registerhandler(s_dht->c->lossless_udp->net, NET_PACKET_CRYPTO, &cryptopacket_handle, s_dht);
805}
806
807static void kill_timedout(Net_Crypto *c) 695static void kill_timedout(Net_Crypto *c)
808{ 696{
809 uint32_t i; 697 uint32_t i;
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index bbb9ee23..ef56b304 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -27,12 +27,6 @@
27#include "Lossless_UDP.h" 27#include "Lossless_UDP.h"
28#include "DHT.h" 28#include "DHT.h"
29 29
30#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */
31#define CRYPTO_PACKET_HARDENING 48 /* Hardening crypto packet ID. */
32#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */
33#define CRYPTO_PACKET_GROUP_CHAT_GET_NODES 48 /* Group chat get Nodes packet */
34#define CRYPTO_PACKET_GROUP_CHAT_SEND_NODES 49 /* Group chat send Nodes packet */
35#define CRYPTO_PACKET_GROUP_CHAT_BROADCAST 50 /* Group chat broadcast packet */
36#define CRYPTO_HANDSHAKE_TIMEOUT (CONNECTION_TIMEOUT * 2) 30#define CRYPTO_HANDSHAKE_TIMEOUT (CONNECTION_TIMEOUT * 2)
37 31
38#define CRYPTO_CONN_NO_CONNECTION 0 32#define CRYPTO_CONN_NO_CONNECTION 0
@@ -58,13 +52,6 @@ typedef struct {
58 52
59} Crypto_Connection; 53} Crypto_Connection;
60 54
61typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data,
62 uint32_t len);
63
64typedef struct {
65 cryptopacket_handler_callback function;
66 void *object;
67} Cryptopacket_Handles;
68 55
69typedef struct { 56typedef struct {
70 Lossless_UDP *lossless_udp; 57 Lossless_UDP *lossless_udp;
@@ -79,9 +66,7 @@ typedef struct {
79 uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 66 uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
80 67
81 /* The secret key used for cookies */ 68 /* The secret key used for cookies */
82 uint8_t secret_symmetric_key[crypto_secretbox_KEYBYTES]; 69 uint8_t secret_symmetric_key[crypto_box_KEYBYTES];
83
84 Cryptopacket_Handles cryptopackethandlers[256];
85} Net_Crypto; 70} Net_Crypto;
86 71
87#include "DHT.h" 72#include "DHT.h"
@@ -102,26 +87,6 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id)
102 */ 87 */
103int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); 88int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
104 89
105/* Create a request to peer.
106 * send_public_key and send_secret_key are the pub/secret keys of the sender.
107 * recv_public_key is public key of reciever.
108 * packet must be an array of MAX_DATA_SIZE big.
109 * Data represents the data we send with the request with length being the length of the data.
110 * request_id is the id of the request (32 = friend request, 254 = ping request).
111 *
112 * return -1 on failure.
113 * return the length of the created packet on success.
114 */
115int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
116 uint8_t *data, uint32_t length, uint8_t request_id);
117
118/* puts the senders public key in the request in public_key, the data from the request
119 in data if a friend or ping request was sent to us and returns the length of the data.
120 packet is the request packet and length is its length
121 return -1 if not valid request. */
122int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
123 uint8_t *request_id, uint8_t *packet, uint16_t length);
124
125/* Function to call when request beginning with byte is received. */ 90/* Function to call when request beginning with byte is received. */
126void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); 91void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object);
127 92
@@ -186,15 +151,13 @@ void load_keys(Net_Crypto *c, uint8_t *keys);
186/* Create new instance of Net_Crypto. 151/* Create new instance of Net_Crypto.
187 * Sets all the global connection variables to their default values. 152 * Sets all the global connection variables to their default values.
188 */ 153 */
189Net_Crypto *new_net_crypto(Networking_Core *net); 154Net_Crypto *new_net_crypto(DHT *dht);
190 155
191/* Main loop. */ 156/* Main loop. */
192void do_net_crypto(Net_Crypto *c); 157void do_net_crypto(Net_Crypto *c);
193 158
194void kill_net_crypto(Net_Crypto *c); 159void kill_net_crypto(Net_Crypto *c);
195 160
196/* Initialize the cryptopacket handling. */
197void init_cryptopackets(void *dht);
198 161
199 162
200#endif 163#endif
diff --git a/toxcore/onion.c b/toxcore/onion.c
index c2ca21f8..8cc851c2 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -26,8 +26,6 @@
26#include "onion.h" 26#include "onion.h"
27#include "util.h" 27#include "util.h"
28 28
29#define MAX_ONION_SIZE MAX_DATA_SIZE
30
31#define RETURN_1 ONION_RETURN_1 29#define RETURN_1 ONION_RETURN_1
32#define RETURN_2 ONION_RETURN_2 30#define RETURN_2 ONION_RETURN_2
33#define RETURN_3 ONION_RETURN_3 31#define RETURN_3 ONION_RETURN_3
@@ -89,13 +87,14 @@ int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes)
89/* Create and send a onion packet. 87/* Create and send a onion packet.
90 * 88 *
91 * Use Onion_Path path to send data of length to dest. 89 * Use Onion_Path path to send data of length to dest.
90 * Maximum length of data is ONION_MAX_DATA_SIZE.
92 * 91 *
93 * return -1 on failure. 92 * return -1 on failure.
94 * return 0 on success. 93 * return 0 on success.
95 */ 94 */
96int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length) 95int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length)
97{ 96{
98 if (1 + length + SEND_1 > MAX_ONION_SIZE || length == 0) 97 if (1 + length + SEND_1 > ONION_MAX_PACKET_SIZE || length == 0)
99 return -1; 98 return -1;
100 99
101 to_net_family(&dest.ip); 100 to_net_family(&dest.ip);
@@ -142,13 +141,18 @@ int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint
142 141
143 return 0; 142 return 0;
144} 143}
144
145/* Create and send a onion response sent initially to dest with. 145/* Create and send a onion response sent initially to dest with.
146 * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
146 * 147 *
147 * return -1 on failure. 148 * return -1 on failure.
148 * return 0 on success. 149 * return 0 on success.
149 */ 150 */
150int send_onion_response(Networking_Core *net, IP_Port dest, uint8_t *data, uint32_t length, uint8_t *ret) 151int send_onion_response(Networking_Core *net, IP_Port dest, uint8_t *data, uint32_t length, uint8_t *ret)
151{ 152{
153 if (length > ONION_RESPONSE_MAX_DATA_SIZE || length == 0)
154 return -1;
155
152 uint8_t packet[1 + RETURN_3 + length]; 156 uint8_t packet[1 + RETURN_3 + length];
153 packet[0] = NET_PACKET_ONION_RECV_3; 157 packet[0] = NET_PACKET_ONION_RECV_3;
154 memcpy(packet + 1, ret, RETURN_3); 158 memcpy(packet + 1, ret, RETURN_3);
@@ -164,7 +168,7 @@ static int handle_send_initial(void *object, IP_Port source, uint8_t *packet, ui
164{ 168{
165 Onion *onion = object; 169 Onion *onion = object;
166 170
167 if (length > MAX_ONION_SIZE) 171 if (length > ONION_MAX_PACKET_SIZE)
168 return 1; 172 return 1;
169 173
170 if (length <= 1 + SEND_1) 174 if (length <= 1 + SEND_1)
@@ -172,7 +176,7 @@ static int handle_send_initial(void *object, IP_Port source, uint8_t *packet, ui
172 176
173 change_symmetric_key(onion); 177 change_symmetric_key(onion);
174 178
175 uint8_t plain[MAX_ONION_SIZE]; 179 uint8_t plain[ONION_MAX_PACKET_SIZE];
176 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 180 uint8_t shared_key[crypto_box_BEFORENMBYTES];
177 get_shared_key(&onion->shared_keys_1, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES); 181 get_shared_key(&onion->shared_keys_1, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES);
178 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 182 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
@@ -190,7 +194,7 @@ int onion_send_1(Onion *onion, uint8_t *plain, uint32_t len, IP_Port source, uin
190 memcpy(&send_to, plain, sizeof(IP_Port)); 194 memcpy(&send_to, plain, sizeof(IP_Port));
191 to_host_family(&send_to.ip); 195 to_host_family(&send_to.ip);
192 196
193 uint8_t data[MAX_ONION_SIZE]; 197 uint8_t data[ONION_MAX_PACKET_SIZE];
194 data[0] = NET_PACKET_ONION_SEND_1; 198 data[0] = NET_PACKET_ONION_SEND_1;
195 memcpy(data + 1, nonce, crypto_box_NONCEBYTES); 199 memcpy(data + 1, nonce, crypto_box_NONCEBYTES);
196 memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port)); 200 memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port));
@@ -215,7 +219,7 @@ static int handle_send_1(void *object, IP_Port source, uint8_t *packet, uint32_t
215{ 219{
216 Onion *onion = object; 220 Onion *onion = object;
217 221
218 if (length > MAX_ONION_SIZE) 222 if (length > ONION_MAX_PACKET_SIZE)
219 return 1; 223 return 1;
220 224
221 if (length <= 1 + SEND_2) 225 if (length <= 1 + SEND_2)
@@ -223,7 +227,7 @@ static int handle_send_1(void *object, IP_Port source, uint8_t *packet, uint32_t
223 227
224 change_symmetric_key(onion); 228 change_symmetric_key(onion);
225 229
226 uint8_t plain[MAX_ONION_SIZE]; 230 uint8_t plain[ONION_MAX_PACKET_SIZE];
227 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 231 uint8_t shared_key[crypto_box_BEFORENMBYTES];
228 get_shared_key(&onion->shared_keys_2, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES); 232 get_shared_key(&onion->shared_keys_2, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES);
229 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 233 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
@@ -236,7 +240,7 @@ static int handle_send_1(void *object, IP_Port source, uint8_t *packet, uint32_t
236 memcpy(&send_to, plain, sizeof(IP_Port)); 240 memcpy(&send_to, plain, sizeof(IP_Port));
237 to_host_family(&send_to.ip); 241 to_host_family(&send_to.ip);
238 242
239 uint8_t data[MAX_ONION_SIZE]; 243 uint8_t data[ONION_MAX_PACKET_SIZE];
240 data[0] = NET_PACKET_ONION_SEND_2; 244 data[0] = NET_PACKET_ONION_SEND_2;
241 memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES); 245 memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES);
242 memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port)); 246 memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port));
@@ -264,7 +268,7 @@ static int handle_send_2(void *object, IP_Port source, uint8_t *packet, uint32_t
264{ 268{
265 Onion *onion = object; 269 Onion *onion = object;
266 270
267 if (length > MAX_ONION_SIZE) 271 if (length > ONION_MAX_PACKET_SIZE)
268 return 1; 272 return 1;
269 273
270 if (length <= 1 + SEND_3) 274 if (length <= 1 + SEND_3)
@@ -272,7 +276,7 @@ static int handle_send_2(void *object, IP_Port source, uint8_t *packet, uint32_t
272 276
273 change_symmetric_key(onion); 277 change_symmetric_key(onion);
274 278
275 uint8_t plain[MAX_ONION_SIZE]; 279 uint8_t plain[ONION_MAX_PACKET_SIZE];
276 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 280 uint8_t shared_key[crypto_box_BEFORENMBYTES];
277 get_shared_key(&onion->shared_keys_3, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES); 281 get_shared_key(&onion->shared_keys_3, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES);
278 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 282 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
@@ -285,7 +289,7 @@ static int handle_send_2(void *object, IP_Port source, uint8_t *packet, uint32_t
285 memcpy(&send_to, plain, sizeof(IP_Port)); 289 memcpy(&send_to, plain, sizeof(IP_Port));
286 to_host_family(&send_to.ip); 290 to_host_family(&send_to.ip);
287 291
288 uint8_t data[MAX_ONION_SIZE]; 292 uint8_t data[ONION_MAX_PACKET_SIZE];
289 memcpy(data, plain + sizeof(IP_Port), len - sizeof(IP_Port)); 293 memcpy(data, plain + sizeof(IP_Port), len - sizeof(IP_Port));
290 uint32_t data_len = (len - sizeof(IP_Port)); 294 uint32_t data_len = (len - sizeof(IP_Port));
291 uint8_t *ret_part = data + (len - sizeof(IP_Port)); 295 uint8_t *ret_part = data + (len - sizeof(IP_Port));
@@ -312,7 +316,7 @@ static int handle_recv_3(void *object, IP_Port source, uint8_t *packet, uint32_t
312{ 316{
313 Onion *onion = object; 317 Onion *onion = object;
314 318
315 if (length > MAX_ONION_SIZE) 319 if (length > ONION_MAX_PACKET_SIZE)
316 return 1; 320 return 1;
317 321
318 if (length <= 1 + RETURN_3) 322 if (length <= 1 + RETURN_3)
@@ -330,7 +334,7 @@ static int handle_recv_3(void *object, IP_Port source, uint8_t *packet, uint32_t
330 IP_Port send_to; 334 IP_Port send_to;
331 memcpy(&send_to, plain, sizeof(IP_Port)); 335 memcpy(&send_to, plain, sizeof(IP_Port));
332 336
333 uint8_t data[MAX_ONION_SIZE]; 337 uint8_t data[ONION_MAX_PACKET_SIZE];
334 data[0] = NET_PACKET_ONION_RECV_2; 338 data[0] = NET_PACKET_ONION_RECV_2;
335 memcpy(data + 1, plain + sizeof(IP_Port), RETURN_2); 339 memcpy(data + 1, plain + sizeof(IP_Port), RETURN_2);
336 memcpy(data + 1 + RETURN_2, packet + 1 + RETURN_3, length - (1 + RETURN_3)); 340 memcpy(data + 1 + RETURN_2, packet + 1 + RETURN_3, length - (1 + RETURN_3));
@@ -346,7 +350,7 @@ static int handle_recv_2(void *object, IP_Port source, uint8_t *packet, uint32_t
346{ 350{
347 Onion *onion = object; 351 Onion *onion = object;
348 352
349 if (length > MAX_ONION_SIZE) 353 if (length > ONION_MAX_PACKET_SIZE)
350 return 1; 354 return 1;
351 355
352 if (length <= 1 + RETURN_2) 356 if (length <= 1 + RETURN_2)
@@ -364,7 +368,7 @@ static int handle_recv_2(void *object, IP_Port source, uint8_t *packet, uint32_t
364 IP_Port send_to; 368 IP_Port send_to;
365 memcpy(&send_to, plain, sizeof(IP_Port)); 369 memcpy(&send_to, plain, sizeof(IP_Port));
366 370
367 uint8_t data[MAX_ONION_SIZE]; 371 uint8_t data[ONION_MAX_PACKET_SIZE];
368 data[0] = NET_PACKET_ONION_RECV_1; 372 data[0] = NET_PACKET_ONION_RECV_1;
369 memcpy(data + 1, plain + sizeof(IP_Port), RETURN_1); 373 memcpy(data + 1, plain + sizeof(IP_Port), RETURN_1);
370 memcpy(data + 1 + RETURN_1, packet + 1 + RETURN_2, length - (1 + RETURN_2)); 374 memcpy(data + 1 + RETURN_1, packet + 1 + RETURN_2, length - (1 + RETURN_2));
@@ -380,7 +384,7 @@ static int handle_recv_1(void *object, IP_Port source, uint8_t *packet, uint32_t
380{ 384{
381 Onion *onion = object; 385 Onion *onion = object;
382 386
383 if (length > MAX_ONION_SIZE) 387 if (length > ONION_MAX_PACKET_SIZE)
384 return 1; 388 return 1;
385 389
386 if (length <= 1 + RETURN_1) 390 if (length <= 1 + RETURN_1)
@@ -424,7 +428,7 @@ Onion *new_onion(DHT *dht)
424 return NULL; 428 return NULL;
425 429
426 onion->dht = dht; 430 onion->dht = dht;
427 onion->net = dht->c->lossless_udp->net; 431 onion->net = dht->net;
428 new_symmetric_key(onion->secret_symmetric_key); 432 new_symmetric_key(onion->secret_symmetric_key);
429 onion->timestamp = unix_time(); 433 onion->timestamp = unix_time();
430 434
diff --git a/toxcore/onion.h b/toxcore/onion.h
index b15caad9..712bbf13 100644
--- a/toxcore/onion.h
+++ b/toxcore/onion.h
@@ -39,6 +39,8 @@ typedef struct {
39 void *callback_object; 39 void *callback_object;
40} Onion; 40} Onion;
41 41
42#define ONION_MAX_PACKET_SIZE 1400
43
42#define ONION_RETURN_1 (crypto_box_NONCEBYTES + sizeof(IP_Port) + crypto_box_MACBYTES) 44#define ONION_RETURN_1 (crypto_box_NONCEBYTES + sizeof(IP_Port) + crypto_box_MACBYTES)
43#define ONION_RETURN_2 (crypto_box_NONCEBYTES + sizeof(IP_Port) + crypto_box_MACBYTES + ONION_RETURN_1) 45#define ONION_RETURN_2 (crypto_box_NONCEBYTES + sizeof(IP_Port) + crypto_box_MACBYTES + ONION_RETURN_1)
44#define ONION_RETURN_3 (crypto_box_NONCEBYTES + sizeof(IP_Port) + crypto_box_MACBYTES + ONION_RETURN_2) 46#define ONION_RETURN_3 (crypto_box_NONCEBYTES + sizeof(IP_Port) + crypto_box_MACBYTES + ONION_RETURN_2)
@@ -48,6 +50,9 @@ typedef struct {
48#define ONION_SEND_2 (crypto_box_NONCEBYTES + ONION_SEND_BASE*2 + ONION_RETURN_1) 50#define ONION_SEND_2 (crypto_box_NONCEBYTES + ONION_SEND_BASE*2 + ONION_RETURN_1)
49#define ONION_SEND_1 (crypto_box_NONCEBYTES + ONION_SEND_BASE*3) 51#define ONION_SEND_1 (crypto_box_NONCEBYTES + ONION_SEND_BASE*3)
50 52
53#define ONION_MAX_DATA_SIZE (ONION_MAX_PACKET_SIZE - (ONION_SEND_1 + 1))
54#define ONION_RESPONSE_MAX_DATA_SIZE (ONION_MAX_PACKET_SIZE - (1 + ONION_RETURN_3))
55
51typedef struct { 56typedef struct {
52 uint8_t shared_key1[crypto_box_BEFORENMBYTES]; 57 uint8_t shared_key1[crypto_box_BEFORENMBYTES];
53 uint8_t shared_key2[crypto_box_BEFORENMBYTES]; 58 uint8_t shared_key2[crypto_box_BEFORENMBYTES];
@@ -76,6 +81,7 @@ int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes);
76/* Create and send a onion packet. 81/* Create and send a onion packet.
77 * 82 *
78 * Use Onion_Path path to send data of length to dest. 83 * Use Onion_Path path to send data of length to dest.
84 * Maximum length of data is ONION_MAX_DATA_SIZE.
79 * 85 *
80 * return -1 on failure. 86 * return -1 on failure.
81 * return 0 on success. 87 * return 0 on success.
@@ -83,6 +89,7 @@ int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes);
83int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length); 89int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length);
84 90
85/* Create and send a onion response sent initially to dest with. 91/* Create and send a onion response sent initially to dest with.
92 * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
86 * 93 *
87 * return -1 on failure. 94 * return -1 on failure.
88 * return 0 on success. 95 * return 0 on success.
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
index 5bab931a..1e73aef9 100644
--- a/toxcore/onion_announce.c
+++ b/toxcore/onion_announce.c
@@ -32,7 +32,7 @@
32#define ANNOUNCE_REQUEST_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_MACBYTES) 32#define ANNOUNCE_REQUEST_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_MACBYTES)
33#define ANNOUNCE_REQUEST_SIZE_RECV (ANNOUNCE_REQUEST_SIZE + ONION_RETURN_3) 33#define ANNOUNCE_REQUEST_SIZE_RECV (ANNOUNCE_REQUEST_SIZE + ONION_RETURN_3)
34 34
35#define DATA_REQUEST_MIN_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) 35#define DATA_REQUEST_MIN_SIZE ONION_DATA_REQUEST_MIN_SIZE
36#define DATA_REQUEST_MIN_SIZE_RECV (DATA_REQUEST_MIN_SIZE + ONION_RETURN_3) 36#define DATA_REQUEST_MIN_SIZE_RECV (DATA_REQUEST_MIN_SIZE + ONION_RETURN_3)
37 37
38/* Create and send an onion announce request packet. 38/* Create and send an onion announce request packet.
@@ -90,6 +90,9 @@ int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format de
90int send_data_request(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *public_key, 90int send_data_request(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *public_key,
91 uint8_t *encrypt_public_key, uint8_t *nonce, uint8_t *data, uint16_t length) 91 uint8_t *encrypt_public_key, uint8_t *nonce, uint8_t *data, uint16_t length)
92{ 92{
93 if (DATA_REQUEST_MIN_SIZE + length > ONION_MAX_DATA_SIZE)
94 return -1;
95
93 uint8_t packet[DATA_REQUEST_MIN_SIZE + length]; 96 uint8_t packet[DATA_REQUEST_MIN_SIZE + length];
94 packet[0] = NET_PACKET_ONION_DATA_REQUEST; 97 packet[0] = NET_PACKET_ONION_DATA_REQUEST;
95 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); 98 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
@@ -304,7 +307,7 @@ static int handle_data_request(void *object, IP_Port source, uint8_t *packet, ui
304 if (length <= DATA_REQUEST_MIN_SIZE_RECV) 307 if (length <= DATA_REQUEST_MIN_SIZE_RECV)
305 return 1; 308 return 1;
306 309
307 if (length >= MAX_DATA_SIZE) 310 if (length > ONION_MAX_PACKET_SIZE)
308 return 1; 311 return 1;
309 312
310 int index = in_entries(onion_a, packet + 1); 313 int index = in_entries(onion_a, packet + 1);
diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h
index 071dc9ff..dff61e99 100644
--- a/toxcore/onion_announce.h
+++ b/toxcore/onion_announce.h
@@ -40,6 +40,9 @@
40#error announce response packets assume that ONION_PING_ID_SIZE is equal to crypto_box_PUBLICKEYBYTES 40#error announce response packets assume that ONION_PING_ID_SIZE is equal to crypto_box_PUBLICKEYBYTES
41#endif 41#endif
42 42
43#define ONION_DATA_REQUEST_MIN_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)
44#define MAX_DATA_REQUEST_SIZE (ONION_MAX_DATA_SIZE - ONION_DATA_REQUEST_MIN_SIZE)
45
43typedef struct { 46typedef struct {
44 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 47 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
45 IP_Port ret_ip_port; 48 IP_Port ret_ip_port;
@@ -86,6 +89,8 @@ int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format de
86 * 89 *
87 * nonce is the nonce to encrypt this packet with 90 * nonce is the nonce to encrypt this packet with
88 * 91 *
92 * The maximum length of data is MAX_DATA_REQUEST_SIZE.
93 *
89 * return -1 on failure. 94 * return -1 on failure.
90 * return 0 on success. 95 * return 0 on success.
91 */ 96 */
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index fbb00a2c..35fb4423 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -183,9 +183,9 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
183 if (random_path(onion_c->dht, &onion_c->onion_paths, pathnum, &path) == -1) 183 if (random_path(onion_c->dht, &onion_c->onion_paths, pathnum, &path) == -1)
184 return -1; 184 return -1;
185 185
186 return send_announce_request(onion_c->net, &path, dest_node, onion_c->dht->c->self_public_key, 186 return send_announce_request(onion_c->net, &path, dest_node, onion_c->c->self_public_key,
187 onion_c->dht->c->self_secret_key, ping_id, 187 onion_c->c->self_secret_key, ping_id,
188 onion_c->dht->c->self_public_key, onion_c->temp_public_key, sendback); 188 onion_c->c->self_public_key, onion_c->temp_public_key, sendback);
189 } else { 189 } else {
190 if (random_path(onion_c->dht, &onion_c->friends_list[num - 1].onion_paths, pathnum, &path) == -1) 190 if (random_path(onion_c->dht, &onion_c->friends_list[num - 1].onion_paths, pathnum, &path) == -1)
191 return -1; 191 return -1;
@@ -236,7 +236,7 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, uint8_t *publ
236 236
237 if (num == 0) { 237 if (num == 0) {
238 list_nodes = onion_c->clients_announce_list; 238 list_nodes = onion_c->clients_announce_list;
239 reference_id = onion_c->dht->c->self_public_key; 239 reference_id = onion_c->c->self_public_key;
240 240
241 if (is_stored && memcmp(pingid_or_key, onion_c->temp_public_key, crypto_box_PUBLICKEYBYTES) != 0) { 241 if (is_stored && memcmp(pingid_or_key, onion_c->temp_public_key, crypto_box_PUBLICKEYBYTES) != 0) {
242 is_stored = 0; 242 is_stored = 0;
@@ -325,7 +325,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n
325 325
326 if (num == 0) { 326 if (num == 0) {
327 list_nodes = onion_c->clients_announce_list; 327 list_nodes = onion_c->clients_announce_list;
328 reference_id = onion_c->dht->c->self_public_key; 328 reference_id = onion_c->c->self_public_key;
329 ping_nodes_sent_second = &onion_c->ping_nodes_sent_second; 329 ping_nodes_sent_second = &onion_c->ping_nodes_sent_second;
330 last_pinged = onion_c->last_pinged; 330 last_pinged = onion_c->last_pinged;
331 last_pinged_index = &onion_c->last_pinged_index; 331 last_pinged_index = &onion_c->last_pinged_index;
@@ -388,7 +388,7 @@ static int handle_announce_response(void *object, IP_Port source, uint8_t *packe
388 int len = -1; 388 int len = -1;
389 389
390 if (num == 0) { 390 if (num == 0) {
391 len = decrypt_data(public_key, onion_c->dht->c->self_secret_key, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH, 391 len = decrypt_data(public_key, onion_c->c->self_secret_key, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH,
392 packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES, 392 packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES,
393 length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES), plain); 393 length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES), plain);
394 } else { 394 } else {
@@ -421,7 +421,7 @@ static int handle_announce_response(void *object, IP_Port source, uint8_t *packe
421 return 0; 421 return 0;
422} 422}
423 423
424#define DATA_IN_RESPONSE_MIN_SIZE (crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) 424#define DATA_IN_RESPONSE_MIN_SIZE ONION_DATA_IN_RESPONSE_MIN_SIZE
425 425
426static int handle_data_response(void *object, IP_Port source, uint8_t *packet, uint32_t length) 426static int handle_data_response(void *object, IP_Port source, uint8_t *packet, uint32_t length)
427{ 427{
@@ -430,7 +430,7 @@ static int handle_data_response(void *object, IP_Port source, uint8_t *packet, u
430 if (length <= (ONION_DATA_RESPONSE_MIN_SIZE + DATA_IN_RESPONSE_MIN_SIZE)) 430 if (length <= (ONION_DATA_RESPONSE_MIN_SIZE + DATA_IN_RESPONSE_MIN_SIZE))
431 return 1; 431 return 1;
432 432
433 if (length > MAX_DATA_SIZE) 433 if (length > MAX_DATA_REQUEST_SIZE)
434 return 1; 434 return 1;
435 435
436 uint8_t temp_plain[length - ONION_DATA_RESPONSE_MIN_SIZE]; 436 uint8_t temp_plain[length - ONION_DATA_RESPONSE_MIN_SIZE];
@@ -442,7 +442,7 @@ static int handle_data_response(void *object, IP_Port source, uint8_t *packet, u
442 return 1; 442 return 1;
443 443
444 uint8_t plain[sizeof(temp_plain) - DATA_IN_RESPONSE_MIN_SIZE]; 444 uint8_t plain[sizeof(temp_plain) - DATA_IN_RESPONSE_MIN_SIZE];
445 len = decrypt_data(temp_plain, onion_c->dht->c->self_secret_key, packet + 1, temp_plain + crypto_box_PUBLICKEYBYTES, 445 len = decrypt_data(temp_plain, onion_c->c->self_secret_key, packet + 1, temp_plain + crypto_box_PUBLICKEYBYTES,
446 sizeof(temp_plain) - crypto_box_PUBLICKEYBYTES, plain); 446 sizeof(temp_plain) - crypto_box_PUBLICKEYBYTES, plain);
447 447
448 if ((uint32_t)len != sizeof(plain)) 448 if ((uint32_t)len != sizeof(plain))
@@ -527,7 +527,7 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, uint8_t *data, uint32
527 if ((uint32_t)friend_num >= onion_c->num_friends) 527 if ((uint32_t)friend_num >= onion_c->num_friends)
528 return -1; 528 return -1;
529 529
530 if (length + DATA_IN_RESPONSE_MIN_SIZE + ONION_DATA_RESPONSE_MIN_SIZE + ONION_SEND_1 > MAX_DATA_SIZE) 530 if (length + DATA_IN_RESPONSE_MIN_SIZE > MAX_DATA_REQUEST_SIZE)
531 return -1; 531 return -1;
532 532
533 if (length == 0) 533 if (length == 0)
@@ -537,8 +537,8 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, uint8_t *data, uint32
537 random_nonce(nonce); 537 random_nonce(nonce);
538 538
539 uint8_t packet[DATA_IN_RESPONSE_MIN_SIZE + length]; 539 uint8_t packet[DATA_IN_RESPONSE_MIN_SIZE + length];
540 memcpy(packet, onion_c->dht->c->self_public_key, crypto_box_PUBLICKEYBYTES); 540 memcpy(packet, onion_c->c->self_public_key, crypto_box_PUBLICKEYBYTES);
541 int len = encrypt_data(onion_c->friends_list[friend_num].real_client_id, onion_c->dht->c->self_secret_key, nonce, data, 541 int len = encrypt_data(onion_c->friends_list[friend_num].real_client_id, onion_c->c->self_secret_key, nonce, data,
542 length, packet + crypto_box_PUBLICKEYBYTES); 542 length, packet + crypto_box_PUBLICKEYBYTES);
543 543
544 if ((uint32_t)len + crypto_box_PUBLICKEYBYTES != sizeof(packet)) 544 if ((uint32_t)len + crypto_box_PUBLICKEYBYTES != sizeof(packet))
@@ -597,15 +597,15 @@ static int send_dht_fakeid(Onion_Client *onion_c, int friend_num, uint8_t *data,
597 new_nonce(nonce); 597 new_nonce(nonce);
598 598
599 uint8_t temp[DATA_IN_RESPONSE_MIN_SIZE + crypto_box_NONCEBYTES + length]; 599 uint8_t temp[DATA_IN_RESPONSE_MIN_SIZE + crypto_box_NONCEBYTES + length];
600 memcpy(temp, onion_c->dht->c->self_public_key, crypto_box_PUBLICKEYBYTES); 600 memcpy(temp, onion_c->c->self_public_key, crypto_box_PUBLICKEYBYTES);
601 memcpy(temp + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); 601 memcpy(temp + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
602 int len = encrypt_data(onion_c->friends_list[friend_num].real_client_id, onion_c->dht->c->self_secret_key, nonce, data, 602 int len = encrypt_data(onion_c->friends_list[friend_num].real_client_id, onion_c->c->self_secret_key, nonce, data,
603 length, temp + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); 603 length, temp + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
604 604
605 if ((uint32_t)len + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES != sizeof(temp)) 605 if ((uint32_t)len + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES != sizeof(temp))
606 return -1; 606 return -1;
607 607
608 uint8_t packet[MAX_DATA_SIZE]; 608 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
609 len = create_request(onion_c->dht->self_public_key, onion_c->dht->self_secret_key, packet, 609 len = create_request(onion_c->dht->self_public_key, onion_c->dht->self_secret_key, packet,
610 onion_c->friends_list[friend_num].fake_client_id, temp, sizeof(temp), FAKEID_DATA_ID); 610 onion_c->friends_list[friend_num].fake_client_id, temp, sizeof(temp), FAKEID_DATA_ID);
611 611
@@ -626,7 +626,7 @@ static int handle_dht_fakeid(void *object, IP_Port source, uint8_t *source_pubke
626 return 1; 626 return 1;
627 627
628 uint8_t plain[FAKEID_DATA_MAX_LENGTH]; 628 uint8_t plain[FAKEID_DATA_MAX_LENGTH];
629 int len = decrypt_data(packet, onion_c->dht->c->self_secret_key, packet + crypto_box_PUBLICKEYBYTES, 629 int len = decrypt_data(packet, onion_c->c->self_secret_key, packet + crypto_box_PUBLICKEYBYTES,
630 packet + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, 630 packet + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
631 length - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES), plain); 631 length - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES), plain);
632 632
@@ -960,7 +960,7 @@ static void do_announce(Onion_Client *onion_c)
960 if (count != MAX_ONION_CLIENTS) { 960 if (count != MAX_ONION_CLIENTS) {
961 if (count < (uint32_t)rand() % MAX_ONION_CLIENTS) { 961 if (count < (uint32_t)rand() % MAX_ONION_CLIENTS) {
962 Node_format nodes_list[MAX_SENT_NODES]; 962 Node_format nodes_list[MAX_SENT_NODES];
963 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->dht->c->self_public_key, nodes_list, 963 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->c->self_public_key, nodes_list,
964 rand() % 2 ? AF_INET : AF_INET6, 1, 0); 964 rand() % 2 ? AF_INET : AF_INET6, 1, 0);
965 965
966 for (i = 0; i < num_nodes; ++i) { 966 for (i = 0; i < num_nodes; ++i) {
@@ -989,9 +989,9 @@ void do_onion_client(Onion_Client *onion_c)
989 onion_c->last_run = unix_time(); 989 onion_c->last_run = unix_time();
990} 990}
991 991
992Onion_Client *new_onion_client(DHT *dht) 992Onion_Client *new_onion_client(Net_Crypto *c)
993{ 993{
994 if (dht == NULL) 994 if (c == NULL)
995 return NULL; 995 return NULL;
996 996
997 Onion_Client *onion_c = calloc(1, sizeof(Onion_Client)); 997 Onion_Client *onion_c = calloc(1, sizeof(Onion_Client));
@@ -999,14 +999,15 @@ Onion_Client *new_onion_client(DHT *dht)
999 if (onion_c == NULL) 999 if (onion_c == NULL)
1000 return NULL; 1000 return NULL;
1001 1001
1002 onion_c->dht = dht; 1002 onion_c->dht = c->dht;
1003 onion_c->net = dht->c->lossless_udp->net; 1003 onion_c->net = c->dht->net;
1004 onion_c->c = c;
1004 new_symmetric_key(onion_c->secret_symmetric_key); 1005 new_symmetric_key(onion_c->secret_symmetric_key);
1005 crypto_box_keypair(onion_c->temp_public_key, onion_c->temp_secret_key); 1006 crypto_box_keypair(onion_c->temp_public_key, onion_c->temp_secret_key);
1006 networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_announce_response, onion_c); 1007 networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_announce_response, onion_c);
1007 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_data_response, onion_c); 1008 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_data_response, onion_c);
1008 oniondata_registerhandler(onion_c, FAKEID_DATA_ID, &handle_fakeid_announce, onion_c); 1009 oniondata_registerhandler(onion_c, FAKEID_DATA_ID, &handle_fakeid_announce, onion_c);
1009 cryptopacket_registerhandler(onion_c->dht->c, FAKEID_DATA_ID, &handle_dht_fakeid, onion_c); 1010 cryptopacket_registerhandler(onion_c->c, FAKEID_DATA_ID, &handle_dht_fakeid, onion_c);
1010 1011
1011 return onion_c; 1012 return onion_c;
1012} 1013}
@@ -1020,7 +1021,7 @@ void kill_onion_client(Onion_Client *onion_c)
1020 networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, NULL, NULL); 1021 networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, NULL, NULL);
1021 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL); 1022 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL);
1022 oniondata_registerhandler(onion_c, FAKEID_DATA_ID, NULL, NULL); 1023 oniondata_registerhandler(onion_c, FAKEID_DATA_ID, NULL, NULL);
1023 cryptopacket_registerhandler(onion_c->dht->c, FAKEID_DATA_ID, NULL, NULL); 1024 cryptopacket_registerhandler(onion_c->c, FAKEID_DATA_ID, NULL, NULL);
1024 memset(onion_c, 0, sizeof(Onion_Client)); 1025 memset(onion_c, 0, sizeof(Onion_Client));
1025 free(onion_c); 1026 free(onion_c);
1026} 1027}
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index 8e09b63a..6b960e40 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -25,6 +25,7 @@
25#define ONION_CLIENT_H 25#define ONION_CLIENT_H
26 26
27#include "onion_announce.h" 27#include "onion_announce.h"
28#include "net_crypto.h"
28 29
29#define MAX_ONION_CLIENTS 8 30#define MAX_ONION_CLIENTS 8
30#define ONION_NODE_PING_INTERVAL 30 31#define ONION_NODE_PING_INTERVAL 30
@@ -105,6 +106,7 @@ typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey,
105 106
106typedef struct { 107typedef struct {
107 DHT *dht; 108 DHT *dht;
109 Net_Crypto *c;
108 Networking_Core *net; 110 Networking_Core *net;
109 Onion_Friend *friends_list; 111 Onion_Friend *friends_list;
110 uint16_t num_friends; 112 uint16_t num_friends;
@@ -170,8 +172,11 @@ int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_on
170 */ 172 */
171int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port); 173int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port);
172 174
175#define ONION_DATA_IN_RESPONSE_MIN_SIZE (crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)
176#define ONION_CLIENT_MAX_DATA_SIZE (MAX_DATA_REQUEST_SIZE - ONION_DATA_IN_RESPONSE_MIN_SIZE)
173 177
174/* Send data of length length to friendnum. 178/* Send data of length length to friendnum.
179 * Maximum length of data is ONION_CLIENT_MAX_DATA_SIZE.
175 * This data will be recieved by the friend using the Onion_Data_Handlers callbacks. 180 * This data will be recieved by the friend using the Onion_Data_Handlers callbacks.
176 * 181 *
177 * Even if this function succeeds, the friend might not recieve any data. 182 * Even if this function succeeds, the friend might not recieve any data.
@@ -186,7 +191,7 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha
186 191
187void do_onion_client(Onion_Client *onion_c); 192void do_onion_client(Onion_Client *onion_c);
188 193
189Onion_Client *new_onion_client(DHT *dht); 194Onion_Client *new_onion_client(Net_Crypto *c);
190 195
191void kill_onion_client(Onion_Client *onion_c); 196void kill_onion_client(Onion_Client *onion_c);
192 197