summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon/tox_bootstrap_daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/bootstrap_daemon/tox_bootstrap_daemon.c')
-rw-r--r--other/bootstrap_daemon/tox_bootstrap_daemon.c69
1 files changed, 62 insertions, 7 deletions
diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.c b/other/bootstrap_daemon/tox_bootstrap_daemon.c
index 410a3453..6853358a 100644
--- a/other/bootstrap_daemon/tox_bootstrap_daemon.c
+++ b/other/bootstrap_daemon/tox_bootstrap_daemon.c
@@ -48,10 +48,12 @@
48#include "../../toxcore/util.h" 48#include "../../toxcore/util.h"
49 49
50// misc 50// misc
51#include "../bootstrap_node_packets.c"
51#include "../../testing/misc_tools.c" 52#include "../../testing/misc_tools.c"
52 53
53 54
54#define DAEMON_NAME "tox_bootstrap_daemon" 55#define DAEMON_NAME "tox_bootstrap_daemon"
56#define DAEMON_VERSION_NUMBER 2014051700UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day
55 57
56#define SLEEP_TIME_MILLISECONDS 30 58#define SLEEP_TIME_MILLISECONDS 30
57#define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) 59#define sleep usleep(1000*SLEEP_TIME_MILLISECONDS)
@@ -62,6 +64,8 @@
62#define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false 64#define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false
63#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false 65#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false
64#define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false 66#define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false
67#define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false
68#define DEFAULT_MOTD DAEMON_NAME
65 69
66#define MIN_ALLOWED_PORT 1 70#define MIN_ALLOWED_PORT 1
67#define MAX_ALLOWED_PORT 65535 71#define MAX_ALLOWED_PORT 65535
@@ -129,7 +133,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
129 } 133 }
130 134
131 if (config_setting_is_array(ports_array) == CONFIG_FALSE) { 135 if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
132 syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n"); 136 syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", NAME_TCP_RELAY_PORTS);
133 return; 137 return;
134 } 138 }
135 139
@@ -161,7 +165,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
161 165
162 (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); 166 (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem);
163 if ((*tcp_relay_ports)[i] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[i] > MAX_ALLOWED_PORT) { 167 if ((*tcp_relay_ports)[i] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[i] > MAX_ALLOWED_PORT) {
164 syslog(LOG_WARNING, "Port #%d: Invalid port value, should be in [%d, %d]. Skipping.\n", i, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); 168 syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[i], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
165 continue; 169 continue;
166 } 170 }
167 171
@@ -176,12 +180,14 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
176// 180//
177// Important: you are responsible for freeing `pid_file_path` and `keys_file_path` 181// Important: you are responsible for freeing `pid_file_path` and `keys_file_path`
178// also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports` 182// also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports`
183// and also `motd` iff `enable_motd` is set
179// 184//
180// returns 1 on success 185// returns 1 on success
181// 0 on failure, doesn't modify any data pointed by arguments 186// 0 on failure, doesn't modify any data pointed by arguments
182 187
183int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, 188int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6,
184 int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) 189 int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count,
190 int *enable_motd, char **motd)
185{ 191{
186 config_t cfg; 192 config_t cfg;
187 193
@@ -191,6 +197,8 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
191 const char *NAME_ENABLE_IPV6 = "enable_ipv6"; 197 const char *NAME_ENABLE_IPV6 = "enable_ipv6";
192 const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; 198 const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery";
193 const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; 199 const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay";
200 const char *NAME_ENABLE_MOTD = "enable_motd";
201 const char *NAME_MOTD = "motd";
194 202
195 config_init(&cfg); 203 config_init(&cfg);
196 204
@@ -261,6 +269,30 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
261 *tcp_relay_port_count = 0; 269 *tcp_relay_port_count = 0;
262 } 270 }
263 271
272 // Get MOTD option
273 if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
274 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD);
275 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD,
276 DEFAULT_ENABLE_MOTD ? "true" : "false");
277 *enable_motd = DEFAULT_ENABLE_MOTD;
278 }
279
280 if (*enable_motd) {
281 // Get MOTD
282 const char *tmp_motd;
283
284 if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) {
285 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD);
286 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
287 tmp_motd = DEFAULT_MOTD;
288 }
289 size_t tmp_motd_length = strlen(tmp_motd) + 1;
290 size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
291 *motd = malloc(motd_length);
292 strncpy(*motd, tmp_motd, motd_length);
293 (*motd)[MAX_MOTD_LENGTH - 1] = '\0';
294 }
295
264 config_destroy(&cfg); 296 config_destroy(&cfg);
265 297
266 syslog(LOG_DEBUG, "Successfully read:\n"); 298 syslog(LOG_DEBUG, "Successfully read:\n");
@@ -269,6 +301,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
269 syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port); 301 syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port);
270 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); 302 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false");
271 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); 303 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
304
272 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); 305 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
273 // show info about tcp ports only if tcp relay is enabled 306 // show info about tcp ports only if tcp relay is enabled
274 if (*enable_tcp_relay) { 307 if (*enable_tcp_relay) {
@@ -283,6 +316,11 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
283 } 316 }
284 } 317 }
285 318
319 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
320 if (*enable_motd) {
321 syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd);
322 }
323
286 return 1; 324 return 1;
287} 325}
288 326
@@ -415,6 +453,8 @@ int main(int argc, char *argv[])
415{ 453{
416 openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); 454 openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON);
417 455
456 syslog(LOG_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER);
457
418 if (argc < 2) { 458 if (argc < 2) {
419 syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); 459 syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n");
420 return 1; 460 return 1;
@@ -428,8 +468,10 @@ int main(int argc, char *argv[])
428 int enable_tcp_relay; 468 int enable_tcp_relay;
429 uint16_t *tcp_relay_ports; 469 uint16_t *tcp_relay_ports;
430 int tcp_relay_port_count; 470 int tcp_relay_port_count;
471 int enable_motd;
472 char *motd;
431 473
432 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)) { 474 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)) {
433 syslog(LOG_DEBUG, "General config read successfully\n"); 475 syslog(LOG_DEBUG, "General config read successfully\n");
434 } else { 476 } else {
435 syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); 477 syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
@@ -465,8 +507,18 @@ int main(int argc, char *argv[])
465 return 1; 507 return 1;
466 } 508 }
467 509
510 if (enable_motd) {
511 if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t*)motd, strlen(motd) + 1) == 0) {
512 syslog(LOG_DEBUG, "Set MOTD successfully.\n");
513 } else {
514 syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd);
515 return 1;
516 }
517 free(motd);
518 }
519
468 if (manage_keys(dht, keys_file_path)) { 520 if (manage_keys(dht, keys_file_path)) {
469 syslog(LOG_DEBUG, "Keys are managed successfully\n"); 521 syslog(LOG_DEBUG, "Keys are managed successfully.\n");
470 } else { 522 } else {
471 syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); 523 syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path);
472 return 1; 524 return 1;
@@ -485,14 +537,16 @@ int main(int argc, char *argv[])
485 // tcp_relay_port_count != 0 at this point 537 // tcp_relay_port_count != 0 at this point
486 free(tcp_relay_ports); 538 free(tcp_relay_ports);
487 539
488 if (tcp_server == NULL) { 540 if (tcp_server != NULL) {
541 syslog(LOG_DEBUG, "Initialized Tox TCP server successfully.\n");
542 } else {
489 syslog(LOG_ERR, "Couldn't initialize Tox TCP server. Exiting.\n"); 543 syslog(LOG_ERR, "Couldn't initialize Tox TCP server. Exiting.\n");
490 return 1; 544 return 1;
491 } 545 }
492 } 546 }
493 547
494 if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { 548 if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) {
495 syslog(LOG_DEBUG, "List of bootstrap nodes read successfully\n"); 549 syslog(LOG_DEBUG, "List of bootstrap nodes read successfully.\n");
496 } else { 550 } else {
497 syslog(LOG_ERR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path); 551 syslog(LOG_ERR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path);
498 return 1; 552 return 1;
@@ -554,6 +608,7 @@ int main(int argc, char *argv[])
554 608
555 if (enable_lan_discovery) { 609 if (enable_lan_discovery) {
556 LANdiscovery_init(dht); 610 LANdiscovery_init(dht);
611 syslog(LOG_DEBUG, "Initialized LAN discovery.\n");
557 } 612 }
558 613
559 while (1) { 614 while (1) {