summaryrefslogtreecommitdiff
path: root/other/bootstrap_serverdaemon
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2014-01-19 13:53:18 -0500
committerMaxim Biro <nurupo.contributions@gmail.com>2014-01-19 13:53:18 -0500
commit885e5898c56fd73d8800ec44373a57e23e3da5bb (patch)
tree6e37cc22e143d1807b2c389d519dfb21c8af57bd /other/bootstrap_serverdaemon
parent5a4756a6c0d4f1f2c20e99f2c926dbbd18e048d0 (diff)
Astyled the code
Diffstat (limited to 'other/bootstrap_serverdaemon')
-rw-r--r--other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c38
1 files changed, 25 insertions, 13 deletions
diff --git a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c b/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c
index fe3fb852..48ecbcfb 100644
--- a/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c
+++ b/other/bootstrap_serverdaemon/tox_dht_bootstrap_server_daemon.c
@@ -53,6 +53,7 @@
53#define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false 53#define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false
54#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false 54#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false
55 55
56
56// Uses the already existing key or creates one if it didn't exist 57// Uses the already existing key or creates one if it didn't exist
57// 58//
58// retirns 1 on success 59// retirns 1 on success
@@ -66,6 +67,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
66 67
67 // Check if file exits, proceed to open and load keys 68 // Check if file exits, proceed to open and load keys
68 keys_file = fopen(keys_file_path, "r"); 69 keys_file = fopen(keys_file_path, "r");
70
69 if (keys_file != NULL) { 71 if (keys_file != NULL) {
70 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); 72 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
71 73
@@ -98,7 +100,8 @@ int manage_keys(DHT *dht, char *keys_file_path)
98// returns 1 on success 100// returns 1 on success
99// 0 on failure, doesn't modify any data pointed by arguments 101// 0 on failure, doesn't modify any data pointed by arguments
100 102
101int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6, int *enable_lan_discovery) 103int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6,
104 int *enable_lan_discovery)
102{ 105{
103 config_t cfg; 106 config_t cfg;
104 107
@@ -132,6 +135,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
132 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); 135 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH);
133 tmp_pid_file = DEFAULT_PID_FILE_PATH; 136 tmp_pid_file = DEFAULT_PID_FILE_PATH;
134 } 137 }
138
135 *pid_file_path = malloc(strlen(tmp_pid_file) + 1); 139 *pid_file_path = malloc(strlen(tmp_pid_file) + 1);
136 strcpy(*pid_file_path, tmp_pid_file); 140 strcpy(*pid_file_path, tmp_pid_file);
137 141
@@ -143,6 +147,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
143 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); 147 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH);
144 tmp_keys_file = DEFAULT_KEYS_FILE_PATH; 148 tmp_keys_file = DEFAULT_KEYS_FILE_PATH;
145 } 149 }
150
146 *keys_file_path = malloc(strlen(tmp_keys_file) + 1); 151 *keys_file_path = malloc(strlen(tmp_keys_file) + 1);
147 strcpy(*keys_file_path, tmp_keys_file); 152 strcpy(*keys_file_path, tmp_keys_file);
148 153
@@ -156,15 +161,16 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
156 // Get LAN discovery option 161 // Get LAN discovery option
157 if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { 162 if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
158 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); 163 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
159 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); 164 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
165 DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
160 *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; 166 *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
161 } 167 }
162 168
163 config_destroy(&cfg); 169 config_destroy(&cfg);
164 170
165 syslog(LOG_DEBUG, "Successfully read:\n"); 171 syslog(LOG_DEBUG, "Successfully read:\n");
166 syslog(LOG_DEBUG, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); 172 syslog(LOG_DEBUG, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path);
167 syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); 173 syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path);
168 syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port); 174 syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port);
169 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); 175 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false");
170 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); 176 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
@@ -219,10 +225,10 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
219 return 0; 225 return 0;
220 } 226 }
221 227
222 // Proceed only if all parts are present 228 // Proceed only if all parts are present
223 if (config_setting_lookup_string(server, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE || 229 if (config_setting_lookup_string(server, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE ||
224 config_setting_lookup_int (server, NAME_PORT, &bs_port) == CONFIG_FALSE || 230 config_setting_lookup_int (server, NAME_PORT, &bs_port) == CONFIG_FALSE ||
225 config_setting_lookup_string(server, NAME_ADDRESS, &bs_address) == CONFIG_FALSE ) { 231 config_setting_lookup_string(server, NAME_ADDRESS, &bs_address) == CONFIG_FALSE ) {
226 goto next; 232 goto next;
227 } 233 }
228 234
@@ -237,7 +243,8 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
237 goto next; 243 goto next;
238 } 244 }
239 245
240 const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), hex_string_to_bin((char*)bs_public_key)); 246 const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port),
247 hex_string_to_bin((char *)bs_public_key));
241 248
242 if (!address_resolved) { 249 if (!address_resolved) {
243 syslog(LOG_WARNING, "bootstrap_server #%d: Invalid '%s': %s.\n", i, NAME_ADDRESS, bs_address); 250 syslog(LOG_WARNING, "bootstrap_server #%d: Invalid '%s': %s.\n", i, NAME_ADDRESS, bs_address);
@@ -246,7 +253,7 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
246 253
247 syslog(LOG_DEBUG, "Successfully connected to %s:%d %s\n", bs_address, bs_port, bs_public_key); 254 syslog(LOG_DEBUG, "Successfully connected to %s:%d %s\n", bs_address, bs_port, bs_public_key);
248 255
249 next: 256next:
250 // config_setting_lookup_string() allocates string inside and doesn't allow us to free it 257 // config_setting_lookup_string() allocates string inside and doesn't allow us to free it
251 // so in order to reuse `bs_public_key` and `bs_address` we have to remove the element 258 // so in order to reuse `bs_public_key` and `bs_address` we have to remove the element
252 // which will cause libconfig to free allocated strings 259 // which will cause libconfig to free allocated strings
@@ -269,6 +276,7 @@ int is_conencted(DHT *dht, int port, int enable_lan_discovery)
269 uint16_t htons_port = htons(port); 276 uint16_t htons_port = htons(port);
270 277
271 int i; 278 int i;
279
272 for (i = 0; i < 100; i ++) { 280 for (i = 0; i < 100; i ++) {
273 do_DHT(dht); 281 do_DHT(dht);
274 282
@@ -296,10 +304,12 @@ void print_public_key(uint8_t *public_key)
296 int index = 0; 304 int index = 0;
297 305
298 int i; 306 int i;
307
299 for (i = 0; i < 32; i++) { 308 for (i = 0; i < 32; i++) {
300 if (public_key[i] < 16) { 309 if (public_key[i] < 16) {
301 index += sprintf(buffer + index, "0"); 310 index += sprintf(buffer + index, "0");
302 } 311 }
312
303 index += sprintf(buffer + index, "%hhX", public_key[i]); 313 index += sprintf(buffer + index, "%hhX", public_key[i]);
304 } 314 }
305 315
@@ -346,6 +356,7 @@ int main(int argc, char *argv[])
346 ip_init(&ip, enable_ipv6); 356 ip_init(&ip, enable_ipv6);
347 357
348 DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port))); 358 DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port)));
359
349 if (dht == NULL) { 360 if (dht == NULL) {
350 syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n"); 361 syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n");
351 return 1; 362 return 1;
@@ -380,6 +391,7 @@ int main(int argc, char *argv[])
380 391
381 // Write the PID file 392 // Write the PID file
382 FILE *pidf = fopen(pid_file_path, "w"); 393 FILE *pidf = fopen(pid_file_path, "w");
394
383 if (pidf == NULL) { 395 if (pidf == NULL) {
384 syslog(LOG_ERR, "Can't open the PID file for writing: %s. Exiting.\n", pid_file_path); 396 syslog(LOG_ERR, "Can't open the PID file for writing: %s. Exiting.\n", pid_file_path);
385 return 1; 397 return 1;
@@ -408,19 +420,19 @@ int main(int argc, char *argv[])
408 fprintf(pidf, "%d\n", pid); 420 fprintf(pidf, "%d\n", pid);
409 fclose(pidf); 421 fclose(pidf);
410 422
411 // Create a new SID for the child process 423 // Create a new SID for the child process
412 if (setsid() < 0) { 424 if (setsid() < 0) {
413 syslog(LOG_ERR, "SID creation failure. Exiting.\n"); 425 syslog(LOG_ERR, "SID creation failure. Exiting.\n");
414 return 1; 426 return 1;
415 } 427 }
416 428
417 // Change the current working directory 429 // Change the current working directory
418 if ((chdir("/")) < 0) { 430 if ((chdir("/")) < 0) {
419 syslog(LOG_ERR, "Couldn't change working directory to '/'. Exiting.\n"); 431 syslog(LOG_ERR, "Couldn't change working directory to '/'. Exiting.\n");
420 return 1; 432 return 1;
421 } 433 }
422 434
423 // Go quiet 435 // Go quiet
424 close(STDOUT_FILENO); 436 close(STDOUT_FILENO);
425 close(STDIN_FILENO); 437 close(STDIN_FILENO);
426 close(STDERR_FILENO); 438 close(STDERR_FILENO);