summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c12
-rw-r--r--toxcore/Messenger.h6
-rw-r--r--toxcore/group.c78
-rw-r--r--toxcore/tox.c6
-rw-r--r--toxcore/tox.h6
5 files changed, 61 insertions, 47 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 45b6fa25..7d93b2da 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1441,13 +1441,15 @@ static int handle_custom_lossy_packet(void *object, int friend_num, const uint8_
1441 1441
1442 if (m->friendlist[friend_num].lossy_packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function) 1442 if (m->friendlist[friend_num].lossy_packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function)
1443 return m->friendlist[friend_num].lossy_packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function( 1443 return m->friendlist[friend_num].lossy_packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function(
1444 m, friend_num, packet, length, m->friendlist[friend_num].lossy_packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].object); 1444 m, friend_num, packet, length, m->friendlist[friend_num].lossy_packethandlers[packet[0] %
1445 PACKET_ID_LOSSY_RANGE_SIZE].object);
1445 1446
1446 return 1; 1447 return 1;
1447} 1448}
1448 1449
1449int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, 1450int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
1450 int (*packet_handler_callback)(Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object), void *object) 1451 int (*packet_handler_callback)(Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object),
1452 void *object)
1451{ 1453{
1452 if (friend_not_valid(m, friendnumber)) 1454 if (friend_not_valid(m, friendnumber))
1453 return -1; 1455 return -1;
@@ -1490,13 +1492,15 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin
1490 1492
1491 if (m->friendlist[friend_num].lossless_packethandlers[packet[0] % PACKET_ID_LOSSLESS_RANGE_SIZE].function) 1493 if (m->friendlist[friend_num].lossless_packethandlers[packet[0] % PACKET_ID_LOSSLESS_RANGE_SIZE].function)
1492 return m->friendlist[friend_num].lossless_packethandlers[packet[0] % PACKET_ID_LOSSLESS_RANGE_SIZE].function( 1494 return m->friendlist[friend_num].lossless_packethandlers[packet[0] % PACKET_ID_LOSSLESS_RANGE_SIZE].function(
1493 m, friend_num, packet, length, m->friendlist[friend_num].lossless_packethandlers[packet[0] % PACKET_ID_LOSSLESS_RANGE_SIZE].object); 1495 m, friend_num, packet, length, m->friendlist[friend_num].lossless_packethandlers[packet[0] %
1496 PACKET_ID_LOSSLESS_RANGE_SIZE].object);
1494 1497
1495 return 1; 1498 return 1;
1496} 1499}
1497 1500
1498int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, 1501int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
1499 int (*packet_handler_callback)(Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object), void *object) 1502 int (*packet_handler_callback)(Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object),
1503 void *object)
1500{ 1504{
1501 if (friend_not_valid(m, friendnumber)) 1505 if (friend_not_valid(m, friendnumber))
1502 return -1; 1506 return -1;
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 2d31f0b9..ae6f54c6 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -843,7 +843,8 @@ int m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data,
843 * return 0 on success. 843 * return 0 on success.
844 */ 844 */
845int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, 845int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
846 int (*packet_handler_callback)(Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object), void *object); 846 int (*packet_handler_callback)(Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object),
847 void *object);
847 848
848/* High level function to send custom lossy packets. 849/* High level function to send custom lossy packets.
849 * 850 *
@@ -861,7 +862,8 @@ int send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uin
861 * return 0 on success. 862 * return 0 on success.
862 */ 863 */
863int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, 864int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
864 int (*packet_handler_callback)(Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object), void *object); 865 int (*packet_handler_callback)(Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object),
866 void *object);
865 867
866/* High level function to send custom lossless packets. 868/* High level function to send custom lossless packets.
867 * 869 *
diff --git a/toxcore/group.c b/toxcore/group.c
index 27ad6261..de8c4558 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 = 0; i < sizeof(uint64_t); ++i) {
202 break; 198 cmp1 = (cmp1 << 8) + (uint64_t)pk1[i];
199 cmp2 = (cmp2 << 8) + (uint64_t)pk2[i];
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;
@@ -437,6 +434,30 @@ static int addpeer(Group_Chats *g_c, int groupnumber, const uint8_t *real_pk, co
437 return (g->numpeers - 1); 434 return (g->numpeers - 1);
438} 435}
439 436
437static int remove_close_conn(Group_Chats *g_c, int groupnumber, int friendcon_id)
438{
439 Group_c *g = get_group_c(g_c, groupnumber);
440
441 if (!g)
442 return -1;
443
444 uint32_t i;
445
446 for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
447 if (g->close[i].type == GROUPCHAT_CLOSE_NONE)
448 continue;
449
450 if (g->close[i].number == friendcon_id) {
451 g->close[i].type = GROUPCHAT_CLOSE_NONE;
452 kill_friend_connection(g_c->fr_c, friendcon_id);
453 return 0;
454 }
455 }
456
457 return -1;
458}
459
460
440/* 461/*
441 * Delete a peer from the group chat. 462 * Delete a peer from the group chat.
442 * 463 *
@@ -460,6 +481,12 @@ static int delpeer(Group_Chats *g_c, int groupnumber, int peer_index)
460 } 481 }
461 } 482 }
462 483
484 int friendcon_id = getfriend_conn_id_pk(g_c->fr_c, g->group[peer_index].real_pk);
485
486 if (friendcon_id != -1) {
487 remove_close_conn(g_c, groupnumber, friendcon_id);
488 }
489
463 Group_Peer *temp; 490 Group_Peer *temp;
464 --g->numpeers; 491 --g->numpeers;
465 492
@@ -536,29 +563,6 @@ static int settitle(Group_Chats *g_c, int groupnumber, int peer_index, const uin
536 return 0; 563 return 0;
537} 564}
538 565
539static int remove_close_conn(Group_Chats *g_c, int groupnumber, int friendcon_id)
540{
541 Group_c *g = get_group_c(g_c, groupnumber);
542
543 if (!g)
544 return -1;
545
546 uint32_t i;
547
548 for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
549 if (g->close[i].type == GROUPCHAT_CLOSE_NONE)
550 continue;
551
552 if (g->close[i].number == friendcon_id) {
553 g->close[i].type = GROUPCHAT_CLOSE_NONE;
554 kill_friend_connection(g_c->fr_c, friendcon_id);
555 return 0;
556 }
557 }
558
559 return -1;
560}
561
562static void set_conns_type_close(Group_Chats *g_c, int groupnumber, int friendcon_id, uint8_t type) 566static void set_conns_type_close(Group_Chats *g_c, int groupnumber, int friendcon_id, uint8_t type)
563{ 567{
564 Group_c *g = get_group_c(g_c, groupnumber); 568 Group_c *g = get_group_c(g_c, groupnumber);
diff --git a/toxcore/tox.c b/toxcore/tox.c
index b85a47b2..66587011 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -490,7 +490,8 @@ void tox_get_keys(Tox *tox, uint8_t *public_key, uint8_t *secret_key)
490 * return 0 on success. 490 * return 0 on success.
491 */ 491 */
492int tox_lossy_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte, 492int tox_lossy_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
493 int (*packet_handler_callback)(Messenger *tox, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object), void *object) 493 int (*packet_handler_callback)(Messenger *tox, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object),
494 void *object)
494{ 495{
495 Messenger *m = tox; 496 Messenger *m = tox;
496 497
@@ -527,7 +528,8 @@ int tox_send_lossy_packet(const Tox *tox, int32_t friendnumber, const uint8_t *d
527 * return 0 on success. 528 * return 0 on success.
528 */ 529 */
529int tox_lossless_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte, 530int tox_lossless_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
530 int (*packet_handler_callback)(Messenger *tox, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object), void *object) 531 int (*packet_handler_callback)(Messenger *tox, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object),
532 void *object)
531{ 533{
532 Messenger *m = tox; 534 Messenger *m = tox;
533 535
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 2caa77e9..b956a38f 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -385,7 +385,8 @@ void tox_get_keys(Tox *tox, uint8_t *public_key, uint8_t *secret_key);
385 * return 0 on success. 385 * return 0 on success.
386 */ 386 */
387int tox_lossy_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte, 387int tox_lossy_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
388 int (*packet_handler_callback)(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object), void *object); 388 int (*packet_handler_callback)(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object),
389 void *object);
389 390
390/* Function to send custom lossy packets. 391/* Function to send custom lossy packets.
391 * First byte of data must be in the range: 200-254. 392 * First byte of data must be in the range: 200-254.
@@ -405,7 +406,8 @@ int tox_send_lossy_packet(const Tox *tox, int32_t friendnumber, const uint8_t *d
405 * return 0 on success. 406 * return 0 on success.
406 */ 407 */
407int tox_lossless_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte, 408int tox_lossless_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
408 int (*packet_handler_callback)(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object), void *object); 409 int (*packet_handler_callback)(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t len, void *object),
410 void *object);
409 411
410/* Function to send custom lossless packets. 412/* Function to send custom lossless packets.
411 * First byte of data must be in the range: 160-191. 413 * First byte of data must be in the range: 160-191.