diff options
Diffstat (limited to 'protocols/Tox/toxcore/other')
-rw-r--r-- | protocols/Tox/toxcore/other/DHT_bootstrap.c | 202 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/DHTnodes | 3 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/Makefile.inc | 20 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/bootstrap_daemon/Makefile.inc | 27 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/bootstrap_daemon/README.md | 62 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/bootstrap_daemon/conf | 54 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/bootstrap_daemon/tox_bootstrap_daemon.c | 685 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/bootstrap_daemon/tox_bootstrap_daemon.sh | 110 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/bootstrap_node_packets.c | 65 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/fun/bootstrap_node_info.py | 98 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/fun/cracker.c | 75 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/fun/sign.c | 130 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/fun/strkey.c | 137 | ||||
-rw-r--r-- | protocols/Tox/toxcore/other/tox.png | bin | 0 -> 17922 bytes |
14 files changed, 1668 insertions, 0 deletions
diff --git a/protocols/Tox/toxcore/other/DHT_bootstrap.c b/protocols/Tox/toxcore/other/DHT_bootstrap.c new file mode 100644 index 0000000000..462360c361 --- /dev/null +++ b/protocols/Tox/toxcore/other/DHT_bootstrap.c @@ -0,0 +1,202 @@ + +/* DHT boostrap + * + * A simple DHT boostrap node for tox. + * + * 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 <http://www.gnu.org/licenses/>. + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "../toxcore/DHT.h" +#include "../toxcore/LAN_discovery.h" +#include "../toxcore/friend_requests.h" +#include "../toxcore/util.h" + +#define TCP_RELAY_ENABLED + +#ifdef TCP_RELAY_ENABLED +#include "../toxcore/TCP_server.h" +#endif + +#include "../testing/misc_tools.c" + +#ifdef DHT_NODE_EXTRA_PACKETS +#include "./bootstrap_node_packets.c" + +#define DHT_VERSION_NUMBER 1 +#define DHT_MOTD "This is a test motd" +#endif + +/* Sleep function (x = milliseconds) */ +#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#define c_sleep(x) Sleep(1*x) +#else +#include <unistd.h> +#include <arpa/inet.h> +#define c_sleep(x) usleep(1000*x) +#endif + +#define PORT 33445 + + +void manage_keys(DHT *dht) +{ + const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; + uint8_t keys[KEYS_SIZE]; + + FILE *keys_file = fopen("key", "r"); + + if (keys_file != NULL) { + /* If file was opened successfully -- load keys, + otherwise save new keys */ + size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); + + if (read_size != KEYS_SIZE) { + printf("Error while reading the key file\nExiting.\n"); + exit(1); + } + + memcpy(dht->self_public_key, keys, crypto_box_PUBLICKEYBYTES); + memcpy(dht->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); + printf("Keys loaded successfully.\n"); + } else { + memcpy(keys, dht->self_public_key, crypto_box_PUBLICKEYBYTES); + memcpy(keys + crypto_box_PUBLICKEYBYTES, dht->self_secret_key, crypto_box_SECRETKEYBYTES); + keys_file = fopen("key", "w"); + + if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) { + printf("Error while writing the key file.\nExiting.\n"); + exit(1); + } + + printf("Keys saved successfully.\n"); + } + + fclose(keys_file); +} + +int main(int argc, char *argv[]) +{ + if (argc == 2 && !strncasecmp(argv[1], "-h", 3)) { + printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]); + printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]); + exit(0); + } + + /* let user override default by cmdline */ + uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ + int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + + if (argvoffset < 0) + exit(1); + + /* Initialize networking - + Bind to ip 0.0.0.0 / [::] : PORT */ + IP ip; + ip_init(&ip, ipv6enabled); + + DHT *dht = new_DHT(new_networking(ip, PORT)); + Onion *onion = new_onion(dht); + Onion_Announce *onion_a = new_onion_announce(dht); + +#ifdef DHT_NODE_EXTRA_PACKETS + bootstrap_set_callbacks(dht->net, DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD)); +#endif + + if (!(onion && onion_a)) { + printf("Something failed to initialize.\n"); + exit(1); + } + + perror("Initialization"); + + manage_keys(dht); + printf("Public key: "); + uint32_t i; + +#ifdef TCP_RELAY_ENABLED +#define NUM_PORTS 3 + uint16_t ports[NUM_PORTS] = {443, 3389, PORT}; + TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht->self_public_key, dht->self_secret_key, onion); + + if (tcp_s == NULL) { + printf("TCP server failed to initialize.\n"); + exit(1); + } + +#endif + + FILE *file; + file = fopen("PUBLIC_ID.txt", "w"); + + for (i = 0; i < 32; i++) { + printf("%02hhX", dht->self_public_key[i]); + fprintf(file, "%02hhX", dht->self_public_key[i]); + } + + fclose(file); + + printf("\n"); + printf("Port: %u\n", ntohs(dht->net->port)); + + if (argc > argvoffset + 3) { + printf("Trying to bootstrap into the network...\n"); + uint16_t port = htons(atoi(argv[argvoffset + 2])); + uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); + int res = DHT_bootstrap_from_address(dht, argv[argvoffset + 1], + ipv6enabled, port, bootstrap_key); + free(bootstrap_key); + + if (!res) { + printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); + exit(1); + } + } + + int is_waiting_for_dht_connection = 1; + + uint64_t last_LANdiscovery = 0; + LANdiscovery_init(dht); + + while (1) { + if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { + printf("Connected to other bootstrap node successfully.\n"); + is_waiting_for_dht_connection = 0; + } + + do_DHT(dht); + + if (is_timeout(last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) { + send_LANdiscovery(htons(PORT), dht); + last_LANdiscovery = unix_time(); + } + +#ifdef TCP_RELAY_ENABLED + do_TCP_server(tcp_s); +#endif + networking_poll(dht->net); + + c_sleep(1); + } + + return 0; +} diff --git a/protocols/Tox/toxcore/other/DHTnodes b/protocols/Tox/toxcore/other/DHTnodes new file mode 100644 index 0000000000..0abdbbd95d --- /dev/null +++ b/protocols/Tox/toxcore/other/DHTnodes @@ -0,0 +1,3 @@ +As maintaining 2 separate 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/protocols/Tox/toxcore/other/Makefile.inc b/protocols/Tox/toxcore/other/Makefile.inc new file mode 100644 index 0000000000..368a32f2d5 --- /dev/null +++ b/protocols/Tox/toxcore/other/Makefile.inc @@ -0,0 +1,20 @@ +bin_PROGRAMS += DHT_bootstrap + +DHT_bootstrap_SOURCES = ../other/DHT_bootstrap.c \ + ../toxcore/DHT.h \ + ../toxcore/friend_requests.h + +DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \ + $(LIBSODIUM_CFLAGS) \ + $(NACL_CFLAGS) + +DHT_bootstrap_LDADD = $(LIBSODIUM_LDFLAGS) \ + $(NACL_LDFLAGS) \ + libtoxcore.la \ + $(LIBSODIUM_LIBS) \ + $(NACL_OBJECTS) \ + $(NACL_LIBS) \ + $(WINSOCK2_LIBS) + +EXTRA_DIST += $(top_srcdir)/other/DHTnodes \ + $(top_srcdir)/other/tox.png diff --git a/protocols/Tox/toxcore/other/bootstrap_daemon/Makefile.inc b/protocols/Tox/toxcore/other/bootstrap_daemon/Makefile.inc new file mode 100644 index 0000000000..0bc02ef93a --- /dev/null +++ b/protocols/Tox/toxcore/other/bootstrap_daemon/Makefile.inc @@ -0,0 +1,27 @@ +if BUILD_DHT_BOOTSTRAP_DAEMON + +bin_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 + diff --git a/protocols/Tox/toxcore/other/bootstrap_daemon/README.md b/protocols/Tox/toxcore/other/bootstrap_daemon/README.md new file mode 100644 index 0000000000..53a25cdbfb --- /dev/null +++ b/protocols/Tox/toxcore/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/protocols/Tox/toxcore/other/bootstrap_daemon/conf b/protocols/Tox/toxcore/other/bootstrap_daemon/conf new file mode 100644 index 0000000000..c05beff1a7 --- /dev/null +++ b/protocols/Tox/toxcore/other/bootstrap_daemon/conf @@ -0,0 +1,54 @@ +// ProjectTox dht bootstrap node daemon configuration file. + +// Listening port. +port = 33445 + +// 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/.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/.tox_bootstrap_daemon.pid" + +// Enable IPv6. +enable_ipv6 = false + +// Automatically bootstrap with nodes on local area network. +enable_lan_discovery = true + +enable_tcp_relay = true + +// Tox uses 443, 3389 and 33445 ports by default, so it's highly recommended to keep +// them. +tcp_relay_ports = [443, 3389, 33445] + +// 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. +// 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 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" + }, + { // Node 2 + address = "example.org" + port = 33445 + public_key = "8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858" + } +) diff --git a/protocols/Tox/toxcore/other/bootstrap_daemon/tox_bootstrap_daemon.c b/protocols/Tox/toxcore/other/bootstrap_daemon/tox_bootstrap_daemon.c new file mode 100644 index 0000000000..5f8f9f76d8 --- /dev/null +++ b/protocols/Tox/toxcore/other/bootstrap_daemon/tox_bootstrap_daemon.c @@ -0,0 +1,685 @@ +/* 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 <http://www.gnu.org/licenses/>. + * + */ + +// system provided +#include <arpa/inet.h> +#include <syslog.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +// C +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// 3rd party +#include <libconfig.h> + +// ./configure +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +// toxcore +#include "../../toxcore/LAN_discovery.h" +#include "../../toxcore/onion_announce.h" +#include "../../toxcore/TCP_server.h" +#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 2014051800UL // 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) + +#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 +#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_COUNT 3 +#define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false +#define DEFAULT_MOTD DAEMON_NAME + +#define MIN_ALLOWED_PORT 1 +#define MAX_ALLOWED_PORT 65535 + + +// 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) { + fclose(keys_file); + return 0; + } + + memcpy(dht->self_public_key, keys, crypto_box_PUBLICKEYBYTES); + memcpy(dht->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); + } else { + // Otherwise save new keys + memcpy(keys, dht->self_public_key, crypto_box_PUBLICKEYBYTES); + memcpy(keys + crypto_box_PUBLICKEYBYTES, dht->self_secret_key, crypto_box_SECRETKEYBYTES); + + 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) { + fclose(keys_file); + return 0; + } + } + + fclose(keys_file); + + 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); + syslog(LOG_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); + + uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS}; + + int i; + + for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { + syslog(LOG_WARNING, "Port #%d: %u\n", i, default_ports[i]); + } + + // similar procedure to the one of reading config file below + *tcp_relay_ports = malloc(DEFAULT_TCP_RELAY_PORTS_COUNT * sizeof(uint16_t)); + + for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { + + (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i]; + + if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT + || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { + syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, + (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); + 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)); + + 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", + NAME_TCP_RELAY_PORTS); + 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)); + + int i; + + for (i = 0; i < config_port_count; i ++) { + config_setting_t *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)[*tcp_relay_port_count] < MIN_ALLOWED_PORT + || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { + syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, + (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); + 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` +// 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_motd, char **motd) +{ + 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"; + const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; + const char *NAME_ENABLE_MOTD = "enable_motd"; + const char *NAME_MOTD = "motd"; + + 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; + } + + // 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; + } + + // 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)[motd_length - 1] = '\0'; + } + + 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"); + + 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]); + } + } + } + + 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; +} + +// 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) != 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; + } + + 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; + } + + 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 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++; + } + + config_destroy(&cfg); + + return 1; +} + +// Prints public key + +void print_public_key(uint8_t *public_key) +{ + char buffer[2 * crypto_box_PUBLICKEYBYTES + 1]; + int index = 0; + + int 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); + + return; +} + +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; + } + + char *cfg_file_path = argv[1]; + char *pid_file_path, *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_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, &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); + return 1; + } + + 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; + } + + // Check if the PID file exists + FILE *pid_file; + + if (pid_file = fopen(pid_file_path, "r")) { + syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); + fclose(pid_file); + } + + IP ip; + ip_init(&ip, enable_ipv6); + + DHT *dht = new_DHT(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_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"); + } else { + syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); + 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_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"); + } else { + syslog(LOG_ERR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path); + return 1; + } + + print_public_key(dht->self_public_key); + + // Write the PID file + FILE *pidf = fopen(pid_file_path, "a+"); + + if (pidf == NULL) { + syslog(LOG_ERR, "Couldn'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) { + fprintf(pidf, "%d ", pid); + fclose(pidf); + syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid); + return 0; + } else { + fclose(pidf); + } + + if (pid < 0) { + syslog(LOG_ERR, "Forking failed. Exiting.\n"); + return 1; + } + + // 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; + + if (enable_lan_discovery) { + LANdiscovery_init(dht); + syslog(LOG_DEBUG, "Initialized LAN discovery.\n"); + } + + 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(); + } + + if (enable_tcp_relay) { + do_TCP_server(tcp_server); + } + + 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/protocols/Tox/toxcore/other/bootstrap_daemon/tox_bootstrap_daemon.sh b/protocols/Tox/toxcore/other/bootstrap_daemon/tox_bootstrap_daemon.sh new file mode 100644 index 0000000000..787498ecdc --- /dev/null +++ b/protocols/Tox/toxcore/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 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/$NAME +DAEMON_ARGS="$CFG" +PIDFILE=/home/$USER/.$NAME/."$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/protocols/Tox/toxcore/other/bootstrap_node_packets.c b/protocols/Tox/toxcore/other/bootstrap_node_packets.c new file mode 100644 index 0000000000..e8df3289ef --- /dev/null +++ b/protocols/Tox/toxcore/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 <http://www.gnu.org/licenses/>. + * + */ + +#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, const 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/protocols/Tox/toxcore/other/fun/bootstrap_node_info.py b/protocols/Tox/toxcore/other/fun/bootstrap_node_info.py new file mode 100644 index 0000000000..1734933472 --- /dev/null +++ b/protocols/Tox/toxcore/other/fun/bootstrap_node_info.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python +""" +Copyright (c) 2014 by nurupo <nurupo.contributions@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +""" + +from socket import * +import sys + +if sys.version_info[0] == 2: + print("This script requires Python 3+ in order to run.") + sys.exit(1) + +def printHelp(): + print("Usage: " + sys.argv[0] + " <ipv4|ipv6> <ip/hostname> <port>") + print(" Example: " + sys.argv[0] + " ipv4 192.210.149.121 33445") + print(" Example: " + sys.argv[0] + " ipv4 23.226.230.47 33445") + print(" Example: " + sys.argv[0] + " ipv4 biribiri.org 33445") + print(" Example: " + sys.argv[0] + " ipv4 cerberus.zodiaclabs.org 33445") + print(" Example: " + sys.argv[0] + " ipv6 2604:180:1::3ded:b280 33445") + print("") + print("Return values:") + print(" 0 - received info reply from a node") + print(" 1 - incorrect command line arguments") + print(" 2 - didn't receive any reply from a node") + print(" 3 - received a malformed/unexpected reply") + +if len(sys.argv) != 4: + printHelp() + sys.exit(1) + +protocol = sys.argv[1] +ip = sys.argv[2] +port = int(sys.argv[3]) + +INFO_PACKET_ID = b"\xF0" # https://github.com/irungentoo/toxcore/blob/4940c4c62b6014d1f0586aa6aca7bf6e4ecfcf29/toxcore/network.h#L128 +INFO_REQUEST_PACKET_LENGTH = 78 # https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L28 +# first byte is INFO_REQUEST_ID, other bytes don't matter as long as reqest's length matches INFO_REQUEST_LENGTH +INFO_REQUEST_PACKET = INFO_PACKET_ID + ( b"0" * (INFO_REQUEST_PACKET_LENGTH - len(INFO_PACKET_ID)) ) + +PACKET_ID_LENGTH = len(INFO_PACKET_ID) +VERSION_LENGTH = 4 # https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L44 +MAX_MOTD_LENGTH = 256 # https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L26 + +MAX_INFO_RESPONSE_PACKET_LENGTH = PACKET_ID_LENGTH + VERSION_LENGTH + MAX_MOTD_LENGTH + +SOCK_TIMEOUT_SECONDS = 1.0 + +sock = None + +if protocol == "ipv4": + sock = socket(AF_INET, SOCK_DGRAM) +elif protocol == "ipv6": + sock = socket(AF_INET6, SOCK_DGRAM) +else: + print("Invalid first argument") + printHelp() + sys.exit(1) + +sock.sendto(INFO_REQUEST_PACKET, (ip, port)) + +sock.settimeout(SOCK_TIMEOUT_SECONDS) + +try: + data, addr = sock.recvfrom(MAX_INFO_RESPONSE_PACKET_LENGTH) +except timeout: + print("The DHT bootstrap node didn't reply in " + str(SOCK_TIMEOUT_SECONDS) + " sec.") + print("The likely reason for that is that the DHT bootstrap node is either offline or has no info set.") + sys.exit(2) + +packetId = data[:PACKET_ID_LENGTH] +if packetId != INFO_PACKET_ID: + print("Bad response, first byte should be", INFO_PACKET_ID, "but got", packetId, "(", data, ")") + print("Are you sure that you are pointing the script at a Tox DHT bootstrap node and that the script is up to date?") + sys.exit(3) + +version = int.from_bytes(data[PACKET_ID_LENGTH:PACKET_ID_LENGTH + VERSION_LENGTH], byteorder='big') +motd = data[PACKET_ID_LENGTH + VERSION_LENGTH:].decode("utf-8") +print("Version: " + str(version)) +print("MOTD: " + motd) +sys.exit(0)
\ No newline at end of file diff --git a/protocols/Tox/toxcore/other/fun/cracker.c b/protocols/Tox/toxcore/other/fun/cracker.c new file mode 100644 index 0000000000..843b9359cb --- /dev/null +++ b/protocols/Tox/toxcore/other/fun/cracker.c @@ -0,0 +1,75 @@ +/* Public key cracker. + * + * Can be used to find public keys starting with specific hex (ABCD) for example. + * + * NOTE: There's probably a way to make this faster. + * + * Usage: ./cracker ABCDEF + * + * Will try to find a public key starting with: ABCDEF + */ + +#include "../../testing/misc_tools.c" +#include <time.h> + +/* NaCl includes*/ +#include <crypto_scalarmult_curve25519.h> +#include <randombytes.h> + +/* Sodium include*/ +//#include <sodium.h> + +void print_key(uint8_t *client_id) +{ + uint32_t j; + + for (j = 0; j < 32; j++) { + printf("%02hhX", client_id[j]); + } +} + + +int main(int argc, char *argv[]) +{ + if (argc < 2) { + printf("usage: ./cracker public_key(or beginning of one in hex format)\n"); + return 0; + } + + long long unsigned int num_tries = 0; + + uint32_t len = strlen(argv[1]) / 2; + unsigned char *key = hex_string_to_bin(argv[1]); + uint8_t pub_key[32], priv_key[32], c_key[32]; + + if (len > 32) + len = 32; + + memcpy(c_key, key, len); + free(key); + randombytes(priv_key, 32); + + while (1) { + crypto_scalarmult_curve25519_base(pub_key, priv_key); + uint32_t i; + + if (memcmp(c_key, pub_key, len) == 0) + break; + + for (i = 32; i != 0; --i) { + priv_key[i - 1] += 1; + + if (priv_key[i - 1] != 0) + break; + } + + ++num_tries; + } + + printf("Public key:\n"); + print_key(pub_key); + printf("\nPrivate key:\n"); + print_key(priv_key); + printf("\n %llu keys tried\n", num_tries); + return 0; +} diff --git a/protocols/Tox/toxcore/other/fun/sign.c b/protocols/Tox/toxcore/other/fun/sign.c new file mode 100644 index 0000000000..56a9d1e2ca --- /dev/null +++ b/protocols/Tox/toxcore/other/fun/sign.c @@ -0,0 +1,130 @@ +/* Binary signer/checker using ed25519 + * + * Compile with: + * gcc -o sign sign.c -lsodium + * + * Generate a keypair: + * ./sign g + * + * Sign a file: + * ./sign s PRIVATEKEY file.bin signedfile.bin + * + * Check a file: + * + * ./sign c PUBKEY signedfile.bin + * + * NOTE: The signature is appended to the end of the file. + */ +#include <sodium.h> +#include <string.h> +#include "../../testing/misc_tools.c" // hex_string_to_bin + +int load_file(char *filename, char **result) +{ + int size = 0; + FILE *f = fopen(filename, "rb"); + + if (f == NULL) { + *result = NULL; + return -1; // -1 means file opening fail + } + + fseek(f, 0, SEEK_END); + size = ftell(f); + fseek(f, 0, SEEK_SET); + *result = (char *)malloc(size + 1); + + if (size != fread(*result, sizeof(char), size, f)) { + free(*result); + fclose(f); + return -2; // -2 means file reading fail + } + + fclose(f); + (*result)[size] = 0; + return size; +} + +int main(int argc, char *argv[]) +{ + unsigned char pk[crypto_sign_ed25519_PUBLICKEYBYTES]; + unsigned char sk[crypto_sign_ed25519_SECRETKEYBYTES]; + + if (argc == 2 && argv[1][0] == 'g') { + crypto_sign_ed25519_keypair(pk, sk); + printf("Public key:\n"); + int i; + + for (i = 0; i < crypto_sign_ed25519_PUBLICKEYBYTES; i++) { + printf("%02hhX", pk[i]); + } + + printf("\nSecret key:\n"); + + for (i = 0; i < crypto_sign_ed25519_SECRETKEYBYTES; i++) { + printf("%02hhX", sk[i]); + } + + printf("\n"); + } + + if (argc == 5 && argv[1][0] == 's') { + unsigned char *secret_key = hex_string_to_bin(argv[2]); + char *data; + int size = load_file(argv[3], &data); + + if (size < 0) + goto fail; + + unsigned long long smlen; + char *sm = malloc(size + crypto_sign_ed25519_BYTES * 2); + crypto_sign_ed25519(sm, &smlen, data, size, secret_key); + free(secret_key); + + if (smlen - size != crypto_sign_ed25519_BYTES) + goto fail; + + FILE *f = fopen(argv[4], "wb"); + + if (f == NULL) + goto fail; + + memcpy(sm + smlen, sm, crypto_sign_ed25519_BYTES); // Move signature from beginning to end of file. + + if (fwrite(sm + (smlen - size), 1, smlen, f) != smlen) + goto fail; + + fclose(f); + printf("Signed successfully.\n"); + } + + if (argc == 4 && argv[1][0] == 'c') { + unsigned char *public_key = hex_string_to_bin(argv[2]); + char *data; + int size = load_file(argv[3], &data); + + if (size < 0) + goto fail; + + char *signe = malloc(size + crypto_sign_ed25519_BYTES); + memcpy(signe, data + size - crypto_sign_ed25519_BYTES, + crypto_sign_ed25519_BYTES); // Move signature from end to beginning of file. + memcpy(signe + crypto_sign_ed25519_BYTES, data, size - crypto_sign_ed25519_BYTES); + unsigned long long smlen; + char *m = malloc(size); + unsigned long long mlen; + + if (crypto_sign_ed25519_open(m, &mlen, signe, size, public_key) == -1) { + printf("Failed checking sig.\n"); + goto fail; + } + + printf("Checked successfully.\n"); + } + + return 0; + +fail: + printf("FAIL\n"); + return 1; +} diff --git a/protocols/Tox/toxcore/other/fun/strkey.c b/protocols/Tox/toxcore/other/fun/strkey.c new file mode 100644 index 0000000000..7e5a1e1cfb --- /dev/null +++ b/protocols/Tox/toxcore/other/fun/strkey.c @@ -0,0 +1,137 @@ +/* strkey -- String in Public Key + * + * Generates Tox's key pairs, checking if a certain string is in the public key. + * + * Requires sodium or nacl library. + * + * There seem to be some problems with the code working on Windows -- it works + * when built in debug mode with MinGW 4.8, but it doesn't work correctly when + * built in release. + * + * Usage: strkey <offset> <string> + * + * Offset - an integer specifying exact byte offset position of the string you + * are looking for within a public key. When offset is negative, the program + * just looks for the desired string being somewhere, doesn't matter where, in + * the public key. + * + * String - a hex string that you want to have in your public key. It must have + * an even number of letters, since every two hexes map to a single byte of + * the public key. + * + * Examples: + * strkey 0 0123 + * Looks for a public key that begins with "0123". + * + * strkey 1 0123 + * Looks for a public key that has "0123" starting at its second byte, i.e. "XX0123...". + * + * strkey 2 0123 + * Looks for a public key that has "0123" starting at its third byte, i.e. "XXXX0123...". + * (each two hexes represent a single byte of a public key) + * + * strkey -1 AF57CC + * Looks for a public key that contains "AF57CC", regardless of its position. + * + * To compile with gcc and sodium: gcc strkey.c -o strkey -lsodium +*/ + +#include <stdio.h> +#include <string.h> + +#include <sodium.h> + +#define PRINT_TRIES_COUNT + +void print_key(unsigned char *key) +{ + size_t i; + for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++) { + if (key[i] < 16) { + fprintf(stdout, "0"); + } + + fprintf(stdout, "%hhX", key[i]); + } +} + +int main(int argc, char *argv[]) +{ + unsigned char public_key[crypto_box_PUBLICKEYBYTES]; // null terminator + unsigned char secret_key[crypto_box_SECRETKEYBYTES]; + int offset = 0; + size_t len; + unsigned char desired_bin[crypto_box_PUBLICKEYBYTES]; // null terminator + + if (argc == 3) { + offset = atoi(argv[1]); + char *desired_hex = argv[2]; + len = strlen(desired_hex); + if (len % 2 != 0) { + fprintf(stderr, "Desired key should have an even number of letters\n"); + exit(1); + } + size_t block_length = (offset < 0 ? 0 : offset) + len/2; + if (block_length > crypto_box_PUBLICKEYBYTES) { + fprintf(stderr, "The given key with the given offset exceed public key's length\n"); + exit(1); + } + + // convert hex to bin + char *pos = desired_hex; + size_t i; + for (i = 0; i < len; pos += 2) { + sscanf(pos, "%2hhx", &desired_bin[i]); + ++i; + } + } else { + fprintf(stdout, "Usage: executable <byte offset> <desired hex string with even number of letters>\n"); + exit(1); + } + + len /= 2; + +#ifdef PRINT_TRIES_COUNT + long long unsigned int tries = 0; +#endif + + if (offset < 0) { + int found = 0; + do { +#ifdef PRINT_TRIES_COUNT + tries ++; +#endif + crypto_box_keypair(public_key, secret_key); + int i; + for (i = 0; i <= crypto_box_PUBLICKEYBYTES - len; i ++) { + if (memcmp(public_key + i, desired_bin, len) == 0) { + found = 1; + break; + } + } + } while (!found); + } else { + unsigned char *p = public_key + offset; + + do { +#ifdef PRINT_TRIES_COUNT + tries ++; +#endif + crypto_box_keypair(public_key, secret_key); + } while (memcmp(p, desired_bin, len) != 0); + } + + fprintf(stdout, "Public key: "); + print_key(public_key); + fprintf(stdout, "\n"); + + fprintf(stdout, "Private key: "); + print_key(secret_key); + fprintf(stdout, "\n"); + +#ifdef PRINT_TRIES_COUNT + fprintf(stdout, "Found the key pair on %llu try.\n", tries); +#endif + + return 0; +} diff --git a/protocols/Tox/toxcore/other/tox.png b/protocols/Tox/toxcore/other/tox.png Binary files differnew file mode 100644 index 0000000000..e3d6869614 --- /dev/null +++ b/protocols/Tox/toxcore/other/tox.png |