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,193 @@
1
+ /*
2
+ * util.c -- Utilities.
3
+ *
4
+ * This file is donated to the Tox Project.
5
+ * Copyright 2013 plutooo
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
+ #ifdef HAVE_CONFIG_H
26
+ #include "config.h"
27
+ #endif
28
+
29
+ #include <time.h>
30
+
31
+ /* for CLIENT_ID_SIZE */
32
+ #include "DHT.h"
33
+
34
+ #include "util.h"
35
+
36
+
37
+ /* don't call into system billions of times for no reason */
38
+ static uint64_t unix_time_value;
39
+ static uint64_t unix_base_time_value;
40
+
41
+ void unix_time_update()
42
+ {
43
+ if (unix_base_time_value == 0)
44
+ unix_base_time_value = ((uint64_t)time(NULL) - (current_time_monotonic() / 1000ULL));
45
+
46
+ unix_time_value = (current_time_monotonic() / 1000ULL) + unix_base_time_value;
47
+ }
48
+
49
+ uint64_t unix_time()
50
+ {
51
+ return unix_time_value;
52
+ }
53
+
54
+ int is_timeout(uint64_t timestamp, uint64_t timeout)
55
+ {
56
+ return timestamp + timeout <= unix_time();
57
+ }
58
+
59
+
60
+ /* id functions */
61
+ bool id_equal(const uint8_t *dest, const uint8_t *src)
62
+ {
63
+ return memcmp(dest, src, CLIENT_ID_SIZE) == 0;
64
+ }
65
+
66
+ uint32_t id_copy(uint8_t *dest, const uint8_t *src)
67
+ {
68
+ memcpy(dest, src, CLIENT_ID_SIZE);
69
+ return CLIENT_ID_SIZE;
70
+ }
71
+
72
+ void host_to_net(uint8_t *num, uint16_t numbytes)
73
+ {
74
+ #ifndef WORDS_BIGENDIAN
75
+ uint32_t i;
76
+ uint8_t buff[numbytes];
77
+
78
+ for (i = 0; i < numbytes; ++i) {
79
+ buff[i] = num[numbytes - i - 1];
80
+ }
81
+
82
+ memcpy(num, buff, numbytes);
83
+ #endif
84
+ return;
85
+ }
86
+
87
+ /* state load/save */
88
+ int load_state(load_state_callback_func load_state_callback, void *outer,
89
+ const uint8_t *data, uint32_t length, uint16_t cookie_inner)
90
+ {
91
+ if (!load_state_callback || !data) {
92
+ #ifdef DEBUG
93
+ fprintf(stderr, "load_state() called with invalid args.\n");
94
+ #endif
95
+ return -1;
96
+ }
97
+
98
+
99
+ uint16_t type;
100
+ uint32_t length_sub, cookie_type;
101
+ uint32_t size_head = sizeof(uint32_t) * 2;
102
+
103
+ while (length >= size_head) {
104
+ memcpy(&length_sub, data, sizeof(length_sub));
105
+ memcpy(&cookie_type, data + sizeof(length_sub), sizeof(cookie_type));
106
+ data += size_head;
107
+ length -= size_head;
108
+
109
+ if (length < length_sub) {
110
+ /* file truncated */
111
+ #ifdef DEBUG
112
+ fprintf(stderr, "state file too short: %u < %u\n", length, length_sub);
113
+ #endif
114
+ return -1;
115
+ }
116
+
117
+ if ((cookie_type >> 16) != cookie_inner) {
118
+ /* something is not matching up in a bad way, give up */
119
+ #ifdef DEBUG
120
+ fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner);
121
+ #endif
122
+ return -1;
123
+ }
124
+
125
+ type = cookie_type & 0xFFFF;
126
+
127
+ if (-1 == load_state_callback(outer, data, length_sub, type))
128
+ return -1;
129
+
130
+ data += length_sub;
131
+ length -= length_sub;
132
+ }
133
+
134
+ return length == 0 ? 0 : -1;
135
+ };
136
+
137
+ /* Converts 4 bytes to uint32_t */
138
+ inline__ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes)
139
+ {
140
+ *dest =
141
+ #ifdef WORDS_BIGENDIAN
142
+ ( ( uint32_t ) * bytes ) |
143
+ ( ( uint32_t ) * ( bytes + 1 ) << 8 ) |
144
+ ( ( uint32_t ) * ( bytes + 2 ) << 16 ) |
145
+ ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ;
146
+ #else
147
+ ( ( uint32_t ) * bytes << 24 ) |
148
+ ( ( uint32_t ) * ( bytes + 1 ) << 16 ) |
149
+ ( ( uint32_t ) * ( bytes + 2 ) << 8 ) |
150
+ ( ( uint32_t ) * ( bytes + 3 ) ) ;
151
+ #endif
152
+ }
153
+
154
+ /* Converts 2 bytes to uint16_t */
155
+ inline__ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes)
156
+ {
157
+ *dest =
158
+ #ifdef WORDS_BIGENDIAN
159
+ ( ( uint16_t ) * bytes ) |
160
+ ( ( uint16_t ) * ( bytes + 1 ) << 8 );
161
+ #else
162
+ ( ( uint16_t ) * bytes << 8 ) |
163
+ ( ( uint16_t ) * ( bytes + 1 ) );
164
+ #endif
165
+ }
166
+
167
+ /* Convert uint32_t to byte string of size 4 */
168
+ inline__ void U32_to_bytes(uint8_t *dest, uint32_t value)
169
+ {
170
+ #ifdef WORDS_BIGENDIAN
171
+ *(dest) = ( value );
172
+ *(dest + 1) = ( value >> 8 );
173
+ *(dest + 2) = ( value >> 16 );
174
+ *(dest + 3) = ( value >> 24 );
175
+ #else
176
+ *(dest) = ( value >> 24 );
177
+ *(dest + 1) = ( value >> 16 );
178
+ *(dest + 2) = ( value >> 8 );
179
+ *(dest + 3) = ( value );
180
+ #endif
181
+ }
182
+
183
+ /* Convert uint16_t to byte string of size 2 */
184
+ inline__ void U16_to_bytes(uint8_t *dest, uint16_t value)
185
+ {
186
+ #ifdef WORDS_BIGENDIAN
187
+ *(dest) = ( value );
188
+ *(dest + 1) = ( value >> 8 );
189
+ #else
190
+ *(dest) = ( value >> 8 );
191
+ *(dest + 1) = ( value );
192
+ #endif
193
+ }
@@ -0,0 +1,63 @@
1
+ /*
2
+ * util.h -- Utilities.
3
+ *
4
+ * This file is donated to the Tox Project.
5
+ * Copyright 2013 plutooo
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
+ #ifndef __UTIL_H__
26
+ #define __UTIL_H__
27
+
28
+ #include <stdbool.h>
29
+ #include <stdint.h>
30
+
31
+ #define inline__ inline __attribute__((always_inline))
32
+
33
+ void unix_time_update();
34
+ uint64_t unix_time();
35
+ int is_timeout(uint64_t timestamp, uint64_t timeout);
36
+
37
+
38
+ /* id functions */
39
+ bool id_equal(const uint8_t *dest, const uint8_t *src);
40
+ uint32_t id_copy(uint8_t *dest, const uint8_t *src); /* return value is CLIENT_ID_SIZE */
41
+
42
+ void host_to_net(uint8_t *num, uint16_t numbytes);
43
+ #define net_to_host(x, y) host_to_net(x, y)
44
+
45
+ /* state load/save */
46
+ typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type);
47
+ int load_state(load_state_callback_func load_state_callback, void *outer,
48
+ const uint8_t *data, uint32_t length, uint16_t cookie_inner);
49
+
50
+ /* Converts 4 bytes to uint32_t */
51
+ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes);
52
+
53
+ /* Converts 2 bytes to uint16_t */
54
+ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes);
55
+
56
+ /* Convert uint32_t to byte string of size 4 */
57
+ void U32_to_bytes(uint8_t *dest, uint32_t value);
58
+
59
+ /* Convert uint16_t to byte string of size 2 */
60
+ void U16_to_bytes(uint8_t *dest, uint16_t value);
61
+
62
+
63
+ #endif /* __UTIL_H__ */
@@ -0,0 +1,29 @@
1
+ lib_LTLIBRARIES += libtoxdns.la
2
+
3
+ libtoxdns_la_include_HEADERS = \
4
+ ../toxdns/toxdns.h
5
+
6
+ libtoxdns_la_includedir = $(includedir)/tox
7
+
8
+ libtoxdns_la_SOURCES = ../toxdns/toxdns.h \
9
+ ../toxdns/toxdns.c
10
+
11
+ libtoxdns_la_CFLAGS = -I$(top_srcdir) \
12
+ -I$(top_srcdir)/toxcore \
13
+ $(LIBSODIUM_CFLAGS) \
14
+ $(NACL_CFLAGS) \
15
+ $(PTHREAD_CFLAGS)
16
+
17
+ libtoxdns_la_LDFLAGS = $(TOXCORE_LT_LDFLAGS) \
18
+ $(EXTRA_LT_LDFLAGS) \
19
+ $(LIBSODIUM_LDFLAGS) \
20
+ $(NACL_LDFLAGS) \
21
+ $(MATH_LDFLAGS) \
22
+ $(RT_LIBS) \
23
+ $(WINSOCK2_LIBS)
24
+
25
+ libtoxdns_la_LIBADD = $(LIBSODIUM_LIBS) \
26
+ $(NACL_OBJECTS) \
27
+ $(NAC_LIBS) \
28
+ $(PTHREAD_LIBS) \
29
+ libtoxcore.la
@@ -0,0 +1,238 @@
1
+ /* toxdns.c
2
+ *
3
+ * Tox secure username DNS toxid resolving functions.
4
+ *
5
+ * Copyright (C) 2013 Tox project All Rights Reserved.
6
+ *
7
+ * This file is part of Tox.
8
+ *
9
+ * Tox is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * Tox is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+ #ifdef HAVE_CONFIG_H
25
+ #include "config.h"
26
+ #endif
27
+
28
+ #include "../toxcore/Messenger.h"
29
+ #include "../toxcore/logger.h"
30
+ #include "toxdns.h"
31
+
32
+ static const char base32[32] = {"abcdefghijklmnopqrstuvwxyz012345"};
33
+
34
+ #define _encode(a, b, c) \
35
+ { \
36
+ uint8_t i = 0; \
37
+ while(i != c) { \
38
+ *a++ = base32[((b[0] >> bits) | (b[1] << (8 - bits))) & 0x1F]; \
39
+ bits += 5; \
40
+ if(bits >= 8) { \
41
+ bits -= 8; \
42
+ b++; \
43
+ i++; \
44
+ } \
45
+ } \
46
+ } \
47
+
48
+ typedef struct {
49
+ uint8_t temp_pk[crypto_box_PUBLICKEYBYTES];
50
+ uint8_t temp_sk[crypto_box_SECRETKEYBYTES];
51
+ uint8_t server_public_key[crypto_box_PUBLICKEYBYTES];
52
+ uint8_t shared_key[crypto_box_KEYBYTES];
53
+ uint32_t nonce;
54
+ uint32_t nonce_start;
55
+ } DNS_Object;
56
+
57
+ static void dns_new_temp_keys(DNS_Object *d)
58
+ {
59
+ d->nonce = d->nonce_start = random_int();
60
+ crypto_box_keypair(d->temp_pk, d->temp_sk);
61
+ encrypt_precompute(d->server_public_key, d->temp_sk, d->shared_key);
62
+ }
63
+
64
+ /* Create a new tox_dns3 object for server with server_public_key.
65
+ *
66
+ * return Null on failure.
67
+ * return pointer object on success.
68
+ */
69
+ void *tox_dns3_new(uint8_t *server_public_key)
70
+ {
71
+ DNS_Object *d = malloc(sizeof(DNS_Object));
72
+
73
+ if (d == NULL)
74
+ return NULL;
75
+
76
+ memcpy(d->server_public_key, server_public_key, crypto_box_PUBLICKEYBYTES);
77
+ dns_new_temp_keys(d);
78
+ return d;
79
+ }
80
+
81
+ /* Destroy the tox dns3 object.
82
+ */
83
+ void tox_dns3_kill(void *dns3_object)
84
+ {
85
+ memset(dns3_object, 0, sizeof(DNS_Object));
86
+ free(dns3_object);
87
+ }
88
+
89
+ /* Generate a dns3 string of string_max_len used to query the dns server referred to by to
90
+ * dns3_object for a tox id registered to user with name of name_len.
91
+ *
92
+ * the uint32_t pointed by request_id will be set to the request id which must be passed to
93
+ * tox_decrypt_dns3_TXT() to correctly decode the response.
94
+ *
95
+ * This is what the string returned looks like:
96
+ * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc
97
+ *
98
+ * returns length of string on success.
99
+ * returns -1 on failure.
100
+ */
101
+ int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint32_t *request_id,
102
+ uint8_t *name, uint8_t name_len)
103
+ {
104
+ #define DOT_INTERVAL (6 * 5)
105
+ int base = (sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + name_len + crypto_box_MACBYTES);
106
+ int end_len = ((base * 8) / 5) + (base / DOT_INTERVAL) + !!(base % 5);
107
+ end_len -= !(base % DOT_INTERVAL);
108
+
109
+ if (end_len > string_max_len)
110
+ return -1;
111
+
112
+ DNS_Object *d = dns3_object;
113
+ uint8_t buffer[1024];
114
+ uint8_t nonce[crypto_box_NONCEBYTES] = {0};
115
+ memcpy(nonce, &d->nonce, sizeof(uint32_t));
116
+ memcpy(buffer, &d->nonce, sizeof(uint32_t));
117
+ memcpy(buffer + sizeof(uint32_t), d->temp_pk, crypto_box_PUBLICKEYBYTES);
118
+ int len = encrypt_data_symmetric(d->shared_key, nonce, name, name_len,
119
+ buffer + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES);
120
+
121
+ if (len == -1)
122
+ return -1;
123
+
124
+ int total_len = len + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES;
125
+ uint8_t *buff = buffer, *old_str = string;
126
+ buffer[total_len] = 0;
127
+ uint8_t bits = 0;
128
+ int i;
129
+
130
+ for (i = !(total_len % DOT_INTERVAL); i < (total_len / DOT_INTERVAL); ++i) {
131
+ _encode(string, buff, DOT_INTERVAL);
132
+ *string = '.';
133
+ ++string;
134
+ }
135
+
136
+ int left = total_len - (buff - buffer);
137
+ _encode(string, buff, left);
138
+ #undef DOT_INTERVAL
139
+ *request_id = d->nonce;
140
+ ++d->nonce;
141
+
142
+ if (d->nonce == d->nonce_start) {
143
+ dns_new_temp_keys(d);
144
+ }
145
+
146
+ if (end_len != string - old_str) {
147
+ LOGGER_ERROR("tox_generate_dns3_string Fail, %u != %lu\n", end_len, string - old_str);
148
+ return -1;
149
+ }
150
+
151
+ return string - old_str;
152
+ }
153
+
154
+
155
+ static int decode(uint8_t *dest, uint8_t *src)
156
+ {
157
+ uint8_t *p = src, *op = dest, bits = 0;
158
+ *op = 0;
159
+
160
+ while (*p) {
161
+ uint8_t ch = *p++;
162
+
163
+ switch (ch) {
164
+ case 'A' ... 'Z': {
165
+ ch = ch - 'A';
166
+ break;
167
+ }
168
+
169
+ case 'a' ... 'z': {
170
+ ch = ch - 'a';
171
+ break;
172
+ }
173
+
174
+ case '0' ... '5': {
175
+ ch = ch - '0' + 26;
176
+ break;
177
+ }
178
+
179
+ default: {
180
+ return - 1;
181
+ }
182
+ }
183
+
184
+ *op |= (ch << bits);
185
+ bits += 5;
186
+
187
+ if (bits >= 8) {
188
+ bits -= 8;
189
+ ++op;
190
+ *op = (ch >> (5 - bits));
191
+ }
192
+ }
193
+
194
+ return op - dest;
195
+ }
196
+
197
+ /* Decode and decrypt the id_record returned of length id_record_len into
198
+ * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE).
199
+ *
200
+ * request_id is the request id given by tox_generate_dns3_string() when creating the request.
201
+ *
202
+ * the id_record passed to this function should look somewhat like this:
203
+ * 2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp
204
+ *
205
+ * returns -1 on failure.
206
+ * returns 0 on success.
207
+ *
208
+ */
209
+ int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len,
210
+ uint32_t request_id)
211
+ {
212
+ DNS_Object *d = dns3_object;
213
+
214
+ if (id_record_len != 87)
215
+ return -1;
216
+
217
+ /*if (id_record_len > 255 || id_record_len <= (sizeof(uint32_t) + crypto_box_MACBYTES))
218
+ return -1;*/
219
+
220
+ uint8_t id_record_null[id_record_len + 1];
221
+ memcpy(id_record_null, id_record, id_record_len);
222
+ id_record_null[id_record_len] = 0;
223
+ uint8_t data[id_record_len];
224
+ int length = decode(data, id_record_null);
225
+
226
+ if (length == -1)
227
+ return -1;
228
+
229
+ uint8_t nonce[crypto_box_NONCEBYTES] = {0};
230
+ memcpy(nonce, &request_id, sizeof(uint32_t));
231
+ nonce[sizeof(uint32_t)] = 1;
232
+ int len = decrypt_data_symmetric(d->shared_key, nonce, data, length, tox_id);
233
+
234
+ if (len != FRIEND_ADDRESS_SIZE)
235
+ return -1;
236
+
237
+ return 0;
238
+ }