From ceaeae8cffad13403618c2b4ffaf90d854e36cbe Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 17 May 2014 20:36:16 -0400 Subject: Added TCP server functionality --- other/bootstrap_daemon/conf | 6 ++ other/bootstrap_daemon/tox_bootstrap_daemon.c | 141 ++++++++++++++++++++++++-- 2 files changed, 140 insertions(+), 7 deletions(-) diff --git a/other/bootstrap_daemon/conf b/other/bootstrap_daemon/conf index 28e638c1..fb524754 100644 --- a/other/bootstrap_daemon/conf +++ b/other/bootstrap_daemon/conf @@ -20,6 +20,12 @@ enable_ipv6 = false // Automatically bootstrap with nodes on local area network. enable_lan_discovery = true +enable_tcp_relay = true + +// Tox uses 443 and 3389 ports by default, so it's highly recommended to keep +// them +tcp_relay_ports = [443, 3389] + // 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. diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.c b/other/bootstrap_daemon/tox_bootstrap_daemon.c index abad7760..8d3ed94a 100644 --- a/other/bootstrap_daemon/tox_bootstrap_daemon.c +++ b/other/bootstrap_daemon/tox_bootstrap_daemon.c @@ -21,25 +21,33 @@ * */ -#include +// system provided +#include +#include #include +#include #include -#include +// C #include #include -#include -#include #include +// 3rd party +#include + +// ./configure #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "../../toxcore/onion_announce.h" +// toxcore #include "../../toxcore/LAN_discovery.h" +#include "../../toxcore/onion_announce.h" +#include "../../toxcore/TCP_server.h" #include "../../toxcore/util.h" +// misc #include "../../testing/misc_tools.c" #define DAEMON_NAME "tox_bootstrap_daemon" @@ -52,6 +60,7 @@ #define DEFAULT_PORT 33445 #define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false +#define DEFAULT_ENABLE_TCP_RELAY 1 // Uses the already existing key or creates one if it didn't exist @@ -96,15 +105,79 @@ int manage_keys(DHT *dht, char *keys_file_path) return 1; } +// Parses tcp relay ports from `cfg` and puts them into `tcp_relay_ports` array +// +// Supposed to be called from get_general_config only +// +// Important: iff `tcp_relay_port_count` > 0, then you are responsible for freeing `tcp_relay_ports` + +void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) +{ + const char *NAME_TCP_RELAY_PORTS = "tcp_relay_ports"; + + *tcp_relay_port_count = 0; + + config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS); + + if (ports_array == NULL) { + syslog(LOG_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); + return; + } + + if (config_setting_is_array(ports_array) == CONFIG_FALSE) { + syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n"); + return; + } + + int config_port_count = config_setting_length(ports_array); + if (config_port_count == 0) { + syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); + return; + } + + *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t)); + + config_setting_t *elem; + int i; + + for (i = 0; i < config_port_count; i ++) { + + elem = config_setting_get_elem(ports_array, i); + + if (elem == NULL) { + // it's NULL if `ports_array` is not an array (we have that check ealier) or if `i` is out of range, which should not be + syslog(LOG_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); + break; + } + + if (config_setting_is_number(elem) == CONFIG_FALSE) { + syslog(LOG_WARNING, "Port #%d: Not a number. Skipping.\n", i); + continue; + } + + (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); + if ((*tcp_relay_ports)[i] < 1 || (*tcp_relay_ports)[i] > 65535) { + syslog(LOG_WARNING, "Port #%d: Invalid port value, should be in [1, 65535]. Skipping.\n", i); + continue; + } + + (*tcp_relay_port_count) ++; + } + + // the loop above skips invalid ports, so we adjust the allocated memory size + *tcp_relay_ports = realloc(*tcp_relay_ports, *tcp_relay_port_count * sizeof(uint16_t)); +} + // Gets general config options // // Important: you are responsible for freeing `pid_file_path` and `keys_file_path` +// also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports` // // 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) + int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) { config_t cfg; @@ -113,6 +186,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi 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"; + const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; config_init(&cfg); @@ -169,6 +243,20 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; } + // Get TCP relay option + if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { + syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); + syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, + DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); + *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; + } + + if (*enable_tcp_relay) { + parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count); + } else { + *tcp_relay_port_count = 0; + } + config_destroy(&cfg); syslog(LOG_DEBUG, "Successfully read:\n"); @@ -177,6 +265,19 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi 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"); + syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); + // show info about tcp ports only if tcp relay is enabled + if (*enable_tcp_relay) { + if (*tcp_relay_port_count == 0) { + syslog(LOG_DEBUG, "No TCP ports could be read.\n"); + } else { + syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count); + int i; + for (i = 0; i < *tcp_relay_port_count; i ++) { + syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); + } + } + } return 1; } @@ -325,8 +426,11 @@ int main(int argc, char *argv[]) int port; int enable_ipv6; int enable_lan_discovery; + int enable_tcp_relay; + uint16_t *tcp_relay_ports; + int tcp_relay_port_count; - if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery)) { + if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count)) { syslog(LOG_DEBUG, "General config read successfully\n"); } else { syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); @@ -374,6 +478,25 @@ int main(int argc, char *argv[]) return 1; } + TCP_Server *tcp_server = NULL; + + if (enable_tcp_relay) { + if (tcp_relay_port_count == 0) { + syslog(LOG_ERR, "No TCP relay ports read. Exiting.\n"); + return 1; + } + + tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key, dht->self_secret_key, onion); + + // tcp_relay_port_count != 0 at this point + free(tcp_relay_ports); + + if (tcp_server == NULL) { + syslog(LOG_ERR, "Couldn't initialize Tox TCP server. Exiting.\n"); + return 1; + } + } + if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { syslog(LOG_DEBUG, "List of bootstrap nodes read successfully\n"); } else { @@ -443,6 +566,10 @@ int main(int argc, char *argv[]) last_LANdiscovery = unix_time(); } + if (enable_tcp_relay) { + do_TCP_server(tcp_server); + } + networking_poll(dht->net); if (waiting_for_dht_connection && DHT_isconnected(dht)) { -- cgit v1.2.3 From 8be6b0986dadee88c2c7506cfcb41a332127add9 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 17 May 2014 20:49:42 -0400 Subject: Extracted min and max allowed port values into named constants --- other/bootstrap_daemon/tox_bootstrap_daemon.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.c b/other/bootstrap_daemon/tox_bootstrap_daemon.c index 8d3ed94a..dce181a4 100644 --- a/other/bootstrap_daemon/tox_bootstrap_daemon.c +++ b/other/bootstrap_daemon/tox_bootstrap_daemon.c @@ -62,6 +62,9 @@ #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false #define DEFAULT_ENABLE_TCP_RELAY 1 +#define MIN_ALLOWED_PORT 1 +#define MAX_ALLOWED_PORT 65535 + // Uses the already existing key or creates one if it didn't exist // @@ -156,8 +159,8 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int } (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); - if ((*tcp_relay_ports)[i] < 1 || (*tcp_relay_ports)[i] > 65535) { - syslog(LOG_WARNING, "Port #%d: Invalid port value, should be in [1, 65535]. Skipping.\n", i); + if ((*tcp_relay_ports)[i] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[i] > MAX_ALLOWED_PORT) { + syslog(LOG_WARNING, "Port #%d: Invalid port value, should be in [%d, %d]. Skipping.\n", i, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); continue; } @@ -359,9 +362,8 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) 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); + if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { + syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); goto next; } @@ -437,9 +439,8 @@ int main(int argc, char *argv[]) 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); + if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) { + syslog(LOG_ERR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); return 1; } -- cgit v1.2.3 From e75930e72e40a69aea94240415af9ca53604ad0e Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 17 May 2014 20:50:17 -0400 Subject: Some small fixes --- other/bootstrap_daemon/tox_bootstrap_daemon.c | 29 ++++++++++++-------------- other/bootstrap_daemon/tox_bootstrap_daemon.sh | 4 ++-- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.c b/other/bootstrap_daemon/tox_bootstrap_daemon.c index dce181a4..410a3453 100644 --- a/other/bootstrap_daemon/tox_bootstrap_daemon.c +++ b/other/bootstrap_daemon/tox_bootstrap_daemon.c @@ -50,6 +50,7 @@ // misc #include "../../testing/misc_tools.c" + #define DAEMON_NAME "tox_bootstrap_daemon" #define SLEEP_TIME_MILLISECONDS 30 @@ -60,7 +61,7 @@ #define DEFAULT_PORT 33445 #define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false -#define DEFAULT_ENABLE_TCP_RELAY 1 +#define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false #define MIN_ALLOWED_PORT 1 #define MAX_ALLOWED_PORT 65535 @@ -356,7 +357,7 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) } // Process settings - if (strlen(bs_public_key) != 64) { + if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES*2) { syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, bs_public_key); goto next; @@ -380,9 +381,9 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) 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_lookup_string() allocates string inside and doesn't allow us to free it direcly + // though it's freed when the element is removed, so we free it right away in order to keep memory + // consumption minimal config_setting_remove_elem(node_list, 0); i++; } @@ -396,17 +397,13 @@ next: void print_public_key(uint8_t *public_key) { - char buffer[64 + 1]; + char buffer[2*crypto_box_PUBLICKEYBYTES + 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]); + for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++) { + index += sprintf(buffer + index, "%02hhX", public_key[i]); } syslog(LOG_INFO, "Public Key: %s\n", buffer); @@ -468,10 +465,6 @@ int main(int argc, char *argv[]) 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 { @@ -559,6 +552,10 @@ int main(int argc, char *argv[]) int waiting_for_dht_connection = 1; + if (enable_lan_discovery) { + LANdiscovery_init(dht); + } + while (1) { do_DHT(dht); diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.sh b/other/bootstrap_daemon/tox_bootstrap_daemon.sh index 83d9a119..e083ea0b 100644 --- a/other/bootstrap_daemon/tox_bootstrap_daemon.sh +++ b/other/bootstrap_daemon/tox_bootstrap_daemon.sh @@ -11,12 +11,12 @@ # 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" +DESC="Tox DHT bootstrap 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=/home/$USER/.$NAME/$NAME DAEMON_ARGS="$CFG" PIDFILE=/home/$USER/.$NAME/pid SCRIPTNAME=/etc/init.d/$NAME -- cgit v1.2.3 From 7dd811214ad335224f554ae658ce7b4c430a40c8 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 17 May 2014 23:57:24 -0400 Subject: Added MOTD functionality --- other/bootstrap_daemon/conf | 14 ++++-- other/bootstrap_daemon/tox_bootstrap_daemon.c | 69 ++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/other/bootstrap_daemon/conf b/other/bootstrap_daemon/conf index fb524754..bbdf7652 100644 --- a/other/bootstrap_daemon/conf +++ b/other/bootstrap_daemon/conf @@ -3,7 +3,7 @@ // Listening port. port = 33445 -// The key file is like a password, so keep it where no one can read it. +// A 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" @@ -23,9 +23,15 @@ enable_lan_discovery = true enable_tcp_relay = true // Tox uses 443 and 3389 ports by default, so it's highly recommended to keep -// them +// them. tcp_relay_ports = [443, 3389] +// It's planned to use message of the day as a convenient method of checking +// whether a node is up or not, though there are other methods of doing that. +enable_motd = true + +motd = "tox_bootstrap_daemon" + // 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. @@ -34,8 +40,8 @@ tcp_relay_ports = [443, 3389] // 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. + // Any ipv4 or ipv6, depending on whether `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_daemon/tox_bootstrap_daemon.c b/other/bootstrap_daemon/tox_bootstrap_daemon.c index 410a3453..6853358a 100644 --- a/other/bootstrap_daemon/tox_bootstrap_daemon.c +++ b/other/bootstrap_daemon/tox_bootstrap_daemon.c @@ -48,10 +48,12 @@ #include "../../toxcore/util.h" // misc +#include "../bootstrap_node_packets.c" #include "../../testing/misc_tools.c" #define DAEMON_NAME "tox_bootstrap_daemon" +#define DAEMON_VERSION_NUMBER 2014051700UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day #define SLEEP_TIME_MILLISECONDS 30 #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) @@ -62,6 +64,8 @@ #define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false #define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false +#define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false +#define DEFAULT_MOTD DAEMON_NAME #define MIN_ALLOWED_PORT 1 #define MAX_ALLOWED_PORT 65535 @@ -129,7 +133,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int } if (config_setting_is_array(ports_array) == CONFIG_FALSE) { - syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n"); + syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", NAME_TCP_RELAY_PORTS); return; } @@ -161,7 +165,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); if ((*tcp_relay_ports)[i] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[i] > MAX_ALLOWED_PORT) { - syslog(LOG_WARNING, "Port #%d: Invalid port value, should be in [%d, %d]. Skipping.\n", i, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); + syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[i], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); continue; } @@ -176,12 +180,14 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int // // Important: you are responsible for freeing `pid_file_path` and `keys_file_path` // also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports` +// and also `motd` iff `enable_motd` is set // // 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, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) + int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count, + int *enable_motd, char **motd) { config_t cfg; @@ -191,6 +197,8 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi const char *NAME_ENABLE_IPV6 = "enable_ipv6"; const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; + const char *NAME_ENABLE_MOTD = "enable_motd"; + const char *NAME_MOTD = "motd"; config_init(&cfg); @@ -261,6 +269,30 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi *tcp_relay_port_count = 0; } + // Get MOTD option + if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { + syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); + syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, + DEFAULT_ENABLE_MOTD ? "true" : "false"); + *enable_motd = DEFAULT_ENABLE_MOTD; + } + + if (*enable_motd) { + // Get MOTD + const char *tmp_motd; + + if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) { + syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); + syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); + tmp_motd = DEFAULT_MOTD; + } + size_t tmp_motd_length = strlen(tmp_motd) + 1; + size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length; + *motd = malloc(motd_length); + strncpy(*motd, tmp_motd, motd_length); + (*motd)[MAX_MOTD_LENGTH - 1] = '\0'; + } + config_destroy(&cfg); syslog(LOG_DEBUG, "Successfully read:\n"); @@ -269,6 +301,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi 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"); + syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); // show info about tcp ports only if tcp relay is enabled if (*enable_tcp_relay) { @@ -283,6 +316,11 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi } } + syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); + if (*enable_motd) { + syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd); + } + return 1; } @@ -415,6 +453,8 @@ int main(int argc, char *argv[]) { openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); + syslog(LOG_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER); + if (argc < 2) { syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); return 1; @@ -428,8 +468,10 @@ int main(int argc, char *argv[]) int enable_tcp_relay; uint16_t *tcp_relay_ports; int tcp_relay_port_count; + int enable_motd; + char *motd; - if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count)) { + if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { syslog(LOG_DEBUG, "General config read successfully\n"); } else { syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); @@ -465,8 +507,18 @@ int main(int argc, char *argv[]) return 1; } + if (enable_motd) { + if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t*)motd, strlen(motd) + 1) == 0) { + syslog(LOG_DEBUG, "Set MOTD successfully.\n"); + } else { + syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd); + return 1; + } + free(motd); + } + if (manage_keys(dht, keys_file_path)) { - syslog(LOG_DEBUG, "Keys are managed successfully\n"); + syslog(LOG_DEBUG, "Keys are managed successfully.\n"); } else { syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); return 1; @@ -485,14 +537,16 @@ int main(int argc, char *argv[]) // tcp_relay_port_count != 0 at this point free(tcp_relay_ports); - if (tcp_server == NULL) { + if (tcp_server != NULL) { + syslog(LOG_DEBUG, "Initialized Tox TCP server successfully.\n"); + } else { syslog(LOG_ERR, "Couldn't initialize Tox TCP server. Exiting.\n"); return 1; } } if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { - syslog(LOG_DEBUG, "List of bootstrap nodes read successfully\n"); + 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; @@ -554,6 +608,7 @@ int main(int argc, char *argv[]) if (enable_lan_discovery) { LANdiscovery_init(dht); + syslog(LOG_DEBUG, "Initialized LAN discovery.\n"); } while (1) { -- cgit v1.2.3