Merge branch 'tox-bootstrapd-network-error-handling' of https://github.com/nurupo/InsertProjectNameHere

This commit is contained in:
irungentoo 2014-10-15 20:01:43 -04:00
commit 6114bd7f3e
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
5 changed files with 78 additions and 32 deletions

View File

@ -21,7 +21,7 @@ Look at the variable declarations in the beginning of `tox-bootstrapd.sh` init s
Go over everything in `tox-bootstrapd.conf`. Make sure `pid_file_path` matches `PIDFILE` from `tox-bootstrapd.sh`. Go over everything in `tox-bootstrapd.conf`. Make sure `pid_file_path` matches `PIDFILE` from `tox-bootstrapd.sh`.
Place `tox-bootstrapd.conf` file to where `CFGFILE` variable from `tox-bootstrapd` tells. By default it's `/etc/tox-bootstrapd.conf`. Place `tox-bootstrapd.conf` file to where `CFGFILE` variable from `tox-bootstrapd.sh` tells. By default it's `/etc/tox-bootstrapd.conf`.
Place `tox-bootstrapd.sh` init file at `/etc/init.d/tox-bootstrapd` (note the disappearance of ".sh" ending). Place `tox-bootstrapd.sh` init file at `/etc/init.d/tox-bootstrapd` (note the disappearance of ".sh" ending).
@ -69,4 +69,4 @@ sudo grep "tox-bootstrapd" /var/log/syslog
- Make sure you have write permission for keys and pid files. - Make sure you have write permission for keys and pid files.
- Make sure you have read permission for the config file. - Make sure you have read permission for the config file.

View File

@ -53,7 +53,7 @@
#define DAEMON_NAME "tox-bootstrapd" #define DAEMON_NAME "tox-bootstrapd"
#define DAEMON_VERSION_NUMBER 2014101000UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day #define DAEMON_VERSION_NUMBER 2014101200UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day
#define SLEEP_TIME_MILLISECONDS 30 #define SLEEP_TIME_MILLISECONDS 30
#define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS)
@ -62,6 +62,7 @@
#define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys" #define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys"
#define DEFAULT_PORT 33445 #define DEFAULT_PORT 33445
#define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false #define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false
#define DEFAULT_ENABLE_IPV4_FALLBACK 1 // 1 - true, 0 - false
#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 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_TCP_RELAY 1 // 1 - true, 0 - false
#define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports. make sure to adjust DEFAULT_TCP_RELAY_PORTS_COUNT accordingly #define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports. make sure to adjust DEFAULT_TCP_RELAY_PORTS_COUNT accordingly
@ -88,7 +89,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
keys_file = fopen(keys_file_path, "r"); keys_file = fopen(keys_file_path, "r");
if (keys_file != NULL) { if (keys_file != NULL) {
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); const size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
if (read_size != KEYS_SIZE) { if (read_size != KEYS_SIZE) {
fclose(keys_file); fclose(keys_file);
@ -104,7 +105,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
keys_file = fopen(keys_file_path, "w"); keys_file = fopen(keys_file_path, "w");
size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); const size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
if (write_size != KEYS_SIZE) { if (write_size != KEYS_SIZE) {
fclose(keys_file); fclose(keys_file);
@ -161,7 +162,12 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
} }
// the loop above skips invalid ports, so we adjust the allocated memory size // 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)); if ((*tcp_relay_port_count) > 0) {
*tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t));
} else {
free(*tcp_relay_ports);
*tcp_relay_ports = NULL;
}
return; return;
} }
@ -210,8 +216,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
} }
// the loop above skips invalid ports, so we adjust the allocated memory size // the loop above skips invalid ports, so we adjust the allocated memory size
if ((*tcp_relay_port_count) * sizeof(uint16_t) > 0) { if ((*tcp_relay_port_count) > 0) {
*tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t));
} else {
free(*tcp_relay_ports);
*tcp_relay_ports = NULL;
} }
} }
@ -224,9 +233,10 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
// returns 1 on success // returns 1 on success
// 0 on failure, doesn't modify any data pointed by arguments // 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 get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port,
int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count, int *enable_ipv6,
int *enable_motd, char **motd) int *enable_ipv4_fallback, 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; config_t cfg;
@ -234,6 +244,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
const char *NAME_PID_FILE_PATH = "pid_file_path"; const char *NAME_PID_FILE_PATH = "pid_file_path";
const char *NAME_KEYS_FILE_PATH = "keys_file_path"; const char *NAME_KEYS_FILE_PATH = "keys_file_path";
const char *NAME_ENABLE_IPV6 = "enable_ipv6"; const char *NAME_ENABLE_IPV6 = "enable_ipv6";
const char *NAME_ENABLE_IPV4_FALLBACK = "enable_ipv4_fallback";
const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery";
const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay";
const char *NAME_ENABLE_MOTD = "enable_motd"; const char *NAME_ENABLE_MOTD = "enable_motd";
@ -286,6 +297,14 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
*enable_ipv6 = DEFAULT_ENABLE_IPV6; *enable_ipv6 = DEFAULT_ENABLE_IPV6;
} }
// Get IPv4 fallback option
if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) {
syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK);
syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK,
DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false");
*enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK;
}
// Get LAN discovery option // Get LAN discovery option
if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { 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, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
@ -340,6 +359,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_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': %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_IPV6, *enable_ipv6 ? "true" : "false");
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "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_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
@ -372,7 +392,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
// returns 1 on success, some or no bootstrap nodes were added // returns 1 on success, some or no bootstrap nodes were added
// 0 on failure, a error accured while parsing config file // 0 on failure, a error accured while parsing config file
int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
{ {
const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes"; const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes";
@ -477,7 +497,7 @@ next:
// Prints public key // Prints public key
void print_public_key(uint8_t *public_key) void print_public_key(const uint8_t *public_key)
{ {
char buffer[2 * crypto_box_PUBLICKEYBYTES + 1]; char buffer[2 * crypto_box_PUBLICKEYBYTES + 1];
int index = 0; int index = 0;
@ -504,10 +524,11 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
char *cfg_file_path = argv[1]; const char *cfg_file_path = argv[1];
char *pid_file_path, *keys_file_path; char *pid_file_path, *keys_file_path;
int port; int port;
int enable_ipv6; int enable_ipv6;
int enable_ipv4_fallback;
int enable_lan_discovery; int enable_lan_discovery;
int enable_tcp_relay; int enable_tcp_relay;
uint16_t *tcp_relay_ports; uint16_t *tcp_relay_ports;
@ -515,8 +536,8 @@ int main(int argc, char *argv[])
int enable_motd; int enable_motd;
char *motd; char *motd;
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_ipv4_fallback,
&enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
syslog(LOG_DEBUG, "General config read successfully\n"); syslog(LOG_DEBUG, "General config read successfully\n");
} else { } else {
syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
@ -539,7 +560,27 @@ int main(int argc, char *argv[])
IP ip; IP ip;
ip_init(&ip, enable_ipv6); ip_init(&ip, enable_ipv6);
DHT *dht = new_DHT(new_networking(ip, port)); Networking_Core *net = new_networking(ip, port);
if (net == NULL) {
if (enable_ipv6 && enable_ipv4_fallback) {
syslog(LOG_DEBUG, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
enable_ipv6 = 0;
ip_init(&ip, enable_ipv6);
net = new_networking(ip, port);
if (net == NULL) {
syslog(LOG_DEBUG, "Couldn't fallback to IPv4. Exiting.\n");
return 1;
}
} else {
syslog(LOG_DEBUG, "Couldn't initialize networking. Exiting.\n");
return 1;
}
}
DHT *dht = new_DHT(net);
if (dht == NULL) { if (dht == NULL) {
syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n"); syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n");
@ -615,7 +656,7 @@ int main(int argc, char *argv[])
free(keys_file_path); free(keys_file_path);
// Fork off from the parent process // Fork off from the parent process
pid_t pid = fork(); const pid_t pid = fork();
if (pid > 0) { if (pid > 0) {
fprintf(pidf, "%d", pid); fprintf(pidf, "%d", pid);
@ -652,7 +693,7 @@ int main(int argc, char *argv[])
close(STDERR_FILENO); close(STDERR_FILENO);
uint64_t last_LANdiscovery = 0; uint64_t last_LANdiscovery = 0;
uint16_t htons_port = htons(port); const uint16_t htons_port = htons(port);
int waiting_for_dht_connection = 1; int waiting_for_dht_connection = 1;

View File

@ -1,46 +1,51 @@
// Tox DHT bootstrap daemon configuration file. // Tox DHT bootstrap daemon configuration file.
// Listening port. // Listening port (UDP).
port = 33445 port = 33445
// A 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.
// If there is no key file, a new one will be generated. // If there is no key file, a new one will be generated.
// The daemon should have permission to read/write to it. // The daemon should have permission to read/write it.
keys_file_path = "/var/lib/tox-bootstrapd/keys" keys_file_path = "/var/lib/tox-bootstrapd/keys"
// The PID file written to by daemon. // The PID file written to by the daemon.
// Make sure that the user who runs the daemon has permissions to write to the // Make sure that the user that daemon runs as has permissions to write to the
// PID file. // PID file.
pid_file_path = "/var/run/tox-bootstrapd/tox-bootstrapd.pid" pid_file_path = "/var/run/tox-bootstrapd/tox-bootstrapd.pid"
// Enable IPv6. // Enable IPv6.
enable_ipv6 = false enable_ipv6 = true
// Fallback to IPv4 in case IPv6 fails.
enable_ipv4_fallback = true
// Automatically bootstrap with nodes on local area network. // Automatically bootstrap with nodes on local area network.
enable_lan_discovery = true enable_lan_discovery = true
enable_tcp_relay = true enable_tcp_relay = true
// Tox uses 443, 3389 and 33445 ports by default, so it's highly recommended to keep // Tox uses 443, 3389 and 33445 ports by default, so it's highly encouraged to keep
// them. // them.
tcp_relay_ports = [443, 3389, 33445] tcp_relay_ports = [443, 3389, 33445]
// It's planned to use message of the day as a convenient method of checking // Reply to MOTD (Message Of The Day) requests.
// whether a node is up or not, though there are other methods of doing that.
enable_motd = true enable_motd = true
// Just a message that is sent when someone requests MOTD.
// Put anything you want, but note that it will be trimmed to fit into 255 bytes.
motd = "tox-bootstrapd" motd = "tox-bootstrapd"
// Any number of nodes the daemon will bootstrap itself off. // Any number of nodes the daemon will bootstrap itself off.
//
// Remember to replace the provided example with your own node list. // 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 // There is a maintained list of bootstrap nodes on Tox's wiki, if you need it
// (http://wiki.tox.im/Nodes). // (http://wiki.tox.im/Nodes).
//
// You may leave the list empty or remove "bootstrap_nodes" completely, // You may leave the list empty or remove "bootstrap_nodes" completely,
// in both cases this will be interpreted as if you don't want to bootstrap // in both cases this will be interpreted as if you don't want to bootstrap
// from anyone. // from anyone.
// address = any ipv4 or Any ipv4 or ipv6, depending on whether `enable_ipv6` //
// is set or not, and also any US-ASCII domain name. // address = any ipv4 or ipv6 address and also any US-ASCII domain name.
bootstrap_nodes = ( bootstrap_nodes = (
{ {
// NikolaiToryzin - US // NikolaiToryzin - US

View File

@ -23,7 +23,7 @@ USER=tox-bootstrapd
GROUP=tox-bootstrapd GROUP=tox-bootstrapd
# Exit if the package is not installed # Exit if the package is not installed
[ -x "$DAEMON" ] || exit 5 [ -x "$DAEMON" ] || exit 0
# Load the VERBOSE setting and other rcS variables # Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh . /lib/init/vars.sh

View File

@ -351,8 +351,8 @@ void networking_poll(Networking_Core *net);
* ip must be in network order EX: 127.0.0.1 = (7F000001). * ip must be in network order EX: 127.0.0.1 = (7F000001).
* port is in host byte order (this means don't worry about it). * port is in host byte order (this means don't worry about it).
* *
* return 0 if no problems. * return Networking_Core object if no problems
* return -1 if there were problems. * return NULL if there are problems.
*/ */
Networking_Core *new_networking(IP ip, uint16_t port); Networking_Core *new_networking(IP ip, uint16_t port);