diff options
-rw-r--r-- | other/DHT_bootstrap.c | 2 | ||||
-rw-r--r-- | other/Makefile.inc | 4 | ||||
-rw-r--r-- | other/bootstrap_daemon/Makefile.inc | 7 | ||||
-rw-r--r-- | other/bootstrap_daemon/src/config.c | 400 | ||||
-rw-r--r-- | other/bootstrap_daemon/src/config.h | 49 | ||||
-rw-r--r-- | other/bootstrap_daemon/src/config_defaults.h | 41 | ||||
-rw-r--r-- | other/bootstrap_daemon/src/global.h | 3 | ||||
-rw-r--r-- | other/bootstrap_daemon/src/tox-bootstrapd.c | 399 | ||||
-rw-r--r-- | other/bootstrap_node_packets.c | 2 | ||||
-rw-r--r-- | other/bootstrap_node_packets.h | 35 |
10 files changed, 541 insertions, 401 deletions
diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 52e84315..16dc87b9 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c | |||
@@ -40,7 +40,7 @@ | |||
40 | #include "../testing/misc_tools.c" | 40 | #include "../testing/misc_tools.c" |
41 | 41 | ||
42 | #ifdef DHT_NODE_EXTRA_PACKETS | 42 | #ifdef DHT_NODE_EXTRA_PACKETS |
43 | #include "./bootstrap_node_packets.c" | 43 | #include "./bootstrap_node_packets.h" |
44 | 44 | ||
45 | #define DHT_VERSION_NUMBER 1 | 45 | #define DHT_VERSION_NUMBER 1 |
46 | #define DHT_MOTD "This is a test motd" | 46 | #define DHT_MOTD "This is a test motd" |
diff --git a/other/Makefile.inc b/other/Makefile.inc index 368a32f2..c86b7922 100644 --- a/other/Makefile.inc +++ b/other/Makefile.inc | |||
@@ -2,7 +2,9 @@ bin_PROGRAMS += DHT_bootstrap | |||
2 | 2 | ||
3 | DHT_bootstrap_SOURCES = ../other/DHT_bootstrap.c \ | 3 | DHT_bootstrap_SOURCES = ../other/DHT_bootstrap.c \ |
4 | ../toxcore/DHT.h \ | 4 | ../toxcore/DHT.h \ |
5 | ../toxcore/friend_requests.h | 5 | ../toxcore/friend_requests.h \ |
6 | ../other/bootstrap_node_packets.h \ | ||
7 | ../other/bootstrap_node_packets.c | ||
6 | 8 | ||
7 | DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \ | 9 | DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \ |
8 | $(LIBSODIUM_CFLAGS) \ | 10 | $(LIBSODIUM_CFLAGS) \ |
diff --git a/other/bootstrap_daemon/Makefile.inc b/other/bootstrap_daemon/Makefile.inc index eb94f0e7..5dce910e 100644 --- a/other/bootstrap_daemon/Makefile.inc +++ b/other/bootstrap_daemon/Makefile.inc | |||
@@ -5,7 +5,12 @@ bin_PROGRAMS += tox-bootstrapd | |||
5 | tox_bootstrapd_SOURCES = \ | 5 | tox_bootstrapd_SOURCES = \ |
6 | ../other/bootstrap_daemon/src/tox-bootstrapd.c \ | 6 | ../other/bootstrap_daemon/src/tox-bootstrapd.c \ |
7 | ../other/bootstrap_daemon/src/log.c \ | 7 | ../other/bootstrap_daemon/src/log.c \ |
8 | ../other/bootstrap_daemon/src/log.h | 8 | ../other/bootstrap_daemon/src/log.h \ |
9 | ../other/bootstrap_daemon/src/config.h \ | ||
10 | ../other/bootstrap_daemon/src/config.c \ | ||
11 | ../other/bootstrap_daemon/src/config_defaults.h \ | ||
12 | ../other/bootstrap_node_packets.h \ | ||
13 | ../other/bootstrap_node_packets.c | ||
9 | 14 | ||
10 | tox_bootstrapd_CFLAGS = \ | 15 | tox_bootstrapd_CFLAGS = \ |
11 | -I$(top_srcdir)/other/bootstrap_daemon \ | 16 | -I$(top_srcdir)/other/bootstrap_daemon \ |
diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c new file mode 100644 index 00000000..270d399f --- /dev/null +++ b/other/bootstrap_daemon/src/config.c | |||
@@ -0,0 +1,400 @@ | |||
1 | /* config.c | ||
2 | * | ||
3 | * Tox DHT bootstrap daemon. | ||
4 | * | ||
5 | * Copyright (C) 2014-2015 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include "config.h" | ||
25 | |||
26 | #include "config_defaults.h" | ||
27 | #include "log.h" | ||
28 | |||
29 | // C | ||
30 | //#include <stdlib.h> | ||
31 | #include <string.h> | ||
32 | |||
33 | // 3rd party | ||
34 | #include <libconfig.h> | ||
35 | |||
36 | // toxcore | ||
37 | #include "../../bootstrap_node_packets.h" | ||
38 | //#include "../../testing/misc_tools.c" | ||
39 | |||
40 | // Parses tcp relay ports from `cfg` and puts them into `tcp_relay_ports` array | ||
41 | // | ||
42 | // Supposed to be called from get_general_config only | ||
43 | // | ||
44 | // Important: iff `tcp_relay_port_count` > 0, then you are responsible for freeing `tcp_relay_ports` | ||
45 | |||
46 | void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) | ||
47 | { | ||
48 | const char *NAME_TCP_RELAY_PORTS = "tcp_relay_ports"; | ||
49 | |||
50 | *tcp_relay_port_count = 0; | ||
51 | |||
52 | config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS); | ||
53 | |||
54 | if (ports_array == NULL) { | ||
55 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); | ||
56 | write_log(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); | ||
57 | |||
58 | uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS}; | ||
59 | |||
60 | int i; | ||
61 | |||
62 | for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { | ||
63 | write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]); | ||
64 | } | ||
65 | |||
66 | // similar procedure to the one of reading config file below | ||
67 | *tcp_relay_ports = malloc(DEFAULT_TCP_RELAY_PORTS_COUNT * sizeof(uint16_t)); | ||
68 | |||
69 | for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { | ||
70 | |||
71 | (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i]; | ||
72 | |||
73 | if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT | ||
74 | || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { | ||
75 | write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, | ||
76 | (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
77 | continue; | ||
78 | } | ||
79 | |||
80 | (*tcp_relay_port_count) ++; | ||
81 | } | ||
82 | |||
83 | // the loop above skips invalid ports, so we adjust the allocated memory size | ||
84 | if ((*tcp_relay_port_count) > 0) { | ||
85 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | ||
86 | } else { | ||
87 | free(*tcp_relay_ports); | ||
88 | *tcp_relay_ports = NULL; | ||
89 | } | ||
90 | |||
91 | return; | ||
92 | } | ||
93 | |||
94 | if (config_setting_is_array(ports_array) == CONFIG_FALSE) { | ||
95 | write_log(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", | ||
96 | NAME_TCP_RELAY_PORTS); | ||
97 | return; | ||
98 | } | ||
99 | |||
100 | int config_port_count = config_setting_length(ports_array); | ||
101 | |||
102 | if (config_port_count == 0) { | ||
103 | write_log(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); | ||
104 | return; | ||
105 | } | ||
106 | |||
107 | *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t)); | ||
108 | |||
109 | int i; | ||
110 | |||
111 | for (i = 0; i < config_port_count; i ++) { | ||
112 | config_setting_t *elem = config_setting_get_elem(ports_array, i); | ||
113 | |||
114 | if (elem == NULL) { | ||
115 | // it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be | ||
116 | write_log(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); | ||
117 | break; | ||
118 | } | ||
119 | |||
120 | if (config_setting_is_number(elem) == CONFIG_FALSE) { | ||
121 | write_log(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i); | ||
122 | continue; | ||
123 | } | ||
124 | |||
125 | (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); | ||
126 | |||
127 | if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT | ||
128 | || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { | ||
129 | write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, | ||
130 | (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
131 | continue; | ||
132 | } | ||
133 | |||
134 | (*tcp_relay_port_count) ++; | ||
135 | } | ||
136 | |||
137 | // the loop above skips invalid ports, so we adjust the allocated memory size | ||
138 | if ((*tcp_relay_port_count) > 0) { | ||
139 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | ||
140 | } else { | ||
141 | free(*tcp_relay_ports); | ||
142 | *tcp_relay_ports = NULL; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, | ||
147 | int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, | ||
148 | int *tcp_relay_port_count, int *enable_motd, char **motd) | ||
149 | { | ||
150 | config_t cfg; | ||
151 | |||
152 | const char *NAME_PORT = "port"; | ||
153 | const char *NAME_PID_FILE_PATH = "pid_file_path"; | ||
154 | const char *NAME_KEYS_FILE_PATH = "keys_file_path"; | ||
155 | const char *NAME_ENABLE_IPV6 = "enable_ipv6"; | ||
156 | const char *NAME_ENABLE_IPV4_FALLBACK = "enable_ipv4_fallback"; | ||
157 | const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; | ||
158 | const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; | ||
159 | const char *NAME_ENABLE_MOTD = "enable_motd"; | ||
160 | const char *NAME_MOTD = "motd"; | ||
161 | |||
162 | config_init(&cfg); | ||
163 | |||
164 | // Read the file. If there is an error, report it and exit. | ||
165 | if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { | ||
166 | write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); | ||
167 | config_destroy(&cfg); | ||
168 | return 0; | ||
169 | } | ||
170 | |||
171 | // Get port | ||
172 | if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { | ||
173 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); | ||
174 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); | ||
175 | *port = DEFAULT_PORT; | ||
176 | } | ||
177 | |||
178 | // Get PID file location | ||
179 | const char *tmp_pid_file; | ||
180 | |||
181 | if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { | ||
182 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); | ||
183 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); | ||
184 | tmp_pid_file = DEFAULT_PID_FILE_PATH; | ||
185 | } | ||
186 | |||
187 | *pid_file_path = malloc(strlen(tmp_pid_file) + 1); | ||
188 | strcpy(*pid_file_path, tmp_pid_file); | ||
189 | |||
190 | // Get keys file location | ||
191 | const char *tmp_keys_file; | ||
192 | |||
193 | if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { | ||
194 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); | ||
195 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); | ||
196 | tmp_keys_file = DEFAULT_KEYS_FILE_PATH; | ||
197 | } | ||
198 | |||
199 | *keys_file_path = malloc(strlen(tmp_keys_file) + 1); | ||
200 | strcpy(*keys_file_path, tmp_keys_file); | ||
201 | |||
202 | // Get IPv6 option | ||
203 | if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { | ||
204 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); | ||
205 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); | ||
206 | *enable_ipv6 = DEFAULT_ENABLE_IPV6; | ||
207 | } | ||
208 | |||
209 | // Get IPv4 fallback option | ||
210 | if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { | ||
211 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); | ||
212 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, | ||
213 | DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false"); | ||
214 | *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; | ||
215 | } | ||
216 | |||
217 | // Get LAN discovery option | ||
218 | if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { | ||
219 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); | ||
220 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, | ||
221 | DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); | ||
222 | *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; | ||
223 | } | ||
224 | |||
225 | // Get TCP relay option | ||
226 | if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { | ||
227 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); | ||
228 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, | ||
229 | DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); | ||
230 | *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; | ||
231 | } | ||
232 | |||
233 | if (*enable_tcp_relay) { | ||
234 | parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count); | ||
235 | } else { | ||
236 | *tcp_relay_port_count = 0; | ||
237 | } | ||
238 | |||
239 | // Get MOTD option | ||
240 | if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { | ||
241 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); | ||
242 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, | ||
243 | DEFAULT_ENABLE_MOTD ? "true" : "false"); | ||
244 | *enable_motd = DEFAULT_ENABLE_MOTD; | ||
245 | } | ||
246 | |||
247 | if (*enable_motd) { | ||
248 | // Get MOTD | ||
249 | const char *tmp_motd; | ||
250 | |||
251 | if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) { | ||
252 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); | ||
253 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); | ||
254 | tmp_motd = DEFAULT_MOTD; | ||
255 | } | ||
256 | |||
257 | size_t tmp_motd_length = strlen(tmp_motd) + 1; | ||
258 | size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length; | ||
259 | *motd = malloc(motd_length); | ||
260 | strncpy(*motd, tmp_motd, motd_length); | ||
261 | (*motd)[motd_length - 1] = '\0'; | ||
262 | } | ||
263 | |||
264 | config_destroy(&cfg); | ||
265 | |||
266 | write_log(LOG_LEVEL_INFO, "Successfully read:\n"); | ||
267 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); | ||
268 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); | ||
269 | write_log(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port); | ||
270 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); | ||
271 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); | ||
272 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); | ||
273 | |||
274 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); | ||
275 | |||
276 | // show info about tcp ports only if tcp relay is enabled | ||
277 | if (*enable_tcp_relay) { | ||
278 | if (*tcp_relay_port_count == 0) { | ||
279 | write_log(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); | ||
280 | } else { | ||
281 | write_log(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); | ||
282 | int i; | ||
283 | |||
284 | for (i = 0; i < *tcp_relay_port_count; i ++) { | ||
285 | write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | |||
290 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); | ||
291 | |||
292 | if (*enable_motd) { | ||
293 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd); | ||
294 | } | ||
295 | |||
296 | return 1; | ||
297 | } | ||
298 | |||
299 | int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) | ||
300 | { | ||
301 | const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes"; | ||
302 | |||
303 | const char *NAME_PUBLIC_KEY = "public_key"; | ||
304 | const char *NAME_PORT = "port"; | ||
305 | const char *NAME_ADDRESS = "address"; | ||
306 | |||
307 | config_t cfg; | ||
308 | |||
309 | config_init(&cfg); | ||
310 | |||
311 | if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { | ||
312 | write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); | ||
313 | config_destroy(&cfg); | ||
314 | return 0; | ||
315 | } | ||
316 | |||
317 | config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES); | ||
318 | |||
319 | if (node_list == NULL) { | ||
320 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES); | ||
321 | config_destroy(&cfg); | ||
322 | return 1; | ||
323 | } | ||
324 | |||
325 | if (config_setting_length(node_list) == 0) { | ||
326 | write_log(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); | ||
327 | config_destroy(&cfg); | ||
328 | return 1; | ||
329 | } | ||
330 | |||
331 | int bs_port; | ||
332 | const char *bs_address; | ||
333 | const char *bs_public_key; | ||
334 | |||
335 | config_setting_t *node; | ||
336 | |||
337 | int i = 0; | ||
338 | |||
339 | while (config_setting_length(node_list)) { | ||
340 | |||
341 | node = config_setting_get_elem(node_list, 0); | ||
342 | |||
343 | if (node == NULL) { | ||
344 | config_destroy(&cfg); | ||
345 | return 0; | ||
346 | } | ||
347 | |||
348 | // Check that all settings are present | ||
349 | if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { | ||
350 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY); | ||
351 | goto next; | ||
352 | } | ||
353 | |||
354 | if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) { | ||
355 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); | ||
356 | goto next; | ||
357 | } | ||
358 | |||
359 | if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { | ||
360 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); | ||
361 | goto next; | ||
362 | } | ||
363 | |||
364 | // Process settings | ||
365 | if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) { | ||
366 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, | ||
367 | bs_public_key); | ||
368 | goto next; | ||
369 | } | ||
370 | |||
371 | if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { | ||
372 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, | ||
373 | bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
374 | goto next; | ||
375 | } | ||
376 | |||
377 | uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key); | ||
378 | const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), | ||
379 | bs_public_key_bin); | ||
380 | free(bs_public_key_bin); | ||
381 | |||
382 | if (!address_resolved) { | ||
383 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); | ||
384 | goto next; | ||
385 | } | ||
386 | |||
387 | write_log(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); | ||
388 | |||
389 | next: | ||
390 | // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly | ||
391 | // though it's freed when the element is removed, so we free it right away in order to keep memory | ||
392 | // consumption minimal | ||
393 | config_setting_remove_elem(node_list, 0); | ||
394 | i++; | ||
395 | } | ||
396 | |||
397 | config_destroy(&cfg); | ||
398 | |||
399 | return 1; | ||
400 | } | ||
diff --git a/other/bootstrap_daemon/src/config.h b/other/bootstrap_daemon/src/config.h new file mode 100644 index 00000000..73bc6e0b --- /dev/null +++ b/other/bootstrap_daemon/src/config.h | |||
@@ -0,0 +1,49 @@ | |||
1 | /* config.h | ||
2 | * | ||
3 | * Tox DHT bootstrap daemon. | ||
4 | * | ||
5 | * Copyright (C) 2014-2015 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifndef CONFIG_H | ||
25 | #define CONFIG_H | ||
26 | |||
27 | #include "../../../toxcore/DHT.h" | ||
28 | |||
29 | // Gets general config options | ||
30 | // | ||
31 | // Important: you are responsible for freeing `pid_file_path` and `keys_file_path` | ||
32 | // also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports` | ||
33 | // and also `motd` iff `enable_motd` is set | ||
34 | // | ||
35 | // returns 1 on success | ||
36 | // 0 on failure, doesn't modify any data pointed by arguments | ||
37 | |||
38 | int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, | ||
39 | int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, | ||
40 | int *tcp_relay_port_count, int *enable_motd, char **motd); | ||
41 | |||
42 | // Bootstraps nodes listed in the config file | ||
43 | // | ||
44 | // returns 1 on success, some or no bootstrap nodes were added | ||
45 | // 0 on failure, a error accured while parsing config file | ||
46 | |||
47 | int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6); | ||
48 | |||
49 | #endif // CONFIG_H | ||
diff --git a/other/bootstrap_daemon/src/config_defaults.h b/other/bootstrap_daemon/src/config_defaults.h new file mode 100644 index 00000000..34d715e7 --- /dev/null +++ b/other/bootstrap_daemon/src/config_defaults.h | |||
@@ -0,0 +1,41 @@ | |||
1 | /* config_defaults.h | ||
2 | * | ||
3 | * Tox DHT bootstrap daemon. | ||
4 | * | ||
5 | * Copyright (C) 2014-2015 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifndef CONFIG_DEFAULTS_H | ||
25 | #define CONFIG_DEFAULTS_H | ||
26 | |||
27 | #include "global.h" | ||
28 | |||
29 | #define DEFAULT_PID_FILE_PATH "tox-bootstrapd.pid" | ||
30 | #define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys" | ||
31 | #define DEFAULT_PORT 33445 | ||
32 | #define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false | ||
33 | #define DEFAULT_ENABLE_IPV4_FALLBACK 1 // 1 - true, 0 - false | ||
34 | #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false | ||
35 | #define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false | ||
36 | #define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports. make sure to adjust DEFAULT_TCP_RELAY_PORTS_COUNT accordingly | ||
37 | #define DEFAULT_TCP_RELAY_PORTS_COUNT 3 | ||
38 | #define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false | ||
39 | #define DEFAULT_MOTD DAEMON_NAME | ||
40 | |||
41 | #endif // CONFIG_DEFAULTS_H | ||
diff --git a/other/bootstrap_daemon/src/global.h b/other/bootstrap_daemon/src/global.h index 7aa42780..71f24635 100644 --- a/other/bootstrap_daemon/src/global.h +++ b/other/bootstrap_daemon/src/global.h | |||
@@ -27,4 +27,7 @@ | |||
27 | #define DAEMON_NAME "tox-bootstrapd" | 27 | #define DAEMON_NAME "tox-bootstrapd" |
28 | #define DAEMON_VERSION_NUMBER 2014101200UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day | 28 | #define DAEMON_VERSION_NUMBER 2014101200UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day |
29 | 29 | ||
30 | #define MIN_ALLOWED_PORT 1 | ||
31 | #define MAX_ALLOWED_PORT 65535 | ||
32 | |||
30 | #endif // GLOBAL_H | 33 | #endif // GLOBAL_H |
diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index a8098e41..d4191e80 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c | |||
@@ -34,9 +34,6 @@ | |||
34 | #include <stdlib.h> | 34 | #include <stdlib.h> |
35 | #include <string.h> | 35 | #include <string.h> |
36 | 36 | ||
37 | // 3rd party | ||
38 | #include <libconfig.h> | ||
39 | |||
40 | // ./configure | 37 | // ./configure |
41 | #ifdef HAVE_CONFIG_H | 38 | #ifdef HAVE_CONFIG_H |
42 | #include "config.h" | 39 | #include "config.h" |
@@ -49,9 +46,10 @@ | |||
49 | #include "../../toxcore/util.h" | 46 | #include "../../toxcore/util.h" |
50 | 47 | ||
51 | // misc | 48 | // misc |
52 | #include "../bootstrap_node_packets.c" | 49 | #include "../bootstrap_node_packets.h" |
53 | #include "../../testing/misc_tools.c" | 50 | #include "../../testing/misc_tools.c" |
54 | 51 | ||
52 | #include "config.h" | ||
55 | #include "global.h" | 53 | #include "global.h" |
56 | #include "log.h" | 54 | #include "log.h" |
57 | 55 | ||
@@ -59,22 +57,6 @@ | |||
59 | #define SLEEP_TIME_MILLISECONDS 30 | 57 | #define SLEEP_TIME_MILLISECONDS 30 |
60 | #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) | 58 | #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) |
61 | 59 | ||
62 | #define DEFAULT_PID_FILE_PATH "tox-bootstrapd.pid" | ||
63 | #define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys" | ||
64 | #define DEFAULT_PORT 33445 | ||
65 | #define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false | ||
66 | #define DEFAULT_ENABLE_IPV4_FALLBACK 1 // 1 - true, 0 - false | ||
67 | #define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false | ||
68 | #define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false | ||
69 | #define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports. make sure to adjust DEFAULT_TCP_RELAY_PORTS_COUNT accordingly | ||
70 | #define DEFAULT_TCP_RELAY_PORTS_COUNT 3 | ||
71 | #define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false | ||
72 | #define DEFAULT_MOTD DAEMON_NAME | ||
73 | |||
74 | #define MIN_ALLOWED_PORT 1 | ||
75 | #define MAX_ALLOWED_PORT 65535 | ||
76 | |||
77 | |||
78 | // Uses the already existing key or creates one if it didn't exist | 60 | // Uses the already existing key or creates one if it didn't exist |
79 | // | 61 | // |
80 | // retirns 1 on success | 62 | // retirns 1 on success |
@@ -122,383 +104,6 @@ int manage_keys(DHT *dht, char *keys_file_path) | |||
122 | return 1; | 104 | return 1; |
123 | } | 105 | } |
124 | 106 | ||
125 | // Parses tcp relay ports from `cfg` and puts them into `tcp_relay_ports` array | ||
126 | // | ||
127 | // Supposed to be called from get_general_config only | ||
128 | // | ||
129 | // Important: iff `tcp_relay_port_count` > 0, then you are responsible for freeing `tcp_relay_ports` | ||
130 | |||
131 | void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int *tcp_relay_port_count) | ||
132 | { | ||
133 | const char *NAME_TCP_RELAY_PORTS = "tcp_relay_ports"; | ||
134 | |||
135 | *tcp_relay_port_count = 0; | ||
136 | |||
137 | config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS); | ||
138 | |||
139 | if (ports_array == NULL) { | ||
140 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); | ||
141 | write_log(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); | ||
142 | |||
143 | uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS}; | ||
144 | |||
145 | int i; | ||
146 | |||
147 | for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { | ||
148 | write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]); | ||
149 | } | ||
150 | |||
151 | // similar procedure to the one of reading config file below | ||
152 | *tcp_relay_ports = malloc(DEFAULT_TCP_RELAY_PORTS_COUNT * sizeof(uint16_t)); | ||
153 | |||
154 | for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { | ||
155 | |||
156 | (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i]; | ||
157 | |||
158 | if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT | ||
159 | || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { | ||
160 | write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, | ||
161 | (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
162 | continue; | ||
163 | } | ||
164 | |||
165 | (*tcp_relay_port_count) ++; | ||
166 | } | ||
167 | |||
168 | // the loop above skips invalid ports, so we adjust the allocated memory size | ||
169 | if ((*tcp_relay_port_count) > 0) { | ||
170 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | ||
171 | } else { | ||
172 | free(*tcp_relay_ports); | ||
173 | *tcp_relay_ports = NULL; | ||
174 | } | ||
175 | |||
176 | return; | ||
177 | } | ||
178 | |||
179 | if (config_setting_is_array(ports_array) == CONFIG_FALSE) { | ||
180 | write_log(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", | ||
181 | NAME_TCP_RELAY_PORTS); | ||
182 | return; | ||
183 | } | ||
184 | |||
185 | int config_port_count = config_setting_length(ports_array); | ||
186 | |||
187 | if (config_port_count == 0) { | ||
188 | write_log(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); | ||
189 | return; | ||
190 | } | ||
191 | |||
192 | *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t)); | ||
193 | |||
194 | int i; | ||
195 | |||
196 | for (i = 0; i < config_port_count; i ++) { | ||
197 | config_setting_t *elem = config_setting_get_elem(ports_array, i); | ||
198 | |||
199 | if (elem == NULL) { | ||
200 | // it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be | ||
201 | write_log(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); | ||
202 | break; | ||
203 | } | ||
204 | |||
205 | if (config_setting_is_number(elem) == CONFIG_FALSE) { | ||
206 | write_log(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i); | ||
207 | continue; | ||
208 | } | ||
209 | |||
210 | (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); | ||
211 | |||
212 | if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT | ||
213 | || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { | ||
214 | write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, | ||
215 | (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
216 | continue; | ||
217 | } | ||
218 | |||
219 | (*tcp_relay_port_count) ++; | ||
220 | } | ||
221 | |||
222 | // the loop above skips invalid ports, so we adjust the allocated memory size | ||
223 | if ((*tcp_relay_port_count) > 0) { | ||
224 | *tcp_relay_ports = realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); | ||
225 | } else { | ||
226 | free(*tcp_relay_ports); | ||
227 | *tcp_relay_ports = NULL; | ||
228 | } | ||
229 | } | ||
230 | |||
231 | // Gets general config options | ||
232 | // | ||
233 | // Important: you are responsible for freeing `pid_file_path` and `keys_file_path` | ||
234 | // also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports` | ||
235 | // and also `motd` iff `enable_motd` is set | ||
236 | // | ||
237 | // returns 1 on success | ||
238 | // 0 on failure, doesn't modify any data pointed by arguments | ||
239 | |||
240 | int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, | ||
241 | int *enable_ipv6, | ||
242 | int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay, uint16_t **tcp_relay_ports, | ||
243 | int *tcp_relay_port_count, int *enable_motd, char **motd) | ||
244 | { | ||
245 | config_t cfg; | ||
246 | |||
247 | const char *NAME_PORT = "port"; | ||
248 | const char *NAME_PID_FILE_PATH = "pid_file_path"; | ||
249 | const char *NAME_KEYS_FILE_PATH = "keys_file_path"; | ||
250 | const char *NAME_ENABLE_IPV6 = "enable_ipv6"; | ||
251 | const char *NAME_ENABLE_IPV4_FALLBACK = "enable_ipv4_fallback"; | ||
252 | const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery"; | ||
253 | const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay"; | ||
254 | const char *NAME_ENABLE_MOTD = "enable_motd"; | ||
255 | const char *NAME_MOTD = "motd"; | ||
256 | |||
257 | config_init(&cfg); | ||
258 | |||
259 | // Read the file. If there is an error, report it and exit. | ||
260 | if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { | ||
261 | write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); | ||
262 | config_destroy(&cfg); | ||
263 | return 0; | ||
264 | } | ||
265 | |||
266 | // Get port | ||
267 | if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { | ||
268 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); | ||
269 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); | ||
270 | *port = DEFAULT_PORT; | ||
271 | } | ||
272 | |||
273 | // Get PID file location | ||
274 | const char *tmp_pid_file; | ||
275 | |||
276 | if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { | ||
277 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); | ||
278 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); | ||
279 | tmp_pid_file = DEFAULT_PID_FILE_PATH; | ||
280 | } | ||
281 | |||
282 | *pid_file_path = malloc(strlen(tmp_pid_file) + 1); | ||
283 | strcpy(*pid_file_path, tmp_pid_file); | ||
284 | |||
285 | // Get keys file location | ||
286 | const char *tmp_keys_file; | ||
287 | |||
288 | if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { | ||
289 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); | ||
290 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); | ||
291 | tmp_keys_file = DEFAULT_KEYS_FILE_PATH; | ||
292 | } | ||
293 | |||
294 | *keys_file_path = malloc(strlen(tmp_keys_file) + 1); | ||
295 | strcpy(*keys_file_path, tmp_keys_file); | ||
296 | |||
297 | // Get IPv6 option | ||
298 | if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { | ||
299 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); | ||
300 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); | ||
301 | *enable_ipv6 = DEFAULT_ENABLE_IPV6; | ||
302 | } | ||
303 | |||
304 | // Get IPv4 fallback option | ||
305 | if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { | ||
306 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); | ||
307 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, | ||
308 | DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false"); | ||
309 | *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; | ||
310 | } | ||
311 | |||
312 | // Get LAN discovery option | ||
313 | if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { | ||
314 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); | ||
315 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, | ||
316 | DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); | ||
317 | *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; | ||
318 | } | ||
319 | |||
320 | // Get TCP relay option | ||
321 | if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { | ||
322 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); | ||
323 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, | ||
324 | DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); | ||
325 | *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; | ||
326 | } | ||
327 | |||
328 | if (*enable_tcp_relay) { | ||
329 | parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count); | ||
330 | } else { | ||
331 | *tcp_relay_port_count = 0; | ||
332 | } | ||
333 | |||
334 | // Get MOTD option | ||
335 | if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { | ||
336 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); | ||
337 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, | ||
338 | DEFAULT_ENABLE_MOTD ? "true" : "false"); | ||
339 | *enable_motd = DEFAULT_ENABLE_MOTD; | ||
340 | } | ||
341 | |||
342 | if (*enable_motd) { | ||
343 | // Get MOTD | ||
344 | const char *tmp_motd; | ||
345 | |||
346 | if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) { | ||
347 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); | ||
348 | write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); | ||
349 | tmp_motd = DEFAULT_MOTD; | ||
350 | } | ||
351 | |||
352 | size_t tmp_motd_length = strlen(tmp_motd) + 1; | ||
353 | size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length; | ||
354 | *motd = malloc(motd_length); | ||
355 | strncpy(*motd, tmp_motd, motd_length); | ||
356 | (*motd)[motd_length - 1] = '\0'; | ||
357 | } | ||
358 | |||
359 | config_destroy(&cfg); | ||
360 | |||
361 | write_log(LOG_LEVEL_INFO, "Successfully read:\n"); | ||
362 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); | ||
363 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); | ||
364 | write_log(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port); | ||
365 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); | ||
366 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); | ||
367 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); | ||
368 | |||
369 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); | ||
370 | |||
371 | // show info about tcp ports only if tcp relay is enabled | ||
372 | if (*enable_tcp_relay) { | ||
373 | if (*tcp_relay_port_count == 0) { | ||
374 | write_log(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); | ||
375 | } else { | ||
376 | write_log(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); | ||
377 | int i; | ||
378 | |||
379 | for (i = 0; i < *tcp_relay_port_count; i ++) { | ||
380 | write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); | ||
381 | } | ||
382 | } | ||
383 | } | ||
384 | |||
385 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); | ||
386 | |||
387 | if (*enable_motd) { | ||
388 | write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd); | ||
389 | } | ||
390 | |||
391 | return 1; | ||
392 | } | ||
393 | |||
394 | // Bootstraps nodes listed in the config file | ||
395 | // | ||
396 | // returns 1 on success, some or no bootstrap nodes were added | ||
397 | // 0 on failure, a error accured while parsing config file | ||
398 | |||
399 | int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) | ||
400 | { | ||
401 | const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes"; | ||
402 | |||
403 | const char *NAME_PUBLIC_KEY = "public_key"; | ||
404 | const char *NAME_PORT = "port"; | ||
405 | const char *NAME_ADDRESS = "address"; | ||
406 | |||
407 | config_t cfg; | ||
408 | |||
409 | config_init(&cfg); | ||
410 | |||
411 | if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { | ||
412 | write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); | ||
413 | config_destroy(&cfg); | ||
414 | return 0; | ||
415 | } | ||
416 | |||
417 | config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES); | ||
418 | |||
419 | if (node_list == NULL) { | ||
420 | write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES); | ||
421 | config_destroy(&cfg); | ||
422 | return 1; | ||
423 | } | ||
424 | |||
425 | if (config_setting_length(node_list) == 0) { | ||
426 | write_log(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); | ||
427 | config_destroy(&cfg); | ||
428 | return 1; | ||
429 | } | ||
430 | |||
431 | int bs_port; | ||
432 | const char *bs_address; | ||
433 | const char *bs_public_key; | ||
434 | |||
435 | config_setting_t *node; | ||
436 | |||
437 | int i = 0; | ||
438 | |||
439 | while (config_setting_length(node_list)) { | ||
440 | |||
441 | node = config_setting_get_elem(node_list, 0); | ||
442 | |||
443 | if (node == NULL) { | ||
444 | config_destroy(&cfg); | ||
445 | return 0; | ||
446 | } | ||
447 | |||
448 | // Check that all settings are present | ||
449 | if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { | ||
450 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY); | ||
451 | goto next; | ||
452 | } | ||
453 | |||
454 | if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) { | ||
455 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); | ||
456 | goto next; | ||
457 | } | ||
458 | |||
459 | if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { | ||
460 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); | ||
461 | goto next; | ||
462 | } | ||
463 | |||
464 | // Process settings | ||
465 | if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) { | ||
466 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, | ||
467 | bs_public_key); | ||
468 | goto next; | ||
469 | } | ||
470 | |||
471 | if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { | ||
472 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, | ||
473 | bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); | ||
474 | goto next; | ||
475 | } | ||
476 | |||
477 | uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key); | ||
478 | const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), | ||
479 | bs_public_key_bin); | ||
480 | free(bs_public_key_bin); | ||
481 | |||
482 | if (!address_resolved) { | ||
483 | write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); | ||
484 | goto next; | ||
485 | } | ||
486 | |||
487 | write_log(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); | ||
488 | |||
489 | next: | ||
490 | // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly | ||
491 | // though it's freed when the element is removed, so we free it right away in order to keep memory | ||
492 | // consumption minimal | ||
493 | config_setting_remove_elem(node_list, 0); | ||
494 | i++; | ||
495 | } | ||
496 | |||
497 | config_destroy(&cfg); | ||
498 | |||
499 | return 1; | ||
500 | } | ||
501 | |||
502 | // Prints public key | 107 | // Prints public key |
503 | 108 | ||
504 | void print_public_key(const uint8_t *public_key) | 109 | void print_public_key(const uint8_t *public_key) |
diff --git a/other/bootstrap_node_packets.c b/other/bootstrap_node_packets.c index 0bf9b9b0..21bb2894 100644 --- a/other/bootstrap_node_packets.c +++ b/other/bootstrap_node_packets.c | |||
@@ -23,7 +23,7 @@ | |||
23 | * | 23 | * |
24 | */ | 24 | */ |
25 | 25 | ||
26 | #define MAX_MOTD_LENGTH 256 /* I recommend you use a maximum of 96 bytes. The hard maximum is this though. */ | 26 | #include "bootstrap_node_packets.h" |
27 | 27 | ||
28 | #define INFO_REQUEST_PACKET_LENGTH 78 | 28 | #define INFO_REQUEST_PACKET_LENGTH 78 |
29 | 29 | ||
diff --git a/other/bootstrap_node_packets.h b/other/bootstrap_node_packets.h new file mode 100644 index 00000000..43356e7b --- /dev/null +++ b/other/bootstrap_node_packets.h | |||
@@ -0,0 +1,35 @@ | |||
1 | /* bootstrap_node_packets.h | ||
2 | * | ||
3 | * Special bootstrap node only packets. | ||
4 | * | ||
5 | * Include it in your bootstrap node and use: bootstrap_set_callbacks() to enable. | ||
6 | * | ||
7 | * Copyright (C) 2015 Tox project All Rights Reserved. | ||
8 | * | ||
9 | * This file is part of Tox. | ||
10 | * | ||
11 | * Tox is free software: you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation, either version 3 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * Tox is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | * You should have received a copy of the GNU General Public License | ||
22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | #ifndef BOOTSTRAP_NODE_PACKETS_H | ||
27 | #define BOOTSTRAP_NODE_PACKETS_H | ||
28 | |||
29 | #include "../toxcore/network.h" | ||
30 | |||
31 | #define MAX_MOTD_LENGTH 256 /* I recommend you use a maximum of 96 bytes. The hard maximum is this though. */ | ||
32 | |||
33 | int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, uint8_t *motd, uint16_t motd_length); | ||
34 | |||
35 | #endif // BOOTSTRAP_NODE_PACKETS_H | ||