summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorplutooo <tfy12vbr@student.lu.se>2013-08-05 15:04:38 -0700
committerplutooo <tfy12vbr@student.lu.se>2013-08-06 08:51:37 -0700
commit99d9ecf74cab43ca6ae24ab5dd62a3b758b3e666 (patch)
tree6b62de268dba0f1240d3c98ae9625366eb3385d7 /core
parent071ac463082646189ade6e31bb3f6051516f81b2 (diff)
core: Move send ping packets functions to ping.c
Diffstat (limited to 'core')
-rw-r--r--core/DHT.c84
-rw-r--r--core/packets.h33
-rw-r--r--core/ping.c67
-rw-r--r--core/ping.h3
-rw-r--r--core/util.c11
-rw-r--r--core/util.h3
6 files changed, 121 insertions, 80 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 01abfdd2..fa36d41f 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..b6d3d364
--- /dev/null
+++ b/core/packets.h
@@ -0,0 +1,33 @@
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} __attribute__((packed)) clientid_t;
11
12typedef enum {
13 PACKET_PING_REQ = 0,
14 PACKET_PING_RES = 1
15} packetid_t;
16
17// Ping packet
18typedef struct {
19 uint8_t magic;
20 clientid_t client_id;
21 uint8_t nonce[crypto_box_NONCEBYTES];
22 uint64_t ping_id;
23 uint8_t padding[ENCRYPTION_PADDING];
24} __attribute__((packed)) pingreq_t;
25
26// Pong packet
27typedef struct {
28 uint8_t magic;
29 clientid_t client_id;
30 uint8_t nonce[crypto_box_NONCEBYTES];
31 uint64_t ping_id;
32 uint8_t padding[ENCRYPTION_PADDING];
33} __attribute__((packed)) pingres_t;
diff --git a/core/ping.c b/core/ping.c
index 8a7d534f..646b0839 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,9 +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;
30
31extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c
26 32
27 33
28void init_ping() { 34void init_ping() {
@@ -86,10 +92,63 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n)
86 for(i=0; i<num_pings; i++) { 92 for(i=0; i<num_pings; i++) {
87 id = (pos_pings + i) % PING_NUM_MAX; 93 id = (pos_pings + i) % PING_NUM_MAX;
88 94
89 if(ipp_eq(pings[id].ipp, ipp) && pings[id].id == ping_id) { 95 // ping_id = 0 means match any id
96 if(ipp_eq(pings[id].ipp, ipp) && (ping_id == 0 || pings[id].id == ping_id)) {
90 return true; 97 return true;
91 } 98 }
92 } 99 }
93 100
94 return false; 101 return false;
95} 102}
103
104int send_ping_request(IP_Port ipp, clientid_t* client_id) {
105 pingreq_t pk;
106 int rc;
107 uint64_t ping_id;
108
109 if(is_pinging(ipp, 0) || id_eq(client_id, self_id))
110 return 1;
111
112 // Generate random ping_id
113 ping_id = add_ping(ipp);
114
115 pk.magic = PACKET_PING_REQ;
116 id_cpy(&pk.client_id, self_id); // Our pubkey
117 random_nonce((uint8_t*) &pk.nonce); // Generate random nonce
118
119 // Encrypt ping_id using recipient privkey
120 rc = encrypt_data((uint8_t*) client_id,
121 self_secret_key,
122 (uint8_t*) &pk.nonce,
123 (uint8_t*) &ping_id, sizeof(ping_id),
124 (uint8_t*) &pk.ping_id);
125
126 if(rc != sizeof(ping_id) + ENCRYPTION_PADDING)
127 return 1;
128
129 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
130}
131
132int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id) {
133 pingres_t pk;
134 int rc;
135
136 if(id_eq(client_id, self_id))
137 return 1;
138
139 pk.magic = PACKET_PING_RES;
140 id_cpy(&pk.client_id, self_id); // Our pubkey
141 random_nonce((uint8_t*) &pk.nonce); // Generate random nonce
142
143 // Encrypt ping_id using recipient privkey
144 rc = encrypt_data((uint8_t*) client_id,
145 self_secret_key,
146 (uint8_t*) &pk.nonce,
147 (uint8_t*) &ping_id, sizeof(ping_id),
148 (uint8_t*) &pk.ping_id);
149
150 if(rc != sizeof(ping_id) + ENCRYPTION_PADDING)
151 return 1;
152
153 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
154}
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 6b40dad7..5b560171 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 return time(NULL); 16 return time(NULL);
@@ -29,3 +30,11 @@ uint64_t random_64b() {
29bool ipp_eq(IP_Port a, IP_Port b) { 30bool ipp_eq(IP_Port a, IP_Port b) {
30 return (a.ip.i == b.ip.i) && (a.port == b.port); 31 return (a.ip.i == b.ip.i) && (a.port == b.port);
31} 32}
33
34bool id_eq(clientid_t* dest, clientid_t* src) {
35 return memcmp(dest, src, sizeof(clientid_t)) == 0;
36}
37
38void id_cpy(clientid_t* dest, clientid_t* src) {
39 memcpy(dest, src, sizeof(clientid_t));
40}
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