summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-01-15 20:47:26 -0500
committerirungentoo <irungentoo@gmail.com>2014-01-15 20:47:26 -0500
commitaff78b159c102ca3a151b1ce02ae258341f9d003 (patch)
tree8af96155ab6f3656041601fae87d33ad95d18219 /toxcore
parentb345bcea8b1c41b7312e793515040a5a4abad230 (diff)
onion_client almost done.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c38
-rw-r--r--toxcore/DHT.h11
-rw-r--r--toxcore/onion_client.c38
-rw-r--r--toxcore/onion_client.h6
4 files changed, 86 insertions, 7 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index e5fe402d..341973a4 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -2021,6 +2021,44 @@ Node_format random_node(DHT *dht, sa_family_t sa_family)
2021 return nodes_list[rand() % num_nodes]; 2021 return nodes_list[rand() % num_nodes];
2022} 2022}
2023 2023
2024/* Put up to max_num nodes in nodes from the closelist.
2025 *
2026 * return the number of nodes.
2027 */
2028uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
2029{
2030 if (max_num == 0)
2031 return 0;
2032
2033 uint16_t count = 0;
2034 Client_data *list = dht->close_clientlist;
2035
2036 uint32_t i;
2037 for (i = LCLIENT_LIST; i != 0; --i) {
2038 IPPTsPng *assoc = NULL;
2039
2040 if (!is_timeout(list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT))
2041 assoc = &list[i - 1].assoc4;
2042
2043 if (!is_timeout(list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) {
2044 if (assoc == NULL)
2045 assoc = &list[i - 1].assoc6;
2046 else if (rand() % 2)
2047 assoc = &list[i - 1].assoc6;
2048 }
2049
2050 if (assoc != NULL) {
2051 memcpy(nodes[count].client_id, list[i - 1].client_id, CLIENT_ID_SIZE);
2052 nodes[count].ip_port = assoc->ip_port;
2053 ++count;
2054
2055 if (count >= max_num)
2056 return count;
2057 }
2058 }
2059
2060 return count;
2061}
2024 2062
2025void do_hardening(DHT *dht) 2063void do_hardening(DHT *dht)
2026{ 2064{
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index c42bceb8..f572c1fc 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -48,9 +48,9 @@
48#define PING_INTERVAL 60 48#define PING_INTERVAL 60
49 49
50/* The number of seconds for a non responsive node to become bad. */ 50/* The number of seconds for a non responsive node to become bad. */
51#define PINGS_MISSED_NODE_GOES_BAD 3 51#define PINGS_MISSED_NODE_GOES_BAD 1
52#define PING_ROUNDTRIP 2 52#define PING_ROUNDTRIP 2
53#define BAD_NODE_TIMEOUT (PING_INTERVAL + PINGS_MISSED_NODE_GOES_BAD * PING_INTERVAL + PING_ROUNDTRIP) 53#define BAD_NODE_TIMEOUT (PING_INTERVAL + PINGS_MISSED_NODE_GOES_BAD * (PING_INTERVAL + PING_ROUNDTRIP))
54 54
55/* Redefinitions of variables for safe transfer over wire. */ 55/* Redefinitions of variables for safe transfer over wire. */
56#define TOX_AF_INET 2 56#define TOX_AF_INET 2
@@ -220,6 +220,13 @@ int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2);
220int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN, 220int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN,
221 uint8_t want_good); 221 uint8_t want_good);
222 222
223
224/* Put up to max_num nodes in nodes from the closelist.
225 *
226 * return the number of nodes.
227 */
228uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num);
229
223/* Run this function at least a couple times per second (It's the main loop). */ 230/* Run this function at least a couple times per second (It's the main loop). */
224void do_DHT(DHT *dht); 231void do_DHT(DHT *dht);
225 232
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 93dd06cd..9363bbd7 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -359,6 +359,8 @@ static int handle_fakeid_announce(void *object, uint8_t *source_pubkey, uint8_t
359 if (DHT_addfriend(onion_c->dht, data + 1) == 1) { 359 if (DHT_addfriend(onion_c->dht, data + 1) == 1) {
360 return 1; 360 return 1;
361 } 361 }
362
363 memcpy(onion_c->friends_list[friend_num].fake_client_id, data + 1, crypto_box_PUBLICKEYBYTES);
362 } 364 }
363 365
364 uint16_t num_nodes = (length - FAKEID_DATA_MIN_LENGTH) / sizeof(Node_format); 366 uint16_t num_nodes = (length - FAKEID_DATA_MIN_LENGTH) / sizeof(Node_format);
@@ -368,7 +370,7 @@ static int handle_fakeid_announce(void *object, uint8_t *source_pubkey, uint8_t
368 370
369 for (i = 0; i < num_nodes; ++i) { 371 for (i = 0; i < num_nodes; ++i) {
370 to_host_family(&nodes[i].ip_port.ip); 372 to_host_family(&nodes[i].ip_port.ip);
371 DHT_bootstrap(onion_c->dht, nodes[i].ip_port, nodes[i].client_id); 373 DHT_getnodes(onion_c->dht, &nodes[i].ip_port, nodes[i].client_id, onion_c->friends_list[friend_num].fake_client_id);
372 } 374 }
373 375
374 //TODO replay protection 376 //TODO replay protection
@@ -429,6 +431,24 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, uint8_t *data, uint32
429 return good; 431 return good;
430} 432}
431 433
434/* Send the packets to tell our friends
435 * return the number of packets sent on success
436 * return -1 on failure.
437 */
438static int send_fakeid_announce(Onion_Client *onion_c, uint16_t friend_num)
439{
440 if (friend_num >= onion_c->num_friends)
441 return -1;
442
443 uint8_t data[FAKEID_DATA_MAX_LENGTH];
444 data[0] = FAKEID_DATA_ID;
445 memcpy(data + 1, onion_c->dht->self_public_key, crypto_box_PUBLICKEYBYTES);
446 Node_format nodes[MAX_SENT_NODES];
447 uint16_t num_nodes = closelist_nodes(onion_c->dht, nodes, MAX_SENT_NODES);
448 memcpy(data + FAKEID_DATA_MIN_LENGTH, nodes, sizeof(Node_format) * num_nodes);
449 return send_onion_data(onion_c, friend_num, data, FAKEID_DATA_MIN_LENGTH + sizeof(Node_format) * num_nodes);
450}
451
432/* Get the friend_num of a friend. 452/* Get the friend_num of a friend.
433 * 453 *
434 * return -1 on failure. 454 * return -1 on failure.
@@ -516,7 +536,7 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num)
516 if ((uint32_t)friend_num >= onion_c->num_friends) 536 if ((uint32_t)friend_num >= onion_c->num_friends)
517 return -1; 537 return -1;
518 538
519 //TODO 539 DHT_delfriend(onion_c->dht, onion_c->friends_list[friend_num].fake_client_id);
520 memset(&(onion_c->friends_list[friend_num]), 0, sizeof(Onion_Friend)); 540 memset(&(onion_c->friends_list[friend_num]), 0, sizeof(Onion_Friend));
521 uint32_t i; 541 uint32_t i;
522 542
@@ -544,8 +564,13 @@ int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port)
544 if ((uint32_t)friend_num >= onion_c->num_friends) 564 if ((uint32_t)friend_num >= onion_c->num_friends)
545 return -1; 565 return -1;
546 566
547 //TODO 567 if (onion_c->friends_list[friend_num].status == 0)
548 return 0; 568 return -1;
569
570 if (DHT_getfriendip(onion_c->dht, onion_c->friends_list[friend_num].fake_client_id, ip_port) == 1)
571 return 0;
572
573 return -1;
549} 574}
550 575
551/* Takes 3 random nodes that we know and puts them in nodes 576/* Takes 3 random nodes that we know and puts them in nodes
@@ -597,7 +622,10 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
597 client_send_announce_request(onion_c, friendnum + 1, nodes_list[i].ip_port, nodes_list[i].client_id, 0); 622 client_send_announce_request(onion_c, friendnum + 1, nodes_list[i].ip_port, nodes_list[i].client_id, 0);
598 } 623 }
599 624
600 //TODO send packets to friend telling them our fake DHT id. 625 /* send packets to friend telling them our fake DHT id. */
626 if (is_timeout(onion_c->friends_list[friendnum].last_fakeid_sent, ONION_FAKEID_INTERVAL))
627 if (send_fakeid_announce(onion_c, friendnum) > 3)
628 onion_c->friends_list[friendnum].last_fakeid_sent = unix_time();
601} 629}
602/* Function to call when onion data packet with contents beginning with byte is received. */ 630/* Function to call when onion data packet with contents beginning with byte is received. */
603void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object) 631void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object)
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index f5940d7c..181bc533 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -28,6 +28,10 @@
28 28
29#define MAX_ONION_CLIENTS 8 29#define MAX_ONION_CLIENTS 8
30#define ONION_NODE_TIMEOUT 200 30#define ONION_NODE_TIMEOUT 200
31
32/* The interval in seconds at which to tell our friends where we are */
33#define ONION_FAKEID_INTERVAL 60
34
31typedef struct { 35typedef struct {
32 uint8_t client_id[CLIENT_ID_SIZE]; 36 uint8_t client_id[CLIENT_ID_SIZE];
33 IP_Port ip_port; 37 IP_Port ip_port;
@@ -46,6 +50,8 @@ typedef struct {
46 Onion_Node clients_list[MAX_ONION_CLIENTS]; 50 Onion_Node clients_list[MAX_ONION_CLIENTS];
47 uint8_t temp_public_key[crypto_box_PUBLICKEYBYTES]; 51 uint8_t temp_public_key[crypto_box_PUBLICKEYBYTES];
48 uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; 52 uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES];
53
54 uint64_t last_fakeid_sent;
49} Onion_Friend; 55} Onion_Friend;
50 56
51typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len); 57typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len);