summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2016-01-04 22:48:58 -0500
committerirungentoo <irungentoo@gmail.com>2016-01-04 22:48:58 -0500
commit405854e1e70a22d042cd0e544d1460055b65d57a (patch)
treee5cc9fc5fda064fa57df467bf949742852f283ac /other/bootstrap_daemon
parentc6bed82d47aa39709bdd568fccc3847714ed1b74 (diff)
Cleanups.
Diffstat (limited to 'other/bootstrap_daemon')
-rw-r--r--other/bootstrap_daemon/src/command_line_arguments.c42
-rw-r--r--other/bootstrap_daemon/src/config.c31
-rw-r--r--other/bootstrap_daemon/src/log.c6
-rw-r--r--other/bootstrap_daemon/src/tox-bootstrapd.c4
4 files changed, 48 insertions, 35 deletions
diff --git a/other/bootstrap_daemon/src/command_line_arguments.c b/other/bootstrap_daemon/src/command_line_arguments.c
index 187fa786..ecfef1ad 100644
--- a/other/bootstrap_daemon/src/command_line_arguments.c
+++ b/other/bootstrap_daemon/src/command_line_arguments.c
@@ -41,26 +41,27 @@ void print_help()
41 // make sure all lines fit into 80 columns 41 // make sure all lines fit into 80 columns
42 // make sure options are listed in alphabetical order 42 // make sure options are listed in alphabetical order
43 write_log(LOG_LEVEL_INFO, 43 write_log(LOG_LEVEL_INFO,
44 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n" 44 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
45 "\n" 45 "\n"
46 "Options:\n" 46 "Options:\n"
47 " --config=FILE_PATH Specify path to the config file.\n" 47 " --config=FILE_PATH Specify path to the config file.\n"
48 " This is a required option.\n" 48 " This is a required option.\n"
49 " Set FILE_PATH to a path to an empty file in order to\n" 49 " Set FILE_PATH to a path to an empty file in order to\n"
50 " use default settings.\n" 50 " use default settings.\n"
51 " --foreground Run the daemon in foreground. The daemon won't fork\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" 52 " (detach from the terminal) and won't use the PID file.\n"
53 " --help Print this help message.\n" 53 " --help Print this help message.\n"
54 " --log-backend=BACKEND Specify which logging backend to use.\n" 54 " --log-backend=BACKEND Specify which logging backend to use.\n"
55 " Valid BACKEND values (case sensetive):\n" 55 " Valid BACKEND values (case sensetive):\n"
56 " syslog Writes log messages to syslog.\n" 56 " syslog Writes log messages to syslog.\n"
57 " Default option when no --log-backend is\n" 57 " Default option when no --log-backend is\n"
58 " specified.\n" 58 " specified.\n"
59 " stdout Writes log messages to stdout/stderr.\n" 59 " stdout Writes log messages to stdout/stderr.\n"
60 " --version Print version information.\n"); 60 " --version Print version information.\n");
61} 61}
62 62
63void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, bool *run_in_foreground) 63void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
64 bool *run_in_foreground)
64{ 65{
65 if (argc < 2) { 66 if (argc < 2) {
66 write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n"); 67 write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
@@ -115,6 +116,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
115 print_help(); 116 print_help();
116 exit(1); 117 exit(1);
117 } 118 }
119
118 break; 120 break;
119 121
120 case 'v': 122 case 'v':
@@ -122,12 +124,12 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
122 exit(0); 124 exit(0);
123 125
124 case '?': 126 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]);
126 print_help(); 128 print_help();
127 exit(1); 129 exit(1);
128 130
129 case ':': 131 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]);
131 print_help(); 133 print_help();
132 exit(1); 134 exit(1);
133 } 135 }
diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c
index 861a3803..12ae693d 100644
--- a/other/bootstrap_daemon/src/config.c
+++ b/other/bootstrap_daemon/src/config.c
@@ -72,7 +72,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
72 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT 72 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
73 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { 73 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
74 write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, 74 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);
76 continue; 76 continue;
77 } 77 }
78 78
@@ -92,7 +92,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
92 92
93 if (config_setting_is_array(ports_array) == CONFIG_FALSE) { 93 if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
94 write_log(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", 94 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);
96 return; 96 return;
97 } 97 }
98 98
@@ -126,7 +126,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
126 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT 126 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
127 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { 127 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
128 write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, 128 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);
130 continue; 130 continue;
131 } 131 }
132 132
@@ -142,7 +142,8 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
142 } 142 }
143} 143}
144 144
145int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, 145int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port,
146 int *enable_ipv6,
146 int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, 147 int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports,
147 int *tcp_relay_port_count, int *enable_motd, char **motd) 148 int *tcp_relay_port_count, int *enable_motd, char **motd)
148{ 149{
@@ -209,7 +210,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
209 if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { 210 if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) {
210 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); 211 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK);
211 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, 212 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");
213 *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; 214 *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK;
214 } 215 }
215 216
@@ -217,7 +218,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
217 if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { 218 if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
218 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); 219 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
219 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, 220 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");
221 *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; 222 *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
222 } 223 }
223 224
@@ -225,7 +226,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
225 if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { 226 if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) {
226 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); 227 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY);
227 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, 228 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");
229 *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; 230 *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY;
230 } 231 }
231 232
@@ -239,7 +240,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
239 if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { 240 if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
240 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); 241 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD);
241 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, 242 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");
243 *enable_motd = DEFAULT_ENABLE_MOTD; 244 *enable_motd = DEFAULT_ENABLE_MOTD;
244 } 245 }
245 246
@@ -315,6 +316,7 @@ uint8_t *hex_string_to_bin(char *hex_string)
315 316
316 char *pos = hex_string; 317 char *pos = hex_string;
317 size_t i; 318 size_t i;
319
318 for (i = 0; i < len; ++i, pos += 2) { 320 for (i = 0; i < len; ++i, pos += 2) {
319 sscanf(pos, "%2hhx", &ret[i]); 321 sscanf(pos, "%2hhx", &ret[i]);
320 } 322 }
@@ -343,7 +345,8 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
343 config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES); 345 config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES);
344 346
345 if (node_list == NULL) { 347 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);
347 config_destroy(&cfg); 350 config_destroy(&cfg);
348 return 1; 351 return 1;
349 } 352 }
@@ -373,7 +376,8 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
373 376
374 // Check that all settings are present 377 // Check that all settings are present
375 if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { 378 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);
377 goto next; 381 goto next;
378 } 382 }
379 383
@@ -390,13 +394,14 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
390 // Process settings 394 // Process settings
391 if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) { 395 if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) {
392 write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, 396 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);
394 goto next; 398 goto next;
395 } 399 }
396 400
397 if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { 401 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, 402 write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i,
399 bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); 403 NAME_PORT,
404 bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
400 goto next; 405 goto next;
401 } 406 }
402 407
diff --git a/other/bootstrap_daemon/src/log.c b/other/bootstrap_daemon/src/log.c
index d441b98e..a0ac20d9 100644
--- a/other/bootstrap_daemon/src/log.c
+++ b/other/bootstrap_daemon/src/log.c
@@ -68,8 +68,10 @@ int level_syslog(LOG_LEVEL level)
68 switch (level) { 68 switch (level) {
69 case LOG_LEVEL_INFO: 69 case LOG_LEVEL_INFO:
70 return LOG_INFO; 70 return LOG_INFO;
71
71 case LOG_LEVEL_WARNING: 72 case LOG_LEVEL_WARNING:
72 return LOG_WARNING; 73 return LOG_WARNING;
74
73 case LOG_LEVEL_ERROR: 75 case LOG_LEVEL_ERROR:
74 return LOG_ERR; 76 return LOG_ERR;
75 } 77 }
@@ -80,11 +82,12 @@ void log_syslog(LOG_LEVEL level, const char *format, va_list args)
80 vsyslog(level_syslog(level), format, args); 82 vsyslog(level_syslog(level), format, args);
81} 83}
82 84
83FILE* level_stdout(LOG_LEVEL level) 85FILE *level_stdout(LOG_LEVEL level)
84{ 86{
85 switch (level) { 87 switch (level) {
86 case LOG_LEVEL_INFO: 88 case LOG_LEVEL_INFO:
87 return stdout; 89 return stdout;
90
88 case LOG_LEVEL_WARNING: // intentional fallthrough 91 case LOG_LEVEL_WARNING: // intentional fallthrough
89 case LOG_LEVEL_ERROR: 92 case LOG_LEVEL_ERROR:
90 return stderr; 93 return stderr;
@@ -106,6 +109,7 @@ bool write_log(LOG_LEVEL level, const char *format, ...)
106 case LOG_BACKEND_SYSLOG: 109 case LOG_BACKEND_SYSLOG:
107 log_syslog(level, format, args); 110 log_syslog(level, format, args);
108 break; 111 break;
112
109 case LOG_BACKEND_STDOUT: 113 case LOG_BACKEND_STDOUT:
110 log_stdout(level, format, args); 114 log_stdout(level, format, args);
111 break; 115 break;
diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c
index e252a37d..27a910f3 100644
--- a/other/bootstrap_daemon/src/tox-bootstrapd.c
+++ b/other/bootstrap_daemon/src/tox-bootstrapd.c
@@ -127,6 +127,7 @@ void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
127 127
128 // Open the PID file for writing 128 // Open the PID file for writing
129 pid_file = fopen(pid_file_path, "a+"); 129 pid_file = fopen(pid_file_path, "a+");
130
130 if (pid_file == NULL) { 131 if (pid_file == NULL) {
131 write_log(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path); 132 write_log(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path);
132 exit(1); 133 exit(1);
@@ -209,7 +210,8 @@ int main(int argc, char *argv[])
209 } 210 }
210 211
211 if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) { 212 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);
213 return 1; 215 return 1;
214 } 216 }
215 217