summaryrefslogtreecommitdiff
path: root/src/dht/gnunet-service-dht_neighbours.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dht/gnunet-service-dht_neighbours.c')
-rw-r--r--src/dht/gnunet-service-dht_neighbours.c2652
1 files changed, 1322 insertions, 1330 deletions
diff --git a/src/dht/gnunet-service-dht_neighbours.c b/src/dht/gnunet-service-dht_neighbours.c
index 4f040558a..90ef5429f 100644
--- a/src/dht/gnunet-service-dht_neighbours.c
+++ b/src/dht/gnunet-service-dht_neighbours.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file dht/gnunet-service-dht_neighbours.c 22 * @file dht/gnunet-service-dht_neighbours.c
@@ -46,7 +46,7 @@
46#include "gnunet-service-dht_routing.h" 46#include "gnunet-service-dht_routing.h"
47#include "dht.h" 47#include "dht.h"
48 48
49#define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__) 49#define LOG_TRAFFIC(kind, ...) GNUNET_log_from(kind, "dht-traffic", __VA_ARGS__)
50 50
51/** 51/**
52 * Enable slow sanity checks to debug issues. 52 * Enable slow sanity checks to debug issues.
@@ -56,7 +56,7 @@
56/** 56/**
57 * How many buckets will we allow total. 57 * How many buckets will we allow total.
58 */ 58 */
59#define MAX_BUCKETS sizeof (struct GNUNET_HashCode) * 8 59#define MAX_BUCKETS sizeof(struct GNUNET_HashCode) * 8
60 60
61/** 61/**
62 * What is the maximum number of peers in a given bucket. 62 * What is the maximum number of peers in a given bucket.
@@ -104,8 +104,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
104/** 104/**
105 * P2P PUT message 105 * P2P PUT message
106 */ 106 */
107struct PeerPutMessage 107struct PeerPutMessage {
108{
109 /** 108 /**
110 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT 109 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
111 */ 110 */
@@ -154,15 +153,13 @@ struct PeerPutMessage
154 /* put path (if tracked) */ 153 /* put path (if tracked) */
155 154
156 /* Payload */ 155 /* Payload */
157
158}; 156};
159 157
160 158
161/** 159/**
162 * P2P Result message 160 * P2P Result message
163 */ 161 */
164struct PeerResultMessage 162struct PeerResultMessage {
165{
166 /** 163 /**
167 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT 164 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
168 */ 165 */
@@ -198,15 +195,13 @@ struct PeerResultMessage
198 /* get path (if tracked) */ 195 /* get path (if tracked) */
199 196
200 /* Payload */ 197 /* Payload */
201
202}; 198};
203 199
204 200
205/** 201/**
206 * P2P GET message 202 * P2P GET message
207 */ 203 */
208struct PeerGetMessage 204struct PeerGetMessage {
209{
210 /** 205 /**
211 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET 206 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET
212 */ 207 */
@@ -255,7 +250,6 @@ struct PeerGetMessage
255 /* xquery */ 250 /* xquery */
256 251
257 /* result bloomfilter */ 252 /* result bloomfilter */
258
259}; 253};
260GNUNET_NETWORK_STRUCT_END 254GNUNET_NETWORK_STRUCT_END
261 255
@@ -263,8 +257,7 @@ GNUNET_NETWORK_STRUCT_END
263/** 257/**
264 * Entry for a peer in a bucket. 258 * Entry for a peer in a bucket.
265 */ 259 */
266struct PeerInfo 260struct PeerInfo {
267{
268 /** 261 /**
269 * Next peer entry (DLL) 262 * Next peer entry (DLL)
270 */ 263 */
@@ -294,15 +287,13 @@ struct PeerInfo
294 * Which bucket is this peer in? 287 * Which bucket is this peer in?
295 */ 288 */
296 int peer_bucket; 289 int peer_bucket;
297
298}; 290};
299 291
300 292
301/** 293/**
302 * Peers are grouped into buckets. 294 * Peers are grouped into buckets.
303 */ 295 */
304struct PeerBucket 296struct PeerBucket {
305{
306 /** 297 /**
307 * Head of DLL 298 * Head of DLL
308 */ 299 */
@@ -323,9 +314,7 @@ struct PeerBucket
323/** 314/**
324 * Information about a peer that we would like to connect to. 315 * Information about a peer that we would like to connect to.
325 */ 316 */
326struct ConnectInfo 317struct ConnectInfo {
327{
328
329 /** 318 /**
330 * Handle to active HELLO offer operation, or NULL. 319 * Handle to active HELLO offer operation, or NULL.
331 */ 320 */
@@ -425,17 +414,17 @@ static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
425 * on error (same hashcode) 414 * on error (same hashcode)
426 */ 415 */
427static int 416static int
428find_bucket (const struct GNUNET_HashCode *hc) 417find_bucket(const struct GNUNET_HashCode *hc)
429{ 418{
430 unsigned int bits; 419 unsigned int bits;
431 420
432 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, hc); 421 bits = GNUNET_CRYPTO_hash_matching_bits(&my_identity_hash, hc);
433 if (bits == MAX_BUCKETS) 422 if (bits == MAX_BUCKETS)
434 { 423 {
435 /* How can all bits match? Got my own ID? */ 424 /* How can all bits match? Got my own ID? */
436 GNUNET_break (0); 425 GNUNET_break(0);
437 return GNUNET_SYSERR; 426 return GNUNET_SYSERR;
438 } 427 }
439 return MAX_BUCKETS - bits - 1; 428 return MAX_BUCKETS - bits - 1;
440} 429}
441 430
@@ -447,7 +436,7 @@ find_bucket (const struct GNUNET_HashCode *hc)
447 * @param cls a `struct ConnectInfo` 436 * @param cls a `struct ConnectInfo`
448 */ 437 */
449static void 438static void
450offer_hello_done (void *cls) 439offer_hello_done(void *cls)
451{ 440{
452 struct ConnectInfo *ci = cls; 441 struct ConnectInfo *ci = cls;
453 442
@@ -464,28 +453,28 @@ offer_hello_done (void *cls)
464 * @return #GNUNET_YES 453 * @return #GNUNET_YES
465 */ 454 */
466static int 455static int
467free_connect_info (void *cls, 456free_connect_info(void *cls,
468 const struct GNUNET_PeerIdentity *peer, 457 const struct GNUNET_PeerIdentity *peer,
469 void *value) 458 void *value)
470{ 459{
471 struct ConnectInfo *ci = value; 460 struct ConnectInfo *ci = value;
472 461
473 (void) cls; 462 (void)cls;
474 GNUNET_assert (GNUNET_YES == 463 GNUNET_assert(GNUNET_YES ==
475 GNUNET_CONTAINER_multipeermap_remove (all_desired_peers, 464 GNUNET_CONTAINER_multipeermap_remove(all_desired_peers,
476 peer, 465 peer,
477 ci)); 466 ci));
478 if (NULL != ci->sh) 467 if (NULL != ci->sh)
479 { 468 {
480 GNUNET_ATS_connectivity_suggest_cancel (ci->sh); 469 GNUNET_ATS_connectivity_suggest_cancel(ci->sh);
481 ci->sh = NULL; 470 ci->sh = NULL;
482 } 471 }
483 if (NULL != ci->oh) 472 if (NULL != ci->oh)
484 { 473 {
485 GNUNET_TRANSPORT_offer_hello_cancel (ci->oh); 474 GNUNET_TRANSPORT_offer_hello_cancel(ci->oh);
486 ci->oh = NULL; 475 ci->oh = NULL;
487 } 476 }
488 GNUNET_free (ci); 477 GNUNET_free(ci);
489 return GNUNET_YES; 478 return GNUNET_YES;
490} 479}
491 480
@@ -499,68 +488,68 @@ free_connect_info (void *cls,
499 * @param h a HELLO message, or NULL 488 * @param h a HELLO message, or NULL
500 */ 489 */
501static void 490static void
502try_connect (const struct GNUNET_PeerIdentity *pid, 491try_connect(const struct GNUNET_PeerIdentity *pid,
503 const struct GNUNET_MessageHeader *h) 492 const struct GNUNET_MessageHeader *h)
504{ 493{
505 int bucket; 494 int bucket;
506 struct GNUNET_HashCode pid_hash; 495 struct GNUNET_HashCode pid_hash;
507 struct ConnectInfo *ci; 496 struct ConnectInfo *ci;
508 uint32_t strength; 497 uint32_t strength;
509 498
510 GNUNET_CRYPTO_hash (pid, 499 GNUNET_CRYPTO_hash(pid,
511 sizeof (struct GNUNET_PeerIdentity), 500 sizeof(struct GNUNET_PeerIdentity),
512 &pid_hash); 501 &pid_hash);
513 bucket = find_bucket (&pid_hash); 502 bucket = find_bucket(&pid_hash);
514 if (bucket < 0) 503 if (bucket < 0)
515 return; /* self? */ 504 return; /* self? */
516 ci = GNUNET_CONTAINER_multipeermap_get (all_desired_peers, 505 ci = GNUNET_CONTAINER_multipeermap_get(all_desired_peers,
517 pid); 506 pid);
518 507
519 if (k_buckets[bucket].peers_size < bucket_size) 508 if (k_buckets[bucket].peers_size < bucket_size)
520 strength = (bucket_size - k_buckets[bucket].peers_size) * bucket; 509 strength = (bucket_size - k_buckets[bucket].peers_size) * bucket;
521 else 510 else
522 strength = bucket; /* minimum value of connectivity */ 511 strength = bucket; /* minimum value of connectivity */
523 if (GNUNET_YES == 512 if (GNUNET_YES ==
524 GNUNET_CONTAINER_multipeermap_contains (all_connected_peers, 513 GNUNET_CONTAINER_multipeermap_contains(all_connected_peers,
525 pid)) 514 pid))
526 strength *= 2; /* double for connected peers */ 515 strength *= 2; /* double for connected peers */
527 else if (k_buckets[bucket].peers_size > bucket_size) 516 else if (k_buckets[bucket].peers_size > bucket_size)
528 strength = 0; /* bucket full, we really do not care about more */ 517 strength = 0; /* bucket full, we really do not care about more */
529 518
530 if ( (0 == strength) && 519 if ((0 == strength) &&
531 (NULL != ci) ) 520 (NULL != ci))
532 { 521 {
533 /* release request */ 522 /* release request */
534 GNUNET_assert (GNUNET_YES == 523 GNUNET_assert(GNUNET_YES ==
535 free_connect_info (NULL, 524 free_connect_info(NULL,
536 pid, 525 pid,
537 ci)); 526 ci));
538 return; 527 return;
539 } 528 }
540 if (NULL == ci) 529 if (NULL == ci)
541 { 530 {
542 ci = GNUNET_new (struct ConnectInfo); 531 ci = GNUNET_new(struct ConnectInfo);
543 GNUNET_assert (GNUNET_OK == 532 GNUNET_assert(GNUNET_OK ==
544 GNUNET_CONTAINER_multipeermap_put (all_desired_peers, 533 GNUNET_CONTAINER_multipeermap_put(all_desired_peers,
545 pid, 534 pid,
546 ci, 535 ci,
547 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 536 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
548 } 537 }
549 if ( (NULL != ci->oh) && 538 if ((NULL != ci->oh) &&
550 (NULL != h) ) 539 (NULL != h))
551 GNUNET_TRANSPORT_offer_hello_cancel (ci->oh); 540 GNUNET_TRANSPORT_offer_hello_cancel(ci->oh);
552 if (NULL != h) 541 if (NULL != h)
553 ci->oh = GNUNET_TRANSPORT_offer_hello (GDS_cfg, 542 ci->oh = GNUNET_TRANSPORT_offer_hello(GDS_cfg,
554 h, 543 h,
555 &offer_hello_done, 544 &offer_hello_done,
556 ci); 545 ci);
557 if ( (NULL != ci->sh) && 546 if ((NULL != ci->sh) &&
558 (ci->strength != strength) ) 547 (ci->strength != strength))
559 GNUNET_ATS_connectivity_suggest_cancel (ci->sh); 548 GNUNET_ATS_connectivity_suggest_cancel(ci->sh);
560 if (ci->strength != strength) 549 if (ci->strength != strength)
561 ci->sh = GNUNET_ATS_connectivity_suggest (ats_ch, 550 ci->sh = GNUNET_ATS_connectivity_suggest(ats_ch,
562 pid, 551 pid,
563 strength); 552 strength);
564 ci->strength = strength; 553 ci->strength = strength;
565} 554}
566 555
@@ -578,14 +567,14 @@ try_connect (const struct GNUNET_PeerIdentity *pid,
578 * @return #GNUNET_YES (continue to iterate) 567 * @return #GNUNET_YES (continue to iterate)
579 */ 568 */
580static int 569static int
581update_desire_strength (void *cls, 570update_desire_strength(void *cls,
582 const struct GNUNET_PeerIdentity *pid, 571 const struct GNUNET_PeerIdentity *pid,
583 void *value) 572 void *value)
584{ 573{
585 (void) cls; 574 (void)cls;
586 (void) value; 575 (void)value;
587 try_connect (pid, 576 try_connect(pid,
588 NULL); 577 NULL);
589 return GNUNET_YES; 578 return GNUNET_YES;
590} 579}
591 580
@@ -597,11 +586,11 @@ update_desire_strength (void *cls,
597 * @param tc scheduler context. 586 * @param tc scheduler context.
598 */ 587 */
599static void 588static void
600update_connect_preferences () 589update_connect_preferences()
601{ 590{
602 GNUNET_CONTAINER_multipeermap_iterate (all_desired_peers, 591 GNUNET_CONTAINER_multipeermap_iterate(all_desired_peers,
603 &update_desire_strength, 592 &update_desire_strength,
604 NULL); 593 NULL);
605} 594}
606 595
607 596
@@ -615,24 +604,24 @@ update_connect_preferences ()
615 * @return #GNUNET_YES (we should continue to iterate) 604 * @return #GNUNET_YES (we should continue to iterate)
616 */ 605 */
617static int 606static int
618add_known_to_bloom (void *cls, 607add_known_to_bloom(void *cls,
619 const struct GNUNET_PeerIdentity *key, 608 const struct GNUNET_PeerIdentity *key,
620 void *value) 609 void *value)
621{ 610{
622 struct GNUNET_BLOCK_Group *bg = cls; 611 struct GNUNET_BLOCK_Group *bg = cls;
623 struct GNUNET_HashCode key_hash; 612 struct GNUNET_HashCode key_hash;
624 613
625 (void) cls; 614 (void)cls;
626 (void) value; 615 (void)value;
627 GNUNET_CRYPTO_hash (key, 616 GNUNET_CRYPTO_hash(key,
628 sizeof (struct GNUNET_PeerIdentity), 617 sizeof(struct GNUNET_PeerIdentity),
629 &key_hash); 618 &key_hash);
630 GNUNET_BLOCK_group_set_seen (bg, 619 GNUNET_BLOCK_group_set_seen(bg,
631 &key_hash, 620 &key_hash,
632 1); 621 1);
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 622 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
634 "Adding known peer (%s) to bloomfilter for FIND PEER\n", 623 "Adding known peer (%s) to bloomfilter for FIND PEER\n",
635 GNUNET_i2s (key)); 624 GNUNET_i2s(key));
636 return GNUNET_YES; 625 return GNUNET_YES;
637} 626}
638 627
@@ -645,68 +634,68 @@ add_known_to_bloom (void *cls,
645 * @param cls closure for this task 634 * @param cls closure for this task
646 */ 635 */
647static void 636static void
648send_find_peer_message (void *cls) 637send_find_peer_message(void *cls)
649{ 638{
650 struct GNUNET_TIME_Relative next_send_time; 639 struct GNUNET_TIME_Relative next_send_time;
651 struct GNUNET_BLOCK_Group *bg; 640 struct GNUNET_BLOCK_Group *bg;
652 struct GNUNET_CONTAINER_BloomFilter *peer_bf; 641 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
653 642
654 (void) cls; 643 (void)cls;
655 find_peer_task = NULL; 644 find_peer_task = NULL;
656 if (newly_found_peers > bucket_size) 645 if (newly_found_peers > bucket_size)
657 { 646 {
658 /* If we are finding many peers already, no need to send out our request right now! */ 647 /* If we are finding many peers already, no need to send out our request right now! */
659 find_peer_task = 648 find_peer_task =
660 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 649 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES,
661 &send_find_peer_message, 650 &send_find_peer_message,
662 NULL); 651 NULL);
663 newly_found_peers = 0; 652 newly_found_peers = 0;
664 return; 653 return;
665 } 654 }
666 bg = GNUNET_BLOCK_group_create (GDS_block_context, 655 bg = GNUNET_BLOCK_group_create(GDS_block_context,
667 GNUNET_BLOCK_TYPE_DHT_HELLO, 656 GNUNET_BLOCK_TYPE_DHT_HELLO,
668 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 657 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
669 UINT32_MAX), 658 UINT32_MAX),
670 NULL, 659 NULL,
671 0, 660 0,
672 "filter-size", 661 "filter-size",
673 DHT_BLOOM_SIZE, 662 DHT_BLOOM_SIZE,
674 NULL); 663 NULL);
675 GNUNET_CONTAINER_multipeermap_iterate (all_connected_peers, 664 GNUNET_CONTAINER_multipeermap_iterate(all_connected_peers,
676 &add_known_to_bloom, 665 &add_known_to_bloom,
677 bg); 666 bg);
678 GNUNET_STATISTICS_update (GDS_stats, 667 GNUNET_STATISTICS_update(GDS_stats,
679 gettext_noop ("# FIND PEER messages initiated"), 668 gettext_noop("# FIND PEER messages initiated"),
680 1, 669 1,
681 GNUNET_NO); 670 GNUNET_NO);
682 peer_bf 671 peer_bf
683 = GNUNET_CONTAINER_bloomfilter_init (NULL, 672 = GNUNET_CONTAINER_bloomfilter_init(NULL,
684 DHT_BLOOM_SIZE, 673 DHT_BLOOM_SIZE,
685 GNUNET_CONSTANTS_BLOOMFILTER_K); 674 GNUNET_CONSTANTS_BLOOMFILTER_K);
686 // FIXME: pass priority!? 675 // FIXME: pass priority!?
687 GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO, 676 GDS_NEIGHBOURS_handle_get(GNUNET_BLOCK_TYPE_DHT_HELLO,
688 GNUNET_DHT_RO_FIND_PEER | GNUNET_DHT_RO_RECORD_ROUTE, 677 GNUNET_DHT_RO_FIND_PEER | GNUNET_DHT_RO_RECORD_ROUTE,
689 FIND_PEER_REPLICATION_LEVEL, 678 FIND_PEER_REPLICATION_LEVEL,
690 0, 679 0,
691 &my_identity_hash, 680 &my_identity_hash,
692 NULL, 681 NULL,
693 0, 682 0,
694 bg, 683 bg,
695 peer_bf); 684 peer_bf);
696 GNUNET_CONTAINER_bloomfilter_free (peer_bf); 685 GNUNET_CONTAINER_bloomfilter_free(peer_bf);
697 GNUNET_BLOCK_group_destroy (bg); 686 GNUNET_BLOCK_group_destroy(bg);
698 /* schedule next round */ 687 /* schedule next round */
699 next_send_time.rel_value_us = 688 next_send_time.rel_value_us =
700 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value_us + 689 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value_us +
701 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 690 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
702 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value_us / 691 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value_us /
703 (newly_found_peers + 1)); 692 (newly_found_peers + 1));
704 newly_found_peers = 0; 693 newly_found_peers = 0;
705 GNUNET_assert (NULL == find_peer_task); 694 GNUNET_assert(NULL == find_peer_task);
706 find_peer_task = 695 find_peer_task =
707 GNUNET_SCHEDULER_add_delayed (next_send_time, 696 GNUNET_SCHEDULER_add_delayed(next_send_time,
708 &send_find_peer_message, 697 &send_find_peer_message,
709 NULL); 698 NULL);
710} 699}
711 700
712 701
@@ -719,61 +708,61 @@ send_find_peer_message (void *cls)
719 * @return our `struct PeerInfo` for @a peer 708 * @return our `struct PeerInfo` for @a peer
720 */ 709 */
721static void * 710static void *
722handle_core_connect (void *cls, 711handle_core_connect(void *cls,
723 const struct GNUNET_PeerIdentity *peer, 712 const struct GNUNET_PeerIdentity *peer,
724 struct GNUNET_MQ_Handle *mq) 713 struct GNUNET_MQ_Handle *mq)
725{ 714{
726 struct PeerInfo *pi; 715 struct PeerInfo *pi;
727 716
728 (void) cls; 717 (void)cls;
729 /* Check for connect to self message */ 718 /* Check for connect to self message */
730 if (0 == GNUNET_memcmp (&my_identity, 719 if (0 == GNUNET_memcmp(&my_identity,
731 peer)) 720 peer))
732 return NULL; 721 return NULL;
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 722 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
734 "Connected to %s\n", 723 "Connected to %s\n",
735 GNUNET_i2s (peer)); 724 GNUNET_i2s(peer));
736 GNUNET_assert (GNUNET_NO == 725 GNUNET_assert(GNUNET_NO ==
737 GNUNET_CONTAINER_multipeermap_get (all_connected_peers, 726 GNUNET_CONTAINER_multipeermap_get(all_connected_peers,
738 peer)); 727 peer));
739 GNUNET_STATISTICS_update (GDS_stats, 728 GNUNET_STATISTICS_update(GDS_stats,
740 gettext_noop ("# peers connected"), 729 gettext_noop("# peers connected"),
741 1, 730 1,
742 GNUNET_NO); 731 GNUNET_NO);
743 pi = GNUNET_new (struct PeerInfo); 732 pi = GNUNET_new(struct PeerInfo);
744 pi->id = peer; 733 pi->id = peer;
745 pi->mq = mq; 734 pi->mq = mq;
746 GNUNET_CRYPTO_hash (peer, 735 GNUNET_CRYPTO_hash(peer,
747 sizeof (struct GNUNET_PeerIdentity), 736 sizeof(struct GNUNET_PeerIdentity),
748 &pi->phash); 737 &pi->phash);
749 pi->peer_bucket = find_bucket (&pi->phash); 738 pi->peer_bucket = find_bucket(&pi->phash);
750 GNUNET_assert ( (pi->peer_bucket >= 0) && 739 GNUNET_assert((pi->peer_bucket >= 0) &&
751 ((unsigned int) pi->peer_bucket < MAX_BUCKETS) ); 740 ((unsigned int)pi->peer_bucket < MAX_BUCKETS));
752 GNUNET_CONTAINER_DLL_insert_tail (k_buckets[pi->peer_bucket].head, 741 GNUNET_CONTAINER_DLL_insert_tail(k_buckets[pi->peer_bucket].head,
753 k_buckets[pi->peer_bucket].tail, 742 k_buckets[pi->peer_bucket].tail,
754 pi); 743 pi);
755 k_buckets[pi->peer_bucket].peers_size++; 744 k_buckets[pi->peer_bucket].peers_size++;
756 closest_bucket = GNUNET_MAX (closest_bucket, 745 closest_bucket = GNUNET_MAX(closest_bucket,
757 (unsigned int) pi->peer_bucket); 746 (unsigned int)pi->peer_bucket);
758 GNUNET_assert (GNUNET_OK == 747 GNUNET_assert(GNUNET_OK ==
759 GNUNET_CONTAINER_multipeermap_put (all_connected_peers, 748 GNUNET_CONTAINER_multipeermap_put(all_connected_peers,
760 pi->id, 749 pi->id,
761 pi, 750 pi,
762 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
763 if ( (pi->peer_bucket > 0) && 752 if ((pi->peer_bucket > 0) &&
764 (k_buckets[pi->peer_bucket].peers_size <= bucket_size)) 753 (k_buckets[pi->peer_bucket].peers_size <= bucket_size))
765 { 754 {
766 update_connect_preferences (); 755 update_connect_preferences();
767 newly_found_peers++; 756 newly_found_peers++;
768 } 757 }
769 if ( (1 == GNUNET_CONTAINER_multipeermap_size (all_connected_peers)) && 758 if ((1 == GNUNET_CONTAINER_multipeermap_size(all_connected_peers)) &&
770 (GNUNET_YES != disable_try_connect)) 759 (GNUNET_YES != disable_try_connect))
771 { 760 {
772 /* got a first connection, good time to start with FIND PEER requests... */ 761 /* got a first connection, good time to start with FIND PEER requests... */
773 GNUNET_assert (NULL == find_peer_task); 762 GNUNET_assert(NULL == find_peer_task);
774 find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message, 763 find_peer_task = GNUNET_SCHEDULER_add_now(&send_find_peer_message,
775 NULL); 764 NULL);
776 } 765 }
777 return pi; 766 return pi;
778} 767}
779 768
@@ -786,45 +775,45 @@ handle_core_connect (void *cls,
786 * @param internal_cls our `struct PeerInfo` for @a peer 775 * @param internal_cls our `struct PeerInfo` for @a peer
787 */ 776 */
788static void 777static void
789handle_core_disconnect (void *cls, 778handle_core_disconnect(void *cls,
790 const struct GNUNET_PeerIdentity *peer, 779 const struct GNUNET_PeerIdentity *peer,
791 void *internal_cls) 780 void *internal_cls)
792{ 781{
793 struct PeerInfo *to_remove = internal_cls; 782 struct PeerInfo *to_remove = internal_cls;
794 783
795 (void) cls; 784 (void)cls;
796 /* Check for disconnect from self message */ 785 /* Check for disconnect from self message */
797 if (NULL == to_remove) 786 if (NULL == to_remove)
798 return; 787 return;
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 788 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
800 "Disconnected %s\n", 789 "Disconnected %s\n",
801 GNUNET_i2s (peer)); 790 GNUNET_i2s(peer));
802 GNUNET_STATISTICS_update (GDS_stats, 791 GNUNET_STATISTICS_update(GDS_stats,
803 gettext_noop ("# peers connected"), 792 gettext_noop("# peers connected"),
804 -1, 793 -1,
805 GNUNET_NO); 794 GNUNET_NO);
806 GNUNET_assert (GNUNET_YES == 795 GNUNET_assert(GNUNET_YES ==
807 GNUNET_CONTAINER_multipeermap_remove (all_connected_peers, 796 GNUNET_CONTAINER_multipeermap_remove(all_connected_peers,
808 peer, 797 peer,
809 to_remove)); 798 to_remove));
810 if ( (0 == GNUNET_CONTAINER_multipeermap_size (all_connected_peers)) && 799 if ((0 == GNUNET_CONTAINER_multipeermap_size(all_connected_peers)) &&
811 (GNUNET_YES != disable_try_connect) ) 800 (GNUNET_YES != disable_try_connect))
812 { 801 {
813 GNUNET_SCHEDULER_cancel (find_peer_task); 802 GNUNET_SCHEDULER_cancel(find_peer_task);
814 find_peer_task = NULL; 803 find_peer_task = NULL;
815 } 804 }
816 GNUNET_assert (to_remove->peer_bucket >= 0); 805 GNUNET_assert(to_remove->peer_bucket >= 0);
817 GNUNET_CONTAINER_DLL_remove (k_buckets[to_remove->peer_bucket].head, 806 GNUNET_CONTAINER_DLL_remove(k_buckets[to_remove->peer_bucket].head,
818 k_buckets[to_remove->peer_bucket].tail, 807 k_buckets[to_remove->peer_bucket].tail,
819 to_remove); 808 to_remove);
820 GNUNET_assert (k_buckets[to_remove->peer_bucket].peers_size > 0); 809 GNUNET_assert(k_buckets[to_remove->peer_bucket].peers_size > 0);
821 k_buckets[to_remove->peer_bucket].peers_size--; 810 k_buckets[to_remove->peer_bucket].peers_size--;
822 while ( (closest_bucket > 0) && 811 while ((closest_bucket > 0) &&
823 (0 == k_buckets[to_remove->peer_bucket].peers_size) ) 812 (0 == k_buckets[to_remove->peer_bucket].peers_size))
824 closest_bucket--; 813 closest_bucket--;
825 if (k_buckets[to_remove->peer_bucket].peers_size < bucket_size) 814 if (k_buckets[to_remove->peer_bucket].peers_size < bucket_size)
826 update_connect_preferences (); 815 update_connect_preferences();
827 GNUNET_free (to_remove); 816 GNUNET_free(to_remove);
828} 817}
829 818
830 819
@@ -837,39 +826,39 @@ handle_core_disconnect (void *cls,
837 * @return Some number of peers to forward the message to 826 * @return Some number of peers to forward the message to
838 */ 827 */
839static unsigned int 828static unsigned int
840get_forward_count (uint32_t hop_count, 829get_forward_count(uint32_t hop_count,
841 uint32_t target_replication) 830 uint32_t target_replication)
842{ 831{
843 uint32_t random_value; 832 uint32_t random_value;
844 uint32_t forward_count; 833 uint32_t forward_count;
845 float target_value; 834 float target_value;
846 835
847 if (hop_count > GDS_NSE_get () * 4.0) 836 if (hop_count > GDS_NSE_get() * 4.0)
848 { 837 {
849 /* forcefully terminate */ 838 /* forcefully terminate */
850 GNUNET_STATISTICS_update (GDS_stats, 839 GNUNET_STATISTICS_update(GDS_stats,
851 gettext_noop ("# requests TTL-dropped"), 840 gettext_noop("# requests TTL-dropped"),
852 1, GNUNET_NO); 841 1, GNUNET_NO);
853 return 0; 842 return 0;
854 } 843 }
855 if (hop_count > GDS_NSE_get () * 2.0) 844 if (hop_count > GDS_NSE_get() * 2.0)
856 { 845 {
857 /* Once we have reached our ideal number of hops, only forward to 1 peer */ 846 /* Once we have reached our ideal number of hops, only forward to 1 peer */
858 return 1; 847 return 1;
859 } 848 }
860 /* bound by system-wide maximum */ 849 /* bound by system-wide maximum */
861 target_replication = 850 target_replication =
862 GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL, target_replication); 851 GNUNET_MIN(MAXIMUM_REPLICATION_LEVEL, target_replication);
863 target_value = 852 target_value =
864 1 + (target_replication - 1.0) / (GDS_NSE_get () + 853 1 + (target_replication - 1.0) / (GDS_NSE_get() +
865 ((float) (target_replication - 1.0) * 854 ((float)(target_replication - 1.0) *
866 hop_count)); 855 hop_count));
867 /* Set forward count to floor of target_value */ 856 /* Set forward count to floor of target_value */
868 forward_count = (uint32_t) target_value; 857 forward_count = (uint32_t)target_value;
869 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */ 858 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
870 target_value = target_value - forward_count; 859 target_value = target_value - forward_count;
871 random_value = 860 random_value =
872 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); 861 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
873 if (random_value < (target_value * UINT32_MAX)) 862 if (random_value < (target_value * UINT32_MAX))
874 forward_count++; 863 forward_count++;
875 return forward_count; 864 return forward_count;
@@ -888,8 +877,8 @@ get_forward_count (uint32_t hop_count,
888 * the two hash codes increases 877 * the two hash codes increases
889 */ 878 */
890static unsigned int 879static unsigned int
891get_distance (const struct GNUNET_HashCode *target, 880get_distance(const struct GNUNET_HashCode *target,
892 const struct GNUNET_HashCode *have) 881 const struct GNUNET_HashCode *have)
893{ 882{
894 unsigned int bucket; 883 unsigned int bucket;
895 unsigned int msb; 884 unsigned int msb;
@@ -911,13 +900,13 @@ get_distance (const struct GNUNET_HashCode *target,
911 900
912 /* first, calculate the most significant 9 bits of our 901 /* first, calculate the most significant 9 bits of our
913 * result, aka the number of LSBs */ 902 * result, aka the number of LSBs */
914 bucket = GNUNET_CRYPTO_hash_matching_bits (target, 903 bucket = GNUNET_CRYPTO_hash_matching_bits(target,
915 have); 904 have);
916 /* bucket is now a value between 0 and 512 */ 905 /* bucket is now a value between 0 and 512 */
917 if (bucket == 512) 906 if (bucket == 512)
918 return 0; /* perfect match */ 907 return 0; /* perfect match */
919 if (bucket == 0) 908 if (bucket == 0)
920 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting 909 return (unsigned int)-1; /* LSB differs; use max (if we did the bit-shifting
921 * below, we'd end up with max+1 (overflow)) */ 910 * below, we'd end up with max+1 (overflow)) */
922 911
923 /* calculate the most significant bits of the final result */ 912 /* calculate the most significant bits of the final result */
@@ -927,14 +916,14 @@ get_distance (const struct GNUNET_HashCode *target,
927 * mismatching bit at 'bucket' */ 916 * mismatching bit at 'bucket' */
928 lsb = 0; 917 lsb = 0;
929 for (i = bucket + 1; 918 for (i = bucket + 1;
930 (i < sizeof (struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++) 919 (i < sizeof(struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
931 { 920 {
932 if (GNUNET_CRYPTO_hash_get_bit (target, i) != 921 if (GNUNET_CRYPTO_hash_get_bit(target, i) !=
933 GNUNET_CRYPTO_hash_get_bit (have, i)) 922 GNUNET_CRYPTO_hash_get_bit(have, i))
934 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10, 923 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
935 * last bit set will be 31 -- if 924 * last bit set will be 31 -- if
936 * i does not reach 512 first... */ 925 * i does not reach 512 first... */
937 } 926 }
938 return msb | lsb; 927 return msb | lsb;
939} 928}
940 929
@@ -950,40 +939,40 @@ get_distance (const struct GNUNET_HashCode *target,
950 * #GNUNET_NO otherwise. 939 * #GNUNET_NO otherwise.
951 */ 940 */
952int 941int
953GDS_am_closest_peer (const struct GNUNET_HashCode *key, 942GDS_am_closest_peer(const struct GNUNET_HashCode *key,
954 const struct GNUNET_CONTAINER_BloomFilter *bloom) 943 const struct GNUNET_CONTAINER_BloomFilter *bloom)
955{ 944{
956 int bits; 945 int bits;
957 int other_bits; 946 int other_bits;
958 int bucket_num; 947 int bucket_num;
959 struct PeerInfo *pos; 948 struct PeerInfo *pos;
960 949
961 if (0 == GNUNET_memcmp (&my_identity_hash, 950 if (0 == GNUNET_memcmp(&my_identity_hash,
962 key)) 951 key))
963 return GNUNET_YES; 952 return GNUNET_YES;
964 bucket_num = find_bucket (key); 953 bucket_num = find_bucket(key);
965 GNUNET_assert (bucket_num >= 0); 954 GNUNET_assert(bucket_num >= 0);
966 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, 955 bits = GNUNET_CRYPTO_hash_matching_bits(&my_identity_hash,
967 key); 956 key);
968 pos = k_buckets[bucket_num].head; 957 pos = k_buckets[bucket_num].head;
969 while (NULL != pos) 958 while (NULL != pos)
970 {
971 if ( (NULL != bloom) &&
972 (GNUNET_YES ==
973 GNUNET_CONTAINER_bloomfilter_test (bloom,
974 &pos->phash)) )
975 { 959 {
960 if ((NULL != bloom) &&
961 (GNUNET_YES ==
962 GNUNET_CONTAINER_bloomfilter_test(bloom,
963 &pos->phash)))
964 {
965 pos = pos->next;
966 continue; /* Skip already checked entries */
967 }
968 other_bits = GNUNET_CRYPTO_hash_matching_bits(&pos->phash,
969 key);
970 if (other_bits > bits)
971 return GNUNET_NO;
972 if (other_bits == bits) /* We match the same number of bits */
973 return GNUNET_YES;
976 pos = pos->next; 974 pos = pos->next;
977 continue; /* Skip already checked entries */
978 } 975 }
979 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->phash,
980 key);
981 if (other_bits > bits)
982 return GNUNET_NO;
983 if (other_bits == bits) /* We match the same number of bits */
984 return GNUNET_YES;
985 pos = pos->next;
986 }
987 /* No peers closer, we are the closest! */ 976 /* No peers closer, we are the closest! */
988 return GNUNET_YES; 977 return GNUNET_YES;
989} 978}
@@ -1007,9 +996,9 @@ GDS_am_closest_peer (const struct GNUNET_HashCode *key,
1007 * @return Peer to route to, or NULL on error 996 * @return Peer to route to, or NULL on error
1008 */ 997 */
1009static struct PeerInfo * 998static struct PeerInfo *
1010select_peer (const struct GNUNET_HashCode *key, 999select_peer(const struct GNUNET_HashCode *key,
1011 const struct GNUNET_CONTAINER_BloomFilter *bloom, 1000 const struct GNUNET_CONTAINER_BloomFilter *bloom,
1012 uint32_t hops) 1001 uint32_t hops)
1013{ 1002{
1014 unsigned int bc; 1003 unsigned int bc;
1015 unsigned int count; 1004 unsigned int count;
@@ -1019,126 +1008,126 @@ select_peer (const struct GNUNET_HashCode *key,
1019 unsigned int smallest_distance; 1008 unsigned int smallest_distance;
1020 struct PeerInfo *chosen; 1009 struct PeerInfo *chosen;
1021 1010
1022 if (hops >= GDS_NSE_get ()) 1011 if (hops >= GDS_NSE_get())
1023 {
1024 /* greedy selection (closest peer that is not in bloomfilter) */
1025 smallest_distance = UINT_MAX;
1026 chosen = NULL;
1027 for (bc = 0; bc <= closest_bucket; bc++)
1028 { 1012 {
1029 pos = k_buckets[bc].head; 1013 /* greedy selection (closest peer that is not in bloomfilter) */
1030 count = 0; 1014 smallest_distance = UINT_MAX;
1031 while ((pos != NULL) && (count < bucket_size)) 1015 chosen = NULL;
1032 { 1016 for (bc = 0; bc <= closest_bucket; bc++)
1033 if ( (NULL == bloom) ||
1034 (GNUNET_NO ==
1035 GNUNET_CONTAINER_bloomfilter_test (bloom,
1036 &pos->phash)))
1037 { 1017 {
1038 dist = get_distance (key, 1018 pos = k_buckets[bc].head;
1039 &pos->phash); 1019 count = 0;
1040 if (dist < smallest_distance) 1020 while ((pos != NULL) && (count < bucket_size))
1041 { 1021 {
1042 chosen = pos; 1022 if ((NULL == bloom) ||
1043 smallest_distance = dist; 1023 (GNUNET_NO ==
1044 } 1024 GNUNET_CONTAINER_bloomfilter_test(bloom,
1025 &pos->phash)))
1026 {
1027 dist = get_distance(key,
1028 &pos->phash);
1029 if (dist < smallest_distance)
1030 {
1031 chosen = pos;
1032 smallest_distance = dist;
1033 }
1034 }
1035 else
1036 {
1037 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1038 "Excluded peer `%s' due to BF match in greedy routing for %s\n",
1039 GNUNET_i2s(pos->id),
1040 GNUNET_h2s(key));
1041 GNUNET_STATISTICS_update(GDS_stats,
1042 gettext_noop("# Peers excluded from routing due to Bloomfilter"),
1043 1,
1044 GNUNET_NO);
1045 dist = get_distance(key,
1046 &pos->phash);
1047 if (dist < smallest_distance)
1048 {
1049 chosen = NULL;
1050 smallest_distance = dist;
1051 }
1052 }
1053 count++;
1054 pos = pos->next;
1055 }
1045 } 1056 }
1046 else 1057 if (NULL == chosen)
1047 { 1058 GNUNET_STATISTICS_update(GDS_stats,
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1059 gettext_noop("# Peer selection failed"),
1049 "Excluded peer `%s' due to BF match in greedy routing for %s\n", 1060 1,
1050 GNUNET_i2s (pos->id), 1061 GNUNET_NO);
1051 GNUNET_h2s (key)); 1062 else
1052 GNUNET_STATISTICS_update (GDS_stats, 1063 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1053 gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1064 "Selected peer `%s' in greedy routing for %s\n",
1054 1, 1065 GNUNET_i2s(chosen->id),
1055 GNUNET_NO); 1066 GNUNET_h2s(key));
1056 dist = get_distance (key, 1067 return chosen;
1057 &pos->phash);
1058 if (dist < smallest_distance)
1059 {
1060 chosen = NULL;
1061 smallest_distance = dist;
1062 }
1063 }
1064 count++;
1065 pos = pos->next;
1066 }
1067 } 1068 }
1068 if (NULL == chosen)
1069 GNUNET_STATISTICS_update (GDS_stats,
1070 gettext_noop ("# Peer selection failed"),
1071 1,
1072 GNUNET_NO);
1073 else
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "Selected peer `%s' in greedy routing for %s\n",
1076 GNUNET_i2s (chosen->id),
1077 GNUNET_h2s (key));
1078 return chosen;
1079 }
1080 1069
1081 /* select "random" peer */ 1070 /* select "random" peer */
1082 /* count number of peers that are available and not filtered */ 1071 /* count number of peers that are available and not filtered */
1083 count = 0; 1072 count = 0;
1084 for (bc = 0; bc <= closest_bucket; bc++) 1073 for (bc = 0; bc <= closest_bucket; bc++)
1085 {
1086 pos = k_buckets[bc].head;
1087 while ( (NULL != pos) && (count < bucket_size) )
1088 { 1074 {
1089 if ( (NULL != bloom) && 1075 pos = k_buckets[bc].head;
1090 (GNUNET_YES == 1076 while ((NULL != pos) && (count < bucket_size))
1091 GNUNET_CONTAINER_bloomfilter_test (bloom, 1077 {
1092 &pos->phash)) ) 1078 if ((NULL != bloom) &&
1093 { 1079 (GNUNET_YES ==
1094 GNUNET_STATISTICS_update (GDS_stats, 1080 GNUNET_CONTAINER_bloomfilter_test(bloom,
1095 gettext_noop 1081 &pos->phash)))
1096 ("# Peers excluded from routing due to Bloomfilter"), 1082 {
1097 1, GNUNET_NO); 1083 GNUNET_STATISTICS_update(GDS_stats,
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1084 gettext_noop
1099 "Excluded peer `%s' due to BF match in random routing for %s\n", 1085 ("# Peers excluded from routing due to Bloomfilter"),
1100 GNUNET_i2s (pos->id), 1086 1, GNUNET_NO);
1101 GNUNET_h2s (key)); 1087 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1102 pos = pos->next; 1088 "Excluded peer `%s' due to BF match in random routing for %s\n",
1103 continue; /* Ignore bloomfiltered peers */ 1089 GNUNET_i2s(pos->id),
1104 } 1090 GNUNET_h2s(key));
1105 count++; 1091 pos = pos->next;
1106 pos = pos->next; 1092 continue; /* Ignore bloomfiltered peers */
1093 }
1094 count++;
1095 pos = pos->next;
1096 }
1107 } 1097 }
1108 }
1109 if (0 == count) /* No peers to select from! */ 1098 if (0 == count) /* No peers to select from! */
1110 { 1099 {
1111 GNUNET_STATISTICS_update (GDS_stats, 1100 GNUNET_STATISTICS_update(GDS_stats,
1112 gettext_noop ("# Peer selection failed"), 1, 1101 gettext_noop("# Peer selection failed"), 1,
1113 GNUNET_NO); 1102 GNUNET_NO);
1114 return NULL; 1103 return NULL;
1115 } 1104 }
1116 /* Now actually choose a peer */ 1105 /* Now actually choose a peer */
1117 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1106 selected = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
1118 count); 1107 count);
1119 count = 0; 1108 count = 0;
1120 for (bc = 0; bc <= closest_bucket; bc++) 1109 for (bc = 0; bc <= closest_bucket; bc++)
1121 {
1122 for (pos = k_buckets[bc].head; ((pos != NULL) && (count < bucket_size)); pos = pos->next)
1123 { 1110 {
1124 if ((bloom != NULL) && 1111 for (pos = k_buckets[bc].head; ((pos != NULL) && (count < bucket_size)); pos = pos->next)
1125 (GNUNET_YES == 1112 {
1126 GNUNET_CONTAINER_bloomfilter_test (bloom, 1113 if ((bloom != NULL) &&
1127 &pos->phash))) 1114 (GNUNET_YES ==
1128 { 1115 GNUNET_CONTAINER_bloomfilter_test(bloom,
1129 continue; /* Ignore bloomfiltered peers */ 1116 &pos->phash)))
1130 } 1117 {
1131 if (0 == selected--) 1118 continue; /* Ignore bloomfiltered peers */
1132 { 1119 }
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1120 if (0 == selected--)
1134 "Selected peer `%s' in random routing for %s\n", 1121 {
1135 GNUNET_i2s (pos->id), 1122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1136 GNUNET_h2s (key)); 1123 "Selected peer `%s' in random routing for %s\n",
1137 return pos; 1124 GNUNET_i2s(pos->id),
1138 } 1125 GNUNET_h2s(key));
1126 return pos;
1127 }
1128 }
1139 } 1129 }
1140 } 1130 GNUNET_break(0);
1141 GNUNET_break (0);
1142 return NULL; 1131 return NULL;
1143} 1132}
1144 1133
@@ -1157,60 +1146,60 @@ select_peer (const struct GNUNET_HashCode *key,
1157 * @return number of peers returned in 'targets'. 1146 * @return number of peers returned in 'targets'.
1158 */ 1147 */
1159static unsigned int 1148static unsigned int
1160get_target_peers (const struct GNUNET_HashCode *key, 1149get_target_peers(const struct GNUNET_HashCode *key,
1161 struct GNUNET_CONTAINER_BloomFilter *bloom, 1150 struct GNUNET_CONTAINER_BloomFilter *bloom,
1162 uint32_t hop_count, 1151 uint32_t hop_count,
1163 uint32_t target_replication, 1152 uint32_t target_replication,
1164 struct PeerInfo ***targets) 1153 struct PeerInfo ***targets)
1165{ 1154{
1166 unsigned int ret; 1155 unsigned int ret;
1167 unsigned int off; 1156 unsigned int off;
1168 struct PeerInfo **rtargets; 1157 struct PeerInfo **rtargets;
1169 struct PeerInfo *nxt; 1158 struct PeerInfo *nxt;
1170 1159
1171 GNUNET_assert (NULL != bloom); 1160 GNUNET_assert(NULL != bloom);
1172 ret = get_forward_count (hop_count, 1161 ret = get_forward_count(hop_count,
1173 target_replication); 1162 target_replication);
1174 if (0 == ret) 1163 if (0 == ret)
1175 { 1164 {
1176 *targets = NULL; 1165 *targets = NULL;
1177 return 0; 1166 return 0;
1178 } 1167 }
1179 rtargets = GNUNET_new_array (ret, 1168 rtargets = GNUNET_new_array(ret,
1180 struct PeerInfo *); 1169 struct PeerInfo *);
1181 for (off = 0; off < ret; off++) 1170 for (off = 0; off < ret; off++)
1182 { 1171 {
1183 nxt = select_peer (key, 1172 nxt = select_peer(key,
1184 bloom, 1173 bloom,
1185 hop_count); 1174 hop_count);
1186 if (NULL == nxt) 1175 if (NULL == nxt)
1187 break; 1176 break;
1188 rtargets[off] = nxt; 1177 rtargets[off] = nxt;
1189 GNUNET_break (GNUNET_NO == 1178 GNUNET_break(GNUNET_NO ==
1190 GNUNET_CONTAINER_bloomfilter_test (bloom, 1179 GNUNET_CONTAINER_bloomfilter_test(bloom,
1191 &nxt->phash)); 1180 &nxt->phash));
1192 GNUNET_CONTAINER_bloomfilter_add (bloom, 1181 GNUNET_CONTAINER_bloomfilter_add(bloom,
1193 &nxt->phash); 1182 &nxt->phash);
1194 } 1183 }
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1196 "Selected %u/%u peers at hop %u for %s (target was %u)\n", 1185 "Selected %u/%u peers at hop %u for %s (target was %u)\n",
1197 off, 1186 off,
1198 GNUNET_CONTAINER_multipeermap_size (all_connected_peers), 1187 GNUNET_CONTAINER_multipeermap_size(all_connected_peers),
1199 (unsigned int) hop_count, 1188 (unsigned int)hop_count,
1200 GNUNET_h2s (key), 1189 GNUNET_h2s(key),
1201 ret); 1190 ret);
1202 if (0 == off) 1191 if (0 == off)
1203 { 1192 {
1204 GNUNET_free (rtargets); 1193 GNUNET_free(rtargets);
1205 *targets = NULL; 1194 *targets = NULL;
1206 return 0; 1195 return 0;
1207 } 1196 }
1208 *targets = rtargets; 1197 *targets = rtargets;
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1210 "Forwarding query `%s' to %u peers (goal was %u peers)\n", 1199 "Forwarding query `%s' to %u peers (goal was %u peers)\n",
1211 GNUNET_h2s (key), 1200 GNUNET_h2s(key),
1212 off, 1201 off,
1213 ret); 1202 ret);
1214 return off; 1203 return off;
1215} 1204}
1216 1205
@@ -1236,17 +1225,17 @@ get_target_peers (const struct GNUNET_HashCode *key,
1236 * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not 1225 * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not
1237 */ 1226 */
1238int 1227int
1239GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type, 1228GDS_NEIGHBOURS_handle_put(enum GNUNET_BLOCK_Type type,
1240 enum GNUNET_DHT_RouteOption options, 1229 enum GNUNET_DHT_RouteOption options,
1241 uint32_t desired_replication_level, 1230 uint32_t desired_replication_level,
1242 struct GNUNET_TIME_Absolute expiration_time, 1231 struct GNUNET_TIME_Absolute expiration_time,
1243 uint32_t hop_count, 1232 uint32_t hop_count,
1244 struct GNUNET_CONTAINER_BloomFilter *bf, 1233 struct GNUNET_CONTAINER_BloomFilter *bf,
1245 const struct GNUNET_HashCode *key, 1234 const struct GNUNET_HashCode *key,
1246 unsigned int put_path_length, 1235 unsigned int put_path_length,
1247 struct GNUNET_PeerIdentity *put_path, 1236 struct GNUNET_PeerIdentity *put_path,
1248 const void *data, 1237 const void *data,
1249 size_t data_size) 1238 size_t data_size)
1250{ 1239{
1251 unsigned int target_count; 1240 unsigned int target_count;
1252 unsigned int i; 1241 unsigned int i;
@@ -1258,97 +1247,97 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1258 struct GNUNET_PeerIdentity *pp; 1247 struct GNUNET_PeerIdentity *pp;
1259 unsigned int skip_count; 1248 unsigned int skip_count;
1260 1249
1261 GNUNET_assert (NULL != bf); 1250 GNUNET_assert(NULL != bf);
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1263 "Adding myself (%s) to PUT bloomfilter for %s\n", 1252 "Adding myself (%s) to PUT bloomfilter for %s\n",
1264 GNUNET_i2s (&my_identity), 1253 GNUNET_i2s(&my_identity),
1265 GNUNET_h2s (key)); 1254 GNUNET_h2s(key));
1266 GNUNET_CONTAINER_bloomfilter_add (bf, 1255 GNUNET_CONTAINER_bloomfilter_add(bf,
1267 &my_identity_hash); 1256 &my_identity_hash);
1268 GNUNET_STATISTICS_update (GDS_stats, 1257 GNUNET_STATISTICS_update(GDS_stats,
1269 gettext_noop ("# PUT requests routed"), 1258 gettext_noop("# PUT requests routed"),
1270 1, 1259 1,
1271 GNUNET_NO); 1260 GNUNET_NO);
1272 target_count 1261 target_count
1273 = get_target_peers (key, 1262 = get_target_peers(key,
1274 bf, 1263 bf,
1275 hop_count, 1264 hop_count,
1276 desired_replication_level, 1265 desired_replication_level,
1277 &targets); 1266 &targets);
1278 if (0 == target_count) 1267 if (0 == target_count)
1279 { 1268 {
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1281 "Routing PUT for %s terminates after %u hops at %s\n", 1270 "Routing PUT for %s terminates after %u hops at %s\n",
1282 GNUNET_h2s (key), 1271 GNUNET_h2s(key),
1283 (unsigned int) hop_count, 1272 (unsigned int)hop_count,
1284 GNUNET_i2s (&my_identity)); 1273 GNUNET_i2s(&my_identity));
1285 return GNUNET_NO; 1274 return GNUNET_NO;
1286 } 1275 }
1287 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size; 1276 msize = put_path_length * sizeof(struct GNUNET_PeerIdentity) + data_size;
1288 if (msize + sizeof (struct PeerPutMessage) 1277 if (msize + sizeof(struct PeerPutMessage)
1289 >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) 1278 >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1290 { 1279 {
1291 put_path_length = 0; 1280 put_path_length = 0;
1292 msize = data_size; 1281 msize = data_size;
1293 } 1282 }
1294 if (msize + sizeof (struct PeerPutMessage) 1283 if (msize + sizeof(struct PeerPutMessage)
1295 >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) 1284 >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1296 { 1285 {
1297 GNUNET_break (0); 1286 GNUNET_break(0);
1298 GNUNET_free (targets); 1287 GNUNET_free(targets);
1299 return GNUNET_NO; 1288 return GNUNET_NO;
1300 } 1289 }
1301 GNUNET_STATISTICS_update (GDS_stats, 1290 GNUNET_STATISTICS_update(GDS_stats,
1302 gettext_noop ("# PUT messages queued for transmission"), 1291 gettext_noop("# PUT messages queued for transmission"),
1303 target_count, 1292 target_count,
1304 GNUNET_NO); 1293 GNUNET_NO);
1305 skip_count = 0; 1294 skip_count = 0;
1306 for (i = 0; i < target_count; i++) 1295 for (i = 0; i < target_count; i++)
1307 {
1308 target = targets[i];
1309 if (GNUNET_MQ_get_length (target->mq) >= MAXIMUM_PENDING_PER_PEER)
1310 { 1296 {
1311 /* skip */ 1297 target = targets[i];
1312 GNUNET_STATISTICS_update (GDS_stats, 1298 if (GNUNET_MQ_get_length(target->mq) >= MAXIMUM_PENDING_PER_PEER)
1313 gettext_noop ("# P2P messages dropped due to full queue"), 1299 {
1314 1, 1300 /* skip */
1315 GNUNET_NO); 1301 GNUNET_STATISTICS_update(GDS_stats,
1316 skip_count++; 1302 gettext_noop("# P2P messages dropped due to full queue"),
1317 continue; 1303 1,
1318 } 1304 GNUNET_NO);
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1305 skip_count++;
1320 "Routing PUT for %s after %u hops to %s\n", 1306 continue;
1321 GNUNET_h2s (key), 1307 }
1322 (unsigned int) hop_count, 1308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1323 GNUNET_i2s (target->id)); 1309 "Routing PUT for %s after %u hops to %s\n",
1324 env = GNUNET_MQ_msg_extra (ppm, 1310 GNUNET_h2s(key),
1325 msize, 1311 (unsigned int)hop_count,
1326 GNUNET_MESSAGE_TYPE_DHT_P2P_PUT); 1312 GNUNET_i2s(target->id));
1327 ppm->options = htonl (options); 1313 env = GNUNET_MQ_msg_extra(ppm,
1328 ppm->type = htonl (type); 1314 msize,
1329 ppm->hop_count = htonl (hop_count + 1); 1315 GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1330 ppm->desired_replication_level = htonl (desired_replication_level); 1316 ppm->options = htonl(options);
1331 ppm->put_path_length = htonl (put_path_length); 1317 ppm->type = htonl(type);
1332 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time); 1318 ppm->hop_count = htonl(hop_count + 1);
1333 GNUNET_break (GNUNET_YES == 1319 ppm->desired_replication_level = htonl(desired_replication_level);
1334 GNUNET_CONTAINER_bloomfilter_test (bf, 1320 ppm->put_path_length = htonl(put_path_length);
1321 ppm->expiration_time = GNUNET_TIME_absolute_hton(expiration_time);
1322 GNUNET_break(GNUNET_YES ==
1323 GNUNET_CONTAINER_bloomfilter_test(bf,
1335 &target->phash)); 1324 &target->phash));
1336 GNUNET_assert (GNUNET_OK == 1325 GNUNET_assert(GNUNET_OK ==
1337 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, 1326 GNUNET_CONTAINER_bloomfilter_get_raw_data(bf,
1338 ppm->bloomfilter, 1327 ppm->bloomfilter,
1339 DHT_BLOOM_SIZE)); 1328 DHT_BLOOM_SIZE));
1340 ppm->key = *key; 1329 ppm->key = *key;
1341 pp = (struct GNUNET_PeerIdentity *) &ppm[1]; 1330 pp = (struct GNUNET_PeerIdentity *)&ppm[1];
1342 GNUNET_memcpy (pp, 1331 GNUNET_memcpy(pp,
1343 put_path, 1332 put_path,
1344 sizeof (struct GNUNET_PeerIdentity) * put_path_length); 1333 sizeof(struct GNUNET_PeerIdentity) * put_path_length);
1345 GNUNET_memcpy (&pp[put_path_length], 1334 GNUNET_memcpy(&pp[put_path_length],
1346 data, 1335 data,
1347 data_size); 1336 data_size);
1348 GNUNET_MQ_send (target->mq, 1337 GNUNET_MQ_send(target->mq,
1349 env); 1338 env);
1350 } 1339 }
1351 GNUNET_free (targets); 1340 GNUNET_free(targets);
1352 return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO; 1341 return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO;
1353} 1342}
1354 1343
@@ -1371,15 +1360,15 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1371 * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not 1360 * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not
1372 */ 1361 */
1373int 1362int
1374GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, 1363GDS_NEIGHBOURS_handle_get(enum GNUNET_BLOCK_Type type,
1375 enum GNUNET_DHT_RouteOption options, 1364 enum GNUNET_DHT_RouteOption options,
1376 uint32_t desired_replication_level, 1365 uint32_t desired_replication_level,
1377 uint32_t hop_count, 1366 uint32_t hop_count,
1378 const struct GNUNET_HashCode *key, 1367 const struct GNUNET_HashCode *key,
1379 const void *xquery, 1368 const void *xquery,
1380 size_t xquery_size, 1369 size_t xquery_size,
1381 struct GNUNET_BLOCK_Group *bg, 1370 struct GNUNET_BLOCK_Group *bg,
1382 struct GNUNET_CONTAINER_BloomFilter *peer_bf) 1371 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1383{ 1372{
1384 unsigned int target_count; 1373 unsigned int target_count;
1385 struct PeerInfo **targets; 1374 struct PeerInfo **targets;
@@ -1393,102 +1382,102 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1393 unsigned int skip_count; 1382 unsigned int skip_count;
1394 uint32_t bf_nonce; 1383 uint32_t bf_nonce;
1395 1384
1396 GNUNET_assert (NULL != peer_bf); 1385 GNUNET_assert(NULL != peer_bf);
1397 GNUNET_STATISTICS_update (GDS_stats, 1386 GNUNET_STATISTICS_update(GDS_stats,
1398 gettext_noop ("# GET requests routed"), 1387 gettext_noop("# GET requests routed"),
1399 1, 1388 1,
1400 GNUNET_NO); 1389 GNUNET_NO);
1401 target_count = get_target_peers (key, 1390 target_count = get_target_peers(key,
1402 peer_bf, 1391 peer_bf,
1403 hop_count, 1392 hop_count,
1404 desired_replication_level, 1393 desired_replication_level,
1405 &targets); 1394 &targets);
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1395 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1407 "Adding myself (%s) to GET bloomfilter for %s\n", 1396 "Adding myself (%s) to GET bloomfilter for %s\n",
1408 GNUNET_i2s (&my_identity), 1397 GNUNET_i2s(&my_identity),
1409 GNUNET_h2s (key)); 1398 GNUNET_h2s(key));
1410 GNUNET_CONTAINER_bloomfilter_add (peer_bf, 1399 GNUNET_CONTAINER_bloomfilter_add(peer_bf,
1411 &my_identity_hash); 1400 &my_identity_hash);
1412 if (0 == target_count) 1401 if (0 == target_count)
1413 { 1402 {
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1403 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1415 "Routing GET for %s terminates after %u hops at %s\n", 1404 "Routing GET for %s terminates after %u hops at %s\n",
1416 GNUNET_h2s (key), 1405 GNUNET_h2s(key),
1417 (unsigned int) hop_count, 1406 (unsigned int)hop_count,
1418 GNUNET_i2s (&my_identity)); 1407 GNUNET_i2s(&my_identity));
1419 return GNUNET_NO; 1408 return GNUNET_NO;
1420 } 1409 }
1421 if (GNUNET_OK != 1410 if (GNUNET_OK !=
1422 GNUNET_BLOCK_group_serialize (bg, 1411 GNUNET_BLOCK_group_serialize(bg,
1423 &bf_nonce, 1412 &bf_nonce,
1424 &reply_bf, 1413 &reply_bf,
1425 &reply_bf_size)) 1414 &reply_bf_size))
1426 { 1415 {
1427 reply_bf = NULL; 1416 reply_bf = NULL;
1428 reply_bf_size = 0; 1417 reply_bf_size = 0;
1429 bf_nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1418 bf_nonce = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
1430 UINT32_MAX); 1419 UINT32_MAX);
1431 } 1420 }
1432 msize = xquery_size + reply_bf_size; 1421 msize = xquery_size + reply_bf_size;
1433 if (msize + sizeof (struct PeerGetMessage) >= GNUNET_MAX_MESSAGE_SIZE) 1422 if (msize + sizeof(struct PeerGetMessage) >= GNUNET_MAX_MESSAGE_SIZE)
1434 { 1423 {
1435 GNUNET_break (0); 1424 GNUNET_break(0);
1436 GNUNET_free_non_null (reply_bf); 1425 GNUNET_free_non_null(reply_bf);
1437 GNUNET_free (targets); 1426 GNUNET_free(targets);
1438 return GNUNET_NO; 1427 return GNUNET_NO;
1439 } 1428 }
1440 GNUNET_STATISTICS_update (GDS_stats, 1429 GNUNET_STATISTICS_update(GDS_stats,
1441 gettext_noop ("# GET messages queued for transmission"), 1430 gettext_noop("# GET messages queued for transmission"),
1442 target_count, 1431 target_count,
1443 GNUNET_NO); 1432 GNUNET_NO);
1444 /* forward request */ 1433 /* forward request */
1445 skip_count = 0; 1434 skip_count = 0;
1446 for (unsigned int i = 0; i < target_count; i++) 1435 for (unsigned int i = 0; i < target_count; i++)
1447 {
1448 target = targets[i];
1449 if (GNUNET_MQ_get_length (target->mq) >= MAXIMUM_PENDING_PER_PEER)
1450 { 1436 {
1451 /* skip */ 1437 target = targets[i];
1452 GNUNET_STATISTICS_update (GDS_stats, 1438 if (GNUNET_MQ_get_length(target->mq) >= MAXIMUM_PENDING_PER_PEER)
1453 gettext_noop ("# P2P messages dropped due to full queue"), 1439 {
1454 1, GNUNET_NO); 1440 /* skip */
1455 skip_count++; 1441 GNUNET_STATISTICS_update(GDS_stats,
1456 continue; 1442 gettext_noop("# P2P messages dropped due to full queue"),
1457 } 1443 1, GNUNET_NO);
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1444 skip_count++;
1459 "Routing GET for %s after %u hops to %s\n", 1445 continue;
1460 GNUNET_h2s (key), 1446 }
1461 (unsigned int) hop_count, 1447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1462 GNUNET_i2s (target->id)); 1448 "Routing GET for %s after %u hops to %s\n",
1463 env = GNUNET_MQ_msg_extra (pgm, 1449 GNUNET_h2s(key),
1464 msize, 1450 (unsigned int)hop_count,
1465 GNUNET_MESSAGE_TYPE_DHT_P2P_GET); 1451 GNUNET_i2s(target->id));
1466 pgm->options = htonl (options); 1452 env = GNUNET_MQ_msg_extra(pgm,
1467 pgm->type = htonl (type); 1453 msize,
1468 pgm->hop_count = htonl (hop_count + 1); 1454 GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1469 pgm->desired_replication_level = htonl (desired_replication_level); 1455 pgm->options = htonl(options);
1470 pgm->xquery_size = htonl (xquery_size); 1456 pgm->type = htonl(type);
1471 pgm->bf_mutator = bf_nonce; 1457 pgm->hop_count = htonl(hop_count + 1);
1472 GNUNET_break (GNUNET_YES == 1458 pgm->desired_replication_level = htonl(desired_replication_level);
1473 GNUNET_CONTAINER_bloomfilter_test (peer_bf, 1459 pgm->xquery_size = htonl(xquery_size);
1460 pgm->bf_mutator = bf_nonce;
1461 GNUNET_break(GNUNET_YES ==
1462 GNUNET_CONTAINER_bloomfilter_test(peer_bf,
1474 &target->phash)); 1463 &target->phash));
1475 GNUNET_assert (GNUNET_OK == 1464 GNUNET_assert(GNUNET_OK ==
1476 GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf, 1465 GNUNET_CONTAINER_bloomfilter_get_raw_data(peer_bf,
1477 pgm->bloomfilter, 1466 pgm->bloomfilter,
1478 DHT_BLOOM_SIZE)); 1467 DHT_BLOOM_SIZE));
1479 pgm->key = *key; 1468 pgm->key = *key;
1480 xq = (char *) &pgm[1]; 1469 xq = (char *)&pgm[1];
1481 GNUNET_memcpy (xq, 1470 GNUNET_memcpy(xq,
1482 xquery, 1471 xquery,
1483 xquery_size); 1472 xquery_size);
1484 GNUNET_memcpy (&xq[xquery_size], 1473 GNUNET_memcpy(&xq[xquery_size],
1485 reply_bf, 1474 reply_bf,
1486 reply_bf_size); 1475 reply_bf_size);
1487 GNUNET_MQ_send (target->mq, 1476 GNUNET_MQ_send(target->mq,
1488 env); 1477 env);
1489 } 1478 }
1490 GNUNET_free (targets); 1479 GNUNET_free(targets);
1491 GNUNET_free_non_null (reply_bf); 1480 GNUNET_free_non_null(reply_bf);
1492 return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO; 1481 return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO;
1493} 1482}
1494 1483
@@ -1510,16 +1499,16 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1510 * @param data_size number of bytes in @a data 1499 * @param data_size number of bytes in @a data
1511 */ 1500 */
1512void 1501void
1513GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, 1502GDS_NEIGHBOURS_handle_reply(const struct GNUNET_PeerIdentity *target,
1514 enum GNUNET_BLOCK_Type type, 1503 enum GNUNET_BLOCK_Type type,
1515 struct GNUNET_TIME_Absolute expiration_time, 1504 struct GNUNET_TIME_Absolute expiration_time,
1516 const struct GNUNET_HashCode *key, 1505 const struct GNUNET_HashCode *key,
1517 unsigned int put_path_length, 1506 unsigned int put_path_length,
1518 const struct GNUNET_PeerIdentity *put_path, 1507 const struct GNUNET_PeerIdentity *put_path,
1519 unsigned int get_path_length, 1508 unsigned int get_path_length,
1520 const struct GNUNET_PeerIdentity *get_path, 1509 const struct GNUNET_PeerIdentity *get_path,
1521 const void *data, 1510 const void *data,
1522 size_t data_size) 1511 size_t data_size)
1523{ 1512{
1524 struct PeerInfo *pi; 1513 struct PeerInfo *pi;
1525 struct GNUNET_MQ_Envelope *env; 1514 struct GNUNET_MQ_Envelope *env;
@@ -1528,68 +1517,68 @@ GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1528 struct GNUNET_PeerIdentity *paths; 1517 struct GNUNET_PeerIdentity *paths;
1529 1518
1530 msize = data_size + (get_path_length + put_path_length) * 1519 msize = data_size + (get_path_length + put_path_length) *
1531 sizeof (struct GNUNET_PeerIdentity); 1520 sizeof(struct GNUNET_PeerIdentity);
1532 if ((msize + sizeof (struct PeerResultMessage) >= GNUNET_MAX_MESSAGE_SIZE) || 1521 if ((msize + sizeof(struct PeerResultMessage) >= GNUNET_MAX_MESSAGE_SIZE) ||
1533 (get_path_length > 1522 (get_path_length >
1534 GNUNET_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || 1523 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_PeerIdentity)) ||
1535 (put_path_length > 1524 (put_path_length >
1536 GNUNET_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || 1525 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_PeerIdentity)) ||
1537 (data_size > GNUNET_MAX_MESSAGE_SIZE)) 1526 (data_size > GNUNET_MAX_MESSAGE_SIZE))
1538 { 1527 {
1539 GNUNET_break (0); 1528 GNUNET_break(0);
1540 return; 1529 return;
1541 } 1530 }
1542 pi = GNUNET_CONTAINER_multipeermap_get (all_connected_peers, 1531 pi = GNUNET_CONTAINER_multipeermap_get(all_connected_peers,
1543 target); 1532 target);
1544 if (NULL == pi) 1533 if (NULL == pi)
1545 { 1534 {
1546 /* peer disconnected in the meantime, drop reply */ 1535 /* peer disconnected in the meantime, drop reply */
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1536 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1548 "No matching peer for reply for key %s\n", 1537 "No matching peer for reply for key %s\n",
1549 GNUNET_h2s (key)); 1538 GNUNET_h2s(key));
1550 return; 1539 return;
1551 } 1540 }
1552 if (GNUNET_MQ_get_length (pi->mq) >= MAXIMUM_PENDING_PER_PEER) 1541 if (GNUNET_MQ_get_length(pi->mq) >= MAXIMUM_PENDING_PER_PEER)
1553 { 1542 {
1554 /* skip */ 1543 /* skip */
1555 GNUNET_STATISTICS_update (GDS_stats, 1544 GNUNET_STATISTICS_update(GDS_stats,
1556 gettext_noop ("# P2P messages dropped due to full queue"), 1545 gettext_noop("# P2P messages dropped due to full queue"),
1557 1, 1546 1,
1558 GNUNET_NO); 1547 GNUNET_NO);
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1548 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1560 "Peer queue full, ignoring reply for key %s\n", 1549 "Peer queue full, ignoring reply for key %s\n",
1561 GNUNET_h2s (key)); 1550 GNUNET_h2s(key));
1562 return; 1551 return;
1563 } 1552 }
1564 1553
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1554 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1566 "Forwarding reply for key %s to peer %s\n", 1555 "Forwarding reply for key %s to peer %s\n",
1567 GNUNET_h2s (key), 1556 GNUNET_h2s(key),
1568 GNUNET_i2s (target)); 1557 GNUNET_i2s(target));
1569 GNUNET_STATISTICS_update (GDS_stats, 1558 GNUNET_STATISTICS_update(GDS_stats,
1570 gettext_noop 1559 gettext_noop
1571 ("# RESULT messages queued for transmission"), 1, 1560 ("# RESULT messages queued for transmission"), 1,
1572 GNUNET_NO); 1561 GNUNET_NO);
1573 env = GNUNET_MQ_msg_extra (prm, 1562 env = GNUNET_MQ_msg_extra(prm,
1574 msize, 1563 msize,
1575 GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT); 1564 GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1576 prm->type = htonl (type); 1565 prm->type = htonl(type);
1577 prm->put_path_length = htonl (put_path_length); 1566 prm->put_path_length = htonl(put_path_length);
1578 prm->get_path_length = htonl (get_path_length); 1567 prm->get_path_length = htonl(get_path_length);
1579 prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time); 1568 prm->expiration_time = GNUNET_TIME_absolute_hton(expiration_time);
1580 prm->key = *key; 1569 prm->key = *key;
1581 paths = (struct GNUNET_PeerIdentity *) &prm[1]; 1570 paths = (struct GNUNET_PeerIdentity *)&prm[1];
1582 GNUNET_memcpy (paths, 1571 GNUNET_memcpy(paths,
1583 put_path, 1572 put_path,
1584 put_path_length * sizeof (struct GNUNET_PeerIdentity)); 1573 put_path_length * sizeof(struct GNUNET_PeerIdentity));
1585 GNUNET_memcpy (&paths[put_path_length], 1574 GNUNET_memcpy(&paths[put_path_length],
1586 get_path, 1575 get_path,
1587 get_path_length * sizeof (struct GNUNET_PeerIdentity)); 1576 get_path_length * sizeof(struct GNUNET_PeerIdentity));
1588 GNUNET_memcpy (&paths[put_path_length + get_path_length], 1577 GNUNET_memcpy(&paths[put_path_length + get_path_length],
1589 data, 1578 data,
1590 data_size); 1579 data_size);
1591 GNUNET_MQ_send (pi->mq, 1580 GNUNET_MQ_send(pi->mq,
1592 env); 1581 env);
1593} 1582}
1594 1583
1595 1584
@@ -1600,18 +1589,18 @@ GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1600 * @param identity the public identity of this peer 1589 * @param identity the public identity of this peer
1601 */ 1590 */
1602static void 1591static void
1603core_init (void *cls, 1592core_init(void *cls,
1604 const struct GNUNET_PeerIdentity *identity) 1593 const struct GNUNET_PeerIdentity *identity)
1605{ 1594{
1606 (void) cls; 1595 (void)cls;
1607 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1596 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1608 "CORE called, I am %s\n", 1597 "CORE called, I am %s\n",
1609 GNUNET_i2s (identity)); 1598 GNUNET_i2s(identity));
1610 my_identity = *identity; 1599 my_identity = *identity;
1611 GNUNET_CRYPTO_hash (identity, 1600 GNUNET_CRYPTO_hash(identity,
1612 sizeof (struct GNUNET_PeerIdentity), 1601 sizeof(struct GNUNET_PeerIdentity),
1613 &my_identity_hash); 1602 &my_identity_hash);
1614 GNUNET_SERVICE_resume (GDS_service); 1603 GNUNET_SERVICE_resume(GDS_service);
1615} 1604}
1616 1605
1617 1606
@@ -1623,24 +1612,24 @@ core_init (void *cls,
1623 * @return #GNUNET_OK if the message is valid 1612 * @return #GNUNET_OK if the message is valid
1624 */ 1613 */
1625static int 1614static int
1626check_dht_p2p_put (void *cls, 1615check_dht_p2p_put(void *cls,
1627 const struct PeerPutMessage *put) 1616 const struct PeerPutMessage *put)
1628{ 1617{
1629 uint32_t putlen; 1618 uint32_t putlen;
1630 uint16_t msize; 1619 uint16_t msize;
1631 1620
1632 (void) cls; 1621 (void)cls;
1633 msize = ntohs (put->header.size); 1622 msize = ntohs(put->header.size);
1634 putlen = ntohl (put->put_path_length); 1623 putlen = ntohl(put->put_path_length);
1635 if ((msize < 1624 if ((msize <
1636 sizeof (struct PeerPutMessage) + 1625 sizeof(struct PeerPutMessage) +
1637 putlen * sizeof (struct GNUNET_PeerIdentity)) || 1626 putlen * sizeof(struct GNUNET_PeerIdentity)) ||
1638 (putlen > 1627 (putlen >
1639 GNUNET_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))) 1628 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_PeerIdentity)))
1640 { 1629 {
1641 GNUNET_break_op (0); 1630 GNUNET_break_op(0);
1642 return GNUNET_SYSERR; 1631 return GNUNET_SYSERR;
1643 } 1632 }
1644 return GNUNET_OK; 1633 return GNUNET_OK;
1645} 1634}
1646 1635
@@ -1652,8 +1641,8 @@ check_dht_p2p_put (void *cls,
1652 * @param message message 1641 * @param message message
1653 */ 1642 */
1654static void 1643static void
1655handle_dht_p2p_put (void *cls, 1644handle_dht_p2p_put(void *cls,
1656 const struct PeerPutMessage *put) 1645 const struct PeerPutMessage *put)
1657{ 1646{
1658 struct PeerInfo *peer = cls; 1647 struct PeerInfo *peer = cls;
1659 const struct GNUNET_PeerIdentity *put_path; 1648 const struct GNUNET_PeerIdentity *put_path;
@@ -1667,196 +1656,198 @@ handle_dht_p2p_put (void *cls,
1667 int forwarded; 1656 int forwarded;
1668 struct GNUNET_TIME_Absolute exp_time; 1657 struct GNUNET_TIME_Absolute exp_time;
1669 1658
1670 exp_time = GNUNET_TIME_absolute_ntoh (put->expiration_time); 1659 exp_time = GNUNET_TIME_absolute_ntoh(put->expiration_time);
1671 if (0 == GNUNET_TIME_absolute_get_remaining (exp_time).rel_value_us) 1660 if (0 == GNUNET_TIME_absolute_get_remaining(exp_time).rel_value_us)
1672 { 1661 {
1673 GNUNET_STATISTICS_update (GDS_stats, 1662 GNUNET_STATISTICS_update(GDS_stats,
1674 gettext_noop ("# Expired PUTs discarded"), 1663 gettext_noop("# Expired PUTs discarded"),
1675 1, 1664 1,
1676 GNUNET_NO); 1665 GNUNET_NO);
1677 return; 1666 return;
1678 } 1667 }
1679 msize = ntohs (put->header.size); 1668 msize = ntohs(put->header.size);
1680 putlen = ntohl (put->put_path_length); 1669 putlen = ntohl(put->put_path_length);
1681 GNUNET_STATISTICS_update (GDS_stats, 1670 GNUNET_STATISTICS_update(GDS_stats,
1682 gettext_noop ("# P2P PUT requests received"), 1671 gettext_noop("# P2P PUT requests received"),
1683 1, 1672 1,
1684 GNUNET_NO); 1673 GNUNET_NO);
1685 GNUNET_STATISTICS_update (GDS_stats, 1674 GNUNET_STATISTICS_update(GDS_stats,
1686 gettext_noop ("# P2P PUT bytes received"), 1675 gettext_noop("# P2P PUT bytes received"),
1687 msize, 1676 msize,
1688 GNUNET_NO); 1677 GNUNET_NO);
1689 put_path = (const struct GNUNET_PeerIdentity *) &put[1]; 1678 put_path = (const struct GNUNET_PeerIdentity *)&put[1];
1690 payload = &put_path[putlen]; 1679 payload = &put_path[putlen];
1691 options = ntohl (put->options); 1680 options = ntohl(put->options);
1692 payload_size = msize - (sizeof (struct PeerPutMessage) + 1681 payload_size = msize - (sizeof(struct PeerPutMessage) +
1693 putlen * sizeof (struct GNUNET_PeerIdentity)); 1682 putlen * sizeof(struct GNUNET_PeerIdentity));
1694 1683
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1684 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1696 "PUT for `%s' from %s\n", 1685 "PUT for `%s' from %s\n",
1697 GNUNET_h2s (&put->key), 1686 GNUNET_h2s(&put->key),
1698 GNUNET_i2s (peer->id)); 1687 GNUNET_i2s(peer->id));
1699 if (GNUNET_YES == log_route_details_stderr) 1688 if (GNUNET_YES == log_route_details_stderr)
1700 { 1689 {
1701 char *tmp; 1690 char *tmp;
1702 char *pp; 1691 char *pp;
1703 1692
1704 pp = GNUNET_STRINGS_pp2s (put_path, 1693 pp = GNUNET_STRINGS_pp2s(put_path,
1705 putlen); 1694 putlen);
1706 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity)); 1695 tmp = GNUNET_strdup(GNUNET_i2s(&my_identity));
1707 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, 1696 LOG_TRAFFIC(GNUNET_ERROR_TYPE_DEBUG,
1708 "R5N PUT %s: %s->%s (%u, %u=>%u, PP: %s)\n", 1697 "R5N PUT %s: %s->%s (%u, %u=>%u, PP: %s)\n",
1709 GNUNET_h2s (&put->key), 1698 GNUNET_h2s(&put->key),
1710 GNUNET_i2s (peer->id), 1699 GNUNET_i2s(peer->id),
1711 tmp, 1700 tmp,
1712 ntohl(put->hop_count), 1701 ntohl(put->hop_count),
1713 GNUNET_CRYPTO_hash_matching_bits (&peer->phash, 1702 GNUNET_CRYPTO_hash_matching_bits(&peer->phash,
1714 &put->key), 1703 &put->key),
1715 GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, 1704 GNUNET_CRYPTO_hash_matching_bits(&my_identity_hash,
1716 &put->key), 1705 &put->key),
1717 pp); 1706 pp);
1718 GNUNET_free (pp); 1707 GNUNET_free(pp);
1719 GNUNET_free (tmp); 1708 GNUNET_free(tmp);
1720 }
1721 switch (GNUNET_BLOCK_get_key
1722 (GDS_block_context,
1723 ntohl (put->type),
1724 payload,
1725 payload_size,
1726 &test_key))
1727 {
1728 case GNUNET_YES:
1729 if (0 != memcmp (&test_key,
1730 &put->key,
1731 sizeof (struct GNUNET_HashCode)))
1732 {
1733 char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
1734
1735 GNUNET_break_op (0);
1736 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1737 "PUT with key `%s' for block with key %s\n",
1738 put_s,
1739 GNUNET_h2s_full (&test_key));
1740 GNUNET_free (put_s);
1741 return;
1742 } 1709 }
1743 break; 1710 switch (GNUNET_BLOCK_get_key
1744 case GNUNET_NO: 1711 (GDS_block_context,
1745 GNUNET_break_op (0); 1712 ntohl(put->type),
1746 return; 1713 payload,
1747 case GNUNET_SYSERR: 1714 payload_size,
1748 /* cannot verify, good luck */ 1715 &test_key))
1749 break;
1750 }
1751 if (ntohl (put->type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
1752 {
1753 switch (GNUNET_BLOCK_evaluate (GDS_block_context,
1754 ntohl (put->type),
1755 NULL, /* query group */
1756 GNUNET_BLOCK_EO_NONE,
1757 NULL, /* query */
1758 NULL, 0, /* xquery */
1759 payload,
1760 payload_size))
1761 { 1716 {
1762 case GNUNET_BLOCK_EVALUATION_OK_MORE: 1717 case GNUNET_YES:
1763 case GNUNET_BLOCK_EVALUATION_OK_LAST: 1718 if (0 != memcmp(&test_key,
1719 &put->key,
1720 sizeof(struct GNUNET_HashCode)))
1721 {
1722 char *put_s = GNUNET_strdup(GNUNET_h2s_full(&put->key));
1723
1724 GNUNET_break_op(0);
1725 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1726 "PUT with key `%s' for block with key %s\n",
1727 put_s,
1728 GNUNET_h2s_full(&test_key));
1729 GNUNET_free(put_s);
1730 return;
1731 }
1764 break; 1732 break;
1765 1733
1766 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE: 1734 case GNUNET_NO:
1767 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID: 1735 GNUNET_break_op(0);
1768 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
1769 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
1770 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
1771 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
1772 default:
1773 GNUNET_break_op (0);
1774 return; 1736 return;
1737
1738 case GNUNET_SYSERR:
1739 /* cannot verify, good luck */
1740 break;
1741 }
1742 if (ntohl(put->type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
1743 {
1744 switch (GNUNET_BLOCK_evaluate(GDS_block_context,
1745 ntohl(put->type),
1746 NULL, /* query group */
1747 GNUNET_BLOCK_EO_NONE,
1748 NULL, /* query */
1749 NULL, 0, /* xquery */
1750 payload,
1751 payload_size))
1752 {
1753 case GNUNET_BLOCK_EVALUATION_OK_MORE:
1754 case GNUNET_BLOCK_EVALUATION_OK_LAST:
1755 break;
1756
1757 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
1758 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
1759 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
1760 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
1761 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
1762 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
1763 default:
1764 GNUNET_break_op(0);
1765 return;
1766 }
1775 } 1767 }
1776 }
1777 1768
1778 bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter, 1769 bf = GNUNET_CONTAINER_bloomfilter_init(put->bloomfilter,
1779 DHT_BLOOM_SIZE, 1770 DHT_BLOOM_SIZE,
1780 GNUNET_CONSTANTS_BLOOMFILTER_K); 1771 GNUNET_CONSTANTS_BLOOMFILTER_K);
1781 GNUNET_break_op (GNUNET_YES == 1772 GNUNET_break_op(GNUNET_YES ==
1782 GNUNET_CONTAINER_bloomfilter_test (bf, 1773 GNUNET_CONTAINER_bloomfilter_test(bf,
1783 &peer->phash)); 1774 &peer->phash));
1784 { 1775 {
1785 struct GNUNET_PeerIdentity pp[putlen + 1]; 1776 struct GNUNET_PeerIdentity pp[putlen + 1];
1786 1777
1787 /* extend 'put path' by sender */ 1778 /* extend 'put path' by sender */
1788 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE)) 1779 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
1789 {
1790#if SANITY_CHECKS
1791 for (unsigned int i=0;i<=putlen;i++)
1792 { 1780 {
1793 for (unsigned int j=0;j<i;j++) 1781#if SANITY_CHECKS
1794 { 1782 for (unsigned int i = 0; i <= putlen; i++)
1795 GNUNET_break (0 != memcmp (&pp[i], 1783 {
1796 &pp[j], 1784 for (unsigned int j = 0; j < i; j++)
1797 sizeof (struct GNUNET_PeerIdentity))); 1785 {
1798 } 1786 GNUNET_break(0 != memcmp(&pp[i],
1799 GNUNET_break (0 != memcmp (&pp[i], 1787 &pp[j],
1800 peer->id, 1788 sizeof(struct GNUNET_PeerIdentity)));
1801 sizeof (struct GNUNET_PeerIdentity))); 1789 }
1802 } 1790 GNUNET_break(0 != memcmp(&pp[i],
1791 peer->id,
1792 sizeof(struct GNUNET_PeerIdentity)));
1793 }
1803#endif 1794#endif
1804 GNUNET_memcpy (pp, 1795 GNUNET_memcpy(pp,
1805 put_path, 1796 put_path,
1806 putlen * sizeof (struct GNUNET_PeerIdentity)); 1797 putlen * sizeof(struct GNUNET_PeerIdentity));
1807 pp[putlen] = *peer->id; 1798 pp[putlen] = *peer->id;
1808 putlen++; 1799 putlen++;
1809 } 1800 }
1810 else 1801 else
1811 putlen = 0; 1802 putlen = 0;
1812 1803
1813 /* give to local clients */ 1804 /* give to local clients */
1814 GDS_CLIENTS_handle_reply (exp_time, 1805 GDS_CLIENTS_handle_reply(exp_time,
1815 &put->key, 1806 &put->key,
1816 0, 1807 0,
1817 NULL, 1808 NULL,
1818 putlen, 1809 putlen,
1819 pp, 1810 pp,
1820 ntohl (put->type), 1811 ntohl(put->type),
1821 payload_size, 1812 payload_size,
1822 payload); 1813 payload);
1823 /* store locally */ 1814 /* store locally */
1824 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || 1815 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1825 (GDS_am_closest_peer (&put->key, bf))) 1816 (GDS_am_closest_peer(&put->key, bf)))
1826 GDS_DATACACHE_handle_put (exp_time, 1817 GDS_DATACACHE_handle_put(exp_time,
1827 &put->key, 1818 &put->key,
1828 putlen, 1819 putlen,
1829 pp, 1820 pp,
1830 ntohl (put->type), 1821 ntohl(put->type),
1831 payload_size, 1822 payload_size,
1832 payload); 1823 payload);
1833 /* route to other peers */ 1824 /* route to other peers */
1834 forwarded = GDS_NEIGHBOURS_handle_put (ntohl (put->type), 1825 forwarded = GDS_NEIGHBOURS_handle_put(ntohl(put->type),
1835 options, 1826 options,
1836 ntohl (put->desired_replication_level), 1827 ntohl(put->desired_replication_level),
1837 exp_time, 1828 exp_time,
1838 ntohl (put->hop_count), 1829 ntohl(put->hop_count),
1839 bf, 1830 bf,
1840 &put->key, 1831 &put->key,
1841 putlen, 1832 putlen,
1842 pp, 1833 pp,
1843 payload, 1834 payload,
1844 payload_size); 1835 payload_size);
1845 /* notify monitoring clients */ 1836 /* notify monitoring clients */
1846 GDS_CLIENTS_process_put (options 1837 GDS_CLIENTS_process_put(options
1847 | ( (GNUNET_OK == forwarded) 1838 | ((GNUNET_OK == forwarded)
1848 ? GNUNET_DHT_RO_LAST_HOP 1839 ? GNUNET_DHT_RO_LAST_HOP
1849 : 0 ), 1840 : 0),
1850 ntohl (put->type), 1841 ntohl(put->type),
1851 ntohl (put->hop_count), 1842 ntohl(put->hop_count),
1852 ntohl (put->desired_replication_level), 1843 ntohl(put->desired_replication_level),
1853 putlen, pp, 1844 putlen, pp,
1854 exp_time, 1845 exp_time,
1855 &put->key, 1846 &put->key,
1856 payload, 1847 payload,
1857 payload_size); 1848 payload_size);
1858 } 1849 }
1859 GNUNET_CONTAINER_bloomfilter_free (bf); 1850 GNUNET_CONTAINER_bloomfilter_free(bf);
1860} 1851}
1861 1852
1862 1853
@@ -1869,9 +1860,9 @@ handle_dht_p2p_put (void *cls,
1869 * @param bg group for filtering peers 1860 * @param bg group for filtering peers
1870 */ 1861 */
1871static void 1862static void
1872handle_find_peer (const struct GNUNET_PeerIdentity *sender, 1863handle_find_peer(const struct GNUNET_PeerIdentity *sender,
1873 const struct GNUNET_HashCode *key, 1864 const struct GNUNET_HashCode *key,
1874 struct GNUNET_BLOCK_Group *bg) 1865 struct GNUNET_BLOCK_Group *bg)
1875{ 1866{
1876 int bucket_idx; 1867 int bucket_idx;
1877 struct PeerBucket *bucket; 1868 struct PeerBucket *bucket;
@@ -1882,98 +1873,99 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1882 1873
1883 /* first, check about our own HELLO */ 1874 /* first, check about our own HELLO */
1884 if (NULL != GDS_my_hello) 1875 if (NULL != GDS_my_hello)
1885 {
1886 hello_size = GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) GDS_my_hello);
1887 GNUNET_break (hello_size >= sizeof (struct GNUNET_MessageHeader));
1888 if (GNUNET_BLOCK_EVALUATION_OK_MORE ==
1889 GNUNET_BLOCK_evaluate (GDS_block_context,
1890 GNUNET_BLOCK_TYPE_DHT_HELLO,
1891 bg,
1892 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
1893 &my_identity_hash,
1894 NULL, 0,
1895 GDS_my_hello,
1896 hello_size))
1897 { 1876 {
1898 GDS_NEIGHBOURS_handle_reply (sender, 1877 hello_size = GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)GDS_my_hello);
1899 GNUNET_BLOCK_TYPE_DHT_HELLO, 1878 GNUNET_break(hello_size >= sizeof(struct GNUNET_MessageHeader));
1900 GNUNET_TIME_relative_to_absolute (hello_expiration), 1879 if (GNUNET_BLOCK_EVALUATION_OK_MORE ==
1901 key, 1880 GNUNET_BLOCK_evaluate(GDS_block_context,
1902 0, 1881 GNUNET_BLOCK_TYPE_DHT_HELLO,
1903 NULL, 1882 bg,
1904 0, 1883 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
1905 NULL, 1884 &my_identity_hash,
1906 GDS_my_hello, 1885 NULL, 0,
1907 hello_size); 1886 GDS_my_hello,
1887 hello_size))
1888 {
1889 GDS_NEIGHBOURS_handle_reply(sender,
1890 GNUNET_BLOCK_TYPE_DHT_HELLO,
1891 GNUNET_TIME_relative_to_absolute(hello_expiration),
1892 key,
1893 0,
1894 NULL,
1895 0,
1896 NULL,
1897 GDS_my_hello,
1898 hello_size);
1899 }
1900 else
1901 {
1902 GNUNET_STATISTICS_update(GDS_stats,
1903 gettext_noop("# FIND PEER requests ignored due to Bloomfilter"),
1904 1,
1905 GNUNET_NO);
1906 }
1908 } 1907 }
1909 else 1908 else
1910 { 1909 {
1911 GNUNET_STATISTICS_update (GDS_stats, 1910 GNUNET_STATISTICS_update(GDS_stats,
1912 gettext_noop ("# FIND PEER requests ignored due to Bloomfilter"), 1911 gettext_noop("# FIND PEER requests ignored due to lack of HELLO"),
1913 1, 1912 1,
1914 GNUNET_NO); 1913 GNUNET_NO);
1915 } 1914 }
1916 }
1917 else
1918 {
1919 GNUNET_STATISTICS_update (GDS_stats,
1920 gettext_noop ("# FIND PEER requests ignored due to lack of HELLO"),
1921 1,
1922 GNUNET_NO);
1923 }
1924 1915
1925 /* then, also consider sending a random HELLO from the closest bucket */ 1916 /* then, also consider sending a random HELLO from the closest bucket */
1926 if (0 == memcmp (&my_identity_hash, 1917 if (0 == memcmp(&my_identity_hash,
1927 key, 1918 key,
1928 sizeof (struct GNUNET_HashCode))) 1919 sizeof(struct GNUNET_HashCode)))
1929 bucket_idx = closest_bucket; 1920 bucket_idx = closest_bucket;
1930 else 1921 else
1931 bucket_idx = GNUNET_MIN ((int) closest_bucket, 1922 bucket_idx = GNUNET_MIN((int)closest_bucket,
1932 find_bucket (key)); 1923 find_bucket(key));
1933 if (bucket_idx < 0) 1924 if (bucket_idx < 0)
1934 return; 1925 return;
1935 bucket = &k_buckets[bucket_idx]; 1926 bucket = &k_buckets[bucket_idx];
1936 if (bucket->peers_size == 0) 1927 if (bucket->peers_size == 0)
1937 return; 1928 return;
1938 choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1929 choice = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
1939 bucket->peers_size); 1930 bucket->peers_size);
1940 peer = bucket->head; 1931 peer = bucket->head;
1941 while (choice > 0) 1932 while (choice > 0)
1942 { 1933 {
1943 GNUNET_assert (NULL != peer); 1934 GNUNET_assert(NULL != peer);
1944 peer = peer->next; 1935 peer = peer->next;
1945 choice--; 1936 choice--;
1946 } 1937 }
1947 choice = bucket->peers_size; 1938 choice = bucket->peers_size;
1948 do 1939 do
1949 { 1940 {
1950 peer = peer->next; 1941 peer = peer->next;
1951 if (0 == choice--) 1942 if (0 == choice--)
1952 return; /* no non-masked peer available */ 1943 return; /* no non-masked peer available */
1953 if (NULL == peer) 1944 if (NULL == peer)
1954 peer = bucket->head; 1945 peer = bucket->head;
1955 hello = GDS_HELLO_get (peer->id); 1946 hello = GDS_HELLO_get(peer->id);
1956 } while ( (NULL == hello) || 1947 }
1957 (GNUNET_BLOCK_EVALUATION_OK_MORE != 1948 while ((NULL == hello) ||
1958 GNUNET_BLOCK_evaluate (GDS_block_context, 1949 (GNUNET_BLOCK_EVALUATION_OK_MORE !=
1959 GNUNET_BLOCK_TYPE_DHT_HELLO, 1950 GNUNET_BLOCK_evaluate(GDS_block_context,
1960 bg, 1951 GNUNET_BLOCK_TYPE_DHT_HELLO,
1961 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO, 1952 bg,
1962 &peer->phash, 1953 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
1963 NULL, 0, 1954 &peer->phash,
1964 hello, 1955 NULL, 0,
1965 (hello_size = GNUNET_HELLO_size (hello)))) ); 1956 hello,
1966 GDS_NEIGHBOURS_handle_reply (sender, 1957 (hello_size = GNUNET_HELLO_size(hello)))));
1967 GNUNET_BLOCK_TYPE_DHT_HELLO, 1958 GDS_NEIGHBOURS_handle_reply(sender,
1968 GNUNET_TIME_relative_to_absolute 1959 GNUNET_BLOCK_TYPE_DHT_HELLO,
1969 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION), 1960 GNUNET_TIME_relative_to_absolute
1970 key, 1961 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1971 0, 1962 key,
1972 NULL, 1963 0,
1973 0, 1964 NULL,
1974 NULL, 1965 0,
1975 hello, 1966 NULL,
1976 hello_size); 1967 hello,
1968 hello_size);
1977} 1969}
1978 1970
1979 1971
@@ -1992,34 +1984,34 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1992 * @param data_size number of bytes in @a data 1984 * @param data_size number of bytes in @a data
1993 */ 1985 */
1994static void 1986static void
1995handle_local_result (void *cls, 1987handle_local_result(void *cls,
1996 enum GNUNET_BLOCK_Type type, 1988 enum GNUNET_BLOCK_Type type,
1997 struct GNUNET_TIME_Absolute expiration_time, 1989 struct GNUNET_TIME_Absolute expiration_time,
1998 const struct GNUNET_HashCode *key, 1990 const struct GNUNET_HashCode *key,
1999 unsigned int put_path_length, 1991 unsigned int put_path_length,
2000 const struct GNUNET_PeerIdentity *put_path, 1992 const struct GNUNET_PeerIdentity *put_path,
2001 unsigned int get_path_length, 1993 unsigned int get_path_length,
2002 const struct GNUNET_PeerIdentity *get_path, 1994 const struct GNUNET_PeerIdentity *get_path,
2003 const void *data, 1995 const void *data,
2004 size_t data_size) 1996 size_t data_size)
2005{ 1997{
2006 struct PeerInfo *peer = cls; 1998 struct PeerInfo *peer = cls;
2007 char *pp; 1999 char *pp;
2008 2000
2009 pp = GNUNET_STRINGS_pp2s (put_path, 2001 pp = GNUNET_STRINGS_pp2s(put_path,
2010 put_path_length); 2002 put_path_length);
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2003 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2012 "Found local result for %s (PP: %s)\n", 2004 "Found local result for %s (PP: %s)\n",
2013 GNUNET_h2s (key), 2005 GNUNET_h2s(key),
2014 pp); 2006 pp);
2015 GNUNET_free (pp); 2007 GNUNET_free(pp);
2016 GDS_NEIGHBOURS_handle_reply (peer->id, 2008 GDS_NEIGHBOURS_handle_reply(peer->id,
2017 type, 2009 type,
2018 expiration_time, 2010 expiration_time,
2019 key, 2011 key,
2020 put_path_length, put_path, 2012 put_path_length, put_path,
2021 get_path_length, get_path, 2013 get_path_length, get_path,
2022 data, data_size); 2014 data, data_size);
2023} 2015}
2024 2016
2025 2017
@@ -2031,20 +2023,20 @@ handle_local_result (void *cls,
2031 * @return #GNUNET_OK if the message is well-formed 2023 * @return #GNUNET_OK if the message is well-formed
2032 */ 2024 */
2033static int 2025static int
2034check_dht_p2p_get (void *cls, 2026check_dht_p2p_get(void *cls,
2035 const struct PeerGetMessage *get) 2027 const struct PeerGetMessage *get)
2036{ 2028{
2037 uint32_t xquery_size; 2029 uint32_t xquery_size;
2038 uint16_t msize; 2030 uint16_t msize;
2039 2031
2040 (void) cls; 2032 (void)cls;
2041 msize = ntohs (get->header.size); 2033 msize = ntohs(get->header.size);
2042 xquery_size = ntohl (get->xquery_size); 2034 xquery_size = ntohl(get->xquery_size);
2043 if (msize < sizeof (struct PeerGetMessage) + xquery_size) 2035 if (msize < sizeof(struct PeerGetMessage) + xquery_size)
2044 { 2036 {
2045 GNUNET_break_op (0); 2037 GNUNET_break_op(0);
2046 return GNUNET_SYSERR; 2038 return GNUNET_SYSERR;
2047 } 2039 }
2048 return GNUNET_OK; 2040 return GNUNET_OK;
2049} 2041}
2050 2042
@@ -2056,8 +2048,8 @@ check_dht_p2p_get (void *cls,
2056 * @param get the message 2048 * @param get the message
2057 */ 2049 */
2058static void 2050static void
2059handle_dht_p2p_get (void *cls, 2051handle_dht_p2p_get(void *cls,
2060 const struct PeerGetMessage *get) 2052 const struct PeerGetMessage *get)
2061{ 2053{
2062 struct PeerInfo *peer = cls; 2054 struct PeerInfo *peer = cls;
2063 uint32_t xquery_size; 2055 uint32_t xquery_size;
@@ -2072,141 +2064,141 @@ handle_dht_p2p_get (void *cls,
2072 int forwarded; 2064 int forwarded;
2073 2065
2074 /* parse and validate message */ 2066 /* parse and validate message */
2075 msize = ntohs (get->header.size); 2067 msize = ntohs(get->header.size);
2076 xquery_size = ntohl (get->xquery_size); 2068 xquery_size = ntohl(get->xquery_size);
2077 reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size); 2069 reply_bf_size = msize - (sizeof(struct PeerGetMessage) + xquery_size);
2078 type = ntohl (get->type); 2070 type = ntohl(get->type);
2079 options = ntohl (get->options); 2071 options = ntohl(get->options);
2080 xquery = (const char *) &get[1]; 2072 xquery = (const char *)&get[1];
2081 GNUNET_STATISTICS_update (GDS_stats, 2073 GNUNET_STATISTICS_update(GDS_stats,
2082 gettext_noop ("# P2P GET requests received"), 2074 gettext_noop("# P2P GET requests received"),
2083 1, 2075 1,
2084 GNUNET_NO); 2076 GNUNET_NO);
2085 GNUNET_STATISTICS_update (GDS_stats, 2077 GNUNET_STATISTICS_update(GDS_stats,
2086 gettext_noop ("# P2P GET bytes received"), 2078 gettext_noop("# P2P GET bytes received"),
2087 msize, 2079 msize,
2088 GNUNET_NO); 2080 GNUNET_NO);
2089 if (GNUNET_YES == log_route_details_stderr) 2081 if (GNUNET_YES == log_route_details_stderr)
2090 { 2082 {
2091 char *tmp; 2083 char *tmp;
2092 2084
2093 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity)); 2085 tmp = GNUNET_strdup(GNUNET_i2s(&my_identity));
2094 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, 2086 LOG_TRAFFIC(GNUNET_ERROR_TYPE_DEBUG,
2095 "R5N GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n", 2087 "R5N GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n",
2096 GNUNET_h2s (&get->key), 2088 GNUNET_h2s(&get->key),
2097 GNUNET_i2s (peer->id), 2089 GNUNET_i2s(peer->id),
2098 tmp, 2090 tmp,
2099 ntohl(get->hop_count), 2091 ntohl(get->hop_count),
2100 GNUNET_CRYPTO_hash_matching_bits (&peer->phash, 2092 GNUNET_CRYPTO_hash_matching_bits(&peer->phash,
2101 &get->key), 2093 &get->key),
2102 GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, 2094 GNUNET_CRYPTO_hash_matching_bits(&my_identity_hash,
2103 &get->key), 2095 &get->key),
2104 ntohl(get->xquery_size), 2096 ntohl(get->xquery_size),
2105 xquery); 2097 xquery);
2106 GNUNET_free (tmp); 2098 GNUNET_free(tmp);
2107 } 2099 }
2108 eval 2100 eval
2109 = GNUNET_BLOCK_evaluate (GDS_block_context, 2101 = GNUNET_BLOCK_evaluate(GDS_block_context,
2110 type, 2102 type,
2111 NULL, 2103 NULL,
2112 GNUNET_BLOCK_EO_NONE, 2104 GNUNET_BLOCK_EO_NONE,
2113 &get->key, 2105 &get->key,
2114 xquery, 2106 xquery,
2115 xquery_size, 2107 xquery_size,
2116 NULL, 2108 NULL,
2117 0); 2109 0);
2118 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID) 2110 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
2119 { 2111 {
2120 /* request invalid or block type not supported */ 2112 /* request invalid or block type not supported */
2121 GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED); 2113 GNUNET_break_op(eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
2122 return; 2114 return;
2123 } 2115 }
2124 peer_bf = GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, 2116 peer_bf = GNUNET_CONTAINER_bloomfilter_init(get->bloomfilter,
2125 DHT_BLOOM_SIZE, 2117 DHT_BLOOM_SIZE,
2126 GNUNET_CONSTANTS_BLOOMFILTER_K); 2118 GNUNET_CONSTANTS_BLOOMFILTER_K);
2127 GNUNET_break_op (GNUNET_YES == 2119 GNUNET_break_op(GNUNET_YES ==
2128 GNUNET_CONTAINER_bloomfilter_test (peer_bf, 2120 GNUNET_CONTAINER_bloomfilter_test(peer_bf,
2129 &peer->phash)); 2121 &peer->phash));
2130 bg = GNUNET_BLOCK_group_create (GDS_block_context, 2122 bg = GNUNET_BLOCK_group_create(GDS_block_context,
2131 type, 2123 type,
2132 get->bf_mutator, 2124 get->bf_mutator,
2133 &xquery[xquery_size], 2125 &xquery[xquery_size],
2134 reply_bf_size, 2126 reply_bf_size,
2135 "filter-size", 2127 "filter-size",
2136 reply_bf_size, 2128 reply_bf_size,
2137 NULL); 2129 NULL);
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2139 "GET for %s at %s after %u hops\n", 2131 "GET for %s at %s after %u hops\n",
2140 GNUNET_h2s (&get->key), 2132 GNUNET_h2s(&get->key),
2141 GNUNET_i2s (&my_identity), 2133 GNUNET_i2s(&my_identity),
2142 (unsigned int) ntohl (get->hop_count)); 2134 (unsigned int)ntohl(get->hop_count));
2143 /* local lookup (this may update the reply_bf) */ 2135 /* local lookup (this may update the reply_bf) */
2144 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || 2136 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
2145 (GDS_am_closest_peer (&get->key, 2137 (GDS_am_closest_peer(&get->key,
2146 peer_bf))) 2138 peer_bf)))
2147 {
2148 if ((0 != (options & GNUNET_DHT_RO_FIND_PEER)))
2149 { 2139 {
2150 GNUNET_STATISTICS_update (GDS_stats, 2140 if ((0 != (options & GNUNET_DHT_RO_FIND_PEER)))
2151 gettext_noop ("# P2P FIND PEER requests processed"), 2141 {
2152 1, 2142 GNUNET_STATISTICS_update(GDS_stats,
2153 GNUNET_NO); 2143 gettext_noop("# P2P FIND PEER requests processed"),
2154 handle_find_peer (peer->id, 2144 1,
2155 &get->key, 2145 GNUNET_NO);
2156 bg); 2146 handle_find_peer(peer->id,
2147 &get->key,
2148 bg);
2149 }
2150 else
2151 {
2152 eval = GDS_DATACACHE_handle_get(&get->key,
2153 type,
2154 xquery,
2155 xquery_size,
2156 bg,
2157 &handle_local_result,
2158 peer);
2159 }
2157 } 2160 }
2158 else 2161 else
2159 { 2162 {
2160 eval = GDS_DATACACHE_handle_get (&get->key, 2163 GNUNET_STATISTICS_update(GDS_stats,
2161 type, 2164 gettext_noop("# P2P GET requests ONLY routed"),
2162 xquery, 2165 1,
2163 xquery_size, 2166 GNUNET_NO);
2164 bg,
2165 &handle_local_result,
2166 peer);
2167 } 2167 }
2168 }
2169 else
2170 {
2171 GNUNET_STATISTICS_update (GDS_stats,
2172 gettext_noop ("# P2P GET requests ONLY routed"),
2173 1,
2174 GNUNET_NO);
2175 }
2176 2168
2177 /* remember request for routing replies */ 2169 /* remember request for routing replies */
2178 GDS_ROUTING_add (peer->id, 2170 GDS_ROUTING_add(peer->id,
2179 type, 2171 type,
2180 bg, /* bg now owned by routing, but valid at least until end of this function! */ 2172 bg, /* bg now owned by routing, but valid at least until end of this function! */
2181 options, 2173 options,
2182 &get->key, 2174 &get->key,
2183 xquery, 2175 xquery,
2184 xquery_size); 2176 xquery_size);
2185 2177
2186 /* P2P forwarding */ 2178 /* P2P forwarding */
2187 forwarded = GNUNET_NO; 2179 forwarded = GNUNET_NO;
2188 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST) 2180 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
2189 forwarded = GDS_NEIGHBOURS_handle_get (type, 2181 forwarded = GDS_NEIGHBOURS_handle_get(type,
2190 options, 2182 options,
2191 ntohl (get->desired_replication_level), 2183 ntohl(get->desired_replication_level),
2192 ntohl (get->hop_count), 2184 ntohl(get->hop_count),
2193 &get->key, 2185 &get->key,
2194 xquery, 2186 xquery,
2195 xquery_size, 2187 xquery_size,
2196 bg, 2188 bg,
2197 peer_bf); 2189 peer_bf);
2198 GDS_CLIENTS_process_get (options 2190 GDS_CLIENTS_process_get(options
2199 | (GNUNET_OK == forwarded) 2191 | (GNUNET_OK == forwarded)
2200 ? GNUNET_DHT_RO_LAST_HOP : 0, 2192 ? GNUNET_DHT_RO_LAST_HOP : 0,
2201 type, 2193 type,
2202 ntohl (get->hop_count), 2194 ntohl(get->hop_count),
2203 ntohl (get->desired_replication_level), 2195 ntohl(get->desired_replication_level),
2204 0, 2196 0,
2205 NULL, 2197 NULL,
2206 &get->key); 2198 &get->key);
2207 2199
2208 /* clean up; note that 'bg' is owned by routing now! */ 2200 /* clean up; note that 'bg' is owned by routing now! */
2209 GNUNET_CONTAINER_bloomfilter_free (peer_bf); 2201 GNUNET_CONTAINER_bloomfilter_free(peer_bf);
2210} 2202}
2211 2203
2212 2204
@@ -2218,29 +2210,29 @@ handle_dht_p2p_get (void *cls,
2218 * @return #GNUNET_YES if the message is well-formed 2210 * @return #GNUNET_YES if the message is well-formed
2219 */ 2211 */
2220static int 2212static int
2221check_dht_p2p_result (void *cls, 2213check_dht_p2p_result(void *cls,
2222 const struct PeerResultMessage *prm) 2214 const struct PeerResultMessage *prm)
2223{ 2215{
2224 uint32_t get_path_length; 2216 uint32_t get_path_length;
2225 uint32_t put_path_length; 2217 uint32_t put_path_length;
2226 uint16_t msize; 2218 uint16_t msize;
2227 2219
2228 (void) cls; 2220 (void)cls;
2229 msize = ntohs (prm->header.size); 2221 msize = ntohs(prm->header.size);
2230 put_path_length = ntohl (prm->put_path_length); 2222 put_path_length = ntohl(prm->put_path_length);
2231 get_path_length = ntohl (prm->get_path_length); 2223 get_path_length = ntohl(prm->get_path_length);
2232 if ((msize < 2224 if ((msize <
2233 sizeof (struct PeerResultMessage) + (get_path_length + 2225 sizeof(struct PeerResultMessage) + (get_path_length +
2234 put_path_length) * 2226 put_path_length) *
2235 sizeof (struct GNUNET_PeerIdentity)) || 2227 sizeof(struct GNUNET_PeerIdentity)) ||
2236 (get_path_length > 2228 (get_path_length >
2237 GNUNET_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || 2229 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_PeerIdentity)) ||
2238 (put_path_length > 2230 (put_path_length >
2239 GNUNET_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))) 2231 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_PeerIdentity)))
2240 { 2232 {
2241 GNUNET_break_op (0); 2233 GNUNET_break_op(0);
2242 return GNUNET_SYSERR; 2234 return GNUNET_SYSERR;
2243 } 2235 }
2244 return GNUNET_OK; 2236 return GNUNET_OK;
2245} 2237}
2246 2238
@@ -2259,64 +2251,64 @@ check_dht_p2p_result (void *cls,
2259 * @param data payload of the reply 2251 * @param data payload of the reply
2260 */ 2252 */
2261static void 2253static void
2262process_reply_with_path (struct GNUNET_TIME_Absolute expiration_time, 2254process_reply_with_path(struct GNUNET_TIME_Absolute expiration_time,
2263 const struct GNUNET_HashCode *key, 2255 const struct GNUNET_HashCode *key,
2264 unsigned int get_path_length, 2256 unsigned int get_path_length,
2265 const struct GNUNET_PeerIdentity *get_path, 2257 const struct GNUNET_PeerIdentity *get_path,
2266 unsigned int put_path_length, 2258 unsigned int put_path_length,
2267 const struct GNUNET_PeerIdentity *put_path, 2259 const struct GNUNET_PeerIdentity *put_path,
2268 enum GNUNET_BLOCK_Type type, 2260 enum GNUNET_BLOCK_Type type,
2269 size_t data_size, 2261 size_t data_size,
2270 const void *data) 2262 const void *data)
2271{ 2263{
2272 /* forward to local clients */ 2264 /* forward to local clients */
2273 GDS_CLIENTS_handle_reply (expiration_time, 2265 GDS_CLIENTS_handle_reply(expiration_time,
2274 key, 2266 key,
2275 get_path_length, 2267 get_path_length,
2276 get_path, 2268 get_path,
2277 put_path_length, 2269 put_path_length,
2278 put_path, 2270 put_path,
2279 type, 2271 type,
2280 data_size, 2272 data_size,
2281 data); 2273 data);
2282 GDS_CLIENTS_process_get_resp (type, 2274 GDS_CLIENTS_process_get_resp(type,
2283 get_path, 2275 get_path,
2284 get_path_length, 2276 get_path_length,
2285 put_path, 2277 put_path,
2286 put_path_length, 2278 put_path_length,
2287 expiration_time, 2279 expiration_time,
2288 key, 2280 key,
2289 data, 2281 data,
2290 data_size); 2282 data_size);
2291 if (GNUNET_YES == cache_results) 2283 if (GNUNET_YES == cache_results)
2292 { 2284 {
2293 struct GNUNET_PeerIdentity xput_path[get_path_length + 1 + put_path_length]; 2285 struct GNUNET_PeerIdentity xput_path[get_path_length + 1 + put_path_length];
2294 2286
2295 GNUNET_memcpy (xput_path, 2287 GNUNET_memcpy(xput_path,
2296 put_path, 2288 put_path,
2297 put_path_length * sizeof (struct GNUNET_PeerIdentity)); 2289 put_path_length * sizeof(struct GNUNET_PeerIdentity));
2298 GNUNET_memcpy (&xput_path[put_path_length], 2290 GNUNET_memcpy(&xput_path[put_path_length],
2299 get_path, 2291 get_path,
2300 get_path_length * sizeof (struct GNUNET_PeerIdentity)); 2292 get_path_length * sizeof(struct GNUNET_PeerIdentity));
2301 2293
2302 GDS_DATACACHE_handle_put (expiration_time, 2294 GDS_DATACACHE_handle_put(expiration_time,
2303 key, 2295 key,
2304 get_path_length + put_path_length, 2296 get_path_length + put_path_length,
2305 xput_path, 2297 xput_path,
2306 type, 2298 type,
2307 data_size, 2299 data_size,
2308 data); 2300 data);
2309 } 2301 }
2310 /* forward to other peers */ 2302 /* forward to other peers */
2311 GDS_ROUTING_process (type, 2303 GDS_ROUTING_process(type,
2312 expiration_time, 2304 expiration_time,
2313 key, 2305 key,
2314 put_path_length, 2306 put_path_length,
2315 put_path, 2307 put_path,
2316 get_path_length, 2308 get_path_length,
2317 get_path, 2309 get_path,
2318 data, 2310 data,
2319 data_size); 2311 data_size);
2320} 2312}
2321 2313
2322 2314
@@ -2327,8 +2319,8 @@ process_reply_with_path (struct GNUNET_TIME_Absolute expiration_time,
2327 * @param message message 2319 * @param message message
2328 */ 2320 */
2329static void 2321static void
2330handle_dht_p2p_result (void *cls, 2322handle_dht_p2p_result(void *cls,
2331 const struct PeerResultMessage *prm) 2323 const struct PeerResultMessage *prm)
2332{ 2324{
2333 struct PeerInfo *peer = cls; 2325 struct PeerInfo *peer = cls;
2334 const struct GNUNET_PeerIdentity *put_path; 2326 const struct GNUNET_PeerIdentity *put_path;
@@ -2342,125 +2334,125 @@ handle_dht_p2p_result (void *cls,
2342 struct GNUNET_TIME_Absolute exp_time; 2334 struct GNUNET_TIME_Absolute exp_time;
2343 2335
2344 /* parse and validate message */ 2336 /* parse and validate message */
2345 exp_time = GNUNET_TIME_absolute_ntoh (prm->expiration_time); 2337 exp_time = GNUNET_TIME_absolute_ntoh(prm->expiration_time);
2346 if (0 == GNUNET_TIME_absolute_get_remaining (exp_time).rel_value_us) 2338 if (0 == GNUNET_TIME_absolute_get_remaining(exp_time).rel_value_us)
2347 {
2348 GNUNET_STATISTICS_update (GDS_stats,
2349 gettext_noop ("# Expired results discarded"),
2350 1,
2351 GNUNET_NO);
2352 return;
2353 }
2354 msize = ntohs (prm->header.size);
2355 put_path_length = ntohl (prm->put_path_length);
2356 get_path_length = ntohl (prm->get_path_length);
2357 put_path = (const struct GNUNET_PeerIdentity *) &prm[1];
2358 get_path = &put_path[put_path_length];
2359 type = ntohl (prm->type);
2360 data = (const void *) &get_path[get_path_length];
2361 data_size = msize - (sizeof (struct PeerResultMessage) +
2362 (get_path_length +
2363 put_path_length) * sizeof (struct GNUNET_PeerIdentity));
2364 GNUNET_STATISTICS_update (GDS_stats,
2365 gettext_noop ("# P2P RESULTS received"),
2366 1,
2367 GNUNET_NO);
2368 GNUNET_STATISTICS_update (GDS_stats,
2369 gettext_noop ("# P2P RESULT bytes received"),
2370 msize,
2371 GNUNET_NO);
2372 if (GNUNET_YES == log_route_details_stderr)
2373 {
2374 char *tmp;
2375 char *pp;
2376 char *gp;
2377
2378 gp = GNUNET_STRINGS_pp2s (get_path,
2379 get_path_length);
2380 pp = GNUNET_STRINGS_pp2s (put_path,
2381 put_path_length);
2382 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
2383 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
2384 "R5N RESULT %s: %s->%s (GP: %s, PP: %s)\n",
2385 GNUNET_h2s (&prm->key),
2386 GNUNET_i2s (peer->id),
2387 tmp,
2388 gp,
2389 pp);
2390 GNUNET_free (gp);
2391 GNUNET_free (pp);
2392 GNUNET_free (tmp);
2393 }
2394 /* if we got a HELLO, consider it for our own routing table */
2395 if (GNUNET_BLOCK_TYPE_DHT_HELLO == type)
2396 {
2397 const struct GNUNET_MessageHeader *h;
2398 struct GNUNET_PeerIdentity pid;
2399
2400 /* Should be a HELLO, validate and consider using it! */
2401 if (data_size < sizeof (struct GNUNET_HELLO_Message))
2402 { 2339 {
2403 GNUNET_break_op (0); 2340 GNUNET_STATISTICS_update(GDS_stats,
2341 gettext_noop("# Expired results discarded"),
2342 1,
2343 GNUNET_NO);
2404 return; 2344 return;
2405 } 2345 }
2406 h = data; 2346 msize = ntohs(prm->header.size);
2407 if (data_size != ntohs (h->size)) 2347 put_path_length = ntohl(prm->put_path_length);
2348 get_path_length = ntohl(prm->get_path_length);
2349 put_path = (const struct GNUNET_PeerIdentity *)&prm[1];
2350 get_path = &put_path[put_path_length];
2351 type = ntohl(prm->type);
2352 data = (const void *)&get_path[get_path_length];
2353 data_size = msize - (sizeof(struct PeerResultMessage) +
2354 (get_path_length +
2355 put_path_length) * sizeof(struct GNUNET_PeerIdentity));
2356 GNUNET_STATISTICS_update(GDS_stats,
2357 gettext_noop("# P2P RESULTS received"),
2358 1,
2359 GNUNET_NO);
2360 GNUNET_STATISTICS_update(GDS_stats,
2361 gettext_noop("# P2P RESULT bytes received"),
2362 msize,
2363 GNUNET_NO);
2364 if (GNUNET_YES == log_route_details_stderr)
2408 { 2365 {
2409 GNUNET_break_op (0); 2366 char *tmp;
2410 return; 2367 char *pp;
2368 char *gp;
2369
2370 gp = GNUNET_STRINGS_pp2s(get_path,
2371 get_path_length);
2372 pp = GNUNET_STRINGS_pp2s(put_path,
2373 put_path_length);
2374 tmp = GNUNET_strdup(GNUNET_i2s(&my_identity));
2375 LOG_TRAFFIC(GNUNET_ERROR_TYPE_DEBUG,
2376 "R5N RESULT %s: %s->%s (GP: %s, PP: %s)\n",
2377 GNUNET_h2s(&prm->key),
2378 GNUNET_i2s(peer->id),
2379 tmp,
2380 gp,
2381 pp);
2382 GNUNET_free(gp);
2383 GNUNET_free(pp);
2384 GNUNET_free(tmp);
2411 } 2385 }
2412 if (GNUNET_OK != 2386 /* if we got a HELLO, consider it for our own routing table */
2413 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h, 2387 if (GNUNET_BLOCK_TYPE_DHT_HELLO == type)
2414 &pid))
2415 { 2388 {
2416 GNUNET_break_op (0); 2389 const struct GNUNET_MessageHeader *h;
2417 return; 2390 struct GNUNET_PeerIdentity pid;
2418 } 2391
2419 if ( (GNUNET_YES != disable_try_connect) && 2392 /* Should be a HELLO, validate and consider using it! */
2420 (0 != memcmp (&my_identity, 2393 if (data_size < sizeof(struct GNUNET_HELLO_Message))
2394 {
2395 GNUNET_break_op(0);
2396 return;
2397 }
2398 h = data;
2399 if (data_size != ntohs(h->size))
2400 {
2401 GNUNET_break_op(0);
2402 return;
2403 }
2404 if (GNUNET_OK !=
2405 GNUNET_HELLO_get_id((const struct GNUNET_HELLO_Message *)h,
2406 &pid))
2407 {
2408 GNUNET_break_op(0);
2409 return;
2410 }
2411 if ((GNUNET_YES != disable_try_connect) &&
2412 (0 != memcmp(&my_identity,
2421 &pid, 2413 &pid,
2422 sizeof (struct GNUNET_PeerIdentity))) ) 2414 sizeof(struct GNUNET_PeerIdentity))))
2423 try_connect (&pid, 2415 try_connect(&pid,
2424 h); 2416 h);
2425 } 2417 }
2426 2418
2427 /* First, check if 'peer' is already on the path, and if 2419 /* First, check if 'peer' is already on the path, and if
2428 so, truncate it instead of expanding. */ 2420 so, truncate it instead of expanding. */
2429 for (unsigned int i=0;i<=get_path_length;i++) 2421 for (unsigned int i = 0; i <= get_path_length; i++)
2430 if (0 == memcmp (&get_path[i], 2422 if (0 == memcmp(&get_path[i],
2431 peer->id, 2423 peer->id,
2432 sizeof (struct GNUNET_PeerIdentity))) 2424 sizeof(struct GNUNET_PeerIdentity)))
2433 { 2425 {
2434 process_reply_with_path (exp_time, 2426 process_reply_with_path(exp_time,
2435 &prm->key, 2427 &prm->key,
2436 i, 2428 i,
2437 get_path, 2429 get_path,
2438 put_path_length, 2430 put_path_length,
2439 put_path, 2431 put_path,
2440 type, 2432 type,
2441 data_size, 2433 data_size,
2442 data); 2434 data);
2443 return; 2435 return;
2444 } 2436 }
2445 2437
2446 /* Need to append 'peer' to 'get_path' (normal case) */ 2438 /* Need to append 'peer' to 'get_path' (normal case) */
2447 { 2439 {
2448 struct GNUNET_PeerIdentity xget_path[get_path_length + 1]; 2440 struct GNUNET_PeerIdentity xget_path[get_path_length + 1];
2449 2441
2450 GNUNET_memcpy (xget_path, 2442 GNUNET_memcpy(xget_path,
2451 get_path, 2443 get_path,
2452 get_path_length * sizeof (struct GNUNET_PeerIdentity)); 2444 get_path_length * sizeof(struct GNUNET_PeerIdentity));
2453 xget_path[get_path_length] = *peer->id; 2445 xget_path[get_path_length] = *peer->id;
2454 2446
2455 process_reply_with_path (exp_time, 2447 process_reply_with_path(exp_time,
2456 &prm->key, 2448 &prm->key,
2457 get_path_length + 1, 2449 get_path_length + 1,
2458 xget_path, 2450 xget_path,
2459 put_path_length, 2451 put_path_length,
2460 put_path, 2452 put_path,
2461 type, 2453 type,
2462 data_size, 2454 data_size,
2463 data); 2455 data);
2464 } 2456 }
2465} 2457}
2466 2458
@@ -2471,55 +2463,55 @@ handle_dht_p2p_result (void *cls,
2471 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 2463 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2472 */ 2464 */
2473int 2465int
2474GDS_NEIGHBOURS_init () 2466GDS_NEIGHBOURS_init()
2475{ 2467{
2476 struct GNUNET_MQ_MessageHandler core_handlers[] = { 2468 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2477 GNUNET_MQ_hd_var_size (dht_p2p_get, 2469 GNUNET_MQ_hd_var_size(dht_p2p_get,
2478 GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 2470 GNUNET_MESSAGE_TYPE_DHT_P2P_GET,
2479 struct PeerGetMessage, 2471 struct PeerGetMessage,
2480 NULL), 2472 NULL),
2481 GNUNET_MQ_hd_var_size (dht_p2p_put, 2473 GNUNET_MQ_hd_var_size(dht_p2p_put,
2482 GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 2474 GNUNET_MESSAGE_TYPE_DHT_P2P_PUT,
2483 struct PeerPutMessage, 2475 struct PeerPutMessage,
2484 NULL), 2476 NULL),
2485 GNUNET_MQ_hd_var_size (dht_p2p_result, 2477 GNUNET_MQ_hd_var_size(dht_p2p_result,
2486 GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 2478 GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT,
2487 struct PeerResultMessage, 2479 struct PeerResultMessage,
2488 NULL), 2480 NULL),
2489 GNUNET_MQ_handler_end () 2481 GNUNET_MQ_handler_end()
2490 }; 2482 };
2491 unsigned long long temp_config_num; 2483 unsigned long long temp_config_num;
2492 2484
2493 disable_try_connect 2485 disable_try_connect
2494 = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg, 2486 = GNUNET_CONFIGURATION_get_value_yesno(GDS_cfg,
2495 "DHT", 2487 "DHT",
2496 "DISABLE_TRY_CONNECT"); 2488 "DISABLE_TRY_CONNECT");
2497 if (GNUNET_OK == 2489 if (GNUNET_OK ==
2498 GNUNET_CONFIGURATION_get_value_number (GDS_cfg, 2490 GNUNET_CONFIGURATION_get_value_number(GDS_cfg,
2499 "DHT", 2491 "DHT",
2500 "bucket_size", 2492 "bucket_size",
2501 &temp_config_num)) 2493 &temp_config_num))
2502 bucket_size = (unsigned int) temp_config_num; 2494 bucket_size = (unsigned int)temp_config_num;
2503 cache_results 2495 cache_results
2504 = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg, 2496 = GNUNET_CONFIGURATION_get_value_yesno(GDS_cfg,
2505 "DHT", 2497 "DHT",
2506 "CACHE_RESULTS"); 2498 "CACHE_RESULTS");
2507 2499
2508 log_route_details_stderr = 2500 log_route_details_stderr =
2509 (NULL != getenv("GNUNET_DHT_ROUTE_DEBUG")) ? GNUNET_YES : GNUNET_NO; 2501 (NULL != getenv("GNUNET_DHT_ROUTE_DEBUG")) ? GNUNET_YES : GNUNET_NO;
2510 ats_ch = GNUNET_ATS_connectivity_init (GDS_cfg); 2502 ats_ch = GNUNET_ATS_connectivity_init(GDS_cfg);
2511 core_api = GNUNET_CORE_connect (GDS_cfg, 2503 core_api = GNUNET_CORE_connect(GDS_cfg,
2512 NULL, 2504 NULL,
2513 &core_init, 2505 &core_init,
2514 &handle_core_connect, 2506 &handle_core_connect,
2515 &handle_core_disconnect, 2507 &handle_core_disconnect,
2516 core_handlers); 2508 core_handlers);
2517 if (NULL == core_api) 2509 if (NULL == core_api)
2518 return GNUNET_SYSERR; 2510 return GNUNET_SYSERR;
2519 all_connected_peers = GNUNET_CONTAINER_multipeermap_create (256, 2511 all_connected_peers = GNUNET_CONTAINER_multipeermap_create(256,
2520 GNUNET_YES); 2512 GNUNET_YES);
2521 all_desired_peers = GNUNET_CONTAINER_multipeermap_create (256, 2513 all_desired_peers = GNUNET_CONTAINER_multipeermap_create(256,
2522 GNUNET_NO); 2514 GNUNET_NO);
2523 return GNUNET_OK; 2515 return GNUNET_OK;
2524} 2516}
2525 2517
@@ -2528,24 +2520,24 @@ GDS_NEIGHBOURS_init ()
2528 * Shutdown neighbours subsystem. 2520 * Shutdown neighbours subsystem.
2529 */ 2521 */
2530void 2522void
2531GDS_NEIGHBOURS_done () 2523GDS_NEIGHBOURS_done()
2532{ 2524{
2533 if (NULL == core_api) 2525 if (NULL == core_api)
2534 return; 2526 return;
2535 GNUNET_CORE_disconnect (core_api); 2527 GNUNET_CORE_disconnect(core_api);
2536 core_api = NULL; 2528 core_api = NULL;
2537 GNUNET_assert (0 == 2529 GNUNET_assert(0 ==
2538 GNUNET_CONTAINER_multipeermap_size (all_connected_peers)); 2530 GNUNET_CONTAINER_multipeermap_size(all_connected_peers));
2539 GNUNET_CONTAINER_multipeermap_destroy (all_connected_peers); 2531 GNUNET_CONTAINER_multipeermap_destroy(all_connected_peers);
2540 all_connected_peers = NULL; 2532 all_connected_peers = NULL;
2541 GNUNET_CONTAINER_multipeermap_iterate (all_desired_peers, 2533 GNUNET_CONTAINER_multipeermap_iterate(all_desired_peers,
2542 &free_connect_info, 2534 &free_connect_info,
2543 NULL); 2535 NULL);
2544 GNUNET_CONTAINER_multipeermap_destroy (all_desired_peers); 2536 GNUNET_CONTAINER_multipeermap_destroy(all_desired_peers);
2545 all_desired_peers = NULL; 2537 all_desired_peers = NULL;
2546 GNUNET_ATS_connectivity_done (ats_ch); 2538 GNUNET_ATS_connectivity_done(ats_ch);
2547 ats_ch = NULL; 2539 ats_ch = NULL;
2548 GNUNET_assert (NULL == find_peer_task); 2540 GNUNET_assert(NULL == find_peer_task);
2549} 2541}
2550 2542
2551 2543
@@ -2555,7 +2547,7 @@ GDS_NEIGHBOURS_done ()
2555 * @return identity of the local node 2547 * @return identity of the local node
2556 */ 2548 */
2557struct GNUNET_PeerIdentity * 2549struct GNUNET_PeerIdentity *
2558GDS_NEIGHBOURS_get_id () 2550GDS_NEIGHBOURS_get_id()
2559{ 2551{
2560 return &my_identity; 2552 return &my_identity;
2561} 2553}