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,47 @@
1
+ /*
2
+ * ping.h -- Buffered pinging using cyclic arrays.
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
+ #ifndef __PING_H__
25
+ #define __PING_H__
26
+
27
+ typedef struct PING PING;
28
+
29
+ /* Add nodes to the to_ping list.
30
+ * All nodes in this list are pinged every TIME_TOPING seconds
31
+ * and are then removed from the list.
32
+ * If the list is full the nodes farthest from our client_id are replaced.
33
+ * The purpose of this list is to enable quick integration of new nodes into the
34
+ * network while preventing amplification attacks.
35
+ *
36
+ * return 0 if node was added.
37
+ * return -1 if node was not added.
38
+ */
39
+ int add_to_ping(PING *ping, const uint8_t *client_id, IP_Port ip_port);
40
+ void do_to_ping(PING *ping);
41
+
42
+ PING *new_ping(DHT *dht);
43
+ void kill_ping(PING *ping);
44
+
45
+ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *client_id);
46
+
47
+ #endif /* __PING_H__ */
@@ -0,0 +1,162 @@
1
+ /* ping_array.c
2
+ *
3
+ * Implementation of an efficient array to store that we pinged something.
4
+ *
5
+ *
6
+ * Copyright (C) 2014 Tox project All Rights Reserved.
7
+ *
8
+ * This file is part of Tox.
9
+ *
10
+ * Tox is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * Tox is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
22
+ *
23
+ */
24
+
25
+ #ifdef HAVE_CONFIG_H
26
+ #include "config.h"
27
+ #endif
28
+
29
+ #include "ping_array.h"
30
+ #include "crypto_core.h"
31
+ #include "util.h"
32
+
33
+ static void clear_entry(Ping_Array *array, uint32_t index)
34
+ {
35
+ free(array->entries[index].data);
36
+ array->entries[index].data = NULL;
37
+ array->entries[index].length =
38
+ array->entries[index].time =
39
+ array->entries[index].ping_id = 0;
40
+ }
41
+
42
+ /* Clear timed out entries.
43
+ */
44
+ static void ping_array_clear_timedout(Ping_Array *array)
45
+ {
46
+ while (array->last_deleted != array->last_added) {
47
+ uint32_t index = array->last_deleted % array->total_size;
48
+
49
+ if (!is_timeout(array->entries[index].time, array->timeout))
50
+ break;
51
+
52
+ clear_entry(array, index);
53
+ ++array->last_deleted;
54
+ }
55
+ }
56
+
57
+ /* Add a data with length to the Ping_Array list and return a ping_id.
58
+ *
59
+ * return ping_id on success.
60
+ * return 0 on failure.
61
+ */
62
+ uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length)
63
+ {
64
+ ping_array_clear_timedout(array);
65
+ uint32_t index = array->last_added % array->total_size;
66
+
67
+ if (array->entries[index].data != NULL) {
68
+ array->last_deleted = array->last_added - array->total_size;
69
+ clear_entry(array, index);
70
+ }
71
+
72
+ array->entries[index].data = malloc(length);
73
+
74
+ if (array->entries[index].data == NULL)
75
+ return 0;
76
+
77
+ memcpy(array->entries[index].data, data, length);
78
+ array->entries[index].length = length;
79
+ array->entries[index].time = unix_time();
80
+ ++array->last_added;
81
+ uint64_t ping_id = random_64b();
82
+ ping_id /= array->total_size;
83
+ ping_id *= array->total_size;
84
+ ping_id += index;
85
+
86
+ if (ping_id == 0)
87
+ ping_id += array->total_size;
88
+
89
+ array->entries[index].ping_id = ping_id;
90
+ return ping_id;
91
+ }
92
+
93
+
94
+ /* Check if ping_id is valid and not timed out.
95
+ *
96
+ * On success, copies the data into data of length,
97
+ *
98
+ * return length of data copied on success.
99
+ * return -1 on failure.
100
+ */
101
+ int ping_array_check(uint8_t *data, uint32_t length, Ping_Array *array, uint64_t ping_id)
102
+ {
103
+ if (ping_id == 0)
104
+ return -1;
105
+
106
+ uint32_t index = ping_id % array->total_size;
107
+
108
+ if (array->entries[index].ping_id != ping_id)
109
+ return -1;
110
+
111
+ if (is_timeout(array->entries[index].time, array->timeout))
112
+ return -1;
113
+
114
+ if (array->entries[index].length > length)
115
+ return -1;
116
+
117
+ if (array->entries[index].data == NULL)
118
+ return -1;
119
+
120
+ memcpy(data, array->entries[index].data, array->entries[index].length);
121
+ uint32_t len = array->entries[index].length;
122
+ clear_entry(array, index);
123
+ return len;
124
+ }
125
+
126
+ /* Initialize a Ping_Array.
127
+ * size represents the total size of the array and should be a power of 2.
128
+ * timeout represents the maximum timeout in seconds for the entry.
129
+ *
130
+ * return 0 on success.
131
+ * return -1 on failure.
132
+ */
133
+ int ping_array_init(Ping_Array *empty_array, uint32_t size, uint32_t timeout)
134
+ {
135
+ if (size == 0 || timeout == 0 || empty_array == NULL)
136
+ return -1;
137
+
138
+ empty_array->entries = calloc(size, sizeof(Ping_Array_Entry));
139
+
140
+ if (empty_array->entries == NULL)
141
+ return -1;
142
+
143
+ empty_array->last_deleted = empty_array->last_added = 0;
144
+ empty_array->total_size = size;
145
+ empty_array->timeout = timeout;
146
+ return 0;
147
+ }
148
+
149
+ /* Free all the allocated memory in a Ping_Array.
150
+ */
151
+ void ping_array_free_all(Ping_Array *array)
152
+ {
153
+ while (array->last_deleted != array->last_added) {
154
+ uint32_t index = array->last_deleted % array->total_size;
155
+ clear_entry(array, index);
156
+ ++array->last_deleted;
157
+ }
158
+
159
+ free(array->entries);
160
+ array->entries = NULL;
161
+ }
162
+
@@ -0,0 +1,75 @@
1
+ /* ping_array.h
2
+ *
3
+ * Implementation of an efficient array to store that we pinged something.
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
+ #ifndef PING_ARRAY_H
24
+ #define PING_ARRAY_H
25
+
26
+ #include "network.h"
27
+
28
+ typedef struct {
29
+ void *data;
30
+ uint32_t length;
31
+ uint64_t time;
32
+ uint64_t ping_id;
33
+ } Ping_Array_Entry;
34
+
35
+
36
+ typedef struct {
37
+ Ping_Array_Entry *entries;
38
+
39
+ uint32_t last_deleted; /* number representing the next entry to be deleted. */
40
+ uint32_t last_added; /* number representing the last entry to be added. */
41
+ uint32_t total_size; /* The length of entries */
42
+ uint32_t timeout; /* The timeout after which entries are cleared. */
43
+ } Ping_Array;
44
+
45
+
46
+ /* Add a data with length to the Ping_Array list and return a ping_id.
47
+ *
48
+ * return ping_id on success.
49
+ * return 0 on failure.
50
+ */
51
+ uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length);
52
+
53
+ /* Check if ping_id is valid and not timed out.
54
+ *
55
+ * On success, copies the data into data of length,
56
+ *
57
+ * return length of data copied on success.
58
+ * return -1 on failure.
59
+ */
60
+ int ping_array_check(uint8_t *data, uint32_t length, Ping_Array *array, uint64_t ping_id);
61
+
62
+ /* Initialize a Ping_Array.
63
+ * size represents the total size of the array and should be a power of 2.
64
+ * timeout represents the maximum timeout in seconds for the entry.
65
+ *
66
+ * return 0 on success.
67
+ * return -1 on failure.
68
+ */
69
+ int ping_array_init(Ping_Array *empty_array, uint32_t size, uint32_t timeout);
70
+
71
+ /* Free all the allocated memory in a Ping_Array.
72
+ */
73
+ void ping_array_free_all(Ping_Array *array);
74
+
75
+ #endif
@@ -0,0 +1,940 @@
1
+ /* tox.c
2
+ *
3
+ * The Tox public API.
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 "Messenger.h"
29
+ #include "logger.h"
30
+
31
+ #define __TOX_DEFINED__
32
+ typedef struct Messenger Tox;
33
+
34
+ #include "tox.h"
35
+
36
+ /*
37
+ * returns a FRIEND_ADDRESS_SIZE byte address to give to others.
38
+ * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
39
+ *
40
+ */
41
+ void tox_get_address(const Tox *tox, uint8_t *address)
42
+ {
43
+ const Messenger *m = tox;
44
+ getaddress(m, address);
45
+ }
46
+
47
+ /*
48
+ * Add a friend.
49
+ * Set the data that will be sent along with friend request.
50
+ * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
51
+ * data is the data and length is the length.
52
+ *
53
+ * return the friend number if success.
54
+ * return FA_TOOLONG if message length is too long.
55
+ * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
56
+ * return FAERR_OWNKEY if user's own key.
57
+ * return FAERR_ALREADYSENT if friend request already sent or already a friend.
58
+ * return FAERR_UNKNOWN for unknown error.
59
+ * return FAERR_BADCHECKSUM if bad checksum in address.
60
+ * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
61
+ * (the nospam for that friend was set to the new one).
62
+ * return FAERR_NOMEM if increasing the friend list size fails.
63
+ */
64
+ int32_t tox_add_friend(Tox *tox, const uint8_t *address, const uint8_t *data, uint16_t length)
65
+ {
66
+ Messenger *m = tox;
67
+ return m_addfriend(m, address, data, length);
68
+ }
69
+
70
+ /* Add a friend without sending a friendrequest.
71
+ *
72
+ * return the friend number if success.
73
+ * return -1 if failure.
74
+ */
75
+ int32_t tox_add_friend_norequest(Tox *tox, const uint8_t *client_id)
76
+ {
77
+ Messenger *m = tox;
78
+ return m_addfriend_norequest(m, client_id);
79
+ }
80
+
81
+ /* return the friend number associated to that client id.
82
+ * return -1 if no such friend.
83
+ */
84
+ int32_t tox_get_friend_number(const Tox *tox, const uint8_t *client_id)
85
+ {
86
+ const Messenger *m = tox;
87
+ return getfriend_id(m, client_id);
88
+ }
89
+
90
+ /* Copies the public key associated to that friend id into client_id buffer.
91
+ * Make sure that client_id is of size CLIENT_ID_SIZE.
92
+ *
93
+ * return 0 if success.
94
+ * return -1 if failure.
95
+ */
96
+ int tox_get_client_id(const Tox *tox, int32_t friendnumber, uint8_t *client_id)
97
+ {
98
+ const Messenger *m = tox;
99
+ return getclient_id(m, friendnumber, client_id);
100
+ }
101
+
102
+ /* Remove a friend. */
103
+ int tox_del_friend(Tox *tox, int32_t friendnumber)
104
+ {
105
+ Messenger *m = tox;
106
+ return m_delfriend(m, friendnumber);
107
+ }
108
+
109
+ /* Checks friend's connecting status.
110
+ *
111
+ * return 1 if friend is connected to us (Online).
112
+ * return 0 if friend is not connected to us (Offline).
113
+ * return -1 on failure.
114
+ */
115
+ int tox_get_friend_connection_status(const Tox *tox, int32_t friendnumber)
116
+ {
117
+ const Messenger *m = tox;
118
+ return m_get_friend_connectionstatus(m, friendnumber);
119
+ }
120
+
121
+ /* Checks if there exists a friend with given friendnumber.
122
+ *
123
+ * return 1 if friend exists.
124
+ * return 0 if friend doesn't exist.
125
+ */
126
+ int tox_friend_exists(const Tox *tox, int32_t friendnumber)
127
+ {
128
+ const Messenger *m = tox;
129
+ return m_friend_exists(m, friendnumber);
130
+ }
131
+
132
+ /* Send a text chat message to an online friend.
133
+ * return the message id if packet was successfully put into the send queue.
134
+ * return 0 if it was not.
135
+ *
136
+ * You will want to retain the return value, it will be passed to your read_receipt callback
137
+ * if one is received.
138
+ */
139
+ uint32_t tox_send_message(Tox *tox, int32_t friendnumber, const uint8_t *message, uint32_t length)
140
+ {
141
+ Messenger *m = tox;
142
+ return m_sendmessage(m, friendnumber, message, length);
143
+ }
144
+
145
+ /* Send an action to an online friend.
146
+ *
147
+ * return the message id if packet was successfully put into the send queue.
148
+ * return 0 if it was not.
149
+ *
150
+ * You will want to retain the return value, it will be passed to your read_receipt callback
151
+ * if one is received.
152
+ */
153
+ uint32_t tox_send_action(Tox *tox, int32_t friendnumber, const uint8_t *action, uint32_t length)
154
+ {
155
+ Messenger *m = tox;
156
+ return m_sendaction(m, friendnumber, action, length);
157
+ }
158
+
159
+ /* Set our nickname.
160
+ * name must be a string of maximum MAX_NAME_LENGTH length.
161
+ * length must be at least 1 byte.
162
+ * length is the length of name with the NULL terminator.
163
+ *
164
+ * return 0 if success.
165
+ * return -1 if failure.
166
+ */
167
+ int tox_set_name(Tox *tox, const uint8_t *name, uint16_t length)
168
+ {
169
+ Messenger *m = tox;
170
+ return setname(m, name, length);
171
+ }
172
+
173
+ /* Get your nickname.
174
+ * m - The messenger context to use.
175
+ * name - Pointer to a string for the name. (must be at least MAX_NAME_LENGTH)
176
+ *
177
+ * return length of the name.
178
+ * return 0 on error.
179
+ */
180
+ uint16_t tox_get_self_name(const Tox *tox, uint8_t *name)
181
+ {
182
+ const Messenger *m = tox;
183
+ return getself_name(m, name);
184
+ }
185
+
186
+ /* Get name of friendnumber and put it in name.
187
+ * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
188
+ *
189
+ * return length of name (with the NULL terminator) if success.
190
+ * return -1 if failure.
191
+ */
192
+ int tox_get_name(const Tox *tox, int32_t friendnumber, uint8_t *name)
193
+ {
194
+ const Messenger *m = tox;
195
+ return getname(m, friendnumber, name);
196
+ }
197
+
198
+ /* returns the length of name on success.
199
+ * returns -1 on failure.
200
+ */
201
+ int tox_get_name_size(const Tox *tox, int32_t friendnumber)
202
+ {
203
+ const Messenger *m = tox;
204
+ return m_get_name_size(m, friendnumber);
205
+ }
206
+
207
+ int tox_get_self_name_size(const Tox *tox)
208
+ {
209
+ const Messenger *m = tox;
210
+ return m_get_self_name_size(m);
211
+ }
212
+
213
+ /* Set our user status;
214
+ * you are responsible for freeing status after.
215
+ *
216
+ * return 0 on success, -1 on failure.
217
+ */
218
+ int tox_set_status_message(Tox *tox, const uint8_t *status, uint16_t length)
219
+ {
220
+ Messenger *m = tox;
221
+ return m_set_statusmessage(m, status, length);
222
+ }
223
+
224
+ int tox_set_user_status(Tox *tox, uint8_t status)
225
+ {
226
+ Messenger *m = tox;
227
+ return m_set_userstatus(m, status);
228
+ }
229
+
230
+ /* returns the length of status message on success.
231
+ * returns -1 on failure.
232
+ */
233
+ int tox_get_status_message_size(const Tox *tox, int32_t friendnumber)
234
+ {
235
+ const Messenger *m = tox;
236
+ return m_get_statusmessage_size(m, friendnumber);
237
+ }
238
+
239
+ int tox_get_self_status_message_size(const Tox *tox)
240
+ {
241
+ const Messenger *m = tox;
242
+ return m_get_self_statusmessage_size(m);
243
+ }
244
+
245
+ /* Copy friendnumber's status message into buf, truncating if size is over maxlen.
246
+ * Get the size you need to allocate from m_get_statusmessage_size.
247
+ * The self variant will copy our own status message.
248
+ */
249
+ int tox_get_status_message(const Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
250
+ {
251
+ const Messenger *m = tox;
252
+ return m_copy_statusmessage(m, friendnumber, buf, maxlen);
253
+ }
254
+
255
+ int tox_get_self_status_message(const Tox *tox, uint8_t *buf, uint32_t maxlen)
256
+ {
257
+ const Messenger *m = tox;
258
+ return m_copy_self_statusmessage(m, buf, maxlen);
259
+ }
260
+
261
+ /* Return one of USERSTATUS values.
262
+ * Values unknown to your application should be represented as USERSTATUS_NONE.
263
+ * As above, the self variant will return our own USERSTATUS.
264
+ * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
265
+ */
266
+ uint8_t tox_get_user_status(const Tox *tox, int32_t friendnumber)
267
+ {
268
+ const Messenger *m = tox;
269
+ return m_get_userstatus(m, friendnumber);
270
+ }
271
+
272
+ uint8_t tox_get_self_user_status(const Tox *tox)
273
+ {
274
+ const Messenger *m = tox;
275
+ return m_get_self_userstatus(m);
276
+ }
277
+
278
+ /* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
279
+ * returns -1 on error.
280
+ */
281
+ uint64_t tox_get_last_online(const Tox *tox, int32_t friendnumber)
282
+ {
283
+ const Messenger *m = tox;
284
+ return m_get_last_online(m, friendnumber);
285
+ }
286
+
287
+ /* Set our typing status for a friend.
288
+ * You are responsible for turning it on or off.
289
+ *
290
+ * returns 0 on success.
291
+ * returns -1 on failure.
292
+ */
293
+ int tox_set_user_is_typing(Tox *tox, int32_t friendnumber, uint8_t is_typing)
294
+ {
295
+ Messenger *m = tox;
296
+ return m_set_usertyping(m, friendnumber, is_typing);
297
+ }
298
+
299
+ /* Get the typing status of a friend.
300
+ *
301
+ * returns 0 if friend is not typing.
302
+ * returns 1 if friend is typing.
303
+ */
304
+ uint8_t tox_get_is_typing(const Tox *tox, int32_t friendnumber)
305
+ {
306
+ const Messenger *m = tox;
307
+ return m_get_istyping(m, friendnumber);
308
+ }
309
+
310
+ /* Return the number of friends in the instance m.
311
+ * You should use this to determine how much memory to allocate
312
+ * for copy_friendlist. */
313
+ uint32_t tox_count_friendlist(const Tox *tox)
314
+ {
315
+ const Messenger *m = tox;
316
+ return count_friendlist(m);
317
+ }
318
+
319
+ /* Return the number of online friends in the instance m. */
320
+ uint32_t tox_get_num_online_friends(const Tox *tox)
321
+ {
322
+ const Messenger *m = tox;
323
+ return get_num_online_friends(m);
324
+ }
325
+
326
+ /* Copy a list of valid friend IDs into the array out_list.
327
+ * If out_list is NULL, returns 0.
328
+ * Otherwise, returns the number of elements copied.
329
+ * If the array was too small, the contents
330
+ * of out_list will be truncated to list_size. */
331
+ uint32_t tox_get_friendlist(const Tox *tox, int32_t *out_list, uint32_t list_size)
332
+ {
333
+ const Messenger *m = tox;
334
+ return copy_friendlist(m, out_list, list_size);
335
+ }
336
+
337
+ /* Set the function that will be executed when a friend request is received.
338
+ * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
339
+ */
340
+ void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, const uint8_t *, const uint8_t *, uint16_t,
341
+ void *), void *userdata)
342
+ {
343
+ Messenger *m = tox;
344
+ m_callback_friendrequest(m, function, userdata);
345
+ }
346
+
347
+
348
+ /* Set the function that will be executed when a message from a friend is received.
349
+ * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length)
350
+ */
351
+ void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
352
+ void *userdata)
353
+ {
354
+ Messenger *m = tox;
355
+ m_callback_friendmessage(m, function, userdata);
356
+ }
357
+
358
+ /* Set the function that will be executed when an action from a friend is received.
359
+ * function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length)
360
+ */
361
+ void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
362
+ void *userdata)
363
+ {
364
+ Messenger *m = tox;
365
+ m_callback_action(m, function, userdata);
366
+ }
367
+
368
+ /* Set the callback for name changes.
369
+ * function(int32_t friendnumber, uint8_t *newname, uint16_t length)
370
+ * You are not responsible for freeing newname.
371
+ */
372
+ void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
373
+ void *userdata)
374
+ {
375
+ Messenger *m = tox;
376
+ m_callback_namechange(m, function, userdata);
377
+ }
378
+
379
+ /* Set the callback for status message changes.
380
+ * function(int32_t friendnumber, uint8_t *newstatus, uint16_t length)
381
+ * You are not responsible for freeing newstatus.
382
+ */
383
+ void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
384
+ void *userdata)
385
+ {
386
+ Messenger *m = tox;
387
+ m_callback_statusmessage(m, function, userdata);
388
+ }
389
+
390
+ /* Set the callback for status type changes.
391
+ * function(int32_t friendnumber, USERSTATUS kind)
392
+ */
393
+ void tox_callback_user_status(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, void *),
394
+ void *userdata)
395
+ {
396
+ Messenger *m = tox;
397
+ m_callback_userstatus(m, function, userdata);
398
+ }
399
+
400
+ /* Set the callback for typing changes.
401
+ * function (int32_t friendnumber, uint8_t is_typing)
402
+ */
403
+ void tox_callback_typing_change(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, void *), void *userdata)
404
+ {
405
+ Messenger *m = tox;
406
+ m_callback_typingchange(m, function, userdata);
407
+ }
408
+
409
+ /* Set the callback for read receipts.
410
+ * function(int32_t friendnumber, uint32_t receipt)
411
+ *
412
+ * If you are keeping a record of returns from m_sendmessage;
413
+ * receipt might be one of those values, meaning the message
414
+ * has been received on the other side.
415
+ * Since core doesn't track ids for you, receipt may not correspond to any message.
416
+ * in that case, you should discard it.
417
+ */
418
+ void tox_callback_read_receipt(Tox *tox, void (*function)(Messenger *tox, int32_t, uint32_t, void *), void *userdata)
419
+ {
420
+ Messenger *m = tox;
421
+ m_callback_read_receipt(m, function, userdata);
422
+ }
423
+
424
+ /* Set the callback for connection status changes.
425
+ * function(int32_t friendnumber, uint8_t status)
426
+ *
427
+ * Status:
428
+ * 0 -- friend went offline after being previously online
429
+ * 1 -- friend went online
430
+ *
431
+ * NOTE: this callback is not called when adding friends, thus the "after
432
+ * being previously online" part. It's assumed that when adding friends,
433
+ * their connection status is offline.
434
+ */
435
+ void tox_callback_connection_status(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, void *),
436
+ void *userdata)
437
+ {
438
+ Messenger *m = tox;
439
+ m_callback_connectionstatus(m, function, userdata);
440
+ }
441
+
442
+ /**********ADVANCED FUNCTIONS (If you don't know what they do you can safely ignore them.) ************/
443
+
444
+ /* Functions to get/set the nospam part of the id.
445
+ */
446
+ uint32_t tox_get_nospam(const Tox *tox)
447
+ {
448
+ const Messenger *m = tox;
449
+ return get_nospam(&(m->fr));
450
+ }
451
+
452
+ void tox_set_nospam(Tox *tox, uint32_t nospam)
453
+ {
454
+ Messenger *m = tox;
455
+ set_nospam(&(m->fr), nospam);
456
+ }
457
+
458
+ /* Copy the public and secret key from the Tox object.
459
+ public_key and secret_key must be 32 bytes big.
460
+ if the pointer is NULL, no data will be copied to it.*/
461
+ void tox_get_keys(Tox *tox, uint8_t *public_key, uint8_t *secret_key)
462
+ {
463
+ Messenger *m = tox;
464
+
465
+ if (public_key)
466
+ memcpy(public_key, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES);
467
+
468
+ if (secret_key)
469
+ memcpy(secret_key, m->net_crypto->self_secret_key, crypto_box_SECRETKEYBYTES);
470
+ }
471
+
472
+ /* Set handlers for custom lossy packets.
473
+ * Set the function to be called when friend sends us a lossy packet starting with byte.
474
+ * byte must be in the 200-254 range.
475
+ *
476
+ * NOTE: lossy packets behave like UDP packets meaning they might never reach the other side
477
+ * or might arrive more than once (if someone is messing with the connection) or might arrive
478
+ * in the wrong order.
479
+ *
480
+ * Unless latency is an issue, it is recommended that you use lossless packets instead.
481
+ *
482
+ * return -1 on failure.
483
+ * return 0 on success.
484
+ */
485
+ int tox_lossy_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
486
+ int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object)
487
+ {
488
+ Messenger *m = tox;
489
+
490
+ if (byte < (PACKET_ID_LOSSY_RANGE_START + 8)) /* First 8 reserved for A/V*/
491
+ return -1;
492
+
493
+ return custom_lossy_packet_registerhandler(m, friendnumber, byte, packet_handler_callback, object);
494
+ }
495
+
496
+ /* Function to send custom lossy packets.
497
+ * First byte of data must be in the range: 200-254.
498
+ *
499
+ * return -1 on failure.
500
+ * return 0 on success.
501
+ */
502
+ int tox_send_lossy_packet(const Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length)
503
+ {
504
+ const Messenger *m = tox;
505
+
506
+ if (length == 0)
507
+ return -1;
508
+
509
+ if (data[0] < (PACKET_ID_LOSSY_RANGE_START + 8)) /* First 8 reserved for A/V*/
510
+ return -1;
511
+
512
+ return send_custom_lossy_packet(m, friendnumber, data, length);
513
+ }
514
+
515
+ /* Set handlers for custom lossless packets.
516
+ * Set the function to be called when friend sends us a lossless packet starting with byte.
517
+ * byte must be in the 160-191 range.
518
+ *
519
+ * return -1 on failure.
520
+ * return 0 on success.
521
+ */
522
+ int tox_lossless_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
523
+ int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object)
524
+ {
525
+ Messenger *m = tox;
526
+
527
+ return custom_lossless_packet_registerhandler(m, friendnumber, byte, packet_handler_callback, object);
528
+ }
529
+
530
+ /* Function to send custom lossless packets.
531
+ * First byte of data must be in the range: 160-191.
532
+ *
533
+ * return -1 on failure.
534
+ * return 0 on success.
535
+ */
536
+ int tox_send_lossless_packet(const Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length)
537
+ {
538
+ const Messenger *m = tox;
539
+
540
+ return send_custom_lossless_packet(m, friendnumber, data, length);
541
+ }
542
+
543
+ /**********GROUP CHAT FUNCTIONS: WARNING Group chats will be rewritten so this might change ************/
544
+
545
+ /* Set the callback for group invites.
546
+ *
547
+ * Function(Tox *tox, int32_t friendnumber, uint8_t *group_public_key, void *userdata)
548
+ */
549
+ void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, void *),
550
+ void *userdata)
551
+ {
552
+ Messenger *m = tox;
553
+ m_callback_group_invite(m, function, userdata);
554
+ }
555
+
556
+ /* Set the callback for group messages.
557
+ *
558
+ * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
559
+ */
560
+ void tox_callback_group_message(Tox *tox, void (*function)(Messenger *tox, int, int, const uint8_t *, uint16_t, void *),
561
+ void *userdata)
562
+ {
563
+ Messenger *m = tox;
564
+ m_callback_group_message(m, function, userdata);
565
+ }
566
+
567
+ /* Set the callback for group actions.
568
+ *
569
+ * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * action, uint16_t length, void *userdata)
570
+ */
571
+ void tox_callback_group_action(Tox *tox, void (*function)(Messenger *tox, int, int, const uint8_t *, uint16_t, void *),
572
+ void *userdata)
573
+ {
574
+ Messenger *m = tox;
575
+ m_callback_group_action(m, function, userdata);
576
+ }
577
+
578
+ /* Set callback function for peer name list changes.
579
+ *
580
+ * It gets called every time the name list changes(new peer/name, deleted peer)
581
+ * Function(Tox *tox, int groupnumber, void *userdata)
582
+ */
583
+ void tox_callback_group_namelist_change(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t, void *), void *userdata)
584
+ {
585
+ Messenger *m = tox;
586
+ m_callback_group_namelistchange(m, function, userdata);
587
+ }
588
+
589
+ /* Creates a new groupchat and puts it in the chats array.
590
+ *
591
+ * return group number on success.
592
+ * return -1 on failure.
593
+ */
594
+ int tox_add_groupchat(Tox *tox)
595
+ {
596
+ Messenger *m = tox;
597
+ return add_groupchat(m);
598
+ }
599
+ /* Delete a groupchat from the chats array.
600
+ *
601
+ * return 0 on success.
602
+ * return -1 if failure.
603
+ */
604
+ int tox_del_groupchat(Tox *tox, int groupnumber)
605
+ {
606
+ Messenger *m = tox;
607
+ return del_groupchat(m, groupnumber);
608
+ }
609
+
610
+ /* Copy the name of peernumber who is in groupnumber to name.
611
+ * name must be at least MAX_NICK_BYTES long.
612
+ *
613
+ * return length of name if success
614
+ * return -1 if failure
615
+ */
616
+ int tox_group_peername(const Tox *tox, int groupnumber, int peernumber, uint8_t *name)
617
+ {
618
+ const Messenger *m = tox;
619
+ return m_group_peername(m, groupnumber, peernumber, name);
620
+ }
621
+ /* invite friendnumber to groupnumber
622
+ * return 0 on success
623
+ * return -1 on failure
624
+ */
625
+ int tox_invite_friend(Tox *tox, int32_t friendnumber, int groupnumber)
626
+ {
627
+ Messenger *m = tox;
628
+ return invite_friend(m, friendnumber, groupnumber);
629
+ }
630
+ /* Join a group (you need to have been invited first.)
631
+ *
632
+ * returns group number on success
633
+ * returns -1 on failure.
634
+ */
635
+ int tox_join_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *friend_group_public_key)
636
+ {
637
+ Messenger *m = tox;
638
+ return join_groupchat(m, friendnumber, friend_group_public_key);
639
+ }
640
+
641
+ /* send a group message
642
+ * return 0 on success
643
+ * return -1 on failure
644
+ */
645
+ int tox_group_message_send(Tox *tox, int groupnumber, const uint8_t *message, uint32_t length)
646
+ {
647
+ Messenger *m = tox;
648
+ return group_message_send(m, groupnumber, message, length);
649
+ }
650
+
651
+ /* send a group action
652
+ * return 0 on success
653
+ * return -1 on failure
654
+ */
655
+ int tox_group_action_send(Tox *tox, int groupnumber, const uint8_t *action, uint32_t length)
656
+ {
657
+ Messenger *m = tox;
658
+ return group_action_send(m, groupnumber, action, length);
659
+ }
660
+
661
+ /* Return the number of peers in the group chat on success.
662
+ * return -1 on failure
663
+ */
664
+ int tox_group_number_peers(const Tox *tox, int groupnumber)
665
+ {
666
+ const Messenger *m = tox;
667
+ return group_number_peers(m, groupnumber);
668
+ }
669
+
670
+ /* List all the peers in the group chat.
671
+ *
672
+ * Copies the names of the peers to the name[length][MAX_NICK_BYTES] array.
673
+ *
674
+ * Copies the lengths of the names to lengths[length]
675
+ *
676
+ * returns the number of peers on success.
677
+ *
678
+ * return -1 on failure.
679
+ */
680
+ int tox_group_get_names(const Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t lengths[],
681
+ uint16_t length)
682
+ {
683
+ const Messenger *m = tox;
684
+ return group_names(m, groupnumber, names, lengths, length);
685
+ }
686
+
687
+ /* Return the number of chats in the instance m.
688
+ * You should use this to determine how much memory to allocate
689
+ * for copy_chatlist. */
690
+ uint32_t tox_count_chatlist(const Tox *tox)
691
+ {
692
+ const Messenger *m = tox;
693
+ return count_chatlist(m);
694
+ }
695
+
696
+ /* Copy a list of valid chat IDs into the array out_list.
697
+ * If out_list is NULL, returns 0.
698
+ * Otherwise, returns the number of elements copied.
699
+ * If the array was too small, the contents
700
+ * of out_list will be truncated to list_size. */
701
+ uint32_t tox_get_chatlist(const Tox *tox, int *out_list, uint32_t list_size)
702
+ {
703
+ const Messenger *m = tox;
704
+ return copy_chatlist(m, out_list, list_size);
705
+ }
706
+
707
+
708
+ /****************FILE SENDING FUNCTIONS*****************/
709
+
710
+
711
+ /* Set the callback for file send requests.
712
+ *
713
+ * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
714
+ */
715
+ void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint64_t,
716
+ const uint8_t *, uint16_t, void *), void *userdata)
717
+ {
718
+ Messenger *m = tox;
719
+ callback_file_sendrequest(m, function, userdata);
720
+ }
721
+ /* Set the callback for file control requests.
722
+ *
723
+ * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
724
+ *
725
+ */
726
+ void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint8_t, uint8_t,
727
+ const uint8_t *, uint16_t, void *), void *userdata)
728
+ {
729
+ Messenger *m = tox;
730
+ callback_file_control(m, function, userdata);
731
+ }
732
+ /* Set the callback for file data.
733
+ *
734
+ * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
735
+ *
736
+ */
737
+ void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, const uint8_t *,
738
+ uint16_t length, void *), void *userdata)
739
+
740
+ {
741
+ Messenger *m = tox;
742
+ callback_file_data(m, function, userdata);
743
+ }
744
+ /* Send a file send request.
745
+ * Maximum filename length is 255 bytes.
746
+ * return file number on success
747
+ * return -1 on failure
748
+ */
749
+ int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, const uint8_t *filename,
750
+ uint16_t filename_length)
751
+ {
752
+ Messenger *m = tox;
753
+ return new_filesender(m, friendnumber, filesize, filename, filename_length);
754
+ }
755
+ /* Send a file control request.
756
+ * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file.
757
+ *
758
+ * return 0 on success
759
+ * return -1 on failure
760
+ */
761
+ int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
762
+ const uint8_t *data, uint16_t length)
763
+ {
764
+ Messenger *m = tox;
765
+ return file_control(m, friendnumber, send_receive, filenumber, message_id, data, length);
766
+ }
767
+ /* Send file data.
768
+ *
769
+ * return 0 on success
770
+ * return -1 on failure
771
+ */
772
+ int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length)
773
+ {
774
+ Messenger *m = tox;
775
+ return file_data(m, friendnumber, filenumber, data, length);
776
+ }
777
+
778
+ /* Returns the recommended/maximum size of the filedata you send with tox_file_send_data()
779
+ *
780
+ * return size on success
781
+ * return -1 on failure (currently will never return -1)
782
+ */
783
+ int tox_file_data_size(const Tox *tox, int32_t friendnumber)
784
+ {
785
+ return MAX_CRYPTO_DATA_SIZE - 2;
786
+ }
787
+
788
+ /* Give the number of bytes left to be sent/received.
789
+ *
790
+ * send_receive is 0 if we want the sending files, 1 if we want the receiving.
791
+ *
792
+ * return number of bytes remaining to be sent/received on success
793
+ * return 0 on failure
794
+ */
795
+ uint64_t tox_file_data_remaining(const Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive)
796
+ {
797
+ const Messenger *m = tox;
798
+ return file_dataremaining(m, friendnumber, filenumber, send_receive);
799
+ }
800
+
801
+ /***************END OF FILE SENDING FUNCTIONS******************/
802
+
803
+ /* Like tox_bootstrap_from_address but for TCP relays only.
804
+ *
805
+ * return 0 on failure.
806
+ * return 1 on success.
807
+ */
808
+ int tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8_t *public_key)
809
+ {
810
+ Messenger *m = tox;
811
+ IP_Port ip_port, ip_port_v4;
812
+ if (!addr_parse_ip(address, &ip_port.ip)) {
813
+ if (m->options.udp_disabled) /* Disable DNS when udp is disabled. */
814
+ return 0;
815
+
816
+ IP *ip_extra = NULL;
817
+ ip_init(&ip_port.ip, m->options.ipv6enabled);
818
+
819
+ if (m->options.ipv6enabled) {
820
+ /* setup for getting BOTH: an IPv6 AND an IPv4 address */
821
+ ip_port.ip.family = AF_UNSPEC;
822
+ ip_reset(&ip_port_v4.ip);
823
+ ip_extra = &ip_port_v4.ip;
824
+ }
825
+
826
+ if (!addr_resolve(address, &ip_port.ip, ip_extra))
827
+ return 0;
828
+ }
829
+
830
+ ip_port.port = htons(port);
831
+ add_tcp_relay(m->net_crypto, ip_port, public_key);
832
+ onion_add_path_node(m->onion_c, ip_port, public_key); //TODO: move this
833
+ return 1;
834
+ }
835
+
836
+ int tox_bootstrap_from_address(Tox *tox, const char *address, uint16_t port, const uint8_t *public_key)
837
+ {
838
+ Messenger *m = tox;
839
+ int ret = tox_add_tcp_relay(tox, address, port, public_key);
840
+ if (m->options.udp_disabled) {
841
+ return ret;
842
+ } else { /* DHT only works on UDP. */
843
+ return DHT_bootstrap_from_address(m->dht, address, m->options.ipv6enabled, htons(port), public_key);
844
+ }
845
+ }
846
+
847
+ /* return 0 if we are not connected to the DHT.
848
+ * return 1 if we are.
849
+ */
850
+ int tox_isconnected(const Tox *tox)
851
+ {
852
+ const Messenger *m = tox;
853
+ return onion_isconnected(m->onion_c);
854
+ }
855
+
856
+ /* Return the time in milliseconds before tox_do() should be called again
857
+ * for optimal performance.
858
+ *
859
+ * returns time (in ms) before the next tox_do() needs to be run on success.
860
+ */
861
+ uint32_t tox_do_interval(Tox *tox)
862
+ {
863
+ Messenger *m = tox;
864
+ return messenger_run_interval(m);
865
+ }
866
+
867
+ /* Run this at startup.
868
+ *
869
+ * return allocated instance of tox on success.
870
+ * return 0 if there are problems.
871
+ */
872
+ Tox *tox_new(Tox_Options *options)
873
+ {
874
+ LOGGER_INIT(LOGGER_OUTPUT_FILE, LOGGER_LEVEL);
875
+ Messenger_Options m_options = {0};
876
+
877
+ if (options == NULL) {
878
+ m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
879
+ } else {
880
+ m_options.ipv6enabled = options->ipv6enabled;
881
+ m_options.udp_disabled = options->udp_disabled;
882
+ m_options.proxy_enabled = options->proxy_enabled;
883
+
884
+ if (m_options.proxy_enabled) {
885
+ ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled);
886
+
887
+ if (m_options.ipv6enabled)
888
+ m_options.proxy_info.ip_port.ip.family = AF_UNSPEC;
889
+
890
+ if (!addr_resolve_or_parse_ip(options->proxy_address, &m_options.proxy_info.ip_port.ip, NULL))
891
+ return NULL;
892
+
893
+ m_options.proxy_info.ip_port.port = htons(options->proxy_port);
894
+ }
895
+ }
896
+
897
+ return new_messenger(&m_options);
898
+ }
899
+
900
+ /* Run this before closing shop.
901
+ * Free all datastructures.
902
+ */
903
+ void tox_kill(Tox *tox)
904
+ {
905
+ Messenger *m = tox;
906
+ kill_messenger(m);
907
+ }
908
+
909
+ /* The main loop that needs to be run at least 20 times per second. */
910
+ void tox_do(Tox *tox)
911
+ {
912
+ Messenger *m = tox;
913
+ do_messenger(m);
914
+ }
915
+
916
+ /* SAVING AND LOADING FUNCTIONS: */
917
+
918
+ /* return size of the messenger data (for saving). */
919
+ uint32_t tox_size(const Tox *tox)
920
+ {
921
+ const Messenger *m = tox;
922
+ return messenger_size(m);
923
+ }
924
+
925
+ /* Save the messenger in data (must be allocated memory of size Messenger_size()). */
926
+ void tox_save(const Tox *tox, uint8_t *data)
927
+ {
928
+ const Messenger *m = tox;
929
+ messenger_save(m, data);
930
+ }
931
+
932
+ /* Load the messenger from data of size length. */
933
+ int tox_load(Tox *tox, const uint8_t *data, uint32_t length)
934
+ {
935
+ if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0)
936
+ return 1;
937
+
938
+ Messenger *m = tox;
939
+ return messenger_load(m, data, length);
940
+ }