summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/DHT.c84
-rw-r--r--core/packets.h37
-rw-r--r--core/ping.c71
-rw-r--r--core/ping.h3
-rw-r--r--core/util.c13
-rw-r--r--core/util.h3
6 files changed, 130 insertions, 81 deletions
diff --git a/core/DHT.c b/core/DHT.c
index f0b32df5..be3ac186 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -24,6 +24,7 @@
24/*----------------------------------------------------------------------------------*/ 24/*----------------------------------------------------------------------------------*/
25 25
26#include "DHT.h" 26#include "DHT.h"
27#include "packets.h"
27#include "ping.h" 28#include "ping.h"
28 29
29/* maximum number of clients stored per friend. */ 30/* maximum number of clients stored per friend. */
@@ -472,71 +473,6 @@ static uint64_t add_gettingnodes(IP_Port ip_port)
472 return 0; 473 return 0;
473} 474}
474 475
475/* send a ping request, only works if none has been sent to that ip/port
476 * in the last 5 seconds.
477 */
478static int pingreq(IP_Port ip_port, uint8_t * public_key)
479{
480 /* check if packet is gonna be sent to ourself */
481 if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0))
482 return 1;
483
484 uint64_t ping_id = add_ping(ip_port);
485 if(ping_id == 0)
486 return 1;
487
488 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
489 uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING];
490 uint8_t nonce[crypto_box_NONCEBYTES];
491 random_nonce(nonce);
492
493 int len = encrypt_data( public_key,
494 self_secret_key,
495 nonce,
496 (uint8_t *)&ping_id,
497 sizeof(ping_id),
498 encrypt );
499
500 if(len != sizeof(ping_id) + ENCRYPTION_PADDING)
501 return -1;
502
503 data[0] = 0;
504 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
505 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
506 memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
507
508 return sendpacket(ip_port, data, sizeof(data));
509}
510
511/* send a ping response */
512static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
513{
514 /* check if packet is gonna be sent to ourself */
515 if(id_equal(public_key, self_public_key))
516 return 1;
517
518 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
519 uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING];
520 uint8_t nonce[crypto_box_NONCEBYTES];
521 random_nonce(nonce);
522
523 int len = encrypt_data( public_key,
524 self_secret_key, nonce,
525 (uint8_t *)&ping_id,
526 sizeof(ping_id),
527 encrypt );
528
529 if(len != sizeof(ping_id) + ENCRYPTION_PADDING)
530 return -1;
531
532 data[0] = 1;
533 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
534 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
535 memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
536
537 return sendpacket(ip_port, data, sizeof(data));
538}
539
540/* send a getnodes request */ 476/* send a getnodes request */
541static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) 477static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
542{ 478{
@@ -641,8 +577,8 @@ static int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
641 if(len != sizeof(ping_id)) 577 if(len != sizeof(ping_id))
642 return 1; 578 return 1;
643 579
644 pingres(source, packet + 1, ping_id); 580 send_ping_response(source, (clientid_t*) (packet + 1), ping_id);
645 pingreq(source, packet + 1); /* TODO: make this smarter? */ 581 send_ping_request(source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */
646 582
647 return 0; 583 return 0;
648} 584}
@@ -701,7 +637,7 @@ static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
701 memcpy(&ping_id, plain, sizeof(ping_id)); 637 memcpy(&ping_id, plain, sizeof(ping_id));
702 sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); 638 sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id);
703 639
704 pingreq(source, packet + 1); /* TODO: make this smarter? */ 640 send_ping_request(source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */
705 641
706 return 0; 642 return 0;
707} 643}
@@ -741,7 +677,7 @@ static int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
741 677
742 uint32_t i; 678 uint32_t i;
743 for(i = 0; i < num_nodes; ++i) { 679 for(i = 0; i < num_nodes; ++i) {
744 pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); 680 send_ping_request(nodes_list[i].ip_port, (clientid_t*) &nodes_list[i].client_id);
745 returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); 681 returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
746 } 682 }
747 683
@@ -831,8 +767,8 @@ static void doDHTFriends(void)
831 /* if node is not dead. */ 767 /* if node is not dead. */
832 if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { 768 if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) {
833 if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { 769 if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
834 pingreq( friends_list[i].client_list[j].ip_port, 770 send_ping_request( friends_list[i].client_list[j].ip_port,
835 friends_list[i].client_list[j].client_id ); 771 (clientid_t*) &friends_list[i].client_list[j].client_id );
836 friends_list[i].client_list[j].last_pinged = temp_time; 772 friends_list[i].client_list[j].last_pinged = temp_time;
837 } 773 }
838 /* if node is good. */ 774 /* if node is good. */
@@ -869,8 +805,8 @@ static void doClose(void)
869 /* if node is not dead. */ 805 /* if node is not dead. */
870 if (!is_timeout(temp_time, close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { 806 if (!is_timeout(temp_time, close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) {
871 if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { 807 if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) {
872 pingreq( close_clientlist[i].ip_port, 808 send_ping_request( close_clientlist[i].ip_port,
873 close_clientlist[i].client_id ); 809 (clientid_t*) &close_clientlist[i].client_id );
874 close_clientlist[i].last_pinged = temp_time; 810 close_clientlist[i].last_pinged = temp_time;
875 } 811 }
876 /* if node is good. */ 812 /* if node is good. */
@@ -1151,7 +1087,7 @@ static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t
1151 /*TODO: improve port guessing algorithm*/ 1087 /*TODO: improve port guessing algorithm*/
1152 uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1); 1088 uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1);
1153 IP_Port pinging = {ip, htons(port)}; 1089 IP_Port pinging = {ip, htons(port)};
1154 pingreq(pinging, friends_list[friend_num].client_id); 1090 send_ping_request(pinging, (clientid_t*) &friends_list[friend_num].client_id);
1155 } 1091 }
1156 friends_list[friend_num].punching_index = i; 1092 friends_list[friend_num].punching_index = i;
1157} 1093}
diff --git a/core/packets.h b/core/packets.h
new file mode 100644
index 00000000..222b1425
--- /dev/null
+++ b/core/packets.h
@@ -0,0 +1,37 @@
1/*
2 * packet.h -- Packet structure
3 *
4 * This file is donated to the Tox Project.
5 * Copyright 2013 plutooo
6 */
7
8typedef struct {
9 uint8_t id[CLIENT_ID_SIZE];
10
11} __attribute__((packed)) clientid_t;
12
13typedef enum {
14 PACKET_PING_REQ = 0,
15 PACKET_PING_RES = 1
16
17} packetid_t;
18
19// Ping packet
20typedef struct {
21 uint8_t magic;
22 clientid_t client_id;
23 uint8_t nonce[crypto_box_NONCEBYTES];
24 uint64_t ping_id;
25 uint8_t padding[ENCRYPTION_PADDING];
26
27} __attribute__((packed)) pingreq_t;
28
29// Pong packet
30typedef struct {
31 uint8_t magic;
32 clientid_t client_id;
33 uint8_t nonce[crypto_box_NONCEBYTES];
34 uint64_t ping_id;
35 uint8_t padding[ENCRYPTION_PADDING];
36
37} __attribute__((packed)) pingres_t;
diff --git a/core/ping.c b/core/ping.c
index ffabe221..a687f2fb 100644
--- a/core/ping.c
+++ b/core/ping.c
@@ -8,6 +8,9 @@
8#include <stdbool.h> 8#include <stdbool.h>
9#include <stdint.h> 9#include <stdint.h>
10 10
11#include "DHT.h"
12#include "net_crypto.h"
13#include "packets.h"
11#include "network.h" 14#include "network.h"
12#include "util.h" 15#include "util.h"
13 16
@@ -20,10 +23,12 @@ typedef struct {
20 uint64_t timestamp; 23 uint64_t timestamp;
21} pinged_t; 24} pinged_t;
22 25
23static pinged_t pings[PING_NUM_MAX]; 26static pinged_t pings[PING_NUM_MAX];
24static size_t num_pings; 27static size_t num_pings;
25static size_t pos_pings; 28static size_t pos_pings;
29static clientid_t* self_id = (clientid_t*) &self_public_key;
26 30
31extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c
27 32
28void init_ping() 33void init_ping()
29{ 34{
@@ -51,15 +56,16 @@ static void remove_timeouts() // O(n)
51 new_num--; 56 new_num--;
52 } 57 }
53 // Break here because list is sorted. 58 // Break here because list is sorted.
54 else 59 else {
55 break; 60 break;
61 }
56 } 62 }
57 63
58 num_pings = new_num; 64 num_pings = new_num;
59 pos_pings = new_pos % PING_NUM_MAX; 65 pos_pings = new_pos % PING_NUM_MAX;
60} 66}
61 67
62uint64_t add_ping(IP_Port ipp) // O(n) 68uint64_t add_ping(IP_Port ipp) // O(n)
63{ 69{
64 size_t p; 70 size_t p;
65 71
@@ -94,6 +100,7 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with
94 for (i=0; i<num_pings; i++) { 100 for (i=0; i<num_pings; i++) {
95 id = (pos_pings + i) % PING_NUM_MAX; 101 id = (pos_pings + i) % PING_NUM_MAX;
96 102
103 // ping_id = 0 means match any id
97 if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) { 104 if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) {
98 return true; 105 return true;
99 } 106 }
@@ -101,3 +108,57 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with
101 108
102 return false; 109 return false;
103} 110}
111
112int send_ping_request(IP_Port ipp, clientid_t* client_id)
113{
114 pingreq_t pk;
115 int rc;
116 uint64_t ping_id;
117
118 if (is_pinging(ipp, 0) || id_eq(client_id, self_id))
119 return 1;
120
121 // Generate random ping_id
122 ping_id = add_ping(ipp);
123
124 pk.magic = PACKET_PING_REQ;
125 id_cpy(&pk.client_id, self_id); // Our pubkey
126 random_nonce((uint8_t*) &pk.nonce); // Generate random nonce
127
128 // Encrypt ping_id using recipient privkey
129 rc = encrypt_data((uint8_t*) client_id,
130 self_secret_key,
131 (uint8_t*) &pk.nonce,
132 (uint8_t*) &ping_id, sizeof(ping_id),
133 (uint8_t*) &pk.ping_id);
134
135 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
136 return 1;
137
138 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
139}
140
141int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id)
142{
143 pingres_t pk;
144 int rc;
145
146 if (id_eq(client_id, self_id))
147 return 1;
148
149 pk.magic = PACKET_PING_RES;
150 id_cpy(&pk.client_id, self_id); // Our pubkey
151 random_nonce((uint8_t*) &pk.nonce); // Generate random nonce
152
153 // Encrypt ping_id using recipient privkey
154 rc = encrypt_data((uint8_t*) client_id,
155 self_secret_key,
156 (uint8_t*) &pk.nonce,
157 (uint8_t*) &ping_id, sizeof(ping_id),
158 (uint8_t*) &pk.ping_id);
159
160 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
161 return 1;
162
163 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
164}
diff --git a/core/ping.h b/core/ping.h
index 1d23df97..f2770a00 100644
--- a/core/ping.h
+++ b/core/ping.h
@@ -10,4 +10,5 @@
10void init_ping(); 10void init_ping();
11uint64_t add_ping(IP_Port ipp); 11uint64_t add_ping(IP_Port ipp);
12bool is_pinging(IP_Port ipp, uint64_t ping_id); 12bool is_pinging(IP_Port ipp, uint64_t ping_id);
13 13int send_ping_request(IP_Port ipp, clientid_t* client_id);
14int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id);
diff --git a/core/util.c b/core/util.c
index 4ce9271e..d201bcb4 100644
--- a/core/util.c
+++ b/core/util.c
@@ -9,7 +9,8 @@
9#include <stdint.h> 9#include <stdint.h>
10#include <stdbool.h> 10#include <stdbool.h>
11 11
12#include "network.h" 12#include "DHT.h"
13#include "packets.h"
13 14
14uint64_t now() 15uint64_t now()
15{ 16{
@@ -32,3 +33,13 @@ bool ipp_eq(IP_Port a, IP_Port b)
32{ 33{
33 return (a.ip.i == b.ip.i) && (a.port == b.port); 34 return (a.ip.i == b.ip.i) && (a.port == b.port);
34} 35}
36
37bool id_eq(clientid_t* dest, clientid_t* src)
38{
39 return memcmp(dest, src, sizeof(clientid_t)) == 0;
40}
41
42void id_cpy(clientid_t* dest, clientid_t* src)
43{
44 memcpy(dest, src, sizeof(clientid_t));
45}
diff --git a/core/util.h b/core/util.h
index aab2ead9..a93be08a 100644
--- a/core/util.h
+++ b/core/util.h
@@ -8,3 +8,6 @@
8uint64_t now(); 8uint64_t now();
9uint64_t random_64b(); 9uint64_t random_64b();
10bool ipp_eq(IP_Port a, IP_Port b); 10bool ipp_eq(IP_Port a, IP_Port b);
11bool id_eq(clientid_t* dest, clientid_t* src);
12void id_cpy(clientid_t* dest, clientid_t* src);
13