summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-16 13:20:45 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-16 13:20:45 -0400
commite63ba9c61b8603232e6fccc24f7bc763d22c3239 (patch)
tree015b4d1d6ac05f6e956f214ec1adc2bb8e7f5c3c /core/DHT.c
parentc8d046e34862f50b82209503025dfa4bb06147c9 (diff)
Loading and saving added to DHT.
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c103
1 files changed, 72 insertions, 31 deletions
diff --git a/core/DHT.c b/core/DHT.c
index ce2ce6f8..18f2de38 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -110,35 +110,22 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)/
110 110
111//check if client with client_id is already in list of length length. 111//check if client with client_id is already in list of length length.
112//if it is set it's corresponding timestamp to current time. 112//if it is set it's corresponding timestamp to current time.
113//if the ip_port is already in the list but associated to a different ip, change it. 113//if the id is already in the list with a different ip_port, update it.
114//return True(1) or False(0) 114//return True(1) or False(0)
115//TODO: maybe optimize this. 115//TODO: maybe optimize this.
116int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) 116int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)
117{ 117{
118 uint32_t i, j; 118 uint32_t i;
119 uint32_t temp_time = unix_time(); 119 uint32_t temp_time = unix_time();
120 120
121 for(i = 0; i < length; i++) 121 for(i = 0; i < length; i++)
122 { 122 {
123 //If the id for an ip/port changes, replace it. 123 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
124 if(list[i].ip_port.ip.i == ip_port.ip.i &&
125 list[i].ip_port.port == ip_port.port)
126 {
127 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
128 }
129
130 for(j = 0; j < CLIENT_ID_SIZE; j++)
131 {
132
133 if(list[i].client_id[j] != client_id[j])
134 {
135 break;
136 }
137 }
138 if(j == CLIENT_ID_SIZE)
139 { 124 {
140 //Refresh the client timestamp. 125 //Refresh the client timestamp.
141 list[i].timestamp = temp_time; 126 list[i].timestamp = temp_time;
127 list[i].ip_port.ip.i = ip_port.ip.i;
128 list[i].ip_port.port = ip_port.port;
142 return 1; 129 return 1;
143 } 130 }
144 } 131 }
@@ -150,18 +137,10 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
150//return True(1) or False(0) 137//return True(1) or False(0)
151int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) 138int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
152{ 139{
153 uint32_t i, j; 140 uint32_t i;
154 for(i = 0; i < length; i++) 141 for(i = 0; i < length; i++)
155 { 142 {
156 for(j = 0; j < CLIENT_ID_SIZE; j++) 143 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
157 {
158
159 if(list[i].client_id[j] != client_id[j])
160 {
161 break;
162 }
163 }
164 if(j == CLIENT_ID_SIZE)
165 { 144 {
166 145
167 return 1; 146 return 1;
@@ -290,7 +269,7 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
290{ 269{
291 uint32_t i; 270 uint32_t i;
292 271
293 //NOTE: current behavior if there are two clients with the same id is to only keep one (the first one) 272 //NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second.
294 if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) 273 if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port))
295 { 274 {
296 275
@@ -818,14 +797,15 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
818{ 797{
819 uint32_t i, j; 798 uint32_t i, j;
820 IP_Port empty = {{{0}}, 0}; 799 IP_Port empty = {{{0}}, 0};
821 800 uint32_t temp_time = unix_time();
822 for(i = 0; i < num_friends; i++) 801 for(i = 0; i < num_friends; i++)
823 { 802 {
824 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal 803 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal
825 { 804 {
826 for(j = 0; j < MAX_FRIEND_CLIENTS; j++) 805 for(j = 0; j < MAX_FRIEND_CLIENTS; j++)
827 { 806 {
828 if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0) 807 if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 &&
808 friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)
829 { 809 {
830 return friends_list[i].client_list[j].ip_port; 810 return friends_list[i].client_list[j].ip_port;
831 } 811 }
@@ -976,3 +956,64 @@ void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
976 956
977} 957}
978 958
959
960//get the size of the DHT (for saving)
961uint32_t DHT_size()
962{
963 return sizeof(close_clientlist) + sizeof(Friend) * num_friends;
964}
965
966//save the DHT in data where data is an array of size DHT_size()
967void DHT_save(uint8_t * data)
968{
969 memcpy(data, close_clientlist, sizeof(close_clientlist));
970 memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends);
971}
972
973//load the DHT from data of size size;
974//return -1 if failure
975//return 0 if success
976int DHT_load(uint8_t * data, uint32_t size)
977{
978 if(size < sizeof(close_clientlist))
979 {
980 return -1;
981 }
982 if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0)
983 {
984 return -1;
985 }
986 uint32_t i, j;
987 //uint32_t temp_time = unix_time();
988 uint16_t temp;
989
990 temp = (size - sizeof(close_clientlist))/sizeof(Friend);
991
992 if(temp != 0)
993 {
994 Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist));
995
996 for(i = 0; i < temp; i++)
997 {
998 DHT_addfriend(tempfriends_list[i].client_id);
999 for(j = 0; j < MAX_FRIEND_CLIENTS; j++)
1000 {
1001 if(tempfriends_list[i].client_list[j].timestamp != 0)
1002 {
1003 getnodes(tempfriends_list[i].client_list[j].ip_port,
1004 tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id);
1005 }
1006 }
1007 }
1008 }
1009 Client_data * tempclose_clientlist = (Client_data *)data;
1010
1011 for(i = 0; i < LCLIENT_LIST; i++)
1012 {
1013 if(tempclose_clientlist[i].timestamp != 0)
1014 {
1015 DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id);
1016 }
1017 }
1018 return 0;
1019}