2 This file is part of GNUnet.
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.
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.
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.
22 * @file rps/gnunet-service-rps_sampler.c
23 * @brief sampler implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
30 #include "gnunet-service-rps_sampler.h"
35 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
37 // multiple 'clients'?
39 // TODO check for overflows
41 // TODO align message structs
43 // hist_size_init, hist_size_max
45 /***********************************************************************
46 * WARNING: This section needs to be reviewed regarding the use of
47 * functions providing (pseudo)randomness!
48 ***********************************************************************/
50 // TODO care about invalid input of the caller (size 0 or less...)
59 * A sampler element sampling one PeerID at a time.
61 struct RPS_SamplerElement
64 * Min-wise linear permutation used by this sampler.
66 * This is an key later used by a hmac.
68 struct GNUNET_CRYPTO_AuthKey auth_key;
71 * The PeerID this sampler currently samples.
73 struct GNUNET_PeerIdentity peer_id;
76 * The according hash value of this PeerID.
78 struct GNUNET_HashCode peer_id_hash;
82 * Time of last request.
84 struct GNUNET_TIME_Absolute last_client_request;
87 * Flag that indicates that we are not holding a valid PeerID right now.
89 enum RPS_SamplerEmpty is_empty;
94 struct GNUNET_TIME_Absolute birth;
97 * How many times a PeerID was put in this sampler.
102 * How many times this sampler changed the peer_id.
109 * Sampler with its own array of SamplerElements
114 * Number of sampler elements we hold.
116 unsigned int sampler_size;
120 * All Samplers in one array.
122 struct RPS_SamplerElement **sampler_elements;
125 * Max time a round takes
127 * Used in the context of RPS
129 struct GNUNET_TIME_Relative max_round_interval;
133 * Closure to _get_n_rand_peers_ready_cb()
135 struct NRandPeersReadyCls
138 * Number of peers we are waiting for.
143 * Number of peers we currently have.
145 uint32_t cur_num_peers;
148 * Pointer to the array holding the ids.
150 struct GNUNET_PeerIdentity *ids;
153 * Callback to be called when all ids are available.
155 RPS_sampler_n_rand_peers_ready_cb callback;
158 * Closure given to the callback
164 * Callback that is called from _get_rand_peer() when the PeerID is ready.
166 * @param cls the closure given alongside this function.
167 * @param id the PeerID that was returned
170 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
171 const struct GNUNET_PeerIdentity *id);
174 * Closure for #sampler_get_rand_peer()
181 struct GetPeerCls *next;
186 struct GetPeerCls *prev;
189 * The sampler this function operates on.
191 struct RPS_Sampler *sampler;
194 * The task for this function.
196 struct GNUNET_SCHEDULER_Task *get_peer_task;
201 RPS_sampler_rand_peer_ready_cont cont;
204 * The closure to the callback @e cont
209 * The address of the id to be stored at
211 struct GNUNET_PeerIdentity *id;
216 // * Global sampler variable.
218 //struct RPS_Sampler *sampler;
222 * The minimal size for the extended sampler elements.
224 static size_t min_size;
227 * The maximal size the extended sampler elements should grow to.
229 static size_t max_size;
232 * The size the extended sampler elements currently have.
234 //static size_t extra_size;
237 * Inedex to the sampler element that is the next to be returned
239 static uint32_t client_get_index;
242 /** FIXME document */
243 static struct GetPeerCls *gpc_head;
244 static struct GetPeerCls *gpc_tail;
248 * Callback to _get_rand_peer() used by _get_n_rand_peers().
250 * Checks whether all n peers are available. If they are,
254 check_n_peers_ready (void *cls,
255 const struct GNUNET_PeerIdentity *id)
257 struct NRandPeersReadyCls *n_peers_cls = cls;
259 n_peers_cls->cur_num_peers++;
260 LOG (GNUNET_ERROR_TYPE_DEBUG,
261 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
262 n_peers_cls->cur_num_peers, n_peers_cls->num_peers);
264 if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers)
265 { /* All peers are ready -- return those to the client */
266 GNUNET_assert (NULL != n_peers_cls->callback);
268 LOG (GNUNET_ERROR_TYPE_DEBUG,
269 "returning %" PRIX32 " peers to the client\n",
270 n_peers_cls->num_peers);
271 n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers);
273 GNUNET_free (n_peers_cls);
279 * Reinitialise a previously initialised sampler element.
281 * @param sampler pointer to the memory that keeps the value.
284 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
286 sampler_el->is_empty = EMPTY;
288 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
289 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
290 &(sampler_el->auth_key.key),
291 GNUNET_CRYPTO_HASH_LENGTH);
293 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
295 sampler_el->birth = GNUNET_TIME_absolute_get ();
296 sampler_el->num_peers = 0;
297 sampler_el->num_change = 0;
302 * (Re)Initialise given Sampler with random min-wise independent function.
304 * In this implementation this means choosing an auth_key for later use in
307 * @return a newly created RPS_SamplerElement which currently holds no id.
309 struct RPS_SamplerElement *
310 RPS_sampler_elem_create (void)
312 struct RPS_SamplerElement *s;
314 s = GNUNET_new (struct RPS_SamplerElement);
316 RPS_sampler_elem_reinit (s);
323 * Input an PeerID into the given sampler element.
325 * @param sampler the sampler the @a s_elem belongs to.
329 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
330 struct RPS_Sampler *sampler,
331 const struct GNUNET_PeerIdentity *other)
333 struct GNUNET_HashCode other_hash;
337 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
339 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
341 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
342 GNUNET_i2s (&(s_elem->peer_id)));
346 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
348 sizeof(struct GNUNET_PeerIdentity),
351 if (EMPTY == s_elem->is_empty)
353 LOG (GNUNET_ERROR_TYPE_DEBUG,
354 "Got PeerID %s; Simply accepting (was empty previously).\n",
356 s_elem->peer_id = *other;
357 s_elem->peer_id_hash = other_hash;
359 s_elem->num_change++;
361 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
363 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
365 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
366 GNUNET_i2s (&s_elem->peer_id));
368 s_elem->num_change++;
372 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
374 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
375 GNUNET_i2s (&s_elem->peer_id));
378 s_elem->is_empty = NOT_EMPTY;
383 * Get the size of the sampler.
385 * @param sampler the sampler to return the size of.
386 * @return the size of the sampler
389 RPS_sampler_get_size (struct RPS_Sampler *sampler)
391 return sampler->sampler_size;
396 * Grow or shrink the size of the sampler.
398 * @param sampler the sampler to resize.
399 * @param new_size the new size of the sampler
402 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
404 unsigned int old_size;
407 // TODO check min and max size
409 old_size = sampler->sampler_size;
411 if (old_size > new_size)
413 /* Temporary store those to properly call the removeCB on those later */
415 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shrinking sampler %d -> %d\n", old_size, new_size);
416 GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, new_size);
417 LOG (GNUNET_ERROR_TYPE_DEBUG,
418 "sampler->sampler_elements now points to %p\n",
419 sampler->sampler_elements);
422 else if (old_size < new_size)
424 LOG (GNUNET_ERROR_TYPE_DEBUG, "Growing sampler %d -> %d\n", old_size, new_size);
425 GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, new_size);
427 for ( i = old_size ; i < new_size ; i++ )
428 { /* Add new sampler elements */
429 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
434 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
438 GNUNET_assert (sampler->sampler_size == new_size);
443 * Grow or shrink the size of the sampler.
445 * @param sampler the sampler to resize.
446 * @param new_size the new size of the sampler
449 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
451 GNUNET_assert (0 < new_size);
452 sampler_resize (sampler, new_size);
459 * @param sampler the sampler to empty.
460 * @param new_size the new size of the sampler
463 sampler_empty (struct RPS_Sampler *sampler)
465 sampler_resize (sampler, 0);
470 * Initialise a tuple of sampler elements.
472 * @param init_size the size the sampler is initialised with
473 * @param ins_cb the callback that will be called on every PeerID that is
474 * newly inserted into a sampler element
475 * @param ins_cls the closure given to #ins_cb
476 * @param rem_cb the callback that will be called on every PeerID that is
477 * removed from a sampler element
478 * @param rem_cls the closure given to #rem_cb
479 * @return a handle to a sampler that consists of sampler elements.
482 RPS_sampler_init (size_t init_size,
483 struct GNUNET_TIME_Relative max_round_interval)
485 struct RPS_Sampler *sampler;
488 /* Initialise context around extended sampler */
489 min_size = 10; // TODO make input to _samplers_init()
490 max_size = 1000; // TODO make input to _samplers_init()
492 sampler = GNUNET_new (struct RPS_Sampler);
493 sampler->sampler_size = 0;
494 sampler->sampler_elements = NULL;
495 sampler->max_round_interval = max_round_interval;
496 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
497 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
498 RPS_sampler_resize (sampler, init_size);
500 client_get_index = 0;
502 //GNUNET_assert (init_size == sampler->sampler_size);
508 * A fuction to update every sampler in the given list
510 * @param sampler the sampler to update.
511 * @param id the PeerID that is put in the sampler
514 RPS_sampler_update (struct RPS_Sampler *sampler,
515 const struct GNUNET_PeerIdentity *id)
519 for ( i = 0 ; i < sampler->sampler_size ; i++ )
520 RPS_sampler_elem_next (sampler->sampler_elements[i],
527 * Reinitialise all previously initialised sampler elements with the given value.
529 * Used to get rid of a PeerID.
531 * @param sampler the sampler to reinitialise a sampler element in.
532 * @param id the id of the sampler elements to update.
535 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
536 const struct GNUNET_PeerIdentity *id)
540 for ( i = 0 ; i < sampler->sampler_size ; i++ )
542 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
544 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
545 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
552 * Get one random peer out of the sampled peers.
554 * We might want to reinitialise this sampler after giving the
555 * corrsponding peer to the client.
556 * Only used internally
559 sampler_get_rand_peer2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
561 struct GetPeerCls *gpc = cls;
564 gpc->get_peer_task = NULL;
565 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, gpc);
566 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
570 * Choose the r_index of the peer we want to return
571 * at random from the interval of the gossip list
573 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
574 gpc->sampler->sampler_size);
576 if ( EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty )
578 gpc->get_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
579 GNUNET_TIME_UNIT_SECONDS,
581 &sampler_get_rand_peer2,
586 *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id;
588 gpc->cont (gpc->cont_cls, gpc->id);
594 * Get one random peer out of the sampled peers.
596 * We might want to reinitialise this sampler after giving the
597 * corrsponding peer to the client.
600 sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
602 struct GetPeerCls *gpc = cls;
603 struct GNUNET_PeerIdentity tmp_id;
604 unsigned int empty_flag;
605 struct RPS_SamplerElement *s_elem;
606 struct GNUNET_TIME_Relative last_request_diff;
607 uint32_t tmp_client_get_index;
609 gpc->get_peer_task = NULL;
610 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
613 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
616 /* Store the next #client_get_index to check whether we cycled over the whole list */
617 if (0 < client_get_index)
618 tmp_client_get_index = client_get_index - 1;
620 tmp_client_get_index = gpc->sampler->sampler_size - 1;
622 LOG (GNUNET_ERROR_TYPE_DEBUG,
623 "sched for later if index reaches %" PRIX32 " (sampler size: %" PRIX32 ").\n",
624 tmp_client_get_index, gpc->sampler->sampler_size);
627 { /* Get first non empty sampler */
628 if (tmp_client_get_index == client_get_index)
629 { /* We once cycled over the whole list */
630 LOG (GNUNET_ERROR_TYPE_DEBUG, "reached tmp_index %" PRIX32 ".\n",
632 GNUNET_assert (NULL == gpc->get_peer_task);
634 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
635 &sampler_get_rand_peer,
640 tmp_id = gpc->sampler->sampler_elements[client_get_index]->peer_id;
641 empty_flag = gpc->sampler->sampler_elements[client_get_index]->is_empty;
642 RPS_sampler_elem_reinit (gpc->sampler->sampler_elements[client_get_index]);
643 if (EMPTY != empty_flag)
644 RPS_sampler_elem_next (gpc->sampler->sampler_elements[client_get_index],
648 /* Cycle the #client_get_index one step further */
649 if ( client_get_index == gpc->sampler->sampler_size - 1 )
650 client_get_index = 0;
654 LOG (GNUNET_ERROR_TYPE_DEBUG, "incremented index to %" PRIX32 ".\n",
656 } while (EMPTY == gpc->sampler->sampler_elements[client_get_index]->is_empty);
658 s_elem = gpc->sampler->sampler_elements[client_get_index];
659 *gpc->id = s_elem->peer_id;
661 /* Check whether we may use this sampler to give it back to the client */
662 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
665 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
666 GNUNET_TIME_absolute_get ());
667 /* We're not going to give it back now if it was
668 * already requested by a client this round */
669 if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us)
671 LOG (GNUNET_ERROR_TYPE_DEBUG,
672 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
673 ///* How many time remains untile the next round has started? */
674 //inv_last_request_diff =
675 // GNUNET_TIME_absolute_get_difference (last_request_diff,
676 // sampler->max_round_interval);
677 // add a little delay
678 /* Schedule it one round later */
679 GNUNET_assert (NULL == gpc->get_peer_task);
681 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
682 &sampler_get_rand_peer,
686 // TODO add other reasons to wait here
689 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
691 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, gpc);
692 gpc->cont (gpc->cont_cls, gpc->id);
698 * Get n random peers out of the sampled peers.
700 * We might want to reinitialise this sampler after giving the
701 * corrsponding peer to the client.
702 * Random with or without consumption?
704 * @param sampler the sampler to get peers from.
705 * @param cb callback that will be called once the ids are ready.
706 * @param cls closure given to @a cb
707 * @param for_client #GNUNET_YES if result is used for client,
708 * #GNUNET_NO if used internally
709 * @param num_peers the number of peers requested
712 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
713 RPS_sampler_n_rand_peers_ready_cb cb,
714 void *cls, uint32_t num_peers, int for_client)
716 GNUNET_assert (0 != sampler->sampler_size);
718 // TODO check if we have too much (distinct) sampled peers
720 struct NRandPeersReadyCls *cb_cls;
721 struct GetPeerCls *gpc;
723 cb_cls = GNUNET_new (struct NRandPeersReadyCls);
724 cb_cls->num_peers = num_peers;
725 cb_cls->cur_num_peers = 0;
726 cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
727 cb_cls->callback = cb;
730 LOG (GNUNET_ERROR_TYPE_DEBUG,
731 "Scheduling requests for %" PRIX32 " peers\n", num_peers);
733 for ( i = 0 ; i < num_peers ; i++ )
735 gpc = GNUNET_new (struct GetPeerCls);
736 gpc->sampler = sampler;
737 gpc->cont = check_n_peers_ready;
738 gpc->cont_cls = cb_cls;
739 gpc->id = &cb_cls->ids[i];
741 // maybe add a little delay
742 if (GNUNET_YES == for_client)
743 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (&sampler_get_rand_peer, gpc);
744 else if (GNUNET_NO == for_client)
745 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (&sampler_get_rand_peer2, gpc);
749 GNUNET_CONTAINER_DLL_insert (gpc_head, gpc_tail, gpc);
755 * Counts how many Samplers currently hold a given PeerID.
757 * @param sampler the sampler to count ids in.
758 * @param id the PeerID to count.
760 * @return the number of occurrences of id.
763 RPS_sampler_count_id (struct RPS_Sampler *sampler,
764 const struct GNUNET_PeerIdentity *id)
770 for ( i = 0 ; i < sampler->sampler_size ; i++ )
772 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
773 && EMPTY != sampler->sampler_elements[i]->is_empty)
781 * Cleans the sampler.
784 RPS_sampler_destroy (struct RPS_Sampler *sampler)
786 struct GetPeerCls *i;
788 for (i = gpc_head; NULL != i; i = gpc_head)
790 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, i);
791 GNUNET_SCHEDULER_cancel (i->get_peer_task);
795 sampler_empty (sampler);
796 GNUNET_free (sampler);
799 /* end of gnunet-service-rps.c */