Skip to content

Commit

Permalink
refactor: the run options object is no longer global
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Jan 23, 2024
1 parent b435a71 commit b81831b
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 143 deletions.
29 changes: 16 additions & 13 deletions src/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static bool nodeslist_needs_update(const char *nodes_path, int update_frequency)
* Return 0 on success.
* Return -1 on failure.
*/
static int curl_fetch_nodes_JSON(struct Recv_Curl_Data *recv_data)
static int curl_fetch_nodes_JSON(const Run_Options *run_opts, struct Recv_Curl_Data *recv_data)
{
CURL *c_handle = curl_easy_init();

Expand Down Expand Up @@ -252,7 +252,7 @@ static int curl_fetch_nodes_JSON(struct Recv_Curl_Data *recv_data)
goto on_exit;
}

int proxy_ret = set_curl_proxy(c_handle, arg_opts.proxy_address, arg_opts.proxy_port, arg_opts.proxy_type);
int proxy_ret = set_curl_proxy(c_handle, run_opts->proxy_address, run_opts->proxy_port, run_opts->proxy_type);

if (proxy_ret != 0) {
fprintf(stderr, "set_curl_proxy() failed with error %d\n", proxy_ret);
Expand Down Expand Up @@ -312,7 +312,7 @@ static int curl_fetch_nodes_JSON(struct Recv_Curl_Data *recv_data)
* Return -4 if data could not be written to disk.
* Return -5 if memory allocation fails.
*/
static int update_DHT_nodeslist(const char *nodes_path, int update_frequency)
static int update_DHT_nodeslist(const Run_Options *run_opts, const char *nodes_path, int update_frequency)
{
if (!nodeslist_needs_update(nodes_path, update_frequency)) {
return 0;
Expand All @@ -331,7 +331,7 @@ static int update_DHT_nodeslist(const char *nodes_path, int update_frequency)
return -5;
}

if (curl_fetch_nodes_JSON(recv_data) == -1) {
if (curl_fetch_nodes_JSON(run_opts, recv_data) == -1) {
free(recv_data);
fclose(fp);
return -2;
Expand All @@ -355,12 +355,12 @@ static int update_DHT_nodeslist(const char *nodes_path, int update_frequency)
return 1;
}

static void get_nodeslist_path(char *buf, size_t buf_size)
static void get_nodeslist_path(const Run_Options *run_opts, 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);
if (!string_is_empty(run_opts->nodes_path)) {
snprintf(buf, buf_size, "%s", run_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);
Expand Down Expand Up @@ -511,14 +511,15 @@ static int extract_node(const char *line, struct Node *node)
/* Loads the DHT nodeslist to memory from json encoded nodes file. */
void *load_nodeslist_thread(void *data)
{
const Client_Config *c_config = (Client_Config *) data;

if (c_config == NULL) {
const Toxic *toxic = (Toxic *) data;

if (toxic == NULL) {
goto on_exit;
}

char nodes_path[PATH_MAX];
get_nodeslist_path(nodes_path, sizeof(nodes_path));
get_nodeslist_path(toxic->run_opts, nodes_path, sizeof(nodes_path));

FILE *fp = NULL;

Expand All @@ -532,7 +533,9 @@ void *load_nodeslist_thread(void *data)
goto on_exit;
}

const int update_err = update_DHT_nodeslist(nodes_path, c_config->nodeslist_update_freq);
const Client_Config *c_config = toxic->c_config;

const int update_err = update_DHT_nodeslist(toxic->run_opts, nodes_path, c_config->nodeslist_update_freq);

if (update_err < 0) {
fprintf(stderr, "update_DHT_nodeslist() failed with error %d\n", update_err);
Expand Down Expand Up @@ -600,7 +603,7 @@ void *load_nodeslist_thread(void *data)
* Return -4 if pthread fails to set detached state.
* Return -5 if thread creation fails.
*/
int load_DHT_nodeslist(const Client_Config *c_config)
int load_DHT_nodeslist(const Toxic *toxic)
{
if (thread_data.active) {
return -1;
Expand All @@ -620,7 +623,7 @@ int load_DHT_nodeslist(const Client_Config *c_config)

thread_data.active = true;

if (pthread_create(&thread_data.tid, &thread_data.attr, load_nodeslist_thread, (void *) c_config) != 0) {
if (pthread_create(&thread_data.tid, &thread_data.attr, load_nodeslist_thread, (void *) toxic) != 0) {
thread_data.active = false;
return -5;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ void do_tox_connection(Tox *tox);
* Return -4 if pthread fails to set detached state.
* Return -5 if thread creation fails.
*/
int load_DHT_nodeslist(const Client_Config *c_config);
int load_DHT_nodeslist(const Toxic *toxic);

#endif /* BOOTSTRAP_H */
2 changes: 1 addition & 1 deletion src/misc_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ int char_rfind(const char *s, char ch, int len);
/* Converts bytes to appropriate unit and puts in buf as a string */
void bytes_convert_str(char *buf, int size, uint64_t bytes);

/* checks if a file exists. Returns true or false */
/* Returns true if the file pointed to by `path` exists. */
bool file_exists(const char *path);

/*
Expand Down
27 changes: 10 additions & 17 deletions src/name_lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ static struct Nameservers {
} Nameservers;

static struct thread_data {
Toxic *toxic;
ToxWindow *self;
char id_bin[TOX_ADDRESS_SIZE];
char addr[MAX_STR_SIZE];
Expand Down Expand Up @@ -240,12 +239,15 @@ static int process_response(struct Recv_Curl_Data *recv_data)

void *lookup_thread_func(void *data)
{
const Client_Config *c_config = (Client_Config *) data;
Toxic *toxic = (Toxic *) data;

if (c_config == NULL) {
if (toxic == NULL) {
kill_lookup_thread();
}

const Client_Config *c_config = toxic->c_config;
const Run_Options *run_opts = toxic->run_opts;

ToxWindow *self = t_data.self;

char input_domain[MAX_STR_SIZE];
Expand Down Expand Up @@ -335,7 +337,7 @@ void *lookup_thread_func(void *data)
goto on_exit;
}

int proxy_ret = set_curl_proxy(c_handle, arg_opts.proxy_address, arg_opts.proxy_port, arg_opts.proxy_type);
int proxy_ret = set_curl_proxy(c_handle, run_opts->proxy_address, run_opts->proxy_port, run_opts->proxy_type);

if (proxy_ret != 0) {
lookup_error(self, c_config, "Failed to set proxy (error %d)\n", proxy_ret);
Expand Down Expand Up @@ -390,7 +392,7 @@ void *lookup_thread_func(void *data)
}

pthread_mutex_lock(&Winthread.lock);
cmd_add_helper(self, t_data.toxic, t_data.id_bin, t_data.msg);
cmd_add_helper(self, toxic, t_data.id_bin, t_data.msg);
pthread_mutex_unlock(&Winthread.lock);

on_exit:
Expand Down Expand Up @@ -425,7 +427,6 @@ bool name_lookup(ToxWindow *self, Toxic *toxic, const char *id_bin, const char *
snprintf(t_data.addr, sizeof(t_data.addr), "%s", addr);
snprintf(t_data.msg, sizeof(t_data.msg), "%s", message);
t_data.self = self;
t_data.toxic = toxic;
t_data.busy = true;

if (pthread_attr_init(&lookup_thread.attr) != 0) {
Expand All @@ -441,7 +442,7 @@ bool name_lookup(ToxWindow *self, Toxic *toxic, const char *id_bin, const char *
return false;
}

if (pthread_create(&lookup_thread.tid, &lookup_thread.attr, lookup_thread_func, (void *) c_config) != 0) {
if (pthread_create(&lookup_thread.tid, &lookup_thread.attr, lookup_thread_func, (void *) toxic) != 0) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread failed to init");
pthread_attr_destroy(&lookup_thread.attr);
clear_thread_data();
Expand All @@ -451,22 +452,14 @@ bool name_lookup(ToxWindow *self, Toxic *toxic, const char *id_bin, const char *
return true;
}

/* Initializes http based name lookups. Note: This function must be called only once before additional
* threads are spawned.
*
* Returns 0 on success.
* Returns -1 if curl failed to init.
* Returns -2 if the nameserver list cannot be found.
* Returns -3 if the nameserver list does not contain any valid entries.
*/
int name_lookup_init(int curl_init_status)
int name_lookup_init(const char *nameserver_path, int curl_init_status)
{
if (curl_init_status != 0) {
t_data.disabled = true;
return -1;
}

const char *path = arg_opts.nameserver_path[0] ? arg_opts.nameserver_path : PACKAGE_DATADIR "/nameservers";
const char *path = !string_is_empty(nameserver_path) ? nameserver_path : PACKAGE_DATADIR "/nameservers";
const int ret = load_nameserver_list(path);

if (ret != 0) {
Expand Down
11 changes: 6 additions & 5 deletions src/name_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
#ifndef NAME_LOOKUP
#define NAME_LOOKUP

/* Initializes http based name lookups.
*
* Note: This function must be called only once before additional threads are spawned.
/* Initializes http based name lookups. Note: This function must be called only once before additional
* threads are spawned.
*
* Returns 0 on success.
* Returns -1 on failure.
* Returns -1 if curl failed to init.
* Returns -2 if the nameserver list cannot be found.
* Returns -3 if the nameserver list does not contain any valid entries.
*/
int name_lookup_init(int curl_init_status);
int name_lookup_init(const char *nameserver_path, int curl_init_status);

/* Attempts to do a tox name lookup.
*
Expand Down
Loading

0 comments on commit b81831b

Please sign in to comment.