Skip to content

Commit 405854e

Browse files
committed
Cleanups.
1 parent c6bed82 commit 405854e

File tree

5 files changed

+50
-43
lines changed

5 files changed

+50
-43
lines changed

other/bootstrap_daemon/src/command_line_arguments.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,27 @@ void print_help()
4141
// make sure all lines fit into 80 columns
4242
// make sure options are listed in alphabetical order
4343
write_log(LOG_LEVEL_INFO,
44-
"Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
45-
"\n"
46-
"Options:\n"
47-
" --config=FILE_PATH Specify path to the config file.\n"
48-
" This is a required option.\n"
49-
" Set FILE_PATH to a path to an empty file in order to\n"
50-
" use default settings.\n"
51-
" --foreground Run the daemon in foreground. The daemon won't fork\n"
52-
" (detach from the terminal) and won't use the PID file.\n"
53-
" --help Print this help message.\n"
54-
" --log-backend=BACKEND Specify which logging backend to use.\n"
55-
" Valid BACKEND values (case sensetive):\n"
56-
" syslog Writes log messages to syslog.\n"
57-
" Default option when no --log-backend is\n"
58-
" specified.\n"
59-
" stdout Writes log messages to stdout/stderr.\n"
60-
" --version Print version information.\n");
44+
"Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
45+
"\n"
46+
"Options:\n"
47+
" --config=FILE_PATH Specify path to the config file.\n"
48+
" This is a required option.\n"
49+
" Set FILE_PATH to a path to an empty file in order to\n"
50+
" use default settings.\n"
51+
" --foreground Run the daemon in foreground. The daemon won't fork\n"
52+
" (detach from the terminal) and won't use the PID file.\n"
53+
" --help Print this help message.\n"
54+
" --log-backend=BACKEND Specify which logging backend to use.\n"
55+
" Valid BACKEND values (case sensetive):\n"
56+
" syslog Writes log messages to syslog.\n"
57+
" Default option when no --log-backend is\n"
58+
" specified.\n"
59+
" stdout Writes log messages to stdout/stderr.\n"
60+
" --version Print version information.\n");
6161
}
6262

63-
void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, bool *run_in_foreground)
63+
void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
64+
bool *run_in_foreground)
6465
{
6566
if (argc < 2) {
6667
write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
@@ -115,19 +116,20 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
115116
print_help();
116117
exit(1);
117118
}
119+
118120
break;
119121

120122
case 'v':
121123
write_log(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
122124
exit(0);
123125

124126
case '?':
125-
write_log(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind-1]);
127+
write_log(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
126128
print_help();
127129
exit(1);
128130

129131
case ':':
130-
write_log(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind-1]);
132+
write_log(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
131133
print_help();
132134
exit(1);
133135
}

other/bootstrap_daemon/src/config.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
7272
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
7373
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
7474
write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
75-
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
75+
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
7676
continue;
7777
}
7878

@@ -92,7 +92,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
9292

9393
if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
9494
write_log(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
95-
NAME_TCP_RELAY_PORTS);
95+
NAME_TCP_RELAY_PORTS);
9696
return;
9797
}
9898

@@ -126,7 +126,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
126126
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
127127
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
128128
write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
129-
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
129+
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
130130
continue;
131131
}
132132

@@ -142,7 +142,8 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
142142
}
143143
}
144144

145-
int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6,
145+
int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port,
146+
int *enable_ipv6,
146147
int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports,
147148
int *tcp_relay_port_count, int *enable_motd, char **motd)
148149
{
@@ -209,23 +210,23 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
209210
if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) {
210211
write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK);
211212
write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK,
212-
DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false");
213+
DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false");
213214
*enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK;
214215
}
215216

216217
// Get LAN discovery option
217218
if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
218219
write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
219220
write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
220-
DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
221+
DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
221222
*enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
222223
}
223224

224225
// Get TCP relay option
225226
if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) {
226227
write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY);
227228
write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY,
228-
DEFAULT_ENABLE_TCP_RELAY ? "true" : "false");
229+
DEFAULT_ENABLE_TCP_RELAY ? "true" : "false");
229230
*enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY;
230231
}
231232

@@ -239,7 +240,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
239240
if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
240241
write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD);
241242
write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD,
242-
DEFAULT_ENABLE_MOTD ? "true" : "false");
243+
DEFAULT_ENABLE_MOTD ? "true" : "false");
243244
*enable_motd = DEFAULT_ENABLE_MOTD;
244245
}
245246

@@ -315,6 +316,7 @@ uint8_t *hex_string_to_bin(char *hex_string)
315316

316317
char *pos = hex_string;
317318
size_t i;
319+
318320
for (i = 0; i < len; ++i, pos += 2) {
319321
sscanf(pos, "%2hhx", &ret[i]);
320322
}
@@ -343,7 +345,8 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
343345
config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES);
344346

345347
if (node_list == NULL) {
346-
write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES);
348+
write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n",
349+
NAME_BOOTSTRAP_NODES);
347350
config_destroy(&cfg);
348351
return 1;
349352
}
@@ -373,7 +376,8 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
373376

374377
// Check that all settings are present
375378
if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) {
376-
write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY);
379+
write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i,
380+
NAME_PUBLIC_KEY);
377381
goto next;
378382
}
379383

@@ -390,13 +394,14 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
390394
// Process settings
391395
if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) {
392396
write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY,
393-
bs_public_key);
397+
bs_public_key);
394398
goto next;
395399
}
396400

397401
if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) {
398-
write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT,
399-
bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
402+
write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i,
403+
NAME_PORT,
404+
bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
400405
goto next;
401406
}
402407

other/bootstrap_daemon/src/log.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ int level_syslog(LOG_LEVEL level)
6868
switch (level) {
6969
case LOG_LEVEL_INFO:
7070
return LOG_INFO;
71+
7172
case LOG_LEVEL_WARNING:
7273
return LOG_WARNING;
74+
7375
case LOG_LEVEL_ERROR:
7476
return LOG_ERR;
7577
}
@@ -80,11 +82,12 @@ void log_syslog(LOG_LEVEL level, const char *format, va_list args)
8082
vsyslog(level_syslog(level), format, args);
8183
}
8284

83-
FILE* level_stdout(LOG_LEVEL level)
85+
FILE *level_stdout(LOG_LEVEL level)
8486
{
8587
switch (level) {
8688
case LOG_LEVEL_INFO:
8789
return stdout;
90+
8891
case LOG_LEVEL_WARNING: // intentional fallthrough
8992
case LOG_LEVEL_ERROR:
9093
return stderr;
@@ -106,6 +109,7 @@ bool write_log(LOG_LEVEL level, const char *format, ...)
106109
case LOG_BACKEND_SYSLOG:
107110
log_syslog(level, format, args);
108111
break;
112+
109113
case LOG_BACKEND_STDOUT:
110114
log_stdout(level, format, args);
111115
break;

other/bootstrap_daemon/src/tox-bootstrapd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
127127

128128
// Open the PID file for writing
129129
pid_file = fopen(pid_file_path, "a+");
130+
130131
if (pid_file == NULL) {
131132
write_log(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path);
132133
exit(1);
@@ -209,7 +210,8 @@ int main(int argc, char *argv[])
209210
}
210211

211212
if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) {
212-
write_log(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
213+
write_log(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT,
214+
MAX_ALLOWED_PORT);
213215
return 1;
214216
}
215217

toxcore/DHT.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@
6565
/* Number of get node requests to send to quickly find close nodes. */
6666
#define MAX_BOOTSTRAP_TIMES 5
6767

68-
static uint8_t calc_dist(uint8_t a, uint8_t b)
69-
{
70-
return a ^ b;
71-
}
72-
7368
/* Compares pk1 and pk2 with pk.
7469
*
7570
* return 0 if both are same distance.
@@ -80,12 +75,11 @@ int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2)
8075
{
8176
size_t i;
8277
uint8_t distance1, distance2;
83-
_Bool d1_abs = 0, d2_abs = 0;
8478

8579
for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
8680

87-
distance1 = calc_dist(pk[i], pk1[i]);
88-
distance2 = calc_dist(pk[i], pk2[i]);
81+
distance1 = pk[i] ^ pk1[i];
82+
distance2 = pk[i] ^ pk2[i];
8983

9084
if (distance1 < distance2)
9185
return 1;

0 commit comments

Comments
 (0)