From 1ef5d409355f433e2b067f0f7dd57ec9382e701a Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 16 Feb 2014 07:25:10 -0500 Subject: Possibly fixed the issue that group chats would sometimes DOS themselves. --- toxcore/group_chats.c | 27 ++++++++++++++++++++++++++- toxcore/group_chats.h | 3 +++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 6dff52ef..e686ee5d 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -250,6 +250,22 @@ static int addpeer(Group_Chat *chat, uint8_t *client_id) return (chat->numpeers - 1); } +/* + * Set a peer from the group chat to deleted. + * + * return 0 if success + * return -1 if error. + */ +static int del_peer_set(Group_Chat *chat, int peernum) +{ + if ((uint32_t)peernum >= chat->numpeers) + return -1; + + chat->group[peernum].deleted = 1; + chat->group[peernum].deleted_time = unix_time(); + return 0; +} + /* * Delete a peer from the group chat. * @@ -488,6 +504,9 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) if (peernum == -1) return 1; + if (chat->group[peernum].deleted) + return 1; + /* Spam prevention (1 message per peer per second limit.) if (chat->group[peernum].last_recv == temp_time) @@ -531,7 +550,7 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) if (contents_len != 0) return 1; - delpeer(chat, peernum); + del_peer_set(chat, peernum); break; case GROUP_CHAT_PEER_NICK: @@ -758,6 +777,7 @@ static void ping_group(Group_Chat *chat) } } +#define DEL_PEER_DELAY 3 static void del_dead_peers(Group_Chat *chat) { uint32_t i; @@ -766,6 +786,11 @@ static void del_dead_peers(Group_Chat *chat) if (is_timeout(chat->group[i].last_recv_msgping, GROUP_PING_INTERVAL * 4)) { delpeer(chat, i); } + + if (chat->group[i].deleted) { + if (is_timeout(chat->group[i].deleted_time, DEL_PEER_DELAY)) + delpeer(chat, i); + } } } diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index 8d5f5e50..e31aa229 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -41,6 +41,9 @@ typedef struct { uint8_t nick[MAX_NICK_BYTES]; uint16_t nick_len; + + uint8_t deleted; + uint64_t deleted_time; } Group_Peer; typedef struct { -- cgit v1.2.3