1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-06-20 07:56:37 +02:00

Add setting to control DHT nodeslist update frequency

Also rename a few things and semi-fix man page format issues
This commit is contained in:
Jfreegman
2016-09-20 13:13:12 -04:00
parent 4f6c603543
commit 151f5f0c51
11 changed files with 77 additions and 49 deletions

View File

@ -33,9 +33,10 @@
#include "misc_tools.h"
#include "configdir.h"
#include "curl_util.h"
#include "settings.h"
extern struct arg_opts arg_opts;
extern struct user_settings *user_settings;
/* URL that we get the JSON encoded nodes list from. */
#define NODES_LIST_URL "https://nodes.tox.chat/json"
@ -48,9 +49,6 @@ extern struct arg_opts arg_opts;
/* Number of nodes to bootstrap to per try */
#define NUM_BOOTSTRAP_NODES 5
/* How often we should update the nodeslist file. */
#define NODELIST_UPDATE_TIMEOUT (60*24*30)
#define IPv4_MAX_SIZE 64
#define PORT_MAX_SIZE 5
@ -72,12 +70,12 @@ extern struct arg_opts arg_opts;
#define NODELEN (MAX_NODE_LINE - TOX_PUBLIC_KEY_SIZE - 7)
#define MAX_NODELIST_SIZE (1024 * MAXNODES)
static struct toxNodes {
static struct DHT_Nodes {
size_t lines;
char nodes[MAXNODES][NODELEN];
uint16_t ports[MAXNODES];
char keys[MAXNODES][TOX_PUBLIC_KEY_SIZE];
} toxNodes;
} Nodes;
/* Return true if nodeslist pointed to by fp needs to be updated.
@ -86,6 +84,10 @@ static struct toxNodes {
*/
static bool nodeslist_needs_update(const char *nodes_path)
{
if (user_settings->nodeslist_update_freq <= 0) {
return false;
}
FILE *fp = fopen(nodes_path, "r+");
if (fp == NULL) {
@ -111,7 +113,7 @@ static bool nodeslist_needs_update(const char *nodes_path)
last_scan_val += LAST_SCAN_JSON_VALUE_LEN;
long long int last_scan = strtoll(last_scan_val, NULL, 10);
if (timed_out(last_scan, NODELIST_UPDATE_TIMEOUT)) {
if (timed_out(last_scan, user_settings->nodeslist_update_freq * 24 * 60 * 60)) {
return true;
}
@ -123,7 +125,7 @@ static bool nodeslist_needs_update(const char *nodes_path)
* Return 0 on success.
* Return -1 on failure.
*/
static int curl_fetch_nodes_JSON(struct Recv_Data *recv_data)
static int curl_fetch_nodes_JSON(struct Recv_Curl_Data *recv_data)
{
CURL *c_handle = curl_easy_init();
@ -137,7 +139,7 @@ static int curl_fetch_nodes_JSON(struct Recv_Data *recv_data)
curl_easy_setopt(c_handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(c_handle, CURLOPT_URL, NODES_LIST_URL);
curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, write_lookup_data);
curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, curl_cb_write_data);
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);
@ -202,8 +204,8 @@ static int update_DHT_nodeslist(const char *nodes_path)
return -1;
}
struct Recv_Data recv_data;
memset(&recv_data, 0, sizeof(struct Recv_Data));
struct Recv_Curl_Data recv_data;
memset(&recv_data, 0, sizeof(struct Recv_Curl_Data));
if (curl_fetch_nodes_JSON(&recv_data) == -1) {
fclose(fp);
@ -276,7 +278,7 @@ int load_DHT_nodeslist(void)
const char *line_start = line;
while ((line_start = strstr(line_start + 1, IPV4_JSON_VALUE)) && toxNodes.lines < MAXNODES) {
while ((line_start = strstr(line_start + 1, IPV4_JSON_VALUE)) && Nodes.lines < MAXNODES) {
/* Extract IPv4 address */
const char *ip_start = strstr(line_start, IPV4_JSON_VALUE);
@ -338,19 +340,19 @@ int load_DHT_nodeslist(void)
key_string[TOX_PUBLIC_KEY_SIZE * 2] = 0;
/* Add entry to nodes list */
snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", ipv4_string);
toxNodes.ports[toxNodes.lines] = port;
snprintf(Nodes.nodes[Nodes.lines], sizeof(Nodes.nodes[Nodes.lines]), "%s", ipv4_string);
Nodes.ports[Nodes.lines] = port;
if (hex_string_to_bin(key_string, key_len, toxNodes.keys[toxNodes.lines], TOX_PUBLIC_KEY_SIZE) == -1)
if (hex_string_to_bin(key_string, key_len, Nodes.keys[Nodes.lines], TOX_PUBLIC_KEY_SIZE) == -1)
continue;
toxNodes.lines++;
Nodes.lines++;
}
/* 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.
*/
if (toxNodes.lines == 0) {
if (Nodes.lines == 0) {
const char *s = "{\"last_scan\":0}";
rewind(fp);
fwrite(s, strlen(s), 1, fp); // Not much we can do if it fails
@ -365,26 +367,26 @@ int load_DHT_nodeslist(void)
/* Connects to NUM_BOOTSTRAP_NODES random DHT nodes listed in the DHTnodes file. */
static void DHT_bootstrap(Tox *m)
{
if (toxNodes.lines == 0) {
if (Nodes.lines == 0) {
return;
}
size_t i;
for (i = 0; i < NUM_BOOTSTRAP_NODES; ++i) {
size_t node = rand() % toxNodes.lines;
size_t node = rand() % Nodes.lines;
TOX_ERR_BOOTSTRAP err;
tox_bootstrap(m, toxNodes.nodes[node], toxNodes.ports[node], (uint8_t *) toxNodes.keys[node], &err);
tox_bootstrap(m, Nodes.nodes[node], Nodes.ports[node], (uint8_t *) Nodes.keys[node], &err);
if (err != TOX_ERR_BOOTSTRAP_OK) {
fprintf(stderr, "Failed to bootstrap %s:%d\n", toxNodes.nodes[node], toxNodes.ports[node]);
fprintf(stderr, "Failed to bootstrap %s:%d\n", Nodes.nodes[node], Nodes.ports[node]);
}
tox_add_tcp_relay(m, toxNodes.nodes[node], toxNodes.ports[node], (uint8_t *) toxNodes.keys[node], &err);
tox_add_tcp_relay(m, Nodes.nodes[node], Nodes.ports[node], (uint8_t *) Nodes.keys[node], &err);
if (err != TOX_ERR_BOOTSTRAP_OK) {
fprintf(stderr, "Failed to add TCP relay %s:%d\n", toxNodes.nodes[node], toxNodes.ports[node]);
fprintf(stderr, "Failed to add TCP relay %s:%d\n", Nodes.nodes[node], Nodes.ports[node]);
}
}
}

View File

@ -74,9 +74,9 @@ int set_curl_proxy(CURL *c_handle, const char *proxy_address, uint16_t port, uin
* Returns number of bytes received from http request on success (don't change this).
* Returns 0 if data exceeds buffer size.
*/
size_t write_lookup_data(void *data, size_t size, size_t nmemb, void *user_pointer)
size_t curl_cb_write_data(void *data, size_t size, size_t nmemb, void *user_pointer)
{
struct Recv_Data *recv_data = (struct Recv_Data *) user_pointer;
struct Recv_Curl_Data *recv_data = (struct Recv_Curl_Data *) user_pointer;
size_t length = size * nmemb;
size_t total_size = length + recv_data->length;

View File

@ -27,7 +27,7 @@
#define MAX_RECV_CURL_DATA_SIZE 32767
/* Holds data received from curl lookup */
struct Recv_Data {
struct Recv_Curl_Data {
char data[MAX_RECV_CURL_DATA_SIZE + 1]; /* Data received from curl write data callback */
size_t length; /* Total number of bytes written to data buffer (doesn't include null) */
};
@ -47,4 +47,4 @@ int set_curl_proxy(CURL *c_handle, const char *proxy_address, uint16_t port, uin
*
* Returns size of bytes written to the data buffer.
*/
size_t write_lookup_data(void *data, size_t size, size_t nmemb, void *user_pointer);
size_t curl_cb_write_data(void *data, size_t size, size_t nmemb, void *user_pointer);

View File

@ -192,7 +192,7 @@ static bool get_domain_match(char *pubkey, char *out_domain, size_t out_domain_s
* Returns -1 on failure.
*/
#define ID_PREFIX "\"tox_id\": \""
static int process_response(struct Recv_Data *recv_data)
static int process_response(struct Recv_Curl_Data *recv_data)
{
size_t prefix_size = strlen(ID_PREFIX);
@ -248,8 +248,8 @@ void *lookup_thread_func(void *data)
kill_lookup_thread();
}
struct Recv_Data recv_data;
memset(&recv_data, 0, sizeof(struct Recv_Data));
struct Recv_Curl_Data recv_data;
memset(&recv_data, 0, sizeof(struct Recv_Curl_Data));
char post_data[MAX_STR_SIZE];
snprintf(post_data, sizeof(post_data), "{\"action\": 3, \"name\": \"%s\"}", name);
@ -261,7 +261,7 @@ void *lookup_thread_func(void *data)
curl_easy_setopt(c_handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(c_handle, CURLOPT_URL, real_domain);
curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, write_lookup_data);
curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, curl_cb_write_data);
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_POSTFIELDS, post_data);

View File

@ -62,6 +62,7 @@ static struct ui_strings {
const char* show_typing_other;
const char* show_welcome_msg;
const char* show_connection_msg;
const char* nodeslist_update_freq;
const char* line_join;
const char* line_quit;
@ -88,6 +89,7 @@ static struct ui_strings {
"show_typing_other",
"show_welcome_msg",
"show_connection_msg",
"nodeslist_update_freq",
"line_join",
"line_quit",
"line_alert",
@ -114,6 +116,7 @@ static void ui_defaults(struct user_settings* settings)
settings->show_typing_other = SHOW_TYPING_ON;
settings->show_welcome_msg = SHOW_WELCOME_MSG_ON;
settings->show_connection_msg = SHOW_CONNECTION_MSG_ON;
settings->nodeslist_update_freq = 30;
snprintf(settings->line_join, LINE_HINT_MAX + 1, "%s", LINE_JOIN);
snprintf(settings->line_quit, LINE_HINT_MAX + 1, "%s", LINE_QUIT);
@ -347,12 +350,14 @@ int settings_load(struct user_settings *s, const char *patharg)
config_setting_lookup_bool(setting, ui_strings.autolog, &s->autolog);
config_setting_lookup_bool(setting, ui_strings.native_colors, &s->colour_theme);
config_setting_lookup_int(setting, ui_strings.history_size, &s->history_size);
config_setting_lookup_bool(setting, ui_strings.show_typing_self, &s->show_typing_self);
config_setting_lookup_bool(setting, ui_strings.show_typing_other, &s->show_typing_other);
config_setting_lookup_bool(setting, ui_strings.show_welcome_msg, &s->show_welcome_msg);
config_setting_lookup_bool(setting, ui_strings.show_connection_msg, &s->show_connection_msg);
config_setting_lookup_int(setting, ui_strings.history_size, &s->history_size);
config_setting_lookup_int(setting, ui_strings.nodeslist_update_freq, &s->nodeslist_update_freq);
if ( config_setting_lookup_string(setting, ui_strings.line_join, &str) ) {
snprintf(s->line_join, sizeof(s->line_join), "%s", str);
}

View File

@ -53,6 +53,7 @@ struct user_settings {
int show_typing_other; /* boolean */
int show_welcome_msg; /* boolean */
int show_connection_msg; /* boolean */
int nodeslist_update_freq; /* int (<= 0 to disable updates) */
char line_join[LINE_HINT_MAX + 1];
char line_quit[LINE_HINT_MAX + 1];