summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon/tox_bootstrap_daemon.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-06-19 16:23:00 -0400
committerirungentoo <irungentoo@gmail.com>2014-06-19 16:23:00 -0400
commit9b2d3e50b20f4aeaca059b951bd7079c0b554233 (patch)
tree8a712309ad12144aa32ffde32690c3e4362c91aa /other/bootstrap_daemon/tox_bootstrap_daemon.c
parent3c64c87ea7e3b458714533d1df8e742c5d274966 (diff)
parent4c12ee3e3057359cf3a9395fc05214f73ec4b5fe (diff)
Merge branch 'bootstrap_daemon-leaks-1' of https://github.com/tux3/toxcore into tux3-bootstrap_daemon-leaks-1
Diffstat (limited to 'other/bootstrap_daemon/tox_bootstrap_daemon.c')
-rw-r--r--other/bootstrap_daemon/tox_bootstrap_daemon.c62
1 files changed, 41 insertions, 21 deletions
diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.c b/other/bootstrap_daemon/tox_bootstrap_daemon.c
index ceb4fded..5f8f9f76 100644
--- a/other/bootstrap_daemon/tox_bootstrap_daemon.c
+++ b/other/bootstrap_daemon/tox_bootstrap_daemon.c
@@ -91,6 +91,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
91 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); 91 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
92 92
93 if (read_size != KEYS_SIZE) { 93 if (read_size != KEYS_SIZE) {
94 fclose(keys_file);
94 return 0; 95 return 0;
95 } 96 }
96 97
@@ -106,6 +107,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
106 size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); 107 size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
107 108
108 if (write_size != KEYS_SIZE) { 109 if (write_size != KEYS_SIZE) {
110 fclose(keys_file);
109 return 0; 111 return 0;
110 } 112 }
111 } 113 }
@@ -147,8 +149,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
147 for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { 149 for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
148 150
149 (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i]; 151 (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i];
150 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { 152
151 syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); 153 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
154 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
155 syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
156 (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
152 continue; 157 continue;
153 } 158 }
154 159
@@ -162,11 +167,13 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
162 } 167 }
163 168
164 if (config_setting_is_array(ports_array) == CONFIG_FALSE) { 169 if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
165 syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", NAME_TCP_RELAY_PORTS); 170 syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
171 NAME_TCP_RELAY_PORTS);
166 return; 172 return;
167 } 173 }
168 174
169 int config_port_count = config_setting_length(ports_array); 175 int config_port_count = config_setting_length(ports_array);
176
170 if (config_port_count == 0) { 177 if (config_port_count == 0) {
171 syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); 178 syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
172 return; 179 return;
@@ -174,12 +181,10 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
174 181
175 *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t)); 182 *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t));
176 183
177 config_setting_t *elem;
178 int i; 184 int i;
179 185
180 for (i = 0; i < config_port_count; i ++) { 186 for (i = 0; i < config_port_count; i ++) {
181 187 config_setting_t *elem = config_setting_get_elem(ports_array, i);
182 elem = config_setting_get_elem(ports_array, i);
183 188
184 if (elem == NULL) { 189 if (elem == NULL) {
185 // it's NULL if `ports_array` is not an array (we have that check ealier) or if `i` is out of range, which should not be 190 // it's NULL if `ports_array` is not an array (we have that check ealier) or if `i` is out of range, which should not be
@@ -193,8 +198,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
193 } 198 }
194 199
195 (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); 200 (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem);
196 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { 201
197 syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); 202 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
203 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
204 syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
205 (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
198 continue; 206 continue;
199 } 207 }
200 208
@@ -315,6 +323,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
315 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); 323 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
316 tmp_motd = DEFAULT_MOTD; 324 tmp_motd = DEFAULT_MOTD;
317 } 325 }
326
318 size_t tmp_motd_length = strlen(tmp_motd) + 1; 327 size_t tmp_motd_length = strlen(tmp_motd) + 1;
319 size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length; 328 size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
320 *motd = malloc(motd_length); 329 *motd = malloc(motd_length);
@@ -332,6 +341,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
332 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); 341 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
333 342
334 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); 343 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
344
335 // show info about tcp ports only if tcp relay is enabled 345 // show info about tcp ports only if tcp relay is enabled
336 if (*enable_tcp_relay) { 346 if (*enable_tcp_relay) {
337 if (*tcp_relay_port_count == 0) { 347 if (*tcp_relay_port_count == 0) {
@@ -339,6 +349,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
339 } else { 349 } else {
340 syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count); 350 syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count);
341 int i; 351 int i;
352
342 for (i = 0; i < *tcp_relay_port_count; i ++) { 353 for (i = 0; i < *tcp_relay_port_count; i ++) {
343 syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); 354 syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]);
344 } 355 }
@@ -346,6 +357,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
346 } 357 }
347 358
348 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); 359 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
360
349 if (*enable_motd) { 361 if (*enable_motd) {
350 syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd); 362 syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd);
351 } 363 }
@@ -424,14 +436,15 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
424 } 436 }
425 437
426 // Process settings 438 // Process settings
427 if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES*2) { 439 if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) {
428 syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, 440 syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY,
429 bs_public_key); 441 bs_public_key);
430 goto next; 442 goto next;
431 } 443 }
432 444
433 if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { 445 if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) {
434 syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); 446 syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT,
447 bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
435 goto next; 448 goto next;
436 } 449 }
437 450
@@ -464,7 +477,7 @@ next:
464 477
465void print_public_key(uint8_t *public_key) 478void print_public_key(uint8_t *public_key)
466{ 479{
467 char buffer[2*crypto_box_PUBLICKEYBYTES + 1]; 480 char buffer[2 * crypto_box_PUBLICKEYBYTES + 1];
468 int index = 0; 481 int index = 0;
469 482
470 int i; 483 int i;
@@ -500,7 +513,8 @@ int main(int argc, char *argv[])
500 int enable_motd; 513 int enable_motd;
501 char *motd; 514 char *motd;
502 515
503 if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { 516 if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery,
517 &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
504 syslog(LOG_DEBUG, "General config read successfully\n"); 518 syslog(LOG_DEBUG, "General config read successfully\n");
505 } else { 519 } else {
506 syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); 520 syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
@@ -513,8 +527,11 @@ int main(int argc, char *argv[])
513 } 527 }
514 528
515 // Check if the PID file exists 529 // Check if the PID file exists
516 if (fopen(pid_file_path, "r")) { 530 FILE *pid_file;
531
532 if (pid_file = fopen(pid_file_path, "r")) {
517 syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); 533 syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path);
534 fclose(pid_file);
518 } 535 }
519 536
520 IP ip; 537 IP ip;
@@ -536,12 +553,13 @@ int main(int argc, char *argv[])
536 } 553 }
537 554
538 if (enable_motd) { 555 if (enable_motd) {
539 if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t*)motd, strlen(motd) + 1) == 0) { 556 if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
540 syslog(LOG_DEBUG, "Set MOTD successfully.\n"); 557 syslog(LOG_DEBUG, "Set MOTD successfully.\n");
541 } else { 558 } else {
542 syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd); 559 syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd);
543 return 1; 560 return 1;
544 } 561 }
562
545 free(motd); 563 free(motd);
546 } 564 }
547 565
@@ -560,7 +578,8 @@ int main(int argc, char *argv[])
560 return 1; 578 return 1;
561 } 579 }
562 580
563 tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key, dht->self_secret_key, onion); 581 tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key,
582 dht->self_secret_key, onion);
564 583
565 // tcp_relay_port_count != 0 at this point 584 // tcp_relay_port_count != 0 at this point
566 free(tcp_relay_ports); 585 free(tcp_relay_ports);
@@ -596,17 +615,18 @@ int main(int argc, char *argv[])
596 // Fork off from the parent process 615 // Fork off from the parent process
597 pid_t pid = fork(); 616 pid_t pid = fork();
598 617
599 if (pid < 0) {
600 fclose(pidf);
601 syslog(LOG_ERR, "Forking failed. Exiting.\n");
602 return 1;
603 }
604
605 if (pid > 0) { 618 if (pid > 0) {
606 fprintf(pidf, "%d ", pid); 619 fprintf(pidf, "%d ", pid);
607 fclose(pidf); 620 fclose(pidf);
608 syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid); 621 syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid);
609 return 0; 622 return 0;
623 } else {
624 fclose(pidf);
625 }
626
627 if (pid < 0) {
628 syslog(LOG_ERR, "Forking failed. Exiting.\n");
629 return 1;
610 } 630 }
611 631
612 // Change the file mode mask 632 // Change the file mode mask