summaryrefslogtreecommitdiff
path: root/other/bootstrap_serverdaemon
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-16 13:11:09 -0400
committerirungentoo <irungentoo@gmail.com>2013-08-16 13:11:09 -0400
commit88ff81d9def5efe69cbaf91aa41906177ba7dde9 (patch)
treecb9f149e438bcd1f18d8c1eb5d8be6b0a22f58a4 /other/bootstrap_serverdaemon
parentc5af8f44a9d040a0bbe0442ec074d9fc8562dd32 (diff)
Passed everything through astyle.
Diffstat (limited to 'other/bootstrap_serverdaemon')
-rw-r--r--other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c86
1 files changed, 48 insertions, 38 deletions
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 @@
1/* DHT boostrap 1/* DHT boostrap
2 * 2 *
3 * A simple DHT boostrap server for tox - daemon edition. 3 * A simple DHT boostrap server for tox - daemon edition.
4 * 4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved. 5 * Copyright (C) 2013 Tox project All Rights Reserved.
6 * 6 *
7 * This file is part of Tox. 7 * This file is part of Tox.
@@ -18,7 +18,7 @@
18 * 18 *
19 * You should have received a copy of the GNU General Public License 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/>. 20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 * 21 *
22 */ 22 */
23 23
24#include <sys/types.h> /* pid_t */ 24#include <sys/types.h> /* pid_t */
@@ -55,19 +55,20 @@ struct server_conf_s {
55 struct server_info_s info[32]; 55 struct server_info_s info[32];
56}; 56};
57 57
58int b16_to_key(char b16_string[], uint8_t *bs_pubkey) { 58int b16_to_key(char b16_string[], uint8_t *bs_pubkey)
59{
59 60
60 int i; 61 int i;
61 unsigned int num1 = 0, num2 = 0; 62 unsigned int num1 = 0, num2 = 0;
62 63
63 for(i = 0; i < 32; ++i) 64 for (i = 0; i < 32; ++i) {
64 { 65 sscanf(&b16_string[i * 2], "%1X", &num1);
65 sscanf(&b16_string[i*2], "%1X", &num1); 66 sscanf(&b16_string[i * 2 + 1], "%1X", &num2);
66 sscanf(&b16_string[i*2+1], "%1X", &num2);
67 num1 = num1 << 4; 67 num1 = num1 << 4;
68 bs_pubkey[i] = bs_pubkey[i] | num1; 68 bs_pubkey[i] = bs_pubkey[i] | num1;
69 bs_pubkey[i] = bs_pubkey[i] | num2; 69 bs_pubkey[i] = bs_pubkey[i] | num2;
70 } 70 }
71
71 return 0; 72 return 0;
72} 73}
73 74
@@ -81,8 +82,8 @@ int connect_to_servers(struct server_info_s *info)
81 int i; 82 int i;
82 int c; 83 int c;
83 84
84 for(i = 0; i < 32; ++i) { 85 for (i = 0; i < 32; ++i) {
85 if(info[i].valid) { 86 if (info[i].valid) {
86 /* Actual bootstrapping code goes here */ 87 /* Actual bootstrapping code goes here */
87 //puts("Calling DHT_bootstrap"); 88 //puts("Calling DHT_bootstrap");
88 DHT_bootstrap(info[i].conn, info[i].bs_pk); 89 DHT_bootstrap(info[i].conn, info[i].bs_pk);
@@ -90,14 +91,16 @@ int connect_to_servers(struct server_info_s *info)
90 } 91 }
91 92
92 /* Check if we're connected to the DHT */ 93 /* Check if we're connected to the DHT */
93 for(c = 0; c != 100; ++c) { 94 for (c = 0; c != 100; ++c) {
94 usleep(10000); 95 usleep(10000);
95 if(DHT_isconnected()) { 96
97 if (DHT_isconnected()) {
96 //puts("Connected"); 98 //puts("Connected");
97 return 1; 99 return 1;
98 break; 100 break;
99 } 101 }
100 if(DHT_isconnected() == 0 && c == 99) { 102
103 if (DHT_isconnected() == 0 && c == 99) {
101 //puts("Not connected"); 104 //puts("Not connected");
102 return -1; 105 return -1;
103 break; 106 break;
@@ -118,17 +121,19 @@ void manage_keys(char *keys_file)
118 uint8_t keys[KEYS_SIZE]; 121 uint8_t keys[KEYS_SIZE];
119 struct stat existence; 122 struct stat existence;
120 FILE *keysf; 123 FILE *keysf;
121 124
122 /* Check if file exits, proceed to open and load keys */ 125 /* Check if file exits, proceed to open and load keys */
123 if(stat(keys_file,&existence) >= 0) { 126 if (stat(keys_file, &existence) >= 0) {
124 keysf = fopen(keys_file, "r"); 127 keysf = fopen(keys_file, "r");
125 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keysf); 128 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keysf);
129
126 if (read_size != KEYS_SIZE) { 130 if (read_size != KEYS_SIZE) {
127 printf("Error while reading the key file\nExiting.\n"); 131 printf("Error while reading the key file\nExiting.\n");
128 exit(1); 132 exit(1);
129 } else { 133 } else {
130 printf("Keys loaded successfully\n"); 134 printf("Keys loaded successfully\n");
131 } 135 }
136
132 load_keys(keys); 137 load_keys(keys);
133 138
134 } else { 139 } else {
@@ -137,6 +142,7 @@ void manage_keys(char *keys_file)
137 new_keys(); 142 new_keys();
138 save_keys(keys); 143 save_keys(keys);
139 keysf = fopen(keys_file, "w"); 144 keysf = fopen(keys_file, "w");
145
140 if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keysf) != KEYS_SIZE) { 146 if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keysf) != KEYS_SIZE) {
141 printf("Error while writing the key file.\nExiting.\n"); 147 printf("Error while writing the key file.\nExiting.\n");
142 exit(1); 148 exit(1);
@@ -183,8 +189,7 @@ struct server_conf_s configure_server(char *cfg_file)
183 config_init(&cfg); 189 config_init(&cfg);
184 190
185 /* Read the file. If there is an error, report it and exit. */ 191 /* Read the file. If there is an error, report it and exit. */
186 if(! config_read_file(&cfg, cfg_file)) 192 if (! config_read_file(&cfg, cfg_file)) {
187 {
188 fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), 193 fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
189 config_error_line(&cfg), config_error_text(&cfg)); 194 config_error_line(&cfg), config_error_text(&cfg));
190 config_destroy(&cfg); 195 config_destroy(&cfg);
@@ -193,14 +198,14 @@ struct server_conf_s configure_server(char *cfg_file)
193 } 198 }
194 199
195 /* Get the port to listen on */ 200 /* Get the port to listen on */
196 if(config_lookup_int(&cfg, "port", &server_conf.port)) { 201 if (config_lookup_int(&cfg, "port", &server_conf.port)) {
197 //printf("Port: %d\n", port); 202 //printf("Port: %d\n", port);
198 } else { 203 } else {
199 fprintf(stderr, "No 'port' setting in configuration file.\n"); 204 fprintf(stderr, "No 'port' setting in configuration file.\n");
200 } 205 }
201 206
202 /* Get PID file location */ 207 /* Get PID file location */
203 if(config_lookup_string(&cfg, "pid_file", &pid_file_tmp)) { 208 if (config_lookup_string(&cfg, "pid_file", &pid_file_tmp)) {
204 //printf("PID file: %s\n", pid_file_tmp); 209 //printf("PID file: %s\n", pid_file_tmp);
205 strcpy(server_conf.pid_file, pid_file_tmp); 210 strcpy(server_conf.pid_file, pid_file_tmp);
206 } else { 211 } else {
@@ -208,7 +213,7 @@ struct server_conf_s configure_server(char *cfg_file)
208 } 213 }
209 214
210 /* Get keys file location */ 215 /* Get keys file location */
211 if(config_lookup_string(&cfg, "keys_file", &keys_file_tmp)) { 216 if (config_lookup_string(&cfg, "keys_file", &keys_file_tmp)) {
212 //printf("Keys file: %s\n", keys_file_tmp); 217 //printf("Keys file: %s\n", keys_file_tmp);
213 strcpy(server_conf.keys_file, keys_file_tmp); 218 strcpy(server_conf.keys_file, keys_file_tmp);
214 } else { 219 } else {
@@ -217,38 +222,40 @@ struct server_conf_s configure_server(char *cfg_file)
217 222
218 /* Get all the servers in the list */ 223 /* Get all the servers in the list */
219 server_list = config_lookup(&cfg, "bootstrap_servers"); 224 server_list = config_lookup(&cfg, "bootstrap_servers");
220 if(server_list != NULL) { 225
226 if (server_list != NULL) {
221 int count = config_setting_length(server_list); 227 int count = config_setting_length(server_list);
222 int i; 228 int i;
223 229
224 char tmp_ip[30]; /* IP */ 230 char tmp_ip[30]; /* IP */
225 char tmp_pk[64]; /* bs_pk */ 231 char tmp_pk[64]; /* bs_pk */
226 for(i = 0; i < count; ++i) { 232
233 for (i = 0; i < count; ++i) {
227 config_setting_t *server = config_setting_get_elem(server_list, i); 234 config_setting_t *server = config_setting_get_elem(server_list, i);
228 /* Get a pointer on the key aray */ 235 /* Get a pointer on the key aray */
229 uint8_t *bs_pk_p = server_conf.info[i].bs_pk; 236 uint8_t *bs_pk_p = server_conf.info[i].bs_pk;
230 237
231 /* Only output the record if all of the expected fields are present. */ 238 /* Only output the record if all of the expected fields are present. */
232 if(!(config_setting_lookup_string(server, "ip", &bs_ip) 239 if (!(config_setting_lookup_string(server, "ip", &bs_ip)
233 && config_setting_lookup_int(server, "port", &bs_port) 240 && config_setting_lookup_int(server, "port", &bs_port)
234 && config_setting_lookup_string(server, "bs_pk", &bs_pk))) 241 && config_setting_lookup_string(server, "bs_pk", &bs_pk)))
235 continue; 242 continue;
236 243
237 /* Converting all that stuff into usable formats and storing 244 /* Converting all that stuff into usable formats and storing
238 it away in the server_info struct */ 245 it away in the server_info struct */
239 server_conf.info[i].valid = 1; 246 server_conf.info[i].valid = 1;
240 247
241 if(resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) { 248 if (resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) {
242 server_conf.info[i].valid = 0; 249 server_conf.info[i].valid = 0;
243 printf("bootstrap_server %d: Invalid IP\n", i); 250 printf("bootstrap_server %d: Invalid IP\n", i);
244 } 251 }
245 252
246 if(strlen(bs_pk) != 64) { 253 if (strlen(bs_pk) != 64) {
247 server_conf.info[i].valid = 0; 254 server_conf.info[i].valid = 0;
248 printf("bootstrap_server %d: Invalid public key\n", i); 255 printf("bootstrap_server %d: Invalid public key\n", i);
249 } 256 }
250 257
251 if(!bs_port) { 258 if (!bs_port) {
252 server_conf.info[i].valid = 0; 259 server_conf.info[i].valid = 0;
253 printf("bootstrap_server %d: Invalid port\n", i); 260 printf("bootstrap_server %d: Invalid port\n", i);
254 } 261 }
@@ -259,8 +266,8 @@ struct server_conf_s configure_server(char *cfg_file)
259 } 266 }
260 267
261 /* Check if at least one server entry is valid */ 268 /* Check if at least one server entry is valid */
262 for(i = 0; i < 32; ++i) { 269 for (i = 0; i < 32; ++i) {
263 if(server_conf.info[i].valid) 270 if (server_conf.info[i].valid)
264 break; 271 break;
265 else 272 else
266 server_conf.err = -2; 273 server_conf.err = -2;
@@ -274,14 +281,15 @@ struct server_conf_s configure_server(char *cfg_file)
274 return server_conf; 281 return server_conf;
275} 282}
276 283
277int main(int argc, char *argv[]) { 284int main(int argc, char *argv[])
285{
278 286
279 pid_t pid, sid; /* Process- and Session-ID */ 287 pid_t pid, sid; /* Process- and Session-ID */
280 struct server_conf_s server_conf; 288 struct server_conf_s server_conf;
281 289
282 FILE *pidf; /* The PID file */ 290 FILE *pidf; /* The PID file */
283 291
284 if(argc < 2) { 292 if (argc < 2) {
285 printf("Please specify a configuration file.\n"); 293 printf("Please specify a configuration file.\n");
286 exit(EXIT_FAILURE); 294 exit(EXIT_FAILURE);
287 } 295 }
@@ -292,10 +300,10 @@ int main(int argc, char *argv[]) {
292 printf("PID file: %s\n", server_conf.pid_file); 300 printf("PID file: %s\n", server_conf.pid_file);
293 printf("Key file: %s\n", server_conf.keys_file); 301 printf("Key file: %s\n", server_conf.keys_file);
294 302
295 if(server_conf.err == -1) 303 if (server_conf.err == -1)
296 printf("Config file not read.\n"); 304 printf("Config file not read.\n");
297 305
298 if(server_conf.err == -2) 306 if (server_conf.err == -2)
299 printf("No valid servers in list.\n"); 307 printf("No valid servers in list.\n");
300 308
301 /* Open PID file for writing - if an error happens, 309 /* Open PID file for writing - if an error happens,
@@ -311,14 +319,15 @@ int main(int argc, char *argv[]) {
311 /* Public key */ 319 /* Public key */
312 int i; 320 int i;
313 printf("\nPublic Key: "); 321 printf("\nPublic Key: ");
314 for(i = 0; i < 32; ++i) 322
315 { 323 for (i = 0; i < 32; ++i) {
316 uint8_t ln, hn; 324 uint8_t ln, hn;
317 ln = 0x0F & self_public_key[i]; 325 ln = 0x0F & self_public_key[i];
318 hn = 0xF0 & self_public_key[i]; 326 hn = 0xF0 & self_public_key[i];
319 hn = hn >> 4; 327 hn = hn >> 4;
320 printf("%X%X", hn, ln); 328 printf("%X%X", hn, ln);
321 } 329 }
330
322 printf("\n"); 331 printf("\n");
323 332
324 /* initialize networking 333 /* initialize networking
@@ -335,7 +344,7 @@ int main(int argc, char *argv[]) {
335 connect_to_servers(server_conf.info); 344 connect_to_servers(server_conf.info);
336 errno = tmperr; 345 errno = tmperr;
337 346
338 if(!DHT_isconnected()) { 347 if (!DHT_isconnected()) {
339 puts("Could not establish DHT connection. Check server settings.\n"); 348 puts("Could not establish DHT connection. Check server settings.\n");
340 exit(EXIT_FAILURE); 349 exit(EXIT_FAILURE);
341 } else { 350 } else {
@@ -355,6 +364,7 @@ int main(int argc, char *argv[]) {
355 364
356 /* Fork off from the parent process */ 365 /* Fork off from the parent process */
357 pid = fork(); 366 pid = fork();
367
358 if (pid < 0) { 368 if (pid < 0) {
359 printf("Forking failed.\n"); 369 printf("Forking failed.\n");
360 exit(EXIT_FAILURE); 370 exit(EXIT_FAILURE);
@@ -378,6 +388,7 @@ int main(int argc, char *argv[]) {
378 388
379 /* Create a new SID for the child process */ 389 /* Create a new SID for the child process */
380 sid = setsid(); 390 sid = setsid();
391
381 if (sid < 0) { 392 if (sid < 0) {
382 printf("SID creation failure.\n"); 393 printf("SID creation failure.\n");
383 exit(EXIT_FAILURE); 394 exit(EXIT_FAILURE);
@@ -396,8 +407,7 @@ int main(int argc, char *argv[]) {
396 /* Main loop */ 407 /* Main loop */
397 friendreq_init(); 408 friendreq_init();
398 409
399 while(1) 410 while (1) {
400 {
401 doDHT(); 411 doDHT();
402 412
403 networking_poll(); 413 networking_poll();