Skip to content

Commit

Permalink
Fix some connection/sync issues
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JFreegman committed Jun 1, 2020
1 parent 5078abe commit 561024e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
37 changes: 31 additions & 6 deletions toxcore/group_chats.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions toxcore/group_chats.h
Expand Up @@ -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;
Expand Down

0 comments on commit 561024e

Please sign in to comment.