diff options
Diffstat (limited to 'testing/experiment')
-rw-r--r-- | testing/experiment/group_chats.c | 25 | ||||
-rw-r--r-- | testing/experiment/group_chats_test.c | 6 | ||||
-rw-r--r-- | testing/experiment/group_chats_test1.c | 114 |
3 files changed, 139 insertions, 6 deletions
diff --git a/testing/experiment/group_chats.c b/testing/experiment/group_chats.c index faf274d5..635bba72 100644 --- a/testing/experiment/group_chats.c +++ b/testing/experiment/group_chats.c | |||
@@ -110,6 +110,9 @@ static int peer_okping(Group_Chat *chat, uint8_t *client_id) | |||
110 | uint32_t i, j = 0; | 110 | uint32_t i, j = 0; |
111 | uint64_t temp_time = unix_time(); | 111 | uint64_t temp_time = unix_time(); |
112 | 112 | ||
113 | if (memcmp(chat->self_public_key, client_id, crypto_box_PUBLICKEYBYTES) == 0) | ||
114 | return -1; | ||
115 | |||
113 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { | 116 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { |
114 | if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) { | 117 | if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) { |
115 | ++j; | 118 | ++j; |
@@ -201,7 +204,7 @@ static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length, | |||
201 | uint64_t temp_time = unix_time(); | 204 | uint64_t temp_time = unix_time(); |
202 | 205 | ||
203 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { | 206 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { |
204 | if (chat->close[i].ip_port.ip.uint32 != 0 && chat->close[i].last_recv > temp_time + BAD_NODE_TIMEOUT) { | 207 | if (chat->close[i].ip_port.ip.uint32 != 0 && chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) { |
205 | if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, data, length, request_id) == 0) | 208 | if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, data, length, request_id) == 0) |
206 | ++sent; | 209 | ++sent; |
207 | } | 210 | } |
@@ -392,17 +395,21 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) | |||
392 | memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); | 395 | memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); |
393 | message_num = ntohl(message_num); | 396 | message_num = ntohl(message_num); |
394 | 397 | ||
395 | if (message_num - chat->group[peernum].last_message_number > 64 || | 398 | if (chat->group[peernum].last_message_number == 0) { |
396 | message_num == chat->group[peernum].last_message_number) | 399 | chat->group[peernum].last_message_number = message_num; |
400 | } else if (message_num - chat->group[peernum].last_message_number > 64 || | ||
401 | message_num == chat->group[peernum].last_message_number) | ||
397 | return 1; | 402 | return 1; |
398 | 403 | ||
399 | chat->group[peernum].last_message_number = message_num; | 404 | chat->group[peernum].last_message_number = message_num; |
400 | 405 | ||
401 | int handled = 0; | 406 | int handled = 0; |
402 | 407 | ||
403 | if (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] == 64 | 408 | if (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] == 64) { |
404 | && chat->group_message != NULL) { /* If message is chat message */ | 409 | if (chat->group_message != NULL) /* If message is chat message */ |
405 | (*chat->group_message)(chat, peernum, data + GROUP_DATA_MIN_SIZE, len - 1, chat->group_message_userdata); | 410 | (*chat->group_message)(chat, peernum, data + GROUP_DATA_MIN_SIZE, len - GROUP_DATA_MIN_SIZE, |
411 | chat->group_message_userdata); | ||
412 | |||
406 | handled = 1; | 413 | handled = 1; |
407 | } | 414 | } |
408 | 415 | ||
@@ -420,10 +427,16 @@ static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t | |||
420 | return 1; | 427 | return 1; |
421 | 428 | ||
422 | uint8_t packet[MAX_DATA_SIZE]; | 429 | uint8_t packet[MAX_DATA_SIZE]; |
430 | ++chat->message_number; | ||
431 | |||
432 | if (chat->message_number == 0) | ||
433 | chat->message_number = 1; | ||
434 | |||
423 | uint32_t message_num = htonl(chat->message_number); | 435 | uint32_t message_num = htonl(chat->message_number); |
424 | //TODO | 436 | //TODO |
425 | memcpy(packet, chat->self_public_key, crypto_box_PUBLICKEYBYTES); | 437 | memcpy(packet, chat->self_public_key, crypto_box_PUBLICKEYBYTES); |
426 | memcpy(packet + crypto_box_PUBLICKEYBYTES, &message_num, sizeof(message_num)); | 438 | memcpy(packet + crypto_box_PUBLICKEYBYTES, &message_num, sizeof(message_num)); |
439 | memcpy(packet + GROUP_DATA_MIN_SIZE, data, len); | ||
427 | packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id; | 440 | packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id; |
428 | return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, 50); | 441 | return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, 50); |
429 | } | 442 | } |
diff --git a/testing/experiment/group_chats_test.c b/testing/experiment/group_chats_test.c index 8ef5b10e..3626c9eb 100644 --- a/testing/experiment/group_chats_test.c +++ b/testing/experiment/group_chats_test.c | |||
@@ -54,6 +54,11 @@ void print_group(Group_Chat *chat) | |||
54 | } | 54 | } |
55 | } | 55 | } |
56 | 56 | ||
57 | void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata) | ||
58 | { | ||
59 | printf("%u: %s | %u\n", peer_number, message, length); | ||
60 | } | ||
61 | |||
57 | int main() | 62 | int main() |
58 | { | 63 | { |
59 | IP ip; | 64 | IP ip; |
@@ -68,6 +73,7 @@ int main() | |||
68 | exit(1); | 73 | exit(1); |
69 | 74 | ||
70 | networking_registerhandler(chats[i]->net, 48, &handle_groupchatpacket, chats[i]); | 75 | networking_registerhandler(chats[i]->net, 48, &handle_groupchatpacket, chats[i]); |
76 | callback_groupmessage(chats[i], &print_message, 0); | ||
71 | } | 77 | } |
72 | 78 | ||
73 | printf("ok\n"); | 79 | printf("ok\n"); |
diff --git a/testing/experiment/group_chats_test1.c b/testing/experiment/group_chats_test1.c new file mode 100644 index 00000000..f66c2d24 --- /dev/null +++ b/testing/experiment/group_chats_test1.c | |||
@@ -0,0 +1,114 @@ | |||
1 | #include "group_chats.h" | ||
2 | #define NUM_CHATS 8 | ||
3 | |||
4 | #ifdef WIN32 | ||
5 | #define c_sleep(x) Sleep(1*x) | ||
6 | #else | ||
7 | #define c_sleep(x) usleep(1000*x) | ||
8 | #endif | ||
9 | Group_Chat *chat; | ||
10 | |||
11 | void print_close(Group_Close *close) | ||
12 | { | ||
13 | uint32_t i, j; | ||
14 | IP_Port p_ip; | ||
15 | printf("___________________CLOSE________________________________\n"); | ||
16 | |||
17 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; i++) { | ||
18 | printf("ClientID: "); | ||
19 | |||
20 | for (j = 0; j < CLIENT_ID_SIZE; j++) { | ||
21 | printf("%02hhX", close[i].client_id[j]); | ||
22 | } | ||
23 | |||
24 | p_ip = close[i].ip_port; | ||
25 | printf("\nIP: %u.%u.%u.%u Port: %u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3], | ||
26 | ntohs(p_ip.port)); | ||
27 | printf("\nTimestamp: %llu", (long long unsigned int) close[i].last_recv); | ||
28 | printf("\n"); | ||
29 | } | ||
30 | } | ||
31 | |||
32 | void print_group(Group_Chat *chat) | ||
33 | { | ||
34 | uint32_t i, j; | ||
35 | printf("-----------------\nClientID: "); | ||
36 | |||
37 | for (j = 0; j < CLIENT_ID_SIZE; j++) { | ||
38 | printf("%02hhX", chat->self_public_key[j]); | ||
39 | } | ||
40 | |||
41 | printf("\n___________________GROUP________________________________\n"); | ||
42 | |||
43 | for (i = 0; i < chat->numpeers; i++) { | ||
44 | printf("ClientID: "); | ||
45 | |||
46 | for (j = 0; j < CLIENT_ID_SIZE; j++) { | ||
47 | printf("%02hhX", chat->group[i].client_id[j]); | ||
48 | } | ||
49 | |||
50 | printf("\nTimestamp: %llu", (long long unsigned int) chat->group[i].last_recv); | ||
51 | printf("\nlast_pinged: %llu", (long long unsigned int) chat->group[i].last_pinged); | ||
52 | printf("\npingid: %llu", (long long unsigned int) chat->group[i].pingid); | ||
53 | printf("\n"); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | unsigned char *hex_string_to_bin(char hex_string[]) | ||
58 | { | ||
59 | size_t len = strlen(hex_string); | ||
60 | unsigned char *val = malloc(len); | ||
61 | char *pos = hex_string; | ||
62 | int i; | ||
63 | |||
64 | for (i = 0; i < len; ++i, pos += 2) | ||
65 | sscanf(pos, "%2hhx", &val[i]); | ||
66 | |||
67 | return val; | ||
68 | } | ||
69 | |||
70 | void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata) | ||
71 | { | ||
72 | printf("%u: %s | %u\n", peer_number, message, length); | ||
73 | } | ||
74 | |||
75 | int main(int argc, char *argv[]) | ||
76 | { | ||
77 | IP ip; | ||
78 | ip.uint32 = 0; | ||
79 | uint32_t i; | ||
80 | |||
81 | chat = new_groupchat(new_networking(ip, 12745)); | ||
82 | |||
83 | if (chat == 0) | ||
84 | exit(1); | ||
85 | |||
86 | networking_registerhandler(chat->net, 48, &handle_groupchatpacket, chat); | ||
87 | |||
88 | callback_groupmessage(chat, &print_message, 0); | ||
89 | |||
90 | printf("ok\n"); | ||
91 | IP_Port bootstrap_ip_port; | ||
92 | bootstrap_ip_port.port = htons(atoi(argv[2])); | ||
93 | /* bootstrap_ip_port.ip.c[0] = 127; | ||
94 | * bootstrap_ip_port.ip.c[1] = 0; | ||
95 | * bootstrap_ip_port.ip.c[2] = 0; | ||
96 | * bootstrap_ip_port.ip.c[3] = 1; */ | ||
97 | bootstrap_ip_port.ip.uint32 = inet_addr(argv[1]); | ||
98 | |||
99 | chat_bootstrap(chat, bootstrap_ip_port, hex_string_to_bin(argv[3])); | ||
100 | |||
101 | while (1) { | ||
102 | |||
103 | networking_poll(chat->net); | ||
104 | do_groupchat(chat); | ||
105 | printf("%u ", chat->numpeers); | ||
106 | printf("%u\n", group_sendmessage(chat, "Install Gentoo", sizeof("Install Gentoo"))); | ||
107 | //print_close(chat->close); | ||
108 | // print_group(chat); | ||
109 | |||
110 | c_sleep(100); | ||
111 | } | ||
112 | |||
113 | return 0; | ||
114 | } | ||