From 88ff81d9def5efe69cbaf91aa41906177ba7dde9 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 16 Aug 2013 13:11:09 -0400 Subject: Passed everything through astyle. --- .../bootstrap_serverdaemon/DHT_bootstrap_daemon.c | 86 ++++++++++++---------- 1 file changed, 48 insertions(+), 38 deletions(-) (limited to 'other/bootstrap_serverdaemon') diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c index 6ce542c1..896d6a69 100644 --- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c @@ -1,7 +1,7 @@ /* DHT boostrap * * A simple DHT boostrap server for tox - daemon edition. - * + * * Copyright (C) 2013 Tox project All Rights Reserved. * * This file is part of Tox. @@ -18,7 +18,7 @@ * * You should have received a copy of the GNU General Public License * along with Tox. If not, see . - * + * */ #include /* pid_t */ @@ -55,19 +55,20 @@ struct server_conf_s { struct server_info_s info[32]; }; -int b16_to_key(char b16_string[], uint8_t *bs_pubkey) { +int b16_to_key(char b16_string[], uint8_t *bs_pubkey) +{ int i; unsigned int num1 = 0, num2 = 0; - for(i = 0; i < 32; ++i) - { - sscanf(&b16_string[i*2], "%1X", &num1); - sscanf(&b16_string[i*2+1], "%1X", &num2); + for (i = 0; i < 32; ++i) { + sscanf(&b16_string[i * 2], "%1X", &num1); + sscanf(&b16_string[i * 2 + 1], "%1X", &num2); num1 = num1 << 4; bs_pubkey[i] = bs_pubkey[i] | num1; bs_pubkey[i] = bs_pubkey[i] | num2; } + return 0; } @@ -81,8 +82,8 @@ int connect_to_servers(struct server_info_s *info) int i; int c; - for(i = 0; i < 32; ++i) { - if(info[i].valid) { + for (i = 0; i < 32; ++i) { + if (info[i].valid) { /* Actual bootstrapping code goes here */ //puts("Calling DHT_bootstrap"); DHT_bootstrap(info[i].conn, info[i].bs_pk); @@ -90,14 +91,16 @@ int connect_to_servers(struct server_info_s *info) } /* Check if we're connected to the DHT */ - for(c = 0; c != 100; ++c) { + for (c = 0; c != 100; ++c) { usleep(10000); - if(DHT_isconnected()) { + + if (DHT_isconnected()) { //puts("Connected"); return 1; break; } - if(DHT_isconnected() == 0 && c == 99) { + + if (DHT_isconnected() == 0 && c == 99) { //puts("Not connected"); return -1; break; @@ -118,17 +121,19 @@ void manage_keys(char *keys_file) uint8_t keys[KEYS_SIZE]; struct stat existence; FILE *keysf; - + /* Check if file exits, proceed to open and load keys */ - if(stat(keys_file,&existence) >= 0) { + if (stat(keys_file, &existence) >= 0) { keysf = fopen(keys_file, "r"); size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keysf); + if (read_size != KEYS_SIZE) { printf("Error while reading the key file\nExiting.\n"); exit(1); } else { printf("Keys loaded successfully\n"); } + load_keys(keys); } else { @@ -137,6 +142,7 @@ void manage_keys(char *keys_file) new_keys(); save_keys(keys); keysf = fopen(keys_file, "w"); + if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keysf) != KEYS_SIZE) { printf("Error while writing the key file.\nExiting.\n"); exit(1); @@ -183,8 +189,7 @@ struct server_conf_s configure_server(char *cfg_file) config_init(&cfg); /* Read the file. If there is an error, report it and exit. */ - if(! config_read_file(&cfg, cfg_file)) - { + if (! config_read_file(&cfg, cfg_file)) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); @@ -193,14 +198,14 @@ struct server_conf_s configure_server(char *cfg_file) } /* Get the port to listen on */ - if(config_lookup_int(&cfg, "port", &server_conf.port)) { + if (config_lookup_int(&cfg, "port", &server_conf.port)) { //printf("Port: %d\n", port); } else { fprintf(stderr, "No 'port' setting in configuration file.\n"); } /* Get PID file location */ - if(config_lookup_string(&cfg, "pid_file", &pid_file_tmp)) { + if (config_lookup_string(&cfg, "pid_file", &pid_file_tmp)) { //printf("PID file: %s\n", pid_file_tmp); strcpy(server_conf.pid_file, pid_file_tmp); } else { @@ -208,7 +213,7 @@ struct server_conf_s configure_server(char *cfg_file) } /* Get keys file location */ - if(config_lookup_string(&cfg, "keys_file", &keys_file_tmp)) { + if (config_lookup_string(&cfg, "keys_file", &keys_file_tmp)) { //printf("Keys file: %s\n", keys_file_tmp); strcpy(server_conf.keys_file, keys_file_tmp); } else { @@ -217,38 +222,40 @@ struct server_conf_s configure_server(char *cfg_file) /* Get all the servers in the list */ server_list = config_lookup(&cfg, "bootstrap_servers"); - if(server_list != NULL) { + + if (server_list != NULL) { int count = config_setting_length(server_list); int i; char tmp_ip[30]; /* IP */ char tmp_pk[64]; /* bs_pk */ - for(i = 0; i < count; ++i) { + + for (i = 0; i < count; ++i) { config_setting_t *server = config_setting_get_elem(server_list, i); /* Get a pointer on the key aray */ uint8_t *bs_pk_p = server_conf.info[i].bs_pk; /* Only output the record if all of the expected fields are present. */ - if(!(config_setting_lookup_string(server, "ip", &bs_ip) - && config_setting_lookup_int(server, "port", &bs_port) - && config_setting_lookup_string(server, "bs_pk", &bs_pk))) + if (!(config_setting_lookup_string(server, "ip", &bs_ip) + && config_setting_lookup_int(server, "port", &bs_port) + && config_setting_lookup_string(server, "bs_pk", &bs_pk))) continue; /* Converting all that stuff into usable formats and storing it away in the server_info struct */ server_conf.info[i].valid = 1; - if(resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) { + if (resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) { server_conf.info[i].valid = 0; printf("bootstrap_server %d: Invalid IP\n", i); } - if(strlen(bs_pk) != 64) { + if (strlen(bs_pk) != 64) { server_conf.info[i].valid = 0; printf("bootstrap_server %d: Invalid public key\n", i); } - if(!bs_port) { + if (!bs_port) { server_conf.info[i].valid = 0; printf("bootstrap_server %d: Invalid port\n", i); } @@ -259,8 +266,8 @@ struct server_conf_s configure_server(char *cfg_file) } /* Check if at least one server entry is valid */ - for(i = 0; i < 32; ++i) { - if(server_conf.info[i].valid) + for (i = 0; i < 32; ++i) { + if (server_conf.info[i].valid) break; else server_conf.err = -2; @@ -274,14 +281,15 @@ struct server_conf_s configure_server(char *cfg_file) return server_conf; } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ pid_t pid, sid; /* Process- and Session-ID */ struct server_conf_s server_conf; FILE *pidf; /* The PID file */ - if(argc < 2) { + if (argc < 2) { printf("Please specify a configuration file.\n"); exit(EXIT_FAILURE); } @@ -292,10 +300,10 @@ int main(int argc, char *argv[]) { printf("PID file: %s\n", server_conf.pid_file); printf("Key file: %s\n", server_conf.keys_file); - if(server_conf.err == -1) + if (server_conf.err == -1) printf("Config file not read.\n"); - if(server_conf.err == -2) + if (server_conf.err == -2) printf("No valid servers in list.\n"); /* Open PID file for writing - if an error happens, @@ -311,14 +319,15 @@ int main(int argc, char *argv[]) { /* Public key */ int i; printf("\nPublic Key: "); - for(i = 0; i < 32; ++i) - { + + for (i = 0; i < 32; ++i) { uint8_t ln, hn; ln = 0x0F & self_public_key[i]; hn = 0xF0 & self_public_key[i]; hn = hn >> 4; printf("%X%X", hn, ln); } + printf("\n"); /* initialize networking @@ -335,7 +344,7 @@ int main(int argc, char *argv[]) { connect_to_servers(server_conf.info); errno = tmperr; - if(!DHT_isconnected()) { + if (!DHT_isconnected()) { puts("Could not establish DHT connection. Check server settings.\n"); exit(EXIT_FAILURE); } else { @@ -355,6 +364,7 @@ int main(int argc, char *argv[]) { /* Fork off from the parent process */ pid = fork(); + if (pid < 0) { printf("Forking failed.\n"); exit(EXIT_FAILURE); @@ -378,6 +388,7 @@ int main(int argc, char *argv[]) { /* Create a new SID for the child process */ sid = setsid(); + if (sid < 0) { printf("SID creation failure.\n"); exit(EXIT_FAILURE); @@ -396,8 +407,7 @@ int main(int argc, char *argv[]) { /* Main loop */ friendreq_init(); - while(1) - { + while (1) { doDHT(); networking_poll(); -- cgit v1.2.3