diff options
Diffstat (limited to 'other/bootstrap_daemon/tox_bootstrap_daemon.c')
-rw-r--r-- | other/bootstrap_daemon/tox_bootstrap_daemon.c | 141 |
1 files changed, 134 insertions, 7 deletions
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 @@ | |||
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #include <sys/types.h> | 24 | // system provided |
25 | #include <arpa/inet.h> | ||
26 | #include <syslog.h> | ||
25 | #include <sys/stat.h> | 27 | #include <sys/stat.h> |
28 | #include <sys/types.h> | ||
26 | #include <unistd.h> | 29 | #include <unistd.h> |
27 | #include <syslog.h> | ||
28 | 30 | ||
31 | // C | ||
29 | #include <stdio.h> | 32 | #include <stdio.h> |
30 | #include <stdlib.h> | 33 | #include <stdlib.h> |
31 | #include <libconfig.h> | ||
32 | #include <arpa/inet.h> | ||
33 | #include <string.h> | 34 | #include <string.h> |
34 | 35 | ||
36 | // 3rd party | ||
37 | #include <libconfig.h> | ||
38 | |||
39 | // ./configure | ||
35 | #ifdef HAVE_CONFIG_H | 40 | #ifdef HAVE_CONFIG_H |
36 | #include "config.h" | 41 | #include "config.h" |
37 | #endif | 42 | #endif |
38 | 43 | ||
39 | #include "../../toxcore/onion_announce.h" | 44 | // toxcore |
40 | #include "../../toxcore/LAN_discovery.h" | 45 | #include "../../toxcore/LAN_discovery.h" |
46 | #include "../../toxcore/onion_announce.h" | ||
47 | #include "../../toxcore/TCP_server.h" | ||
41 | #include "../../toxcore/util.h" | 48 | #include "../../toxcore/util.h" |
42 | 49 | ||
50 | // misc | ||
43 | #include "../../testing/misc_tools.c" | 51 | #include "../../testing/misc_tools.c" |
44 | 52 | ||
45 | #define DAEMON_NAME "tox_bootstrap_daemon" | 53 | #define DAEMON_NAME "tox_bootstrap_daemon" |
@@ -52,6 +60,7 @@ | |||
52 | #define DEFAULT_PORT 33445 | 60 | #define DEFAULT_PORT 33445 |
53 | #define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false | 61 | #define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false |
54 | #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false | 62 | #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false |
63 | #define DEFAULT_ENABLE_TCP_RELAY 1 | ||
55 | 64 | ||
56 | 65 | ||
57 | // Uses the already existing key or creates one if it didn't exist | 66 | // 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) | |||
96 | return 1; | 105 | return 1; |
97 | } | 106 | } |
98 | 107 | ||
108 | // Parses tcp relay ports from `cfg` and puts them into `tcp_relay_ports` array | ||
109 | // | ||
110 | // Supposed to be called from get_general_config only | ||
111 | // | ||
112 | // Important: iff `tcp_relay_port_count` > 0, then you are responsible for freeing `tcp_relay_ports` | ||
113 | |||
114 | void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) | ||
115 | { | ||
116 | const char *NAME_TCP_RELAY_PORTS = "tcp_relay_ports"; | ||
117 | |||
118 | *tcp_relay_port_count = 0; | ||
119 | |||
120 | config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS); | ||
121 | |||
122 | if (ports_array == NULL) { | ||
123 | syslog(LOG_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); | ||
124 | return; | ||
125 | } | ||
126 | |||
127 | if (config_setting_is_array(ports_array) == CONFIG_FALSE) { | ||
128 | syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n"); | ||
129 | return; | ||
130 | } | ||
131 | |||
132 | int config_port_count = config_setting_length(ports_array); | ||
133 | if (config_port_count == 0) { | ||
134 | syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); | ||
135 | return; | ||
136 | } | ||
137 | |||
138 | *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t)); | ||
139 | |||
140 | config_setting_t *elem; | ||
141 | int i; | ||
142 | |||
143 | for (i = 0; i < config_port_count; i ++) { | ||
144 | |||
145 | elem = config_setting_get_elem(ports_array, i); | ||
146 | |||
147 | if (elem == NULL) { | ||
148 | // 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 | ||
149 | syslog(LOG_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); | ||
150 | break; | ||
151 | } | ||
152 | |||
153 | if (config_setting_is_number(elem) == CONFIG_FALSE) { | ||
154 | syslog(LOG_WARNING, "Port #%d: Not a number. Skipping.\n", i); | ||
155 | continue; | ||
156 | } | ||
157 | |||
158 | (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); | ||
159 | if ((*tcp_relay_ports)[i] < 1 || (*tcp_relay_ports)[i] > 65535) { | ||
160 | syslog(LOG_WARNING, "Port #%d: Invalid port value, should be in [1, 65535]. Skipping.\n", i); | ||
161 | continue; | ||
162 | } | ||
163 | |||
164 | (*tcp_relay_port_count) ++; | ||
165 | } | ||
166 | |||
167 | // the loop above skips invalid ports, so we adjust the allocated memory size | ||
168 | *tcp_relay_ports = realloc(*tcp_relay_ports, *tcp_relay_port_count * sizeof(uint16_t)); | ||
169 | } | ||
170 | |||
99 | // Gets general config options | 171 | // Gets general config options |
100 | // | 172 | // |
101 | // Important: you are responsible for freeing `pid_file_path` and `keys_file_path` | 173 | // Important: you are responsible for freeing `pid_file_path` and `keys_file_path` |
174 | // also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports` | ||
102 | // | 175 | // |
103 | // returns 1 on success | 176 | // returns 1 on success |
104 | // 0 on failure, doesn't modify any data pointed by arguments | 177 | // 0 on failure, doesn't modify any data pointed by arguments |
105 | 178 | ||
106 | int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, | 179 | int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, |
107 | int *enable_lan_discovery) | 180 | int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) |
108 | { | 181 | { |
109 | config_t cfg; | 182 | config_t cfg; |
110 | 183 | ||
@@ -113,6 +186,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi | |||
113 | const char *NAME_KEYS_FILE_PATH = "keys_file_path"; | 186 | const char *NAME_KEYS_FILE_PATH = "keys_file_path"; |
114 | const char *NAME_ENABLE_IPV6 = "enable_ipv6"; | 187 | const char *NAME_ENABLE_IPV6 = "enable_ipv6"; |
115 | const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; | 188 | const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; |
189 | const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; | ||
116 | 190 | ||
117 | config_init(&cfg); | 191 | config_init(&cfg); |
118 | 192 | ||
@@ -169,6 +243,20 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi | |||
169 | *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; | 243 | *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; |
170 | } | 244 | } |
171 | 245 | ||
246 | // Get TCP relay option | ||
247 | if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { | ||
248 | syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); | ||
249 | syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, | ||
250 | DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); | ||
251 | *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; | ||
252 | } | ||
253 | |||
254 | if (*enable_tcp_relay) { | ||
255 | parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count); | ||
256 | } else { | ||
257 | *tcp_relay_port_count = 0; | ||
258 | } | ||
259 | |||
172 | config_destroy(&cfg); | 260 | config_destroy(&cfg); |
173 | 261 | ||
174 | syslog(LOG_DEBUG, "Successfully read:\n"); | 262 | 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 | |||
177 | syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port); | 265 | syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port); |
178 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); | 266 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); |
179 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); | 267 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); |
268 | syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); | ||
269 | // show info about tcp ports only if tcp relay is enabled | ||
270 | if (*enable_tcp_relay) { | ||
271 | if (*tcp_relay_port_count == 0) { | ||
272 | syslog(LOG_DEBUG, "No TCP ports could be read.\n"); | ||
273 | } else { | ||
274 | syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count); | ||
275 | int i; | ||
276 | for (i = 0; i < *tcp_relay_port_count; i ++) { | ||
277 | syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); | ||
278 | } | ||
279 | } | ||
280 | } | ||
180 | 281 | ||
181 | return 1; | 282 | return 1; |
182 | } | 283 | } |
@@ -325,8 +426,11 @@ int main(int argc, char *argv[]) | |||
325 | int port; | 426 | int port; |
326 | int enable_ipv6; | 427 | int enable_ipv6; |
327 | int enable_lan_discovery; | 428 | int enable_lan_discovery; |
429 | int enable_tcp_relay; | ||
430 | uint16_t *tcp_relay_ports; | ||
431 | int tcp_relay_port_count; | ||
328 | 432 | ||
329 | if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery)) { | 433 | 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)) { |
330 | syslog(LOG_DEBUG, "General config read successfully\n"); | 434 | syslog(LOG_DEBUG, "General config read successfully\n"); |
331 | } else { | 435 | } else { |
332 | syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); | 436 | syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); |
@@ -374,6 +478,25 @@ int main(int argc, char *argv[]) | |||
374 | return 1; | 478 | return 1; |
375 | } | 479 | } |
376 | 480 | ||
481 | TCP_Server *tcp_server = NULL; | ||
482 | |||
483 | if (enable_tcp_relay) { | ||
484 | if (tcp_relay_port_count == 0) { | ||
485 | syslog(LOG_ERR, "No TCP relay ports read. Exiting.\n"); | ||
486 | return 1; | ||
487 | } | ||
488 | |||
489 | tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key, dht->self_secret_key, onion); | ||
490 | |||
491 | // tcp_relay_port_count != 0 at this point | ||
492 | free(tcp_relay_ports); | ||
493 | |||
494 | if (tcp_server == NULL) { | ||
495 | syslog(LOG_ERR, "Couldn't initialize Tox TCP server. Exiting.\n"); | ||
496 | return 1; | ||
497 | } | ||
498 | } | ||
499 | |||
377 | if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { | 500 | if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { |
378 | syslog(LOG_DEBUG, "List of bootstrap nodes read successfully\n"); | 501 | syslog(LOG_DEBUG, "List of bootstrap nodes read successfully\n"); |
379 | } else { | 502 | } else { |
@@ -443,6 +566,10 @@ int main(int argc, char *argv[]) | |||
443 | last_LANdiscovery = unix_time(); | 566 | last_LANdiscovery = unix_time(); |
444 | } | 567 | } |
445 | 568 | ||
569 | if (enable_tcp_relay) { | ||
570 | do_TCP_server(tcp_server); | ||
571 | } | ||
572 | |||
446 | networking_poll(dht->net); | 573 | networking_poll(dht->net); |
447 | 574 | ||
448 | if (waiting_for_dht_connection && DHT_isconnected(dht)) { | 575 | if (waiting_for_dht_connection && DHT_isconnected(dht)) { |