diff --git a/testing/experiment/group_chats.c b/testing/experiment/group_chats.c index 635bba72..0698ebf8 100644 --- a/testing/experiment/group_chats.c +++ b/testing/experiment/group_chats.c @@ -278,7 +278,7 @@ static int delpeer(Group_Chat *chat, uint8_t *client_id) #define PING_TIMEOUT 5 static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) { - if (peernum < 0 || peernum >= chat->numpeers) + if ((uint32_t)peernum >= chat->numpeers) return -1; uint64_t temp_time = unix_time(); @@ -296,7 +296,7 @@ static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid) { - if (peernum < 0 || peernum >= chat->numpeers) + if ((uint32_t)peernum >= chat->numpeers) return -1; sendnodes_data contents; @@ -321,7 +321,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_ if (len != sizeof(getnodes_data)) return 1; - if (peernum < 0 || peernum >= chat->numpeers) + if ((uint32_t)peernum >= chat->numpeers) return 1; getnodes_data contents; @@ -336,7 +336,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_ static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) { - if (peernum < 0 || peernum >= chat->numpeers) + if ((uint32_t)peernum >= chat->numpeers) return 1; if (len > sizeof(sendnodes_data) || len < sizeof(uint64_t)) @@ -384,13 +384,17 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) //TODO: int peernum = peer_in_chat(chat, data); - if (peernum == -1) { /*NOTE: This is just for testing and will be removed later.*/ - peernum = addpeer(chat, data); - } - if (peernum == -1) return 1; + uint64_t temp_time = unix_time(); + /* Spam prevention (1 message per peer per second limit.) + + if (chat->group[peernum].last_recv == temp_time) + return 1; + + chat->group[peernum].last_recv = temp_time; + */ uint32_t message_num; memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); message_num = ntohl(message_num); @@ -403,14 +407,34 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) chat->group[peernum].last_message_number = message_num; - int handled = 0; + int handled = 1; + uint8_t *contents = data + GROUP_DATA_MIN_SIZE; + uint16_t contents_len = len - GROUP_DATA_MIN_SIZE; - if (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] == 64) { - if (chat->group_message != NULL) /* If message is chat message */ - (*chat->group_message)(chat, peernum, data + GROUP_DATA_MIN_SIZE, len - GROUP_DATA_MIN_SIZE, - chat->group_message_userdata); + switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) { + case 0: /* If message is ping */ + if (contents_len != 0) + return 1; + + chat->group[peernum].last_recv_msgping = temp_time; + + case 16: /* If message is new peer */ + if (contents_len != crypto_box_PUBLICKEYBYTES) + return 1; + + addpeer(chat, contents); + break; + + case 64: /* If message is chat message */ + if (chat->group_message != NULL) + (*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata); + + break; + + default: + handled = 0; + break; - handled = 1; } if (handled == 1) { @@ -465,9 +489,6 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui int peernum = peer_in_chat(chat, public_key); - if (peernum == -1)/*NOTE: This is just for testing and will be removed later.*/ - peernum = addpeer(chat, public_key); - if (peernum == -1) return 1; @@ -493,6 +514,12 @@ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length) return send_data(chat, message, length, 64); //TODO: better return values? } +uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) +{ + addpeer(chat, client_id); + return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, 16); //TODO: better return values? +} + void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), void *userdata) { diff --git a/testing/experiment/group_chats.h b/testing/experiment/group_chats.h index 42ea3e08..3330ce10 100644 --- a/testing/experiment/group_chats.h +++ b/testing/experiment/group_chats.h @@ -33,9 +33,11 @@ extern "C" { typedef struct { uint8_t client_id[crypto_box_PUBLICKEYBYTES]; - uint64_t last_recv; uint64_t pingid; uint64_t last_pinged; + + uint64_t last_recv; + uint64_t last_recv_msgping; uint32_t last_message_number; } Group_Peer; @@ -78,6 +80,14 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, */ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); + +/* + * Tell everyone about a new peer (a person we are inviting for example.) + * + */ +uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id); + + /* Create a new group chat. * * Returns a new group chat instance if success. diff --git a/testing/experiment/group_chats_test.c b/testing/experiment/group_chats_test.c index 3626c9eb..9ddf5e85 100644 --- a/testing/experiment/group_chats_test.c +++ b/testing/experiment/group_chats_test.c @@ -84,6 +84,7 @@ int main() ip_port.port = htons(12745); for (i = 0; i < NUM_CHATS; ++i) { + group_newpeer(chats[0], chats[i]->self_public_key); chat_bootstrap(chats[i], ip_port, chats[0]->self_public_key); printf("%u\n", i); }