diff options
Diffstat (limited to 'other/bootstrap_daemon/src/tox-bootstrapd.c')
-rw-r--r-- | other/bootstrap_daemon/src/tox-bootstrapd.c | 399 |
1 files changed, 2 insertions, 397 deletions
diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index a8098e41..d4191e80 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c | |||
@@ -34,9 +34,6 @@ | |||
34 | #include <stdlib.h> | 34 | #include <stdlib.h> |
35 | #include <string.h> | 35 | #include <string.h> |
36 | 36 | ||
37 | // 3rd party | ||
38 | #include <libconfig.h> | ||
39 | |||
40 | // ./configure | 37 | // ./configure |
41 | #ifdef HAVE_CONFIG_H | 38 | #ifdef HAVE_CONFIG_H |
42 | #include "config.h" | 39 | #include "config.h" |
@@ -49,9 +46,10 @@ | |||
49 | #include "../../toxcore/util.h" | 46 | #include "../../toxcore/util.h" |
50 | 47 | ||
51 | // misc | 48 | // misc |
52 | #include "../bootstrap_node_packets.c" | 49 | #include "../bootstrap_node_packets.h" |
53 | #include "../../testing/misc_tools.c" | 50 | #include "../../testing/misc_tools.c" |
54 | 51 | ||
52 | #include "config.h" | ||
55 | #include "global.h" | 53 | #include "global.h" |
56 | #include "log.h" | 54 | #include "log.h" |
57 | 55 | ||
@@ -59,22 +57,6 @@ | |||
59 | #define SLEEP_TIME_MILLISECONDS 30 | 57 | #define SLEEP_TIME_MILLISECONDS 30 |
60 | #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) | 58 | #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) |
61 | 59 | ||
62 | #define DEFAULT_PID_FILE_PATH "tox-bootstrapd.pid" | ||
63 | #define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys" | ||
64 | #define DEFAULT_PORT 33445 | ||
65 | #define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false | ||
66 | #define DEFAULT_ENABLE_IPV4_FALLBACK 1 // 1 - true, 0 - false | ||
67 | #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false | ||
68 | #define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false | ||
69 | #define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports. make sure to adjust DEFAULT_TCP_RELAY_PORTS_COUNT accordingly | ||
70 | #define DEFAULT_TCP_RELAY_PORTS_COUNT 3 | ||
71 | #define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false | ||
72 | #define DEFAULT_MOTD DAEMON_NAME | ||
73 | |||
74 | #define MIN_ALLOWED_PORT 1 | ||
75 | #define MAX_ALLOWED_PORT 65535 | ||
76 | |||
77 | |||
78 | // Uses the already existing key or creates one if it didn't exist | 60 | // Uses the already existing key or creates one if it didn't exist |
79 | // | 61 | // |
80 | // retirns 1 on success | 62 | // retirns 1 on success |
@@ -122,383 +104,6 @@ int manage_keys(DHT *dht, char *keys_file_path) | |||
122 | return 1; | 104 | return 1; |
123 | } | 105 | } |
124 | 106 | ||
125 | // Parses tcp relay ports from `cfg` and puts them into `tcp_relay_ports` array | ||
126 | // | ||
127 | // Supposed to be called from get_general_config only | ||
128 | // | ||
129 | // Important: iff `tcp_relay_port_count` > 0, then you are responsible for freeing `tcp_relay_ports` | ||
130 | |||
131 | void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) | ||
132 | { | ||
133 | const char *NAME_TCP_RELAY_PORTS = "tcp_relay_ports"; | ||
134 | |||
135 | *tcp_relay_port_count = 0; | ||
136 | |||
137 | config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS); | ||
138 | |||
139 | if (ports_array == NULL) { | ||
140 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); | ||
141 | write_log(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); | ||
142 | |||
143 | uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS}; | ||
144 | |||
145 | int i; | ||
146 | |||
147 | for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { | ||
148 | write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]); | ||
149 | } | ||
150 | |||
151 | // similar procedure to the one of reading config file below | ||
152 | *tcp_relay_ports = malloc(DEFAULT_TCP_RELAY_PORTS_COUNT * sizeof(uint16_t)); | ||
153 | |||
154 | for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { | ||
155 | |||
156 | (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i]; | ||
157 | |||
158 | if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT | ||
159 | || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { | ||
160 | write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, | ||
161 | (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
162 | continue; | ||
163 | } | ||
164 | |||
165 | (*tcp_relay_port_count) ++; | ||
166 | } | ||
167 | |||
168 | // the loop above skips invalid ports, so we adjust the allocated memory size | ||
169 | if ((*tcp_relay_port_count) > 0) { | ||
170 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | ||
171 | } else { | ||
172 | free(*tcp_relay_ports); | ||
173 | *tcp_relay_ports = NULL; | ||
174 | } | ||
175 | |||
176 | return; | ||
177 | } | ||
178 | |||
179 | if (config_setting_is_array(ports_array) == CONFIG_FALSE) { | ||
180 | write_log(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", | ||
181 | NAME_TCP_RELAY_PORTS); | ||
182 | return; | ||
183 | } | ||
184 | |||
185 | int config_port_count = config_setting_length(ports_array); | ||
186 | |||
187 | if (config_port_count == 0) { | ||
188 | write_log(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); | ||
189 | return; | ||
190 | } | ||
191 | |||
192 | *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t)); | ||
193 | |||
194 | int i; | ||
195 | |||
196 | for (i = 0; i < config_port_count; i ++) { | ||
197 | config_setting_t *elem = config_setting_get_elem(ports_array, i); | ||
198 | |||
199 | if (elem == NULL) { | ||
200 | // it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be | ||
201 | write_log(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); | ||
202 | break; | ||
203 | } | ||
204 | |||
205 | if (config_setting_is_number(elem) == CONFIG_FALSE) { | ||
206 | write_log(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i); | ||
207 | continue; | ||
208 | } | ||
209 | |||
210 | (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); | ||
211 | |||
212 | if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT | ||
213 | || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { | ||
214 | write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, | ||
215 | (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
216 | continue; | ||
217 | } | ||
218 | |||
219 | (*tcp_relay_port_count) ++; | ||
220 | } | ||
221 | |||
222 | // the loop above skips invalid ports, so we adjust the allocated memory size | ||
223 | if ((*tcp_relay_port_count) > 0) { | ||
224 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | ||
225 | } else { | ||
226 | free(*tcp_relay_ports); | ||
227 | *tcp_relay_ports = NULL; | ||
228 | } | ||
229 | } | ||
230 | |||
231 | // Gets general config options | ||
232 | // | ||
233 | // Important: you are responsible for freeing `pid_file_path` and `keys_file_path` | ||
234 | // also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports` | ||
235 | // and also `motd` iff `enable_motd` is set | ||
236 | // | ||
237 | // returns 1 on success | ||
238 | // 0 on failure, doesn't modify any data pointed by arguments | ||
239 | |||
240 | int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, | ||
241 | int *enable_ipv6, | ||
242 | int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, | ||
243 | int *tcp_relay_port_count, int *enable_motd, char **motd) | ||
244 | { | ||
245 | config_t cfg; | ||
246 | |||
247 | const char *NAME_PORT = "port"; | ||
248 | const char *NAME_PID_FILE_PATH = "pid_file_path"; | ||
249 | const char *NAME_KEYS_FILE_PATH = "keys_file_path"; | ||
250 | const char *NAME_ENABLE_IPV6 = "enable_ipv6"; | ||
251 | const char *NAME_ENABLE_IPV4_FALLBACK = "enable_ipv4_fallback"; | ||
252 | const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; | ||
253 | const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; | ||
254 | const char *NAME_ENABLE_MOTD = "enable_motd"; | ||
255 | const char *NAME_MOTD = "motd"; | ||
256 | |||
257 | config_init(&cfg); | ||
258 | |||
259 | // Read the file. If there is an error, report it and exit. | ||
260 | if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { | ||
261 | write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); | ||
262 | config_destroy(&cfg); | ||
263 | return 0; | ||
264 | } | ||
265 | |||
266 | // Get port | ||
267 | if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { | ||
268 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); | ||
269 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); | ||
270 | *port = DEFAULT_PORT; | ||
271 | } | ||
272 | |||
273 | // Get PID file location | ||
274 | const char *tmp_pid_file; | ||
275 | |||
276 | if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { | ||
277 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); | ||
278 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); | ||
279 | tmp_pid_file = DEFAULT_PID_FILE_PATH; | ||
280 | } | ||
281 | |||
282 | *pid_file_path = malloc(strlen(tmp_pid_file) + 1); | ||
283 | strcpy(*pid_file_path, tmp_pid_file); | ||
284 | |||
285 | // Get keys file location | ||
286 | const char *tmp_keys_file; | ||
287 | |||
288 | if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { | ||
289 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); | ||
290 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); | ||
291 | tmp_keys_file = DEFAULT_KEYS_FILE_PATH; | ||
292 | } | ||
293 | |||
294 | *keys_file_path = malloc(strlen(tmp_keys_file) + 1); | ||
295 | strcpy(*keys_file_path, tmp_keys_file); | ||
296 | |||
297 | // Get IPv6 option | ||
298 | if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { | ||
299 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); | ||
300 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); | ||
301 | *enable_ipv6 = DEFAULT_ENABLE_IPV6; | ||
302 | } | ||
303 | |||
304 | // Get IPv4 fallback option | ||
305 | if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { | ||
306 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); | ||
307 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, | ||
308 | DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false"); | ||
309 | *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; | ||
310 | } | ||
311 | |||
312 | // Get LAN discovery option | ||
313 | if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { | ||
314 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); | ||
315 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, | ||
316 | DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); | ||
317 | *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; | ||
318 | } | ||
319 | |||
320 | // Get TCP relay option | ||
321 | if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { | ||
322 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); | ||
323 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, | ||
324 | DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); | ||
325 | *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; | ||
326 | } | ||
327 | |||
328 | if (*enable_tcp_relay) { | ||
329 | parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count); | ||
330 | } else { | ||
331 | *tcp_relay_port_count = 0; | ||
332 | } | ||
333 | |||
334 | // Get MOTD option | ||
335 | if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { | ||
336 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); | ||
337 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, | ||
338 | DEFAULT_ENABLE_MOTD ? "true" : "false"); | ||
339 | *enable_motd = DEFAULT_ENABLE_MOTD; | ||
340 | } | ||
341 | |||
342 | if (*enable_motd) { | ||
343 | // Get MOTD | ||
344 | const char *tmp_motd; | ||
345 | |||
346 | if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) { | ||
347 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); | ||
348 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); | ||
349 | tmp_motd = DEFAULT_MOTD; | ||
350 | } | ||
351 | |||
352 | size_t tmp_motd_length = strlen(tmp_motd) + 1; | ||
353 | size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length; | ||
354 | *motd = malloc(motd_length); | ||
355 | strncpy(*motd, tmp_motd, motd_length); | ||
356 | (*motd)[motd_length - 1] = '\0'; | ||
357 | } | ||
358 | |||
359 | config_destroy(&cfg); | ||
360 | |||
361 | write_log(LOG_LEVEL_INFO, "Successfully read:\n"); | ||
362 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); | ||
363 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); | ||
364 | write_log(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port); | ||
365 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); | ||
366 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); | ||
367 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); | ||
368 | |||
369 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); | ||
370 | |||
371 | // show info about tcp ports only if tcp relay is enabled | ||
372 | if (*enable_tcp_relay) { | ||
373 | if (*tcp_relay_port_count == 0) { | ||
374 | write_log(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); | ||
375 | } else { | ||
376 | write_log(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); | ||
377 | int i; | ||
378 | |||
379 | for (i = 0; i < *tcp_relay_port_count; i ++) { | ||
380 | write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); | ||
381 | } | ||
382 | } | ||
383 | } | ||
384 | |||
385 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); | ||
386 | |||
387 | if (*enable_motd) { | ||
388 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd); | ||
389 | } | ||
390 | |||
391 | return 1; | ||
392 | } | ||
393 | |||
394 | // Bootstraps nodes listed in the config file | ||
395 | // | ||
396 | // returns 1 on success, some or no bootstrap nodes were added | ||
397 | // 0 on failure, a error accured while parsing config file | ||
398 | |||
399 | int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) | ||
400 | { | ||
401 | const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes"; | ||
402 | |||
403 | const char *NAME_PUBLIC_KEY = "public_key"; | ||
404 | const char *NAME_PORT = "port"; | ||
405 | const char *NAME_ADDRESS = "address"; | ||
406 | |||
407 | config_t cfg; | ||
408 | |||
409 | config_init(&cfg); | ||
410 | |||
411 | if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { | ||
412 | write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); | ||
413 | config_destroy(&cfg); | ||
414 | return 0; | ||
415 | } | ||
416 | |||
417 | config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES); | ||
418 | |||
419 | if (node_list == NULL) { | ||
420 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES); | ||
421 | config_destroy(&cfg); | ||
422 | return 1; | ||
423 | } | ||
424 | |||
425 | if (config_setting_length(node_list) == 0) { | ||
426 | write_log(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); | ||
427 | config_destroy(&cfg); | ||
428 | return 1; | ||
429 | } | ||
430 | |||
431 | int bs_port; | ||
432 | const char *bs_address; | ||
433 | const char *bs_public_key; | ||
434 | |||
435 | config_setting_t *node; | ||
436 | |||
437 | int i = 0; | ||
438 | |||
439 | while (config_setting_length(node_list)) { | ||
440 | |||
441 | node = config_setting_get_elem(node_list, 0); | ||
442 | |||
443 | if (node == NULL) { | ||
444 | config_destroy(&cfg); | ||
445 | return 0; | ||
446 | } | ||
447 | |||
448 | // Check that all settings are present | ||
449 | if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { | ||
450 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY); | ||
451 | goto next; | ||
452 | } | ||
453 | |||
454 | if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) { | ||
455 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); | ||
456 | goto next; | ||
457 | } | ||
458 | |||
459 | if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { | ||
460 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); | ||
461 | goto next; | ||
462 | } | ||
463 | |||
464 | // Process settings | ||
465 | if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) { | ||
466 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, | ||
467 | bs_public_key); | ||
468 | goto next; | ||
469 | } | ||
470 | |||
471 | if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { | ||
472 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, | ||
473 | bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
474 | goto next; | ||
475 | } | ||
476 | |||
477 | uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key); | ||
478 | const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), | ||
479 | bs_public_key_bin); | ||
480 | free(bs_public_key_bin); | ||
481 | |||
482 | if (!address_resolved) { | ||
483 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); | ||
484 | goto next; | ||
485 | } | ||
486 | |||
487 | write_log(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); | ||
488 | |||
489 | next: | ||
490 | // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly | ||
491 | // though it's freed when the element is removed, so we free it right away in order to keep memory | ||
492 | // consumption minimal | ||
493 | config_setting_remove_elem(node_list, 0); | ||
494 | i++; | ||
495 | } | ||
496 | |||
497 | config_destroy(&cfg); | ||
498 | |||
499 | return 1; | ||
500 | } | ||
501 | |||
502 | // Prints public key | 107 | // Prints public key |
503 | 108 | ||
504 | void print_public_key(const uint8_t *public_key) | 109 | void print_public_key(const uint8_t *public_key) |