From 561024eb35497c8f2cc7e4b7d7ebbe607773d655 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Sun, 31 May 2020 22:56:56 -0400 Subject: [PATCH] Fix some connection/sync issues We now make sure that peers without a valid shared state can't accept invite requests or sync requests. In addition, connected peers no longer accept announcements --- toxcore/group_chats.c | 37 +++++++++++++++++++++++++++++++------ toxcore/group_chats.h | 7 +++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 8f394121c31..3cc7efb41dc 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -1072,8 +1072,9 @@ static int handle_gc_sync_response(Messenger *m, int group_number, int peer_numb return -1; } - chat->connection_state = CS_CONNECTED; - send_gc_peer_exchange(c, chat, gconn); + if (chat->connection_state == CS_CONNECTED) { + send_gc_peer_exchange(c, chat, gconn); + } if (c->self_join && chat->time_connected == 0) { (*c->self_join)(m, group_number, c->self_join_userdata); @@ -1134,6 +1135,10 @@ static int handle_gc_sync_request(const Messenger *m, int group_number, int peer return -1; } + if (chat->connection_state != CS_CONNECTED) { + return -1; + } + if (chat->numpeers <= 1) { LOGGER_WARNING(m->log, "Got sync request with empty peer list?"); return -1; @@ -1377,7 +1382,11 @@ static int handle_gc_invite_response(const Messenger *m, int group_number, GC_Co sync_flags = 0xffff; } - return send_gc_sync_request(chat, gconn, sync_flags); + if (chat->connection_state != CS_CONNECTED) { + return send_gc_sync_request(chat, gconn, sync_flags); + } + + return 0; } static int handle_gc_invite_response_reject(Messenger *m, int group_number, const uint8_t *data, uint32_t length) @@ -1444,6 +1453,10 @@ static int handle_gc_invite_request(Messenger *m, int group_number, uint32_t pee return -1; } + if (chat->connection_state != CS_CONNECTED) { + return -1; + } + GC_Connection *gconn = gcc_get_connection(chat, peer_number); if (gconn == nullptr) { @@ -2221,6 +2234,8 @@ static int handle_gc_sanctions_list(Messenger *m, int group_number, uint32_t pee } } + chat->connection_state = CS_CONNECTED; + return 0; } @@ -4228,7 +4243,11 @@ static int handle_gc_handshake_response(const Messenger *m, int group_number, co break; case HS_PEER_INFO_EXCHANGE: - ret = send_gc_peer_exchange(m->group_handler, chat, gconn); + if (chat->connection_state != CS_CONNECTED) { + ret = -1; + } else { + ret = send_gc_peer_exchange(m->group_handler, chat, gconn); + } break; default: @@ -5475,7 +5494,7 @@ static int create_new_group(GC_Session *c, const uint8_t *nick, size_t nick_leng chat->group_number = group_number; chat->numpeers = 0; - chat->connection_state = CS_CONNECTING; + chat->connection_state = CS_CONNECTED; chat->net = m->net; chat->mono_time = m->mono_time; chat->logger = m->log; @@ -5594,7 +5613,6 @@ int gc_group_load(GC_Session *c, const Saved_Group *save, int group_number) chat->group_number = group_number; chat->numpeers = 0; - chat->connection_state = is_active_chat ? CS_CONNECTING : CS_DISCONNECTED; chat->join_type = HJ_PRIVATE; chat->net = m->net; chat->mono_time = m->mono_time; @@ -5632,6 +5650,8 @@ int gc_group_load(GC_Session *c, const Saved_Group *save, int group_number) chat->chat_id_hash = get_chat_id_hash(get_chat_id(chat->chat_public_key)); chat->self_public_key_hash = get_peer_key_hash(chat->self_public_key); + chat->connection_state = chat->shared_state.version == 0 ? CS_CONNECTING : CS_CONNECTED; + if (peer_add(m, group_number, nullptr, save->self_public_key) != 0) { return -1; } @@ -5654,6 +5674,7 @@ int gc_group_load(GC_Session *c, const Saved_Group *save, int group_number) } if (!is_active_chat) { + chat->connection_state = CS_DISCONNECTED; chat->save = (Saved_Group *)malloc(sizeof(Saved_Group)); if (chat->save == nullptr) { @@ -6434,6 +6455,10 @@ int add_peers_from_announces(const GC_Session *gc_session, GC_Chat *chat, GC_Ann return -1; } + if (chat->connection_state == CS_CONNECTED && chat->numpeers > 1) { + return 0; + } + int added_peers = 0; for (uint8_t i = 0; i < gc_announces_count; ++i) { diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index d345b2f5538..b32a02c1663 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -99,9 +99,16 @@ typedef enum Group_Peer_Status { } Group_Peer_Status; typedef enum GC_Conn_State { + /* The group index is vacant */ CS_NONE, + + /* Not connected or attempting to connect to other peers */ CS_DISCONNECTED, + + /* Attempting to connect to other peers but does not yet have a valid shared state */ CS_CONNECTING, + + /* Has a valid shared state and is connected or attempting to connect to other peers */ CS_CONNECTED, CS_INVALID, } GC_Conn_State;