summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon/tox-bootstrapd.c
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2014-08-16 23:19:23 -0400
committerMaxim Biro <nurupo.contributions@gmail.com>2014-08-16 23:19:23 -0400
commit2040fc41d2ee53eb2d462e43e0f306613f48a685 (patch)
treee7b77afaceca61a9a0ac2222086421db6a0a6582 /other/bootstrap_daemon/tox-bootstrapd.c
parentbb1bb583524cc2a955b2492d0f0dbc2dc570fe4c (diff)
Renamed tox_bootstrap_daemon into tox-bootstrapd
Diffstat (limited to 'other/bootstrap_daemon/tox-bootstrapd.c')
-rw-r--r--other/bootstrap_daemon/tox-bootstrapd.c685
1 files changed, 685 insertions, 0 deletions
diff --git a/other/bootstrap_daemon/tox-bootstrapd.c b/other/bootstrap_daemon/tox-bootstrapd.c
new file mode 100644
index 00000000..0e17b94b
--- /dev/null
+++ b/other/bootstrap_daemon/tox-bootstrapd.c
@@ -0,0 +1,685 @@
1/* tox-bootstrapd.c
2 *
3 * Tox DHT bootstrap daemon.
4 *
5 * Copyright (C) 2014 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24// system provided
25#include <arpa/inet.h>
26#include <syslog.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <unistd.h>
30
31// C
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36// 3rd party
37#include <libconfig.h>
38
39// ./configure
40#ifdef HAVE_CONFIG_H
41#include "config.h"
42#endif
43
44// toxcore
45#include "../../toxcore/LAN_discovery.h"
46#include "../../toxcore/onion_announce.h"
47#include "../../toxcore/TCP_server.h"
48#include "../../toxcore/util.h"
49
50// misc
51#include "../bootstrap_node_packets.c"
52#include "../../testing/misc_tools.c"
53
54
55#define DAEMON_NAME "tox-bootstrapd"
56#define DAEMON_VERSION_NUMBER 2014081600UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day
57
58#define SLEEP_TIME_MILLISECONDS 30
59#define sleep usleep(1000*SLEEP_TIME_MILLISECONDS)
60
61#define DEFAULT_PID_FILE_PATH ".tox-bootstrapd.pid"
62#define DEFAULT_KEYS_FILE_PATH ".tox-bootstrapd.keys"
63#define DEFAULT_PORT 33445
64#define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false
65#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false
66#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_COUNT 3
69#define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false
70#define DEFAULT_MOTD DAEMON_NAME
71
72#define MIN_ALLOWED_PORT 1
73#define MAX_ALLOWED_PORT 65535
74
75
76// Uses the already existing key or creates one if it didn't exist
77//
78// retirns 1 on success
79// 0 on failure - no keys were read or stored
80
81int manage_keys(DHT *dht, char *keys_file_path)
82{
83 const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
84 uint8_t keys[KEYS_SIZE];
85 FILE *keys_file;
86
87 // Check if file exits, proceed to open and load keys
88 keys_file = fopen(keys_file_path, "r");
89
90 if (keys_file != NULL) {
91 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
92
93 if (read_size != KEYS_SIZE) {
94 fclose(keys_file);
95 return 0;
96 }
97
98 memcpy(dht->self_public_key, keys, crypto_box_PUBLICKEYBYTES);
99 memcpy(dht->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
100 } else {
101 // Otherwise save new keys
102 memcpy(keys, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
103 memcpy(keys + crypto_box_PUBLICKEYBYTES, dht->self_secret_key, crypto_box_SECRETKEYBYTES);
104
105 keys_file = fopen(keys_file_path, "w");
106
107 size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
108
109 if (write_size != KEYS_SIZE) {
110 fclose(keys_file);
111 return 0;
112 }
113 }
114
115 fclose(keys_file);
116
117 return 1;
118}
119
120// Parses tcp relay ports from `cfg` and puts them into `tcp_relay_ports` array
121//
122// Supposed to be called from get_general_config only
123//
124// Important: iff `tcp_relay_port_count` > 0, then you are responsible for freeing `tcp_relay_ports`
125
126void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int *tcp_relay_port_count)
127{
128 const char *NAME_TCP_RELAY_PORTS = "tcp_relay_ports";
129
130 *tcp_relay_port_count = 0;
131
132 config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS);
133
134 if (ports_array == NULL) {
135 syslog(LOG_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS);
136 syslog(LOG_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS);
137
138 uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS};
139
140 int i;
141
142 for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
143 syslog(LOG_WARNING, "Port #%d: %u\n", i, default_ports[i]);
144 }
145
146 // similar procedure to the one of reading config file below
147 *tcp_relay_ports = malloc(DEFAULT_TCP_RELAY_PORTS_COUNT * sizeof(uint16_t));
148
149 for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
150
151 (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i];
152
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);
157 continue;
158 }
159
160 (*tcp_relay_port_count) ++;
161 }
162
163 // 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
166 return;
167 }
168
169 if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
170 syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
171 NAME_TCP_RELAY_PORTS);
172 return;
173 }
174
175 int config_port_count = config_setting_length(ports_array);
176
177 if (config_port_count == 0) {
178 syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
179 return;
180 }
181
182 *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t));
183
184 int i;
185
186 for (i = 0; i < config_port_count; i ++) {
187 config_setting_t *elem = config_setting_get_elem(ports_array, i);
188
189 if (elem == NULL) {
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
191 syslog(LOG_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i);
192 break;
193 }
194
195 if (config_setting_is_number(elem) == CONFIG_FALSE) {
196 syslog(LOG_WARNING, "Port #%d: Not a number. Skipping.\n", i);
197 continue;
198 }
199
200 (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem);
201
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);
206 continue;
207 }
208
209 (*tcp_relay_port_count) ++;
210 }
211
212 // the loop above skips invalid ports, so we adjust the allocated memory size
213 *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t));
214}
215
216// Gets general config options
217//
218// Important: you are responsible for freeing `pid_file_path` and `keys_file_path`
219// also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports`
220// and also `motd` iff `enable_motd` is set
221//
222// returns 1 on success
223// 0 on failure, doesn't modify any data pointed by arguments
224
225int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6,
226 int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count,
227 int *enable_motd, char **motd)
228{
229 config_t cfg;
230
231 const char *NAME_PORT = "port";
232 const char *NAME_PID_FILE_PATH = "pid_file_path";
233 const char *NAME_KEYS_FILE_PATH = "keys_file_path";
234 const char *NAME_ENABLE_IPV6 = "enable_ipv6";
235 const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery";
236 const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay";
237 const char *NAME_ENABLE_MOTD = "enable_motd";
238 const char *NAME_MOTD = "motd";
239
240 config_init(&cfg);
241
242 // Read the file. If there is an error, report it and exit.
243 if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
244 syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
245 config_destroy(&cfg);
246 return 0;
247 }
248
249 // Get port
250 if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) {
251 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT);
252 syslog(LOG_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT);
253 *port = DEFAULT_PORT;
254 }
255
256 // Get PID file location
257 const char *tmp_pid_file;
258
259 if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) {
260 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH);
261 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH);
262 tmp_pid_file = DEFAULT_PID_FILE_PATH;
263 }
264
265 *pid_file_path = malloc(strlen(tmp_pid_file) + 1);
266 strcpy(*pid_file_path, tmp_pid_file);
267
268 // Get keys file location
269 const char *tmp_keys_file;
270
271 if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) {
272 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH);
273 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH);
274 tmp_keys_file = DEFAULT_KEYS_FILE_PATH;
275 }
276
277 *keys_file_path = malloc(strlen(tmp_keys_file) + 1);
278 strcpy(*keys_file_path, tmp_keys_file);
279
280 // Get IPv6 option
281 if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) {
282 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6);
283 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false");
284 *enable_ipv6 = DEFAULT_ENABLE_IPV6;
285 }
286
287 // Get LAN discovery option
288 if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
289 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
290 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
291 DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
292 *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
293 }
294
295 // Get TCP relay option
296 if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) {
297 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY);
298 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY,
299 DEFAULT_ENABLE_TCP_RELAY ? "true" : "false");
300 *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY;
301 }
302
303 if (*enable_tcp_relay) {
304 parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count);
305 } else {
306 *tcp_relay_port_count = 0;
307 }
308
309 // Get MOTD option
310 if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
311 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD);
312 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD,
313 DEFAULT_ENABLE_MOTD ? "true" : "false");
314 *enable_motd = DEFAULT_ENABLE_MOTD;
315 }
316
317 if (*enable_motd) {
318 // Get MOTD
319 const char *tmp_motd;
320
321 if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) {
322 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD);
323 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
324 tmp_motd = DEFAULT_MOTD;
325 }
326
327 size_t tmp_motd_length = strlen(tmp_motd) + 1;
328 size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
329 *motd = malloc(motd_length);
330 strncpy(*motd, tmp_motd, motd_length);
331 (*motd)[motd_length - 1] = '\0';
332 }
333
334 config_destroy(&cfg);
335
336 syslog(LOG_DEBUG, "Successfully read:\n");
337 syslog(LOG_DEBUG, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path);
338 syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path);
339 syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port);
340 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false");
341 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
342
343 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
344
345 // show info about tcp ports only if tcp relay is enabled
346 if (*enable_tcp_relay) {
347 if (*tcp_relay_port_count == 0) {
348 syslog(LOG_DEBUG, "No TCP ports could be read.\n");
349 } else {
350 syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count);
351 int i;
352
353 for (i = 0; i < *tcp_relay_port_count; i ++) {
354 syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]);
355 }
356 }
357 }
358
359 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
360
361 if (*enable_motd) {
362 syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd);
363 }
364
365 return 1;
366}
367
368// Bootstraps nodes listed in the config file
369//
370// returns 1 on success, some or no bootstrap nodes were added
371// 0 on failure, a error accured while parsing config file
372
373int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
374{
375 const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes";
376
377 const char *NAME_PUBLIC_KEY = "public_key";
378 const char *NAME_PORT = "port";
379 const char *NAME_ADDRESS = "address";
380
381 config_t cfg;
382
383 config_init(&cfg);
384
385 if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
386 syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
387 config_destroy(&cfg);
388 return 0;
389 }
390
391 config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES);
392
393 if (node_list == NULL) {
394 syslog(LOG_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES);
395 config_destroy(&cfg);
396 return 1;
397 }
398
399 if (config_setting_length(node_list) == 0) {
400 syslog(LOG_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n");
401 config_destroy(&cfg);
402 return 1;
403 }
404
405 int bs_port;
406 const char *bs_address;
407 const char *bs_public_key;
408
409 config_setting_t *node;
410
411 int i = 0;
412
413 while (config_setting_length(node_list)) {
414
415 node = config_setting_get_elem(node_list, 0);
416
417 if (node == NULL) {
418 config_destroy(&cfg);
419 return 0;
420 }
421
422 // Check that all settings are present
423 if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) {
424 syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY);
425 goto next;
426 }
427
428 if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) {
429 syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT);
430 goto next;
431 }
432
433 if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) {
434 syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS);
435 goto next;
436 }
437
438 // Process settings
439 if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) {
440 syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY,
441 bs_public_key);
442 goto next;
443 }
444
445 if (bs_port < MIN_ALLOWED_PORT || bs_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);
448 goto next;
449 }
450
451 uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key);
452 const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port),
453 bs_public_key_bin);
454 free(bs_public_key_bin);
455
456 if (!address_resolved) {
457 syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address);
458 goto next;
459 }
460
461 syslog(LOG_DEBUG, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key);
462
463next:
464 // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly
465 // though it's freed when the element is removed, so we free it right away in order to keep memory
466 // consumption minimal
467 config_setting_remove_elem(node_list, 0);
468 i++;
469 }
470
471 config_destroy(&cfg);
472
473 return 1;
474}
475
476// Prints public key
477
478void print_public_key(uint8_t *public_key)
479{
480 char buffer[2 * crypto_box_PUBLICKEYBYTES + 1];
481 int index = 0;
482
483 int i;
484
485 for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++) {
486 index += sprintf(buffer + index, "%02hhX", public_key[i]);
487 }
488
489 syslog(LOG_INFO, "Public Key: %s\n", buffer);
490
491 return;
492}
493
494int main(int argc, char *argv[])
495{
496 openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON);
497
498 syslog(LOG_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER);
499
500 if (argc < 2) {
501 syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n");
502 return 1;
503 }
504
505 char *cfg_file_path = argv[1];
506 char *pid_file_path, *keys_file_path;
507 int port;
508 int enable_ipv6;
509 int enable_lan_discovery;
510 int enable_tcp_relay;
511 uint16_t *tcp_relay_ports;
512 int tcp_relay_port_count;
513 int enable_motd;
514 char *motd;
515
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)) {
518 syslog(LOG_DEBUG, "General config read successfully\n");
519 } else {
520 syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
521 return 1;
522 }
523
524 if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) {
525 syslog(LOG_ERR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
526 return 1;
527 }
528
529 // Check if the PID file exists
530 FILE *pid_file;
531
532 if (pid_file = fopen(pid_file_path, "r")) {
533 syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path);
534 fclose(pid_file);
535 }
536
537 IP ip;
538 ip_init(&ip, enable_ipv6);
539
540 DHT *dht = new_DHT(new_networking(ip, port));
541
542 if (dht == NULL) {
543 syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n");
544 return 1;
545 }
546
547 Onion *onion = new_onion(dht);
548 Onion_Announce *onion_a = new_onion_announce(dht);
549
550 if (!(onion && onion_a)) {
551 syslog(LOG_ERR, "Couldn't initialize Tox Onion. Exiting.\n");
552 return 1;
553 }
554
555 if (enable_motd) {
556 if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
557 syslog(LOG_DEBUG, "Set MOTD successfully.\n");
558 } else {
559 syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd);
560 return 1;
561 }
562
563 free(motd);
564 }
565
566 if (manage_keys(dht, keys_file_path)) {
567 syslog(LOG_DEBUG, "Keys are managed successfully.\n");
568 } else {
569 syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path);
570 return 1;
571 }
572
573 TCP_Server *tcp_server = NULL;
574
575 if (enable_tcp_relay) {
576 if (tcp_relay_port_count == 0) {
577 syslog(LOG_ERR, "No TCP relay ports read. Exiting.\n");
578 return 1;
579 }
580
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);
583
584 // tcp_relay_port_count != 0 at this point
585 free(tcp_relay_ports);
586
587 if (tcp_server != NULL) {
588 syslog(LOG_DEBUG, "Initialized Tox TCP server successfully.\n");
589 } else {
590 syslog(LOG_ERR, "Couldn't initialize Tox TCP server. Exiting.\n");
591 return 1;
592 }
593 }
594
595 if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) {
596 syslog(LOG_DEBUG, "List of bootstrap nodes read successfully.\n");
597 } else {
598 syslog(LOG_ERR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path);
599 return 1;
600 }
601
602 print_public_key(dht->self_public_key);
603
604 // Write the PID file
605 FILE *pidf = fopen(pid_file_path, "a+");
606
607 if (pidf == NULL) {
608 syslog(LOG_ERR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path);
609 return 1;
610 }
611
612 free(pid_file_path);
613 free(keys_file_path);
614
615 // Fork off from the parent process
616 pid_t pid = fork();
617
618 if (pid > 0) {
619 fprintf(pidf, "%d ", pid);
620 fclose(pidf);
621 syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid);
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;
630 }
631
632 // Change the file mode mask
633 umask(0);
634
635 // Create a new SID for the child process
636 if (setsid() < 0) {
637 syslog(LOG_ERR, "SID creation failure. Exiting.\n");
638 return 1;
639 }
640
641 // Change the current working directory
642 if ((chdir("/")) < 0) {
643 syslog(LOG_ERR, "Couldn't change working directory to '/'. Exiting.\n");
644 return 1;
645 }
646
647 // Go quiet
648 close(STDOUT_FILENO);
649 close(STDIN_FILENO);
650 close(STDERR_FILENO);
651
652 uint64_t last_LANdiscovery = 0;
653 uint16_t htons_port = htons(port);
654
655 int waiting_for_dht_connection = 1;
656
657 if (enable_lan_discovery) {
658 LANdiscovery_init(dht);
659 syslog(LOG_DEBUG, "Initialized LAN discovery.\n");
660 }
661
662 while (1) {
663 do_DHT(dht);
664
665 if (enable_lan_discovery && is_timeout(last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) {
666 send_LANdiscovery(htons_port, dht);
667 last_LANdiscovery = unix_time();
668 }
669
670 if (enable_tcp_relay) {
671 do_TCP_server(tcp_server);
672 }
673
674 networking_poll(dht->net);
675
676 if (waiting_for_dht_connection && DHT_isconnected(dht)) {
677 syslog(LOG_DEBUG, "Connected to other bootstrap node successfully.\n");
678 waiting_for_dht_connection = 0;
679 }
680
681 sleep;
682 }
683
684 return 1;
685}