2013-06-26 21:56:15 +08:00
|
|
|
/* DHT.c
|
|
|
|
*
|
|
|
|
* An implementation of the DHT as seen in docs/DHT.txt
|
|
|
|
*
|
2013-06-27 03:02:42 +08:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
2013-06-26 21:56:15 +08:00
|
|
|
*/
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-25 04:50:43 +08:00
|
|
|
|
2013-06-27 03:02:42 +08:00
|
|
|
|
|
|
|
|
2013-06-26 21:56:15 +08:00
|
|
|
#include "DHT.h"
|
2013-06-25 04:50:43 +08:00
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
|
|
|
IP_Port ip_port;
|
|
|
|
uint32_t timestamp;
|
|
|
|
uint32_t last_pinged;
|
|
|
|
}Client_data;
|
|
|
|
//maximum number of clients stored per friend.
|
|
|
|
#define MAX_FRIEND_CLIENTS 8
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
|
|
|
Client_data client_list[MAX_FRIEND_CLIENTS];
|
|
|
|
|
|
|
|
}Friend;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
|
|
|
IP_Port ip_port;
|
|
|
|
}Node_format;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
IP_Port ip_port;
|
|
|
|
uint32_t ping_id;
|
|
|
|
uint32_t timestamp;
|
|
|
|
|
|
|
|
}Pinged;
|
|
|
|
|
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
uint8_t self_client_id[CLIENT_ID_SIZE];
|
2013-06-25 04:50:43 +08:00
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
|
|
|
|
//TODO: Move these out of here and put them into the .c file.
|
|
|
|
//A list of the clients mathematically closest to ours.
|
|
|
|
#define LCLIENT_LIST 32
|
|
|
|
Client_data close_clientlist[LCLIENT_LIST];
|
|
|
|
|
|
|
|
|
|
|
|
//Hard maximum number of friends
|
|
|
|
#define MAX_FRIENDS 256
|
|
|
|
|
|
|
|
//Let's start with a static array for testing.
|
|
|
|
Friend friends_list[MAX_FRIENDS];
|
|
|
|
uint16_t num_friends;
|
|
|
|
|
|
|
|
//The list of ip ports along with the ping_id of what we sent them and a timestamp
|
|
|
|
#define LPING_ARRAY 128
|
|
|
|
|
|
|
|
Pinged pings[LPING_ARRAY];
|
|
|
|
|
|
|
|
#define LSEND_NODES_ARRAY LPING_ARRAY/2
|
|
|
|
|
|
|
|
Pinged send_nodes[LSEND_NODES_ARRAY];
|
|
|
|
|
|
|
|
|
2013-06-24 22:42:55 +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-07-06 05:00:39 +08:00
|
|
|
int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)//tested
|
2013-06-24 22:42:55 +08:00
|
|
|
{
|
2013-06-25 00:23:46 +08:00
|
|
|
uint32_t i;
|
2013-06-24 22:42:55 +08:00
|
|
|
for(i = 0; i < CLIENT_ID_SIZE; i++)
|
|
|
|
{
|
|
|
|
if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i]))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i]))
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
}
|
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
//check if client with client_id is already in list of length length.
|
2013-06-25 02:26:29 +08:00
|
|
|
//if it is set it's corresponding timestamp to current time.
|
2013-06-26 20:32:42 +08:00
|
|
|
//if the ip_port is already in the list but associated to a different ip, change it.
|
2013-06-25 00:23:46 +08:00
|
|
|
//return True(1) or False(0)
|
2013-06-26 03:24:31 +08:00
|
|
|
//TODO: maybe optimize this.
|
2013-07-06 05:00:39 +08:00
|
|
|
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, j;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
for(i = 0; i < length; i++)
|
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
//If the id for an ip/port changes, replace it.
|
|
|
|
if(list[i].ip_port.ip.i == ip_port.ip.i &&
|
|
|
|
list[i].ip_port.port == ip_port.port)
|
|
|
|
{
|
|
|
|
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
}
|
2013-06-25 00:23:46 +08:00
|
|
|
for(j = 0; j < CLIENT_ID_SIZE; j++)
|
|
|
|
{
|
|
|
|
|
|
|
|
if(list[i].client_id[j] != client_id[j])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
if(j == CLIENT_ID_SIZE)
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
2013-06-25 02:26:29 +08:00
|
|
|
//Refresh the client timestamp.
|
2013-06-26 03:24:31 +08:00
|
|
|
list[i].timestamp = temp_time;
|
2013-06-25 00:23:46 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
//check if client with client_id is already in node format list of length length.
|
2013-06-25 08:51:07 +08:00
|
|
|
//return True(1) or False(0)
|
2013-07-06 05:00:39 +08:00
|
|
|
int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
|
2013-06-25 08:51:07 +08:00
|
|
|
{
|
|
|
|
uint32_t i, j;
|
|
|
|
for(i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
for(j = 0; j < CLIENT_ID_SIZE; j++)
|
|
|
|
{
|
|
|
|
|
|
|
|
if(list[i].client_id[j] != client_id[j])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
if(j == CLIENT_ID_SIZE)
|
2013-06-25 08:51:07 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 08:51:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
//the number of seconds for a non responsive node to become bad.
|
|
|
|
#define BAD_NODE_TIMEOUT 130
|
2013-06-25 08:51:07 +08:00
|
|
|
//The max number of nodes to send with send nodes.
|
|
|
|
#define MAX_SENT_NODES 8
|
|
|
|
|
|
|
|
|
|
|
|
//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: Make this function much more efficient.
|
2013-07-06 05:00:39 +08:00
|
|
|
int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
|
2013-06-25 08:51:07 +08:00
|
|
|
{
|
|
|
|
uint32_t i, j, k;
|
|
|
|
int num_nodes=0;
|
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
for(i = 0; i < LCLIENT_LIST; i++)
|
|
|
|
{
|
|
|
|
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time &&
|
|
|
|
!client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id))
|
|
|
|
//if node is good and not already in list.
|
|
|
|
{
|
|
|
|
if(num_nodes < MAX_SENT_NODES)
|
|
|
|
{
|
|
|
|
memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
|
|
|
|
nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port;
|
|
|
|
num_nodes++;
|
|
|
|
}
|
|
|
|
else for(j = 0; j < MAX_SENT_NODES; j++)
|
|
|
|
{
|
|
|
|
if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2)
|
|
|
|
{
|
|
|
|
memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
|
|
|
|
nodes_list[j].ip_port = close_clientlist[i].ip_port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
for(i = 0; i < num_friends; i++)
|
|
|
|
{
|
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; j++)
|
|
|
|
{
|
|
|
|
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time &&
|
|
|
|
!client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id))
|
|
|
|
//if node is good and not already in list.
|
|
|
|
{
|
|
|
|
if(num_nodes < MAX_SENT_NODES)
|
|
|
|
{
|
|
|
|
memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE);
|
|
|
|
nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port;
|
|
|
|
num_nodes++;
|
|
|
|
}
|
|
|
|
else for(k = 0; k < MAX_SENT_NODES; k++)
|
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2)
|
2013-06-25 08:51:07 +08:00
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE);
|
|
|
|
nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port;
|
2013-06-25 08:51:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
return num_nodes;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 08:51:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
|
|
|
|
//replace first bad (or empty) node with this one
|
2013-07-09 00:36:11 +08:00
|
|
|
//return 0 if successful
|
2013-06-25 00:23:46 +08:00
|
|
|
//return 1 if not (list contains no bad nodes)
|
2013-07-06 05:00:39 +08:00
|
|
|
int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)//tested
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-06-25 08:51:07 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
2013-06-25 00:23:46 +08:00
|
|
|
for(i = 0; i < length; i++)
|
|
|
|
{
|
2013-06-25 08:51:07 +08:00
|
|
|
if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time)//if node is bad.
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
|
|
|
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
list[i].ip_port = ip_port;
|
2013-06-25 08:51:07 +08:00
|
|
|
list[i].timestamp = temp_time;
|
2013-06-25 00:23:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-26 19:40:07 +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-07-06 05:00:39 +08:00
|
|
|
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;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
for(i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
if(id_closest(comp_client_id, list[i].client_id, client_id) == 2)
|
|
|
|
{
|
|
|
|
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
list[i].ip_port = ip_port;
|
2013-06-26 03:24:31 +08:00
|
|
|
list[i].timestamp = temp_time;
|
2013-06-25 00:23:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
2013-06-24 22:42:55 +08:00
|
|
|
|
2013-06-24 20:59:42 +08:00
|
|
|
//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist
|
2013-07-06 05:00:39 +08:00
|
|
|
void addto_lists(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-06-24 20:59:42 +08:00
|
|
|
|
2013-07-09 00:36:11 +08:00
|
|
|
//NOTE: current behavior if there are two clients with the same id is to only keep one (the first one)
|
2013-06-26 03:24:31 +08:00
|
|
|
if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port))
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port))
|
|
|
|
{
|
|
|
|
//if we can't replace bad nodes we try replacing good ones
|
|
|
|
replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_client_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
for(i = 0; i < num_friends; i++)
|
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port))
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
|
|
|
|
2013-06-25 08:51:07 +08:00
|
|
|
if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port))
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
|
|
|
//if we can't replace bad nodes we try replacing good ones
|
2013-06-25 08:51:07 +08:00
|
|
|
replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, self_client_id);
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 20:59:42 +08:00
|
|
|
}
|
2013-06-24 20:28:19 +08:00
|
|
|
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
//ping timeout in seconds
|
2013-06-26 19:40:07 +08:00
|
|
|
#define PING_TIMEOUT 5
|
2013-06-26 03:24:31 +08:00
|
|
|
//check if we are currently pinging an ip_port and/or a ping_id
|
|
|
|
//Variables with values of zero will not be checked.
|
2013-06-25 02:26:29 +08:00
|
|
|
//if we are already, return 1
|
|
|
|
//else return 0
|
|
|
|
//TODO: Maybe optimize this
|
2013-06-26 03:24:31 +08:00
|
|
|
int is_pinging(IP_Port ip_port, uint32_t ping_id)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint8_t pinging;
|
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
for(i = 0; i < LPING_ARRAY; i++ )
|
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
if((pings[i].timestamp + PING_TIMEOUT) > temp_time)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
2013-06-26 19:40:07 +08:00
|
|
|
pinging = 0;
|
2013-06-26 03:24:31 +08:00
|
|
|
if(ip_port.ip.i != 0)
|
|
|
|
{
|
|
|
|
if(pings[i].ip_port.ip.i == ip_port.ip.i &&
|
|
|
|
pings[i].ip_port.port == ip_port.port)
|
|
|
|
{
|
2013-06-26 19:40:07 +08:00
|
|
|
pinging++;
|
2013-06-26 03:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ping_id != 0)
|
|
|
|
{
|
|
|
|
if(pings[i].ping_id == ping_id)
|
|
|
|
{
|
2013-06-26 19:40:07 +08:00
|
|
|
pinging++;
|
2013-06-26 03:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-26 19:40:07 +08:00
|
|
|
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
|
2013-06-26 03:24:31 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Same as last function but for get_node requests.
|
2013-06-26 03:24:31 +08:00
|
|
|
int is_gettingnodes(IP_Port ip_port, uint32_t ping_id)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint8_t pinging;
|
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
|
|
|
for(i = 0; i < LPING_ARRAY; i++ )
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
2013-06-26 19:40:07 +08:00
|
|
|
pinging = 0;
|
2013-06-26 03:24:31 +08:00
|
|
|
if(ip_port.ip.i != 0)
|
|
|
|
{
|
|
|
|
if(send_nodes[i].ip_port.ip.i == ip_port.ip.i &&
|
|
|
|
send_nodes[i].ip_port.port == ip_port.port)
|
|
|
|
{
|
2013-06-26 19:40:07 +08:00
|
|
|
pinging++;
|
2013-06-26 03:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ping_id != 0)
|
|
|
|
{
|
|
|
|
if(send_nodes[i].ping_id == ping_id)
|
|
|
|
{
|
2013-06-26 19:40:07 +08:00
|
|
|
pinging++;
|
2013-06-26 03:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-26 19:40:07 +08:00
|
|
|
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
|
2013-06-26 03:24:31 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-26 03:24:31 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
//Add a new ping request to the list of ping requests
|
|
|
|
//returns the ping_id to put in the ping request
|
2013-06-25 21:19:01 +08:00
|
|
|
//returns 0 if problem.
|
2013-06-25 02:26:29 +08:00
|
|
|
//TODO: Maybe optimize this
|
|
|
|
int add_pinging(IP_Port ip_port)
|
|
|
|
{
|
|
|
|
uint32_t i, j;
|
|
|
|
int ping_id = rand();
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
for(i = 0; i < PING_TIMEOUT; i++ )
|
|
|
|
{
|
|
|
|
for(j = 0; j < LPING_ARRAY; j++ )
|
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
pings[j].timestamp = temp_time;
|
2013-06-25 02:26:29 +08:00
|
|
|
pings[j].ip_port = ip_port;
|
|
|
|
pings[j].ping_id = ping_id;
|
|
|
|
return ping_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Same but for get node requests
|
|
|
|
int add_gettingnodes(IP_Port ip_port)
|
|
|
|
{
|
|
|
|
uint32_t i, j;
|
|
|
|
int ping_id = rand();
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
for(i = 0; i < PING_TIMEOUT; i++ )
|
|
|
|
{
|
|
|
|
for(j = 0; j < LSEND_NODES_ARRAY; j++ )
|
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
send_nodes[j].timestamp = temp_time;
|
2013-06-25 02:26:29 +08:00
|
|
|
send_nodes[j].ip_port = ip_port;
|
|
|
|
send_nodes[j].ping_id = ping_id;
|
|
|
|
return ping_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-26 03:24:31 +08:00
|
|
|
|
2013-06-24 20:59:42 +08:00
|
|
|
//send a ping request
|
2013-06-26 19:40:07 +08:00
|
|
|
//Ping request only works if none has been sent to that ip/port in the last 5 seconds.
|
2013-06-24 20:59:42 +08:00
|
|
|
int pingreq(IP_Port ip_port)
|
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
if(is_pinging(ip_port, 0))
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ping_id = add_pinging(ip_port);
|
2013-06-25 21:19:01 +08:00
|
|
|
if(ping_id == 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-25 02:26:29 +08:00
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
uint8_t data[5 + CLIENT_ID_SIZE];
|
2013-06-24 20:59:42 +08:00
|
|
|
data[0] = 0;
|
2013-06-25 02:26:29 +08:00
|
|
|
memcpy(data + 1, &ping_id, 4);
|
2013-06-24 22:42:55 +08:00
|
|
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
return sendpacket(ip_port, data, sizeof(data));
|
2013-06-24 20:59:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
|
2013-06-24 20:59:42 +08:00
|
|
|
//send a ping response
|
|
|
|
int pingres(IP_Port ip_port, uint32_t ping_id)
|
|
|
|
{
|
2013-07-06 05:00:39 +08:00
|
|
|
uint8_t data[5 + CLIENT_ID_SIZE];
|
2013-06-24 20:59:42 +08:00
|
|
|
data[0] = 1;
|
|
|
|
|
|
|
|
memcpy(data + 1, &ping_id, 4);
|
2013-06-24 22:42:55 +08:00
|
|
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
return sendpacket(ip_port, data, sizeof(data));
|
2013-06-24 20:59:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
//send a getnodes request
|
2013-07-06 05:00:39 +08:00
|
|
|
int getnodes(IP_Port ip_port, uint8_t * client_id)
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
if(is_gettingnodes(ip_port, 0))
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
int ping_id = add_gettingnodes(ip_port);
|
|
|
|
|
|
|
|
if(ping_id == 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-25 02:26:29 +08:00
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
uint8_t data[5 + CLIENT_ID_SIZE*2];
|
2013-06-25 02:26:29 +08:00
|
|
|
data[0] = 2;
|
|
|
|
|
|
|
|
memcpy(data + 1, &ping_id, 4);
|
|
|
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
|
|
|
memcpy(data + 5 + CLIENT_ID_SIZE, client_id, CLIENT_ID_SIZE);
|
|
|
|
|
|
|
|
return sendpacket(ip_port, data, sizeof(data));
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-25 08:51:07 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
//send a send nodes response
|
2013-07-06 05:00:39 +08:00
|
|
|
int sendnodes(IP_Port ip_port, uint8_t * client_id, uint32_t ping_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-07-06 05:00:39 +08:00
|
|
|
uint8_t data[5 + CLIENT_ID_SIZE + (CLIENT_ID_SIZE + sizeof(IP_Port))*MAX_SENT_NODES];
|
2013-06-26 00:11:11 +08:00
|
|
|
Node_format nodes_list[MAX_SENT_NODES];
|
|
|
|
|
|
|
|
int num_nodes = get_close_nodes(client_id, nodes_list);
|
|
|
|
|
|
|
|
if(num_nodes == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
data[0] = 3;
|
|
|
|
|
|
|
|
memcpy(data + 1, &ping_id, 4);
|
|
|
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
|
|
|
memcpy(data + 5 + CLIENT_ID_SIZE, nodes_list, num_nodes * (CLIENT_ID_SIZE + sizeof(IP_Port)));
|
|
|
|
|
|
|
|
return sendpacket(ip_port, data, 5 + CLIENT_ID_SIZE + num_nodes * (CLIENT_ID_SIZE + sizeof(IP_Port)));
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-24 20:59:42 +08:00
|
|
|
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
//Packet handling functions
|
2013-07-09 00:36:11 +08:00
|
|
|
//One to handle each types of packets we receive
|
2013-06-28 08:59:16 +08:00
|
|
|
//return 0 if handled correctly, 1 if packet is bad.
|
2013-07-06 05:00:39 +08:00
|
|
|
int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)//tested
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-06-24 22:42:55 +08:00
|
|
|
if(length != 5 + CLIENT_ID_SIZE)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 20:59:42 +08:00
|
|
|
uint32_t ping_id;
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-24 20:59:42 +08:00
|
|
|
memcpy(&ping_id, packet + 1, 4);
|
2013-06-26 03:24:31 +08:00
|
|
|
IP_Port bad_ip = {{{0}}, 0};
|
|
|
|
|
|
|
|
if(is_pinging(bad_ip, ping_id))//check if packet is from ourself.
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 20:59:42 +08:00
|
|
|
pingres(source, ping_id);
|
2013-06-25 21:19:01 +08:00
|
|
|
|
|
|
|
pingreq(source);
|
2013-06-25 08:51:07 +08:00
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-06-24 22:42:55 +08:00
|
|
|
if(length != (5 + CLIENT_ID_SIZE))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t ping_id;
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-26 03:24:31 +08:00
|
|
|
memcpy(&ping_id, packet + 1, 4);
|
|
|
|
if(is_pinging(source, ping_id))
|
|
|
|
{
|
|
|
|
addto_lists(source, packet + 5);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-06-24 22:42:55 +08:00
|
|
|
if(length != (5 + CLIENT_ID_SIZE*2))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
uint32_t ping_id;
|
|
|
|
memcpy(&ping_id, packet + 1, 4);
|
|
|
|
sendnodes(source, packet + 5 + CLIENT_ID_SIZE, ping_id);
|
2013-06-25 00:23:46 +08:00
|
|
|
|
2013-06-26 03:24:31 +08:00
|
|
|
IP_Port bad_ip = {{{0}}, 0};
|
|
|
|
|
|
|
|
if(is_gettingnodes(bad_ip, ping_id))//check if packet is from ourself.
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-25 21:59:54 +08:00
|
|
|
pingreq(source);
|
|
|
|
|
2013-06-25 08:51:07 +08:00
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)//tested
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-06-28 08:59:16 +08:00
|
|
|
if(length > (5 + CLIENT_ID_SIZE + MAX_SENT_NODES * (CLIENT_ID_SIZE + sizeof(IP_Port))) ||
|
2013-06-26 00:11:11 +08:00
|
|
|
(length - 5 - CLIENT_ID_SIZE) % (CLIENT_ID_SIZE + sizeof(IP_Port)) != 0)
|
2013-06-24 22:42:55 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-26 05:50:30 +08:00
|
|
|
uint32_t num_nodes = (length - 5 - CLIENT_ID_SIZE) / (CLIENT_ID_SIZE + sizeof(IP_Port));
|
2013-06-25 21:19:01 +08:00
|
|
|
uint32_t i;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t ping_id;
|
|
|
|
|
|
|
|
memcpy(&ping_id, packet + 1, 4);
|
|
|
|
if(!is_gettingnodes(source, ping_id))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
|
|
|
|
Node_format nodes_list[MAX_SENT_NODES];
|
2013-06-26 00:11:11 +08:00
|
|
|
memcpy(nodes_list, packet + 5 + CLIENT_ID_SIZE, num_nodes * (CLIENT_ID_SIZE + sizeof(IP_Port)));
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
for(i = 0; i < num_nodes; i++)
|
|
|
|
{
|
|
|
|
pingreq(nodes_list[i].ip_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
addto_lists(source, packet + 5);
|
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
2013-06-25 04:50:43 +08:00
|
|
|
//END of packet handling functions
|
2013-06-24 20:28:19 +08:00
|
|
|
|
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
int DHT_addfriend(uint8_t * client_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-26 03:24:31 +08:00
|
|
|
//TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS
|
2013-06-26 00:11:11 +08:00
|
|
|
if(MAX_FRIENDS > num_friends)
|
|
|
|
{
|
|
|
|
memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
num_friends++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
int DHT_delfriend(uint8_t * client_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-25 04:50:43 +08:00
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; i < num_friends; i++)
|
|
|
|
{
|
|
|
|
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal
|
|
|
|
{
|
|
|
|
memcpy(friends_list[num_friends].client_id, friends_list[i].client_id, CLIENT_ID_SIZE);
|
|
|
|
num_friends--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
//TODO: Optimize this.
|
2013-07-07 23:54:25 +08:00
|
|
|
IP_Port DHT_getfriendip(uint8_t * client_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
uint32_t i, j;
|
|
|
|
IP_Port empty = {{{0}}, 0};
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
for(i = 0; i < num_friends; i++)
|
|
|
|
{
|
|
|
|
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal
|
|
|
|
{
|
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; j++)
|
|
|
|
{
|
|
|
|
if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0)
|
|
|
|
{
|
|
|
|
return friends_list[i].client_list[j].ip_port;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
empty.ip.i = 1;
|
|
|
|
return empty;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
2013-06-24 13:05:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-24 20:28:19 +08:00
|
|
|
switch (packet[0]) {
|
|
|
|
case 0:
|
2013-06-25 21:19:01 +08:00
|
|
|
return handle_pingreq(packet, length, source);
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
case 1:
|
2013-06-25 21:19:01 +08:00
|
|
|
return handle_pingres(packet, length, source);
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
case 2:
|
2013-06-25 21:19:01 +08:00
|
|
|
return handle_getnodes(packet, length, source);
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
case 3:
|
2013-06-25 21:19:01 +08:00
|
|
|
return handle_sendnodes(packet, length, source);
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
default:
|
2013-06-25 04:50:43 +08:00
|
|
|
return 1;
|
2013-06-24 20:28:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-26 00:11:11 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
//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
|
|
|
|
|
2013-06-26 00:11:11 +08:00
|
|
|
//ping interval in seconds for each random sending of a get nodes request.
|
2013-06-26 03:24:31 +08:00
|
|
|
#define GET_NODE_INTERVAL 10
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
//Ping each client in the "friends" list every 60 seconds.
|
|
|
|
//Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list.
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-26 19:40:07 +08:00
|
|
|
static uint32_t friend_lastgetnode[MAX_FRIENDS];
|
2013-06-26 00:11:11 +08:00
|
|
|
|
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
void doDHTFriends()
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
uint32_t i, j;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-06-26 00:11:11 +08:00
|
|
|
uint32_t num_nodes = 0;
|
|
|
|
uint32_t rand_node;
|
|
|
|
uint32_t index[MAX_FRIEND_CLIENTS];
|
|
|
|
|
2013-06-25 21:19:01 +08:00
|
|
|
for(i = 0; i < num_friends; i++)
|
|
|
|
{
|
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; j++)
|
|
|
|
{
|
|
|
|
if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead.
|
|
|
|
{
|
2013-06-27 19:37:06 +08:00
|
|
|
if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time)
|
2013-06-25 21:19:01 +08:00
|
|
|
{
|
|
|
|
pingreq(friends_list[i].client_list[j].ip_port);
|
2013-06-27 19:37:06 +08:00
|
|
|
friends_list[i].client_list[j].last_pinged = temp_time;
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
2013-06-26 00:11:11 +08:00
|
|
|
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good.
|
|
|
|
{
|
|
|
|
index[num_nodes] = j;
|
|
|
|
num_nodes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(friend_lastgetnode[i] + GET_NODE_INTERVAL <= temp_time && num_nodes != 0)
|
|
|
|
{
|
|
|
|
rand_node = rand() % num_nodes;
|
|
|
|
getnodes(friends_list[i].client_list[index[rand_node]].ip_port,
|
|
|
|
friends_list[i].client_list[index[rand_node]].client_id);
|
|
|
|
friend_lastgetnode[i] = temp_time;
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-06-26 19:40:07 +08:00
|
|
|
static uint32_t close_lastgetnodes;
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-06-26 00:11:11 +08:00
|
|
|
//Ping each client in the close nodes list every 60 seconds.
|
2013-06-26 21:56:15 +08:00
|
|
|
//Send a get nodes request every 20 seconds to a random good node in the list.
|
2013-06-26 00:11:11 +08:00
|
|
|
void doClose()//tested
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
uint32_t i;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-06-26 00:11:11 +08:00
|
|
|
uint32_t num_nodes = 0;
|
|
|
|
uint32_t rand_node;
|
|
|
|
uint32_t index[LCLIENT_LIST];
|
|
|
|
|
|
|
|
for(i = 0; i < LCLIENT_LIST; i++)
|
2013-06-25 21:19:01 +08:00
|
|
|
{
|
|
|
|
if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead.
|
|
|
|
{
|
2013-06-27 19:37:06 +08:00
|
|
|
if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
|
2013-06-25 21:19:01 +08:00
|
|
|
{
|
|
|
|
pingreq(close_clientlist[i].ip_port);
|
2013-06-27 19:37:06 +08:00
|
|
|
close_clientlist[i].last_pinged = temp_time;
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
2013-06-26 00:11:11 +08:00
|
|
|
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good.
|
|
|
|
{
|
|
|
|
index[num_nodes] = i;
|
|
|
|
num_nodes++;
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
2013-06-26 00:11:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0)
|
|
|
|
{
|
|
|
|
rand_node = rand() % num_nodes;
|
|
|
|
getnodes(close_clientlist[index[rand_node]].ip_port,
|
|
|
|
close_clientlist[index[rand_node]].client_id);
|
|
|
|
close_lastgetnodes = temp_time;
|
|
|
|
}
|
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
void doDHT()
|
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
doClose();
|
2013-07-09 08:50:25 +08:00
|
|
|
doDHTFriends();
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
void DHT_bootstrap(IP_Port ip_port)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-24 20:59:42 +08:00
|
|
|
|
|
|
|
getnodes(ip_port, self_client_id);
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-06-24 19:26:30 +08:00
|
|
|
}
|
|
|
|
|