From b9ef9b91aff17533254073538467684d62ed465f Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 25 Jan 2014 20:00:34 -0500 Subject: [PATCH 1/3] Added more error checking --- .../tox_dht_bootstrap_server_daemon.c | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) 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; } From 4782a8475a3476642d5a9f747a5de7080a44b1ea Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 25 Jan 2014 20:37:01 -0500 Subject: [PATCH 2/3] Made bootstrapping optional --- other/bootstrap_serverdaemon/conf | 35 +++++------ .../tox_dht_bootstrap_server_daemon.c | 61 ++++++------------- 2 files changed, 37 insertions(+), 59 deletions(-) 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; } From 553472442fd1ddcf224b7a3bbfc1ed51c2cedfad Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 25 Jan 2014 21:00:31 -0500 Subject: [PATCH 3/3] Shortened daemon's name --- other/bootstrap_serverdaemon/Makefile.inc | 12 +++++----- other/bootstrap_serverdaemon/README.md | 22 +++++++++---------- other/bootstrap_serverdaemon/conf | 4 ++-- ...server_daemon.c => tox_bootstrap_daemon.c} | 10 ++++----- ...rver_daemon.sh => tox_bootstrap_daemon.sh} | 11 +++++----- 5 files changed, 30 insertions(+), 29 deletions(-) rename other/bootstrap_serverdaemon/{tox_dht_bootstrap_server_daemon.c => tox_bootstrap_daemon.c} (97%) rename other/bootstrap_serverdaemon/{tox_dht_bootstrap_server_daemon.sh => tox_bootstrap_daemon.sh} (88%) 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_dht_bootstrap_server_daemon.c b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c similarity index 97% rename from other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c rename to other/bootstrap_serverdaemon/tox_bootstrap_daemon.c index a6cffb54..e82e49cd 100644 --- a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c +++ b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c @@ -1,6 +1,6 @@ -/* tox_dht_bootstrap_server_daemon +/* tox_bootstrap_daemon.c * - * A simple DHT bootstrap server for tox - daemon edition. + * Tox DHT bootstrap server daemon. * * Copyright (C) 2014 Tox project All Rights Reserved. * @@ -42,13 +42,13 @@ #include "../../testing/misc_tools.c" -#define DAEMON_NAME "tox_dht_bootstrap_server_daemon" +#define DAEMON_NAME "tox_bootstrap_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_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 diff --git a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.sh b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh similarity index 88% rename from other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.sh rename to other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh index 678db5e3..83d9a119 100644 --- a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.sh +++ b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh @@ -1,18 +1,19 @@ #! /bin/sh ### BEGIN INIT INFO -# Provides: tox_dht_bootstrap_server_daemon +# 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 bootstrapping server -# Description: Starts the Tox bootstrapping server +# 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="ProjectTox bootstrap server daemon" -NAME=tox_dht_bootstrap_server_daemon +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