summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-05 10:31:29 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-05 23:09:28 +0000
commit8739f7fccb7cafc54ca0f5fa074c9a740f7048ba (patch)
tree88e9f53fa6e734cd8095487d1896c56f844c782c /toxcore/Messenger.c
parent64d0297acc7d6a1697683052e15cc76383312c38 (diff)
Make tox.c unambiguously parseable.
Rules: 1. Constants are uppercase names: THE_CONSTANT. 2. SUE[1] types start with an uppercase letter and have at least one lowercase letter in it: The_Type, THE_Type. 3. Function types end in "_cb": tox_friend_connection_cb. 4. Variable and function names are all lowercase: the_function. This makes it easier for humans reading the code to determine what an identifier means. I'm not convinced by the enum type name change, but I don't know a better rule. Currently, a lot of enum types are spelled like constants, which is confusing. [1] struct/union/enum
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c179
1 files changed, 88 insertions, 91 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 7b931bdb..b8509706 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -383,7 +383,7 @@ static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata)
383 } 383 }
384 384
385 if (m->read_receipt) { 385 if (m->read_receipt) {
386 (*m->read_receipt)(m, friendnumber, receipts->msg_id, userdata); 386 m->read_receipt(m, friendnumber, receipts->msg_id, userdata);
387 } 387 }
388 388
389 struct Receipts *r_next = receipts->next; 389 struct Receipts *r_next = receipts->next;
@@ -684,7 +684,7 @@ int m_set_userstatus(Messenger *m, uint8_t status)
684 return 0; 684 return 0;
685 } 685 }
686 686
687 m->userstatus = (USERSTATUS)status; 687 m->userstatus = (Userstatus)status;
688 uint32_t i; 688 uint32_t i;
689 689
690 for (i = 0; i < m->numfriends; ++i) { 690 for (i = 0; i < m->numfriends; ++i) {
@@ -830,7 +830,7 @@ static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, co
830 830
831static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status) 831static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
832{ 832{
833 m->friendlist[friendnumber].userstatus = (USERSTATUS)status; 833 m->friendlist[friendnumber].userstatus = (Userstatus)status;
834} 834}
835 835
836static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t is_typing) 836static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t is_typing)
@@ -844,55 +844,53 @@ void m_callback_log(Messenger *m, logger_cb *function, void *context, void *user
844} 844}
845 845
846/* Set the function that will be executed when a friend request is received. */ 846/* Set the function that will be executed when a friend request is received. */
847void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const uint8_t *, const uint8_t *, size_t, 847void m_callback_friendrequest(Messenger *m, m_friend_request_cb *function)
848 void *))
849{ 848{
850 callback_friendrequest(m->fr, (void (*)(void *, const uint8_t *, const uint8_t *, size_t, void *))function, m); 849 callback_friendrequest(m->fr, (fr_friend_request_cb *)function, m);
851} 850}
852 851
853/* Set the function that will be executed when a message from a friend is received. */ 852/* Set the function that will be executed when a message from a friend is received. */
854void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, uint32_t, unsigned int, const uint8_t *, 853void m_callback_friendmessage(Messenger *m, m_friend_message_cb *function)
855 size_t, void *))
856{ 854{
857 m->friend_message = function; 855 m->friend_message = function;
858} 856}
859 857
860void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, size_t, void *)) 858void m_callback_namechange(Messenger *m, m_friend_name_cb *function)
861{ 859{
862 m->friend_namechange = function; 860 m->friend_namechange = function;
863} 861}
864 862
865void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, size_t, void *)) 863void m_callback_statusmessage(Messenger *m, m_friend_status_message_cb *function)
866{ 864{
867 m->friend_statusmessagechange = function; 865 m->friend_statusmessagechange = function;
868} 866}
869 867
870void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, uint32_t, unsigned int, void *)) 868void m_callback_userstatus(Messenger *m, m_friend_status_cb *function)
871{ 869{
872 m->friend_userstatuschange = function; 870 m->friend_userstatuschange = function;
873} 871}
874 872
875void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, uint32_t, bool, void *)) 873void m_callback_typingchange(Messenger *m, m_friend_typing_cb *function)
876{ 874{
877 m->friend_typingchange = function; 875 m->friend_typingchange = function;
878} 876}
879 877
880void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, void *)) 878void m_callback_read_receipt(Messenger *m, m_friend_read_receipt_cb *function)
881{ 879{
882 m->read_receipt = function; 880 m->read_receipt = function;
883} 881}
884 882
885void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, uint32_t, unsigned int, void *)) 883void m_callback_connectionstatus(Messenger *m, m_friend_connection_status_cb *function)
886{ 884{
887 m->friend_connectionstatuschange = function; 885 m->friend_connectionstatuschange = function;
888} 886}
889 887
890void m_callback_core_connection(Messenger *m, void (*function)(Messenger *m, unsigned int, void *)) 888void m_callback_core_connection(Messenger *m, m_self_connection_status_cb *function)
891{ 889{
892 m->core_connection_change = function; 890 m->core_connection_change = function;
893} 891}
894 892
895void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, uint32_t, uint8_t, void *), 893void m_callback_connectionstatus_internal_av(Messenger *m, m_friend_connectionstatuschange_internal_cb *function,
896 void *userdata) 894 void *userdata)
897{ 895{
898 m->friend_connectionstatuschange_internal = function; 896 m->friend_connectionstatuschange_internal = function;
@@ -993,8 +991,7 @@ static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_
993 * 991 *
994 * Function(Messenger *m, uint32_t friendnumber, uint8_t *data, uint16_t length, void *userdata) 992 * Function(Messenger *m, uint32_t friendnumber, uint8_t *data, uint16_t length, void *userdata)
995 */ 993 */
996void m_callback_conference_invite(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, uint16_t, 994void m_callback_conference_invite(Messenger *m, m_conference_invite_cb *function)
997 void *))
998{ 995{
999 m->conference_invite = function; 996 m->conference_invite = function;
1000} 997}
@@ -1017,8 +1014,7 @@ int send_conference_invite_packet(const Messenger *m, int32_t friendnumber, cons
1017 * 1014 *
1018 * Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint32_t filetype, uint64_t filesize, uint8_t *filename, size_t filename_length, void *userdata) 1015 * Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint32_t filetype, uint64_t filesize, uint8_t *filename, size_t filename_length, void *userdata)
1019 */ 1016 */
1020void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, uint32_t, uint64_t, 1017void callback_file_sendrequest(Messenger *m, m_file_recv_cb *function)
1021 const uint8_t *, size_t, void *))
1022{ 1018{
1023 m->file_sendrequest = function; 1019 m->file_sendrequest = function;
1024} 1020}
@@ -1028,7 +1024,7 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, uin
1028 * Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, unsigned int control_type, void *userdata) 1024 * Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, unsigned int control_type, void *userdata)
1029 * 1025 *
1030 */ 1026 */
1031void callback_file_control(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, unsigned int, void *)) 1027void callback_file_control(Messenger *m, m_file_recv_control_cb *function)
1032{ 1028{
1033 m->file_filecontrol = function; 1029 m->file_filecontrol = function;
1034} 1030}
@@ -1038,8 +1034,7 @@ void callback_file_control(Messenger *m, void (*function)(Messenger *m, uint32_t
1038 * Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, uint8_t *data, size_t length, void *userdata) 1034 * Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, uint8_t *data, size_t length, void *userdata)
1039 * 1035 *
1040 */ 1036 */
1041void callback_file_data(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, uint64_t, const uint8_t *, 1037void callback_file_data(Messenger *m, m_file_recv_chunk_cb *function)
1042 size_t, void *))
1043{ 1038{
1044 m->file_filedata = function; 1039 m->file_filedata = function;
1045} 1040}
@@ -1049,7 +1044,7 @@ void callback_file_data(Messenger *m, void (*function)(Messenger *m, uint32_t, u
1049 * Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, size_t length, void *userdata) 1044 * Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, size_t length, void *userdata)
1050 * 1045 *
1051 */ 1046 */
1052void callback_file_reqchunk(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, uint64_t, size_t, void *)) 1047void callback_file_reqchunk(Messenger *m, m_file_chunk_request_cb *function)
1053{ 1048{
1054 m->file_reqchunk = function; 1049 m->file_reqchunk = function;
1055} 1050}
@@ -1579,7 +1574,7 @@ static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userd
1579 } 1574 }
1580 1575
1581 // Allocate 1 slot to this file transfer. 1576 // Allocate 1 slot to this file transfer.
1582 ft->slots_allocated++; 1577 ++ft->slots_allocated;
1583 1578
1584 const uint16_t length = min_u64(ft->size - ft->requested, MAX_FILE_DATA_SIZE); 1579 const uint16_t length = min_u64(ft->size - ft->requested, MAX_FILE_DATA_SIZE);
1585 const uint64_t position = ft->requested; 1580 const uint64_t position = ft->requested;
@@ -1628,11 +1623,11 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdat
1628 // 1623 //
1629 // TODO(zoff99): Fix this to exit the loop properly when we're done 1624 // TODO(zoff99): Fix this to exit the loop properly when we're done
1630 // requesting all chunks for all file transfers. 1625 // requesting all chunks for all file transfers.
1631 const uint32_t MAX_FT_LOOPS = 16; 1626 const uint32_t max_ft_loops = 16;
1632 1627
1633 while (((free_slots > 0) || loop_counter == 0) && any_active_fts && (loop_counter < MAX_FT_LOOPS)) { 1628 while (((free_slots > 0) || loop_counter == 0) && any_active_fts && (loop_counter < max_ft_loops)) {
1634 any_active_fts = do_all_filetransfers(m, friendnumber, userdata, &free_slots); 1629 any_active_fts = do_all_filetransfers(m, friendnumber, userdata, &free_slots);
1635 loop_counter++; 1630 ++loop_counter;
1636 } 1631 }
1637} 1632}
1638 1633
@@ -1773,7 +1768,8 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv
1773 return -1; 1768 return -1;
1774 } 1769 }
1775 1770
1776 ft->transferred = ft->requested = position; 1771 ft->requested = position;
1772 ft->transferred = position;
1777 return 0; 1773 return 0;
1778 } 1774 }
1779 1775
@@ -1791,8 +1787,7 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv
1791 * 1787 *
1792 * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata) 1788 * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata)
1793 */ 1789 */
1794void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, uint16_t, void *), 1790void m_callback_msi_packet(Messenger *m, m_msi_packet_cb *function, void *userdata)
1795 void *userdata)
1796{ 1791{
1797 m->msi_packet = function; 1792 m->msi_packet = function;
1798 m->msi_packet_userdata = userdata; 1793 m->msi_packet_userdata = userdata;
@@ -1834,14 +1829,13 @@ static int m_handle_custom_lossy_packet(void *object, int friend_num, const uint
1834 return 1; 1829 return 1;
1835} 1830}
1836 1831
1837void custom_lossy_packet_registerhandler(Messenger *m, void (*lossy_packethandler)(Messenger *m, 1832void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb *lossy_packethandler)
1838 uint32_t friendnumber, const uint8_t *data, size_t len, void *object))
1839{ 1833{
1840 m->lossy_packethandler = lossy_packethandler; 1834 m->lossy_packethandler = lossy_packethandler;
1841} 1835}
1842 1836
1843int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, int (*function)(Messenger *m, 1837int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lossy_rtp_packet_cb *function,
1844 uint32_t friendnumber, const uint8_t *data, uint16_t len, void *object), void *object) 1838 void *object)
1845{ 1839{
1846 if (friend_not_valid(m, friendnumber)) { 1840 if (friend_not_valid(m, friendnumber)) {
1847 return -1; 1841 return -1;
@@ -1915,8 +1909,7 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin
1915 return 1; 1909 return 1;
1916} 1910}
1917 1911
1918void custom_lossless_packet_registerhandler(Messenger *m, void (*lossless_packethandler)(Messenger *m, 1912void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_packet_cb *lossless_packethandler)
1919 uint32_t friendnumber, const uint8_t *data, size_t len, void *object))
1920{ 1913{
1921 m->lossless_packethandler = lossless_packethandler; 1914 m->lossless_packethandler = lossless_packethandler;
1922} 1915}
@@ -2025,7 +2018,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
2025 return nullptr; 2018 return nullptr;
2026 } 2019 }
2027 2020
2028 m->dht = new_DHT(m->log, m->net, options->hole_punching_enabled); 2021 m->dht = new_dht(m->log, m->net, options->hole_punching_enabled);
2029 2022
2030 if (m->dht == nullptr) { 2023 if (m->dht == nullptr) {
2031 kill_networking(m->net); 2024 kill_networking(m->net);
@@ -2039,7 +2032,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
2039 2032
2040 if (m->net_crypto == nullptr) { 2033 if (m->net_crypto == nullptr) {
2041 kill_networking(m->net); 2034 kill_networking(m->net);
2042 kill_DHT(m->dht); 2035 kill_dht(m->dht);
2043 friendreq_kill(m->fr); 2036 friendreq_kill(m->fr);
2044 logger_kill(m->log); 2037 logger_kill(m->log);
2045 free(m); 2038 free(m);
@@ -2057,7 +2050,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
2057 kill_onion_announce(m->onion_a); 2050 kill_onion_announce(m->onion_a);
2058 kill_onion_client(m->onion_c); 2051 kill_onion_client(m->onion_c);
2059 kill_net_crypto(m->net_crypto); 2052 kill_net_crypto(m->net_crypto);
2060 kill_DHT(m->dht); 2053 kill_dht(m->dht);
2061 kill_networking(m->net); 2054 kill_networking(m->net);
2062 friendreq_kill(m->fr); 2055 friendreq_kill(m->fr);
2063 logger_kill(m->log); 2056 logger_kill(m->log);
@@ -2075,7 +2068,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
2075 kill_onion_announce(m->onion_a); 2068 kill_onion_announce(m->onion_a);
2076 kill_onion_client(m->onion_c); 2069 kill_onion_client(m->onion_c);
2077 kill_net_crypto(m->net_crypto); 2070 kill_net_crypto(m->net_crypto);
2078 kill_DHT(m->dht); 2071 kill_dht(m->dht);
2079 kill_networking(m->net); 2072 kill_networking(m->net);
2080 friendreq_kill(m->fr); 2073 friendreq_kill(m->fr);
2081 logger_kill(m->log); 2074 logger_kill(m->log);
@@ -2121,7 +2114,7 @@ void kill_messenger(Messenger *m)
2121 kill_onion_announce(m->onion_a); 2114 kill_onion_announce(m->onion_a);
2122 kill_onion_client(m->onion_c); 2115 kill_onion_client(m->onion_c);
2123 kill_net_crypto(m->net_crypto); 2116 kill_net_crypto(m->net_crypto);
2124 kill_DHT(m->dht); 2117 kill_dht(m->dht);
2125 kill_networking(m->net); 2118 kill_networking(m->net);
2126 2119
2127 for (i = 0; i < m->numfriends; ++i) { 2120 for (i = 0; i < m->numfriends; ++i) {
@@ -2241,7 +2234,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le
2241 break; 2234 break;
2242 } 2235 }
2243 2236
2244 USERSTATUS status = (USERSTATUS)data[0]; 2237 Userstatus status = (Userstatus)data[0];
2245 2238
2246 if (status >= USERSTATUS_INVALID) { 2239 if (status >= USERSTATUS_INVALID) {
2247 break; 2240 break;
@@ -2541,7 +2534,7 @@ static void do_friends(Messenger *m, void *userdata)
2541 } 2534 }
2542} 2535}
2543 2536
2544static void connection_status_cb(Messenger *m, void *userdata) 2537static void connection_status_callback(Messenger *m, void *userdata)
2545{ 2538{
2546 unsigned int conn_status = onion_connection_status(m->onion_c); 2539 unsigned int conn_status = onion_connection_status(m->onion_c);
2547 2540
@@ -2566,7 +2559,7 @@ static char *id_to_string(const uint8_t *pk, char *id_str, size_t length)
2566 return id_str; 2559 return id_str;
2567 } 2560 }
2568 2561
2569 for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; i++) { 2562 for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) {
2570 sprintf(&id_str[i * 2], "%02X", pk[i]); 2563 sprintf(&id_str[i * 2], "%02X", pk[i]);
2571 } 2564 }
2572 2565
@@ -2622,7 +2615,7 @@ void do_messenger(Messenger *m, void *userdata)
2622 2615
2623 if (!m->options.udp_disabled) { 2616 if (!m->options.udp_disabled) {
2624 networking_poll(m->net, userdata); 2617 networking_poll(m->net, userdata);
2625 do_DHT(m->dht); 2618 do_dht(m->dht);
2626 } 2619 }
2627 2620
2628 if (m->tcp_server) { 2621 if (m->tcp_server) {
@@ -2633,18 +2626,18 @@ void do_messenger(Messenger *m, void *userdata)
2633 do_onion_client(m->onion_c); 2626 do_onion_client(m->onion_c);
2634 do_friend_connections(m->fr_c, userdata); 2627 do_friend_connections(m->fr_c, userdata);
2635 do_friends(m, userdata); 2628 do_friends(m, userdata);
2636 connection_status_cb(m, userdata); 2629 connection_status_callback(m, userdata);
2637 2630
2638 if (unix_time() > m->lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { 2631 if (unix_time() > m->lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
2639 m->lastdump = unix_time(); 2632 m->lastdump = unix_time();
2640 uint32_t client, last_pinged; 2633 uint32_t client, last_pinged;
2641 2634
2642 for (client = 0; client < LCLIENT_LIST; client++) { 2635 for (client = 0; client < LCLIENT_LIST; ++client) {
2643 const Client_data *cptr = dht_get_close_client(m->dht, client); 2636 const Client_data *cptr = dht_get_close_client(m->dht, client);
2644 const IPPTsPng *assoc = nullptr; 2637 const IPPTsPng *assoc = nullptr;
2645 uint32_t a; 2638 uint32_t a;
2646 2639
2647 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) { 2640 for (a = 0, assoc = &cptr->assoc4; a < 2; ++a, assoc = &cptr->assoc6) {
2648 if (ip_isset(&assoc->ip_port.ip)) { 2641 if (ip_isset(&assoc->ip_port.ip)) {
2649 last_pinged = m->lastdump - assoc->last_pinged; 2642 last_pinged = m->lastdump - assoc->last_pinged;
2650 2643
@@ -2670,7 +2663,7 @@ void do_messenger(Messenger *m, void *userdata)
2670 VLA(int32_t, m2dht, num_dhtfriends); 2663 VLA(int32_t, m2dht, num_dhtfriends);
2671 VLA(int32_t, dht2m, num_dhtfriends); 2664 VLA(int32_t, dht2m, num_dhtfriends);
2672 2665
2673 for (friend_idx = 0; friend_idx < num_dhtfriends; friend_idx++) { 2666 for (friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2674 m2dht[friend_idx] = -1; 2667 m2dht[friend_idx] = -1;
2675 dht2m[friend_idx] = -1; 2668 dht2m[friend_idx] = -1;
2676 2669
@@ -2678,7 +2671,7 @@ void do_messenger(Messenger *m, void *userdata)
2678 continue; 2671 continue;
2679 } 2672 }
2680 2673
2681 for (dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); dhtfriend++) { 2674 for (dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); ++dhtfriend) {
2682 if (id_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) { 2675 if (id_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) {
2683 m2dht[friend_idx] = dhtfriend; 2676 m2dht[friend_idx] = dhtfriend;
2684 break; 2677 break;
@@ -2686,7 +2679,7 @@ void do_messenger(Messenger *m, void *userdata)
2686 } 2679 }
2687 } 2680 }
2688 2681
2689 for (friend_idx = 0; friend_idx < num_dhtfriends; friend_idx++) { 2682 for (friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2690 if (m2dht[friend_idx] >= 0) { 2683 if (m2dht[friend_idx] >= 0) {
2691 dht2m[m2dht[friend_idx]] = friend_idx; 2684 dht2m[m2dht[friend_idx]] = friend_idx;
2692 } 2685 }
@@ -2699,7 +2692,7 @@ void do_messenger(Messenger *m, void *userdata)
2699 Friend *msgfptr; 2692 Friend *msgfptr;
2700 DHT_Friend *dhtfptr; 2693 DHT_Friend *dhtfptr;
2701 2694
2702 for (friend_idx = 0; friend_idx < num_dhtfriends; friend_idx++) { 2695 for (friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2703 if (dht2m[friend_idx] >= 0) { 2696 if (dht2m[friend_idx] >= 0) {
2704 msgfptr = &m->friendlist[dht2m[friend_idx]]; 2697 msgfptr = &m->friendlist[dht2m[friend_idx]];
2705 } else { 2698 } else {
@@ -2719,11 +2712,11 @@ void do_messenger(Messenger *m, void *userdata)
2719 id_to_string(dht_friend_public_key(dhtfptr), id_str, sizeof(id_str))); 2712 id_to_string(dht_friend_public_key(dhtfptr), id_str, sizeof(id_str)));
2720 } 2713 }
2721 2714
2722 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { 2715 for (client = 0; client < MAX_FRIEND_CLIENTS; ++client) {
2723 const Client_data *cptr = dht_friend_client(dhtfptr, client); 2716 const Client_data *cptr = dht_friend_client(dhtfptr, client);
2724 const IPPTsPng *const assocs[] = {&cptr->assoc4, &cptr->assoc6}; 2717 const IPPTsPng *const assocs[] = {&cptr->assoc4, &cptr->assoc6};
2725 2718
2726 for (size_t a = 0; a < sizeof(assocs) / sizeof(assocs[0]); a++) { 2719 for (size_t a = 0; a < sizeof(assocs) / sizeof(assocs[0]); ++a) {
2727 const IPPTsPng *const assoc = assocs[a]; 2720 const IPPTsPng *const assoc = assocs[a];
2728 2721
2729 if (ip_isset(&assoc->ip_port.ip)) { 2722 if (ip_isset(&assoc->ip_port.ip)) {
@@ -2764,7 +2757,7 @@ void do_messenger(Messenger *m, void *userdata)
2764#define SAVED_FRIEND_REQUEST_SIZE 1024 2757#define SAVED_FRIEND_REQUEST_SIZE 1024
2765#define NUM_SAVED_PATH_NODES 8 2758#define NUM_SAVED_PATH_NODES 8
2766 2759
2767struct SAVED_FRIEND { 2760struct Saved_Friend {
2768 uint8_t status; 2761 uint8_t status;
2769 uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE]; 2762 uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
2770 uint8_t info[SAVED_FRIEND_REQUEST_SIZE]; // the data that is sent during the friend requests we do. 2763 uint8_t info[SAVED_FRIEND_REQUEST_SIZE]; // the data that is sent during the friend requests we do.
@@ -2781,21 +2774,21 @@ struct SAVED_FRIEND {
2781static uint32_t friend_size(void) 2774static uint32_t friend_size(void)
2782{ 2775{
2783 uint32_t data = 0; 2776 uint32_t data = 0;
2784 const struct SAVED_FRIEND *const temp = nullptr; 2777 const struct Saved_Friend *const temp = nullptr;
2785 2778
2786#define VALUE_MEMBER(NAME) data += sizeof(temp->NAME) 2779#define VALUE_MEMBER(name) do { data += sizeof(temp->name); } while (0)
2787#define ARRAY_MEMBER(NAME) data += sizeof(temp->NAME) 2780#define ARRAY_MEMBER(name) do { data += sizeof(temp->name); } while (0)
2788 2781
2789 // Exactly the same in friend_load, friend_save, and friend_size 2782 // Exactly the same in friend_load, friend_save, and friend_size
2790 VALUE_MEMBER(status); 2783 VALUE_MEMBER(status);
2791 ARRAY_MEMBER(real_pk); 2784 ARRAY_MEMBER(real_pk);
2792 ARRAY_MEMBER(info); 2785 ARRAY_MEMBER(info);
2793 data++; // padding 2786 ++data; // padding
2794 VALUE_MEMBER(info_size); 2787 VALUE_MEMBER(info_size);
2795 ARRAY_MEMBER(name); 2788 ARRAY_MEMBER(name);
2796 VALUE_MEMBER(name_length); 2789 VALUE_MEMBER(name_length);
2797 ARRAY_MEMBER(statusmessage); 2790 ARRAY_MEMBER(statusmessage);
2798 data++; // padding 2791 ++data; // padding
2799 VALUE_MEMBER(statusmessage_length); 2792 VALUE_MEMBER(statusmessage_length);
2800 VALUE_MEMBER(userstatus); 2793 VALUE_MEMBER(userstatus);
2801 data += 3; // padding 2794 data += 3; // padding
@@ -2813,26 +2806,28 @@ static uint32_t saved_friendslist_size(const Messenger *m)
2813 return count_friendlist(m) * friend_size(); 2806 return count_friendlist(m) * friend_size();
2814} 2807}
2815 2808
2816static uint8_t *friend_save(const struct SAVED_FRIEND *temp, uint8_t *data) 2809static uint8_t *friend_save(const struct Saved_Friend *temp, uint8_t *data)
2817{ 2810{
2818#define VALUE_MEMBER(NAME) \ 2811#define VALUE_MEMBER(name) do { \
2819 memcpy(data, &temp->NAME, sizeof(temp->NAME)); \ 2812 memcpy(data, &temp->name, sizeof(temp->name)); \
2820 data += sizeof(temp->NAME) 2813 data += sizeof(temp->name); \
2814} while (0)
2821 2815
2822#define ARRAY_MEMBER(NAME) \ 2816#define ARRAY_MEMBER(name) do { \
2823 memcpy(data, temp->NAME, sizeof(temp->NAME)); \ 2817 memcpy(data, temp->name, sizeof(temp->name)); \
2824 data += sizeof(temp->NAME) 2818 data += sizeof(temp->name); \
2819} while (0)
2825 2820
2826 // Exactly the same in friend_load, friend_save, and friend_size 2821 // Exactly the same in friend_load, friend_save, and friend_size
2827 VALUE_MEMBER(status); 2822 VALUE_MEMBER(status);
2828 ARRAY_MEMBER(real_pk); 2823 ARRAY_MEMBER(real_pk);
2829 ARRAY_MEMBER(info); 2824 ARRAY_MEMBER(info);
2830 data++; // padding 2825 ++data; // padding
2831 VALUE_MEMBER(info_size); 2826 VALUE_MEMBER(info_size);
2832 ARRAY_MEMBER(name); 2827 ARRAY_MEMBER(name);
2833 VALUE_MEMBER(name_length); 2828 VALUE_MEMBER(name_length);
2834 ARRAY_MEMBER(statusmessage); 2829 ARRAY_MEMBER(statusmessage);
2835 data++; // padding 2830 ++data; // padding
2836 VALUE_MEMBER(statusmessage_length); 2831 VALUE_MEMBER(statusmessage_length);
2837 VALUE_MEMBER(userstatus); 2832 VALUE_MEMBER(userstatus);
2838 data += 3; // padding 2833 data += 3; // padding
@@ -2851,9 +2846,9 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
2851 uint32_t num = 0; 2846 uint32_t num = 0;
2852 uint8_t *cur_data = data; 2847 uint8_t *cur_data = data;
2853 2848
2854 for (i = 0; i < m->numfriends; i++) { 2849 for (i = 0; i < m->numfriends; ++i) {
2855 if (m->friendlist[i].status > 0) { 2850 if (m->friendlist[i].status > 0) {
2856 struct SAVED_FRIEND temp = { 0 }; 2851 struct Saved_Friend temp = { 0 };
2857 temp.status = m->friendlist[i].status; 2852 temp.status = m->friendlist[i].status;
2858 memcpy(temp.real_pk, m->friendlist[i].real_pk, CRYPTO_PUBLIC_KEY_SIZE); 2853 memcpy(temp.real_pk, m->friendlist[i].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
2859 2854
@@ -2884,7 +2879,7 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
2884 assert(memcmp(cur_data, &temp, friend_size()) == 0); 2879 assert(memcmp(cur_data, &temp, friend_size()) == 0);
2885#endif 2880#endif
2886 cur_data = next_data; 2881 cur_data = next_data;
2887 num++; 2882 ++num;
2888 } 2883 }
2889 } 2884 }
2890 2885
@@ -2892,26 +2887,28 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
2892 return cur_data - data; 2887 return cur_data - data;
2893} 2888}
2894 2889
2895static const uint8_t *friend_load(struct SAVED_FRIEND *temp, const uint8_t *data) 2890static const uint8_t *friend_load(struct Saved_Friend *temp, const uint8_t *data)
2896{ 2891{
2897#define VALUE_MEMBER(NAME) \ 2892#define VALUE_MEMBER(name) do { \
2898 memcpy(&temp->NAME, data, sizeof(temp->NAME)); \ 2893 memcpy(&temp->name, data, sizeof(temp->name)); \
2899 data += sizeof(temp->NAME) 2894 data += sizeof(temp->name); \
2895} while (0)
2900 2896
2901#define ARRAY_MEMBER(NAME) \ 2897#define ARRAY_MEMBER(name) do { \
2902 memcpy(temp->NAME, data, sizeof(temp->NAME)); \ 2898 memcpy(temp->name, data, sizeof(temp->name)); \
2903 data += sizeof(temp->NAME) 2899 data += sizeof(temp->name); \
2900} while (0)
2904 2901
2905 // Exactly the same in friend_load, friend_save, and friend_size 2902 // Exactly the same in friend_load, friend_save, and friend_size
2906 VALUE_MEMBER(status); 2903 VALUE_MEMBER(status);
2907 ARRAY_MEMBER(real_pk); 2904 ARRAY_MEMBER(real_pk);
2908 ARRAY_MEMBER(info); 2905 ARRAY_MEMBER(info);
2909 data++; // padding 2906 ++data; // padding
2910 VALUE_MEMBER(info_size); 2907 VALUE_MEMBER(info_size);
2911 ARRAY_MEMBER(name); 2908 ARRAY_MEMBER(name);
2912 VALUE_MEMBER(name_length); 2909 VALUE_MEMBER(name_length);
2913 ARRAY_MEMBER(statusmessage); 2910 ARRAY_MEMBER(statusmessage);
2914 data++; // padding 2911 ++data; // padding
2915 VALUE_MEMBER(statusmessage_length); 2912 VALUE_MEMBER(statusmessage_length);
2916 VALUE_MEMBER(userstatus); 2913 VALUE_MEMBER(userstatus);
2917 data += 3; // padding 2914 data += 3; // padding
@@ -2935,7 +2932,7 @@ static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length)
2935 const uint8_t *cur_data = data; 2932 const uint8_t *cur_data = data;
2936 2933
2937 for (i = 0; i < num; ++i) { 2934 for (i = 0; i < num; ++i) {
2938 struct SAVED_FRIEND temp = { 0 }; 2935 struct Saved_Friend temp = { 0 };
2939 const uint8_t *next_data = friend_load(&temp, cur_data); 2936 const uint8_t *next_data = friend_load(&temp, cur_data);
2940 assert(next_data - cur_data == friend_size()); 2937 assert(next_data - cur_data == friend_size());
2941#ifdef __LP64__ 2938#ifdef __LP64__
@@ -2977,7 +2974,7 @@ uint32_t messenger_size(const Messenger *m)
2977 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; 2974 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
2978 return size32 * 2 // global cookie 2975 return size32 * 2 // global cookie
2979 + sizesubhead + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE 2976 + sizesubhead + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE
2980 + sizesubhead + DHT_size(m->dht) // DHT 2977 + sizesubhead + dht_size(m->dht) // DHT
2981 + sizesubhead + saved_friendslist_size(m) // Friendlist itself. 2978 + sizesubhead + saved_friendslist_size(m) // Friendlist itself.
2982 + sizesubhead + m->name_length // Own nickname. 2979 + sizesubhead + m->name_length // Own nickname.
2983 + sizesubhead + m->statusmessage_length // status message 2980 + sizesubhead + m->statusmessage_length // status message
@@ -3042,10 +3039,10 @@ void messenger_save(const Messenger *m, uint8_t *data)
3042 *data = m->userstatus; 3039 *data = m->userstatus;
3043 data += len; 3040 data += len;
3044 3041
3045 len = DHT_size(m->dht); 3042 len = dht_size(m->dht);
3046 type = MESSENGER_STATE_TYPE_DHT; 3043 type = MESSENGER_STATE_TYPE_DHT;
3047 data = messenger_save_subheader(data, len, type); 3044 data = messenger_save_subheader(data, len, type);
3048 DHT_save(m->dht, data); 3045 dht_save(m->dht, data);
3049 data += len; 3046 data += len;
3050 3047
3051 Node_format relays[NUM_SAVED_TCP_RELAYS]; 3048 Node_format relays[NUM_SAVED_TCP_RELAYS];
@@ -3098,7 +3095,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
3098 break; 3095 break;
3099 3096
3100 case MESSENGER_STATE_TYPE_DHT: 3097 case MESSENGER_STATE_TYPE_DHT:
3101 DHT_load(m->dht, data, length); 3098 dht_load(m->dht, data, length);
3102 break; 3099 break;
3103 3100
3104 case MESSENGER_STATE_TYPE_FRIENDS: 3101 case MESSENGER_STATE_TYPE_FRIENDS:
@@ -3199,9 +3196,9 @@ uint32_t count_friendlist(const Messenger *m)
3199 uint32_t ret = 0; 3196 uint32_t ret = 0;
3200 uint32_t i; 3197 uint32_t i;
3201 3198
3202 for (i = 0; i < m->numfriends; i++) { 3199 for (i = 0; i < m->numfriends; ++i) {
3203 if (m->friendlist[i].status > 0) { 3200 if (m->friendlist[i].status > 0) {
3204 ret++; 3201 ++ret;
3205 } 3202 }
3206 } 3203 }
3207 3204
@@ -3226,14 +3223,14 @@ uint32_t copy_friendlist(Messenger const *m, uint32_t *out_list, uint32_t list_s
3226 uint32_t i; 3223 uint32_t i;
3227 uint32_t ret = 0; 3224 uint32_t ret = 0;
3228 3225
3229 for (i = 0; i < m->numfriends; i++) { 3226 for (i = 0; i < m->numfriends; ++i) {
3230 if (ret >= list_size) { 3227 if (ret >= list_size) {
3231 break; /* Abandon ship */ 3228 break; /* Abandon ship */
3232 } 3229 }
3233 3230
3234 if (m->friendlist[i].status > 0) { 3231 if (m->friendlist[i].status > 0) {
3235 out_list[ret] = i; 3232 out_list[ret] = i;
3236 ret++; 3233 ++ret;
3237 } 3234 }
3238 } 3235 }
3239 3236