ffi-tox 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/ProjectTox-Core/AUTHORS +0 -0
  3. data/ProjectTox-Core/ChangeLog +0 -0
  4. data/ProjectTox-Core/INSTALL +370 -0
  5. data/ProjectTox-Core/INSTALL.md +455 -56
  6. data/ProjectTox-Core/Makefile.am +35 -0
  7. data/ProjectTox-Core/NEWS +0 -0
  8. data/ProjectTox-Core/README +43 -0
  9. data/ProjectTox-Core/README.md +34 -44
  10. data/ProjectTox-Core/auto_tests/Makefile.inc +110 -0
  11. data/ProjectTox-Core/auto_tests/TCP_test.c +519 -0
  12. data/ProjectTox-Core/auto_tests/assoc_test.c +160 -0
  13. data/ProjectTox-Core/auto_tests/crypto_test.c +302 -0
  14. data/ProjectTox-Core/auto_tests/dht_test.c +362 -0
  15. data/ProjectTox-Core/auto_tests/encryptsave_test.c +104 -0
  16. data/ProjectTox-Core/auto_tests/friends_test.c +238 -0
  17. data/ProjectTox-Core/auto_tests/helpers.h +15 -0
  18. data/ProjectTox-Core/auto_tests/messenger_test.c +365 -0
  19. data/ProjectTox-Core/auto_tests/network_test.c +171 -0
  20. data/ProjectTox-Core/auto_tests/onion_test.c +363 -0
  21. data/ProjectTox-Core/auto_tests/skeleton_test.c +49 -0
  22. data/ProjectTox-Core/auto_tests/tox_test.c +454 -0
  23. data/ProjectTox-Core/auto_tests/toxav_basic_test.c +597 -0
  24. data/ProjectTox-Core/auto_tests/toxav_many_test.c +402 -0
  25. data/ProjectTox-Core/autogen.sh +6 -0
  26. data/ProjectTox-Core/build/Makefile.am +14 -0
  27. data/ProjectTox-Core/configure.ac +694 -0
  28. data/ProjectTox-Core/dist-build/android-arm.sh +3 -0
  29. data/ProjectTox-Core/dist-build/android-armv7.sh +3 -0
  30. data/ProjectTox-Core/dist-build/android-build.sh +59 -0
  31. data/ProjectTox-Core/dist-build/android-mips.sh +3 -0
  32. data/ProjectTox-Core/dist-build/android-x86.sh +3 -0
  33. data/ProjectTox-Core/docs/Group-Chats.md +71 -0
  34. data/ProjectTox-Core/docs/Hardening.txt +60 -0
  35. data/ProjectTox-Core/docs/Hardening_docs.txt +30 -0
  36. data/ProjectTox-Core/docs/Prevent_Tracking.txt +160 -0
  37. data/ProjectTox-Core/docs/TCP_Network.txt +154 -0
  38. data/ProjectTox-Core/docs/TODO +62 -0
  39. data/ProjectTox-Core/docs/Tox_middle_level_network_protocol.txt +120 -0
  40. data/ProjectTox-Core/docs/av_api.md +194 -0
  41. data/ProjectTox-Core/libtoxav.pc.in +11 -0
  42. data/ProjectTox-Core/libtoxcore.pc.in +11 -0
  43. data/ProjectTox-Core/m4/ax_have_epoll.m4 +104 -0
  44. data/ProjectTox-Core/m4/ax_pthread.m4 +317 -0
  45. data/ProjectTox-Core/m4/pkg.m4 +199 -0
  46. data/ProjectTox-Core/other/DHT_bootstrap.c +121 -58
  47. data/ProjectTox-Core/other/DHTnodes +3 -0
  48. data/ProjectTox-Core/other/Makefile.inc +20 -0
  49. data/ProjectTox-Core/other/bootstrap_node_packets.c +65 -0
  50. data/ProjectTox-Core/other/tox.png +0 -0
  51. data/ProjectTox-Core/testing/DHT_test.c +170 -98
  52. data/ProjectTox-Core/testing/Makefile.inc +112 -0
  53. data/ProjectTox-Core/testing/Messenger_test.c +133 -69
  54. data/ProjectTox-Core/testing/dns3_test.c +115 -0
  55. data/ProjectTox-Core/testing/misc_tools.c +59 -13
  56. data/ProjectTox-Core/testing/nTox.c +1127 -264
  57. data/ProjectTox-Core/testing/nTox.h +10 -19
  58. data/ProjectTox-Core/testing/tox_shell.c +159 -0
  59. data/ProjectTox-Core/testing/tox_sync.c +299 -0
  60. data/ProjectTox-Core/tools/README +11 -0
  61. data/ProjectTox-Core/tools/astylerc +11 -0
  62. data/ProjectTox-Core/tools/pre-commit +17 -0
  63. data/ProjectTox-Core/toxav/Makefile.inc +36 -0
  64. data/ProjectTox-Core/toxav/codec.c +357 -0
  65. data/ProjectTox-Core/toxav/codec.h +116 -0
  66. data/ProjectTox-Core/toxav/msi.c +1949 -0
  67. data/ProjectTox-Core/toxav/msi.h +267 -0
  68. data/ProjectTox-Core/toxav/rtp.c +600 -0
  69. data/ProjectTox-Core/toxav/rtp.h +196 -0
  70. data/ProjectTox-Core/toxav/toxav.c +1148 -0
  71. data/ProjectTox-Core/toxav/toxav.h +389 -0
  72. data/ProjectTox-Core/toxcore/DHT.c +2521 -0
  73. data/ProjectTox-Core/toxcore/DHT.h +412 -0
  74. data/ProjectTox-Core/toxcore/LAN_discovery.c +322 -0
  75. data/ProjectTox-Core/{core → toxcore}/LAN_discovery.h +17 -12
  76. data/ProjectTox-Core/toxcore/Makefile.inc +67 -0
  77. data/ProjectTox-Core/toxcore/Messenger.c +3006 -0
  78. data/ProjectTox-Core/toxcore/Messenger.h +818 -0
  79. data/ProjectTox-Core/toxcore/TCP_client.c +858 -0
  80. data/ProjectTox-Core/toxcore/TCP_client.h +156 -0
  81. data/ProjectTox-Core/toxcore/TCP_server.c +1332 -0
  82. data/ProjectTox-Core/toxcore/TCP_server.h +181 -0
  83. data/ProjectTox-Core/toxcore/assoc.c +1033 -0
  84. data/ProjectTox-Core/toxcore/assoc.h +104 -0
  85. data/ProjectTox-Core/toxcore/crypto_core.c +278 -0
  86. data/ProjectTox-Core/toxcore/crypto_core.h +151 -0
  87. data/ProjectTox-Core/toxcore/friend_requests.c +175 -0
  88. data/ProjectTox-Core/toxcore/friend_requests.h +83 -0
  89. data/ProjectTox-Core/toxcore/group_chats.c +837 -0
  90. data/ProjectTox-Core/toxcore/group_chats.h +199 -0
  91. data/ProjectTox-Core/toxcore/list.c +256 -0
  92. data/ProjectTox-Core/toxcore/list.h +85 -0
  93. data/ProjectTox-Core/toxcore/logger.c +153 -0
  94. data/ProjectTox-Core/toxcore/logger.h +84 -0
  95. data/ProjectTox-Core/toxcore/misc_tools.h +70 -0
  96. data/ProjectTox-Core/toxcore/net_crypto.c +2753 -0
  97. data/ProjectTox-Core/toxcore/net_crypto.h +410 -0
  98. data/ProjectTox-Core/toxcore/network.c +979 -0
  99. data/ProjectTox-Core/toxcore/network.h +367 -0
  100. data/ProjectTox-Core/toxcore/onion.c +540 -0
  101. data/ProjectTox-Core/toxcore/onion.h +150 -0
  102. data/ProjectTox-Core/toxcore/onion_announce.c +433 -0
  103. data/ProjectTox-Core/toxcore/onion_announce.h +139 -0
  104. data/ProjectTox-Core/toxcore/onion_client.c +1347 -0
  105. data/ProjectTox-Core/toxcore/onion_client.h +253 -0
  106. data/ProjectTox-Core/toxcore/ping.c +346 -0
  107. data/ProjectTox-Core/toxcore/ping.h +47 -0
  108. data/ProjectTox-Core/toxcore/ping_array.c +162 -0
  109. data/ProjectTox-Core/toxcore/ping_array.h +75 -0
  110. data/ProjectTox-Core/toxcore/tox.c +940 -0
  111. data/ProjectTox-Core/toxcore/tox.h +734 -0
  112. data/ProjectTox-Core/toxcore/util.c +193 -0
  113. data/ProjectTox-Core/toxcore/util.h +63 -0
  114. data/ProjectTox-Core/toxdns/Makefile.inc +29 -0
  115. data/ProjectTox-Core/toxdns/toxdns.c +238 -0
  116. data/ProjectTox-Core/toxdns/toxdns.h +88 -0
  117. data/ProjectTox-Core/toxencryptsave/Makefile.inc +45 -0
  118. data/ProjectTox-Core/toxencryptsave/toxencryptsave.c +179 -0
  119. data/ProjectTox-Core/toxencryptsave/toxencryptsave.h +74 -0
  120. data/interfaces/libtox.i +2 -6
  121. data/lib/ffi-tox/libtox.rb +406 -28
  122. metadata +124 -46
  123. data/ProjectTox-Core/CMakeLists.txt +0 -50
  124. data/ProjectTox-Core/cmake/FindLIBCONFIG.cmake +0 -15
  125. data/ProjectTox-Core/cmake/FindNaCl.cmake +0 -17
  126. data/ProjectTox-Core/cmake/FindSODIUM.cmake +0 -15
  127. data/ProjectTox-Core/core/CMakeLists.txt +0 -19
  128. data/ProjectTox-Core/core/DHT.c +0 -1104
  129. data/ProjectTox-Core/core/DHT.h +0 -111
  130. data/ProjectTox-Core/core/LAN_discovery.c +0 -79
  131. data/ProjectTox-Core/core/Lossless_UDP.c +0 -755
  132. data/ProjectTox-Core/core/Lossless_UDP.h +0 -106
  133. data/ProjectTox-Core/core/Messenger.c +0 -596
  134. data/ProjectTox-Core/core/Messenger.h +0 -165
  135. data/ProjectTox-Core/core/friend_requests.c +0 -131
  136. data/ProjectTox-Core/core/friend_requests.h +0 -51
  137. data/ProjectTox-Core/core/net_crypto.c +0 -575
  138. data/ProjectTox-Core/core/net_crypto.h +0 -134
  139. data/ProjectTox-Core/core/network.c +0 -205
  140. data/ProjectTox-Core/core/network.h +0 -134
  141. data/ProjectTox-Core/docs/commands.md +0 -25
  142. data/ProjectTox-Core/docs/start_guide.de.md +0 -40
  143. data/ProjectTox-Core/docs/start_guide.md +0 -38
  144. data/ProjectTox-Core/other/CMakeLists.txt +0 -9
  145. data/ProjectTox-Core/testing/CMakeLists.txt +0 -18
  146. data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +0 -228
  147. data/ProjectTox-Core/testing/DHT_sendfiletest.c +0 -176
  148. data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +0 -214
  149. data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +0 -201
  150. data/ProjectTox-Core/testing/misc_tools.h +0 -29
  151. data/ProjectTox-Core/testing/nTox_win32.c +0 -387
  152. data/ProjectTox-Core/testing/nTox_win32.h +0 -40
  153. data/ProjectTox-Core/testing/rect.py +0 -45
@@ -0,0 +1,171 @@
1
+ #ifdef HAVE_CONFIG_H
2
+ #include "config.h"
3
+ #endif
4
+
5
+ #include <sys/types.h>
6
+ #include <stdint.h>
7
+ #include <string.h>
8
+ #include <check.h>
9
+ #include <stdlib.h>
10
+ #include <time.h>
11
+
12
+ #include "../toxcore/network.h"
13
+
14
+ #include "helpers.h"
15
+
16
+ START_TEST(test_addr_resolv_localhost)
17
+ {
18
+ #ifdef __CYGWIN__
19
+ /* force initialization of network stack
20
+ * normally this should happen automatically
21
+ * cygwin doesn't do it for every network related function though
22
+ * e.g. not for getaddrinfo... */
23
+ socket(0, 0, 0);
24
+ errno = 0;
25
+ #endif
26
+
27
+ const char localhost[] = "localhost";
28
+ int localhost_split = 0;
29
+
30
+ IP ip;
31
+ ip_init(&ip, 0);
32
+
33
+ int res = addr_resolve(localhost, &ip, NULL);
34
+
35
+ ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
36
+
37
+ if (res > 0) {
38
+ ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family);
39
+ ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr));
40
+ }
41
+
42
+ ip_init(&ip, 1);
43
+ res = addr_resolve(localhost, &ip, NULL);
44
+
45
+ if (res < 1) {
46
+ res = addr_resolve("ip6-localhost", &ip, NULL);
47
+ localhost_split = 1;
48
+ }
49
+
50
+ ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
51
+
52
+ if (res > 0) {
53
+ ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
54
+ ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
55
+ }
56
+
57
+ if (!localhost_split) {
58
+ ip_init(&ip, 1);
59
+ ip.family = AF_UNSPEC;
60
+ IP extra;
61
+ ip_reset(&extra);
62
+ res = addr_resolve(localhost, &ip, &extra);
63
+ ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
64
+
65
+ if (res > 0) {
66
+ ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
67
+ ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
68
+
69
+ ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family);
70
+ ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr));
71
+ }
72
+ } else {
73
+ printf("Localhost seems to be split in two.\n");
74
+ }
75
+ }
76
+ END_TEST
77
+
78
+ START_TEST(test_ip_equal)
79
+ {
80
+ int res;
81
+ IP ip1, ip2;
82
+ ip_reset(&ip1);
83
+ ip_reset(&ip2);
84
+
85
+ res = ip_equal(NULL, NULL);
86
+ ck_assert_msg(res == 0, "ip_equal(NULL, NULL): expected result 0, got %u.", res);
87
+
88
+ res = ip_equal(&ip1, NULL);
89
+ ck_assert_msg(res == 0, "ip_equal(PTR, NULL): expected result 0, got %u.", res);
90
+
91
+ res = ip_equal(NULL, &ip1);
92
+ ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res);
93
+
94
+ ip1.family = AF_INET;
95
+ ip1.ip4.uint32 = htonl(0x7F000001);
96
+
97
+ res = ip_equal(&ip1, &ip2);
98
+ ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): expected result 0, got %u.", res);
99
+
100
+ ip2.family = AF_INET;
101
+ ip2.ip4.uint32 = htonl(0x7F000001);
102
+
103
+ res = ip_equal(&ip1, &ip2);
104
+ ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.1} ): expected result != 0, got 0.");
105
+
106
+ ip2.ip4.uint32 = htonl(0x7F000002);
107
+
108
+ res = ip_equal(&ip1, &ip2);
109
+ ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.2} ): expected result 0, got %u.", res);
110
+
111
+ ip2.family = AF_INET6;
112
+ ip2.ip6.uint32[0] = 0;
113
+ ip2.ip6.uint32[1] = 0;
114
+ ip2.ip6.uint32[2] = htonl(0xFFFF);
115
+ ip2.ip6.uint32[3] = htonl(0x7F000001);
116
+
117
+ ck_assert_msg(IN6_IS_ADDR_V4MAPPED(&ip2.ip6.in6_addr) != 0,
118
+ "IN6_IS_ADDR_V4MAPPED(::ffff:127.0.0.1): expected != 0, got 0.");
119
+
120
+ res = ip_equal(&ip1, &ip2);
121
+ ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::ffff:127.0.0.1} ): expected result != 0, got 0.");
122
+
123
+ memcpy(&ip2.ip6, &in6addr_loopback, sizeof(IP6));
124
+ res = ip_equal(&ip1, &ip2);
125
+ ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::1} ): expected result 0, got %u.", res);
126
+
127
+ memcpy(&ip1, &ip2, sizeof(IP));
128
+ res = ip_equal(&ip1, &ip2);
129
+ ck_assert_msg(res != 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::1} ): expected result != 0, got 0.");
130
+
131
+ ip2.ip6.uint8[15]++;
132
+ res = ip_equal(&ip1, &ip2);
133
+ ck_assert_msg(res == 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::2} ): expected result 0, got %res.", res);
134
+ }
135
+ END_TEST
136
+
137
+ START_TEST(test_struct_sizes)
138
+ {
139
+ ck_assert_msg(sizeof(IP4) == 4, "sizeof(IP4): expected result 4, got %u.", sizeof(IP4));
140
+ ck_assert_msg(sizeof(IP6) == 16, "sizeof(IP6): expected result 16, got %u.", sizeof(IP6));
141
+ ck_assert_msg(sizeof(IP) == 17, "sizeof(IP): expected result 17, got %u.", sizeof(IP));
142
+ ck_assert_msg(sizeof(IP_Port) == 19, "sizeof(IP_Port): expected result 19, got %u.", sizeof(IP_Port));
143
+ }
144
+ END_TEST
145
+
146
+ Suite *network_suite(void)
147
+ {
148
+ Suite *s = suite_create("Network");
149
+
150
+ DEFTESTCASE(addr_resolv_localhost);
151
+ DEFTESTCASE(ip_equal);
152
+ DEFTESTCASE(struct_sizes);
153
+
154
+ return s;
155
+ }
156
+
157
+ int main(int argc, char *argv[])
158
+ {
159
+ srand((unsigned int) time(NULL));
160
+
161
+ Suite *network = network_suite();
162
+ SRunner *test_runner = srunner_create(network);
163
+ int number_failed = 0;
164
+
165
+ srunner_run_all(test_runner, CK_NORMAL);
166
+ number_failed = srunner_ntests_failed(test_runner);
167
+
168
+ srunner_free(test_runner);
169
+
170
+ return number_failed;
171
+ }
@@ -0,0 +1,363 @@
1
+ #ifdef HAVE_CONFIG_H
2
+ #include "config.h"
3
+ #endif
4
+
5
+ #include <sys/types.h>
6
+ #include <stdint.h>
7
+ #include <string.h>
8
+ #include <check.h>
9
+ #include <stdlib.h>
10
+ #include <time.h>
11
+
12
+ #include "../toxcore/onion.h"
13
+ #include "../toxcore/onion_announce.h"
14
+ #include "../toxcore/onion_client.h"
15
+ #include "../toxcore/util.h"
16
+
17
+ #include "helpers.h"
18
+
19
+ #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
20
+ #define c_sleep(x) Sleep(1*x)
21
+ #else
22
+ #include <unistd.h>
23
+ #define c_sleep(x) usleep(1000*x)
24
+ #endif
25
+
26
+ void do_onion(Onion *onion)
27
+ {
28
+ networking_poll(onion->net);
29
+ do_DHT(onion->dht);
30
+ }
31
+
32
+ static int handled_test_1;
33
+ static int handle_test_1(void *object, IP_Port source, const uint8_t *packet, uint32_t length)
34
+ {
35
+ Onion *onion = object;
36
+
37
+ if (memcmp(packet, "Install Gentoo", sizeof("Install Gentoo")) != 0)
38
+ return 1;
39
+
40
+ if (send_onion_response(onion->net, source, (uint8_t *)"install gentoo", sizeof("install gentoo"),
41
+ packet + sizeof("Install Gentoo")) == -1)
42
+ return 1;
43
+
44
+ handled_test_1 = 1;
45
+ return 0;
46
+ }
47
+
48
+ static int handled_test_2;
49
+ static int handle_test_2(void *object, IP_Port source, const uint8_t *packet, uint32_t length)
50
+ {
51
+ if (length != sizeof("install Gentoo"))
52
+ return 1;
53
+
54
+ if (memcmp(packet, (uint8_t *)"install gentoo", sizeof("install gentoo")) != 0)
55
+ return 1;
56
+
57
+ handled_test_2 = 1;
58
+ return 0;
59
+ }
60
+ /*
61
+ void print_client_id(uint8_t *client_id, uint32_t length)
62
+ {
63
+ uint32_t j;
64
+
65
+ for (j = 0; j < length; j++) {
66
+ printf("%02hhX", client_id[j]);
67
+ }
68
+ printf("\n");
69
+ }
70
+ */
71
+ uint8_t sb_data[ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
72
+ static int handled_test_3;
73
+ uint8_t test_3_pub_key[crypto_box_PUBLICKEYBYTES];
74
+ uint8_t test_3_ping_id[crypto_hash_sha256_BYTES];
75
+ static int handle_test_3(void *object, IP_Port source, const uint8_t *packet, uint32_t length)
76
+ {
77
+ Onion *onion = object;
78
+
79
+ if (length != (1 + crypto_box_NONCEBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + 1 + crypto_hash_sha256_BYTES +
80
+ crypto_box_MACBYTES))
81
+ return 1;
82
+
83
+ uint8_t plain[1 + crypto_hash_sha256_BYTES];
84
+ //print_client_id(packet, length);
85
+ int len = decrypt_data(test_3_pub_key, onion->dht->self_secret_key, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH,
86
+ packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES,
87
+ 1 + crypto_hash_sha256_BYTES + crypto_box_MACBYTES, plain);
88
+
89
+ if (len == -1)
90
+ return 1;
91
+
92
+
93
+ if (memcmp(packet + 1, sb_data, ONION_ANNOUNCE_SENDBACK_DATA_LENGTH) != 0)
94
+ return 1;
95
+
96
+ memcpy(test_3_ping_id, plain + 1, crypto_hash_sha256_BYTES);
97
+ //print_client_id(test_3_ping_id, sizeof(test_3_ping_id));
98
+ handled_test_3 = 1;
99
+ return 0;
100
+ }
101
+
102
+ uint8_t nonce[crypto_box_NONCEBYTES];
103
+ static int handled_test_4;
104
+ static int handle_test_4(void *object, IP_Port source, const uint8_t *packet, uint32_t length)
105
+ {
106
+ Onion *onion = object;
107
+
108
+ if (length != (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + sizeof("Install gentoo") + crypto_box_MACBYTES))
109
+ return 1;
110
+
111
+ uint8_t plain[sizeof("Install gentoo")] = {0};
112
+
113
+ if (memcmp(nonce, packet + 1, crypto_box_NONCEBYTES) != 0)
114
+ return 1;
115
+
116
+ int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion->dht->self_secret_key, packet + 1,
117
+ packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, sizeof("Install gentoo") + crypto_box_MACBYTES, plain);
118
+
119
+ if (len == -1)
120
+ return 1;
121
+
122
+ if (memcmp(plain, "Install gentoo", sizeof("Install gentoo")) != 0)
123
+ return 1;
124
+
125
+ handled_test_4 = 1;
126
+ return 0;
127
+ }
128
+
129
+ START_TEST(test_basic)
130
+ {
131
+ IP ip;
132
+ ip_init(&ip, 1);
133
+ ip.ip6.uint8[15] = 1;
134
+ Onion *onion1 = new_onion(new_DHT(new_networking(ip, 34567)));
135
+ Onion *onion2 = new_onion(new_DHT(new_networking(ip, 34568)));
136
+ ck_assert_msg((onion1 != NULL) && (onion2 != NULL), "Onion failed initializing.");
137
+ networking_registerhandler(onion2->net, 'I', &handle_test_1, onion2);
138
+
139
+ IP_Port on1 = {ip, onion1->net->port};
140
+ Node_format n1;
141
+ memcpy(n1.client_id, onion1->dht->self_public_key, crypto_box_PUBLICKEYBYTES);
142
+ n1.ip_port = on1;
143
+
144
+ IP_Port on2 = {ip, onion2->net->port};
145
+ Node_format n2;
146
+ memcpy(n2.client_id, onion2->dht->self_public_key, crypto_box_PUBLICKEYBYTES);
147
+ n2.ip_port = on2;
148
+
149
+ Node_format nodes[4];
150
+ nodes[0] = n1;
151
+ nodes[1] = n2;
152
+ nodes[2] = n1;
153
+ nodes[3] = n2;
154
+ Onion_Path path;
155
+ create_onion_path(onion1->dht, &path, nodes);
156
+ int ret = send_onion_packet(onion1->net, &path, nodes[3].ip_port, (uint8_t *)"Install Gentoo",
157
+ sizeof("Install Gentoo"));
158
+ ck_assert_msg(ret == 0, "Failed to create/send onion packet.");
159
+
160
+ handled_test_1 = 0;
161
+
162
+ while (handled_test_1 == 0) {
163
+ do_onion(onion1);
164
+ do_onion(onion2);
165
+ }
166
+
167
+ networking_registerhandler(onion1->net, 'i', &handle_test_2, onion1);
168
+ handled_test_2 = 0;
169
+
170
+ while (handled_test_2 == 0) {
171
+ do_onion(onion1);
172
+ do_onion(onion2);
173
+ }
174
+
175
+ Onion_Announce *onion1_a = new_onion_announce(onion1->dht);
176
+ Onion_Announce *onion2_a = new_onion_announce(onion2->dht);
177
+ networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1);
178
+ ck_assert_msg((onion1_a != NULL) && (onion2_a != NULL), "Onion_Announce failed initializing.");
179
+ uint8_t zeroes[64] = {0};
180
+ randombytes(sb_data, sizeof(sb_data));
181
+ uint64_t s;
182
+ memcpy(&s, sb_data, sizeof(uint64_t));
183
+ memcpy(test_3_pub_key, nodes[3].client_id, crypto_box_PUBLICKEYBYTES);
184
+ ret = send_announce_request(onion1->net, &path, nodes[3], onion1->dht->self_public_key,
185
+ onion1->dht->self_secret_key,
186
+ zeroes, onion1->dht->self_public_key, onion1->dht->self_public_key, s);
187
+ ck_assert_msg(ret == 0, "Failed to create/send onion announce_request packet.");
188
+ handled_test_3 = 0;
189
+
190
+ while (handled_test_3 == 0) {
191
+ do_onion(onion1);
192
+ do_onion(onion2);
193
+ c_sleep(50);
194
+ }
195
+
196
+ randombytes(sb_data, sizeof(sb_data));
197
+ memcpy(&s, sb_data, sizeof(uint64_t));
198
+ memcpy(onion2_a->entries[1].public_key, onion2->dht->self_public_key, crypto_box_PUBLICKEYBYTES);
199
+ onion2_a->entries[1].time = unix_time();
200
+ networking_registerhandler(onion1->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_test_4, onion1);
201
+ send_announce_request(onion1->net, &path, nodes[3], onion1->dht->self_public_key, onion1->dht->self_secret_key,
202
+ test_3_ping_id, onion1->dht->self_public_key, onion1->dht->self_public_key, s);
203
+
204
+ while (memcmp(onion2_a->entries[ONION_ANNOUNCE_MAX_ENTRIES - 2].public_key, onion1->dht->self_public_key,
205
+ crypto_box_PUBLICKEYBYTES) != 0) {
206
+ do_onion(onion1);
207
+ do_onion(onion2);
208
+ c_sleep(50);
209
+ }
210
+
211
+ c_sleep(1000);
212
+ Onion *onion3 = new_onion(new_DHT(new_networking(ip, 34569)));
213
+ ck_assert_msg((onion3 != NULL), "Onion failed initializing.");
214
+
215
+ new_nonce(nonce);
216
+ ret = send_data_request(onion3->net, &path, nodes[3].ip_port, onion1->dht->self_public_key,
217
+ onion1->dht->self_public_key,
218
+ nonce, (uint8_t *)"Install gentoo", sizeof("Install gentoo"));
219
+ ck_assert_msg(ret == 0, "Failed to create/send onion data_request packet.");
220
+ handled_test_4 = 0;
221
+
222
+ while (handled_test_4 == 0) {
223
+ do_onion(onion1);
224
+ do_onion(onion2);
225
+ c_sleep(50);
226
+ }
227
+ }
228
+ END_TEST
229
+
230
+ typedef struct {
231
+ Onion *onion;
232
+ Onion_Announce *onion_a;
233
+ Onion_Client *onion_c;
234
+ } Onions;
235
+
236
+ Onions *new_onions(uint16_t port)
237
+ {
238
+ IP ip;
239
+ ip_init(&ip, 1);
240
+ ip.ip6.uint8[15] = 1;
241
+ Onions *on = malloc(sizeof(Onions));
242
+ DHT *dht = new_DHT(new_networking(ip, port));
243
+ on->onion = new_onion(dht);
244
+ on->onion_a = new_onion_announce(dht);
245
+ on->onion_c = new_onion_client(new_net_crypto(dht, 0));
246
+
247
+ if (on->onion && on->onion_a && on->onion_c)
248
+ return on;
249
+
250
+ return NULL;
251
+ }
252
+
253
+ void do_onions(Onions *on)
254
+ {
255
+ networking_poll(on->onion->net);
256
+ do_DHT(on->onion->dht);
257
+ do_onion_client(on->onion_c);
258
+ }
259
+
260
+ #define NUM_ONIONS 50
261
+
262
+ START_TEST(test_announce)
263
+ {
264
+ uint32_t i, j;
265
+ Onions *onions[NUM_ONIONS];
266
+
267
+ for (i = 0; i < NUM_ONIONS; ++i) {
268
+ onions[i] = new_onions(i + 34655);
269
+ ck_assert_msg(onions[i] != 0, "Failed to create onions. %u");
270
+ }
271
+
272
+ IP ip;
273
+ ip_init(&ip, 1);
274
+ ip.ip6.uint8[15] = 1;
275
+
276
+ for (i = 3; i < NUM_ONIONS; ++i) {
277
+ IP_Port ip_port = {ip, onions[i - 1]->onion->net->port};
278
+ DHT_bootstrap(onions[i]->onion->dht, ip_port, onions[i - 1]->onion->dht->self_public_key);
279
+ IP_Port ip_port1 = {ip, onions[i - 2]->onion->net->port};
280
+ DHT_bootstrap(onions[i]->onion->dht, ip_port1, onions[i - 2]->onion->dht->self_public_key);
281
+ IP_Port ip_port2 = {ip, onions[i - 3]->onion->net->port};
282
+ DHT_bootstrap(onions[i]->onion->dht, ip_port2, onions[i - 3]->onion->dht->self_public_key);
283
+ }
284
+
285
+ uint32_t connected = 0;
286
+
287
+ while (connected != NUM_ONIONS) {
288
+ connected = 0;
289
+
290
+ for (i = 0; i < NUM_ONIONS; ++i) {
291
+ do_onions(onions[i]);
292
+ connected += DHT_isconnected(onions[i]->onion->dht);
293
+ }
294
+
295
+ c_sleep(50);
296
+ }
297
+
298
+ for (i = 0; i < 25 * 2; ++i) {
299
+ for (j = 0; j < NUM_ONIONS; ++j) {
300
+ do_onions(onions[j]);
301
+ }
302
+
303
+ c_sleep(50);
304
+ }
305
+
306
+ onion_addfriend(onions[7]->onion_c, onions[37]->onion_c->c->self_public_key);
307
+ int frnum = onion_addfriend(onions[37]->onion_c, onions[7]->onion_c->c->self_public_key);
308
+
309
+ int ok = -1;
310
+
311
+ IP_Port ip_port;
312
+
313
+ while (ok == -1) {
314
+ for (i = 0; i < NUM_ONIONS; ++i) {
315
+ networking_poll(onions[i]->onion->net);
316
+ do_onion_client(onions[i]->onion_c);
317
+ }
318
+
319
+ ok = onion_getfriendip(onions[37]->onion_c, frnum, &ip_port);
320
+
321
+ c_sleep(50);
322
+ }
323
+
324
+ printf("id discovered\n");
325
+
326
+ while (ok != 1) {
327
+ for (i = 0; i < NUM_ONIONS; ++i) {
328
+ do_onions(onions[i]);
329
+ }
330
+
331
+ ok = onion_getfriendip(onions[37]->onion_c, frnum, &ip_port);
332
+
333
+ c_sleep(50);
334
+ }
335
+
336
+ ck_assert_msg(ip_port.port == onions[7]->onion->net->port, "Port in returned ip not correct.");
337
+ }
338
+ END_TEST
339
+
340
+ Suite *onion_suite(void)
341
+ {
342
+ Suite *s = suite_create("Onion");
343
+
344
+ DEFTESTCASE_SLOW(basic, 5);
345
+ DEFTESTCASE_SLOW(announce, 50);
346
+ return s;
347
+ }
348
+
349
+ int main(int argc, char *argv[])
350
+ {
351
+ srand((unsigned int) time(NULL));
352
+
353
+ Suite *onion = onion_suite();
354
+ SRunner *test_runner = srunner_create(onion);
355
+
356
+ int number_failed = 0;
357
+ srunner_run_all(test_runner, CK_NORMAL);
358
+ number_failed = srunner_ntests_failed(test_runner);
359
+
360
+ srunner_free(test_runner);
361
+
362
+ return number_failed;
363
+ }