diff options
author | aunsane <aunsane@gmail.com> | 2018-08-09 21:34:33 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-08-09 21:34:33 +0300 |
commit | 7258aff5987814c6a106b1e8c4bab975e34b7012 (patch) | |
tree | 0effa93c441b72cce1ad7a91bc6ca5a97d2fc5ab /protocols/Tox/libtox | |
parent | eab7305c241be4d85b86601ae97b4602935b067e (diff) |
Tox: toxcore updated to v0.2.5
Diffstat (limited to 'protocols/Tox/libtox')
18 files changed, 783 insertions, 397 deletions
diff --git a/protocols/Tox/libtox/docs/CHANGELOG.md b/protocols/Tox/libtox/docs/CHANGELOG.md index e80f359878..8b494f074b 100644 --- a/protocols/Tox/libtox/docs/CHANGELOG.md +++ b/protocols/Tox/libtox/docs/CHANGELOG.md @@ -1,9 +1,38 @@ +## v0.2.5 + +### Merged PRs: + +- [#1048](https://github.com/TokTok/c-toxcore/pull/1048) Fix error message in m_send_generic_message +- [#1047](https://github.com/TokTok/c-toxcore/pull/1047) Remove unused `m_callback_log` function. +- [#1041](https://github.com/TokTok/c-toxcore/pull/1041) Avoid multiple for-next expressions. +- [#1037](https://github.com/TokTok/c-toxcore/pull/1037) Run all tests in the Autotools build +- [#1035](https://github.com/TokTok/c-toxcore/pull/1035) Fix problems with initial connections and name-setting in conferences +- [#1032](https://github.com/TokTok/c-toxcore/pull/1032) Use auto_test fixture in some tests and standardise filenames +- [#1030](https://github.com/TokTok/c-toxcore/pull/1030) Make a separate `struct Tox` containing the Messenger. +- [#1029](https://github.com/TokTok/c-toxcore/pull/1029) Add `by_id` and `get_id` functions, renaming from `*_uid`. +- [#1025](https://github.com/TokTok/c-toxcore/pull/1025) More fixed_width ints and incorporating file_saving_test.c +- [#1023](https://github.com/TokTok/c-toxcore/pull/1023) Run buildifier on c-toxcore BUILD files. +- [#1022](https://github.com/TokTok/c-toxcore/pull/1022) Make `resize` in `list.c` return bool instead of 0/1. +- [#1021](https://github.com/TokTok/c-toxcore/pull/1021) Remove redundant casts to the same type. +- [#1020](https://github.com/TokTok/c-toxcore/pull/1020) Add github usernames to TODOs. +- [#1019](https://github.com/TokTok/c-toxcore/pull/1019) Synchronise parameter names in headers with those in the implementation. +- [#1018](https://github.com/TokTok/c-toxcore/pull/1018) Reduce nesting by doing more early returns on error. +- [#1017](https://github.com/TokTok/c-toxcore/pull/1017) Add missing braces in dht_test.c. +- [#1011](https://github.com/TokTok/c-toxcore/pull/1011) Run Clang global static analysis on Travis. +- [#1010](https://github.com/TokTok/c-toxcore/pull/1010) Avoid implementations in .h files or #including .c files. + +### Closed issues: + +- [#1028](https://github.com/TokTok/c-toxcore/issues/1028) qTox crashes 1-2 times a day after update to 0.2.4 +- [#1002](https://github.com/TokTok/c-toxcore/issues/1002) Implement an abstraction over pthread and windows thread synchronisation primitives + ## v0.2.4 ### Merged PRs: +- [#1024](https://github.com/TokTok/c-toxcore/pull/1024) Release v0.2.4 - [#1014](https://github.com/TokTok/c-toxcore/pull/1014) Use string comparison operator in configure.ac. - [#1013](https://github.com/TokTok/c-toxcore/pull/1013) Link -lsocket and -lnsl for socket functions on Solaris. - [#1012](https://github.com/TokTok/c-toxcore/pull/1012) Correct the max hostname length constant. diff --git a/protocols/Tox/libtox/src/libtox.def b/protocols/Tox/libtox/src/libtox.def index 5540b08ac3..9df7b08801 100644 --- a/protocols/Tox/libtox/src/libtox.def +++ b/protocols/Tox/libtox/src/libtox.def @@ -2,12 +2,81 @@ LIBRARY libtox.mir DESCRIPTION EXPORTS -tox_add_tcp_relay +tox_get_salt +tox_is_data_encrypted +tox_pass_decrypt +tox_pass_encrypt +tox_pass_encryption_extra_length +tox_pass_key_decrypt +tox_pass_key_derive +tox_pass_key_derive_with_salt +tox_pass_key_encrypt +tox_pass_key_free +tox_pass_key_length +tox_pass_salt_length +create_recursive_mutex +host_to_net +id_copy +id_equal +max_s32 +min_u32 +min_u64 +net_to_host tox_address_size +tox_file_id_length +tox_hash_length +tox_max_custom_packet_size +tox_max_filename_length +tox_max_friend_request_length +tox_max_hostname_length +tox_max_message_length +tox_max_name_length +tox_max_status_message_length +tox_nospam_size +tox_options_default +tox_options_free +tox_options_get_end_port +tox_options_get_hole_punching_enabled +tox_options_get_ipv6_enabled +tox_options_get_local_discovery_enabled +tox_options_get_log_callback +tox_options_get_log_user_data +tox_options_get_proxy_host +tox_options_get_proxy_port +tox_options_get_proxy_type +tox_options_get_savedata_data +tox_options_get_savedata_length +tox_options_get_savedata_type +tox_options_get_start_port +tox_options_get_tcp_port +tox_options_get_udp_enabled +tox_options_new +tox_options_set_end_port +tox_options_set_hole_punching_enabled +tox_options_set_ipv6_enabled +tox_options_set_local_discovery_enabled +tox_options_set_log_callback +tox_options_set_log_user_data +tox_options_set_proxy_host +tox_options_set_proxy_port +tox_options_set_proxy_type +tox_options_set_savedata_data +tox_options_set_savedata_length +tox_options_set_savedata_type +tox_options_set_start_port +tox_options_set_tcp_port +tox_options_set_udp_enabled +tox_public_key_size +tox_secret_key_size +tox_version_major +tox_version_minor +tox_version_patch +tox_add_tcp_relay tox_bootstrap tox_callback_conference_invite tox_callback_conference_message tox_callback_conference_peer_list_changed +tox_callback_conference_peer_name tox_callback_conference_title tox_callback_file_chunk_request tox_callback_file_recv @@ -24,12 +93,16 @@ tox_callback_friend_status tox_callback_friend_status_message tox_callback_friend_typing tox_callback_self_connection_status +tox_conference_by_id +tox_conference_by_uid tox_conference_delete tox_conference_get_chatlist tox_conference_get_chatlist_size +tox_conference_get_id tox_conference_get_title tox_conference_get_title_size tox_conference_get_type +tox_conference_get_uid tox_conference_invite tox_conference_join tox_conference_new @@ -42,7 +115,6 @@ tox_conference_send_message tox_conference_set_title tox_file_control tox_file_get_file_id -tox_file_id_length tox_file_seek tox_file_send tox_file_send_chunk @@ -63,67 +135,13 @@ tox_friend_get_typing tox_friend_send_lossless_packet tox_friend_send_lossy_packet tox_friend_send_message -tox_get_salt tox_get_savedata tox_get_savedata_size tox_hash -tox_hash_length -tox_is_data_encrypted tox_iterate tox_iteration_interval tox_kill -tox_max_custom_packet_size -tox_max_filename_length -tox_max_friend_request_length -tox_max_message_length -tox_max_name_length -tox_max_status_message_length tox_new -tox_options_default -tox_options_free -tox_options_get_end_port -tox_options_get_hole_punching_enabled -tox_options_get_ipv6_enabled -tox_options_get_local_discovery_enabled -tox_options_get_log_callback -tox_options_get_log_user_data -tox_options_get_proxy_host -tox_options_get_proxy_port -tox_options_get_proxy_type -tox_options_get_savedata_data -tox_options_get_savedata_length -tox_options_get_savedata_type -tox_options_get_start_port -tox_options_get_tcp_port -tox_options_get_udp_enabled -tox_options_new -tox_options_set_end_port -tox_options_set_hole_punching_enabled -tox_options_set_ipv6_enabled -tox_options_set_local_discovery_enabled -tox_options_set_log_callback -tox_options_set_log_user_data -tox_options_set_proxy_host -tox_options_set_proxy_port -tox_options_set_proxy_type -tox_options_set_savedata_data -tox_options_set_savedata_length -tox_options_set_savedata_type -tox_options_set_start_port -tox_options_set_tcp_port -tox_options_set_udp_enabled -tox_pass_decrypt -tox_pass_encrypt -tox_pass_encryption_extra_length -tox_pass_key_decrypt -tox_pass_key_derive -tox_pass_key_derive_with_salt -tox_pass_key_encrypt -tox_pass_key_free -tox_pass_key_length -tox_pass_salt_length -tox_public_key_size -tox_secret_key_size tox_self_get_address tox_self_get_connection_status tox_self_get_dht_id @@ -145,8 +163,3 @@ tox_self_set_status tox_self_set_status_message tox_self_set_typing tox_version_is_compatible -tox_version_major -tox_version_minor -tox_version_patch -tox_conference_get_uid -tox_conference_by_uid diff --git a/protocols/Tox/libtox/src/toxcore/DHT.c b/protocols/Tox/libtox/src/toxcore/DHT.c index 01fcfd66b1..543ee19176 100644 --- a/protocols/Tox/libtox/src/toxcore/DHT.c +++ b/protocols/Tox/libtox/src/toxcore/DHT.c @@ -1704,12 +1704,10 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co /* If node is not dead. */ Client_data *client = &list[i]; - IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; - - uint32_t j = 0; + IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4 }; - for (IPPTsPng * const *it = assocs; *it; ++it, ++j) { - IPPTsPng *const assoc = *it; + for (uint32_t j = 0; j < sizeof(assocs) / sizeof(assocs[0]); ++j) { + IPPTsPng *const assoc = assocs[j]; if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { sort = 0; diff --git a/protocols/Tox/libtox/src/toxcore/Messenger.c b/protocols/Tox/libtox/src/toxcore/Messenger.c index c45debed47..a52c07f0b7 100644 --- a/protocols/Tox/libtox/src/toxcore/Messenger.c +++ b/protocols/Tox/libtox/src/toxcore/Messenger.c @@ -505,7 +505,7 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con } if (length >= MAX_CRYPTO_DATA_SIZE) { - LOGGER_ERROR(m->log, "Message length %d is too large", friendnumber); + LOGGER_ERROR(m->log, "Message length %u is too large", length); return -2; } @@ -842,11 +842,6 @@ static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t m->friendlist[friendnumber].is_typing = is_typing; } -void m_callback_log(Messenger *m, logger_cb *function, void *context, void *userdata) -{ - logger_callback_log(m->log, function, context, userdata); -} - /* Set the function that will be executed when a friend request is received. */ void m_callback_friendrequest(Messenger *m, m_friend_request_cb *function) { @@ -1985,7 +1980,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) return nullptr; } - logger_callback_log(m->log, options->log_callback, m, options->log_user_data); + logger_callback_log(m->log, options->log_callback, options->log_context, options->log_user_data); unsigned int net_err = 0; @@ -2631,10 +2626,11 @@ void do_messenger(Messenger *m, void *userdata) for (client = 0; client < LCLIENT_LIST; ++client) { const Client_data *cptr = dht_get_close_client(m->dht, client); - const IPPTsPng *assoc = nullptr; - uint32_t a; + const IPPTsPng *const assocs[] = { &cptr->assoc4, &cptr->assoc4, nullptr }; + + for (const IPPTsPng * const *it = assocs; *it; ++it) { + const IPPTsPng *const assoc = *it; - for (a = 0, assoc = &cptr->assoc4; a < 2; ++a, assoc = &cptr->assoc6) { if (ip_isset(&assoc->ip_port.ip)) { last_pinged = m->lastdump - assoc->last_pinged; diff --git a/protocols/Tox/libtox/src/toxcore/Messenger.h b/protocols/Tox/libtox/src/toxcore/Messenger.h index 8a4686f12e..8cab9801a0 100644 --- a/protocols/Tox/libtox/src/toxcore/Messenger.h +++ b/protocols/Tox/libtox/src/toxcore/Messenger.h @@ -61,6 +61,7 @@ typedef struct Messenger_Options { bool local_discovery_enabled; logger_cb *log_callback; + void *log_context; void *log_user_data; } Messenger_Options; @@ -480,10 +481,6 @@ int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing); */ int m_get_istyping(const Messenger *m, int32_t friendnumber); -/* Set the logger callback. - */ -void m_callback_log(Messenger *m, logger_cb *function, void *context, void *userdata); - /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, size_t length) */ @@ -689,14 +686,14 @@ int m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, * return 0 on success. */ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, - m_lossy_rtp_packet_cb *packet_handler_callback, void *object); + m_lossy_rtp_packet_cb *function, void *object); /**********************************************/ /* Set handlers for custom lossy packets. * */ -void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb *packet_handler_callback); +void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb *lossy_packethandler); /* High level function to send custom lossy packets. * @@ -713,7 +710,7 @@ int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const u /* Set handlers for custom lossless packets. * */ -void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_packet_cb *packet_handler_callback); +void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_packet_cb *lossless_packethandler); /* High level function to send custom lossless packets. * diff --git a/protocols/Tox/libtox/src/toxcore/TCP_client.h b/protocols/Tox/libtox/src/toxcore/TCP_client.h index 8bfe415563..6d1cbb3bd3 100644 --- a/protocols/Tox/libtox/src/toxcore/TCP_client.h +++ b/protocols/Tox/libtox/src/toxcore/TCP_client.h @@ -60,7 +60,7 @@ TCP_Client_Status tcp_con_status(const TCP_Client_Connection *con); void *tcp_con_custom_object(const TCP_Client_Connection *con); uint32_t tcp_con_custom_uint(const TCP_Client_Connection *con); void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object); -void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t uint); +void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value); /* Create new TCP connection to ip_port/public_key */ diff --git a/protocols/Tox/libtox/src/toxcore/group.c b/protocols/Tox/libtox/src/toxcore/group.c index 95de1afbaf..317b885f56 100644 --- a/protocols/Tox/libtox/src/toxcore/group.c +++ b/protocols/Tox/libtox/src/toxcore/group.c @@ -405,8 +405,12 @@ static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *user get_friendcon_public_keys(real_pk, dht_temp_pk, g_c->fr_c, g->close[i].number); if (!pk_in_closest_peers(g, real_pk)) { - g->close[i].type = GROUPCHAT_CLOSE_NONE; - kill_friend_connection(g_c->fr_c, g->close[i].number); + g->close[i].closest = false; + + if (!g->close[i].introducer && !g->close[i].introduced) { + g->close[i].type = GROUPCHAT_CLOSE_NONE; + kill_friend_connection(g_c->fr_c, g->close[i].number); + } } } @@ -618,6 +622,8 @@ static int setnick(Group_Chats *g_c, uint32_t groupnumber, int peer_index, const return -1; } + g->group[peer_index].nick_updated = true; + /* same name as already stored? */ if (g->group[peer_index].nick_len == nick_len) { if (nick_len == 0 || !memcmp(g->group[peer_index].nick, nick, nick_len)) { @@ -740,7 +746,7 @@ static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t gr } if (g->close[i].number == (uint32_t)friendcon_id) { - g->close[i].closest = closest; + g->close[i].closest |= closest; return i; /* Already in list. */ } } @@ -756,6 +762,8 @@ static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t gr g->close[ind].type = GROUPCHAT_CLOSE_CONNECTION; g->close[ind].number = friendcon_id; g->close[ind].closest = closest; + g->close[ind].introducer = false; + g->close[ind].introduced = false; // TODO(irungentoo): friend_connection_callbacks(g_c->m->fr_c, friendcon_id, GROUPCHAT_CALLBACK_INDEX, &g_handle_status, &g_handle_packet, &handle_lossy, g_c, friendcon_id); @@ -1062,6 +1070,7 @@ static unsigned int send_lossy_group_peer(Friend_Connections *fr_c, int friendco * return 0 on success. * return -1 if groupnumber is invalid. * return -2 if invite packet failed to send. + * return -3 if we are not connected to the group chat. */ int invite_friend(Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber) { @@ -1071,6 +1080,10 @@ int invite_friend(Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber) return -1; } + if (g->status != GROUPCHAT_STATUS_CONNECTED) { + return -3; + } + uint8_t invite[INVITE_PACKET_SIZE]; invite[0] = INVITE_ID; uint16_t groupchat_num = net_htons((uint16_t)groupnumber); @@ -1081,7 +1094,6 @@ int invite_friend(Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber) return 0; } - wipe_group_chat(g_c, groupnumber); return -2; } @@ -1148,6 +1160,7 @@ int join_groupchat(Group_Chats *g_c, uint32_t friendnumber, uint8_t expected_typ g->close[close_index].group_number = other_groupnum; g->close[close_index].type = GROUPCHAT_CLOSE_ONLINE; g->number_joined = friendcon_id; + g->close[close_index].introducer = true; } send_peer_query(g_c, friendcon_id, other_groupnum); @@ -1503,6 +1516,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con if (close_index != -1) { g->close[close_index].group_number = other_groupnum; g->close[close_index].type = GROUPCHAT_CLOSE_ONLINE; + g->close[close_index].introduced = true; } group_new_peer_send(g_c, groupnum, peer_number, real_pk, temp_pk); @@ -1606,21 +1620,6 @@ static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_ g->close[index].type = GROUPCHAT_CLOSE_ONLINE; send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->identifier); - if (g->number_joined != -1 && count_close_connected(g) >= DESIRED_CLOSE_CONNECTIONS) { - int fr_close_index = friend_in_close(g, g->number_joined); - - if (fr_close_index == -1) { - return -1; - } - - if (!g->close[fr_close_index].closest) { - g->close[fr_close_index].type = GROUPCHAT_CLOSE_NONE; - send_peer_kill(g_c, g->close[fr_close_index].number, g->close[fr_close_index].group_number); - kill_friend_connection(g_c->fr_c, g->close[fr_close_index].number); - g->number_joined = -1; - } - } - return 0; } @@ -1729,11 +1728,6 @@ static int handle_send_peers(Group_Chats *g_c, uint32_t groupnumber, const uint8 memcpy(&peer_num, d, sizeof(peer_num)); peer_num = net_ntohs(peer_num); d += sizeof(uint16_t); - int peer_index = addpeer(g_c, groupnumber, d, d + CRYPTO_PUBLIC_KEY_SIZE, peer_num, userdata, true); - - if (peer_index == -1) { - return -1; - } if (g->status == GROUPCHAT_STATUS_VALID && public_key_cmp(d, nc_get_self_public_key(g_c->m->net_crypto)) == 0) { @@ -1742,6 +1736,12 @@ static int handle_send_peers(Group_Chats *g_c, uint32_t groupnumber, const uint8 group_name_send(g_c, groupnumber, g_c->m->name, g_c->m->name_length); } + int peer_index = addpeer(g_c, groupnumber, d, d + CRYPTO_PUBLIC_KEY_SIZE, peer_num, userdata, true); + + if (peer_index == -1) { + return -1; + } + d += CRYPTO_PUBLIC_KEY_SIZE * 2; uint8_t name_length = *d; d += 1; @@ -1750,7 +1750,10 @@ static int handle_send_peers(Group_Chats *g_c, uint32_t groupnumber, const uint8 return -1; } - setnick(g_c, groupnumber, peer_index, d, name_length, userdata, true); + if (!g->group[peer_index].nick_updated) { + setnick(g_c, groupnumber, peer_index, d, name_length, userdata, true); + } + d += name_length; } @@ -2037,6 +2040,58 @@ int send_group_lossy_packet(const Group_Chats *g_c, uint32_t groupnumber, const return 0; } +static Message_Info *find_message_slot_or_reject(uint32_t message_number, uint8_t message_id, Group_Peer *peer) +{ + const bool ignore_older = (message_id == GROUP_MESSAGE_NAME_ID || message_id == GROUP_MESSAGE_TITLE_ID); + + Message_Info *i; + + for (i = peer->last_message_infos; i < peer->last_message_infos + peer->num_last_message_infos; ++i) { + if (message_number > i->message_number) { + break; + } + + if (message_number == i->message_number) { + return nullptr; + } + + if (ignore_older && message_id == i->message_id) { + return nullptr; + } + } + + return i; +} + +/* Stores message info in peer->last_message_infos. + * + * return true if message should be processed; + * return false otherwise. + */ +static bool check_message_info(uint32_t message_number, uint8_t message_id, Group_Peer *peer) +{ + Message_Info *const i = find_message_slot_or_reject(message_number, message_id, peer); + + if (i == nullptr) { + return false; + } + + if (i == peer->last_message_infos + MAX_LAST_MESSAGE_INFOS) { + return false; + } + + if (peer->num_last_message_infos < MAX_LAST_MESSAGE_INFOS) { + ++peer->num_last_message_infos; + } + + memmove(i + 1, i, ((peer->last_message_infos + peer->num_last_message_infos - 1) - i) * sizeof(Message_Info)); + + i->message_number = message_number; + i->message_id = message_id; + + return true; +} + static void handle_message_packet_group(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length, int close_index, void *userdata) { @@ -2063,23 +2118,41 @@ static void handle_message_packet_group(Group_Chats *g_c, uint32_t groupnumber, return; } + if (g->number_joined != -1 && count_close_connected(g) >= DESIRED_CLOSE_CONNECTIONS) { + const int fr_close_index = friend_in_close(g, g->number_joined); + + if (fr_close_index >= 0 && fr_close_index != close_index && !g->close[fr_close_index].closest) { + uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE]; + get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->close[fr_close_index].number); + + if (id_equal(g->group[index].real_pk, real_pk)) { + /* Received message from peer relayed via another peer, so + * the introduction was successful */ + g->number_joined = -1; + g->close[fr_close_index].introducer = false; + + if (!g->close[fr_close_index].closest && !g->close[fr_close_index].introduced) { + g->close[fr_close_index].type = GROUPCHAT_CLOSE_NONE; + send_peer_kill(g_c, g->close[fr_close_index].number, g->close[fr_close_index].group_number); + kill_friend_connection(g_c->fr_c, g->close[fr_close_index].number); + } + } + } + } + uint32_t message_number; memcpy(&message_number, data + sizeof(uint16_t), sizeof(message_number)); message_number = net_ntohl(message_number); - if (g->group[index].last_message_number == 0) { - g->group[index].last_message_number = message_number; - } else if (message_number - g->group[index].last_message_number > 64 || - message_number == g->group[index].last_message_number) { - return; - } - - g->group[index].last_message_number = message_number; - uint8_t message_id = data[sizeof(uint16_t) + sizeof(message_number)]; const uint8_t *msg_data = data + sizeof(uint16_t) + sizeof(message_number) + 1; uint16_t msg_data_len = length - (sizeof(uint16_t) + sizeof(message_number) + 1); + // FIXME(zugz) update discussion of message numbers in the spec + if (!check_message_info(message_number, message_id, &g->group[index])) { + return; + } + switch (message_id) { case GROUP_MESSAGE_PING_ID: { if (msg_data_len != 0) { @@ -2270,12 +2343,9 @@ static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16 g->group[peer_index].top_lossy_number = message_number; g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1; g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1; - - return 0; } else { // top_distance < MAX_LOSSY_COUNT - unsigned int i; - - for (i = g->group[peer_index].bottom_lossy_number; i != (g->group[peer_index].bottom_lossy_number + top_distance); + for (unsigned int i = g->group[peer_index].bottom_lossy_number; + i != g->group[peer_index].bottom_lossy_number + top_distance; ++i) { g->group[peer_index].recv_lossy[i % MAX_LOSSY_COUNT] = 0; } @@ -2283,9 +2353,10 @@ static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16 g->group[peer_index].top_lossy_number = message_number; g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1; g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1; - - return 0; } + + return 0; + } static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata) diff --git a/protocols/Tox/libtox/src/toxcore/group.h b/protocols/Tox/libtox/src/toxcore/group.h index 9b4541c471..970cf7fb04 100644 --- a/protocols/Tox/libtox/src/toxcore/group.h +++ b/protocols/Tox/libtox/src/toxcore/group.h @@ -39,15 +39,26 @@ typedef enum Groupchat_Type { #define MAX_LOSSY_COUNT 256 +typedef struct Message_Info { + uint32_t message_number; + uint8_t message_id; +} Message_Info; + +#define MAX_LAST_MESSAGE_INFOS 8 + typedef struct Group_Peer { uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE]; uint64_t last_recv; - uint32_t last_message_number; + + Message_Info + last_message_infos[MAX_LAST_MESSAGE_INFOS]; /* received messages, strictly decreasing in message_number */ + uint8_t num_last_message_infos; uint8_t nick[MAX_NAME_LENGTH]; uint8_t nick_len; + bool nick_updated; uint16_t peer_number; @@ -70,7 +81,9 @@ typedef enum Groupchat_Close_Type { typedef struct Groupchat_Close { uint8_t type; /* GROUPCHAT_CLOSE_* */ - uint8_t closest; + bool closest; /* connected to peer because it is one of our closest peers */ + bool introducer; /* connected to peer because it introduced us to the group */ + bool introduced; /* connected to peer because we introduced it to the group */ uint32_t number; uint16_t group_number; } Groupchat_Close; @@ -91,6 +104,7 @@ typedef struct Group_c { Group_Peer *group; uint32_t numpeers; + /* TODO(zugz) rename close to something more accurate - "connected"? */ Groupchat_Close close[MAX_GROUP_CONNECTIONS]; uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE]; diff --git a/protocols/Tox/libtox/src/toxcore/list.c b/protocols/Tox/libtox/src/toxcore/list.c index 4846ce2540..e4c160544e 100644 --- a/protocols/Tox/libtox/src/toxcore/list.c +++ b/protocols/Tox/libtox/src/toxcore/list.c @@ -29,6 +29,7 @@ #include "list.h" +#include <stdbool.h> #include <stdlib.h> #include <string.h> @@ -118,23 +119,22 @@ static int find(const BS_List *list, const uint8_t *data) } } -/* Resized the list list +/** + * Resizes the list. * - * return value: - * 1 : success - * 0 : failure + * @return true on success. */ -static int resize(BS_List *list, uint32_t new_size) +static bool resize(BS_List *list, uint32_t new_size) { if (new_size == 0) { bs_list_free(list); - return 1; + return true; } uint8_t *data = (uint8_t *)realloc(list->data, list->element_size * new_size); if (!data) { - return 0; + return false; } list->data = data; @@ -142,12 +142,12 @@ static int resize(BS_List *list, uint32_t new_size) int *ids = (int *)realloc(list->ids, sizeof(int) * new_size); if (!ids) { - return 0; + return false; } list->ids = ids; - return 1; + return true; } diff --git a/protocols/Tox/libtox/src/toxcore/logger.c b/protocols/Tox/libtox/src/toxcore/logger.c index ac80b194ab..5e16233c9b 100644 --- a/protocols/Tox/libtox/src/toxcore/logger.c +++ b/protocols/Tox/libtox/src/toxcore/logger.c @@ -44,19 +44,19 @@ struct Logger { static const char *logger_level_name(Logger_Level level) { switch (level) { - case LOG_TRACE: + case LOGGER_LEVEL_TRACE: return "TRACE"; - case LOG_DEBUG: + case LOGGER_LEVEL_DEBUG: return "DEBUG"; - case LOG_INFO: + case LOGGER_LEVEL_INFO: return "INFO"; - case LOG_WARNING: + case LOGGER_LEVEL_WARNING: return "WARNING"; - case LOG_ERROR: + case LOGGER_LEVEL_ERROR: return "ERROR"; } diff --git a/protocols/Tox/libtox/src/toxcore/logger.h b/protocols/Tox/libtox/src/toxcore/logger.h index 38cc0ac9fb..dd7edee768 100644 --- a/protocols/Tox/libtox/src/toxcore/logger.h +++ b/protocols/Tox/libtox/src/toxcore/logger.h @@ -29,15 +29,15 @@ #include "ccompat.h" #ifndef MIN_LOGGER_LEVEL -#define MIN_LOGGER_LEVEL LOG_INFO +#define MIN_LOGGER_LEVEL LOGGER_LEVEL_INFO #endif typedef enum Logger_Level { - LOG_TRACE, - LOG_DEBUG, - LOG_INFO, - LOG_WARNING, - LOG_ERROR + LOGGER_LEVEL_TRACE, + LOGGER_LEVEL_DEBUG, + LOGGER_LEVEL_INFO, + LOGGER_LEVEL_WARNING, + LOGGER_LEVEL_ERROR } Logger_Level; typedef struct Logger Logger; @@ -84,10 +84,10 @@ void logger_write( } while (0) /* To log with an logger */ -#define LOGGER_TRACE(log, ...) LOGGER_WRITE(log, LOG_TRACE , __VA_ARGS__) -#define LOGGER_DEBUG(log, ...) LOGGER_WRITE(log, LOG_DEBUG , __VA_ARGS__) -#define LOGGER_INFO(log, ...) LOGGER_WRITE(log, LOG_INFO , __VA_ARGS__) -#define LOGGER_WARNING(log, ...) LOGGER_WRITE(log, LOG_WARNING, __VA_ARGS__) -#define LOGGER_ERROR(log, ...) LOGGER_WRITE(log, LOG_ERROR , __VA_ARGS__) +#define LOGGER_TRACE(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_TRACE , __VA_ARGS__) +#define LOGGER_DEBUG(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_DEBUG , __VA_ARGS__) +#define LOGGER_INFO(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_INFO , __VA_ARGS__) +#define LOGGER_WARNING(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_WARNING, __VA_ARGS__) +#define LOGGER_ERROR(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_ERROR , __VA_ARGS__) #endif /* TOXLOGGER_H */ diff --git a/protocols/Tox/libtox/src/toxcore/network.h b/protocols/Tox/libtox/src/toxcore/network.h index 7f7cde60cc..276896b6b7 100644 --- a/protocols/Tox/libtox/src/toxcore/network.h +++ b/protocols/Tox/libtox/src/toxcore/network.h @@ -76,19 +76,19 @@ extern const Socket net_invalid_socket; /** * Calls send(sockfd, buf, len, MSG_NOSIGNAL). */ -int net_send(Socket sockfd, const void *buf, size_t len); +int net_send(Socket sock, const void *buf, size_t len); /** * Calls recv(sockfd, buf, len, MSG_NOSIGNAL). */ -int net_recv(Socket sockfd, void *buf, size_t len); +int net_recv(Socket sock, void *buf, size_t len); /** * Calls listen(sockfd, backlog). */ -int net_listen(Socket sockfd, int backlog); +int net_listen(Socket sock, int backlog); /** * Calls accept(sockfd, nullptr, nullptr). */ -Socket net_accept(Socket sockfd); +Socket net_accept(Socket sock); /** * return the amount of data in the tcp recv buffer. diff --git a/protocols/Tox/libtox/src/toxcore/onion_client.h b/protocols/Tox/libtox/src/toxcore/onion_client.h index 6e73c3abc7..660ccdd28e 100644 --- a/protocols/Tox/libtox/src/toxcore/onion_client.h +++ b/protocols/Tox/libtox/src/toxcore/onion_client.h @@ -135,7 +135,7 @@ typedef int recv_tcp_relay_cb(void *object, uint32_t number, IP_Port ip_port, co * return 0 on success. */ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, - recv_tcp_relay_cb *tcp_relay_node_callback, void *object, uint32_t number); + recv_tcp_relay_cb *callback, void *object, uint32_t number); typedef void onion_dht_pk_cb(void *data, int32_t number, const uint8_t *dht_public_key, void *userdata); diff --git a/protocols/Tox/libtox/src/toxcore/tox.api.h b/protocols/Tox/libtox/src/toxcore/tox.api.h index 97a85312b4..243606410c 100644 --- a/protocols/Tox/libtox/src/toxcore/tox.api.h +++ b/protocols/Tox/libtox/src/toxcore/tox.api.h @@ -182,7 +182,7 @@ const VERSION_MINOR = 2; * The patch or revision number. Incremented when bugfixes are applied without * changing any functionality or API or ABI. */ -const VERSION_PATCH = 4; +const VERSION_PATCH = 5; /** * A macro to check at preprocessing time whether the client code is compatible @@ -245,10 +245,17 @@ const SECRET_KEY_SIZE = 32; /** * The size of a Tox Conference unique id in bytes. + * + * @deprecated Use $CONFERENCE_ID_SIZE instead. */ const CONFERENCE_UID_SIZE = 32; /** + * The size of a Tox Conference unique id in bytes. + */ +const CONFERENCE_ID_SIZE = 32; + +/** * The size of the nospam in bytes when written in a Tox address. */ const NOSPAM_SIZE = sizeof(uint32_t); @@ -2299,6 +2306,10 @@ namespace conference { * The invite packet failed to send. */ FAIL_SEND, + /** + * The client is not connected to the conference. + */ + NO_CONNECTION, } @@ -2464,11 +2475,38 @@ namespace conference { /** * Get the conference unique ID. * + * If id is NULL, this function has no effect. + * + * @param id A memory region large enough to store $CONFERENCE_ID_SIZE bytes. + * + * @return true on success. + */ + const bool get_id(uint32_t conference_number, uint8_t[CONFERENCE_ID_SIZE] id); + + /** + * Return the conference number associated with the specified id. + * + * @param id A byte array containing the conference id ($CONFERENCE_ID_SIZE). + * + * @return the conference number on success, an unspecified value on failure. + */ + const uint32_t by_id(const uint8_t[CONFERENCE_ID_SIZE] id) { + NULL, + /** + * No conference with the given id exists on the conference list. + */ + NOT_FOUND, + } + + /** + * Get the conference unique ID. + * * If uid is NULL, this function has no effect. * * @param uid A memory region large enough to store $CONFERENCE_UID_SIZE bytes. * * @return true on success. + * @deprecated use $get_id instead (exactly the same function, just renamed). */ const bool get_uid(uint32_t conference_number, uint8_t[CONFERENCE_UID_SIZE] uid); @@ -2478,6 +2516,7 @@ namespace conference { * @param uid A byte array containing the conference id ($CONFERENCE_UID_SIZE). * * @return the conference number on success, an unspecified value on failure. + * @deprecated use $by_id instead (exactly the same function, just renamed). */ const uint32_t by_uid(const uint8_t[CONFERENCE_UID_SIZE] uid) { NULL, @@ -2678,6 +2717,7 @@ typedef TOX_ERR_FILE_SEND_CHUNK Tox_Err_File_Send_Chunk; typedef TOX_ERR_CONFERENCE_NEW Tox_Err_Conference_New; typedef TOX_ERR_CONFERENCE_DELETE Tox_Err_Conference_Delete; typedef TOX_ERR_CONFERENCE_PEER_QUERY Tox_Err_Conference_Peer_Query; +typedef TOX_ERR_CONFERENCE_BY_ID Tox_Err_Conference_By_Id; typedef TOX_ERR_CONFERENCE_BY_UID Tox_Err_Conference_By_Uid; typedef TOX_ERR_CONFERENCE_INVITE Tox_Err_Conference_Invite; typedef TOX_ERR_CONFERENCE_JOIN Tox_Err_Conference_Join; diff --git a/protocols/Tox/libtox/src/toxcore/tox.c b/protocols/Tox/libtox/src/toxcore/tox.c index 80ff42c343..bf84f022e7 100644 --- a/protocols/Tox/libtox/src/toxcore/tox.c +++ b/protocols/Tox/libtox/src/toxcore/tox.c @@ -25,13 +25,14 @@ #include "config.h" #endif +#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 600 +#endif -#define TOX_DEFINED -typedef struct Messenger Tox; #include "tox.h" #include <assert.h> +#include <stdlib.h> #include <string.h> #include "Messenger.h" @@ -74,6 +75,240 @@ typedef struct Messenger Tox; #error "TOX_MAX_STATUS_MESSAGE_LENGTH is assumed to be equal to MAX_STATUSMESSAGE_LENGTH" #endif +struct Tox { + Messenger *m; + tox_self_connection_status_cb *self_connection_status_callback; + tox_friend_name_cb *friend_name_callback; + tox_friend_status_message_cb *friend_status_message_callback; + tox_friend_status_cb *friend_status_callback; + tox_friend_connection_status_cb *friend_connection_status_callback; + tox_friend_typing_cb *friend_typing_callback; + tox_friend_read_receipt_cb *friend_read_receipt_callback; + tox_friend_request_cb *friend_request_callback; + tox_friend_message_cb *friend_message_callback; + tox_file_recv_control_cb *file_recv_control_callback; + tox_file_chunk_request_cb *file_chunk_request_callback; + tox_file_recv_cb *file_recv_callback; + tox_file_recv_chunk_cb *file_recv_chunk_callback; + tox_conference_invite_cb *conference_invite_callback; + tox_conference_message_cb *conference_message_callback; + tox_conference_title_cb *conference_title_callback; + tox_conference_peer_name_cb *conference_peer_name_callback; + tox_conference_peer_list_changed_cb *conference_peer_list_changed_callback; + tox_friend_lossy_packet_cb *friend_lossy_packet_callback; + tox_friend_lossless_packet_cb *friend_lossless_packet_callback; +}; + +struct Tox_Userdata { + Tox *tox; + void *user_data; +}; + +static void tox_self_connection_status_handler(Messenger *m, unsigned int connection_status, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->self_connection_status_callback != nullptr) { + tox_data->tox->self_connection_status_callback(tox_data->tox, (Tox_Connection)connection_status, tox_data->user_data); + } +} + +static void tox_friend_name_handler(Messenger *m, uint32_t friend_number, const uint8_t *name, size_t length, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_name_callback != nullptr) { + tox_data->tox->friend_name_callback(tox_data->tox, friend_number, name, length, tox_data->user_data); + } +} + +static void tox_friend_status_message_handler(Messenger *m, uint32_t friend_number, const uint8_t *message, + size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_status_message_callback != nullptr) { + tox_data->tox->friend_status_message_callback(tox_data->tox, friend_number, message, length, tox_data->user_data); + } +} + +static void tox_friend_status_handler(Messenger *m, uint32_t friend_number, unsigned int status, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_status_callback != nullptr) { + tox_data->tox->friend_status_callback(tox_data->tox, friend_number, (Tox_User_Status)status, tox_data->user_data); + } +} + +static void tox_friend_connection_status_handler(Messenger *m, uint32_t friend_number, unsigned int connection_status, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_connection_status_callback != nullptr) { + tox_data->tox->friend_connection_status_callback(tox_data->tox, friend_number, (Tox_Connection)connection_status, + tox_data->user_data); + } +} + +static void tox_friend_typing_handler(Messenger *m, uint32_t friend_number, bool is_typing, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_typing_callback != nullptr) { + tox_data->tox->friend_typing_callback(tox_data->tox, friend_number, is_typing, tox_data->user_data); + } +} + +static void tox_friend_read_receipt_handler(Messenger *m, uint32_t friend_number, uint32_t message_id, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_read_receipt_callback != nullptr) { + tox_data->tox->friend_read_receipt_callback(tox_data->tox, friend_number, message_id, tox_data->user_data); + } +} + +static void tox_friend_request_handler(Messenger *m, const uint8_t *public_key, const uint8_t *message, size_t length, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_request_callback != nullptr) { + tox_data->tox->friend_request_callback(tox_data->tox, public_key, message, length, tox_data->user_data); + } +} + +static void tox_friend_message_handler(Messenger *m, uint32_t friend_number, unsigned int type, const uint8_t *message, + size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_message_callback != nullptr) { + tox_data->tox->friend_message_callback(tox_data->tox, friend_number, (Tox_Message_Type)type, message, length, + tox_data->user_data); + } +} + +static void tox_file_recv_control_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, + unsigned int control, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->file_recv_control_callback != nullptr) { + tox_data->tox->file_recv_control_callback(tox_data->tox, friend_number, file_number, (Tox_File_Control)control, + tox_data->user_data); + } +} + +static void tox_file_chunk_request_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, + uint64_t position, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->file_chunk_request_callback != nullptr) { + tox_data->tox->file_chunk_request_callback(tox_data->tox, friend_number, file_number, position, length, + tox_data->user_data); + } +} + +static void tox_file_recv_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, uint32_t kind, + uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->file_recv_callback != nullptr) { + tox_data->tox->file_recv_callback(tox_data->tox, friend_number, file_number, kind, file_size, filename, filename_length, + tox_data->user_data); + } +} + +static void tox_file_recv_chunk_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, uint64_t position, + const uint8_t *data, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->file_recv_chunk_callback != nullptr) { + tox_data->tox->file_recv_chunk_callback(tox_data->tox, friend_number, file_number, position, data, length, + tox_data->user_data); + } +} + +static void tox_conference_invite_handler(Messenger *m, uint32_t friend_number, int type, const uint8_t *cookie, + size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_invite_callback != nullptr) { + tox_data->tox->conference_invite_callback(tox_data->tox, friend_number, (Tox_Conference_Type)type, cookie, length, + tox_data->user_data); + } +} + +static void tox_conference_message_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number, int type, + const uint8_t *message, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_message_callback != nullptr) { + tox_data->tox->conference_message_callback(tox_data->tox, conference_number, peer_number, (Tox_Message_Type)type, + message, length, tox_data->user_data); + } +} + +static void tox_conference_title_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number, + const uint8_t *title, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_title_callback != nullptr) { + tox_data->tox->conference_title_callback(tox_data->tox, conference_number, peer_number, title, length, + tox_data->user_data); + } +} + +static void tox_conference_peer_name_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number, + const uint8_t *name, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_peer_name_callback != nullptr) { + tox_data->tox->conference_peer_name_callback(tox_data->tox, conference_number, peer_number, name, length, + tox_data->user_data); + } +} + +static void tox_conference_peer_list_changed_handler(Messenger *m, uint32_t conference_number, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_peer_list_changed_callback != nullptr) { + tox_data->tox->conference_peer_list_changed_callback(tox_data->tox, conference_number, tox_data->user_data); + } +} + +static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, const uint8_t *data, size_t length, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_lossy_packet_callback != nullptr) { + tox_data->tox->friend_lossy_packet_callback(tox_data->tox, friend_number, data, length, tox_data->user_data); + } +} + +static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, const uint8_t *data, size_t length, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_lossless_packet_callback != nullptr) { + tox_data->tox->friend_lossless_packet_callback(tox_data->tox, friend_number, data, length, tox_data->user_data); + } +} + bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) { @@ -83,6 +318,13 @@ bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) { + Tox *tox = (Tox *)calloc(1, sizeof(Tox)); + + if (tox == nullptr) { + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); + return nullptr; + } + Messenger_Options m_options = {0}; bool load_savedata_sk = false, load_savedata_tox = false; @@ -99,6 +341,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) case TOX_ERR_OPTIONS_NEW_MALLOC: SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); + free(tox); return nullptr; } } @@ -110,6 +353,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox_options_get_savedata_data(opts) == nullptr || tox_options_get_savedata_length(opts) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); tox_options_free(default_options); + free(tox); return nullptr; } } @@ -118,6 +362,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox_options_get_savedata_length(opts) != TOX_SECRET_KEY_SIZE) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); tox_options_free(default_options); + free(tox); return nullptr; } @@ -126,12 +371,14 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox_options_get_savedata_length(opts) < TOX_ENC_SAVE_MAGIC_LENGTH) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); tox_options_free(default_options); + free(tox); return nullptr; } if (crypto_memcmp(tox_options_get_savedata_data(opts), TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); tox_options_free(default_options); + free(tox); return nullptr; } @@ -147,6 +394,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) m_options.local_discovery_enabled = tox_options_get_local_discovery_enabled(opts); m_options.log_callback = (logger_cb *)tox_options_get_log_callback(opts); + m_options.log_context = tox; m_options.log_user_data = tox_options_get_log_user_data(opts); switch (tox_options_get_proxy_type(opts)) { @@ -165,6 +413,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) default: SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_TYPE); tox_options_free(default_options); + free(tox); return nullptr; } @@ -172,6 +421,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox_options_get_proxy_port(opts) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); tox_options_free(default_options); + free(tox); return nullptr; } @@ -185,6 +435,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. tox_options_free(default_options); + free(tox); return nullptr; } @@ -192,8 +443,11 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } unsigned int m_error; - Messenger *m = new_messenger(&m_options, &m_error); + Messenger *const m = new_messenger(&m_options, &m_error); + tox->m = m; + // TODO(iphydf): Clarify this code, check for NULL before new_groupchats, so + // new_groupchats can assume m is non-NULL. if (!new_groupchats(m)) { kill_messenger(m); @@ -206,6 +460,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } tox_options_free(default_options); + free(tox); return nullptr; } @@ -219,8 +474,29 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); } + m_callback_namechange(m, tox_friend_name_handler); + m_callback_core_connection(m, tox_self_connection_status_handler); + m_callback_statusmessage(m, tox_friend_status_message_handler); + m_callback_userstatus(m, tox_friend_status_handler); + m_callback_connectionstatus(m, tox_friend_connection_status_handler); + m_callback_typingchange(m, tox_friend_typing_handler); + m_callback_read_receipt(m, tox_friend_read_receipt_handler); + m_callback_friendrequest(m, tox_friend_request_handler); + m_callback_friendmessage(m, tox_friend_message_handler); + callback_file_control(m, tox_file_recv_control_handler); + callback_file_reqchunk(m, tox_file_chunk_request_handler); + callback_file_sendrequest(m, tox_file_recv_handler); + callback_file_data(m, tox_file_recv_chunk_handler); + g_callback_group_invite((Group_Chats *)m->conferences_object, tox_conference_invite_handler); + g_callback_group_message((Group_Chats *)m->conferences_object, tox_conference_message_handler); + g_callback_group_title((Group_Chats *)m->conferences_object, tox_conference_title_handler); + g_callback_peer_name((Group_Chats *)m->conferences_object, tox_conference_peer_name_handler); + g_callback_peer_list_changed((Group_Chats *)m->conferences_object, tox_conference_peer_list_changed_handler); + custom_lossy_packet_registerhandler(m, tox_friend_lossy_packet_handler); + custom_lossless_packet_registerhandler(m, tox_friend_lossless_packet_handler); + tox_options_free(default_options); - return m; + return tox; } void tox_kill(Tox *tox) @@ -229,21 +505,22 @@ void tox_kill(Tox *tox) return; } - Messenger *m = tox; + Messenger *m = tox->m; kill_groupchats((Group_Chats *)m->conferences_object); kill_messenger(m); + free(tox); } size_t tox_get_savedata_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return messenger_size(m); } void tox_get_savedata(const Tox *tox, uint8_t *savedata) { if (savedata) { - const Messenger *m = tox; + const Messenger *m = tox->m; messenger_save(m, savedata); } } @@ -275,7 +552,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub for (i = 0; i < count; ++i) { root[i].port = net_htons(port); - Messenger *m = tox; + Messenger *m = tox->m; onion_add_bs_path_node(m->onion_c, root[i], public_key); dht_bootstrap(m->dht, root[i], public_key); } @@ -319,7 +596,7 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t for (i = 0; i < count; ++i) { root[i].port = net_htons(port); - Messenger *m = tox; + Messenger *m = tox->m; add_tcp_relay(m->net_crypto, root[i], public_key); } @@ -336,7 +613,7 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t Tox_Connection tox_self_get_connection_status(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; unsigned int ret = onion_connection_status(m->onion_c); @@ -354,46 +631,46 @@ Tox_Connection tox_self_get_connection_status(const Tox *tox) void tox_callback_self_connection_status(Tox *tox, tox_self_connection_status_cb *callback) { - Messenger *m = tox; - m_callback_core_connection(m, (m_self_connection_status_cb *)callback); + tox->self_connection_status_callback = callback; } uint32_t tox_iteration_interval(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return messenger_run_interval(m); } void tox_iterate(Tox *tox, void *user_data) { - Messenger *m = tox; - do_messenger(m, user_data); - do_groupchats((Group_Chats *)m->conferences_object, user_data); + Messenger *m = tox->m; + struct Tox_Userdata tox_data = { tox, user_data }; + do_messenger(m, &tox_data); + do_groupchats((Group_Chats *)m->conferences_object, &tox_data); } void tox_self_get_address(const Tox *tox, uint8_t *address) { if (address) { - const Messenger *m = tox; + const Messenger *m = tox->m; getaddress(m, address); } } void tox_self_set_nospam(Tox *tox, uint32_t nospam) { - Messenger *m = tox; + Messenger *m = tox->m; set_nospam(m->fr, net_htonl(nospam)); } uint32_t tox_self_get_nospam(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return net_ntohl(get_nospam(m->fr)); } void tox_self_get_public_key(const Tox *tox, uint8_t *public_key) { - const Messenger *m = tox; + const Messenger *m = tox->m; if (public_key) { memcpy(public_key, nc_get_self_public_key(m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE); @@ -402,7 +679,7 @@ void tox_self_get_public_key(const Tox *tox, uint8_t *public_key) void tox_self_get_secret_key(const Tox *tox, uint8_t *secret_key) { - const Messenger *m = tox; + const Messenger *m = tox->m; if (secret_key) { memcpy(secret_key, nc_get_self_secret_key(m->net_crypto), CRYPTO_SECRET_KEY_SIZE); @@ -416,7 +693,7 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set return 0; } - Messenger *m = tox; + Messenger *m = tox->m; if (setname(m, name, length) == 0) { // TODO(irungentoo): function to set different per group names? @@ -431,14 +708,14 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set size_t tox_self_get_name_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return m_get_self_name_size(m); } void tox_self_get_name(const Tox *tox, uint8_t *name) { if (name) { - const Messenger *m = tox; + const Messenger *m = tox->m; getself_name(m, name); } } @@ -450,7 +727,7 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t return 0; } - Messenger *m = tox; + Messenger *m = tox->m; if (m_set_statusmessage(m, status_message, length) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); @@ -463,27 +740,27 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t size_t tox_self_get_status_message_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return m_get_self_statusmessage_size(m); } void tox_self_get_status_message(const Tox *tox, uint8_t *status_message) { if (status_message) { - const Messenger *m = tox; + const Messenger *m = tox->m; m_copy_self_statusmessage(m, status_message); } } void tox_self_set_status(Tox *tox, Tox_User_Status status) { - Messenger *m = tox; + Messenger *m = tox->m; m_set_userstatus(m, status); } Tox_User_Status tox_self_get_status(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; const uint8_t status = m_get_self_userstatus(m); return (Tox_User_Status)status; } @@ -534,7 +811,7 @@ uint32_t tox_friend_add(Tox *tox, const uint8_t *address, const uint8_t *message return UINT32_MAX; } - Messenger *m = tox; + Messenger *m = tox->m; int32_t ret = m_addfriend(m, address, message, length); if (ret >= 0) { @@ -553,7 +830,7 @@ uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t *public_key, Tox_Err_F return UINT32_MAX; } - Messenger *m = tox; + Messenger *m = tox->m; int32_t ret = m_addfriend_norequest(m, public_key); if (ret >= 0) { @@ -567,7 +844,7 @@ uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t *public_key, Tox_Err_F bool tox_friend_delete(Tox *tox, uint32_t friend_number, Tox_Err_Friend_Delete *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = m_delfriend(m, friend_number); // TODO(irungentoo): handle if realloc fails? @@ -587,7 +864,7 @@ uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox return UINT32_MAX; } - const Messenger *m = tox; + const Messenger *m = tox->m; int32_t ret = getfriend_id(m, public_key); if (ret == -1) { @@ -606,7 +883,7 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t * return 0; } - const Messenger *m = tox; + const Messenger *m = tox->m; if (get_real_pk(m, friend_number, public_key) == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND); @@ -619,13 +896,13 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t * bool tox_friend_exists(const Tox *tox, uint32_t friend_number) { - const Messenger *m = tox; + const Messenger *m = tox->m; return m_friend_exists(m, friend_number); } uint64_t tox_friend_get_last_online(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Get_Last_Online *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; uint64_t timestamp = m_get_last_online(m, friend_number); if (timestamp == UINT64_MAX) { @@ -639,14 +916,14 @@ uint64_t tox_friend_get_last_online(const Tox *tox, uint32_t friend_number, Tox_ size_t tox_self_get_friend_list_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return count_friendlist(m); } void tox_self_get_friend_list(const Tox *tox, uint32_t *friend_list) { if (friend_list) { - const Messenger *m = tox; + const Messenger *m = tox->m; // TODO(irungentoo): size parameter? copy_friendlist(m, friend_list, tox_self_get_friend_list_size(tox)); } @@ -654,7 +931,7 @@ void tox_self_get_friend_list(const Tox *tox, uint32_t *friend_list) size_t tox_friend_get_name_size(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_name_size(m, friend_number); if (ret == -1) { @@ -673,7 +950,7 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name, return 0; } - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = getname(m, friend_number, name); if (ret == -1) { @@ -687,13 +964,12 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name, void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *callback) { - Messenger *m = tox; - m_callback_namechange(m, callback); + tox->friend_name_callback = callback; } size_t tox_friend_get_status_message_size(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_statusmessage_size(m, friend_number); if (ret == -1) { @@ -713,7 +989,7 @@ bool tox_friend_get_status_message(const Tox *tox, uint32_t friend_number, uint8 return 0; } - const Messenger *m = tox; + const Messenger *m = tox->m; // TODO(irungentoo): size parameter? int ret = m_copy_statusmessage(m, friend_number, status_message, m_get_statusmessage_size(m, friend_number)); @@ -728,13 +1004,12 @@ bool tox_friend_get_status_message(const Tox *tox, uint32_t friend_number, uint8 void tox_callback_friend_status_message(Tox *tox, tox_friend_status_message_cb *callback) { - Messenger *m = tox; - m_callback_statusmessage(m, callback); + tox->friend_status_message_callback = callback; } Tox_User_Status tox_friend_get_status(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_userstatus(m, friend_number); @@ -749,13 +1024,12 @@ Tox_User_Status tox_friend_get_status(const Tox *tox, uint32_t friend_number, To void tox_callback_friend_status(Tox *tox, tox_friend_status_cb *callback) { - Messenger *m = tox; - m_callback_userstatus(m, (m_friend_status_cb *)callback); + tox->friend_status_callback = callback; } Tox_Connection tox_friend_get_connection_status(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_friend_connectionstatus(m, friend_number); @@ -770,13 +1044,12 @@ Tox_Connection tox_friend_get_connection_status(const Tox *tox, uint32_t friend_ void tox_callback_friend_connection_status(Tox *tox, tox_friend_connection_status_cb *callback) { - Messenger *m = tox; - m_callback_connectionstatus(m, (m_friend_connection_status_cb *)callback); + tox->friend_connection_status_callback = callback; } bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_istyping(m, friend_number); if (ret == -1) { @@ -790,13 +1063,12 @@ bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Frien void tox_callback_friend_typing(Tox *tox, tox_friend_typing_cb *callback) { - Messenger *m = tox; - m_callback_typingchange(m, callback); + tox->friend_typing_callback = callback; } bool tox_self_set_typing(Tox *tox, uint32_t friend_number, bool typing, Tox_Err_Set_Typing *error) { - Messenger *m = tox; + Messenger *m = tox->m; if (m_set_usertyping(m, friend_number, typing) == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND); @@ -851,7 +1123,7 @@ uint32_t tox_friend_send_message(Tox *tox, uint32_t friend_number, Tox_Message_T return 0; } - Messenger *m = tox; + Messenger *m = tox->m; uint32_t message_id = 0; set_message_error(m_send_message_generic(m, friend_number, type, message, length, &message_id), error); return message_id; @@ -859,20 +1131,17 @@ uint32_t tox_friend_send_message(Tox *tox, uint32_t friend_number, Tox_Message_T void tox_callback_friend_read_receipt(Tox *tox, tox_friend_read_receipt_cb *callback) { - Messenger *m = tox; - m_callback_read_receipt(m, callback); + tox->friend_read_receipt_callback = callback; } void tox_callback_friend_request(Tox *tox, tox_friend_request_cb *callback) { - Messenger *m = tox; - m_callback_friendrequest(m, callback); + tox->friend_request_callback = callback; } void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback) { - Messenger *m = tox; - m_callback_friendmessage(m, (m_friend_message_cb *)callback); + tox->friend_message_callback = callback; } bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length) @@ -888,7 +1157,7 @@ bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length) bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, Tox_Err_File_Control *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = file_control(m, friend_number, file_number, control); if (ret == 0) { @@ -937,7 +1206,7 @@ bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, To bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, Tox_Err_File_Seek *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = file_seek(m, friend_number, file_number, position); if (ret == 0) { @@ -978,8 +1247,7 @@ bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint6 void tox_callback_file_recv_control(Tox *tox, tox_file_recv_control_cb *callback) { - Messenger *m = tox; - callback_file_control(m, (m_file_recv_control_cb *)callback); + tox->file_recv_control_callback = callback; } bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_number, uint8_t *file_id, @@ -990,7 +1258,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_ return 0; } - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = file_get_id(m, friend_number, file_number, file_id); if (ret == 0) { @@ -1023,7 +1291,7 @@ uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_id = f_id; } - Messenger *m = tox; + Messenger *m = tox->m; long int file_num = new_filesender(m, friend_number, kind, file_size, file_id, filename, filename_length); if (file_num >= 0) { @@ -1056,7 +1324,7 @@ uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t bool tox_file_send_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length, Tox_Err_File_Send_Chunk *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = file_data(m, friend_number, file_number, position, data, length); if (ret == 0) { @@ -1100,55 +1368,47 @@ bool tox_file_send_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, void tox_callback_file_chunk_request(Tox *tox, tox_file_chunk_request_cb *callback) { - Messenger *m = tox; - callback_file_reqchunk(m, callback); + tox->file_chunk_request_callback = callback; } void tox_callback_file_recv(Tox *tox, tox_file_recv_cb *callback) { - Messenger *m = tox; - callback_file_sendrequest(m, callback); + tox->file_recv_callback = callback; } void tox_callback_file_recv_chunk(Tox *tox, tox_file_recv_chunk_cb *callback) { - Messenger *m = tox; - callback_file_data(m, callback); + tox->file_recv_chunk_callback = callback; } void tox_callback_conference_invite(Tox *tox, tox_conference_invite_cb *callback) { - Messenger *m = tox; - g_callback_group_invite((Group_Chats *)m->conferences_object, (g_conference_invite_cb *)callback); + tox->conference_invite_callback = callback; } void tox_callback_conference_message(Tox *tox, tox_conference_message_cb *callback) { - Messenger *m = tox; - g_callback_group_message((Group_Chats *)m->conferences_object, (g_conference_message_cb *)callback); + tox->conference_message_callback = callback; } void tox_callback_conference_title(Tox *tox, tox_conference_title_cb *callback) { - Messenger *m = tox; - g_callback_group_title((Group_Chats *)m->conferences_object, callback); + tox->conference_title_callback = callback; } void tox_callback_conference_peer_name(Tox *tox, tox_conference_peer_name_cb *callback) { - Messenger *m = tox; - g_callback_peer_name((Group_Chats *)m->conferences_object, callback); + tox->conference_peer_name_callback = callback; } void tox_callback_conference_peer_list_changed(Tox *tox, tox_conference_peer_list_changed_cb *callback) { - Messenger *m = tox; - g_callback_peer_list_changed((Group_Chats *)m->conferences_object, callback); + tox->conference_peer_list_changed_callback = callback; } uint32_t tox_conference_new(Tox *tox, Tox_Err_Conference_New *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = add_groupchat((Group_Chats *)m->conferences_object, GROUPCHAT_TYPE_TEXT); if (ret == -1) { @@ -1162,7 +1422,7 @@ uint32_t tox_conference_new(Tox *tox, Tox_Err_Conference_New *error) bool tox_conference_delete(Tox *tox, uint32_t conference_number, Tox_Err_Conference_Delete *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = del_groupchat((Group_Chats *)m->conferences_object, conference_number); if (ret == -1) { @@ -1176,7 +1436,7 @@ bool tox_conference_delete(Tox *tox, uint32_t conference_number, Tox_Err_Confere uint32_t tox_conference_peer_count(const Tox *tox, uint32_t conference_number, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_number_peers((Group_Chats *)m->conferences_object, conference_number); if (ret == -1) { @@ -1191,7 +1451,7 @@ uint32_t tox_conference_peer_count(const Tox *tox, uint32_t conference_number, T size_t tox_conference_peer_get_name_size(const Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_peername_size((Group_Chats *)m->conferences_object, conference_number, peer_number); switch (ret) { @@ -1211,7 +1471,7 @@ size_t tox_conference_peer_get_name_size(const Tox *tox, uint32_t conference_num bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, uint32_t peer_number, uint8_t *name, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_peername((Group_Chats *)m->conferences_object, conference_number, peer_number, name); switch (ret) { @@ -1231,7 +1491,7 @@ bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, ui bool tox_conference_peer_get_public_key(const Tox *tox, uint32_t conference_number, uint32_t peer_number, uint8_t *public_key, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_peer_pubkey((Group_Chats *)m->conferences_object, conference_number, peer_number, public_key); switch (ret) { @@ -1251,7 +1511,7 @@ bool tox_conference_peer_get_public_key(const Tox *tox, uint32_t conference_numb bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_peernumber_is_ours((Group_Chats *)m->conferences_object, conference_number, peer_number); switch (ret) { @@ -1275,7 +1535,7 @@ bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_numb bool tox_conference_invite(Tox *tox, uint32_t friend_number, uint32_t conference_number, Tox_Err_Conference_Invite *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = invite_friend((Group_Chats *)m->conferences_object, friend_number, conference_number); switch (ret) { @@ -1286,6 +1546,10 @@ bool tox_conference_invite(Tox *tox, uint32_t friend_number, uint32_t conference case -2: SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_INVITE_FAIL_SEND); return false; + + case -3: + SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_INVITE_NO_CONNECTION); + return false; } SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_INVITE_OK); @@ -1295,7 +1559,7 @@ bool tox_conference_invite(Tox *tox, uint32_t friend_number, uint32_t conference uint32_t tox_conference_join(Tox *tox, uint32_t friend_number, const uint8_t *cookie, size_t length, Tox_Err_Conference_Join *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = join_groupchat((Group_Chats *)m->conferences_object, friend_number, GROUPCHAT_TYPE_TEXT, cookie, length); switch (ret) { @@ -1331,7 +1595,7 @@ uint32_t tox_conference_join(Tox *tox, uint32_t friend_number, const uint8_t *co bool tox_conference_send_message(Tox *tox, uint32_t conference_number, Tox_Message_Type type, const uint8_t *message, size_t length, Tox_Err_Conference_Send_Message *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = 0; if (type == TOX_MESSAGE_TYPE_NORMAL) { @@ -1364,7 +1628,7 @@ bool tox_conference_send_message(Tox *tox, uint32_t conference_number, Tox_Messa size_t tox_conference_get_title_size(const Tox *tox, uint32_t conference_number, Tox_Err_Conference_Title *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_title_get_size((Group_Chats *)m->conferences_object, conference_number); switch (ret) { @@ -1384,7 +1648,7 @@ size_t tox_conference_get_title_size(const Tox *tox, uint32_t conference_number, bool tox_conference_get_title(const Tox *tox, uint32_t conference_number, uint8_t *title, Tox_Err_Conference_Title *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_title_get((Group_Chats *)m->conferences_object, conference_number, title); switch (ret) { @@ -1404,7 +1668,7 @@ bool tox_conference_get_title(const Tox *tox, uint32_t conference_number, uint8_ bool tox_conference_set_title(Tox *tox, uint32_t conference_number, const uint8_t *title, size_t length, Tox_Err_Conference_Title *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = group_title_send((Group_Chats *)m->conferences_object, conference_number, title, length); switch (ret) { @@ -1427,13 +1691,13 @@ bool tox_conference_set_title(Tox *tox, uint32_t conference_number, const uint8_ size_t tox_conference_get_chatlist_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return count_chatlist((Group_Chats *)m->conferences_object); } void tox_conference_get_chatlist(const Tox *tox, uint32_t *chatlist) { - const Messenger *m = tox; + const Messenger *m = tox->m; size_t list_size = tox_conference_get_chatlist_size(tox); copy_chatlist((Group_Chats *)m->conferences_object, chatlist, list_size); } @@ -1441,7 +1705,7 @@ void tox_conference_get_chatlist(const Tox *tox, uint32_t *chatlist) Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_number, Tox_Err_Conference_Get_Type *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_get_type((Group_Chats *)m->conferences_object, conference_number); if (ret == -1) { @@ -1453,31 +1717,58 @@ Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_ return (Tox_Conference_Type)ret; } +bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *id /* TOX_CONFERENCE_ID_SIZE bytes */) +{ + return conference_get_uid((Group_Chats *)tox->m->conferences_object, conference_number, id); +} + +// TODO(iphydf): Delete in 0.3.0. bool tox_conference_get_uid(const Tox *tox, uint32_t conference_number, uint8_t *uid /* TOX_CONFERENCE_ID_SIZE bytes */) { - const Messenger *m = tox; - return conference_get_uid((Group_Chats *)m->conferences_object, conference_number, uid); + return tox_conference_get_id(tox, conference_number, uid); } -uint32_t tox_conference_by_uid(const Tox *tox, const uint8_t *uid, Tox_Err_Conference_By_Uid *error) +uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Conference_By_Id *error) { - if (!uid) { - SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_UID_NULL); + if (!id) { + SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_ID_NULL); return UINT32_MAX; } - const Messenger *m = tox; - int32_t ret = conference_by_uid((Group_Chats *)m->conferences_object, uid); + int32_t ret = conference_by_uid((Group_Chats *)tox->m->conferences_object, id); if (ret == -1) { - SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_UID_NOT_FOUND); + SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_ID_NOT_FOUND); return UINT32_MAX; } - SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_UID_OK); + SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_ID_OK); return ret; } +// TODO(iphydf): Delete in 0.3.0. +uint32_t tox_conference_by_uid(const Tox *tox, const uint8_t *uid, Tox_Err_Conference_By_Uid *error) +{ + Tox_Err_Conference_By_Id id_error; + const uint32_t res = tox_conference_by_id(tox, uid, &id_error); + + switch (id_error) { + case TOX_ERR_CONFERENCE_BY_ID_OK: + SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_UID_OK); + break; + + case TOX_ERR_CONFERENCE_BY_ID_NULL: + SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_UID_NULL); + break; + + case TOX_ERR_CONFERENCE_BY_ID_NOT_FOUND: + SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_UID_NOT_FOUND); + break; + } + + return res; +} + static void set_custom_packet_error(int ret, Tox_Err_Friend_Custom_Packet *error) { switch (ret) { @@ -1515,7 +1806,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_ return 0; } - Messenger *m = tox; + Messenger *m = tox->m; if (length == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY); @@ -1542,8 +1833,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_ void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback) { - Messenger *m = tox; - custom_lossy_packet_registerhandler(m, callback); + tox->friend_lossy_packet_callback = callback; } bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, @@ -1554,7 +1844,7 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin return 0; } - Messenger *m = tox; + Messenger *m = tox->m; if (length == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY); @@ -1574,21 +1864,20 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback) { - Messenger *m = tox; - custom_lossless_packet_registerhandler(m, callback); + tox->friend_lossless_packet_callback = callback; } void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id) { if (dht_id) { - const Messenger *m = tox; + const Messenger *m = tox->m; memcpy(dht_id, dht_get_self_public_key(m->dht), CRYPTO_PUBLIC_KEY_SIZE); } } uint16_t tox_self_get_udp_port(const Tox *tox, Tox_Err_Get_Port *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; uint16_t port = net_htons(net_port(m->net)); if (port) { @@ -1602,7 +1891,7 @@ uint16_t tox_self_get_udp_port(const Tox *tox, Tox_Err_Get_Port *error) uint16_t tox_self_get_tcp_port(const Tox *tox, Tox_Err_Get_Port *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; if (m->tcp_server) { SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_OK); diff --git a/protocols/Tox/libtox/src/toxcore/tox.h b/protocols/Tox/libtox/src/toxcore/tox.h index f8279afeee..2c40389bf6 100644 --- a/protocols/Tox/libtox/src/toxcore/tox.h +++ b/protocols/Tox/libtox/src/toxcore/tox.h @@ -183,7 +183,7 @@ uint32_t tox_version_minor(void); * The patch or revision number. Incremented when bugfixes are applied without * changing any functionality or API or ABI. */ -#define TOX_VERSION_PATCH 4 +#define TOX_VERSION_PATCH 5 uint32_t tox_version_patch(void); @@ -250,12 +250,21 @@ uint32_t tox_secret_key_size(void); /** * The size of a Tox Conference unique id in bytes. + * + * @deprecated Use TOX_CONFERENCE_ID_SIZE instead. */ #define TOX_CONFERENCE_UID_SIZE 32 uint32_t tox_conference_uid_size(void); /** + * The size of a Tox Conference unique id in bytes. + */ +#define TOX_CONFERENCE_ID_SIZE 32 + +uint32_t tox_conference_id_size(void); + +/** * The size of the nospam in bytes when written in a Tox address. */ #define TOX_NOSPAM_SIZE (sizeof(uint32_t)) @@ -2604,6 +2613,11 @@ typedef enum TOX_ERR_CONFERENCE_INVITE { */ TOX_ERR_CONFERENCE_INVITE_FAIL_SEND, + /** + * The client is not connected to the conference. + */ + TOX_ERR_CONFERENCE_INVITE_NO_CONNECTION, + } TOX_ERR_CONFERENCE_INVITE; @@ -2817,11 +2831,52 @@ TOX_CONFERENCE_TYPE tox_conference_get_type(const Tox *tox, uint32_t conference_ /** * Get the conference unique ID. * + * If id is NULL, this function has no effect. + * + * @param id A memory region large enough to store TOX_CONFERENCE_ID_SIZE bytes. + * + * @return true on success. + */ +bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *id); + +typedef enum TOX_ERR_CONFERENCE_BY_ID { + + /** + * The function returned successfully. + */ + TOX_ERR_CONFERENCE_BY_ID_OK, + + /** + * One of the arguments to the function was NULL when it was not expected. + */ + TOX_ERR_CONFERENCE_BY_ID_NULL, + + /** + * No conference with the given id exists on the conference list. + */ + TOX_ERR_CONFERENCE_BY_ID_NOT_FOUND, + +} TOX_ERR_CONFERENCE_BY_ID; + + +/** + * Return the conference number associated with the specified id. + * + * @param id A byte array containing the conference id (TOX_CONFERENCE_ID_SIZE). + * + * @return the conference number on success, an unspecified value on failure. + */ +uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, TOX_ERR_CONFERENCE_BY_ID *error); + +/** + * Get the conference unique ID. + * * If uid is NULL, this function has no effect. * * @param uid A memory region large enough to store TOX_CONFERENCE_UID_SIZE bytes. * * @return true on success. + * @deprecated use tox_conference_get_id instead (exactly the same function, just renamed). */ bool tox_conference_get_uid(const Tox *tox, uint32_t conference_number, uint8_t *uid); @@ -2851,6 +2906,7 @@ typedef enum TOX_ERR_CONFERENCE_BY_UID { * @param uid A byte array containing the conference id (TOX_CONFERENCE_UID_SIZE). * * @return the conference number on success, an unspecified value on failure. + * @deprecated use tox_conference_by_id instead (exactly the same function, just renamed). */ uint32_t tox_conference_by_uid(const Tox *tox, const uint8_t *uid, TOX_ERR_CONFERENCE_BY_UID *error); @@ -3054,6 +3110,7 @@ typedef TOX_ERR_FILE_SEND_CHUNK Tox_Err_File_Send_Chunk; typedef TOX_ERR_CONFERENCE_NEW Tox_Err_Conference_New; typedef TOX_ERR_CONFERENCE_DELETE Tox_Err_Conference_Delete; typedef TOX_ERR_CONFERENCE_PEER_QUERY Tox_Err_Conference_Peer_Query; +typedef TOX_ERR_CONFERENCE_BY_ID Tox_Err_Conference_By_Id; typedef TOX_ERR_CONFERENCE_BY_UID Tox_Err_Conference_By_Uid; typedef TOX_ERR_CONFERENCE_INVITE Tox_Err_Conference_Invite; typedef TOX_ERR_CONFERENCE_JOIN Tox_Err_Conference_Join; diff --git a/protocols/Tox/libtox/src/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/utils.c b/protocols/Tox/libtox/src/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/utils.c deleted file mode 100644 index e61ccf3ecf..0000000000 --- a/protocols/Tox/libtox/src/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/utils.c +++ /dev/null @@ -1,78 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif -#ifdef VANILLA_NACL /* toxcore only uses this when libsodium is unavailable */ - -#ifndef __STDC_WANT_LIB_EXT1__ -# define __STDC_WANT_LIB_EXT1__ 1 -#endif -#include <assert.h> -#include <errno.h> -#include <limits.h> -#include <signal.h> -#include <stddef.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#ifdef HAVE_SYS_MMAN_H -# include <sys/mman.h> -#endif - -#include "utils.h" - -#ifdef _WIN32 -# include <windows.h> -# include <wincrypt.h> -#else -# include <unistd.h> -#endif - -#ifdef HAVE_WEAK_SYMBOLS -__attribute__((weak)) void -__sodium_dummy_symbol_to_prevent_lto(void * const pnt, const size_t len) -{ - (void) pnt; - (void) len; -} -#endif - -void -sodium_memzero(void * const pnt, const size_t len) -{ -#ifdef _WIN32 - SecureZeroMemory(pnt, len); -#elif defined(HAVE_MEMSET_S) - if (memset_s(pnt, (rsize_t) len, 0, (rsize_t) len) != 0) { - abort(); - } -#elif defined(HAVE_EXPLICIT_BZERO) - explicit_bzero(pnt, len); -#elif HAVE_WEAK_SYMBOLS - memset(pnt, 0, len); - __sodium_dummy_symbol_to_prevent_lto(pnt, len); -#else - volatile unsigned char *pnt_ = (volatile unsigned char *) pnt; - size_t i = (size_t) 0U; - - while (i < len) { - pnt_[i++] = 0U; - } -#endif -} - -int -sodium_memcmp(const void * const b1_, const void * const b2_, size_t len) -{ - const unsigned char *b1 = (const unsigned char *) b1_; - const unsigned char *b2 = (const unsigned char *) b2_; - size_t i; - unsigned char d = (unsigned char) 0U; - - for (i = 0U; i < len; i++) { - d |= b1[i] ^ b2[i]; - } - return (int) ((1 & ((d - 1) >> 8)) - 1); -} - -#endif diff --git a/protocols/Tox/libtox/src/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/utils.h b/protocols/Tox/libtox/src/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/utils.h deleted file mode 100644 index fb2020c35d..0000000000 --- a/protocols/Tox/libtox/src/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/utils.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif -#ifdef VANILLA_NACL /* toxcore only uses this when libsodium is unavailable */ - -#ifndef __SODIUM_UTILS_H__ -#define __SODIUM_UTILS_H__ - -#include <stddef.h> - -#include "export.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(__cplusplus) || !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L -# define _SODIUM_C99(X) -#else -# define _SODIUM_C99(X) X -#endif - -SODIUM_EXPORT -void sodium_memzero(void * const pnt, const size_t len); - -/* WARNING: sodium_memcmp() must be used to verify if two secret keys - * are equal, in constant time. - * It returns 0 if the keys are equal, and -1 if they differ. - * This function is not designed for lexicographical comparisons. - */ -SODIUM_EXPORT -int sodium_memcmp(const void * const b1_, const void * const b2_, size_t len); - -#ifdef __cplusplus -} -#endif - -#endif - -#endif |