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,5 +1,5 @@
1
1
  /* nTox.h
2
- *
2
+ *
3
3
  *Textual frontend for Tox.
4
4
  *
5
5
  * Copyright (C) 2013 Tox project All Rights Reserved.
@@ -18,34 +18,25 @@
18
18
  *
19
19
  * You should have received a copy of the GNU General Public License
20
20
  * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21
- *
21
+ *
22
22
  */
23
23
 
24
24
  #ifndef NTOX_H
25
25
  #define NTOX_H
26
26
 
27
- #include <stdio.h>
28
- #include <stdlib.h>
29
- #include <string.h>
30
- #include <ncurses.h>
31
- #include <curses.h>
27
+ /*
28
+ * module actually exports nothing for the outside
29
+ */
30
+
32
31
  #include <ctype.h>
33
- #include <sys/socket.h>
34
- #include <netinet/in.h>
35
- #include <arpa/inet.h>
36
- #include <sys/types.h>
37
- #include <netdb.h>
38
- #include "../core/Messenger.h"
39
- #include "../core/network.h"
32
+ #include <curses.h>
33
+
34
+ #include "../toxcore/tox.h"
35
+
40
36
  #define STRING_LENGTH 256
41
37
  #define HISTORY 50
42
- #define PUB_KEY_BYTES 32
43
38
 
44
39
  void new_lines(char *line);
45
- void line_eval(char lines[HISTORY][STRING_LENGTH], char *line);
46
- void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ;
47
- int count_lines(char *string) ;
48
- char *appender(char *str, const char c);
49
40
  void do_refresh();
50
41
 
51
42
  #endif
@@ -0,0 +1,159 @@
1
+ /* Tox Shell
2
+ *
3
+ * Proof of concept ssh like server software using tox.
4
+ *
5
+ * Command line arguments are the ip, port and public_key of a node (for bootstrapping).
6
+ *
7
+ * EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
8
+ *
9
+ *
10
+ * Copyright (C) 2014 Tox project All Rights Reserved.
11
+ *
12
+ * This file is part of Tox.
13
+ *
14
+ * Tox is free software: you can redistribute it and/or modify
15
+ * it under the terms of the GNU General Public License as published by
16
+ * the Free Software Foundation, either version 3 of the License, or
17
+ * (at your option) any later version.
18
+ *
19
+ * Tox is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License
25
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
26
+ *
27
+ */
28
+
29
+ #ifdef HAVE_CONFIG_H
30
+ #include "config.h"
31
+ #endif
32
+
33
+ #include "../toxcore/tox.h"
34
+ #include "misc_tools.c"
35
+
36
+ #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
37
+ #include <util.h>
38
+ #elif defined(__FreeBSD__)
39
+ #include <libutil.h>
40
+ #else
41
+ #include <pty.h>
42
+ #endif
43
+ #include <unistd.h>
44
+ #include <fcntl.h>
45
+
46
+ #define c_sleep(x) usleep(1000*x)
47
+
48
+ void print_online(Tox *tox, int friendnumber, uint8_t status, void *userdata)
49
+ {
50
+ if (status == 1)
51
+ printf("\nOther went online.\n");
52
+ else
53
+ printf("\nOther went offline.\n");
54
+ }
55
+
56
+ void print_message(Tox *tox, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
57
+ {
58
+ int master = *((int *)userdata);
59
+ write(master, string, length);
60
+ write(master, "\n", 1);
61
+ }
62
+
63
+ int main(int argc, char *argv[])
64
+ {
65
+ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
66
+ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
67
+
68
+ if (argvoffset < 0)
69
+ exit(1);
70
+
71
+ /* with optional --ipvx, now it can be 1-4 arguments... */
72
+ if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) {
73
+ printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]);
74
+ exit(0);
75
+ }
76
+
77
+ int *master = malloc(sizeof(int));
78
+ int ret = forkpty(master, NULL, NULL, NULL);
79
+
80
+ if (ret == -1) {
81
+ printf("fork failed\n");
82
+ return 1;
83
+ }
84
+
85
+ if (ret == 0) {
86
+ execl("/bin/sh", "sh", NULL);
87
+ return 0;
88
+ }
89
+
90
+ int flags = fcntl(*master, F_GETFL, 0);
91
+ int r = fcntl(*master, F_SETFL, flags | O_NONBLOCK);
92
+
93
+ if (r < 0) {
94
+ printf("error setting flags\n");
95
+ }
96
+
97
+ Tox *tox = tox_new(0);
98
+ tox_callback_connection_status(tox, print_online, NULL);
99
+ tox_callback_friend_message(tox, print_message, master);
100
+
101
+
102
+ uint16_t port = atoi(argv[argvoffset + 2]);
103
+ unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
104
+ int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], port, binary_string);
105
+ free(binary_string);
106
+
107
+ if (!res) {
108
+ printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
109
+ exit(1);
110
+ }
111
+
112
+ uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
113
+ tox_get_address(tox, address);
114
+ uint32_t i;
115
+
116
+ for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
117
+ printf("%02X", address[i]);
118
+ }
119
+
120
+ char temp_id[128];
121
+ printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n");
122
+
123
+ if (scanf("%s", temp_id) != 1) {
124
+ return 1;
125
+ }
126
+
127
+ uint8_t *bin_id = hex_string_to_bin(temp_id);
128
+ int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
129
+ free(bin_id);
130
+
131
+ if (num < 0) {
132
+ printf("\nSomething went wrong when adding friend.\n");
133
+ return 1;
134
+ }
135
+
136
+ uint8_t notconnected = 1;
137
+
138
+ while (1) {
139
+ if (tox_isconnected(tox) && notconnected) {
140
+ printf("\nDHT connected.\n");
141
+ notconnected = 0;
142
+ }
143
+
144
+ while (tox_get_friend_connection_status(tox, num) == 1) {
145
+ uint8_t buf[TOX_MAX_MESSAGE_LENGTH];
146
+ ret = read(*master, buf, sizeof(buf));
147
+
148
+ if (ret <= 0)
149
+ break;
150
+
151
+ tox_send_message(tox, num, buf, ret);
152
+ }
153
+
154
+ tox_do(tox);
155
+ c_sleep(1);
156
+ }
157
+
158
+ return 0;
159
+ }
@@ -0,0 +1,299 @@
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
+ #include <netinet/in.h>
44
+ #include <arpa/inet.h>
45
+
46
+ #define NUM_FILE_SENDERS 256
47
+ typedef struct {
48
+ FILE *file;
49
+ uint16_t friendnum;
50
+ uint8_t filenumber;
51
+ uint8_t nextpiece[1400];
52
+ uint16_t piecelength;
53
+ } File_t;
54
+ File_t file_senders[NUM_FILE_SENDERS];
55
+ File_t file_recv[NUM_FILE_SENDERS];
56
+ uint8_t numfilesenders;
57
+
58
+ void send_filesenders(Tox *m)
59
+ {
60
+ uint32_t i;
61
+
62
+ for (i = 0; i < NUM_FILE_SENDERS; ++i) {
63
+ if (file_senders[i].file == 0)
64
+ continue;
65
+
66
+ while (1) {
67
+ if (tox_file_send_data(m, file_senders[i].friendnum, file_senders[i].filenumber, file_senders[i].nextpiece,
68
+ file_senders[i].piecelength) != 0)
69
+ break;
70
+
71
+ file_senders[i].piecelength = fread(file_senders[i].nextpiece, 1, tox_file_data_size(m, file_senders[i].friendnum),
72
+ file_senders[i].file);
73
+
74
+ if (file_senders[i].piecelength == 0) {
75
+ fclose(file_senders[i].file);
76
+ file_senders[i].file = 0;
77
+
78
+ printf("[t] %u file transfer: %u completed %i\n", file_senders[i].friendnum, file_senders[i].filenumber,
79
+ tox_file_send_control(m, file_senders[i].friendnum, 0, file_senders[i].filenumber, TOX_FILECONTROL_FINISHED, 0, 0));
80
+ break;
81
+ }
82
+ }
83
+ }
84
+ }
85
+ int add_filesender(Tox *m, uint16_t friendnum, char *filename)
86
+ {
87
+ FILE *tempfile = fopen(filename, "rb");
88
+
89
+ if (tempfile == 0)
90
+ return -1;
91
+
92
+ fseek(tempfile, 0, SEEK_END);
93
+ uint64_t filesize = ftell(tempfile);
94
+ fseek(tempfile, 0, SEEK_SET);
95
+ int filenum = tox_new_file_sender(m, friendnum, filesize, (uint8_t *)filename, strlen(filename) + 1);
96
+
97
+ if (filenum == -1)
98
+ return -1;
99
+
100
+ file_senders[numfilesenders].file = tempfile;
101
+ file_senders[numfilesenders].piecelength = fread(file_senders[numfilesenders].nextpiece, 1, tox_file_data_size(m,
102
+ file_senders[numfilesenders].friendnum),
103
+ file_senders[numfilesenders].file);
104
+ file_senders[numfilesenders].friendnum = friendnum;
105
+ file_senders[numfilesenders].filenumber = filenum;
106
+ ++numfilesenders;
107
+ return filenum;
108
+ }
109
+
110
+ void kill_filesender(Tox *m, uint8_t filenum)
111
+ {
112
+ uint32_t i;
113
+
114
+ for (i = 0; i < NUM_FILE_SENDERS; ++i)
115
+ if (file_senders[i].file != 0 && file_senders[i].filenumber == filenum) {
116
+ fclose(file_senders[i].file);
117
+ file_senders[i].file = 0;
118
+ }
119
+ }
120
+ int not_sending()
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
+ return 1;
129
+ }
130
+
131
+ static char path[1024];
132
+
133
+ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, const uint8_t *filename,
134
+ uint16_t filename_length, void *userdata)
135
+ {
136
+ char fullpath[1024];
137
+ uint32_t i;
138
+ uint16_t rm = 0;
139
+
140
+ for (i = 0; i < strlen((char *)filename); ++i) {
141
+ if (filename[i] == '/')
142
+ rm = i;
143
+ }
144
+
145
+ if (path[strlen(path) - 1] == '/')
146
+ sprintf(fullpath, "%s%s", path, filename + rm + 1);
147
+ else
148
+ sprintf(fullpath, "%s/%s", path, filename + rm + 1);
149
+
150
+ FILE *tempfile = fopen(fullpath, "rb");
151
+
152
+ if (tempfile != 0) {
153
+ fclose(tempfile);
154
+ tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_KILL, 0, 0);
155
+ return;
156
+ }
157
+
158
+ file_recv[filenumber].file = fopen(fullpath, "wb");
159
+
160
+ if (file_recv[filenumber].file == 0) {
161
+ tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_KILL, 0, 0);
162
+ return;
163
+ }
164
+
165
+ if (tox_file_send_control(m, friendnumber, 1, filenumber, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) {
166
+ printf("Accepted file transfer. (file: %s)\n", fullpath);
167
+ }
168
+
169
+ }
170
+
171
+ void file_print_control(Tox *m, int friendnumber, uint8_t recieve_send, uint8_t filenumber, uint8_t control_type,
172
+ const uint8_t *data,
173
+ uint16_t length, void *userdata)
174
+ {
175
+ if (recieve_send == 1 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) {
176
+ kill_filesender(m, filenumber);
177
+ return;
178
+ }
179
+
180
+ if (recieve_send == 0 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) {
181
+ fclose(file_recv[filenumber].file);
182
+ printf("File closed\n");
183
+ file_recv[filenumber].file = 0;
184
+ return;
185
+ }
186
+ }
187
+
188
+ void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length, void *userdata)
189
+ {
190
+ if (file_recv[filenumber].file != 0)
191
+ if (fwrite(data, length, 1, file_recv[filenumber].file) != 1)
192
+ printf("Error writing data\n");
193
+ }
194
+
195
+ void print_online(Tox *tox, int friendnumber, uint8_t status, void *userdata)
196
+ {
197
+ if (status == 1)
198
+ printf("\nOther went online.\n");
199
+ else
200
+ printf("\nOther went offline.\n");
201
+ }
202
+
203
+ int main(int argc, char *argv[])
204
+ {
205
+ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
206
+ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
207
+
208
+ if (argvoffset < 0)
209
+ exit(1);
210
+
211
+ /* with optional --ipvx, now it can be 1-4 arguments... */
212
+ if ((argc != argvoffset + 3) && (argc != argvoffset + 5)) {
213
+ printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node) folder (to sync)\n", argv[0]);
214
+ exit(0);
215
+ }
216
+
217
+ Tox *tox = tox_new(0);
218
+ tox_callback_file_data(tox, write_file, NULL);
219
+ tox_callback_file_control(tox, file_print_control, NULL);
220
+ tox_callback_file_send_request(tox, file_request_accept, NULL);
221
+ tox_callback_connection_status(tox, print_online, NULL);
222
+
223
+ uint16_t port = atoi(argv[argvoffset + 2]);
224
+ unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
225
+ int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], port, binary_string);
226
+ free(binary_string);
227
+
228
+ if (!res) {
229
+ printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
230
+ exit(1);
231
+ }
232
+
233
+ uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
234
+ tox_get_address(tox, address);
235
+ uint32_t i;
236
+
237
+ for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
238
+ printf("%02X", address[i]);
239
+ }
240
+
241
+ char temp_id[128];
242
+ printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n");
243
+
244
+ if (scanf("%s", temp_id) != 1) {
245
+ return 1;
246
+ }
247
+
248
+ uint8_t *bin_id = hex_string_to_bin(temp_id);
249
+ int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
250
+ free(bin_id);
251
+
252
+ if (num < 0) {
253
+ printf("\nSomething went wrong when adding friend.\n");
254
+ return 1;
255
+ }
256
+
257
+ memcpy(path, argv[argvoffset + 4], strlen(argv[argvoffset + 4]));
258
+ DIR *d;
259
+ struct dirent *dir;
260
+ uint8_t notconnected = 1;
261
+
262
+ while (1) {
263
+ if (tox_isconnected(tox) && notconnected) {
264
+ printf("\nDHT connected.\n");
265
+ notconnected = 0;
266
+ }
267
+
268
+ if (not_sending() && tox_get_friend_connection_status(tox, num)) {
269
+ d = opendir(path);
270
+
271
+ if (d) {
272
+ while ((dir = readdir(d)) != NULL) {
273
+ if (dir->d_type == DT_REG) {
274
+ char fullpath[1024];
275
+
276
+ if (path[strlen(path) - 1] == '/')
277
+ sprintf(fullpath, "%s%s", path, dir->d_name);
278
+ else
279
+ sprintf(fullpath, "%s/%s", path, dir->d_name);
280
+
281
+ add_filesender(tox, num, fullpath);
282
+ }
283
+ }
284
+
285
+ closedir(d);
286
+
287
+ } else {
288
+ printf("\nFailed to open directory.\n");
289
+ return 1;
290
+ }
291
+ }
292
+
293
+ send_filesenders(tox);
294
+ tox_do(tox);
295
+ c_sleep(1);
296
+ }
297
+
298
+ return 0;
299
+ }