From b9ef9b91aff17533254073538467684d62ed465f Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 25 Jan 2014 20:00:34 -0500 Subject: Added more error checking --- .../tox_dht_bootstrap_server_daemon.c | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'other') diff --git a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c b/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c index 24bb8266..1a68393f 100644 --- a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c +++ b/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c @@ -233,20 +233,29 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) } // Proceed only if all parts are present - if (config_setting_lookup_string(server, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE || - config_setting_lookup_int (server, NAME_PORT, &bs_port) == CONFIG_FALSE || - config_setting_lookup_string(server, NAME_ADDRESS, &bs_address) == CONFIG_FALSE ) { + if (config_setting_lookup_string(server, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PUBLIC_KEY); + goto next; + } + + if (config_setting_lookup_int(server, NAME_PORT, &bs_port) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PORT); + goto next; + } + + if (config_setting_lookup_string(server, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_ADDRESS); goto next; } if (strlen(bs_public_key) != 64) { - syslog(LOG_WARNING, "bootstrap_server #%d: Invalid '%s': %s.\n", i, NAME_PUBLIC_KEY, bs_public_key); + syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_PUBLIC_KEY, bs_public_key); goto next; } // not (1 <= port <= 65535) if (bs_port < 1 || bs_port > 65535) { - syslog(LOG_WARNING, "bootstrap_server #%d: Invalid '%s': %d.\n", i, NAME_PORT, bs_port); + syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %d. Skipping the server.\n", i, NAME_PORT, bs_port); goto next; } @@ -254,11 +263,11 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) hex_string_to_bin((char *)bs_public_key)); if (!address_resolved) { - syslog(LOG_WARNING, "bootstrap_server #%d: Invalid '%s': %s.\n", i, NAME_ADDRESS, bs_address); + syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_ADDRESS, bs_address); goto next; } - syslog(LOG_DEBUG, "Successfully connected to %s:%d %s\n", bs_address, bs_port, bs_public_key); + syslog(LOG_DEBUG, "Successfully added bootstrap server #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); next: // config_setting_lookup_string() allocates string inside and doesn't allow us to free it @@ -330,7 +339,7 @@ int main(int argc, char *argv[]) openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); if (argc < 2) { - syslog(LOG_ERR, "Please specify a configuration file. Exiting.\n"); + syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); return 1; } -- cgit v1.2.3 From 4782a8475a3476642d5a9f747a5de7080a44b1ea Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 25 Jan 2014 20:37:01 -0500 Subject: Made bootstrapping optional --- other/bootstrap_serverdaemon/conf | 35 +++++++------ .../tox_dht_bootstrap_server_daemon.c | 61 +++++++--------------- 2 files changed, 37 insertions(+), 59 deletions(-) (limited to 'other') diff --git a/other/bootstrap_serverdaemon/conf b/other/bootstrap_serverdaemon/conf index 70dbdb14..136db0f5 100644 --- a/other/bootstrap_serverdaemon/conf +++ b/other/bootstrap_serverdaemon/conf @@ -1,33 +1,34 @@ -// ProjectTox bootstrap server configuration file +// ProjectTox dht bootstrap server daemon configuration file. -// listening port +// Listening port. port = 33445 -// The key file is like a password, so keep it where no one can read it -// The daemon should have permission to read/write to it -// Remember to replace the provided example with -// your own path +// The key file is like a password, so keep it where no one can read it. +// The daemon should have permission to read/write to it. +// Remember to replace the provided example with your own path. keys_file_path = "/home/tom/.tox_dht_bootstrap_server_daemon/keys" -// The PID file written to by daemon, -// make sure that the user who runs the server -// does have permissions to write to it -// Remember to replace the provided example with -// your own path +// The PID file written to by daemon. +// Make sure that the user who runs the daemon has permissions to write to the +// PID file. +// Remember to replace the provided example with your own path. pid_file_path = "/home/tom/.tox_dht_bootstrap_server_daemon/pid" +// Enable IPv6. enable_ipv6 = false -// Automatically bootstrap with nodes on local network +// Automatically bootstrap with nodes on local area network. enable_lan_discovery = true -// Any number of nodes the daemon will bootstrap itself from -// Remember to replace the provided example with -// your own server list +// Any number of servers the daemon will bootstrap itself from. +// Remember to replace the provided example with your own server list. +// You may leave the list empty or remove "bootstrap_servers" complitely, +// in both cases this will be interpreted as if you don't want to bootstrap +// from anyone. bootstrap_servers = ( { // Server 1 - // any ipv4 or ipv6, depending if `enable_ipv6` is set or not - // also any US-ASCII domain name + // Any ipv4 or ipv6, depending if `enable_ipv6` is set or not, and also + // any US-ASCII domain name. address = "198.46.136.167" port = 33445 public_key = "728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854" diff --git a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c b/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c index 1a68393f..a6cffb54 100644 --- a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c +++ b/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c @@ -186,8 +186,8 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi // Bootstraps servers listed in the config file // -// returns 1 on success -// 0 on failure, either no or only some servers were bootstrapped +// returns 1 on success, some or no bootstrap servers were added +// 0 on failure, a error accured while parsing config file int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) { @@ -210,9 +210,15 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) config_setting_t *server_list = config_lookup(&cfg, NAME_BOOTSTRAP_SERVERS); if (server_list == NULL) { - syslog(LOG_ERR, "No '%s' setting in configuration file.\n", NAME_BOOTSTRAP_SERVERS); + syslog(LOG_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_SERVERS); config_destroy(&cfg); - return 0; + return 1; + } + + if (config_setting_length(server_list) == 0) { + syslog(LOG_WARNING, "No bootstrap servers found. Skipping bootstrapping.\n"); + config_destroy(&cfg); + return 1; } int bs_port; @@ -232,7 +238,7 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) return 0; } - // Proceed only if all parts are present + // Check that all settings are present if (config_setting_lookup_string(server, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PUBLIC_KEY); goto next; @@ -248,6 +254,7 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) goto next; } + // Process settings if (strlen(bs_public_key) != 64) { syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_PUBLIC_KEY, bs_public_key); goto next; @@ -282,36 +289,6 @@ next: return 1; } -// Checks if we are connected to the DHT -// -// returns 1 on success -// 0 on failure - -int try_connect(DHT *dht, int port, int enable_lan_discovery) -{ - uint16_t htons_port = htons(port); - - int i; - - for (i = 0; i < 100; i ++) { - do_DHT(dht); - - if (enable_lan_discovery) { - send_LANdiscovery(htons_port, dht); - } - - networking_poll(dht->c->lossless_udp->net); - - if (DHT_isconnected(dht)) { - return 1; - } - - sleep; - } - - return 0; -} - // Prints public key void print_public_key(uint8_t *public_key) @@ -404,13 +381,6 @@ int main(int argc, char *argv[]) return 1; } - if (try_connect(dht, port, enable_lan_discovery)) { - syslog(LOG_INFO, "Successfully connected to DHT\n"); - } else { - syslog(LOG_ERR, "Couldn't connect to the DHT. Check settings and network connections. Exiting.\n"); - return 1; - } - print_public_key(dht->c->self_public_key); // Write the PID file @@ -463,6 +433,8 @@ int main(int argc, char *argv[]) uint64_t last_LANdiscovery = 0; uint16_t htons_port = htons(port); + int waiting_for_dht_connection = 1; + while (1) { do_DHT(dht); @@ -473,6 +445,11 @@ int main(int argc, char *argv[]) networking_poll(dht->net); + if (waiting_for_dht_connection && DHT_isconnected(dht)) { + syslog(LOG_DEBUG, "Connected to other bootstrap server successfully.\n"); + waiting_for_dht_connection = 0; + } + sleep; } -- cgit v1.2.3 From 553472442fd1ddcf224b7a3bbfc1ed51c2cedfad Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 25 Jan 2014 21:00:31 -0500 Subject: Shortened daemon's name --- other/bootstrap_serverdaemon/Makefile.inc | 12 +- other/bootstrap_serverdaemon/README.md | 22 +- other/bootstrap_serverdaemon/conf | 4 +- .../bootstrap_serverdaemon/tox_bootstrap_daemon.c | 457 +++++++++++++++++++++ .../bootstrap_serverdaemon/tox_bootstrap_daemon.sh | 110 +++++ .../tox_dht_bootstrap_server_daemon.c | 457 --------------------- .../tox_dht_bootstrap_server_daemon.sh | 109 ----- 7 files changed, 586 insertions(+), 585 deletions(-) create mode 100644 other/bootstrap_serverdaemon/tox_bootstrap_daemon.c create mode 100644 other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh delete mode 100644 other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c delete mode 100644 other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.sh (limited to 'other') diff --git a/other/bootstrap_serverdaemon/Makefile.inc b/other/bootstrap_serverdaemon/Makefile.inc index 5ab17b56..effe59e8 100644 --- a/other/bootstrap_serverdaemon/Makefile.inc +++ b/other/bootstrap_serverdaemon/Makefile.inc @@ -1,17 +1,17 @@ if BUILD_DHT_BOOTSTRAP_DAEMON -noinst_PROGRAMS += tox_dht_bootstrap_server_daemon +noinst_PROGRAMS += tox_bootstrap_daemon -tox_dht_bootstrap_server_daemon_SOURCES = \ - ../other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c +tox_bootstrap_daemon_SOURCES = \ + ../other/bootstrap_serverdaemon/tox_bootstrap_daemon.c -tox_dht_bootstrap_server_daemon_CFLAGS = \ +tox_bootstrap_daemon_CFLAGS = \ -I$(top_srcdir)/other/bootstrap_serverdaemon \ $(LIBSODIUM_CFLAGS) \ $(NACL_CFLAGS) \ $(LIBCONFIG_CFLAGS) -tox_dht_bootstrap_server_daemon_LDADD = \ +tox_bootstrap_daemon_LDADD = \ $(LIBSODIUM_LDFLAGS) \ $(NACL_LDFLAGS) \ libtoxcore.la \ @@ -23,5 +23,5 @@ endif EXTRA_DIST += \ $(top_srcdir)/other/bootstrap_serverdaemon/conf \ - $(top_srcdir)/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.sh + $(top_srcdir)/other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh \ No newline at end of file diff --git a/other/bootstrap_serverdaemon/README.md b/other/bootstrap_serverdaemon/README.md index 30cb14aa..53a25cdb 100644 --- a/other/bootstrap_serverdaemon/README.md +++ b/other/bootstrap_serverdaemon/README.md @@ -2,44 +2,44 @@ The following commands are to be executed as root: -1. In `tox_dht_bootstrap_server_daemon.sh` file change: +1. In `tox_bootstrap_daemon.sh` file change: - `CFG` to where your config file (`conf`) will be; read rights required - `DAEMON` to point to the executable - `PIDFILE` to point to a pid file daemon would have rights to create -2. Go over everything in `conf`. Make sure `pid_file_path` matches `PIDFILE` from `tox_dht_bootstrap_server_daemon.sh` +2. Go over everything in `conf`. Make sure `pid_file_path` matches `PIDFILE` from `tox_bootstrap_daemon.sh` 3. Execute: ``` -mv tox_dht_bootstrap_server_daemon.sh /etc/init.d/tox_dht_bootstrap_server_daemon +mv tox_bootstrap_daemon.sh /etc/init.d/tox_bootstrap_daemon ``` *(note that we removed `.sh` ending)* 4. Give the right permissions to this file: ``` -chmod 755 /etc/init.d/tox_dht_bootstrap_server_daemon +chmod 755 /etc/init.d/tox_bootstrap_daemon ``` 5. Execute: ``` -update-rc.d tox_dht_bootstrap_server_daemon defaults +update-rc.d tox_bootstrap_daemon defaults ``` 6. Start the service: ``` -service tox_dht_bootstrap_server_daemon start +service tox_bootstrap_daemon start ``` 7. Verify that the service is running: ``` -service tox_dht_bootstrap_server_daemon status +service tox_bootstrap_daemon status ``` -- You can see daemon's log with ``` -grep "tox_dht_bootstrap_server_daemon" /var/log/syslog +grep "tox_bootstrap_daemon" /var/log/syslog ``` **Note that system log is where you find your public key** @@ -50,12 +50,12 @@ grep "tox_dht_bootstrap_server_daemon" /var/log/syslog 1. Check the log for errors with ``` -grep "tox_dht_bootstrap_server_daemon" /var/log/syslog +grep "tox_bootstrap_daemon" /var/log/syslog ``` -2. Check that paths in the beginning of `/etc/init.d/tox_dht_bootstrap_server_daemon` are valid +2. Check that paths in the beginning of `/etc/init.d/tox_bootstrap_daemon` are valid -3. Make sure that `PIDFILE` from `/etc/init.d/tox_dht_bootstrap_server_daemon` matches with the `pid_file_path` from `conf` +3. Make sure that `PIDFILE` from `/etc/init.d/tox_bootstrap_daemon` matches with the `pid_file_path` from `conf` 4. Make sure you have write permission to keys and pid files diff --git a/other/bootstrap_serverdaemon/conf b/other/bootstrap_serverdaemon/conf index 136db0f5..8451d9a0 100644 --- a/other/bootstrap_serverdaemon/conf +++ b/other/bootstrap_serverdaemon/conf @@ -6,13 +6,13 @@ port = 33445 // The key file is like a password, so keep it where no one can read it. // The daemon should have permission to read/write to it. // Remember to replace the provided example with your own path. -keys_file_path = "/home/tom/.tox_dht_bootstrap_server_daemon/keys" +keys_file_path = "/home/tom/.tox_bootstrap_daemon/keys" // The PID file written to by daemon. // Make sure that the user who runs the daemon has permissions to write to the // PID file. // Remember to replace the provided example with your own path. -pid_file_path = "/home/tom/.tox_dht_bootstrap_server_daemon/pid" +pid_file_path = "/home/tom/.tox_bootstrap_daemon/pid" // Enable IPv6. enable_ipv6 = false diff --git a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c new file mode 100644 index 00000000..e82e49cd --- /dev/null +++ b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c @@ -0,0 +1,457 @@ +/* tox_bootstrap_daemon.c + * + * Tox DHT bootstrap server daemon. + * + * Copyright (C) 2014 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "../../toxcore/DHT.h" +#include "../../toxcore/friend_requests.h" +#include "../../toxcore/LAN_discovery.h" + +#include "../../testing/misc_tools.c" + +#define DAEMON_NAME "tox_bootstrap_daemon" + +#define SLEEP_TIME_MILLISECONDS 30 +#define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) + +#define DEFAULT_PID_FILE_PATH ".tox_bootstrap_daemon.pid" +#define DEFAULT_KEYS_FILE_PATH ".tox_bootstrap_daemon.keys" +#define DEFAULT_PORT 33445 +#define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false +#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false + + +// Uses the already existing key or creates one if it didn't exist +// +// retirns 1 on success +// 0 on failure - no keys were read or stored + +int manage_keys(DHT *dht, char *keys_file_path) +{ + const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; + uint8_t keys[KEYS_SIZE]; + FILE *keys_file; + + // Check if file exits, proceed to open and load keys + keys_file = fopen(keys_file_path, "r"); + + if (keys_file != NULL) { + size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); + + if (read_size != KEYS_SIZE) { + return 0; + } + + load_keys(dht->c, keys); + } else { + // Otherwise save new keys + new_keys(dht->c); + save_keys(dht->c, keys); + + keys_file = fopen(keys_file_path, "w"); + + size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); + + if (write_size != KEYS_SIZE) { + return 0; + } + } + + fclose(keys_file); + + // We want our DHT public key to be the same as our internal one since this is a bootstrap server + memcpy(dht->self_public_key, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES); + memcpy(dht->self_secret_key, dht->c->self_secret_key, crypto_box_SECRETKEYBYTES); + + return 1; +} + +// Gets general config options +// +// Important: you are responsible for freeing `pid_file_path` and `keys_file_path` +// +// returns 1 on success +// 0 on failure, doesn't modify any data pointed by arguments + +int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, + int *enable_lan_discovery) +{ + config_t cfg; + + const char *NAME_PORT = "port"; + const char *NAME_PID_FILE_PATH = "pid_file_path"; + const char *NAME_KEYS_FILE_PATH = "keys_file_path"; + const char *NAME_ENABLE_IPV6 = "enable_ipv6"; + const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; + + config_init(&cfg); + + // Read the file. If there is an error, report it and exit. + if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { + syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); + config_destroy(&cfg); + return 0; + } + + // Get port + if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { + syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); + syslog(LOG_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); + *port = DEFAULT_PORT; + } + + // Get PID file location + const char *tmp_pid_file; + + if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { + syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); + syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); + tmp_pid_file = DEFAULT_PID_FILE_PATH; + } + + *pid_file_path = malloc(strlen(tmp_pid_file) + 1); + strcpy(*pid_file_path, tmp_pid_file); + + // Get keys file location + const char *tmp_keys_file; + + if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { + syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); + syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); + tmp_keys_file = DEFAULT_KEYS_FILE_PATH; + } + + *keys_file_path = malloc(strlen(tmp_keys_file) + 1); + strcpy(*keys_file_path, tmp_keys_file); + + // Get IPv6 option + if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { + syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); + syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); + *enable_ipv6 = DEFAULT_ENABLE_IPV6; + } + + // Get LAN discovery option + if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { + syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); + syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, + DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); + *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; + } + + config_destroy(&cfg); + + syslog(LOG_DEBUG, "Successfully read:\n"); + syslog(LOG_DEBUG, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); + syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); + syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port); + syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); + syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); + + return 1; +} + +// Bootstraps servers listed in the config file +// +// returns 1 on success, some or no bootstrap servers were added +// 0 on failure, a error accured while parsing config file + +int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) +{ + const char *NAME_BOOTSTRAP_SERVERS = "bootstrap_servers"; + + const char *NAME_PUBLIC_KEY = "public_key"; + const char *NAME_PORT = "port"; + const char *NAME_ADDRESS = "address"; + + config_t cfg; + + config_init(&cfg); + + if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { + syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); + config_destroy(&cfg); + return 0; + } + + config_setting_t *server_list = config_lookup(&cfg, NAME_BOOTSTRAP_SERVERS); + + if (server_list == NULL) { + syslog(LOG_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_SERVERS); + config_destroy(&cfg); + return 1; + } + + if (config_setting_length(server_list) == 0) { + syslog(LOG_WARNING, "No bootstrap servers found. Skipping bootstrapping.\n"); + config_destroy(&cfg); + return 1; + } + + int bs_port; + const char *bs_address; + const char *bs_public_key; + + config_setting_t *server; + + int i = 0; + + while (config_setting_length(server_list)) { + + server = config_setting_get_elem(server_list, 0); + + if (server == NULL) { + config_destroy(&cfg); + return 0; + } + + // Check that all settings are present + if (config_setting_lookup_string(server, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PUBLIC_KEY); + goto next; + } + + if (config_setting_lookup_int(server, NAME_PORT, &bs_port) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PORT); + goto next; + } + + if (config_setting_lookup_string(server, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_ADDRESS); + goto next; + } + + // Process settings + if (strlen(bs_public_key) != 64) { + syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_PUBLIC_KEY, bs_public_key); + goto next; + } + + // not (1 <= port <= 65535) + if (bs_port < 1 || bs_port > 65535) { + syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %d. Skipping the server.\n", i, NAME_PORT, bs_port); + goto next; + } + + const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), + hex_string_to_bin((char *)bs_public_key)); + + if (!address_resolved) { + syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_ADDRESS, bs_address); + goto next; + } + + syslog(LOG_DEBUG, "Successfully added bootstrap server #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); + +next: + // config_setting_lookup_string() allocates string inside and doesn't allow us to free it + // so in order to reuse `bs_public_key` and `bs_address` we have to remove the element + // which will cause libconfig to free allocated strings + config_setting_remove_elem(server_list, 0); + i++; + } + + config_destroy(&cfg); + + return 1; +} + +// Prints public key + +void print_public_key(uint8_t *public_key) +{ + char buffer[64 + 1]; + int index = 0; + + int i; + + for (i = 0; i < 32; i++) { + if (public_key[i] < 16) { + index += sprintf(buffer + index, "0"); + } + + index += sprintf(buffer + index, "%hhX", public_key[i]); + } + + syslog(LOG_INFO, "Public Key: %s\n", buffer); + + return; +} + +int main(int argc, char *argv[]) +{ + openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); + + if (argc < 2) { + syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); + return 1; + } + + char *cfg_file_path = argv[1]; + char *pid_file_path, *keys_file_path; + int port; + int enable_ipv6; + int enable_lan_discovery; + + if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery)) { + syslog(LOG_DEBUG, "General config read successfully\n"); + } else { + syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); + return 1; + } + + // not (1 <= port <= 65535) + if (port < 1 || port > 65535) { + syslog(LOG_ERR, "Invalid port: %d, must be 1 <= port <= 65535. Exiting.\n", port); + return 1; + } + + // Check if the PID file exists + if (fopen(pid_file_path, "r")) { + syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists. Exiting.\n", pid_file_path); + return 1; + } + + IP ip; + ip_init(&ip, enable_ipv6); + + DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port))); + + if (dht == NULL) { + syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n"); + return 1; + } + + Onion *onion = new_onion(dht); + Onion_Announce *onion_a = new_onion_announce(dht); + + if (!(onion && onion_a)) { + syslog(LOG_ERR, "Couldn't initialize Tox Onion. Exiting.\n"); + return 1; + } + + if (enable_lan_discovery) { + LANdiscovery_init(dht); + } + + if (manage_keys(dht, keys_file_path)) { + syslog(LOG_DEBUG, "Keys are managed successfully\n"); + } else { + syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); + return 1; + } + + if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { + syslog(LOG_DEBUG, "List of bootstrap servers read successfully\n"); + } else { + syslog(LOG_ERR, "Couldn't read list of bootstrap servers in %s. Exiting.\n", cfg_file_path); + return 1; + } + + print_public_key(dht->c->self_public_key); + + // Write the PID file + FILE *pidf = fopen(pid_file_path, "w"); + + if (pidf == NULL) { + syslog(LOG_ERR, "Can't open the PID file for writing: %s. Exiting.\n", pid_file_path); + return 1; + } + + free(pid_file_path); + free(keys_file_path); + + // Fork off from the parent process + pid_t pid = fork(); + + if (pid < 0) { + syslog(LOG_ERR, "Forking failed. Exiting.\n"); + return 1; + } + + if (pid > 0) { + syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid); + return 0; + } + + // Change the file mode mask + umask(0); + + fprintf(pidf, "%d\n", pid); + fclose(pidf); + + // Create a new SID for the child process + if (setsid() < 0) { + syslog(LOG_ERR, "SID creation failure. Exiting.\n"); + return 1; + } + + // Change the current working directory + if ((chdir("/")) < 0) { + syslog(LOG_ERR, "Couldn't change working directory to '/'. Exiting.\n"); + return 1; + } + + // Go quiet + close(STDOUT_FILENO); + close(STDIN_FILENO); + close(STDERR_FILENO); + + uint64_t last_LANdiscovery = 0; + uint16_t htons_port = htons(port); + + int waiting_for_dht_connection = 1; + + while (1) { + do_DHT(dht); + + if (enable_lan_discovery && is_timeout(last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) { + send_LANdiscovery(htons_port, dht); + last_LANdiscovery = unix_time(); + } + + networking_poll(dht->net); + + if (waiting_for_dht_connection && DHT_isconnected(dht)) { + syslog(LOG_DEBUG, "Connected to other bootstrap server successfully.\n"); + waiting_for_dht_connection = 0; + } + + sleep; + } + + return 1; +} diff --git a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh new file mode 100644 index 00000000..83d9a119 --- /dev/null +++ b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh @@ -0,0 +1,110 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: tox_bootstrap_daemon +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Starts the Tox DHT bootstrapping server daemon +# Description: Starts the Tox DHT bootstrapping server daemon +### END INIT INFO + +# PATH should only include /usr/* if it runs after the mountnfs.sh script +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DESC="Tox DHT bootstrap server daemon" +NAME=tox_bootstrap_daemon +# You may want to change USER if you are using it anywhere else +USER=tom +CFG=/home/$USER/.$NAME/conf +DAEMON=/home/$USER/$NAME +DAEMON_ARGS="$CFG" +PIDFILE=/home/$USER/.$NAME/pid +SCRIPTNAME=/etc/init.d/$NAME + +# Exit if the package is not installed +[ -x "$DAEMON" ] || exit 0 + +# Read configuration variable file if it is present +#[ -r /etc/default/$NAME ] && . /etc/default/$NAME + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +# Depend on lsb-base (>= 3.2-14) to ensure that this file is present +# and status_of_proc is working. +. /lib/lsb/init-functions + +# +# Function that starts the daemon/service +# +do_start() +{ + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ + || return 1 + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \ + $DAEMON_ARGS \ + || return 2 + sleep 1 +} + +# +# Function that stops the daemon/service +# +do_stop() +{ + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON + RETVAL="$?" + [ "$RETVAL" = 2 ] && return 2 + + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON + [ "$?" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + status) + status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $? + ;; + + restart) #|force-reload) + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + *) + echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2 + exit 3 + ;; +esac diff --git a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c b/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c deleted file mode 100644 index a6cffb54..00000000 --- a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c +++ /dev/null @@ -1,457 +0,0 @@ -/* tox_dht_bootstrap_server_daemon - * - * A simple DHT bootstrap server for tox - daemon edition. - * - * Copyright (C) 2014 Tox project All Rights Reserved. - * - * This file is part of Tox. - * - * Tox is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Tox is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Tox. If not, see . - * - */ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "../../toxcore/DHT.h" -#include "../../toxcore/friend_requests.h" -#include "../../toxcore/LAN_discovery.h" - -#include "../../testing/misc_tools.c" - -#define DAEMON_NAME "tox_dht_bootstrap_server_daemon" - -#define SLEEP_TIME_MILLISECONDS 30 -#define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) - -#define DEFAULT_PID_FILE_PATH ".tox_dht_bootstrap_server_daemon.pid" -#define DEFAULT_KEYS_FILE_PATH ".tox_dht_bootstrap_server_daemon.keys" -#define DEFAULT_PORT 33445 -#define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false -#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false - - -// Uses the already existing key or creates one if it didn't exist -// -// retirns 1 on success -// 0 on failure - no keys were read or stored - -int manage_keys(DHT *dht, char *keys_file_path) -{ - const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; - uint8_t keys[KEYS_SIZE]; - FILE *keys_file; - - // Check if file exits, proceed to open and load keys - keys_file = fopen(keys_file_path, "r"); - - if (keys_file != NULL) { - size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); - - if (read_size != KEYS_SIZE) { - return 0; - } - - load_keys(dht->c, keys); - } else { - // Otherwise save new keys - new_keys(dht->c); - save_keys(dht->c, keys); - - keys_file = fopen(keys_file_path, "w"); - - size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); - - if (write_size != KEYS_SIZE) { - return 0; - } - } - - fclose(keys_file); - - // We want our DHT public key to be the same as our internal one since this is a bootstrap server - memcpy(dht->self_public_key, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES); - memcpy(dht->self_secret_key, dht->c->self_secret_key, crypto_box_SECRETKEYBYTES); - - return 1; -} - -// Gets general config options -// -// Important: you are responsible for freeing `pid_file_path` and `keys_file_path` -// -// returns 1 on success -// 0 on failure, doesn't modify any data pointed by arguments - -int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, - int *enable_lan_discovery) -{ - config_t cfg; - - const char *NAME_PORT = "port"; - const char *NAME_PID_FILE_PATH = "pid_file_path"; - const char *NAME_KEYS_FILE_PATH = "keys_file_path"; - const char *NAME_ENABLE_IPV6 = "enable_ipv6"; - const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; - - config_init(&cfg); - - // Read the file. If there is an error, report it and exit. - if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { - syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); - config_destroy(&cfg); - return 0; - } - - // Get port - if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); - syslog(LOG_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); - *port = DEFAULT_PORT; - } - - // Get PID file location - const char *tmp_pid_file; - - if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); - tmp_pid_file = DEFAULT_PID_FILE_PATH; - } - - *pid_file_path = malloc(strlen(tmp_pid_file) + 1); - strcpy(*pid_file_path, tmp_pid_file); - - // Get keys file location - const char *tmp_keys_file; - - if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); - tmp_keys_file = DEFAULT_KEYS_FILE_PATH; - } - - *keys_file_path = malloc(strlen(tmp_keys_file) + 1); - strcpy(*keys_file_path, tmp_keys_file); - - // Get IPv6 option - if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); - *enable_ipv6 = DEFAULT_ENABLE_IPV6; - } - - // Get LAN discovery option - if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, - DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); - *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; - } - - config_destroy(&cfg); - - syslog(LOG_DEBUG, "Successfully read:\n"); - syslog(LOG_DEBUG, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); - syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); - syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port); - syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); - syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); - - return 1; -} - -// Bootstraps servers listed in the config file -// -// returns 1 on success, some or no bootstrap servers were added -// 0 on failure, a error accured while parsing config file - -int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) -{ - const char *NAME_BOOTSTRAP_SERVERS = "bootstrap_servers"; - - const char *NAME_PUBLIC_KEY = "public_key"; - const char *NAME_PORT = "port"; - const char *NAME_ADDRESS = "address"; - - config_t cfg; - - config_init(&cfg); - - if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { - syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); - config_destroy(&cfg); - return 0; - } - - config_setting_t *server_list = config_lookup(&cfg, NAME_BOOTSTRAP_SERVERS); - - if (server_list == NULL) { - syslog(LOG_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_SERVERS); - config_destroy(&cfg); - return 1; - } - - if (config_setting_length(server_list) == 0) { - syslog(LOG_WARNING, "No bootstrap servers found. Skipping bootstrapping.\n"); - config_destroy(&cfg); - return 1; - } - - int bs_port; - const char *bs_address; - const char *bs_public_key; - - config_setting_t *server; - - int i = 0; - - while (config_setting_length(server_list)) { - - server = config_setting_get_elem(server_list, 0); - - if (server == NULL) { - config_destroy(&cfg); - return 0; - } - - // Check that all settings are present - if (config_setting_lookup_string(server, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { - syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PUBLIC_KEY); - goto next; - } - - if (config_setting_lookup_int(server, NAME_PORT, &bs_port) == CONFIG_FALSE) { - syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PORT); - goto next; - } - - if (config_setting_lookup_string(server, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { - syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_ADDRESS); - goto next; - } - - // Process settings - if (strlen(bs_public_key) != 64) { - syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_PUBLIC_KEY, bs_public_key); - goto next; - } - - // not (1 <= port <= 65535) - if (bs_port < 1 || bs_port > 65535) { - syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %d. Skipping the server.\n", i, NAME_PORT, bs_port); - goto next; - } - - const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), - hex_string_to_bin((char *)bs_public_key)); - - if (!address_resolved) { - syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_ADDRESS, bs_address); - goto next; - } - - syslog(LOG_DEBUG, "Successfully added bootstrap server #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); - -next: - // config_setting_lookup_string() allocates string inside and doesn't allow us to free it - // so in order to reuse `bs_public_key` and `bs_address` we have to remove the element - // which will cause libconfig to free allocated strings - config_setting_remove_elem(server_list, 0); - i++; - } - - config_destroy(&cfg); - - return 1; -} - -// Prints public key - -void print_public_key(uint8_t *public_key) -{ - char buffer[64 + 1]; - int index = 0; - - int i; - - for (i = 0; i < 32; i++) { - if (public_key[i] < 16) { - index += sprintf(buffer + index, "0"); - } - - index += sprintf(buffer + index, "%hhX", public_key[i]); - } - - syslog(LOG_INFO, "Public Key: %s\n", buffer); - - return; -} - -int main(int argc, char *argv[]) -{ - openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); - - if (argc < 2) { - syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); - return 1; - } - - char *cfg_file_path = argv[1]; - char *pid_file_path, *keys_file_path; - int port; - int enable_ipv6; - int enable_lan_discovery; - - if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery)) { - syslog(LOG_DEBUG, "General config read successfully\n"); - } else { - syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); - return 1; - } - - // not (1 <= port <= 65535) - if (port < 1 || port > 65535) { - syslog(LOG_ERR, "Invalid port: %d, must be 1 <= port <= 65535. Exiting.\n", port); - return 1; - } - - // Check if the PID file exists - if (fopen(pid_file_path, "r")) { - syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists. Exiting.\n", pid_file_path); - return 1; - } - - IP ip; - ip_init(&ip, enable_ipv6); - - DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port))); - - if (dht == NULL) { - syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n"); - return 1; - } - - Onion *onion = new_onion(dht); - Onion_Announce *onion_a = new_onion_announce(dht); - - if (!(onion && onion_a)) { - syslog(LOG_ERR, "Couldn't initialize Tox Onion. Exiting.\n"); - return 1; - } - - if (enable_lan_discovery) { - LANdiscovery_init(dht); - } - - if (manage_keys(dht, keys_file_path)) { - syslog(LOG_DEBUG, "Keys are managed successfully\n"); - } else { - syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); - return 1; - } - - if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { - syslog(LOG_DEBUG, "List of bootstrap servers read successfully\n"); - } else { - syslog(LOG_ERR, "Couldn't read list of bootstrap servers in %s. Exiting.\n", cfg_file_path); - return 1; - } - - print_public_key(dht->c->self_public_key); - - // Write the PID file - FILE *pidf = fopen(pid_file_path, "w"); - - if (pidf == NULL) { - syslog(LOG_ERR, "Can't open the PID file for writing: %s. Exiting.\n", pid_file_path); - return 1; - } - - free(pid_file_path); - free(keys_file_path); - - // Fork off from the parent process - pid_t pid = fork(); - - if (pid < 0) { - syslog(LOG_ERR, "Forking failed. Exiting.\n"); - return 1; - } - - if (pid > 0) { - syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid); - return 0; - } - - // Change the file mode mask - umask(0); - - fprintf(pidf, "%d\n", pid); - fclose(pidf); - - // Create a new SID for the child process - if (setsid() < 0) { - syslog(LOG_ERR, "SID creation failure. Exiting.\n"); - return 1; - } - - // Change the current working directory - if ((chdir("/")) < 0) { - syslog(LOG_ERR, "Couldn't change working directory to '/'. Exiting.\n"); - return 1; - } - - // Go quiet - close(STDOUT_FILENO); - close(STDIN_FILENO); - close(STDERR_FILENO); - - uint64_t last_LANdiscovery = 0; - uint16_t htons_port = htons(port); - - int waiting_for_dht_connection = 1; - - while (1) { - do_DHT(dht); - - if (enable_lan_discovery && is_timeout(last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) { - send_LANdiscovery(htons_port, dht); - last_LANdiscovery = unix_time(); - } - - networking_poll(dht->net); - - if (waiting_for_dht_connection && DHT_isconnected(dht)) { - syslog(LOG_DEBUG, "Connected to other bootstrap server successfully.\n"); - waiting_for_dht_connection = 0; - } - - sleep; - } - - return 1; -} diff --git a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.sh b/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.sh deleted file mode 100644 index 678db5e3..00000000 --- a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.sh +++ /dev/null @@ -1,109 +0,0 @@ -#! /bin/sh -### BEGIN INIT INFO -# Provides: tox_dht_bootstrap_server_daemon -# Required-Start: $remote_fs $syslog -# Required-Stop: $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Starts the Tox bootstrapping server -# Description: Starts the Tox bootstrapping server -### END INIT INFO - -# PATH should only include /usr/* if it runs after the mountnfs.sh script -PATH=/sbin:/usr/sbin:/bin:/usr/bin -DESC="ProjectTox bootstrap server daemon" -NAME=tox_dht_bootstrap_server_daemon -USER=tom -CFG=/home/$USER/.$NAME/conf -DAEMON=/home/$USER/$NAME -DAEMON_ARGS="$CFG" -PIDFILE=/home/$USER/.$NAME/pid -SCRIPTNAME=/etc/init.d/$NAME - -# Exit if the package is not installed -[ -x "$DAEMON" ] || exit 0 - -# Read configuration variable file if it is present -#[ -r /etc/default/$NAME ] && . /etc/default/$NAME - -# Load the VERBOSE setting and other rcS variables -. /lib/init/vars.sh - -# Define LSB log_* functions. -# Depend on lsb-base (>= 3.2-14) to ensure that this file is present -# and status_of_proc is working. -. /lib/lsb/init-functions - -# -# Function that starts the daemon/service -# -do_start() -{ - start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ - || return 1 - start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \ - $DAEMON_ARGS \ - || return 2 - sleep 1 -} - -# -# Function that stops the daemon/service -# -do_stop() -{ - start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON - RETVAL="$?" - [ "$RETVAL" = 2 ] && return 2 - - start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON - [ "$?" = 2 ] && return 2 - # Many daemons don't delete their pidfiles when they exit. - rm -f $PIDFILE - return "$RETVAL" -} - -case "$1" in - start) - [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" - do_start - case "$?" in - 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; - 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; - esac - ;; - stop) - [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" - do_stop - case "$?" in - 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; - 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; - esac - ;; - status) - status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $? - ;; - - restart) #|force-reload) - log_daemon_msg "Restarting $DESC" "$NAME" - do_stop - case "$?" in - 0|1) - do_start - case "$?" in - 0) log_end_msg 0 ;; - 1) log_end_msg 1 ;; # Old process is still running - *) log_end_msg 1 ;; # Failed to start - esac - ;; - *) - # Failed to stop - log_end_msg 1 - ;; - esac - ;; - *) - echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2 - exit 3 - ;; -esac -- cgit v1.2.3 From 04580cf122f4b85bd9f49a7d2c7f4df7299ed9d7 Mon Sep 17 00:00:00 2001 From: tawm Date: Thu, 30 Jan 2014 15:56:45 -0500 Subject: Fixed PID forgetfulness. --- other/bootstrap_serverdaemon/tox_bootstrap_daemon.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'other') diff --git a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c index e82e49cd..f2c54bb1 100644 --- a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c @@ -398,11 +398,14 @@ int main(int argc, char *argv[]) pid_t pid = fork(); if (pid < 0) { + fclose(pidf); syslog(LOG_ERR, "Forking failed. Exiting.\n"); return 1; } if (pid > 0) { + fprintf(pidf, "%d\n", pid); + fclose(pidf); syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid); return 0; } @@ -410,9 +413,6 @@ int main(int argc, char *argv[]) // Change the file mode mask umask(0); - fprintf(pidf, "%d\n", pid); - fclose(pidf); - // Create a new SID for the child process if (setsid() < 0) { syslog(LOG_ERR, "SID creation failure. Exiting.\n"); -- cgit v1.2.3