summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-21 13:14:36 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-21 13:14:36 -0400
commit7fecd73ae03245ffa39937ae0f815648c8c62832 (patch)
treefbfdc2181633891a148493857f1c9049b813bc11 /core/DHT.c
parent38e49709c1857d2a8b6a3420492fee9fe74723d3 (diff)
Added routing functions to DHT.
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c119
1 files changed, 117 insertions, 2 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 4aa73697..c6b7e81b 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -34,6 +34,8 @@ typedef struct
34 IP_Port ip_port; 34 IP_Port ip_port;
35 uint32_t timestamp; 35 uint32_t timestamp;
36 uint32_t last_pinged; 36 uint32_t last_pinged;
37 IP_Port ret_ip_port;/* The ip_port returned by this node for the friend
38 (for nodes in friends_list) or us (for nodes in close_clientlist) */
37}Client_data; 39}Client_data;
38/* maximum number of clients stored per friend. */ 40/* maximum number of clients stored per friend. */
39#define MAX_FRIEND_CLIENTS 8 41#define MAX_FRIEND_CLIENTS 8
@@ -237,6 +239,8 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por
237 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 239 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
238 list[i].ip_port = ip_port; 240 list[i].ip_port = ip_port;
239 list[i].timestamp = temp_time; 241 list[i].timestamp = temp_time;
242 list[i].ret_ip_port.ip.i = 0;
243 list[i].ret_ip_port.port = 0;
240 return 0; 244 return 0;
241 } 245 }
242 } 246 }
@@ -257,6 +261,8 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po
257 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 261 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
258 list[i].ip_port = ip_port; 262 list[i].ip_port = ip_port;
259 list[i].timestamp = temp_time; 263 list[i].timestamp = temp_time;
264 list[i].ret_ip_port.ip.i = 0;
265 list[i].ret_ip_port.port = 0;
260 return 0; 266 return 0;
261 } 267 }
262 } 268 }
@@ -294,6 +300,38 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
294 } 300 }
295} 301}
296 302
303/* If client_id is a friend or us, update ret_ip_port
304 nodeclient_id is the id of the node that sent us this info */
305void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id)
306{
307 uint32_t i, j;
308 if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0)
309 {
310 for(i = 0; i < LCLIENT_LIST; ++i)
311 {
312 if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0)
313 {
314 close_clientlist[i].ret_ip_port = ip_port;
315 return;
316 }
317 }
318 }
319 else
320 for(i = 0; i < num_friends; ++i)
321 {
322 if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0)
323 {
324 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
325 {
326 if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0)
327 {
328 friends_list[i].client_list[j].ret_ip_port = ip_port;
329 return;
330 }
331 }
332 }
333 }
334}
297 335
298/* ping timeout in seconds */ 336/* ping timeout in seconds */
299#define PING_TIMEOUT 5 337#define PING_TIMEOUT 5
@@ -725,15 +763,16 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
725 Node_format nodes_list[MAX_SENT_NODES]; 763 Node_format nodes_list[MAX_SENT_NODES];
726 memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); 764 memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format));
727 765
766 addto_lists(source, packet + 1);
767
728 uint32_t i; 768 uint32_t i;
729 for(i = 0; i < num_nodes; ++i) 769 for(i = 0; i < num_nodes; ++i)
730 { 770 {
731 pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); 771 pingreq(nodes_list[i].ip_port, nodes_list[i].client_id);
772 returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
732 } 773 }
733 774
734 addto_lists(source, packet + 1);
735 return 0; 775 return 0;
736
737} 776}
738 777
739/* END of packet handling functions */ 778/* END of packet handling functions */
@@ -950,6 +989,81 @@ void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
950} 989}
951 990
952 991
992
993/* send the given packet to node with client_id
994 returns -1 if failure */
995int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length)
996{
997 uint32_t i;
998 for(i = 0; i < LCLIENT_LIST; ++i)
999 {
1000 if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0)
1001 {
1002 return sendpacket(close_clientlist[i].ip_port, packet, length);
1003 }
1004 }
1005 return -1;
1006}
1007
1008/* Send the following packet to everyone who tells us they are connected to friend_id
1009 returns the number of nodes it sent the packet to */
1010int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
1011{
1012 uint32_t i, j;
1013 uint32_t sent = 0;
1014 uint32_t temp_time = unix_time();
1015 for(i = 0; i < num_friends; ++i)
1016 {
1017 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */
1018 {
1019 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
1020 {
1021 if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 &&
1022 friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)
1023 /*If ip is not zero and node is good */
1024 {
1025 if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length)
1026 {
1027 ++sent;
1028 }
1029 }
1030 }
1031 return sent;
1032 }
1033 }
1034 return 0;
1035}
1036
1037/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
1038 ip_portlist must be at least MAX_FRIEND_CLIENTS big
1039 returns the number of ips returned
1040 returns -1 if no such friend*/
1041int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id)
1042{
1043 int num_ips = 0;
1044 uint32_t i, j;
1045 uint32_t temp_time = unix_time();
1046 for(i = 0; i < num_friends; ++i)
1047 {
1048 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */
1049 {
1050 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
1051 {
1052 if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 &&
1053 friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)
1054 /*If ip is not zero and node is good */
1055 {
1056 ip_portlist[num_ips] = friends_list[i].client_list[j].ret_ip_port;
1057 ++num_ips;
1058 }
1059 }
1060 return num_ips;
1061 }
1062
1063 }
1064 return 0;
1065}
1066
953/* get the size of the DHT (for saving) */ 1067/* get the size of the DHT (for saving) */
954uint32_t DHT_size() 1068uint32_t DHT_size()
955{ 1069{
@@ -1026,3 +1140,4 @@ int DHT_isconnected()
1026 } 1140 }
1027 return 0; 1141 return 0;
1028} 1142}
1143