Skip to content

Commit 5752fc2

Browse files
committed
refactor: Make tox-bootstrapd use bool instead of int
A continuation of the cleanup done in b7404f2. tox-bootrstrapd historically had used ints for boolean values, as it was initially written in C89 which has no stdbool.h. Since then it has modernized and moved on to using C11, but the usage of the int type to represent boolean values, "boolean ints", remained. Recently, driven by a desire to eliminate implicit int-to-bool conversion, @iphydf did a cleanup in b7404f2, changing some of the boolean ints to bools and doing manual int-to-bool conversion on the remaining boolean ints. This left the codebase in an inconsistent state of both ints and bools now being used to represent boolean values, not to mention that the explicit int-to-bool conversions are a bit ugly. The only boolean ints that remained are those stemming from libconfig's config_lookup_bool() taking an *int parameter to return a boolean value, as libconfig still uses C89. This commit adds a wrapper function around libconfig's config_lookup_bool() that takes a *bool instead, eliminating the remaining boolean ints and majority of the explicit int-to-bool conversions in tox-bootstrapd.
1 parent df67578 commit 5752fc2

File tree

4 files changed

+56
-43
lines changed

4 files changed

+56
-43
lines changed

other/bootstrap_daemon/src/config.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,20 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
138138
}
139139
}
140140

141+
// A wrapper function that actually takes a bool argument
142+
static int tox_config_lookup_bool(const config_t *config, const char *path, bool *bool_value)
143+
{
144+
int int_value = 0;
145+
if (config_lookup_bool(config, path, &int_value) == CONFIG_FALSE) {
146+
return CONFIG_FALSE;
147+
}
148+
*bool_value = int_value != 0;
149+
return CONFIG_TRUE;
150+
}
151+
141152
bool get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port,
142-
int *enable_ipv6, int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay,
143-
uint16_t **tcp_relay_ports, int *tcp_relay_port_count, int *enable_motd, char **motd)
153+
bool *enable_ipv6, bool *enable_ipv4_fallback, bool *enable_lan_discovery, bool *enable_tcp_relay,
154+
uint16_t **tcp_relay_ports, int *tcp_relay_port_count, bool *enable_motd, char **motd)
144155
{
145156
config_t cfg;
146157

@@ -207,51 +218,51 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char **
207218
memcpy(*keys_file_path, tmp_keys_file, keys_file_path_len);
208219

209220
// Get IPv6 option
210-
if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) {
221+
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) {
211222
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6);
212223
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false");
213224
*enable_ipv6 = DEFAULT_ENABLE_IPV6;
214225
}
215226

216227
// Get IPv4 fallback option
217-
if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) {
228+
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) {
218229
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK);
219230
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK,
220231
DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false");
221232
*enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK;
222233
}
223234

224235
// Get LAN discovery option
225-
if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
236+
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
226237
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
227238
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
228239
DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
229240
*enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
230241
}
231242

232243
// Get TCP relay option
233-
if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) {
244+
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) {
234245
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY);
235246
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY,
236247
DEFAULT_ENABLE_TCP_RELAY ? "true" : "false");
237248
*enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY;
238249
}
239250

240-
if (*enable_tcp_relay != 0) {
251+
if (*enable_tcp_relay) {
241252
parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count);
242253
} else {
243254
*tcp_relay_port_count = 0;
244255
}
245256

246257
// Get MOTD option
247-
if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
258+
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
248259
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD);
249260
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD,
250261
DEFAULT_ENABLE_MOTD ? "true" : "false");
251262
*enable_motd = DEFAULT_ENABLE_MOTD;
252263
}
253264

254-
if (*enable_motd != 0) {
265+
if (*enable_motd) {
255266
// Get MOTD
256267
const char *tmp_motd;
257268

@@ -273,14 +284,14 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char **
273284
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path);
274285
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path);
275286
log_write(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port);
276-
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 != 0 ? "true" : "false");
277-
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback != 0 ? "true" : "false");
278-
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery != 0 ? "true" : "false");
287+
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false");
288+
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false");
289+
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
279290

280-
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay != 0 ? "true" : "false");
291+
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
281292

282293
// Show info about tcp ports only if tcp relay is enabled
283-
if (*enable_tcp_relay != 0) {
294+
if (*enable_tcp_relay) {
284295
if (*tcp_relay_port_count == 0) {
285296
log_write(LOG_LEVEL_ERROR, "No TCP ports could be read.\n");
286297
} else {
@@ -292,9 +303,9 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char **
292303
}
293304
}
294305

295-
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd != 0 ? "true" : "false");
306+
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
296307

297-
if (*enable_motd != 0) {
308+
if (*enable_motd) {
298309
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd);
299310
}
300311

other/bootstrap_daemon/src/config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
*
1818
* Important: You are responsible for freeing `pid_file_path` and `keys_file_path`
1919
* also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports`
20-
* and also `motd` iff `enable_motd` is set.
20+
* and also `motd` iff `enable_motd` is true.
2121
*
2222
* @return true on success,
2323
* false on failure, doesn't modify any data pointed by arguments.
2424
*/
2525
bool get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port,
26-
int *enable_ipv6, int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay,
27-
uint16_t **tcp_relay_ports, int *tcp_relay_port_count, int *enable_motd, char **motd);
26+
bool *enable_ipv6, bool *enable_ipv4_fallback, bool *enable_lan_discovery, bool *enable_tcp_relay,
27+
uint16_t **tcp_relay_ports, int *tcp_relay_port_count, bool *enable_motd, char **motd);
2828

2929
/**
3030
* Bootstraps off nodes listed in the config file.
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: GPL-3.0-or-later
2-
* Copyright © 2016-2023 The TokTok team.
2+
* Copyright © 2016-2024 The TokTok team.
33
* Copyright © 2014-2016 Tox project.
44
*/
55

@@ -10,17 +10,19 @@
1010
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_DEFAULTS_H
1111
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_DEFAULTS_H
1212

13+
#include <stdbool.h>
14+
1315
#include "global.h"
1416

1517
#define DEFAULT_PID_FILE_PATH "tox-bootstrapd.pid"
1618
#define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys"
1719
#define DEFAULT_PORT 33445
18-
#define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false
19-
#define DEFAULT_ENABLE_IPV4_FALLBACK 1 // 1 - true, 0 - false
20-
#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false
21-
#define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false
20+
#define DEFAULT_ENABLE_IPV6 true
21+
#define DEFAULT_ENABLE_IPV4_FALLBACK true
22+
#define DEFAULT_ENABLE_LAN_DISCOVERY true
23+
#define DEFAULT_ENABLE_TCP_RELAY true
2224
#define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports
23-
#define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false
25+
#define DEFAULT_ENABLE_MOTD true
2426
#define DEFAULT_MOTD DAEMON_NAME
2527

2628
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_DEFAULTS_H

other/bootstrap_daemon/src/tox-bootstrapd.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: GPL-3.0-or-later
2-
* Copyright © 2016-2018 The TokTok team.
2+
* Copyright © 2016-2024 The TokTok team.
33
* Copyright © 2014-2016 Tox project.
44
*/
55

@@ -240,13 +240,13 @@ int main(int argc, char *argv[])
240240
char *pid_file_path = nullptr;
241241
char *keys_file_path = nullptr;
242242
int start_port = 0;
243-
int enable_ipv6 = 0;
244-
int enable_ipv4_fallback = 0;
245-
int enable_lan_discovery = 0;
246-
int enable_tcp_relay = 0;
243+
bool enable_ipv6 = false;
244+
bool enable_ipv4_fallback = false;
245+
bool enable_lan_discovery = false;
246+
bool enable_tcp_relay = false;
247247
uint16_t *tcp_relay_ports = nullptr;
248248
int tcp_relay_port_count = 0;
249-
int enable_motd = 0;
249+
bool enable_motd = false;
250250
char *motd = nullptr;
251251

252252
if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &start_port, &enable_ipv6, &enable_ipv4_fallback,
@@ -281,7 +281,7 @@ int main(int argc, char *argv[])
281281
free(pid_file_path);
282282

283283
IP ip;
284-
ip_init(&ip, enable_ipv6 != 0);
284+
ip_init(&ip, enable_ipv6);
285285

286286
Logger *logger = logger_new();
287287

@@ -296,10 +296,10 @@ int main(int argc, char *argv[])
296296
Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
297297

298298
if (net == nullptr) {
299-
if (enable_ipv6 != 0 && enable_ipv4_fallback != 0) {
299+
if (enable_ipv6 && enable_ipv4_fallback) {
300300
log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
301-
enable_ipv6 = 0;
302-
ip_init(&ip, enable_ipv6 != 0);
301+
enable_ipv6 = false;
302+
ip_init(&ip, enable_ipv6);
303303
net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
304304

305305
if (net == nullptr) {
@@ -334,7 +334,7 @@ int main(int argc, char *argv[])
334334

335335
mono_time_update(mono_time);
336336

337-
DHT *const dht = new_dht(logger, mem, rng, ns, mono_time, net, true, enable_lan_discovery != 0);
337+
DHT *const dht = new_dht(logger, mem, rng, ns, mono_time, net, true, enable_lan_discovery);
338338

339339
if (dht == nullptr) {
340340
log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n");
@@ -429,7 +429,7 @@ int main(int argc, char *argv[])
429429

430430
gca_onion_init(group_announce, onion_a);
431431

432-
if (enable_motd != 0) {
432+
if (enable_motd) {
433433
if (bootstrap_set_callbacks(dht_get_net(dht), DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
434434
log_write(LOG_LEVEL_INFO, "Set MOTD successfully.\n");
435435
free(motd);
@@ -472,7 +472,7 @@ int main(int argc, char *argv[])
472472

473473
TCP_Server *tcp_server = nullptr;
474474

475-
if (enable_tcp_relay != 0) {
475+
if (enable_tcp_relay) {
476476
if (tcp_relay_port_count == 0) {
477477
log_write(LOG_LEVEL_ERROR, "No TCP relay ports read. Exiting.\n");
478478
kill_onion_announce(onion_a);
@@ -488,7 +488,7 @@ int main(int argc, char *argv[])
488488
return 1;
489489
}
490490

491-
tcp_server = new_tcp_server(logger, mem, rng, ns, enable_ipv6 != 0,
491+
tcp_server = new_tcp_server(logger, mem, rng, ns, enable_ipv6,
492492
tcp_relay_port_count, tcp_relay_ports,
493493
dht_get_self_secret_key(dht), onion, forwarding);
494494

@@ -535,7 +535,7 @@ int main(int argc, char *argv[])
535535
}
536536
}
537537

538-
if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6 != 0)) {
538+
if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) {
539539
log_write(LOG_LEVEL_INFO, "List of bootstrap nodes read successfully.\n");
540540
} else {
541541
log_write(LOG_LEVEL_ERROR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path);
@@ -561,7 +561,7 @@ int main(int argc, char *argv[])
561561

562562
Broadcast_Info *broadcast = nullptr;
563563

564-
if (enable_lan_discovery != 0) {
564+
if (enable_lan_discovery) {
565565
broadcast = lan_discovery_init(ns);
566566
log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n");
567567
}
@@ -589,12 +589,12 @@ int main(int argc, char *argv[])
589589

590590
do_dht(dht);
591591

592-
if (enable_lan_discovery != 0 && mono_time_is_timeout(mono_time, last_lan_discovery, LAN_DISCOVERY_INTERVAL)) {
592+
if (enable_lan_discovery && mono_time_is_timeout(mono_time, last_lan_discovery, LAN_DISCOVERY_INTERVAL)) {
593593
lan_discovery_send(dht_get_net(dht), broadcast, dht_get_self_public_key(dht), net_htons_port);
594594
last_lan_discovery = mono_time_get(mono_time);
595595
}
596596

597-
if (enable_tcp_relay != 0) {
597+
if (enable_tcp_relay) {
598598
do_tcp_server(tcp_server, mono_time);
599599
}
600600

0 commit comments

Comments
 (0)