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
@@ -1,12 +1,7 @@
1
+
1
2
  /* DHT boostrap
2
3
  *
3
- * A simple DHT boostrap server for tox.
4
- *
5
- * Build commands (use one or the other):
6
- * gcc -O2 -Wall -D VANILLA_NACL -o bootstrap_server ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../core/friend_requests.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} DHT_bootstrap.c
7
- *
8
- * gcc -O2 -Wall -o bootstrap_server ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../core/friend_requests.c -lsodium DHT_bootstrap.c
9
- *
4
+ * A simple DHT boostrap node for tox.
10
5
  *
11
6
  * Copyright (C) 2013 Tox project All Rights Reserved.
12
7
  *
@@ -24,15 +19,35 @@
24
19
  *
25
20
  * You should have received a copy of the GNU General Public License
26
21
  * along with Tox. If not, see <http://www.gnu.org/licenses/>.
27
- *
22
+ *
28
23
  */
29
24
 
30
- #include "../core/DHT.h"
31
- #include "../core/friend_requests.h"
32
- #include "../testing/misc_tools.h"
25
+ #ifdef HAVE_CONFIG_H
26
+ #include "config.h"
27
+ #endif
28
+
29
+ #include "../toxcore/DHT.h"
30
+ #include "../toxcore/LAN_discovery.h"
31
+ #include "../toxcore/friend_requests.h"
32
+ #include "../toxcore/util.h"
33
+
34
+ #define TCP_RELAY_ENABLED
35
+
36
+ #ifdef TCP_RELAY_ENABLED
37
+ #include "../toxcore/TCP_server.h"
38
+ #endif
39
+
40
+ #include "../testing/misc_tools.c"
41
+
42
+ #ifdef DHT_NODE_EXTRA_PACKETS
43
+ #include "./bootstrap_node_packets.c"
44
+
45
+ #define DHT_VERSION_NUMBER 1
46
+ #define DHT_MOTD "This is a test motd"
47
+ #endif
33
48
 
34
- //Sleep function (x = milliseconds)
35
- #ifdef WIN32
49
+ /* Sleep function (x = milliseconds) */
50
+ #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
36
51
  #define c_sleep(x) Sleep(1*x)
37
52
  #else
38
53
  #include <unistd.h>
@@ -43,32 +58,37 @@
43
58
  #define PORT 33445
44
59
 
45
60
 
46
-
47
- void manage_keys()
61
+ void manage_keys(DHT *dht)
48
62
  {
49
63
  const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
50
64
  uint8_t keys[KEYS_SIZE];
51
65
 
52
66
  FILE *keys_file = fopen("key", "r");
67
+
53
68
  if (keys_file != NULL) {
54
- //if file was opened successfully -- load keys
69
+ /* If file was opened successfully -- load keys,
70
+ otherwise save new keys */
55
71
  size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
72
+
56
73
  if (read_size != KEYS_SIZE) {
57
74
  printf("Error while reading the key file\nExiting.\n");
58
75
  exit(1);
59
76
  }
60
- load_keys(keys);
61
- printf("Keys loaded successfully\n");
77
+
78
+ memcpy(dht->self_public_key, keys, crypto_box_PUBLICKEYBYTES);
79
+ memcpy(dht->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
80
+ printf("Keys loaded successfully.\n");
62
81
  } else {
63
- //otherwise save new keys
64
- new_keys();
65
- save_keys(keys);
82
+ memcpy(keys, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
83
+ memcpy(keys + crypto_box_PUBLICKEYBYTES, dht->self_secret_key, crypto_box_SECRETKEYBYTES);
66
84
  keys_file = fopen("key", "w");
85
+
67
86
  if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
68
87
  printf("Error while writing the key file.\nExiting.\n");
69
88
  exit(1);
70
89
  }
71
- printf("Keys saved successfully\n");
90
+
91
+ printf("Keys saved successfully.\n");
72
92
  }
73
93
 
74
94
  fclose(keys_file);
@@ -76,64 +96,107 @@ void manage_keys()
76
96
 
77
97
  int main(int argc, char *argv[])
78
98
  {
79
- manage_keys();
99
+ if (argc == 2 && !strncasecmp(argv[1], "-h", 3)) {
100
+ printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]);
101
+ printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]);
102
+ exit(0);
103
+ }
104
+
105
+ /* let user override default by cmdline */
106
+ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
107
+ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
108
+
109
+ if (argvoffset < 0)
110
+ exit(1);
111
+
112
+ /* Initialize networking -
113
+ Bind to ip 0.0.0.0 / [::] : PORT */
114
+ IP ip;
115
+ ip_init(&ip, ipv6enabled);
116
+
117
+ DHT *dht = new_DHT(new_networking(ip, PORT));
118
+ Onion *onion = new_onion(dht);
119
+ Onion_Announce *onion_a = new_onion_announce(dht);
120
+
121
+ #ifdef DHT_NODE_EXTRA_PACKETS
122
+ bootstrap_set_callbacks(dht->net, DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD));
123
+ #endif
124
+
125
+ if (!(onion && onion_a)) {
126
+ printf("Something failed to initialize.\n");
127
+ exit(1);
128
+ }
129
+
130
+ perror("Initialization");
131
+
132
+ manage_keys(dht);
80
133
  printf("Public key: ");
81
134
  uint32_t i;
82
135
 
136
+ #ifdef TCP_RELAY_ENABLED
137
+ #define NUM_PORTS 3
138
+ uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
139
+ TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht->self_public_key, dht->self_secret_key, onion);
140
+
141
+ if (tcp_s == NULL) {
142
+ printf("TCP server failed to initialize.\n");
143
+ exit(1);
144
+ }
145
+
146
+ #endif
147
+
83
148
  FILE *file;
84
149
  file = fopen("PUBLIC_ID.txt", "w");
85
150
 
86
- for(i = 0; i < 32; i++)
87
- {
88
- if(self_public_key[i] < 16)
89
- printf("0");
90
- printf("%hhX",self_public_key[i]);
91
- fprintf(file, "%hhX",self_public_key[i]);
151
+ for (i = 0; i < 32; i++) {
152
+ printf("%02hhX", dht->self_public_key[i]);
153
+ fprintf(file, "%02hhX", dht->self_public_key[i]);
92
154
  }
93
155
 
94
156
  fclose(file);
95
157
 
96
158
  printf("\n");
97
- printf("Port: %u\n", PORT);
98
- //initialize networking
99
- //bind to ip 0.0.0.0:PORT
100
- IP ip;
101
- ip.i = 0;
102
- init_networking(ip, PORT);
159
+ printf("Port: %u\n", ntohs(dht->net->port));
103
160
 
104
- perror("Initialization");
105
-
106
- if (argc > 3) {
161
+ if (argc > argvoffset + 3) {
107
162
  printf("Trying to bootstrap into the network...\n");
108
- IP_Port bootstrap_info;
109
- bootstrap_info.ip.i = inet_addr(argv[1]);
110
- bootstrap_info.port = htons(atoi(argv[2]));
111
- uint8_t *bootstrap_key = hex_string_to_bin(argv[3]);
112
- DHT_bootstrap(bootstrap_info, bootstrap_key);
163
+ uint16_t port = htons(atoi(argv[argvoffset + 2]));
164
+ uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
165
+ int res = DHT_bootstrap_from_address(dht, argv[argvoffset + 1],
166
+ ipv6enabled, port, bootstrap_key);
113
167
  free(bootstrap_key);
114
- }
115
168
 
116
- IP_Port ip_port;
117
- uint8_t data[MAX_UDP_PACKET_SIZE];
118
- uint32_t length;
169
+ if (!res) {
170
+ printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
171
+ exit(1);
172
+ }
173
+ }
119
174
 
120
175
  int is_waiting_for_dht_connection = 1;
121
- while(1)
122
- {
123
- if (is_waiting_for_dht_connection && DHT_isconnected())
124
- {
125
- printf("Connected to other bootstrap server successfully.\n");
176
+
177
+ uint64_t last_LANdiscovery = 0;
178
+ LANdiscovery_init(dht);
179
+
180
+ while (1) {
181
+ if (is_waiting_for_dht_connection && DHT_isconnected(dht)) {
182
+ printf("Connected to other bootstrap node successfully.\n");
126
183
  is_waiting_for_dht_connection = 0;
127
184
  }
128
- doDHT();
129
185
 
130
- while(receivepacket(&ip_port, data, &length) != -1)
131
- {
132
- DHT_handlepacket(data, length, ip_port);
133
- friendreq_handlepacket(data, length, ip_port);
186
+ do_DHT(dht);
187
+
188
+ if (is_timeout(last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) {
189
+ send_LANdiscovery(htons(PORT), dht);
190
+ last_LANdiscovery = unix_time();
134
191
  }
192
+
193
+ #ifdef TCP_RELAY_ENABLED
194
+ do_TCP_server(tcp_s);
195
+ #endif
196
+ networking_poll(dht->net);
197
+
135
198
  c_sleep(1);
136
199
  }
137
- shutdown_networking();
200
+
138
201
  return 0;
139
202
  }
@@ -0,0 +1,3 @@
1
+ As maintaining 2 separate lists of the same information seemed redundant, this list has been phased out.
2
+
3
+ For a current DHT node list please visit http://wiki.tox.im/nodes
@@ -0,0 +1,20 @@
1
+ bin_PROGRAMS += DHT_bootstrap
2
+
3
+ DHT_bootstrap_SOURCES = ../other/DHT_bootstrap.c \
4
+ ../toxcore/DHT.h \
5
+ ../toxcore/friend_requests.h
6
+
7
+ DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \
8
+ $(LIBSODIUM_CFLAGS) \
9
+ $(NACL_CFLAGS)
10
+
11
+ DHT_bootstrap_LDADD = $(LIBSODIUM_LDFLAGS) \
12
+ $(NACL_LDFLAGS) \
13
+ libtoxcore.la \
14
+ $(LIBSODIUM_LIBS) \
15
+ $(NACL_OBJECTS) \
16
+ $(NACL_LIBS) \
17
+ $(WINSOCK2_LIBS)
18
+
19
+ EXTRA_DIST += $(top_srcdir)/other/DHTnodes \
20
+ $(top_srcdir)/other/tox.png
@@ -0,0 +1,65 @@
1
+ /* bootstrap_node_packets.c
2
+ *
3
+ * Special bootstrap node only packets.
4
+ *
5
+ * Include it in your bootstrap node and use: bootstrap_set_callbacks() to enable.
6
+ *
7
+ * Copyright (C) 2013 Tox project All Rights Reserved.
8
+ *
9
+ * This file is part of Tox.
10
+ *
11
+ * Tox is free software: you can redistribute it and/or modify
12
+ * it under the terms of the GNU General Public License as published by
13
+ * the Free Software Foundation, either version 3 of the License, or
14
+ * (at your option) any later version.
15
+ *
16
+ * Tox is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23
+ *
24
+ */
25
+
26
+ #define MAX_MOTD_LENGTH 256 /* I recommend you use a maximum of 96 bytes. The hard maximum is this though. */
27
+
28
+ #define INFO_REQUEST_PACKET_LENGTH 78
29
+
30
+ static uint32_t bootstrap_version;
31
+ static uint8_t bootstrap_motd[MAX_MOTD_LENGTH];
32
+ static uint16_t bootstrap_motd_length;
33
+
34
+ /* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH
35
+ * with the first byte being BOOTSTRAP_INFO_PACKET_ID
36
+ */
37
+ static int handle_info_request(void *object, IP_Port source, const uint8_t *packet, uint32_t length)
38
+ {
39
+ if (length != INFO_REQUEST_PACKET_LENGTH)
40
+ return 1;
41
+
42
+ uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH];
43
+ data[0] = BOOTSTRAP_INFO_PACKET_ID;
44
+ memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version));
45
+ uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length;
46
+ memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length);
47
+
48
+ if (sendpacket(object, source, data, len) == len)
49
+ return 0;
50
+
51
+ return 1;
52
+ }
53
+
54
+ int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, uint8_t *motd, uint16_t motd_length)
55
+ {
56
+ if (motd_length > MAX_MOTD_LENGTH)
57
+ return -1;
58
+
59
+ bootstrap_version = htonl(version);
60
+ memcpy(bootstrap_motd, motd, motd_length);
61
+ bootstrap_motd_length = motd_length;
62
+
63
+ networking_registerhandler(net, BOOTSTRAP_INFO_PACKET_ID, &handle_info_request, net);
64
+ return 0;
65
+ }
@@ -1,11 +1,11 @@
1
1
  /* DHT test
2
2
  * A file with a main that runs our DHT for testing.
3
- *
3
+ *
4
4
  * Compile with: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} DHT_test.c
5
- *
5
+ *
6
6
  * Command line arguments are the ip, port and public key of a node.
7
7
  * EX: ./test 127.0.0.1 33445 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8
- *
8
+ *
9
9
  * The test will then ask you for the id (in hex format) of the friend you wish to add
10
10
  *
11
11
  * Copyright (C) 2013 Tox project All Rights Reserved.
@@ -24,18 +24,22 @@
24
24
  *
25
25
  * You should have received a copy of the GNU General Public License
26
26
  * along with Tox. If not, see <http://www.gnu.org/licenses/>.
27
- *
27
+ *
28
28
  */
29
-
29
+
30
+ #ifdef HAVE_CONFIG_H
31
+ #include "config.h"
32
+ #endif
33
+
30
34
  //#include "../core/network.h"
31
- #include "../core/DHT.c"
32
- #include "../core/friend_requests.c"
33
- #include "misc_tools.h"
35
+ #include "../toxcore/DHT.h"
36
+ #include "../toxcore/friend_requests.h"
37
+ #include "misc_tools.c"
34
38
 
35
39
  #include <string.h>
36
40
 
37
41
  //Sleep function (x = milliseconds)
38
- #ifdef WIN32
42
+ #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
39
43
 
40
44
  #define c_sleep(x) Sleep(1*x)
41
45
 
@@ -48,135 +52,203 @@
48
52
 
49
53
  #define PORT 33445
50
54
 
51
- void print_clientlist()
55
+ uint8_t zeroes_cid[CLIENT_ID_SIZE];
56
+
57
+ void print_client_id(uint8_t *client_id)
52
58
  {
53
- uint32_t i, j;
54
- IP_Port p_ip;
59
+ uint32_t j;
60
+
61
+ for (j = 0; j < CLIENT_ID_SIZE; j++) {
62
+ printf("%02hhX", client_id[j]);
63
+ }
64
+ }
65
+
66
+ void print_hardening(Hardening *h)
67
+ {
68
+ printf("Hardening:\n");
69
+ printf("routes_requests_ok: %hhu\n", h->routes_requests_ok);
70
+ printf("routes_requests_timestamp: %llu\n", (long long unsigned int)h->routes_requests_timestamp);
71
+ printf("routes_requests_pingedid: ");
72
+ print_client_id(h->routes_requests_pingedid);
73
+ printf("\nsend_nodes_ok: %hhu\n", h->send_nodes_ok);
74
+ printf("send_nodes_timestamp: %llu\n", (long long unsigned int)h->send_nodes_timestamp);
75
+ printf("send_nodes_pingedid: ");
76
+ print_client_id(h->send_nodes_pingedid);
77
+ printf("\ntesting_requests: %hhu\n", h->testing_requests);
78
+ printf("testing_timestamp: %llu\n", (long long unsigned int)h->testing_timestamp);
79
+ printf("testing_pingedid: ");
80
+ print_client_id(h->testing_pingedid);
81
+ printf("\n\n");
82
+ }
83
+
84
+ void print_assoc(IPPTsPng *assoc, uint8_t ours)
85
+ {
86
+ IP_Port *ipp = &assoc->ip_port;
87
+ printf("\nIP: %s Port: %u", ip_ntoa(&ipp->ip), ntohs(ipp->port));
88
+ printf("\nTimestamp: %llu", (long long unsigned int) assoc->timestamp);
89
+ printf("\nLast pinged: %llu\n", (long long unsigned int) assoc->last_pinged);
90
+
91
+ ipp = &assoc->ret_ip_port;
92
+
93
+ if (ours)
94
+ printf("OUR IP: %s Port: %u\n", ip_ntoa(&ipp->ip), ntohs(ipp->port));
95
+ else
96
+ printf("RET IP: %s Port: %u\n", ip_ntoa(&ipp->ip), ntohs(ipp->port));
97
+
98
+ printf("Timestamp: %llu\n", (long long unsigned int) assoc->ret_timestamp);
99
+ print_hardening(&assoc->hardening);
100
+
101
+ }
102
+
103
+ void print_clientlist(DHT *dht)
104
+ {
105
+ uint32_t i;
55
106
  printf("___________________CLOSE________________________________\n");
56
- for(i = 0; i < 4; i++) {
107
+
108
+ for (i = 0; i < LCLIENT_LIST; i++) {
109
+ Client_data *client = &dht->close_clientlist[i];
110
+
111
+ if (memcmp(client->client_id, zeroes_cid, CLIENT_ID_SIZE) == 0)
112
+ continue;
113
+
57
114
  printf("ClientID: ");
58
- for(j = 0; j < 32; j++) {
59
- printf("%c", close_clientlist[i].client_id[j]);
60
- }
61
- p_ip = close_clientlist[i].ip_port;
62
- printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
63
- printf("\nTimestamp: %u", close_clientlist[i].timestamp);
64
- printf("\nLast pinged: %u\n", close_clientlist[i].last_pinged);
65
- p_ip = close_clientlist[i].ret_ip_port;
66
- printf("OUR IP: %u.%u.%u.%u Port: %u\n",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
67
- printf("Timestamp: %u\n", close_clientlist[i].ret_timestamp);
68
- }
115
+ print_client_id(client->client_id);
116
+
117
+ print_assoc(&client->assoc4, 1);
118
+ print_assoc(&client->assoc6, 1);
119
+ }
69
120
  }
70
121
 
71
- void print_friendlist()
122
+ void print_friendlist(DHT *dht)
72
123
  {
73
- uint32_t i, j, k;
124
+ uint32_t i, k;
74
125
  IP_Port p_ip;
75
126
  printf("_________________FRIENDS__________________________________\n");
76
- for(k = 0; k < num_friends; k++) {
127
+
128
+ for (k = 0; k < dht->num_friends; k++) {
77
129
  printf("FRIEND %u\n", k);
78
130
  printf("ID: ");
79
- for(j = 0; j < 32; j++) {
80
- printf("%c", friends_list[k].client_id[j]);
81
- }
82
- p_ip = DHT_getfriendip(friends_list[k].client_id);
83
- printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
131
+
132
+ print_client_id(dht->friends_list[k].client_id);
133
+
134
+ int friendok = DHT_getfriendip(dht, dht->friends_list[k].client_id, &p_ip);
135
+ printf("\nIP: %s:%u (%d)", ip_ntoa(&p_ip.ip), ntohs(p_ip.port), friendok);
84
136
 
85
137
  printf("\nCLIENTS IN LIST:\n\n");
86
-
87
- for(i = 0; i < 4; i++) {
138
+
139
+ for (i = 0; i < MAX_FRIEND_CLIENTS; i++) {
140
+ Client_data *client = &dht->friends_list[k].client_list[i];
141
+
142
+ if (memcmp(client->client_id, zeroes_cid, CLIENT_ID_SIZE) == 0)
143
+ continue;
144
+
88
145
  printf("ClientID: ");
89
- for(j = 0; j < 32; j++) {
90
- if(friends_list[k].client_list[i].client_id[j] < 16)
91
- printf("0");
92
- printf("%hhX", friends_list[k].client_list[i].client_id[j]);
93
- }
94
- p_ip = friends_list[k].client_list[i].ip_port;
95
- printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
96
- printf("\nTimestamp: %u", friends_list[k].client_list[i].timestamp);
97
- printf("\nLast pinged: %u\n", friends_list[k].client_list[i].last_pinged);
98
- p_ip = friends_list[k].client_list[i].ret_ip_port;
99
- printf("ret IP: %u.%u.%u.%u:%u\n",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
100
- printf("Timestamp: %u\n", friends_list[k].client_list[i].ret_timestamp);
146
+ print_client_id(client->client_id);
147
+
148
+ print_assoc(&client->assoc4, 0);
149
+ print_assoc(&client->assoc6, 0);
101
150
  }
102
151
  }
103
152
  }
104
153
 
105
- void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
154
+ void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
106
155
  {
107
156
  uint32_t i;
108
157
  printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
109
158
  printf("--------------------BEGIN-----------------------------\n");
110
- for(i = 0; i < length; i++) {
111
- if(data[i] < 16)
159
+
160
+ for (i = 0; i < length; i++) {
161
+ if (data[i] < 16)
112
162
  printf("0");
113
- printf("%hhX",data[i]);
163
+
164
+ printf("%hhX", data[i]);
114
165
  }
166
+
115
167
  printf("\n--------------------END-----------------------------\n\n\n");
116
168
  }
117
169
 
118
170
  int main(int argc, char *argv[])
119
171
  {
120
- //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
121
-
122
172
  if (argc < 4) {
123
- printf("usage %s ip port public_key\n", argv[0]);
173
+ printf("Usage: %s [--ipv4|--ipv6] ip port public_key\n", argv[0]);
124
174
  exit(0);
125
175
  }
126
- new_keys();
176
+
177
+ /* let user override default by cmdline */
178
+ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
179
+ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
180
+
181
+ if (argvoffset < 0)
182
+ exit(1);
183
+
184
+ //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
185
+ /* initialize networking */
186
+ /* bind to ip 0.0.0.0:PORT */
187
+ IP ip;
188
+ ip_init(&ip, ipv6enabled);
189
+
190
+ DHT *dht = new_DHT(new_networking(ip, PORT));
127
191
  printf("OUR ID: ");
128
192
  uint32_t i;
129
- for(i = 0; i < 32; i++) {
130
- if(self_public_key[i] < 16)
193
+
194
+ for (i = 0; i < 32; i++) {
195
+ if (dht->self_public_key[i] < 16)
131
196
  printf("0");
132
- printf("%hhX",self_public_key[i]);
197
+
198
+ printf("%hhX", dht->self_public_key[i]);
133
199
  }
134
-
200
+
135
201
  char temp_id[128];
136
202
  printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
137
- if(scanf("%s", temp_id) != 1)
203
+
204
+ if (!fgets(temp_id, sizeof(temp_id), stdin))
138
205
  exit(0);
139
-
140
- DHT_addfriend(hex_string_to_bin(temp_id));
141
-
142
- /* initialize networking */
143
- /* bind to ip 0.0.0.0:PORT */
144
- IP ip;
145
- ip.i = 0;
146
- init_networking(ip, PORT);
147
-
206
+
207
+ if ((strlen(temp_id) > 0) && (temp_id[strlen(temp_id) - 1] == '\n'))
208
+ temp_id[strlen(temp_id) - 1] = '\0';
209
+
210
+ uint8_t *bin_id = hex_string_to_bin(temp_id);
211
+ DHT_addfriend(dht, bin_id);
212
+ free(bin_id);
148
213
 
149
214
  perror("Initialization");
150
- IP_Port bootstrap_ip_port;
151
- bootstrap_ip_port.port = htons(atoi(argv[2]));
152
- /* bootstrap_ip_port.ip.c[0] = 127;
153
- * bootstrap_ip_port.ip.c[1] = 0;
154
- * bootstrap_ip_port.ip.c[2] = 0;
155
- * bootstrap_ip_port.ip.c[3] = 1; */
156
- bootstrap_ip_port.ip.i = inet_addr(argv[1]);
157
- DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
158
-
159
- IP_Port ip_port;
160
- uint8_t data[MAX_UDP_PACKET_SIZE];
161
- uint32_t length;
162
-
163
- while(1) {
164
-
165
- doDHT();
166
-
167
- while(receivepacket(&ip_port, data, &length) != -1) {
168
- if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) {
169
- //unhandled packet
170
- printpacket(data, length, ip_port);
171
- } else {
172
- printf("Received handled packet with length: %u\n", length);
173
- }
174
- }
175
- print_clientlist();
176
- print_friendlist();
215
+
216
+ uint16_t port = htons(atoi(argv[argvoffset + 2]));
217
+ unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
218
+ int res = DHT_bootstrap_from_address(dht, argv[argvoffset + 1], ipv6enabled, port, binary_string);
219
+ free(binary_string);
220
+
221
+ if (!res) {
222
+ printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
223
+ return 1;
224
+ }
225
+
226
+ /*
227
+ IP_Port ip_port;
228
+ uint8_t data[MAX_UDP_PACKET_SIZE];
229
+ uint32_t length;
230
+ */
231
+
232
+ while (1) {
233
+
234
+ do_DHT(dht);
235
+
236
+ /* slvrTODO:
237
+ while(receivepacket(&ip_port, data, &length) != -1) {
238
+ if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) {
239
+ //unhandled packet
240
+ printpacket(data, length, ip_port);
241
+ } else {
242
+ printf("Received handled packet with length: %u\n", length);
243
+ }
244
+ }
245
+ */
246
+ networking_poll(dht->net);
247
+
248
+ print_clientlist(dht);
249
+ print_friendlist(dht);
177
250
  c_sleep(300);
178
251
  }
179
-
180
- shutdown_networking();
181
- return 0;
252
+
253
+ return 0;
182
254
  }