From 046f92cf523124d29c6ff1730e51a4a2b14b8abf Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Thu, 20 Feb 2014 18:14:20 -0500 Subject: Removed typedefs for consistency --- toxcore/tox.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'toxcore') diff --git a/toxcore/tox.c b/toxcore/tox.c index bdef1eaa..0115e827 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -385,23 +385,19 @@ void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int, /* Set the callback for status type changes. * function(int friendnumber, USERSTATUS kind) */ -void tox_callback_user_status(Tox *tox, void (*_function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata) +void tox_callback_user_status(Tox *tox, void (*function)(Messenger *tox, int, TOX_USERSTATUS, void *), void *userdata) { Messenger *m = tox; - typedef void (*function_type)(Messenger *, int, USERSTATUS, void *); - function_type function = (function_type)_function; m_callback_userstatus(m, function, userdata); } /* Set the callback for typing changes. * function (int friendnumber, int is_typing) */ -void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int, int, void *), void *userdata) +void tox_callback_typing_change(Tox *tox, void (*function)(Messenger *tox, int, int, void *), void *userdata) { Messenger *m = tox; - typedef void (*function_type)(Messenger *, int, int, void *); - function_type function_new = (function_type)function; - m_callback_typingchange(m, function_new, userdata); + m_callback_typingchange(m, function, userdata); } /* Set the callback for read receipts. -- cgit v1.2.3 From 5a142bb697651693e4ce7654d2f74e688aab8894 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 22 Feb 2014 17:06:07 -0500 Subject: Renamed dht server to dht node --- build/Makefile.am | 2 +- docs/TCP_Network.txt | 4 +- docs/updates/DHT.md | 2 +- other/DHT_bootstrap.c | 12 +- other/DHTnodes | 3 + other/DHTservers | 3 - other/Makefile.inc | 2 +- other/bootstrap_daemon/Makefile.inc | 27 ++ other/bootstrap_daemon/README.md | 62 +++ other/bootstrap_daemon/conf | 42 ++ other/bootstrap_daemon/tox_bootstrap_daemon.c | 460 +++++++++++++++++++++ other/bootstrap_daemon/tox_bootstrap_daemon.sh | 110 +++++ other/bootstrap_node_packets.c | 65 +++ other/bootstrap_server_packets.c | 65 --- other/bootstrap_serverdaemon/Makefile.inc | 27 -- other/bootstrap_serverdaemon/README.md | 62 --- other/bootstrap_serverdaemon/conf | 41 -- .../bootstrap_serverdaemon/tox_bootstrap_daemon.c | 460 --------------------- .../bootstrap_serverdaemon/tox_bootstrap_daemon.sh | 110 ----- testing/nTox.c | 2 +- toxcore/network.h | 2 +- 21 files changed, 782 insertions(+), 781 deletions(-) create mode 100644 other/DHTnodes delete mode 100644 other/DHTservers create mode 100644 other/bootstrap_daemon/Makefile.inc create mode 100644 other/bootstrap_daemon/README.md create mode 100644 other/bootstrap_daemon/conf create mode 100644 other/bootstrap_daemon/tox_bootstrap_daemon.c create mode 100644 other/bootstrap_daemon/tox_bootstrap_daemon.sh create mode 100644 other/bootstrap_node_packets.c delete mode 100644 other/bootstrap_server_packets.c delete mode 100644 other/bootstrap_serverdaemon/Makefile.inc delete mode 100644 other/bootstrap_serverdaemon/README.md delete mode 100644 other/bootstrap_serverdaemon/conf delete mode 100644 other/bootstrap_serverdaemon/tox_bootstrap_daemon.c delete mode 100644 other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh (limited to 'toxcore') diff --git a/build/Makefile.am b/build/Makefile.am index 5514a4d5..aa004388 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -8,5 +8,5 @@ include ../toxcore/Makefile.inc include ../toxav/Makefile.inc include ../other/Makefile.inc include ../testing/Makefile.inc -include ../other/bootstrap_serverdaemon/Makefile.inc +include ../other/bootstrap_daemon/Makefile.inc include ../auto_tests/Makefile.inc diff --git a/docs/TCP_Network.txt b/docs/TCP_Network.txt index f44cca71..e8608791 100644 --- a/docs/TCP_Network.txt +++ b/docs/TCP_Network.txt @@ -12,9 +12,9 @@ can use Tox regardless of the quality of their internet connection. How it's probably going to work: 1. Alice, a Tox client on a TCP only network generates a temporary public key -and connects to a bootstrap server. +and connects to a bootstrap node. -2. Using the bootstrap server she find and connects to a couple (exact number +2. Using the bootstrap node she find and connects to a couple (exact number to be determined later) number of random nodes that have TCP relay support. 3. She uses the onion through the TCP relay connections to send friend requests diff --git a/docs/updates/DHT.md b/docs/updates/DHT.md index 79ec8c4b..db825a3c 100644 --- a/docs/updates/DHT.md +++ b/docs/updates/DHT.md @@ -7,7 +7,7 @@ But: Vastly simplified packet format and encryption. Boostrapping: -The first time you install the client we bootstrap it with a node on our servers.(bandwidth should not be a problem as the client only needs to be sent one reply.) +The first time you install the client we bootstrap it with a node. (bandwidth should not be a problem as the client only needs to be sent one reply.) Basics diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 307f80f6..291d6589 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -1,7 +1,7 @@ /* DHT boostrap * - * A simple DHT boostrap server for tox. + * A simple DHT boostrap node for tox. * * Copyright (C) 2013 Tox project All Rights Reserved. * @@ -33,8 +33,8 @@ #include "../testing/misc_tools.c" -#ifdef DHT_SERVER_EXTRA_PACKETS -#include "./bootstrap_server_packets.c" +#ifdef DHT_NODE_EXTRA_PACKETS +#include "./bootstrap_node_packets.c" #define DHT_VERSION_NUMBER 1 #define DHT_MOTD "This is a test motd" @@ -111,7 +111,7 @@ int main(int argc, char *argv[]) Onion *onion = new_onion(dht); Onion_Announce *onion_a = new_onion_announce(dht); -#ifdef DHT_SERVER_EXTRA_PACKETS +#ifdef DHT_NODE_EXTRA_PACKETS bootstrap_set_callbacks(dht->net, DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD)); #endif @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) perror("Initialization"); manage_keys(dht); - /* We want our DHT public key to be the same as our internal one since this is a bootstrap server */ + /* We want our DHT public key to be the same as our internal one since this is a bootstrap node */ 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); printf("Public key: "); @@ -166,7 +166,7 @@ int main(int argc, char *argv[]) while (1) { if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { - printf("Connected to other bootstrap server successfully.\n"); + printf("Connected to other bootstrap node successfully.\n"); is_waiting_for_dht_connection = 0; } diff --git a/other/DHTnodes b/other/DHTnodes new file mode 100644 index 00000000..35509931 --- /dev/null +++ b/other/DHTnodes @@ -0,0 +1,3 @@ +As maintaining 2 seperate lists of the same information seemed redundant, this list has been phased out. + +For a current DHT node list please visit http://wiki.tox.im/nodes diff --git a/other/DHTservers b/other/DHTservers deleted file mode 100644 index 48e1d115..00000000 --- a/other/DHTservers +++ /dev/null @@ -1,3 +0,0 @@ -As maintaining 2 seperate lists of the same information seemed redundant, this list has been phased out. - -For a current DHT server list please visit http://wiki.tox.im/Servers diff --git a/other/Makefile.inc b/other/Makefile.inc index 5ec2fc2a..72a514ad 100644 --- a/other/Makefile.inc +++ b/other/Makefile.inc @@ -15,5 +15,5 @@ DHT_bootstrap_LDADD = $(LIBSODIUM_LDFLAGS) \ $(NACL_LIBS) \ $(WINSOCK2_LIBS) -EXTRA_DIST += $(top_srcdir)/other/DHTservers \ +EXTRA_DIST += $(top_srcdir)/other/DHTnodes \ $(top_srcdir)/other/tox.png diff --git a/other/bootstrap_daemon/Makefile.inc b/other/bootstrap_daemon/Makefile.inc new file mode 100644 index 00000000..b75d935f --- /dev/null +++ b/other/bootstrap_daemon/Makefile.inc @@ -0,0 +1,27 @@ +if BUILD_DHT_BOOTSTRAP_DAEMON + +noinst_PROGRAMS += tox_bootstrap_daemon + +tox_bootstrap_daemon_SOURCES = \ + ../other/bootstrap_daemon/tox_bootstrap_daemon.c + +tox_bootstrap_daemon_CFLAGS = \ + -I$(top_srcdir)/other/bootstrap_daemon \ + $(LIBSODIUM_CFLAGS) \ + $(NACL_CFLAGS) \ + $(LIBCONFIG_CFLAGS) + +tox_bootstrap_daemon_LDADD = \ + $(LIBSODIUM_LDFLAGS) \ + $(NACL_LDFLAGS) \ + libtoxcore.la \ + $(LIBCONFIG_LIBS) \ + $(LIBSODIUM_LIBS) \ + $(NACL_LIBS) + +endif + +EXTRA_DIST += \ + $(top_srcdir)/other/bootstrap_daemon/conf \ + $(top_srcdir)/other/bootstrap_daemon/tox_bootstrap_daemon.sh + \ No newline at end of file diff --git a/other/bootstrap_daemon/README.md b/other/bootstrap_daemon/README.md new file mode 100644 index 00000000..53a25cdb --- /dev/null +++ b/other/bootstrap_daemon/README.md @@ -0,0 +1,62 @@ +##Instructions for Debian + +The following commands are to be executed as root: + +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_bootstrap_daemon.sh` + +3. Execute: +``` +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_bootstrap_daemon +``` + +5. Execute: +``` +update-rc.d tox_bootstrap_daemon defaults +``` + +6. Start the service: +``` +service tox_bootstrap_daemon start +``` + +7. Verify that the service is running: +``` +service tox_bootstrap_daemon status +``` + +-- + +You can see daemon's log with +``` +grep "tox_bootstrap_daemon" /var/log/syslog +``` + +**Note that system log is where you find your public key** + +-- + +###Troubleshooting: + +1. Check the log for errors with +``` +grep "tox_bootstrap_daemon" /var/log/syslog +``` + +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_bootstrap_daemon` matches with the `pid_file_path` from `conf` + +4. Make sure you have write permission to keys and pid files + +5. Make sure you have read permission for config file \ No newline at end of file diff --git a/other/bootstrap_daemon/conf b/other/bootstrap_daemon/conf new file mode 100644 index 00000000..28e638c1 --- /dev/null +++ b/other/bootstrap_daemon/conf @@ -0,0 +1,42 @@ +// ProjectTox dht bootstrap node daemon configuration file. + +// 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. +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_bootstrap_daemon/pid" + +// Enable IPv6. +enable_ipv6 = false + +// 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 node list. +// There is a maintained list of bootstrap nodes on Tox's wiki, if you need it. +// You may leave the list empty or remove "bootstrap_nodes" complitely, +// in both cases this will be interpreted as if you don't want to bootstrap +// from anyone. +bootstrap_nodes = ( + { // Node 1 + // 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" + }, + { // Node 2 + address = "example.org" + port = 33445 + public_key = "8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858" + } +) diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.c b/other/bootstrap_daemon/tox_bootstrap_daemon.c new file mode 100644 index 00000000..66f6436d --- /dev/null +++ b/other/bootstrap_daemon/tox_bootstrap_daemon.c @@ -0,0 +1,460 @@ +/* tox_bootstrap_daemon.c + * + * Tox DHT bootstrap node 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 node + 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 nodes listed in the config file +// +// returns 1 on success, some or no bootstrap nodes 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_NODES = "bootstrap_nodes"; + + 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 *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES); + + if (node_list == NULL) { + syslog(LOG_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES); + config_destroy(&cfg); + return 1; + } + + if (config_setting_length(node_list) == 0) { + syslog(LOG_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); + config_destroy(&cfg); + return 1; + } + + int bs_port; + const char *bs_address; + const char *bs_public_key; + + config_setting_t *node; + + int i = 0; + + while (config_setting_length(node_list)) { + + node = config_setting_get_elem(node_list, 0); + + if (node == NULL) { + config_destroy(&cfg); + return 0; + } + + // Check that all settings are present + if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY); + goto next; + } + + if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); + goto next; + } + + if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); + goto next; + } + + // Process settings + if (strlen(bs_public_key) != 64) { + syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\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 node #%d: Invalid '%s': %d. Skipping the node.\n", i, NAME_PORT, bs_port); + goto next; + } + + uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key); + const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), + bs_public_key_bin); + free(bs_public_key_bin); + + if (!address_resolved) { + syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); + goto next; + } + + syslog(LOG_DEBUG, "Successfully added bootstrap node #%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(node_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 nodes read successfully\n"); + } else { + syslog(LOG_ERR, "Couldn't read list of bootstrap nodes 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) { + 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; + } + + // Change the file mode mask + umask(0); + + // 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 node successfully.\n"); + waiting_for_dht_connection = 0; + } + + sleep; + } + + return 1; +} diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.sh b/other/bootstrap_daemon/tox_bootstrap_daemon.sh new file mode 100644 index 00000000..83d9a119 --- /dev/null +++ b/other/bootstrap_daemon/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_node_packets.c b/other/bootstrap_node_packets.c new file mode 100644 index 00000000..ea57b0d2 --- /dev/null +++ b/other/bootstrap_node_packets.c @@ -0,0 +1,65 @@ +/* bootstrap_node_packets.c + * + * Special bootstrap node only packets. + * + * Include it in your bootstrap node and use: bootstrap_set_callbacks() to enable. + * + * Copyright (C) 2013 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 . + * + */ + +#define MAX_MOTD_LENGTH 256 /* I recommend you use a maximum of 96 bytes. The hard maximum is this though. */ + +#define INFO_REQUEST_PACKET_LENGTH 78 + +static uint32_t bootstrap_version; +static uint8_t bootstrap_motd[MAX_MOTD_LENGTH]; +static uint16_t bootstrap_motd_length; + +/* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH + * with the first byte being BOOTSTRAP_INFO_PACKET_ID + */ +static int handle_info_request(void *object, IP_Port source, uint8_t *packet, uint32_t length) +{ + if (length != INFO_REQUEST_PACKET_LENGTH) + return 1; + + uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH]; + data[0] = BOOTSTRAP_INFO_PACKET_ID; + memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version)); + uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length; + memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length); + + if (sendpacket(object, source, data, len) == len) + return 0; + + return 1; +} + +int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, uint8_t *motd, uint16_t motd_length) +{ + if (motd_length > MAX_MOTD_LENGTH) + return -1; + + bootstrap_version = htonl(version); + memcpy(bootstrap_motd, motd, motd_length); + bootstrap_motd_length = motd_length; + + networking_registerhandler(net, BOOTSTRAP_INFO_PACKET_ID, &handle_info_request, net); + return 0; +} diff --git a/other/bootstrap_server_packets.c b/other/bootstrap_server_packets.c deleted file mode 100644 index c9297535..00000000 --- a/other/bootstrap_server_packets.c +++ /dev/null @@ -1,65 +0,0 @@ -/* bootstrap_server_packets.c - * - * Special bootstrap server only packets. - * - * Include it in your bootstrap server and use: bootstrap_set_callbacks() to enable. - * - * Copyright (C) 2013 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 . - * - */ - -#define MAX_MOTD_LENGTH 256 /* I recommend you use a maximum of 96 bytes. The hard maximum is this though. */ - -#define INFO_REQUEST_PACKET_LENGTH 78 - -static uint32_t bootstrap_version; -static uint8_t bootstrap_motd[MAX_MOTD_LENGTH]; -static uint16_t bootstrap_motd_length; - -/* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH - * with the first byte being BOOTSTRAP_INFO_PACKET_ID - */ -static int handle_info_request(void *object, IP_Port source, uint8_t *packet, uint32_t length) -{ - if (length != INFO_REQUEST_PACKET_LENGTH) - return 1; - - uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH]; - data[0] = BOOTSTRAP_INFO_PACKET_ID; - memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version)); - uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length; - memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length); - - if (sendpacket(object, source, data, len) == len) - return 0; - - return 1; -} - -int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, uint8_t *motd, uint16_t motd_length) -{ - if (motd_length > MAX_MOTD_LENGTH) - return -1; - - bootstrap_version = htonl(version); - memcpy(bootstrap_motd, motd, motd_length); - bootstrap_motd_length = motd_length; - - networking_registerhandler(net, BOOTSTRAP_INFO_PACKET_ID, &handle_info_request, net); - return 0; -} diff --git a/other/bootstrap_serverdaemon/Makefile.inc b/other/bootstrap_serverdaemon/Makefile.inc deleted file mode 100644 index effe59e8..00000000 --- a/other/bootstrap_serverdaemon/Makefile.inc +++ /dev/null @@ -1,27 +0,0 @@ -if BUILD_DHT_BOOTSTRAP_DAEMON - -noinst_PROGRAMS += tox_bootstrap_daemon - -tox_bootstrap_daemon_SOURCES = \ - ../other/bootstrap_serverdaemon/tox_bootstrap_daemon.c - -tox_bootstrap_daemon_CFLAGS = \ - -I$(top_srcdir)/other/bootstrap_serverdaemon \ - $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) \ - $(LIBCONFIG_CFLAGS) - -tox_bootstrap_daemon_LDADD = \ - $(LIBSODIUM_LDFLAGS) \ - $(NACL_LDFLAGS) \ - libtoxcore.la \ - $(LIBCONFIG_LIBS) \ - $(LIBSODIUM_LIBS) \ - $(NACL_LIBS) - -endif - -EXTRA_DIST += \ - $(top_srcdir)/other/bootstrap_serverdaemon/conf \ - $(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 deleted file mode 100644 index 53a25cdb..00000000 --- a/other/bootstrap_serverdaemon/README.md +++ /dev/null @@ -1,62 +0,0 @@ -##Instructions for Debian - -The following commands are to be executed as root: - -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_bootstrap_daemon.sh` - -3. Execute: -``` -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_bootstrap_daemon -``` - -5. Execute: -``` -update-rc.d tox_bootstrap_daemon defaults -``` - -6. Start the service: -``` -service tox_bootstrap_daemon start -``` - -7. Verify that the service is running: -``` -service tox_bootstrap_daemon status -``` - --- - -You can see daemon's log with -``` -grep "tox_bootstrap_daemon" /var/log/syslog -``` - -**Note that system log is where you find your public key** - --- - -###Troubleshooting: - -1. Check the log for errors with -``` -grep "tox_bootstrap_daemon" /var/log/syslog -``` - -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_bootstrap_daemon` matches with the `pid_file_path` from `conf` - -4. Make sure you have write permission to keys and pid files - -5. Make sure you have read permission for config file \ No newline at end of file diff --git a/other/bootstrap_serverdaemon/conf b/other/bootstrap_serverdaemon/conf deleted file mode 100644 index 8451d9a0..00000000 --- a/other/bootstrap_serverdaemon/conf +++ /dev/null @@ -1,41 +0,0 @@ -// ProjectTox dht bootstrap server daemon configuration file. - -// 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. -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_bootstrap_daemon/pid" - -// Enable IPv6. -enable_ipv6 = false - -// Automatically bootstrap with nodes on local area network. -enable_lan_discovery = true - -// 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, and also - // any US-ASCII domain name. - address = "198.46.136.167" - port = 33445 - public_key = "728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854" - }, - { // Server 2 - address = "example.org" - port = 33445 - public_key = "8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858" - } -) diff --git a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c deleted file mode 100644 index b7684115..00000000 --- a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c +++ /dev/null @@ -1,460 +0,0 @@ -/* 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; - } - - uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key); - const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), - bs_public_key_bin); - free(bs_public_key_bin); - - 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) { - 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; - } - - // Change the file mode mask - umask(0); - - // 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 deleted file mode 100644 index 83d9a119..00000000 --- a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.sh +++ /dev/null @@ -1,110 +0,0 @@ -#! /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/testing/nTox.c b/testing/nTox.c index 8f7dc7ea..9d3eb9c0 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -995,7 +995,7 @@ void print_help(char *prog_name) puts("Options: (order IS relevant)"); puts(" --ipv4 / --ipv6 [Optional] Support IPv4 only or IPv4 & IPv6."); - puts(" IP PORT KEY [REQUIRED] A server to connect to (IP/Port) and its key."); + puts(" IP PORT KEY [REQUIRED] A node to connect to (IP/Port) and its key."); puts(" -f keyfile [Optional] Specify a keyfile to read from and write to."); } diff --git a/toxcore/network.h b/toxcore/network.h index aaf89f19..b0d306e4 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -149,7 +149,7 @@ typedef int sock_t; #define NET_PACKET_ONION_RECV_2 141 #define NET_PACKET_ONION_RECV_1 142 -/* Only used for bootstrap servers */ +/* Only used for bootstrap nodes */ #define BOOTSTRAP_INFO_PACKET_ID 240 -- cgit v1.2.3