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 api -- possibility of getting weak random peer immideately)
47 // TODO malicious peer
49 // TODO Change API to accept initialisation peers
54 static const struct GNUNET_CONFIGURATION_Handle *cfg;
59 struct GNUNET_PeerIdentity *own_identity;
62 struct GNUNET_PeerIdentity *
63 get_rand_peer(struct GNUNET_PeerIdentity *peer_list, unsigned int size);
65 /***********************************************************************
68 * WARNING: This section needs to be reviewed regarding the use of
69 * functions providing (pseudo)randomness!
70 ***********************************************************************/
72 // TODO care about invalid input of the caller (size 0 or less...)
74 // It might be interesting to formulate this independent of PeerIDs.
77 * Callback that is called when a new PeerID is inserted into a sampler.
79 * @param cls the closure given alongside this function.
80 * @param id the PeerID that is inserted
81 * @param hash the hash the sampler produced of the PeerID
83 typedef void (* SAMPLER_insertCB) (void *cls,
84 const struct GNUNET_PeerIdentity *id,
85 struct GNUNET_HashCode hash);
88 * Callback that is called when a new PeerID is removed from a sampler.
90 * @param cls the closure given alongside this function.
91 * @param id the PeerID that is removed
92 * @param hash the hash the sampler produced of the PeerID
94 typedef void (* SAMPLER_removeCB) (void *cls,
95 const struct GNUNET_PeerIdentity *id,
96 struct GNUNET_HashCode hash);
99 * A sampler sampling PeerIDs.
104 * Min-wise linear permutation used by this sampler.
106 * This is an key later used by a hmac.
108 struct GNUNET_CRYPTO_AuthKey auth_key;
111 * The PeerID this sampler currently samples.
113 struct GNUNET_PeerIdentity *peer_id;
116 * The according hash value of this PeerID.
118 struct GNUNET_HashCode peer_id_hash;
121 * Samplers are kept in a linked list.
123 struct Sampler *next;
126 * Samplers are kept in a linked list.
128 struct Sampler *prev;
133 * A n-tuple of samplers.
138 * Number of samplers we hold.
144 * All PeerIDs in one array.
146 struct GNUNET_PeerIdentity *peer_ids;
149 * Callback to be called when a peer gets inserted into a sampler.
151 SAMPLER_insertCB insertCB;
154 * Closure to the insertCB.
159 * Callback to be called when a peer gets inserted into a sampler.
161 SAMPLER_removeCB removeCB;
164 * Closure to the removeCB.
169 * The head of the DLL.
171 struct Sampler *head;
174 * The tail of the DLL.
176 struct Sampler *tail;
181 * (Re)Initialise given Sampler with random min-wise independent function.
183 * In this implementation this means choosing an auth_key for later use in
186 * @param id pointer to the place where this sampler will store the PeerID.
187 * This will be overwritten.
190 SAMPLER_init(struct GNUNET_PeerIdentity *id)
194 s = GNUNET_new(struct Sampler);
196 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
197 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
199 GNUNET_CRYPTO_HASH_LENGTH);
201 GNUNET_assert(NULL != id);
203 memcpy(s->peer_id, own_identity, sizeof(struct GNUNET_PeerIdentity)); // FIXME this should probably be NULL -- the caller has to handle those.
204 //s->peer_id = own_identity; // Maybe set to own PeerID. So we always have
205 // a valid PeerID in the sampler.
206 // Maybe take a PeerID as second argument.
207 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: initialised with PeerID %s (at %p) \n",
208 GNUNET_i2s(s->peer_id), s->peer_id);
210 GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id,
211 sizeof(struct GNUNET_PeerIdentity),
221 * Input an PeerID into the given sampler.
224 SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *other,
225 SAMPLER_insertCB insertCB, void *insertCLS,
226 SAMPLER_removeCB removeCB, void *removeCLS)
227 // TODO call update herein
229 struct GNUNET_HashCode other_hash;
231 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: New PeerID %s at %p\n",
232 GNUNET_i2s(other), other);
233 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Old PeerID %s at %p\n",
234 GNUNET_i2s(s->peer_id), s->peer_id);
236 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(other, s->peer_id) )
238 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
240 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s\n",
241 GNUNET_i2s(s->peer_id));
245 GNUNET_CRYPTO_hmac(&s->auth_key,
247 sizeof(struct GNUNET_PeerIdentity),
250 if ( NULL == s->peer_id )
251 { // Or whatever is a valid way to say
252 // "we have no PeerID at the moment"
253 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Simply accepting (got NULL previously).\n",
255 memcpy(s->peer_id, other, sizeof(struct GNUNET_PeerIdentity));
256 //s->peer_id = other;
257 s->peer_id_hash = other_hash;
258 if (NULL != insertCB)
260 insertCB(insertCLS, s->peer_id, s->peer_id_hash);
263 else if ( 0 > GNUNET_CRYPTO_hash_cmp(&other_hash, &s->peer_id_hash) )
265 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
267 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s\n",
268 GNUNET_i2s(s->peer_id));
270 if ( NULL != removeCB )
272 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s with the remove callback.\n",
273 GNUNET_i2s(s->peer_id));
274 removeCB(removeCLS, s->peer_id, s->peer_id_hash);
277 memcpy(s->peer_id, other, sizeof(struct GNUNET_PeerIdentity));
278 //s->peer_id = other;
279 s->peer_id_hash = other_hash;
281 if ( NULL != insertCB )
283 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Inserting new PeerID %s with the insert callback.\n",
284 GNUNET_i2s(s->peer_id));
285 insertCB(insertCLS, s->peer_id, s->peer_id_hash);
290 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
292 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s\n",
293 GNUNET_i2s(s->peer_id));
299 * Gow or shrink the size of the tuple of samplers.
301 * @param samplers the samplers to grow
302 * @param new_size the new size of the samplers
303 * @param fill_up_id if growing, that has to point to a
304 * valid PeerID and will be used
305 * to initialise newly created samplers
308 SAMPLER_samplers_resize (struct Samplers * samplers,
309 unsigned int new_size,
310 struct GNUNET_PeerIdentity *fill_up_id)
312 if ( samplers->size == new_size )
314 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Size remains the same -- nothing to do\n");
318 unsigned int old_size;
319 struct Sampler *iter;
323 old_size = samplers->size;
324 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Growing/Shrinking samplers %u -> %u\n", old_size, new_size);
326 iter = samplers->head;
328 if ( new_size < old_size )
330 for ( i = new_size ; i < old_size ; i++ )
331 {/* Remove unneeded rest */
333 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing %" PRIX64 ". sampler\n", i);
334 if (NULL != samplers->removeCB)
335 samplers->removeCB(samplers->removeCLS, iter->peer_id, iter->peer_id_hash);
336 GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, iter);
342 GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
343 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: samplers->peer_ids now points to %p\n", samplers->peer_ids);
345 if ( new_size > old_size )
347 GNUNET_assert( NULL != fill_up_id );
348 for ( i = 0 ; i < new_size ; i++ )
351 { /* Update old samplers */
352 iter->peer_id = &samplers->peer_ids[i];
353 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Updated %" PRIX64 ". sampler, now pointing to %p, contains %s\n",
354 i, &samplers->peer_ids[i], GNUNET_i2s(iter->peer_id));
358 { /* Add new samplers */
359 memcpy(&samplers->peer_ids[i], fill_up_id, sizeof(struct GNUNET_PeerIdentity));
360 iter = SAMPLER_init(&samplers->peer_ids[i]);
361 if (NULL != samplers->insertCB)
363 samplers->insertCB(samplers->insertCLS, iter->peer_id, iter->peer_id_hash);
365 GNUNET_CONTAINER_DLL_insert_tail(samplers->head, samplers->tail, iter);
366 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Added %" PRIX64 ". sampler, now pointing to %p, contains %s\n",
367 i, &samplers->peer_ids[i], GNUNET_i2s(iter->peer_id));
371 else// if ( new_size < old_size )
373 for ( i = 0 ; i < new_size ; i++)
376 /* Update remaining samplers */
377 iter->peer_id = &samplers->peer_ids[i];
378 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Updatied %" PRIX64 ". sampler, now pointing to %p, contains %s\n",
379 i, &samplers->peer_ids[i], GNUNET_i2s(iter->peer_id));
385 GNUNET_assert(samplers->size == new_size);
386 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Finished growing/shrinking.\n");
391 * Initialise a tuple of samplers.
394 SAMPLER_samplers_init(size_t init_size, struct GNUNET_PeerIdentity *id,
395 SAMPLER_insertCB insertCB, void *insertCLS,
396 SAMPLER_removeCB removeCB, void *removeCLS)
398 struct Samplers *samplers;
402 samplers = GNUNET_new(struct Samplers);
404 samplers->peer_ids = NULL;
405 samplers->insertCB = insertCB;
406 samplers->insertCLS = insertCLS;
407 samplers->removeCB = removeCB;
408 samplers->removeCLS = removeCLS;
409 samplers->head = samplers->tail = NULL;
410 //samplers->peer_ids = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
412 SAMPLER_samplers_resize(samplers, init_size, id);
414 //for ( i = 0 ; i < init_size ; i++ )
416 // GNUNET_array_append(samplers->peer_ids,
419 // s = SAMPLER_init(&samplers->peer_ids[i]);
420 // GNUNET_CONTAINER_DLL_insert_tail(samplers->head,
424 //samplers->size = init_size;
425 GNUNET_assert(init_size == samplers->size);
431 * A fuction to update every sampler in the given list
434 SAMPLER_update_list(struct Samplers *samplers, const struct GNUNET_PeerIdentity *id)
436 struct Sampler *iter;
438 iter = samplers->head;
439 while ( NULL != iter->next )
441 SAMPLER_next(iter, id,
442 samplers->insertCB, samplers->insertCLS,
443 samplers->removeCB, samplers->removeCLS);
450 * Get one random peer out of the sampled peers.
452 * We might want to reinitialise this sampler after giving the
453 * corrsponding peer to the client.
455 const struct GNUNET_PeerIdentity*
456 SAMPLER_get_rand_peer (struct Samplers *samplers)
458 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
460 if ( 0 == samplers->size )
462 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
467 const struct GNUNET_PeerIdentity *peer;
469 peer = get_rand_peer(samplers->peer_ids, samplers->size);
470 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s\n", GNUNET_i2s(peer));
471 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: (own ID: %s)\n", GNUNET_i2s(own_identity));
478 * Get n random peers out of the sampled peers.
480 * We might want to reinitialise this sampler after giving the
481 * corrsponding peer to the client.
482 * Random with or without consumption?
484 const struct GNUNET_PeerIdentity* // TODO give back simple array
485 SAMPLER_get_n_rand_peers (struct Samplers *samplers, uint64_t n)
487 // TODO check if we have too much (distinct) sampled peers
488 // If we are not ready yet maybe schedule for later
489 struct GNUNET_PeerIdentity *peers;
492 peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity));
494 for ( i = 0 ; i < n ; i++ ) {
495 //peers[i] = SAMPLER_get_rand_peer(samplers);
496 memcpy(&peers[i], SAMPLER_get_rand_peer(samplers), sizeof(struct GNUNET_PeerIdentity));
499 // TODO something else missing?
504 * Counts how many Samplers currently hold a given PeerID.
507 SAMPLER_count_id ( struct Samplers *samplers, const struct GNUNET_PeerIdentity *id )
509 struct Sampler *iter;
512 iter = samplers->head;
514 while ( NULL != iter )
516 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity( iter->peer_id, id) )
525 * Cleans the samplers.
527 * @param samplers the samplers to clean up.
530 SAMPLER_samplers_destroy (struct Samplers *samplers)
532 //struct Sampler *iter;
533 //struct Sampler *tmp;
535 SAMPLER_samplers_resize(samplers, 0, NULL);
536 //iter = samplers->head;
537 //while (NULL != iter) // use _samplers_resize(0)?
539 // iter = iter->next;
541 // GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, iter->prev);
544 //GNUNET_array_grow(samplers->peer_ids, samplers->size, 0);
545 GNUNET_free(samplers);
548 /***********************************************************************
550 ***********************************************************************/
554 /***********************************************************************
555 * Housekeeping with peers
556 ***********************************************************************/
559 * Struct used to store the context of a connected client.
564 * The message queue to communicate with the client.
566 struct GNUNET_MQ_Handle *mq;
570 * Used to keep track in what lists single peerIDs are.
572 enum in_list_flag // probably unneeded
574 in_other_sampler_list = 0x1,
575 in_other_gossip_list = 0x2, // unneeded?
576 in_own_sampler_list = 0x4,
577 in_own_gossip_list = 0x8 // unneeded?
581 * Struct used to keep track of other peer's status
583 * This is stored in a multipeermap.
588 * In own gossip/sampler list, in other's gossip/sampler list
590 uint32_t in_flags; // unneeded?
593 * Message queue open to client
595 struct GNUNET_MQ_Handle *mq;
598 * Channel open to client.
600 struct GNUNET_CADET_Channel *to_channel;
603 * Channel open from client.
605 struct GNUNET_CADET_Channel *from_channel; // unneeded
608 * This is pobably followed by 'statistical' data (when we first saw
609 * him, how did we get his ID, how many pushes (in a timeinterval),
614 /***********************************************************************
615 * /Housekeeping with peers
616 ***********************************************************************/
619 * Set of all peers to keep track of them.
621 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
627 struct Samplers *sampler_list;
631 * The gossiped list of peers.
633 struct GNUNET_PeerIdentity *gossip_list;
636 * Size of the gossiped list
638 unsigned int gossip_list_size;
642 * The estimated size of the network.
644 * Influenced by the stdev.
646 unsigned int est_size;
651 * Percentage of total peer number in the gossip list
652 * to send random PUSHes to
657 * Percentage of total peer number in the gossip list
658 * to send random PULLs to
663 * The percentage gamma of history updates.
664 * Simply 1 - alpha - beta
671 * Identifier for the main task that runs periodically.
673 struct GNUNET_SCHEDULER_Task * do_round_task;
676 * Time inverval the do_round task runs in.
678 struct GNUNET_TIME_Relative round_interval;
683 * List to store peers received through pushes temporary.
685 struct GNUNET_PeerIdentity *push_list;
688 * Size of the push_list;
690 unsigned int push_list_size;
691 //size_t push_list_size;
694 * List to store peers received through pulls temporary.
696 struct GNUNET_PeerIdentity *pull_list;
699 * Size of the pull_list;
701 unsigned int pull_list_size;
702 //size_t pull_list_size;
708 struct GNUNET_NSE_Handle *nse;
713 struct GNUNET_CADET_Handle *cadet_handle;
716 /***********************************************************************
718 ***********************************************************************/
721 * Get random peer from the gossip list.
723 struct GNUNET_PeerIdentity *
724 get_rand_peer(struct GNUNET_PeerIdentity *peer_list, unsigned int size)
727 struct GNUNET_PeerIdentity *peer;
729 // TODO find a better solution.
730 // FIXME if we have only own ID in gossip list this will block
731 // but then we might have a problem nevertheless ?
736 * Choose the r_index of the peer we want to return
737 * at random from the interval of the gossip list
739 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
742 peer = &(peer_list[r_index]);
743 } while ( own_identity == peer || NULL == peer );
749 * Make sure the context of a given peer exists.
752 touch_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
754 struct peer_context *ctx;
756 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) )
758 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer);
762 ctx = GNUNET_malloc(sizeof(struct peer_context));
765 ctx->to_channel = NULL;
766 ctx->from_channel = NULL;
767 GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
772 * Get the context of a peer. If not existing, create.
774 struct peer_context *
775 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
777 struct peer_context *ctx;
779 touch_peer_ctx(peer_map, peer);
780 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer);
785 * Get the channel of a peer. If not existing, create.
788 touch_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
790 struct peer_context *ctx;
792 ctx = get_peer_ctx (peer_map, peer);
793 if (NULL == ctx->to_channel)
795 ctx->to_channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer,
796 GNUNET_RPS_CADET_PORT,
797 GNUNET_CADET_OPTION_RELIABLE);
798 //TODO do I have to explicitly put it in the peer_map?
803 * Get the channel of a peer. If not existing, create.
805 struct GNUNET_CADET_Channel *
806 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
808 struct peer_context *ctx;
810 ctx = get_peer_ctx (peer_map, peer);
811 touch_channel(peer_map, peer);
812 return ctx->to_channel;
816 * Make sure the mq for a given peer exists.
818 * If we already have a message queue open to this client,
819 * simply return it, otherways create one.
822 touch_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer_id)
824 struct peer_context *ctx;
826 ctx = get_peer_ctx(peer_map, peer_id);
829 touch_channel(peer_map, peer_id);
830 ctx->mq = GNUNET_CADET_mq_create(ctx->to_channel);
831 //TODO do I have to explicitly put it in the peer_map?
832 //GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
833 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
838 * Get the message queue of a specific peer.
840 * If we already have a message queue open to this client,
841 * simply return it, otherways create one.
843 struct GNUNET_MQ_Handle *
844 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer_id)
846 struct peer_context *ctx;
848 ctx = get_peer_ctx(peer_map, peer_id);
849 touch_mq(peer_map, peer_id);
854 /***********************************************************************
856 ***********************************************************************/
859 * Function called by NSE.
861 * Updates sizes of sampler list and gossip list and adapt those lists
865 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
868 //double scale; // TODO this might go gloabal/config
870 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
872 estimate = GNUNET_NSE_log_estimate_to_n(logestimate);
873 // GNUNET_NSE_log_estimate_to_n (logestimate);
874 estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add
875 if ( 0 < estimate ) {
876 LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
879 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
884 * Handle RPS request from the client.
887 * @param client identification of the client
888 * @param message the actual message
892 handle_cs_request (void *cls,
893 struct GNUNET_SERVER_Client *client,
894 const struct GNUNET_MessageHeader *message)
896 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
898 struct GNUNET_RPS_CS_RequestMessage *msg;
899 //unsigned int n_arr[sampler_list->size];// =
900 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
901 //struct GNUNET_MQ_Handle *mq;
902 struct client_ctx *cli_ctx;
903 struct GNUNET_MQ_Envelope *ev;
904 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
909 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
910 // Does not work because the compiler seems not to find it.
911 cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx);
912 if ( NULL == cli_ctx ) {
913 cli_ctx = GNUNET_new(struct client_ctx);
914 cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client);
915 GNUNET_SERVER_client_set_user_context(client, cli_ctx);
918 // TODO How many peers do we give back?
919 // Wait until we have enough random peers?
921 ev = GNUNET_MQ_msg_extra(out_msg,
922 GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity),
923 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
924 out_msg->num_peers = msg->num_peers; // No conversion between network and host order
926 num_peers = GNUNET_ntohll(msg->num_peers);
927 //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers);
929 SAMPLER_get_n_rand_peers(sampler_list, num_peers),
930 num_peers * sizeof(struct GNUNET_PeerIdentity));
932 GNUNET_MQ_send(cli_ctx->mq, ev);
933 //GNUNET_MQ_destroy(mq);
935 GNUNET_SERVER_receive_done (client,
940 * Handle a PUSH message from another peer.
942 * Check the proof of work and store the PeerID
943 * in the temporary list for pushed PeerIDs.
946 * @param channel The channel the PUSH was received over
947 * @param channel_ctx The context associated with this channel
948 * @param msg The message header
951 handle_peer_push (void *cls,
952 struct GNUNET_CADET_Channel *channel,
954 const struct GNUNET_MessageHeader *msg)
956 LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
958 struct GNUNET_PeerIdentity *peer;
960 // TODO check the proof of work
961 // and check limit for PUSHes
962 // IF we count per peer PUSHes
963 // maybe remove from gossip/sampler list
965 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
967 /* Add the sending peer to the push_list */
968 LOG(GNUNET_ERROR_TYPE_DEBUG, "Adding peer to push_list of size %u\n", push_list_size);
969 GNUNET_array_append(push_list, push_list_size, *peer);
970 LOG(GNUNET_ERROR_TYPE_DEBUG, "Size of push_list is now %u\n", push_list_size);
976 * Handle PULL REQUEST request message from another peer.
978 * Reply with the gossip list of PeerIDs.
981 * @param channel The channel the PUSH was received over
982 * @param channel_ctx The context associated with this channel
983 * @param msg The message header
986 handle_peer_pull_request (void *cls,
987 struct GNUNET_CADET_Channel *channel,
989 const struct GNUNET_MessageHeader *msg)
992 struct GNUNET_PeerIdentity *peer;
993 struct GNUNET_MQ_Handle *mq;
994 //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg;
995 struct GNUNET_MQ_Envelope *ev;
996 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
998 // TODO find some way to keep one peer from spamming with pull requests
999 // allow only one request per time interval ?
1000 // otherwise remove from peerlist?
1002 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER);
1003 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer));
1005 mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
1006 //mq = get_mq(peer_map, peer);
1008 //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg;
1009 // TODO how many peers do we actually send?
1010 // GNUNET_ntohll(in_msg->num_peers)
1011 ev = GNUNET_MQ_msg_extra(out_msg,
1012 gossip_list_size * sizeof(struct GNUNET_PeerIdentity),
1013 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1014 out_msg->num_peers = GNUNET_htonll(gossip_list_size);
1015 memcpy(&out_msg[1], gossip_list,
1016 gossip_list_size * sizeof(struct GNUNET_PeerIdentity));
1018 GNUNET_MQ_send(mq, ev);
1020 GNUNET_MQ_destroy(mq);
1027 * Handle PULL REPLY message from another peer.
1029 * Check whether we sent a corresponding request and
1030 * whether this reply is the first one.
1032 * @param cls Closure
1033 * @param channel The channel the PUSH was received over
1034 * @param channel_ctx The context associated with this channel
1035 * @param msg The message header
1038 handle_peer_pull_reply (void *cls,
1039 struct GNUNET_CADET_Channel *channel,
1041 const struct GNUNET_MessageHeader *msg)
1043 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1045 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1046 struct GNUNET_PeerIdentity *peers;
1049 // TODO check that we sent a request and that it is the first reply
1051 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1052 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1053 for ( i = 0 ; i < GNUNET_ntohll(in_msg->num_peers) ; i++ ) {
1054 GNUNET_array_append(pull_list, pull_list_size, peers[i]);
1057 // TODO maybe a disconnect happens here
1064 * Send out PUSHes and PULLs.
1066 * This is executed regylary.
1069 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1071 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
1074 //unsigned int *n_arr;
1075 struct GNUNET_RPS_P2P_PushMessage *push_msg;
1076 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
1077 struct GNUNET_MQ_Envelope *ev;
1078 struct GNUNET_PeerIdentity *peer;
1080 // TODO print lists, ...
1081 // TODO cleanup peer_map
1084 /* If the NSE has changed adapt the lists accordingly */
1085 if ( sampler_list->size != est_size )
1086 SAMPLER_samplers_resize(sampler_list, est_size, own_identity);
1088 GNUNET_array_grow(gossip_list, gossip_list_size, est_size);
1091 /* Would it make sense to have one shuffeled gossip list and then
1092 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1093 * use the rest to update sampler?
1094 * in essence get random peers with consumption */
1097 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
1098 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %f (%f * %u) peers.\n",
1099 alpha * gossip_list_size, alpha, gossip_list_size);
1100 for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO compute length
1101 peer = get_rand_peer(gossip_list, gossip_list_size);
1102 // TODO check NULL == peer
1103 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s(peer));
1105 ev = GNUNET_MQ_msg(push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1106 //ev = GNUNET_MQ_msg_extra();
1107 /* TODO Compute proof of work here
1109 push_msg->placeholder = 0;
1110 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
1112 // TODO modify in_flags of respective peer?
1116 /* Send PULL requests */
1118 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1119 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %f (%f * %u) peers.\n",
1120 beta * gossip_list_size, beta, gossip_list_size);
1121 for ( i = 0 ; i < beta * gossip_list_size ; i++ ){ // TODO compute length
1122 peer = get_rand_peer(gossip_list, gossip_list_size);
1123 // TODO check NULL == peer
1124 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s(peer));
1126 ev = GNUNET_MQ_msg(pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1127 //ev = GNUNET_MQ_msg_extra();
1128 pull_msg->placeholder = 0;
1129 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
1130 // TODO modify in_flags of respective peer?
1134 /* Update gossip list */
1137 if ( push_list_size <= alpha * gossip_list_size &&
1138 push_list_size != 0 &&
1139 pull_list_size != 0 )
1141 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
1143 uint64_t first_border;
1144 uint64_t second_border;
1146 first_border = round(alpha * gossip_list_size);
1147 for ( i = 0 ; i < first_border ; i++ )
1148 { // TODO use SAMPLER_get_n_rand_peers
1149 /* Update gossip list with peers received through PUSHes */
1150 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1152 gossip_list[i] = push_list[r_index];
1153 // TODO change the in_flags accordingly
1156 second_border = first_border + round(beta * gossip_list_size);
1157 for ( i = first_border ; i < second_border ; i++ )
1159 /* Update gossip list with peers received through PULLs */
1160 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1162 gossip_list[i] = pull_list[r_index];
1163 // TODO change the in_flags accordingly
1166 for ( i = second_border ; i < gossip_list_size ; i++ )
1168 /* Update gossip list with peers from history */
1169 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1170 sampler_list->size);
1171 gossip_list[i] = sampler_list->peer_ids[r_index];
1172 // TODO change the in_flags accordingly
1178 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1180 // TODO independent of that also get some peers from CADET_get_peers()?
1183 /* Update samplers */
1185 for ( i = 0 ; i < push_list_size ; i++ )
1187 SAMPLER_update_list(sampler_list, &push_list[i]);
1188 // TODO set in_flag?
1191 for ( i = 0 ; i < pull_list_size ; i++ )
1193 SAMPLER_update_list(sampler_list, &pull_list[i]);
1194 // TODO set in_flag?
1198 /* Empty push/pull lists */
1199 GNUNET_array_grow(push_list, push_list_size, 0);
1200 push_list_size = 0; // I guess that's not necessary but doesn't hurt
1201 GNUNET_array_grow(pull_list, pull_list_size, 0);
1202 pull_list_size = 0; // I guess that's not necessary but doesn't hurt
1205 /* Schedule next round */
1206 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1207 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1211 * Open a connection to given peer and store channel and mq.
1214 insertCB (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
1216 touch_mq(peer_map, id);
1220 * Close the connection to given peer and delete channel and mq.
1223 removeCB (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
1226 struct peer_context *ctx;
1228 s = SAMPLER_count_id(sampler_list, id);
1230 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(peer_map, id))
1232 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, id);
1233 if (NULL != ctx->to_channel)
1235 if (NULL != ctx->mq)
1237 GNUNET_MQ_destroy(ctx->mq);
1239 GNUNET_CADET_channel_destroy(ctx->to_channel);
1241 // TODO cleanup peer
1242 GNUNET_CONTAINER_multipeermap_remove_all(peer_map, id);
1248 rps_start (struct GNUNET_SERVER_Handle *server);
1251 * This is called from GNUNET_CADET_get_peers().
1253 * It is called on every peer(ID) that cadet somehow has contact with.
1254 * We use those to initialise the sampler.
1257 init_peer_cb (void *cls,
1258 const struct GNUNET_PeerIdentity *peer,
1259 int tunnel, // "Do we have a tunnel towards this peer?"
1260 unsigned int n_paths, // "Number of known paths towards this peer"
1261 unsigned int best_path) // "How long is the best path?
1262 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1266 LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s (at %p) from CADET\n", GNUNET_i2s(peer), peer);
1267 SAMPLER_update_list(sampler_list, peer);
1268 touch_peer_ctx(peer_map, peer);
1271 i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size);
1272 gossip_list[i] = *peer;
1273 // TODO send push/pull to each of those peers?
1277 rps_start( (struct GNUNET_SERVER_Handle *) cls);
1285 * Task run during shutdown.
1291 shutdown_task (void *cls,
1292 const struct GNUNET_SCHEDULER_TaskContext *tc)
1294 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1296 if ( NULL != do_round_task )
1298 GNUNET_SCHEDULER_cancel (do_round_task);
1299 do_round_task = NULL;
1302 GNUNET_NSE_disconnect(nse);
1303 GNUNET_CADET_disconnect(cadet_handle);
1304 GNUNET_free(own_identity);
1305 //GNUNET_free(round_interval);
1306 //GNUNET_free(gossip_list);
1307 SAMPLER_samplers_destroy(sampler_list);
1308 GNUNET_array_grow(gossip_list, gossip_list_size, 0);
1309 GNUNET_array_grow(push_list, push_list_size, 0);
1310 GNUNET_array_grow(pull_list, pull_list_size, 0);
1311 // TODO delete global data
1316 * A client disconnected. Remove all of its data structure entries.
1318 * @param cls closure, NULL
1319 * @param client identification of the client
1322 handle_client_disconnect (void *cls,
1323 struct GNUNET_SERVER_Client * client)
1325 // TODO reinitialise that sampler
1329 * Handle the channel a peer opens to us.
1331 * @param cls The closure
1332 * @param channel The channel the peer wants to establish
1333 * @param initiator The peer's peer ID
1334 * @param port The port the channel is being established over
1335 * @param options Further options
1338 handle_inbound_channel (void *cls,
1339 struct GNUNET_CADET_Channel *channel,
1340 const struct GNUNET_PeerIdentity *initiator,
1342 enum GNUNET_CADET_ChannelOption options)
1344 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1346 GNUNET_assert( NULL != channel );
1348 // TODO we might not even store the from_channel
1350 if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) {
1351 ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel;
1352 // FIXME there might already be an established channel
1354 struct peer_context *ctx;
1356 ctx = GNUNET_malloc( sizeof(struct peer_context));
1357 ctx->in_flags = in_other_gossip_list;
1358 ctx->mq = NULL; // TODO create mq?
1359 ctx->from_channel = channel;
1361 GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1363 return NULL; // TODO
1367 * This is called when a remote peer destroys a channel.
1369 * @param cls The closure
1370 * @param channel The channel being closed
1371 * @param channel_ctx The context associated with this channel
1374 cleanup_channel(void *cls,
1375 const struct GNUNET_CADET_Channel *channel,
1378 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1379 // TODO test whether that was a peer in the samplers/a peer we opened a connection to
1380 // and if so, reinitialise the sampler
1384 * Actually start the service.
1387 rps_start (struct GNUNET_SERVER_Handle *server)
1389 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1390 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1394 GNUNET_SERVER_add_handlers (server, handlers);
1395 GNUNET_SERVER_disconnect_notify (server,
1396 &handle_client_disconnect,
1398 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1402 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1403 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1405 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1413 * Process statistics requests.
1415 * @param cls closure
1416 * @param server the initialized server
1417 * @param c configuration to use
1421 struct GNUNET_SERVER_Handle *server,
1422 const struct GNUNET_CONFIGURATION_Handle *c)
1424 // TODO check what this does -- copied from gnunet-boss
1425 // - seems to work as expected
1426 GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL);
1428 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1433 own_identity = GNUNET_new(struct GNUNET_PeerIdentity); // needed?
1435 GNUNET_CRYPTO_get_peer_identity(cfg, own_identity); // TODO check return value
1437 GNUNET_assert(NULL != own_identity);
1439 LOG(GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1443 /* Get time interval from the configuration */
1444 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1448 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1449 GNUNET_SCHEDULER_shutdown();
1453 /* Get initial size of sampler/gossip list from the configuration */
1454 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1456 (long long unsigned int *) &est_size))
1458 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1459 GNUNET_SCHEDULER_shutdown();
1462 LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1464 //gossip_list_size = est_size; // TODO rename est_size
1468 GNUNET_array_grow(gossip_list, gossip_list_size, est_size);
1471 /* connect to NSE */
1472 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1473 // TODO check whether that was successful
1474 // TODO disconnect on shutdown
1475 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1480 // TODO initialise thresholds - ?
1482 ///* Get alpha from the configuration */
1483 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1487 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1489 //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1491 ///* Get beta from the configuration */
1492 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1496 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1498 //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1501 peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1504 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1505 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1506 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1507 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1511 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1512 cadet_handle = GNUNET_CADET_connect(cfg,
1514 &handle_inbound_channel,
1518 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1521 /* Initialise sampler and gossip list */
1523 sampler_list = SAMPLER_samplers_init(est_size, own_identity, insertCB, NULL, removeCB, NULL);
1531 LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1532 GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server);
1533 // FIXME use magic 0000 PeerID to _start_ the service
1535 // TODO send push/pull to each of those peers?
1540 * The main function for the rps service.
1542 * @param argc number of arguments from the command line
1543 * @param argv command line arguments
1544 * @return 0 ok, 1 on error
1547 main (int argc, char *const *argv)
1549 return (GNUNET_OK ==
1550 GNUNET_SERVICE_run (argc,
1553 GNUNET_SERVICE_OPTION_NONE,
1554 &run, NULL)) ? 0 : 1;
1557 /* end of gnunet-service-rps.c */