summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c2
-rw-r--r--toxcore/Messenger.h2
-rw-r--r--toxcore/tox.c115
-rw-r--r--toxcore/tox.h6
4 files changed, 112 insertions, 13 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 4cf0edeb..194a633e 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -2350,7 +2350,7 @@ static char *ID2String(const uint8_t *pk)
2350 * 2350 *
2351 * returns time (in ms) before the next do_messenger() needs to be run on success. 2351 * returns time (in ms) before the next do_messenger() needs to be run on success.
2352 */ 2352 */
2353uint32_t messenger_run_interval(Messenger *m) 2353uint32_t messenger_run_interval(const Messenger *m)
2354{ 2354{
2355 uint32_t crypto_interval = crypto_run_interval(m->net_crypto); 2355 uint32_t crypto_interval = crypto_run_interval(m->net_crypto);
2356 2356
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index a9931d1f..367e65c7 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -891,7 +891,7 @@ void do_messenger(Messenger *m);
891 * 891 *
892 * returns time (in ms) before the next do_messenger() needs to be run on success. 892 * returns time (in ms) before the next do_messenger() needs to be run on success.
893 */ 893 */
894uint32_t messenger_run_interval(Messenger *m); 894uint32_t messenger_run_interval(const Messenger *m);
895 895
896/* SAVING AND LOADING FUNCTIONS: */ 896/* SAVING AND LOADING FUNCTIONS: */
897 897
diff --git a/toxcore/tox.c b/toxcore/tox.c
index b509fc3b..dbd8a96c 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -20,11 +20,7 @@
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 * 21 *
22 */ 22 */
23/* 23
24TODO:
25-replace bool with uint8_t
26-remove enums (typedef enum in api to uint8_t)
27*/
28#ifdef HAVE_CONFIG_H 24#ifdef HAVE_CONFIG_H
29#include "config.h" 25#include "config.h"
30#endif 26#endif
@@ -157,6 +153,7 @@ Tox *tox_new(struct Tox_Options const *options, uint8_t const *data, size_t leng
157 153
158 if (!new_groupchats(m)) { 154 if (!new_groupchats(m)) {
159 kill_messenger(m); 155 kill_messenger(m);
156 SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
160 return NULL; 157 return NULL;
161 } 158 }
162 159
@@ -180,16 +177,120 @@ void tox_kill(Tox *tox)
180 logger_kill_global(); 177 logger_kill_global();
181} 178}
182 179
183
184size_t tox_save_size(Tox const *tox) 180size_t tox_save_size(Tox const *tox)
185{ 181{
186 const Messenger *m = tox; 182 const Messenger *m = tox;
187 return messenger_size(m); 183 return messenger_size(m);
188} 184}
189 185
190
191void tox_save(Tox const *tox, uint8_t *data) 186void tox_save(Tox const *tox, uint8_t *data)
192{ 187{
193 const Messenger *m = tox; 188 const Messenger *m = tox;
194 messenger_save(m, data); 189 messenger_save(m, data);
195} 190}
191
192static int address_to_ip(Messenger *m, char const *address, IP_Port *ip_port, IP_Port *ip_port_v4)
193{
194 if (!addr_parse_ip(address, &ip_port->ip)) {
195 if (m->options.udp_disabled) { /* Disable DNS when udp is disabled. */
196 return -1;
197 }
198
199 IP *ip_extra = NULL;
200 ip_init(&ip_port->ip, m->options.ipv6enabled);
201
202 if (m->options.ipv6enabled && ip_port_v4) {
203 /* setup for getting BOTH: an IPv6 AND an IPv4 address */
204 ip_port->ip.family = AF_UNSPEC;
205 ip_reset(&ip_port_v4->ip);
206 ip_extra = &ip_port_v4->ip;
207 }
208
209 if (!addr_resolve(address, &ip_port->ip, ip_extra)) {
210 return -1;
211 }
212 }
213
214 return 0;
215}
216
217bool tox_bootstrap(Tox *tox, char const *address, uint16_t port, uint8_t const *public_key, TOX_ERR_BOOTSTRAP *error)
218{
219 Messenger *m = tox;
220 bool ret = tox_add_tcp_relay(tox, address, port, public_key, error);
221
222 if (!ret) {
223 return 0;
224 }
225
226 if (m->options.udp_disabled) {
227 return ret;
228 } else { /* DHT only works on UDP. */
229 if (DHT_bootstrap_from_address(m->dht, address, m->options.ipv6enabled, htons(port), public_key) == 0) {
230 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_ADDRESS);
231 return 0;
232 }
233
234 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK);
235 return 1;
236 }
237}
238
239bool tox_add_tcp_relay(Tox *tox, char const *address, uint16_t port, uint8_t const *public_key,
240 TOX_ERR_BOOTSTRAP *error)
241{
242 Messenger *m = tox;
243 IP_Port ip_port, ip_port_v4;
244
245 if (port == 0) {
246 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT);
247 return 0;
248 }
249
250 if (address_to_ip(m, address, &ip_port, &ip_port_v4) == -1) {
251 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_ADDRESS);
252 return 0;
253 }
254
255 ip_port.port = htons(port);
256 add_tcp_relay(m->net_crypto, ip_port, public_key);
257 onion_add_bs_path_node(m->onion_c, ip_port, public_key); //TODO: move this
258
259 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK);
260 return 1;
261}
262
263TOX_CONNECTION tox_get_connection_status(Tox const *tox)
264{
265 const Messenger *m = tox;
266
267 if (onion_isconnected(m->onion_c)) {
268 if (DHT_non_lan_connected(m->dht)) {
269 return TOX_CONNECTION_UDP;
270 }
271
272 return TOX_CONNECTION_TCP;
273 }
274
275 return TOX_CONNECTION_NONE;
276}
277
278
279void tox_callback_connection_status(Tox *tox, tox_connection_status_cb *function, void *user_data)
280{
281 //TODO
282}
283
284uint32_t tox_iteration_interval(Tox const *tox)
285{
286 const Messenger *m = tox;
287 return messenger_run_interval(m);
288}
289
290void tox_iteration(Tox *tox)
291{
292 Messenger *m = tox;
293 do_messenger(m);
294 do_groupchats(m->group_chat_object);
295}
296
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 61f40539..056dcd31 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -571,16 +571,14 @@ typedef enum TOX_CONNECTION {
571 * is connected through a TCP relay, only. For a friend, this means that the 571 * is connected through a TCP relay, only. For a friend, this means that the
572 * connection to that particular friend goes through a TCP relay. 572 * connection to that particular friend goes through a TCP relay.
573 */ 573 */
574 TOX_CONNECTION_TCP4, 574 TOX_CONNECTION_TCP,
575 TOX_CONNECTION_TCP6,
576 /** 575 /**
577 * A UDP connection has been established. For the own instance, this means it 576 * A UDP connection has been established. For the own instance, this means it
578 * is able to send UDP packets to DHT nodes, but may still be connected to 577 * is able to send UDP packets to DHT nodes, but may still be connected to
579 * a TCP relay. For a friend, this means that the connection to that 578 * a TCP relay. For a friend, this means that the connection to that
580 * particular friend was built using direct UDP packets. 579 * particular friend was built using direct UDP packets.
581 */ 580 */
582 TOX_CONNECTION_UDP4, 581 TOX_CONNECTION_UDP
583 TOX_CONNECTION_UDP6
584} TOX_CONNECTION; 582} TOX_CONNECTION;
585 583
586 584