summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-02-02 17:45:42 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-02-05 18:15:31 +0000
commitf71ec8dd02ef6e12b8ac954e79795a74a8323550 (patch)
treed1b3c4d4b51a3a3be92938ac5db671b3fcf322e9 /testing
parent9f2582d699887d751d467a8b1522d1e6860191b4 (diff)
Move the tox_sync tool to the toxins repository.
Diffstat (limited to 'testing')
-rw-r--r--testing/BUILD.bazel9
-rw-r--r--testing/Makefile.inc15
-rw-r--r--testing/tox_sync.c344
3 files changed, 0 insertions, 368 deletions
diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel
index 37141cbd..ee97fd1f 100644
--- a/testing/BUILD.bazel
+++ b/testing/BUILD.bazel
@@ -61,12 +61,3 @@ cc_binary(
61 "//c-toxcore/toxcore", 61 "//c-toxcore/toxcore",
62 ], 62 ],
63) 63)
64
65cc_binary(
66 name = "tox_sync",
67 srcs = ["tox_sync.c"],
68 deps = [
69 ":misc_tools",
70 "//c-toxcore/toxcore",
71 ],
72)
diff --git a/testing/Makefile.inc b/testing/Makefile.inc
index 77f5f3a0..b489a701 100644
--- a/testing/Makefile.inc
+++ b/testing/Makefile.inc
@@ -35,21 +35,6 @@ Messenger_test_LDADD = $(LIBSODIUM_LDFLAGS) \
35 35
36if !WIN32 36if !WIN32
37 37
38noinst_PROGRAMS += tox_sync
39
40tox_sync_SOURCES = ../testing/tox_sync.c
41
42tox_sync_CFLAGS = $(LIBSODIUM_CFLAGS) \
43 $(NACL_CFLAGS)
44
45tox_sync_LDADD = $(LIBSODIUM_LDFLAGS) \
46 $(NACL_LDFLAGS) \
47 libtoxcore.la \
48 $(LIBSODIUM_LIBS) \
49 $(NACL_OBJECTS) \
50 $(NACL_LIBS)
51
52
53noinst_PROGRAMS += tox_shell 38noinst_PROGRAMS += tox_shell
54 39
55tox_shell_SOURCES = ../testing/tox_shell.c 40tox_shell_SOURCES = ../testing/tox_shell.c
diff --git a/testing/tox_sync.c b/testing/tox_sync.c
deleted file mode 100644
index 382e49f9..00000000
--- a/testing/tox_sync.c
+++ /dev/null
@@ -1,344 +0,0 @@
1/* Tox Sync
2 *
3 * Proof of concept bittorrent sync like software using tox, syncs two directories.
4 *
5 * Command line arguments are the ip, port and public_key of a node (for bootstrapping) and the folder to sync.
6 *
7 * EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C ./sync_folder/
8 *
9 * NOTE: for security purposes, both tox sync instances must manually add each other as friend for it to work.
10 */
11
12/*
13 * Copyright © 2016-2017 The TokTok team.
14 * Copyright © 2013 Tox project.
15 *
16 * This file is part of Tox, the free peer to peer instant messenger.
17 *
18 * Tox is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * Tox is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
30 */
31#define _XOPEN_SOURCE 600
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
36
37#include "../toxcore/ccompat.h"
38#include "../toxcore/tox.h"
39#include "misc_tools.c"
40
41#include <arpa/inet.h>
42#include <dirent.h>
43#include <netinet/in.h>
44#include <stdio.h>
45#include <sys/stat.h>
46
47#define NUM_FILE_SENDERS 256
48typedef struct {
49 FILE *file;
50 uint32_t friendnum;
51 uint32_t filenumber;
52} File_t;
53static File_t file_senders[NUM_FILE_SENDERS];
54static File_t file_recv[NUM_FILE_SENDERS];
55static uint8_t numfilesenders;
56
57static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position,
58 size_t length,
59 void *user_data)
60{
61 unsigned int i;
62
63 for (i = 0; i < NUM_FILE_SENDERS; ++i) {
64 /* This is slow */
65 if (file_senders[i].file && file_senders[i].friendnum == friend_number && file_senders[i].filenumber == file_number) {
66 if (length == 0) {
67 fclose(file_senders[i].file);
68 file_senders[i].file = 0;
69 printf("[t] %u file transfer: %u completed\n", file_senders[i].friendnum, file_senders[i].filenumber);
70 break;
71 }
72
73 fseek(file_senders[i].file, position, SEEK_SET);
74 VLA(uint8_t, data, length);
75 int len = fread(data, 1, length, file_senders[i].file);
76 tox_file_send_chunk(tox, friend_number, file_number, position, data, len, 0);
77 break;
78 }
79 }
80}
81
82
83static uint32_t add_filesender(Tox *m, uint16_t friendnum, char *filename)
84{
85 FILE *tempfile = fopen(filename, "rb");
86
87 if (tempfile == 0) {
88 return -1;
89 }
90
91 fseek(tempfile, 0, SEEK_END);
92 uint64_t filesize = ftell(tempfile);
93 fseek(tempfile, 0, SEEK_SET);
94 uint32_t filenum = tox_file_send(m, friendnum, TOX_FILE_KIND_DATA, filesize, 0, (uint8_t *)filename,
95 strlen(filename), 0);
96
97 if (filenum == -1) {
98 fclose(tempfile);
99 return -1;
100 }
101
102 file_senders[numfilesenders].file = tempfile;
103 file_senders[numfilesenders].friendnum = friendnum;
104 file_senders[numfilesenders].filenumber = filenum;
105 ++numfilesenders;
106 return filenum;
107}
108
109static void kill_filesender(Tox *m, uint32_t filenum)
110{
111 uint32_t i;
112
113 for (i = 0; i < NUM_FILE_SENDERS; ++i) {
114 if (file_senders[i].file != 0 && file_senders[i].filenumber == filenum) {
115 fclose(file_senders[i].file);
116 file_senders[i].file = 0;
117 }
118 }
119}
120static int not_sending(void)
121{
122 uint32_t i;
123
124 for (i = 0; i < NUM_FILE_SENDERS; ++i) {
125 if (file_senders[i].file != 0) {
126 return 0;
127 }
128 }
129
130 return 1;
131}
132
133static char path[1024];
134
135static void file_request_accept(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t type,
136 uint64_t file_size,
137 const uint8_t *filename, size_t filename_length, void *user_data)
138{
139 if (type != TOX_FILE_KIND_DATA) {
140 printf("Refused invalid file type.");
141 tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0);
142 return;
143 }
144
145 char fullpath[1024];
146 uint32_t i;
147 uint16_t rm = 0;
148
149 for (i = 0; i < strlen((const char *)filename); ++i) {
150 if (filename[i] == '/') {
151 rm = i;
152 }
153 }
154
155 if (path[strlen(path) - 1] == '/') {
156 sprintf(fullpath, "%s%s", path, filename + rm + 1);
157 } else {
158 sprintf(fullpath, "%s/%s", path, filename + rm + 1);
159 }
160
161 FILE *tempfile = fopen(fullpath, "rb");
162
163 if (tempfile != 0) {
164 fclose(tempfile);
165 tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0);
166 return;
167 }
168
169 uint8_t file_index = (file_number >> 16) - 1;
170 file_recv[file_index].file = fopen(fullpath, "wb");
171
172 if (file_recv[file_index].file == 0) {
173 tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0);
174 return;
175 }
176
177 if (tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, 0)) {
178 printf("Accepted file transfer. (file: %s)\n", fullpath);
179 }
180}
181
182static void file_print_control(Tox *tox, uint32_t friend_number, uint32_t file_number, TOX_FILE_CONTROL control,
183 void *user_data)
184{
185 if (file_number < (1 << 15) && (control == TOX_FILE_CONTROL_CANCEL)) {
186 kill_filesender(tox, file_number);
187 return;
188 }
189
190 if (file_number > (1 << 15) && (control == TOX_FILE_CONTROL_CANCEL)) {
191 uint8_t file_index = (file_number >> 16) - 1;
192 fclose(file_recv[file_index].file);
193 printf("File closed\n");
194 file_recv[file_index].file = 0;
195 return;
196 }
197}
198
199static void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data,
200 size_t length, void *user_data)
201{
202 uint8_t file_index = (filenumber >> 16) - 1;
203
204 if (length == 0) {
205 fclose(file_recv[file_index].file);
206 printf("File closed\n");
207 file_recv[file_index].file = 0;
208 printf("%u file transfer: %u completed\n", friendnumber, filenumber);
209 return;
210 }
211
212 if (file_recv[file_index].file != 0) {
213 fseek(file_recv[file_index].file, position, SEEK_SET);
214
215 if (fwrite(data, length, 1, file_recv[file_index].file) != 1) {
216 printf("Error writing data\n");
217 }
218 }
219}
220
221static void print_online(Tox *tox, uint32_t friendnumber, TOX_CONNECTION status, void *userdata)
222{
223 if (status) {
224 printf("\nOther went online.\n");
225 } else {
226 printf("\nOther went offline.\n");
227 unsigned int i;
228
229 for (i = 0; i < NUM_FILE_SENDERS; ++i) {
230 if (file_senders[i].file != 0) {
231 fclose(file_senders[i].file);
232 file_senders[i].file = 0;
233 }
234
235 if (file_recv[i].file != 0) {
236 fclose(file_recv[i].file);
237 file_recv[i].file = 0;
238 }
239 }
240 }
241}
242
243int main(int argc, char *argv[])
244{
245 uint8_t ipv6enabled = 1; /* x */
246 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
247
248 if (argvoffset < 0) {
249 exit(1);
250 }
251
252 /* with optional --ipvx, now it can be 1-4 arguments... */
253 if ((argc != argvoffset + 3) && (argc != argvoffset + 5)) {
254 printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node) folder (to sync)\n", argv[0]);
255 exit(0);
256 }
257
258 Tox *tox = tox_new(0, 0);
259 tox_callback_file_recv_chunk(tox, write_file);
260 tox_callback_file_recv_control(tox, file_print_control);
261 tox_callback_file_recv(tox, file_request_accept);
262 tox_callback_file_chunk_request(tox, tox_file_chunk_request);
263 tox_callback_friend_connection_status(tox, print_online);
264
265 uint16_t port = atoi(argv[argvoffset + 2]);
266 unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
267 int res = tox_bootstrap(tox, argv[argvoffset + 1], port, binary_string, 0);
268 free(binary_string);
269
270 if (!res) {
271 printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
272 exit(1);
273 }
274
275 uint8_t address[TOX_ADDRESS_SIZE];
276 tox_self_get_address(tox, address);
277 uint32_t i;
278
279 for (i = 0; i < TOX_ADDRESS_SIZE; i++) {
280 printf("%02X", address[i]);
281 }
282
283 char temp_id[128];
284 printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n");
285
286 if (scanf("%s", temp_id) != 1) {
287 return 1;
288 }
289
290 uint8_t *bin_id = hex_string_to_bin(temp_id);
291 uint32_t num = tox_friend_add(tox, bin_id, (const uint8_t *)"Install Gentoo", sizeof("Install Gentoo"), 0);
292 free(bin_id);
293
294 if (num == UINT32_MAX) {
295 printf("\nSomething went wrong when adding friend.\n");
296 return 1;
297 }
298
299 memcpy(path, argv[argvoffset + 4], strlen(argv[argvoffset + 4]));
300 DIR *d;
301 struct dirent *dir;
302 struct stat statbuf;
303 uint8_t notconnected = 1;
304
305 while (1) {
306 if (tox_self_get_connection_status(tox) && notconnected) {
307 printf("\nDHT connected.\n");
308 notconnected = 0;
309 }
310
311 if (not_sending() && tox_friend_get_connection_status(tox, num, 0)) {
312 d = opendir(path);
313
314 if (d) {
315 while ((dir = readdir(d)) != nullptr) {
316 VLA(char, filepath, strlen(path) + strlen(dir->d_name) + 1);
317 memcpy(filepath, path, strlen(path));
318 memcpy(filepath + strlen(path), dir->d_name, strlen(dir->d_name) + 1);
319 stat(filepath, &statbuf);
320
321 if (S_ISREG(statbuf.st_mode)) {
322 char fullpath[1024];
323
324 if (path[strlen(path) - 1] == '/') {
325 sprintf(fullpath, "%s%s", path, dir->d_name);
326 } else {
327 sprintf(fullpath, "%s/%s", path, dir->d_name);
328 }
329
330 add_filesender(tox, num, fullpath);
331 }
332 }
333
334 closedir(d);
335 } else {
336 printf("\nFailed to open directory.\n");
337 return 1;
338 }
339 }
340
341 tox_iterate(tox, nullptr);
342 c_sleep(1);
343 }
344}