summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/tox_test.c152
-rw-r--r--toxav/codec.c5
-rw-r--r--toxav/rtp.c2
-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
8 files changed, 212 insertions, 55 deletions
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c
index c78bc1f5..b99c5c57 100644
--- a/auto_tests/tox_test.c
+++ b/auto_tests/tox_test.c
@@ -70,8 +70,8 @@ void print_typingchange(Tox *m, int friendnumber, uint8_t typing, void *userdata
70 70
71uint32_t custom_packet; 71uint32_t custom_packet;
72 72
73int handle_custom_packet(Tox* m, uint32_t friend_num, const uint8_t *data, 73int handle_custom_packet(Tox *m, uint32_t friend_num, const uint8_t *data,
74 uint32_t len, void* object) 74 uint32_t len, void *object)
75{ 75{
76 uint8_t number = *((uint32_t *)object); 76 uint8_t number = *((uint32_t *)object);
77 77
@@ -423,11 +423,154 @@ loop_top:
423 c_sleep(50); 423 c_sleep(50);
424 } 424 }
425 425
426 printf("test_many_clients succeeded, took %llu seconds\n", time(NULL) - cur_time);
427
428 for (i = 0; i < NUM_TOXES; ++i) { 426 for (i = 0; i < NUM_TOXES; ++i) {
429 tox_kill(toxes[i]); 427 tox_kill(toxes[i]);
430 } 428 }
429
430 printf("test_many_clients succeeded, took %llu seconds\n", time(NULL) - cur_time);
431}
432END_TEST
433
434#define NUM_GROUP_TOX 32
435
436void g_accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
437{
438 if (*((uint32_t *)userdata) != 234212)
439 return;
440
441 if (length == 7 && memcmp("Gentoo", data, 7) == 0) {
442 tox_add_friend_norequest(m, public_key);
443 }
444}
445
446static Tox *invite_tox;
447static unsigned int invite_counter;
448
449void print_group_invite_callback(Tox *tox, int32_t friendnumber, uint8_t type, const uint8_t *data, uint16_t length,
450 void *userdata)
451{
452 if (*((uint32_t *)userdata) != 234212)
453 return;
454
455 int g_num;
456
457 if ((g_num = tox_join_groupchat(tox, friendnumber, data, length)) == -1)
458 return;
459
460 ck_assert_msg(g_num == 0, "Group number was not 0");
461 ck_assert_msg(tox_join_groupchat(tox, friendnumber, data, length) == -1,
462 "Joining groupchat twice should be impossible.");
463
464 invite_tox = tox;
465 invite_counter = 4;
466}
467
468static unsigned int num_recv;
469
470void print_group_message(Tox *tox, int groupnumber, int peernumber, const uint8_t *message, uint16_t length,
471 void *userdata)
472{
473 if (*((uint32_t *)userdata) != 234212)
474 return;
475
476 if (length != length)
477 if (memcmp(message, "Install Gentoo", sizeof("Install Gentoo") - 1) != 0)
478 return;
479
480 ++num_recv;
481}
482
483START_TEST(test_many_group)
484{
485 long long unsigned int cur_time = time(NULL);
486 Tox *toxes[NUM_GROUP_TOX];
487 unsigned int i, j;
488
489 uint32_t to_comp = 234212;
490
491 for (i = 0; i < NUM_GROUP_TOX; ++i) {
492 toxes[i] = tox_new(0);
493 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i);
494 tox_callback_friend_request(toxes[i], &g_accept_friend_request, &to_comp);
495 tox_callback_group_invite(toxes[i], &print_group_invite_callback, &to_comp);
496 }
497
498 uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
499 tox_get_address(toxes[NUM_GROUP_TOX - 1], address);
500
501 for (i = 0; i < NUM_GROUP_TOX; ++i) {
502 ck_assert_msg(tox_add_friend(toxes[i], address, (uint8_t *)"Gentoo", 7) == 0, "Failed to add friend");
503
504 tox_get_address(toxes[i], address);
505 }
506
507 while (1) {
508 for (i = 0; i < NUM_GROUP_TOX; ++i) {
509 if (tox_get_friend_connection_status(toxes[i], 0) != 1) {
510 break;
511 }
512 }
513
514 if (i == NUM_GROUP_TOX)
515 break;
516
517 for (i = 0; i < NUM_GROUP_TOX; ++i) {
518 tox_do(toxes[i]);
519 }
520
521 c_sleep(50);
522 }
523
524 printf("friends connected, took %llu seconds\n", time(NULL) - cur_time);
525
526 ck_assert_msg(tox_add_groupchat(toxes[0]) != -1, "Failed to create group");
527 ck_assert_msg(tox_invite_friend(toxes[0], 0, 0) == 0, "Failed to invite friend");
528 invite_counter = ~0;
529
530 unsigned int done = ~0;
531 done -= 5;
532
533 while (1) {
534 for (i = 0; i < NUM_GROUP_TOX; ++i) {
535 tox_do(toxes[i]);
536 }
537
538 if (!invite_counter) {
539 ck_assert_msg(tox_invite_friend(invite_tox, 0, 0) == 0, "Failed to invite friend");
540 }
541
542 if (done == invite_counter) {
543 break;
544 }
545
546 --invite_counter;
547 c_sleep(50);
548 }
549
550 for (i = 0; i < NUM_GROUP_TOX; ++i) {
551 ck_assert_msg(tox_group_number_peers(toxes[i], 0) == NUM_GROUP_TOX, "Bad number of group peers.");
552 }
553
554 printf("group connected\n");
555
556 for (i = 0; i < NUM_GROUP_TOX; ++i) {
557 tox_callback_group_message(toxes[i], &print_group_message, &to_comp);
558 }
559
560 ck_assert_msg(tox_group_message_send(toxes[rand() % NUM_GROUP_TOX], 0, "Install Gentoo",
561 sizeof("Install Gentoo") - 1) == 0, "Failed to send group message.");
562 num_recv = 0;
563
564 for (j = 0; j < 20; ++j) {
565 for (i = 0; i < NUM_GROUP_TOX; ++i) {
566 tox_do(toxes[i]);
567 }
568
569 c_sleep(50);
570 }
571
572 ck_assert_msg(num_recv == NUM_GROUP_TOX, "Failed to recv group messages.");
573 printf("test_many_group succeeded, took %llu seconds\n", time(NULL) - cur_time);
431} 574}
432END_TEST 575END_TEST
433 576
@@ -437,6 +580,7 @@ Suite *tox_suite(void)
437 580
438 DEFTESTCASE_SLOW(few_clients, 50); 581 DEFTESTCASE_SLOW(few_clients, 50);
439 DEFTESTCASE_SLOW(many_clients, 150); 582 DEFTESTCASE_SLOW(many_clients, 150);
583 DEFTESTCASE_SLOW(many_group, 100);
440 return s; 584 return s;
441} 585}
442 586
diff --git a/toxav/codec.c b/toxav/codec.c
index eb770f39..baaad47a 100644
--- a/toxav/codec.c
+++ b/toxav/codec.c
@@ -379,9 +379,8 @@ void cs_do(CSSession *cs)
379 pthread_mutex_unlock(cs->queue_mutex); 379 pthread_mutex_unlock(cs->queue_mutex);
380 380
381 381
382 uint16_t fsize = (cs->audio_decoder_channels * 382 uint16_t fsize = ((cs->audio_decoder_sample_rate * cs->audio_decoder_frame_duration) / 1000);
383 (cs->audio_decoder_sample_rate * cs->audio_decoder_frame_duration) / 1000); 383 int16_t tmp[fsize * cs->audio_decoder_channels];
384 int16_t tmp[fsize];
385 384
386 rc = opus_decode(cs->audio_decoder, p->data, p->size, tmp, fsize, (p->size == 0)); 385 rc = opus_decode(cs->audio_decoder, p->data, p->size, tmp, fsize, (p->size == 0));
387 free(p); 386 free(p);
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 3696fd0a..2af89ebf 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -341,7 +341,7 @@ RTPMessage *msg_parse ( const uint8_t *data, int length )
341/** 341/**
342 * Callback for networking core. 342 * Callback for networking core.
343 */ 343 */
344int rtp_handle_packet ( Messenger* m, int32_t friendnumber, const uint8_t *data, uint32_t length, void *object ) 344int rtp_handle_packet ( Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length, void *object )
345{ 345{
346 RTPSession *session = object; 346 RTPSession *session = object;
347 RTPMessage *msg; 347 RTPMessage *msg;
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.