diff options
Diffstat (limited to 'src/rps/gnunet-service-rps.c')
-rw-r--r-- | src/rps/gnunet-service-rps.c | 1382 |
1 files changed, 1382 insertions, 0 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c new file mode 100644 index 000000000..6d96f5486 --- /dev/null +++ b/src/rps/gnunet-service-rps.c | |||
@@ -0,0 +1,1382 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) | ||
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 rps/gnunet-service-rps.c | ||
23 | * @brief rps service implementation | ||
24 | * @author Julius Bünger | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_cadet_service.h" | ||
29 | #include "gnunet_nse_service.h" | ||
30 | #include "rps.h" | ||
31 | |||
32 | #include <math.h> | ||
33 | #include <inttypes.h> | ||
34 | |||
35 | #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__) | ||
36 | |||
37 | // TODO modify @brief in every file | ||
38 | |||
39 | // TODO take care that messages are not longer than 64k | ||
40 | |||
41 | // TODO check for overflows | ||
42 | |||
43 | // TODO align message structs | ||
44 | |||
45 | // TODO multipeerlist indep of gossiped list | ||
46 | |||
47 | // TODO maybe wait during initialisation some time to get some peers | ||
48 | // - initialise peers before proceeding | ||
49 | // - Use the magic 0000 peer GNUNET_CADET_get_peers() returns | ||
50 | |||
51 | // (TODO api -- possibility of getting weak random peer immideately) | ||
52 | |||
53 | // TODO malicious peer | ||
54 | |||
55 | // TODO switch Slist -> DLL | ||
56 | |||
57 | /** | ||
58 | * Our configuration. | ||
59 | */ | ||
60 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
61 | |||
62 | /** | ||
63 | * Our own identity. | ||
64 | */ | ||
65 | struct GNUNET_PeerIdentity *own_identity; | ||
66 | |||
67 | /** | ||
68 | * Compare two peer identities. Taken from secretsharing. | ||
69 | * | ||
70 | * @param p1 Some peer identity. | ||
71 | * @param p2 Some peer identity. | ||
72 | * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2. | ||
73 | */ | ||
74 | static int | ||
75 | peer_id_cmp (const void *p1, const void *p2) | ||
76 | { | ||
77 | return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity)); | ||
78 | } | ||
79 | |||
80 | /*********************************************************************** | ||
81 | * Sampler | ||
82 | * | ||
83 | * WARNING: This section needs to be reviewed regarding the use of | ||
84 | * functions providing (pseudo)randomness! | ||
85 | ***********************************************************************/ | ||
86 | |||
87 | // TODO init list | ||
88 | // TODO grow/shrink list | ||
89 | |||
90 | /** | ||
91 | * A sampler sampling PeerIDs. | ||
92 | */ | ||
93 | struct Sampler | ||
94 | { | ||
95 | /** | ||
96 | * Min-wise linear permutation used by this sampler. | ||
97 | * | ||
98 | * This is an key later used by a hmac. | ||
99 | */ | ||
100 | struct GNUNET_CRYPTO_AuthKey auth_key; | ||
101 | |||
102 | /** | ||
103 | * The PeerID this sampler currently samples. | ||
104 | */ | ||
105 | struct GNUNET_PeerIdentity *peer_id; | ||
106 | |||
107 | /** | ||
108 | * The according hash value of this PeerID. | ||
109 | */ | ||
110 | struct GNUNET_HashCode peer_id_hash; | ||
111 | |||
112 | }; | ||
113 | |||
114 | typedef void (* SAMPLER_deleteCB) (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash); | ||
115 | |||
116 | /** | ||
117 | * (Re)Initialise given Sampler with random min-wise independent function. | ||
118 | * | ||
119 | * In this implementation this means choosing an auth_key for later use in | ||
120 | * a hmac at random. | ||
121 | */ | ||
122 | struct Sampler * | ||
123 | SAMPLER_init() | ||
124 | { | ||
125 | struct Sampler *s; | ||
126 | |||
127 | s = GNUNET_new(struct Sampler); | ||
128 | |||
129 | // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()... | ||
130 | GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG, | ||
131 | &(s->auth_key.key), | ||
132 | GNUNET_CRYPTO_HASH_LENGTH); | ||
133 | |||
134 | s->peer_id = own_identity; // Maybe set to own PeerID. So we always have | ||
135 | // a valid PeerID in the sampler. | ||
136 | // Maybe take a PeerID as second argument. | ||
137 | |||
138 | GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id, | ||
139 | sizeof(struct GNUNET_PeerIdentity), | ||
140 | &s->peer_id_hash); | ||
141 | |||
142 | return s; | ||
143 | } | ||
144 | |||
145 | /** | ||
146 | * Compare two hashes. | ||
147 | * | ||
148 | * Returns if the first one is smaller then the second. | ||
149 | * Used by SAMPLER_next() to compare hashes. | ||
150 | */ | ||
151 | int | ||
152 | hash_cmp(struct GNUNET_HashCode hash1, struct GNUNET_HashCode hash2) | ||
153 | { | ||
154 | return memcmp( (const void *) &hash1, (const void *) & hash2, sizeof(struct GNUNET_HashCode)) < 0; | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * Input an PeerID into the given sampler. | ||
159 | */ | ||
160 | static void | ||
161 | SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *id, SAMPLER_deleteCB del_cb, void *cb_cls) | ||
162 | { | ||
163 | struct GNUNET_HashCode other_hash; | ||
164 | |||
165 | GNUNET_CRYPTO_hmac(&s->auth_key, | ||
166 | id, | ||
167 | sizeof(struct GNUNET_PeerIdentity), | ||
168 | &other_hash); | ||
169 | |||
170 | if ( NULL == s->peer_id ) { // Or whatever is a valid way to say | ||
171 | // "we have no PeerID at the moment" | ||
172 | *s->peer_id = *id; | ||
173 | s->peer_id_hash = other_hash; | ||
174 | |||
175 | } else { | ||
176 | |||
177 | if ( hash_cmp(other_hash, s->peer_id_hash) ) { | ||
178 | LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Discarding old PeerID %s\n", | ||
179 | GNUNET_i2s(id), GNUNET_i2s(s->peer_id)); | ||
180 | |||
181 | if ( NULL != del_cb ) { | ||
182 | del_cb(cb_cls, s->peer_id, s->peer_id_hash); | ||
183 | } | ||
184 | *s->peer_id = *id; | ||
185 | s->peer_id_hash = other_hash; | ||
186 | |||
187 | } else { | ||
188 | LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Keeping old PeerID %s\n", | ||
189 | GNUNET_i2s(id), GNUNET_i2s(s->peer_id)); | ||
190 | } | ||
191 | |||
192 | } | ||
193 | } | ||
194 | |||
195 | |||
196 | |||
197 | /** | ||
198 | * A fuction to update every sampler in the given list | ||
199 | */ | ||
200 | static void | ||
201 | SAMPLER_update_list(struct GNUNET_CONTAINER_SList *lst, const struct GNUNET_PeerIdentity *id, | ||
202 | SAMPLER_deleteCB del_cb, void *cb_cls) | ||
203 | { | ||
204 | struct GNUNET_CONTAINER_SList_Iterator *iter; | ||
205 | struct Sampler *sampler; | ||
206 | size_t s; | ||
207 | |||
208 | iter = GNUNET_malloc(sizeof(struct GNUNET_CONTAINER_SList_Iterator)); | ||
209 | *iter = GNUNET_CONTAINER_slist_begin(lst); | ||
210 | s = sizeof(struct Sampler); | ||
211 | do { | ||
212 | sampler = (struct Sampler *) GNUNET_CONTAINER_slist_get(iter, &s); | ||
213 | SAMPLER_next(sampler, id, del_cb, cb_cls); | ||
214 | } while ( GNUNET_NO != GNUNET_CONTAINER_slist_next(iter) ); | ||
215 | |||
216 | GNUNET_CONTAINER_slist_iter_destroy(iter); | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * Get one random peer out of the sampled peers. | ||
221 | * | ||
222 | * We might want to reinitialise this sampler after giving the | ||
223 | * corrsponding peer to the client. | ||
224 | */ | ||
225 | struct GNUNET_PeerIdentity* | ||
226 | SAMPLER_get_rand_peer (struct GNUNET_CONTAINER_SList *lst) | ||
227 | { | ||
228 | uint64_t list_size; | ||
229 | |||
230 | LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n"); | ||
231 | |||
232 | list_size = (uint64_t) GNUNET_CONTAINER_slist_count(lst); | ||
233 | |||
234 | if ( 0 == list_size ) { | ||
235 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity)); | ||
236 | return own_identity; | ||
237 | } else { | ||
238 | uint64_t index; | ||
239 | struct GNUNET_CONTAINER_SList_Iterator *iter; | ||
240 | uint64_t i; | ||
241 | size_t s; | ||
242 | struct GNUNET_PeerIdentity *peer; | ||
243 | |||
244 | /** | ||
245 | * Choose the index of the peer we want to give back | ||
246 | * at random from the interval of the sampler list | ||
247 | */ | ||
248 | index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, | ||
249 | list_size); | ||
250 | // TODO check that it does not overflow | ||
251 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Length of Slist: %" PRIu64 ", index: %" PRIu64 "\n", list_size, index); | ||
252 | |||
253 | s = sizeof( struct Sampler ); | ||
254 | iter = GNUNET_malloc(sizeof(struct GNUNET_CONTAINER_SList_Iterator)); | ||
255 | *iter = GNUNET_CONTAINER_slist_begin(lst); | ||
256 | for ( i = 0 ; i < index ; i++ ) { | ||
257 | if (GNUNET_NO == GNUNET_CONTAINER_slist_next(iter) ) { // Maybe unneeded | ||
258 | *iter = GNUNET_CONTAINER_slist_begin(lst); | ||
259 | } | ||
260 | } | ||
261 | |||
262 | // TODO something missing? | ||
263 | |||
264 | // FIXME this looks wrong: | ||
265 | peer = ((struct Sampler *) GNUNET_CONTAINER_slist_get(iter, &s))->peer_id; | ||
266 | GNUNET_CONTAINER_slist_iter_destroy(iter); | ||
267 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s (own ID: %s)\n", GNUNET_i2s(peer), GNUNET_i2s(own_identity)); | ||
268 | |||
269 | return peer; | ||
270 | } | ||
271 | } | ||
272 | |||
273 | /** | ||
274 | * Get n random peers out of the sampled peers. | ||
275 | * | ||
276 | * We might want to reinitialise this sampler after giving the | ||
277 | * corrsponding peer to the client. | ||
278 | * Random with or without consumption? | ||
279 | */ | ||
280 | struct GNUNET_PeerIdentity** // TODO give back simple array | ||
281 | SAMPLER_get_n_rand_peers (struct GNUNET_CONTAINER_SList *lst, uint64_t n) | ||
282 | { | ||
283 | // TODO check if we have too much (distinct) sampled peers | ||
284 | // If we are not ready yet maybe schedule for later | ||
285 | struct GNUNET_PeerIdentity **peers; | ||
286 | uint64_t i; | ||
287 | |||
288 | peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity *)); | ||
289 | |||
290 | for ( i = 0 ; i < n ; i++ ) { | ||
291 | peers[i] = SAMPLER_get_rand_peer(lst); | ||
292 | } | ||
293 | |||
294 | // TODO something else missing? | ||
295 | return peers; | ||
296 | } | ||
297 | |||
298 | /** | ||
299 | * Counts how many Samplers currently hold a given PeerID. | ||
300 | */ | ||
301 | uint64_t | ||
302 | SAMPLER_count_id ( struct GNUNET_CONTAINER_SList *lst, struct GNUNET_PeerIdentity *id ) { | ||
303 | size_t s; | ||
304 | struct GNUNET_CONTAINER_SList_Iterator *iter; | ||
305 | uint64_t count; | ||
306 | |||
307 | s = sizeof( struct Sampler ); | ||
308 | iter = GNUNET_new(struct GNUNET_CONTAINER_SList_Iterator); | ||
309 | *iter = GNUNET_CONTAINER_slist_begin(lst); | ||
310 | count = 0; | ||
311 | while ( GNUNET_YES == GNUNET_CONTAINER_slist_next(iter) ) { | ||
312 | if ( peer_id_cmp( ((struct Sampler *) GNUNET_CONTAINER_slist_get(iter, &s))->peer_id, id) ) { | ||
313 | count++; | ||
314 | } | ||
315 | } | ||
316 | GNUNET_CONTAINER_slist_iter_destroy(iter); | ||
317 | return count; | ||
318 | } | ||
319 | |||
320 | |||
321 | /*********************************************************************** | ||
322 | * /Sampler | ||
323 | ***********************************************************************/ | ||
324 | |||
325 | |||
326 | |||
327 | /*********************************************************************** | ||
328 | * Gossip list | ||
329 | ***********************************************************************/ | ||
330 | |||
331 | ///** | ||
332 | // * Get one random peer out of the gossiped peer list. | ||
333 | // */ | ||
334 | // struct GNUNET_PeerIdentity * | ||
335 | //get_random_peer(struct GNUNET_CONTAINER_MultiPeerMap * lst) | ||
336 | //{ | ||
337 | // size_t n; | ||
338 | // struct GNUNET_CONTAINER_MultiPeerMapIterator *iter; | ||
339 | // uint64_t index; | ||
340 | // uint64_t i; | ||
341 | // struct GNUNET_PeerIdentity *peer; | ||
342 | // | ||
343 | // n = (size_t) GNUNET_CONTAINER_multipeermap_size(lst); | ||
344 | // index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, | ||
345 | // (uint64_t) n); | ||
346 | // iter = GNUNET_CONTAINER_multipeermap_iterator_create(lst); | ||
347 | // | ||
348 | // for ( i = 0 ; i < index ; i++ ) { | ||
349 | // GNUNET_CONTAINER_multipeermap_iterator_next(iter, NULL, NULL); | ||
350 | // } | ||
351 | // | ||
352 | // peer = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity)); | ||
353 | // GNUNET_CONTAINER_multipeermap_iterator_next(iter, peer, NULL); | ||
354 | // | ||
355 | // return peer; | ||
356 | //} | ||
357 | |||
358 | |||
359 | /*********************************************************************** | ||
360 | * /Gossip list | ||
361 | ***********************************************************************/ | ||
362 | |||
363 | |||
364 | |||
365 | /*********************************************************************** | ||
366 | * Housekeeping with peers | ||
367 | ***********************************************************************/ | ||
368 | |||
369 | /** | ||
370 | * Struct used to store the context of a connected client. | ||
371 | */ | ||
372 | struct client_ctx | ||
373 | { | ||
374 | /** | ||
375 | * The message queue to communicate with the client. | ||
376 | */ | ||
377 | struct GNUNET_MQ_Handle *mq; | ||
378 | }; | ||
379 | |||
380 | /** | ||
381 | * Used to keep track in what lists single peerIDs are. | ||
382 | */ | ||
383 | enum in_list_flag // probably unneeded | ||
384 | { | ||
385 | in_other_sampler_list = 0x1, | ||
386 | in_other_gossip_list = 0x2, // unneeded? | ||
387 | in_own_sampler_list = 0x4, | ||
388 | in_own_gossip_list = 0x8 // unneeded? | ||
389 | }; | ||
390 | |||
391 | /** | ||
392 | * Struct used to keep track of other peer's status | ||
393 | * | ||
394 | * This is stored in a multipeermap. | ||
395 | */ | ||
396 | struct peer_context | ||
397 | { | ||
398 | /** | ||
399 | * In own gossip/sampler list, in other's gossip/sampler list | ||
400 | */ | ||
401 | uint32_t in_flags; // unneeded? | ||
402 | |||
403 | /** | ||
404 | * Message queue open to client | ||
405 | */ | ||
406 | struct GNUNET_MQ_Handle *mq; | ||
407 | |||
408 | /** | ||
409 | * Channel open to client. | ||
410 | */ | ||
411 | struct GNUNET_CADET_Channel *to_channel; | ||
412 | |||
413 | /** | ||
414 | * Channel open from client. | ||
415 | */ | ||
416 | struct GNUNET_CADET_Channel *from_channel; // unneeded | ||
417 | |||
418 | /** | ||
419 | * This is pobably followed by 'statistical' data (when we first saw | ||
420 | * him, how did we get his ID, how many pushes (in a timeinterval), | ||
421 | * ...) | ||
422 | */ | ||
423 | }; | ||
424 | |||
425 | /*********************************************************************** | ||
426 | * /Housekeeping with peers | ||
427 | ***********************************************************************/ | ||
428 | |||
429 | /** | ||
430 | * Set of all peers to keep track of them. | ||
431 | */ | ||
432 | static struct GNUNET_CONTAINER_MultiPeerMap *peer_map; | ||
433 | |||
434 | |||
435 | // -- gossip list length -- | ||
436 | // Depends on the (estimated) size of the | ||
437 | // network. - Initial size might be the | ||
438 | // number of peers cadet provides. | ||
439 | // TODO other events to grow/shrink size? | ||
440 | |||
441 | /** | ||
442 | * List of samplers | ||
443 | */ | ||
444 | struct GNUNET_CONTAINER_SList *sampler_list; | ||
445 | |||
446 | /** | ||
447 | * Sampler list size | ||
448 | * | ||
449 | * Adapts to the nse. Size should be in BigTheta(network_size)^(1/3). | ||
450 | */ | ||
451 | size_t sampler_list_size; | ||
452 | |||
453 | |||
454 | /** | ||
455 | * The gossiped list of peers. | ||
456 | */ | ||
457 | struct GNUNET_PeerIdentity *gossip_list; | ||
458 | |||
459 | /** | ||
460 | * Size of the gossiped list | ||
461 | */ | ||
462 | unsigned int gossip_list_size; | ||
463 | |||
464 | /** | ||
465 | * Min size of the gossip list | ||
466 | */ | ||
467 | uint64_t gossip_list_min_size; | ||
468 | |||
469 | ///** | ||
470 | // * Max size of the gossip list | ||
471 | // * | ||
472 | // * This will probably be left to be set by the client. | ||
473 | // */ | ||
474 | //uint64_t gossip_list_max_size; | ||
475 | |||
476 | |||
477 | /** | ||
478 | * The estimated size of the network. | ||
479 | * | ||
480 | * Influenced by the stdev. | ||
481 | */ | ||
482 | size_t est_size; | ||
483 | |||
484 | |||
485 | |||
486 | /** | ||
487 | * Percentage of total peer number in the gossip list | ||
488 | * to send random PUSHes to | ||
489 | */ | ||
490 | float alpha; | ||
491 | |||
492 | /** | ||
493 | * Percentage of total peer number in the gossip list | ||
494 | * to send random PULLs to | ||
495 | */ | ||
496 | float beta; | ||
497 | |||
498 | /** | ||
499 | * The percentage gamma of history updates. | ||
500 | * Simply 1 - alpha - beta | ||
501 | */ | ||
502 | |||
503 | |||
504 | |||
505 | |||
506 | /** | ||
507 | * Identifier for the main task that runs periodically. | ||
508 | */ | ||
509 | GNUNET_SCHEDULER_TaskIdentifier do_round_task; | ||
510 | |||
511 | /** | ||
512 | * Time inverval the do_round task runs in. | ||
513 | */ | ||
514 | struct GNUNET_TIME_Relative round_interval; | ||
515 | |||
516 | |||
517 | |||
518 | /** | ||
519 | * List to store peers received through pushes temporary. | ||
520 | */ | ||
521 | struct GNUNET_CONTAINER_SList *push_list; | ||
522 | |||
523 | /** | ||
524 | * List to store peers received through pulls temporary. | ||
525 | */ | ||
526 | struct GNUNET_CONTAINER_SList *pull_list; | ||
527 | |||
528 | |||
529 | /** | ||
530 | * Handler to NSE. | ||
531 | */ | ||
532 | struct GNUNET_NSE_Handle *nse; | ||
533 | |||
534 | /** | ||
535 | * Handler to CADET. | ||
536 | */ | ||
537 | struct GNUNET_CADET_Handle *cadet_handle; | ||
538 | |||
539 | |||
540 | /*********************************************************************** | ||
541 | * Util functions | ||
542 | ***********************************************************************/ | ||
543 | |||
544 | /** | ||
545 | * Get random peer from the gossip list. | ||
546 | */ | ||
547 | struct GNUNET_PeerIdentity * | ||
548 | get_rand_gossip_peer() | ||
549 | { | ||
550 | uint64_t index; | ||
551 | struct GNUNET_PeerIdentity *peer; | ||
552 | |||
553 | // TODO find a better solution. | ||
554 | // FIXME if we have only own ID in gossip list this will block | ||
555 | // but then we might have a problem nevertheless ? | ||
556 | |||
557 | do { | ||
558 | |||
559 | /**; | ||
560 | * Choose the index of the peer we want to return | ||
561 | * at random from the interval of the gossip list | ||
562 | */ | ||
563 | index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, | ||
564 | gossip_list_size); | ||
565 | |||
566 | peer = &(gossip_list[index]); | ||
567 | } while ( own_identity == peer || NULL == peer ); | ||
568 | |||
569 | return peer; | ||
570 | } | ||
571 | |||
572 | /** | ||
573 | * Get the message queue of a specific peer. | ||
574 | * | ||
575 | * If we already have a message queue open to this client, | ||
576 | * simply return it, otherways create one. | ||
577 | */ | ||
578 | struct GNUNET_MQ_Handle * | ||
579 | get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, struct GNUNET_PeerIdentity *peer_id) | ||
580 | { | ||
581 | struct peer_context *ctx; | ||
582 | struct GNUNET_MQ_Handle * mq; | ||
583 | struct GNUNET_CADET_Channel *channel; | ||
584 | |||
585 | if ( GNUNET_OK != GNUNET_CONTAINER_multipeermap_contains( peer_map, peer_id ) ) { | ||
586 | |||
587 | channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id, | ||
588 | GNUNET_RPS_CADET_PORT, | ||
589 | GNUNET_CADET_OPTION_RELIABLE); | ||
590 | mq = GNUNET_CADET_mq_create(channel); | ||
591 | |||
592 | ctx = GNUNET_malloc(sizeof(struct peer_context)); | ||
593 | ctx->in_flags = 0; | ||
594 | ctx->to_channel = channel; | ||
595 | ctx->mq = mq; | ||
596 | |||
597 | GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx, | ||
598 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | ||
599 | } else { | ||
600 | ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer_id); | ||
601 | if ( NULL == ctx->mq ) { | ||
602 | if ( NULL == ctx->to_channel ) { | ||
603 | channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id, | ||
604 | GNUNET_RPS_CADET_PORT, | ||
605 | GNUNET_CADET_OPTION_RELIABLE); | ||
606 | ctx->to_channel = channel; | ||
607 | } | ||
608 | |||
609 | mq = GNUNET_CADET_mq_create(ctx->to_channel); | ||
610 | ctx->mq = mq; | ||
611 | } | ||
612 | } | ||
613 | |||
614 | return ctx->mq; | ||
615 | } | ||
616 | |||
617 | |||
618 | /*********************************************************************** | ||
619 | * /Util functions | ||
620 | ***********************************************************************/ | ||
621 | |||
622 | /** | ||
623 | * Function called by NSE. | ||
624 | * | ||
625 | * Updates sizes of sampler list and gossip list and adapt those lists | ||
626 | * accordingly. | ||
627 | */ | ||
628 | void | ||
629 | nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev) | ||
630 | { | ||
631 | double estimate; | ||
632 | //double scale; // TODO this might go gloabal/config | ||
633 | |||
634 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev); | ||
635 | //scale = .01; | ||
636 | estimate = 1 << (uint64_t) round(logestimate); | ||
637 | // GNUNET_NSE_log_estimate_to_n (logestimate); | ||
638 | estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add | ||
639 | if ( 0 < estimate ) { | ||
640 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate); | ||
641 | est_size = estimate; | ||
642 | } else { | ||
643 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate); | ||
644 | } | ||
645 | } | ||
646 | |||
647 | /** | ||
648 | * Handle RPS request from the client. | ||
649 | * | ||
650 | * @param cls closure | ||
651 | * @param client identification of the client | ||
652 | * @param message the actual message | ||
653 | */ | ||
654 | static void | ||
655 | // TODO rename | ||
656 | handle_cs_request (void *cls, | ||
657 | struct GNUNET_SERVER_Client *client, | ||
658 | const struct GNUNET_MessageHeader *message) | ||
659 | { | ||
660 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n"); | ||
661 | |||
662 | struct GNUNET_RPS_CS_RequestMessage *msg; | ||
663 | //unsigned int n_arr[sampler_list_size];// = | ||
664 | //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size); | ||
665 | //struct GNUNET_MQ_Handle *mq; | ||
666 | struct client_ctx *cli_ctx; | ||
667 | struct GNUNET_MQ_Envelope *ev; | ||
668 | struct GNUNET_RPS_CS_ReplyMessage *out_msg; | ||
669 | uint64_t num_peers; | ||
670 | uint64_t i; | ||
671 | |||
672 | // TODO | ||
673 | msg = (struct GNUNET_RPS_CS_RequestMessage *) message; | ||
674 | // Does not work because the compiler seems not to find it. | ||
675 | cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx); | ||
676 | if ( NULL == cli_ctx ) { | ||
677 | cli_ctx = GNUNET_new(struct client_ctx); | ||
678 | cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client); | ||
679 | GNUNET_SERVER_client_set_user_context(client, cli_ctx); | ||
680 | } | ||
681 | |||
682 | //mq = GNUNET_MQ_queue_for_server_client(client); | ||
683 | |||
684 | // TODO How many peers do we give back? | ||
685 | // Wait until we have enough random peers? | ||
686 | |||
687 | ev = GNUNET_MQ_msg_extra(out_msg, | ||
688 | GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity), | ||
689 | GNUNET_MESSAGE_TYPE_RPS_CS_REPLY); | ||
690 | out_msg->num_peers = GNUNET_ntohll(msg->num_peers); | ||
691 | |||
692 | num_peers = GNUNET_ntohll(msg->num_peers); | ||
693 | //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers); | ||
694 | for ( i = 0 ; i < num_peers ; i++ ) { | ||
695 | memcpy(&out_msg[1] + i * sizeof(struct GNUNET_PeerIdentity), | ||
696 | SAMPLER_get_rand_peer(sampler_list), | ||
697 | sizeof(struct GNUNET_PeerIdentity)); | ||
698 | } | ||
699 | |||
700 | GNUNET_MQ_send(cli_ctx->mq, ev); | ||
701 | //GNUNET_MQ_send(mq, ev); | ||
702 | //GNUNET_MQ_destroy(mq); | ||
703 | |||
704 | GNUNET_SERVER_receive_done (client, | ||
705 | GNUNET_OK); | ||
706 | } | ||
707 | |||
708 | /** | ||
709 | * Handle a PUSH message from another peer. | ||
710 | * | ||
711 | * Check the proof of work and store the PeerID | ||
712 | * in the temporary list for pushed PeerIDs. | ||
713 | * | ||
714 | * @param cls Closure | ||
715 | * @param channel The channel the PUSH was received over | ||
716 | * @param channel_ctx The context associated with this channel | ||
717 | * @param msg The message header | ||
718 | */ | ||
719 | static int | ||
720 | handle_peer_push (void *cls, | ||
721 | struct GNUNET_CADET_Channel *channel, | ||
722 | void **channel_ctx, | ||
723 | const struct GNUNET_MessageHeader *msg) | ||
724 | { | ||
725 | LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n"); | ||
726 | |||
727 | struct GNUNET_PeerIdentity *peer; | ||
728 | |||
729 | // TODO check the proof of work | ||
730 | // and check limit for PUSHes | ||
731 | // IF we count per peer PUSHes | ||
732 | // maybe remove from gossip/sampler list | ||
733 | |||
734 | peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER ); | ||
735 | |||
736 | /* Add the sending peer to the push_list */ | ||
737 | GNUNET_CONTAINER_slist_add(push_list, | ||
738 | GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, | ||
739 | peer, sizeof(struct GNUNET_PeerIdentity)); | ||
740 | |||
741 | return GNUNET_OK; | ||
742 | } | ||
743 | |||
744 | /** | ||
745 | * Handle PULL REQUEST request message from another peer. | ||
746 | * | ||
747 | * Reply with the gossip list of PeerIDs. | ||
748 | * | ||
749 | * @param cls Closure | ||
750 | * @param channel The channel the PUSH was received over | ||
751 | * @param channel_ctx The context associated with this channel | ||
752 | * @param msg The message header | ||
753 | */ | ||
754 | static int | ||
755 | handle_peer_pull_request (void *cls, | ||
756 | struct GNUNET_CADET_Channel *channel, | ||
757 | void **channel_ctx, | ||
758 | const struct GNUNET_MessageHeader *msg) | ||
759 | { | ||
760 | |||
761 | struct GNUNET_PeerIdentity *peer; | ||
762 | struct GNUNET_MQ_Handle *mq; | ||
763 | //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg; | ||
764 | struct GNUNET_MQ_Envelope *ev; | ||
765 | struct GNUNET_RPS_P2P_PullReplyMessage *out_msg; | ||
766 | |||
767 | // TODO find some way to keep one peer from spamming with pull requests | ||
768 | // allow only one request per time interval ? | ||
769 | // otherwise remove from peerlist? | ||
770 | |||
771 | peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER); | ||
772 | LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer)); | ||
773 | |||
774 | mq = GNUNET_CADET_mq_create(channel); // TODO without mq? | ||
775 | //mq = get_mq(peer_map, peer); | ||
776 | |||
777 | //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg; | ||
778 | // TODO how many peers do we actually send? | ||
779 | // GNUNET_ntohll(in_msg->num_peers) | ||
780 | ev = GNUNET_MQ_msg_extra(out_msg, | ||
781 | gossip_list_size * sizeof(struct GNUNET_PeerIdentity), | ||
782 | GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY); | ||
783 | out_msg->num_peers = GNUNET_htonll(gossip_list_size); | ||
784 | memcpy(&out_msg[1], gossip_list, | ||
785 | gossip_list_size * sizeof(struct GNUNET_PeerIdentity)); | ||
786 | |||
787 | GNUNET_MQ_send(mq, ev); | ||
788 | |||
789 | GNUNET_MQ_destroy(mq); | ||
790 | |||
791 | |||
792 | return GNUNET_OK; | ||
793 | } | ||
794 | |||
795 | /** | ||
796 | * Handle PULL REPLY message from another peer. | ||
797 | * | ||
798 | * Check whether we sent a corresponding request and | ||
799 | * whether this reply is the first one. | ||
800 | * | ||
801 | * @param cls Closure | ||
802 | * @param channel The channel the PUSH was received over | ||
803 | * @param channel_ctx The context associated with this channel | ||
804 | * @param msg The message header | ||
805 | */ | ||
806 | static int | ||
807 | handle_peer_pull_reply (void *cls, | ||
808 | struct GNUNET_CADET_Channel *channel, | ||
809 | void **channel_ctx, | ||
810 | const struct GNUNET_MessageHeader *msg) | ||
811 | { | ||
812 | LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n"); | ||
813 | |||
814 | struct GNUNET_RPS_P2P_PullReplyMessage *in_msg; | ||
815 | uint64_t i; | ||
816 | |||
817 | // TODO check that we sent a request and that it is the first reply | ||
818 | |||
819 | in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg; | ||
820 | for ( i = 0 ; i < in_msg->num_peers ; i++ ) { | ||
821 | GNUNET_CONTAINER_slist_add(pull_list, | ||
822 | GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, | ||
823 | &in_msg[1] + i * sizeof(struct GNUNET_PeerIdentity), | ||
824 | sizeof(struct GNUNET_PeerIdentity)); | ||
825 | } | ||
826 | |||
827 | // TODO maybe a disconnect happens here | ||
828 | |||
829 | return GNUNET_OK; | ||
830 | } | ||
831 | |||
832 | |||
833 | /** | ||
834 | * Callback called when a Sampler is updated. | ||
835 | */ | ||
836 | void | ||
837 | delete_cb (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash) | ||
838 | { | ||
839 | size_t s; | ||
840 | |||
841 | s = SAMPLER_count_id(sampler_list, id); | ||
842 | if ( 1 >= s ) { | ||
843 | // TODO cleanup peer | ||
844 | GNUNET_CONTAINER_multipeermap_remove_all( peer_map, id); | ||
845 | } | ||
846 | } | ||
847 | |||
848 | |||
849 | /** | ||
850 | * Send out PUSHes and PULLs. | ||
851 | * | ||
852 | * This is executed regylary. | ||
853 | */ | ||
854 | static void | ||
855 | do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
856 | { | ||
857 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n"); | ||
858 | |||
859 | uint64_t i; | ||
860 | struct Sampler *s; | ||
861 | struct GNUNET_CONTAINER_SList_Iterator *iter; | ||
862 | //unsigned int *n_arr; | ||
863 | struct GNUNET_RPS_P2P_PushMessage *push_msg; | ||
864 | struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message | ||
865 | struct GNUNET_MQ_Envelope *ev; | ||
866 | struct GNUNET_PeerIdentity *peer; | ||
867 | |||
868 | // TODO print lists, ... | ||
869 | // TODO cleanup peer_map | ||
870 | |||
871 | iter = GNUNET_new(struct GNUNET_CONTAINER_SList_Iterator); | ||
872 | |||
873 | |||
874 | /* If the NSE has changed adapt the lists accordingly */ | ||
875 | // TODO check nse == 0! | ||
876 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Checking size estimate.\n"); | ||
877 | if ( sampler_list_size < est_size ) { | ||
878 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Growing size.\n"); | ||
879 | /* Grow the lists. */ | ||
880 | for ( i = 0 ; i < est_size - sampler_list_size ; i++ ) { | ||
881 | s = SAMPLER_init(); | ||
882 | GNUNET_CONTAINER_slist_add_end(sampler_list, | ||
883 | GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, // DEPRECATED | ||
884 | s, | ||
885 | sizeof(struct Sampler)); | ||
886 | |||
887 | // TODO add peers to gossiped ones? | ||
888 | } | ||
889 | } else if ( sampler_list_size > est_size ) { | ||
890 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Shrinking size.\n"); | ||
891 | /* Shrink the lists. */ | ||
892 | for ( i = 0 ; i < sampler_list_size - est_size ; i++ ) { | ||
893 | *iter = GNUNET_CONTAINER_slist_begin(sampler_list); | ||
894 | GNUNET_CONTAINER_slist_erase(iter); | ||
895 | GNUNET_CONTAINER_slist_iter_destroy(iter); // Maybe unneeded but I don't know whether _erase() also deletes the iter | ||
896 | } | ||
897 | } | ||
898 | |||
899 | GNUNET_array_grow(gossip_list, gossip_list_size, est_size); // FIXME Do conversion correct or change type | ||
900 | |||
901 | gossip_list_size = sampler_list_size = est_size; | ||
902 | |||
903 | |||
904 | |||
905 | |||
906 | /* Would it make sense to have one shuffeled gossip list and then | ||
907 | * to send PUSHes to first alpha peers, PULL requests to next beta peers and | ||
908 | * use the rest to update sampler? */ | ||
909 | |||
910 | /* Send PUSHes */ | ||
911 | //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size); | ||
912 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %f (%f * %" PRIu64 ") peers.\n", | ||
913 | alpha * gossip_list_size, alpha, gossip_list_size); | ||
914 | for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO compute length | ||
915 | peer = get_rand_gossip_peer(); | ||
916 | // TODO check NULL == peer | ||
917 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s(peer)); | ||
918 | |||
919 | ev = GNUNET_MQ_msg(push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH); | ||
920 | //ev = GNUNET_MQ_msg_extra(); | ||
921 | /* TODO Compute proof of work here | ||
922 | push_msg; */ | ||
923 | push_msg->placeholder = 0; | ||
924 | GNUNET_MQ_send( get_mq(peer_map, peer), ev ); | ||
925 | |||
926 | // TODO modify in_flags of respective peer? | ||
927 | } | ||
928 | |||
929 | |||
930 | /* Send PULL requests */ | ||
931 | // TODO | ||
932 | //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size); | ||
933 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %f (%f * %" PRIu64 ") peers.\n", | ||
934 | beta * gossip_list_size, beta, gossip_list_size); | ||
935 | for ( i = 0 ; i < beta * gossip_list_size ; i++ ){ // TODO compute length | ||
936 | peer = get_rand_gossip_peer(); | ||
937 | // TODO check NULL == peer | ||
938 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s(peer)); | ||
939 | |||
940 | ev = GNUNET_MQ_msg(pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST); | ||
941 | //ev = GNUNET_MQ_msg_extra(); | ||
942 | pull_msg->placeholder = 0; | ||
943 | GNUNET_MQ_send( get_mq(peer_map, peer), ev ); | ||
944 | // TODO modify in_flags of respective peer? | ||
945 | } | ||
946 | |||
947 | |||
948 | |||
949 | |||
950 | /* Update gossip list */ | ||
951 | uint64_t tmp_index; | ||
952 | |||
953 | if ( GNUNET_CONTAINER_slist_count(push_list) <= alpha * gossip_list_size && | ||
954 | GNUNET_CONTAINER_slist_count(push_list) != 0 && | ||
955 | GNUNET_CONTAINER_slist_count(pull_list) != 0 ) { | ||
956 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n"); | ||
957 | |||
958 | for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO use SAMPLER_get_n_rand_peers | ||
959 | /* Update gossip list with peers received through PUSHes */ | ||
960 | gossip_list[i] = *SAMPLER_get_rand_peer(push_list); | ||
961 | // TODO change the in_flags accordingly | ||
962 | } | ||
963 | |||
964 | for ( i = 0 ; i < beta * gossip_list_size ; i++ ) { | ||
965 | /* Update gossip list with peers received through PULLs */ | ||
966 | tmp_index = i + round(alpha * gossip_list_size); | ||
967 | gossip_list[tmp_index] = *SAMPLER_get_rand_peer(pull_list); | ||
968 | // TODO change the in_flags accordingly | ||
969 | } | ||
970 | |||
971 | for ( i = 0 ; i < (1 - (alpha + beta)) * gossip_list_size ; i++ ) { | ||
972 | /* Update gossip list with peers from history */ | ||
973 | tmp_index = i + round((alpha + beta) * gossip_list_size); | ||
974 | gossip_list[tmp_index] = *SAMPLER_get_rand_peer(sampler_list); | ||
975 | // TODO change the in_flags accordingly | ||
976 | } | ||
977 | |||
978 | } else { | ||
979 | LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n"); | ||
980 | } | ||
981 | // TODO independent of that also get some peers from CADET_get_peers()? | ||
982 | |||
983 | |||
984 | |||
985 | /* Update samplers */ | ||
986 | size_t size; | ||
987 | |||
988 | if ( 0 < GNUNET_CONTAINER_slist_count(push_list) ) { | ||
989 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the sampler list from pushes.\n"); | ||
990 | |||
991 | *iter = GNUNET_CONTAINER_slist_begin(push_list); | ||
992 | size = sizeof(struct GNUNET_PeerIdentity); | ||
993 | |||
994 | while ( GNUNET_NO != GNUNET_CONTAINER_slist_next(iter) ) { | ||
995 | peer = (struct GNUNET_PeerIdentity *) GNUNET_CONTAINER_slist_get(iter, &size); | ||
996 | SAMPLER_update_list(sampler_list, peer, NULL, NULL); | ||
997 | // TODO set in_flag | ||
998 | } | ||
999 | GNUNET_CONTAINER_slist_iter_destroy(iter); | ||
1000 | |||
1001 | } else { | ||
1002 | LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the sampler list - received no pushes.\n"); | ||
1003 | } | ||
1004 | |||
1005 | if ( 0 < GNUNET_CONTAINER_slist_count(pull_list) ) { | ||
1006 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the sampler list - received no pushes.\n"); | ||
1007 | |||
1008 | *iter = GNUNET_CONTAINER_slist_begin(pull_list); | ||
1009 | |||
1010 | while ( GNUNET_NO != GNUNET_CONTAINER_slist_next(iter) ) { | ||
1011 | peer = (struct GNUNET_PeerIdentity *) GNUNET_CONTAINER_slist_get(iter, &size); | ||
1012 | SAMPLER_update_list(sampler_list, peer, NULL, NULL); | ||
1013 | // TODO set in_flag | ||
1014 | } | ||
1015 | GNUNET_CONTAINER_slist_iter_destroy(iter); | ||
1016 | } else { | ||
1017 | LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the sampler list - received no pulls.\n"); | ||
1018 | } | ||
1019 | |||
1020 | |||
1021 | GNUNET_free(iter); | ||
1022 | |||
1023 | |||
1024 | // TODO go over whole peer_map and do cleanups | ||
1025 | // delete unneeded peers, set in_flags, check channel/mq | ||
1026 | |||
1027 | |||
1028 | |||
1029 | /* Empty push/pull lists */ | ||
1030 | if ( 0 != GNUNET_CONTAINER_slist_count(push_list) ) { | ||
1031 | GNUNET_CONTAINER_slist_clear(push_list); | ||
1032 | } | ||
1033 | |||
1034 | if ( 0 != GNUNET_CONTAINER_slist_count(push_list) ) { | ||
1035 | GNUNET_CONTAINER_slist_clear(push_list); | ||
1036 | } | ||
1037 | |||
1038 | |||
1039 | /* Schedule next round */ | ||
1040 | // TODO | ||
1041 | do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL ); | ||
1042 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n"); | ||
1043 | } | ||
1044 | |||
1045 | static void | ||
1046 | rps_start (struct GNUNET_SERVER_Handle *server); | ||
1047 | |||
1048 | /** | ||
1049 | * This is called from GNUNET_CADET_get_peers(). | ||
1050 | * | ||
1051 | * It is called on every peer(ID) that cadet somehow has contact with. | ||
1052 | * We use those to initialise the sampler. | ||
1053 | */ | ||
1054 | void | ||
1055 | init_peer_cb (void *cls, | ||
1056 | const struct GNUNET_PeerIdentity *peer, | ||
1057 | int tunnel, // "Do we have a tunnel towards this peer?" | ||
1058 | unsigned int n_paths, // "Number of known paths towards this peer" | ||
1059 | unsigned int best_path) // "How long is the best path? | ||
1060 | // (0 = unknown, 1 = ourselves, 2 = neighbor)" | ||
1061 | { | ||
1062 | // FIXME use the magic 0000 PeerID | ||
1063 | if ( NULL != peer ) { | ||
1064 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s from CADET\n", GNUNET_i2s(peer)); | ||
1065 | SAMPLER_update_list(sampler_list, peer, NULL, NULL); | ||
1066 | if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) ) { | ||
1067 | } else { | ||
1068 | struct peer_context *ctx; | ||
1069 | |||
1070 | ctx = GNUNET_malloc(sizeof(struct peer_context)); | ||
1071 | ctx->in_flags = 0; | ||
1072 | ctx->mq = NULL; | ||
1073 | ctx->to_channel = NULL; | ||
1074 | ctx->from_channel = NULL; | ||
1075 | GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | ||
1076 | } | ||
1077 | |||
1078 | uint64_t i; | ||
1079 | i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size); | ||
1080 | gossip_list[i] = *peer; | ||
1081 | // TODO send push/pull to each of those peers? | ||
1082 | } else { | ||
1083 | rps_start( (struct GNUNET_SERVER_Handle *) cls); | ||
1084 | } | ||
1085 | } | ||
1086 | |||
1087 | |||
1088 | |||
1089 | |||
1090 | /** | ||
1091 | * Task run during shutdown. | ||
1092 | * | ||
1093 | * @param cls unused | ||
1094 | * @param tc unused | ||
1095 | */ | ||
1096 | static void | ||
1097 | shutdown_task (void *cls, | ||
1098 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
1099 | { | ||
1100 | LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n"); | ||
1101 | |||
1102 | GNUNET_NSE_disconnect(nse); | ||
1103 | GNUNET_CADET_disconnect(cadet_handle); | ||
1104 | // TODO delete global data | ||
1105 | } | ||
1106 | |||
1107 | |||
1108 | /** | ||
1109 | * A client disconnected. Remove all of its data structure entries. | ||
1110 | * | ||
1111 | * @param cls closure, NULL | ||
1112 | * @param client identification of the client | ||
1113 | */ | ||
1114 | static void | ||
1115 | handle_client_disconnect (void *cls, | ||
1116 | struct GNUNET_SERVER_Client * client) | ||
1117 | { | ||
1118 | // TODO reinitialise that sampler | ||
1119 | } | ||
1120 | |||
1121 | /** | ||
1122 | * Handle the channel a peer opens to us. | ||
1123 | * | ||
1124 | * @param cls The closure | ||
1125 | * @param channel The channel the peer wants to establish | ||
1126 | * @param initiator The peer's peer ID | ||
1127 | * @param port The port the channel is being established over | ||
1128 | * @param options Further options | ||
1129 | */ | ||
1130 | static void * | ||
1131 | handle_inbound_channel (void *cls, | ||
1132 | struct GNUNET_CADET_Channel *channel, | ||
1133 | const struct GNUNET_PeerIdentity *initiator, | ||
1134 | uint32_t port, | ||
1135 | enum GNUNET_CADET_ChannelOption options) | ||
1136 | { | ||
1137 | LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n"); | ||
1138 | |||
1139 | GNUNET_assert( NULL != channel ); | ||
1140 | |||
1141 | // TODO we might even not store the from_channel | ||
1142 | |||
1143 | if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) { | ||
1144 | ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel; | ||
1145 | // FIXME there might already be an established channel | ||
1146 | } else { | ||
1147 | struct peer_context *ctx; | ||
1148 | |||
1149 | ctx = GNUNET_malloc( sizeof(struct peer_context)); | ||
1150 | ctx->in_flags = in_other_gossip_list; | ||
1151 | ctx->mq = NULL; // TODO create mq? | ||
1152 | ctx->from_channel = channel; | ||
1153 | |||
1154 | GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | ||
1155 | } | ||
1156 | return NULL; // TODO | ||
1157 | } | ||
1158 | |||
1159 | /** | ||
1160 | * This is called when a remote peer destroys a channel. | ||
1161 | * | ||
1162 | * @param cls The closure | ||
1163 | * @param channel The channel being closed | ||
1164 | * @param channel_ctx The context associated with this channel | ||
1165 | */ | ||
1166 | static void | ||
1167 | cleanup_channel(void *cls, | ||
1168 | const struct GNUNET_CADET_Channel *channel, | ||
1169 | void *channel_ctx) | ||
1170 | { | ||
1171 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n"); | ||
1172 | } | ||
1173 | |||
1174 | /** | ||
1175 | * Actually start the service. | ||
1176 | */ | ||
1177 | static void | ||
1178 | rps_start (struct GNUNET_SERVER_Handle *server) | ||
1179 | { | ||
1180 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { | ||
1181 | {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0}, | ||
1182 | {NULL, NULL, 0, 0} | ||
1183 | }; | ||
1184 | |||
1185 | GNUNET_SERVER_add_handlers (server, handlers); | ||
1186 | GNUNET_SERVER_disconnect_notify (server, | ||
1187 | &handle_client_disconnect, | ||
1188 | NULL); | ||
1189 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n"); | ||
1190 | |||
1191 | |||
1192 | |||
1193 | do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL); | ||
1194 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n"); | ||
1195 | |||
1196 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | ||
1197 | &shutdown_task, | ||
1198 | NULL); | ||
1199 | } | ||
1200 | |||
1201 | |||
1202 | |||
1203 | /** | ||
1204 | * Process statistics requests. | ||
1205 | * | ||
1206 | * @param cls closure | ||
1207 | * @param server the initialized server | ||
1208 | * @param c configuration to use | ||
1209 | */ | ||
1210 | static void | ||
1211 | run (void *cls, | ||
1212 | struct GNUNET_SERVER_Handle *server, | ||
1213 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
1214 | { | ||
1215 | // TODO check what this does -- copied from gnunet-boss | ||
1216 | // - seems to work as expected | ||
1217 | GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL); | ||
1218 | |||
1219 | LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n"); | ||
1220 | |||
1221 | uint32_t i; | ||
1222 | |||
1223 | cfg = c; | ||
1224 | |||
1225 | |||
1226 | own_identity = GNUNET_new(struct GNUNET_PeerIdentity); | ||
1227 | |||
1228 | GNUNET_CRYPTO_get_peer_identity(cfg, own_identity); | ||
1229 | |||
1230 | |||
1231 | |||
1232 | /* Get time interval from the configuration */ | ||
1233 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS", | ||
1234 | "ROUNDINTERVAL", | ||
1235 | &round_interval)) | ||
1236 | { | ||
1237 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n"); | ||
1238 | GNUNET_SCHEDULER_shutdown(); | ||
1239 | return; | ||
1240 | } | ||
1241 | |||
1242 | /* Get initial size of sampler/gossip list from the configuration */ | ||
1243 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", | ||
1244 | "INITSIZE", | ||
1245 | (long long unsigned int *) &est_size)) // FIXME convert | ||
1246 | { | ||
1247 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n"); | ||
1248 | GNUNET_SCHEDULER_shutdown(); | ||
1249 | return; | ||
1250 | } | ||
1251 | LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size); | ||
1252 | |||
1253 | gossip_list_size = sampler_list_size = est_size; // TODO rename est_size | ||
1254 | |||
1255 | |||
1256 | gossip_list = NULL; | ||
1257 | |||
1258 | static unsigned int tmp = 0; | ||
1259 | |||
1260 | GNUNET_array_grow(gossip_list, tmp, gossip_list_size); | ||
1261 | |||
1262 | |||
1263 | |||
1264 | /* connect to NSE */ | ||
1265 | nse = GNUNET_NSE_connect(cfg, nse_callback, NULL); | ||
1266 | // TODO check whether that was successful | ||
1267 | // TODO disconnect on shutdown | ||
1268 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n"); | ||
1269 | |||
1270 | |||
1271 | alpha = 0.45; | ||
1272 | beta = 0.45; | ||
1273 | // TODO initialise thresholds - ? | ||
1274 | |||
1275 | ///* Get alpha from the configuration */ | ||
1276 | //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS", | ||
1277 | // "ALPHA", | ||
1278 | // &alpha)) | ||
1279 | //{ | ||
1280 | // LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n"); | ||
1281 | //} | ||
1282 | //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha); | ||
1283 | |||
1284 | ///* Get beta from the configuration */ | ||
1285 | //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS", | ||
1286 | // "BETA", | ||
1287 | // &beta)) | ||
1288 | //{ | ||
1289 | // LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n"); | ||
1290 | //} | ||
1291 | //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta); | ||
1292 | |||
1293 | |||
1294 | |||
1295 | |||
1296 | peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO); | ||
1297 | |||
1298 | |||
1299 | /* Initialise sampler and gossip list */ | ||
1300 | struct Sampler *s; | ||
1301 | |||
1302 | sampler_list = GNUNET_CONTAINER_slist_create(); | ||
1303 | |||
1304 | //if ( gossip_list_size == sampler_list_size ) { | ||
1305 | for ( i = 0 ; i < sampler_list_size ; i++ ) { | ||
1306 | /* Init sampler list */ | ||
1307 | s = SAMPLER_init(); | ||
1308 | GNUNET_CONTAINER_slist_add(sampler_list, | ||
1309 | GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC, // TODO DEPRECATED | ||
1310 | s, | ||
1311 | sizeof(struct Sampler)); | ||
1312 | /* Init gossip list */ | ||
1313 | // TODO init gossip list | ||
1314 | // What do we need to do here? | ||
1315 | } | ||
1316 | //} else { | ||
1317 | // for ( i = 0 ; i < gossip_list_size ; i++ ) { | ||
1318 | // // TODO init gossip list | ||
1319 | // } | ||
1320 | // for ( i = 0 ; i < sampler_list_size ; i++ ) { | ||
1321 | // // TODO init RPF func | ||
1322 | // // TODO init Sample list | ||
1323 | // // TODO init Sampled list | ||
1324 | // } | ||
1325 | //} | ||
1326 | uint64_t tmp_s = (uint64_t) GNUNET_CONTAINER_slist_count(sampler_list); | ||
1327 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Initialised sampler list %" PRIu64 "\n", tmp_s); | ||
1328 | |||
1329 | |||
1330 | |||
1331 | push_list = GNUNET_CONTAINER_slist_create(); | ||
1332 | pull_list = GNUNET_CONTAINER_slist_create(); | ||
1333 | |||
1334 | |||
1335 | |||
1336 | static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = { | ||
1337 | {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0}, | ||
1338 | {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0}, | ||
1339 | {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0}, | ||
1340 | {NULL, 0, 0} | ||
1341 | }; | ||
1342 | |||
1343 | const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h | ||
1344 | cadet_handle = GNUNET_CADET_connect(cfg, | ||
1345 | cls, | ||
1346 | &handle_inbound_channel, | ||
1347 | &cleanup_channel, | ||
1348 | cadet_handlers, | ||
1349 | ports); | ||
1350 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n"); | ||
1351 | |||
1352 | |||
1353 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n"); | ||
1354 | GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server); | ||
1355 | // FIXME use magic 0000 PeerID to _start_ the service | ||
1356 | |||
1357 | // TODO send push/pull to each of those peers? | ||
1358 | |||
1359 | |||
1360 | |||
1361 | } | ||
1362 | |||
1363 | |||
1364 | /** | ||
1365 | * The main function for the rps service. | ||
1366 | * | ||
1367 | * @param argc number of arguments from the command line | ||
1368 | * @param argv command line arguments | ||
1369 | * @return 0 ok, 1 on error | ||
1370 | */ | ||
1371 | int | ||
1372 | main (int argc, char *const *argv) | ||
1373 | { | ||
1374 | return (GNUNET_OK == | ||
1375 | GNUNET_SERVICE_run (argc, | ||
1376 | argv, | ||
1377 | "rps", | ||
1378 | GNUNET_SERVICE_OPTION_NONE, | ||
1379 | &run, NULL)) ? 0 : 1; | ||
1380 | } | ||
1381 | |||
1382 | /* end of gnunet-service-rps.c */ | ||