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 api -- possibility of getting weak random peer immideately)
49 // TODO malicious peer
51 // TODO switch Slist -> DLL
56 static const struct GNUNET_CONFIGURATION_Handle *cfg;
61 struct GNUNET_PeerIdentity *own_identity;
64 * Compare two peer identities. Taken from secretsharing.
66 * @param p1 Some peer identity.
67 * @param p2 Some peer identity.
68 * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2.
71 peer_id_cmp (const void *p1, const void *p2)
73 return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity));
76 /***********************************************************************
79 * WARNING: This section needs to be reviewed regarding the use of
80 * functions providing (pseudo)randomness!
81 ***********************************************************************/
83 // TODO care about invalid input of the caller (size 0 or less...)
86 * A sampler sampling PeerIDs.
91 * Min-wise linear permutation used by this sampler.
93 * This is an key later used by a hmac.
95 struct GNUNET_CRYPTO_AuthKey auth_key;
98 * The PeerID this sampler currently samples.
100 struct GNUNET_PeerIdentity *peer_id;
103 * The according hash value of this PeerID.
105 struct GNUNET_HashCode peer_id_hash;
108 * Samplers are kept in a linked list.
110 struct Sampler *next;
113 * Samplers are kept in a linked list.
115 struct Sampler *prev;
120 * A n-tuple of samplers.
125 * Number of samplers we hold.
131 * All PeerIDs in one array.
133 struct GNUNET_PeerIdentity *peer_ids;
136 * The head of the DLL.
138 struct Sampler *head;
141 * The tail of the DLL.
143 struct Sampler *tail;
148 typedef void (* SAMPLER_deleteCB) (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash);
151 * (Re)Initialise given Sampler with random min-wise independent function.
153 * In this implementation this means choosing an auth_key for later use in
156 * @param id pointer to the place where this sampler will store the PeerID.
157 * This will be overwritten.
160 SAMPLER_init(struct GNUNET_PeerIdentity *id)
164 s = GNUNET_new(struct Sampler);
166 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
167 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
169 GNUNET_CRYPTO_HASH_LENGTH);
171 //s->peer_id = GNUNET_new( struct GNUNET_PeerIdentity );
172 GNUNET_assert(NULL != id);
174 memcpy(s->peer_id, own_identity, sizeof(struct GNUNET_PeerIdentity));
175 //s->peer_id = own_identity; // Maybe set to own PeerID. So we always have
176 // a valid PeerID in the sampler.
177 // Maybe take a PeerID as second argument.
178 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: initialised with PeerID %s (at %p) \n", GNUNET_i2s(s->peer_id), s->peer_id);
180 GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id,
181 sizeof(struct GNUNET_PeerIdentity),
191 * Compare two hashes.
193 * Returns if the first one is smaller then the second.
194 * Used by SAMPLER_next() to compare hashes.
197 hash_cmp(struct GNUNET_HashCode hash1, struct GNUNET_HashCode hash2)
199 return memcmp( (const void *) &hash1, (const void *) & hash2, sizeof(struct GNUNET_HashCode));
203 * Input an PeerID into the given sampler.
206 SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *id, SAMPLER_deleteCB del_cb, void *cb_cls)
207 // TODO set id in peer_ids
209 struct GNUNET_HashCode other_hash;
211 if ( id == s->peer_id )
213 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
215 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s\n",
216 GNUNET_i2s(s->peer_id));
220 GNUNET_CRYPTO_hmac(&s->auth_key,
222 sizeof(struct GNUNET_PeerIdentity),
225 if ( NULL == s->peer_id )
226 { // Or whatever is a valid way to say
227 // "we have no PeerID at the moment"
228 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Simply accepting (got NULL previously).\n",
230 memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
232 s->peer_id_hash = other_hash;
234 else if ( 0 > hash_cmp(other_hash, s->peer_id_hash) )
236 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
238 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s\n",
239 GNUNET_i2s(s->peer_id));
241 if ( NULL != del_cb )
243 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s with the delete callback.\n",
244 GNUNET_i2s(s->peer_id));
245 del_cb(cb_cls, s->peer_id, s->peer_id_hash);
248 memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
250 s->peer_id_hash = other_hash;
254 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
256 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s\n",
257 GNUNET_i2s(s->peer_id), s->peer_id);
264 * Initialise a tuple of samplers.
267 SAMPLER_samplers_init(size_t init_size)
269 struct Samplers *samplers;
273 samplers = GNUNET_new(struct Samplers);
275 samplers->head = samplers->tail = NULL;
276 samplers->peer_ids = NULL;
277 //samplers->peer_ids = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
279 for ( i = 0 ; i < init_size ; i++ )
281 GNUNET_array_append(samplers->peer_ids,
285 s = SAMPLER_init(&samplers->peer_ids[i]);
286 GNUNET_CONTAINER_DLL_insert_tail(samplers->head,
290 //samplers->size = init_size;
291 GNUNET_assert(init_size == samplers->size);
297 * A fuction to update every sampler in the given list
300 SAMPLER_update_list(struct Samplers *samplers, const struct GNUNET_PeerIdentity *id,
301 SAMPLER_deleteCB del_cb, void *cb_cls)
303 struct Sampler *iter;
305 iter = samplers->head;
306 while ( NULL != iter->next )
308 SAMPLER_next(iter, id, del_cb, cb_cls);
315 * Get one random peer out of the sampled peers.
317 * We might want to reinitialise this sampler after giving the
318 * corrsponding peer to the client.
320 const struct GNUNET_PeerIdentity*
321 SAMPLER_get_rand_peer (struct Samplers *samplers)
323 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
325 if ( 0 == samplers->size )
327 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
333 struct Sampler *iter;
335 const struct GNUNET_PeerIdentity *peer;
338 * Choose the index of the peer we want to give back
339 * at random from the interval of the sampler list
341 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
343 // TODO check that it does not overflow
344 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Length of Slist: %" PRIu64 ", index: %" PRIu64 "\n", samplers->size, index);
346 iter = samplers->head;
347 for ( i = 0 ; i < index ; i++ )
349 if ( NULL == iter->next )
351 iter = samplers->head;
355 // TODO something missing?
357 peer = iter->peer_id;
358 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s\n", GNUNET_i2s(peer));
359 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: (own ID: %s)\n", GNUNET_i2s(own_identity));
366 * Get n random peers out of the sampled peers.
368 * We might want to reinitialise this sampler after giving the
369 * corrsponding peer to the client.
370 * Random with or without consumption?
372 const struct GNUNET_PeerIdentity* // TODO give back simple array
373 SAMPLER_get_n_rand_peers (struct Samplers *samplers, uint64_t n)
375 // TODO check if we have too much (distinct) sampled peers
376 // If we are not ready yet maybe schedule for later
377 struct GNUNET_PeerIdentity *peers;
380 peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity));
382 for ( i = 0 ; i < n ; i++ ) {
383 //peers[i] = SAMPLER_get_rand_peer(samplers);
384 memcpy(&peers[i], SAMPLER_get_rand_peer(samplers), sizeof(struct GNUNET_PeerIdentity));
387 // TODO something else missing?
392 * Counts how many Samplers currently hold a given PeerID.
395 SAMPLER_count_id ( struct Samplers *samplers, struct GNUNET_PeerIdentity *id )
397 struct Sampler *iter;
400 iter = samplers->head;
402 while ( NULL != iter )
404 if ( peer_id_cmp( iter->peer_id, id) )
412 * Gow the size of the tuple of samplers.
415 SAMPLER_samplers_grow (struct Samplers * samplers, size_t new_size)
418 struct Sampler *sampler;
420 if ( new_size > samplers->size )
422 GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
423 for ( i = 0 ; i < new_size - samplers->size ; i++ )
425 sampler = SAMPLER_init(&samplers->peer_ids[samplers->size + i]);
426 GNUNET_CONTAINER_DLL_insert_tail(samplers->head, samplers->tail, sampler);
429 else if ( new_size < samplers->size )
431 for ( i = 0 ; i < samplers->size - new_size ; i++)
433 // TODO call delCB on elem?
434 sampler = samplers->tail;
435 GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, sampler);
437 GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
440 samplers->size = new_size;
443 /***********************************************************************
445 ***********************************************************************/
449 /***********************************************************************
450 * Housekeeping with peers
451 ***********************************************************************/
454 * Struct used to store the context of a connected client.
459 * The message queue to communicate with the client.
461 struct GNUNET_MQ_Handle *mq;
465 * Used to keep track in what lists single peerIDs are.
467 enum in_list_flag // probably unneeded
469 in_other_sampler_list = 0x1,
470 in_other_gossip_list = 0x2, // unneeded?
471 in_own_sampler_list = 0x4,
472 in_own_gossip_list = 0x8 // unneeded?
476 * Struct used to keep track of other peer's status
478 * This is stored in a multipeermap.
483 * In own gossip/sampler list, in other's gossip/sampler list
485 uint32_t in_flags; // unneeded?
488 * Message queue open to client
490 struct GNUNET_MQ_Handle *mq;
493 * Channel open to client.
495 struct GNUNET_CADET_Channel *to_channel;
498 * Channel open from client.
500 struct GNUNET_CADET_Channel *from_channel; // unneeded
503 * This is pobably followed by 'statistical' data (when we first saw
504 * him, how did we get his ID, how many pushes (in a timeinterval),
509 /***********************************************************************
510 * /Housekeeping with peers
511 ***********************************************************************/
514 * Set of all peers to keep track of them.
516 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
519 // -- gossip list length --
520 // Depends on the (estimated) size of the
521 // network. - Initial size might be the
522 // number of peers cadet provides.
523 // TODO other events to grow/shrink size?
528 struct Samplers *sampler_list; // TODO rename to sampler_list
531 * Sampler list size // TODO get rid of that
533 * Adapts to the nse. Size should be in BigTheta(network_size)^(1/3).
535 size_t sampler_list_size;
539 * The gossiped list of peers.
541 struct GNUNET_PeerIdentity *gossip_list;
544 * Size of the gossiped list
546 unsigned int gossip_list_size;
549 * Min size of the gossip list
551 uint64_t gossip_list_min_size;
554 // * Max size of the gossip list
556 // * This will probably be left to be set by the client.
558 //uint64_t gossip_list_max_size;
562 * The estimated size of the network.
564 * Influenced by the stdev.
571 * Percentage of total peer number in the gossip list
572 * to send random PUSHes to
577 * Percentage of total peer number in the gossip list
578 * to send random PULLs to
583 * The percentage gamma of history updates.
584 * Simply 1 - alpha - beta
591 * Identifier for the main task that runs periodically.
593 GNUNET_SCHEDULER_TaskIdentifier do_round_task;
596 * Time inverval the do_round task runs in.
598 struct GNUNET_TIME_Relative round_interval;
603 * List to store peers received through pushes temporary.
605 struct GNUNET_PeerIdentity *push_list;
608 * Size of the push_list;
610 unsigned int push_list_size;
611 //size_t push_list_size;
614 * List to store peers received through pulls temporary.
616 struct GNUNET_PeerIdentity *pull_list;
619 * Size of the pull_list;
621 unsigned int pull_list_size;
622 //size_t pull_list_size;
628 struct GNUNET_NSE_Handle *nse;
633 struct GNUNET_CADET_Handle *cadet_handle;
636 /***********************************************************************
638 ***********************************************************************/
641 * Get random peer from the gossip list.
643 struct GNUNET_PeerIdentity *
644 get_rand_gossip_peer()
647 struct GNUNET_PeerIdentity *peer;
649 // TODO find a better solution.
650 // FIXME if we have only own ID in gossip list this will block
651 // but then we might have a problem nevertheless ?
656 * Choose the index of the peer we want to return
657 * at random from the interval of the gossip list
659 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
662 peer = &(gossip_list[index]);
663 } while ( own_identity == peer || NULL == peer );
669 * Get the message queue of a specific peer.
671 * If we already have a message queue open to this client,
672 * simply return it, otherways create one.
674 struct GNUNET_MQ_Handle *
675 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, struct GNUNET_PeerIdentity *peer_id)
677 struct peer_context *ctx;
678 struct GNUNET_MQ_Handle * mq;
679 struct GNUNET_CADET_Channel *channel;
681 if ( GNUNET_OK != GNUNET_CONTAINER_multipeermap_contains( peer_map, peer_id ) ) {
683 channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
684 GNUNET_RPS_CADET_PORT,
685 GNUNET_CADET_OPTION_RELIABLE);
686 mq = GNUNET_CADET_mq_create(channel);
688 ctx = GNUNET_malloc(sizeof(struct peer_context));
690 ctx->to_channel = channel;
693 GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
694 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
696 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer_id);
697 if ( NULL == ctx->mq ) {
698 if ( NULL == ctx->to_channel ) {
699 channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
700 GNUNET_RPS_CADET_PORT,
701 GNUNET_CADET_OPTION_RELIABLE);
702 ctx->to_channel = channel;
705 mq = GNUNET_CADET_mq_create(ctx->to_channel);
714 /***********************************************************************
716 ***********************************************************************/
719 * Function called by NSE.
721 * Updates sizes of sampler list and gossip list and adapt those lists
725 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
728 //double scale; // TODO this might go gloabal/config
730 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
732 estimate = 1 << (uint64_t) round(logestimate);
733 // GNUNET_NSE_log_estimate_to_n (logestimate);
734 estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add
735 if ( 0 < estimate ) {
736 LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
739 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
744 * Handle RPS request from the client.
747 * @param client identification of the client
748 * @param message the actual message
752 handle_cs_request (void *cls,
753 struct GNUNET_SERVER_Client *client,
754 const struct GNUNET_MessageHeader *message)
756 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
758 struct GNUNET_RPS_CS_RequestMessage *msg;
759 //unsigned int n_arr[sampler_list_size];// =
760 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
761 //struct GNUNET_MQ_Handle *mq;
762 struct client_ctx *cli_ctx;
763 struct GNUNET_MQ_Envelope *ev;
764 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
769 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
770 // Does not work because the compiler seems not to find it.
771 cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx);
772 if ( NULL == cli_ctx ) {
773 cli_ctx = GNUNET_new(struct client_ctx);
774 cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client);
775 GNUNET_SERVER_client_set_user_context(client, cli_ctx);
778 //mq = GNUNET_MQ_queue_for_server_client(client);
780 // TODO How many peers do we give back?
781 // Wait until we have enough random peers?
783 ev = GNUNET_MQ_msg_extra(out_msg,
784 GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity),
785 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
786 out_msg->num_peers = GNUNET_ntohll(msg->num_peers);
788 num_peers = GNUNET_ntohll(msg->num_peers);
789 //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers);
790 for ( i = 0 ; i < num_peers ; i++ ) {
791 memcpy(&out_msg[1] + i * sizeof(struct GNUNET_PeerIdentity),
792 SAMPLER_get_rand_peer(sampler_list),
793 sizeof(struct GNUNET_PeerIdentity));
796 GNUNET_MQ_send(cli_ctx->mq, ev);
797 //GNUNET_MQ_send(mq, ev);
798 //GNUNET_MQ_destroy(mq);
800 GNUNET_SERVER_receive_done (client,
805 * Handle a PUSH message from another peer.
807 * Check the proof of work and store the PeerID
808 * in the temporary list for pushed PeerIDs.
811 * @param channel The channel the PUSH was received over
812 * @param channel_ctx The context associated with this channel
813 * @param msg The message header
816 handle_peer_push (void *cls,
817 struct GNUNET_CADET_Channel *channel,
819 const struct GNUNET_MessageHeader *msg)
821 LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
823 struct GNUNET_PeerIdentity *peer;
825 // TODO check the proof of work
826 // and check limit for PUSHes
827 // IF we count per peer PUSHes
828 // maybe remove from gossip/sampler list
830 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
832 /* Add the sending peer to the push_list */
833 GNUNET_array_append(push_list, push_list_size, *peer);
840 * Handle PULL REQUEST request message from another peer.
842 * Reply with the gossip list of PeerIDs.
845 * @param channel The channel the PUSH was received over
846 * @param channel_ctx The context associated with this channel
847 * @param msg The message header
850 handle_peer_pull_request (void *cls,
851 struct GNUNET_CADET_Channel *channel,
853 const struct GNUNET_MessageHeader *msg)
856 struct GNUNET_PeerIdentity *peer;
857 struct GNUNET_MQ_Handle *mq;
858 //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg;
859 struct GNUNET_MQ_Envelope *ev;
860 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
862 // TODO find some way to keep one peer from spamming with pull requests
863 // allow only one request per time interval ?
864 // otherwise remove from peerlist?
866 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER);
867 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer));
869 mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
870 //mq = get_mq(peer_map, peer);
872 //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg;
873 // TODO how many peers do we actually send?
874 // GNUNET_ntohll(in_msg->num_peers)
875 ev = GNUNET_MQ_msg_extra(out_msg,
876 gossip_list_size * sizeof(struct GNUNET_PeerIdentity),
877 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
878 out_msg->num_peers = GNUNET_htonll(gossip_list_size);
879 memcpy(&out_msg[1], gossip_list,
880 gossip_list_size * sizeof(struct GNUNET_PeerIdentity));
882 GNUNET_MQ_send(mq, ev);
884 GNUNET_MQ_destroy(mq);
891 * Handle PULL REPLY message from another peer.
893 * Check whether we sent a corresponding request and
894 * whether this reply is the first one.
897 * @param channel The channel the PUSH was received over
898 * @param channel_ctx The context associated with this channel
899 * @param msg The message header
902 handle_peer_pull_reply (void *cls,
903 struct GNUNET_CADET_Channel *channel,
905 const struct GNUNET_MessageHeader *msg)
907 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
909 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
910 struct GNUNET_PeerIdentity *peers;
913 // TODO check that we sent a request and that it is the first reply
915 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
916 peers = (struct GNUNET_PeerIdentity *) &msg[1];
917 for ( i = 0 ; i < GNUNET_ntohll(in_msg->num_peers) ; i++ ) {
918 GNUNET_array_append(pull_list, pull_list_size, peers[i]);
922 // TODO maybe a disconnect happens here
929 * Callback called when a Sampler is updated.
932 delete_cb (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
936 s = SAMPLER_count_id(sampler_list, id);
939 GNUNET_CONTAINER_multipeermap_remove_all( peer_map, id);
945 * Send out PUSHes and PULLs.
947 * This is executed regylary.
950 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
952 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
955 //unsigned int *n_arr;
956 struct GNUNET_RPS_P2P_PushMessage *push_msg;
957 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
958 struct GNUNET_MQ_Envelope *ev;
959 struct GNUNET_PeerIdentity *peer;
961 // TODO print lists, ...
962 // TODO cleanup peer_map
965 /* If the NSE has changed adapt the lists accordingly */
966 // TODO check nse == 0!
967 LOG(GNUNET_ERROR_TYPE_DEBUG, "Checking size estimate.\n");
968 SAMPLER_samplers_grow(sampler_list, est_size);
970 GNUNET_array_grow(gossip_list, gossip_list_size, est_size); // FIXME Do conversion correct or change type
972 gossip_list_size = sampler_list_size = est_size;
977 /* Would it make sense to have one shuffeled gossip list and then
978 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
979 * use the rest to update sampler? */
982 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
983 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %f (%f * %" PRIu64 ") peers.\n",
984 alpha * gossip_list_size, alpha, gossip_list_size);
985 for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO compute length
986 peer = get_rand_gossip_peer();
987 // TODO check NULL == peer
988 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s(peer));
990 ev = GNUNET_MQ_msg(push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
991 //ev = GNUNET_MQ_msg_extra();
992 /* TODO Compute proof of work here
994 push_msg->placeholder = 0;
995 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
997 // TODO modify in_flags of respective peer?
1001 /* Send PULL requests */
1003 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
1004 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %f (%f * %" PRIu64 ") peers.\n",
1005 beta * gossip_list_size, beta, gossip_list_size);
1006 for ( i = 0 ; i < beta * gossip_list_size ; i++ ){ // TODO compute length
1007 peer = get_rand_gossip_peer();
1008 // TODO check NULL == peer
1009 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s(peer));
1011 ev = GNUNET_MQ_msg(pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1012 //ev = GNUNET_MQ_msg_extra();
1013 pull_msg->placeholder = 0;
1014 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
1015 // TODO modify in_flags of respective peer?
1021 /* Update gossip list */
1025 if ( push_list_size <= alpha * gossip_list_size &&
1026 push_list_size != 0 &&
1027 pull_list_size != 0 ) {
1028 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
1030 for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO use SAMPLER_get_n_rand_peers
1031 /* Update gossip list with peers received through PUSHes */
1032 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1034 gossip_list[i] = push_list[index];
1035 // TODO change the in_flags accordingly
1038 for ( i = 0 ; i < beta * gossip_list_size ; i++ ) {
1039 /* Update gossip list with peers received through PULLs */
1040 tmp_index = i + round(alpha * gossip_list_size);
1041 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1043 gossip_list[tmp_index] = pull_list[index];
1044 // TODO change the in_flags accordingly
1047 for ( i = 0 ; i < (1 - (alpha + beta)) * gossip_list_size ; i++ ) {
1048 /* Update gossip list with peers from history */
1049 tmp_index = i + round((alpha + beta) * gossip_list_size);
1050 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1051 sampler_list->size);
1052 gossip_list[tmp_index] = sampler_list->peer_ids[index];
1053 // TODO change the in_flags accordingly
1057 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1059 // TODO independent of that also get some peers from CADET_get_peers()?
1063 /* Update samplers */
1065 for ( i = 0 ; i < push_list_size ; i++ )
1067 SAMPLER_update_list(sampler_list, &push_list[i], NULL, NULL);
1068 // TODO set in_flag?
1071 for ( i = 0 ; i < pull_list_size ; i++ )
1073 SAMPLER_update_list(sampler_list, &pull_list[i], NULL, NULL);
1074 // TODO set in_flag?
1078 // TODO go over whole peer_map and do cleanups
1079 // delete unneeded peers, set in_flags, check channel/mq
1082 /* Empty push/pull lists */
1083 GNUNET_array_grow(push_list, push_list_size, 0);
1085 GNUNET_array_grow(pull_list, pull_list_size, 0);
1089 /* Schedule next round */
1091 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1092 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1096 rps_start (struct GNUNET_SERVER_Handle *server);
1099 * This is called from GNUNET_CADET_get_peers().
1101 * It is called on every peer(ID) that cadet somehow has contact with.
1102 * We use those to initialise the sampler.
1105 init_peer_cb (void *cls,
1106 const struct GNUNET_PeerIdentity *peer,
1107 int tunnel, // "Do we have a tunnel towards this peer?"
1108 unsigned int n_paths, // "Number of known paths towards this peer"
1109 unsigned int best_path) // "How long is the best path?
1110 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1112 // FIXME use the magic 0000 PeerID
1113 if ( NULL != peer ) {
1114 LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s (at %p) from CADET\n", GNUNET_i2s(peer), peer);
1115 SAMPLER_update_list(sampler_list, peer, NULL, NULL);
1116 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) ) {
1118 struct peer_context *ctx;
1120 ctx = GNUNET_malloc(sizeof(struct peer_context));
1123 ctx->to_channel = NULL;
1124 ctx->from_channel = NULL;
1125 GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1129 i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size);
1130 gossip_list[i] = *peer;
1131 // TODO send push/pull to each of those peers?
1133 rps_start( (struct GNUNET_SERVER_Handle *) cls);
1141 * Task run during shutdown.
1147 shutdown_task (void *cls,
1148 const struct GNUNET_SCHEDULER_TaskContext *tc)
1150 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1152 if ( GNUNET_SCHEDULER_NO_TASK != do_round_task )
1154 GNUNET_SCHEDULER_cancel (do_round_task);
1155 do_round_task = GNUNET_SCHEDULER_NO_TASK;
1158 GNUNET_NSE_disconnect(nse);
1159 GNUNET_CADET_disconnect(cadet_handle);
1160 GNUNET_free(own_identity);
1161 //GNUNET_free(round_interval);
1162 //GNUNET_free(est_size);
1163 //GNUNET_free(gossip_list_size);
1164 //GNUNET_free(sampler_list_size);
1165 GNUNET_free(gossip_list);
1166 // TODO for i in sampler_list free sampler
1167 // TODO destroy sampler_list
1168 // TODO destroy push/pull_list
1169 // TODO delete global data
1174 * A client disconnected. Remove all of its data structure entries.
1176 * @param cls closure, NULL
1177 * @param client identification of the client
1180 handle_client_disconnect (void *cls,
1181 struct GNUNET_SERVER_Client * client)
1183 // TODO reinitialise that sampler
1187 * Handle the channel a peer opens to us.
1189 * @param cls The closure
1190 * @param channel The channel the peer wants to establish
1191 * @param initiator The peer's peer ID
1192 * @param port The port the channel is being established over
1193 * @param options Further options
1196 handle_inbound_channel (void *cls,
1197 struct GNUNET_CADET_Channel *channel,
1198 const struct GNUNET_PeerIdentity *initiator,
1200 enum GNUNET_CADET_ChannelOption options)
1202 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1204 GNUNET_assert( NULL != channel );
1206 // TODO we might even not store the from_channel
1208 if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) {
1209 ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel;
1210 // FIXME there might already be an established channel
1212 struct peer_context *ctx;
1214 ctx = GNUNET_malloc( sizeof(struct peer_context));
1215 ctx->in_flags = in_other_gossip_list;
1216 ctx->mq = NULL; // TODO create mq?
1217 ctx->from_channel = channel;
1219 GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1221 return NULL; // TODO
1225 * This is called when a remote peer destroys a channel.
1227 * @param cls The closure
1228 * @param channel The channel being closed
1229 * @param channel_ctx The context associated with this channel
1232 cleanup_channel(void *cls,
1233 const struct GNUNET_CADET_Channel *channel,
1236 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1240 * Actually start the service.
1243 rps_start (struct GNUNET_SERVER_Handle *server)
1245 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1246 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1250 GNUNET_SERVER_add_handlers (server, handlers);
1251 GNUNET_SERVER_disconnect_notify (server,
1252 &handle_client_disconnect,
1254 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1258 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1259 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1261 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1269 * Process statistics requests.
1271 * @param cls closure
1272 * @param server the initialized server
1273 * @param c configuration to use
1277 struct GNUNET_SERVER_Handle *server,
1278 const struct GNUNET_CONFIGURATION_Handle *c)
1280 // TODO check what this does -- copied from gnunet-boss
1281 // - seems to work as expected
1282 GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL);
1284 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1289 own_identity = GNUNET_new(struct GNUNET_PeerIdentity);
1291 GNUNET_CRYPTO_get_peer_identity(cfg, own_identity); // TODO check return value
1293 LOG(GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1297 /* Get time interval from the configuration */
1298 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1302 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1303 GNUNET_SCHEDULER_shutdown();
1307 /* Get initial size of sampler/gossip list from the configuration */
1308 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1310 (long long unsigned int *) &est_size))
1312 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1313 GNUNET_SCHEDULER_shutdown();
1316 LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1318 gossip_list_size = sampler_list_size = est_size; // TODO rename est_size
1323 static unsigned int tmp = 0;
1325 GNUNET_array_grow(gossip_list, tmp, gossip_list_size);
1329 /* connect to NSE */
1330 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1331 // TODO check whether that was successful
1332 // TODO disconnect on shutdown
1333 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1338 // TODO initialise thresholds - ?
1340 ///* Get alpha from the configuration */
1341 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1345 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1347 //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1349 ///* Get beta from the configuration */
1350 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1354 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1356 //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1361 peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1364 /* Initialise sampler and gossip list */
1366 sampler_list = SAMPLER_samplers_init(est_size);
1373 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1374 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1375 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1376 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1380 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1381 cadet_handle = GNUNET_CADET_connect(cfg,
1383 &handle_inbound_channel,
1387 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1390 LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1391 GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server);
1392 // FIXME use magic 0000 PeerID to _start_ the service
1394 // TODO send push/pull to each of those peers?
1399 * The main function for the rps service.
1401 * @param argc number of arguments from the command line
1402 * @param argv command line arguments
1403 * @return 0 ok, 1 on error
1406 main (int argc, char *const *argv)
1408 return (GNUNET_OK ==
1409 GNUNET_SERVICE_run (argc,
1412 GNUNET_SERVICE_OPTION_NONE,
1413 &run, NULL)) ? 0 : 1;
1416 /* end of gnunet-service-rps.c */