From ca1fca5aa5b1acc553d71a6363284472f05111ce Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 24 May 2015 17:56:30 -0400 Subject: [PATCH] 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; }