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.
130 * All PeerIDs in one array.
132 struct GNUNET_PeerIdentity peer_ids[];
135 * The head of the DLL.
137 struct Sampler *head;
140 * The tail of the DLL.
142 struct Sampler *tail;
147 typedef void (* SAMPLER_deleteCB) (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash);
150 * (Re)Initialise given Sampler with random min-wise independent function.
152 * In this implementation this means choosing an auth_key for later use in
155 * @param id pointer to the place where this sampler will store the PeerID.
156 * This will be overwritten.
159 SAMPLER_init(struct GNUNET_PeerIdentity *id)
163 s = GNUNET_new(struct Sampler);
165 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
166 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
168 GNUNET_CRYPTO_HASH_LENGTH);
170 //s->peer_id = GNUNET_new( struct GNUNET_PeerIdentity );
171 GNUENT_assert(NULL != id);
173 memcpy(s->peer_id, own_identity, sizeof(struct GNUNET_PeerIdentity));
174 //s->peer_id = own_identity; // Maybe set to own PeerID. So we always have
175 // a valid PeerID in the sampler.
176 // Maybe take a PeerID as second argument.
177 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: initialised with PeerID %s (at %p) \n", GNUNET_i2s(s->peer_id), s->peer_id);
179 GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id,
180 sizeof(struct GNUNET_PeerIdentity),
190 * Compare two hashes.
192 * Returns if the first one is smaller then the second.
193 * Used by SAMPLER_next() to compare hashes.
196 hash_cmp(struct GNUNET_HashCode hash1, struct GNUNET_HashCode hash2)
198 return memcmp( (const void *) &hash1, (const void *) & hash2, sizeof(struct GNUNET_HashCode));
202 * Input an PeerID into the given sampler.
205 SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *id, SAMPLER_deleteCB del_cb, void *cb_cls)
206 // TODO set id in peer_ids
208 struct GNUNET_HashCode other_hash;
210 if ( id == s->peer_id )
212 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
214 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s\n",
215 GNUNET_i2s(s->peer_id));
219 GNUNET_CRYPTO_hmac(&s->auth_key,
221 sizeof(struct GNUNET_PeerIdentity),
224 if ( NULL == s->peer_id )
225 { // Or whatever is a valid way to say
226 // "we have no PeerID at the moment"
227 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Simply accepting (got NULL previously).\n",
229 memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
231 s->peer_id_hash = other_hash;
233 else if ( 0 > hash_cmp(other_hash, s->peer_id_hash) )
235 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
237 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s\n",
238 GNUNET_i2s(s->peer_id));
240 if ( NULL != del_cb )
242 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s with the delete callback.\n",
243 GNUNET_i2s(s->peer_id));
244 del_cb(cb_cls, s->peer_id, s->peer_id_hash);
247 memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
249 s->peer_id_hash = other_hash;
253 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
255 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s\n",
256 GNUNET_i2s(s->peer_id), s->peer_id);
263 * Initialise a tuple of samplers.
266 SAMPLER_samplers_init(size_t init_size)
268 struct Samplers *samplers;
272 samplers = GNUNET_new(struct Samplers);
273 samplers->size = init_size;
274 samplers->head = samplers->tail = NULL;
275 samplers->peer_ids = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
277 for ( i = 0 ; i < init_size ; i++ )
279 GNUNET_array_append(samplers->peer_ids,
280 sizeof(struct GNUNET_PeerIdentity),
282 s = SAMPLER_init(&samplers->peer_ids[i]);
283 GNUNET_CONTAINER_DLL_insert_tail(samplers->head,
292 * A fuction to update every sampler in the given list
295 SAMPLER_update_list(struct Samplers *samplers, const struct GNUNET_PeerIdentity *id,
296 SAMPLER_deleteCB del_cb, void *cb_cls)
298 struct Sampler *sampler;
300 sampler = samplers->head;
301 while ( NULL != sampler->next )
303 SAMPLER_next(sampler, id, del_cb, cb_cls);
309 * Get one random peer out of the sampled peers.
311 * We might want to reinitialise this sampler after giving the
312 * corrsponding peer to the client.
314 const struct GNUNET_PeerIdentity*
315 SAMPLER_get_rand_peer (struct Samplers *samplers)
317 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
319 if ( 0 == samplers->size )
321 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
327 struct Sampler *iter;
330 const struct GNUNET_PeerIdentity *peer;
333 * Choose the index of the peer we want to give back
334 * at random from the interval of the sampler list
336 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
338 // TODO check that it does not overflow
339 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Length of Slist: %" PRIu64 ", index: %" PRIu64 "\n", list_size, index);
341 s = sizeof( struct Sampler );
342 iter = samplers->head;
343 for ( i = 0 ; i < index ; i++ )
345 if ( NULL == iter->next )
347 iter = samplers->head;
351 // TODO something missing?
353 peer = iter->peer_id;
354 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s\n", GNUNET_i2s(peer));
355 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: (own ID: %s)\n", GNUNET_i2s(own_identity));
362 * Get n random peers out of the sampled peers.
364 * We might want to reinitialise this sampler after giving the
365 * corrsponding peer to the client.
366 * Random with or without consumption?
368 const struct GNUNET_PeerIdentity* // TODO give back simple array
369 SAMPLER_get_n_rand_peers (struct Samplers *samplers, uint64_t n)
371 // TODO check if we have too much (distinct) sampled peers
372 // If we are not ready yet maybe schedule for later
373 const struct GNUNET_PeerIdentity *peers;
376 peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity));
378 for ( i = 0 ; i < n ; i++ ) {
379 peers[i] = SAMPLER_get_rand_peer(samplers);
382 // TODO something else missing?
387 * Counts how many Samplers currently hold a given PeerID.
390 SAMPLER_count_id ( struct Samplers *samplers, struct GNUNET_PeerIdentity *id )
392 struct Sampler *iter;
395 iter = samplers->head;
397 while ( NULL != iter )
399 if ( peer_id_cmp( iter->peer_id, id) )
407 * Gow the size of the tuple of samplers.
410 SAMPLER_samplers_grow (struct Samplers * samplers, size_t new_size)
415 if ( new_size > samplers->size )
417 GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
418 for ( i = 0 ; i < new_size - samplers-size ; i++ )
420 sampler = SAMPLER_init(&samplers->peer_ids[samplers->size + i]);
421 GNUNET_CONTAINER_DLL_insert_tail(samplers->head, samplers->tail, sampler);
424 else if ( new_size < samplers->size )
426 for ( i = 0 ; i < samplers->size - new_size ; i++)
428 // TODO call delCB on elem?
429 GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, samplers->tail);
431 GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
434 samplers->size = new_size;
437 /***********************************************************************
439 ***********************************************************************/
443 /***********************************************************************
444 * Housekeeping with peers
445 ***********************************************************************/
448 * Struct used to store the context of a connected client.
453 * The message queue to communicate with the client.
455 struct GNUNET_MQ_Handle *mq;
459 * Used to keep track in what lists single peerIDs are.
461 enum in_list_flag // probably unneeded
463 in_other_sampler_list = 0x1,
464 in_other_gossip_list = 0x2, // unneeded?
465 in_own_sampler_list = 0x4,
466 in_own_gossip_list = 0x8 // unneeded?
470 * Struct used to keep track of other peer's status
472 * This is stored in a multipeermap.
477 * In own gossip/sampler list, in other's gossip/sampler list
479 uint32_t in_flags; // unneeded?
482 * Message queue open to client
484 struct GNUNET_MQ_Handle *mq;
487 * Channel open to client.
489 struct GNUNET_CADET_Channel *to_channel;
492 * Channel open from client.
494 struct GNUNET_CADET_Channel *from_channel; // unneeded
497 * This is pobably followed by 'statistical' data (when we first saw
498 * him, how did we get his ID, how many pushes (in a timeinterval),
503 /***********************************************************************
504 * /Housekeeping with peers
505 ***********************************************************************/
508 * Set of all peers to keep track of them.
510 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
513 // -- gossip list length --
514 // Depends on the (estimated) size of the
515 // network. - Initial size might be the
516 // number of peers cadet provides.
517 // TODO other events to grow/shrink size?
520 * List of samplers // TODO get rid of that
522 struct GNUNET_CONTAINER_SList *sampler_list;
527 struct Samplers *samplers; // TODO rename to sampler_list
530 * Sampler list size // TODO get rid of that
532 * Adapts to the nse. Size should be in BigTheta(network_size)^(1/3).
534 size_t sampler_list_size;
538 * The gossiped list of peers.
540 struct GNUNET_PeerIdentity *gossip_list;
543 * Size of the gossiped list
545 unsigned int gossip_list_size;
548 * Min size of the gossip list
550 uint64_t gossip_list_min_size;
553 // * Max size of the gossip list
555 // * This will probably be left to be set by the client.
557 //uint64_t gossip_list_max_size;
561 * The estimated size of the network.
563 * Influenced by the stdev.
570 * Percentage of total peer number in the gossip list
571 * to send random PUSHes to
576 * Percentage of total peer number in the gossip list
577 * to send random PULLs to
582 * The percentage gamma of history updates.
583 * Simply 1 - alpha - beta
590 * Identifier for the main task that runs periodically.
592 GNUNET_SCHEDULER_TaskIdentifier do_round_task;
595 * Time inverval the do_round task runs in.
597 struct GNUNET_TIME_Relative round_interval;
602 * List to store peers received through pushes temporary.
604 struct GNUNET_PeerIdentity *push_list;
607 * Size of the push_list;
609 size_t push_list_size;
612 * List to store peers received through pulls temporary.
614 struct GNUNET_PeerIdentity *pull_list;
617 * Size of the pull_list;
619 size_t pull_list_size;
625 struct GNUNET_NSE_Handle *nse;
630 struct GNUNET_CADET_Handle *cadet_handle;
633 /***********************************************************************
635 ***********************************************************************/
638 * Get random peer from the gossip list.
640 struct GNUNET_PeerIdentity *
641 get_rand_gossip_peer()
644 struct GNUNET_PeerIdentity *peer;
646 // TODO find a better solution.
647 // FIXME if we have only own ID in gossip list this will block
648 // but then we might have a problem nevertheless ?
653 * Choose the index of the peer we want to return
654 * at random from the interval of the gossip list
656 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
659 peer = &(gossip_list[index]);
660 } while ( own_identity == peer || NULL == peer );
666 * Get the message queue of a specific peer.
668 * If we already have a message queue open to this client,
669 * simply return it, otherways create one.
671 struct GNUNET_MQ_Handle *
672 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, struct GNUNET_PeerIdentity *peer_id)
674 struct peer_context *ctx;
675 struct GNUNET_MQ_Handle * mq;
676 struct GNUNET_CADET_Channel *channel;
678 if ( GNUNET_OK != GNUNET_CONTAINER_multipeermap_contains( peer_map, peer_id ) ) {
680 channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
681 GNUNET_RPS_CADET_PORT,
682 GNUNET_CADET_OPTION_RELIABLE);
683 mq = GNUNET_CADET_mq_create(channel);
685 ctx = GNUNET_malloc(sizeof(struct peer_context));
687 ctx->to_channel = channel;
690 GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
693 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer_id);
694 if ( NULL == ctx->mq ) {
695 if ( NULL == ctx->to_channel ) {
696 channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
697 GNUNET_RPS_CADET_PORT,
698 GNUNET_CADET_OPTION_RELIABLE);
699 ctx->to_channel = channel;
702 mq = GNUNET_CADET_mq_create(ctx->to_channel);
711 /***********************************************************************
713 ***********************************************************************/
716 * Function called by NSE.
718 * Updates sizes of sampler list and gossip list and adapt those lists
722 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
725 //double scale; // TODO this might go gloabal/config
727 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
729 estimate = 1 << (uint64_t) round(logestimate);
730 // GNUNET_NSE_log_estimate_to_n (logestimate);
731 estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add
732 if ( 0 < estimate ) {
733 LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
736 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
741 * Handle RPS request from the client.
744 * @param client identification of the client
745 * @param message the actual message
749 handle_cs_request (void *cls,
750 struct GNUNET_SERVER_Client *client,
751 const struct GNUNET_MessageHeader *message)
753 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
755 struct GNUNET_RPS_CS_RequestMessage *msg;
756 //unsigned int n_arr[sampler_list_size];// =
757 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
758 //struct GNUNET_MQ_Handle *mq;
759 struct client_ctx *cli_ctx;
760 struct GNUNET_MQ_Envelope *ev;
761 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
766 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
767 // Does not work because the compiler seems not to find it.
768 cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx);
769 if ( NULL == cli_ctx ) {
770 cli_ctx = GNUNET_new(struct client_ctx);
771 cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client);
772 GNUNET_SERVER_client_set_user_context(client, cli_ctx);
775 //mq = GNUNET_MQ_queue_for_server_client(client);
777 // TODO How many peers do we give back?
778 // Wait until we have enough random peers?
780 ev = GNUNET_MQ_msg_extra(out_msg,
781 GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity),
782 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
783 out_msg->num_peers = GNUNET_ntohll(msg->num_peers);
785 num_peers = GNUNET_ntohll(msg->num_peers);
786 //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers);
787 for ( i = 0 ; i < num_peers ; i++ ) {
788 memcpy(&out_msg[1] + i * sizeof(struct GNUNET_PeerIdentity),
789 SAMPLER_get_rand_peer(sampler_list),
790 sizeof(struct GNUNET_PeerIdentity));
793 GNUNET_MQ_send(cli_ctx->mq, ev);
794 //GNUNET_MQ_send(mq, ev);
795 //GNUNET_MQ_destroy(mq);
797 GNUNET_SERVER_receive_done (client,
802 * Handle a PUSH message from another peer.
804 * Check the proof of work and store the PeerID
805 * in the temporary list for pushed PeerIDs.
808 * @param channel The channel the PUSH was received over
809 * @param channel_ctx The context associated with this channel
810 * @param msg The message header
813 handle_peer_push (void *cls,
814 struct GNUNET_CADET_Channel *channel,
816 const struct GNUNET_MessageHeader *msg)
818 LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
820 struct GNUNET_PeerIdentity *peer;
822 // TODO check the proof of work
823 // and check limit for PUSHes
824 // IF we count per peer PUSHes
825 // maybe remove from gossip/sampler list
827 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
829 /* Add the sending peer to the push_list */
830 GNUNET_array_append(push_list, push_list_size, *peer);
837 * Handle PULL REQUEST request message from another peer.
839 * Reply with the gossip list of PeerIDs.
842 * @param channel The channel the PUSH was received over
843 * @param channel_ctx The context associated with this channel
844 * @param msg The message header
847 handle_peer_pull_request (void *cls,
848 struct GNUNET_CADET_Channel *channel,
850 const struct GNUNET_MessageHeader *msg)
853 struct GNUNET_PeerIdentity *peer;
854 struct GNUNET_MQ_Handle *mq;
855 //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg;
856 struct GNUNET_MQ_Envelope *ev;
857 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
859 // TODO find some way to keep one peer from spamming with pull requests
860 // allow only one request per time interval ?
861 // otherwise remove from peerlist?
863 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER);
864 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer));
866 mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
867 //mq = get_mq(peer_map, peer);
869 //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg;
870 // TODO how many peers do we actually send?
871 // GNUNET_ntohll(in_msg->num_peers)
872 ev = GNUNET_MQ_msg_extra(out_msg,
873 gossip_list_size * sizeof(struct GNUNET_PeerIdentity),
874 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
875 out_msg->num_peers = GNUNET_htonll(gossip_list_size);
876 memcpy(&out_msg[1], gossip_list,
877 gossip_list_size * sizeof(struct GNUNET_PeerIdentity));
879 GNUNET_MQ_send(mq, ev);
881 GNUNET_MQ_destroy(mq);
888 * Handle PULL REPLY message from another peer.
890 * Check whether we sent a corresponding request and
891 * whether this reply is the first one.
894 * @param channel The channel the PUSH was received over
895 * @param channel_ctx The context associated with this channel
896 * @param msg The message header
899 handle_peer_pull_reply (void *cls,
900 struct GNUNET_CADET_Channel *channel,
902 const struct GNUNET_MessageHeader *msg)
904 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
906 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
909 // TODO check that we sent a request and that it is the first reply
911 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
912 for ( i = 0 ; i < GNUNET_ntohll(in_msg->num_peers) ; i++ ) {
913 GNUNET_array_append(pull_list, pull_list_size, in_msg[i]);
917 // TODO maybe a disconnect happens here
924 * Callback called when a Sampler is updated.
927 delete_cb (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
931 //s = SAMPLER_count_id(samplers, id); // TODO
932 s = SAMPLER_count_id(sampler_list, id);
935 GNUNET_CONTAINER_multipeermap_remove_all( peer_map, id);
941 * Send out PUSHes and PULLs.
943 * This is executed regylary.
946 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
948 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
952 struct GNUNET_CONTAINER_SList_Iterator *iter;
953 //unsigned int *n_arr;
954 struct GNUNET_RPS_P2P_PushMessage *push_msg;
955 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
956 struct GNUNET_MQ_Envelope *ev;
957 struct GNUNET_PeerIdentity *peer;
959 // TODO print lists, ...
960 // TODO cleanup peer_map
962 iter = GNUNET_new(struct GNUNET_CONTAINER_SList_Iterator);
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(samplers, 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,
1052 gossip_list[tmp_index] = samplers->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 */
1067 for ( i = 0 ; i < push_list_size ; i++ )
1069 SAMPLER_update_list(samplers, push_list[i]);
1070 // TODO set in_flag?
1073 for ( i = 0 ; i < pull_list_size ; i++ )
1075 SAMPLER_update_list(samplers, pull_list[i]);
1076 // TODO set in_flag?
1080 // TODO go over whole peer_map and do cleanups
1081 // delete unneeded peers, set in_flags, check channel/mq
1084 /* Empty push/pull lists */
1085 GNUNET_array_grow(push_list, push_list_size, 0);
1087 GNUNET_array_grow(pull_list, pull_list_size, 0);
1091 /* Schedule next round */
1093 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1094 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1098 rps_start (struct GNUNET_SERVER_Handle *server);
1101 * This is called from GNUNET_CADET_get_peers().
1103 * It is called on every peer(ID) that cadet somehow has contact with.
1104 * We use those to initialise the sampler.
1107 init_peer_cb (void *cls,
1108 const struct GNUNET_PeerIdentity *peer,
1109 int tunnel, // "Do we have a tunnel towards this peer?"
1110 unsigned int n_paths, // "Number of known paths towards this peer"
1111 unsigned int best_path) // "How long is the best path?
1112 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1114 // FIXME use the magic 0000 PeerID
1115 if ( NULL != peer ) {
1116 LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s (at %p) from CADET\n", GNUNET_i2s(peer), peer);
1117 SAMPLER_update_list(sampler_list, peer, NULL, NULL);
1118 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) ) {
1120 struct peer_context *ctx;
1122 ctx = GNUNET_malloc(sizeof(struct peer_context));
1125 ctx->to_channel = NULL;
1126 ctx->from_channel = NULL;
1127 GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1131 i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size);
1132 gossip_list[i] = *peer;
1133 // TODO send push/pull to each of those peers?
1135 rps_start( (struct GNUNET_SERVER_Handle *) cls);
1143 * Task run during shutdown.
1149 shutdown_task (void *cls,
1150 const struct GNUNET_SCHEDULER_TaskContext *tc)
1152 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1154 if ( GNUNET_SCHEDULER_NO_TASK != do_round_task )
1156 GNUNET_SCHEDULER_cancel (do_round_task);
1157 do_round_task = GNUNET_SCHEDULER_NO_TASK;
1160 GNUNET_NSE_disconnect(nse);
1161 GNUNET_CADET_disconnect(cadet_handle);
1162 GNUNET_free(own_identity);
1163 //GNUNET_free(round_interval);
1164 //GNUNET_free(est_size);
1165 //GNUNET_free(gossip_list_size);
1166 //GNUNET_free(sampler_list_size);
1167 GNUNET_free(gossip_list);
1168 // TODO for i in sampler_list free sampler
1169 // TODO destroy sampler_list
1170 // TODO destroy push/pull_list
1171 // TODO delete global data
1176 * A client disconnected. Remove all of its data structure entries.
1178 * @param cls closure, NULL
1179 * @param client identification of the client
1182 handle_client_disconnect (void *cls,
1183 struct GNUNET_SERVER_Client * client)
1185 // TODO reinitialise that sampler
1189 * Handle the channel a peer opens to us.
1191 * @param cls The closure
1192 * @param channel The channel the peer wants to establish
1193 * @param initiator The peer's peer ID
1194 * @param port The port the channel is being established over
1195 * @param options Further options
1198 handle_inbound_channel (void *cls,
1199 struct GNUNET_CADET_Channel *channel,
1200 const struct GNUNET_PeerIdentity *initiator,
1202 enum GNUNET_CADET_ChannelOption options)
1204 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1206 GNUNET_assert( NULL != channel );
1208 // TODO we might even not store the from_channel
1210 if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) {
1211 ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel;
1212 // FIXME there might already be an established channel
1214 struct peer_context *ctx;
1216 ctx = GNUNET_malloc( sizeof(struct peer_context));
1217 ctx->in_flags = in_other_gossip_list;
1218 ctx->mq = NULL; // TODO create mq?
1219 ctx->from_channel = channel;
1221 GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1223 return NULL; // TODO
1227 * This is called when a remote peer destroys a channel.
1229 * @param cls The closure
1230 * @param channel The channel being closed
1231 * @param channel_ctx The context associated with this channel
1234 cleanup_channel(void *cls,
1235 const struct GNUNET_CADET_Channel *channel,
1238 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1242 * Actually start the service.
1245 rps_start (struct GNUNET_SERVER_Handle *server)
1247 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1248 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1252 GNUNET_SERVER_add_handlers (server, handlers);
1253 GNUNET_SERVER_disconnect_notify (server,
1254 &handle_client_disconnect,
1256 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1260 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1261 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1263 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1271 * Process statistics requests.
1273 * @param cls closure
1274 * @param server the initialized server
1275 * @param c configuration to use
1279 struct GNUNET_SERVER_Handle *server,
1280 const struct GNUNET_CONFIGURATION_Handle *c)
1282 // TODO check what this does -- copied from gnunet-boss
1283 // - seems to work as expected
1284 GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL);
1286 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1293 own_identity = GNUNET_new(struct GNUNET_PeerIdentity);
1295 GNUNET_CRYPTO_get_peer_identity(cfg, own_identity); // TODO check return value
1297 LOG(GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1301 /* Get time interval from the configuration */
1302 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1306 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1307 GNUNET_SCHEDULER_shutdown();
1311 /* Get initial size of sampler/gossip list from the configuration */
1312 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1314 (long long unsigned int *) &est_size))
1316 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1317 GNUNET_SCHEDULER_shutdown();
1320 LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1322 gossip_list_size = sampler_list_size = est_size; // TODO rename est_size
1327 static unsigned int tmp = 0;
1329 GNUNET_array_grow(gossip_list, tmp, gossip_list_size);
1333 /* connect to NSE */
1334 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1335 // TODO check whether that was successful
1336 // TODO disconnect on shutdown
1337 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1342 // TODO initialise thresholds - ?
1344 ///* Get alpha from the configuration */
1345 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1349 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1351 //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1353 ///* Get beta from the configuration */
1354 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1358 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1360 //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1365 peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1368 /* Initialise sampler and gossip list */
1371 samplers = SAMPLER_samplers_init(est_size);
1378 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1379 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1380 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1381 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1385 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1386 cadet_handle = GNUNET_CADET_connect(cfg,
1388 &handle_inbound_channel,
1392 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1395 LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1396 GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server);
1397 // FIXME use magic 0000 PeerID to _start_ the service
1399 // TODO send push/pull to each of those peers?
1404 * The main function for the rps service.
1406 * @param argc number of arguments from the command line
1407 * @param argv command line arguments
1408 * @return 0 ok, 1 on error
1411 main (int argc, char *const *argv)
1413 return (GNUNET_OK ==
1414 GNUNET_SERVICE_run (argc,
1417 GNUNET_SERVICE_OPTION_NONE,
1418 &run, NULL)) ? 0 : 1;
1421 /* end of gnunet-service-rps.c */