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.c
23 * @brief rps service implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_nse_service.h"
35 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
37 // TODO modify @brief in every file
39 // TODO take care that messages are not longer than 64k
41 // TODO check for overflows
43 // TODO align message structs
45 // TODO multipeerlist indep of gossiped list
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
51 // (TODO api -- possibility of getting weak random peer immideately)
53 // TODO malicious peer
55 // TODO switch Slist -> DLL
60 static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 struct GNUNET_PeerIdentity *own_identity;
68 * Compare two peer identities. Taken from secretsharing.
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.
75 peer_id_cmp (const void *p1, const void *p2)
77 return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity));
80 /***********************************************************************
83 * WARNING: This section needs to be reviewed regarding the use of
84 * functions providing (pseudo)randomness!
85 ***********************************************************************/
88 // TODO grow/shrink list
91 * A sampler sampling PeerIDs.
96 * Min-wise linear permutation used by this sampler.
98 * This is an key later used by a hmac.
100 struct GNUNET_CRYPTO_AuthKey auth_key;
103 * The PeerID this sampler currently samples.
105 struct GNUNET_PeerIdentity *peer_id;
108 * The according hash value of this PeerID.
110 struct GNUNET_HashCode peer_id_hash;
114 typedef void (* SAMPLER_deleteCB) (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash);
117 * (Re)Initialise given Sampler with random min-wise independent function.
119 * In this implementation this means choosing an auth_key for later use in
127 s = GNUNET_new(struct Sampler);
129 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
130 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
132 GNUNET_CRYPTO_HASH_LENGTH);
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.
138 GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id,
139 sizeof(struct GNUNET_PeerIdentity),
146 * Compare two hashes.
148 * Returns if the first one is smaller then the second.
149 * Used by SAMPLER_next() to compare hashes.
152 hash_cmp(struct GNUNET_HashCode hash1, struct GNUNET_HashCode hash2)
154 return memcmp( (const void *) &hash1, (const void *) & hash2, sizeof(struct GNUNET_HashCode)) < 0;
158 * Input an PeerID into the given sampler.
161 SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *id, SAMPLER_deleteCB del_cb, void *cb_cls)
163 struct GNUNET_HashCode other_hash;
165 GNUNET_CRYPTO_hmac(&s->auth_key,
167 sizeof(struct GNUNET_PeerIdentity),
170 if ( NULL == s->peer_id ) { // Or whatever is a valid way to say
171 // "we have no PeerID at the moment"
173 s->peer_id_hash = other_hash;
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));
181 if ( NULL != del_cb ) {
182 del_cb(cb_cls, s->peer_id, s->peer_id_hash);
185 s->peer_id_hash = other_hash;
188 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Keeping old PeerID %s\n",
189 GNUNET_i2s(id), GNUNET_i2s(s->peer_id));
198 * A fuction to update every sampler in the given list
201 SAMPLER_update_list(struct GNUNET_CONTAINER_SList *lst, const struct GNUNET_PeerIdentity *id,
202 SAMPLER_deleteCB del_cb, void *cb_cls)
204 struct GNUNET_CONTAINER_SList_Iterator *iter;
205 struct Sampler *sampler;
208 iter = GNUNET_malloc(sizeof(struct GNUNET_CONTAINER_SList_Iterator));
209 *iter = GNUNET_CONTAINER_slist_begin(lst);
210 s = sizeof(struct Sampler);
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) );
216 GNUNET_CONTAINER_slist_iter_destroy(iter);
220 * Get one random peer out of the sampled peers.
222 * We might want to reinitialise this sampler after giving the
223 * corrsponding peer to the client.
225 struct GNUNET_PeerIdentity*
226 SAMPLER_get_rand_peer (struct GNUNET_CONTAINER_SList *lst)
230 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
232 list_size = (uint64_t) GNUNET_CONTAINER_slist_count(lst);
234 if ( 0 == list_size ) {
235 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
239 struct GNUNET_CONTAINER_SList_Iterator *iter;
242 struct GNUNET_PeerIdentity *peer;
245 * Choose the index of the peer we want to give back
246 * at random from the interval of the sampler list
248 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
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);
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);
262 // TODO something missing?
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));
274 * Get n random peers out of the sampled peers.
276 * We might want to reinitialise this sampler after giving the
277 * corrsponding peer to the client.
278 * Random with or without consumption?
280 struct GNUNET_PeerIdentity** // TODO give back simple array
281 SAMPLER_get_n_rand_peers (struct GNUNET_CONTAINER_SList *lst, uint64_t n)
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;
288 peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity *));
290 for ( i = 0 ; i < n ; i++ ) {
291 peers[i] = SAMPLER_get_rand_peer(lst);
294 // TODO something else missing?
299 * Counts how many Samplers currently hold a given PeerID.
302 SAMPLER_count_id ( struct GNUNET_CONTAINER_SList *lst, struct GNUNET_PeerIdentity *id ) {
304 struct GNUNET_CONTAINER_SList_Iterator *iter;
307 s = sizeof( struct Sampler );
308 iter = GNUNET_new(struct GNUNET_CONTAINER_SList_Iterator);
309 *iter = GNUNET_CONTAINER_slist_begin(lst);
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) ) {
316 GNUNET_CONTAINER_slist_iter_destroy(iter);
321 /***********************************************************************
323 ***********************************************************************/
327 /***********************************************************************
329 ***********************************************************************/
332 // * Get one random peer out of the gossiped peer list.
334 // struct GNUNET_PeerIdentity *
335 //get_random_peer(struct GNUNET_CONTAINER_MultiPeerMap * lst)
338 // struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
341 // struct GNUNET_PeerIdentity *peer;
343 // n = (size_t) GNUNET_CONTAINER_multipeermap_size(lst);
344 // index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
346 // iter = GNUNET_CONTAINER_multipeermap_iterator_create(lst);
348 // for ( i = 0 ; i < index ; i++ ) {
349 // GNUNET_CONTAINER_multipeermap_iterator_next(iter, NULL, NULL);
352 // peer = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
353 // GNUNET_CONTAINER_multipeermap_iterator_next(iter, peer, NULL);
359 /***********************************************************************
361 ***********************************************************************/
365 /***********************************************************************
366 * Housekeeping with peers
367 ***********************************************************************/
370 * Struct used to store the context of a connected client.
375 * The message queue to communicate with the client.
377 struct GNUNET_MQ_Handle *mq;
381 * Used to keep track in what lists single peerIDs are.
383 enum in_list_flag // probably unneeded
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?
392 * Struct used to keep track of other peer's status
394 * This is stored in a multipeermap.
399 * In own gossip/sampler list, in other's gossip/sampler list
401 uint32_t in_flags; // unneeded?
404 * Message queue open to client
406 struct GNUNET_MQ_Handle *mq;
409 * Channel open to client.
411 struct GNUNET_CADET_Channel *to_channel;
414 * Channel open from client.
416 struct GNUNET_CADET_Channel *from_channel; // unneeded
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),
425 /***********************************************************************
426 * /Housekeeping with peers
427 ***********************************************************************/
430 * Set of all peers to keep track of them.
432 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
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?
444 struct GNUNET_CONTAINER_SList *sampler_list;
449 * Adapts to the nse. Size should be in BigTheta(network_size)^(1/3).
451 size_t sampler_list_size;
455 * The gossiped list of peers.
457 struct GNUNET_PeerIdentity *gossip_list;
460 * Size of the gossiped list
462 unsigned int gossip_list_size;
465 * Min size of the gossip list
467 uint64_t gossip_list_min_size;
470 // * Max size of the gossip list
472 // * This will probably be left to be set by the client.
474 //uint64_t gossip_list_max_size;
478 * The estimated size of the network.
480 * Influenced by the stdev.
487 * Percentage of total peer number in the gossip list
488 * to send random PUSHes to
493 * Percentage of total peer number in the gossip list
494 * to send random PULLs to
499 * The percentage gamma of history updates.
500 * Simply 1 - alpha - beta
507 * Identifier for the main task that runs periodically.
509 GNUNET_SCHEDULER_TaskIdentifier do_round_task;
512 * Time inverval the do_round task runs in.
514 struct GNUNET_TIME_Relative round_interval;
519 * List to store peers received through pushes temporary.
521 struct GNUNET_CONTAINER_SList *push_list;
524 * List to store peers received through pulls temporary.
526 struct GNUNET_CONTAINER_SList *pull_list;
532 struct GNUNET_NSE_Handle *nse;
537 struct GNUNET_CADET_Handle *cadet_handle;
540 /***********************************************************************
542 ***********************************************************************/
545 * Get random peer from the gossip list.
547 struct GNUNET_PeerIdentity *
548 get_rand_gossip_peer()
551 struct GNUNET_PeerIdentity *peer;
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 ?
560 * Choose the index of the peer we want to return
561 * at random from the interval of the gossip list
563 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
566 peer = &(gossip_list[index]);
567 } while ( own_identity == peer || NULL == peer );
573 * Get the message queue of a specific peer.
575 * If we already have a message queue open to this client,
576 * simply return it, otherways create one.
578 struct GNUNET_MQ_Handle *
579 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, struct GNUNET_PeerIdentity *peer_id)
581 struct peer_context *ctx;
582 struct GNUNET_MQ_Handle * mq;
583 struct GNUNET_CADET_Channel *channel;
585 if ( GNUNET_OK != GNUNET_CONTAINER_multipeermap_contains( peer_map, peer_id ) ) {
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);
592 ctx = GNUNET_malloc(sizeof(struct peer_context));
594 ctx->to_channel = channel;
597 GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
598 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
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;
609 mq = GNUNET_CADET_mq_create(ctx->to_channel);
618 /***********************************************************************
620 ***********************************************************************/
623 * Function called by NSE.
625 * Updates sizes of sampler list and gossip list and adapt those lists
629 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
632 //double scale; // TODO this might go gloabal/config
634 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
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);
643 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
648 * Handle RPS request from the client.
651 * @param client identification of the client
652 * @param message the actual message
656 handle_cs_request (void *cls,
657 struct GNUNET_SERVER_Client *client,
658 const struct GNUNET_MessageHeader *message)
660 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
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;
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);
682 //mq = GNUNET_MQ_queue_for_server_client(client);
684 // TODO How many peers do we give back?
685 // Wait until we have enough random peers?
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);
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));
700 GNUNET_MQ_send(cli_ctx->mq, ev);
701 //GNUNET_MQ_send(mq, ev);
702 //GNUNET_MQ_destroy(mq);
704 GNUNET_SERVER_receive_done (client,
709 * Handle a PUSH message from another peer.
711 * Check the proof of work and store the PeerID
712 * in the temporary list for pushed PeerIDs.
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
720 handle_peer_push (void *cls,
721 struct GNUNET_CADET_Channel *channel,
723 const struct GNUNET_MessageHeader *msg)
725 LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
727 struct GNUNET_PeerIdentity *peer;
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
734 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
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));
745 * Handle PULL REQUEST request message from another peer.
747 * Reply with the gossip list of PeerIDs.
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
755 handle_peer_pull_request (void *cls,
756 struct GNUNET_CADET_Channel *channel,
758 const struct GNUNET_MessageHeader *msg)
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;
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?
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));
774 mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
775 //mq = get_mq(peer_map, peer);
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));
787 GNUNET_MQ_send(mq, ev);
789 GNUNET_MQ_destroy(mq);
796 * Handle PULL REPLY message from another peer.
798 * Check whether we sent a corresponding request and
799 * whether this reply is the first one.
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
807 handle_peer_pull_reply (void *cls,
808 struct GNUNET_CADET_Channel *channel,
810 const struct GNUNET_MessageHeader *msg)
812 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
814 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
817 // TODO check that we sent a request and that it is the first reply
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));
827 // TODO maybe a disconnect happens here
834 * Callback called when a Sampler is updated.
837 delete_cb (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
841 s = SAMPLER_count_id(sampler_list, id);
844 GNUNET_CONTAINER_multipeermap_remove_all( peer_map, id);
850 * Send out PUSHes and PULLs.
852 * This is executed regylary.
855 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
857 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
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;
868 // TODO print lists, ...
869 // TODO cleanup peer_map
871 iter = GNUNET_new(struct GNUNET_CONTAINER_SList_Iterator);
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++ ) {
882 GNUNET_CONTAINER_slist_add_end(sampler_list,
883 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, // DEPRECATED
885 sizeof(struct Sampler));
887 // TODO add peers to gossiped ones?
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
899 GNUNET_array_grow(gossip_list, gossip_list_size, est_size); // FIXME Do conversion correct or change type
901 gossip_list_size = sampler_list_size = est_size;
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? */
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));
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
923 push_msg->placeholder = 0;
924 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
926 // TODO modify in_flags of respective peer?
930 /* Send PULL requests */
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));
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?
950 /* Update gossip list */
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");
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
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
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
979 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
981 // TODO independent of that also get some peers from CADET_get_peers()?
985 /* Update samplers */
988 if ( 0 < GNUNET_CONTAINER_slist_count(push_list) ) {
989 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the sampler list from pushes.\n");
991 *iter = GNUNET_CONTAINER_slist_begin(push_list);
992 size = sizeof(struct GNUNET_PeerIdentity);
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);
999 GNUNET_CONTAINER_slist_iter_destroy(iter);
1002 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the sampler list - received no pushes.\n");
1005 if ( 0 < GNUNET_CONTAINER_slist_count(pull_list) ) {
1006 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the sampler list - received no pushes.\n");
1008 *iter = GNUNET_CONTAINER_slist_begin(pull_list);
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);
1015 GNUNET_CONTAINER_slist_iter_destroy(iter);
1017 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the sampler list - received no pulls.\n");
1024 // TODO go over whole peer_map and do cleanups
1025 // delete unneeded peers, set in_flags, check channel/mq
1029 /* Empty push/pull lists */
1030 if ( 0 != GNUNET_CONTAINER_slist_count(push_list) ) {
1031 GNUNET_CONTAINER_slist_clear(push_list);
1034 if ( 0 != GNUNET_CONTAINER_slist_count(push_list) ) {
1035 GNUNET_CONTAINER_slist_clear(push_list);
1039 /* Schedule next round */
1041 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1042 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1046 rps_start (struct GNUNET_SERVER_Handle *server);
1049 * This is called from GNUNET_CADET_get_peers().
1051 * It is called on every peer(ID) that cadet somehow has contact with.
1052 * We use those to initialise the sampler.
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)"
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 ) ) {
1068 struct peer_context *ctx;
1070 ctx = GNUNET_malloc(sizeof(struct peer_context));
1073 ctx->to_channel = NULL;
1074 ctx->from_channel = NULL;
1075 GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
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?
1083 rps_start( (struct GNUNET_SERVER_Handle *) cls);
1091 * Task run during shutdown.
1097 shutdown_task (void *cls,
1098 const struct GNUNET_SCHEDULER_TaskContext *tc)
1100 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1102 GNUNET_NSE_disconnect(nse);
1103 GNUNET_CADET_disconnect(cadet_handle);
1104 // TODO delete global data
1109 * A client disconnected. Remove all of its data structure entries.
1111 * @param cls closure, NULL
1112 * @param client identification of the client
1115 handle_client_disconnect (void *cls,
1116 struct GNUNET_SERVER_Client * client)
1118 // TODO reinitialise that sampler
1122 * Handle the channel a peer opens to us.
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
1131 handle_inbound_channel (void *cls,
1132 struct GNUNET_CADET_Channel *channel,
1133 const struct GNUNET_PeerIdentity *initiator,
1135 enum GNUNET_CADET_ChannelOption options)
1137 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1139 GNUNET_assert( NULL != channel );
1141 // TODO we might even not store the from_channel
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
1147 struct peer_context *ctx;
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;
1154 GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1156 return NULL; // TODO
1160 * This is called when a remote peer destroys a channel.
1162 * @param cls The closure
1163 * @param channel The channel being closed
1164 * @param channel_ctx The context associated with this channel
1167 cleanup_channel(void *cls,
1168 const struct GNUNET_CADET_Channel *channel,
1171 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1175 * Actually start the service.
1178 rps_start (struct GNUNET_SERVER_Handle *server)
1180 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1181 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1185 GNUNET_SERVER_add_handlers (server, handlers);
1186 GNUNET_SERVER_disconnect_notify (server,
1187 &handle_client_disconnect,
1189 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1193 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1194 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1196 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1204 * Process statistics requests.
1206 * @param cls closure
1207 * @param server the initialized server
1208 * @param c configuration to use
1212 struct GNUNET_SERVER_Handle *server,
1213 const struct GNUNET_CONFIGURATION_Handle *c)
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);
1219 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1226 own_identity = GNUNET_new(struct GNUNET_PeerIdentity);
1228 GNUNET_CRYPTO_get_peer_identity(cfg, own_identity);
1232 /* Get time interval from the configuration */
1233 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1237 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1238 GNUNET_SCHEDULER_shutdown();
1242 /* Get initial size of sampler/gossip list from the configuration */
1243 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1245 (long long unsigned int *) &est_size)) // FIXME convert
1247 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1248 GNUNET_SCHEDULER_shutdown();
1251 LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1253 gossip_list_size = sampler_list_size = est_size; // TODO rename est_size
1258 static unsigned int tmp = 0;
1260 GNUNET_array_grow(gossip_list, tmp, gossip_list_size);
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");
1273 // TODO initialise thresholds - ?
1275 ///* Get alpha from the configuration */
1276 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1280 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1282 //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1284 ///* Get beta from the configuration */
1285 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1289 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1291 //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1296 peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1299 /* Initialise sampler and gossip list */
1302 sampler_list = GNUNET_CONTAINER_slist_create();
1304 //if ( gossip_list_size == sampler_list_size ) {
1305 for ( i = 0 ; i < sampler_list_size ; i++ ) {
1306 /* Init sampler list */
1308 GNUNET_CONTAINER_slist_add(sampler_list,
1309 GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC, // TODO DEPRECATED
1311 sizeof(struct Sampler));
1312 /* Init gossip list */
1313 // TODO init gossip list
1314 // What do we need to do here?
1317 // for ( i = 0 ; i < gossip_list_size ; i++ ) {
1318 // // TODO init gossip list
1320 // for ( i = 0 ; i < sampler_list_size ; i++ ) {
1321 // // TODO init RPF func
1322 // // TODO init Sample list
1323 // // TODO init Sampled list
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);
1331 push_list = GNUNET_CONTAINER_slist_create();
1332 pull_list = GNUNET_CONTAINER_slist_create();
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},
1343 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1344 cadet_handle = GNUNET_CADET_connect(cfg,
1346 &handle_inbound_channel,
1350 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
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
1357 // TODO send push/pull to each of those peers?
1365 * The main function for the rps service.
1367 * @param argc number of arguments from the command line
1368 * @param argv command line arguments
1369 * @return 0 ok, 1 on error
1372 main (int argc, char *const *argv)
1374 return (GNUNET_OK ==
1375 GNUNET_SERVICE_run (argc,
1378 GNUNET_SERVICE_OPTION_NONE,
1379 &run, NULL)) ? 0 : 1;
1382 /* end of gnunet-service-rps.c */