mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
More functions added, some fixes.
This commit is contained in:
parent
73a742a2ba
commit
3bc95be051
113
core/DHT.c
113
core/DHT.c
|
@ -13,9 +13,9 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length)
|
|||
//return 0 if both are same distance
|
||||
//return 1 if client_id1 is closer.
|
||||
//return 2 if client_id2 is closer.
|
||||
int id_distance(char * client_id, char * client_id1, char * client_id2)
|
||||
int id_closest(char * client_id, char * client_id1, char * client_id2)
|
||||
{
|
||||
int i;
|
||||
uint32_t i;
|
||||
for(i = 0; i < CLIENT_ID_SIZE; i++)
|
||||
{
|
||||
if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i]))
|
||||
|
@ -32,11 +32,97 @@ int id_distance(char * client_id, char * client_id1, char * client_id2)
|
|||
return 0;
|
||||
}
|
||||
|
||||
//check if client with client_id is already in list of length length.
|
||||
//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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
//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)
|
||||
{
|
||||
uint32_t i, j;
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -113,7 +199,7 @@ int handle_pingreq(char * packet, uint32_t length, IP_Port source)
|
|||
memcpy(&ping_id, packet + 1, 4);
|
||||
pingres(source, ping_id);
|
||||
|
||||
addto_lists(source, packet + 5);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -125,7 +211,7 @@ int handle_pingres(char * packet, uint32_t length, IP_Port source)
|
|||
return 1;
|
||||
}
|
||||
|
||||
|
||||
addto_lists(source, packet + 5);
|
||||
}
|
||||
|
||||
int handle_getnodes(char * packet, uint32_t length, IP_Port source)
|
||||
|
@ -136,7 +222,7 @@ int handle_getnodes(char * packet, uint32_t length, IP_Port source)
|
|||
}
|
||||
|
||||
|
||||
addto_lists(source, packet + 5);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -147,7 +233,7 @@ int handle_sendnodes(char * packet, uint32_t length, IP_Port source)
|
|||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
addto_lists(source, packet + 5);
|
||||
|
||||
}
|
||||
|
||||
|
@ -212,8 +298,23 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source)
|
|||
|
||||
}
|
||||
|
||||
//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()
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void doClose()
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void doDHT()
|
||||
|
|
|
@ -92,7 +92,7 @@ char delfriend(char * client_id);
|
|||
//ip must be 4 bytes long.
|
||||
//port must be 2 bytes long.
|
||||
//returns ip if success
|
||||
//returns ip of 0 if failure (This means the friend is either offline of we have not found him yet.)
|
||||
//returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.)
|
||||
IP_Port getfriendip(char * client_id);
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user