diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-12-05 10:25:18 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-12-05 10:25:18 +0000 |
commit | c015c4ba3cd0733d628df63f9d2e4d2a04247c52 (patch) | |
tree | ba55de289d775681fe227104b85dced245c63e8b /src/dht/gnunet-service-xdht_neighbours.c | |
parent | ba295d24fe546446aae8e78cc112af3ae84d4ee0 (diff) | |
download | gnunet-c015c4ba3cd0733d628df63f9d2e4d2a04247c52.tar.gz gnunet-c015c4ba3cd0733d628df63f9d2e4d2a04247c52.zip |
-template for supriti/xvine
Diffstat (limited to 'src/dht/gnunet-service-xdht_neighbours.c')
-rw-r--r-- | src/dht/gnunet-service-xdht_neighbours.c | 2227 |
1 files changed, 2227 insertions, 0 deletions
diff --git a/src/dht/gnunet-service-xdht_neighbours.c b/src/dht/gnunet-service-xdht_neighbours.c new file mode 100644 index 000000000..f15a33c79 --- /dev/null +++ b/src/dht/gnunet-service-xdht_neighbours.c | |||
@@ -0,0 +1,2227 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009-2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | 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 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_neighbours.c | ||
23 | * @brief GNUnet DHT service's bucket and neighbour management code | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_block_lib.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_constants.h" | ||
33 | #include "gnunet_protocols.h" | ||
34 | #include "gnunet_nse_service.h" | ||
35 | #include "gnunet_ats_service.h" | ||
36 | #include "gnunet_core_service.h" | ||
37 | #include "gnunet_datacache_lib.h" | ||
38 | #include "gnunet_transport_service.h" | ||
39 | #include "gnunet_hello_lib.h" | ||
40 | #include "gnunet_dht_service.h" | ||
41 | #include "gnunet_statistics_service.h" | ||
42 | #include "gnunet-service-dht.h" | ||
43 | #include "gnunet-service-dht_clients.h" | ||
44 | #include "gnunet-service-dht_datacache.h" | ||
45 | #include "gnunet-service-dht_hello.h" | ||
46 | #include "gnunet-service-dht_neighbours.h" | ||
47 | #include "gnunet-service-dht_nse.h" | ||
48 | #include "gnunet-service-dht_routing.h" | ||
49 | #include <fenv.h> | ||
50 | #include "dht.h" | ||
51 | |||
52 | #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__) | ||
53 | |||
54 | /** | ||
55 | * How many buckets will we allow total. | ||
56 | */ | ||
57 | #define MAX_BUCKETS sizeof (struct GNUNET_HashCode) * 8 | ||
58 | |||
59 | /** | ||
60 | * What is the maximum number of peers in a given bucket. | ||
61 | */ | ||
62 | #define DEFAULT_BUCKET_SIZE 8 | ||
63 | |||
64 | /** | ||
65 | * Desired replication level for FIND PEER requests | ||
66 | */ | ||
67 | #define FIND_PEER_REPLICATION_LEVEL 4 | ||
68 | |||
69 | /** | ||
70 | * Maximum allowed replication level for all requests. | ||
71 | */ | ||
72 | #define MAXIMUM_REPLICATION_LEVEL 16 | ||
73 | |||
74 | /** | ||
75 | * Maximum allowed number of pending messages per peer. | ||
76 | */ | ||
77 | #define MAXIMUM_PENDING_PER_PEER 64 | ||
78 | |||
79 | /** | ||
80 | * How often to update our preference levels for peers in our routing tables. | ||
81 | */ | ||
82 | #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2) | ||
83 | |||
84 | /** | ||
85 | * How long at least to wait before sending another find peer request. | ||
86 | */ | ||
87 | #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30) | ||
88 | |||
89 | /** | ||
90 | * How long at most to wait before sending another find peer request. | ||
91 | */ | ||
92 | #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10) | ||
93 | |||
94 | /** | ||
95 | * How long at most to wait for transmission of a GET request to another peer? | ||
96 | */ | ||
97 | #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2) | ||
98 | |||
99 | /** | ||
100 | * Hello address expiration | ||
101 | */ | ||
102 | extern struct GNUNET_TIME_Relative hello_expiration; | ||
103 | |||
104 | |||
105 | GNUNET_NETWORK_STRUCT_BEGIN | ||
106 | |||
107 | /** | ||
108 | * P2P PUT message | ||
109 | */ | ||
110 | struct PeerPutMessage | ||
111 | { | ||
112 | /** | ||
113 | * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT | ||
114 | */ | ||
115 | struct GNUNET_MessageHeader header; | ||
116 | |||
117 | /** | ||
118 | * Processing options | ||
119 | */ | ||
120 | uint32_t options GNUNET_PACKED; | ||
121 | |||
122 | /** | ||
123 | * Content type. | ||
124 | */ | ||
125 | uint32_t type GNUNET_PACKED; | ||
126 | |||
127 | /** | ||
128 | * Hop count | ||
129 | */ | ||
130 | uint32_t hop_count GNUNET_PACKED; | ||
131 | |||
132 | /** | ||
133 | * Replication level for this message | ||
134 | */ | ||
135 | uint32_t desired_replication_level GNUNET_PACKED; | ||
136 | |||
137 | /** | ||
138 | * Length of the PUT path that follows (if tracked). | ||
139 | */ | ||
140 | uint32_t put_path_length GNUNET_PACKED; | ||
141 | |||
142 | /** | ||
143 | * When does the content expire? | ||
144 | */ | ||
145 | struct GNUNET_TIME_AbsoluteNBO expiration_time; | ||
146 | |||
147 | /** | ||
148 | * Bloomfilter (for peer identities) to stop circular routes | ||
149 | */ | ||
150 | char bloomfilter[DHT_BLOOM_SIZE]; | ||
151 | |||
152 | /** | ||
153 | * The key we are storing under. | ||
154 | */ | ||
155 | struct GNUNET_HashCode key; | ||
156 | |||
157 | /* put path (if tracked) */ | ||
158 | |||
159 | /* Payload */ | ||
160 | |||
161 | }; | ||
162 | |||
163 | |||
164 | /** | ||
165 | * P2P Result message | ||
166 | */ | ||
167 | struct PeerResultMessage | ||
168 | { | ||
169 | /** | ||
170 | * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT | ||
171 | */ | ||
172 | struct GNUNET_MessageHeader header; | ||
173 | |||
174 | /** | ||
175 | * Content type. | ||
176 | */ | ||
177 | uint32_t type GNUNET_PACKED; | ||
178 | |||
179 | /** | ||
180 | * Length of the PUT path that follows (if tracked). | ||
181 | */ | ||
182 | uint32_t put_path_length GNUNET_PACKED; | ||
183 | |||
184 | /** | ||
185 | * Length of the GET path that follows (if tracked). | ||
186 | */ | ||
187 | uint32_t get_path_length GNUNET_PACKED; | ||
188 | |||
189 | /** | ||
190 | * When does the content expire? | ||
191 | */ | ||
192 | struct GNUNET_TIME_AbsoluteNBO expiration_time; | ||
193 | |||
194 | /** | ||
195 | * The key of the corresponding GET request. | ||
196 | */ | ||
197 | struct GNUNET_HashCode key; | ||
198 | |||
199 | /* put path (if tracked) */ | ||
200 | |||
201 | /* get path (if tracked) */ | ||
202 | |||
203 | /* Payload */ | ||
204 | |||
205 | }; | ||
206 | |||
207 | |||
208 | /** | ||
209 | * P2P GET message | ||
210 | */ | ||
211 | struct PeerGetMessage | ||
212 | { | ||
213 | /** | ||
214 | * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET | ||
215 | */ | ||
216 | struct GNUNET_MessageHeader header; | ||
217 | |||
218 | /** | ||
219 | * Processing options | ||
220 | */ | ||
221 | uint32_t options GNUNET_PACKED; | ||
222 | |||
223 | /** | ||
224 | * Desired content type. | ||
225 | */ | ||
226 | uint32_t type GNUNET_PACKED; | ||
227 | |||
228 | /** | ||
229 | * Hop count | ||
230 | */ | ||
231 | uint32_t hop_count GNUNET_PACKED; | ||
232 | |||
233 | /** | ||
234 | * Desired replication level for this request. | ||
235 | */ | ||
236 | uint32_t desired_replication_level GNUNET_PACKED; | ||
237 | |||
238 | /** | ||
239 | * Size of the extended query. | ||
240 | */ | ||
241 | uint32_t xquery_size; | ||
242 | |||
243 | /** | ||
244 | * Bloomfilter mutator. | ||
245 | */ | ||
246 | uint32_t bf_mutator; | ||
247 | |||
248 | /** | ||
249 | * Bloomfilter (for peer identities) to stop circular routes | ||
250 | */ | ||
251 | char bloomfilter[DHT_BLOOM_SIZE]; | ||
252 | |||
253 | /** | ||
254 | * The key we are looking for. | ||
255 | */ | ||
256 | struct GNUNET_HashCode key; | ||
257 | |||
258 | /* xquery */ | ||
259 | |||
260 | /* result bloomfilter */ | ||
261 | |||
262 | }; | ||
263 | GNUNET_NETWORK_STRUCT_END | ||
264 | |||
265 | /** | ||
266 | * Linked list of messages to send to a particular other peer. | ||
267 | */ | ||
268 | struct P2PPendingMessage | ||
269 | { | ||
270 | /** | ||
271 | * Pointer to next item in the list | ||
272 | */ | ||
273 | struct P2PPendingMessage *next; | ||
274 | |||
275 | /** | ||
276 | * Pointer to previous item in the list | ||
277 | */ | ||
278 | struct P2PPendingMessage *prev; | ||
279 | |||
280 | /** | ||
281 | * Message importance level. FIXME: used? useful? | ||
282 | */ | ||
283 | unsigned int importance; | ||
284 | |||
285 | /** | ||
286 | * When does this message time out? | ||
287 | */ | ||
288 | struct GNUNET_TIME_Absolute timeout; | ||
289 | |||
290 | /** | ||
291 | * Actual message to be sent, allocated at the end of the struct: | ||
292 | * // msg = (cast) &pm[1]; | ||
293 | * // memcpy (&pm[1], data, len); | ||
294 | */ | ||
295 | const struct GNUNET_MessageHeader *msg; | ||
296 | |||
297 | }; | ||
298 | |||
299 | |||
300 | /** | ||
301 | * Entry for a peer in a bucket. | ||
302 | */ | ||
303 | struct PeerInfo | ||
304 | { | ||
305 | /** | ||
306 | * Next peer entry (DLL) | ||
307 | */ | ||
308 | struct PeerInfo *next; | ||
309 | |||
310 | /** | ||
311 | * Prev peer entry (DLL) | ||
312 | */ | ||
313 | struct PeerInfo *prev; | ||
314 | |||
315 | /** | ||
316 | * Count of outstanding messages for peer. | ||
317 | */ | ||
318 | unsigned int pending_count; | ||
319 | |||
320 | /** | ||
321 | * Head of pending messages to be sent to this peer. | ||
322 | */ | ||
323 | struct P2PPendingMessage *head; | ||
324 | |||
325 | /** | ||
326 | * Tail of pending messages to be sent to this peer. | ||
327 | */ | ||
328 | struct P2PPendingMessage *tail; | ||
329 | |||
330 | /** | ||
331 | * Core handle for sending messages to this peer. | ||
332 | */ | ||
333 | struct GNUNET_CORE_TransmitHandle *th; | ||
334 | |||
335 | /** | ||
336 | * Task for scheduling preference updates | ||
337 | */ | ||
338 | GNUNET_SCHEDULER_TaskIdentifier preference_task; | ||
339 | |||
340 | /** | ||
341 | * What is the identity of the peer? | ||
342 | */ | ||
343 | struct GNUNET_PeerIdentity id; | ||
344 | |||
345 | #if 0 | ||
346 | /** | ||
347 | * What is the average latency for replies received? | ||
348 | */ | ||
349 | struct GNUNET_TIME_Relative latency; | ||
350 | |||
351 | /** | ||
352 | * Transport level distance to peer. | ||
353 | */ | ||
354 | unsigned int distance; | ||
355 | #endif | ||
356 | |||
357 | }; | ||
358 | |||
359 | |||
360 | /** | ||
361 | * Peers are grouped into buckets. | ||
362 | */ | ||
363 | struct PeerBucket | ||
364 | { | ||
365 | /** | ||
366 | * Head of DLL | ||
367 | */ | ||
368 | struct PeerInfo *head; | ||
369 | |||
370 | /** | ||
371 | * Tail of DLL | ||
372 | */ | ||
373 | struct PeerInfo *tail; | ||
374 | |||
375 | /** | ||
376 | * Number of peers in the bucket. | ||
377 | */ | ||
378 | unsigned int peers_size; | ||
379 | }; | ||
380 | |||
381 | |||
382 | /** | ||
383 | * Do we cache all results that we are routing in the local datacache? | ||
384 | */ | ||
385 | static int cache_results; | ||
386 | |||
387 | /** | ||
388 | * Should routing details be logged to stderr (for debugging)? | ||
389 | */ | ||
390 | static int log_route_details_stderr; | ||
391 | |||
392 | /** | ||
393 | * The lowest currently used bucket, initially 0 (for 0-bits matching bucket). | ||
394 | */ | ||
395 | static unsigned int closest_bucket; | ||
396 | |||
397 | /** | ||
398 | * How many peers have we added since we sent out our last | ||
399 | * find peer request? | ||
400 | */ | ||
401 | static unsigned int newly_found_peers; | ||
402 | |||
403 | /** | ||
404 | * Option for testing that disables the 'connect' function of the DHT. | ||
405 | */ | ||
406 | static int disable_try_connect; | ||
407 | |||
408 | /** | ||
409 | * The buckets. Array of size MAX_BUCKET_SIZE. Offset 0 means 0 bits matching. | ||
410 | */ | ||
411 | static struct PeerBucket k_buckets[MAX_BUCKETS]; | ||
412 | |||
413 | /** | ||
414 | * Hash map of all known peers, for easy removal from k_buckets on disconnect. | ||
415 | */ | ||
416 | static struct GNUNET_CONTAINER_MultiPeerMap *all_known_peers; | ||
417 | |||
418 | /** | ||
419 | * Maximum size for each bucket. | ||
420 | */ | ||
421 | static unsigned int bucket_size = DEFAULT_BUCKET_SIZE; | ||
422 | |||
423 | /** | ||
424 | * Task that sends FIND PEER requests. | ||
425 | */ | ||
426 | static GNUNET_SCHEDULER_TaskIdentifier find_peer_task; | ||
427 | |||
428 | /** | ||
429 | * Identity of this peer. | ||
430 | */ | ||
431 | static struct GNUNET_PeerIdentity my_identity; | ||
432 | |||
433 | /** | ||
434 | * Hash of the identity of this peer. | ||
435 | */ | ||
436 | static struct GNUNET_HashCode my_identity_hash; | ||
437 | |||
438 | /** | ||
439 | * Handle to CORE. | ||
440 | */ | ||
441 | static struct GNUNET_CORE_Handle *core_api; | ||
442 | |||
443 | /** | ||
444 | * Handle to ATS. | ||
445 | */ | ||
446 | static struct GNUNET_ATS_PerformanceHandle *atsAPI; | ||
447 | |||
448 | |||
449 | |||
450 | /** | ||
451 | * Find the optimal bucket for this key. | ||
452 | * | ||
453 | * @param hc the hashcode to compare our identity to | ||
454 | * @return the proper bucket index, or GNUNET_SYSERR | ||
455 | * on error (same hashcode) | ||
456 | */ | ||
457 | static int | ||
458 | find_bucket (const struct GNUNET_HashCode *hc) | ||
459 | { | ||
460 | unsigned int bits; | ||
461 | |||
462 | bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, hc); | ||
463 | if (bits == MAX_BUCKETS) | ||
464 | { | ||
465 | /* How can all bits match? Got my own ID? */ | ||
466 | GNUNET_break (0); | ||
467 | return GNUNET_SYSERR; | ||
468 | } | ||
469 | return MAX_BUCKETS - bits - 1; | ||
470 | } | ||
471 | |||
472 | |||
473 | /** | ||
474 | * Let GNUnet core know that we like the given peer. | ||
475 | * | ||
476 | * @param cls the `struct PeerInfo` of the peer | ||
477 | * @param tc scheduler context. | ||
478 | */ | ||
479 | static void | ||
480 | update_core_preference (void *cls, | ||
481 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
482 | { | ||
483 | struct PeerInfo *peer = cls; | ||
484 | uint64_t preference; | ||
485 | unsigned int matching; | ||
486 | int bucket; | ||
487 | struct GNUNET_HashCode phash; | ||
488 | |||
489 | peer->preference_task = GNUNET_SCHEDULER_NO_TASK; | ||
490 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
491 | return; | ||
492 | GNUNET_CRYPTO_hash (&peer->id, | ||
493 | sizeof (struct GNUNET_PeerIdentity), | ||
494 | &phash); | ||
495 | matching = | ||
496 | GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, | ||
497 | &phash); | ||
498 | if (matching >= 64) | ||
499 | matching = 63; | ||
500 | bucket = find_bucket (&phash); | ||
501 | if (bucket == GNUNET_SYSERR) | ||
502 | preference = 0; | ||
503 | else | ||
504 | { | ||
505 | GNUNET_assert (k_buckets[bucket].peers_size != 0); | ||
506 | preference = (1LL << matching) / k_buckets[bucket].peers_size; | ||
507 | } | ||
508 | if (preference == 0) | ||
509 | { | ||
510 | peer->preference_task = | ||
511 | GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL, | ||
512 | &update_core_preference, peer); | ||
513 | return; | ||
514 | } | ||
515 | GNUNET_STATISTICS_update (GDS_stats, | ||
516 | gettext_noop ("# Preference updates given to core"), | ||
517 | 1, GNUNET_NO); | ||
518 | GNUNET_ATS_performance_change_preference (atsAPI, &peer->id, | ||
519 | GNUNET_ATS_PREFERENCE_BANDWIDTH, | ||
520 | (double) preference, GNUNET_ATS_PREFERENCE_END); | ||
521 | peer->preference_task = | ||
522 | GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL, | ||
523 | &update_core_preference, peer); | ||
524 | |||
525 | |||
526 | } | ||
527 | |||
528 | |||
529 | /** | ||
530 | * Closure for 'add_known_to_bloom'. | ||
531 | */ | ||
532 | struct BloomConstructorContext | ||
533 | { | ||
534 | /** | ||
535 | * Bloom filter under construction. | ||
536 | */ | ||
537 | struct GNUNET_CONTAINER_BloomFilter *bloom; | ||
538 | |||
539 | /** | ||
540 | * Mutator to use. | ||
541 | */ | ||
542 | uint32_t bf_mutator; | ||
543 | }; | ||
544 | |||
545 | |||
546 | /** | ||
547 | * Add each of the peers we already know to the bloom filter of | ||
548 | * the request so that we don't get duplicate HELLOs. | ||
549 | * | ||
550 | * @param cls the 'struct BloomConstructorContext'. | ||
551 | * @param key peer identity to add to the bloom filter | ||
552 | * @param value value the peer information (unused) | ||
553 | * @return #GNUNET_YES (we should continue to iterate) | ||
554 | */ | ||
555 | static int | ||
556 | add_known_to_bloom (void *cls, | ||
557 | const struct GNUNET_PeerIdentity *key, | ||
558 | void *value) | ||
559 | { | ||
560 | struct BloomConstructorContext *ctx = cls; | ||
561 | struct GNUNET_HashCode key_hash; | ||
562 | struct GNUNET_HashCode mh; | ||
563 | |||
564 | GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_PeerIdentity), &key_hash); | ||
565 | GNUNET_BLOCK_mingle_hash (&key_hash, ctx->bf_mutator, &mh); | ||
566 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
567 | "Adding known peer (%s) to bloomfilter for FIND PEER with mutation %u\n", | ||
568 | GNUNET_i2s (key), ctx->bf_mutator); | ||
569 | GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh); | ||
570 | return GNUNET_YES; | ||
571 | } | ||
572 | |||
573 | |||
574 | /** | ||
575 | * Task to send a find peer message for our own peer identifier | ||
576 | * so that we can find the closest peers in the network to ourselves | ||
577 | * and attempt to connect to them. | ||
578 | * | ||
579 | * @param cls closure for this task | ||
580 | * @param tc the context under which the task is running | ||
581 | */ | ||
582 | static void | ||
583 | send_find_peer_message (void *cls, | ||
584 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
585 | { | ||
586 | struct GNUNET_TIME_Relative next_send_time; | ||
587 | struct BloomConstructorContext bcc; | ||
588 | struct GNUNET_CONTAINER_BloomFilter *peer_bf; | ||
589 | |||
590 | find_peer_task = GNUNET_SCHEDULER_NO_TASK; | ||
591 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
592 | return; | ||
593 | if (newly_found_peers > bucket_size) | ||
594 | { | ||
595 | /* If we are finding many peers already, no need to send out our request right now! */ | ||
596 | find_peer_task = | ||
597 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, | ||
598 | &send_find_peer_message, NULL); | ||
599 | newly_found_peers = 0; | ||
600 | return; | ||
601 | } | ||
602 | bcc.bf_mutator = | ||
603 | GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); | ||
604 | bcc.bloom = | ||
605 | GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, | ||
606 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
607 | GNUNET_CONTAINER_multipeermap_iterate (all_known_peers, &add_known_to_bloom, | ||
608 | &bcc); | ||
609 | GNUNET_STATISTICS_update (GDS_stats, | ||
610 | gettext_noop ("# FIND PEER messages initiated"), 1, | ||
611 | GNUNET_NO); | ||
612 | peer_bf = | ||
613 | GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, | ||
614 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
615 | // FIXME: pass priority!? | ||
616 | GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO, | ||
617 | GNUNET_DHT_RO_FIND_PEER, | ||
618 | FIND_PEER_REPLICATION_LEVEL, 0, | ||
619 | &my_identity_hash, NULL, 0, bcc.bloom, | ||
620 | bcc.bf_mutator, peer_bf); | ||
621 | GNUNET_CONTAINER_bloomfilter_free (peer_bf); | ||
622 | GNUNET_CONTAINER_bloomfilter_free (bcc.bloom); | ||
623 | /* schedule next round */ | ||
624 | next_send_time.rel_value_us = | ||
625 | DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value_us + | ||
626 | GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
627 | DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value_us / | ||
628 | (newly_found_peers + 1)); | ||
629 | newly_found_peers = 0; | ||
630 | find_peer_task = | ||
631 | GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message, | ||
632 | NULL); | ||
633 | } | ||
634 | |||
635 | |||
636 | /** | ||
637 | * Method called whenever a peer connects. | ||
638 | * | ||
639 | * @param cls closure | ||
640 | * @param peer peer identity this notification is about | ||
641 | */ | ||
642 | static void | ||
643 | handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
644 | { | ||
645 | struct PeerInfo *ret; | ||
646 | struct GNUNET_HashCode phash; | ||
647 | int peer_bucket; | ||
648 | |||
649 | /* Check for connect to self message */ | ||
650 | if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))) | ||
651 | return; | ||
652 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
653 | "Connected to %s\n", | ||
654 | GNUNET_i2s (peer)); | ||
655 | if (GNUNET_YES == | ||
656 | GNUNET_CONTAINER_multipeermap_contains (all_known_peers, | ||
657 | peer)) | ||
658 | { | ||
659 | GNUNET_break (0); | ||
660 | return; | ||
661 | } | ||
662 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), 1, | ||
663 | GNUNET_NO); | ||
664 | GNUNET_CRYPTO_hash (peer, | ||
665 | sizeof (struct GNUNET_PeerIdentity), | ||
666 | &phash); | ||
667 | peer_bucket = find_bucket (&phash); | ||
668 | GNUNET_assert ((peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS)); | ||
669 | ret = GNUNET_malloc (sizeof (struct PeerInfo)); | ||
670 | #if 0 | ||
671 | ret->latency = latency; | ||
672 | ret->distance = distance; | ||
673 | #endif | ||
674 | ret->id = *peer; | ||
675 | GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head, | ||
676 | k_buckets[peer_bucket].tail, ret); | ||
677 | k_buckets[peer_bucket].peers_size++; | ||
678 | closest_bucket = GNUNET_MAX (closest_bucket, peer_bucket); | ||
679 | if ((peer_bucket > 0) && (k_buckets[peer_bucket].peers_size <= bucket_size)) | ||
680 | { | ||
681 | ret->preference_task = | ||
682 | GNUNET_SCHEDULER_add_now (&update_core_preference, ret); | ||
683 | newly_found_peers++; | ||
684 | } | ||
685 | GNUNET_assert (GNUNET_OK == | ||
686 | GNUNET_CONTAINER_multipeermap_put (all_known_peers, | ||
687 | peer, ret, | ||
688 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
689 | if (1 == GNUNET_CONTAINER_multipeermap_size (all_known_peers) && | ||
690 | (GNUNET_YES != disable_try_connect)) | ||
691 | { | ||
692 | /* got a first connection, good time to start with FIND PEER requests... */ | ||
693 | find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL); | ||
694 | } | ||
695 | } | ||
696 | |||
697 | |||
698 | /** | ||
699 | * Method called whenever a peer disconnects. | ||
700 | * | ||
701 | * @param cls closure | ||
702 | * @param peer peer identity this notification is about | ||
703 | */ | ||
704 | static void | ||
705 | handle_core_disconnect (void *cls, | ||
706 | const struct GNUNET_PeerIdentity *peer) | ||
707 | { | ||
708 | struct PeerInfo *to_remove; | ||
709 | int current_bucket; | ||
710 | struct P2PPendingMessage *pos; | ||
711 | unsigned int discarded; | ||
712 | struct GNUNET_HashCode phash; | ||
713 | |||
714 | /* Check for disconnect from self message */ | ||
715 | if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))) | ||
716 | return; | ||
717 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
718 | "Disconnected %s\n", | ||
719 | GNUNET_i2s (peer)); | ||
720 | to_remove = | ||
721 | GNUNET_CONTAINER_multipeermap_get (all_known_peers, peer); | ||
722 | if (NULL == to_remove) | ||
723 | { | ||
724 | GNUNET_break (0); | ||
725 | return; | ||
726 | } | ||
727 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), -1, | ||
728 | GNUNET_NO); | ||
729 | GNUNET_assert (GNUNET_YES == | ||
730 | GNUNET_CONTAINER_multipeermap_remove (all_known_peers, | ||
731 | peer, | ||
732 | to_remove)); | ||
733 | if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task) | ||
734 | { | ||
735 | GNUNET_SCHEDULER_cancel (to_remove->preference_task); | ||
736 | to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK; | ||
737 | } | ||
738 | GNUNET_CRYPTO_hash (peer, | ||
739 | sizeof (struct GNUNET_PeerIdentity), | ||
740 | &phash); | ||
741 | current_bucket = find_bucket (&phash); | ||
742 | GNUNET_assert (current_bucket >= 0); | ||
743 | GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head, | ||
744 | k_buckets[current_bucket].tail, to_remove); | ||
745 | GNUNET_assert (k_buckets[current_bucket].peers_size > 0); | ||
746 | k_buckets[current_bucket].peers_size--; | ||
747 | while ((closest_bucket > 0) && (k_buckets[closest_bucket].peers_size == 0)) | ||
748 | closest_bucket--; | ||
749 | |||
750 | if (to_remove->th != NULL) | ||
751 | { | ||
752 | GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th); | ||
753 | to_remove->th = NULL; | ||
754 | } | ||
755 | discarded = 0; | ||
756 | while (NULL != (pos = to_remove->head)) | ||
757 | { | ||
758 | GNUNET_CONTAINER_DLL_remove (to_remove->head, to_remove->tail, pos); | ||
759 | discarded++; | ||
760 | GNUNET_free (pos); | ||
761 | } | ||
762 | GNUNET_STATISTICS_update (GDS_stats, | ||
763 | gettext_noop | ||
764 | ("# Queued messages discarded (peer disconnected)"), | ||
765 | discarded, GNUNET_NO); | ||
766 | GNUNET_free (to_remove); | ||
767 | } | ||
768 | |||
769 | |||
770 | /** | ||
771 | * Called when core is ready to send a message we asked for | ||
772 | * out to the destination. | ||
773 | * | ||
774 | * @param cls the 'struct PeerInfo' of the target peer | ||
775 | * @param size number of bytes available in buf | ||
776 | * @param buf where the callee should write the message | ||
777 | * @return number of bytes written to buf | ||
778 | */ | ||
779 | static size_t | ||
780 | core_transmit_notify (void *cls, size_t size, void *buf) | ||
781 | { | ||
782 | struct PeerInfo *peer = cls; | ||
783 | char *cbuf = buf; | ||
784 | struct P2PPendingMessage *pending; | ||
785 | size_t off; | ||
786 | size_t msize; | ||
787 | |||
788 | peer->th = NULL; | ||
789 | while ((NULL != (pending = peer->head)) && | ||
790 | (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us)) | ||
791 | { | ||
792 | peer->pending_count--; | ||
793 | GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending); | ||
794 | GNUNET_free (pending); | ||
795 | } | ||
796 | if (pending == NULL) | ||
797 | { | ||
798 | /* no messages pending */ | ||
799 | return 0; | ||
800 | } | ||
801 | if (buf == NULL) | ||
802 | { | ||
803 | peer->th = | ||
804 | GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO, | ||
805 | pending->importance, | ||
806 | GNUNET_TIME_absolute_get_remaining | ||
807 | (pending->timeout), &peer->id, | ||
808 | ntohs (pending->msg->size), | ||
809 | &core_transmit_notify, peer); | ||
810 | GNUNET_break (NULL != peer->th); | ||
811 | return 0; | ||
812 | } | ||
813 | off = 0; | ||
814 | while ((NULL != (pending = peer->head)) && | ||
815 | (size - off >= (msize = ntohs (pending->msg->size)))) | ||
816 | { | ||
817 | GNUNET_STATISTICS_update (GDS_stats, | ||
818 | gettext_noop | ||
819 | ("# Bytes transmitted to other peers"), msize, | ||
820 | GNUNET_NO); | ||
821 | memcpy (&cbuf[off], pending->msg, msize); | ||
822 | off += msize; | ||
823 | peer->pending_count--; | ||
824 | GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending); | ||
825 | GNUNET_free (pending); | ||
826 | } | ||
827 | if (peer->head != NULL) | ||
828 | { | ||
829 | peer->th = | ||
830 | GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO, | ||
831 | pending->importance, | ||
832 | GNUNET_TIME_absolute_get_remaining | ||
833 | (pending->timeout), &peer->id, msize, | ||
834 | &core_transmit_notify, peer); | ||
835 | GNUNET_break (NULL != peer->th); | ||
836 | } | ||
837 | return off; | ||
838 | } | ||
839 | |||
840 | |||
841 | /** | ||
842 | * Transmit all messages in the peer's message queue. | ||
843 | * | ||
844 | * @param peer message queue to process | ||
845 | */ | ||
846 | static void | ||
847 | process_peer_queue (struct PeerInfo *peer) | ||
848 | { | ||
849 | struct P2PPendingMessage *pending; | ||
850 | |||
851 | if (NULL == (pending = peer->head)) | ||
852 | return; | ||
853 | if (NULL != peer->th) | ||
854 | return; | ||
855 | GNUNET_STATISTICS_update (GDS_stats, | ||
856 | gettext_noop | ||
857 | ("# Bytes of bandwidth requested from core"), | ||
858 | ntohs (pending->msg->size), GNUNET_NO); | ||
859 | peer->th = | ||
860 | GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO, | ||
861 | pending->importance, | ||
862 | GNUNET_TIME_absolute_get_remaining | ||
863 | (pending->timeout), &peer->id, | ||
864 | ntohs (pending->msg->size), | ||
865 | &core_transmit_notify, peer); | ||
866 | GNUNET_break (NULL != peer->th); | ||
867 | } | ||
868 | |||
869 | |||
870 | /** | ||
871 | * To how many peers should we (on average) forward the request to | ||
872 | * obtain the desired target_replication count (on average). | ||
873 | * | ||
874 | * @param hop_count number of hops the message has traversed | ||
875 | * @param target_replication the number of total paths desired | ||
876 | * @return Some number of peers to forward the message to | ||
877 | */ | ||
878 | static unsigned int | ||
879 | get_forward_count (uint32_t hop_count, uint32_t target_replication) | ||
880 | { | ||
881 | uint32_t random_value; | ||
882 | uint32_t forward_count; | ||
883 | float target_value; | ||
884 | |||
885 | if (hop_count > GDS_NSE_get () * 4.0) | ||
886 | { | ||
887 | /* forcefully terminate */ | ||
888 | GNUNET_STATISTICS_update (GDS_stats, | ||
889 | gettext_noop ("# requests TTL-dropped"), | ||
890 | 1, GNUNET_NO); | ||
891 | return 0; | ||
892 | } | ||
893 | if (hop_count > GDS_NSE_get () * 2.0) | ||
894 | { | ||
895 | /* Once we have reached our ideal number of hops, only forward to 1 peer */ | ||
896 | return 1; | ||
897 | } | ||
898 | /* bound by system-wide maximum */ | ||
899 | target_replication = | ||
900 | GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL, target_replication); | ||
901 | target_value = | ||
902 | 1 + (target_replication - 1.0) / (GDS_NSE_get () + | ||
903 | ((float) (target_replication - 1.0) * | ||
904 | hop_count)); | ||
905 | /* Set forward count to floor of target_value */ | ||
906 | forward_count = (uint32_t) target_value; | ||
907 | /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */ | ||
908 | target_value = target_value - forward_count; | ||
909 | random_value = | ||
910 | GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); | ||
911 | if (random_value < (target_value * UINT32_MAX)) | ||
912 | forward_count++; | ||
913 | return forward_count; | ||
914 | } | ||
915 | |||
916 | |||
917 | /** | ||
918 | * Compute the distance between have and target as a 32-bit value. | ||
919 | * Differences in the lower bits must count stronger than differences | ||
920 | * in the higher bits. | ||
921 | * | ||
922 | * @param target | ||
923 | * @param have | ||
924 | * @return 0 if have==target, otherwise a number | ||
925 | * that is larger as the distance between | ||
926 | * the two hash codes increases | ||
927 | */ | ||
928 | static unsigned int | ||
929 | get_distance (const struct GNUNET_HashCode *target, | ||
930 | const struct GNUNET_HashCode *have) | ||
931 | { | ||
932 | unsigned int bucket; | ||
933 | unsigned int msb; | ||
934 | unsigned int lsb; | ||
935 | unsigned int i; | ||
936 | |||
937 | /* We have to represent the distance between two 2^9 (=512)-bit | ||
938 | * numbers as a 2^5 (=32)-bit number with "0" being used for the | ||
939 | * two numbers being identical; furthermore, we need to | ||
940 | * guarantee that a difference in the number of matching | ||
941 | * bits is always represented in the result. | ||
942 | * | ||
943 | * We use 2^32/2^9 numerical values to distinguish between | ||
944 | * hash codes that have the same LSB bit distance and | ||
945 | * use the highest 2^9 bits of the result to signify the | ||
946 | * number of (mis)matching LSB bits; if we have 0 matching | ||
947 | * and hence 512 mismatching LSB bits we return -1 (since | ||
948 | * 512 itself cannot be represented with 9 bits) */ | ||
949 | |||
950 | /* first, calculate the most significant 9 bits of our | ||
951 | * result, aka the number of LSBs */ | ||
952 | bucket = GNUNET_CRYPTO_hash_matching_bits (target, have); | ||
953 | /* bucket is now a value between 0 and 512 */ | ||
954 | if (bucket == 512) | ||
955 | return 0; /* perfect match */ | ||
956 | if (bucket == 0) | ||
957 | return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting | ||
958 | * below, we'd end up with max+1 (overflow)) */ | ||
959 | |||
960 | /* calculate the most significant bits of the final result */ | ||
961 | msb = (512 - bucket) << (32 - 9); | ||
962 | /* calculate the 32-9 least significant bits of the final result by | ||
963 | * looking at the differences in the 32-9 bits following the | ||
964 | * mismatching bit at 'bucket' */ | ||
965 | lsb = 0; | ||
966 | for (i = bucket + 1; | ||
967 | (i < sizeof (struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++) | ||
968 | { | ||
969 | if (GNUNET_CRYPTO_hash_get_bit (target, i) != | ||
970 | GNUNET_CRYPTO_hash_get_bit (have, i)) | ||
971 | lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10, | ||
972 | * last bit set will be 31 -- if | ||
973 | * i does not reach 512 first... */ | ||
974 | } | ||
975 | return msb | lsb; | ||
976 | } | ||
977 | |||
978 | |||
979 | /** | ||
980 | * Check whether my identity is closer than any known peers. If a | ||
981 | * non-null bloomfilter is given, check if this is the closest peer | ||
982 | * that hasn't already been routed to. | ||
983 | * | ||
984 | * @param key hash code to check closeness to | ||
985 | * @param bloom bloomfilter, exclude these entries from the decision | ||
986 | * @return GNUNET_YES if node location is closest, | ||
987 | * GNUNET_NO otherwise. | ||
988 | */ | ||
989 | static int | ||
990 | am_closest_peer (const struct GNUNET_HashCode *key, | ||
991 | const struct GNUNET_CONTAINER_BloomFilter *bloom) | ||
992 | { | ||
993 | int bits; | ||
994 | int other_bits; | ||
995 | int bucket_num; | ||
996 | int count; | ||
997 | struct PeerInfo *pos; | ||
998 | struct GNUNET_HashCode phash; | ||
999 | |||
1000 | if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode))) | ||
1001 | return GNUNET_YES; | ||
1002 | bucket_num = find_bucket (key); | ||
1003 | GNUNET_assert (bucket_num >= 0); | ||
1004 | bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, key); | ||
1005 | pos = k_buckets[bucket_num].head; | ||
1006 | count = 0; | ||
1007 | while ((pos != NULL) && (count < bucket_size)) | ||
1008 | { | ||
1009 | GNUNET_CRYPTO_hash (&pos->id, | ||
1010 | sizeof (struct GNUNET_PeerIdentity), | ||
1011 | &phash); | ||
1012 | if ((bloom != NULL) && | ||
1013 | (GNUNET_YES == | ||
1014 | GNUNET_CONTAINER_bloomfilter_test (bloom, &phash))) | ||
1015 | { | ||
1016 | pos = pos->next; | ||
1017 | continue; /* Skip already checked entries */ | ||
1018 | } | ||
1019 | other_bits = GNUNET_CRYPTO_hash_matching_bits (&phash, key); | ||
1020 | if (other_bits > bits) | ||
1021 | return GNUNET_NO; | ||
1022 | if (other_bits == bits) /* We match the same number of bits */ | ||
1023 | return GNUNET_YES; | ||
1024 | pos = pos->next; | ||
1025 | } | ||
1026 | /* No peers closer, we are the closest! */ | ||
1027 | return GNUNET_YES; | ||
1028 | } | ||
1029 | |||
1030 | |||
1031 | /** | ||
1032 | * Select a peer from the routing table that would be a good routing | ||
1033 | * destination for sending a message for "key". The resulting peer | ||
1034 | * must not be in the set of blocked peers.<p> | ||
1035 | * | ||
1036 | * Note that we should not ALWAYS select the closest peer to the | ||
1037 | * target, peers further away from the target should be chosen with | ||
1038 | * exponentially declining probability. | ||
1039 | * | ||
1040 | * FIXME: double-check that this is fine | ||
1041 | * | ||
1042 | * | ||
1043 | * @param key the key we are selecting a peer to route to | ||
1044 | * @param bloom a bloomfilter containing entries this request has seen already | ||
1045 | * @param hops how many hops has this message traversed thus far | ||
1046 | * @return Peer to route to, or NULL on error | ||
1047 | */ | ||
1048 | static struct PeerInfo * | ||
1049 | select_peer (const struct GNUNET_HashCode * key, | ||
1050 | const struct GNUNET_CONTAINER_BloomFilter *bloom, uint32_t hops) | ||
1051 | { | ||
1052 | unsigned int bc; | ||
1053 | unsigned int count; | ||
1054 | unsigned int selected; | ||
1055 | struct PeerInfo *pos; | ||
1056 | unsigned int dist; | ||
1057 | unsigned int smallest_distance; | ||
1058 | struct PeerInfo *chosen; | ||
1059 | struct GNUNET_HashCode phash; | ||
1060 | |||
1061 | if (hops >= GDS_NSE_get ()) | ||
1062 | { | ||
1063 | /* greedy selection (closest peer that is not in bloomfilter) */ | ||
1064 | smallest_distance = UINT_MAX; | ||
1065 | chosen = NULL; | ||
1066 | for (bc = 0; bc <= closest_bucket; bc++) | ||
1067 | { | ||
1068 | pos = k_buckets[bc].head; | ||
1069 | count = 0; | ||
1070 | while ((pos != NULL) && (count < bucket_size)) | ||
1071 | { | ||
1072 | GNUNET_CRYPTO_hash (&pos->id, | ||
1073 | sizeof (struct GNUNET_PeerIdentity), | ||
1074 | &phash); | ||
1075 | if ((bloom == NULL) || | ||
1076 | (GNUNET_NO == | ||
1077 | GNUNET_CONTAINER_bloomfilter_test (bloom, &phash))) | ||
1078 | { | ||
1079 | dist = get_distance (key, &phash); | ||
1080 | if (dist < smallest_distance) | ||
1081 | { | ||
1082 | chosen = pos; | ||
1083 | smallest_distance = dist; | ||
1084 | } | ||
1085 | } | ||
1086 | else | ||
1087 | { | ||
1088 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1089 | "Excluded peer `%s' due to BF match in greedy routing for %s\n", | ||
1090 | GNUNET_i2s (&pos->id), GNUNET_h2s (key)); | ||
1091 | GNUNET_STATISTICS_update (GDS_stats, | ||
1092 | gettext_noop | ||
1093 | ("# Peers excluded from routing due to Bloomfilter"), | ||
1094 | 1, GNUNET_NO); | ||
1095 | dist = get_distance (key, &phash); | ||
1096 | if (dist < smallest_distance) | ||
1097 | { | ||
1098 | chosen = NULL; | ||
1099 | smallest_distance = dist; | ||
1100 | } | ||
1101 | } | ||
1102 | count++; | ||
1103 | pos = pos->next; | ||
1104 | } | ||
1105 | } | ||
1106 | if (NULL == chosen) | ||
1107 | GNUNET_STATISTICS_update (GDS_stats, | ||
1108 | gettext_noop ("# Peer selection failed"), 1, | ||
1109 | GNUNET_NO); | ||
1110 | return chosen; | ||
1111 | } | ||
1112 | |||
1113 | /* select "random" peer */ | ||
1114 | /* count number of peers that are available and not filtered */ | ||
1115 | count = 0; | ||
1116 | for (bc = 0; bc <= closest_bucket; bc++) | ||
1117 | { | ||
1118 | pos = k_buckets[bc].head; | ||
1119 | while ((pos != NULL) && (count < bucket_size)) | ||
1120 | { | ||
1121 | GNUNET_CRYPTO_hash (&pos->id, | ||
1122 | sizeof (struct GNUNET_PeerIdentity), | ||
1123 | &phash); | ||
1124 | if ((bloom != NULL) && | ||
1125 | (GNUNET_YES == | ||
1126 | GNUNET_CONTAINER_bloomfilter_test (bloom, &phash))) | ||
1127 | { | ||
1128 | GNUNET_STATISTICS_update (GDS_stats, | ||
1129 | gettext_noop | ||
1130 | ("# Peers excluded from routing due to Bloomfilter"), | ||
1131 | 1, GNUNET_NO); | ||
1132 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1133 | "Excluded peer `%s' due to BF match in random routing for %s\n", | ||
1134 | GNUNET_i2s (&pos->id), GNUNET_h2s (key)); | ||
1135 | pos = pos->next; | ||
1136 | continue; /* Ignore bloomfiltered peers */ | ||
1137 | } | ||
1138 | count++; | ||
1139 | pos = pos->next; | ||
1140 | } | ||
1141 | } | ||
1142 | if (0 == count) /* No peers to select from! */ | ||
1143 | { | ||
1144 | GNUNET_STATISTICS_update (GDS_stats, | ||
1145 | gettext_noop ("# Peer selection failed"), 1, | ||
1146 | GNUNET_NO); | ||
1147 | return NULL; | ||
1148 | } | ||
1149 | /* Now actually choose a peer */ | ||
1150 | selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count); | ||
1151 | count = 0; | ||
1152 | for (bc = 0; bc <= closest_bucket; bc++) | ||
1153 | { | ||
1154 | for (pos = k_buckets[bc].head; ((pos != NULL) && (count < bucket_size)); pos = pos->next) | ||
1155 | { | ||
1156 | GNUNET_CRYPTO_hash (&pos->id, | ||
1157 | sizeof (struct GNUNET_PeerIdentity), | ||
1158 | &phash); | ||
1159 | if ((bloom != NULL) && | ||
1160 | (GNUNET_YES == | ||
1161 | GNUNET_CONTAINER_bloomfilter_test (bloom, &phash))) | ||
1162 | { | ||
1163 | continue; /* Ignore bloomfiltered peers */ | ||
1164 | } | ||
1165 | if (0 == selected--) | ||
1166 | return pos; | ||
1167 | } | ||
1168 | } | ||
1169 | GNUNET_break (0); | ||
1170 | return NULL; | ||
1171 | } | ||
1172 | |||
1173 | |||
1174 | /** | ||
1175 | * Compute the set of peers that the given request should be | ||
1176 | * forwarded to. | ||
1177 | * | ||
1178 | * @param key routing key | ||
1179 | * @param bloom bloom filter excluding peers as targets, all selected | ||
1180 | * peers will be added to the bloom filter | ||
1181 | * @param hop_count number of hops the request has traversed so far | ||
1182 | * @param target_replication desired number of replicas | ||
1183 | * @param targets where to store an array of target peers (to be | ||
1184 | * free'd by the caller) | ||
1185 | * @return number of peers returned in 'targets'. | ||
1186 | */ | ||
1187 | static unsigned int | ||
1188 | get_target_peers (const struct GNUNET_HashCode *key, | ||
1189 | struct GNUNET_CONTAINER_BloomFilter *bloom, | ||
1190 | uint32_t hop_count, uint32_t target_replication, | ||
1191 | struct PeerInfo ***targets) | ||
1192 | { | ||
1193 | unsigned int ret; | ||
1194 | unsigned int off; | ||
1195 | struct PeerInfo **rtargets; | ||
1196 | struct PeerInfo *nxt; | ||
1197 | struct GNUNET_HashCode nhash; | ||
1198 | |||
1199 | GNUNET_assert (NULL != bloom); | ||
1200 | ret = get_forward_count (hop_count, target_replication); | ||
1201 | if (0 == ret) | ||
1202 | { | ||
1203 | *targets = NULL; | ||
1204 | return 0; | ||
1205 | } | ||
1206 | rtargets = GNUNET_malloc (sizeof (struct PeerInfo *) * ret); | ||
1207 | for (off = 0; off < ret; off++) | ||
1208 | { | ||
1209 | nxt = select_peer (key, bloom, hop_count); | ||
1210 | if (NULL == nxt) | ||
1211 | break; | ||
1212 | rtargets[off] = nxt; | ||
1213 | GNUNET_CRYPTO_hash (&nxt->id, | ||
1214 | sizeof (struct GNUNET_PeerIdentity), | ||
1215 | &nhash); | ||
1216 | GNUNET_break (GNUNET_NO == | ||
1217 | GNUNET_CONTAINER_bloomfilter_test (bloom, | ||
1218 | &nhash)); | ||
1219 | GNUNET_CONTAINER_bloomfilter_add (bloom, &nhash); | ||
1220 | } | ||
1221 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1222 | "Selected %u/%u peers at hop %u for %s (target was %u)\n", off, | ||
1223 | GNUNET_CONTAINER_multipeermap_size (all_known_peers), | ||
1224 | (unsigned int) hop_count, GNUNET_h2s (key), ret); | ||
1225 | if (0 == off) | ||
1226 | { | ||
1227 | GNUNET_free (rtargets); | ||
1228 | *targets = NULL; | ||
1229 | return 0; | ||
1230 | } | ||
1231 | *targets = rtargets; | ||
1232 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1233 | "Forwarding query `%s' to %u peers (goal was %u peers)\n", | ||
1234 | GNUNET_h2s (key), | ||
1235 | off, | ||
1236 | ret); | ||
1237 | return off; | ||
1238 | } | ||
1239 | |||
1240 | |||
1241 | /** | ||
1242 | * Perform a PUT operation. Forwards the given request to other | ||
1243 | * peers. Does not store the data locally. Does not give the | ||
1244 | * data to local clients. May do nothing if this is the only | ||
1245 | * peer in the network (or if we are the closest peer in the | ||
1246 | * network). | ||
1247 | * | ||
1248 | * @param type type of the block | ||
1249 | * @param options routing options | ||
1250 | * @param desired_replication_level desired replication count | ||
1251 | * @param expiration_time when does the content expire | ||
1252 | * @param hop_count how many hops has this message traversed so far | ||
1253 | * @param bf Bloom filter of peers this PUT has already traversed | ||
1254 | * @param key key for the content | ||
1255 | * @param put_path_length number of entries in @a put_path | ||
1256 | * @param put_path peers this request has traversed so far (if tracked) | ||
1257 | * @param data payload to store | ||
1258 | * @param data_size number of bytes in @a data | ||
1259 | */ | ||
1260 | void | ||
1261 | GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type, | ||
1262 | enum GNUNET_DHT_RouteOption options, | ||
1263 | uint32_t desired_replication_level, | ||
1264 | struct GNUNET_TIME_Absolute expiration_time, | ||
1265 | uint32_t hop_count, | ||
1266 | struct GNUNET_CONTAINER_BloomFilter *bf, | ||
1267 | const struct GNUNET_HashCode *key, | ||
1268 | unsigned int put_path_length, | ||
1269 | struct GNUNET_PeerIdentity *put_path, | ||
1270 | const void *data, size_t data_size) | ||
1271 | { | ||
1272 | unsigned int target_count; | ||
1273 | unsigned int i; | ||
1274 | struct PeerInfo **targets; | ||
1275 | struct PeerInfo *target; | ||
1276 | struct P2PPendingMessage *pending; | ||
1277 | size_t msize; | ||
1278 | struct PeerPutMessage *ppm; | ||
1279 | struct GNUNET_PeerIdentity *pp; | ||
1280 | struct GNUNET_HashCode thash; | ||
1281 | |||
1282 | GNUNET_assert (NULL != bf); | ||
1283 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1284 | "Adding myself (%s) to PUT bloomfilter for %s\n", | ||
1285 | GNUNET_i2s (&my_identity), GNUNET_h2s (key)); | ||
1286 | GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity_hash); | ||
1287 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# PUT requests routed"), | ||
1288 | 1, GNUNET_NO); | ||
1289 | target_count = | ||
1290 | get_target_peers (key, bf, hop_count, desired_replication_level, | ||
1291 | &targets); | ||
1292 | if (0 == target_count) | ||
1293 | { | ||
1294 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1295 | "Routing PUT for %s terminates after %u hops at %s\n", | ||
1296 | GNUNET_h2s (key), (unsigned int) hop_count, | ||
1297 | GNUNET_i2s (&my_identity)); | ||
1298 | return; | ||
1299 | } | ||
1300 | msize = | ||
1301 | put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + | ||
1302 | sizeof (struct PeerPutMessage); | ||
1303 | if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
1304 | { | ||
1305 | put_path_length = 0; | ||
1306 | msize = data_size + sizeof (struct PeerPutMessage); | ||
1307 | } | ||
1308 | if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
1309 | { | ||
1310 | GNUNET_break (0); | ||
1311 | GNUNET_free (targets); | ||
1312 | return; | ||
1313 | } | ||
1314 | GNUNET_STATISTICS_update (GDS_stats, | ||
1315 | gettext_noop | ||
1316 | ("# PUT messages queued for transmission"), | ||
1317 | target_count, GNUNET_NO); | ||
1318 | for (i = 0; i < target_count; i++) | ||
1319 | { | ||
1320 | target = targets[i]; | ||
1321 | if (target->pending_count >= MAXIMUM_PENDING_PER_PEER) | ||
1322 | { | ||
1323 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"), | ||
1324 | 1, GNUNET_NO); | ||
1325 | continue; /* skip */ | ||
1326 | } | ||
1327 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1328 | "Routing PUT for %s after %u hops to %s\n", GNUNET_h2s (key), | ||
1329 | (unsigned int) hop_count, GNUNET_i2s (&target->id)); | ||
1330 | pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); | ||
1331 | pending->importance = 0; /* FIXME */ | ||
1332 | pending->timeout = expiration_time; | ||
1333 | ppm = (struct PeerPutMessage *) &pending[1]; | ||
1334 | pending->msg = &ppm->header; | ||
1335 | ppm->header.size = htons (msize); | ||
1336 | ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT); | ||
1337 | ppm->options = htonl (options); | ||
1338 | ppm->type = htonl (type); | ||
1339 | ppm->hop_count = htonl (hop_count + 1); | ||
1340 | ppm->desired_replication_level = htonl (desired_replication_level); | ||
1341 | ppm->put_path_length = htonl (put_path_length); | ||
1342 | ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time); | ||
1343 | GNUNET_CRYPTO_hash (&target->id, | ||
1344 | sizeof (struct GNUNET_PeerIdentity), | ||
1345 | &thash); | ||
1346 | GNUNET_break (GNUNET_YES == | ||
1347 | GNUNET_CONTAINER_bloomfilter_test (bf, | ||
1348 | &thash)); | ||
1349 | GNUNET_assert (GNUNET_OK == | ||
1350 | GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, | ||
1351 | ppm->bloomfilter, | ||
1352 | DHT_BLOOM_SIZE)); | ||
1353 | ppm->key = *key; | ||
1354 | pp = (struct GNUNET_PeerIdentity *) &ppm[1]; | ||
1355 | memcpy (pp, put_path, | ||
1356 | sizeof (struct GNUNET_PeerIdentity) * put_path_length); | ||
1357 | memcpy (&pp[put_path_length], data, data_size); | ||
1358 | GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending); | ||
1359 | target->pending_count++; | ||
1360 | process_peer_queue (target); | ||
1361 | } | ||
1362 | GNUNET_free (targets); | ||
1363 | } | ||
1364 | |||
1365 | |||
1366 | /** | ||
1367 | * Perform a GET operation. Forwards the given request to other | ||
1368 | * peers. Does not lookup the key locally. May do nothing if this is | ||
1369 | * the only peer in the network (or if we are the closest peer in the | ||
1370 | * network). | ||
1371 | * | ||
1372 | * @param type type of the block | ||
1373 | * @param options routing options | ||
1374 | * @param desired_replication_level desired replication count | ||
1375 | * @param hop_count how many hops did this request traverse so far? | ||
1376 | * @param key key for the content | ||
1377 | * @param xquery extended query | ||
1378 | * @param xquery_size number of bytes in @a xquery | ||
1379 | * @param reply_bf bloomfilter to filter duplicates | ||
1380 | * @param reply_bf_mutator mutator for @a reply_bf | ||
1381 | * @param peer_bf filter for peers not to select (again) | ||
1382 | */ | ||
1383 | void | ||
1384 | GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, | ||
1385 | enum GNUNET_DHT_RouteOption options, | ||
1386 | uint32_t desired_replication_level, | ||
1387 | uint32_t hop_count, const struct GNUNET_HashCode * key, | ||
1388 | const void *xquery, size_t xquery_size, | ||
1389 | const struct GNUNET_CONTAINER_BloomFilter *reply_bf, | ||
1390 | uint32_t reply_bf_mutator, | ||
1391 | struct GNUNET_CONTAINER_BloomFilter *peer_bf) | ||
1392 | { | ||
1393 | unsigned int target_count; | ||
1394 | unsigned int i; | ||
1395 | struct PeerInfo **targets; | ||
1396 | struct PeerInfo *target; | ||
1397 | struct P2PPendingMessage *pending; | ||
1398 | size_t msize; | ||
1399 | struct PeerGetMessage *pgm; | ||
1400 | char *xq; | ||
1401 | size_t reply_bf_size; | ||
1402 | struct GNUNET_HashCode thash; | ||
1403 | |||
1404 | GNUNET_assert (NULL != peer_bf); | ||
1405 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# GET requests routed"), | ||
1406 | 1, GNUNET_NO); | ||
1407 | target_count = | ||
1408 | get_target_peers (key, peer_bf, hop_count, desired_replication_level, | ||
1409 | &targets); | ||
1410 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1411 | "Adding myself (%s) to GET bloomfilter for %s\n", | ||
1412 | GNUNET_i2s (&my_identity), GNUNET_h2s (key)); | ||
1413 | GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity_hash); | ||
1414 | if (0 == target_count) | ||
1415 | { | ||
1416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1417 | "Routing GET for %s terminates after %u hops at %s\n", | ||
1418 | GNUNET_h2s (key), (unsigned int) hop_count, | ||
1419 | GNUNET_i2s (&my_identity)); | ||
1420 | return; | ||
1421 | } | ||
1422 | reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf); | ||
1423 | msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size; | ||
1424 | if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
1425 | { | ||
1426 | GNUNET_break (0); | ||
1427 | GNUNET_free (targets); | ||
1428 | return; | ||
1429 | } | ||
1430 | GNUNET_STATISTICS_update (GDS_stats, | ||
1431 | gettext_noop | ||
1432 | ("# GET messages queued for transmission"), | ||
1433 | target_count, GNUNET_NO); | ||
1434 | /* forward request */ | ||
1435 | for (i = 0; i < target_count; i++) | ||
1436 | { | ||
1437 | target = targets[i]; | ||
1438 | if (target->pending_count >= MAXIMUM_PENDING_PER_PEER) | ||
1439 | { | ||
1440 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"), | ||
1441 | 1, GNUNET_NO); | ||
1442 | continue; /* skip */ | ||
1443 | } | ||
1444 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1445 | "Routing GET for %s after %u hops to %s\n", GNUNET_h2s (key), | ||
1446 | (unsigned int) hop_count, GNUNET_i2s (&target->id)); | ||
1447 | pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); | ||
1448 | pending->importance = 0; /* FIXME */ | ||
1449 | pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT); | ||
1450 | pgm = (struct PeerGetMessage *) &pending[1]; | ||
1451 | pending->msg = &pgm->header; | ||
1452 | pgm->header.size = htons (msize); | ||
1453 | pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET); | ||
1454 | pgm->options = htonl (options); | ||
1455 | pgm->type = htonl (type); | ||
1456 | pgm->hop_count = htonl (hop_count + 1); | ||
1457 | pgm->desired_replication_level = htonl (desired_replication_level); | ||
1458 | pgm->xquery_size = htonl (xquery_size); | ||
1459 | pgm->bf_mutator = reply_bf_mutator; | ||
1460 | GNUNET_CRYPTO_hash (&target->id, | ||
1461 | sizeof (struct GNUNET_PeerIdentity), | ||
1462 | &thash); | ||
1463 | GNUNET_break (GNUNET_YES == | ||
1464 | GNUNET_CONTAINER_bloomfilter_test (peer_bf, | ||
1465 | &thash)); | ||
1466 | GNUNET_assert (GNUNET_OK == | ||
1467 | GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf, | ||
1468 | pgm->bloomfilter, | ||
1469 | DHT_BLOOM_SIZE)); | ||
1470 | pgm->key = *key; | ||
1471 | xq = (char *) &pgm[1]; | ||
1472 | memcpy (xq, xquery, xquery_size); | ||
1473 | if (NULL != reply_bf) | ||
1474 | GNUNET_assert (GNUNET_OK == | ||
1475 | GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf, | ||
1476 | &xq | ||
1477 | [xquery_size], | ||
1478 | reply_bf_size)); | ||
1479 | GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending); | ||
1480 | target->pending_count++; | ||
1481 | process_peer_queue (target); | ||
1482 | } | ||
1483 | GNUNET_free (targets); | ||
1484 | } | ||
1485 | |||
1486 | |||
1487 | /** | ||
1488 | * Handle a reply (route to origin). Only forwards the reply back to | ||
1489 | * the given peer. Does not do local caching or forwarding to local | ||
1490 | * clients. | ||
1491 | * | ||
1492 | * @param target neighbour that should receive the block (if still connected) | ||
1493 | * @param type type of the block | ||
1494 | * @param expiration_time when does the content expire | ||
1495 | * @param key key for the content | ||
1496 | * @param put_path_length number of entries in @a put_path | ||
1497 | * @param put_path peers the original PUT traversed (if tracked) | ||
1498 | * @param get_path_length number of entries in @a get_path | ||
1499 | * @param get_path peers this reply has traversed so far (if tracked) | ||
1500 | * @param data payload of the reply | ||
1501 | * @param data_size number of bytes in @a data | ||
1502 | */ | ||
1503 | void | ||
1504 | GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, | ||
1505 | enum GNUNET_BLOCK_Type type, | ||
1506 | struct GNUNET_TIME_Absolute expiration_time, | ||
1507 | const struct GNUNET_HashCode * key, | ||
1508 | unsigned int put_path_length, | ||
1509 | const struct GNUNET_PeerIdentity *put_path, | ||
1510 | unsigned int get_path_length, | ||
1511 | const struct GNUNET_PeerIdentity *get_path, | ||
1512 | const void *data, size_t data_size) | ||
1513 | { | ||
1514 | struct PeerInfo *pi; | ||
1515 | struct P2PPendingMessage *pending; | ||
1516 | size_t msize; | ||
1517 | struct PeerResultMessage *prm; | ||
1518 | struct GNUNET_PeerIdentity *paths; | ||
1519 | |||
1520 | msize = | ||
1521 | data_size + sizeof (struct PeerResultMessage) + (get_path_length + | ||
1522 | put_path_length) * | ||
1523 | sizeof (struct GNUNET_PeerIdentity); | ||
1524 | if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) || | ||
1525 | (get_path_length > | ||
1526 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || | ||
1527 | (put_path_length > | ||
1528 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || | ||
1529 | (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)) | ||
1530 | { | ||
1531 | GNUNET_break (0); | ||
1532 | return; | ||
1533 | } | ||
1534 | pi = GNUNET_CONTAINER_multipeermap_get (all_known_peers, target); | ||
1535 | if (NULL == pi) | ||
1536 | { | ||
1537 | /* peer disconnected in the meantime, drop reply */ | ||
1538 | return; | ||
1539 | } | ||
1540 | if (pi->pending_count >= MAXIMUM_PENDING_PER_PEER) | ||
1541 | { | ||
1542 | /* skip */ | ||
1543 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"), | ||
1544 | 1, GNUNET_NO); | ||
1545 | return; | ||
1546 | } | ||
1547 | |||
1548 | GNUNET_STATISTICS_update (GDS_stats, | ||
1549 | gettext_noop | ||
1550 | ("# RESULT messages queued for transmission"), 1, | ||
1551 | GNUNET_NO); | ||
1552 | pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); | ||
1553 | pending->importance = 0; /* FIXME */ | ||
1554 | pending->timeout = expiration_time; | ||
1555 | prm = (struct PeerResultMessage *) &pending[1]; | ||
1556 | pending->msg = &prm->header; | ||
1557 | prm->header.size = htons (msize); | ||
1558 | prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT); | ||
1559 | prm->type = htonl (type); | ||
1560 | prm->put_path_length = htonl (put_path_length); | ||
1561 | prm->get_path_length = htonl (get_path_length); | ||
1562 | prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time); | ||
1563 | prm->key = *key; | ||
1564 | paths = (struct GNUNET_PeerIdentity *) &prm[1]; | ||
1565 | memcpy (paths, put_path, | ||
1566 | put_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
1567 | memcpy (&paths[put_path_length], get_path, | ||
1568 | get_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
1569 | memcpy (&paths[put_path_length + get_path_length], data, data_size); | ||
1570 | GNUNET_CONTAINER_DLL_insert (pi->head, pi->tail, pending); | ||
1571 | pi->pending_count++; | ||
1572 | process_peer_queue (pi); | ||
1573 | } | ||
1574 | |||
1575 | |||
1576 | /** | ||
1577 | * To be called on core init/fail. | ||
1578 | * | ||
1579 | * @param cls service closure | ||
1580 | * @param identity the public identity of this peer | ||
1581 | */ | ||
1582 | static void | ||
1583 | core_init (void *cls, | ||
1584 | const struct GNUNET_PeerIdentity *identity) | ||
1585 | { | ||
1586 | my_identity = *identity; | ||
1587 | GNUNET_CRYPTO_hash (identity, | ||
1588 | sizeof (struct GNUNET_PeerIdentity), | ||
1589 | &my_identity_hash); | ||
1590 | } | ||
1591 | |||
1592 | |||
1593 | /** | ||
1594 | * Core handler for p2p put requests. | ||
1595 | * | ||
1596 | * @param cls closure | ||
1597 | * @param peer sender of the request | ||
1598 | * @param message message | ||
1599 | * @param peer peer identity this notification is about | ||
1600 | * @return #GNUNET_OK to keep the connection open, | ||
1601 | * #GNUNET_SYSERR to close it (signal serious error) | ||
1602 | */ | ||
1603 | static int | ||
1604 | handle_dht_p2p_put (void *cls, | ||
1605 | const struct GNUNET_PeerIdentity *peer, | ||
1606 | const struct GNUNET_MessageHeader *message) | ||
1607 | { | ||
1608 | const struct PeerPutMessage *put; | ||
1609 | const struct GNUNET_PeerIdentity *put_path; | ||
1610 | const void *payload; | ||
1611 | uint32_t putlen; | ||
1612 | uint16_t msize; | ||
1613 | size_t payload_size; | ||
1614 | enum GNUNET_DHT_RouteOption options; | ||
1615 | struct GNUNET_CONTAINER_BloomFilter *bf; | ||
1616 | struct GNUNET_HashCode test_key; | ||
1617 | struct GNUNET_HashCode phash; | ||
1618 | |||
1619 | msize = ntohs (message->size); | ||
1620 | if (msize < sizeof (struct PeerPutMessage)) | ||
1621 | { | ||
1622 | GNUNET_break_op (0); | ||
1623 | return GNUNET_YES; | ||
1624 | } | ||
1625 | put = (const struct PeerPutMessage *) message; | ||
1626 | putlen = ntohl (put->put_path_length); | ||
1627 | if ((msize < | ||
1628 | sizeof (struct PeerPutMessage) + | ||
1629 | putlen * sizeof (struct GNUNET_PeerIdentity)) || | ||
1630 | (putlen > | ||
1631 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))) | ||
1632 | { | ||
1633 | GNUNET_break_op (0); | ||
1634 | return GNUNET_YES; | ||
1635 | } | ||
1636 | GNUNET_STATISTICS_update (GDS_stats, | ||
1637 | gettext_noop ("# P2P PUT requests received"), 1, | ||
1638 | GNUNET_NO); | ||
1639 | GNUNET_STATISTICS_update (GDS_stats, | ||
1640 | gettext_noop ("# P2P PUT bytes received"), msize, | ||
1641 | GNUNET_NO); | ||
1642 | put_path = (const struct GNUNET_PeerIdentity *) &put[1]; | ||
1643 | payload = &put_path[putlen]; | ||
1644 | options = ntohl (put->options); | ||
1645 | payload_size = | ||
1646 | msize - (sizeof (struct PeerPutMessage) + | ||
1647 | putlen * sizeof (struct GNUNET_PeerIdentity)); | ||
1648 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PUT for `%s' from %s\n", | ||
1649 | GNUNET_h2s (&put->key), GNUNET_i2s (peer)); | ||
1650 | GNUNET_CRYPTO_hash (peer, sizeof (struct GNUNET_PeerIdentity), &phash); | ||
1651 | if (GNUNET_YES == log_route_details_stderr) | ||
1652 | { | ||
1653 | char *tmp; | ||
1654 | |||
1655 | tmp = GNUNET_strdup (GNUNET_i2s (&my_identity)); | ||
1656 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, | ||
1657 | "XDHT PUT %s: %s->%s (%u, %u=>%u)\n", | ||
1658 | GNUNET_h2s (&put->key), GNUNET_i2s (peer), tmp, | ||
1659 | ntohl(put->hop_count), | ||
1660 | GNUNET_CRYPTO_hash_matching_bits (&phash, &put->key), | ||
1661 | GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, &put->key) | ||
1662 | ); | ||
1663 | GNUNET_free (tmp); | ||
1664 | } | ||
1665 | switch (GNUNET_BLOCK_get_key | ||
1666 | (GDS_block_context, ntohl (put->type), payload, payload_size, | ||
1667 | &test_key)) | ||
1668 | { | ||
1669 | case GNUNET_YES: | ||
1670 | if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode))) | ||
1671 | { | ||
1672 | char *put_s = GNUNET_strdup (GNUNET_h2s (&put->key)); | ||
1673 | GNUNET_break_op (0); | ||
1674 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1675 | "PUT with key `%s' for block with key %s\n", | ||
1676 | put_s, GNUNET_h2s (&test_key)); | ||
1677 | GNUNET_free (put_s); | ||
1678 | return GNUNET_YES; | ||
1679 | } | ||
1680 | break; | ||
1681 | case GNUNET_NO: | ||
1682 | GNUNET_break_op (0); | ||
1683 | return GNUNET_YES; | ||
1684 | case GNUNET_SYSERR: | ||
1685 | /* cannot verify, good luck */ | ||
1686 | break; | ||
1687 | } | ||
1688 | if (ntohl (put->type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */ | ||
1689 | { | ||
1690 | switch (GNUNET_BLOCK_evaluate (GDS_block_context, | ||
1691 | ntohl (put->type), | ||
1692 | NULL, /* query */ | ||
1693 | NULL, 0, /* bloom filer */ | ||
1694 | NULL, 0, /* xquery */ | ||
1695 | payload, payload_size)) | ||
1696 | { | ||
1697 | case GNUNET_BLOCK_EVALUATION_OK_MORE: | ||
1698 | case GNUNET_BLOCK_EVALUATION_OK_LAST: | ||
1699 | break; | ||
1700 | |||
1701 | case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE: | ||
1702 | case GNUNET_BLOCK_EVALUATION_RESULT_INVALID: | ||
1703 | case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT: | ||
1704 | case GNUNET_BLOCK_EVALUATION_REQUEST_VALID: | ||
1705 | case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID: | ||
1706 | case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED: | ||
1707 | default: | ||
1708 | GNUNET_break_op (0); | ||
1709 | return GNUNET_OK; | ||
1710 | } | ||
1711 | } | ||
1712 | |||
1713 | bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter, DHT_BLOOM_SIZE, | ||
1714 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
1715 | GNUNET_break_op (GNUNET_YES == | ||
1716 | GNUNET_CONTAINER_bloomfilter_test (bf, &phash)); | ||
1717 | { | ||
1718 | struct GNUNET_PeerIdentity pp[putlen + 1]; | ||
1719 | |||
1720 | /* extend 'put path' by sender */ | ||
1721 | if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE)) | ||
1722 | { | ||
1723 | memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity)); | ||
1724 | pp[putlen] = *peer; | ||
1725 | putlen++; | ||
1726 | } | ||
1727 | else | ||
1728 | putlen = 0; | ||
1729 | |||
1730 | /* give to local clients */ | ||
1731 | GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time), | ||
1732 | &put->key, 0, NULL, putlen, pp, ntohl (put->type), | ||
1733 | payload_size, payload); | ||
1734 | /* store locally */ | ||
1735 | if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || | ||
1736 | (am_closest_peer (&put->key, bf))) | ||
1737 | GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh | ||
1738 | (put->expiration_time), &put->key, putlen, pp, | ||
1739 | ntohl (put->type), payload_size, payload); | ||
1740 | /* route to other peers */ | ||
1741 | GDS_NEIGHBOURS_handle_put (ntohl (put->type), options, | ||
1742 | ntohl (put->desired_replication_level), | ||
1743 | GNUNET_TIME_absolute_ntoh (put->expiration_time), | ||
1744 | ntohl (put->hop_count), bf, &put->key, putlen, | ||
1745 | pp, payload, payload_size); | ||
1746 | /* notify monitoring clients */ | ||
1747 | GDS_CLIENTS_process_put (options, | ||
1748 | ntohl (put->type), | ||
1749 | ntohl (put->hop_count), | ||
1750 | ntohl (put->desired_replication_level), | ||
1751 | putlen, pp, | ||
1752 | GNUNET_TIME_absolute_ntoh (put->expiration_time), | ||
1753 | &put->key, | ||
1754 | payload, | ||
1755 | payload_size); | ||
1756 | } | ||
1757 | GNUNET_CONTAINER_bloomfilter_free (bf); | ||
1758 | return GNUNET_YES; | ||
1759 | } | ||
1760 | |||
1761 | |||
1762 | /** | ||
1763 | * We have received a FIND PEER request. Send matching | ||
1764 | * HELLOs back. | ||
1765 | * | ||
1766 | * @param sender sender of the FIND PEER request | ||
1767 | * @param key peers close to this key are desired | ||
1768 | * @param bf peers matching this bf are excluded | ||
1769 | * @param bf_mutator mutator for bf | ||
1770 | */ | ||
1771 | static void | ||
1772 | handle_find_peer (const struct GNUNET_PeerIdentity *sender, | ||
1773 | const struct GNUNET_HashCode * key, | ||
1774 | struct GNUNET_CONTAINER_BloomFilter *bf, uint32_t bf_mutator) | ||
1775 | { | ||
1776 | int bucket_idx; | ||
1777 | struct PeerBucket *bucket; | ||
1778 | struct PeerInfo *peer; | ||
1779 | unsigned int choice; | ||
1780 | struct GNUNET_HashCode phash; | ||
1781 | struct GNUNET_HashCode mhash; | ||
1782 | const struct GNUNET_HELLO_Message *hello; | ||
1783 | |||
1784 | /* first, check about our own HELLO */ | ||
1785 | if (NULL != GDS_my_hello) | ||
1786 | { | ||
1787 | GNUNET_BLOCK_mingle_hash (&my_identity_hash, bf_mutator, &mhash); | ||
1788 | if ((NULL == bf) || | ||
1789 | (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))) | ||
1790 | { | ||
1791 | GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO, | ||
1792 | GNUNET_TIME_relative_to_absolute | ||
1793 | (hello_expiration), | ||
1794 | key, 0, NULL, 0, NULL, GDS_my_hello, | ||
1795 | GNUNET_HELLO_size ((const struct | ||
1796 | GNUNET_HELLO_Message *) | ||
1797 | GDS_my_hello)); | ||
1798 | } | ||
1799 | else | ||
1800 | { | ||
1801 | GNUNET_STATISTICS_update (GDS_stats, | ||
1802 | gettext_noop | ||
1803 | ("# FIND PEER requests ignored due to Bloomfilter"), | ||
1804 | 1, GNUNET_NO); | ||
1805 | } | ||
1806 | } | ||
1807 | else | ||
1808 | { | ||
1809 | GNUNET_STATISTICS_update (GDS_stats, | ||
1810 | gettext_noop | ||
1811 | ("# FIND PEER requests ignored due to lack of HELLO"), | ||
1812 | 1, GNUNET_NO); | ||
1813 | } | ||
1814 | |||
1815 | /* then, also consider sending a random HELLO from the closest bucket */ | ||
1816 | if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode))) | ||
1817 | bucket_idx = closest_bucket; | ||
1818 | else | ||
1819 | bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key)); | ||
1820 | if (bucket_idx == GNUNET_SYSERR) | ||
1821 | return; | ||
1822 | bucket = &k_buckets[bucket_idx]; | ||
1823 | if (bucket->peers_size == 0) | ||
1824 | return; | ||
1825 | choice = | ||
1826 | GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, bucket->peers_size); | ||
1827 | peer = bucket->head; | ||
1828 | while (choice > 0) | ||
1829 | { | ||
1830 | GNUNET_assert (NULL != peer); | ||
1831 | peer = peer->next; | ||
1832 | choice--; | ||
1833 | } | ||
1834 | choice = bucket->peers_size; | ||
1835 | do | ||
1836 | { | ||
1837 | peer = peer->next; | ||
1838 | if (choice-- == 0) | ||
1839 | return; /* no non-masked peer available */ | ||
1840 | if (peer == NULL) | ||
1841 | peer = bucket->head; | ||
1842 | GNUNET_CRYPTO_hash (&peer->id, sizeof (struct GNUNET_PeerIdentity), &phash); | ||
1843 | GNUNET_BLOCK_mingle_hash (&phash, bf_mutator, &mhash); | ||
1844 | hello = GDS_HELLO_get (&peer->id); | ||
1845 | } | ||
1846 | while ((hello == NULL) || | ||
1847 | (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))); | ||
1848 | GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO, | ||
1849 | GNUNET_TIME_relative_to_absolute | ||
1850 | (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION), key, | ||
1851 | 0, NULL, 0, NULL, hello, | ||
1852 | GNUNET_HELLO_size (hello)); | ||
1853 | } | ||
1854 | |||
1855 | |||
1856 | /** | ||
1857 | * Core handler for p2p get requests. | ||
1858 | * | ||
1859 | * @param cls closure | ||
1860 | * @param peer sender of the request | ||
1861 | * @param message message | ||
1862 | * @return #GNUNET_OK to keep the connection open, | ||
1863 | * #GNUNET_SYSERR to close it (signal serious error) | ||
1864 | */ | ||
1865 | static int | ||
1866 | handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
1867 | const struct GNUNET_MessageHeader *message) | ||
1868 | { | ||
1869 | struct PeerGetMessage *get; | ||
1870 | uint32_t xquery_size; | ||
1871 | size_t reply_bf_size; | ||
1872 | uint16_t msize; | ||
1873 | enum GNUNET_BLOCK_Type type; | ||
1874 | enum GNUNET_DHT_RouteOption options; | ||
1875 | enum GNUNET_BLOCK_EvaluationResult eval; | ||
1876 | struct GNUNET_CONTAINER_BloomFilter *reply_bf; | ||
1877 | struct GNUNET_CONTAINER_BloomFilter *peer_bf; | ||
1878 | const char *xquery; | ||
1879 | struct GNUNET_HashCode phash; | ||
1880 | |||
1881 | GNUNET_break (0 != | ||
1882 | memcmp (peer, &my_identity, | ||
1883 | sizeof (struct GNUNET_PeerIdentity))); | ||
1884 | /* parse and validate message */ | ||
1885 | msize = ntohs (message->size); | ||
1886 | if (msize < sizeof (struct PeerGetMessage)) | ||
1887 | { | ||
1888 | GNUNET_break_op (0); | ||
1889 | return GNUNET_YES; | ||
1890 | } | ||
1891 | get = (struct PeerGetMessage *) message; | ||
1892 | xquery_size = ntohl (get->xquery_size); | ||
1893 | if (msize < sizeof (struct PeerGetMessage) + xquery_size) | ||
1894 | { | ||
1895 | GNUNET_break_op (0); | ||
1896 | return GNUNET_YES; | ||
1897 | } | ||
1898 | reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size); | ||
1899 | type = ntohl (get->type); | ||
1900 | options = ntohl (get->options); | ||
1901 | xquery = (const char *) &get[1]; | ||
1902 | reply_bf = NULL; | ||
1903 | GNUNET_STATISTICS_update (GDS_stats, | ||
1904 | gettext_noop ("# P2P GET requests received"), 1, | ||
1905 | GNUNET_NO); | ||
1906 | GNUNET_STATISTICS_update (GDS_stats, | ||
1907 | gettext_noop ("# P2P GET bytes received"), msize, | ||
1908 | GNUNET_NO); | ||
1909 | GNUNET_CRYPTO_hash (peer, sizeof (struct GNUNET_PeerIdentity), &phash); | ||
1910 | if (GNUNET_YES == log_route_details_stderr) | ||
1911 | { | ||
1912 | char *tmp; | ||
1913 | |||
1914 | tmp = GNUNET_strdup (GNUNET_i2s (&my_identity)); | ||
1915 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, | ||
1916 | "XDHT GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n", | ||
1917 | GNUNET_h2s (&get->key), GNUNET_i2s (peer), tmp, | ||
1918 | ntohl(get->hop_count), | ||
1919 | GNUNET_CRYPTO_hash_matching_bits (&phash, &get->key), | ||
1920 | GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, &get->key), | ||
1921 | ntohl(get->xquery_size), xquery | ||
1922 | ); | ||
1923 | GNUNET_free (tmp); | ||
1924 | } | ||
1925 | |||
1926 | if (reply_bf_size > 0) | ||
1927 | reply_bf = | ||
1928 | GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size], reply_bf_size, | ||
1929 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
1930 | eval = | ||
1931 | GNUNET_BLOCK_evaluate (GDS_block_context, type, &get->key, &reply_bf, | ||
1932 | get->bf_mutator, xquery, xquery_size, NULL, 0); | ||
1933 | if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID) | ||
1934 | { | ||
1935 | /* request invalid or block type not supported */ | ||
1936 | GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED); | ||
1937 | if (NULL != reply_bf) | ||
1938 | GNUNET_CONTAINER_bloomfilter_free (reply_bf); | ||
1939 | return GNUNET_YES; | ||
1940 | } | ||
1941 | peer_bf = | ||
1942 | GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, DHT_BLOOM_SIZE, | ||
1943 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
1944 | GNUNET_break_op (GNUNET_YES == | ||
1945 | GNUNET_CONTAINER_bloomfilter_test (peer_bf, | ||
1946 | &phash)); | ||
1947 | /* remember request for routing replies */ | ||
1948 | GDS_ROUTING_add (peer, type, options, &get->key, xquery, xquery_size, | ||
1949 | reply_bf, get->bf_mutator); | ||
1950 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GET for %s at %s after %u hops\n", | ||
1951 | GNUNET_h2s (&get->key), GNUNET_i2s (&my_identity), | ||
1952 | (unsigned int) ntohl (get->hop_count)); | ||
1953 | /* local lookup (this may update the reply_bf) */ | ||
1954 | if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || | ||
1955 | (am_closest_peer (&get->key, peer_bf))) | ||
1956 | { | ||
1957 | if ((0 != (options & GNUNET_DHT_RO_FIND_PEER))) | ||
1958 | { | ||
1959 | GNUNET_STATISTICS_update (GDS_stats, | ||
1960 | gettext_noop | ||
1961 | ("# P2P FIND PEER requests processed"), 1, | ||
1962 | GNUNET_NO); | ||
1963 | handle_find_peer (peer, &get->key, reply_bf, get->bf_mutator); | ||
1964 | } | ||
1965 | else | ||
1966 | { | ||
1967 | eval = | ||
1968 | GDS_DATACACHE_handle_get (&get->key, type, xquery, xquery_size, | ||
1969 | &reply_bf, get->bf_mutator); | ||
1970 | } | ||
1971 | } | ||
1972 | else | ||
1973 | { | ||
1974 | GNUNET_STATISTICS_update (GDS_stats, | ||
1975 | gettext_noop ("# P2P GET requests ONLY routed"), | ||
1976 | 1, GNUNET_NO); | ||
1977 | } | ||
1978 | |||
1979 | GDS_CLIENTS_process_get (options, | ||
1980 | type, | ||
1981 | ntohl(get->hop_count), | ||
1982 | ntohl(get->desired_replication_level), | ||
1983 | 0, NULL, | ||
1984 | &get->key); | ||
1985 | |||
1986 | /* P2P forwarding */ | ||
1987 | if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST) | ||
1988 | GDS_NEIGHBOURS_handle_get (type, options, | ||
1989 | ntohl (get->desired_replication_level), | ||
1990 | ntohl (get->hop_count), &get->key, xquery, | ||
1991 | xquery_size, reply_bf, get->bf_mutator, peer_bf); | ||
1992 | /* clean up */ | ||
1993 | if (NULL != reply_bf) | ||
1994 | GNUNET_CONTAINER_bloomfilter_free (reply_bf); | ||
1995 | GNUNET_CONTAINER_bloomfilter_free (peer_bf); | ||
1996 | return GNUNET_YES; | ||
1997 | } | ||
1998 | |||
1999 | |||
2000 | /** | ||
2001 | * Core handler for p2p result messages. | ||
2002 | * | ||
2003 | * @param cls closure | ||
2004 | * @param message message | ||
2005 | * @param peer peer identity this notification is about | ||
2006 | * @return #GNUNET_YES (do not cut p2p connection) | ||
2007 | */ | ||
2008 | static int | ||
2009 | handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
2010 | const struct GNUNET_MessageHeader *message) | ||
2011 | { | ||
2012 | const struct PeerResultMessage *prm; | ||
2013 | const struct GNUNET_PeerIdentity *put_path; | ||
2014 | const struct GNUNET_PeerIdentity *get_path; | ||
2015 | const void *data; | ||
2016 | uint32_t get_path_length; | ||
2017 | uint32_t put_path_length; | ||
2018 | uint16_t msize; | ||
2019 | size_t data_size; | ||
2020 | enum GNUNET_BLOCK_Type type; | ||
2021 | |||
2022 | /* parse and validate message */ | ||
2023 | msize = ntohs (message->size); | ||
2024 | if (msize < sizeof (struct PeerResultMessage)) | ||
2025 | { | ||
2026 | GNUNET_break_op (0); | ||
2027 | return GNUNET_YES; | ||
2028 | } | ||
2029 | prm = (struct PeerResultMessage *) message; | ||
2030 | put_path_length = ntohl (prm->put_path_length); | ||
2031 | get_path_length = ntohl (prm->get_path_length); | ||
2032 | if ((msize < | ||
2033 | sizeof (struct PeerResultMessage) + (get_path_length + | ||
2034 | put_path_length) * | ||
2035 | sizeof (struct GNUNET_PeerIdentity)) || | ||
2036 | (get_path_length > | ||
2037 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || | ||
2038 | (put_path_length > | ||
2039 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))) | ||
2040 | { | ||
2041 | GNUNET_break_op (0); | ||
2042 | return GNUNET_YES; | ||
2043 | } | ||
2044 | put_path = (const struct GNUNET_PeerIdentity *) &prm[1]; | ||
2045 | get_path = &put_path[put_path_length]; | ||
2046 | type = ntohl (prm->type); | ||
2047 | data = (const void *) &get_path[get_path_length]; | ||
2048 | data_size = | ||
2049 | msize - (sizeof (struct PeerResultMessage) + | ||
2050 | (get_path_length + | ||
2051 | put_path_length) * sizeof (struct GNUNET_PeerIdentity)); | ||
2052 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P RESULTS received"), | ||
2053 | 1, GNUNET_NO); | ||
2054 | GNUNET_STATISTICS_update (GDS_stats, | ||
2055 | gettext_noop ("# P2P RESULT bytes received"), | ||
2056 | msize, GNUNET_NO); | ||
2057 | if (GNUNET_YES == log_route_details_stderr) | ||
2058 | { | ||
2059 | char *tmp; | ||
2060 | |||
2061 | tmp = GNUNET_strdup (GNUNET_i2s (&my_identity)); | ||
2062 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "XDHT RESULT %s: %s->%s (%u)\n", | ||
2063 | GNUNET_h2s (&prm->key), GNUNET_i2s (peer), tmp, | ||
2064 | get_path_length + 1); | ||
2065 | GNUNET_free (tmp); | ||
2066 | } | ||
2067 | /* if we got a HELLO, consider it for our own routing table */ | ||
2068 | if (type == GNUNET_BLOCK_TYPE_DHT_HELLO) | ||
2069 | { | ||
2070 | const struct GNUNET_MessageHeader *h; | ||
2071 | struct GNUNET_PeerIdentity pid; | ||
2072 | int bucket; | ||
2073 | |||
2074 | /* Should be a HELLO, validate and consider using it! */ | ||
2075 | if (data_size < sizeof (struct GNUNET_MessageHeader)) | ||
2076 | { | ||
2077 | GNUNET_break_op (0); | ||
2078 | return GNUNET_YES; | ||
2079 | } | ||
2080 | h = data; | ||
2081 | if (data_size != ntohs (h->size)) | ||
2082 | { | ||
2083 | GNUNET_break_op (0); | ||
2084 | return GNUNET_YES; | ||
2085 | } | ||
2086 | if (GNUNET_OK != | ||
2087 | GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h, &pid)) | ||
2088 | { | ||
2089 | GNUNET_break_op (0); | ||
2090 | return GNUNET_YES; | ||
2091 | } | ||
2092 | if ((GNUNET_YES != disable_try_connect) && | ||
2093 | 0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity))) | ||
2094 | { | ||
2095 | struct GNUNET_HashCode pid_hash; | ||
2096 | |||
2097 | GNUNET_CRYPTO_hash (&pid, sizeof (struct GNUNET_PeerIdentity), &pid_hash); | ||
2098 | bucket = find_bucket (&pid_hash); | ||
2099 | if ((bucket >= 0) && | ||
2100 | (k_buckets[bucket].peers_size < bucket_size) && | ||
2101 | (NULL != GDS_transport_handle)) | ||
2102 | { | ||
2103 | GNUNET_TRANSPORT_offer_hello (GDS_transport_handle, h, NULL, NULL); | ||
2104 | GNUNET_TRANSPORT_try_connect (GDS_transport_handle, &pid, NULL, NULL); /*FIXME TRY_CONNECT change */ | ||
2105 | } | ||
2106 | } | ||
2107 | } | ||
2108 | |||
2109 | /* append 'peer' to 'get_path' */ | ||
2110 | { | ||
2111 | struct GNUNET_PeerIdentity xget_path[get_path_length + 1]; | ||
2112 | |||
2113 | memcpy (xget_path, get_path, | ||
2114 | get_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
2115 | xget_path[get_path_length] = *peer; | ||
2116 | get_path_length++; | ||
2117 | |||
2118 | /* forward to local clients */ | ||
2119 | GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time), | ||
2120 | &prm->key, get_path_length, xget_path, | ||
2121 | put_path_length, put_path, type, data_size, data); | ||
2122 | GDS_CLIENTS_process_get_resp (type, | ||
2123 | xget_path, get_path_length, | ||
2124 | put_path, put_path_length, | ||
2125 | GNUNET_TIME_absolute_ntoh ( | ||
2126 | prm->expiration_time), | ||
2127 | &prm->key, | ||
2128 | data, | ||
2129 | data_size); | ||
2130 | if (GNUNET_YES == cache_results) | ||
2131 | { | ||
2132 | struct GNUNET_PeerIdentity xput_path[get_path_length + 1 + put_path_length]; | ||
2133 | |||
2134 | memcpy (xput_path, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
2135 | memcpy (&xput_path[put_path_length], | ||
2136 | xget_path, | ||
2137 | get_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
2138 | |||
2139 | GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (prm->expiration_time), | ||
2140 | &prm->key, | ||
2141 | get_path_length + put_path_length, xput_path, | ||
2142 | type, data_size, data); | ||
2143 | } | ||
2144 | /* forward to other peers */ | ||
2145 | GDS_ROUTING_process (type, GNUNET_TIME_absolute_ntoh (prm->expiration_time), | ||
2146 | &prm->key, put_path_length, put_path, get_path_length, | ||
2147 | xget_path, data, data_size); | ||
2148 | } | ||
2149 | |||
2150 | return GNUNET_YES; | ||
2151 | } | ||
2152 | |||
2153 | |||
2154 | /** | ||
2155 | * Initialize neighbours subsystem. | ||
2156 | * | ||
2157 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
2158 | */ | ||
2159 | int | ||
2160 | GDS_NEIGHBOURS_init () | ||
2161 | { | ||
2162 | static struct GNUNET_CORE_MessageHandler core_handlers[] = { | ||
2163 | {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0}, | ||
2164 | {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0}, | ||
2165 | {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0}, | ||
2166 | {NULL, 0, 0} | ||
2167 | }; | ||
2168 | unsigned long long temp_config_num; | ||
2169 | |||
2170 | disable_try_connect | ||
2171 | = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg, "DHT", "DISABLE_TRY_CONNECT"); | ||
2172 | if (GNUNET_OK == | ||
2173 | GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size", | ||
2174 | &temp_config_num)) | ||
2175 | bucket_size = (unsigned int) temp_config_num; | ||
2176 | cache_results | ||
2177 | = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg, "DHT", "CACHE_RESULTS"); | ||
2178 | |||
2179 | log_route_details_stderr = | ||
2180 | (NULL != getenv("GNUNET_DHT_ROUTE_DEBUG")) ? GNUNET_YES : GNUNET_NO; | ||
2181 | atsAPI = GNUNET_ATS_performance_init (GDS_cfg, NULL, NULL); | ||
2182 | core_api = | ||
2183 | GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect, | ||
2184 | &handle_core_disconnect, NULL, GNUNET_NO, NULL, | ||
2185 | GNUNET_NO, core_handlers); | ||
2186 | if (core_api == NULL) | ||
2187 | return GNUNET_SYSERR; | ||
2188 | all_known_peers = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO); | ||
2189 | return GNUNET_OK; | ||
2190 | } | ||
2191 | |||
2192 | |||
2193 | /** | ||
2194 | * Shutdown neighbours subsystem. | ||
2195 | */ | ||
2196 | void | ||
2197 | GDS_NEIGHBOURS_done () | ||
2198 | { | ||
2199 | if (NULL == core_api) | ||
2200 | return; | ||
2201 | GNUNET_CORE_disconnect (core_api); | ||
2202 | core_api = NULL; | ||
2203 | GNUNET_ATS_performance_done (atsAPI); | ||
2204 | atsAPI = NULL; | ||
2205 | GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (all_known_peers)); | ||
2206 | GNUNET_CONTAINER_multipeermap_destroy (all_known_peers); | ||
2207 | all_known_peers = NULL; | ||
2208 | if (GNUNET_SCHEDULER_NO_TASK != find_peer_task) | ||
2209 | { | ||
2210 | GNUNET_SCHEDULER_cancel (find_peer_task); | ||
2211 | find_peer_task = GNUNET_SCHEDULER_NO_TASK; | ||
2212 | } | ||
2213 | } | ||
2214 | |||
2215 | /** | ||
2216 | * Get the ID of the local node. | ||
2217 | * | ||
2218 | * @return identity of the local node | ||
2219 | */ | ||
2220 | struct GNUNET_PeerIdentity * | ||
2221 | GDS_NEIGHBOURS_get_id () | ||
2222 | { | ||
2223 | return &my_identity; | ||
2224 | } | ||
2225 | |||
2226 | |||
2227 | /* end of gnunet-service-dht_neighbours.c */ | ||