2013-06-24 13:05:28 +08:00
|
|
|
#include "DHT.h"
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
|
|
|
|
//Function to send packet(data) of length length to ip_port
|
|
|
|
int sendpacket(IP_Port ip_port, char * data, uint32_t length)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-24 20:28:19 +08:00
|
|
|
ADDR addr = {.family = AF_INET, .ip = ip_port.ip, .port = ip_port.port};
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
}
|
|
|
|
|
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-06-25 00:23:46 +08:00
|
|
|
int id_closest(char * client_id, char * client_id1, char * client_id2)
|
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-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-25 00:23:46 +08:00
|
|
|
//return True(1) or False(0)
|
|
|
|
int client_in_list(Client_data * list, uint32_t length, char * client_id)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((j - 1) == CLIENT_ID_SIZE)
|
|
|
|
{
|
2013-06-25 02:26:29 +08:00
|
|
|
//Refresh the client timestamp.
|
|
|
|
list[i].timestamp = unix_time();
|
2013-06-25 00:23:46 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//the number of seconds for a non responsive node to become bad.
|
|
|
|
#define BAD_NODE_TIMEOUT 130
|
|
|
|
|
|
|
|
//replace first bad (or empty) node with this one
|
|
|
|
//return 0 if successfull
|
|
|
|
//return 1 if not (list contains no bad nodes)
|
|
|
|
int replace_bad(Client_data * list, uint32_t length, char * client_id, IP_Port ip_port)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
if(list[i].timestamp + BAD_NODE_TIMEOUT < unix_time())
|
|
|
|
{
|
|
|
|
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
list[i].ip_port = ip_port;
|
|
|
|
list[i].timestamp = unix_time();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//replace the first good node further to the comp_client_id than that of the client_id
|
|
|
|
int replace_good(Client_data * list, uint32_t length, char * client_id, IP_Port ip_port, char * comp_client_id)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
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;
|
|
|
|
list[i].timestamp = unix_time();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
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
|
|
|
|
int addto_lists(IP_Port ip_port, char * client_id)
|
|
|
|
{
|
2013-06-25 00:23:46 +08:00
|
|
|
uint32_t i, j;
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
//NOTE: current behaviour if there are two clients with the same id is to only keep one (the first one)
|
|
|
|
if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id))
|
|
|
|
{
|
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
if(!client_in_list(friends_list[i].client_list, LCLIENT_LIST, client_id))
|
|
|
|
{
|
|
|
|
|
|
|
|
if(replace_bad(friends_list[i].client_list, LCLIENT_LIST, client_id, ip_port))
|
|
|
|
{
|
|
|
|
//if we can't replace bad nodes we try replacing good ones
|
|
|
|
replace_good(friends_list[i].client_list, LCLIENT_LIST, client_id, ip_port, self_client_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
#define PING_TIMEOUT 10
|
|
|
|
//check if we are currently pinging an ip_port
|
|
|
|
//if we are already, return 1
|
|
|
|
//else return 0
|
|
|
|
//TODO: Maybe optimize this
|
|
|
|
int is_pinging(IP_Port ip_port)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for(i = 0; i < LPING_ARRAY; i++ )
|
|
|
|
{
|
|
|
|
if((pings[i].timestamp + PING_TIMEOUT) > unix_time() && pings[i].ip_port == ip_port)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Same as last function but for get_node requests.
|
|
|
|
int is_gettingnodes(IP_Port ip_port)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for(i = 0; i < LSEND_NODES_ARRAY; i++ )
|
|
|
|
{
|
|
|
|
if((send_nodes[i].timestamp + PING_TIMEOUT) > unix_time() && send_nodes[i].ip_port == ip_port)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add a new ping request to the list of ping requests
|
|
|
|
//returns the ping_id to put in the ping request
|
|
|
|
//TODO: Maybe optimize this
|
|
|
|
int add_pinging(IP_Port ip_port)
|
|
|
|
{
|
|
|
|
uint32_t i, j;
|
|
|
|
int ping_id = rand();
|
|
|
|
for(i = 0; i < PING_TIMEOUT; i++ )
|
|
|
|
{
|
|
|
|
for(j = 0; j < LPING_ARRAY; j++ )
|
|
|
|
{
|
|
|
|
if((pings[j].timestamp + PING_TIMEOUT - i) < unix_time())
|
|
|
|
{
|
|
|
|
pings[j].timestamp = unix_time();
|
|
|
|
pings[j].ip_port = ip_port;
|
|
|
|
pings[j].ping_id = ping_id;
|
|
|
|
return ping_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Same but for get node requests
|
|
|
|
int add_gettingnodes(IP_Port ip_port)
|
|
|
|
{
|
|
|
|
uint32_t i, j;
|
|
|
|
int ping_id = rand();
|
|
|
|
for(i = 0; i < PING_TIMEOUT; i++ )
|
|
|
|
{
|
|
|
|
for(j = 0; j < LSEND_NODES_ARRAY; j++ )
|
|
|
|
{
|
|
|
|
if((send_nodes[j].timestamp + PING_TIMEOUT - i) < unix_time())
|
|
|
|
{
|
|
|
|
send_nodes[j].timestamp = unix_time();
|
|
|
|
send_nodes[j].ip_port = ip_port;
|
|
|
|
send_nodes[j].ping_id = ping_id;
|
|
|
|
return ping_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-24 20:59:42 +08:00
|
|
|
//send a ping request
|
|
|
|
int pingreq(IP_Port ip_port)
|
|
|
|
{
|
2013-06-25 02:26:29 +08:00
|
|
|
if(is_pinging(ip_port))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ping_id = add_pinging(ip_port);
|
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
char 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-06-24 22:42:55 +08:00
|
|
|
char 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
|
|
|
|
int getnodes(IP_Port ip_port, char * client_id)
|
|
|
|
{
|
2013-06-25 02:26:29 +08:00
|
|
|
if(is_gettingnodes(ip_port))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ping_id = add_pinging(ip_port);
|
|
|
|
|
|
|
|
char data[5 + CLIENT_ID_SIZE*2];
|
|
|
|
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-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
//send a send nodes response
|
|
|
|
//Currently incomplete: missing bunch of stuff
|
2013-06-24 20:59:42 +08:00
|
|
|
int sendnodes(IP_Port ip_port, char * client_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-24 22:42:55 +08:00
|
|
|
char data[5 + (CLIENT_ID_SIZE + 6)*8];
|
2013-06-24 20:59:42 +08:00
|
|
|
data[0] = 3;
|
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
|
|
|
memcpy(data + 5 + CLIENT_ID_SIZE, client_id, CLIENT_ID_SIZE);
|
2013-06-24 20:59:42 +08:00
|
|
|
|
|
|
|
sendpacket(ip_port, data, sizeof(data));
|
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
|
|
|
|
//One to handle each types of packets
|
|
|
|
|
|
|
|
int handle_pingreq(char * packet, uint32_t length, IP_Port source)
|
|
|
|
{
|
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);
|
|
|
|
pingres(source, ping_id);
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
|
|
|
|
return 0;
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int handle_pingres(char * packet, uint32_t length, IP_Port source)
|
|
|
|
{
|
2013-06-24 22:42:55 +08:00
|
|
|
if(length != (5 + CLIENT_ID_SIZE))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
addto_lists(source, packet + 5);
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int handle_getnodes(char * packet, uint32_t length, IP_Port source)
|
|
|
|
{
|
2013-06-24 22:42:55 +08:00
|
|
|
if(length != (5 + CLIENT_ID_SIZE*2))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
return 0;
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int handle_sendnodes(char * packet, uint32_t length, IP_Port source)
|
|
|
|
{
|
2013-06-24 22:42:55 +08:00
|
|
|
if(length > 325 || (length - 5) % (CLIENT_ID_SIZE + 6) != 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-25 00:23:46 +08:00
|
|
|
addto_lists(source, packet + 5);
|
2013-06-24 20:28:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
|
|
|
|
void addfriend(char * client_id)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-24 19:26:30 +08:00
|
|
|
char delfriend(char * client_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IP_Port getfriendip(char * client_id)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
void DHT_recvpacket(char * 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:
|
|
|
|
handle_pingreq(packet, length, source);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
handle_pingres(packet, length, source);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
handle_getnodes(packet, length, source);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
handle_sendnodes(packet, length, source);
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-24 20:59:42 +08:00
|
|
|
return;
|
2013-06-24 20:28:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-24 19:26:30 +08:00
|
|
|
|
2013-06-24 13:05:28 +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.
|
|
|
|
void doFriends()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2013-06-24 13:05:28 +08:00
|
|
|
|
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
void doClose()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void bootstrap(IP_Port ip_port)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|