summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c284
-rw-r--r--toxcore/Messenger.h190
-rw-r--r--toxcore/friend_requests.c12
-rw-r--r--toxcore/friend_requests.h7
-rw-r--r--toxcore/group_chats.c4
-rw-r--r--toxcore/network.c71
-rw-r--r--toxcore/network.h7
-rw-r--r--toxcore/tox.c172
-rw-r--r--toxcore/tox.h261
9 files changed, 578 insertions, 430 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 6e476e6e..94e8d0b4 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -34,16 +34,16 @@
34#define MIN(a,b) (((a)<(b))?(a):(b)) 34#define MIN(a,b) (((a)<(b))?(a):(b))
35 35
36 36
37static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); 37static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status);
38static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); 38static int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
39 39
40// friend_not_valid determines if the friendnumber passed is valid in the Messenger object 40// friend_not_valid determines if the friendnumber passed is valid in the Messenger object
41static uint8_t friend_not_valid(Messenger *m, int friendnumber) 41static uint8_t friend_not_valid(Messenger *m, int32_t friendnumber)
42{ 42{
43 return (unsigned int)friendnumber >= m->numfriends; 43 return (unsigned int)friendnumber >= m->numfriends;
44} 44}
45 45
46static int add_online_friend(Messenger *m, int friendnumber) 46static int add_online_friend(Messenger *m, int32_t friendnumber)
47{ 47{
48 if (friend_not_valid(m, friendnumber)) 48 if (friend_not_valid(m, friendnumber))
49 return -1; 49 return -1;
@@ -74,7 +74,7 @@ static int add_online_friend(Messenger *m, int friendnumber)
74} 74}
75 75
76 76
77static int remove_online_friend(Messenger *m, int friendnumber) 77static int remove_online_friend(Messenger *m, int32_t friendnumber)
78{ 78{
79 uint32_t i; 79 uint32_t i;
80 Online_Friend *temp; 80 Online_Friend *temp;
@@ -132,7 +132,7 @@ int realloc_friendlist(Messenger *m, uint32_t num)
132/* return the friend id associated to that public key. 132/* return the friend id associated to that public key.
133 * return -1 if no such friend. 133 * return -1 if no such friend.
134 */ 134 */
135int getfriend_id(Messenger *m, uint8_t *client_id) 135int32_t getfriend_id(Messenger *m, uint8_t *client_id)
136{ 136{
137 uint32_t i; 137 uint32_t i;
138 138
@@ -151,13 +151,13 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
151 * return 0 if success. 151 * return 0 if success.
152 * return -1 if failure. 152 * return -1 if failure.
153 */ 153 */
154int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) 154int getclient_id(Messenger *m, int32_t friendnumber, uint8_t *client_id)
155{ 155{
156 if (friend_not_valid(m, friend_id)) 156 if (friend_not_valid(m, friendnumber))
157 return -1; 157 return -1;
158 158
159 if (m->friendlist[friend_id].status > 0) { 159 if (m->friendlist[friendnumber].status > 0) {
160 memcpy(client_id, m->friendlist[friend_id].client_id, CLIENT_ID_SIZE); 160 memcpy(client_id, m->friendlist[friendnumber].client_id, CLIENT_ID_SIZE);
161 return 0; 161 return 0;
162 } 162 }
163 163
@@ -210,7 +210,7 @@ void getaddress(Messenger *m, uint8_t *address)
210 * (the nospam for that friend was set to the new one). 210 * (the nospam for that friend was set to the new one).
211 * return FAERR_NOMEM if increasing the friend list size fails. 211 * return FAERR_NOMEM if increasing the friend list size fails.
212 */ 212 */
213int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) 213int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
214{ 214{
215 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES 215 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
216 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES 216 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
@@ -231,7 +231,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
231 if (id_equal(client_id, m->net_crypto->self_public_key)) 231 if (id_equal(client_id, m->net_crypto->self_public_key))
232 return FAERR_OWNKEY; 232 return FAERR_OWNKEY;
233 233
234 int friend_id = getfriend_id(m, client_id); 234 int32_t friend_id = getfriend_id(m, client_id);
235 235
236 if (friend_id != -1) { 236 if (friend_id != -1) {
237 uint32_t nospam; 237 uint32_t nospam;
@@ -250,7 +250,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
250 250
251 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); 251 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
252 252
253 int onion_friendnum = onion_addfriend(m->onion_c, client_id); 253 int32_t onion_friendnum = onion_addfriend(m->onion_c, client_id);
254 254
255 if (onion_friendnum == -1) 255 if (onion_friendnum == -1)
256 return FAERR_UNKNOWN; 256 return FAERR_UNKNOWN;
@@ -285,7 +285,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
285 return FAERR_UNKNOWN; 285 return FAERR_UNKNOWN;
286} 286}
287 287
288int m_addfriend_norequest(Messenger *m, uint8_t *client_id) 288int32_t m_addfriend_norequest(Messenger *m, uint8_t *client_id)
289{ 289{
290 if (getfriend_id(m, client_id) != -1) 290 if (getfriend_id(m, client_id) != -1)
291 return -1; 291 return -1;
@@ -299,7 +299,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
299 299
300 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); 300 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
301 301
302 int onion_friendnum = onion_addfriend(m->onion_c, client_id); 302 int32_t onion_friendnum = onion_addfriend(m->onion_c, client_id);
303 303
304 if (onion_friendnum == -1) 304 if (onion_friendnum == -1)
305 return FAERR_UNKNOWN; 305 return FAERR_UNKNOWN;
@@ -335,7 +335,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
335 * return 0 if success. 335 * return 0 if success.
336 * return -1 if failure. 336 * return -1 if failure.
337 */ 337 */
338int m_delfriend(Messenger *m, int friendnumber) 338int m_delfriend(Messenger *m, int32_t friendnumber)
339{ 339{
340 if (friend_not_valid(m, friendnumber)) 340 if (friend_not_valid(m, friendnumber))
341 return -1; 341 return -1;
@@ -362,7 +362,7 @@ int m_delfriend(Messenger *m, int friendnumber)
362 return 0; 362 return 0;
363} 363}
364 364
365int m_get_friend_connectionstatus(Messenger *m, int friendnumber) 365int m_get_friend_connectionstatus(Messenger *m, int32_t friendnumber)
366{ 366{
367 if (friend_not_valid(m, friendnumber)) 367 if (friend_not_valid(m, friendnumber))
368 return -1; 368 return -1;
@@ -370,7 +370,7 @@ int m_get_friend_connectionstatus(Messenger *m, int friendnumber)
370 return m->friendlist[friendnumber].status == FRIEND_ONLINE; 370 return m->friendlist[friendnumber].status == FRIEND_ONLINE;
371} 371}
372 372
373int m_friend_exists(Messenger *m, int friendnumber) 373int m_friend_exists(Messenger *m, int32_t friendnumber)
374{ 374{
375 if (friend_not_valid(m, friendnumber)) 375 if (friend_not_valid(m, friendnumber))
376 return 0; 376 return 0;
@@ -383,7 +383,7 @@ int m_friend_exists(Messenger *m, int friendnumber)
383 * return the message id if packet was successfully put into the send queue. 383 * return the message id if packet was successfully put into the send queue.
384 * return 0 if it was not. 384 * return 0 if it was not.
385 */ 385 */
386uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) 386uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uint32_t length)
387{ 387{
388 if (friend_not_valid(m, friendnumber)) 388 if (friend_not_valid(m, friendnumber))
389 return 0; 389 return 0;
@@ -400,7 +400,7 @@ uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_
400 return 0; 400 return 0;
401} 401}
402 402
403uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) 403uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length)
404{ 404{
405 if (length >= (MAX_DATA_SIZE - sizeof(theid))) 405 if (length >= (MAX_DATA_SIZE - sizeof(theid)))
406 return 0; 406 return 0;
@@ -417,7 +417,7 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui
417 * return the message id if packet was successfully put into the send queue. 417 * return the message id if packet was successfully put into the send queue.
418 * return 0 if it was not. 418 * return 0 if it was not.
419 */ 419 */
420uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) 420uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint32_t length)
421{ 421{
422 if (friend_not_valid(m, friendnumber)) 422 if (friend_not_valid(m, friendnumber))
423 return 0; 423 return 0;
@@ -434,7 +434,7 @@ uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t
434 return 0; 434 return 0;
435} 435}
436 436
437uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length) 437uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length)
438{ 438{
439 if (length >= (MAX_DATA_SIZE - sizeof(theid))) 439 if (length >= (MAX_DATA_SIZE - sizeof(theid)))
440 return 0; 440 return 0;
@@ -449,7 +449,7 @@ uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uin
449/* Send a name packet to friendnumber. 449/* Send a name packet to friendnumber.
450 * length is the length with the NULL terminator. 450 * length is the length with the NULL terminator.
451 */ 451 */
452static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) 452static int m_sendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length)
453{ 453{
454 if (length > MAX_NAME_LENGTH || length == 0) 454 if (length > MAX_NAME_LENGTH || length == 0)
455 return 0; 455 return 0;
@@ -462,7 +462,7 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
462 * return 0 if success. 462 * return 0 if success.
463 * return -1 if failure. 463 * return -1 if failure.
464 */ 464 */
465int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) 465int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length)
466{ 466{
467 if (friend_not_valid(m, friendnumber)) 467 if (friend_not_valid(m, friendnumber))
468 return -1; 468 return -1;
@@ -507,18 +507,15 @@ int setname(Messenger *m, uint8_t *name, uint16_t length)
507 * 507 *
508 * return the length of the name. 508 * return the length of the name.
509 */ 509 */
510uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) 510uint16_t getself_name(Messenger *m, uint8_t *name)
511{ 511{
512 uint16_t len; 512 if (name == NULL) {
513
514 if (name == NULL || nlen == 0) {
515 return 0; 513 return 0;
516 } 514 }
517 515
518 len = MIN(nlen, m->name_length); 516 memcpy(name, m->name, m->name_length);
519 memcpy(name, m->name, len);
520 517
521 return len; 518 return m->name_length;
522} 519}
523 520
524/* Get name of friendnumber and put it in name. 521/* Get name of friendnumber and put it in name.
@@ -527,7 +524,7 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
527 * return length of name if success. 524 * return length of name if success.
528 * return -1 if failure. 525 * return -1 if failure.
529 */ 526 */
530int getname(Messenger *m, int friendnumber, uint8_t *name) 527int getname(Messenger *m, int32_t friendnumber, uint8_t *name)
531{ 528{
532 if (friend_not_valid(m, friendnumber)) 529 if (friend_not_valid(m, friendnumber))
533 return -1; 530 return -1;
@@ -536,6 +533,19 @@ int getname(Messenger *m, int friendnumber, uint8_t *name)
536 return m->friendlist[friendnumber].name_length; 533 return m->friendlist[friendnumber].name_length;
537} 534}
538 535
536int m_get_name_size(Messenger *m, int32_t friendnumber)
537{
538 if (friend_not_valid(m, friendnumber))
539 return -1;
540
541 return m->friendlist[friendnumber].name_length;
542}
543
544int m_get_self_name_size(Messenger *m)
545{
546 return m->name_length;
547}
548
539int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length) 549int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length)
540{ 550{
541 if (length > MAX_STATUSMESSAGE_LENGTH) 551 if (length > MAX_STATUSMESSAGE_LENGTH)
@@ -552,7 +562,7 @@ int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length)
552 return 0; 562 return 0;
553} 563}
554 564
555int m_set_userstatus(Messenger *m, USERSTATUS status) 565int m_set_userstatus(Messenger *m, uint8_t status)
556{ 566{
557 if (status >= USERSTATUS_INVALID) { 567 if (status >= USERSTATUS_INVALID) {
558 return -1; 568 return -1;
@@ -570,7 +580,7 @@ int m_set_userstatus(Messenger *m, USERSTATUS status)
570/* return the size of friendnumber's user status. 580/* return the size of friendnumber's user status.
571 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH. 581 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
572 */ 582 */
573int m_get_statusmessage_size(Messenger *m, int friendnumber) 583int m_get_statusmessage_size(Messenger *m, int32_t friendnumber)
574{ 584{
575 if (friend_not_valid(m, friendnumber)) 585 if (friend_not_valid(m, friendnumber))
576 return -1; 586 return -1;
@@ -581,7 +591,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber)
581/* Copy the user status of friendnumber into buf, truncating if needed to maxlen 591/* Copy the user status of friendnumber into buf, truncating if needed to maxlen
582 * bytes, use m_get_statusmessage_size to find out how much you need to allocate. 592 * bytes, use m_get_statusmessage_size to find out how much you need to allocate.
583 */ 593 */
584int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) 594int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
585{ 595{
586 if (friend_not_valid(m, friendnumber)) 596 if (friend_not_valid(m, friendnumber))
587 return -1; 597 return -1;
@@ -591,6 +601,14 @@ int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t
591 return MIN(maxlen, m->friendlist[friendnumber].statusmessage_length); 601 return MIN(maxlen, m->friendlist[friendnumber].statusmessage_length);
592} 602}
593 603
604/* return the size of friendnumber's user status.
605 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
606 */
607int m_get_self_statusmessage_size(Messenger *m)
608{
609 return m->statusmessage_length;
610}
611
594int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen) 612int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
595{ 613{
596 memset(buf, 0, maxlen); 614 memset(buf, 0, maxlen);
@@ -598,12 +616,12 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
598 return MIN(maxlen, m->statusmessage_length); 616 return MIN(maxlen, m->statusmessage_length);
599} 617}
600 618
601USERSTATUS m_get_userstatus(Messenger *m, int friendnumber) 619uint8_t m_get_userstatus(Messenger *m, int32_t friendnumber)
602{ 620{
603 if (friend_not_valid(m, friendnumber)) 621 if (friend_not_valid(m, friendnumber))
604 return USERSTATUS_INVALID; 622 return USERSTATUS_INVALID;
605 623
606 USERSTATUS status = m->friendlist[friendnumber].userstatus; 624 uint8_t status = m->friendlist[friendnumber].userstatus;
607 625
608 if (status >= USERSTATUS_INVALID) { 626 if (status >= USERSTATUS_INVALID) {
609 status = USERSTATUS_NONE; 627 status = USERSTATUS_NONE;
@@ -612,12 +630,12 @@ USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
612 return status; 630 return status;
613} 631}
614 632
615USERSTATUS m_get_self_userstatus(Messenger *m) 633uint8_t m_get_self_userstatus(Messenger *m)
616{ 634{
617 return m->userstatus; 635 return m->userstatus;
618} 636}
619 637
620uint64_t m_get_last_online(Messenger *m, int friendnumber) 638uint64_t m_get_last_online(Messenger *m, int32_t friendnumber)
621{ 639{
622 if (friend_not_valid(m, friendnumber)) 640 if (friend_not_valid(m, friendnumber))
623 return -1; 641 return -1;
@@ -625,7 +643,8 @@ uint64_t m_get_last_online(Messenger *m, int friendnumber)
625 return m->friendlist[friendnumber].ping_lastrecv; 643 return m->friendlist[friendnumber].ping_lastrecv;
626} 644}
627 645
628int m_set_usertyping(Messenger *m, int friendnumber, uint8_t is_typing) 646int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing)
647
629{ 648{
630 if (is_typing != 0 && is_typing != 1) { 649 if (is_typing != 0 && is_typing != 1) {
631 return -1; 650 return -1;
@@ -640,7 +659,7 @@ int m_set_usertyping(Messenger *m, int friendnumber, uint8_t is_typing)
640 return 0; 659 return 0;
641} 660}
642 661
643int m_get_istyping(Messenger *m, int friendnumber) 662int m_get_istyping(Messenger *m, int32_t friendnumber)
644{ 663{
645 if (friend_not_valid(m, friendnumber)) 664 if (friend_not_valid(m, friendnumber))
646 return -1; 665 return -1;
@@ -648,24 +667,23 @@ int m_get_istyping(Messenger *m, int friendnumber)
648 return m->friendlist[friendnumber].is_typing; 667 return m->friendlist[friendnumber].is_typing;
649} 668}
650 669
651static int send_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) 670static int send_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length)
652{ 671{
653 return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length); 672 return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length);
654} 673}
655 674
656static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status) 675static int send_userstatus(Messenger *m, int32_t friendnumber, uint8_t status)
657{ 676{
658 uint8_t stat = status; 677 return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &status, sizeof(status));
659 return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat));
660} 678}
661 679
662static int send_user_istyping(Messenger *m, int friendnumber, uint8_t is_typing) 680static int send_user_istyping(Messenger *m, int32_t friendnumber, uint8_t is_typing)
663{ 681{
664 uint8_t typing = is_typing; 682 uint8_t typing = is_typing;
665 return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing)); 683 return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing));
666} 684}
667 685
668static int send_ping(Messenger *m, int friendnumber) 686static int send_ping(Messenger *m, int32_t friendnumber)
669{ 687{
670 int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0); 688 int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0);
671 689
@@ -675,12 +693,12 @@ static int send_ping(Messenger *m, int friendnumber)
675 return ret; 693 return ret;
676} 694}
677 695
678static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) 696static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length)
679{ 697{
680 if (friend_not_valid(m, friendnumber)) 698 if (friend_not_valid(m, friendnumber))
681 return -1; 699 return -1;
682 700
683 uint8_t *newstatus = calloc(length, 1); 701 uint8_t *newstatus = calloc(length + 1, 1);
684 memcpy(newstatus, status, length); 702 memcpy(newstatus, status, length);
685 free(m->friendlist[friendnumber].statusmessage); 703 free(m->friendlist[friendnumber].statusmessage);
686 m->friendlist[friendnumber].statusmessage = newstatus; 704 m->friendlist[friendnumber].statusmessage = newstatus;
@@ -688,18 +706,18 @@ static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *sta
688 return 0; 706 return 0;
689} 707}
690 708
691static void set_friend_userstatus(Messenger *m, int friendnumber, USERSTATUS status) 709static void set_friend_userstatus(Messenger *m, int32_t friendnumber, uint8_t status)
692{ 710{
693 m->friendlist[friendnumber].userstatus = status; 711 m->friendlist[friendnumber].userstatus = status;
694} 712}
695 713
696static void set_friend_typing(Messenger *m, int friendnumber, uint8_t is_typing) 714static void set_friend_typing(Messenger *m, int32_t friendnumber, uint8_t is_typing)
697{ 715{
698 m->friendlist[friendnumber].is_typing = is_typing; 716 m->friendlist[friendnumber].is_typing = is_typing;
699} 717}
700 718
701/* Sets whether we send read receipts for friendnumber. */ 719/* Sets whether we send read receipts for friendnumber. */
702void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno) 720void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno)
703{ 721{
704 if (yesno != 0 && yesno != 1) 722 if (yesno != 0 && yesno != 1)
705 return; 723 return;
@@ -712,72 +730,75 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
712 730
713/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ 731/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */
714/* Set the function that will be executed when a friend request is received. */ 732/* Set the function that will be executed when a friend request is received. */
715void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) 733void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *),
734 void *userdata)
716{ 735{
717 callback_friendrequest(&(m->fr), function, userdata); 736 void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *) = function;
737 callback_friendrequest(&(m->fr), handle_friendrequest, m, userdata);
718} 738}
719 739
720/* Set the function that will be executed when a message from a friend is received. */ 740/* Set the function that will be executed when a message from a friend is received. */
721void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 741void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
722 void *userdata) 742 void *userdata)
723{ 743{
724 m->friend_message = function; 744 m->friend_message = function;
725 m->friend_message_userdata = userdata; 745 m->friend_message_userdata = userdata;
726} 746}
727 747
728void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata) 748void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
749 void *userdata)
729{ 750{
730 m->friend_action = function; 751 m->friend_action = function;
731 m->friend_action_userdata = userdata; 752 m->friend_action_userdata = userdata;
732} 753}
733 754
734void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 755void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
735 void *userdata) 756 void *userdata)
736{ 757{
737 m->friend_namechange = function; 758 m->friend_namechange = function;
738 m->friend_namechange_userdata = userdata; 759 m->friend_namechange_userdata = userdata;
739} 760}
740 761
741void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 762void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
742 void *userdata) 763 void *userdata)
743{ 764{
744 m->friend_statusmessagechange = function; 765 m->friend_statusmessagechange = function;
745 m->friend_statuschange_userdata = userdata; 766 m->friend_statuschange_userdata = userdata;
746} 767}
747 768
748void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata) 769void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), void *userdata)
749{ 770{
750 m->friend_userstatuschange = function; 771 m->friend_userstatuschange = function;
751 m->friend_userstatuschange_userdata = userdata; 772 m->friend_userstatuschange_userdata = userdata;
752} 773}
753 774
754void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int, int, void *), void *userdata) 775void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int32_t, int, void *), void *userdata)
755{ 776{
756 m->friend_typingchange = function; 777 m->friend_typingchange = function;
757 m->friend_typingchange_userdata = userdata; 778 m->friend_typingchange_userdata = userdata;
758} 779}
759 780
760void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata) 781void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int32_t, uint32_t, void *), void *userdata)
761{ 782{
762 m->read_receipt = function; 783 m->read_receipt = function;
763 m->read_receipt_userdata = userdata; 784 m->read_receipt_userdata = userdata;
764} 785}
765 786
766void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata) 787void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), void *userdata)
767{ 788{
768 m->friend_connectionstatuschange = function; 789 m->friend_connectionstatuschange = function;
769 m->friend_connectionstatuschange_userdata = userdata; 790 m->friend_connectionstatuschange_userdata = userdata;
770} 791}
771 792
772void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), 793void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *),
773 void *userdata) 794 void *userdata)
774{ 795{
775 m->friend_connectionstatuschange_internal = function; 796 m->friend_connectionstatuschange_internal = function;
776 m->friend_connectionstatuschange_internal_userdata = userdata; 797 m->friend_connectionstatuschange_internal_userdata = userdata;
777} 798}
778 799
779static void break_files(Messenger *m, int friendnumber); 800static void break_files(Messenger *m, int32_t friendnumber);
780static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) 801static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status)
781{ 802{
782 if (status == NOFRIEND) 803 if (status == NOFRIEND)
783 return; 804 return;
@@ -804,13 +825,13 @@ static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_
804 } 825 }
805} 826}
806 827
807void set_friend_status(Messenger *m, int friendnumber, uint8_t status) 828void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status)
808{ 829{
809 check_friend_connectionstatus(m, friendnumber, status); 830 check_friend_connectionstatus(m, friendnumber, status);
810 m->friendlist[friendnumber].status = status; 831 m->friendlist[friendnumber].status = status;
811} 832}
812 833
813int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) 834int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
814{ 835{
815 if (friend_not_valid(m, friendnumber)) 836 if (friend_not_valid(m, friendnumber))
816 return 0; 837 return 0;
@@ -850,7 +871,7 @@ static uint8_t groupnumber_not_valid(Messenger *m, int groupnumber)
850/* returns valid ip port of connected friend on success 871/* returns valid ip port of connected friend on success
851 * returns zeroed out IP_Port on failure 872 * returns zeroed out IP_Port on failure
852 */ 873 */
853IP_Port get_friend_ipport(Messenger *m, int friendnumber) 874IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber)
854{ 875{
855 IP_Port zero; 876 IP_Port zero;
856 memset(&zero, 0, sizeof(zero)); 877 memset(&zero, 0, sizeof(zero));
@@ -884,9 +905,9 @@ static int group_num(Messenger *m, uint8_t *group_public_key)
884 905
885/* Set the callback for group invites. 906/* Set the callback for group invites.
886 * 907 *
887 * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata) 908 * Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata)
888 */ 909 */
889void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata) 910void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, void *), void *userdata)
890{ 911{
891 m->group_invite = function; 912 m->group_invite = function;
892 m->group_invite_userdata = userdata; 913 m->group_invite_userdata = userdata;
@@ -946,10 +967,12 @@ static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *m
946 if (i == -1) 967 if (i == -1)
947 return; 968 return;
948 969
949 message[length - 1] = 0; /* Force NULL terminator */ 970 uint8_t message_terminated[length + 1];
971 memcpy(message_terminated, message, length);
972 message_terminated[length] = 0; /* Force NULL terminator */
950 973
951 if (m->group_message) 974 if (m->group_message)
952 (*m->group_message)(m, i, peer_number, message, length, m->group_message_userdata); 975 (*m->group_message)(m, i, peer_number, message_terminated, length, m->group_message_userdata);
953} 976}
954 977
955static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata) 978static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata)
@@ -960,10 +983,12 @@ static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *ac
960 if (i == -1) 983 if (i == -1)
961 return; 984 return;
962 985
963 action[length - 1] = 0; /* Force NULL terminator */ 986 uint8_t action_terminated[length + 1];
987 memcpy(action_terminated, action, length);
988 action_terminated[length] = 0; /* Force NULL terminator */
964 989
965 if (m->group_action) 990 if (m->group_action)
966 (*m->group_action)(m, i, peer_number, action, length, m->group_action_userdata); 991 (*m->group_action)(m, i, peer_number, action_terminated, length, m->group_action_userdata);
967} 992}
968 993
969static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata) 994static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata)
@@ -1089,7 +1114,7 @@ int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *nam
1089 1114
1090/* Store the fact that we invited a specific friend. 1115/* Store the fact that we invited a specific friend.
1091 */ 1116 */
1092static void group_store_friendinvite(Messenger *m, int friendnumber, int groupnumber) 1117static void group_store_friendinvite(Messenger *m, int32_t friendnumber, int groupnumber)
1093{ 1118{
1094 /* Add 1 to the groupchat number because 0 (default value in invited_groups) is a valid groupchat number */ 1119 /* Add 1 to the groupchat number because 0 (default value in invited_groups) is a valid groupchat number */
1095 m->friendlist[friendnumber].invited_groups[m->friendlist[friendnumber].invited_groups_num % MAX_INVITED_GROUPS] = 1120 m->friendlist[friendnumber].invited_groups[m->friendlist[friendnumber].invited_groups_num % MAX_INVITED_GROUPS] =
@@ -1100,7 +1125,7 @@ static void group_store_friendinvite(Messenger *m, int friendnumber, int groupnu
1100/* return 1 if that friend was invited to the group 1125/* return 1 if that friend was invited to the group
1101 * return 0 if the friend was not or error. 1126 * return 0 if the friend was not or error.
1102 */ 1127 */
1103static uint8_t group_invited(Messenger *m, int friendnumber, int groupnumber) 1128static uint8_t group_invited(Messenger *m, int32_t friendnumber, int groupnumber)
1104{ 1129{
1105 1130
1106 uint32_t i; 1131 uint32_t i;
@@ -1122,7 +1147,7 @@ static uint8_t group_invited(Messenger *m, int friendnumber, int groupnumber)
1122 * return 0 on success 1147 * return 0 on success
1123 * return -1 on failure 1148 * return -1 on failure
1124 */ 1149 */
1125int invite_friend(Messenger *m, int friendnumber, int groupnumber) 1150int invite_friend(Messenger *m, int32_t friendnumber, int groupnumber)
1126{ 1151{
1127 if (friend_not_valid(m, friendnumber) || (unsigned int)groupnumber >= m->numchats) 1152 if (friend_not_valid(m, friendnumber) || (unsigned int)groupnumber >= m->numchats)
1128 return -1; 1153 return -1;
@@ -1148,7 +1173,7 @@ int invite_friend(Messenger *m, int friendnumber, int groupnumber)
1148 * returns group number on success 1173 * returns group number on success
1149 * returns -1 on failure. 1174 * returns -1 on failure.
1150 */ 1175 */
1151int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key) 1176int join_groupchat(Messenger *m, int32_t friendnumber, uint8_t *friend_group_public_key)
1152{ 1177{
1153 if (friend_not_valid(m, friendnumber)) 1178 if (friend_not_valid(m, friendnumber))
1154 return -1; 1179 return -1;
@@ -1265,9 +1290,10 @@ static void do_allgroupchats(Messenger *m)
1265 1290
1266/* Set the callback for file send requests. 1291/* Set the callback for file send requests.
1267 * 1292 *
1268 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) 1293 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
1269 */ 1294 */
1270void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, 1295void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *,
1296 uint16_t,
1271 void *), void *userdata) 1297 void *), void *userdata)
1272{ 1298{
1273 m->file_sendrequest = function; 1299 m->file_sendrequest = function;
@@ -1276,10 +1302,10 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int,
1276 1302
1277/* Set the callback for file control requests. 1303/* Set the callback for file control requests.
1278 * 1304 *
1279 * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 1305 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
1280 * 1306 *
1281 */ 1307 */
1282void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, 1308void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *,
1283 uint16_t, 1309 uint16_t,
1284 void *), void *userdata) 1310 void *), void *userdata)
1285{ 1311{
@@ -1289,10 +1315,11 @@ void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uin
1289 1315
1290/* Set the callback for file data. 1316/* Set the callback for file data.
1291 * 1317 *
1292 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 1318 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
1293 * 1319 *
1294 */ 1320 */
1295void callback_file_data(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *), 1321void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length,
1322 void *),
1296 void *userdata) 1323 void *userdata)
1297{ 1324{
1298 m->file_filedata = function; 1325 m->file_filedata = function;
@@ -1306,7 +1333,7 @@ void callback_file_data(Messenger *m, void (*function)(Messenger *m, int, uint8_
1306 * return 1 on success 1333 * return 1 on success
1307 * return 0 on failure 1334 * return 0 on failure
1308 */ 1335 */
1309int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, 1336int file_sendrequest(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename,
1310 uint16_t filename_length) 1337 uint16_t filename_length)
1311{ 1338{
1312 if (friend_not_valid(m, friendnumber)) 1339 if (friend_not_valid(m, friendnumber))
@@ -1329,7 +1356,7 @@ int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_
1329 * return file number on success 1356 * return file number on success
1330 * return -1 on failure 1357 * return -1 on failure
1331 */ 1358 */
1332int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) 1359int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length)
1333{ 1360{
1334 if (friend_not_valid(m, friendnumber)) 1361 if (friend_not_valid(m, friendnumber))
1335 return -1; 1362 return -1;
@@ -1359,7 +1386,7 @@ int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *f
1359 * return 0 on success 1386 * return 0 on success
1360 * return -1 on failure 1387 * return -1 on failure
1361 */ 1388 */
1362int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, 1389int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
1363 uint8_t *data, uint16_t length) 1390 uint8_t *data, uint16_t length)
1364{ 1391{
1365 if (length > MAX_DATA_SIZE - 3) 1392 if (length > MAX_DATA_SIZE - 3)
@@ -1447,7 +1474,7 @@ int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t f
1447 * return 0 on success 1474 * return 0 on success
1448 * return -1 on failure 1475 * return -1 on failure
1449 */ 1476 */
1450int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) 1477int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length)
1451{ 1478{
1452 if (length > MAX_DATA_SIZE - 1) 1479 if (length > MAX_DATA_SIZE - 1)
1453 return -1; 1480 return -1;
@@ -1482,7 +1509,7 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data,
1482 * return number of bytes remaining to be sent/received on success 1509 * return number of bytes remaining to be sent/received on success
1483 * return 0 on failure 1510 * return 0 on failure
1484 */ 1511 */
1485uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t send_receive) 1512uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive)
1486{ 1513{
1487 if (friend_not_valid(m, friendnumber)) 1514 if (friend_not_valid(m, friendnumber))
1488 return 0; 1515 return 0;
@@ -1505,7 +1532,7 @@ uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber,
1505/* Run this when the friend disconnects. 1532/* Run this when the friend disconnects.
1506 * Sets all current file transfers to broken. 1533 * Sets all current file transfers to broken.
1507 */ 1534 */
1508static void break_files(Messenger *m, int friendnumber) 1535static void break_files(Messenger *m, int32_t friendnumber)
1509{ 1536{
1510 uint32_t i; 1537 uint32_t i;
1511 1538
@@ -1518,7 +1545,7 @@ static void break_files(Messenger *m, int friendnumber)
1518 } 1545 }
1519} 1546}
1520 1547
1521static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t receive_send, uint8_t filenumber, 1548static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber,
1522 uint8_t message_id, uint8_t *data, 1549 uint8_t message_id, uint8_t *data,
1523 uint16_t length) 1550 uint16_t length)
1524{ 1551{
@@ -1608,7 +1635,7 @@ static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t receive_se
1608 * 1635 *
1609 * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata) 1636 * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata)
1610 */ 1637 */
1611void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 1638void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
1612 void *userdata) 1639 void *userdata)
1613{ 1640{
1614 m->msi_packet = function; 1641 m->msi_packet = function;
@@ -1620,12 +1647,12 @@ void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uin
1620 * return 1 on success 1647 * return 1 on success
1621 * return 0 on failure 1648 * return 0 on failure
1622 */ 1649 */
1623int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length) 1650int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length)
1624{ 1651{
1625 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length); 1652 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length);
1626} 1653}
1627 1654
1628static int friendnum_from_ip_port(Messenger *m, IP_Port ip_port) 1655static int32_t friendnum_from_ip_port(Messenger *m, IP_Port ip_port)
1629{ 1656{
1630 uint32_t i; 1657 uint32_t i;
1631 1658
@@ -1640,7 +1667,7 @@ static int friendnum_from_ip_port(Messenger *m, IP_Port ip_port)
1640static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *packet, uint32_t length) 1667static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *packet, uint32_t length)
1641{ 1668{
1642 Messenger *m = object; 1669 Messenger *m = object;
1643 int friend_num = friendnum_from_ip_port(m, source); 1670 int32_t friend_num = friendnum_from_ip_port(m, source);
1644 1671
1645 if (friend_num == -1) 1672 if (friend_num == -1)
1646 return 1; 1673 return 1;
@@ -1653,7 +1680,7 @@ static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *pack
1653} 1680}
1654 1681
1655 1682
1656int custom_user_packet_registerhandler(Messenger *m, int friendnumber, uint8_t byte, packet_handler_callback cb, 1683int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb,
1657 void *object) 1684 void *object)
1658{ 1685{
1659 if (friend_not_valid(m, friendnumber)) 1686 if (friend_not_valid(m, friendnumber))
@@ -1668,7 +1695,7 @@ int custom_user_packet_registerhandler(Messenger *m, int friendnumber, uint8_t b
1668 return 0; 1695 return 0;
1669} 1696}
1670 1697
1671int send_custom_user_packet(Messenger *m, int friendnumber, uint8_t *data, uint32_t length) 1698int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length)
1672{ 1699{
1673 if (friend_not_valid(m, friendnumber)) 1700 if (friend_not_valid(m, friendnumber))
1674 return -1; 1701 return -1;
@@ -1905,13 +1932,15 @@ void do_friends(Messenger *m)
1905 break; 1932 break;
1906 1933
1907 /* Make sure the NULL terminator is present. */ 1934 /* Make sure the NULL terminator is present. */
1908 data[data_length - 1] = 0; 1935 uint8_t data_terminated[data_length + 1];
1936 memcpy(data_terminated, data, data_length);
1937 data_terminated[data_length] = 0;
1909 1938
1910 /* inform of namechange before we overwrite the old name */ 1939 /* inform of namechange before we overwrite the old name */
1911 if (m->friend_namechange) 1940 if (m->friend_namechange)
1912 m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); 1941 m->friend_namechange(m, i, data_terminated, data_length, m->friend_namechange_userdata);
1913 1942
1914 memcpy(m->friendlist[i].name, data, data_length); 1943 memcpy(m->friendlist[i].name, data_terminated, data_length + 1);
1915 m->friendlist[i].name_length = data_length; 1944 m->friendlist[i].name_length = data_length;
1916 1945
1917 break; 1946 break;
@@ -1921,13 +1950,16 @@ void do_friends(Messenger *m)
1921 if (data_length == 0 || data_length > MAX_STATUSMESSAGE_LENGTH) 1950 if (data_length == 0 || data_length > MAX_STATUSMESSAGE_LENGTH)
1922 break; 1951 break;
1923 1952
1924 data[data_length - 1] = 0; /* Make sure the NULL terminator is present. */ 1953 /* Make sure the NULL terminator is present. */
1954 uint8_t data_terminated[data_length + 1];
1955 memcpy(data_terminated, data, data_length);
1956 data_terminated[data_length] = 0;
1925 1957
1926 if (m->friend_statusmessagechange) 1958 if (m->friend_statusmessagechange)
1927 m->friend_statusmessagechange(m, i, data, data_length, 1959 m->friend_statusmessagechange(m, i, data_terminated, data_length,
1928 m->friend_statuschange_userdata); 1960 m->friend_statuschange_userdata);
1929 1961
1930 set_friend_statusmessage(m, i, data, data_length); 1962 set_friend_statusmessage(m, i, data_terminated, data_length);
1931 break; 1963 break;
1932 } 1964 }
1933 1965
@@ -1968,14 +2000,17 @@ void do_friends(Messenger *m)
1968 uint8_t *message = data + message_id_length; 2000 uint8_t *message = data + message_id_length;
1969 uint16_t message_length = data_length - message_id_length; 2001 uint16_t message_length = data_length - message_id_length;
1970 2002
1971 message[message_length - 1] = 0;/* Make sure the NULL terminator is present. */ 2003 /* Make sure the NULL terminator is present. */
2004 uint8_t message_terminated[message_length + 1];
2005 memcpy(message_terminated, message, message_length);
2006 message_terminated[message_length] = 0;
1972 2007
1973 if (m->friendlist[i].receives_read_receipts) { 2008 if (m->friendlist[i].receives_read_receipts) {
1974 write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); 2009 write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
1975 } 2010 }
1976 2011
1977 if (m->friend_message) 2012 if (m->friend_message)
1978 (*m->friend_message)(m, i, message, message_length, m->friend_message_userdata); 2013 (*m->friend_message)(m, i, message_terminated, message_length, m->friend_message_userdata);
1979 2014
1980 break; 2015 break;
1981 } 2016 }
@@ -1990,14 +2025,17 @@ void do_friends(Messenger *m)
1990 uint8_t *action = data + message_id_length; 2025 uint8_t *action = data + message_id_length;
1991 uint16_t action_length = data_length - message_id_length; 2026 uint16_t action_length = data_length - message_id_length;
1992 2027
1993 action[action_length - 1] = 0;/* Make sure the NULL terminator is present. */ 2028 /* Make sure the NULL terminator is present. */
2029 uint8_t action_terminated[action_length + 1];
2030 memcpy(action_terminated, action, action_length);
2031 action_terminated[action_length] = 0;
1994 2032
1995 if (m->friendlist[i].receives_read_receipts) { 2033 if (m->friendlist[i].receives_read_receipts) {
1996 write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); 2034 write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
1997 } 2035 }
1998 2036
1999 if (m->friend_action) 2037 if (m->friend_action)
2000 (*m->friend_action)(m, i, action, action_length, m->friend_action_userdata); 2038 (*m->friend_action)(m, i, action_terminated, action_length, m->friend_action_userdata);
2001 2039
2002 break; 2040 break;
2003 } 2041 }
@@ -2057,10 +2095,13 @@ void do_friends(Messenger *m)
2057 m->friendlist[i].file_receiving[filenumber].size = filesize; 2095 m->friendlist[i].file_receiving[filenumber].size = filesize;
2058 m->friendlist[i].file_receiving[filenumber].transferred = 0; 2096 m->friendlist[i].file_receiving[filenumber].transferred = 0;
2059 2097
2060 data[data_length - 1] = 0; /* Force NULL terminate file name. */ 2098 /* Force NULL terminate file name. */
2099 uint8_t filename_terminated[data_length - 1 - sizeof(uint64_t) + 1];
2100 memcpy(filename_terminated, data + 1 + sizeof(uint64_t), data_length - 1 - sizeof(uint64_t));
2101 filename_terminated[data_length - 1 - sizeof(uint64_t)] = 0;
2061 2102
2062 if (m->file_sendrequest) 2103 if (m->file_sendrequest)
2063 (*m->file_sendrequest)(m, i, filenumber, filesize, data + 1 + sizeof(uint64_t), data_length - 1 - sizeof(uint64_t), 2104 (*m->file_sendrequest)(m, i, filenumber, filesize, filename_terminated, data_length - 1 - sizeof(uint64_t),
2064 m->file_sendrequest_userdata); 2105 m->file_sendrequest_userdata);
2065 2106
2066 break; 2107 break;
@@ -2322,19 +2363,24 @@ void do_messenger(Messenger *m)
2322/* 2363/*
2323 * functions to avoid excessive polling 2364 * functions to avoid excessive polling
2324 */ 2365 */
2325int wait_prepare_messenger(Messenger *m, uint8_t *data, uint16_t *lenptr) 2366size_t wait_data_size()
2326{ 2367{
2327 return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data, lenptr); 2368 return networking_wait_data_size();
2328} 2369}
2329 2370
2330int wait_execute_messenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds) 2371int wait_prepare_messenger(Messenger *m, uint8_t *data)
2331{ 2372{
2332 return networking_wait_execute(data, len, milliseconds); 2373 return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data);
2333}; 2374}
2375
2376int wait_execute_messenger(uint8_t *data, long seconds, long microseconds)
2377{
2378 return networking_wait_execute(data, seconds, microseconds);
2379}
2334 2380
2335void wait_cleanup_messenger(Messenger *m, uint8_t *data, uint16_t len) 2381int wait_cleanup_messenger(Messenger *m, uint8_t *data)
2336{ 2382{
2337 networking_wait_cleanup(m->net, data, len); 2383 return networking_wait_cleanup(m->net, data);
2338} 2384}
2339 2385
2340/* new messenger format for load/save, more robust and forward compatible */ 2386/* new messenger format for load/save, more robust and forward compatible */
@@ -2791,7 +2837,7 @@ uint32_t get_num_online_friends(Messenger *m)
2791 * Otherwise, returns the number of elements copied. 2837 * Otherwise, returns the number of elements copied.
2792 * If the array was too small, the contents 2838 * If the array was too small, the contents
2793 * of out_list will be truncated to list_size. */ 2839 * of out_list will be truncated to list_size. */
2794uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size) 2840uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size)
2795{ 2841{
2796 if (!out_list) 2842 if (!out_list)
2797 return 0; 2843 return 0;
@@ -2823,7 +2869,7 @@ uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size)
2823 * retun 0 if success. 2869 * retun 0 if success.
2824 * return -1 if failure. 2870 * return -1 if failure.
2825 */ 2871 */
2826int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length) 2872int get_friendlist(Messenger *m, int32_t **out_list, uint32_t *out_list_length)
2827{ 2873{
2828 uint32_t i; 2874 uint32_t i;
2829 2875
@@ -2834,7 +2880,7 @@ int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length)
2834 return 0; 2880 return 0;
2835 } 2881 }
2836 2882
2837 *out_list = malloc(m->numfriends * sizeof(int)); 2883 *out_list = malloc(m->numfriends * sizeof(int32_t));
2838 2884
2839 if (*out_list == NULL) { 2885 if (*out_list == NULL) {
2840 return -1; 2886 return -1;
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index f1a527ec..f9d723a5 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -198,28 +198,28 @@ typedef struct Messenger {
198 198
199 uint64_t last_LANdiscovery; 199 uint64_t last_LANdiscovery;
200 200
201 void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *); 201 void (*friend_message)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *);
202 void *friend_message_userdata; 202 void *friend_message_userdata;
203 void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void *); 203 void (*friend_action)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *);
204 void *friend_action_userdata; 204 void *friend_action_userdata;
205 void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void *); 205 void (*friend_namechange)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *);
206 void *friend_namechange_userdata; 206 void *friend_namechange_userdata;
207 void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void *); 207 void (*friend_statusmessagechange)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *);
208 void *friend_statusmessagechange_userdata; 208 void *friend_statusmessagechange_userdata;
209 void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void *); 209 void (*friend_userstatuschange)(struct Messenger *m, int32_t, uint8_t, void *);
210 void *friend_userstatuschange_userdata; 210 void *friend_userstatuschange_userdata;
211 void (*friend_typingchange)(struct Messenger *m, int, int, void *); 211 void (*friend_typingchange)(struct Messenger *m, int32_t, int, void *);
212 void *friend_typingchange_userdata; 212 void *friend_typingchange_userdata;
213 void (*read_receipt)(struct Messenger *m, int, uint32_t, void *); 213 void (*read_receipt)(struct Messenger *m, int32_t, uint32_t, void *);
214 void *read_receipt_userdata; 214 void *read_receipt_userdata;
215 void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void *); 215 void (*friend_statuschange)(struct Messenger *m, int32_t, uint8_t, void *);
216 void *friend_statuschange_userdata; 216 void *friend_statuschange_userdata;
217 void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void *); 217 void (*friend_connectionstatuschange)(struct Messenger *m, int32_t, uint8_t, void *);
218 void *friend_connectionstatuschange_userdata; 218 void *friend_connectionstatuschange_userdata;
219 void (*friend_connectionstatuschange_internal)(struct Messenger *m, int, uint8_t, void *); 219 void (*friend_connectionstatuschange_internal)(struct Messenger *m, int32_t, uint8_t, void *);
220 void *friend_connectionstatuschange_internal_userdata; 220 void *friend_connectionstatuschange_internal_userdata;
221 221
222 void (*group_invite)(struct Messenger *m, int, uint8_t *, void *); 222 void (*group_invite)(struct Messenger *m, int32_t, uint8_t *, void *);
223 void *group_invite_userdata; 223 void *group_invite_userdata;
224 void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); 224 void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *);
225 void *group_message_userdata; 225 void *group_message_userdata;
@@ -228,14 +228,14 @@ typedef struct Messenger {
228 void (*group_namelistchange)(struct Messenger *m, int, int, uint8_t, void *); 228 void (*group_namelistchange)(struct Messenger *m, int, int, uint8_t, void *);
229 void *group_namelistchange_userdata; 229 void *group_namelistchange_userdata;
230 230
231 void (*file_sendrequest)(struct Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, void *); 231 void (*file_sendrequest)(struct Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *, uint16_t, void *);
232 void *file_sendrequest_userdata; 232 void *file_sendrequest_userdata;
233 void (*file_filecontrol)(struct Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *); 233 void (*file_filecontrol)(struct Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *);
234 void *file_filecontrol_userdata; 234 void *file_filecontrol_userdata;
235 void (*file_filedata)(struct Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *); 235 void (*file_filedata)(struct Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length, void *);
236 void *file_filedata_userdata; 236 void *file_filedata_userdata;
237 237
238 void (*msi_packet)(struct Messenger *m, int, uint8_t *, uint16_t, void *); 238 void (*msi_packet)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *);
239 void *msi_packet_userdata; 239 void *msi_packet_userdata;
240 240
241} Messenger; 241} Messenger;
@@ -262,19 +262,19 @@ void getaddress(Messenger *m, uint8_t *address);
262 * (the nospam for that friend was set to the new one). 262 * (the nospam for that friend was set to the new one).
263 * return -8 if increasing the friend list size fails. 263 * return -8 if increasing the friend list size fails.
264 */ 264 */
265int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); 265int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
266 266
267 267
268/* Add a friend without sending a friendrequest. 268/* Add a friend without sending a friendrequest.
269 * return the friend number if success. 269 * return the friend number if success.
270 * return -1 if failure. 270 * return -1 if failure.
271 */ 271 */
272int m_addfriend_norequest(Messenger *m, uint8_t *client_id); 272int32_t m_addfriend_norequest(Messenger *m, uint8_t *client_id);
273 273
274/* return the friend id associated to that client id. 274/* return the friend number associated to that client id.
275 * return -1 if no such friend. 275 * return -1 if no such friend.
276 */ 276 */
277int getfriend_id(Messenger *m, uint8_t *client_id); 277int32_t getfriend_id(Messenger *m, uint8_t *client_id);
278 278
279/* Copies the public key associated to that friend id into client_id buffer. 279/* Copies the public key associated to that friend id into client_id buffer.
280 * Make sure that client_id is of size CLIENT_ID_SIZE. 280 * Make sure that client_id is of size CLIENT_ID_SIZE.
@@ -282,10 +282,14 @@ int getfriend_id(Messenger *m, uint8_t *client_id);
282 * return 0 if success 282 * return 0 if success
283 * return -1 if failure 283 * return -1 if failure
284 */ 284 */
285int getclient_id(Messenger *m, int friend_id, uint8_t *client_id); 285int getclient_id(Messenger *m, int32_t friendnumber, uint8_t *client_id);
286 286
287/* Remove a friend. */ 287/* Remove a friend.
288int m_delfriend(Messenger *m, int friendnumber); 288 *
289 * return 0 if success
290 * return -1 if failure
291 */
292int m_delfriend(Messenger *m, int32_t friendnumber);
289 293
290/* Checks friend's connecting status. 294/* Checks friend's connecting status.
291 * 295 *
@@ -293,14 +297,14 @@ int m_delfriend(Messenger *m, int friendnumber);
293 * return 0 if friend is not connected to us (Offline). 297 * return 0 if friend is not connected to us (Offline).
294 * return -1 on failure. 298 * return -1 on failure.
295 */ 299 */
296int m_get_friend_connectionstatus(Messenger *m, int friendnumber); 300int m_get_friend_connectionstatus(Messenger *m, int32_t friendnumber);
297 301
298/* Checks if there exists a friend with given friendnumber. 302/* Checks if there exists a friend with given friendnumber.
299 * 303 *
300 * return 1 if friend exists. 304 * return 1 if friend exists.
301 * return 0 if friend doesn't exist. 305 * return 0 if friend doesn't exist.
302 */ 306 */
303int m_friend_exists(Messenger *m, int friendnumber); 307int m_friend_exists(Messenger *m, int32_t friendnumber);
304 308
305/* Send a text chat message to an online friend. 309/* Send a text chat message to an online friend.
306 * 310 *
@@ -312,8 +316,8 @@ int m_friend_exists(Messenger *m, int friendnumber);
312 * m_sendmessage_withid will send a message with the id of your choosing, 316 * m_sendmessage_withid will send a message with the id of your choosing,
313 * however we can generate an id for you by calling plain m_sendmessage. 317 * however we can generate an id for you by calling plain m_sendmessage.
314 */ 318 */
315uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length); 319uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uint32_t length);
316uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); 320uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
317 321
318/* Send an action to an online friend. 322/* Send an action to an online friend.
319 * 323 *
@@ -325,8 +329,8 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui
325 * m_sendaction_withid will send an action message with the id of your choosing, 329 * m_sendaction_withid will send an action message with the id of your choosing,
326 * however we can generate an id for you by calling plain m_sendaction. 330 * however we can generate an id for you by calling plain m_sendaction.
327 */ 331 */
328uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); 332uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint32_t length);
329uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length); 333uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length);
330 334
331/* Set the name and name_length of a friend. 335/* Set the name and name_length of a friend.
332 * name must be a string of maximum MAX_NAME_LENGTH length. 336 * name must be a string of maximum MAX_NAME_LENGTH length.
@@ -336,7 +340,7 @@ uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uin
336 * return 0 if success. 340 * return 0 if success.
337 * return -1 if failure. 341 * return -1 if failure.
338 */ 342 */
339int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length); 343int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length);
340 344
341/* Set our nickname. 345/* Set our nickname.
342 * name must be a string of maximum MAX_NAME_LENGTH length. 346 * name must be a string of maximum MAX_NAME_LENGTH length.
@@ -351,13 +355,12 @@ int setname(Messenger *m, uint8_t *name, uint16_t length);
351/* 355/*
352 * Get your nickname. 356 * Get your nickname.
353 * m - The messanger context to use. 357 * m - The messanger context to use.
354 * name - Pointer to a string for the name. 358 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
355 * nlen - The length of the string buffer.
356 * 359 *
357 * return length of the name. 360 * return length of the name.
358 * return 0 on error. 361 * return 0 on error.
359 */ 362 */
360uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); 363uint16_t getself_name(Messenger *m, uint8_t *name);
361 364
362/* Get name of friendnumber and put it in name. 365/* Get name of friendnumber and put it in name.
363 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 366 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
@@ -365,12 +368,18 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen);
365 * return length of name if success. 368 * return length of name if success.
366 * return -1 if failure. 369 * return -1 if failure.
367 */ 370 */
368int getname(Messenger *m, int friendnumber, uint8_t *name); 371int getname(Messenger *m, int32_t friendnumber, uint8_t *name);
372
373/* return the length of name, including null on success.
374 * return -1 on failure.
375 */
376int m_get_name_size(Messenger *m, int32_t friendnumber);
377int m_get_self_name_size(Messenger *m);
369 378
370/* returns valid ip port of connected friend on success 379/* returns valid ip port of connected friend on success
371 * returns zeroed out IP_Port on failure 380 * returns zeroed out IP_Port on failure
372 */ 381 */
373IP_Port get_friend_ipport(Messenger *m, int friendnumber); 382IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber);
374 383
375/* Set our user status. 384/* Set our user status.
376 * You are responsible for freeing status after. 385 * You are responsible for freeing status after.
@@ -379,12 +388,13 @@ IP_Port get_friend_ipport(Messenger *m, int friendnumber);
379 * returns -1 on failure. 388 * returns -1 on failure.
380 */ 389 */
381int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); 390int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length);
382int m_set_userstatus(Messenger *m, USERSTATUS status); 391int m_set_userstatus(Messenger *m, uint8_t status);
383 392
384/* return the length of friendnumber's status message, including null. 393/* return the length of friendnumber's status message, including null on success.
385 * Pass it into malloc. 394 * return -1 on failure.
386 */ 395 */
387int m_get_statusmessage_size(Messenger *m, int friendnumber); 396int m_get_statusmessage_size(Messenger *m, int32_t friendnumber);
397int m_get_self_statusmessage_size(Messenger *m);
388 398
389/* Copy friendnumber's status message into buf, truncating if size is over maxlen. 399/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
390 * Get the size you need to allocate from m_get_statusmessage_size. 400 * Get the size you need to allocate from m_get_statusmessage_size.
@@ -393,7 +403,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber);
393 * returns the length of the copied data on success 403 * returns the length of the copied data on success
394 * retruns -1 on failure. 404 * retruns -1 on failure.
395 */ 405 */
396int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen); 406int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen);
397int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); 407int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
398 408
399/* return one of USERSTATUS values. 409/* return one of USERSTATUS values.
@@ -401,13 +411,13 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
401 * As above, the self variant will return our own USERSTATUS. 411 * As above, the self variant will return our own USERSTATUS.
402 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. 412 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
403 */ 413 */
404USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); 414uint8_t m_get_userstatus(Messenger *m, int32_t friendnumber);
405USERSTATUS m_get_self_userstatus(Messenger *m); 415uint8_t m_get_self_userstatus(Messenger *m);
406 416
407/* returns timestamp of last time friendnumber was seen online, or 0 if never seen. 417/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
408 * returns -1 on error. 418 * returns -1 on error.
409 */ 419 */
410uint64_t m_get_last_online(Messenger *m, int friendnumber); 420uint64_t m_get_last_online(Messenger *m, int32_t friendnumber);
411 421
412/* Set our typing status for a friend. 422/* Set our typing status for a friend.
413 * You are responsible for turning it on or off. 423 * You are responsible for turning it on or off.
@@ -415,63 +425,65 @@ uint64_t m_get_last_online(Messenger *m, int friendnumber);
415 * returns 0 on success. 425 * returns 0 on success.
416 * returns -1 on failure. 426 * returns -1 on failure.
417 */ 427 */
418int m_set_usertyping(Messenger *m, int friendnumber, uint8_t is_typing); 428int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing);
419 429
420/* Get the typing status of a friend. 430/* Get the typing status of a friend.
421 * 431 *
422 * returns 0 if friend is not typing. 432 * returns 0 if friend is not typing.
423 * returns 1 if friend is typing. 433 * returns 1 if friend is typing.
424 */ 434 */
425int m_get_istyping(Messenger *m, int friendnumber); 435int m_get_istyping(Messenger *m, int32_t friendnumber);
426 436
427/* Sets whether we send read receipts for friendnumber. 437/* Sets whether we send read receipts for friendnumber.
428 * This function is not lazy, and it will fail if yesno is not (0 or 1). 438 * This function is not lazy, and it will fail if yesno is not (0 or 1).
429 */ 439 */
430void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); 440void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno);
431 441
432/* Set the function that will be executed when a friend request is received. 442/* Set the function that will be executed when a friend request is received.
433 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 443 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
434 */ 444 */
435void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); 445void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *),
446 void *userdata);
436 447
437/* Set the function that will be executed when a message from a friend is received. 448/* Set the function that will be executed when a message from a friend is received.
438 * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) 449 * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length)
439 */ 450 */
440void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 451void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
441 void *userdata); 452 void *userdata);
442 453
443/* Set the function that will be executed when an action from a friend is received. 454/* Set the function that will be executed when an action from a friend is received.
444 * Function format is: function(int friendnumber, uint8_t * action, uint32_t length) 455 * Function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length)
445 */ 456 */
446void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata); 457void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
458 void *userdata);
447 459
448/* Set the callback for name changes. 460/* Set the callback for name changes.
449 * Function(int friendnumber, uint8_t *newname, uint16_t length) 461 * Function(int32_t friendnumber, uint8_t *newname, uint16_t length)
450 * You are not responsible for freeing newname. 462 * You are not responsible for freeing newname.
451 */ 463 */
452void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 464void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
453 void *userdata); 465 void *userdata);
454 466
455/* Set the callback for status message changes. 467/* Set the callback for status message changes.
456 * Function(int friendnumber, uint8_t *newstatus, uint16_t length) 468 * Function(int32_t friendnumber, uint8_t *newstatus, uint16_t length)
457 * 469 *
458 * You are not responsible for freeing newstatus 470 * You are not responsible for freeing newstatus
459 */ 471 */
460void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 472void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
461 void *userdata); 473 void *userdata);
462 474
463/* Set the callback for status type changes. 475/* Set the callback for status type changes.
464 * Function(int friendnumber, USERSTATUS kind) 476 * Function(int32_t friendnumber, USERSTATUS kind)
465 */ 477 */
466void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata); 478void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), void *userdata);
467 479
468/* Set the callback for typing changes. 480/* Set the callback for typing changes.
469 * Function(int friendnumber, int is_typing) 481 * Function(int32_t friendnumber, int is_typing)
470 */ 482 */
471void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int, int, void *), void *userdata); 483void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int32_t, int, void *), void *userdata);
472 484
473/* Set the callback for read receipts. 485/* Set the callback for read receipts.
474 * Function(int friendnumber, uint32_t receipt) 486 * Function(int32_t friendnumber, uint32_t receipt)
475 * 487 *
476 * If you are keeping a record of returns from m_sendmessage, 488 * If you are keeping a record of returns from m_sendmessage,
477 * receipt might be one of those values, meaning the message 489 * receipt might be one of those values, meaning the message
@@ -479,10 +491,10 @@ void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int, in
479 * Since core doesn't track ids for you, receipt may not correspond to any message. 491 * Since core doesn't track ids for you, receipt may not correspond to any message.
480 * In that case, you should discard it. 492 * In that case, you should discard it.
481 */ 493 */
482void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata); 494void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int32_t, uint32_t, void *), void *userdata);
483 495
484/* Set the callback for connection status changes. 496/* Set the callback for connection status changes.
485 * function(int friendnumber, uint8_t status) 497 * function(int32_t friendnumber, uint8_t status)
486 * 498 *
487 * Status: 499 * Status:
488 * 0 -- friend went offline after being previously online. 500 * 0 -- friend went offline after being previously online.
@@ -492,18 +504,19 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u
492 * being previously online" part. 504 * being previously online" part.
493 * It's assumed that when adding friends, their connection status is offline. 505 * It's assumed that when adding friends, their connection status is offline.
494 */ 506 */
495void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); 507void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *),
508 void *userdata);
496/* Same as previous but for internal A/V core usage only */ 509/* Same as previous but for internal A/V core usage only */
497void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), 510void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *),
498 void *userdata); 511 void *userdata);
499 512
500/**********GROUP CHATS************/ 513/**********GROUP CHATS************/
501 514
502/* Set the callback for group invites. 515/* Set the callback for group invites.
503 * 516 *
504 * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata) 517 * Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata)
505 */ 518 */
506void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata); 519void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, void *), void *userdata);
507 520
508/* Set the callback for group messages. 521/* Set the callback for group messages.
509 * 522 *
@@ -553,14 +566,14 @@ int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *nam
553 * return 0 on success 566 * return 0 on success
554 * return -1 on failure 567 * return -1 on failure
555 */ 568 */
556int invite_friend(Messenger *m, int friendnumber, int groupnumber); 569int invite_friend(Messenger *m, int32_t friendnumber, int groupnumber);
557 570
558/* Join a group (you need to have been invited first.) 571/* Join a group (you need to have been invited first.)
559 * 572 *
560 * returns group number on success 573 * returns group number on success
561 * returns -1 on failure. 574 * returns -1 on failure.
562 */ 575 */
563int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key); 576int join_groupchat(Messenger *m, int32_t friendnumber, uint8_t *friend_group_public_key);
564 577
565/* send a group message 578/* send a group message
566 * return 0 on success 579 * return 0 on success
@@ -594,25 +607,27 @@ int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES],
594 607
595/* Set the callback for file send requests. 608/* Set the callback for file send requests.
596 * 609 *
597 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) 610 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
598 */ 611 */
599void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, 612void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *,
613 uint16_t,
600 void *), void *userdata); 614 void *), void *userdata);
601 615
602/* Set the callback for file control requests. 616/* Set the callback for file control requests.
603 * 617 *
604 * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 618 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
605 * 619 *
606 */ 620 */
607void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, 621void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *,
608 uint16_t, void *), void *userdata); 622 uint16_t, void *), void *userdata);
609 623
610/* Set the callback for file data. 624/* Set the callback for file data.
611 * 625 *
612 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 626 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
613 * 627 *
614 */ 628 */
615void callback_file_data(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *), 629void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length,
630 void *),
616 void *userdata); 631 void *userdata);
617 632
618/* Send a file send request. 633/* Send a file send request.
@@ -620,7 +635,7 @@ void callback_file_data(Messenger *m, void (*function)(Messenger *m, int, uint8_
620 * return 1 on success 635 * return 1 on success
621 * return 0 on failure 636 * return 0 on failure
622 */ 637 */
623int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, 638int file_sendrequest(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename,
624 uint16_t filename_length); 639 uint16_t filename_length);
625 640
626/* Send a file send request. 641/* Send a file send request.
@@ -628,7 +643,7 @@ int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_
628 * return file number on success 643 * return file number on success
629 * return -1 on failure 644 * return -1 on failure
630 */ 645 */
631int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); 646int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length);
632 647
633/* Send a file control request. 648/* Send a file control request.
634 * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file. 649 * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file.
@@ -636,7 +651,7 @@ int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *f
636 * return 1 on success 651 * return 1 on success
637 * return 0 on failure 652 * return 0 on failure
638 */ 653 */
639int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, 654int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
640 uint8_t *data, uint16_t length); 655 uint8_t *data, uint16_t length);
641 656
642/* Send file data. 657/* Send file data.
@@ -644,7 +659,7 @@ int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t f
644 * return 1 on success 659 * return 1 on success
645 * return 0 on failure 660 * return 0 on failure
646 */ 661 */
647int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); 662int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length);
648 663
649/* Give the number of bytes left to be sent/received. 664/* Give the number of bytes left to be sent/received.
650 * 665 *
@@ -653,15 +668,15 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data,
653 * return number of bytes remaining to be sent/received on success 668 * return number of bytes remaining to be sent/received on success
654 * return 0 on failure 669 * return 0 on failure
655 */ 670 */
656uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t send_receive); 671uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive);
657 672
658/*************** A/V related ******************/ 673/*************** A/V related ******************/
659 674
660/* Set the callback for msi packets. 675/* Set the callback for msi packets.
661 * 676 *
662 * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata) 677 * Function(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length, void *userdata)
663 */ 678 */
664void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 679void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
665 void *userdata); 680 void *userdata);
666 681
667/* Send an msi packet. 682/* Send an msi packet.
@@ -669,7 +684,7 @@ void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uin
669 * return 1 on success 684 * return 1 on success
670 * return 0 on failure 685 * return 0 on failure
671 */ 686 */
672int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length); 687int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length);
673 688
674/**********************************************/ 689/**********************************************/
675 690
@@ -678,7 +693,7 @@ int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length)
678 * return -1 on failure. 693 * return -1 on failure.
679 * return 0 on success. 694 * return 0 on success.
680 */ 695 */
681int custom_user_packet_registerhandler(Messenger *m, int friendnumber, uint8_t byte, packet_handler_callback cb, 696int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb,
682 void *object); 697 void *object);
683 698
684/* High level function to send custom user packets. 699/* High level function to send custom user packets.
@@ -686,7 +701,7 @@ int custom_user_packet_registerhandler(Messenger *m, int friendnumber, uint8_t b
686 * return -1 on failure. 701 * return -1 on failure.
687 * return number of bytes sent on success. 702 * return number of bytes sent on success.
688 */ 703 */
689int send_custom_user_packet(Messenger *m, int friendnumber, uint8_t *data, uint32_t length); 704int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length);
690 705
691/**********************************************/ 706/**********************************************/
692/* Run this at startup. 707/* Run this at startup.
@@ -706,9 +721,10 @@ void do_messenger(Messenger *m);
706/* 721/*
707 * functions to avoid excessive polling 722 * functions to avoid excessive polling
708 */ 723 */
709int wait_prepare_messenger(Messenger *m, uint8_t *data, uint16_t *lenptr); 724size_t wait_data_size();
710int wait_execute_messenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds); 725int wait_prepare_messenger(Messenger *m, uint8_t *data);
711void wait_cleanup_messenger(Messenger *m, uint8_t *data, uint16_t len); 726int wait_execute_messenger(uint8_t *data, long seconds, long microseconds);
727int wait_cleanup_messenger(Messenger *m, uint8_t *data);
712 728
713/* SAVING AND LOADING FUNCTIONS: */ 729/* SAVING AND LOADING FUNCTIONS: */
714 730
@@ -752,7 +768,7 @@ uint32_t get_num_online_friends(Messenger *m);
752 * Otherwise, returns the number of elements copied. 768 * Otherwise, returns the number of elements copied.
753 * If the array was too small, the contents 769 * If the array was too small, the contents
754 * of out_list will be truncated to list_size. */ 770 * of out_list will be truncated to list_size. */
755uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size); 771uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size);
756 772
757/* Allocate and return a list of valid friend id's. List must be freed by the 773/* Allocate and return a list of valid friend id's. List must be freed by the
758 * caller. 774 * caller.
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c
index 9ac72097..7574a881 100644
--- a/toxcore/friend_requests.c
+++ b/toxcore/friend_requests.c
@@ -72,11 +72,12 @@ uint32_t get_nospam(Friend_Requests *fr)
72 72
73 73
74/* Set the function that will be executed when a friend request is received. */ 74/* Set the function that will be executed when a friend request is received. */
75void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), 75void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *),
76 void *userdata) 76 void *object, void *userdata)
77{ 77{
78 fr->handle_friendrequest = function; 78 fr->handle_friendrequest = function;
79 fr->handle_friendrequest_isset = 1; 79 fr->handle_friendrequest_isset = 1;
80 fr->handle_friendrequest_object = object;
80 fr->handle_friendrequest_userdata = userdata; 81 fr->handle_friendrequest_userdata = userdata;
81} 82}
82/* Set the function used to check if a friend request should be displayed to the user or not. */ 83/* Set the function used to check if a friend request should be displayed to the user or not. */
@@ -141,9 +142,12 @@ static int friendreq_handlepacket(void *object, uint8_t *source_pubkey, uint8_t
141 142
142 addto_receivedlist(fr, source_pubkey); 143 addto_receivedlist(fr, source_pubkey);
143 144
144 packet[length - 1] = 0; /* Force NULL terminator. */ 145 uint8_t message[length - 4 + 1];
146 memcpy(message, packet + 4, length - 4);
147 message[sizeof(message) - 1] = 0; /* Be sure the message is null terminated. */
145 148
146 (*fr->handle_friendrequest)(source_pubkey, packet + 4, length - 4, fr->handle_friendrequest_userdata); 149 (*fr->handle_friendrequest)(fr->handle_friendrequest_object, source_pubkey, message, length - 4,
150 fr->handle_friendrequest_userdata);
147 return 0; 151 return 0;
148} 152}
149 153
diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h
index 732dc4a2..722c7431 100644
--- a/toxcore/friend_requests.h
+++ b/toxcore/friend_requests.h
@@ -30,8 +30,9 @@
30 30
31typedef struct { 31typedef struct {
32 uint32_t nospam; 32 uint32_t nospam;
33 void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void *); 33 void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *);
34 uint8_t handle_friendrequest_isset; 34 uint8_t handle_friendrequest_isset;
35 void *handle_friendrequest_object;
35 void *handle_friendrequest_userdata; 36 void *handle_friendrequest_userdata;
36 37
37 int (*filter_function)(uint8_t *, void *); 38 int (*filter_function)(uint8_t *, void *);
@@ -57,8 +58,8 @@ uint32_t get_nospam(Friend_Requests *fr);
57/* Set the function that will be executed when a friend request for us is received. 58/* Set the function that will be executed when a friend request for us is received.
58 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length, void * userdata) 59 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length, void * userdata)
59 */ 60 */
60void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), 61void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *),
61 void *userdata); 62 void *object, void *userdata);
62 63
63/* Set the function used to check if a friend request should be displayed to the user or not. 64/* Set the function used to check if a friend request should be displayed to the user or not.
64 * Function format is int function(uint8_t * public_key, void * userdata) 65 * Function format is int function(uint8_t * public_key, void * userdata)
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c
index 1ec8ede5..22cac286 100644
--- a/toxcore/group_chats.c
+++ b/toxcore/group_chats.c
@@ -335,7 +335,7 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name)
335 335
336static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len) 336static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len)
337{ 337{
338 if (contents_len > MAX_NICK_BYTES || contents_len == 0) 338 if (contents_len >= MAX_NICK_BYTES || contents_len == 0)
339 return; 339 return;
340 340
341 /* same name as already stored? */ 341 /* same name as already stored? */
@@ -345,7 +345,7 @@ static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t c
345 345
346 memcpy(chat->group[peernum].nick, contents, contents_len); 346 memcpy(chat->group[peernum].nick, contents, contents_len);
347 /* Force null termination */ 347 /* Force null termination */
348 chat->group[peernum].nick[contents_len - 1] = 0; 348 chat->group[peernum].nick[contents_len] = 0;
349 chat->group[peernum].nick_len = contents_len; 349 chat->group[peernum].nick_len = contents_len;
350 350
351 if (chat->peer_namelistchange != NULL) 351 if (chat->peer_namelistchange != NULL)
diff --git a/toxcore/network.c b/toxcore/network.c
index 64f0af2f..69cc23d8 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -311,17 +311,17 @@ typedef struct {
311 uint64_t send_fail_eagain; 311 uint64_t send_fail_eagain;
312} select_info; 312} select_info;
313 313
314int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr) 314size_t networking_wait_data_size()
315{ 315{
316 if ((data == NULL) || !lenptr || (*lenptr < sizeof(select_info))) { 316 return sizeof(select_info);
317 if (lenptr) { 317}
318 *lenptr = sizeof(select_info); 318
319 return 0; 319int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data)
320 } else 320{
321 return -1; 321 if (data == NULL) {
322 return 0;
322 } 323 }
323 324
324 *lenptr = sizeof(select_info);
325 select_info *s = (select_info *)data; 325 select_info *s = (select_info *)data;
326 s->sock = net->sock; 326 s->sock = net->sock;
327 s->sendqueue_length = sendqueue_length; 327 s->sendqueue_length = sendqueue_length;
@@ -331,25 +331,41 @@ int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uin
331 return 1; 331 return 1;
332} 332}
333 333
334int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) 334/* *** Function MUSTN'T poll. ***
335* The function mustn't modify anything at all, so it can be called completely
336* asynchronously without any worry.
337*/
338int networking_wait_execute(uint8_t *data, long seconds, long microseconds)
335{ 339{
336 /* WIN32: supported since Win2K, but might need some adjustements */ 340 /* WIN32: supported since Win2K, but might need some adjustements */
337 /* UNIX: this should work for any remotely Unix'ish system */ 341 /* UNIX: this should work for any remotely Unix'ish system */
338 342
343 if (data == NULL) {
344 return 0;
345 }
346
339 select_info *s = (select_info *)data; 347 select_info *s = (select_info *)data;
340 348
341 /* add only if we had a failed write */ 349 /* add only if we had a failed write */
342 int writefds_add = 0; 350 int writefds_add = 0;
343 351
352 /* if send_fail_eagain is set, that means that socket's buffer was full and couldn't fit data we tried to send,
353 * so this is the only case when we need to know when the socket becomes write-ready, i.e. socket's buffer gets
354 * some free space for us to put data to be sent in, but select will tell us that the socket is writable even
355 * if we can fit a small part of our data (say 1 byte), so we wait some time, in hope that large enough chunk
356 * of socket's buffer will be available (at least that's how I understand intentions of the previous author of
357 * that code)
358 */
344 if (s->send_fail_eagain != 0) { 359 if (s->send_fail_eagain != 0) {
345 // current_time(): microseconds 360 // current_time(): microseconds
346 uint64_t now = current_time(); 361 uint64_t now = current_time();
347 362
348 /* s->sendqueue_length: might be used to guess how long we keep checking */ 363 /* s->sendqueue_length: might be used to guess how long we keep checking */
349 /* for now, threshold is hardcoded to 500ms, too long for a really really 364 /* for now, threshold is hardcoded to 250ms, too long for a really really
350 * fast link, but too short for a sloooooow link... */ 365 * fast link, but too short for a sloooooow link... */
351 if (now - s->send_fail_eagain < 500000) 366 if (now - s->send_fail_eagain < 250000) {
352 writefds_add = 1; 367 writefds_add = 1;
368 }
353 } 369 }
354 370
355 int nfds = 1 + s->sock; 371 int nfds = 1 + s->sock;
@@ -362,27 +378,34 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds)
362 fd_set writefds; 378 fd_set writefds;
363 FD_ZERO(&writefds); 379 FD_ZERO(&writefds);
364 380
365 if (writefds_add) 381 if (writefds_add) {
366 FD_SET(s->sock, &writefds); 382 FD_SET(s->sock, &writefds);
383 }
367 384
368 fd_set exceptfds; 385 fd_set exceptfds;
369 FD_ZERO(&exceptfds); 386 FD_ZERO(&exceptfds);
370 FD_SET(s->sock, &exceptfds); 387 FD_SET(s->sock, &exceptfds);
371 388
372 struct timeval timeout; 389 struct timeval timeout;
373 timeout.tv_sec = 0; 390 struct timeval *timeout_ptr = &timeout;
374 timeout.tv_usec = milliseconds * 1000; 391
392 if (seconds < 0 || microseconds < 0) {
393 timeout_ptr = NULL;
394 } else {
395 timeout.tv_sec = seconds;
396 timeout.tv_usec = microseconds;
397 }
375 398
376#ifdef LOGGING 399#ifdef LOGGING
377 errno = 0; 400 errno = 0;
378#endif 401#endif
379 /* returns -1 on error, 0 on timeout, the socket on activity */ 402 /* returns -1 on error, 0 on timeout, the socket on activity */
380 int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); 403 int res = select(nfds, &readfds, &writefds, &exceptfds, timeout_ptr);
381#ifdef LOGGING 404#ifdef LOGGING
382 405
383 /* only dump if not timeout */ 406 /* only dump if not timeout */
384 if (res) { 407 if (res) {
385 sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno, 408 sprintf(logbuffer, "select(%d, %d): %d (%d, %s) - %d %d %d\n", microseconds, seconds, res, errno,
386 strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), 409 strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds),
387 FD_ISSET(s->sock, &exceptfds)); 410 FD_ISSET(s->sock, &exceptfds));
388 loglog(logbuffer); 411 loglog(logbuffer);
@@ -390,18 +413,26 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds)
390 413
391#endif 414#endif
392 415
393 if (FD_ISSET(s->sock, &writefds)) 416 if (FD_ISSET(s->sock, &writefds)) {
394 s->send_fail_reset = 1; 417 s->send_fail_reset = 1;
418 }
395 419
396 return res > 0 ? 1 : 0; 420 return res > 0 ? 2 : 1;
397} 421}
398 422
399void networking_wait_cleanup(Networking_Core *net, uint8_t *data, uint16_t len) 423int networking_wait_cleanup(Networking_Core *net, uint8_t *data)
400{ 424{
425 if (data == NULL) {
426 return 0;
427 }
428
401 select_info *s = (select_info *)data; 429 select_info *s = (select_info *)data;
402 430
403 if (s->send_fail_reset) 431 if (s->send_fail_reset) {
404 net->send_fail_eagain = 0; 432 net->send_fail_eagain = 0;
433 }
434
435 return 1;
405} 436}
406 437
407uint8_t at_startup_ran = 0; 438uint8_t at_startup_ran = 0;
diff --git a/toxcore/network.h b/toxcore/network.h
index 5e434c1a..5845932f 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -316,9 +316,10 @@ void networking_poll(Networking_Core *net);
316/* 316/*
317 * functions to avoid excessive polling 317 * functions to avoid excessive polling
318 */ 318 */
319int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr); 319size_t networking_wait_data_size();
320int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds); 320int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data);
321void networking_wait_cleanup(Networking_Core *net, uint8_t *data, uint16_t len); 321int networking_wait_execute(uint8_t *data, long seconds, long microseconds);
322int networking_wait_cleanup(Networking_Core *net, uint8_t *data);
322 323
323/* Initialize networking. 324/* Initialize networking.
324 * bind to ip and port. 325 * bind to ip and port.
diff --git a/toxcore/tox.c b/toxcore/tox.c
index ce4bd996..a928f3f9 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -60,7 +60,7 @@ void tox_get_address(Tox *tox, uint8_t *address)
60 * (the nospam for that friend was set to the new one). 60 * (the nospam for that friend was set to the new one).
61 * return FAERR_NOMEM if increasing the friend list size fails. 61 * return FAERR_NOMEM if increasing the friend list size fails.
62 */ 62 */
63int tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length) 63int32_t tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length)
64{ 64{
65 Messenger *m = tox; 65 Messenger *m = tox;
66 return m_addfriend(m, address, data, length); 66 return m_addfriend(m, address, data, length);
@@ -71,16 +71,16 @@ int tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length)
71 * return the friend number if success. 71 * return the friend number if success.
72 * return -1 if failure. 72 * return -1 if failure.
73 */ 73 */
74int tox_add_friend_norequest(Tox *tox, uint8_t *client_id) 74int32_t tox_add_friend_norequest(Tox *tox, uint8_t *client_id)
75{ 75{
76 Messenger *m = tox; 76 Messenger *m = tox;
77 return m_addfriend_norequest(m, client_id); 77 return m_addfriend_norequest(m, client_id);
78} 78}
79 79
80/* return the friend id associated to that client id. 80/* return the friend number associated to that client id.
81 * return -1 if no such friend. 81 * return -1 if no such friend.
82 */ 82 */
83int tox_get_friend_id(Tox *tox, uint8_t *client_id) 83int32_t tox_get_friend_number(Tox *tox, uint8_t *client_id)
84{ 84{
85 Messenger *m = tox; 85 Messenger *m = tox;
86 return getfriend_id(m, client_id); 86 return getfriend_id(m, client_id);
@@ -92,14 +92,14 @@ int tox_get_friend_id(Tox *tox, uint8_t *client_id)
92 * return 0 if success. 92 * return 0 if success.
93 * return -1 if failure. 93 * return -1 if failure.
94 */ 94 */
95int tox_get_client_id(Tox *tox, int friend_id, uint8_t *client_id) 95int tox_get_client_id(Tox *tox, int32_t friendnumber, uint8_t *client_id)
96{ 96{
97 Messenger *m = tox; 97 Messenger *m = tox;
98 return getclient_id(m, friend_id, client_id); 98 return getclient_id(m, friendnumber, client_id);
99} 99}
100 100
101/* Remove a friend. */ 101/* Remove a friend. */
102int tox_del_friend(Tox *tox, int friendnumber) 102int tox_del_friend(Tox *tox, int32_t friendnumber)
103{ 103{
104 Messenger *m = tox; 104 Messenger *m = tox;
105 return m_delfriend(m, friendnumber); 105 return m_delfriend(m, friendnumber);
@@ -111,7 +111,7 @@ int tox_del_friend(Tox *tox, int friendnumber)
111 * return 0 if friend is not connected to us (Offline). 111 * return 0 if friend is not connected to us (Offline).
112 * return -1 on failure. 112 * return -1 on failure.
113 */ 113 */
114int tox_get_friend_connection_status(Tox *tox, int friendnumber) 114int tox_get_friend_connection_status(Tox *tox, int32_t friendnumber)
115{ 115{
116 Messenger *m = tox; 116 Messenger *m = tox;
117 return m_get_friend_connectionstatus(m, friendnumber); 117 return m_get_friend_connectionstatus(m, friendnumber);
@@ -122,7 +122,7 @@ int tox_get_friend_connection_status(Tox *tox, int friendnumber)
122 * return 1 if friend exists. 122 * return 1 if friend exists.
123 * return 0 if friend doesn't exist. 123 * return 0 if friend doesn't exist.
124 */ 124 */
125int tox_friend_exists(Tox *tox, int friendnumber) 125int tox_friend_exists(Tox *tox, int32_t friendnumber)
126{ 126{
127 Messenger *m = tox; 127 Messenger *m = tox;
128 return m_friend_exists(m, friendnumber); 128 return m_friend_exists(m, friendnumber);
@@ -137,13 +137,13 @@ int tox_friend_exists(Tox *tox, int friendnumber)
137 * m_sendmessage_withid will send a message with the id of your choosing, 137 * m_sendmessage_withid will send a message with the id of your choosing,
138 * however we can generate an id for you by calling plain m_sendmessage. 138 * however we can generate an id for you by calling plain m_sendmessage.
139 */ 139 */
140uint32_t tox_send_message(Tox *tox, int friendnumber, uint8_t *message, uint32_t length) 140uint32_t tox_send_message(Tox *tox, int32_t friendnumber, uint8_t *message, uint32_t length)
141{ 141{
142 Messenger *m = tox; 142 Messenger *m = tox;
143 return m_sendmessage(m, friendnumber, message, length); 143 return m_sendmessage(m, friendnumber, message, length);
144} 144}
145 145
146uint32_t tox_send_message_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) 146uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length)
147{ 147{
148 Messenger *m = tox; 148 Messenger *m = tox;
149 return m_sendmessage_withid(m, friendnumber, theid, message, length); 149 return m_sendmessage_withid(m, friendnumber, theid, message, length);
@@ -159,13 +159,13 @@ uint32_t tox_send_message_withid(Tox *tox, int friendnumber, uint32_t theid, uin
159 * m_sendaction_withid will send an action message with the id of your choosing, 159 * m_sendaction_withid will send an action message with the id of your choosing,
160 * however we can generate an id for you by calling plain m_sendaction. 160 * however we can generate an id for you by calling plain m_sendaction.
161 */ 161 */
162uint32_t tox_send_action(Tox *tox, int friendnumber, uint8_t *action, uint32_t length) 162uint32_t tox_send_action(Tox *tox, int32_t friendnumber, uint8_t *action, uint32_t length)
163{ 163{
164 Messenger *m = tox; 164 Messenger *m = tox;
165 return m_sendaction(m, friendnumber, action, length); 165 return m_sendaction(m, friendnumber, action, length);
166} 166}
167 167
168uint32_t tox_send_action_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length) 168uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length)
169{ 169{
170 Messenger *m = tox; 170 Messenger *m = tox;
171 return m_sendaction_withid(m, friendnumber, theid, action, length); 171 return m_sendaction_withid(m, friendnumber, theid, action, length);
@@ -187,16 +187,15 @@ int tox_set_name(Tox *tox, uint8_t *name, uint16_t length)
187 187
188/* Get your nickname. 188/* Get your nickname.
189 * m - The messanger context to use. 189 * m - The messanger context to use.
190 * name - Pointer to a string for the name. 190 * name - Pointer to a string for the name. (must be at least MAX_NAME_LENGTH)
191 * nlen - The length of the string buffer.
192 * 191 *
193 * return length of the name. 192 * return length of the name.
194 * return 0 on error. 193 * return 0 on error.
195 */ 194 */
196uint16_t tox_get_self_name(Tox *tox, uint8_t *name, uint16_t nlen) 195uint16_t tox_get_self_name(Tox *tox, uint8_t *name)
197{ 196{
198 Messenger *m = tox; 197 Messenger *m = tox;
199 return getself_name(m, name, nlen); 198 return getself_name(m, name);
200} 199}
201 200
202/* Get name of friendnumber and put it in name. 201/* Get name of friendnumber and put it in name.
@@ -205,12 +204,27 @@ uint16_t tox_get_self_name(Tox *tox, uint8_t *name, uint16_t nlen)
205 * return length of name (with the NULL terminator) if success. 204 * return length of name (with the NULL terminator) if success.
206 * return -1 if failure. 205 * return -1 if failure.
207 */ 206 */
208int tox_get_name(Tox *tox, int friendnumber, uint8_t *name) 207int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name)
209{ 208{
210 Messenger *m = tox; 209 Messenger *m = tox;
211 return getname(m, friendnumber, name); 210 return getname(m, friendnumber, name);
212} 211}
213 212
213/* returns the length of name on success.
214 * returns -1 on failure.
215 */
216int tox_get_name_size(Tox *tox, int32_t friendnumber)
217{
218 Messenger *m = tox;
219 return m_get_name_size(m, friendnumber);
220}
221
222int tox_get_self_name_size(Tox *tox)
223{
224 Messenger *m = tox;
225 return m_get_self_name_size(m);
226}
227
214/* Set our user status; 228/* Set our user status;
215 * you are responsible for freeing status after. 229 * you are responsible for freeing status after.
216 * 230 *
@@ -222,26 +236,32 @@ int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length)
222 return m_set_statusmessage(m, status, length); 236 return m_set_statusmessage(m, status, length);
223} 237}
224 238
225int tox_set_user_status(Tox *tox, TOX_USERSTATUS status) 239int tox_set_user_status(Tox *tox, uint8_t status)
226{ 240{
227 Messenger *m = tox; 241 Messenger *m = tox;
228 return m_set_userstatus(m, (USERSTATUS)status); 242 return m_set_userstatus(m, status);
229} 243}
230 244
231/* return the length of friendnumber's status message, including null. 245/* returns the length of status message on success.
232 * Pass it into malloc. 246 * returns -1 on failure.
233 */ 247 */
234int tox_get_status_message_size(Tox *tox, int friendnumber) 248int tox_get_status_message_size(Tox *tox, int32_t friendnumber)
235{ 249{
236 Messenger *m = tox; 250 Messenger *m = tox;
237 return m_get_statusmessage_size(m, friendnumber); 251 return m_get_statusmessage_size(m, friendnumber);
238} 252}
239 253
254int tox_get_self_status_message_size(Tox *tox)
255{
256 Messenger *m = tox;
257 return m_get_self_statusmessage_size(m);
258}
259
240/* Copy friendnumber's status message into buf, truncating if size is over maxlen. 260/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
241 * Get the size you need to allocate from m_get_statusmessage_size. 261 * Get the size you need to allocate from m_get_statusmessage_size.
242 * The self variant will copy our own status message. 262 * The self variant will copy our own status message.
243 */ 263 */
244int tox_get_status_message(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) 264int tox_get_status_message(Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
245{ 265{
246 Messenger *m = tox; 266 Messenger *m = tox;
247 return m_copy_statusmessage(m, friendnumber, buf, maxlen); 267 return m_copy_statusmessage(m, friendnumber, buf, maxlen);
@@ -258,22 +278,22 @@ int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen)
258 * As above, the self variant will return our own USERSTATUS. 278 * As above, the self variant will return our own USERSTATUS.
259 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. 279 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
260 */ 280 */
261TOX_USERSTATUS tox_get_user_status(Tox *tox, int friendnumber) 281uint8_t tox_get_user_status(Tox *tox, int32_t friendnumber)
262{ 282{
263 Messenger *m = tox; 283 Messenger *m = tox;
264 return (TOX_USERSTATUS)m_get_userstatus(m, friendnumber); 284 return m_get_userstatus(m, friendnumber);
265} 285}
266 286
267TOX_USERSTATUS tox_get_self_user_status(Tox *tox) 287uint8_t tox_get_self_user_status(Tox *tox)
268{ 288{
269 Messenger *m = tox; 289 Messenger *m = tox;
270 return (TOX_USERSTATUS)m_get_self_userstatus(m); 290 return m_get_self_userstatus(m);
271} 291}
272 292
273/* returns timestamp of last time friendnumber was seen online, or 0 if never seen. 293/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
274 * returns -1 on error. 294 * returns -1 on error.
275 */ 295 */
276uint64_t tox_get_last_online(Tox *tox, int friendnumber) 296uint64_t tox_get_last_online(Tox *tox, int32_t friendnumber)
277{ 297{
278 Messenger *m = tox; 298 Messenger *m = tox;
279 return m_get_last_online(m, friendnumber); 299 return m_get_last_online(m, friendnumber);
@@ -285,7 +305,7 @@ uint64_t tox_get_last_online(Tox *tox, int friendnumber)
285 * returns 0 on success. 305 * returns 0 on success.
286 * returns -1 on failure. 306 * returns -1 on failure.
287 */ 307 */
288int tox_set_user_is_typing(Tox *tox, int friendnumber, uint8_t is_typing) 308int tox_set_user_is_typing(Tox *tox, int32_t friendnumber, uint8_t is_typing)
289{ 309{
290 Messenger *m = tox; 310 Messenger *m = tox;
291 return m_set_usertyping(m, friendnumber, is_typing); 311 return m_set_usertyping(m, friendnumber, is_typing);
@@ -296,7 +316,7 @@ int tox_set_user_is_typing(Tox *tox, int friendnumber, uint8_t is_typing)
296 * returns 0 if friend is not typing. 316 * returns 0 if friend is not typing.
297 * returns 1 if friend is typing. 317 * returns 1 if friend is typing.
298 */ 318 */
299int tox_get_is_typing(Tox *tox, int friendnumber) 319int tox_get_is_typing(Tox *tox, int32_t friendnumber)
300{ 320{
301 Messenger *m = tox; 321 Messenger *m = tox;
302 return m_get_istyping(m, friendnumber); 322 return m_get_istyping(m, friendnumber);
@@ -305,7 +325,7 @@ int tox_get_is_typing(Tox *tox, int friendnumber)
305/* Sets whether we send read receipts for friendnumber. 325/* Sets whether we send read receipts for friendnumber.
306 * This function is not lazy, and it will fail if yesno is not (0 or 1). 326 * This function is not lazy, and it will fail if yesno is not (0 or 1).
307 */ 327 */
308void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno) 328void tox_set_sends_receipts(Tox *tox, int32_t friendnumber, int yesno)
309{ 329{
310 Messenger *m = tox; 330 Messenger *m = tox;
311 m_set_sends_receipts(m, friendnumber, yesno); 331 m_set_sends_receipts(m, friendnumber, yesno);
@@ -332,7 +352,7 @@ uint32_t tox_get_num_online_friends(Tox *tox)
332 * Otherwise, returns the number of elements copied. 352 * Otherwise, returns the number of elements copied.
333 * If the array was too small, the contents 353 * If the array was too small, the contents
334 * of out_list will be truncated to list_size. */ 354 * of out_list will be truncated to list_size. */
335uint32_t tox_get_friendlist(Tox *tox, int *out_list, uint32_t list_size) 355uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size)
336{ 356{
337 Messenger *m = tox; 357 Messenger *m = tox;
338 return copy_friendlist(m, out_list, list_size); 358 return copy_friendlist(m, out_list, list_size);
@@ -341,7 +361,8 @@ uint32_t tox_get_friendlist(Tox *tox, int *out_list, uint32_t list_size)
341/* Set the function that will be executed when a friend request is received. 361/* Set the function that will be executed when a friend request is received.
342 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 362 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
343 */ 363 */
344void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) 364void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, uint8_t *, uint8_t *, uint16_t, void *),
365 void *userdata)
345{ 366{
346 Messenger *m = tox; 367 Messenger *m = tox;
347 m_callback_friendrequest(m, function, userdata); 368 m_callback_friendrequest(m, function, userdata);
@@ -349,9 +370,9 @@ void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t *
349 370
350 371
351/* Set the function that will be executed when a message from a friend is received. 372/* Set the function that will be executed when a message from a friend is received.
352 * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) 373 * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length)
353 */ 374 */
354void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 375void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *),
355 void *userdata) 376 void *userdata)
356{ 377{
357 Messenger *m = tox; 378 Messenger *m = tox;
@@ -359,9 +380,9 @@ void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int,
359} 380}
360 381
361/* Set the function that will be executed when an action from a friend is received. 382/* Set the function that will be executed when an action from a friend is received.
362 * function format is: function(int friendnumber, uint8_t * action, uint32_t length) 383 * function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length)
363 */ 384 */
364void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 385void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *),
365 void *userdata) 386 void *userdata)
366{ 387{
367 Messenger *m = tox; 388 Messenger *m = tox;
@@ -369,10 +390,10 @@ void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int,
369} 390}
370 391
371/* Set the callback for name changes. 392/* Set the callback for name changes.
372 * function(int friendnumber, uint8_t *newname, uint16_t length) 393 * function(int32_t friendnumber, uint8_t *newname, uint16_t length)
373 * You are not responsible for freeing newname. 394 * You are not responsible for freeing newname.
374 */ 395 */
375void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 396void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *),
376 void *userdata) 397 void *userdata)
377{ 398{
378 Messenger *m = tox; 399 Messenger *m = tox;
@@ -380,10 +401,10 @@ void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int, ui
380} 401}
381 402
382/* Set the callback for status message changes. 403/* Set the callback for status message changes.
383 * function(int friendnumber, uint8_t *newstatus, uint16_t length) 404 * function(int32_t friendnumber, uint8_t *newstatus, uint16_t length)
384 * You are not responsible for freeing newstatus. 405 * You are not responsible for freeing newstatus.
385 */ 406 */
386void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 407void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *),
387 void *userdata) 408 void *userdata)
388{ 409{
389 Messenger *m = tox; 410 Messenger *m = tox;
@@ -391,25 +412,26 @@ void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int,
391} 412}
392 413
393/* Set the callback for status type changes. 414/* Set the callback for status type changes.
394 * function(int friendnumber, USERSTATUS kind) 415 * function(int32_t friendnumber, USERSTATUS kind)
395 */ 416 */
396void tox_callback_user_status(Tox *tox, void (*function)(Messenger *tox, int, TOX_USERSTATUS, void *), void *userdata) 417void tox_callback_user_status(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, void *),
418 void *userdata)
397{ 419{
398 Messenger *m = tox; 420 Messenger *m = tox;
399 m_callback_userstatus(m, function, userdata); 421 m_callback_userstatus(m, function, userdata);
400} 422}
401 423
402/* Set the callback for typing changes. 424/* Set the callback for typing changes.
403 * function (int friendnumber, int is_typing) 425 * function (int32_t friendnumber, int is_typing)
404 */ 426 */
405void tox_callback_typing_change(Tox *tox, void (*function)(Messenger *tox, int, int, void *), void *userdata) 427void tox_callback_typing_change(Tox *tox, void (*function)(Messenger *tox, int32_t, int, void *), void *userdata)
406{ 428{
407 Messenger *m = tox; 429 Messenger *m = tox;
408 m_callback_typingchange(m, function, userdata); 430 m_callback_typingchange(m, function, userdata);
409} 431}
410 432
411/* Set the callback for read receipts. 433/* Set the callback for read receipts.
412 * function(int friendnumber, uint32_t receipt) 434 * function(int32_t friendnumber, uint32_t receipt)
413 * 435 *
414 * If you are keeping a record of returns from m_sendmessage; 436 * If you are keeping a record of returns from m_sendmessage;
415 * receipt might be one of those values, meaning the message 437 * receipt might be one of those values, meaning the message
@@ -417,14 +439,14 @@ void tox_callback_typing_change(Tox *tox, void (*function)(Messenger *tox, int,
417 * Since core doesn't track ids for you, receipt may not correspond to any message. 439 * Since core doesn't track ids for you, receipt may not correspond to any message.
418 * in that case, you should discard it. 440 * in that case, you should discard it.
419 */ 441 */
420void tox_callback_read_receipt(Tox *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) 442void tox_callback_read_receipt(Tox *tox, void (*function)(Messenger *tox, int32_t, uint32_t, void *), void *userdata)
421{ 443{
422 Messenger *m = tox; 444 Messenger *m = tox;
423 m_callback_read_receipt(m, function, userdata); 445 m_callback_read_receipt(m, function, userdata);
424} 446}
425 447
426/* Set the callback for connection status changes. 448/* Set the callback for connection status changes.
427 * function(int friendnumber, uint8_t status) 449 * function(int32_t friendnumber, uint8_t status)
428 * 450 *
429 * Status: 451 * Status:
430 * 0 -- friend went offline after being previously online 452 * 0 -- friend went offline after being previously online
@@ -434,7 +456,8 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Messenger *tox, int, u
434 * being previously online" part. It's assumed that when adding friends, 456 * being previously online" part. It's assumed that when adding friends,
435 * their connection status is offline. 457 * their connection status is offline.
436 */ 458 */
437void tox_callback_connection_status(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) 459void tox_callback_connection_status(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, void *),
460 void *userdata)
438{ 461{
439 Messenger *m = tox; 462 Messenger *m = tox;
440 m_callback_connectionstatus(m, function, userdata); 463 m_callback_connectionstatus(m, function, userdata);
@@ -444,9 +467,9 @@ void tox_callback_connection_status(Tox *tox, void (*function)(Messenger *tox, i
444 467
445/* Set the callback for group invites. 468/* Set the callback for group invites.
446 * 469 *
447 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata) 470 * Function(Tox *tox, int32_t friendnumber, uint8_t *group_public_key, void *userdata)
448 */ 471 */
449void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, void *), void *userdata) 472void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, void *), void *userdata)
450{ 473{
451 Messenger *m = tox; 474 Messenger *m = tox;
452 m_callback_group_invite(m, function, userdata); 475 m_callback_group_invite(m, function, userdata);
@@ -521,7 +544,7 @@ int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name)
521 * return 0 on success 544 * return 0 on success
522 * return -1 on failure 545 * return -1 on failure
523 */ 546 */
524int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber) 547int tox_invite_friend(Tox *tox, int32_t friendnumber, int groupnumber)
525{ 548{
526 Messenger *m = tox; 549 Messenger *m = tox;
527 return invite_friend(m, friendnumber, groupnumber); 550 return invite_friend(m, friendnumber, groupnumber);
@@ -531,7 +554,7 @@ int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber)
531 * returns group number on success 554 * returns group number on success
532 * returns -1 on failure. 555 * returns -1 on failure.
533 */ 556 */
534int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_key) 557int tox_join_groupchat(Tox *tox, int32_t friendnumber, uint8_t *friend_group_public_key)
535{ 558{
536 Messenger *m = tox; 559 Messenger *m = tox;
537 return join_groupchat(m, friendnumber, friend_group_public_key); 560 return join_groupchat(m, friendnumber, friend_group_public_key);
@@ -606,9 +629,9 @@ uint32_t tox_get_chatlist(Tox *tox, int *out_list, uint32_t list_size)
606 629
607/* Set the callback for file send requests. 630/* Set the callback for file send requests.
608 * 631 *
609 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) 632 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
610 */ 633 */
611void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, uint64_t, uint8_t *, 634void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint64_t, uint8_t *,
612 uint16_t, 635 uint16_t,
613 void *), void *userdata) 636 void *), void *userdata)
614{ 637{
@@ -617,10 +640,10 @@ void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, i
617} 640}
618/* Set the callback for file control requests. 641/* Set the callback for file control requests.
619 * 642 *
620 * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 643 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
621 * 644 *
622 */ 645 */
623void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, uint8_t, uint8_t, uint8_t *, 646void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *,
624 uint16_t, void *), void *userdata) 647 uint16_t, void *), void *userdata)
625{ 648{
626 Messenger *m = tox; 649 Messenger *m = tox;
@@ -628,10 +651,10 @@ void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int, u
628} 651}
629/* Set the callback for file data. 652/* Set the callback for file data.
630 * 653 *
631 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 654 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
632 * 655 *
633 */ 656 */
634void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, uint8_t *, uint16_t length, 657void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint8_t *, uint16_t length,
635 void *), 658 void *),
636 void *userdata) 659 void *userdata)
637 660
@@ -644,7 +667,7 @@ void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int, uint
644 * return file number on success 667 * return file number on success
645 * return -1 on failure 668 * return -1 on failure
646 */ 669 */
647int tox_new_file_sender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) 670int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length)
648{ 671{
649 Messenger *m = tox; 672 Messenger *m = tox;
650 return new_filesender(m, friendnumber, filesize, filename, filename_length); 673 return new_filesender(m, friendnumber, filesize, filename, filename_length);
@@ -655,7 +678,7 @@ int tox_new_file_sender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *
655 * return 0 on success 678 * return 0 on success
656 * return -1 on failure 679 * return -1 on failure
657 */ 680 */
658int tox_file_send_control(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, 681int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
659 uint8_t *data, uint16_t length) 682 uint8_t *data, uint16_t length)
660{ 683{
661 Messenger *m = tox; 684 Messenger *m = tox;
@@ -666,7 +689,7 @@ int tox_file_send_control(Tox *tox, int friendnumber, uint8_t send_receive, uint
666 * return 0 on success 689 * return 0 on success
667 * return -1 on failure 690 * return -1 on failure
668 */ 691 */
669int tox_file_send_data(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) 692int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length)
670{ 693{
671 Messenger *m = tox; 694 Messenger *m = tox;
672 return file_data(m, friendnumber, filenumber, data, length); 695 return file_data(m, friendnumber, filenumber, data, length);
@@ -677,7 +700,7 @@ int tox_file_send_data(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *
677 * return size on success 700 * return size on success
678 * return -1 on failure (currently will never return -1) 701 * return -1 on failure (currently will never return -1)
679 */ 702 */
680int tox_file_data_size(Tox *tox, int friendnumber) 703int tox_file_data_size(Tox *tox, int32_t friendnumber)
681{ 704{
682 return MAX_DATA_SIZE - crypto_box_MACBYTES - 3; 705 return MAX_DATA_SIZE - crypto_box_MACBYTES - 3;
683} 706}
@@ -689,7 +712,7 @@ int tox_file_data_size(Tox *tox, int friendnumber)
689 * return number of bytes remaining to be sent/received on success 712 * return number of bytes remaining to be sent/received on success
690 * return 0 on failure 713 * return 0 on failure
691 */ 714 */
692uint64_t tox_file_data_remaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive) 715uint64_t tox_file_data_remaining(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive)
693{ 716{
694 Messenger *m = tox; 717 Messenger *m = tox;
695 return file_dataremaining(m, friendnumber, filenumber, send_receive); 718 return file_dataremaining(m, friendnumber, filenumber, send_receive);
@@ -753,22 +776,27 @@ void tox_do(Tox *tox)
753/* 776/*
754 * functions to avoid excessive polling 777 * functions to avoid excessive polling
755 */ 778 */
756int tox_wait_prepare(Tox *tox, uint8_t *data, uint16_t *lenptr) 779
780size_t tox_wait_data_size()
757{ 781{
758 Messenger *m = tox; 782 return wait_data_size();
759 return wait_prepare_messenger(m, data, lenptr);
760} 783}
761 784
762int tox_wait_execute(Tox *tox, uint8_t *data, uint16_t len, uint16_t milliseconds) 785int tox_wait_prepare(Tox *tox, uint8_t *data)
763{ 786{
764 Messenger *m = tox; 787 Messenger *m = tox;
765 return wait_execute_messenger(m, data, len, milliseconds); 788 return wait_prepare_messenger(m, data);
789}
790
791int tox_wait_execute(uint8_t *data, long seconds, long microseconds)
792{
793 return wait_execute_messenger(data, seconds, microseconds);
766} 794}
767 795
768void tox_wait_cleanup(Tox *tox, uint8_t *data, uint16_t len) 796int tox_wait_cleanup(Tox *tox, uint8_t *data)
769{ 797{
770 Messenger *m = tox; 798 Messenger *m = tox;
771 wait_cleanup_messenger(m, data, len); 799 return wait_cleanup_messenger(m, data);
772} 800}
773 801
774/* SAVING AND LOADING FUNCTIONS: */ 802/* SAVING AND LOADING FUNCTIONS: */
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 1bc16c32..256c701a 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -36,8 +36,6 @@
36#include <windows.h> 36#include <windows.h>
37#include <ws2tcpip.h> 37#include <ws2tcpip.h>
38 38
39/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */
40typedef short sa_family_t;
41 39
42#ifndef true 40#ifndef true
43#define true 1 41#define true 1
@@ -58,7 +56,7 @@ extern "C" {
58#endif 56#endif
59 57
60#define TOX_MAX_NAME_LENGTH 128 58#define TOX_MAX_NAME_LENGTH 128
61#define TOX_MAX_STATUSMESSAGE_LENGTH 128 59#define TOX_MAX_STATUSMESSAGE_LENGTH 1007
62#define TOX_CLIENT_ID_SIZE 32 60#define TOX_CLIENT_ID_SIZE 32
63 61
64#define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) 62#define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t))
@@ -67,37 +65,8 @@ extern "C" {
67#define TOX_PORTRANGE_TO 33545 65#define TOX_PORTRANGE_TO 33545
68#define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM 66#define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM
69 67
70typedef union {
71 uint8_t c[4];
72 uint16_t s[2];
73 uint32_t i;
74} tox_IP4;
75
76typedef union {
77 uint8_t uint8[16];
78 uint16_t uint16[8];
79 uint32_t uint32[4];
80 struct in6_addr in6_addr;
81} tox_IP6;
82
83typedef struct {
84 sa_family_t family;
85 union {
86 tox_IP4 ip4;
87 tox_IP6 ip6;
88 };
89} tox_IP;
90
91/* will replace IP_Port as soon as the complete infrastructure is in place
92 * removed the unused union and padding also */
93typedef struct {
94 tox_IP ip;
95 uint16_t port;
96} tox_IP_Port;
97
98#define TOX_ENABLE_IPV6_DEFAULT 1 68#define TOX_ENABLE_IPV6_DEFAULT 1
99 69
100
101/* Errors for m_addfriend 70/* Errors for m_addfriend
102 * FAERR - Friend Add Error 71 * FAERR - Friend Add Error
103 */ 72 */
@@ -128,11 +97,11 @@ TOX_USERSTATUS;
128typedef struct Tox Tox; 97typedef struct Tox Tox;
129#endif 98#endif
130 99
131/* NOTE: Strings in Tox are all UTF-8, also the last byte in all strings must be NULL (0). 100/* NOTE: Strings in Tox are all UTF-8, (This means that there is no terminating NULL character.)
132 * 101 *
133 * The length when passing those strings to the core includes that NULL character. 102 * The exact buffer you send will be received at the other end without modification.
134 * 103 *
135 * If you send non NULL terminated strings Tox will force NULL terminates them when it receives them. 104 * Do not treat Tox strings as C strings.
136 */ 105 */
137 106
138/* return TOX_FRIEND_ADDRESS_SIZE byte address to give to others. 107/* return TOX_FRIEND_ADDRESS_SIZE byte address to give to others.
@@ -156,28 +125,32 @@ void tox_get_address(Tox *tox, uint8_t *address);
156 * (the nospam for that friend was set to the new one). 125 * (the nospam for that friend was set to the new one).
157 * return TOX_FAERR_NOMEM if increasing the friend list size fails. 126 * return TOX_FAERR_NOMEM if increasing the friend list size fails.
158 */ 127 */
159int tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); 128int32_t tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length);
160 129
161 130
162/* Add a friend without sending a friendrequest. 131/* Add a friend without sending a friendrequest.
163 * return the friend number if success. 132 * return the friend number if success.
164 * return -1 if failure. 133 * return -1 if failure.
165 */ 134 */
166int tox_add_friend_norequest(Tox *tox, uint8_t *client_id); 135int32_t tox_add_friend_norequest(Tox *tox, uint8_t *client_id);
167 136
168/* return the friend id associated to that client id. 137/* return the friend number associated to that client id.
169 return -1 if no such friend */ 138 return -1 if no such friend */
170int tox_get_friend_id(Tox *tox, uint8_t *client_id); 139int32_t tox_get_friend_number(Tox *tox, uint8_t *client_id);
171 140
172/* Copies the public key associated to that friend id into client_id buffer. 141/* Copies the public key associated to that friend id into client_id buffer.
173 * Make sure that client_id is of size CLIENT_ID_SIZE. 142 * Make sure that client_id is of size CLIENT_ID_SIZE.
174 * return 0 if success. 143 * return 0 if success.
175 * return -1 if failure. 144 * return -1 if failure.
176 */ 145 */
177int tox_get_client_id(Tox *tox, int friend_id, uint8_t *client_id); 146int tox_get_client_id(Tox *tox, int32_t friendnumber, uint8_t *client_id);
178 147
179/* Remove a friend. */ 148/* Remove a friend.
180int tox_del_friend(Tox *tox, int friendnumber); 149 *
150 * return 0 if success.
151 * return -1 if failure.
152 */
153int tox_del_friend(Tox *tox, int32_t friendnumber);
181 154
182/* Checks friend's connecting status. 155/* Checks friend's connecting status.
183 * 156 *
@@ -185,14 +158,14 @@ int tox_del_friend(Tox *tox, int friendnumber);
185 * return 0 if friend is not connected to us (Offline). 158 * return 0 if friend is not connected to us (Offline).
186 * return -1 on failure. 159 * return -1 on failure.
187 */ 160 */
188int tox_get_friend_connection_status(Tox *tox, int friendnumber); 161int tox_get_friend_connection_status(Tox *tox, int32_t friendnumber);
189 162
190/* Checks if there exists a friend with given friendnumber. 163/* Checks if there exists a friend with given friendnumber.
191 * 164 *
192 * return 1 if friend exists. 165 * return 1 if friend exists.
193 * return 0 if friend doesn't exist. 166 * return 0 if friend doesn't exist.
194 */ 167 */
195int tox_friend_exists(Tox *tox, int friendnumber); 168int tox_friend_exists(Tox *tox, int32_t friendnumber);
196 169
197/* Send a text chat message to an online friend. 170/* Send a text chat message to an online friend.
198 * 171 *
@@ -204,8 +177,8 @@ int tox_friend_exists(Tox *tox, int friendnumber);
204 * m_sendmessage_withid will send a message with the id of your choosing, 177 * m_sendmessage_withid will send a message with the id of your choosing,
205 * however we can generate an id for you by calling plain m_sendmessage. 178 * however we can generate an id for you by calling plain m_sendmessage.
206 */ 179 */
207uint32_t tox_send_message(Tox *tox, int friendnumber, uint8_t *message, uint32_t length); 180uint32_t tox_send_message(Tox *tox, int32_t friendnumber, uint8_t *message, uint32_t length);
208uint32_t tox_send_message_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); 181uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
209 182
210/* Send an action to an online friend. 183/* Send an action to an online friend.
211 * 184 *
@@ -217,8 +190,8 @@ uint32_t tox_send_message_withid(Tox *tox, int friendnumber, uint32_t theid, uin
217 * m_sendaction_withid will send an action message with the id of your choosing, 190 * m_sendaction_withid will send an action message with the id of your choosing,
218 * however we can generate an id for you by calling plain m_sendaction. 191 * however we can generate an id for you by calling plain m_sendaction.
219 */ 192 */
220uint32_t tox_send_action(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); 193uint32_t tox_send_action(Tox *tox, int32_t friendnumber, uint8_t *action, uint32_t length);
221uint32_t tox_send_action_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length); 194uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length);
222 195
223/* Set our nickname. 196/* Set our nickname.
224 * name must be a string of maximum MAX_NAME_LENGTH length. 197 * name must be a string of maximum MAX_NAME_LENGTH length.
@@ -233,34 +206,42 @@ int tox_set_name(Tox *tox, uint8_t *name, uint16_t length);
233/* 206/*
234 * Get your nickname. 207 * Get your nickname.
235 * m - The messanger context to use. 208 * m - The messanger context to use.
236 * name - Pointer to a string for the name. 209 * name - needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
237 * nlen - The length of the string buffer.
238 * 210 *
239 * return length of name. 211 * return length of name.
240 * return 0 on error. 212 * return 0 on error.
241 */ 213 */
242uint16_t tox_get_self_name(Tox *tox, uint8_t *name, uint16_t nlen); 214uint16_t tox_get_self_name(Tox *tox, uint8_t *name);
243 215
244/* Get name of friendnumber and put it in name. 216/* Get name of friendnumber and put it in name.
245 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 217 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
246 * 218 *
247 * return length of name (with the NULL terminator) if success. 219 * return length of name if success.
248 * return -1 if failure. 220 * return -1 if failure.
249 */ 221 */
250int tox_get_name(Tox *tox, int friendnumber, uint8_t *name); 222int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name);
223
224/* returns the length of name on success.
225 * returns -1 on failure.
226 */
227int tox_get_name_size(Tox *tox, int32_t friendnumber);
228int tox_get_self_name_size(Tox *tox);
251 229
252/* Set our user status. 230/* Set our user status.
253 * 231 *
232 * userstatus must be one of TOX_USERSTATUS values.
233 *
254 * returns 0 on success. 234 * returns 0 on success.
255 * returns -1 on failure. 235 * returns -1 on failure.
256 */ 236 */
257int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length); 237int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length);
258int tox_set_user_status(Tox *tox, TOX_USERSTATUS status); 238int tox_set_user_status(Tox *tox, uint8_t userstatus);
259 239
260/* return the length of friendnumber's status message, including null. 240/* returns the length of status message on success.
261 * Pass it into malloc 241 * returns -1 on failure.
262 */ 242 */
263int tox_get_status_message_size(Tox *tox, int friendnumber); 243int tox_get_status_message_size(Tox *tox, int32_t friendnumber);
244int tox_get_self_status_message_size(Tox *tox);
264 245
265/* Copy friendnumber's status message into buf, truncating if size is over maxlen. 246/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
266 * Get the size you need to allocate from m_get_statusmessage_size. 247 * Get the size you need to allocate from m_get_statusmessage_size.
@@ -269,22 +250,22 @@ int tox_get_status_message_size(Tox *tox, int friendnumber);
269 * returns the length of the copied data on success 250 * returns the length of the copied data on success
270 * retruns -1 on failure. 251 * retruns -1 on failure.
271 */ 252 */
272int tox_get_status_message(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); 253int tox_get_status_message(Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen);
273int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen); 254int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen);
274 255
275/* return one of USERSTATUS values. 256/* return one of TOX_USERSTATUS values.
276 * Values unknown to your application should be represented as USERSTATUS_NONE. 257 * Values unknown to your application should be represented as TOX_USERSTATUS_NONE.
277 * As above, the self variant will return our own USERSTATUS. 258 * As above, the self variant will return our own TOX_USERSTATUS.
278 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. 259 * If friendnumber is invalid, this shall return TOX_USERSTATUS_INVALID.
279 */ 260 */
280TOX_USERSTATUS tox_get_user_status(Tox *tox, int friendnumber); 261uint8_t tox_get_user_status(Tox *tox, int32_t friendnumber);
281TOX_USERSTATUS tox_get_self_user_status(Tox *tox); 262uint8_t tox_get_self_user_status(Tox *tox);
282 263
283 264
284/* returns timestamp of last time friendnumber was seen online, or 0 if never seen. 265/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
285 * returns -1 on error. 266 * returns -1 on error.
286 */ 267 */
287uint64_t tox_get_last_online(Tox *tox, int friendnumber); 268uint64_t tox_get_last_online(Tox *tox, int32_t friendnumber);
288 269
289/* Set our typing status for a friend. 270/* Set our typing status for a friend.
290 * You are responsible for turning it on or off. 271 * You are responsible for turning it on or off.
@@ -292,19 +273,19 @@ uint64_t tox_get_last_online(Tox *tox, int friendnumber);
292 * returns 0 on success. 273 * returns 0 on success.
293 * returns -1 on failure. 274 * returns -1 on failure.
294 */ 275 */
295int tox_set_user_is_typing(Tox *tox, int friendnumber, uint8_t is_typing); 276int tox_set_user_is_typing(Tox *tox, int32_t friendnumber, uint8_t is_typing);
296 277
297/* Get the typing status of a friend. 278/* Get the typing status of a friend.
298 * 279 *
299 * returns 0 if friend is not typing. 280 * returns 0 if friend is not typing.
300 * returns 1 if friend is typing. 281 * returns 1 if friend is typing.
301 */ 282 */
302int tox_get_is_typing(Tox *tox, int friendnumber); 283int tox_get_is_typing(Tox *tox, int32_t friendnumber);
303 284
304/* Sets whether we send read receipts for friendnumber. 285/* Sets whether we send read receipts for friendnumber.
305 * This function is not lazy, and it will fail if yesno is not (0 or 1). 286 * This function is not lazy, and it will fail if yesno is not (0 or 1).
306 */ 287 */
307void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); 288void tox_set_sends_receipts(Tox *tox, int32_t friendnumber, int yesno);
308 289
309/* Return the number of friends in the instance m. 290/* Return the number of friends in the instance m.
310 * You should use this to determine how much memory to allocate 291 * You should use this to determine how much memory to allocate
@@ -319,50 +300,52 @@ uint32_t tox_get_num_online_friends(Tox *tox);
319 * Otherwise, returns the number of elements copied. 300 * Otherwise, returns the number of elements copied.
320 * If the array was too small, the contents 301 * If the array was too small, the contents
321 * of out_list will be truncated to list_size. */ 302 * of out_list will be truncated to list_size. */
322uint32_t tox_get_friendlist(Tox *tox, int *out_list, uint32_t list_size); 303uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size);
323 304
324/* Set the function that will be executed when a friend request is received. 305/* Set the function that will be executed when a friend request is received.
325 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 306 * Function format is function(Tox *tox, uint8_t * public_key, uint8_t * data, uint16_t length, void *userdata)
326 */ 307 */
327void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); 308void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, uint8_t *, uint8_t *, uint16_t, void *),
309 void *userdata);
328 310
329/* Set the function that will be executed when a message from a friend is received. 311/* Set the function that will be executed when a message from a friend is received.
330 * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) 312 * Function format is: function(Tox *tox, int friendnumber, uint8_t * message, uint32_t length, void *userdata)
331 */ 313 */
332void tox_callback_friend_message(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), 314void tox_callback_friend_message(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *),
333 void *userdata); 315 void *userdata);
334 316
335/* Set the function that will be executed when an action from a friend is received. 317/* Set the function that will be executed when an action from a friend is received.
336 * Function format is: function(int friendnumber, uint8_t * action, uint32_t length) 318 * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * action, uint32_t length, void *userdata)
337 */ 319 */
338void tox_callback_friend_action(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); 320void tox_callback_friend_action(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *),
321 void *userdata);
339 322
340/* Set the callback for name changes. 323/* Set the callback for name changes.
341 * function(int friendnumber, uint8_t *newname, uint16_t length) 324 * function(Tox *tox, int32_t friendnumber, uint8_t *newname, uint16_t length, void *userdata)
342 * You are not responsible for freeing newname 325 * You are not responsible for freeing newname
343 */ 326 */
344void tox_callback_name_change(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), 327void tox_callback_name_change(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *),
345 void *userdata); 328 void *userdata);
346 329
347/* Set the callback for status message changes. 330/* Set the callback for status message changes.
348 * function(int friendnumber, uint8_t *newstatus, uint16_t length) 331 * function(Tox *tox, int32_t friendnumber, uint8_t *newstatus, uint16_t length, void *userdata)
349 * You are not responsible for freeing newstatus. 332 * You are not responsible for freeing newstatus.
350 */ 333 */
351void tox_callback_status_message(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), 334void tox_callback_status_message(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *),
352 void *userdata); 335 void *userdata);
353 336
354/* Set the callback for status type changes. 337/* Set the callback for status type changes.
355 * function(int friendnumber, USERSTATUS kind) 338 * function(Tox *tox, int32_t friendnumber, uint8_t TOX_USERSTATUS, void *userdata)
356 */ 339 */
357void tox_callback_user_status(Tox *tox, void (*function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata); 340void tox_callback_user_status(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t, void *), void *userdata);
358 341
359/* Set the callback for typing changes. 342/* Set the callback for typing changes.
360 * function (int friendnumber, int is_typing) 343 * function (Tox *tox, int32_t friendnumber, int is_typing, void *userdata)
361 */ 344 */
362void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int, int, void *), void *userdata); 345void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int32_t, int, void *), void *userdata);
363 346
364/* Set the callback for read receipts. 347/* Set the callback for read receipts.
365 * function(int friendnumber, uint32_t receipt) 348 * function(Tox *tox, int32_t friendnumber, uint32_t receipt, void *userdata)
366 * 349 *
367 * If you are keeping a record of returns from m_sendmessage; 350 * If you are keeping a record of returns from m_sendmessage;
368 * receipt might be one of those values, meaning the message 351 * receipt might be one of those values, meaning the message
@@ -370,10 +353,10 @@ void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int, int, v
370 * Since core doesn't track ids for you, receipt may not correspond to any message. 353 * Since core doesn't track ids for you, receipt may not correspond to any message.
371 * In that case, you should discard it. 354 * In that case, you should discard it.
372 */ 355 */
373void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_t, void *), void *userdata); 356void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int32_t, uint32_t, void *), void *userdata);
374 357
375/* Set the callback for connection status changes. 358/* Set the callback for connection status changes.
376 * function(int friendnumber, uint8_t status) 359 * function(Tox *tox, int32_t friendnumber, uint8_t status, void *userdata)
377 * 360 *
378 * Status: 361 * Status:
379 * 0 -- friend went offline after being previously online 362 * 0 -- friend went offline after being previously online
@@ -383,7 +366,7 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_
383 * being previously online" part. it's assumed that when adding friends, 366 * being previously online" part. it's assumed that when adding friends,
384 * their connection status is offline. 367 * their connection status is offline.
385 */ 368 */
386void tox_callback_connection_status(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); 369void tox_callback_connection_status(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t, void *), void *userdata);
387 370
388/**********GROUP CHAT FUNCTIONS: WARNING WILL BREAK A LOT************/ 371/**********GROUP CHAT FUNCTIONS: WARNING WILL BREAK A LOT************/
389 372
@@ -391,7 +374,7 @@ void tox_callback_connection_status(Tox *tox, void (*function)(Tox *tox, int, ui
391 * 374 *
392 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata) 375 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata)
393 */ 376 */
394void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, void *), void *userdata); 377void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, void *), void *userdata);
395 378
396/* Set the callback for group messages. 379/* Set the callback for group messages.
397 * 380 *
@@ -447,14 +430,14 @@ int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name)
447 * return 0 on success 430 * return 0 on success
448 * return -1 on failure 431 * return -1 on failure
449 */ 432 */
450int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber); 433int tox_invite_friend(Tox *tox, int32_t friendnumber, int groupnumber);
451 434
452/* Join a group (you need to have been invited first.) 435/* Join a group (you need to have been invited first.)
453 * 436 *
454 * returns group number on success 437 * returns group number on success
455 * returns -1 on failure. 438 * returns -1 on failure.
456 */ 439 */
457int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_key); 440int tox_join_groupchat(Tox *tox, int32_t friendnumber, uint8_t *friend_group_public_key);
458 441
459/* send a group message 442/* send a group message
460 * return 0 on success 443 * return 0 on success
@@ -533,9 +516,9 @@ enum {
533}; 516};
534/* Set the callback for file send requests. 517/* Set the callback for file send requests.
535 * 518 *
536 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) 519 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
537 */ 520 */
538void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, 521void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint64_t, uint8_t *, uint16_t,
539 void *), void *userdata); 522 void *), void *userdata);
540 523
541/* Set the callback for file control requests. 524/* Set the callback for file control requests.
@@ -543,18 +526,18 @@ void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int, uint
543 * receive_send is 1 if the message is for a slot on which we are currently sending a file and 0 if the message 526 * receive_send is 1 if the message is for a slot on which we are currently sending a file and 0 if the message
544 * is for a slot on which we are receiving the file 527 * is for a slot on which we are receiving the file
545 * 528 *
546 * Function(Tox *tox, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 529 * Function(Tox *tox, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
547 * 530 *
548 */ 531 */
549void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, 532void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *,
550 uint16_t, void *), void *userdata); 533 uint16_t, void *), void *userdata);
551 534
552/* Set the callback for file data. 535/* Set the callback for file data.
553 * 536 *
554 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 537 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
555 * 538 *
556 */ 539 */
557void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint8_t *, uint16_t length, void *), 540void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint8_t *, uint16_t length, void *),
558 void *userdata); 541 void *userdata);
559 542
560 543
@@ -563,7 +546,7 @@ void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int, uint8_t, uin
563 * return file number on success 546 * return file number on success
564 * return -1 on failure 547 * return -1 on failure
565 */ 548 */
566int tox_new_file_sender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); 549int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length);
567 550
568/* Send a file control request. 551/* Send a file control request.
569 * 552 *
@@ -573,7 +556,7 @@ int tox_new_file_sender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *
573 * return 0 on success 556 * return 0 on success
574 * return -1 on failure 557 * return -1 on failure
575 */ 558 */
576int tox_file_send_control(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, 559int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
577 uint8_t *data, uint16_t length); 560 uint8_t *data, uint16_t length);
578 561
579/* Send file data. 562/* Send file data.
@@ -581,14 +564,14 @@ int tox_file_send_control(Tox *tox, int friendnumber, uint8_t send_receive, uint
581 * return 0 on success 564 * return 0 on success
582 * return -1 on failure 565 * return -1 on failure
583 */ 566 */
584int tox_file_send_data(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); 567int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length);
585 568
586/* Returns the recommended/maximum size of the filedata you send with tox_file_send_data() 569/* Returns the recommended/maximum size of the filedata you send with tox_file_send_data()
587 * 570 *
588 * return size on success 571 * return size on success
589 * return -1 on failure (currently will never return -1) 572 * return -1 on failure (currently will never return -1)
590 */ 573 */
591int tox_file_data_size(Tox *tox, int friendnumber); 574int tox_file_data_size(Tox *tox, int32_t friendnumber);
592 575
593/* Give the number of bytes left to be sent/received. 576/* Give the number of bytes left to be sent/received.
594 * 577 *
@@ -597,19 +580,51 @@ int tox_file_data_size(Tox *tox, int friendnumber);
597 * return number of bytes remaining to be sent/received on success 580 * return number of bytes remaining to be sent/received on success
598 * return 0 on failure 581 * return 0 on failure
599 */ 582 */
600uint64_t tox_file_data_remaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive); 583uint64_t tox_file_data_remaining(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive);
601 584
602/***************END OF FILE SENDING FUNCTIONS******************/ 585/***************END OF FILE SENDING FUNCTIONS******************/
603 586
587/* WARNING: DEPRECATED, DO NOT USE. */
588typedef union {
589 uint8_t c[4];
590 uint16_t s[2];
591 uint32_t i;
592} tox_IP4;
604 593
605/* 594typedef union {
606 * Use these two functions to bootstrap the client. 595 uint8_t uint8[16];
607 */ 596 uint16_t uint16[8];
597 uint32_t uint32[4];
598 struct in6_addr in6_addr;
599} tox_IP6;
600
601typedef struct {
602 uint8_t family;
603 /* Not used for anything right now. */
604 uint8_t padding[3];
605 union {
606 tox_IP4 ip4;
607 tox_IP6 ip6;
608 };
609} tox_IP;
610
611/* will replace IP_Port as soon as the complete infrastructure is in place
612 * removed the unused union and padding also */
613typedef struct {
614 tox_IP ip;
615 uint16_t port;
616} tox_IP_Port;
617/* WARNING: DEPRECATED, DO NOT USE. */
608/* Sends a "get nodes" request to the given node with ip, port and public_key 618/* Sends a "get nodes" request to the given node with ip, port and public_key
609 * to setup connections 619 * to setup connections
610 */ 620 */
611void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); 621void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key);
612 622
623
624/*
625 * Use this function to bootstrap the client.
626 */
627
613/* Resolves address into an IP address. If successful, sends a "get nodes" 628/* Resolves address into an IP address. If successful, sends a "get nodes"
614 * request to the given node with ip, port (in network byte order, HINT: use htons()) 629 * request to the given node with ip, port (in network byte order, HINT: use htons())
615 * and public_key to setup connections 630 * and public_key to setup connections
@@ -653,37 +668,43 @@ void tox_kill(Tox *tox);
653void tox_do(Tox *tox); 668void tox_do(Tox *tox);
654 669
655/* 670/*
656 * tox_wait_prepare(): function should be called under lock 671 * tox_wait_data_size():
672 *
673 * returns a size of data buffer to allocate. the size is constant.
674 *
675 * tox_wait_prepare(): function should be called under lock every time we want to call tox_wait_execute()
657 * Prepares the data required to call tox_wait_execute() asynchronously 676 * Prepares the data required to call tox_wait_execute() asynchronously
658 * 677 *
659 * data[] is reserved and kept by the caller 678 * data[] should be of at least tox_wait_data_size() size and it's reserved and kept by the caller
660 * *lenptr is in/out: in = reserved data[], out = required data[] 679 * Use that data[] to call tox_wait_execute()
661 * 680 *
662 * returns 1 on success 681 * returns 1 on success
663 * returns 0 if *lenptr is insufficient 682 * returns 0 if data was NULL
664 * returns -1 if lenptr is NULL
665 * 683 *
666 * 684 *
667 * tox_wait_execute(): function can be called asynchronously 685 * tox_wait_execute(): function can be called asynchronously
668 * Waits for something to happen on the socket for up to milliseconds milliseconds. 686 * Waits for something to happen on the socket for up to seconds seconds and mircoseconds microseconds.
669 * *** Function MUSTN'T poll. *** 687 * mircoseconds should be between 0 and 999999.
670 * The function mustn't modify anything at all, so it can be called completely 688 * If you set either or both seconds and microseconds to negatives, it will block indefinetly until there
671 * asynchronously without any worry. 689 * is an activity.
672 * 690 *
673 * returns 1 if there is socket activity (i.e. tox_do() should be called) 691 * returns 2 if there is socket activity (i.e. tox_do() should be called)
674 * returns 0 if the timeout was reached 692 * returns 1 if the timeout was reached (tox_do() should be called anyway. it's advised to call it at least
675 * returns -1 if data was NULL or len too short 693 * once per second)
694 * returns 0 if data was NULL
676 * 695 *
677 * 696 *
678 * tox_wait_cleanup(): function should be called under lock 697 * tox_wait_cleanup(): function should be called under lock, every time tox_wait_execute() finishes
679 * Stores results from tox_wait_execute(). 698 * Stores results from tox_wait_execute().
680 * 699 *
681 * data[]/len shall be the exact same as given to tox_wait_execute() 700 * returns 1 on success
701 * returns 0 if data was NULL
682 * 702 *
683 */ 703 */
684int tox_wait_prepare(Tox *tox, uint8_t *data, uint16_t *lenptr); 704size_t tox_wait_data_size();
685int tox_wait_execute(Tox *tox, uint8_t *data, uint16_t len, uint16_t milliseconds); 705int tox_wait_prepare(Tox *tox, uint8_t *data);
686void tox_wait_cleanup(Tox *tox, uint8_t *data, uint16_t len); 706int tox_wait_execute(uint8_t *data, long seconds, long microseconds);
707int tox_wait_cleanup(Tox *tox, uint8_t *data);
687 708
688 709
689/* SAVING AND LOADING FUNCTIONS: */ 710/* SAVING AND LOADING FUNCTIONS: */