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 * File name to log to
140 * Closure to _get_n_rand_peers_ready_cb()
142 struct NRandPeersReadyCls
145 * Number of peers we are waiting for.
150 * Number of peers we currently have.
152 uint32_t cur_num_peers;
155 * Pointer to the array holding the ids.
157 struct GNUNET_PeerIdentity *ids;
160 * Callback to be called when all ids are available.
162 RPS_sampler_n_rand_peers_ready_cb callback;
165 * Closure given to the callback
171 * Callback that is called from _get_rand_peer() when the PeerID is ready.
173 * @param cls the closure given alongside this function.
174 * @param id the PeerID that was returned
177 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
178 const struct GNUNET_PeerIdentity *id);
181 * Closure for #sampler_get_rand_peer()
188 struct GetPeerCls *next;
193 struct GetPeerCls *prev;
196 * The sampler this function operates on.
198 struct RPS_Sampler *sampler;
201 * The task for this function.
203 struct GNUNET_SCHEDULER_Task *get_peer_task;
208 RPS_sampler_rand_peer_ready_cont cont;
211 * The closure to the callback @e cont
216 * The address of the id to be stored at
218 struct GNUNET_PeerIdentity *id;
223 // * Global sampler variable.
225 //struct RPS_Sampler *sampler;
229 * The minimal size for the extended sampler elements.
231 static size_t min_size;
234 * The maximal size the extended sampler elements should grow to.
236 static size_t max_size;
239 * The size the extended sampler elements currently have.
241 //static size_t extra_size;
244 * Inedex to the sampler element that is the next to be returned
246 static uint32_t client_get_index;
251 * This function is used to facilitate writing important information to disk
253 #define to_file(file_name, ...) do {char tmp_buf[512];\
255 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
257 LOG (GNUNET_ERROR_TYPE_WARNING,\
258 "Failed to create tmp_buf\n");\
260 to_file_(file_name,tmp_buf);\
264 to_file_ (char *file_name, char *line)
266 struct GNUNET_DISK_FileHandle *f;
267 char output_buffer[512];
273 if (NULL == (f = GNUNET_DISK_file_open (file_name,
274 GNUNET_DISK_OPEN_APPEND |
275 GNUNET_DISK_OPEN_WRITE |
276 GNUNET_DISK_OPEN_CREATE,
277 GNUNET_DISK_PERM_USER_WRITE)))
279 LOG (GNUNET_ERROR_TYPE_WARNING,
280 "Not able to open file %s\n",
284 size = GNUNET_snprintf (output_buffer,
285 sizeof (output_buffer),
287 GNUNET_TIME_absolute_get ().abs_value_us,
291 LOG (GNUNET_ERROR_TYPE_WARNING,
292 "Failed to write string to buffer (size: %i)\n",
297 size2 = GNUNET_DISK_file_write (f, output_buffer, size);
300 LOG (GNUNET_ERROR_TYPE_WARNING,
301 "Unable to write to file! (Size: %u, size2: %u)\n",
307 if (GNUNET_YES != GNUNET_DISK_file_close (f))
308 LOG (GNUNET_ERROR_TYPE_WARNING,
309 "Unable to close file\n");
314 /** FIXME document */
315 static struct GetPeerCls *gpc_head;
316 static struct GetPeerCls *gpc_tail;
320 * Callback to _get_rand_peer() used by _get_n_rand_peers().
322 * Checks whether all n peers are available. If they are,
326 check_n_peers_ready (void *cls,
327 const struct GNUNET_PeerIdentity *id)
329 struct NRandPeersReadyCls *n_peers_cls = cls;
331 n_peers_cls->cur_num_peers++;
332 LOG (GNUNET_ERROR_TYPE_DEBUG,
333 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
334 n_peers_cls->cur_num_peers, n_peers_cls->num_peers);
336 if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers)
337 { /* All peers are ready -- return those to the client */
338 GNUNET_assert (NULL != n_peers_cls->callback);
340 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "returning %" PRIX32 " peers to the client\n",
342 n_peers_cls->num_peers);
343 n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers);
345 GNUNET_free (n_peers_cls);
351 * Reinitialise a previously initialised sampler element.
353 * @param sampler pointer to the memory that keeps the value.
356 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
358 sampler_el->is_empty = EMPTY;
360 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
361 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
362 &(sampler_el->auth_key.key),
363 GNUNET_CRYPTO_HASH_LENGTH);
365 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
367 sampler_el->birth = GNUNET_TIME_absolute_get ();
368 sampler_el->num_peers = 0;
369 sampler_el->num_change = 0;
374 * (Re)Initialise given Sampler with random min-wise independent function.
376 * In this implementation this means choosing an auth_key for later use in
379 * @return a newly created RPS_SamplerElement which currently holds no id.
381 struct RPS_SamplerElement *
382 RPS_sampler_elem_create (void)
384 struct RPS_SamplerElement *s;
386 s = GNUNET_new (struct RPS_SamplerElement);
388 RPS_sampler_elem_reinit (s);
395 * Input an PeerID into the given sampler element.
397 * @param sampler the sampler the @a s_elem belongs to.
401 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
402 struct RPS_Sampler *sampler,
403 const struct GNUNET_PeerIdentity *other)
405 struct GNUNET_HashCode other_hash;
409 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
411 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
413 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
414 GNUNET_i2s (&(s_elem->peer_id)));
418 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
420 sizeof(struct GNUNET_PeerIdentity),
423 if (EMPTY == s_elem->is_empty)
425 LOG (GNUNET_ERROR_TYPE_DEBUG,
426 "Got PeerID %s; Simply accepting (was empty previously).\n",
428 s_elem->peer_id = *other;
429 s_elem->peer_id_hash = other_hash;
431 s_elem->num_change++;
433 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
435 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
437 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
438 GNUNET_i2s (&s_elem->peer_id));
439 s_elem->peer_id = *other;
440 s_elem->peer_id_hash = other_hash;
442 s_elem->num_change++;
446 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
449 GNUNET_i2s (&s_elem->peer_id));
452 s_elem->is_empty = NOT_EMPTY;
457 * Get the size of the sampler.
459 * @param sampler the sampler to return the size of.
460 * @return the size of the sampler
463 RPS_sampler_get_size (struct RPS_Sampler *sampler)
465 return sampler->sampler_size;
470 * Grow or shrink the size of the sampler.
472 * @param sampler the sampler to resize.
473 * @param new_size the new size of the sampler
476 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
478 unsigned int old_size;
481 // TODO check min and max size
483 old_size = sampler->sampler_size;
485 if (old_size > new_size)
487 /* Temporary store those to properly call the removeCB on those later */
489 LOG (GNUNET_ERROR_TYPE_DEBUG,
490 "Shrinking sampler %d -> %d\n",
494 to_file (sampler->file_name,
495 "Shrinking sampler %d -> %d\n",
499 GNUNET_array_grow (sampler->sampler_elements,
500 sampler->sampler_size,
502 LOG (GNUNET_ERROR_TYPE_DEBUG,
503 "sampler->sampler_elements now points to %p\n",
504 sampler->sampler_elements);
507 else if (old_size < new_size)
509 LOG (GNUNET_ERROR_TYPE_DEBUG,
510 "Growing sampler %d -> %d\n",
514 to_file (sampler->file_name,
515 "Growing sampler %d -> %d\n",
519 GNUNET_array_grow (sampler->sampler_elements,
520 sampler->sampler_size,
523 for (i = old_size ; i < new_size ; i++)
524 { /* Add new sampler elements */
525 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
527 to_file (sampler->file_name,
528 "%" PRIu32 ": Initialised empty sampler element\n",
530 //"New sampler with key %s\n",
531 //GNUNET_h2s_full (sampler->sampler_elements[i]->auth_key));
537 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
541 GNUNET_assert (sampler->sampler_size == new_size);
546 * Grow or shrink the size of the sampler.
548 * @param sampler the sampler to resize.
549 * @param new_size the new size of the sampler
552 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
554 GNUNET_assert (0 < new_size);
555 sampler_resize (sampler, new_size);
562 * @param sampler the sampler to empty.
563 * @param new_size the new size of the sampler
566 sampler_empty (struct RPS_Sampler *sampler)
568 sampler_resize (sampler, 0);
573 * Initialise a tuple of sampler elements.
575 * @param init_size the size the sampler is initialised with
576 * @param ins_cb the callback that will be called on every PeerID that is
577 * newly inserted into a sampler element
578 * @param ins_cls the closure given to #ins_cb
579 * @param rem_cb the callback that will be called on every PeerID that is
580 * removed from a sampler element
581 * @param rem_cls the closure given to #rem_cb
582 * @return a handle to a sampler that consists of sampler elements.
585 RPS_sampler_init (size_t init_size,
586 struct GNUNET_TIME_Relative max_round_interval)
588 struct RPS_Sampler *sampler;
591 /* Initialise context around extended sampler */
592 min_size = 10; // TODO make input to _samplers_init()
593 max_size = 1000; // TODO make input to _samplers_init()
595 sampler = GNUNET_new (struct RPS_Sampler);
598 if (NULL == (sampler->file_name = GNUNET_DISK_mktemp ("sampler-")))
599 LOG (GNUNET_ERROR_TYPE_WARNING,
600 "Could not create file\n");
603 sampler->sampler_size = 0;
604 sampler->sampler_elements = NULL;
605 sampler->max_round_interval = max_round_interval;
606 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
607 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
608 RPS_sampler_resize (sampler, init_size);
610 client_get_index = 0;
612 //GNUNET_assert (init_size == sampler->sampler_size);
618 * A fuction to update every sampler in the given list
620 * @param sampler the sampler to update.
621 * @param id the PeerID that is put in the sampler
624 RPS_sampler_update (struct RPS_Sampler *sampler,
625 const struct GNUNET_PeerIdentity *id)
629 for (i = 0 ; i < sampler->sampler_size ; i++)
631 RPS_sampler_elem_next (sampler->sampler_elements[i],
635 to_file (sampler->file_name,
636 "%" PRIu32 ": Now contains %s\n",
638 GNUNET_i2s_full (&sampler->sampler_elements[i]->peer_id));
645 * Reinitialise all previously initialised sampler elements with the given value.
647 * Used to get rid of a PeerID.
649 * @param sampler the sampler to reinitialise a sampler element in.
650 * @param id the id of the sampler elements to update.
653 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
654 const struct GNUNET_PeerIdentity *id)
658 for ( i = 0 ; i < sampler->sampler_size ; i++ )
660 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
662 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
663 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
670 * Get one random peer out of the sampled peers.
672 * We might want to reinitialise this sampler after giving the
673 * corrsponding peer to the client.
674 * Only used internally
677 sampler_get_rand_peer2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
679 struct GetPeerCls *gpc = cls;
682 gpc->get_peer_task = NULL;
683 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, gpc);
684 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
688 * Choose the r_index of the peer we want to return
689 * at random from the interval of the gossip list
691 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
692 gpc->sampler->sampler_size);
694 if ( EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty )
696 gpc->get_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
697 GNUNET_TIME_UNIT_SECONDS,
699 &sampler_get_rand_peer2,
704 *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id;
706 gpc->cont (gpc->cont_cls, gpc->id);
712 * Get one random peer out of the sampled peers.
714 * We might want to reinitialise this sampler after giving the
715 * corrsponding peer to the client.
718 sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
720 struct GetPeerCls *gpc = cls;
721 struct GNUNET_PeerIdentity tmp_id;
722 unsigned int empty_flag;
723 struct RPS_SamplerElement *s_elem;
724 struct GNUNET_TIME_Relative last_request_diff;
725 uint32_t tmp_client_get_index;
727 gpc->get_peer_task = NULL;
728 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
731 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
734 /* Store the next #client_get_index to check whether we cycled over the whole list */
735 if (0 < client_get_index)
736 tmp_client_get_index = client_get_index - 1;
738 tmp_client_get_index = gpc->sampler->sampler_size - 1;
740 LOG (GNUNET_ERROR_TYPE_DEBUG,
741 "sched for later if index reaches %" PRIX32 " (sampler size: %" PRIX32 ").\n",
742 tmp_client_get_index, gpc->sampler->sampler_size);
745 { /* Get first non empty sampler */
746 if (tmp_client_get_index == client_get_index)
747 { /* We once cycled over the whole list */
748 LOG (GNUNET_ERROR_TYPE_DEBUG, "reached tmp_index %" PRIX32 ".\n",
750 GNUNET_assert (NULL == gpc->get_peer_task);
752 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
753 &sampler_get_rand_peer,
758 tmp_id = gpc->sampler->sampler_elements[client_get_index]->peer_id;
759 empty_flag = gpc->sampler->sampler_elements[client_get_index]->is_empty;
760 RPS_sampler_elem_reinit (gpc->sampler->sampler_elements[client_get_index]);
761 if (EMPTY != empty_flag)
762 RPS_sampler_elem_next (gpc->sampler->sampler_elements[client_get_index],
766 /* Cycle the #client_get_index one step further */
767 if ( client_get_index == gpc->sampler->sampler_size - 1 )
768 client_get_index = 0;
772 LOG (GNUNET_ERROR_TYPE_DEBUG, "incremented index to %" PRIX32 ".\n",
774 } while (EMPTY == gpc->sampler->sampler_elements[client_get_index]->is_empty);
776 s_elem = gpc->sampler->sampler_elements[client_get_index];
777 *gpc->id = s_elem->peer_id;
779 /* Check whether we may use this sampler to give it back to the client */
780 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
783 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
784 GNUNET_TIME_absolute_get ());
785 /* We're not going to give it back now if it was
786 * already requested by a client this round */
787 if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us)
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
791 ///* How many time remains untile the next round has started? */
792 //inv_last_request_diff =
793 // GNUNET_TIME_absolute_get_difference (last_request_diff,
794 // sampler->max_round_interval);
795 // add a little delay
796 /* Schedule it one round later */
797 GNUNET_assert (NULL == gpc->get_peer_task);
799 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
800 &sampler_get_rand_peer,
804 // TODO add other reasons to wait here
807 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
809 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, gpc);
810 gpc->cont (gpc->cont_cls, gpc->id);
816 * Get n random peers out of the sampled peers.
818 * We might want to reinitialise this sampler after giving the
819 * corrsponding peer to the client.
820 * Random with or without consumption?
822 * @param sampler the sampler to get peers from.
823 * @param cb callback that will be called once the ids are ready.
824 * @param cls closure given to @a cb
825 * @param for_client #GNUNET_YES if result is used for client,
826 * #GNUNET_NO if used internally
827 * @param num_peers the number of peers requested
830 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
831 RPS_sampler_n_rand_peers_ready_cb cb,
832 void *cls, uint32_t num_peers, int for_client)
834 GNUNET_assert (0 != sampler->sampler_size);
836 // TODO check if we have too much (distinct) sampled peers
838 struct NRandPeersReadyCls *cb_cls;
839 struct GetPeerCls *gpc;
841 cb_cls = GNUNET_new (struct NRandPeersReadyCls);
842 cb_cls->num_peers = num_peers;
843 cb_cls->cur_num_peers = 0;
844 cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
845 cb_cls->callback = cb;
849 if (GNUNET_NO == for_client)
851 to_file (sampler->file_name,
852 "This sampler is probably for Brahms itself\n");
856 LOG (GNUNET_ERROR_TYPE_DEBUG,
857 "Scheduling requests for %" PRIX32 " peers\n", num_peers);
859 for (i = 0 ; i < num_peers ; i++)
861 gpc = GNUNET_new (struct GetPeerCls);
862 gpc->sampler = sampler;
863 gpc->cont = check_n_peers_ready;
864 gpc->cont_cls = cb_cls;
865 gpc->id = &cb_cls->ids[i];
867 // maybe add a little delay
868 if (GNUNET_YES == for_client)
869 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (&sampler_get_rand_peer, gpc);
870 else if (GNUNET_NO == for_client)
871 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (&sampler_get_rand_peer2, gpc);
875 GNUNET_CONTAINER_DLL_insert (gpc_head, gpc_tail, gpc);
881 * Counts how many Samplers currently hold a given PeerID.
883 * @param sampler the sampler to count ids in.
884 * @param id the PeerID to count.
886 * @return the number of occurrences of id.
889 RPS_sampler_count_id (struct RPS_Sampler *sampler,
890 const struct GNUNET_PeerIdentity *id)
896 for ( i = 0 ; i < sampler->sampler_size ; i++ )
898 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
899 && EMPTY != sampler->sampler_elements[i]->is_empty)
907 * Cleans the sampler.
910 RPS_sampler_destroy (struct RPS_Sampler *sampler)
912 struct GetPeerCls *i;
914 for (i = gpc_head; NULL != i; i = gpc_head)
916 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, i);
917 GNUNET_SCHEDULER_cancel (i->get_peer_task);
921 sampler_empty (sampler);
922 GNUNET_free (sampler);
925 /* end of gnunet-service-rps.c */