diff options
author | irungentoo <irungentoo@gmail.com> | 2014-10-15 20:01:43 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-10-15 20:01:43 -0400 |
commit | 6114bd7f3ef1fda71e45e81f259074cf4f8e58eb (patch) | |
tree | 8dda87c3ea6c9f2f2af9fec54001db8e0ee3f50a /other/bootstrap_daemon/tox-bootstrapd.c | |
parent | 34311d1ba87f8f32a346422e2db4aa7f1d3fbb57 (diff) | |
parent | e8d7763e92c9d54bb1daaea848d20ba5d42fbda9 (diff) |
Merge branch 'tox-bootstrapd-network-error-handling' of https://github.com/nurupo/InsertProjectNameHere
Diffstat (limited to 'other/bootstrap_daemon/tox-bootstrapd.c')
-rw-r--r-- | other/bootstrap_daemon/tox-bootstrapd.c | 73 |
1 files changed, 57 insertions, 16 deletions
diff --git a/other/bootstrap_daemon/tox-bootstrapd.c b/other/bootstrap_daemon/tox-bootstrapd.c index 7e78da4e..1bcae732 100644 --- a/other/bootstrap_daemon/tox-bootstrapd.c +++ b/other/bootstrap_daemon/tox-bootstrapd.c | |||
@@ -53,7 +53,7 @@ | |||
53 | 53 | ||
54 | 54 | ||
55 | #define DAEMON_NAME "tox-bootstrapd" | 55 | #define DAEMON_NAME "tox-bootstrapd" |
56 | #define DAEMON_VERSION_NUMBER 2014101000UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day | 56 | #define DAEMON_VERSION_NUMBER 2014101200UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day |
57 | 57 | ||
58 | #define SLEEP_TIME_MILLISECONDS 30 | 58 | #define SLEEP_TIME_MILLISECONDS 30 |
59 | #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) | 59 | #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) |
@@ -62,6 +62,7 @@ | |||
62 | #define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys" | 62 | #define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys" |
63 | #define DEFAULT_PORT 33445 | 63 | #define DEFAULT_PORT 33445 |
64 | #define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false | 64 | #define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false |
65 | #define DEFAULT_ENABLE_IPV4_FALLBACK 1 // 1 - true, 0 - false | ||
65 | #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false | 66 | #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false |
66 | #define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false | 67 | #define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false |
67 | #define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports. make sure to adjust DEFAULT_TCP_RELAY_PORTS_COUNT accordingly | 68 | #define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports. make sure to adjust DEFAULT_TCP_RELAY_PORTS_COUNT accordingly |
@@ -88,7 +89,7 @@ int manage_keys(DHT *dht, char *keys_file_path) | |||
88 | keys_file = fopen(keys_file_path, "r"); | 89 | keys_file = fopen(keys_file_path, "r"); |
89 | 90 | ||
90 | if (keys_file != NULL) { | 91 | if (keys_file != NULL) { |
91 | size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); | 92 | const size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); |
92 | 93 | ||
93 | if (read_size != KEYS_SIZE) { | 94 | if (read_size != KEYS_SIZE) { |
94 | fclose(keys_file); | 95 | fclose(keys_file); |
@@ -104,7 +105,7 @@ int manage_keys(DHT *dht, char *keys_file_path) | |||
104 | 105 | ||
105 | keys_file = fopen(keys_file_path, "w"); | 106 | keys_file = fopen(keys_file_path, "w"); |
106 | 107 | ||
107 | size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); | 108 | const size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); |
108 | 109 | ||
109 | if (write_size != KEYS_SIZE) { | 110 | if (write_size != KEYS_SIZE) { |
110 | fclose(keys_file); | 111 | fclose(keys_file); |
@@ -161,7 +162,12 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int | |||
161 | } | 162 | } |
162 | 163 | ||
163 | // the loop above skips invalid ports, so we adjust the allocated memory size | 164 | // the loop above skips invalid ports, so we adjust the allocated memory size |
164 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | 165 | if ((*tcp_relay_port_count) > 0) { |
166 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | ||
167 | } else { | ||
168 | free(*tcp_relay_ports); | ||
169 | *tcp_relay_ports = NULL; | ||
170 | } | ||
165 | 171 | ||
166 | return; | 172 | return; |
167 | } | 173 | } |
@@ -210,8 +216,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int | |||
210 | } | 216 | } |
211 | 217 | ||
212 | // the loop above skips invalid ports, so we adjust the allocated memory size | 218 | // the loop above skips invalid ports, so we adjust the allocated memory size |
213 | if ((*tcp_relay_port_count) * sizeof(uint16_t) > 0) { | 219 | if ((*tcp_relay_port_count) > 0) { |
214 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | 220 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); |
221 | } else { | ||
222 | free(*tcp_relay_ports); | ||
223 | *tcp_relay_ports = NULL; | ||
215 | } | 224 | } |
216 | } | 225 | } |
217 | 226 | ||
@@ -224,9 +233,10 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int | |||
224 | // returns 1 on success | 233 | // returns 1 on success |
225 | // 0 on failure, doesn't modify any data pointed by arguments | 234 | // 0 on failure, doesn't modify any data pointed by arguments |
226 | 235 | ||
227 | int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, | 236 | int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, |
228 | int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count, | 237 | int *enable_ipv6, |
229 | int *enable_motd, char **motd) | 238 | int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, |
239 | int *tcp_relay_port_count, int *enable_motd, char **motd) | ||
230 | { | 240 | { |
231 | config_t cfg; | 241 | config_t cfg; |
232 | 242 | ||
@@ -234,6 +244,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi | |||
234 | const char *NAME_PID_FILE_PATH = "pid_file_path"; | 244 | const char *NAME_PID_FILE_PATH = "pid_file_path"; |
235 | const char *NAME_KEYS_FILE_PATH = "keys_file_path"; | 245 | const char *NAME_KEYS_FILE_PATH = "keys_file_path"; |
236 | const char *NAME_ENABLE_IPV6 = "enable_ipv6"; | 246 | const char *NAME_ENABLE_IPV6 = "enable_ipv6"; |
247 | const char *NAME_ENABLE_IPV4_FALLBACK = "enable_ipv4_fallback"; | ||
237 | const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; | 248 | const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; |
238 | const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; | 249 | const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; |
239 | const char *NAME_ENABLE_MOTD = "enable_motd"; | 250 | const char *NAME_ENABLE_MOTD = "enable_motd"; |
@@ -286,6 +297,14 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi | |||
286 | *enable_ipv6 = DEFAULT_ENABLE_IPV6; | 297 | *enable_ipv6 = DEFAULT_ENABLE_IPV6; |
287 | } | 298 | } |
288 | 299 | ||
300 | // Get IPv4 fallback option | ||
301 | if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { | ||
302 | syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); | ||
303 | syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, | ||
304 | DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false"); | ||
305 | *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; | ||
306 | } | ||
307 | |||
289 | // Get LAN discovery option | 308 | // Get LAN discovery option |
290 | if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { | 309 | if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { |
291 | syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); | 310 | syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); |
@@ -340,6 +359,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi | |||
340 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); | 359 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); |
341 | syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port); | 360 | syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port); |
342 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); | 361 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); |
362 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); | ||
343 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); | 363 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); |
344 | 364 | ||
345 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); | 365 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); |
@@ -372,7 +392,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi | |||
372 | // returns 1 on success, some or no bootstrap nodes were added | 392 | // returns 1 on success, some or no bootstrap nodes were added |
373 | // 0 on failure, a error accured while parsing config file | 393 | // 0 on failure, a error accured while parsing config file |
374 | 394 | ||
375 | int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) | 395 | int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) |
376 | { | 396 | { |
377 | const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes"; | 397 | const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes"; |
378 | 398 | ||
@@ -477,7 +497,7 @@ next: | |||
477 | 497 | ||
478 | // Prints public key | 498 | // Prints public key |
479 | 499 | ||
480 | void print_public_key(uint8_t *public_key) | 500 | void print_public_key(const uint8_t *public_key) |
481 | { | 501 | { |
482 | char buffer[2 * crypto_box_PUBLICKEYBYTES + 1]; | 502 | char buffer[2 * crypto_box_PUBLICKEYBYTES + 1]; |
483 | int index = 0; | 503 | int index = 0; |
@@ -504,10 +524,11 @@ int main(int argc, char *argv[]) | |||
504 | return 1; | 524 | return 1; |
505 | } | 525 | } |
506 | 526 | ||
507 | char *cfg_file_path = argv[1]; | 527 | const char *cfg_file_path = argv[1]; |
508 | char *pid_file_path, *keys_file_path; | 528 | char *pid_file_path, *keys_file_path; |
509 | int port; | 529 | int port; |
510 | int enable_ipv6; | 530 | int enable_ipv6; |
531 | int enable_ipv4_fallback; | ||
511 | int enable_lan_discovery; | 532 | int enable_lan_discovery; |
512 | int enable_tcp_relay; | 533 | int enable_tcp_relay; |
513 | uint16_t *tcp_relay_ports; | 534 | uint16_t *tcp_relay_ports; |
@@ -515,8 +536,8 @@ int main(int argc, char *argv[]) | |||
515 | int enable_motd; | 536 | int enable_motd; |
516 | char *motd; | 537 | char *motd; |
517 | 538 | ||
518 | if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery, | 539 | if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_ipv4_fallback, |
519 | &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { | 540 | &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { |
520 | syslog(LOG_DEBUG, "General config read successfully\n"); | 541 | syslog(LOG_DEBUG, "General config read successfully\n"); |
521 | } else { | 542 | } else { |
522 | syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); | 543 | syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); |
@@ -539,7 +560,27 @@ int main(int argc, char *argv[]) | |||
539 | IP ip; | 560 | IP ip; |
540 | ip_init(&ip, enable_ipv6); | 561 | ip_init(&ip, enable_ipv6); |
541 | 562 | ||
542 | DHT *dht = new_DHT(new_networking(ip, port)); | 563 | Networking_Core *net = new_networking(ip, port); |
564 | |||
565 | if (net == NULL) { | ||
566 | if (enable_ipv6 && enable_ipv4_fallback) { | ||
567 | syslog(LOG_DEBUG, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n"); | ||
568 | enable_ipv6 = 0; | ||
569 | ip_init(&ip, enable_ipv6); | ||
570 | net = new_networking(ip, port); | ||
571 | |||
572 | if (net == NULL) { | ||
573 | syslog(LOG_DEBUG, "Couldn't fallback to IPv4. Exiting.\n"); | ||
574 | return 1; | ||
575 | } | ||
576 | } else { | ||
577 | syslog(LOG_DEBUG, "Couldn't initialize networking. Exiting.\n"); | ||
578 | return 1; | ||
579 | } | ||
580 | } | ||
581 | |||
582 | |||
583 | DHT *dht = new_DHT(net); | ||
543 | 584 | ||
544 | if (dht == NULL) { | 585 | if (dht == NULL) { |
545 | syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n"); | 586 | syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n"); |
@@ -615,7 +656,7 @@ int main(int argc, char *argv[]) | |||
615 | free(keys_file_path); | 656 | free(keys_file_path); |
616 | 657 | ||
617 | // Fork off from the parent process | 658 | // Fork off from the parent process |
618 | pid_t pid = fork(); | 659 | const pid_t pid = fork(); |
619 | 660 | ||
620 | if (pid > 0) { | 661 | if (pid > 0) { |
621 | fprintf(pidf, "%d", pid); | 662 | fprintf(pidf, "%d", pid); |
@@ -652,7 +693,7 @@ int main(int argc, char *argv[]) | |||
652 | close(STDERR_FILENO); | 693 | close(STDERR_FILENO); |
653 | 694 | ||
654 | uint64_t last_LANdiscovery = 0; | 695 | uint64_t last_LANdiscovery = 0; |
655 | uint16_t htons_port = htons(port); | 696 | const uint16_t htons_port = htons(port); |
656 | 697 | ||
657 | int waiting_for_dht_connection = 1; | 698 | int waiting_for_dht_connection = 1; |
658 | 699 | ||