summaryrefslogtreecommitdiff
path: root/core/net_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/net_crypto.c')
-rw-r--r--core/net_crypto.c33
1 files changed, 15 insertions, 18 deletions
diff --git a/core/net_crypto.c b/core/net_crypto.c
index dcf36a12..2e63d2f1 100644
--- a/core/net_crypto.c
+++ b/core/net_crypto.c
@@ -265,34 +265,35 @@ static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uin
265 return -1; 265 return -1;
266} 266}
267 267
268void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb) 268void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object)
269{ 269{
270 c->cryptopackethandlers[byte] = cb; 270 c->cryptopackethandlers[byte].function = cb;
271 c->cryptopackethandlers[byte].object = object;
271} 272}
272 273
273static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, uint32_t length) 274static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, uint32_t length)
274{ 275{
275 Net_Crypto *c = object; 276 DHT *dht = object;
276 if (packet[0] == 32) { 277 if (packet[0] == 32) {
277 if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || 278 if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
278 length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 279 length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
279 return 1; 280 return 1;
280 281
281 if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us. 282 if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
282 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 283 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
283 uint8_t data[MAX_DATA_SIZE]; 284 uint8_t data[MAX_DATA_SIZE];
284 uint8_t number; 285 uint8_t number;
285 int len = handle_request(c, public_key, data, &number, packet, length); 286 int len = handle_request(dht->c, public_key, data, &number, packet, length);
286 287
287 if (len == -1 || len == 0) 288 if (len == -1 || len == 0)
288 return 1; 289 return 1;
289 290
290 if (!c->cryptopackethandlers[number]) return 1; 291 if (!dht->c->cryptopackethandlers[number].function) return 1;
291 292
292 c->cryptopackethandlers[number](source, public_key, data, len); 293 dht->c->cryptopackethandlers[number].function(dht->c->cryptopackethandlers[number].object, source, public_key, data, len);
293 294
294 } else { /* if request is not for us, try routing it. */ 295 } else { /* if request is not for us, try routing it. */
295 if (route_packet(packet + 1, packet, length) == length) //NOTE 296 if (route_packet(dht, packet + 1, packet, length) == length) //NOTE
296 return 0; 297 return 0;
297 } 298 }
298 } 299 }
@@ -572,17 +573,9 @@ int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
572 return CONN_NO_CONNECTION; 573 return CONN_NO_CONNECTION;
573} 574}
574 575
575/* Generate our public and private keys
576 Only call this function the first time the program starts. */
577
578extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];//TODO: Remove this
579extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
580
581void new_keys(Net_Crypto *c) 576void new_keys(Net_Crypto *c)
582{ 577{
583 crypto_box_keypair(c->self_public_key, c->self_secret_key); 578 crypto_box_keypair(c->self_public_key, c->self_secret_key);
584 memcpy(self_public_key, c->self_public_key, crypto_box_PUBLICKEYBYTES);
585 memcpy(self_secret_key, c->self_secret_key, crypto_box_PUBLICKEYBYTES);
586} 579}
587 580
588/* save the public and private keys to the keys array 581/* save the public and private keys to the keys array
@@ -713,12 +706,16 @@ Net_Crypto * new_net_crypto(Networking_Core * net)
713 if (temp->lossless_udp == NULL) 706 if (temp->lossless_udp == NULL)
714 return NULL; 707 return NULL;
715 memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING); 708 memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING);
716 //networking_registerhandler(temp, 32, &cryptopacket_handle);
717 networking_registerhandler(net, 32, &cryptopacket_handle, temp);
718 temp_net_crypto = temp; //TODO remove 709 temp_net_crypto = temp; //TODO remove
719 return temp; 710 return temp;
720} 711}
721 712
713void init_cryptopackets(void *dht)
714{
715 DHT *s_dht = dht;
716 networking_registerhandler(s_dht->c->lossless_udp->net, 32, &cryptopacket_handle, s_dht);
717}
718
722static void kill_timedout(Net_Crypto *c) 719static void kill_timedout(Net_Crypto *c)
723{ 720{
724 uint32_t i; 721 uint32_t i;