aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-04-10 12:16:44 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-04-10 12:16:44 +0000
commit8cb36e97194eef321a9aa6d74c00e477c937a3dc (patch)
treeb5920344ff57ff9e5b5aba1939fc023b8da239f7
parent46d9b8ca21dead581b8fa3a1cb6b00d55965b15e (diff)
downloadgnunet-8cb36e97194eef321a9aa6d74c00e477c937a3dc.tar.gz
gnunet-8cb36e97194eef321a9aa6d74c00e477c937a3dc.zip
simplified hostfile reading
-rw-r--r--src/peerinfo/gnunet-service-peerinfo.c145
1 files changed, 38 insertions, 107 deletions
diff --git a/src/peerinfo/gnunet-service-peerinfo.c b/src/peerinfo/gnunet-service-peerinfo.c
index a76ac81d2..d725968d8 100644
--- a/src/peerinfo/gnunet-service-peerinfo.c
+++ b/src/peerinfo/gnunet-service-peerinfo.c
@@ -325,22 +325,16 @@ static void
325read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r) 325read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r)
326{ 326{
327 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN; 327 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
328 const struct GNUNET_HELLO_Message *hello_1st; 328
329 const struct GNUNET_HELLO_Message *hello_2nd;
330 struct GNUNET_HELLO_Message *hello_clean_1st;
331 struct GNUNET_HELLO_Message *hello_clean_2nd;
332 int size_1st;
333 int size_2nd;
334 int size_total; 329 int size_total;
335 struct GNUNET_TIME_Absolute now; 330 struct GNUNET_TIME_Absolute now;
336 unsigned int left; 331 unsigned int left;
337 332
338 hello_1st = NULL; 333 const struct GNUNET_HELLO_Message *hello;
339 hello_2nd = NULL; 334 struct GNUNET_HELLO_Message *hello_clean;
340 hello_clean_1st = NULL; 335 unsigned read_pos;
341 hello_clean_2nd = NULL; 336 int size_hello;
342 size_1st = 0; 337
343 size_2nd = 0;
344 size_total = 0; 338 size_total = 0;
345 r->friend_only_hello = NULL; 339 r->friend_only_hello = NULL;
346 r->hello = NULL; 340 r->hello = NULL;
@@ -362,132 +356,69 @@ read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *
362 return; 356 return;
363 } 357 }
364 358
365 hello_1st = (const struct GNUNET_HELLO_Message *) buffer; 359 read_pos = 0;
366 size_1st = ntohs (((struct GNUNET_MessageHeader *) &buffer)->size); 360 while (read_pos < size_total)
367 if (size_1st < sizeof (struct GNUNET_MessageHeader))
368 {
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 _("Failed to parse HELLO in file `%s': %s %u \n"),
371 fn, "1st HELLO has invalid size of ", size_1st);
372 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
373 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
374 return;
375 }
376 if (size_1st != GNUNET_HELLO_size (hello_1st))
377 {
378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
379 _("Failed to parse HELLO in file `%s': %s \n"),
380 fn, "1st HELLO is invalid");
381 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
382 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
383 return;
384 }
385
386 if (size_total > size_1st)
387 { 361 {
388 hello_2nd = (const struct GNUNET_HELLO_Message *) &buffer[size_1st]; 362 hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
389 size_2nd = ntohs (((const struct GNUNET_MessageHeader *) hello_2nd)->size); 363 size_hello = GNUNET_HELLO_size (hello);
390 if ((size_2nd < sizeof (struct GNUNET_MessageHeader)) || 364 if (0 == size_hello)
391 (size_2nd != GNUNET_HELLO_size (hello_2nd))) 365 {
392 { 366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 367 _("Failed to parse HELLO in file `%s': %s %u \n"),
394 _("Failed to parse HELLO in file `%s': %s\n"), 368 fn, "HELLO is invalid and has size of ", size_hello);
395 fn, "2nd HELLO has wrong size"); 369 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
396 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn))) 370 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
397 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
398 return; 371 return;
399 } 372 }
400 }
401
402 if (size_total != (size_1st + size_2nd))
403 {
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 _("Failed to parse HELLO in file `%s': %s\n"),
406 fn, "Multiple HELLOs but total size is wrong");
407 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
408 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
409 return;
410 }
411
412 now = GNUNET_TIME_absolute_get ();
413 hello_clean_1st = GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_YES,
414 &discard_expired, &now);
415 left = 0;
416 (void) GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_NO,
417 &count_addresses, &left);
418 if (0 == left)
419 {
420 /* no addresses left, remove from disk */
421 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
422 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
423 }
424
425 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_1st))
426 {
427 if (NULL == r->hello)
428 r->hello = hello_clean_1st;
429 else
430 {
431 GNUNET_break (0);
432 GNUNET_free (r->hello);
433 r->hello = hello_clean_1st;
434 }
435 }
436 else
437 {
438 if (NULL == r->friend_only_hello)
439 r->friend_only_hello = hello_clean_1st;
440 else
441 {
442 GNUNET_break (0);
443 GNUNET_free (r->friend_only_hello);
444 r->friend_only_hello = hello_clean_1st;
445 }
446 }
447 373
448 if (NULL != hello_2nd) 374 now = GNUNET_TIME_absolute_get ();
449 { 375 hello_clean = GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES,
450 hello_clean_2nd = GNUNET_HELLO_iterate_addresses (hello_2nd, GNUNET_YES, 376 &discard_expired, &now);
451 &discard_expired, &now);
452 left = 0; 377 left = 0;
453 (void) GNUNET_HELLO_iterate_addresses (hello_clean_2nd, GNUNET_NO, 378 (void) GNUNET_HELLO_iterate_addresses (hello_clean, GNUNET_NO,
454 &count_addresses, &left); 379 &count_addresses, &left);
380
455 if (0 == left) 381 if (0 == left)
456 { 382 {
457 /* no addresses left, remove from disk */ 383 GNUNET_free (hello_clean);
458 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn))) 384 break;
459 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
460 } 385 }
461 386
462 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_2nd)) 387 if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello_clean))
463 { 388 {
464 if (NULL == r->hello) 389 if (NULL == r->hello)
465 r->hello = hello_clean_2nd; 390 r->hello = hello_clean;
466 else 391 else
467 { 392 {
468 GNUNET_break (0); 393 GNUNET_break (0);
469 GNUNET_free (r->hello); 394 GNUNET_free (r->hello);
470 r->hello = hello_clean_2nd; 395 r->hello = hello_clean;
471 } 396 }
472 } 397 }
473 else 398 else
474 { 399 {
475 if (NULL == r->friend_only_hello) 400 if (NULL == r->friend_only_hello)
476 r->friend_only_hello = hello_clean_2nd; 401 r->friend_only_hello = hello_clean;
477 else 402 else
478 { 403 {
479 GNUNET_break (0); 404 GNUNET_break (0);
480 GNUNET_free (r->friend_only_hello); 405 GNUNET_free (r->friend_only_hello);
481 r->friend_only_hello = hello_clean_2nd; 406 r->friend_only_hello = hello_clean;
482 } 407 }
483 } 408 }
409 read_pos += size_hello;
410 }
411
412 if (0 == left)
413 {
414 /* no addresses left, remove from disk */
415 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
416 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
484 } 417 }
485 418
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' and `%s' HELLO message in file\n", 419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' and `%s' HELLO message in file\n",
487 (NULL != r->hello) ? "public" : "NO public", 420 (NULL != r->hello) ? "public" : "NO public",
488 (NULL != r->friend_only_hello) ? "friend only" : "NO friend only"); 421 (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
489
490
491} 422}
492 423
493 424