toxcore/core/DHT.c

1261 lines
39 KiB
C
Raw Normal View History

/* DHT.c
*
* An implementation of the DHT as seen in http://wiki.tox.im/index.php/DHT
*
* Copyright (C) 2013 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*----------------------------------------------------------------------------------*/
#include "DHT.h"
#include "packets.h"
#include "ping.h"
/* the number of seconds for a non responsive node to become bad. */
#define BAD_NODE_TIMEOUT 70
/* the max number of nodes to send with send nodes. */
#define MAX_SENT_NODES 8
/* ping timeout in seconds */
#define PING_TIMEOUT 5
/* The timeout after which a node is discarded completely. */
#define Kill_NODE_TIMEOUT 300
/* ping interval in seconds for each node in our lists. */
#define PING_INTERVAL 60
/* ping interval in seconds for each random sending of a get nodes request. */
#define GET_NODE_INTERVAL 10
#define MAX_PUNCHING_PORTS 32
/*Interval in seconds between punching attempts*/
#define PUNCH_INTERVAL 10
2013-08-21 00:08:55 +08:00
/*Ping newly announced nodes to ping per TIME_TOPING seconds*/
#define TIME_TOPING 5
Client_data *DHT_get_close_list(DHT *dht)
{
2013-08-21 00:08:55 +08:00
return dht->close_clientlist;
}
2013-07-20 11:00:10 +08:00
/* Compares client_id1 and client_id2 with client_id
* return 0 if both are same distance
* return 1 if client_id1 is closer
* return 2 if client_id2 is closer
*/
2013-08-17 01:11:09 +08:00
static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2)
{
size_t i;
uint8_t distance1, distance2;
2013-08-17 01:11:09 +08:00
for (i = 0; i < CLIENT_ID_SIZE; ++i) {
distance1 = abs(id[i] ^ id1[i]);
distance2 = abs(id[i] ^ id2[i]);
2013-08-17 01:11:09 +08:00
if (distance1 < distance2)
return 1;
2013-08-17 01:11:09 +08:00
if (distance1 > distance2)
return 2;
}
2013-08-17 01:11:09 +08:00
return 0;
}
static int ipport_equal(IP_Port a, IP_Port b)
{
2013-08-17 01:11:09 +08:00
return (a.ip.i == b.ip.i) && (a.port == b.port);
}
2013-08-17 01:11:09 +08:00
static int id_equal(uint8_t *a, uint8_t *b)
{
2013-08-17 01:11:09 +08:00
return memcmp(a, b, CLIENT_ID_SIZE) == 0;
}
static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
{
2013-08-17 01:11:09 +08:00
return timestamp + timeout <= time_now;
}
2013-07-20 11:00:10 +08:00
/* check if client with client_id is already in list of length length.
* if it is then set its corresponding timestamp to current time.
* if the id is already in the list with a different ip_port, update it.
* return True(1) or False(0)
*
* TODO: maybe optimize this.
*/
2013-08-17 01:11:09 +08:00
static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port)
2013-06-25 00:23:46 +08:00
{
uint32_t i;
uint64_t temp_time = unix_time();
2013-07-27 07:18:45 +08:00
2013-08-17 01:11:09 +08:00
for (i = 0; i < length; ++i) {
2013-07-23 05:21:58 +08:00
/*If ip_port is assigned to a different client_id replace it*/
2013-08-17 01:11:09 +08:00
if (ipport_equal(list[i].ip_port, ip_port)) {
2013-07-23 05:21:58 +08:00
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
}
2013-07-27 07:18:45 +08:00
2013-08-17 01:11:09 +08:00
if (id_equal(list[i].client_id, client_id)) {
2013-07-20 11:00:10 +08:00
/* Refresh the client timestamp. */
list[i].timestamp = temp_time;
2013-07-17 01:20:45 +08:00
list[i].ip_port.ip.i = ip_port.ip.i;
list[i].ip_port.port = ip_port.port;
2013-06-25 00:23:46 +08:00
return 1;
}
}
2013-08-17 01:11:09 +08:00
2013-06-25 00:23:46 +08:00
return 0;
}
2013-07-20 11:00:10 +08:00
/* check if client with client_id is already in node format list of length length.
* return True(1) or False(0)
*/
2013-08-17 01:11:09 +08:00
static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id)
{
2013-07-17 01:20:45 +08:00
uint32_t i;
2013-08-17 01:11:09 +08:00
for (i = 0; i < length; ++i) {
if (id_equal(list[i].client_id, client_id))
return 1;
}
return 0;
}
/* Returns the friend number from the client_id, or -1 if a failure occurs
*/
static int friend_number(DHT *dht, uint8_t *client_id)
{
uint32_t i;
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
if (id_equal(dht->friends_list[i].client_id, client_id))
return i;
}
return -1;
}
2013-07-20 11:00:10 +08:00
/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request:
* put them in the nodes_list and return how many were found.
*
* TODO: For the love of based Allah make this function cleaner and much more efficient.
*/
static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list)
{
uint32_t i, j, k;
uint64_t temp_time = unix_time();
int num_nodes = 0, closest, tout, inlist;
for (i = 0; i < LCLIENT_LIST; ++i) {
2013-08-21 00:08:55 +08:00
tout = is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT);
inlist = client_in_nodelist(nodes_list, MAX_SENT_NODES, dht->close_clientlist[i].client_id);
/* if node isn't good or is already in list. */
2013-08-17 01:11:09 +08:00
if (tout || inlist)
continue;
2013-08-17 01:11:09 +08:00
if (num_nodes < MAX_SENT_NODES) {
2013-08-17 01:11:09 +08:00
memcpy( nodes_list[num_nodes].client_id,
2013-08-21 00:08:55 +08:00
dht->close_clientlist[i].client_id,
CLIENT_ID_SIZE );
2013-08-21 00:08:55 +08:00
nodes_list[num_nodes].ip_port = dht->close_clientlist[i].ip_port;
num_nodes++;
} else {
2013-08-17 01:11:09 +08:00
for (j = 0; j < MAX_SENT_NODES; ++j) {
closest = id_closest( client_id,
nodes_list[j].client_id,
2013-08-21 00:08:55 +08:00
dht->close_clientlist[i].client_id );
2013-08-17 01:11:09 +08:00
if (closest == 2) {
memcpy( nodes_list[j].client_id,
2013-08-21 00:08:55 +08:00
dht->close_clientlist[i].client_id,
CLIENT_ID_SIZE);
2013-08-21 00:08:55 +08:00
nodes_list[j].ip_port = dht->close_clientlist[i].ip_port;
break;
}
}
}
}
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
2013-08-17 01:11:09 +08:00
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
2013-08-21 00:08:55 +08:00
tout = is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT);
inlist = client_in_nodelist( nodes_list,
2013-08-17 01:11:09 +08:00
MAX_SENT_NODES,
2013-08-21 00:08:55 +08:00
dht->friends_list[i].client_list[j].client_id);
/* if node isn't good or is already in list. */
2013-08-17 01:11:09 +08:00
if (tout || inlist)
continue;
2013-08-17 01:11:09 +08:00
if (num_nodes < MAX_SENT_NODES) {
2013-08-17 01:11:09 +08:00
memcpy( nodes_list[num_nodes].client_id,
2013-08-21 00:08:55 +08:00
dht->friends_list[i].client_list[j].client_id,
CLIENT_ID_SIZE);
2013-08-21 00:08:55 +08:00
nodes_list[num_nodes].ip_port = dht->friends_list[i].client_list[j].ip_port;
num_nodes++;
} else {
2013-08-17 01:11:09 +08:00
for (k = 0; k < MAX_SENT_NODES; ++k) {
2013-08-17 01:11:09 +08:00
closest = id_closest( client_id,
nodes_list[k].client_id,
2013-08-21 00:08:55 +08:00
dht->friends_list[i].client_list[j].client_id );
2013-08-17 01:11:09 +08:00
if (closest == 2) {
memcpy( nodes_list[k].client_id,
2013-08-21 00:08:55 +08:00
dht->friends_list[i].client_list[j].client_id,
CLIENT_ID_SIZE );
2013-08-21 00:08:55 +08:00
nodes_list[k].ip_port = dht->friends_list[i].client_list[j].ip_port;
break;
}
}
}
}
}
2013-08-17 01:11:09 +08:00
return num_nodes;
}
2013-07-20 11:00:10 +08:00
/* replace first bad (or empty) node with this one
2013-08-02 17:39:04 +08:00
* return 0 if successful
* return 1 if not (list contains no bad nodes)
*/
2013-08-17 01:11:09 +08:00
static int replace_bad( Client_data *list,
uint32_t length,
uint8_t *client_id,
IP_Port ip_port )
2013-06-25 00:23:46 +08:00
{
uint32_t i;
uint64_t temp_time = unix_time();
2013-08-17 01:11:09 +08:00
for (i = 0; i < length; ++i) {
2013-08-02 17:39:04 +08:00
/* if node is bad */
2013-08-17 01:11:09 +08:00
if (is_timeout(temp_time, list[i].timestamp, BAD_NODE_TIMEOUT)) {
2013-06-25 00:23:46 +08:00
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
list[i].ip_port = ip_port;
list[i].timestamp = temp_time;
2013-07-22 01:14:36 +08:00
list[i].ret_ip_port.ip.i = 0;
list[i].ret_ip_port.port = 0;
2013-07-23 04:26:10 +08:00
list[i].ret_timestamp = 0;
2013-06-25 00:23:46 +08:00
return 0;
}
2013-08-02 17:39:04 +08:00
}
2013-07-27 07:18:45 +08:00
2013-06-25 00:23:46 +08:00
return 1;
}
2013-08-17 01:11:09 +08:00
/*Sort the list. It will be sorted from furthest to closest.
TODO: this is innefficient and needs to be optimized.*/
static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id)
{
2013-08-17 01:11:09 +08:00
if (length == 0)
return;
2013-08-17 01:11:09 +08:00
uint32_t i, count;
2013-08-17 01:11:09 +08:00
while (1) {
count = 0;
2013-08-17 01:11:09 +08:00
for (i = 0; i < (length - 1); ++i) {
if (id_closest(comp_client_id, list[i].client_id, list[i + 1].client_id) == 1) {
Client_data temp = list[i + 1];
list[i + 1] = list[i];
list[i] = temp;
++count;
}
}
2013-08-17 01:11:09 +08:00
if (count == 0)
return;
}
}
2013-06-25 00:23:46 +08:00
2013-07-20 11:00:10 +08:00
/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
2013-08-17 01:11:09 +08:00
static int replace_good( Client_data *list,
uint32_t length,
uint8_t *client_id,
IP_Port ip_port,
uint8_t *comp_client_id )
2013-06-25 00:23:46 +08:00
{
uint32_t i;
uint64_t temp_time = unix_time();
sort_list(list, length, comp_client_id);
2013-08-17 01:11:09 +08:00
for (i = 0; i < length; ++i)
if (id_closest(comp_client_id, list[i].client_id, client_id) == 2) {
2013-06-25 00:23:46 +08:00
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
list[i].ip_port = ip_port;
list[i].timestamp = temp_time;
2013-07-22 01:14:36 +08:00
list[i].ret_ip_port.ip.i = 0;
list[i].ret_ip_port.port = 0;
2013-07-23 04:26:10 +08:00
list[i].ret_timestamp = 0;
2013-06-25 00:23:46 +08:00
return 0;
}
2013-07-27 07:18:45 +08:00
2013-06-25 00:23:46 +08:00
return 1;
}
2013-08-17 01:11:09 +08:00
/* Attempt to add client with ip_port and client_id to the friends client list
* and close_clientlist
2013-08-02 17:39:04 +08:00
*/
void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
2013-06-24 20:59:42 +08:00
{
2013-06-25 02:31:50 +08:00
uint32_t i;
2013-08-02 17:39:04 +08:00
/* NOTE: current behavior if there are two clients with the same id is
2013-08-17 01:11:09 +08:00
* to replace the first ip by the second.
2013-08-02 17:39:04 +08:00
*/
2013-08-21 00:08:55 +08:00
if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
2013-07-20 11:00:10 +08:00
/* if we can't replace bad nodes we try replacing good ones */
2013-08-21 00:08:55 +08:00
replace_good( dht->close_clientlist,
2013-08-17 01:11:09 +08:00
LCLIENT_LIST,
client_id,
ip_port,
2013-08-21 00:08:55 +08:00
dht->c->self_public_key );
2013-08-02 17:39:04 +08:00
}
}
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
if (!client_in_list( dht->friends_list[i].client_list,
2013-08-17 01:11:09 +08:00
MAX_FRIEND_CLIENTS,
client_id,
2013-08-02 17:39:04 +08:00
ip_port )) {
2013-08-21 00:08:55 +08:00
if (replace_bad( dht->friends_list[i].client_list,
2013-08-02 17:39:04 +08:00
MAX_FRIEND_CLIENTS,
2013-08-17 01:11:09 +08:00
client_id,
2013-08-02 17:39:04 +08:00
ip_port )) {
2013-07-20 11:00:10 +08:00
/* if we can't replace bad nodes we try replacing good ones. */
2013-08-21 00:08:55 +08:00
replace_good( dht->friends_list[i].client_list,
2013-08-17 01:11:09 +08:00
MAX_FRIEND_CLIENTS,
client_id,
ip_port,
2013-08-21 00:08:55 +08:00
dht->friends_list[i].client_id );
2013-08-02 17:39:04 +08:00
}
}
}
2013-06-24 20:59:42 +08:00
}
/* If client_id is a friend or us, update ret_ip_port
2013-08-02 17:43:00 +08:00
* nodeclient_id is the id of the node that sent us this info
*/
static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id)
2013-07-22 01:14:36 +08:00
{
uint32_t i, j;
uint64_t temp_time = unix_time();
2013-08-02 17:43:00 +08:00
2013-08-21 00:08:55 +08:00
if (id_equal(client_id, dht->c->self_public_key)) {
2013-08-02 17:43:00 +08:00
for (i = 0; i < LCLIENT_LIST; ++i) {
2013-08-21 00:08:55 +08:00
if (id_equal(nodeclient_id, dht->close_clientlist[i].client_id)) {
dht->close_clientlist[i].ret_ip_port = ip_port;
dht->close_clientlist[i].ret_timestamp = temp_time;
2013-07-22 01:14:36 +08:00
return;
}
2013-08-02 17:43:00 +08:00
}
2013-08-02 17:43:00 +08:00
} else {
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
if (id_equal(client_id, dht->friends_list[i].client_id)) {
2013-08-02 17:43:00 +08:00
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
2013-08-21 00:08:55 +08:00
if (id_equal(nodeclient_id, dht->friends_list[i].client_list[j].client_id)) {
dht->friends_list[i].client_list[j].ret_ip_port = ip_port;
dht->friends_list[i].client_list[j].ret_timestamp = temp_time;
return;
}
2013-08-02 17:43:00 +08:00
}
}
}
2013-08-02 17:43:00 +08:00
}
2013-07-22 01:14:36 +08:00
}
2013-07-20 11:00:10 +08:00
/* Same as last function but for get_node requests. */
static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id)
2013-06-25 02:26:29 +08:00
{
uint32_t i;
uint8_t pinging;
uint64_t temp_time = unix_time();
2013-08-17 01:11:09 +08:00
for (i = 0; i < LSEND_NODES_ARRAY; ++i ) {
2013-08-21 00:08:55 +08:00
if (!is_timeout(temp_time, dht->send_nodes[i].timestamp, PING_TIMEOUT)) {
2013-06-26 19:40:07 +08:00
pinging = 0;
2013-08-21 00:08:55 +08:00
if (ip_port.ip.i != 0 && ipport_equal(dht->send_nodes[i].ip_port, ip_port))
2013-08-17 01:11:09 +08:00
++pinging;
2013-08-21 00:08:55 +08:00
if (ping_id != 0 && dht->send_nodes[i].ping_id == ping_id)
2013-08-17 01:11:09 +08:00
++pinging;
2013-08-17 01:11:09 +08:00
if (pinging == (ping_id != 0) + (ip_port.ip.i != 0))
2013-07-27 07:18:45 +08:00
return 1;
2013-06-25 02:26:29 +08:00
}
2013-08-02 17:43:00 +08:00
}
2013-06-25 02:26:29 +08:00
return 0;
}
2013-07-20 11:00:10 +08:00
/* Same but for get node requests */
static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port)
2013-06-25 02:26:29 +08:00
{
uint32_t i, j;
uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
uint64_t temp_time = unix_time();
2013-07-27 07:18:45 +08:00
2013-08-17 01:11:09 +08:00
for (i = 0; i < PING_TIMEOUT; ++i ) {
for (j = 0; j < LSEND_NODES_ARRAY; ++j ) {
2013-08-21 00:08:55 +08:00
if (is_timeout(temp_time, dht->send_nodes[j].timestamp, PING_TIMEOUT - i)) {
dht->send_nodes[j].timestamp = temp_time;
dht->send_nodes[j].ip_port = ip_port;
dht->send_nodes[j].ping_id = ping_id;
return ping_id;
2013-06-25 02:26:29 +08:00
}
2013-08-02 17:43:00 +08:00
}
}
2013-07-27 07:18:45 +08:00
return 0;
2013-06-25 02:26:29 +08:00
}
2013-07-20 11:00:10 +08:00
/* send a getnodes request */
static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id)
{
/* check if packet is gonna be sent to ourself */
2013-08-21 00:08:55 +08:00
if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0))
2013-06-25 02:26:29 +08:00
return 1;
2013-08-21 00:08:55 +08:00
uint64_t ping_id = add_gettingnodes(dht, ip_port);
2013-08-17 01:11:09 +08:00
if (ping_id == 0)
return 1;
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
uint8_t nonce[crypto_box_NONCEBYTES];
random_nonce(nonce);
memcpy(plain, &ping_id, sizeof(ping_id));
memcpy(plain + sizeof(ping_id), client_id, CLIENT_ID_SIZE);
2013-08-17 01:11:09 +08:00
int len = encrypt_data( public_key,
2013-08-21 00:08:55 +08:00
dht->c->self_secret_key,
2013-08-17 01:11:09 +08:00
nonce,
plain,
sizeof(ping_id) + CLIENT_ID_SIZE,
2013-08-02 17:47:32 +08:00
encrypt );
2013-08-17 01:11:09 +08:00
if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING)
return -1;
2013-08-02 17:47:32 +08:00
data[0] = 2;
2013-08-21 00:08:55 +08:00
memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE);
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
2013-08-02 17:47:32 +08:00
2013-08-21 00:08:55 +08:00
return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, sizeof(data));
2013-06-24 13:05:28 +08:00
}
2013-07-20 11:00:10 +08:00
/* send a send nodes response */
static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id)
2013-06-24 13:05:28 +08:00
{
2013-08-02 17:47:32 +08:00
/* check if packet is gonna be sent to ourself */
2013-08-21 00:08:55 +08:00
if (id_equal(public_key, dht->c->self_public_key))
return 1;
2013-07-27 07:18:45 +08:00
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
+ sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
2013-07-27 07:18:45 +08:00
Node_format nodes_list[MAX_SENT_NODES];
2013-08-21 00:08:55 +08:00
int num_nodes = get_close_nodes(dht, client_id, nodes_list);
2013-07-27 07:18:45 +08:00
2013-08-17 01:11:09 +08:00
if (num_nodes == 0)
return 0;
2013-07-27 07:18:45 +08:00
uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES];
uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
uint8_t nonce[crypto_box_NONCEBYTES];
random_nonce(nonce);
memcpy(plain, &ping_id, sizeof(ping_id));
memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * sizeof(Node_format));
2013-08-17 01:11:09 +08:00
int len = encrypt_data( public_key,
2013-08-21 00:08:55 +08:00
dht->c->self_secret_key,
2013-08-17 01:11:09 +08:00
nonce,
2013-08-02 17:47:32 +08:00
plain,
2013-08-17 01:11:09 +08:00
sizeof(ping_id) + num_nodes * sizeof(Node_format),
2013-08-02 17:47:32 +08:00
encrypt );
2013-08-17 01:11:09 +08:00
if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING)
return -1;
data[0] = 3;
2013-08-21 00:08:55 +08:00
memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE);
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
2013-07-27 07:18:45 +08:00
2013-08-21 00:08:55 +08:00
return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
2013-06-24 13:05:28 +08:00
}
static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
DHT *dht = object;
uint64_t ping_id;
2013-08-02 17:47:32 +08:00
2013-08-17 01:11:09 +08:00
if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES
2013-08-02 17:47:32 +08:00
+ sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING ))
return 1;
2013-08-02 17:47:32 +08:00
/* check if packet is from ourself. */
2013-08-21 00:08:55 +08:00
if (id_equal(packet + 1, dht->c->self_public_key))
return 1;
2013-07-27 07:18:45 +08:00
uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
2013-07-27 07:18:45 +08:00
2013-08-17 01:11:09 +08:00
int len = decrypt_data( packet + 1,
2013-08-21 00:08:55 +08:00
dht->c->self_secret_key,
2013-08-02 17:47:32 +08:00
packet + 1 + CLIENT_ID_SIZE,
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
2013-08-17 01:11:09 +08:00
sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING,
2013-08-02 17:47:32 +08:00
plain );
2013-07-27 07:18:45 +08:00
2013-08-02 17:47:32 +08:00
if (len != sizeof(ping_id) + CLIENT_ID_SIZE)
return 1;
2013-07-27 07:18:45 +08:00
memcpy(&ping_id, plain, sizeof(ping_id));
2013-08-21 00:08:55 +08:00
sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id);
2013-07-27 07:18:45 +08:00
2013-08-21 00:08:55 +08:00
//send_ping_request(dht, source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */
2013-07-27 07:18:45 +08:00
return 0;
}
static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
DHT *dht = object;
2013-07-27 08:54:02 +08:00
uint64_t ping_id;
2013-08-02 17:47:32 +08:00
uint32_t cid_size = 1 + CLIENT_ID_SIZE;
cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING;
if (length > (cid_size + sizeof(Node_format) * MAX_SENT_NODES) ||
2013-08-17 01:11:09 +08:00
((length - cid_size) % sizeof(Node_format)) != 0 ||
(length < cid_size + sizeof(Node_format)))
return 1;
2013-07-27 07:18:45 +08:00
2013-08-02 17:47:32 +08:00
uint32_t num_nodes = (length - cid_size) / sizeof(Node_format);
uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES];
2013-07-27 07:18:45 +08:00
2013-08-17 01:11:09 +08:00
int len = decrypt_data(
packet + 1,
2013-08-21 00:08:55 +08:00
dht->c->self_secret_key,
2013-08-17 01:11:09 +08:00
packet + 1 + CLIENT_ID_SIZE,
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain );
2013-07-27 07:18:45 +08:00
2013-08-17 01:11:09 +08:00
if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
return 1;
2013-07-27 07:18:45 +08:00
memcpy(&ping_id, plain, sizeof(ping_id));
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
if (!is_gettingnodes(dht, source, ping_id))
return 1;
2013-07-27 07:18:45 +08:00
Node_format nodes_list[MAX_SENT_NODES];
memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format));
2013-07-27 07:18:45 +08:00
2013-08-21 00:08:55 +08:00
addto_lists(dht, source, packet + 1);
2013-07-27 07:18:45 +08:00
uint32_t i;
2013-08-17 01:11:09 +08:00
for (i = 0; i < num_nodes; ++i) {
2013-08-21 02:47:32 +08:00
send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, (clientid_t *) &nodes_list[i].client_id);
2013-08-21 00:08:55 +08:00
returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
}
2013-07-27 07:18:45 +08:00
return 0;
}
/*----------------------------------------------------------------------------------*/
/*------------------------END of packet handling functions--------------------------*/
2013-06-24 13:05:28 +08:00
int DHT_addfriend(DHT *dht, uint8_t *client_id)
2013-06-24 13:05:28 +08:00
{
2013-08-21 00:08:55 +08:00
if (friend_number(dht, client_id) != -1) /*Is friend already in DHT?*/
return 1;
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
DHT_Friend *temp;
temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1));
2013-08-17 01:11:09 +08:00
2013-08-02 17:49:54 +08:00
if (temp == NULL)
return 1;
2013-07-27 07:18:45 +08:00
2013-08-21 00:08:55 +08:00
dht->friends_list = temp;
memset(&dht->friends_list[dht->num_friends], 0, sizeof(DHT_Friend));
memcpy(dht->friends_list[dht->num_friends].client_id, client_id, CLIENT_ID_SIZE);
2013-08-02 17:49:54 +08:00
2013-08-21 00:08:55 +08:00
dht->friends_list[dht->num_friends].NATping_id = ((uint64_t)random_int() << 32) + random_int();
++dht->num_friends;
return 0;
2013-06-24 13:05:28 +08:00
}
int DHT_delfriend(DHT *dht, uint8_t *client_id)
2013-06-24 13:05:28 +08:00
{
uint32_t i;
2013-08-21 00:08:55 +08:00
DHT_Friend *temp;
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
/* Equal */
2013-08-21 00:08:55 +08:00
if (id_equal(dht->friends_list[i].client_id, client_id)) {
--dht->num_friends;
2013-08-21 00:08:55 +08:00
if (dht->num_friends != i) {
memcpy( dht->friends_list[i].client_id,
dht->friends_list[dht->num_friends].client_id,
2013-08-02 17:49:54 +08:00
CLIENT_ID_SIZE );
}
2013-08-21 00:08:55 +08:00
if (dht->num_friends == 0) {
free(dht->friends_list);
dht->friends_list = NULL;
return 0;
}
2013-08-21 00:08:55 +08:00
temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends));
2013-08-17 01:11:09 +08:00
if (temp == NULL)
return 1;
2013-08-21 00:08:55 +08:00
dht->friends_list = temp;
return 0;
}
2013-08-02 17:49:54 +08:00
}
2013-07-27 07:18:45 +08:00
return 1;
2013-06-24 13:05:28 +08:00
}
2013-07-20 11:00:10 +08:00
/* TODO: Optimize this. */
IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id)
2013-06-24 13:05:28 +08:00
{
uint32_t i, j;
uint64_t temp_time = unix_time();
IP_Port empty = {{{0}}, 0};
2013-08-02 17:49:54 +08:00
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
/* Equal */
2013-08-21 00:08:55 +08:00
if (id_equal(dht->friends_list[i].client_id, client_id)) {
2013-08-02 17:49:54 +08:00
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
2013-08-21 00:08:55 +08:00
if (id_equal(dht->friends_list[i].client_list[j].client_id, client_id)
&& !is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT))
return dht->friends_list[i].client_list[j].ip_port;
2013-08-02 17:49:54 +08:00
}
2013-08-17 01:11:09 +08:00
return empty;
}
2013-08-02 17:49:54 +08:00
}
2013-08-17 01:11:09 +08:00
empty.ip.i = 1;
return empty;
}
2013-06-24 13:05:28 +08:00
/* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request
* every GET_NODE_INTERVAL seconds to a random good node for each "friend" in our "friends" list.
2013-08-02 17:49:54 +08:00
*/
static void do_DHT_friends(DHT *dht)
2013-06-25 00:23:46 +08:00
{
uint32_t i, j;
uint64_t temp_time = unix_time();
uint32_t rand_node;
uint32_t index[MAX_FRIEND_CLIENTS];
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
2013-07-21 07:59:01 +08:00
uint32_t num_nodes = 0;
2013-08-17 01:11:09 +08:00
2013-08-02 17:49:54 +08:00
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
/* if node is not dead. */
2013-08-21 00:08:55 +08:00
if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) {
if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
2013-08-21 02:47:32 +08:00
send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port,
(clientid_t *) &dht->friends_list[i].client_list[j].client_id );
2013-08-21 00:08:55 +08:00
dht->friends_list[i].client_list[j].last_pinged = temp_time;
}
2013-08-17 01:11:09 +08:00
2013-08-02 17:49:54 +08:00
/* if node is good. */
2013-08-21 00:08:55 +08:00
if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT)) {
index[num_nodes] = j;
++num_nodes;
}
}
2013-08-02 17:49:54 +08:00
}
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
if (dht->friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
rand_node = rand() % num_nodes;
2013-08-21 00:08:55 +08:00
getnodes(dht, dht->friends_list[i].client_list[index[rand_node]].ip_port,
dht->friends_list[i].client_list[index[rand_node]].client_id,
dht->friends_list[i].client_id );
2013-08-21 00:08:55 +08:00
dht->friends_list[i].lastgetnode = temp_time;
}
}
2013-06-25 00:23:46 +08:00
}
2013-06-24 13:05:28 +08:00
/* Ping each client in the close nodes list every PING_INTERVAL seconds.
* Send a get nodes request every GET_NODE_INTERVAL seconds to a random good node in the list.
*/
static void do_Close(DHT *dht)
2013-06-25 00:23:46 +08:00
{
uint32_t i;
uint64_t temp_time = unix_time();
uint32_t num_nodes = 0;
uint32_t rand_node;
uint32_t index[LCLIENT_LIST];
for (i = 0; i < LCLIENT_LIST; ++i) {
/* if node is not dead. */
2013-08-21 00:08:55 +08:00
if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) {
if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) {
2013-08-21 02:47:32 +08:00
send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port,
(clientid_t *) &dht->close_clientlist[i].client_id );
2013-08-21 00:08:55 +08:00
dht->close_clientlist[i].last_pinged = temp_time;
}
2013-08-17 01:11:09 +08:00
/* if node is good. */
2013-08-21 00:08:55 +08:00
if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT)) {
index[num_nodes] = i;
++num_nodes;
}
2013-07-27 07:18:45 +08:00
}
}
2013-08-21 00:08:55 +08:00
if (dht->close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
rand_node = rand() % num_nodes;
2013-08-21 00:08:55 +08:00
getnodes(dht, dht->close_clientlist[index[rand_node]].ip_port,
dht->close_clientlist[index[rand_node]].client_id,
dht->c->self_public_key );
2013-08-21 00:08:55 +08:00
dht->close_lastgetnodes = temp_time;
}
2013-06-25 00:23:46 +08:00
}
void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key)
2013-06-24 13:05:28 +08:00
{
2013-08-21 00:08:55 +08:00
getnodes(dht, ip_port, public_key, dht->c->self_public_key);
2013-08-21 02:47:32 +08:00
send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key);
}
2013-07-22 01:14:36 +08:00
/* send the given packet to node with client_id
2013-08-17 01:11:09 +08:00
* returns -1 if failure
*/
int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
2013-07-22 01:14:36 +08:00
{
uint32_t i;
for (i = 0; i < LCLIENT_LIST; ++i) {
2013-08-21 00:08:55 +08:00
if (id_equal(client_id, dht->close_clientlist[i].client_id))
return sendpacket(dht->c->lossless_udp->net->sock, dht->close_clientlist[i].ip_port, packet, length);
}
2013-07-22 01:14:36 +08:00
return -1;
}
/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist
* ip_portlist must be at least MAX_FRIEND_CLIENTS big
* returns the number of ips returned
* return 0 if we are connected to friend or if no ips were found.
* returns -1 if no such friend
*/
static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
{
int num_ips = 0;
uint32_t i;
uint64_t temp_time = unix_time();
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
if (friend_num >= dht->num_friends)
return -1;
2013-08-21 00:08:55 +08:00
DHT_Friend *friend = &dht->friends_list[friend_num];
2013-08-17 01:11:09 +08:00
Client_data *client;
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
client = &friend->client_list[i];
/*If ip is not zero and node is good */
if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
if (id_equal(client->client_id, friend->client_id))
return 0;
ip_portlist[num_ips] = client->ret_ip_port;
++num_ips;
}
}
2013-08-17 01:11:09 +08:00
return num_ips;
}
2013-07-22 01:14:36 +08:00
/* Send the following packet to everyone who tells us they are connected to friend_id
* returns the number of nodes it sent the packet to
*
* Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend.
*/
int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
2013-07-22 01:14:36 +08:00
{
2013-08-21 00:08:55 +08:00
int num = friend_number(dht, friend_id);
2013-08-17 01:11:09 +08:00
if (num == -1)
return 0;
uint32_t i, sent = 0;
IP_Port ip_list[MAX_FRIEND_CLIENTS];
2013-08-21 00:08:55 +08:00
int ip_num = friend_iplist(dht, ip_list, num);
if (ip_num < (MAX_FRIEND_CLIENTS / 2))
return 0;
uint64_t temp_time = unix_time();
2013-08-21 00:08:55 +08:00
DHT_Friend *friend = &dht->friends_list[num];
2013-08-17 01:11:09 +08:00
Client_data *client;
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
client = &friend->client_list[i];
/*If ip is not zero and node is good */
if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
2013-08-21 00:08:55 +08:00
if (sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length) == length)
++sent;
2013-07-22 01:14:36 +08:00
}
}
2013-08-17 01:11:09 +08:00
return sent;
2013-07-22 01:14:36 +08:00
}
/* Send the following packet to one random person who tells us they are connected to friend_id
* returns the number of nodes it sent the packet to
*/
static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
{
2013-08-21 00:08:55 +08:00
int num = friend_number(dht, friend_id);
2013-08-17 01:11:09 +08:00
if (num == -1)
return 0;
2013-08-21 00:08:55 +08:00
DHT_Friend *friend = &dht->friends_list[num];
2013-08-17 01:11:09 +08:00
Client_data *client;
IP_Port ip_list[MAX_FRIEND_CLIENTS];
int n = 0;
2013-08-03 03:28:48 +08:00
uint32_t i;
uint64_t temp_time = unix_time();
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
client = &friend->client_list[i];
/*If ip is not zero and node is good */
2013-08-17 01:11:09 +08:00
if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
ip_list[n] = client->ip_port;
++n;
}
}
2013-08-17 01:11:09 +08:00
if (n < 1)
return 0;
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
if (sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length) == length)
return 1;
2013-08-17 01:11:09 +08:00
return 0;
}
/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
* ip_portlist must be at least MAX_FRIEND_CLIENTS big
* returns the number of ips returned
* return 0 if we are connected to friend or if no ips were found.
* returns -1 if no such friend
*/
int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id)
2013-07-22 01:14:36 +08:00
{
uint32_t i;
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
/* Equal */
2013-08-21 00:08:55 +08:00
if (id_equal(dht->friends_list[i].client_id, friend_id))
return friend_iplist(dht, ip_portlist, i);
}
2013-08-17 01:11:09 +08:00
return -1;
}
/*----------------------------------------------------------------------------------*/
/*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/
static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t type)
{
uint8_t data[sizeof(uint64_t) + 1];
uint8_t packet[MAX_DATA_SIZE];
int num = 0;
data[0] = type;
memcpy(data + 1, &ping_id, sizeof(uint64_t));
2013-08-03 03:28:48 +08:00
/* 254 is NAT ping request packet id */
int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, data,
sizeof(uint64_t) + 1, 254);
if (len == -1)
return -1;
2013-07-27 07:18:45 +08:00
if (type == 0) /*If packet is request use many people to route it*/
2013-08-21 00:08:55 +08:00
num = route_tofriend(dht, public_key, packet, len);
else if (type == 1) /*If packet is response use only one person to route it*/
2013-08-21 00:08:55 +08:00
num = routeone_tofriend(dht, public_key, packet, len);
2013-07-27 07:18:45 +08:00
if (num == 0)
return -1;
2013-08-17 01:11:09 +08:00
return num;
}
/* Handle a received ping request for */
static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length)
{
DHT *dht = object;
uint64_t ping_id;
memcpy(&ping_id, packet + 1, sizeof(uint64_t));
2013-08-21 00:08:55 +08:00
int friendnumber = friend_number(dht, source_pubkey);
2013-08-17 01:11:09 +08:00
if (friendnumber == -1)
return 1;
2013-08-21 00:08:55 +08:00
DHT_Friend *friend = &dht->friends_list[friendnumber];
if (packet[0] == 0) {
/* 1 is reply */
2013-08-21 00:08:55 +08:00
send_NATping(dht, source_pubkey, ping_id, 1);
friend->recvNATping_timestamp = unix_time();
return 0;
} else if (packet[0] == 1) {
if (friend->NATping_id == ping_id) {
friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
friend->hole_punching = 1;
return 0;
}
}
2013-08-17 01:11:09 +08:00
return 1;
}
/* Get the most common ip in the ip_portlist
* Only return ip if it appears in list min_num or more
* len must not be bigger than MAX_FRIEND_CLIENTS
2013-08-17 01:11:09 +08:00
* return ip of 0 if failure
*/
2013-08-17 01:11:09 +08:00
static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num)
{
IP zero = {{0}};
2013-08-17 01:11:09 +08:00
if (len > MAX_FRIEND_CLIENTS)
return zero;
2013-07-22 01:14:36 +08:00
uint32_t i, j;
uint16_t numbers[MAX_FRIEND_CLIENTS] = {0};
2013-08-17 01:11:09 +08:00
for (i = 0; i < len; ++i) {
for (j = 0; j < len; ++j) {
if (ip_portlist[i].ip.i == ip_portlist[j].ip.i)
++numbers[i];
}
2013-08-17 01:11:09 +08:00
if (numbers[i] >= min_num)
return ip_portlist[i].ip;
}
2013-08-17 01:11:09 +08:00
return zero;
}
/* Return all the ports for one ip in a list
* portlist must be at least len long
* where len is the length of ip_portlist
* returns the number of ports and puts the list of ports in portlist
*/
2013-08-17 01:11:09 +08:00
static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip)
{
uint32_t i;
uint16_t num = 0;
2013-08-17 01:11:09 +08:00
for (i = 0; i < len; ++i) {
if (ip_portlist[i].ip.i == ip.i) {
portlist[num] = ntohs(ip_portlist[i].port);
++num;
}
}
2013-08-17 01:11:09 +08:00
return num;
}
static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num)
{
2013-08-17 01:11:09 +08:00
if (numports > MAX_FRIEND_CLIENTS || numports == 0)
return;
uint32_t i;
2013-08-21 00:08:55 +08:00
uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS;
2013-08-21 00:08:55 +08:00
for (i = dht->friends_list[friend_num].punching_index; i != top; i++) {
/*TODO: improve port guessing algorithm*/
2013-08-17 01:11:09 +08:00
uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
IP_Port pinging = {ip, htons(port)};
2013-08-21 02:47:32 +08:00
send_ping_request(dht->ping, dht->c, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id);
}
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
dht->friends_list[friend_num].punching_index = i;
}
static void do_NAT(DHT *dht)
{
uint32_t i;
uint64_t temp_time = unix_time();
2013-08-21 00:08:55 +08:00
for (i = 0; i < dht->num_friends; ++i) {
IP_Port ip_list[MAX_FRIEND_CLIENTS];
2013-08-21 00:08:55 +08:00
int num = friend_iplist(dht, ip_list, i);
/*If already connected or friend is not online don't try to hole punch*/
2013-08-17 01:11:09 +08:00
if (num < MAX_FRIEND_CLIENTS / 2)
continue;
2013-08-21 00:08:55 +08:00
if (dht->friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) {
send_NATping(dht, dht->friends_list[i].client_id, dht->friends_list[i].NATping_id, 0); /*0 is request*/
dht->friends_list[i].NATping_timestamp = temp_time;
}
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
if (dht->friends_list[i].hole_punching == 1 &&
dht->friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time &&
dht->friends_list[i].recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) {
2013-08-17 01:11:09 +08:00
IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2);
if (ip.i == 0)
continue;
2013-07-27 07:18:45 +08:00
uint16_t port_list[MAX_FRIEND_CLIENTS];
uint16_t numports = NAT_getports(port_list, ip_list, num, ip);
2013-08-21 00:08:55 +08:00
punch_holes(dht, ip, port_list, numports, i);
2013-08-21 00:08:55 +08:00
dht->friends_list[i].punching_timestamp = temp_time;
dht->friends_list[i].hole_punching = 0;
}
}
}
/*----------------------------------------------------------------------------------*/
/*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/
/* Add nodes to the toping list
all nodes in this list are pinged every TIME_TOPING seconds
and are then removed from the list.
if the list is full the nodes farthest from our client_id are replaced
the purpose of this list is to enable quick integration of new nodes into the
2013-08-17 01:11:09 +08:00
network while preventing amplification attacks.
return 0 if node was added
return -1 if node was not added */
int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port)
{
if (ip_port.ip.i == 0)
return -1;
2013-08-17 01:11:09 +08:00
uint32_t i;
2013-08-17 01:11:09 +08:00
for (i = 0; i < MAX_TOPING; ++i) {
2013-08-21 00:08:55 +08:00
if (dht->toping[i].ip_port.ip.i == 0) {
memcpy(dht->toping[i].client_id, client_id, CLIENT_ID_SIZE);
dht->toping[i].ip_port.ip.i = ip_port.ip.i;
dht->toping[i].ip_port.port = ip_port.port;
return 0;
}
}
2013-08-17 01:11:09 +08:00
for (i = 0; i < MAX_TOPING; ++i) {
2013-08-21 00:08:55 +08:00
if (id_closest(dht->c->self_public_key, dht->toping[i].client_id, client_id) == 2) {
memcpy(dht->toping[i].client_id, client_id, CLIENT_ID_SIZE);
dht->toping[i].ip_port.ip.i = ip_port.ip.i;
dht->toping[i].ip_port.port = ip_port.port;
return 0;
}
}
2013-08-17 01:11:09 +08:00
return -1;
}
/*Ping all the valid nodes in the toping list every TIME_TOPING seconds
this function must be run at least once every TIME_TOPING seconds*/
static void do_toping(DHT *dht)
{
uint64_t temp_time = unix_time();
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
if (!is_timeout(temp_time, dht->last_toping, TIME_TOPING))
return;
2013-08-17 01:11:09 +08:00
2013-08-21 00:08:55 +08:00
dht->last_toping = temp_time;
uint32_t i;
2013-08-17 01:11:09 +08:00
for (i = 0; i < MAX_TOPING; ++i) {
2013-08-21 00:08:55 +08:00
if (dht->toping[i].ip_port.ip.i == 0)
return;
2013-08-17 01:11:09 +08:00
2013-08-21 02:47:32 +08:00
send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id);
2013-08-21 00:08:55 +08:00
dht->toping[i].ip_port.ip.i = 0;
}
}
DHT *new_DHT(Net_Crypto *c)
{
if (c == NULL)
return NULL;
DHT *temp = calloc(1, sizeof(DHT));
2013-08-21 00:08:55 +08:00
if (temp == NULL)
return NULL;
2013-08-21 02:47:32 +08:00
temp->ping = new_ping();
if (temp->ping == NULL) {
kill_DHT(temp);
return NULL;
2013-08-21 02:47:32 +08:00
}
2013-08-21 00:08:55 +08:00
temp->c = c;
networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp);
networking_registerhandler(c->lossless_udp->net, 1, &handle_ping_response, temp);
networking_registerhandler(c->lossless_udp->net, 2, &handle_getnodes, temp);
networking_registerhandler(c->lossless_udp->net, 3, &handle_sendnodes, temp);
cryptopacket_registerhandler(c, 254, &handle_NATping, temp);
return temp;
2013-07-22 01:14:36 +08:00
}
void do_DHT(DHT *dht)
2013-08-21 00:08:55 +08:00
{
do_Close(dht);
do_DHT_friends(dht);
do_NAT(dht);
do_toping(dht);
}
void kill_DHT(DHT *dht)
{
2013-08-21 02:47:32 +08:00
kill_ping(dht->ping);
2013-08-21 00:08:55 +08:00
free(dht->friends_list);
free(dht);
}
2013-07-20 11:00:10 +08:00
/* get the size of the DHT (for saving) */
uint32_t DHT_size(DHT *dht)
2013-07-17 01:20:45 +08:00
{
2013-08-21 00:08:55 +08:00
return sizeof(dht->close_clientlist) + sizeof(DHT_Friend) * dht->num_friends;
2013-07-17 01:20:45 +08:00
}
2013-07-20 11:00:10 +08:00
/* save the DHT in data where data is an array of size DHT_size() */
void DHT_save(DHT *dht, uint8_t *data)
2013-07-17 01:20:45 +08:00
{
2013-08-21 00:08:55 +08:00
memcpy(data, dht->close_clientlist, sizeof(dht->close_clientlist));
memcpy(data + sizeof(dht->close_clientlist), dht->friends_list, sizeof(DHT_Friend) * dht->num_friends);
2013-07-17 01:20:45 +08:00
}
2013-07-20 11:00:10 +08:00
/* load the DHT from data of size size;
* return -1 if failure
2013-08-17 01:11:09 +08:00
* return 0 if success
*/
int DHT_load(DHT *dht, uint8_t *data, uint32_t size)
2013-07-17 01:20:45 +08:00
{
2013-08-21 00:08:55 +08:00
if (size < sizeof(dht->close_clientlist))
2013-07-17 01:20:45 +08:00
return -1;
2013-08-21 00:08:55 +08:00
if ((size - sizeof(dht->close_clientlist)) % sizeof(DHT_Friend) != 0)
2013-07-17 01:20:45 +08:00
return -1;
2013-07-17 01:20:45 +08:00
uint32_t i, j;
uint16_t temp;
/* uint64_t temp_time = unix_time(); */
2013-08-17 01:11:09 +08:00
Client_data *client;
2013-08-21 00:08:55 +08:00
temp = (size - sizeof(dht->close_clientlist)) / sizeof(DHT_Friend);
2013-08-17 01:11:09 +08:00
if (temp != 0) {
2013-08-21 00:08:55 +08:00
DHT_Friend *tempfriends_list = (DHT_Friend *)(data + sizeof(dht->close_clientlist));
2013-07-17 01:20:45 +08:00
2013-08-17 01:11:09 +08:00
for (i = 0; i < temp; ++i) {
2013-08-21 00:08:55 +08:00
DHT_addfriend(dht, tempfriends_list[i].client_id);
2013-08-17 01:11:09 +08:00
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
client = &tempfriends_list[i].client_list[j];
2013-08-17 01:11:09 +08:00
if (client->timestamp != 0)
2013-08-21 00:08:55 +08:00
getnodes(dht, client->ip_port, client->client_id, tempfriends_list[i].client_id);
}
2013-07-17 01:20:45 +08:00
}
}
2013-08-17 01:11:09 +08:00
Client_data *tempclose_clientlist = (Client_data *)data;
for (i = 0; i < LCLIENT_LIST; ++i) {
if (tempclose_clientlist[i].timestamp != 0)
2013-08-21 00:08:55 +08:00
DHT_bootstrap(dht, tempclose_clientlist[i].ip_port,
tempclose_clientlist[i].client_id );
}
2013-08-17 01:11:09 +08:00
2013-07-17 01:20:45 +08:00
return 0;
}
2013-07-20 11:00:10 +08:00
/* returns 0 if we are not connected to the DHT
2013-08-17 01:11:09 +08:00
* returns 1 if we are
*/
int DHT_isconnected(DHT *dht)
{
2013-08-03 03:28:48 +08:00
uint32_t i;
uint64_t temp_time = unix_time();
2013-08-17 01:11:09 +08:00
for (i = 0; i < LCLIENT_LIST; ++i) {
2013-08-21 00:08:55 +08:00
if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT))
return 1;
}
2013-08-17 01:11:09 +08:00
return 0;
2013-07-27 07:18:45 +08:00
}