GNUnet debian-0.24.3-24-gfea921bd2
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 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"
29#include "gnunet_protocols.h"
31#include "gnunet_pils_service.h"
32#include "gnunet-service-dht.h"
35#include "dht.h"
36#include "dht_helper.h"
37
38#define LOG_TRAFFIC(kind, ...) GNUNET_log_from (kind, "dht-traffic", \
39 __VA_ARGS__)
40
52#define SANITY_CHECKS 2
53
57#define MAX_BUCKETS sizeof(struct GNUNET_HashCode) * 8
58
62#define DEFAULT_BUCKET_SIZE 8
63
67#define FIND_PEER_REPLICATION_LEVEL 4
68
72#define MAXIMUM_PENDING_PER_PEER 64
73
79#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply ( \
80 GNUNET_TIME_UNIT_MINUTES, 2)
81
82
92#define DHT_AVG_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply ( \
93 GNUNET_TIME_UNIT_SECONDS, 6)
94
98#define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
99
100
102
103
108{
113
118
123
128
133
138
143
148
149 /* trunc_peer (if truncated) */
150
151 /* put path (if tracked) */
152
153 /* get path (if tracked) */
154
155 /* sender_sig (if path tracking is on) */
156
157 /* Payload */
158};
159
160
165{
170
175
180
185
190
195
200
205
206 /* result bloomfilter */
207
208 /* xquery */
209
210};
212
213
217struct PeerInfo;
218
219
223struct Target
224{
228 struct Target *next;
229
233 struct Target *prev;
234
239
244
248 struct PeerInfo *pi;
249
254
258 unsigned int load;
259
265
266};
267
268
273{
278
283
288
292 struct PeerInfo *next;
293
297 struct PeerInfo *prev;
298
302 struct Target *t_head;
303
307 struct Target *t_tail;
308
312 void *hello;
313
318
323};
324
325
330{
334 struct PeerInfo *head;
335
339 struct PeerInfo *tail;
340
344 unsigned int peers_size;
345};
346
347
351static int cache_results;
352
356static unsigned int closest_bucket;
357
362static unsigned int newly_found_peers;
363
368
373
379
383static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
384
389
390
398static void
399send_done_cb (void *cls)
400{
401 struct Target *t = cls;
402 struct PeerInfo *pi = t->pi; /* NULL if t->dropped! */
403
404 GNUNET_assert (t->load > 0);
405 t->load--;
406 if (0 < t->load)
407 return;
408 if (t->dropped)
409 {
410 GNUNET_free (t);
411 return;
412 }
413 /* move target back to the front */
415 pi->t_tail,
416 t);
418 pi->t_tail,
419 t);
420}
421
422
429static void
430do_send (struct PeerInfo *pi,
431 const struct GNUNET_MessageHeader *msg)
432{
433 struct Target *t;
434
435 for (t = pi->t_head;
436 NULL != t;
437 t = t->next)
438 if (t->load < MAXIMUM_PENDING_PER_PEER)
439 break;
440 if (NULL == t)
441 {
442 /* all targets busy, drop message */
444 "# messages dropped (underlays busy)",
445 1,
446 GNUNET_NO);
447 return;
448 }
449 t->load++;
450 /* rotate busy targets to the end */
451 if (MAXIMUM_PENDING_PER_PEER == t->load)
452 {
454 pi->t_tail,
455 t);
457 pi->t_tail,
458 t);
459 }
460 GDS_u_send (t->u,
461 t->utarget,
462 msg,
463 ntohs (msg->size),
465 t);
466}
467
468
476static int
477find_bucket (const struct GNUNET_HashCode *hc)
478{
479 struct GNUNET_HashCode xor;
480 unsigned int bits;
481
484 &xor);
486 if (bits == MAX_BUCKETS)
487 {
488 /* How can all bits match? Got my own ID? */
489 GNUNET_break (0);
490 return -1;
491 }
492 return MAX_BUCKETS - bits - 1;
493}
494
495
507 const struct GNUNET_PeerIdentity *key,
508 void *value)
509{
510 struct GNUNET_BLOCK_Group *bg = cls;
511 struct PeerInfo *pi = value;
512
514 &pi->phash,
515 1);
517 "Adding known peer (%s) to Bloom filter for FIND PEER\n",
518 GNUNET_i2s (key));
519 return GNUNET_YES;
520}
521
522
530static void
532{
533 (void) cls;
534
535 /* Compute when to do this again (and if we should
536 even send a message right now) */
537 {
538 struct GNUNET_TIME_Relative next_send_time;
539 bool done_early;
540
541 find_peer_task = NULL;
542 done_early = (newly_found_peers > bucket_size);
543 /* schedule next round, taking longer if we found more peers
544 in the last round. */
545 next_send_time.rel_value_us =
551 1 + 100 * (1 + newly_found_peers) / bucket_size).rel_value_us);
555 GNUNET_SCHEDULER_add_delayed (next_send_time,
557 NULL);
558 if (done_early)
559 return;
560 }
561
562 /* actually send 'find peer' request */
563 {
564 struct GNUNET_BLOCK_Group *bg;
565 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
566
569 NULL,
570 0,
571 "seen-set-size",
574 NULL);
577 bg);
578 peer_bf
582 if (GNUNET_OK !=
587 0, /* hop count */
589 NULL, 0, /* xquery */
590 bg,
591 peer_bf))
592 {
594 "# Failed to initiate FIND PEER lookup",
595 1,
596 GNUNET_NO);
597 }
598 else
599 {
601 "# FIND PEER messages initiated",
602 1,
603 GNUNET_NO);
604 }
607 }
608}
609
610
618static void
619update_hold (struct PeerBucket *bucket)
620{
621 unsigned int off = 0;
622
623 /* find the peer -- we just go over all of them, should
624 be hardly any more expensive than just finding the 'right'
625 one. */
626 for (struct PeerInfo *pos = bucket->head;
627 NULL != pos;
628 pos = pos->next)
629 {
630 if (off > bucket_size)
631 break; /* We only hold up to #bucket_size peers per bucket */
632 off++;
633 for (struct Target *tp = pos->t_head;
634 NULL != tp;
635 tp = tp->next)
636 if (NULL == tp->ph)
637 tp->ph = GDS_u_hold (tp->u,
638 tp->utarget);
639 }
640}
641
642
643void
644GDS_u_connect (void *cls,
645 struct GNUNET_DHTU_Target *target,
646 const struct GNUNET_PeerIdentity *pid,
647 void **ctx)
648{
649 struct GDS_Underlay *u = cls;
650 struct PeerInfo *pi;
651 struct PeerBucket *bucket;
652 bool do_hold = false;
653
654 /* Check for connect to self message */
656 pid))
657 return;
659 "Connected to peer %s\n",
660 GNUNET_i2s (pid));
662 pid);
663 if (NULL == pi)
664 {
666 "# peers connected",
667 1,
668 GNUNET_NO);
669 pi = GNUNET_new (struct PeerInfo);
670 pi->id = *pid;
672 sizeof(*pid),
673 &pi->phash);
674 pi->peer_bucket = find_bucket (&pi->phash);
675 GNUNET_assert ( (pi->peer_bucket >= 0) &&
676 ((unsigned int) pi->peer_bucket < MAX_BUCKETS));
677 bucket = &k_buckets[pi->peer_bucket];
679 bucket->tail,
680 pi);
681 bucket->peers_size++;
683 (unsigned int) pi->peer_bucket + 1);
686 &pi->id,
687 pi,
689 if (bucket->peers_size <= bucket_size)
690 {
692 do_hold = true;
693 }
696 {
697 /* got a first connection, good time to start with FIND PEER requests... */
700 NULL);
701 }
702 }
703 {
704 struct Target *t;
705
706 t = GNUNET_new (struct Target);
707 t->u = u;
708 t->utarget = target;
709 t->pi = pi;
711 pi->t_tail,
712 t);
713 *ctx = t;
714
715 }
716 if (do_hold)
717 update_hold (bucket);
718}
719
720
721void
723{
724 struct Target *t = ctx;
725 struct PeerInfo *pi;
726 struct PeerBucket *bucket;
727 bool was_held = false;
728
729 /* Check for disconnect from self message (on shutdown) */
730 if (NULL == t)
731 return;
732 pi = t->pi;
734 pi->t_tail,
735 t);
736 if (NULL != t->ph)
737 {
738 GDS_u_drop (t->u,
739 t->ph);
740 t->ph = NULL;
741 was_held = true;
742 }
743 if (t->load > 0)
744 {
745 t->dropped = true;
746 t->pi = NULL;
747 }
748 else
749 {
750 GNUNET_free (t);
751 }
752 if (NULL != pi->t_head)
753 return; /* got other connections still */
755 "Disconnected from peer %s\n",
756 GNUNET_i2s (&pi->id));
758 "# peers connected",
759 -1,
760 GNUNET_NO);
763 &pi->id,
764 pi));
767 {
769 find_peer_task = NULL;
770 }
771 GNUNET_assert (pi->peer_bucket >= 0);
772 bucket = &k_buckets[pi->peer_bucket];
774 bucket->tail,
775 pi);
776 GNUNET_assert (bucket->peers_size > 0);
777 bucket->peers_size--;
778 if ( (was_held) &&
779 (bucket->peers_size >= bucket_size - 1) )
780 update_hold (bucket);
781 while ( (closest_bucket > 0) &&
784 GNUNET_free (pi->hello);
785 GNUNET_free (pi);
786}
787
788
797static unsigned int
798get_forward_count (uint16_t hop_count,
799 uint16_t target_replication)
800{
801 uint32_t random_value;
802 uint32_t forward_count;
803 float target_value;
804 double rm1;
805
806 if (hop_count > GDS_NSE_get () * 4.0)
807 {
808 /* forcefully terminate */
810 "# requests TTL-dropped",
811 1,
812 GNUNET_NO);
813 return 0;
814 }
815 if (hop_count > GDS_NSE_get () * 2.0)
816 {
817 /* Once we have reached our ideal number of hops, only forward to 1 peer */
818 return 1;
819 }
820 /* bound by system-wide maximum and minimum */
821 if (0 == target_replication)
822 target_replication = 1; /* 0 is verboten */
823 target_replication =
825 target_replication);
826 rm1 = target_replication - 1.0;
827 target_value =
828 1 + (rm1) / (GDS_NSE_get () + (rm1 * hop_count));
829
830 /* Set forward count to floor of target_value */
831 forward_count = (uint32_t) target_value;
832 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
833 target_value = target_value - forward_count;
835 UINT32_MAX);
836 if (random_value < (target_value * UINT32_MAX))
837 forward_count++;
838 return GNUNET_MIN (forward_count,
840}
841
842
855 const struct GNUNET_CONTAINER_BloomFilter *bloom)
856{
857 int delta;
859 key))
860 return GNUNET_YES;
861 for (int bucket_num = find_bucket (key);
862 bucket_num < closest_bucket;
863 bucket_num++)
864 {
865 unsigned int count = 0;
866
867 GNUNET_assert (bucket_num >= 0);
868 for (struct PeerInfo *pos = k_buckets[bucket_num].head;
869 NULL != pos;
870 pos = pos->next)
871 {
872 if (count >= bucket_size)
873 break; /* we only consider first #bucket_size entries per bucket */
874 count++;
875 if ( (NULL != bloom) &&
876 (GNUNET_YES ==
878 &pos->phash)) )
879 continue; /* Ignore filtered peers */
880 /* All peers in this bucket must be closer than us, as
881 they mismatch with our PID on the pivotal bit. So
882 because an unfiltered peer exists, we are not the
883 closest. */
884 delta = GNUNET_CRYPTO_hash_xorcmp (&pos->phash,
886 key);
887 switch (delta)
888 {
889 case -1: /* pos closer */
890 return GNUNET_NO;
891 case 0: /* identical, impossible! */
892 GNUNET_assert (0);
893 break;
894 case 1: /* I am closer */
895 break;
896 }
897 }
898 }
899 /* No closer (unfiltered) peers found; we must be the closest! */
900 return GNUNET_YES;
901}
902
903
925static struct PeerInfo *
927 const struct GNUNET_CONTAINER_BloomFilter *bloom,
928 uint32_t hops)
929{
930 if (0 == closest_bucket)
931 {
933 "# Peer selection failed",
934 1,
935 GNUNET_NO);
936 return NULL; /* we have zero connections */
937 }
938 if (hops >= GDS_NSE_get ())
939 {
940 /* greedy selection (closest peer that is not in Bloom filter) */
941 struct PeerInfo *chosen = NULL;
942 int best_bucket;
943 int bucket_offset;
944
945 {
946 struct GNUNET_HashCode xor;
947
950 &xor);
951 best_bucket = GNUNET_CRYPTO_hash_count_leading_zeros (&xor);
952 }
953 if (best_bucket >= closest_bucket)
954 bucket_offset = closest_bucket - 1;
955 else
956 bucket_offset = best_bucket;
957 while (-1 != bucket_offset)
958 {
959 struct PeerBucket *bucket = &k_buckets[bucket_offset];
960 unsigned int count = 0;
961
962 for (struct PeerInfo *pos = bucket->head;
963 NULL != pos;
964 pos = pos->next)
965 {
966 if (count >= bucket_size)
967 break; /* we only consider first #bucket_size entries per bucket */
968 count++;
969 if ( (NULL != bloom) &&
970 (GNUNET_YES ==
972 &pos->phash)) )
973 {
975 "Excluded peer `%s' due to BF match in greedy routing for %s\n",
976 GNUNET_i2s (&pos->id),
977 GNUNET_h2s (key));
978 continue;
979 }
980 if (NULL == chosen)
981 {
982 /* First candidate */
983 chosen = pos;
984 }
985 else
986 {
987 int delta = GNUNET_CRYPTO_hash_xorcmp (&pos->phash,
988 &chosen->phash,
989 key);
990 switch (delta)
991 {
992 case -1: /* pos closer */
993 chosen = pos;
994 break;
995 case 0: /* identical, impossible! */
996 GNUNET_assert (0);
997 break;
998 case 1: /* chosen closer */
999 break;
1000 }
1001 }
1002 count++;
1003 } /* for all (#bucket_size) peers in bucket */
1004 if (NULL != chosen)
1005 break;
1006
1007 /* If we chose nothing in first iteration, first go through deeper
1008 buckets (best chance to find a good match), and if we still found
1009 nothing, then to shallower buckets. Terminate on any match in the
1010 current bucket, as this search order guarantees that it can only get
1011 worse as we keep going. */
1012 if (bucket_offset > best_bucket)
1013 {
1014 /* Go through more deeper buckets */
1015 bucket_offset++;
1016 if (bucket_offset == closest_bucket)
1017 {
1018 /* Can't go any deeper, if nothing selected,
1019 go for shallower buckets */
1020 bucket_offset = best_bucket - 1;
1021 }
1022 }
1023 else
1024 {
1025 /* We're either at the 'best_bucket' or already moving
1026 on to shallower buckets. */
1027 if (bucket_offset == best_bucket)
1028 bucket_offset++; /* go for deeper buckets */
1029 else
1030 bucket_offset--; /* go for shallower buckets */
1031 }
1032 } /* for applicable buckets (starting at best match) */
1033 if (NULL == chosen)
1034 {
1036 "# Peer selection failed",
1037 1,
1038 GNUNET_NO);
1039 return NULL;
1040 }
1042 "Selected peer `%s' in greedy routing for %s\n",
1043 GNUNET_i2s (&chosen->id),
1044 GNUNET_h2s (key));
1045 return chosen;
1046 } /* end of 'greedy' peer selection */
1047
1048 /* select "random" peer */
1049 /* count number of peers that are available and not filtered,
1050 but limit to at most #bucket_size peers, starting with
1051 those 'furthest' from us. */
1052 {
1053 unsigned int total = 0;
1054 unsigned int selected;
1055
1056 for (unsigned int bc = 0; bc < closest_bucket; bc++)
1057 {
1058 struct PeerBucket *bucket = &k_buckets[bc];
1059 unsigned int count = 0;
1060
1061 for (struct PeerInfo *pos = bucket->head;
1062 NULL != pos;
1063 pos = pos->next)
1064 {
1065 count++;
1066 if (count > bucket_size)
1067 break; /* limits search to #bucket_size peers per bucket */
1068 if ( (NULL != bloom) &&
1069 (GNUNET_YES ==
1071 &pos->phash)) )
1072 {
1074 "Excluded peer `%s' due to BF match in random routing for %s\n",
1075 GNUNET_i2s (&pos->id),
1076 GNUNET_h2s (key));
1077 continue; /* Ignore filtered peers */
1078 }
1079 total++;
1080 } /* for all peers in bucket */
1081 } /* for all buckets */
1082 if (0 == total) /* No peers to select from! */
1083 {
1085 "# Peer selection failed",
1086 1,
1087 GNUNET_NO);
1088 return NULL;
1089 }
1090
1091 /* Now actually choose a peer */
1093 total);
1094 for (unsigned int bc = 0; bc < closest_bucket; bc++)
1095 {
1096 unsigned int count = 0;
1097
1098 for (struct PeerInfo *pos = k_buckets[bc].head;
1099 pos != NULL;
1100 pos = pos->next)
1101 {
1102 count++;
1103 if (count > bucket_size)
1104 break; /* limits search to #bucket_size peers per bucket */
1105
1106 if ( (NULL != bloom) &&
1107 (GNUNET_YES ==
1109 &pos->phash)) )
1110 continue; /* Ignore bloomfiltered peers */
1111 if (0 == selected--)
1112 {
1114 "Selected peer `%s' in random routing for %s\n",
1115 GNUNET_i2s (&pos->id),
1116 GNUNET_h2s (key));
1117 return pos;
1118 }
1119 } /* for peers in bucket */
1120 } /* for all buckets */
1121 } /* random peer selection scope */
1122 GNUNET_break (0);
1123 return NULL;
1124}
1125
1126
1140static unsigned int
1142 struct GNUNET_CONTAINER_BloomFilter *bloom,
1143 uint16_t hop_count,
1144 uint16_t target_replication,
1145 struct PeerInfo ***targets)
1146{
1147 unsigned int target;
1148 unsigned int off;
1149 struct PeerInfo **rtargets;
1150
1151 GNUNET_assert (NULL != bloom);
1152 target = get_forward_count (hop_count,
1153 target_replication);
1154 if (0 == target)
1155 {
1156 *targets = NULL;
1157 return 0;
1158 }
1159 rtargets = GNUNET_new_array (target,
1160 struct PeerInfo *);
1161 for (off = 0; off < target; off++)
1162 {
1163 struct PeerInfo *nxt;
1164
1165 nxt = select_peer (key,
1166 bloom,
1167 hop_count);
1168 if (NULL == nxt)
1169 break;
1170 rtargets[off] = nxt;
1171 }
1173 "Selected %u/%u peers at hop %u for %s (target was %u)\n",
1174 off,
1176 (unsigned int) hop_count,
1177 GNUNET_h2s (key),
1178 target);
1179 if (0 == off)
1180 {
1181 GNUNET_free (rtargets);
1182 *targets = NULL;
1183 return 0;
1184 }
1185 *targets = rtargets;
1187 "Forwarding query `%s' to %u peers (goal was %u peers)\n",
1188 GNUNET_h2s (key),
1189 off,
1190 target);
1191 return off;
1192}
1193
1194
1200static void
1202{
1203 struct GNUNET_HELLO_Parser *b;
1204
1206 return;
1207
1209 bd->data_size);
1211 {
1214 NULL);
1215 }
1217}
1218
1219
1222 uint16_t desired_replication_level,
1223 uint16_t hop_count,
1225{
1226 unsigned int target_count;
1227 struct PeerInfo **targets;
1228 size_t msize;
1229 enum GNUNET_DHT_RouteOption ro = bd->ro;
1230 unsigned int put_path_length = bd->put_path_length;
1231 const struct GNUNET_DHT_PathElement *put_path = bd->put_path;
1232 bool truncated = (0 != (bd->ro & GNUNET_DHT_RO_TRUNCATED));
1233 const struct GNUNET_PeerIdentity *trunc_peer
1234 = truncated
1235 ? &bd->trunc_peer
1236 : NULL;
1237 struct GNUNET_PeerIdentity trunc_peer_out;
1239
1242 bd->ro, &ro,
1243 bd->expiration_time,
1244 bd->data, bd->data_size,
1245 put_path, put_path_length,
1246 &put_path_length,
1247 trunc_peer,
1248 &trunc_peer_out,
1249 &truncated);
1250 if (truncated)
1251 trunc_peer = &trunc_peer_out;
1252 /* Path may have been truncated by the call above */
1254 "Adding myself (%s) to PUT bloomfilter for %s with RO(%s/%s)\n",
1256 GNUNET_h2s (&bd->key),
1257 (bd->ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE) ? "x" : "-",
1258 (bd->ro & GNUNET_DHT_RO_RECORD_ROUTE) ? "R" : "-");
1259
1260 /* if we got a HELLO, consider it for our own routing table */
1261 hello_check (bd);
1262 GNUNET_assert (NULL != bf);
1266 "# PUT requests routed",
1267 1,
1268 GNUNET_NO);
1269 if (GNUNET_OK != ret)
1270 return ret;
1271 target_count
1272 = get_target_peers (&bd->key,
1273 bf,
1274 hop_count,
1275 desired_replication_level,
1276 &targets);
1277 if (0 == target_count)
1278 {
1280 "Routing PUT for %s terminates after %u hops at %s\n",
1281 GNUNET_h2s (&bd->key),
1282 (unsigned int) hop_count,
1284 return GNUNET_NO;
1285 }
1286 for (unsigned int i = 0; i < target_count; i++)
1287 {
1288 struct PeerInfo *target = targets[i];
1289
1291 &target->phash);
1292 }
1293 for (unsigned int i = 0; i < target_count; i++)
1294 {
1295 struct PeerInfo *target = targets[i];
1296 struct PeerPutMessage *ppm;
1297 char buf[msize] GNUNET_ALIGN;
1298
1299 ppm = (struct PeerPutMessage *) buf;
1300 GDS_helper_make_put_message (ppm, msize,
1302 &target->id,
1303 &target->phash,
1304 bf,
1305 &bd->key,
1306 ro,
1307 bd->type,
1308 bd->expiration_time,
1309 bd->data, bd->data_size,
1310 put_path, put_path_length,
1311 hop_count,
1313 trunc_peer);
1315 "Routing PUT for %s after %u hops to %s\n",
1316 GNUNET_h2s (&bd->key),
1317 (unsigned int) hop_count,
1318 GNUNET_i2s (&target->id));
1319 do_send (target,
1320 &ppm->header);
1321 }
1322 GNUNET_free (targets);
1324 "# PUT messages queued for transmission",
1325 target_count,
1326 GNUNET_NO);
1327 return GNUNET_OK;
1328}
1329
1330
1335 uint16_t hop_count,
1336 const struct GNUNET_HashCode *key,
1337 const void *xquery,
1338 size_t xquery_size,
1339 struct GNUNET_BLOCK_Group *bg,
1340 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1341{
1342 unsigned int target_count;
1343 struct PeerInfo **targets;
1344 size_t msize;
1345 size_t result_filter_size;
1346 void *result_filter;
1347
1348 GNUNET_assert (NULL != peer_bf);
1350 "# GET requests routed",
1351 1,
1352 GNUNET_NO);
1353 target_count = get_target_peers (key,
1354 peer_bf,
1355 hop_count,
1356 desired_replication_level,
1357 &targets);
1359 "Adding myself (%s) to GET bloomfilter for %s with RO(%s/%s)\n",
1361 GNUNET_h2s (key),
1363 (options & GNUNET_DHT_RO_RECORD_ROUTE) ? "R" : "-");
1364
1367 if (0 == target_count)
1368 {
1370 "Routing GET for %s terminates after %u hops at %s\n",
1371 GNUNET_h2s (key),
1372 (unsigned int) hop_count,
1374 return GNUNET_NO;
1375 }
1376 if (GNUNET_OK !=
1378 &result_filter,
1379 &result_filter_size))
1380 {
1381 result_filter = NULL;
1382 result_filter_size = 0;
1383 }
1384 msize = xquery_size + result_filter_size;
1385 if (msize + sizeof(struct PeerGetMessage) >= GNUNET_MAX_MESSAGE_SIZE)
1386 {
1387 GNUNET_break (0);
1388 GNUNET_free (result_filter);
1389 GNUNET_free (targets);
1390 return GNUNET_NO;
1391 }
1392 /* update BF */
1393 for (unsigned int i = 0; i < target_count; i++)
1394 {
1395 struct PeerInfo *target = targets[i];
1396
1398 &target->phash);
1399 }
1400 /* forward request */
1401 for (unsigned int i = 0; i < target_count; i++)
1402 {
1403 struct PeerInfo *target = targets[i];
1404 struct PeerGetMessage *pgm;
1405 char buf[sizeof (*pgm) + msize] GNUNET_ALIGN;
1406 char *rf;
1407
1409 "Routing GET for %s after %u hops to %s\n",
1410 GNUNET_h2s (key),
1411 (unsigned int) hop_count,
1412 GNUNET_i2s (&target->id));
1413 pgm = (struct PeerGetMessage *) buf;
1415 pgm->header.size = htons (sizeof (buf));
1416 pgm->type = htonl (type);
1417 pgm->options = htons (options);
1418 pgm->hop_count = htons (hop_count + 1);
1420 pgm->result_filter_size = htons ((uint16_t) result_filter_size);
1423 pgm->bloomfilter,
1425 pgm->key = *key;
1426 rf = (char *) &pgm[1];
1427 GNUNET_memcpy (rf,
1428 result_filter,
1431 xquery,
1432 xquery_size);
1433 do_send (target,
1434 &pgm->header);
1435 }
1437 "# GET messages queued for transmission",
1438 target_count,
1439 GNUNET_NO);
1440 GNUNET_free (targets);
1441 GNUNET_free (result_filter);
1442 return (0 < target_count) ? GNUNET_OK : GNUNET_NO;
1443}
1444
1445
1446struct PeerInfo *
1448{
1450 target);
1451}
1452
1453
1454bool
1456 const struct GNUNET_DATACACHE_Block *bd,
1457 const struct GNUNET_HashCode *query_hash,
1458 unsigned int get_path_length,
1459 const struct GNUNET_DHT_PathElement *get_path)
1460{
1461 struct GNUNET_DHT_PathElement *paths;
1462 size_t msize;
1463 unsigned int ppl = bd->put_path_length;
1464 const struct GNUNET_DHT_PathElement *put_path = bd->put_path;
1465 enum GNUNET_DHT_RouteOption ro = bd->ro;
1466 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
1467 const struct GNUNET_PeerIdentity *trunc_peer
1468 = truncated
1469 ? &bd->trunc_peer
1470 : NULL;
1471 bool tracking = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
1472#if SANITY_CHECKS > 1
1473 unsigned int failure_offset;
1474
1475 failure_offset
1477 bd->data_size,
1478 bd->expiration_time,
1479 trunc_peer,
1480 put_path,
1481 ppl,
1482 get_path,
1483 get_path_length,
1485 if (0 != failure_offset)
1486 {
1487 GNUNET_assert (failure_offset <= ppl + get_path_length);
1488 GNUNET_break_op (0);
1489 if (failure_offset < ppl)
1490 {
1491 trunc_peer = &put_path[failure_offset - 1].pred;
1492 put_path += failure_offset;
1493 ppl -= failure_offset;
1494 truncated = true;
1496 }
1497 else
1498 {
1499 failure_offset -= ppl;
1500 if (0 == failure_offset)
1501 trunc_peer = &put_path[ppl - 1].pred;
1502 else
1503 trunc_peer = &get_path[failure_offset - 1].pred;
1504 ppl = 0;
1505 put_path = NULL;
1506 truncated = true;
1508 get_path += failure_offset;
1509 get_path_length -= failure_offset;
1510 }
1511 }
1512#endif
1513 msize = bd->data_size + sizeof (struct PeerResultMessage);
1514 if (msize > GNUNET_MAX_MESSAGE_SIZE)
1515 {
1516 GNUNET_break_op (0);
1517 return false;
1518 }
1519 if (truncated)
1520 msize += sizeof (struct GNUNET_PeerIdentity);
1521 if (tracking)
1522 msize += sizeof (struct GNUNET_CRYPTO_EddsaSignature);
1523 if (msize < bd->data_size)
1524 {
1525 GNUNET_break_op (0);
1526 return false;
1527 }
1528 if ( (GNUNET_MAX_MESSAGE_SIZE - msize)
1529 / sizeof(struct GNUNET_DHT_PathElement)
1530 < (get_path_length + ppl) )
1531 {
1532 get_path_length = 0;
1533 ppl = 0;
1534 }
1535 if ( (get_path_length > UINT16_MAX) ||
1536 (ppl > UINT16_MAX) )
1537 {
1538 GNUNET_break (0);
1539 get_path_length = 0;
1540 ppl = 0;
1541 }
1542 msize += (get_path_length + ppl)
1543 * sizeof(struct GNUNET_DHT_PathElement);
1545 "Forwarding reply for key %s to peer %s\n",
1546 GNUNET_h2s (query_hash),
1547 GNUNET_i2s (&pi->id));
1549 "# RESULT messages queued for transmission",
1550 1,
1551 GNUNET_NO);
1552 {
1553 struct PeerResultMessage *prm;
1554 char buf[msize] GNUNET_ALIGN;
1555 void *data;
1556
1557 prm = (struct PeerResultMessage *) buf;
1559 prm->header.size = htons (sizeof (buf));
1560 prm->type = htonl ((uint32_t) bd->type);
1561 prm->reserved = htons (0);
1562 prm->options = htons ((uint16_t) ro);
1563 prm->put_path_length = htons ((uint16_t) ppl);
1564 prm->get_path_length = htons ((uint16_t) get_path_length);
1566 prm->key = *query_hash;
1567 if (truncated)
1568 {
1569 void *tgt = &prm[1];
1570
1571 GNUNET_memcpy (tgt,
1572 trunc_peer,
1573 sizeof (struct GNUNET_PeerIdentity));
1574 paths = (struct GNUNET_DHT_PathElement *)
1575 (tgt + sizeof (struct GNUNET_PeerIdentity));
1576 }
1577 else
1578 {
1579 paths = (struct GNUNET_DHT_PathElement *) &prm[1];
1580 }
1581 if (NULL != put_path)
1582 {
1583 GNUNET_memcpy (paths,
1584 put_path,
1585 ppl * sizeof(struct GNUNET_DHT_PathElement));
1586 }
1587 else
1588 {
1589 GNUNET_assert (0 == ppl);
1590 }
1591 if (NULL != get_path)
1592 {
1593 GNUNET_memcpy (&paths[ppl],
1594 get_path,
1595 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
1596 }
1597 else
1598 {
1599 GNUNET_assert (0 == get_path_length);
1600 }
1601 if (tracking)
1602 {
1604 void *tgt = &paths[get_path_length + ppl];
1605 const struct GNUNET_PeerIdentity *pred;
1606
1607 if (ppl + get_path_length > 0)
1608 pred = &paths[ppl + get_path_length - 1].pred;
1609 else if (truncated)
1610 pred = trunc_peer;
1611 else
1612 pred = NULL; /* we are first! */
1613 /* Note that the last signature in 'paths' was not initialized before,
1614 so this is crucial to avoid sending garbage. */
1616 bd->data_size,
1618 bd->expiration_time,
1619 pred,
1620 &pi->id,
1621 &sig);
1622 memcpy (tgt,
1623 &sig,
1624 sizeof (sig));
1625 data = tgt + sizeof (sig);
1627 "Signing GET PATH %u/%u of %s => %s\n",
1628 ppl,
1629 get_path_length,
1630 GNUNET_h2s (query_hash),
1631 GNUNET_B2S (&sig));
1632#if SANITY_CHECKS > 1
1633 {
1634 struct GNUNET_DHT_PathElement xpaths[get_path_length + 1];
1635
1636 memcpy (xpaths,
1637 &paths[ppl],
1638 get_path_length * sizeof (struct GNUNET_DHT_PathElement));
1639 xpaths[get_path_length].sig = sig;
1640 xpaths[get_path_length].pred = GDS_my_identity;
1641 if (0 !=
1643 bd->data_size,
1644 bd->expiration_time,
1645 trunc_peer,
1646 paths,
1647 ppl,
1648 xpaths,
1649 get_path_length + 1,
1650 &pi->id))
1651 {
1652 GNUNET_break (0);
1653 return false;
1654 }
1655 }
1656#endif
1657 }
1658 else
1659 {
1660 data = &prm[1];
1661 }
1663 bd->data,
1664 bd->data_size);
1665 do_send (pi,
1666 &prm->header);
1667 }
1668 return true;
1669}
1670
1671
1679static enum GNUNET_GenericReturnValue
1681 const struct PeerPutMessage *put)
1682{
1683 enum GNUNET_DHT_RouteOption ro = ntohs (put->options);
1684 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
1685 bool has_path = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
1686 uint16_t msize = ntohs (put->header.size);
1687 uint16_t putlen = ntohs (put->put_path_length);
1688 size_t xsize = (has_path
1689 ? sizeof (struct GNUNET_CRYPTO_EddsaSignature)
1690 : 0)
1691 + (truncated
1692 ? sizeof (struct GNUNET_PeerIdentity)
1693 : 0);
1694 size_t var_meta_size
1695 = putlen * sizeof(struct GNUNET_DHT_PathElement)
1696 + xsize;
1697
1698 (void) cls;
1699 if ( (msize <
1700 sizeof (struct PeerPutMessage) + var_meta_size) ||
1701 (putlen >
1702 (GNUNET_MAX_MESSAGE_SIZE
1703 - sizeof (struct PeerPutMessage)
1704 - xsize)
1705 / sizeof(struct GNUNET_DHT_PathElement)) )
1706 {
1707 GNUNET_break_op (0);
1708 return GNUNET_SYSERR;
1709 }
1710 if (GNUNET_BLOCK_TYPE_ANY == htonl (put->type))
1711 {
1712 GNUNET_break_op (0);
1713 return GNUNET_SYSERR;
1714 }
1715 return GNUNET_OK;
1716}
1717
1718
1725static void
1727 const struct PeerPutMessage *put)
1728{
1729 struct Target *t = cls;
1730 struct PeerInfo *peer = t->pi;
1731 enum GNUNET_DHT_RouteOption ro = ntohs (put->options);
1732 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
1733 bool has_path = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
1734 uint16_t msize = ntohs (put->header.size);
1735 uint16_t putlen = ntohs (put->put_path_length);
1736 const struct GNUNET_PeerIdentity *trunc_peer
1737 = truncated
1738 ? (const struct GNUNET_PeerIdentity *) &put[1]
1739 : NULL;
1740 const struct GNUNET_DHT_PathElement *put_path
1741 = truncated
1742 ? (const struct GNUNET_DHT_PathElement *) &trunc_peer[1]
1743 : (const struct GNUNET_DHT_PathElement *) &put[1];
1744 const struct GNUNET_CRYPTO_EddsaSignature *last_sig
1745 = has_path
1746 ? (const struct GNUNET_CRYPTO_EddsaSignature *) &put_path[putlen]
1747 : NULL;
1748 const char *data
1749 = has_path
1750 ? (const char *) &last_sig[1]
1751 : (const char *) &put_path[putlen];
1752 size_t var_meta_size
1753 = putlen * sizeof(struct GNUNET_DHT_PathElement)
1754 + (has_path ? sizeof (*last_sig) : 0)
1755 + (truncated ? sizeof (*trunc_peer) : 0);
1756 struct GNUNET_DATACACHE_Block bd = {
1757 .key = put->key,
1758 .expiration_time = GNUNET_TIME_absolute_ntoh (put->expiration_time),
1759 .type = ntohl (put->type),
1760 .ro = ro,
1761 .data_size = msize - sizeof(*put) - var_meta_size,
1762 .data = data
1763 };
1764
1765 if (NULL != trunc_peer)
1766 bd.trunc_peer = *trunc_peer;
1768 "PUT for `%s' from %s with RO (%s/%s)\n",
1769 GNUNET_h2s (&put->key),
1770 GNUNET_i2s (&peer->id),
1771 (bd.ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE) ? "x" : "-",
1772 has_path ? "R" : "-");
1774 {
1776 "# Expired PUTs discarded",
1777 1,
1778 GNUNET_NO);
1779 return;
1780 }
1781 {
1782 /* Only call 'check_block' if that keeps our CPU load (from
1783 the cryptography) below 50% on average */
1784 static struct GNUNET_TIME_Relative avg_latency;
1785 static struct GNUNET_TIME_Absolute next_time;
1786
1787 if (GNUNET_TIME_absolute_is_past (next_time))
1788 {
1789 struct GNUNET_TIME_Absolute now
1791 struct GNUNET_TIME_Relative latency;
1793
1794 if (GNUNET_NO ==
1796 bd.type,
1797 bd.data,
1798 bd.data_size))
1799 {
1800 GNUNET_break_op (0);
1801 return;
1802 }
1803 latency = GNUNET_TIME_absolute_get_duration (now);
1804 /* Use *moving average* to estimate check_block latency */
1805 avg_latency
1808 GNUNET_TIME_relative_multiply (avg_latency,
1809 7),
1810 latency),
1811 8);
1812 /* average delay = 50% of avg_latency => 50% CPU load from crypto (at most) */
1815 avg_latency.rel_value_us > 0
1816 ? avg_latency.rel_value_us
1817 : 1LLU);
1819 }
1820 }
1821 if (! has_path)
1822 putlen = 0;
1824 "# P2P PUT requests received",
1825 1,
1826 GNUNET_NO);
1828 "# P2P PUT bytes received",
1829 msize,
1830 GNUNET_NO);
1831 {
1832 struct GNUNET_HashCode test_key;
1834
1836 bd.type,
1837 bd.data,
1838 bd.data_size,
1839 &test_key);
1840 switch (ret)
1841 {
1842 case GNUNET_YES:
1843 if (0 != GNUNET_memcmp (&test_key,
1844 &bd.key))
1845 {
1846 GNUNET_break_op (0);
1847 return;
1848 }
1849 break;
1850 case GNUNET_NO:
1851 /* cannot verify, good luck */
1852 break;
1853 case GNUNET_SYSERR:
1854 /* block type not supported, good luck */
1855 break;
1856 }
1857 }
1858
1859 {
1861 struct GNUNET_DHT_PathElement pp[putlen + 1];
1862
1868 &peer->phash));
1869 /* extend 'put path' by sender */
1870 bd.put_path = pp;
1871 bd.put_path_length = putlen + 1;
1872 if (has_path)
1873 {
1874 unsigned int failure_offset;
1875
1876 GNUNET_memcpy (pp,
1877 put_path,
1878 putlen * sizeof(struct GNUNET_DHT_PathElement));
1879 pp[putlen].pred = peer->id;
1880 pp[putlen].sig = *last_sig;
1881#if SANITY_CHECKS
1882 /* TODO: might want to eventually implement probabilistic
1883 load-based path verification, but for now it is all or nothing */
1884 failure_offset
1886 bd.data_size,
1887 bd.expiration_time,
1888 trunc_peer,
1889 pp,
1890 putlen + 1,
1891 NULL, 0, /* get_path */
1893#else
1894 failure_offset = 0;
1895#endif
1896 if (0 != failure_offset)
1897 {
1898 GNUNET_break_op (0);
1900 "Recorded put path invalid at offset %u, truncating\n",
1901 failure_offset);
1902 GNUNET_assert (failure_offset <= putlen + 1);
1903 bd.put_path = &pp[failure_offset];
1904 bd.put_path_length = (putlen + 1) - failure_offset;
1906 bd.trunc_peer = pp[failure_offset - 1].pred;
1907 }
1908 }
1909 else
1910 {
1911 bd.put_path_length = 0;
1912 }
1913
1914 /* give to local clients */
1916 &bd.key,
1917 0, NULL /* get path */));
1918
1919 /* store locally */
1920 if ( (0 != (bd.ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1921 (GDS_am_closest_peer (&put->key,
1922 bf)) )
1924 {
1925 enum GNUNET_GenericReturnValue forwarded;
1926
1927 /* route to other peers */
1928 forwarded
1930 ntohs (put->desired_replication_level),
1931 ntohs (put->hop_count),
1932 bf);
1933 /* notify monitoring clients */
1934 bd.ro |= ((GNUNET_OK == forwarded)
1936 : 0);
1938 ntohs (put->hop_count),
1939 ntohs (put->desired_replication_level));
1940 }
1942 }
1943}
1944
1945
1947{
1948 struct PeerInfo *pi;
1951};
1952
1953
1962static void
1964 const struct GNUNET_HashCode *query_hash,
1965 struct GNUNET_BLOCK_Group *bg)
1966{
1967
1968 if (NULL == GDS_my_hello)
1969 {
1971 "# FIND PEER requests ignored due to lack of HELLO",
1972 1,
1973 GNUNET_NO);
1974 }
1975 else if (GNUNET_BLOCK_REPLY_OK_MORE ==
1978 bg,
1980 NULL, 0,
1982 ntohs (GDS_my_hello->size)))
1983 {
1984 struct GNUNET_DATACACHE_Block bd = {
1986 .expiration_time
1989 .key = GDS_my_identity_hash,
1990 .data = GDS_my_hello,
1991 .data_size = ntohs (GDS_my_hello->size)
1992 };
1993
1995 &bd,
1996 query_hash,
1997 0, NULL /* get path */));
1998 }
1999 else
2000 {
2002 "# FIND PEER requests ignored due to Bloomfilter",
2003 1,
2004 GNUNET_NO);
2005 }
2006}
2007
2008
2017static void
2019 const struct GNUNET_HashCode *query_hash,
2020 struct GNUNET_BLOCK_Group *bg)
2021{
2022 /* Force non-random selection by hop count */
2023 struct PeerInfo *peer;
2024
2025 peer = select_peer (query_hash,
2026 NULL,
2027 GDS_NSE_get () + 1);
2028 if ( (NULL != peer->hello) &&
2034 bg,
2035 &peer->phash,
2036 NULL, 0, /* xquery */
2037 peer->hello,
2038 peer->hello_size)) )
2039 {
2040 struct GNUNET_DATACACHE_Block bd = {
2042 .expiration_time = peer->hello_expiration,
2043 .key = peer->phash,
2044 .data = peer->hello,
2045 .data_size = peer->hello_size
2046 };
2047
2049 &bd,
2050 query_hash,
2051 0, NULL /* get path */));
2052 }
2053}
2054
2055
2062static void
2064 const struct GNUNET_DATACACHE_Block *bd)
2065{
2066 struct PeerInfo *peer = cls;
2067
2069 bd,
2070 &bd->key,
2071 0, NULL /* get path */));
2072}
2073
2074
2082static enum GNUNET_GenericReturnValue
2084 const struct PeerGetMessage *get)
2085{
2086 uint16_t msize = ntohs (get->header.size);
2087 uint16_t result_filter_size = ntohs (get->result_filter_size);
2088
2089 (void) cls;
2090 if (msize < sizeof(*get) + result_filter_size)
2091 {
2092 GNUNET_break_op (0);
2093 return GNUNET_SYSERR;
2094 }
2095 return GNUNET_OK;
2096}
2097
2098
2105static void
2107 const struct PeerGetMessage *get)
2108{
2109 struct Target *t = cls;
2110 struct PeerInfo *peer = t->pi;
2111 uint16_t msize = ntohs (get->header.size);
2112 uint16_t result_filter_size = ntohs (get->result_filter_size);
2113 uint16_t hop_count = ntohs (get->hop_count);
2114 enum GNUNET_BLOCK_Type type = ntohl (get->type);
2115 enum GNUNET_DHT_RouteOption options = ntohs (get->options);
2117 const void *result_filter = (const void *) &get[1];
2118 const void *xquery = result_filter + result_filter_size;
2119 size_t xquery_size = msize - sizeof (*get) - result_filter_size;
2120
2121 /* parse and validate message */
2123 "# P2P GET requests received",
2124 1,
2125 GNUNET_NO);
2127 "# P2P GET bytes received",
2128 msize,
2129 GNUNET_NO);
2130 if (GNUNET_NO ==
2132 type,
2133 &get->key,
2134 xquery,
2135 xquery_size))
2136 {
2137 /* request invalid */
2138 GNUNET_break_op (0);
2139 return;
2140 }
2141
2142 {
2143 struct GNUNET_BLOCK_Group *bg;
2144 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
2145
2146 peer_bf = GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
2149 ;
2152 &peer->phash));
2154 type,
2155 result_filter,
2156 result_filter_size,
2157 "filter-size",
2158 result_filter_size,
2159 NULL);
2161 "GET for %s at %s after %u hops\n",
2162 GNUNET_h2s (&get->key),
2164 (unsigned int) hop_count);
2165 /* local lookup (this may update the bg) */
2167 (GDS_am_closest_peer (&get->key,
2168 peer_bf)) )
2169 {
2172 {
2174 "# P2P HELLO lookup requests processed",
2175 1,
2176 GNUNET_NO);
2178 &get->key,
2179 bg);
2182 &get->key,
2183 bg);
2184 }
2186 {
2188 eval = GDS_DATACACHE_get_closest (&get->key,
2189 type,
2190 xquery,
2191 xquery_size,
2192 bg,
2194 peer);
2195 else
2196 eval = GDS_DATACACHE_handle_get (&get->key,
2197 type,
2198 xquery,
2199 xquery_size,
2200 bg,
2202 peer);
2203 }
2204 }
2205 else
2206 {
2208 "# P2P GET requests ONLY routed",
2209 1,
2210 GNUNET_NO);
2211 }
2212
2213 /* remember request for routing replies
2214 TODO: why should we do this if GNUNET_BLOCK_REPLY_OK_LAST == eval?
2215 */
2216 GDS_ROUTING_add (&peer->id,
2217 type,
2218 bg, /* bg now owned by routing, but valid at least until end of this function! */
2219 options,
2220 &get->key,
2221 xquery,
2222 xquery_size);
2223
2224 /* P2P forwarding */
2225 {
2226 bool forwarded = false;
2227 uint16_t desired_replication_level = ntohs (
2228 get->desired_replication_level);
2229
2230 if (eval != GNUNET_BLOCK_REPLY_OK_LAST)
2231 forwarded = (GNUNET_OK ==
2233 options,
2234 desired_replication_level,
2235 hop_count,
2236 &get->key,
2237 xquery,
2238 xquery_size,
2239 bg,
2240 peer_bf));
2242 options
2243 | (forwarded
2244 ? 0
2246 type,
2247 hop_count,
2248 desired_replication_level,
2249 &get->key);
2250 }
2251 /* clean up; note that 'bg' is owned by routing now! */
2253 }
2254}
2255
2256
2266static bool
2268 const struct GNUNET_HashCode *query_hash,
2269 unsigned int get_path_length,
2270 const struct GNUNET_DHT_PathElement *get_path)
2271{
2272 /* forward to local clients */
2274 "Forwarding reply to local clients\n");
2275 if (! GDS_CLIENTS_handle_reply (bd,
2276 query_hash,
2277 get_path_length,
2278 get_path))
2279 {
2280 GNUNET_break (0);
2281 return false;
2282 }
2284 get_path,
2285 get_path_length);
2287 {
2288 struct GNUNET_DHT_PathElement xput_path[GNUNET_NZL (get_path_length
2289 + bd->put_path_length)];
2290 struct GNUNET_DATACACHE_Block bdx = *bd;
2291
2292 if (NULL != bd->put_path)
2293 GNUNET_memcpy (xput_path,
2294 bd->put_path,
2295 bd->put_path_length * sizeof(struct
2297 GNUNET_memcpy (&xput_path[bd->put_path_length],
2298 get_path,
2299 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
2300 bdx.put_path = xput_path;
2301 bdx.put_path_length += get_path_length;
2303 }
2304 /* forward to other peers */
2306 query_hash,
2307 get_path_length,
2308 get_path);
2309 return true;
2310}
2311
2312
2320static enum GNUNET_GenericReturnValue
2322 const struct PeerResultMessage *prm)
2323{
2324 uint16_t msize = ntohs (prm->header.size) - sizeof (*prm);
2325 enum GNUNET_DHT_RouteOption ro = ntohs (prm->options);
2326 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
2327 bool tracked = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
2328
2329 uint16_t get_path_length = ntohs (prm->get_path_length);
2330 uint16_t put_path_length = ntohs (prm->put_path_length);
2331 size_t vsize = (truncated ? sizeof (struct GNUNET_PeerIdentity) : 0)
2332 + (tracked ? sizeof (struct GNUNET_CRYPTO_EddsaSignature) : 0);
2333
2334 (void) cls;
2335 if ( (msize < vsize) ||
2336 (msize - vsize <
2337 (get_path_length + put_path_length)
2338 * sizeof(struct GNUNET_DHT_PathElement)) ||
2339 (get_path_length >
2340 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_DHT_PathElement)) ||
2341 (put_path_length >
2342 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_DHT_PathElement)) )
2343 {
2344 GNUNET_break_op (0);
2345 return GNUNET_SYSERR;
2346 }
2347 return GNUNET_OK;
2348}
2349
2350
2357static void
2359 const struct PeerResultMessage *prm)
2360{
2361 struct Target *t = cls;
2362 struct PeerInfo *peer = t->pi;
2363 uint16_t msize = ntohs (prm->header.size) - sizeof (*prm);
2364 enum GNUNET_DHT_RouteOption ro = ntohs (prm->options);
2365 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
2366 bool tracked = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
2367 uint16_t get_path_length = ntohs (prm->get_path_length);
2368 uint16_t put_path_length = ntohs (prm->put_path_length);
2369 const struct GNUNET_PeerIdentity *trunc_peer
2370 = truncated
2371 ? (const struct GNUNET_PeerIdentity *) &prm[1]
2372 : NULL;
2373 const struct GNUNET_DHT_PathElement *put_path
2374 = truncated
2375 ? (const struct GNUNET_DHT_PathElement *) &trunc_peer[1]
2376 : (const struct GNUNET_DHT_PathElement *) &prm[1];
2377 const struct GNUNET_DHT_PathElement *get_path
2378 = &put_path[put_path_length];
2379 const struct GNUNET_CRYPTO_EddsaSignature *last_sig
2380 = tracked
2381 ? (const struct GNUNET_CRYPTO_EddsaSignature *) &get_path[get_path_length]
2382 : NULL;
2383 const void *data
2384 = tracked
2385 ? (const void *) &last_sig[1]
2386 : (const void *) &get_path[get_path_length];
2387 size_t vsize = (truncated ? sizeof (struct GNUNET_PeerIdentity) : 0)
2388 + (tracked ? sizeof (struct GNUNET_CRYPTO_EddsaSignature) : 0);
2389 struct GNUNET_DATACACHE_Block bd = {
2391 .put_path = put_path,
2392 .put_path_length = put_path_length,
2393 .key = prm->key,
2394 .type = ntohl (prm->type),
2395 .ro = ro,
2396 .data = data,
2397 .data_size = msize - vsize - (get_path_length + put_path_length)
2398 * sizeof(struct GNUNET_DHT_PathElement)
2399 };
2400
2401 /* parse and validate message */
2402 if (GNUNET_TIME_absolute_is_past (bd.expiration_time))
2403 {
2405 "# Expired results discarded",
2406 1,
2407 GNUNET_NO);
2408 return;
2409 }
2410 if (GNUNET_OK !=
2412 bd.type,
2413 bd.data,
2414 bd.data_size))
2415 {
2416 GNUNET_break_op (0);
2417 return;
2418 }
2420 "# P2P RESULTS received",
2421 1,
2422 GNUNET_NO);
2424 "# P2P RESULT bytes received",
2425 msize,
2426 GNUNET_NO);
2427 {
2429
2431 bd.type,
2432 bd.data,
2433 bd.data_size,
2434 &bd.key);
2435 if (GNUNET_NO == ret)
2436 bd.key = prm->key;
2437 }
2438
2439 /* if we got a HELLO, consider it for our own routing table */
2440 hello_check (&bd);
2441
2442 /* Need to append 'peer' to 'get_path' */
2443 if (tracked)
2444 {
2445 struct GNUNET_DHT_PathElement xget_path[get_path_length + 1];
2446 struct GNUNET_DHT_PathElement *gp = xget_path;
2447 unsigned int failure_offset;
2448
2449 GNUNET_memcpy (xget_path,
2450 get_path,
2451 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
2452 xget_path[get_path_length].pred = peer->id;
2453 /* use memcpy(), as last_sig may not be aligned */
2454 memcpy (&xget_path[get_path_length].sig,
2455 last_sig,
2456 sizeof (*last_sig));
2457#if SANITY_CHECKS
2458 /* TODO: might want to eventually implement probabilistic
2459 load-based path verification, but for now it is all or nothing */
2460 failure_offset
2461 = GNUNET_DHT_verify_path (bd.data,
2462 bd.data_size,
2463 bd.expiration_time,
2464 trunc_peer,
2465 put_path,
2466 put_path_length,
2467 gp,
2468 get_path_length + 1,
2470#else
2471 failure_offset = 0;
2472#endif
2473 if (0 != failure_offset)
2474 {
2476 "Recorded path invalid at offset %u, truncating\n",
2477 failure_offset);
2478 GNUNET_assert (failure_offset <= bd.put_path_length + get_path_length
2479 + 1);
2480 if (failure_offset < bd.put_path_length)
2481 {
2482 /* failure on put path */
2483 trunc_peer = &bd.put_path[failure_offset - 1].pred;
2484 bd.ro |= GNUNET_DHT_RO_TRUNCATED;
2485 bd.put_path = &bd.put_path[failure_offset];
2486 bd.put_path_length -= failure_offset;
2487 truncated = true;
2488 }
2489 else
2490 {
2491 /* failure on get path */
2492 failure_offset -= bd.put_path_length;
2493 if (0 == failure_offset)
2494 trunc_peer = &bd.put_path[bd.put_path_length - 1].pred;
2495 else
2496 trunc_peer = &gp[failure_offset - 1].pred;
2497 get_path_length -= failure_offset;
2498 gp = &gp[failure_offset];
2499 bd.put_path_length = 0;
2500 bd.put_path = NULL;
2501 bd.ro |= GNUNET_DHT_RO_TRUNCATED;
2502 truncated = true;
2503 }
2504 }
2506 "Extending GET path of length %u with %s\n",
2507 get_path_length,
2508 GNUNET_i2s (&peer->id));
2509 if (truncated)
2510 {
2511 GNUNET_assert (NULL != trunc_peer);
2512 bd.trunc_peer = *trunc_peer;
2513 }
2515 &prm->key,
2516 get_path_length + 1,
2517 gp));
2518 }
2519 else
2520 {
2521 if (truncated)
2522 {
2523 GNUNET_assert (NULL != trunc_peer);
2524 bd.trunc_peer = *trunc_peer;
2525 }
2527 &prm->key,
2528 0,
2529 NULL));
2530 }
2531}
2532
2533
2541static enum GNUNET_GenericReturnValue
2543 const struct GNUNET_MessageHeader *hello)
2544{
2545 struct Target *t = cls;
2546 struct PeerInfo *peer = t->pi;
2548 size_t hellob_size;
2549 void *hellob;
2551
2553 &peer->id,
2554 &hellob,
2555 &hellob_size,
2556 &expiration);
2557 GNUNET_free (hellob);
2558 return ret;
2559}
2560
2561
2568static void
2570 const struct GNUNET_MessageHeader *hello)
2571{
2572 struct Target *t = cls;
2573 struct PeerInfo *peer = t->pi;
2574
2575 GNUNET_free (peer->hello);
2576 peer->hello_size = 0;
2579 &peer->id,
2580 &peer->hello,
2581 &peer->hello_size,
2582 &peer->hello_expiration));
2583}
2584
2585
2586void
2587GDS_u_receive (void *cls,
2588 void **tctx,
2589 void **sctx,
2590 const void *message,
2591 size_t message_size)
2592{
2593 struct Target *t = *tctx;
2594 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2595 GNUNET_MQ_hd_var_size (dht_p2p_get,
2597 struct PeerGetMessage,
2598 t),
2599 GNUNET_MQ_hd_var_size (dht_p2p_put,
2601 struct PeerPutMessage,
2602 t),
2603 GNUNET_MQ_hd_var_size (dht_p2p_result,
2605 struct PeerResultMessage,
2606 t),
2607 GNUNET_MQ_hd_var_size (dht_p2p_hello,
2609 struct GNUNET_MessageHeader,
2610 t),
2612 };
2613 const struct GNUNET_MessageHeader *mh = message;
2614
2615 (void) cls; /* the 'struct GDS_Underlay' */
2616 (void) sctx; /* our receiver address */
2617 if (NULL == t)
2618 {
2619 /* Received message claiming to originate from myself?
2620 Ignore! */
2621 GNUNET_break_op (0);
2622 return;
2623 }
2624 if (message_size < sizeof (*mh))
2625 {
2626 GNUNET_break_op (0);
2627 return;
2628 }
2629 if (message_size != ntohs (mh->size))
2630 {
2631 GNUNET_break_op (0);
2632 return;
2633 }
2635 "Handling message of type %u from peer %s\n",
2636 ntohs (mh->type),
2637 GNUNET_i2s (&t->pi->id));
2638 if (GNUNET_OK !=
2639 GNUNET_MQ_handle_message (core_handlers,
2640 mh))
2641 {
2642 GNUNET_break_op (0);
2643 return;
2644 }
2645}
2646
2647
2655void
2657 const struct GNUNET_PeerIdentity *pid,
2658 const char *uri)
2659{
2660 struct GNUNET_HashCode phash;
2661 int peer_bucket;
2662 struct PeerBucket *bucket;
2663 (void) cls;
2664
2665 if (0 == GNUNET_memcmp (&GDS_my_identity,
2666 pid))
2667 {
2669 "Got a HELLO for my own PID, ignoring it\n");
2670 return; /* that's us! */
2671 }
2673 sizeof(*pid),
2674 &phash);
2675 peer_bucket = find_bucket (&phash);
2676 GNUNET_assert ( (peer_bucket >= 0) &&
2677 ((unsigned int) peer_bucket < MAX_BUCKETS));
2678 bucket = &k_buckets[peer_bucket];
2679 for (struct PeerInfo *pi = bucket->head;
2680 NULL != pi;
2681 pi = pi->next)
2682 if (0 ==
2683 GNUNET_memcmp (&pi->id,
2684 pid))
2685 {
2686 /* already connected */
2688 uri);
2689 return;
2690 }
2691 if (bucket->peers_size >= bucket_size)
2692 return; /* do not care */
2694 "Discovered peer %s at %s suitable for bucket %d (%u/%u), trying to connect\n",
2695 GNUNET_i2s (pid),
2696 uri,
2697 peer_bucket,
2698 bucket->peers_size,
2699 bucket_size);
2700 /* new peer that we like! */
2702 uri);
2703}
2704
2705
2711void
2713{
2714 for (unsigned int bc = 0; bc<closest_bucket; bc++)
2715 {
2716 struct PeerBucket *bucket = &k_buckets[bc];
2717 unsigned int count = 0;
2718
2719 for (struct PeerInfo *pos = bucket->head;
2720 NULL != pos;
2721 pos = pos->next)
2722 {
2723 if (count >= bucket_size)
2724 break; /* we only consider first #bucket_size entries per bucket */
2725 count++;
2726 do_send (pos,
2727 msg);
2728 }
2729 }
2730}
2731
2732
2735{
2736
2737 unsigned long long temp_config_num;
2738
2741 "DHT",
2742 "DISABLE_TRY_CONNECT");
2743 if (GNUNET_OK ==
2745 "DHT",
2746 "bucket_size",
2747 &temp_config_num))
2748 bucket_size = (unsigned int) temp_config_num;
2751 "DHT",
2752 "CACHE_RESULTS");
2754 GNUNET_YES);
2755 return GNUNET_OK;
2756}
2757
2758
2759void
2761{
2762 if (NULL == all_connected_peers)
2763 return;
2764 GNUNET_assert (0 ==
2767 all_connected_peers = NULL;
2768 GNUNET_assert (NULL == find_peer_task);
2769}
2770
2771
2772struct GNUNET_PeerIdentity *
2774{
2775 return &GDS_my_identity;
2776}
2777
2778
2779/* 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
void 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)
Definition: dht_helper.c:178
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:36
void 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, struct GNUNET_CRYPTO_EddsaSignature *sig)
Sign that we are routing a message from pred to succ.
Definition: dht_helper.c:151
Helper functions for DHT.
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
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 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_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
struct GNUNET_PeerIdentity GDS_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_CRYPTO_EddsaPrivateKey GDS_my_private_key
Our private key.
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.
struct GNUNET_HashCode GDS_my_identity_hash
Hash of the identity of this peer.
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.
struct GNUNET_BLOCK_Context * GDS_block_context
Our handle to the BLOCK library.
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.
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...
bool 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)
Handle a reply (route to origin).
static void handle_dht_p2p_result(void *cls, const struct PeerResultMessage *prm)
Core handler for p2p result messages.
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_put(const struct GNUNET_DATACACHE_Block *bd, uint16_t desired_replication_level, uint16_t hop_count, struct GNUNET_CONTAINER_BloomFilter *bf)
Perform a PUT operation.
#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.
struct PeerInfo * GDS_NEIGHBOURS_lookup_peer(const struct GNUNET_PeerIdentity *target)
Lookup peer by peer's identity.
struct GNUNET_PeerIdentity * GDS_NEIGHBOURS_get_id()
Get the ID of the local node.
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 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 void handle_find_local_hello(struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg)
We have received a request for nearby HELLOs.
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 void do_send(struct PeerInfo *pi, const struct GNUNET_MessageHeader *msg)
Send msg to pi.
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.
#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 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.
#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 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 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 unsigned int bucket_size
Maximum size for each bucket.
static void handle_find_my_hello(struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg)
We have received a request for a HELLO.
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
The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
static struct PeerBucket k_buckets[sizeof(struct GNUNET_HashCode) *8]
The buckets.
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_init()
Initialize neighbours subsystem.
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.
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_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_PeerIdentity pid
Identity of the peer we transmit to / connect to.
static struct GNUNET_SCHEDULER_Task * t
Main task.
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.
Helper library for handling HELLO URIs.
Constants for network protocols.
#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(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
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:1349
@ 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:41
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
Definition: crypto_hash.c:132
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).
Definition: crypto_hash.c:240
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:379
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:997
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:975
#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:523
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros(const struct GNUNET_HashCode *h)
Count the number of leading 0 bits in h.
Definition: crypto_hash.c:174
#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_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:205
#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:980
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:1304
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:1277
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:741
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:587
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:552
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:640
bool GNUNET_TIME_absolute_is_past(struct GNUNET_TIME_Absolute abs)
Test if abs is truly in the past (excluding now).
Definition: time.c:671
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:36
const struct GNUNET_HashCode * query_hash
struct PeerInfo * pi
struct GNUNET_BLOCK_Group * bg
Information we keep per underlay.
Block group data.
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:232
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:136
struct GNUNET_SCHEDULER_Task * next
This is a linked list.
Definition: scheduler.c:140
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.
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 hop_count
Hop count.
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.