GNUnet 0.28.1-dev.4-24-g0cf3356dd
 
Loading...
Searching...
No Matches
gnunet-service-dht_neighbours.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2017, 2021-2022, 2026 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
27#include "gnunet_common.h"
28#include "gnunet_constants.h"
30#include "gnunet_dht_service.h"
31#include "gnunet_protocols.h"
33#include "gnunet_pils_service.h"
34#include "gnunet-service-dht.h"
37#include "dht.h"
38#include "dht_helper.h"
40#include "gnunet_util_lib.h"
41
42#define LOG_TRAFFIC(kind, ...) GNUNET_log_from (kind, "dht-traffic", \
43 __VA_ARGS__)
44
56#define SANITY_CHECKS 2
57
61#define MAX_BUCKETS sizeof(struct GNUNET_HashCode) * 8
62
66#define DEFAULT_BUCKET_SIZE 8
67
71#define FIND_PEER_REPLICATION_LEVEL 4
72
76#define MAXIMUM_PENDING_PER_PEER 64
77
83#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply ( \
84 GNUNET_TIME_UNIT_MINUTES, 2)
85
86
96#define DHT_AVG_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply ( \
97 GNUNET_TIME_UNIT_SECONDS, 6)
98
102#define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
103
104
106
107
112{
117
122
127
132
137
142
147
152
153 /* trunc_peer (if truncated) */
154
155 /* put path (if tracked) */
156
157 /* get path (if tracked) */
158
159 /* sender_sig (if path tracking is on) */
160
161 /* Payload */
162};
163
164
169{
174
179
184
189
194
199
204
209
210 /* result bloomfilter */
211
212 /* xquery */
213
214};
216
217
221struct PeerInfo;
222
223
227struct Target
228{
232 struct Target *next;
233
237 struct Target *prev;
238
243
248
252 struct PeerInfo *pi;
253
258
262 unsigned int load;
263
269
270};
271
272
277{
282
287
292
296 struct PeerInfo *next;
297
301 struct PeerInfo *prev;
302
306 struct Target *t_head;
307
311 struct Target *t_tail;
312
316 void *hello;
317
322
327};
328
329
334{
338 struct PeerInfo *head;
339
343 struct PeerInfo *tail;
344
348 unsigned int peers_size;
349};
350
351
355static int cache_results;
356
361static unsigned int closest_bucket;
362
367static unsigned int newly_found_peers;
368
373
378
384
388static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
389
394
395
403static void
404send_done_cb (void *cls)
405{
406 struct Target *t = cls;
407 struct PeerInfo *pi = t->pi; /* NULL if t->dropped! */
408
409 GNUNET_assert (t->load > 0);
410 t->load--;
411 if (0 < t->load)
412 return;
413 if (t->dropped)
414 {
415 GNUNET_free (t);
416 return;
417 }
418 /* move target back to the front */
420 pi->t_tail,
421 t);
423 pi->t_tail,
424 t);
425}
426
427
436static bool
437do_send (struct PeerInfo *pi,
438 const struct GNUNET_MessageHeader *msg)
439{
440 struct Target *t;
441
442 for (t = pi->t_head;
443 NULL != t;
444 t = t->next)
445 if (t->load < MAXIMUM_PENDING_PER_PEER)
446 break;
447 if (NULL == t)
448 {
449 /* all targets busy, drop message */
451 "# messages dropped (underlays busy)",
452 1,
453 GNUNET_NO);
454 return false;
455 }
456 t->load++;
457 /* rotate busy targets to the end */
458 if (MAXIMUM_PENDING_PER_PEER == t->load)
459 {
461 pi->t_tail,
462 t);
464 pi->t_tail,
465 t);
466 }
467 GDS_u_send (t->u,
468 t->utarget,
469 msg,
470 ntohs (msg->size),
472 t);
473 return true;
474}
475
476
487static int
488find_bucket (const struct GNUNET_HashCode *hc)
489{
490 const struct GNUNET_HashCode *my_identity_hash;
491 struct GNUNET_HashCode xor;
492 unsigned int bits;
493
494 my_identity_hash = GNUNET_PILS_get_identity_hash (GDS_pils);
495 GNUNET_assert (NULL != my_identity_hash);
496
498 my_identity_hash,
499 &xor);
501 if (bits == MAX_BUCKETS)
502 {
503 /* How can all bits match? Got my own ID? */
504 GNUNET_break (0);
505 return -1;
506 }
507 return bits;
508}
509
510
522 const struct GNUNET_PeerIdentity *key,
523 void *value)
524{
525 struct GNUNET_BLOCK_Group *bg = cls;
526 struct PeerInfo *pi = value;
527
529 &pi->phash,
530 1);
532 "Adding known peer (%s) to Bloom filter for FIND PEER\n",
533 GNUNET_i2s (key));
534 return GNUNET_YES;
535}
536
537
545static void
547{
548 (void) cls;
549
550 /* Compute when to do this again (and if we should
551 even send a message right now) */
552 {
553 struct GNUNET_TIME_Relative next_send_time;
554 bool done_early;
555
556 find_peer_task = NULL;
557 done_early = (newly_found_peers > bucket_size);
558 /* schedule next round, taking longer if we found more peers
559 in the last round. */
560 next_send_time.rel_value_us =
565 1 + 100 * (1 + newly_found_peers) / bucket_size).rel_value_us);
569 GNUNET_SCHEDULER_add_delayed (next_send_time,
571 NULL);
572 if (done_early)
573 return;
574 }
575
576 /* actually send 'find peer' request */
577 {
578 const struct GNUNET_HashCode *my_identity_hash;
579 struct GNUNET_BLOCK_Group *bg;
580 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
581
582 my_identity_hash = GNUNET_PILS_get_identity_hash (GDS_pils);
583 GNUNET_assert (NULL != my_identity_hash);
584
587 NULL,
588 0,
589 "seen-set-size",
592 NULL);
595 bg);
596 peer_bf
600 if (GNUNET_OK !=
605 0, /* hop count */
606 my_identity_hash,
607 NULL, 0, /* xquery */
608 bg,
609 peer_bf))
610 {
612 "# Failed to initiate FIND PEER lookup",
613 1,
614 GNUNET_NO);
615 }
616 else
617 {
619 "# FIND PEER messages initiated",
620 1,
621 GNUNET_NO);
622 }
625 }
626}
627
628
636static void
637update_hold (struct PeerBucket *bucket)
638{
639 unsigned int off = 0;
640
641 /* find the peer -- we just go over all of them, should
642 be hardly any more expensive than just finding the 'right'
643 one. */
644 for (struct PeerInfo *pos = bucket->head;
645 NULL != pos;
646 pos = pos->next)
647 {
648 if (off > bucket_size)
649 break; /* We only hold up to #bucket_size peers per bucket */
650 off++;
651 for (struct Target *tp = pos->t_head;
652 NULL != tp;
653 tp = tp->next)
654 if (NULL == tp->ph)
655 tp->ph = GDS_u_hold (tp->u,
656 tp->utarget);
657 }
658}
659
660
661void
662GDS_u_connect (void *cls,
663 struct GNUNET_DHTU_Target *target,
664 const struct GNUNET_PeerIdentity *pid,
665 void **ctx)
666{
667 const struct GNUNET_PeerIdentity *my_identity;
668 struct GDS_Underlay *u = cls;
669 struct PeerInfo *pi;
670 struct PeerBucket *bucket;
671 bool do_hold = false;
672
674 GNUNET_assert (NULL != my_identity);
675
676 /* Check for connect to self message */
677 if (0 == GNUNET_memcmp (my_identity, pid))
678 return;
680 "Connected to peer %s\n",
681 GNUNET_i2s (pid));
683 pid);
684 if (NULL == pi)
685 {
687 "# peers connected",
688 1,
689 GNUNET_NO);
690 pi = GNUNET_new (struct PeerInfo);
691 pi->id = *pid;
693 sizeof(*pid),
694 &pi->phash);
695 pi->peer_bucket = find_bucket (&pi->phash);
696 GNUNET_assert ( (pi->peer_bucket >= 0) &&
697 ((unsigned int) pi->peer_bucket < MAX_BUCKETS));
698 bucket = &k_buckets[pi->peer_bucket];
700 bucket->tail,
701 pi);
702 bucket->peers_size++;
704 (unsigned int) pi->peer_bucket + 1);
707 &pi->id,
708 pi,
710 if (bucket->peers_size <= bucket_size)
711 {
713 do_hold = true;
714 }
717 {
718 /* got a first connection, good time to start with FIND PEER requests... */
721 NULL);
722 }
723 /* A pending GET that found no route is sitting on an exponential
724 back-off of up to 15 minutes; now that we have somewhere to send
725 it, do so immediately. A PUT that found no route was not sent at
726 all and nothing else will ever send it. */
729 }
730 {
731 struct Target *t;
732
733 t = GNUNET_new (struct Target);
734 t->u = u;
735 t->utarget = target;
736 t->pi = pi;
738 pi->t_tail,
739 t);
740 *ctx = t;
741
742 }
743 if (do_hold)
744 update_hold (bucket);
745}
746
747
748void
750{
751 struct Target *t = ctx;
752 struct PeerInfo *pi;
753 struct PeerBucket *bucket;
754 bool was_held = false;
755
756 /* Check for disconnect from self message (on shutdown) */
757 if (NULL == t)
758 return;
759 pi = t->pi;
761 pi->t_tail,
762 t);
763 if (NULL != t->ph)
764 {
765 GDS_u_drop (t->u,
766 t->ph);
767 t->ph = NULL;
768 was_held = true;
769 }
770 if (t->load > 0)
771 {
772 t->dropped = true;
773 t->pi = NULL;
774 }
775 else
776 {
777 GNUNET_free (t);
778 }
779 if (NULL != pi->t_head)
780 return; /* got other connections still */
782 "Disconnected from peer %s\n",
783 GNUNET_i2s (&pi->id));
785 "# peers connected",
786 -1,
787 GNUNET_NO);
790 &pi->id,
791 pi));
794 {
796 find_peer_task = NULL;
797 }
798 GNUNET_assert (pi->peer_bucket >= 0);
799 bucket = &k_buckets[pi->peer_bucket];
801 bucket->tail,
802 pi);
803 GNUNET_assert (bucket->peers_size > 0);
804 bucket->peers_size--;
805 if ( (was_held) &&
806 (bucket->peers_size >= bucket_size - 1) )
807 update_hold (bucket);
808 while ( (closest_bucket > 0) &&
811 GNUNET_free (pi->hello);
812 GNUNET_free (pi);
813}
814
815
824static unsigned int
825get_forward_count (uint16_t hop_count,
826 uint16_t target_replication)
827{
828 uint32_t random_value;
829 uint32_t forward_count;
830 float target_value;
831 double rm1;
832 double nse = GDS_NSE_get ();
833
834 /* Guard the divisor below: an NSE of zero would make target_value
835 infinite (or NaN), and the conversion to uint32_t undefined. */
836 if (! (nse >= MINIMUM_LOG_NSE))
838 if (hop_count > nse * 4.0)
839 {
840 /* forcefully terminate */
842 "# requests TTL-dropped",
843 1,
844 GNUNET_NO);
845 return 0;
846 }
847 if (hop_count > nse * 2.0)
848 {
849 /* Once we have reached our ideal number of hops, only forward to 1 peer */
850 return 1;
851 }
852 /* bound by system-wide maximum and minimum */
853 if (0 == target_replication)
854 target_replication = 1; /* 0 is verboten */
855 target_replication =
857 target_replication);
858 rm1 = target_replication - 1.0;
859 target_value =
860 1 + (rm1) / (nse + (rm1 * hop_count));
861
862 /* Set forward count to floor of target_value */
863 forward_count = (uint32_t) target_value;
864 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
865 target_value = target_value - forward_count;
866 random_value = GNUNET_CRYPTO_random_u32 (UINT32_MAX);
867 if (random_value < (target_value * UINT32_MAX))
868 forward_count++;
869 return GNUNET_MIN (forward_count,
871}
872
873
886 const struct GNUNET_CONTAINER_BloomFilter *bloom)
887{
888 const struct GNUNET_HashCode *my_identity_hash;
889 int delta;
890 my_identity_hash = GNUNET_PILS_get_identity_hash (GDS_pils);
891 GNUNET_assert (NULL != my_identity_hash);
892 if (0 == GNUNET_memcmp (my_identity_hash, key))
893 return GNUNET_YES;
894 for (int bucket_num = find_bucket (key);
895 bucket_num < closest_bucket;
896 bucket_num++)
897 {
898 unsigned int count = 0;
899 GNUNET_assert (bucket_num >= 0);
900 for (struct PeerInfo *pos = k_buckets[bucket_num].head;
901 NULL != pos;
902 pos = pos->next)
903 {
904 if (count >= bucket_size)
905 break; /* we only consider first #bucket_size entries per bucket */
906 count++;
907 if ( (NULL != bloom) &&
908 (GNUNET_YES ==
910 &pos->phash)) )
911 continue; /* Ignore filtered peers */
912 /* All peers in this bucket must be closer than us, as
913 they mismatch with our PID on the pivotal bit. So
914 because an unfiltered peer exists, we are not the
915 closest. */
916 delta = GNUNET_CRYPTO_hash_xorcmp (&pos->phash,
917 my_identity_hash,
918 key);
919 switch (delta)
920 {
921 case -1: /* pos closer */
922 return GNUNET_NO;
923 case 0: /* identical, impossible! */
924 GNUNET_assert (0);
925 break;
926 case 1: /* I am closer */
927 break;
928 }
929 }
930 }
931 /* No closer (unfiltered) peers found; we must be the closest! */
932 return GNUNET_YES;
933}
934
935
957static struct PeerInfo *
959 const struct GNUNET_CONTAINER_BloomFilter *bloom,
960 uint32_t hops)
961{
962 if (0 == closest_bucket)
963 {
965 "# Peer selection failed",
966 1,
967 GNUNET_NO);
968 return NULL; /* we have zero connections */
969 }
970 if (hops >= GDS_NSE_get ())
971 {
972 /* greedy selection (closest peer that is not in Bloom filter) */
973 struct PeerInfo *chosen = NULL;
974 int best_bucket;
975 int bucket_offset;
976
977 {
978 const struct GNUNET_HashCode *my_identity_hash;
979 struct GNUNET_HashCode xor;
980 my_identity_hash = GNUNET_PILS_get_identity_hash (GDS_pils);
981 GNUNET_assert (NULL != my_identity_hash);
983 my_identity_hash,
984 &xor);
985 best_bucket = GNUNET_CRYPTO_hash_count_leading_zeros (&xor);
986 }
987 if (best_bucket >= closest_bucket)
988 bucket_offset = closest_bucket - 1;
989 else
990 bucket_offset = best_bucket;
991 while (-1 != bucket_offset)
992 {
993 struct PeerBucket *bucket = &k_buckets[bucket_offset];
994 unsigned int count = 0;
995
996 for (struct PeerInfo *pos = bucket->head;
997 NULL != pos;
998 pos = pos->next)
999 {
1000 if (count >= bucket_size)
1001 break; /* we only consider first #bucket_size entries per bucket */
1002 count++;
1003 if ( (NULL != bloom) &&
1004 (GNUNET_YES ==
1006 &pos->phash)) )
1007 {
1009 "Excluded peer `%s' due to BF match in greedy routing for %s\n",
1010 GNUNET_i2s (&pos->id),
1011 GNUNET_h2s (key));
1012 continue;
1013 }
1014 if (NULL == chosen)
1015 {
1016 /* First candidate */
1017 chosen = pos;
1018 }
1019 else
1020 {
1021 int delta = GNUNET_CRYPTO_hash_xorcmp (&pos->phash,
1022 &chosen->phash,
1023 key);
1024 switch (delta)
1025 {
1026 case -1: /* pos closer */
1027 chosen = pos;
1028 break;
1029 case 0: /* identical, impossible! */
1030 GNUNET_assert (0);
1031 break;
1032 case 1: /* chosen closer */
1033 break;
1034 }
1035 }
1036 } /* for all (#bucket_size) peers in bucket */
1037 if (NULL != chosen)
1038 break;
1039
1040 /* If we chose nothing in first iteration, first go through deeper
1041 buckets (best chance to find a good match), and if we still found
1042 nothing, then to shallower buckets. Terminate on any match in the
1043 current bucket, as this search order guarantees that it can only get
1044 worse as we keep going. */
1045 /* Both upward steps must be bounded by #closest_bucket, and the test
1046 has to be `>=': stepping from #best_bucket to #best_bucket + 1
1047 below can jump straight over an `==' test, after which nothing
1048 stops the offset from running off the end of #k_buckets. */
1049 if (bucket_offset > best_bucket)
1050 {
1051 /* Go through more deeper buckets */
1052 bucket_offset++;
1053 if (bucket_offset >= closest_bucket)
1054 {
1055 /* Can't go any deeper, if nothing selected,
1056 go for shallower buckets */
1057 bucket_offset = best_bucket - 1;
1058 }
1059 }
1060 else
1061 {
1062 /* We're either at the 'best_bucket' or already moving
1063 on to shallower buckets. */
1064 if (bucket_offset == best_bucket)
1065 {
1066 bucket_offset++; /* go for deeper buckets */
1067 if (bucket_offset >= closest_bucket)
1068 bucket_offset = best_bucket - 1;
1069 }
1070 else
1071 bucket_offset--; /* go for shallower buckets */
1072 }
1073 } /* for applicable buckets (starting at best match) */
1074 if (NULL == chosen)
1075 {
1077 "# Peer selection failed",
1078 1,
1079 GNUNET_NO);
1080 return NULL;
1081 }
1083 "Selected peer `%s' in greedy routing for %s\n",
1084 GNUNET_i2s (&chosen->id),
1085 GNUNET_h2s (key));
1086 return chosen;
1087 } /* end of 'greedy' peer selection */
1088
1089 /* select "random" peer */
1090 /* count number of peers that are available and not filtered,
1091 but limit to at most #bucket_size peers, starting with
1092 those 'furthest' from us. */
1093 {
1094 unsigned int total = 0;
1095 unsigned int selected;
1096
1097 for (unsigned int bc = 0; bc < closest_bucket; bc++)
1098 {
1099 struct PeerBucket *bucket = &k_buckets[bc];
1100 unsigned int count = 0;
1101
1102 for (struct PeerInfo *pos = bucket->head;
1103 NULL != pos;
1104 pos = pos->next)
1105 {
1106 count++;
1107 if (count > bucket_size)
1108 break; /* limits search to #bucket_size peers per bucket */
1109 if ( (NULL != bloom) &&
1110 (GNUNET_YES ==
1112 &pos->phash)) )
1113 {
1115 "Excluded peer `%s' due to BF match in random routing for %s\n",
1116 GNUNET_i2s (&pos->id),
1117 GNUNET_h2s (key));
1118 continue; /* Ignore filtered peers */
1119 }
1120 total++;
1121 } /* for all peers in bucket */
1122 } /* for all buckets */
1123 if (0 == total) /* No peers to select from! */
1124 {
1126 "# Peer selection failed",
1127 1,
1128 GNUNET_NO);
1129 return NULL;
1130 }
1131
1132 /* Now actually choose a peer */
1133 selected = GNUNET_CRYPTO_random_u32 (total);
1134 for (unsigned int bc = 0; bc < closest_bucket; bc++)
1135 {
1136 unsigned int count = 0;
1137
1138 for (struct PeerInfo *pos = k_buckets[bc].head;
1139 pos != NULL;
1140 pos = pos->next)
1141 {
1142 count++;
1143 if (count > bucket_size)
1144 break; /* limits search to #bucket_size peers per bucket */
1145
1146 if ( (NULL != bloom) &&
1147 (GNUNET_YES ==
1149 &pos->phash)) )
1150 continue; /* Ignore bloomfiltered peers */
1151 if (0 == selected--)
1152 {
1154 "Selected peer `%s' in random routing for %s\n",
1155 GNUNET_i2s (&pos->id),
1156 GNUNET_h2s (key));
1157 return pos;
1158 }
1159 } /* for peers in bucket */
1160 } /* for all buckets */
1161 } /* random peer selection scope */
1162 GNUNET_break (0);
1163 return NULL;
1164}
1165
1166
1180static unsigned int
1182 struct GNUNET_CONTAINER_BloomFilter *bloom,
1183 uint16_t hop_count,
1184 uint16_t target_replication,
1185 struct PeerInfo ***targets)
1186{
1187 unsigned int target;
1188 unsigned int off;
1189 struct PeerInfo **rtargets;
1190
1191 GNUNET_assert (NULL != bloom);
1192 target = get_forward_count (hop_count,
1193 target_replication);
1194 if (0 == target)
1195 {
1196 *targets = NULL;
1197 return 0;
1198 }
1199 rtargets = GNUNET_new_array (target,
1200 struct PeerInfo *);
1201 for (off = 0; off < target; off++)
1202 {
1203 struct PeerInfo *nxt;
1204
1205 nxt = select_peer (key,
1206 bloom,
1207 hop_count);
1208 if (NULL == nxt)
1209 break;
1210 rtargets[off] = nxt;
1211 /* Exclude the peer we just picked from the next round, or
1212 select_peer() would hand us the same peer again. */
1215 &nxt->phash));
1217 &nxt->phash);
1218 }
1220 "Selected %u/%u peers at hop %u for %s (target was %u)\n",
1221 off,
1223 (unsigned int) hop_count,
1224 GNUNET_h2s (key),
1225 target);
1226 if (0 == off)
1227 {
1228 GNUNET_free (rtargets);
1229 *targets = NULL;
1230 return 0;
1231 }
1232 *targets = rtargets;
1234 "Forwarding query `%s' to %u peers (goal was %u peers)\n",
1235 GNUNET_h2s (key),
1236 off,
1237 target);
1238 return off;
1239}
1240
1241
1247static void
1249{
1250 struct GNUNET_HELLO_Parser *b;
1251
1253 return;
1254
1256 bd->data_size);
1258 {
1261 NULL);
1262 }
1264}
1265
1266
1268{
1269 unsigned int hop_count;
1270 unsigned int target_count;
1273 unsigned int index;
1274 unsigned int *queued;
1279 bool *sent;
1281 void *cb_cls;
1282};
1283
1284
1285static bool
1287 size_t msize,
1288 struct PeerPutMessage *ppm)
1289{
1290 struct GDS_RoutingPutCallbackData *gds_routing = cls;
1291 struct PeerInfo *target;
1292
1293 if (NULL == ppm)
1294 {
1295 *(gds_routing->queued) = *(gds_routing->queued) + 1;
1296 if (*(gds_routing->queued) >= gds_routing->target_count)
1297 {
1298 if (gds_routing->cb)
1299 gds_routing->cb (gds_routing->cb_cls,
1300 *(gds_routing->sent)
1301 ? GNUNET_OK
1302 : GNUNET_SYSERR);
1303
1304 GNUNET_free (gds_routing->targets);
1305 GNUNET_free (gds_routing->queued);
1306 GNUNET_free (gds_routing->sent);
1307 }
1308
1309 return true;
1310 }
1311
1312 target = gds_routing->targets[gds_routing->index];
1313
1315 "Routing PUT for %s after %u hops to %s\n",
1316 GNUNET_h2s (&(gds_routing->key)),
1317 (unsigned int) gds_routing->hop_count,
1318 GNUNET_i2s (&target->id));
1319 if (do_send (target,
1320 &ppm->header))
1321 *(gds_routing->sent) = true;
1322 *(gds_routing->queued) = *(gds_routing->queued) + 1;
1323
1324 if (*(gds_routing->queued) >= gds_routing->target_count)
1325 {
1326 /* Report success only if some underlay actually took the message:
1327 otherwise nothing stores this block and the caller has to keep
1328 it and try again. */
1329 if (gds_routing->cb)
1330 gds_routing->cb (gds_routing->cb_cls,
1331 *(gds_routing->sent)
1332 ? GNUNET_OK
1333 : GNUNET_NO);
1334
1335 GNUNET_free (gds_routing->targets);
1337 "# PUT messages queued for transmission",
1338 gds_routing->target_count,
1339 GNUNET_NO);
1340 GNUNET_free (gds_routing->queued);
1341 GNUNET_free (gds_routing->sent);
1342 }
1343
1344 return true;
1345}
1346
1347
1348void
1350 uint16_t desired_replication_level,
1351 uint16_t hop_count,
1354 void *cb_cls)
1355{
1356 const struct GNUNET_PeerIdentity *my_identity;
1357 const struct GNUNET_HashCode *my_identity_hash;
1358 struct GDS_RoutingPutCallbackData gds_routing;
1359 size_t msize;
1360 enum GNUNET_DHT_RouteOption ro = bd->ro;
1361 unsigned int put_path_length = bd->put_path_length;
1362 const struct GNUNET_DHT_PathElement *put_path = bd->put_path;
1363 bool truncated = (0 != (bd->ro & GNUNET_DHT_RO_TRUNCATED));
1364 const struct GNUNET_PeerIdentity *trunc_peer
1365 = truncated
1366 ? &bd->trunc_peer
1367 : NULL;
1368 struct GNUNET_PeerIdentity trunc_peer_out;
1370
1372 my_identity_hash = GNUNET_PILS_get_identity_hash (GDS_pils);
1373 GNUNET_assert (NULL != my_identity);
1374
1377 bd->ro, &ro,
1378 bd->expiration_time,
1379 bd->data, bd->data_size,
1380 put_path, put_path_length,
1381 &put_path_length,
1382 trunc_peer,
1383 &trunc_peer_out,
1384 &truncated);
1385 if (truncated)
1386 trunc_peer = &trunc_peer_out;
1387 /* Path may have been truncated by the call above */
1389 "Adding myself (%s) to PUT bloomfilter for %s with RO(%s/%s)\n",
1391 GNUNET_h2s (&bd->key),
1392 (bd->ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE) ? "x" : "-",
1393 (bd->ro & GNUNET_DHT_RO_RECORD_ROUTE) ? "R" : "-");
1394
1395 /* if we got a HELLO, consider it for our own routing table */
1396 hello_check (bd);
1397 GNUNET_assert ((NULL != bf) && (NULL != my_identity_hash));
1398 GNUNET_CONTAINER_bloomfilter_add (bf, my_identity_hash);
1400 "# PUT requests routed",
1401 1,
1402 GNUNET_NO);
1403 if (GNUNET_OK != ret)
1404 {
1405 if (cb)
1406 cb (cb_cls, ret);
1407 return;
1408 }
1409 gds_routing.target_count
1410 = get_target_peers (&bd->key,
1411 bf,
1412 hop_count,
1413 desired_replication_level,
1414 &(gds_routing.targets));
1415 if (0 == gds_routing.target_count)
1416 {
1418 "Routing PUT for %s terminates after %u hops at %s\n",
1419 GNUNET_h2s (&bd->key),
1420 (unsigned int) hop_count,
1422 if (cb)
1423 cb (cb_cls, GNUNET_NO);
1424 if (gds_routing.targets)
1425 GNUNET_free (gds_routing.targets);
1426 return;
1427 }
1428 GNUNET_memcpy (&(gds_routing.key), &(bd->key),
1429 sizeof (gds_routing.key));
1430 gds_routing.hop_count = hop_count;
1431 /* targets were added to @a bf by get_target_peers() */
1432
1433 gds_routing.queued = GNUNET_new (unsigned int);
1434 *(gds_routing.queued) = 0;
1435 gds_routing.sent = GNUNET_new (bool);
1436 *(gds_routing.sent) = false;
1437
1438 gds_routing.cb = cb;
1439 gds_routing.cb_cls = cb_cls;
1440
1441 for (unsigned int i = 0; i < gds_routing.target_count; i++)
1442 {
1443 struct PeerInfo *target = gds_routing.targets[i];
1444 struct PeerPutMessage *ppm;
1445 char buf[msize] GNUNET_ALIGN;
1446
1447 gds_routing.index = i;
1448
1449 ppm = (struct PeerPutMessage *) buf;
1450 GDS_helper_make_put_message (ppm, msize,
1452 &target->id,
1453 &target->phash,
1454 bf,
1455 &bd->key,
1456 ro,
1457 bd->type,
1458 bd->expiration_time,
1459 bd->data, bd->data_size,
1460 put_path, put_path_length,
1461 hop_count,
1463 trunc_peer,
1465 sizeof (gds_routing),
1466 &gds_routing);
1467 }
1468}
1469
1470
1475 uint16_t hop_count,
1476 const struct GNUNET_HashCode *key,
1477 const void *xquery,
1478 size_t xquery_size,
1479 struct GNUNET_BLOCK_Group *bg,
1480 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1481{
1482 const struct GNUNET_PeerIdentity *my_identity;
1483 const struct GNUNET_HashCode *my_identity_hash;
1484 unsigned int target_count;
1485 struct PeerInfo **targets;
1486 size_t msize;
1487 size_t result_filter_size;
1488 void *result_filter;
1489 bool sent = false;
1490
1492 my_identity_hash = GNUNET_PILS_get_identity_hash (GDS_pils);
1493
1494 if (NULL == my_identity_hash)
1495 return GNUNET_NO;
1496
1497 GNUNET_assert (NULL != peer_bf);
1499 "# GET requests routed",
1500 1,
1501 GNUNET_NO);
1502 target_count = get_target_peers (key,
1503 peer_bf,
1504 hop_count,
1505 desired_replication_level,
1506 &targets);
1508 "Adding myself (%s) to GET bloomfilter for %s with RO(%s/%s)\n",
1510 GNUNET_h2s (key),
1512 (options & GNUNET_DHT_RO_RECORD_ROUTE) ? "R" : "-");
1513 GNUNET_assert (NULL != my_identity_hash);
1514 GNUNET_CONTAINER_bloomfilter_add (peer_bf, my_identity_hash);
1515 if (0 == target_count)
1516 {
1518 "Routing GET for %s terminates after %u hops at %s\n",
1519 GNUNET_h2s (key),
1520 (unsigned int) hop_count,
1522 return GNUNET_NO;
1523 }
1524 if (GNUNET_OK !=
1526 &result_filter,
1527 &result_filter_size))
1528 {
1529 result_filter = NULL;
1530 result_filter_size = 0;
1531 }
1532 msize = xquery_size + result_filter_size;
1533 if (msize + sizeof(struct PeerGetMessage) >= GNUNET_MAX_MESSAGE_SIZE)
1534 {
1535 GNUNET_break (0);
1536 GNUNET_free (result_filter);
1537 GNUNET_free (targets);
1538 return GNUNET_NO;
1539 }
1540 /* targets were added to @a peer_bf by get_target_peers() */
1541 /* forward request */
1542 for (unsigned int i = 0; i < target_count; i++)
1543 {
1544 struct PeerInfo *target = targets[i];
1545 struct PeerGetMessage *pgm;
1546 char buf[sizeof (*pgm) + msize] GNUNET_ALIGN;
1547 char *rf;
1548
1550 "Routing GET for %s after %u hops to %s\n",
1551 GNUNET_h2s (key),
1552 (unsigned int) hop_count,
1553 GNUNET_i2s (&target->id));
1554 pgm = (struct PeerGetMessage *) buf;
1556 pgm->header.size = htons (sizeof (buf));
1557 pgm->type = htonl (type);
1558 pgm->options = htons (options);
1559 pgm->hop_count = htons (hop_count + 1);
1561 pgm->result_filter_size = htons ((uint16_t) result_filter_size);
1564 pgm->bloomfilter,
1566 pgm->key = *key;
1567 rf = (char *) &pgm[1];
1568 GNUNET_memcpy (rf,
1569 result_filter,
1572 xquery,
1573 xquery_size);
1574 if (do_send (target,
1575 &pgm->header))
1576 sent = true;
1577 }
1579 "# GET messages queued for transmission",
1580 target_count,
1581 GNUNET_NO);
1582 GNUNET_free (targets);
1583 GNUNET_free (result_filter);
1584 /* Having picked a target is not the same as having sent to one: if
1585 every underlay dropped the message, the request was not routed and
1586 must go on the fast retry, not on the regular back-off. */
1587 return sent ? GNUNET_OK : GNUNET_NO;
1588}
1589
1590
1591struct PeerInfo *
1597
1598
1614
1615
1616static void
1618{
1619 if (reply->block_data)
1620 GNUNET_free (reply->block_data);
1621 if ((reply->bd.put_path_length > 0) && (reply->put_path))
1622 GNUNET_free (reply->put_path);
1623 if (reply->buf)
1624 GNUNET_free (reply->buf);
1625}
1626
1627
1628static void
1631 bool success)
1632{
1633 GNUNET_break (success);
1634 if (cb)
1635 cb (cls);
1636}
1637
1638
1639static bool
1641 const struct GNUNET_CRYPTO_EddsaSignature *sig)
1642{
1643 struct GDS_NeighboursReply *reply = cls;
1644 struct PeerResultMessage *prm = reply->prm;
1645 struct GNUNET_DHT_PathElement *paths = reply->paths;
1646 unsigned int ppl = ntohs (prm->put_path_length);
1647 unsigned int get_path_length = ntohs (prm->get_path_length);
1648 void *tgt = &paths[get_path_length + ppl];
1649 void *data;
1650
1651 if (! sig)
1652 {
1654 safe_neighbours_callback (reply->cb_cls, reply->cb, false);
1655 return true;
1656 }
1657
1658 memcpy (tgt,
1659 sig,
1660 sizeof (*sig));
1661 data = tgt + sizeof (*sig);
1663 "Signing GET PATH %u/%u of %s => %s\n",
1664 ppl,
1665 get_path_length,
1666 GNUNET_h2s (&prm->key),
1667 GNUNET_B2S (sig));
1668#if SANITY_CHECKS > 1
1669 {
1670 const struct GNUNET_PeerIdentity *my_identity;
1671 struct GNUNET_DHT_PathElement xpaths[get_path_length + 1];
1672 const struct GNUNET_PeerIdentity *trunc_peer = reply->trunc_peer_is_null?
1673 NULL : &reply->trunc_peer_id;
1674
1676 GNUNET_assert (NULL != my_identity);
1677
1678 memcpy (xpaths,
1679 &paths[ppl],
1680 get_path_length * sizeof (struct GNUNET_DHT_PathElement));
1681 xpaths[get_path_length].sig = *sig;
1682 xpaths[get_path_length].pred = *my_identity;
1683 if (0 !=
1685 reply->bd.data_size,
1686 reply->bd.expiration_time,
1687 trunc_peer,
1688 paths,
1689 ppl,
1690 xpaths,
1691 get_path_length + 1,
1692 &reply->pi->id))
1693 {
1694 GNUNET_break (0);
1696 safe_neighbours_callback (reply->cb_cls, reply->cb, false);
1697 return true;
1698 }
1699 }
1700#endif
1702 reply->bd.data,
1703 reply->bd.data_size);
1704 do_send (reply->pi,
1705 &prm->header);
1707 safe_neighbours_callback (reply->cb_cls, reply->cb, true);
1708 return true;
1709}
1710
1711
1712void
1714 const struct GNUNET_DATACACHE_Block *bd,
1715 const struct GNUNET_HashCode *query_hash,
1716 unsigned int get_path_length,
1717 const struct GNUNET_DHT_PathElement *get_path,
1719 void *cb_cls)
1720{
1721 struct GNUNET_DHT_PathElement *paths;
1722 size_t msize;
1723 unsigned int ppl = bd->put_path_length;
1724 const struct GNUNET_DHT_PathElement *put_path = bd->put_path;
1725 enum GNUNET_DHT_RouteOption ro = bd->ro;
1726 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
1727 const struct GNUNET_PeerIdentity *trunc_peer
1728 = truncated
1729 ? &bd->trunc_peer
1730 : NULL;
1731 bool tracking = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
1732#if SANITY_CHECKS > 1
1733 const struct GNUNET_PeerIdentity *my_identity;
1734 unsigned int failure_offset;
1735
1737 GNUNET_assert (NULL != my_identity);
1738
1739 failure_offset
1741 bd->data_size,
1742 bd->expiration_time,
1743 trunc_peer,
1744 put_path,
1745 ppl,
1746 get_path,
1747 get_path_length,
1748 my_identity);
1749 if (0 != failure_offset)
1750 {
1751 GNUNET_assert (failure_offset <= ppl + get_path_length);
1752 GNUNET_break_op (0);
1753 if (failure_offset < ppl)
1754 {
1755 trunc_peer = &put_path[failure_offset - 1].pred;
1756 put_path += failure_offset;
1757 ppl -= failure_offset;
1758 truncated = true;
1760 }
1761 else
1762 {
1763 failure_offset -= ppl;
1764 if (0 == failure_offset)
1765 trunc_peer = &put_path[ppl - 1].pred;
1766 else
1767 trunc_peer = &get_path[failure_offset - 1].pred;
1768 ppl = 0;
1769 put_path = NULL;
1770 truncated = true;
1772 get_path += failure_offset;
1773 get_path_length -= failure_offset;
1774 }
1775 }
1776#endif
1777 msize = bd->data_size + sizeof (struct PeerResultMessage);
1778 if (msize > GNUNET_MAX_MESSAGE_SIZE)
1779 {
1780 GNUNET_break_op (0);
1781 safe_neighbours_callback (cb_cls, cb, false);
1782 return;
1783 }
1784 if (truncated)
1785 msize += sizeof (struct GNUNET_PeerIdentity);
1786 if (tracking)
1787 msize += sizeof (struct GNUNET_CRYPTO_EddsaSignature);
1788 if (msize < bd->data_size)
1789 {
1790 GNUNET_break_op (0);
1791 safe_neighbours_callback (cb_cls, cb, false);
1792 return;
1793 }
1794 if ( (GNUNET_MAX_MESSAGE_SIZE - msize)
1795 / sizeof(struct GNUNET_DHT_PathElement)
1796 < (get_path_length + ppl) )
1797 {
1798 get_path_length = 0;
1799 ppl = 0;
1800 }
1801 if ( (get_path_length > UINT16_MAX) ||
1802 (ppl > UINT16_MAX) )
1803 {
1804 GNUNET_break (0);
1805 get_path_length = 0;
1806 ppl = 0;
1807 }
1808 msize += (get_path_length + ppl)
1809 * sizeof(struct GNUNET_DHT_PathElement);
1811 "Forwarding reply for key %s to peer %s\n",
1812 GNUNET_h2s (query_hash),
1813 GNUNET_i2s (&pi->id));
1815 "# RESULT messages queued for transmission",
1816 1,
1817 GNUNET_NO);
1818 {
1819 struct PeerResultMessage *prm;
1820 char buf[msize] GNUNET_ALIGN;
1821
1822 prm = (struct PeerResultMessage *) buf;
1824 prm->header.size = htons (sizeof (buf));
1825 prm->type = htonl ((uint32_t) bd->type);
1826 prm->reserved = htons (0);
1827 prm->options = htons ((uint16_t) ro);
1828 prm->put_path_length = htons ((uint16_t) ppl);
1829 prm->get_path_length = htons ((uint16_t) get_path_length);
1831 prm->key = *query_hash;
1832 if (truncated)
1833 {
1834 void *tgt = &prm[1];
1835
1836 GNUNET_memcpy (tgt,
1837 trunc_peer,
1838 sizeof (struct GNUNET_PeerIdentity));
1839 paths = (struct GNUNET_DHT_PathElement *)
1840 (tgt + sizeof (struct GNUNET_PeerIdentity));
1841 }
1842 else
1843 {
1844 paths = (struct GNUNET_DHT_PathElement *) &prm[1];
1845 }
1846 if (NULL != put_path)
1847 {
1848 GNUNET_memcpy (paths,
1849 put_path,
1850 ppl * sizeof(struct GNUNET_DHT_PathElement));
1851 }
1852 else
1853 {
1854 GNUNET_assert (0 == ppl);
1855 }
1856 if (NULL != get_path)
1857 {
1858 GNUNET_memcpy (&paths[ppl],
1859 get_path,
1860 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
1861 }
1862 else
1863 {
1864 GNUNET_assert (0 == get_path_length);
1865 }
1866 if (tracking)
1867 {
1868 struct GDS_NeighboursReply reply;
1869 const struct GNUNET_PeerIdentity *pred;
1870
1871 reply.pi = pi;
1872 GNUNET_memcpy (&reply.bd, bd, sizeof (reply.bd));
1873 reply.block_data = GNUNET_memdup (bd->data, bd->data_size);
1874 reply.put_path = GNUNET_memdup (bd->put_path,
1875 sizeof (struct GNUNET_DHT_PathElement)
1876 * bd->put_path_length);
1877
1878 reply.bd.data = reply.block_data;
1879 reply.bd.put_path = reply.put_path;
1880
1881 reply.buf = GNUNET_memdup (buf, msize);
1882 reply.prm = (struct PeerResultMessage*) reply.buf;
1883 reply.paths = (struct GNUNET_DHT_PathElement*)
1884 (reply.buf + ((const char *) paths - buf));
1885
1886 if (trunc_peer)
1887 {
1888 reply.trunc_peer_is_null = false;
1889 GNUNET_memcpy (&reply.trunc_peer_id, trunc_peer,
1890 sizeof (reply.trunc_peer_id));
1891 }
1892 else
1893 {
1894 reply.trunc_peer_is_null = true;
1895 }
1896
1897 reply.cb = cb;
1898 reply.cb_cls = cb_cls;
1899
1900 if (ppl + get_path_length > 0)
1901 pred = &paths[ppl + get_path_length - 1].pred;
1902 else if (truncated)
1903 pred = trunc_peer;
1904 else
1905 pred = NULL; /* we are first! */
1906 /* Note that the last signature in 'paths' was not initialized before,
1907 so this is crucial to avoid sending garbage. */
1909 bd->data_size,
1911 bd->expiration_time,
1912 pred,
1913 &pi->id,
1915 sizeof (reply),
1916 &reply);
1917 }
1918 else
1919 {
1920 void *data;
1921 data = &prm[1];
1923 bd->data,
1924 bd->data_size);
1925 do_send (pi,
1926 &prm->header);
1927 safe_neighbours_callback (cb_cls, cb, true);
1928 return;
1929 }
1930 }
1931}
1932
1933
1941static enum GNUNET_GenericReturnValue
1943 const struct PeerPutMessage *put)
1944{
1945 enum GNUNET_DHT_RouteOption ro = ntohs (put->options);
1946 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
1947 bool has_path = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
1948 uint16_t msize = ntohs (put->header.size);
1949 uint16_t putlen = ntohs (put->put_path_length);
1950 size_t xsize = (has_path
1951 ? sizeof (struct GNUNET_CRYPTO_EddsaSignature)
1952 : 0)
1953 + (truncated
1954 ? sizeof (struct GNUNET_PeerIdentity)
1955 : 0);
1956 size_t var_meta_size
1957 = putlen * sizeof(struct GNUNET_DHT_PathElement)
1958 + xsize;
1959
1960 (void) cls;
1961 if ( (msize <
1962 sizeof (struct PeerPutMessage) + var_meta_size) ||
1963 (putlen >
1964 (GNUNET_MAX_MESSAGE_SIZE
1965 - sizeof (struct PeerPutMessage)
1966 - xsize)
1967 / sizeof(struct GNUNET_DHT_PathElement)) )
1968 {
1969 GNUNET_break_op (0);
1970 return GNUNET_SYSERR;
1971 }
1972 if (GNUNET_BLOCK_TYPE_ANY == htonl (put->type))
1973 {
1974 GNUNET_break_op (0);
1975 return GNUNET_SYSERR;
1976 }
1977 return GNUNET_OK;
1978}
1979
1980
1991
1992
1993static void
1995 enum GNUNET_GenericReturnValue forwarded)
1996{
1997 struct ForwardedDHTPut *put = cls;
1998
1999 /* notify monitoring clients; we are the last hop exactly when we did
2000 not manage to forward, as in the GET case */
2001 put->block.ro |= ((GNUNET_OK == forwarded)
2002 ? 0
2005 put->hop_count,
2007
2008 if (put->put_path)
2009 GNUNET_free (put->put_path);
2010 GNUNET_free (put->data);
2011 GNUNET_free (put);
2012}
2013
2014
2021static void
2023 const struct PeerPutMessage *put)
2024{
2025 struct Target *t = cls;
2026 struct PeerInfo *peer = t->pi;
2027 enum GNUNET_DHT_RouteOption ro = ntohs (put->options);
2028 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
2029 bool has_path = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
2030 uint16_t msize = ntohs (put->header.size);
2031 uint16_t putlen = ntohs (put->put_path_length);
2032 const struct GNUNET_PeerIdentity *trunc_peer
2033 = truncated
2034 ? (const struct GNUNET_PeerIdentity *) &put[1]
2035 : NULL;
2036 const struct GNUNET_DHT_PathElement *put_path
2037 = truncated
2038 ? (const struct GNUNET_DHT_PathElement *) &trunc_peer[1]
2039 : (const struct GNUNET_DHT_PathElement *) &put[1];
2040 const struct GNUNET_CRYPTO_EddsaSignature *last_sig
2041 = has_path
2042 ? (const struct GNUNET_CRYPTO_EddsaSignature *) &put_path[putlen]
2043 : NULL;
2044 const char *data
2045 = has_path
2046 ? (const char *) &last_sig[1]
2047 : (const char *) &put_path[putlen];
2048 size_t var_meta_size
2049 = putlen * sizeof(struct GNUNET_DHT_PathElement)
2050 + (has_path ? sizeof (*last_sig) : 0)
2051 + (truncated ? sizeof (*trunc_peer) : 0);
2052 struct GNUNET_DATACACHE_Block bd = {
2053 .key = put->key,
2054 .expiration_time = GNUNET_TIME_absolute_ntoh (put->expiration_time),
2055 .type = ntohl (put->type),
2056 .ro = ro,
2057 .data_size = msize - sizeof(*put) - var_meta_size,
2058 .data = data
2059 };
2060
2061 if (NULL != trunc_peer)
2062 bd.trunc_peer = *trunc_peer;
2064 "PUT for `%s' from %s with RO (%s/%s)\n",
2065 GNUNET_h2s (&put->key),
2066 GNUNET_i2s (&peer->id),
2067 (bd.ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE) ? "x" : "-",
2068 has_path ? "R" : "-");
2070 {
2072 "# Expired PUTs discarded",
2073 1,
2074 GNUNET_NO);
2075 return;
2076 }
2077 {
2078 /* Only call 'check_block' if that keeps our CPU load (from
2079 the cryptography) below 50% on average */
2080 static struct GNUNET_TIME_Relative avg_latency;
2081 static struct GNUNET_TIME_Absolute next_time;
2082
2083 if (GNUNET_TIME_absolute_is_past (next_time))
2084 {
2085 struct GNUNET_TIME_Absolute now
2087 struct GNUNET_TIME_Relative latency;
2089
2090 if (GNUNET_NO ==
2092 bd.type,
2093 bd.data,
2094 bd.data_size))
2095 {
2096 GNUNET_break_op (0);
2097 return;
2098 }
2099 latency = GNUNET_TIME_absolute_get_duration (now);
2100 /* Use *moving average* to estimate check_block latency */
2101 avg_latency
2104 GNUNET_TIME_relative_multiply (avg_latency,
2105 7),
2106 latency),
2107 8);
2108 /* average delay = 50% of avg_latency => 50% CPU load from crypto (at most) */
2110 = GNUNET_CRYPTO_random_u64 (avg_latency.rel_value_us > 0
2111 ? avg_latency.rel_value_us
2112 : 1LLU);
2114 }
2115 }
2116 if (! has_path)
2117 putlen = 0;
2119 "# P2P PUT requests received",
2120 1,
2121 GNUNET_NO);
2123 "# P2P PUT bytes received",
2124 msize,
2125 GNUNET_NO);
2126 {
2127 struct GNUNET_HashCode test_key;
2129
2131 bd.type,
2132 bd.data,
2133 bd.data_size,
2134 &test_key);
2135 switch (ret)
2136 {
2137 case GNUNET_YES:
2138 if (0 != GNUNET_memcmp (&test_key,
2139 &bd.key))
2140 {
2141 GNUNET_break_op (0);
2142 return;
2143 }
2144 break;
2145 case GNUNET_NO:
2146 /* cannot verify, good luck */
2147 break;
2148 case GNUNET_SYSERR:
2149 /* block type not supported, good luck */
2150 break;
2151 }
2152 }
2153
2154 {
2156 struct GNUNET_DHT_PathElement pp[putlen + 1];
2157
2163 &peer->phash));
2164 /* extend 'put path' by sender */
2165 bd.put_path = pp;
2166 bd.put_path_length = putlen + 1;
2167 if (has_path)
2168 {
2169 unsigned int failure_offset;
2170
2171 GNUNET_memcpy (pp,
2172 put_path,
2173 putlen * sizeof(struct GNUNET_DHT_PathElement));
2174 pp[putlen].pred = peer->id;
2175 pp[putlen].sig = *last_sig;
2176#if SANITY_CHECKS
2177 {
2178 const struct GNUNET_PeerIdentity *my_identity;
2180 GNUNET_assert (NULL != my_identity);
2181 /* TODO: might want to eventually implement probabilistic
2182 load-based path verification, but for now it is all or nothing */
2183 failure_offset
2185 bd.data_size,
2186 bd.expiration_time,
2187 trunc_peer,
2188 pp,
2189 putlen + 1,
2190 NULL, 0, /* get_path */
2191 my_identity);
2192 }
2193#else
2194 failure_offset = 0;
2195#endif
2196 if (0 != failure_offset)
2197 {
2198 GNUNET_break_op (0);
2200 "Recorded put path invalid at offset %u, truncating\n",
2201 failure_offset);
2202 GNUNET_assert (failure_offset <= putlen + 1);
2203 bd.put_path = &pp[failure_offset];
2204 bd.put_path_length = (putlen + 1) - failure_offset;
2206 bd.trunc_peer = pp[failure_offset - 1].pred;
2207 }
2208 }
2209 else
2210 {
2211 bd.put_path_length = 0;
2212 }
2213
2214 /* give to local clients */
2216 &bd.key,
2217 0, NULL /* get path */));
2218
2219 /* store locally */
2220 if ( (0 != (bd.ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
2221 (GDS_am_closest_peer (&put->key,
2222 bf)) )
2224
2225 {
2227 GNUNET_memcpy (&forward->block, &bd, sizeof (bd));
2228
2229 if (bd.put_path_length > 0)
2230 {
2231 forward->put_path = GNUNET_memdup (
2232 bd.put_path,
2233 sizeof (struct GNUNET_DHT_PathElement) * bd.put_path_length);
2234 forward->block.put_path = forward->put_path;
2235 }
2236
2237 forward->data = GNUNET_memdup (bd.data, bd.data_size);
2238 forward->block.data = forward->data;
2239
2240 forward->desired_replication_level = ntohs (put->desired_replication_level
2241 );
2242 forward->hop_count = ntohs (put->hop_count);
2243
2244 /* route to other peers */
2246 forward->desired_replication_level,
2247 forward->hop_count,
2248 bf,
2250 forward);
2251 }
2253 }
2254}
2255
2256
2258{
2259 struct PeerInfo *pi;
2262};
2263
2264
2273static void
2275 const struct GNUNET_HashCode *query_hash,
2276 struct GNUNET_BLOCK_Group *bg,
2278 void *cb_cls)
2279{
2280 const struct GNUNET_HashCode *my_identity_hash;
2281 const struct GNUNET_PeerIdentity *my_identity;
2282 struct GNUNET_TIME_Absolute block_expiration;
2283 size_t block_size;
2284 void *block;
2285
2286 my_identity_hash = GNUNET_PILS_get_identity_hash (GDS_pils);
2289 "Handle finding my own HELLO %s\n",
2290 GNUNET_h2s (my_identity_hash));
2291 if (NULL == GDS_my_hello)
2292 {
2294 "# FIND PEER requests ignored due to lack of HELLO",
2295 1,
2296 GNUNET_NO);
2297 if (cb)
2298 cb (cb_cls);
2299 return;
2300 }
2301
2304 &block,
2305 &block_size,
2306 &block_expiration))
2307 {
2308 if (cb)
2309 cb (cb_cls);
2310 return;
2311 }
2312
2316 bg,
2317 my_identity_hash,
2318 NULL, 0,
2319 block,
2320 block_size))
2321 {
2322 struct GNUNET_DATACACHE_Block bd = {
2324 .expiration_time
2327 .key = *my_identity_hash,
2328 .data = block,
2329 .data_size = block_size
2330 };
2331
2333 &bd,
2334 query_hash,
2335 0, NULL /* get path */,
2336 cb,
2337 cb_cls);
2338 }
2339 else
2340 {
2342 "# FIND PEER requests ignored due to Bloomfilter",
2343 1,
2344 GNUNET_NO);
2345 if (cb)
2346 cb (cb_cls);
2347 }
2348
2349 GNUNET_free (block);
2350}
2351
2352
2361static void
2363 const struct GNUNET_HashCode *query_hash,
2364 struct GNUNET_BLOCK_Group *bg,
2366 void *cb_cls)
2367{
2368 /* Force non-random selection by hop count */
2369 struct PeerInfo *peer;
2370
2371 peer = select_peer (query_hash,
2372 NULL,
2373 GDS_NSE_get () + 1);
2374 if (NULL == peer)
2375 {
2376 /* no neighbour at all, nothing to answer with */
2377 if (cb)
2378 cb (cb_cls);
2379 return;
2380 }
2382 "Handle finding local HELLO %s\n",
2383 GNUNET_h2s (&peer->phash));
2384 if ( (NULL != peer->hello) &&
2390 bg,
2391 &peer->phash,
2392 NULL, 0, /* xquery */
2393 peer->hello,
2394 peer->hello_size)) )
2395 {
2396 struct GNUNET_DATACACHE_Block bd = {
2398 .expiration_time = peer->hello_expiration,
2399 .key = peer->phash,
2400 .data = peer->hello,
2401 .data_size = peer->hello_size
2402 };
2403
2405 &bd,
2406 query_hash,
2407 0, NULL /* get path */,
2408 cb,
2409 cb_cls);
2410 }
2411 else if (cb)
2412 cb (cb_cls);
2413}
2414
2415
2422static void
2424 const struct GNUNET_DATACACHE_Block *bd)
2425{
2426 struct PeerInfo *peer = cls;
2427
2429 bd,
2430 &bd->key,
2431 0, NULL /* get path */,
2432 NULL,
2433 NULL);
2434}
2435
2436
2444static enum GNUNET_GenericReturnValue
2446 const struct PeerGetMessage *get)
2447{
2448 uint16_t msize = ntohs (get->header.size);
2449 uint16_t result_filter_size = ntohs (get->result_filter_size);
2450
2451 (void) cls;
2452 if (msize < sizeof(*get) + result_filter_size)
2453 {
2454 GNUNET_break_op (0);
2455 return GNUNET_SYSERR;
2456 }
2457 return GNUNET_OK;
2458}
2459
2460
2469
2470
2471static void
2473{
2474 struct HandleCallbackGet *handle = cls;
2475 enum GNUNET_DHT_RouteOption options = ntohs (handle->get->options);
2476 enum GNUNET_BLOCK_Type type = ntohl (handle->get->type);
2477 const void *result_filter = (const void *) &handle->get[1];
2478 uint16_t msize = ntohs (handle->get->header.size);
2479 uint16_t result_filter_size = ntohs (handle->get->result_filter_size);
2480 const void *xquery = result_filter + result_filter_size;
2481 size_t xquery_size = msize - sizeof (*handle->get) - result_filter_size;
2482
2483 /* remember request for routing replies
2484 TODO: why should we do this if GNUNET_BLOCK_REPLY_OK_LAST == eval?
2485 */
2486 GDS_ROUTING_add (&handle->t->pi->id,
2487 type,
2488 handle->bg, /* bg now owned by routing, but valid at least until end of this function! */
2489 options,
2490 &handle->get->key,
2491 xquery,
2492 xquery_size);
2493
2494 /* P2P forwarding */
2495 {
2496 bool forwarded = false;
2497 uint16_t desired_replication_level = ntohs (
2498 handle->get->desired_replication_level);
2499 uint16_t hop_count = ntohs (handle->get->hop_count);
2500
2502 forwarded = (GNUNET_OK ==
2504 options,
2505 desired_replication_level,
2506 hop_count,
2507 &handle->get->key,
2508 xquery,
2509 xquery_size,
2510 handle->bg,
2511 handle->peer_bf));
2513 options
2514 | (forwarded
2515 ? 0
2517 type,
2518 hop_count,
2519 desired_replication_level,
2520 &handle->get->key);
2521 }
2522 /* clean up; note that 'bg' is owned by routing now! */
2524
2525 GNUNET_free (handle->get);
2527}
2528
2529
2530static void
2532{
2533 struct HandleCallbackGet *handle = cls;
2534 enum GNUNET_BLOCK_Type type = ntohl (handle->get->type);
2535
2537 "Handle getting local HELLO %s of type %u\n",
2538 GNUNET_h2s (&handle->get->key),
2539 type);
2540
2542 {
2543 enum GNUNET_DHT_RouteOption options = ntohs (handle->get->options);
2544 const void *result_filter = (const void *) &handle->get[1];
2545 uint16_t msize = ntohs (handle->get->header.size);
2546 uint16_t result_filter_size = ntohs (handle->get->result_filter_size);
2547 const void *xquery = result_filter + result_filter_size;
2548 size_t xquery_size = msize - sizeof (*handle->get) - result_filter_size;
2549 /* The datacache callback runs once per *result*, never on completion,
2550 so it must not carry the continuation: no result would drop the
2551 request, several would run the continuation several times. Both
2552 lookups below are synchronous. */
2554 handle->eval = GDS_DATACACHE_get_closest (&handle->get->key,
2555 type,
2556 xquery,
2557 xquery_size,
2558 handle->bg,
2560 handle->t->pi);
2561 else
2562 handle->eval = GDS_DATACACHE_handle_get (&handle->get->key,
2563 type,
2564 xquery,
2565 xquery_size,
2566 handle->bg,
2568 handle->t->pi);
2569 }
2571}
2572
2573
2574static void
2576{
2577 struct HandleCallbackGet *handle = cls;
2578 enum GNUNET_DHT_RouteOption options = ntohs (handle->get->options);
2579
2582 &handle->get->key,
2583 handle->bg,
2585 handle);
2586 else
2588}
2589
2590
2597static void
2599 const struct PeerGetMessage *get)
2600{
2601 struct Target *t = cls;
2602 struct PeerInfo *peer = t->pi;
2603 uint16_t msize = ntohs (get->header.size);
2604 uint16_t result_filter_size = ntohs (get->result_filter_size);
2605 uint16_t hop_count = ntohs (get->hop_count);
2606 enum GNUNET_BLOCK_Type type = ntohl (get->type);
2607 enum GNUNET_DHT_RouteOption options = ntohs (get->options);
2608 const void *result_filter = (const void *) &get[1];
2609 const void *xquery = result_filter + result_filter_size;
2610 size_t xquery_size = msize - sizeof (*get) - result_filter_size;
2611
2612 /* parse and validate message */
2614 "# P2P GET requests received",
2615 1,
2616 GNUNET_NO);
2618 "# P2P GET bytes received",
2619 msize,
2620 GNUNET_NO);
2621 if (GNUNET_NO ==
2623 type,
2624 &get->key,
2625 xquery,
2626 xquery_size))
2627 {
2628 /* request invalid */
2629 GNUNET_break_op (0);
2630 return;
2631 }
2632
2633 {
2634 const struct GNUNET_PeerIdentity *my_identity;
2635 struct HandleCallbackGet *handle;
2636
2638 handle->t = t;
2639 handle->get = GNUNET_memdup (get, msize);
2641
2643 GNUNET_assert (NULL != my_identity);
2644
2645 handle->peer_bf = GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
2650 &peer->phash));
2652 type,
2653 result_filter,
2654 result_filter_size,
2655 "filter-size",
2656 result_filter_size,
2657 NULL);
2659 "GET for %s at %s after %u hops\n",
2660 GNUNET_h2s (&get->key),
2662 (unsigned int) hop_count);
2663 /* local lookup (this may update the bg) */
2665 (GDS_am_closest_peer (&get->key,
2666 handle->peer_bf)) )
2667 {
2670 {
2672 "# P2P HELLO lookup requests processed",
2673 1,
2674 GNUNET_NO);
2676 &get->key,
2677 handle->bg,
2679 handle);
2680 }
2681 else
2683 }
2684 else
2685 {
2687 "# P2P GET requests ONLY routed",
2688 1,
2689 GNUNET_NO);
2691 }
2692 }
2693}
2694
2695
2704static void
2706 const struct GNUNET_HashCode *query_hash,
2707 unsigned int get_path_length,
2708 const struct GNUNET_DHT_PathElement *get_path)
2709{
2710 /* forward to local clients */
2712 "Forwarding reply to local clients\n");
2713 if (! GDS_CLIENTS_handle_reply (bd,
2714 query_hash,
2715 get_path_length,
2716 get_path))
2717 {
2718 GNUNET_break (0);
2719 return;
2720 }
2722 get_path,
2723 get_path_length);
2725 {
2726 struct GNUNET_DHT_PathElement xput_path[GNUNET_NZL (get_path_length
2727 + bd->put_path_length)];
2728 struct GNUNET_DATACACHE_Block bdx = *bd;
2729
2730 if (NULL != bd->put_path)
2731 GNUNET_memcpy (xput_path,
2732 bd->put_path,
2733 bd->put_path_length * sizeof(struct
2735 GNUNET_memcpy (&xput_path[bd->put_path_length],
2736 get_path,
2737 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
2738 bdx.put_path = xput_path;
2739 bdx.put_path_length += get_path_length;
2741 }
2742 /* forward to other peers */
2744 query_hash,
2745 get_path_length,
2746 get_path);
2747}
2748
2749
2757static enum GNUNET_GenericReturnValue
2759 const struct PeerResultMessage *prm)
2760{
2761 uint16_t msize = ntohs (prm->header.size) - sizeof (*prm);
2762 enum GNUNET_DHT_RouteOption ro = ntohs (prm->options);
2763 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
2764 bool tracked = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
2765
2766 uint16_t get_path_length = ntohs (prm->get_path_length);
2767 uint16_t put_path_length = ntohs (prm->put_path_length);
2768 size_t vsize = (truncated ? sizeof (struct GNUNET_PeerIdentity) : 0)
2769 + (tracked ? sizeof (struct GNUNET_CRYPTO_EddsaSignature) : 0);
2770
2771 (void) cls;
2772 if ( (msize < vsize) ||
2773 (msize - vsize <
2774 (get_path_length + put_path_length)
2775 * sizeof(struct GNUNET_DHT_PathElement)) ||
2776 (get_path_length >
2777 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_DHT_PathElement)) ||
2778 (put_path_length >
2779 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_DHT_PathElement)) )
2780 {
2781 GNUNET_break_op (0);
2782 return GNUNET_SYSERR;
2783 }
2784 return GNUNET_OK;
2785}
2786
2787
2794static void
2796 const struct PeerResultMessage *prm)
2797{
2798 struct Target *t = cls;
2799 struct PeerInfo *peer = t->pi;
2800 uint16_t msize = ntohs (prm->header.size) - sizeof (*prm);
2801 enum GNUNET_DHT_RouteOption ro = ntohs (prm->options);
2802 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
2803 bool tracked = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
2804 uint16_t get_path_length = ntohs (prm->get_path_length);
2805 uint16_t put_path_length = ntohs (prm->put_path_length);
2806 const struct GNUNET_PeerIdentity *trunc_peer
2807 = truncated
2808 ? (const struct GNUNET_PeerIdentity *) &prm[1]
2809 : NULL;
2810 const struct GNUNET_DHT_PathElement *put_path
2811 = truncated
2812 ? (const struct GNUNET_DHT_PathElement *) &trunc_peer[1]
2813 : (const struct GNUNET_DHT_PathElement *) &prm[1];
2814 const struct GNUNET_DHT_PathElement *get_path
2815 = &put_path[put_path_length];
2816 const struct GNUNET_CRYPTO_EddsaSignature *last_sig
2817 = tracked
2818 ? (const struct GNUNET_CRYPTO_EddsaSignature *) &get_path[get_path_length]
2819 : NULL;
2820 const void *data
2821 = tracked
2822 ? (const void *) &last_sig[1]
2823 : (const void *) &get_path[get_path_length];
2824 size_t vsize = (truncated ? sizeof (struct GNUNET_PeerIdentity) : 0)
2825 + (tracked ? sizeof (struct GNUNET_CRYPTO_EddsaSignature) : 0);
2826 struct GNUNET_DATACACHE_Block bd = {
2828 .put_path = put_path,
2829 .put_path_length = put_path_length,
2830 .key = prm->key,
2831 .type = ntohl (prm->type),
2832 .ro = ro,
2833 .data = data,
2834 .data_size = msize - vsize - (get_path_length + put_path_length)
2835 * sizeof(struct GNUNET_DHT_PathElement)
2836 };
2837
2838 /* parse and validate message */
2839 if (GNUNET_TIME_absolute_is_past (bd.expiration_time))
2840 {
2842 "# Expired results discarded",
2843 1,
2844 GNUNET_NO);
2845 return;
2846 }
2847 if (GNUNET_OK !=
2849 bd.type,
2850 bd.data,
2851 bd.data_size))
2852 {
2853 GNUNET_break_op (0);
2854 return;
2855 }
2857 "# P2P RESULTS received",
2858 1,
2859 GNUNET_NO);
2861 "# P2P RESULT bytes received",
2862 msize,
2863 GNUNET_NO);
2864 {
2866
2868 bd.type,
2869 bd.data,
2870 bd.data_size,
2871 &bd.key);
2872 if (GNUNET_NO == ret)
2873 bd.key = prm->key;
2874 }
2875
2876 /* if we got a HELLO, consider it for our own routing table */
2877 hello_check (&bd);
2878
2879 /* Need to append 'peer' to 'get_path' */
2880 if (tracked)
2881 {
2882 struct GNUNET_DHT_PathElement xget_path[get_path_length + 1];
2883 struct GNUNET_DHT_PathElement *gp = xget_path;
2884 unsigned int failure_offset;
2885
2886 GNUNET_memcpy (xget_path,
2887 get_path,
2888 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
2889 xget_path[get_path_length].pred = peer->id;
2890 /* use memcpy(), as last_sig may not be aligned */
2891 memcpy (&xget_path[get_path_length].sig,
2892 last_sig,
2893 sizeof (*last_sig));
2894#if SANITY_CHECKS
2895 {
2896 const struct GNUNET_PeerIdentity *my_identity;
2898 GNUNET_assert (NULL != my_identity);
2899 /* TODO: might want to eventually implement probabilistic
2900 load-based path verification, but for now it is all or nothing */
2901 failure_offset
2902 = GNUNET_DHT_verify_path (bd.data,
2903 bd.data_size,
2904 bd.expiration_time,
2905 trunc_peer,
2906 put_path,
2907 put_path_length,
2908 gp,
2909 get_path_length + 1,
2910 my_identity);
2911 }
2912#else
2913 failure_offset = 0;
2914#endif
2915 if (0 != failure_offset)
2916 {
2918 "Recorded path invalid at offset %u, truncating\n",
2919 failure_offset);
2920 GNUNET_assert (failure_offset <= bd.put_path_length + get_path_length
2921 + 1);
2922 if (failure_offset < bd.put_path_length)
2923 {
2924 /* failure on put path */
2925 trunc_peer = &bd.put_path[failure_offset - 1].pred;
2926 bd.ro |= GNUNET_DHT_RO_TRUNCATED;
2927 bd.put_path = &bd.put_path[failure_offset];
2928 bd.put_path_length -= failure_offset;
2929 truncated = true;
2930 }
2931 else
2932 {
2933 /* failure on get path */
2934 failure_offset -= bd.put_path_length;
2935 if (0 == failure_offset)
2936 trunc_peer = &bd.put_path[bd.put_path_length - 1].pred;
2937 else
2938 trunc_peer = &gp[failure_offset - 1].pred;
2939 get_path_length -= failure_offset;
2940 gp = &gp[failure_offset];
2941 bd.put_path_length = 0;
2942 bd.put_path = NULL;
2943 bd.ro |= GNUNET_DHT_RO_TRUNCATED;
2944 truncated = true;
2945 }
2946 }
2948 "Extending GET path of length %u with %s\n",
2949 get_path_length,
2950 GNUNET_i2s (&peer->id));
2951 if (truncated)
2952 {
2953 GNUNET_assert (NULL != trunc_peer);
2954 bd.trunc_peer = *trunc_peer;
2955 }
2957 &prm->key,
2958 get_path_length + 1,
2959 gp);
2960 }
2961 else
2962 {
2963 if (truncated)
2964 {
2965 GNUNET_assert (NULL != trunc_peer);
2966 bd.trunc_peer = *trunc_peer;
2967 }
2969 &prm->key,
2970 0,
2971 NULL);
2972 }
2973}
2974
2975
2983static enum GNUNET_GenericReturnValue
2985 const struct GNUNET_MessageHeader *hello)
2986{
2987 struct Target *t = cls;
2988 struct PeerInfo *peer = t->pi;
2990 size_t hellob_size;
2991 void *hellob;
2993
2995 &peer->id,
2996 &hellob,
2997 &hellob_size,
2998 &expiration);
2999 GNUNET_free (hellob);
3000 return ret;
3001}
3002
3003
3010static void
3012 const struct GNUNET_MessageHeader *hello)
3013{
3014 struct Target *t = cls;
3015 struct PeerInfo *peer = t->pi;
3016
3017 GNUNET_free (peer->hello);
3018 peer->hello_size = 0;
3021 &peer->id,
3022 &peer->hello,
3023 &peer->hello_size,
3024 &peer->hello_expiration));
3025}
3026
3027
3028void
3029GDS_u_receive (void *cls,
3030 void **tctx,
3031 void **sctx,
3032 const void *message,
3033 size_t message_size)
3034{
3035 struct Target *t = *tctx;
3036 struct GNUNET_MQ_MessageHandler core_handlers[] = {
3037 GNUNET_MQ_hd_var_size (dht_p2p_get,
3039 struct PeerGetMessage,
3040 t),
3041 GNUNET_MQ_hd_var_size (dht_p2p_put,
3043 struct PeerPutMessage,
3044 t),
3045 GNUNET_MQ_hd_var_size (dht_p2p_result,
3047 struct PeerResultMessage,
3048 t),
3049 GNUNET_MQ_hd_var_size (dht_p2p_hello,
3051 struct GNUNET_MessageHeader,
3052 t),
3054 };
3055 const struct GNUNET_MessageHeader *mh = message;
3056
3057 (void) cls; /* the 'struct GDS_Underlay' */
3058 (void) sctx; /* our receiver address */
3059 if (NULL == t)
3060 {
3061 /* Received message claiming to originate from myself?
3062 Ignore! */
3063 GNUNET_break_op (0);
3064 return;
3065 }
3066 if (message_size < sizeof (*mh))
3067 {
3068 GNUNET_break_op (0);
3069 return;
3070 }
3071 if (message_size != ntohs (mh->size))
3072 {
3073 GNUNET_break_op (0);
3074 return;
3075 }
3077 "Handling message of type %u from peer %s\n",
3078 ntohs (mh->type),
3079 GNUNET_i2s (&t->pi->id));
3080 if (GNUNET_OK !=
3081 GNUNET_MQ_handle_message (core_handlers,
3082 mh))
3083 {
3084 GNUNET_break_op (0);
3085 return;
3086 }
3087}
3088
3089
3097void
3099 const struct GNUNET_PeerIdentity *pid,
3100 const char *uri)
3101{
3102 const struct GNUNET_PeerIdentity *my_identity;
3103 struct GNUNET_HashCode phash;
3104 int peer_bucket;
3105 struct PeerBucket *bucket;
3106 (void) cls;
3107
3109 GNUNET_assert (NULL != my_identity);
3110
3111 if (0 == GNUNET_memcmp (my_identity, pid))
3112 {
3114 "Got a HELLO for my own PID, ignoring it\n");
3115 return; /* that's us! */
3116 }
3117 GNUNET_CRYPTO_hash (pid,
3118 sizeof(*pid),
3119 &phash);
3120 peer_bucket = find_bucket (&phash);
3121 GNUNET_assert ( (peer_bucket >= 0) &&
3122 ((unsigned int) peer_bucket < MAX_BUCKETS));
3123 bucket = &k_buckets[peer_bucket];
3124 for (struct PeerInfo *pi = bucket->head;
3125 NULL != pi;
3126 pi = pi->next)
3127 if (0 ==
3128 GNUNET_memcmp (&pi->id,
3129 pid))
3130 {
3131 /* already connected */
3132 GDS_u_try_connect (pid,
3133 uri);
3134 return;
3135 }
3136 if (bucket->peers_size >= bucket_size)
3137 return; /* do not care */
3139 "Discovered peer %s at %s suitable for bucket %d (%u/%u), trying to connect\n",
3140 GNUNET_i2s (pid),
3141 uri,
3142 peer_bucket,
3143 bucket->peers_size,
3144 bucket_size);
3145 /* new peer that we like! */
3146 GDS_u_try_connect (pid,
3147 uri);
3148}
3149
3150
3156void
3158{
3159 for (unsigned int bc = 0; bc<closest_bucket; bc++)
3160 {
3161 struct PeerBucket *bucket = &k_buckets[bc];
3162 unsigned int count = 0;
3163
3164 for (struct PeerInfo *pos = bucket->head;
3165 NULL != pos;
3166 pos = pos->next)
3167 {
3168 if (count >= bucket_size)
3169 break; /* we only consider first #bucket_size entries per bucket */
3170 count++;
3171 do_send (pos,
3172 msg);
3173 }
3174 }
3175}
3176
3177
3180{
3181
3182 unsigned long long temp_config_num;
3183
3186 "DHT",
3187 "DISABLE_TRY_CONNECT");
3188 if (GNUNET_OK ==
3190 "DHT",
3191 "bucket_size",
3192 &temp_config_num))
3193 bucket_size = (unsigned int) temp_config_num;
3196 "DHT",
3197 "CACHE_RESULTS");
3199 GNUNET_YES);
3200 return GNUNET_OK;
3201}
3202
3203
3204void
3215
3216
3217const struct GNUNET_PeerIdentity *
3222
3223
3224/* end of gnunet-service-dht_neighbours.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
struct GNUNET_MessageHeader * msg
Definition 005.c:2
static mp_limb_t u[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
#define DHT_BLOOM_SIZE
Size of the bloom filter the DHT uses to filter peers.
Definition dht.h:34
bool GDS_helper_make_put_message(struct PeerPutMessage *ppm, size_t msize, const struct GNUNET_CRYPTO_EddsaPrivateKey *sk, const struct GNUNET_PeerIdentity *target, const struct GNUNET_HashCode *target_hash, const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *block_key, enum GNUNET_DHT_RouteOption ro, enum GNUNET_BLOCK_Type block_type, struct GNUNET_TIME_Absolute block_expiration_time, const uint8_t *block_data, size_t block_data_len, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_len, size_t hop_count, uint32_t desired_replication_level, const struct GNUNET_PeerIdentity *trunc_peer, GDS_HelperMsgCallback cb, size_t cb_data_size, void *cb_data)
Definition dht_helper.c:326
bool GDS_helper_sign_path(const void *data, size_t data_size, const struct GNUNET_CRYPTO_EddsaPrivateKey *sk, struct GNUNET_TIME_Absolute exp_time, const struct GNUNET_PeerIdentity *pred, const struct GNUNET_PeerIdentity *succ, GDS_HelperCallback cb, size_t cb_data_size, void *cb_data)
Sign that we are routing a message from pred to succ.
Definition dht_helper.c:226
enum GNUNET_GenericReturnValue GDS_helper_put_message_get_size(size_t *msize_out, const struct GNUNET_PeerIdentity *my_identity, enum GNUNET_DHT_RouteOption ro_in, enum GNUNET_DHT_RouteOption *ro_out, struct GNUNET_TIME_Absolute block_expiration_time, const uint8_t *block_data, size_t block_data_len, const struct GNUNET_DHT_PathElement *put_path_in, unsigned int put_path_len_in, unsigned int *put_path_len_out, const struct GNUNET_PeerIdentity *trunc_peer, struct GNUNET_PeerIdentity *trunc_peer_out, bool *truncated)
Definition dht_helper.c:96
Helper functions for DHT.
struct GNUNET_PILS_Handle * GDS_pils
Handle for the pils service.
static int forward
Search direction: forward.
Definition gnunet-abd.c:163
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_CADET_Handle * mh
Cadet handle.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
static int get
Get DID Documement for DID Flag.
Definition gnunet-did.c:63
static struct GNUNET_FS_Handle * ctx
static struct GNUNET_SCHEDULER_Task * t
Main task.
static char * value
Value of the record to add/remove.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
static struct GNUNET_NSE_Handle * nse
The handle to the NSE service.
Definition gnunet-nse.c:34
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static struct GNUNET_PeerIdentity my_identity
Identity of this peer.
double GDS_NSE_get(void)
Return the current NSE.
struct GNUNET_MessageHeader * GDS_my_hello
Our HELLO.
struct GNUNET_DHTU_PreferenceHandle * GDS_u_hold(struct GDS_Underlay *u, struct GNUNET_DHTU_Target *target)
Create a hold on target at underlay u.
void GDS_u_send(struct GDS_Underlay *u, struct GNUNET_DHTU_Target *target, const void *msg, size_t msg_size, GNUNET_SCHEDULER_TaskCallback finished_cb, void *finished_cb_cls)
Send message to some other participant over the network.
void GDS_u_drop(struct GDS_Underlay *u, struct GNUNET_DHTU_PreferenceHandle *ph)
Drop a hold ph from underlay u.
void GDS_u_try_connect(const struct GNUNET_PeerIdentity *pid, const char *address)
Ask all underlays to connect to peer pid at address.
GNUnet DHT globals.
void GDS_CLIENTS_process_get(enum GNUNET_DHT_RouteOption options, enum GNUNET_BLOCK_Type type, uint32_t hop_count, uint32_t desired_replication_level, const struct GNUNET_HashCode *key)
Check if some client is monitoring GET messages and notify them in that case.
void GDS_CLIENTS_process_get_resp(const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_length)
Check if some client is monitoring GET RESP messages and notify them in that case.
struct GNUNET_STATISTICS_Handle * GDS_stats
Handle for the statistics service.
void GDS_CLIENTS_retry_puts(void)
Our routing table gained a peer.
struct GNUNET_BLOCK_Context * GDS_block_context
Our handle to the BLOCK library.
void GDS_CLIENTS_retry_requests(void)
Our routing table gained a peer.
void GDS_CLIENTS_process_put(const struct GNUNET_DATACACHE_Block *bd, uint32_t hop_count, uint32_t desired_replication_level)
Check if some client is monitoring PUT messages and notify them in that case.
bool GDS_CLIENTS_handle_reply(const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path)
Handle a reply we've received from another peer.
const struct GNUNET_CONFIGURATION_Handle * GDS_cfg
Configuration we use.
#define MINIMUM_LOG_NSE
Floor for the log of the network size estimate, and the value we use before the NSE service has told ...
enum GNUNET_BLOCK_ReplyEvaluationResult GDS_DATACACHE_get_closest(const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, const void *xquery, size_t xquery_size, struct GNUNET_BLOCK_Group *bg, GDS_DATACACHE_GetCallback cb, void *cb_cls)
Handle a request for data close to a key that we have received from another peer.
enum GNUNET_BLOCK_ReplyEvaluationResult GDS_DATACACHE_handle_get(const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, const void *xquery, size_t xquery_size, struct GNUNET_BLOCK_Group *bg, GDS_DATACACHE_GetCallback gc, void *gc_cls)
Handle a GET request we've received from another peer.
void GDS_DATACACHE_handle_put(const struct GNUNET_DATACACHE_Block *bd)
Handle a datum we've received from another peer.
static void send_find_peer_message(void *cls)
Task to send a find peer message for our own peer identifier so that we can find the closest peers in...
static void handle_dht_p2p_result(void *cls, const struct PeerResultMessage *prm)
Core handler for p2p result messages.
void GDS_NEIGHBOURS_handle_reply(struct PeerInfo *pi, const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Handle a reply (route to origin).
#define MAXIMUM_PENDING_PER_PEER
Maximum allowed number of pending messages per peer.
static int disable_try_connect
Option for testing that disables the 'connect' function of the DHT.
void GDS_try_connect(void *cls, const struct GNUNET_PeerIdentity *pid, const char *uri)
Callback function used to extract URIs from a builder.
static void cleanup_neighbours_reply(struct GDS_NeighboursReply *reply)
struct PeerInfo * GDS_NEIGHBOURS_lookup_peer(const struct GNUNET_PeerIdentity *target)
Lookup peer by peer's identity.
static void handle_find_my_hello(struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
We have received a request for a HELLO.
static unsigned int newly_found_peers
How many peers have we added since we sent out our last find peer request?
static void send_done_cb(void *cls)
Function called whenever we finished sending to a target.
static struct PeerInfo * select_peer(const struct GNUNET_HashCode *key, const struct GNUNET_CONTAINER_BloomFilter *bloom, uint32_t hops)
Select a peer from the routing table that would be a good routing destination for sending a message f...
#define DEFAULT_BUCKET_SIZE
What is the maximum number of peers in a given bucket.
void GDS_NEIGHBOURS_done()
Shutdown neighbours subsystem.
void GDS_u_disconnect(void *ctx)
Function to call when we disconnected from a peer and can henceforth cannot transmit to that peer any...
static void handle_dht_p2p_hello(void *cls, const struct GNUNET_MessageHeader *hello)
Core handler for p2p HELLO messages.
static void cb_forwarded_dht_p2p_put(void *cls, enum GNUNET_GenericReturnValue forwarded)
static void handle_dht_p2p_get(void *cls, const struct PeerGetMessage *get)
Core handler for p2p get requests.
void GDS_NEIGHBOURS_broadcast(const struct GNUNET_MessageHeader *msg)
Send msg to all peers in our buckets.
#define FIND_PEER_REPLICATION_LEVEL
Desired replication level for FIND PEER requests.
static unsigned int get_forward_count(uint16_t hop_count, uint16_t target_replication)
To how many peers should we (on average) forward the request to obtain the desired target_replication...
static int find_bucket(const struct GNUNET_HashCode *hc)
Find the optimal bucket for this key.
static enum GNUNET_GenericReturnValue check_dht_p2p_put(void *cls, const struct PeerPutMessage *put)
Check validity of a p2p put request.
static struct GNUNET_CONTAINER_MultiPeerMap * all_connected_peers
Hash map of all CORE-connected peers, for easy removal from k_buckets on disconnect.
static void handle_local_result(void *cls, const struct GNUNET_DATACACHE_Block *bd)
Handle an exact result from local datacache for a GET operation.
static void cb_handle_dht_p2p_get_my_hello(void *cls)
#define DHT_MINIMUM_FIND_PEER_INTERVAL
How long at least to wait before sending another find peer request.
static int cache_results
Do we cache all results that we are routing in the local datacache?
void GDS_u_connect(void *cls, struct GNUNET_DHTU_Target *target, const struct GNUNET_PeerIdentity *pid, void **ctx)
Function to call when we connect to a peer and can henceforth transmit to that peer.
static enum GNUNET_GenericReturnValue check_dht_p2p_result(void *cls, const struct PeerResultMessage *prm)
Check validity of p2p result message.
static struct GNUNET_SCHEDULER_Task * find_peer_task
Task that sends FIND PEER requests.
static bool cb_routing_put_message(void *cls, size_t msize, struct PeerPutMessage *ppm)
#define MAX_BUCKETS
How many buckets will we allow in total.
static enum GNUNET_GenericReturnValue check_dht_p2p_get(void *cls, const struct PeerGetMessage *get)
Check validity of p2p get request.
static enum GNUNET_GenericReturnValue check_dht_p2p_hello(void *cls, const struct GNUNET_MessageHeader *hello)
Check validity of a p2p hello message.
static void cb_handle_dht_p2p_get_local_hello(void *cls)
static unsigned int get_target_peers(const struct GNUNET_HashCode *key, struct GNUNET_CONTAINER_BloomFilter *bloom, uint16_t hop_count, uint16_t target_replication, struct PeerInfo ***targets)
Compute the set of peers that the given request should be forwarded to.
static void hello_check(const struct GNUNET_DATACACHE_Block *bd)
If we got a HELLO, consider it for our own routing table.
static void safe_neighbours_callback(void *cls, GNUNET_SCHEDULER_TaskCallback cb, bool success)
static enum GNUNET_GenericReturnValue add_known_to_bloom(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Add each of the peers we already know to the Bloom filter of the request so that we don't get duplica...
static void handle_find_local_hello(struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
We have received a request for nearby HELLOs.
static unsigned int bucket_size
Maximum size for each bucket.
static void process_reply_with_path(const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path)
Process a reply, after the get_path has been updated.
enum GNUNET_GenericReturnValue GDS_am_closest_peer(const struct GNUNET_HashCode *key, const struct GNUNET_CONTAINER_BloomFilter *bloom)
Check whether my identity is closer than any known peers.
static unsigned int closest_bucket
One past the deepest currently used bucket, initially 0 (no peers at all).
static bool cb_path_signed(void *cls, const struct GNUNET_CRYPTO_EddsaSignature *sig)
static struct PeerBucket k_buckets[sizeof(struct GNUNET_HashCode) *8]
The buckets.
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_init()
Initialize neighbours subsystem.
static void cb_handle_dht_p2p_get_local_result(void *cls)
static void update_hold(struct PeerBucket *bucket)
The list of the first bucket_size peers of bucket changed.
void GDS_u_receive(void *cls, void **tctx, void **sctx, const void *message, size_t message_size)
Function to call when we receive a message.
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_get(enum GNUNET_BLOCK_Type type, enum GNUNET_DHT_RouteOption options, uint16_t desired_replication_level, uint16_t hop_count, const struct GNUNET_HashCode *key, const void *xquery, size_t xquery_size, struct GNUNET_BLOCK_Group *bg, struct GNUNET_CONTAINER_BloomFilter *peer_bf)
Perform a GET operation.
void GDS_NEIGHBOURS_handle_put(const struct GNUNET_DATACACHE_Block *bd, uint16_t desired_replication_level, uint16_t hop_count, struct GNUNET_CONTAINER_BloomFilter *bf, GDS_PutOperationCallback cb, void *cb_cls)
Perform a PUT operation.
static bool do_send(struct PeerInfo *pi, const struct GNUNET_MessageHeader *msg)
Send msg to pi.
const struct GNUNET_PeerIdentity * GDS_NEIGHBOURS_get_id()
Get the ID of the local node.
static void handle_dht_p2p_put(void *cls, const struct PeerPutMessage *put)
Core handler for p2p put requests.
#define DHT_AVG_FIND_PEER_INTERVAL
How long to additionally wait on average per bucket_size to send out the FIND PEER requests if we did...
GNUnet DHT routing code.
void(* GDS_PutOperationCallback)(void *cls, enum GNUNET_GenericReturnValue forwarded)
void GDS_ROUTING_add(const struct GNUNET_PeerIdentity *sender, enum GNUNET_BLOCK_Type type, struct GNUNET_BLOCK_Group *bg, enum GNUNET_DHT_RouteOption options, const struct GNUNET_HashCode *key, const void *xquery, size_t xquery_size)
Add a new entry to our routing table.
void GDS_ROUTING_process(const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path)
Handle a reply (route to origin).
GNUnet DHT tracking of requests for routing replies.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Definition gnunet-vpn.c:35
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
@ GNUNET_BLOCK_TYPE_DHT_HELLO
Type of a block that contains a DHT-NG HELLO for a peer.
API to the DHT service.
Helper library for handling HELLO URIs.
const struct GNUNET_HashCode * GNUNET_PILS_get_identity_hash(const struct GNUNET_PILS_Handle *handle)
Return the hash of the current peer identity from a given handle.
Definition pils_api.c:884
const struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_PILS_get_private_key(const struct GNUNET_PILS_Handle *handle)
Return the private key of the current peer identity.
Definition pils_api.c:943
const struct GNUNET_PeerIdentity * GNUNET_PILS_get_identity(const struct GNUNET_PILS_Handle *handle)
Return the current peer identity of a given handle.
Definition pils_api.c:875
Constants for network protocols.
API to schedule computations using continuation passing style.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
enum GNUNET_GenericReturnValue GNUNET_BLOCK_group_set_seen(struct GNUNET_BLOCK_Group *bg, const struct GNUNET_HashCode *seen_results, unsigned int seen_results_count)
Update block group to filter out the given results.
Definition block.c:365
enum GNUNET_BLOCK_ReplyEvaluationResult GNUNET_BLOCK_check_reply(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, struct GNUNET_BLOCK_Group *group, const struct GNUNET_HashCode *query, const void *xquery, size_t xquery_size, const void *reply_block, size_t reply_block_size)
Function called to validate if a reply is good for a particular query.
Definition block.c:339
enum GNUNET_GenericReturnValue GNUNET_BLOCK_check_block(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *block, size_t block_size)
Function called to validate a block.
Definition block.c:321
enum GNUNET_GenericReturnValue GNUNET_BLOCK_check_query(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode *query, const void *xquery, size_t xquery_size)
Function called to validate a request.
Definition block.c:298
GNUNET_BLOCK_ReplyEvaluationResult
Possible ways for how a block may relate to a query.
void GNUNET_BLOCK_group_destroy(struct GNUNET_BLOCK_Group *bg)
Destroy resources used by a block group.
Definition block.c:194
enum GNUNET_GenericReturnValue GNUNET_BLOCK_group_serialize(struct GNUNET_BLOCK_Group *bg, void **raw_data, size_t *raw_data_size)
Serialize state of a block group.
Definition block.c:177
struct GNUNET_BLOCK_Group * GNUNET_BLOCK_group_create(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *raw_data, size_t raw_data_size,...)
Create a new block group.
Definition block.c:249
enum GNUNET_GenericReturnValue GNUNET_BLOCK_get_key(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *block, size_t block_size, struct GNUNET_HashCode *key)
Function called to obtain the key for a block.
Definition block.c:278
@ GNUNET_BLOCK_REPLY_OK_MORE
Valid result, and there may be more.
@ GNUNET_BLOCK_REPLY_OK_LAST
Last possible valid result.
struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_init(const char *data, size_t size, unsigned int k)
Create a Bloom filter from raw bits.
void GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Add an element to the filter.
bool GNUNET_CONTAINER_bloomfilter_test(const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Test if an element is in the filter.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_bloomfilter_get_raw_data(const struct GNUNET_CONTAINER_BloomFilter *bf, char *data, size_t size)
Copy the raw data of this Bloom filter into the given data array.
void GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf)
Free the space associated with a filter in memory, flush to drive if needed (do not free the space on...
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
#define GNUNET_CONSTANTS_BLOOMFILTER_K
K-value that must be used for the bloom filters in 'GET' queries.
uint64_t GNUNET_CRYPTO_random_u64(uint64_t max)
Generate a random unsigned 64-bit value.
uint32_t GNUNET_CRYPTO_random_u32(uint32_t i)
Produce a random value.
GNUNET_DHT_RouteOption
Options for routing.
#define GNUNET_DHT_MAXIMUM_REPLICATION_LEVEL
Maximum allowed replication level for all requests.
unsigned int GNUNET_DHT_verify_path(const void *data, size_t data_size, struct GNUNET_TIME_Absolute exp_time, const struct GNUNET_PeerIdentity *trunc_peer, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_len, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_len, const struct GNUNET_PeerIdentity *me)
Verify signatures on a path consisting of put_path and get_path in reverse order (starting at the las...
Definition dht_api.c:1354
@ GNUNET_DHT_RO_TRUNCATED
Flag set if the path was truncated.
@ GNUNET_DHT_RO_RECORD_ROUTE
We should keep track of the route that the message took in the P2P network.
@ GNUNET_DHT_RO_LAST_HOP
Flag given to monitors if this was the last hop for a GET/PUT.
@ GNUNET_DHT_RO_FIND_APPROXIMATE
Approximate results are fine.
@ GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
Each peer along the way should process the request (otherwise only peers locally closest to the key w...
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition crypto_hash.c:40
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
int GNUNET_CRYPTO_hash_xorcmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2, const struct GNUNET_HashCode *target)
Find out which of the two GNUNET_CRYPTO_hash codes is closer to target in the XOR metric (Kademlia).
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
void GNUNET_HELLO_parser_free(struct GNUNET_HELLO_Parser *parser)
Release resources of a builder.
Definition hello-uri.c:380
enum GNUNET_GenericReturnValue GNUNET_HELLO_dht_msg_to_block(const struct GNUNET_MessageHeader *hello, const struct GNUNET_PeerIdentity *pid, void **block, size_t *block_size, struct GNUNET_TIME_Absolute *block_expiration)
Convert a DHT hello message to a HELLO block.
Definition hello-uri.c:1061
const struct GNUNET_PeerIdentity * GNUNET_HELLO_parser_iterate(const struct GNUNET_HELLO_Parser *parser, GNUNET_HELLO_UriCallback uc, void *uc_cls)
Iterate over URIs in a parser.
Definition hello-uri.c:1039
#define GNUNET_HELLO_ADDRESS_EXPIRATION
For how long are HELLO signatures valid?
struct GNUNET_HELLO_Parser * GNUNET_HELLO_parser_from_block(const void *block, size_t block_size)
Parse block.
Definition hello-uri.c:569
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros(const struct GNUNET_HashCode *h)
Count the number of leading 0 bits in h.
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_log(kind,...)
#define GNUNET_B2S(obj)
Convert a fixed-sized object to a string using GNUNET_b2s().
#define GNUNET_MAX(a, b)
#define GNUNET_NZL(l)
Macro used to avoid using 0 for the length of a variable-size array (Non-Zero-Length).
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_MIN(a, b)
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_PACKED
gcc-ism to get packed structs.
uint32_t bits[512/8/sizeof(uint32_t)]
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
enum GNUNET_GenericReturnValue GNUNET_MQ_handle_message(const struct GNUNET_MQ_MessageHandler *handlers, const struct GNUNET_MessageHeader *mh)
Call the message message handler that was registered for the type of the given message in the given h...
Definition mq.c:281
#define GNUNET_MESSAGE_TYPE_DHT_P2P_HELLO
HELLO advertising a neighbours addresses.
#define GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
Data is returned to peer from DHT.
#define GNUNET_MESSAGE_TYPE_DHT_P2P_GET
Peer tries to find data in DHT.
#define GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
Peer is storing data in DHT.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition scheduler.c:1310
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition scheduler.c:1283
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition time.c:438
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition time.c:737
struct GNUNET_TIME_Relative GNUNET_TIME_relative_add(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Add relative times together.
Definition time.c:583
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition time.c:316
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition time.c:486
struct GNUNET_TIME_Relative GNUNET_TIME_relative_divide(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Divide relative time by a given factor.
Definition time.c:548
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition time.c:636
bool GNUNET_TIME_absolute_is_past(struct GNUNET_TIME_Absolute abs)
Test if abs is truly in the past (excluding now).
Definition time.c:667
static struct GNUNET_TIME_Relative delta
Definition speedup.c:36
const struct GNUNET_HashCode * query_hash
struct GNUNET_BLOCK_Group * bg
struct GNUNET_DATACACHE_Block block
struct GNUNET_DHT_PathElement * put_path
struct GNUNET_DHT_PathElement * paths
struct GNUNET_PeerIdentity trunc_peer_id
struct PeerResultMessage * prm
GNUNET_SCHEDULER_TaskCallback cb
struct GNUNET_DHT_PathElement * put_path
struct GNUNET_DATACACHE_Block bd
bool * sent
Did any target actually take the message? Shared between the per-target copies of this struct,...
Information we keep per underlay.
Internal representation of the hash map.
an ECC signature using EdDSA.
Information about a block stored in the datacache.
const struct GNUNET_DHT_PathElement * put_path
PUT path taken by the block, array of peer identities.
enum GNUNET_BLOCK_Type type
Type of the block.
const void * data
Actual block data.
enum GNUNET_DHT_RouteOption ro
Options for routing for the block.
struct GNUNET_PeerIdentity trunc_peer
If the path was truncated, this is the peer ID at which the path was truncated.
struct GNUNET_HashCode key
Key of the block.
size_t data_size
Number of bytes in data.
unsigned int put_path_length
Length of the put_path array.
struct GNUNET_TIME_Absolute expiration_time
When does the block expire?
Opaque handle expressing a preference of the DHT to keep a particular target connected.
Opaque handle that the underlay offers for the target peer when sending messages to another peer.
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
struct GNUNET_PeerIdentity pred
Previous peer on the path (matches "pred" in the signed field).
struct GNUNET_CRYPTO_EddsaSignature sig
Signature affirming the hop of type GNUNET_SIGNATURE_PURPOSE_DHT_HOP.
Context for parsing HELLOs.
Definition hello-uri.c:233
A 512-bit hashcode.
Message handler for a specific message type.
Header for all communications.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition scheduler.c:141
struct GNUNET_SCHEDULER_Task * next
This is a linked list.
Definition scheduler.c:145
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
struct GNUNET_CONTAINER_BloomFilter * peer_bf
struct GNUNET_BLOCK_Group * bg
enum GNUNET_BLOCK_ReplyEvaluationResult eval
Peers are grouped into buckets.
struct PeerInfo * head
Head of DLL.
struct PeerInfo * tail
Tail of DLL.
unsigned int peers_size
Number of peers in the bucket.
uint16_t result_filter_size
Size of the result filter.
char bloomfilter[128]
Bloomfilter (for peer identities) to stop circular routes.
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_DHT_P2P_GET.
uint16_t desired_replication_level
Desired replication level for this request.
uint16_t options
Processing options.
struct GNUNET_HashCode key
The key we are looking for.
uint32_t type
Desired content type.
Entry for a peer in a bucket.
struct GNUNET_TIME_Absolute hello_expiration
When does our HELLO from this peer expire?
size_t hello_size
Number of bytes in hello.
struct PeerInfo * next
Next peer entry (DLL)
struct GNUNET_PeerIdentity id
What is the identity of the peer?
struct Target * t_tail
Tail of DLL of targets for this peer.
struct PeerInfo * prev
Prev peer entry (DLL)
struct GNUNET_HashCode phash
Hash of id.
int peer_bucket
Which bucket is this peer in?
struct Target * t_head
Head of DLL of targets for this peer.
void * hello
Block with a HELLO of this peer.
P2P PUT message.
Definition dht.h:429
uint16_t desired_replication_level
Replication level for this message.
Definition dht.h:453
uint16_t hop_count
Hop count.
Definition dht.h:448
uint32_t type
Content type, must not be zero.
Definition dht.h:438
char bloomfilter[128]
Bloomfilter (for peer identities) to stop circular routes.
Definition dht.h:468
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_DHT_P2P_PUT.
Definition dht.h:433
struct GNUNET_HashCode key
The key we are storing under.
Definition dht.h:473
uint16_t options
Processing options.
Definition dht.h:443
struct GNUNET_TIME_AbsoluteNBO expiration_time
When does the content expire?
Definition dht.h:463
uint16_t put_path_length
Length of the PUT path that follows (if tracked).
Definition dht.h:458
uint16_t get_path_length
Length of the GET path that follows (if tracked).
uint16_t options
Message options, actually an 'enum GNUNET_DHT_RouteOption' value in NBO.
struct GNUNET_HashCode key
The key of the corresponding GET request.
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT.
uint16_t put_path_length
Length of the PUT path that follows (if tracked).
struct GNUNET_TIME_AbsoluteNBO expiration_time
When does the content expire?
List of targets that we can use to reach this peer.
struct Target * prev
Kept in a DLL.
struct PeerInfo * pi
Peer this is a target for.
struct Target * next
Kept in a DLL.
unsigned int load
Set to number of messages are waiting for the transmission to finish.
struct GNUNET_DHTU_PreferenceHandle * ph
Handle used to 'hold' the connection to this peer.
struct GDS_Underlay * u
Underlay providing this target.
struct GNUNET_DHTU_Target * utarget
Handle for sending messages to this peer.
bool dropped
Set to true if the target was dropped, but we could not clean up yet because busy was also true.