From 36640224afbc97f9f87437145607378e8fe245f7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 24 May 2015 17:36:13 -0400 Subject: [PATCH 1/2] toxcore API changes to tox_new --- src/toxic.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/toxic.c b/src/toxic.c index 38e8145..a2e2e55 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -640,7 +640,11 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, TOX_ERR_NEW (uint8_t *) plain, &pwerr); if (pwerr == TOX_ERR_DECRYPTION_OK) { - m = tox_new(tox_opts, (uint8_t *) plain, plain_len, new_err); + tox_opts->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; + tox_opts->savedata_data = (uint8_t *) plain; + tox_opts->savedata_length = plain_len; + + m = tox_new(tox_opts, new_err); if (m == NULL) { fclose(fp); @@ -658,7 +662,11 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, TOX_ERR_NEW } } } else { /* data is not encrypted */ - m = tox_new(tox_opts, (uint8_t *) data, len, new_err); + tox_opts->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; + tox_opts->savedata_data = (uint8_t *) data; + tox_opts->savedata_length = len; + + m = tox_new(tox_opts, new_err); if (m == NULL) { fclose(fp); @@ -671,7 +679,9 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, TOX_ERR_NEW if (file_exists(data_path)) exit_toxic_err("failed in load_toxic", FATALERR_FILEOP); - m = tox_new(tox_opts, NULL, 0, new_err); + tox_opts->savedata_type = TOX_SAVEDATA_TYPE_NONE; + + m = tox_new(tox_opts, new_err); if (m == NULL) return NULL; From d0a7ca17d298e3c1da9b77ca8b0d88fd109dc65e Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 24 May 2015 17:56:30 -0400 Subject: [PATCH 2/2] separate bootstrapping and adding TCP relays per toxcore API changes --- src/global_commands.c | 1 + src/toxic.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/global_commands.c b/src/global_commands.c index f5e6fd7..fc7e3b8 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -260,6 +260,7 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv) TOX_ERR_BOOTSTRAP err; tox_bootstrap(m, ip, atoi(port), (uint8_t *) binary_string, &err); + tox_add_tcp_relay(m, ip, atoi(port), (uint8_t *) binary_string, &err); free(binary_string); switch (err) { diff --git a/src/toxic.c b/src/toxic.c index a2e2e55..9d14328 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -305,14 +305,34 @@ static int load_nodelist(const char *filename) return 0; } +/* Bootstraps and adds as TCP relay. + * Returns 0 if both actions are successful. + * Returns -1 otherwise. + */ int init_connection_helper(Tox *m, int line) { - return tox_bootstrap(m, toxNodes.nodes[line], toxNodes.ports[line], (uint8_t *) toxNodes.keys[line], NULL); + TOX_ERR_BOOTSTRAP err; + tox_bootstrap(m, toxNodes.nodes[line], toxNodes.ports[line], (uint8_t *) toxNodes.keys[line], &err); + + if (err != TOX_ERR_BOOTSTRAP_OK) { + fprintf(stderr, "Failed to bootstrap %s:%d\n", toxNodes.nodes[line], toxNodes.ports[line]); + return -1; + } + + tox_add_tcp_relay(m, toxNodes.nodes[line], toxNodes.ports[line], (uint8_t *) toxNodes.keys[line], &err); + + if (err != TOX_ERR_BOOTSTRAP_OK) { + fprintf(stderr, "Failed to add TCP relay %s:%d\n", toxNodes.nodes[line], toxNodes.ports[line]); + return -1; + } + + return 0; } /* Connects to a random DHT node listed in the DHTnodes file * * return codes: + * 0: success * 1: failed to open node file * 2: no line of sufficient length in node file * 3: failed to resolve name to IP @@ -324,8 +344,10 @@ static bool srvlist_loaded = false; int init_connection(Tox *m) { - if (toxNodes.lines > 0) /* already loaded nodelist */ - return init_connection_helper(m, rand() % toxNodes.lines) ? 0 : 3; + if (toxNodes.lines > 0) { /* already loaded nodelist */ + init_connection_helper(m, rand() % toxNodes.lines); + return 0; + } /* only once: * - load the nodelist @@ -348,7 +370,7 @@ int init_connection(Tox *m) int n = MIN(NUM_INIT_NODES, toxNodes.lines); for (i = 0; i < n; ++i) { - if (init_connection_helper(m, rand() % toxNodes.lines)) + if (init_connection_helper(m, rand() % toxNodes.lines) == 0) res = 0; }