2016-09-17 20:26:23 +02:00
|
|
|
/* bootstrap.c
|
|
|
|
*
|
|
|
|
*
|
2016-09-18 03:38:32 +02:00
|
|
|
* Copyright (C) 2016 Toxic All Rights Reserved.
|
2016-09-17 20:26:23 +02:00
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2016-09-20 06:00:09 +02:00
|
|
|
#include <limits.h>
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
#include <curl/curl.h>
|
2016-09-17 20:26:23 +02:00
|
|
|
#include <tox/tox.h>
|
|
|
|
|
|
|
|
#include "line_info.h"
|
|
|
|
#include "windows.h"
|
|
|
|
#include "misc_tools.h"
|
2016-09-20 06:00:09 +02:00
|
|
|
#include "configdir.h"
|
|
|
|
#include "curl_util.h"
|
2016-09-20 19:13:12 +02:00
|
|
|
#include "settings.h"
|
2018-02-25 06:00:06 +01:00
|
|
|
#include "prompt.h"
|
2016-09-17 20:26:23 +02:00
|
|
|
|
|
|
|
extern struct arg_opts arg_opts;
|
2016-09-20 19:13:12 +02:00
|
|
|
extern struct user_settings *user_settings;
|
2016-09-22 20:09:07 +02:00
|
|
|
extern struct Winthread Winthread;
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
/* URL that we get the JSON encoded nodes list from. */
|
|
|
|
#define NODES_LIST_URL "https://nodes.tox.chat/json"
|
|
|
|
|
2016-09-20 06:53:53 +02:00
|
|
|
#define DEFAULT_NODES_FILENAME "DHTnodes.json"
|
2016-09-17 20:26:23 +02:00
|
|
|
|
|
|
|
/* Time to wait between bootstrap attempts */
|
|
|
|
#define TRY_BOOTSTRAP_INTERVAL 5
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
/* Number of nodes to bootstrap to per try */
|
|
|
|
#define NUM_BOOTSTRAP_NODES 5
|
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
/* Number of seconds since last successful ping before we consider a node offline */
|
|
|
|
#define NODE_OFFLINE_TIMOUT (60*60*24*2)
|
|
|
|
|
|
|
|
#define IP_MAX_SIZE 45
|
2016-09-22 03:22:05 +02:00
|
|
|
#define IP_MIN_SIZE 7
|
2016-09-18 03:38:32 +02:00
|
|
|
#define PORT_MAX_SIZE 5
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
#define LAST_SCAN_JSON_KEY "\"last_scan\":"
|
|
|
|
#define LAST_SCAN_JSON_KEY_LEN (sizeof(LAST_SCAN_JSON_KEY) - 1)
|
|
|
|
|
|
|
|
#define IPV4_JSON_KEY "\"ipv4\":\""
|
|
|
|
#define IPV4_JSON_KEY_LEN (sizeof(IPV4_JSON_KEY) - 1)
|
2016-09-20 06:00:09 +02:00
|
|
|
|
2016-09-22 03:22:05 +02:00
|
|
|
#define IPV6_JSON_KEY "\"ipv6\":\""
|
|
|
|
#define IPV6_JSON_KEY_LEN (sizeof(IPV6_JSON_KEY) - 1)
|
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
#define PORT_JSON_KEY "\"port\":"
|
|
|
|
#define PORT_JSON_KEY_LEN (sizeof(PORT_JSON_KEY) - 1)
|
2016-09-18 03:38:32 +02:00
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
#define PK_JSON_KEY "\"public_key\":\""
|
|
|
|
#define PK_JSON_KEY_LEN (sizeof(PK_JSON_KEY) - 1)
|
2016-09-18 03:38:32 +02:00
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
#define LAST_PING_JSON_KEY "\"last_ping\":"
|
|
|
|
#define LAST_PING_JSON_KEY_LEN (sizeof(LAST_PING_JSON_KEY) - 1)
|
|
|
|
|
|
|
|
/* Maximum allowable size of the nodes list */
|
|
|
|
#define MAX_NODELIST_SIZE (MAX_RECV_CURL_DATA_SIZE)
|
2016-09-18 03:38:32 +02:00
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
|
2016-09-23 00:00:14 +02:00
|
|
|
static struct Thread_Data {
|
2016-09-22 20:09:07 +02:00
|
|
|
pthread_t tid;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
volatile bool active;
|
|
|
|
} thread_data;
|
|
|
|
|
|
|
|
#define MAX_NODES 50
|
2016-09-21 18:51:57 +02:00
|
|
|
struct Node {
|
|
|
|
char ip4[IP_MAX_SIZE + 1];
|
2016-09-22 03:22:05 +02:00
|
|
|
bool have_ip4;
|
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
char ip6[IP_MAX_SIZE + 1];
|
2016-09-22 03:22:05 +02:00
|
|
|
bool have_ip6;
|
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
char key[TOX_PUBLIC_KEY_SIZE];
|
|
|
|
uint16_t port;
|
|
|
|
};
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-20 19:13:12 +02:00
|
|
|
static struct DHT_Nodes {
|
2016-09-22 20:09:07 +02:00
|
|
|
struct Node list[MAX_NODES];
|
2016-09-21 18:51:57 +02:00
|
|
|
size_t count;
|
2016-09-23 00:31:52 +02:00
|
|
|
time_t last_updated;
|
2016-09-20 19:13:12 +02:00
|
|
|
} Nodes;
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
|
|
|
|
/* Determine if a node is offline by comparing the age of the nodeslist
|
|
|
|
* to the last time the node was successfully pinged.
|
|
|
|
*/
|
2018-01-20 19:30:35 +01:00
|
|
|
static bool node_is_offline(unsigned long long int last_ping)
|
|
|
|
{
|
|
|
|
return last_ping + NODE_OFFLINE_TIMOUT <= last_ping;
|
|
|
|
}
|
2016-09-21 18:51:57 +02:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
/* Return true if nodeslist pointed to by fp needs to be updated.
|
|
|
|
* This will be the case if the file is empty, has an invalid format,
|
|
|
|
* or if the file is older than the given timeout.
|
|
|
|
*/
|
|
|
|
static bool nodeslist_needs_update(const char *nodes_path)
|
|
|
|
{
|
2016-09-20 19:13:12 +02:00
|
|
|
if (user_settings->nodeslist_update_freq <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
FILE *fp = fopen(nodes_path, "r+");
|
|
|
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* last_scan value should be at beginning of file */
|
2016-09-21 18:51:57 +02:00
|
|
|
char line[LAST_SCAN_JSON_KEY_LEN + 32];
|
2016-09-20 06:00:09 +02:00
|
|
|
|
|
|
|
if (fgets(line, sizeof(line), fp) == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
const char *last_scan_val = strstr(line, LAST_SCAN_JSON_KEY);
|
2016-09-20 06:00:09 +02:00
|
|
|
|
|
|
|
if (last_scan_val == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
long long int last_scan = strtoll(last_scan_val + LAST_SCAN_JSON_KEY_LEN, NULL, 10);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&thread_data.lock);
|
2016-09-21 18:51:57 +02:00
|
|
|
Nodes.last_updated = last_scan;
|
2016-09-22 20:09:07 +02:00
|
|
|
pthread_mutex_unlock(&thread_data.lock);
|
2016-09-20 06:00:09 +02:00
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
bool is_timeout = timed_out(last_scan, user_settings->nodeslist_update_freq * 24 * 60 * 60);
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
|
|
|
|
if (is_timeout) {
|
2016-09-20 06:00:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fetches the JSON encoded DHT nodeslist from NODES_LIST_URL.
|
|
|
|
*
|
|
|
|
* Return 0 on success.
|
|
|
|
* Return -1 on failure.
|
|
|
|
*/
|
2016-09-20 19:13:12 +02:00
|
|
|
static int curl_fetch_nodes_JSON(struct Recv_Curl_Data *recv_data)
|
2016-09-20 06:00:09 +02:00
|
|
|
{
|
|
|
|
CURL *c_handle = curl_easy_init();
|
|
|
|
|
|
|
|
if (c_handle == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
int err = -1;
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
struct curl_slist *headers = NULL;
|
|
|
|
headers = curl_slist_append(headers, "Content-Type: application/json");
|
|
|
|
headers = curl_slist_append(headers, "charsets: utf-8");
|
|
|
|
|
|
|
|
curl_easy_setopt(c_handle, CURLOPT_HTTPHEADER, headers);
|
|
|
|
curl_easy_setopt(c_handle, CURLOPT_URL, NODES_LIST_URL);
|
2016-09-20 19:13:12 +02:00
|
|
|
curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, curl_cb_write_data);
|
2016-09-20 06:00:09 +02:00
|
|
|
curl_easy_setopt(c_handle, CURLOPT_WRITEDATA, recv_data);
|
|
|
|
curl_easy_setopt(c_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
|
|
|
curl_easy_setopt(c_handle, CURLOPT_HTTPGET, 1L);
|
|
|
|
|
|
|
|
int proxy_ret = set_curl_proxy(c_handle, arg_opts.proxy_address, arg_opts.proxy_port, arg_opts.proxy_type);
|
|
|
|
|
|
|
|
if (proxy_ret != 0) {
|
|
|
|
fprintf(stderr, "set_curl_proxy() failed with error %d\n", proxy_ret);
|
2016-09-22 20:09:07 +02:00
|
|
|
goto on_exit;
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ret = curl_easy_setopt(c_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
|
|
|
|
|
|
|
|
if (ret != CURLE_OK) {
|
|
|
|
fprintf(stderr, "TLSv1.2 could not be set (libcurl error %d)", ret);
|
2016-09-22 20:09:07 +02:00
|
|
|
goto on_exit;
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = curl_easy_setopt(c_handle, CURLOPT_SSL_CIPHER_LIST, TLS_CIPHER_SUITE_LIST);
|
|
|
|
|
|
|
|
if (ret != CURLE_OK) {
|
|
|
|
fprintf(stderr, "Failed to set TLS cipher list (libcurl error %d)", ret);
|
2016-09-22 20:09:07 +02:00
|
|
|
goto on_exit;
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = curl_easy_perform(c_handle);
|
|
|
|
|
|
|
|
if (ret != CURLE_OK) {
|
|
|
|
/* If system doesn't support any of the specified ciphers suites, fall back to default */
|
|
|
|
if (ret == CURLE_SSL_CIPHER) {
|
|
|
|
curl_easy_setopt(c_handle, CURLOPT_SSL_CIPHER_LIST, NULL);
|
|
|
|
ret = curl_easy_perform(c_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != CURLE_OK) {
|
|
|
|
fprintf(stderr, "HTTPS lookup error (libcurl error %d)\n", ret);
|
2016-09-22 20:09:07 +02:00
|
|
|
goto on_exit;
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
err = 0;
|
|
|
|
|
|
|
|
on_exit:
|
|
|
|
curl_slist_free_all(headers);
|
|
|
|
curl_easy_cleanup(c_handle);
|
|
|
|
return err;
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempts to update the DHT nodeslist.
|
|
|
|
*
|
|
|
|
* Return 1 if list was updated successfully.
|
|
|
|
* Return 0 if list does not need to be updated.
|
|
|
|
* Return -1 if file cannot be opened.
|
|
|
|
* Return -2 if http lookup failed.
|
|
|
|
* Return -3 if http reponse was empty.
|
|
|
|
* Return -4 if data could not be written to disk.
|
2020-10-27 17:34:12 +01:00
|
|
|
* Return -5 if memory allocation fails.
|
2016-09-20 06:00:09 +02:00
|
|
|
*/
|
|
|
|
static int update_DHT_nodeslist(const char *nodes_path)
|
|
|
|
{
|
|
|
|
if (!nodeslist_needs_update(nodes_path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *fp = fopen(nodes_path, "r+");
|
|
|
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:34:12 +01:00
|
|
|
struct Recv_Curl_Data *recv_data = calloc(1, sizeof(struct Recv_Curl_Data));
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2020-10-27 17:34:12 +01:00
|
|
|
if (recv_data == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return -5;
|
|
|
|
}
|
2016-09-20 06:00:09 +02:00
|
|
|
|
2020-10-27 17:34:12 +01:00
|
|
|
if (curl_fetch_nodes_JSON(recv_data) == -1) {
|
|
|
|
free(recv_data);
|
2016-09-20 06:00:09 +02:00
|
|
|
fclose(fp);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:34:12 +01:00
|
|
|
if (recv_data->length == 0) {
|
|
|
|
free(recv_data);
|
2016-09-20 06:00:09 +02:00
|
|
|
fclose(fp);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:34:12 +01:00
|
|
|
if (fwrite(recv_data->data, recv_data->length, 1, fp) != 1) {
|
|
|
|
free(recv_data);
|
2016-09-20 06:00:09 +02:00
|
|
|
fclose(fp);
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:34:12 +01:00
|
|
|
free(recv_data);
|
2016-09-20 06:00:09 +02:00
|
|
|
fclose(fp);
|
2020-10-27 17:34:12 +01:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_nodeslist_path(char *buf, size_t buf_size)
|
|
|
|
{
|
|
|
|
char *config_dir = NULL;
|
|
|
|
|
|
|
|
if (arg_opts.nodes_path[0]) {
|
|
|
|
snprintf(buf, buf_size, "%s", arg_opts.nodes_path);
|
|
|
|
} else if ((config_dir = get_user_config_dir()) != NULL) {
|
|
|
|
snprintf(buf, buf_size, "%s%s%s", config_dir, CONFIGDIR, DEFAULT_NODES_FILENAME);
|
|
|
|
free(config_dir);
|
|
|
|
} else {
|
|
|
|
snprintf(buf, buf_size, "%s", DEFAULT_NODES_FILENAME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 03:22:05 +02:00
|
|
|
/* Return true if json encoded string s contains a valid IP address and puts address in ip_buf.
|
|
|
|
*
|
|
|
|
* ip_type should be set to 1 for ipv4 address, or 0 for ipv6 addresses.
|
|
|
|
* ip_buf must have room for at least IP_MAX_SIZE + 1 bytes.
|
|
|
|
*/
|
|
|
|
static bool extract_val_ip(const char *s, char *ip_buf, unsigned short int ip_type)
|
|
|
|
{
|
|
|
|
int ip_len = char_find(0, s, '"');
|
|
|
|
|
|
|
|
if (ip_len < IP_MIN_SIZE || ip_len > IP_MAX_SIZE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ip_buf, s, ip_len);
|
|
|
|
ip_buf[ip_len] = 0;
|
|
|
|
|
|
|
|
return (ip_type == 1) ? is_ip4_address(ip_buf) : is_ip6_address(ip_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extracts the port from json encoded string s.
|
|
|
|
*
|
|
|
|
* Return port number on success.
|
|
|
|
* Return 0 on failure.
|
|
|
|
*/
|
|
|
|
static uint16_t extract_val_port(const char *s)
|
|
|
|
{
|
|
|
|
long int port = strtol(s, NULL, 10);
|
|
|
|
return (port > 0 && port <= MAX_PORT_RANGE) ? port : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extracts the last pinged value from json encoded string s.
|
|
|
|
*
|
|
|
|
* Return timestamp on success.
|
|
|
|
* Return -1 on failure.
|
|
|
|
*/
|
|
|
|
static long long int extract_val_last_pinged(const char *s)
|
|
|
|
{
|
|
|
|
long long int last_pinged = strtoll(s, NULL, 10);
|
|
|
|
return (last_pinged <= 0) ? -1 : last_pinged;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extracts DHT public key from json encoded string s and puts key in key_buf.
|
|
|
|
* key_buf must have room for at least TOX_PUBLIC_KEY_SIZE * 2 + 1 bytes.
|
|
|
|
*
|
|
|
|
* Return number of bytes copied to key_buf on success.
|
|
|
|
* Return -1 on failure.
|
|
|
|
*/
|
|
|
|
static int extract_val_pk(const char *s, char *key_buf)
|
|
|
|
{
|
|
|
|
|
|
|
|
int key_len = char_find(0, s, '"');
|
|
|
|
|
|
|
|
if (key_len != TOX_PUBLIC_KEY_SIZE * 2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(key_buf, s, key_len);
|
|
|
|
key_buf[key_len] = 0;
|
|
|
|
|
|
|
|
return key_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extracts values from json formatted string, validats them, and puts them in node.
|
|
|
|
*
|
|
|
|
* Return 0 on success.
|
|
|
|
* Return -1 if line is empty.
|
|
|
|
* Return -2 if line does not appear to be a valid nodes list entry.
|
|
|
|
* Return -3 if node appears to be offline.
|
|
|
|
* Return -4 if entry does not contain either a valid ipv4 or ipv6 address.
|
|
|
|
* Return -5 if port value is invalid.
|
|
|
|
* Return -6 if public key is invalid.
|
|
|
|
*/
|
|
|
|
static int extract_node(const char *line, struct Node *node)
|
|
|
|
{
|
|
|
|
if (!line) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ip4_start = strstr(line, IPV4_JSON_KEY);
|
|
|
|
const char *ip6_start = strstr(line, IPV6_JSON_KEY);
|
|
|
|
const char *port_start = strstr(line, PORT_JSON_KEY);
|
|
|
|
const char *key_start = strstr(line, PK_JSON_KEY);
|
|
|
|
const char *last_pinged_str = strstr(line, LAST_PING_JSON_KEY);
|
|
|
|
|
|
|
|
if (!ip4_start || !ip6_start || !port_start || !key_start || !last_pinged_str) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long int last_pinged = extract_val_last_pinged(last_pinged_str + LAST_PING_JSON_KEY_LEN);
|
|
|
|
|
2018-01-20 19:30:35 +01:00
|
|
|
if (last_pinged <= 0 || node_is_offline(last_pinged)) {
|
2016-09-22 03:22:05 +02:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
char ip4_string[IP_MAX_SIZE + 1];
|
|
|
|
bool have_ip4 = extract_val_ip(ip4_start + IPV4_JSON_KEY_LEN, ip4_string, 1);
|
|
|
|
|
|
|
|
char ip6_string[IP_MAX_SIZE + 1];
|
|
|
|
bool have_ip6 = extract_val_ip(ip6_start + IPV6_JSON_KEY_LEN, ip6_string, 0);
|
|
|
|
|
|
|
|
if (!have_ip6 && !have_ip4) {
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t port = extract_val_port(port_start + PORT_JSON_KEY_LEN);
|
|
|
|
|
|
|
|
if (port == 0) {
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
|
|
|
char key_string[TOX_PUBLIC_KEY_SIZE * 2 + 1];
|
|
|
|
int key_len = extract_val_pk(key_start + PK_JSON_KEY_LEN, key_string);
|
|
|
|
|
|
|
|
if (key_len == -1) {
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hex_string_to_bin(key_string, key_len, node->key, TOX_PUBLIC_KEY_SIZE) == -1) {
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_ip4) {
|
|
|
|
snprintf(node->ip4, sizeof(node->ip4), "%s", ip4_string);
|
|
|
|
node->have_ip4 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_ip6) {
|
|
|
|
snprintf(node->ip6, sizeof(node->ip6), "%s", ip6_string);
|
|
|
|
node->have_ip6 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->port = port;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
/* Loads the DHT nodeslist to memory from json encoded nodes file. */
|
|
|
|
void *load_nodeslist_thread(void *data)
|
2016-09-17 20:26:23 +02:00
|
|
|
{
|
2020-03-15 19:57:00 +01:00
|
|
|
UNUSED_VAR(data);
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
char nodes_path[PATH_MAX];
|
|
|
|
get_nodeslist_path(nodes_path, sizeof(nodes_path));
|
|
|
|
|
|
|
|
FILE *fp = NULL;
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
if (!file_exists(nodes_path)) {
|
|
|
|
if ((fp = fopen(nodes_path, "w+")) == NULL) {
|
2016-09-22 20:09:07 +02:00
|
|
|
fprintf(stderr, "nodeslist load error: failed to create file '%s'\n", nodes_path);
|
|
|
|
goto on_exit;
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
|
|
|
} else if ((fp = fopen(nodes_path, "r+")) == NULL) {
|
2016-09-22 20:09:07 +02:00
|
|
|
fprintf(stderr, "nodeslist load error: failed to open file '%s'\n", nodes_path);
|
|
|
|
goto on_exit;
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int update_err = update_DHT_nodeslist(nodes_path);
|
|
|
|
|
|
|
|
if (update_err < 0) {
|
|
|
|
fprintf(stderr, "update_DHT_nodeslist() failed with error %d\n", update_err);
|
|
|
|
}
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-21 18:51:57 +02:00
|
|
|
char line[MAX_NODELIST_SIZE + 1];
|
2016-09-18 03:38:32 +02:00
|
|
|
|
|
|
|
if (fgets(line, sizeof(line), fp) == NULL) {
|
2016-09-20 06:00:09 +02:00
|
|
|
fclose(fp);
|
2016-09-22 20:09:07 +02:00
|
|
|
fprintf(stderr, "nodeslist load error: file empty.\n");
|
|
|
|
goto on_exit;
|
2016-09-18 03:38:32 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
size_t idx = 0;
|
2016-09-18 03:38:32 +02:00
|
|
|
const char *line_start = line;
|
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
while ((line_start = strstr(line_start + 1, IPV4_JSON_KEY))) {
|
|
|
|
pthread_mutex_lock(&thread_data.lock);
|
|
|
|
idx = Nodes.count;
|
|
|
|
|
|
|
|
if (idx >= MAX_NODES) {
|
|
|
|
pthread_mutex_unlock(&thread_data.lock);
|
|
|
|
break;
|
|
|
|
}
|
2016-09-18 03:38:32 +02:00
|
|
|
|
2016-09-22 03:22:05 +02:00
|
|
|
if (extract_node(line_start, &Nodes.list[idx]) == 0) {
|
|
|
|
++Nodes.count;
|
2016-09-21 06:28:16 +02:00
|
|
|
}
|
2016-09-22 20:09:07 +02:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&thread_data.lock);
|
2016-09-17 20:26:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
/* If nodeslist does not contain any valid entries we set the last_scan value
|
|
|
|
* to 0 so that it will fetch a new list the next time this function is called.
|
|
|
|
*/
|
2016-10-23 18:26:18 +02:00
|
|
|
if (Nodes.count == 0) {
|
2016-09-20 06:00:09 +02:00
|
|
|
const char *s = "{\"last_scan\":0}";
|
|
|
|
rewind(fp);
|
|
|
|
fwrite(s, strlen(s), 1, fp); // Not much we can do if it fails
|
|
|
|
fclose(fp);
|
2016-09-22 20:09:07 +02:00
|
|
|
fprintf(stderr, "nodeslist load error: List did not contain any valid entries.\n");
|
|
|
|
goto on_exit;
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
fclose(fp);
|
2016-09-22 20:09:07 +02:00
|
|
|
|
|
|
|
on_exit:
|
|
|
|
thread_data.active = false;
|
|
|
|
pthread_attr_destroy(&thread_data.attr);
|
|
|
|
pthread_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates a new thread that will load the DHT nodeslist to memory
|
|
|
|
* from json encoded nodes file obtained at NODES_LIST_URL. Only one
|
|
|
|
* thread may run at a time.
|
|
|
|
*
|
|
|
|
* Return 0 on success.
|
|
|
|
* Return -1 if a thread is already active.
|
|
|
|
* Return -2 if mutex fails to init.
|
|
|
|
* Return -3 if pthread attribute fails to init.
|
|
|
|
* Return -4 if pthread fails to set detached state.
|
|
|
|
* Return -5 if thread creation fails.
|
|
|
|
*/
|
|
|
|
int load_DHT_nodeslist(void)
|
|
|
|
{
|
|
|
|
if (thread_data.active) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_mutex_init(&thread_data.lock, NULL) != 0) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_attr_init(&thread_data.attr) != 0) {
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_attr_setdetachstate(&thread_data.attr, PTHREAD_CREATE_DETACHED) != 0) {
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_data.active = true;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
if (pthread_create(&thread_data.tid, &thread_data.attr, load_nodeslist_thread, NULL) != 0) {
|
|
|
|
thread_data.active = false;
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
2016-09-17 20:26:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
/* Connects to NUM_BOOTSTRAP_NODES random DHT nodes listed in the DHTnodes file. */
|
2016-09-17 20:26:23 +02:00
|
|
|
static void DHT_bootstrap(Tox *m)
|
|
|
|
{
|
2016-09-22 20:09:07 +02:00
|
|
|
pthread_mutex_lock(&thread_data.lock);
|
|
|
|
size_t num_nodes = Nodes.count;
|
|
|
|
pthread_mutex_unlock(&thread_data.lock);
|
|
|
|
|
|
|
|
if (num_nodes == 0) {
|
2016-09-17 20:26:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
size_t i;
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
pthread_mutex_lock(&thread_data.lock);
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
for (i = 0; i < NUM_BOOTSTRAP_NODES; ++i) {
|
2016-09-22 03:22:05 +02:00
|
|
|
struct Node *node = &Nodes.list[rand() % Nodes.count];
|
|
|
|
const char *addr = node->have_ip4 ? node->ip4 : node->ip6;
|
|
|
|
|
|
|
|
if (!addr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-10 05:06:57 +02:00
|
|
|
Tox_Err_Bootstrap err;
|
2016-09-22 03:22:05 +02:00
|
|
|
tox_bootstrap(m, addr, node->port, (uint8_t *) node->key, &err);
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
if (err != TOX_ERR_BOOTSTRAP_OK) {
|
2016-09-22 03:22:05 +02:00
|
|
|
fprintf(stderr, "Failed to bootstrap %s:%d\n", addr, node->port);
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 03:22:05 +02:00
|
|
|
tox_add_tcp_relay(m, addr, node->port, (uint8_t *) node->key, &err);
|
2016-09-17 20:26:23 +02:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
if (err != TOX_ERR_BOOTSTRAP_OK) {
|
2016-09-22 03:22:05 +02:00
|
|
|
fprintf(stderr, "Failed to add TCP relay %s:%d\n", addr, node->port);
|
2016-09-20 06:00:09 +02:00
|
|
|
}
|
2016-09-17 20:26:23 +02:00
|
|
|
}
|
2016-09-22 20:09:07 +02:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&thread_data.lock);
|
2016-09-17 20:26:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Manages connection to the Tox DHT network. */
|
|
|
|
void do_tox_connection(Tox *m)
|
|
|
|
{
|
2016-09-23 00:31:52 +02:00
|
|
|
static time_t last_bootstrap_time = 0;
|
2018-02-25 06:00:06 +01:00
|
|
|
bool connected = prompt_selfConnectionStatus() != TOX_CONNECTION_NONE;
|
2016-09-17 20:26:23 +02:00
|
|
|
|
|
|
|
if (!connected && timed_out(last_bootstrap_time, TRY_BOOTSTRAP_INTERVAL)) {
|
|
|
|
DHT_bootstrap(m);
|
|
|
|
last_bootstrap_time = get_unix_time();
|
|
|
|
}
|
|
|
|
}
|