diff options
author | aunsane <aunsane@gmail.com> | 2018-03-03 16:35:09 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-03-03 16:35:33 +0300 |
commit | fa58f69fe117640e29cefb1b699bede4d045bc2f (patch) | |
tree | a1bf8406d63e2b246bebdc572ceccfaabcffa359 /protocols/Tox/libtox/src/toxcore/Messenger.c | |
parent | 0e8f5a3aa5f5e73b413c5646444751783e367f2b (diff) |
Tox:
- updated toxcore due to release v0.2
- removed message correction
- reworked nodes update
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/Messenger.c')
-rw-r--r-- | protocols/Tox/libtox/src/toxcore/Messenger.c | 297 |
1 files changed, 176 insertions, 121 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/Messenger.c b/protocols/Tox/libtox/src/toxcore/Messenger.c index fcee455ccc..6e209f4db3 100644 --- a/protocols/Tox/libtox/src/toxcore/Messenger.c +++ b/protocols/Tox/libtox/src/toxcore/Messenger.c @@ -33,7 +33,6 @@ #include <assert.h> -static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata); static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data, uint32_t length, uint8_t congestion_control); @@ -57,13 +56,13 @@ static int realloc_friendlist(Messenger *m, uint32_t num) { if (num == 0) { free(m->friendlist); - m->friendlist = NULL; + m->friendlist = nullptr; return 0; } Friend *newfriendlist = (Friend *)realloc(m->friendlist, num * sizeof(Friend)); - if (newfriendlist == NULL) { + if (newfriendlist == nullptr) { return -1; } @@ -322,8 +321,8 @@ static int clear_receipts(Messenger *m, int32_t friendnumber) receipts = temp_r; } - m->friendlist[friendnumber].receipts_start = NULL; - m->friendlist[friendnumber].receipts_end = NULL; + m->friendlist[friendnumber].receipts_start = nullptr; + m->friendlist[friendnumber].receipts_end = nullptr; return 0; } @@ -349,7 +348,7 @@ static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, } m->friendlist[friendnumber].receipts_end = new_receipts; - new_receipts->next = NULL; + new_receipts->next = nullptr; return 0; } /* @@ -393,7 +392,7 @@ static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata) } if (!m->friendlist[friendnumber].receipts_start) { - m->friendlist[friendnumber].receipts_end = NULL; + m->friendlist[friendnumber].receipts_end = nullptr; } return 0; @@ -416,7 +415,8 @@ int m_delfriend(Messenger *m, int32_t friendnumber) clear_receipts(m, friendnumber); remove_request_received(m->fr, m->friendlist[friendnumber].real_pk); - friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, 0, 0, 0, 0, 0); + friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, nullptr, + nullptr, nullptr, nullptr, 0); if (friend_con_connected(m->fr_c, m->friendlist[friendnumber].friendcon_id) == FRIENDCONN_STATUS_CONNECTED) { send_offline_packet(m, m->friendlist[friendnumber].friendcon_id); @@ -488,8 +488,7 @@ int m_friend_exists(const Messenger *m, int32_t friendnumber) int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, const uint8_t *message, uint32_t length, uint32_t *message_id) { - /* MESSAGE_LAST itself is incorrect value */ - if (type >= MESSAGE_LAST) { + if (type > MESSAGE_ACTION) { return -5; } @@ -601,7 +600,7 @@ int setname(Messenger *m, const uint8_t *name, uint16_t length) */ uint16_t getself_name(const Messenger *m, uint8_t *name) { - if (name == NULL) { + if (name == nullptr) { return 0; } @@ -949,7 +948,7 @@ static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, ui } } -void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata) +static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata) { check_friend_connectionstatus(m, friendnumber, status, userdata); m->friendlist[friendnumber].status = status; @@ -1283,7 +1282,7 @@ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, } } - if (send_file_control_packet(m, friendnumber, send_receive, file_number, control, 0, 0)) { + if (send_file_control_packet(m, friendnumber, send_receive, file_number, control, nullptr, 0)) { if (control == FILECONTROL_KILL) { ft->status = FILESTATUS_NONE; @@ -1482,119 +1481,159 @@ uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t fi return 0; } + const struct File_Transfers *const sending = &m->friendlist[friendnumber].file_sending[filenumber]; + if (send_receive == 0) { - if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE) { + if (sending->status == FILESTATUS_NONE) { return 0; } - return m->friendlist[friendnumber].file_sending[filenumber].size - - m->friendlist[friendnumber].file_sending[filenumber].transferred; + return sending->size - sending->transferred; } - if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE) { + const struct File_Transfers *const receiving = &m->friendlist[friendnumber].file_receiving[filenumber]; + + if (receiving->status == FILESTATUS_NONE) { return 0; } - return m->friendlist[friendnumber].file_receiving[filenumber].size - - m->friendlist[friendnumber].file_receiving[filenumber].transferred; + return receiving->size - receiving->transferred; } -static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdata) +/** + * Iterate over all file transfers and request chunks (from the client) for each + * of them. + * + * The free_slots parameter is updated by this function. + * + * @param m Our messenger object. + * @param friendnumber The friend we're sending files to. + * @param userdata The client userdata to pass along to chunk request callbacks. + * @param free_slots A pointer to the number of free send queue slots in the + * crypto connection. + * + * @return true if there are still file transfers ongoing, false if all file + * transfers are complete. + */ +static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userdata, uint32_t *free_slots) { - if (!m->friendlist[friendnumber].num_sending_files) { - return; - } + Friend *const friendcon = &m->friendlist[friendnumber]; + uint32_t num = friendcon->num_sending_files; - int free_slots = crypto_num_free_sendqueue_slots(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, - m->friendlist[friendnumber].friendcon_id)); + bool any_active_fts = false; - if (free_slots < MIN_SLOTS_FREE) { - free_slots = 0; - } else { - free_slots -= MIN_SLOTS_FREE; - } - - unsigned int i, num = m->friendlist[friendnumber].num_sending_files; - - for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) { - struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i]; + // Iterate over all file transfers, including inactive ones. I.e. we always + // iterate exactly MAX_CONCURRENT_FILE_PIPES times. + for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) { + struct File_Transfers *const ft = &friendcon->file_sending[i]; + // Any status other than NONE means the file transfer is active. if (ft->status != FILESTATUS_NONE) { + any_active_fts = true; --num; - if (ft->status == FILESTATUS_FINISHED) { - /* Check if file was entirely sent. */ - if (friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) { - if (m->file_reqchunk) { - (*m->file_reqchunk)(m, friendnumber, i, ft->transferred, 0, userdata); - } - - ft->status = FILESTATUS_NONE; - --m->friendlist[friendnumber].num_sending_files; + // If the file transfer is complete, we request a chunk of size 0. + if (ft->status == FILESTATUS_FINISHED && friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) { + if (m->file_reqchunk) { + m->file_reqchunk(m, friendnumber, i, ft->transferred, 0, userdata); } - } - /* TODO(irungentoo): if file is too slow, switch to the next. */ - if (ft->slots_allocated > (unsigned int)free_slots) { - free_slots = 0; - } else { - free_slots -= ft->slots_allocated; + // Now it's inactive, we're no longer sending this. + ft->status = FILESTATUS_NONE; + --friendcon->num_sending_files; } + + // Decrease free slots by the number of slots this FT uses. + *free_slots = max_s32(0, (int32_t) * free_slots - ft->slots_allocated); } - while (ft->status == FILESTATUS_TRANSFERRING && (ft->paused == FILE_PAUSE_NOT)) { - if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, - m->friendlist[friendnumber].friendcon_id))) { - free_slots = 0; + if (ft->status == FILESTATUS_TRANSFERRING && ft->paused == FILE_PAUSE_NOT) { + if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id( + m->fr_c, friendcon->friendcon_id))) { + *free_slots = 0; } - if (free_slots == 0) { - break; + if (*free_slots == 0) { + continue; } - uint16_t length = MAX_FILE_DATA_SIZE; - if (ft->size == 0) { /* Send 0 data to friend if file is 0 length. */ - file_data(m, friendnumber, i, 0, 0, 0); - break; + file_data(m, friendnumber, i, 0, nullptr, 0); + continue; } if (ft->size == ft->requested) { - break; - } - - if (ft->size - ft->requested < length) { - length = ft->size - ft->requested; + // This file transfer is done. + continue; } - ++ft->slots_allocated; + // Allocate 1 slot to this file transfer. + ft->slots_allocated++; - uint64_t position = ft->requested; + const uint16_t length = min_u64(ft->size - ft->requested, MAX_FILE_DATA_SIZE); + const uint64_t position = ft->requested; ft->requested += length; if (m->file_reqchunk) { - (*m->file_reqchunk)(m, friendnumber, i, position, length, userdata); + m->file_reqchunk(m, friendnumber, i, position, length, userdata); } - --free_slots; + // The allocated slot is no longer free. + --*free_slots; } if (num == 0) { - break; + continue; } } + + return any_active_fts; +} + +static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdata) +{ + // We're not currently doing any file transfers. + if (m->friendlist[friendnumber].num_sending_files == 0) { + return; + } + + // The number of packet slots left in the sendbuffer. + // This is a per friend count (CRYPTO_PACKET_BUFFER_SIZE). + uint32_t free_slots = crypto_num_free_sendqueue_slots( + m->net_crypto, + friend_connection_crypt_connection_id( + m->fr_c, + m->friendlist[friendnumber].friendcon_id)); + + // We keep MIN_SLOTS_FREE slots free for other packets, otherwise file + // transfers might block other traffic for a long time. + free_slots = max_s32(0, (int32_t)free_slots - MIN_SLOTS_FREE); + + bool any_active_fts = true; + uint32_t loop_counter = 0; + // Maximum number of outer loops below. If the client doesn't send file + // chunks from within the chunk request callback handler, we never realise + // that the file transfer has finished and may end up in an infinite loop. + // + // TODO(zoff99): Fix this to exit the loop properly when we're done + // requesting all chunks for all file transfers. + const uint32_t MAX_FT_LOOPS = 16; + + while (((free_slots > 0) || loop_counter == 0) && any_active_fts && (loop_counter < MAX_FT_LOOPS)) { + any_active_fts = do_all_filetransfers(m, friendnumber, userdata, &free_slots); + loop_counter++; + } } + /* Run this when the friend disconnects. * Kill all current file transfers. */ static void break_files(const Messenger *m, int32_t friendnumber) { - uint32_t i; - // TODO(irungentoo): Inform the client which file transfers get killed with a callback? - for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) { + for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) { if (m->friendlist[friendnumber].file_sending[i].status != FILESTATUS_NONE) { m->friendlist[friendnumber].file_sending[i].status = FILESTATUS_NONE; } @@ -1619,7 +1658,7 @@ static struct File_Transfers *get_file_transfer(uint8_t receive_send, uint8_t fi } if (ft->status == FILESTATUS_NONE) { - return NULL; + return nullptr; } return ft; @@ -1639,10 +1678,10 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv uint32_t real_filenumber; struct File_Transfers *ft = get_file_transfer(receive_send, filenumber, &real_filenumber, &m->friendlist[friendnumber]); - if (ft == NULL) { + if (ft == nullptr) { LOGGER_DEBUG(m->log, "file control (friend %d, file %d): file transfer does not exist; telling the other to kill it", friendnumber, filenumber); - send_file_control_packet(m, friendnumber, !receive_send, filenumber, FILECONTROL_KILL, 0, 0); + send_file_control_packet(m, friendnumber, !receive_send, filenumber, FILECONTROL_KILL, nullptr, 0); return -1; } @@ -1785,13 +1824,13 @@ static int m_handle_custom_lossy_packet(void *object, int friend_num, const uint return 1; } -void custom_lossy_packet_registerhandler(Messenger *m, void (*packet_handler_callback)(Messenger *m, +void custom_lossy_packet_registerhandler(Messenger *m, void (*lossy_packethandler)(Messenger *m, uint32_t friendnumber, const uint8_t *data, size_t len, void *object)) { - m->lossy_packethandler = packet_handler_callback; + m->lossy_packethandler = lossy_packethandler; } -int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, int (*packet_handler_callback)(Messenger *m, +int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, int (*function)(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t len, void *object), void *object) { if (friend_not_valid(m, friendnumber)) { @@ -1806,8 +1845,7 @@ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, int return -1; } - m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].function = - packet_handler_callback; + m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].function = function; m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].object = object; return 0; } @@ -1867,10 +1905,10 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin return 1; } -void custom_lossless_packet_registerhandler(Messenger *m, void (*packet_handler_callback)(Messenger *m, +void custom_lossless_packet_registerhandler(Messenger *m, void (*lossless_packethandler)(Messenger *m, uint32_t friendnumber, const uint8_t *data, size_t len, void *object)) { - m->lossless_packethandler = packet_handler_callback; + m->lossless_packethandler = lossless_packethandler; } int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length) @@ -1919,7 +1957,7 @@ static int friend_already_added(const uint8_t *real_pk, void *data) Messenger *new_messenger(Messenger_Options *options, unsigned int *error) { if (!options) { - return NULL; + return nullptr; } if (error) { @@ -1929,22 +1967,22 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) Messenger *m = (Messenger *)calloc(1, sizeof(Messenger)); if (!m) { - return NULL; + return nullptr; } m->fr = friendreq_new(); if (!m->fr) { free(m); - return NULL; + return nullptr; } - Logger *log = NULL; + Logger *log = nullptr; if (options->log_callback) { log = logger_new(); - if (log != NULL) { + if (log != nullptr) { logger_callback_log(log, options->log_callback, m, options->log_user_data); } } @@ -1961,34 +1999,37 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) m->net = new_networking_ex(log, ip, options->port_range[0], options->port_range[1], &net_err); } - if (m->net == NULL) { + if (m->net == nullptr) { friendreq_kill(m->fr); + logger_kill(m->log); free(m); if (error && net_err == 1) { *error = MESSENGER_ERROR_PORT; } - return NULL; + return nullptr; } m->dht = new_DHT(m->log, m->net, options->hole_punching_enabled); - if (m->dht == NULL) { + if (m->dht == nullptr) { kill_networking(m->net); friendreq_kill(m->fr); + logger_kill(m->log); free(m); - return NULL; + return nullptr; } m->net_crypto = new_net_crypto(m->log, m->dht, &options->proxy_info); - if (m->net_crypto == NULL) { + if (m->net_crypto == nullptr) { kill_networking(m->net); kill_DHT(m->dht); friendreq_kill(m->fr); + logger_kill(m->log); free(m); - return NULL; + return nullptr; } m->onion = new_onion(m->dht); @@ -2005,14 +2046,16 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) kill_DHT(m->dht); kill_networking(m->net); friendreq_kill(m->fr); + logger_kill(m->log); free(m); - return NULL; + return nullptr; } if (options->tcp_server_port) { - m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, m->dht->self_secret_key, m->onion); + m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, dht_get_self_secret_key(m->dht), + m->onion); - if (m->tcp_server == NULL) { + if (m->tcp_server == nullptr) { kill_friend_connections(m->fr_c); kill_onion(m->onion); kill_onion_announce(m->onion_a); @@ -2021,13 +2064,14 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) kill_DHT(m->dht); kill_networking(m->net); friendreq_kill(m->fr); + logger_kill(m->log); free(m); if (error) { *error = MESSENGER_ERROR_TCP_SERVER; } - return NULL; + return nullptr; } } @@ -2214,8 +2258,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le } case PACKET_ID_MESSAGE: // fall-through - case PACKET_ID_ACTION: - case PACKET_ID_CORRECTION: { + case PACKET_ID_ACTION: { if (data_length == 0) { break; } @@ -2257,10 +2300,14 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le uint8_t filenumber = data[0]; +#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES + if (filenumber >= MAX_CONCURRENT_FILE_PIPES) { break; } +#endif + uint64_t filesize; uint32_t file_type; uint16_t filename_length = data_length - head_length; @@ -2287,7 +2334,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH); VLA(uint8_t, filename_terminated, filename_length + 1); - uint8_t *filename = NULL; + uint8_t *filename = nullptr; if (filename_length) { /* Force NULL terminate file name. */ @@ -2317,10 +2364,14 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le uint8_t filenumber = data[1]; uint8_t control_type = data[2]; +#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES + if (filenumber >= MAX_CONCURRENT_FILE_PIPES) { break; } +#endif + if (handle_filecontrol(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3, userdata) == -1) { // TODO(iphydf): Do something different here? Right now, this // check is pointless. @@ -2337,10 +2388,14 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le uint8_t filenumber = data[0]; +#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES + if (filenumber >= MAX_CONCURRENT_FILE_PIPES) { break; } +#endif + struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber]; if (ft->status != FILESTATUS_TRANSFERRING) { @@ -2355,7 +2410,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le const uint8_t *file_data; if (file_data_length == 0) { - file_data = NULL; + file_data = nullptr; } else { file_data = data + 1; } @@ -2373,7 +2428,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le if (file_data_length && (ft->transferred >= ft->size || file_data_length != MAX_FILE_DATA_SIZE)) { file_data_length = 0; - file_data = NULL; + file_data = nullptr; position = ft->transferred; /* Full file received. */ @@ -2467,7 +2522,7 @@ static void do_friends(Messenger *m, void *userdata) do_receipts(m, i, userdata); do_reqchunk_filecb(m, i, userdata); - m->friendlist[i].last_seen_time = (uint64_t) time(NULL); + m->friendlist[i].last_seen_time = (uint64_t) time(nullptr); } } } @@ -2543,7 +2598,7 @@ void do_messenger(Messenger *m, void *userdata) IP_Port local_ip_port; local_ip_port.port = m->options.tcp_server_port; local_ip_port.ip.family = TOX_AF_INET; - local_ip_port.ip.ip4 = get_ip4_loopback(); + local_ip_port.ip.ip.v4 = get_ip4_loopback(); add_tcp_relay(m->net_crypto, local_ip_port, tcp_server_public_key(m->tcp_server)); } @@ -2571,8 +2626,8 @@ void do_messenger(Messenger *m, void *userdata) uint32_t client, last_pinged; for (client = 0; client < LCLIENT_LIST; client++) { - Client_data *cptr = &m->dht->close_clientlist[client]; - IPPTsPng *assoc = NULL; + const Client_data *cptr = dht_get_close_client(m->dht, client); + const IPPTsPng *assoc = nullptr; uint32_t a; for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) { @@ -2597,7 +2652,7 @@ void do_messenger(Messenger *m, void *userdata) uint32_t friend_idx, dhtfriend; /* dht contains additional "friends" (requests) */ - uint32_t num_dhtfriends = m->dht->num_friends; + uint32_t num_dhtfriends = dht_get_num_friends(m->dht); VLA(int32_t, m2dht, num_dhtfriends); VLA(int32_t, dht2m, num_dhtfriends); @@ -2609,8 +2664,8 @@ void do_messenger(Messenger *m, void *userdata) continue; } - for (dhtfriend = 0; dhtfriend < m->dht->num_friends; dhtfriend++) { - if (id_equal(m->friendlist[friend_idx].real_pk, m->dht->friends_list[dhtfriend].public_key)) { + for (dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); dhtfriend++) { + if (id_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) { m2dht[friend_idx] = dhtfriend; break; } @@ -2623,8 +2678,8 @@ void do_messenger(Messenger *m, void *userdata) } } - if (m->numfriends != m->dht->num_friends) { - LOGGER_TRACE(m->log, "Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends); + if (m->numfriends != dht_get_num_friends(m->dht)) { + LOGGER_TRACE(m->log, "Friend num in DHT %u != friend num in msger %u\n", dht_get_num_friends(m->dht), m->numfriends); } Friend *msgfptr; @@ -2634,10 +2689,10 @@ void do_messenger(Messenger *m, void *userdata) if (dht2m[friend_idx] >= 0) { msgfptr = &m->friendlist[dht2m[friend_idx]]; } else { - msgfptr = NULL; + msgfptr = nullptr; } - dhtfptr = &m->dht->friends_list[friend_idx]; + dhtfptr = dht_get_friend(m->dht, friend_idx); if (msgfptr) { char id_str[IDSTRING_LEN]; @@ -2652,7 +2707,7 @@ void do_messenger(Messenger *m, void *userdata) for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { Client_data *cptr = &dhtfptr->client_list[client]; - IPPTsPng *assoc = NULL; + IPPTsPng *assoc = nullptr; uint32_t a; for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) { @@ -2708,13 +2763,13 @@ struct SAVED_FRIEND { uint64_t last_seen_time; }; -static uint32_t friend_size() +static uint32_t friend_size(void) { uint32_t data = 0; - const struct SAVED_FRIEND temp = { 0 }; + const struct SAVED_FRIEND *const temp = nullptr; -#define VALUE_MEMBER(NAME) data += sizeof(temp.NAME) -#define ARRAY_MEMBER(NAME) data += sizeof(temp.NAME) +#define VALUE_MEMBER(NAME) data += sizeof(temp->NAME) +#define ARRAY_MEMBER(NAME) data += sizeof(temp->NAME) // Exactly the same in friend_load, friend_save, and friend_size VALUE_MEMBER(status); @@ -3061,7 +3116,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3 break; } - unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, 0, data, length, 1); + unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, nullptr, data, length, 1); m->has_added_relays = 0; break; @@ -3074,7 +3129,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3 break; } - int i, num = unpack_nodes(nodes, NUM_SAVED_PATH_NODES, 0, data, length, 0); + int i, num = unpack_nodes(nodes, NUM_SAVED_PATH_NODES, nullptr, data, length, 0); for (i = 0; i < num; ++i) { onion_add_bs_path_node(m->onion_c, nodes[i].ip_port, nodes[i].public_key); |