summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-12-06 21:56:06 -0500
committerirungentoo <irungentoo@gmail.com>2014-12-06 21:56:06 -0500
commit2bb1549bc97267ba6c00f6907ba16ea4d78aad4e (patch)
treed31162576aba39a48bd54d0fd188de409260e7ae
parent422f228921ca77b8a7c7ef1f1c4d95797d4ad0aa (diff)
Group stability fixes.
-rw-r--r--toxcore/group.c25
1 files changed, 11 insertions, 14 deletions
diff --git a/toxcore/group.c b/toxcore/group.c
index 27ad6261..5f075c04 100644
--- a/toxcore/group.c
+++ b/toxcore/group.c
@@ -188,21 +188,18 @@ static int get_peer_index(Group_c *g, uint16_t peer_number)
188} 188}
189 189
190 190
191static uint16_t calculate_comp_value(const uint8_t *pk1, const uint8_t *pk2) 191static uint64_t calculate_comp_value(const uint8_t *pk1, const uint8_t *pk2)
192{ 192{
193 uint8_t cmp1, cmp2 = 0; 193 uint64_t cmp1 = 0, cmp2 = 0;
194 194
195 for (cmp1 = crypto_box_PUBLICKEYBYTES; cmp1 != 0; --cmp1) { 195 unsigned int i;
196 uint8_t index = crypto_box_PUBLICKEYBYTES - cmp1;
197
198 if (pk1[index] == pk2[index])
199 continue;
200 196
201 cmp2 = pk1[index] - pk2[index]; 197 for (i = sizeof(uint64_t); i != 0; --i) {
202 break; 198 cmp1 = (cmp1 << 8) + (uint64_t)pk1[i - 1];
199 cmp2 = (cmp2 << 8) + (uint64_t)pk2[i - 1];
203 } 200 }
204 201
205 return (cmp1 << 8) + cmp2; 202 return (cmp1 - cmp2);
206} 203}
207 204
208enum { 205enum {
@@ -241,11 +238,11 @@ static int add_to_closest(Group_Chats *g_c, int groupnumber, const uint8_t *real
241 } 238 }
242 239
243 if (index == DESIRED_CLOSE_CONNECTIONS) { 240 if (index == DESIRED_CLOSE_CONNECTIONS) {
244 uint16_t comp_val = calculate_comp_value(g->real_pk, real_pk); 241 uint64_t comp_val = calculate_comp_value(g->real_pk, real_pk);
245 uint16_t comp_d = 0; 242 uint64_t comp_d = 0;
246 243
247 for (i = 0; i < (DESIRED_CLOSE_CONNECTIONS / 2); ++i) { 244 for (i = 0; i < (DESIRED_CLOSE_CONNECTIONS / 2); ++i) {
248 uint16_t comp; 245 uint64_t comp;
249 comp = calculate_comp_value(g->real_pk, g->closest_peers[i].real_pk); 246 comp = calculate_comp_value(g->real_pk, g->closest_peers[i].real_pk);
250 247
251 if (comp > comp_val && comp > comp_d) { 248 if (comp > comp_val && comp > comp_d) {
@@ -257,7 +254,7 @@ static int add_to_closest(Group_Chats *g_c, int groupnumber, const uint8_t *real
257 comp_val = calculate_comp_value(real_pk, g->real_pk); 254 comp_val = calculate_comp_value(real_pk, g->real_pk);
258 255
259 for (i = (DESIRED_CLOSE_CONNECTIONS / 2); i < DESIRED_CLOSE_CONNECTIONS; ++i) { 256 for (i = (DESIRED_CLOSE_CONNECTIONS / 2); i < DESIRED_CLOSE_CONNECTIONS; ++i) {
260 uint16_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk); 257 uint64_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk);
261 258
262 if (comp > comp_val && comp > comp_d) { 259 if (comp > comp_val && comp > comp_d) {
263 index = i; 260 index = i;