diff options
Diffstat (limited to 'testing')
-rw-r--r-- | testing/experiment/group_chats.c | 61 | ||||
-rw-r--r-- | testing/experiment/group_chats.h | 12 | ||||
-rw-r--r-- | testing/experiment/group_chats_test.c | 1 |
3 files changed, 56 insertions, 18 deletions
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) | |||
278 | #define PING_TIMEOUT 5 | 278 | #define PING_TIMEOUT 5 |
279 | static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) | 279 | static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) |
280 | { | 280 | { |
281 | if (peernum < 0 || peernum >= chat->numpeers) | 281 | if ((uint32_t)peernum >= chat->numpeers) |
282 | return -1; | 282 | return -1; |
283 | 283 | ||
284 | uint64_t temp_time = unix_time(); | 284 | uint64_t temp_time = unix_time(); |
@@ -296,7 +296,7 @@ static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) | |||
296 | 296 | ||
297 | static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid) | 297 | static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid) |
298 | { | 298 | { |
299 | if (peernum < 0 || peernum >= chat->numpeers) | 299 | if ((uint32_t)peernum >= chat->numpeers) |
300 | return -1; | 300 | return -1; |
301 | 301 | ||
302 | sendnodes_data contents; | 302 | sendnodes_data contents; |
@@ -321,7 +321,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_ | |||
321 | if (len != sizeof(getnodes_data)) | 321 | if (len != sizeof(getnodes_data)) |
322 | return 1; | 322 | return 1; |
323 | 323 | ||
324 | if (peernum < 0 || peernum >= chat->numpeers) | 324 | if ((uint32_t)peernum >= chat->numpeers) |
325 | return 1; | 325 | return 1; |
326 | 326 | ||
327 | getnodes_data contents; | 327 | getnodes_data contents; |
@@ -336,7 +336,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_ | |||
336 | 336 | ||
337 | static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) | 337 | static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) |
338 | { | 338 | { |
339 | if (peernum < 0 || peernum >= chat->numpeers) | 339 | if ((uint32_t)peernum >= chat->numpeers) |
340 | return 1; | 340 | return 1; |
341 | 341 | ||
342 | if (len > sizeof(sendnodes_data) || len < sizeof(uint64_t)) | 342 | 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) | |||
384 | //TODO: | 384 | //TODO: |
385 | int peernum = peer_in_chat(chat, data); | 385 | int peernum = peer_in_chat(chat, data); |
386 | 386 | ||
387 | if (peernum == -1) { /*NOTE: This is just for testing and will be removed later.*/ | ||
388 | peernum = addpeer(chat, data); | ||
389 | } | ||
390 | |||
391 | if (peernum == -1) | 387 | if (peernum == -1) |
392 | return 1; | 388 | return 1; |
393 | 389 | ||
390 | uint64_t temp_time = unix_time(); | ||
391 | /* Spam prevention (1 message per peer per second limit.) | ||
392 | |||
393 | if (chat->group[peernum].last_recv == temp_time) | ||
394 | return 1; | ||
395 | |||
396 | chat->group[peernum].last_recv = temp_time; | ||
397 | */ | ||
394 | uint32_t message_num; | 398 | uint32_t message_num; |
395 | memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); | 399 | memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); |
396 | message_num = ntohl(message_num); | 400 | message_num = ntohl(message_num); |
@@ -403,14 +407,34 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) | |||
403 | 407 | ||
404 | chat->group[peernum].last_message_number = message_num; | 408 | chat->group[peernum].last_message_number = message_num; |
405 | 409 | ||
406 | int handled = 0; | 410 | int handled = 1; |
411 | uint8_t *contents = data + GROUP_DATA_MIN_SIZE; | ||
412 | uint16_t contents_len = len - GROUP_DATA_MIN_SIZE; | ||
413 | |||
414 | switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) { | ||
415 | case 0: /* If message is ping */ | ||
416 | if (contents_len != 0) | ||
417 | return 1; | ||
418 | |||
419 | chat->group[peernum].last_recv_msgping = temp_time; | ||
420 | |||
421 | case 16: /* If message is new peer */ | ||
422 | if (contents_len != crypto_box_PUBLICKEYBYTES) | ||
423 | return 1; | ||
407 | 424 | ||
408 | if (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] == 64) { | 425 | addpeer(chat, contents); |
409 | if (chat->group_message != NULL) /* If message is chat message */ | 426 | break; |
410 | (*chat->group_message)(chat, peernum, data + GROUP_DATA_MIN_SIZE, len - GROUP_DATA_MIN_SIZE, | 427 | |
411 | chat->group_message_userdata); | 428 | case 64: /* If message is chat message */ |
429 | if (chat->group_message != NULL) | ||
430 | (*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata); | ||
431 | |||
432 | break; | ||
433 | |||
434 | default: | ||
435 | handled = 0; | ||
436 | break; | ||
412 | 437 | ||
413 | handled = 1; | ||
414 | } | 438 | } |
415 | 439 | ||
416 | if (handled == 1) { | 440 | if (handled == 1) { |
@@ -465,9 +489,6 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui | |||
465 | 489 | ||
466 | int peernum = peer_in_chat(chat, public_key); | 490 | int peernum = peer_in_chat(chat, public_key); |
467 | 491 | ||
468 | if (peernum == -1)/*NOTE: This is just for testing and will be removed later.*/ | ||
469 | peernum = addpeer(chat, public_key); | ||
470 | |||
471 | if (peernum == -1) | 492 | if (peernum == -1) |
472 | return 1; | 493 | return 1; |
473 | 494 | ||
@@ -493,6 +514,12 @@ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length) | |||
493 | return send_data(chat, message, length, 64); //TODO: better return values? | 514 | return send_data(chat, message, length, 64); //TODO: better return values? |
494 | } | 515 | } |
495 | 516 | ||
517 | uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) | ||
518 | { | ||
519 | addpeer(chat, client_id); | ||
520 | return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, 16); //TODO: better return values? | ||
521 | } | ||
522 | |||
496 | void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), | 523 | void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), |
497 | void *userdata) | 524 | void *userdata) |
498 | { | 525 | { |
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" { | |||
33 | 33 | ||
34 | typedef struct { | 34 | typedef struct { |
35 | uint8_t client_id[crypto_box_PUBLICKEYBYTES]; | 35 | uint8_t client_id[crypto_box_PUBLICKEYBYTES]; |
36 | uint64_t last_recv; | ||
37 | uint64_t pingid; | 36 | uint64_t pingid; |
38 | uint64_t last_pinged; | 37 | uint64_t last_pinged; |
38 | |||
39 | uint64_t last_recv; | ||
40 | uint64_t last_recv_msgping; | ||
39 | uint32_t last_message_number; | 41 | uint32_t last_message_number; |
40 | } Group_Peer; | 42 | } Group_Peer; |
41 | 43 | ||
@@ -78,6 +80,14 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, | |||
78 | */ | 80 | */ |
79 | uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); | 81 | uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); |
80 | 82 | ||
83 | |||
84 | /* | ||
85 | * Tell everyone about a new peer (a person we are inviting for example.) | ||
86 | * | ||
87 | */ | ||
88 | uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id); | ||
89 | |||
90 | |||
81 | /* Create a new group chat. | 91 | /* Create a new group chat. |
82 | * | 92 | * |
83 | * Returns a new group chat instance if success. | 93 | * 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() | |||
84 | ip_port.port = htons(12745); | 84 | ip_port.port = htons(12745); |
85 | 85 | ||
86 | for (i = 0; i < NUM_CHATS; ++i) { | 86 | for (i = 0; i < NUM_CHATS; ++i) { |
87 | group_newpeer(chats[0], chats[i]->self_public_key); | ||
87 | chat_bootstrap(chats[i], ip_port, chats[0]->self_public_key); | 88 | chat_bootstrap(chats[i], ip_port, chats[0]->self_public_key); |
88 | printf("%u\n", i); | 89 | printf("%u\n", i); |
89 | } | 90 | } |