summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authordubslow <bunslow@gmail.com>2014-09-28 12:37:47 -0500
committerdubslow <bunslow@gmail.com>2014-09-28 12:37:47 -0500
commit57666d95bac5e070acda74020f5faca88cfa4bd5 (patch)
tree7dd17305a43ce332984034a1dec783d8e96ac780 /toxcore/DHT.c
parent3711b881cb6f64cf08209f70216ec75672464d1a (diff)
parent72d6a92efd8cad191282c8a2e294a768c49d5f25 (diff)
Merge branch 'master' of https://github.com/irungentoo/toxcore
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c138
1 files changed, 103 insertions, 35 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index ca4e021b..db03def1 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -620,7 +620,7 @@ static int replace_all( Client_data *list,
620 const uint8_t *comp_client_id ) 620 const uint8_t *comp_client_id )
621{ 621{
622 if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6)) 622 if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6))
623 return 1; 623 return 0;
624 624
625 uint32_t i, replace = ~0, bad = ~0, possibly_bad = ~0, good = ~0; 625 uint32_t i, replace = ~0, bad = ~0, possibly_bad = ~0, good = ~0;
626 626
@@ -708,14 +708,41 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id)
708 } else 708 } else
709 used++; 709 used++;
710 710
711 DHT_Friend *friend_foundip = 0;
712
711 for (i = 0; i < dht->num_friends; ++i) { 713 for (i = 0; i < dht->num_friends; ++i) {
712 if (!client_or_ip_port_in_list(dht->friends_list[i].client_list, 714 if (!client_or_ip_port_in_list(dht->friends_list[i].client_list,
713 MAX_FRIEND_CLIENTS, client_id, ip_port)) { 715 MAX_FRIEND_CLIENTS, client_id, ip_port)) {
714 if (replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, 716 if (replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
715 client_id, ip_port, dht->friends_list[i].client_id)) 717 client_id, ip_port, dht->friends_list[i].client_id)) {
718
719 DHT_Friend *friend = &dht->friends_list[i];
720
721 if (memcmp(client_id, friend->client_id, CLIENT_ID_SIZE) == 0) {
722 friend_foundip = friend;
723 }
724
716 used++; 725 used++;
717 } else 726 }
727 } else {
728 DHT_Friend *friend = &dht->friends_list[i];
729
730 if (memcmp(client_id, friend->client_id, CLIENT_ID_SIZE) == 0) {
731 friend_foundip = friend;
732 }
733
718 used++; 734 used++;
735 }
736 }
737
738 if (friend_foundip) {
739 uint32_t j;
740
741 for (j = 0; j < friend_foundip->lock_count; ++j) {
742 if (friend_foundip->callbacks[j].ip_callback)
743 friend_foundip->callbacks[j].ip_callback(friend_foundip->callbacks[j].data, friend_foundip->callbacks[j].number,
744 ip_port);
745 }
719 } 746 }
720 747
721#ifdef ENABLE_ASSOC_DHT 748#ifdef ENABLE_ASSOC_DHT
@@ -1089,23 +1116,54 @@ static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_
1089 } 1116 }
1090} 1117}
1091*/ 1118*/
1092int DHT_addfriend(DHT *dht, const uint8_t *client_id) 1119int DHT_addfriend(DHT *dht, const uint8_t *client_id, void (*ip_callback)(void *data, int32_t number, IP_Port),
1120 void *data, int32_t number, uint16_t *lock_count)
1093{ 1121{
1094 if (friend_number(dht, client_id) != -1) /* Is friend already in DHT? */ 1122 int friend_num = friend_number(dht, client_id);
1095 return 1; 1123
1124 uint16_t lock_num;
1125
1126 if (friend_num != -1) { /* Is friend already in DHT? */
1127 DHT_Friend *friend = &dht->friends_list[friend_num];
1128
1129 if (friend->lock_count == DHT_FRIEND_MAX_LOCKS)
1130 return -1;
1131
1132 lock_num = friend->lock_count;
1133 ++friend->lock_count;
1134 friend->callbacks[lock_num].ip_callback = ip_callback;
1135 friend->callbacks[lock_num].data = data;
1136 friend->callbacks[lock_num].number = number;
1137
1138 if (lock_count)
1139 *lock_count = lock_num + 1;
1140
1141 return 0;
1142 }
1096 1143
1097 DHT_Friend *temp; 1144 DHT_Friend *temp;
1098 temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); 1145 temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1));
1099 1146
1100 if (temp == NULL) 1147 if (temp == NULL)
1101 return 1; 1148 return -1;
1102 1149
1103 dht->friends_list = temp; 1150 dht->friends_list = temp;
1104 memset(&dht->friends_list[dht->num_friends], 0, sizeof(DHT_Friend)); 1151 DHT_Friend *friend = &dht->friends_list[dht->num_friends];
1105 memcpy(dht->friends_list[dht->num_friends].client_id, client_id, CLIENT_ID_SIZE); 1152 memset(friend, 0, sizeof(DHT_Friend));
1153 memcpy(friend->client_id, client_id, CLIENT_ID_SIZE);
1106 1154
1107 dht->friends_list[dht->num_friends].nat.NATping_id = random_64b(); 1155 friend->nat.NATping_id = random_64b();
1108 ++dht->num_friends; 1156 ++dht->num_friends;
1157
1158 lock_num = friend->lock_count;
1159 ++friend->lock_count;
1160 friend->callbacks[lock_num].ip_callback = ip_callback;
1161 friend->callbacks[lock_num].data = data;
1162 friend->callbacks[lock_num].number = number;
1163
1164 if (lock_count)
1165 *lock_count = lock_num + 1;
1166
1109#ifdef ENABLE_ASSOC_DHT 1167#ifdef ENABLE_ASSOC_DHT
1110 1168
1111 if (dht->assoc) { 1169 if (dht->assoc) {
@@ -1143,39 +1201,49 @@ int DHT_addfriend(DHT *dht, const uint8_t *client_id)
1143 return 0; 1201 return 0;
1144} 1202}
1145 1203
1146int DHT_delfriend(DHT *dht, const uint8_t *client_id) 1204int DHT_delfriend(DHT *dht, const uint8_t *client_id, uint16_t lock_count)
1147{ 1205{
1148 uint32_t i; 1206 int friend_num = friend_number(dht, client_id);
1149 DHT_Friend *temp;
1150 1207
1151 for (i = 0; i < dht->num_friends; ++i) { 1208 if (friend_num == -1) {
1152 /* Equal */ 1209 return -1;
1153 if (id_equal(dht->friends_list[i].client_id, client_id)) { 1210 }
1154 --dht->num_friends;
1155 1211
1156 if (dht->num_friends != i) { 1212 DHT_Friend *friend = &dht->friends_list[friend_num];
1157 memcpy( &dht->friends_list[i], 1213 --friend->lock_count;
1158 &dht->friends_list[dht->num_friends],
1159 sizeof(DHT_Friend) );
1160 }
1161 1214
1162 if (dht->num_friends == 0) { 1215 if (friend->lock_count && lock_count) { /* DHT friend is still in use.*/
1163 free(dht->friends_list); 1216 --lock_count;
1164 dht->friends_list = NULL; 1217 friend->callbacks[lock_count].ip_callback = NULL;
1165 return 0; 1218 friend->callbacks[lock_count].data = NULL;
1166 } 1219 friend->callbacks[lock_count].number = 0;
1220 return 0;
1221 }
1222
1223 uint32_t i;
1224 DHT_Friend *temp;
1167 1225
1168 temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); 1226 --dht->num_friends;
1169 1227
1170 if (temp == NULL) 1228 if (dht->num_friends != friend_num) {
1171 return 1; 1229 memcpy( &dht->friends_list[friend_num],
1230 &dht->friends_list[dht->num_friends],
1231 sizeof(DHT_Friend) );
1232 }
1172 1233
1173 dht->friends_list = temp; 1234 if (dht->num_friends == 0) {
1174 return 0; 1235 free(dht->friends_list);
1175 } 1236 dht->friends_list = NULL;
1237 return 0;
1176 } 1238 }
1177 1239
1178 return 1; 1240 temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends));
1241
1242 if (temp == NULL)
1243 return -1;
1244
1245 dht->friends_list = temp;
1246 return 0;
1179} 1247}
1180 1248
1181/* TODO: Optimize this. */ 1249/* TODO: Optimize this. */
@@ -2216,7 +2284,7 @@ DHT *new_DHT(Networking_Core *net)
2216 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { 2284 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) {
2217 uint8_t random_key_bytes[CLIENT_ID_SIZE]; 2285 uint8_t random_key_bytes[CLIENT_ID_SIZE];
2218 randombytes(random_key_bytes, sizeof(random_key_bytes)); 2286 randombytes(random_key_bytes, sizeof(random_key_bytes));
2219 DHT_addfriend(dht, random_key_bytes); 2287 DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0);
2220 } 2288 }
2221 2289
2222 return dht; 2290 return dht;