diff options
author | irungentoo <irungentoo@gmail.com> | 2013-10-05 11:14:06 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-10-05 11:14:06 -0400 |
commit | ac117322a98530c7d211095bf75efd240c2e6763 (patch) | |
tree | 92e8e578efe3865ec365548bdcbf944226dbb940 | |
parent | b2c5e16132686f05c6726cd41e117656f3f1e636 (diff) |
Tox sync added.
Tox sync is a proof of concept bittorrent sync inspired application that
uses tox to sync the contents of two folders.
Currently only works on POSIX compilant operating systems.
-rw-r--r-- | testing/Makefile.inc | 18 | ||||
-rw-r--r-- | testing/tox_sync.c | 291 |
2 files changed, 308 insertions, 1 deletions
diff --git a/testing/Makefile.inc b/testing/Makefile.inc index ee68e289..65f9f64c 100644 --- a/testing/Makefile.inc +++ b/testing/Makefile.inc | |||
@@ -23,7 +23,8 @@ noinst_PROGRAMS += DHT_test \ | |||
23 | Lossless_UDP_testclient \ | 23 | Lossless_UDP_testclient \ |
24 | Lossless_UDP_testserver \ | 24 | Lossless_UDP_testserver \ |
25 | Messenger_test \ | 25 | Messenger_test \ |
26 | crypto_speed_test | 26 | crypto_speed_test \ |
27 | tox_sync | ||
27 | 28 | ||
28 | DHT_test_SOURCES = ../testing/DHT_test.c | 29 | DHT_test_SOURCES = ../testing/DHT_test.c |
29 | 30 | ||
@@ -99,4 +100,19 @@ crypto_speed_test_LDADD = \ | |||
99 | $(NACL_LIBS) \ | 100 | $(NACL_LIBS) \ |
100 | $(WINSOCK2_LIBS) | 101 | $(WINSOCK2_LIBS) |
101 | 102 | ||
103 | tox_sync_SOURCES = \ | ||
104 | ../testing/tox_sync.c | ||
105 | |||
106 | tox_sync_CFLAGS = \ | ||
107 | $(LIBSODIUM_CFLAGS) \ | ||
108 | $(NACL_CFLAGS) | ||
109 | |||
110 | tox_sync_LDADD = \ | ||
111 | $(LIBSODIUM_LDFLAGS) \ | ||
112 | $(NACL_LDFLAGS) \ | ||
113 | libtoxcore.la \ | ||
114 | $(LIBSODIUM_LIBS) \ | ||
115 | $(NACL_LIBS) \ | ||
116 | $(WINSOCK2_LIBS) | ||
117 | |||
102 | EXTRA_DIST += $(top_srcdir)/testing/misc_tools.c | 118 | EXTRA_DIST += $(top_srcdir)/testing/misc_tools.c |
diff --git a/testing/tox_sync.c b/testing/tox_sync.c new file mode 100644 index 00000000..486e4b3a --- /dev/null +++ b/testing/tox_sync.c | |||
@@ -0,0 +1,291 @@ | |||
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 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
13 | * | ||
14 | * This file is part of Tox. | ||
15 | * | ||
16 | * Tox is free software: you can redistribute it and/or modify | ||
17 | * it under the terms of the GNU General Public License as published by | ||
18 | * the Free Software Foundation, either version 3 of the License, or | ||
19 | * (at your option) any later version. | ||
20 | * | ||
21 | * Tox is distributed in the hope that it will be useful, | ||
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
24 | * GNU General Public License for more details. | ||
25 | * | ||
26 | * You should have received a copy of the GNU General Public License | ||
27 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
28 | * | ||
29 | */ | ||
30 | |||
31 | #ifdef HAVE_CONFIG_H | ||
32 | #include "config.h" | ||
33 | #endif | ||
34 | |||
35 | #include "../toxcore/tox.h" | ||
36 | #include "misc_tools.c" | ||
37 | |||
38 | #include <unistd.h> | ||
39 | #define c_sleep(x) usleep(1000*x) | ||
40 | |||
41 | #include <dirent.h> | ||
42 | #include <stdio.h> | ||
43 | |||
44 | #define NUM_FILE_SENDERS 256 | ||
45 | typedef struct { | ||
46 | FILE *file; | ||
47 | uint16_t friendnum; | ||
48 | uint8_t filenumber; | ||
49 | uint8_t nextpiece[1024]; | ||
50 | uint16_t piecelength; | ||
51 | } File_t; | ||
52 | File_t file_senders[NUM_FILE_SENDERS]; | ||
53 | File_t file_recv[NUM_FILE_SENDERS]; | ||
54 | uint8_t numfilesenders; | ||
55 | |||
56 | void send_filesenders(Tox *m) | ||
57 | { | ||
58 | uint32_t i; | ||
59 | |||
60 | for (i = 0; i < NUM_FILE_SENDERS; ++i) { | ||
61 | if (file_senders[i].file == 0) | ||
62 | continue; | ||
63 | |||
64 | while (1) { | ||
65 | if (!tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece, | ||
66 | file_senders[i].piecelength)) | ||
67 | break; | ||
68 | |||
69 | file_senders[i].piecelength = fread(file_senders[i].nextpiece, 1, 1000, file_senders[i].file); | ||
70 | |||
71 | if (file_senders[i].piecelength == 0) { | ||
72 | fclose(file_senders[i].file); | ||
73 | file_senders[i].file = 0; | ||
74 | |||
75 | printf("[t] %u file transfer: %u completed %i\n", file_senders[i].friendnum, file_senders[i].filenumber, | ||
76 | tox_file_sendcontrol(m, file_senders[i].friendnum, 0, file_senders[i].filenumber, TOX_FILECONTROL_FINISHED, 0, 0)); | ||
77 | break; | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | int add_filesender(Tox *m, uint16_t friendnum, char *filename) | ||
83 | { | ||
84 | FILE *tempfile = fopen(filename, "rb"); | ||
85 | |||
86 | if (tempfile == 0) | ||
87 | return -1; | ||
88 | |||
89 | fseek(tempfile, 0, SEEK_END); | ||
90 | uint64_t filesize = ftell(tempfile); | ||
91 | fseek(tempfile, 0, SEEK_SET); | ||
92 | int filenum = tox_new_filesender(m, friendnum, filesize, (uint8_t *)filename, strlen(filename) + 1); | ||
93 | |||
94 | if (filenum == -1) | ||
95 | return -1; | ||
96 | |||
97 | file_senders[numfilesenders].file = tempfile; | ||
98 | file_senders[numfilesenders].piecelength = fread(file_senders[numfilesenders].nextpiece, 1, 1000, | ||
99 | file_senders[numfilesenders].file); | ||
100 | file_senders[numfilesenders].friendnum = friendnum; | ||
101 | file_senders[numfilesenders].filenumber = filenum; | ||
102 | ++numfilesenders; | ||
103 | return filenum; | ||
104 | } | ||
105 | |||
106 | void kill_filesender(Tox *m, uint8_t filenum) | ||
107 | { | ||
108 | uint32_t i; | ||
109 | |||
110 | for (i = 0; i < NUM_FILE_SENDERS; ++i) | ||
111 | if (file_senders[i].file != 0 && file_senders[i].filenumber == filenum) { | ||
112 | fclose(file_senders[i].file); | ||
113 | file_senders[i].file = 0; | ||
114 | } | ||
115 | } | ||
116 | int not_sending() | ||
117 | { | ||
118 | uint32_t i; | ||
119 | |||
120 | for (i = 0; i < NUM_FILE_SENDERS; ++i) | ||
121 | if (file_senders[i].file != 0) | ||
122 | return 0; | ||
123 | |||
124 | return 1; | ||
125 | } | ||
126 | |||
127 | static char path[1024]; | ||
128 | |||
129 | void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, | ||
130 | uint16_t filename_length, void *userdata) | ||
131 | { | ||
132 | char fullpath[1024]; | ||
133 | uint32_t i; | ||
134 | uint16_t rm = 0; | ||
135 | |||
136 | for (i = 0; i < strlen((char *)filename); ++i) { | ||
137 | if (filename[i] == '/') | ||
138 | rm = i; | ||
139 | } | ||
140 | |||
141 | if (path[strlen(path) - 1] == '/') | ||
142 | sprintf(fullpath, "%s%s", path, filename + rm + 1); | ||
143 | else | ||
144 | sprintf(fullpath, "%s/%s", path, filename + rm + 1); | ||
145 | |||
146 | FILE *tempfile = fopen(fullpath, "rb"); | ||
147 | |||
148 | if (tempfile != 0) { | ||
149 | fclose(tempfile); | ||
150 | tox_file_sendcontrol(m, friendnumber, 1, filenumber, TOX_FILECONTROL_KILL, 0, 0); | ||
151 | return; | ||
152 | } | ||
153 | |||
154 | file_recv[filenumber].file = fopen(fullpath, "wb"); | ||
155 | |||
156 | if (file_recv[filenumber].file == 0) { | ||
157 | tox_file_sendcontrol(m, friendnumber, 1, filenumber, TOX_FILECONTROL_KILL, 0, 0); | ||
158 | return; | ||
159 | } | ||
160 | |||
161 | if (tox_file_sendcontrol(m, friendnumber, 1, filenumber, TOX_FILECONTROL_ACCEPT, 0, 0)) { | ||
162 | printf("Accepted file transfer. (file: %s)\n", fullpath); | ||
163 | } | ||
164 | |||
165 | } | ||
166 | |||
167 | void file_print_control(Tox *m, int friendnumber, uint8_t recieve_send, uint8_t filenumber, uint8_t control_type, | ||
168 | uint8_t *data, | ||
169 | uint16_t length, void *userdata) | ||
170 | { | ||
171 | if (recieve_send == 1 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) { | ||
172 | kill_filesender(m, filenumber); | ||
173 | return; | ||
174 | } | ||
175 | |||
176 | if (recieve_send == 0 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) { | ||
177 | fclose(file_recv[filenumber].file); | ||
178 | printf("File closed\n"); | ||
179 | file_recv[filenumber].file = 0; | ||
180 | return; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) | ||
185 | { | ||
186 | if (file_recv[filenumber].file != 0) | ||
187 | fwrite(data, length, 1, file_recv[filenumber].file); | ||
188 | } | ||
189 | |||
190 | void print_online(Tox *tox, int friendnumber, uint8_t status, void *userdata) | ||
191 | { | ||
192 | if (status == 1) | ||
193 | printf("\nOther went online.\n"); | ||
194 | else | ||
195 | printf("\nOther went offline.\n"); | ||
196 | } | ||
197 | |||
198 | int main(int argc, char *argv[]) | ||
199 | { | ||
200 | uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ | ||
201 | int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); | ||
202 | |||
203 | if (argvoffset < 0) | ||
204 | exit(1); | ||
205 | |||
206 | /* with optional --ipvx, now it can be 1-4 arguments... */ | ||
207 | if ((argc != argvoffset + 3) && (argc != argvoffset + 5)) { | ||
208 | printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node) folder (to sync)\n", argv[0]); | ||
209 | exit(0); | ||
210 | } | ||
211 | |||
212 | Tox *tox = tox_new(ipv6enabled); | ||
213 | tox_callback_file_data(tox, write_file, NULL); | ||
214 | tox_callback_file_control(tox, file_print_control, NULL); | ||
215 | tox_callback_file_sendrequest(tox, file_request_accept, NULL); | ||
216 | tox_callback_connectionstatus(tox, print_online, NULL); | ||
217 | |||
218 | uint16_t port = htons(atoi(argv[argvoffset + 2])); | ||
219 | unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); | ||
220 | int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], ipv6enabled, port, binary_string); | ||
221 | |||
222 | if (!res) { | ||
223 | printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); | ||
224 | exit(1); | ||
225 | } | ||
226 | |||
227 | uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; | ||
228 | tox_getaddress(tox, address); | ||
229 | uint32_t i; | ||
230 | |||
231 | for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) { | ||
232 | printf("%02X", address[i]); | ||
233 | } | ||
234 | |||
235 | char temp_id[128]; | ||
236 | printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n"); | ||
237 | |||
238 | if (scanf("%s", temp_id) != 1) { | ||
239 | return 1; | ||
240 | } | ||
241 | |||
242 | int num = tox_addfriend(tox, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); | ||
243 | |||
244 | if (num < 0) { | ||
245 | printf("\nSomething went wrong when adding friend.\n"); | ||
246 | return 1; | ||
247 | } | ||
248 | |||
249 | memcpy(path, argv[argvoffset + 4], strlen(argv[argvoffset + 4])); | ||
250 | DIR *d; | ||
251 | struct dirent *dir; | ||
252 | uint8_t notconnected = 1; | ||
253 | |||
254 | while (1) { | ||
255 | if (tox_isconnected(tox) && notconnected) { | ||
256 | printf("\nDHT connected.\n"); | ||
257 | notconnected = 0; | ||
258 | } | ||
259 | |||
260 | if (not_sending() && tox_get_friend_connectionstatus(tox, num)) { | ||
261 | d = opendir(path); | ||
262 | |||
263 | if (d) { | ||
264 | while ((dir = readdir(d)) != NULL) { | ||
265 | if (dir->d_type == DT_REG) { | ||
266 | char fullpath[1024]; | ||
267 | |||
268 | if (path[strlen(path) - 1] == '/') | ||
269 | sprintf(fullpath, "%s%s", path, dir->d_name); | ||
270 | else | ||
271 | sprintf(fullpath, "%s/%s", path, dir->d_name); | ||
272 | |||
273 | add_filesender(tox, num, fullpath); | ||
274 | } | ||
275 | } | ||
276 | |||
277 | closedir(d); | ||
278 | |||
279 | } else { | ||
280 | printf("\nFailed to open directory.\n"); | ||
281 | return 1; | ||
282 | } | ||
283 | } | ||
284 | |||
285 | send_filesenders(tox); | ||
286 | tox_do(tox); | ||
287 | c_sleep(1); | ||
288 | } | ||
289 | |||
290 | return 0; | ||
291 | } | ||