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 * Compare two hashes.
223 * Used by SAMPLER_next() to compare hashes.
226 hash_cmp(struct GNUNET_HashCode *hash1, struct GNUNET_HashCode *hash2)
228 return memcmp( (const void *) hash1, (const void *) hash2, sizeof(struct GNUNET_HashCode));
232 * Compare two PeerIDs.
234 * Used by SAMPLER_next() to compare hashes.
235 * @param p1 Some peer identity.
236 * @param p2 Some peer identity.
237 * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2.
240 peer_cmp(const struct GNUNET_PeerIdentity *id1, const struct GNUNET_PeerIdentity *id2)
242 return memcmp( (const void *) id1, (const void *) id2, sizeof(struct GNUNET_PeerIdentity));
246 * Input an PeerID into the given sampler.
249 SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *other,
250 SAMPLER_insertCB insertCB, void *insertCLS,
251 SAMPLER_removeCB removeCB, void *removeCLS)
252 // TODO call update herein
254 struct GNUNET_HashCode other_hash;
256 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: New PeerID %s at %p\n",
257 GNUNET_i2s(other), other);
258 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Old PeerID %s at %p\n",
259 GNUNET_i2s(s->peer_id), s->peer_id);
261 if ( 0 == peer_cmp(other, s->peer_id) )
263 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
265 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s\n",
266 GNUNET_i2s(s->peer_id));
270 GNUNET_CRYPTO_hmac(&s->auth_key,
272 sizeof(struct GNUNET_PeerIdentity),
275 if ( NULL == s->peer_id )
276 { // Or whatever is a valid way to say
277 // "we have no PeerID at the moment"
278 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Simply accepting (got NULL previously).\n",
280 memcpy(s->peer_id, other, sizeof(struct GNUNET_PeerIdentity));
281 //s->peer_id = other;
282 s->peer_id_hash = other_hash;
283 if (NULL != insertCB)
285 insertCB(insertCLS, s->peer_id, s->peer_id_hash);
288 else if ( 0 > hash_cmp(&other_hash, &s->peer_id_hash) )
290 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
292 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s\n",
293 GNUNET_i2s(s->peer_id));
295 if ( NULL != removeCB )
297 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s with the remove callback.\n",
298 GNUNET_i2s(s->peer_id));
299 removeCB(removeCLS, s->peer_id, s->peer_id_hash);
302 memcpy(s->peer_id, other, sizeof(struct GNUNET_PeerIdentity));
303 //s->peer_id = other;
304 s->peer_id_hash = other_hash;
306 if ( NULL != insertCB )
308 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Inserting new PeerID %s with the insert callback.\n",
309 GNUNET_i2s(s->peer_id));
310 insertCB(insertCLS, s->peer_id, s->peer_id_hash);
315 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
317 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s\n",
318 GNUNET_i2s(s->peer_id));
324 * Gow or shrink the size of the tuple of samplers.
326 * @param samplers the samplers to grow
327 * @param new_size the new size of the samplers
328 * @param fill_up_id if growing, that has to point to a
329 * valid PeerID and will be used
330 * to initialise newly created samplers
333 SAMPLER_samplers_resize (struct Samplers * samplers,
334 unsigned int new_size,
335 struct GNUNET_PeerIdentity *fill_up_id)
337 if ( samplers->size == new_size )
339 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Size remains the same -- nothing to do\n");
343 unsigned int old_size;
344 struct Sampler *iter;
348 old_size = samplers->size;
349 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Growing/Shrinking samplers %u -> %u\n", old_size, new_size);
351 iter = samplers->head;
353 if ( new_size < old_size )
355 for ( i = new_size ; i < old_size ; i++ )
356 {/* Remove unneeded rest */
358 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing %" PRIX64 ". sampler\n", i);
359 if (NULL != samplers->removeCB)
360 samplers->removeCB(samplers->removeCLS, iter->peer_id, iter->peer_id_hash);
361 GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, iter);
367 GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
368 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: samplers->peer_ids now points to %p\n", samplers->peer_ids);
370 if ( new_size > old_size )
372 GNUNET_assert( NULL != fill_up_id );
373 for ( i = 0 ; i < new_size ; i++ )
376 { /* Update old samplers */
377 iter->peer_id = &samplers->peer_ids[i];
378 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Updated %" PRIX64 ". sampler, now pointing to %p, contains %s\n",
379 i, &samplers->peer_ids[i], GNUNET_i2s(iter->peer_id));
383 { /* Add new samplers */
384 memcpy(&samplers->peer_ids[i], fill_up_id, sizeof(struct GNUNET_PeerIdentity));
385 iter = SAMPLER_init(&samplers->peer_ids[i]);
386 if (NULL != samplers->insertCB)
388 samplers->insertCB(samplers->insertCLS, iter->peer_id, iter->peer_id_hash);
390 GNUNET_CONTAINER_DLL_insert_tail(samplers->head, samplers->tail, iter);
391 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Added %" PRIX64 ". sampler, now pointing to %p, contains %s\n",
392 i, &samplers->peer_ids[i], GNUNET_i2s(iter->peer_id));
396 else// if ( new_size < old_size )
398 for ( i = 0 ; i < new_size ; i++)
401 /* Update remaining samplers */
402 iter->peer_id = &samplers->peer_ids[i];
403 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Updatied %" PRIX64 ". sampler, now pointing to %p, contains %s\n",
404 i, &samplers->peer_ids[i], GNUNET_i2s(iter->peer_id));
410 GNUNET_assert(samplers->size == new_size);
411 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Finished growing/shrinking.\n");
416 * Initialise a tuple of samplers.
419 SAMPLER_samplers_init(size_t init_size, struct GNUNET_PeerIdentity *id,
420 SAMPLER_insertCB insertCB, void *insertCLS,
421 SAMPLER_removeCB removeCB, void *removeCLS)
423 struct Samplers *samplers;
427 samplers = GNUNET_new(struct Samplers);
429 samplers->peer_ids = NULL;
430 samplers->insertCB = insertCB;
431 samplers->insertCLS = insertCLS;
432 samplers->removeCB = removeCB;
433 samplers->removeCLS = removeCLS;
434 samplers->head = samplers->tail = NULL;
435 //samplers->peer_ids = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
437 SAMPLER_samplers_resize(samplers, init_size, id);
439 //for ( i = 0 ; i < init_size ; i++ )
441 // GNUNET_array_append(samplers->peer_ids,
444 // s = SAMPLER_init(&samplers->peer_ids[i]);
445 // GNUNET_CONTAINER_DLL_insert_tail(samplers->head,
449 //samplers->size = init_size;
450 GNUNET_assert(init_size == samplers->size);
456 * A fuction to update every sampler in the given list
459 SAMPLER_update_list(struct Samplers *samplers, const struct GNUNET_PeerIdentity *id)
461 struct Sampler *iter;
463 iter = samplers->head;
464 while ( NULL != iter->next )
466 SAMPLER_next(iter, id,
467 samplers->insertCB, samplers->insertCLS,
468 samplers->removeCB, samplers->removeCLS);
475 * Get one random peer out of the sampled peers.
477 * We might want to reinitialise this sampler after giving the
478 * corrsponding peer to the client.
480 const struct GNUNET_PeerIdentity*
481 SAMPLER_get_rand_peer (struct Samplers *samplers)
483 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
485 if ( 0 == samplers->size )
487 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
492 const struct GNUNET_PeerIdentity *peer;
494 peer = get_rand_peer(samplers->peer_ids, samplers->size);
495 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s\n", GNUNET_i2s(peer));
496 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: (own ID: %s)\n", GNUNET_i2s(own_identity));
503 * Get n random peers out of the sampled peers.
505 * We might want to reinitialise this sampler after giving the
506 * corrsponding peer to the client.
507 * Random with or without consumption?
509 const struct GNUNET_PeerIdentity* // TODO give back simple array
510 SAMPLER_get_n_rand_peers (struct Samplers *samplers, uint64_t n)
512 // TODO check if we have too much (distinct) sampled peers
513 // If we are not ready yet maybe schedule for later
514 struct GNUNET_PeerIdentity *peers;
517 peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity));
519 for ( i = 0 ; i < n ; i++ ) {
520 //peers[i] = SAMPLER_get_rand_peer(samplers);
521 memcpy(&peers[i], SAMPLER_get_rand_peer(samplers), sizeof(struct GNUNET_PeerIdentity));
524 // TODO something else missing?
529 * Counts how many Samplers currently hold a given PeerID.
532 SAMPLER_count_id ( struct Samplers *samplers, const struct GNUNET_PeerIdentity *id )
534 struct Sampler *iter;
537 iter = samplers->head;
539 while ( NULL != iter )
541 if ( 0 == peer_cmp( iter->peer_id, id) )
550 * Cleans the samplers.
552 * @param samplers the samplers to clean up.
555 SAMPLER_samplers_destroy (struct Samplers *samplers)
557 //struct Sampler *iter;
558 //struct Sampler *tmp;
560 SAMPLER_samplers_resize(samplers, 0, NULL);
561 //iter = samplers->head;
562 //while (NULL != iter) // use _samplers_resize(0)?
564 // iter = iter->next;
566 // GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, iter->prev);
569 //GNUNET_array_grow(samplers->peer_ids, samplers->size, 0);
570 GNUNET_free(samplers);
573 /***********************************************************************
575 ***********************************************************************/
579 /***********************************************************************
580 * Housekeeping with peers
581 ***********************************************************************/
584 * Struct used to store the context of a connected client.
589 * The message queue to communicate with the client.
591 struct GNUNET_MQ_Handle *mq;
595 * Used to keep track in what lists single peerIDs are.
597 enum in_list_flag // probably unneeded
599 in_other_sampler_list = 0x1,
600 in_other_gossip_list = 0x2, // unneeded?
601 in_own_sampler_list = 0x4,
602 in_own_gossip_list = 0x8 // unneeded?
606 * Struct used to keep track of other peer's status
608 * This is stored in a multipeermap.
613 * In own gossip/sampler list, in other's gossip/sampler list
615 uint32_t in_flags; // unneeded?
618 * Message queue open to client
620 struct GNUNET_MQ_Handle *mq;
623 * Channel open to client.
625 struct GNUNET_CADET_Channel *to_channel;
628 * Channel open from client.
630 struct GNUNET_CADET_Channel *from_channel; // unneeded
633 * This is pobably followed by 'statistical' data (when we first saw
634 * him, how did we get his ID, how many pushes (in a timeinterval),
639 /***********************************************************************
640 * /Housekeeping with peers
641 ***********************************************************************/
644 * Set of all peers to keep track of them.
646 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
652 struct Samplers *sampler_list;
656 * The gossiped list of peers.
658 struct GNUNET_PeerIdentity *gossip_list;
661 * Size of the gossiped list
663 unsigned int gossip_list_size;
667 * The estimated size of the network.
669 * Influenced by the stdev.
671 unsigned int est_size;
676 * Percentage of total peer number in the gossip list
677 * to send random PUSHes to
682 * Percentage of total peer number in the gossip list
683 * to send random PULLs to
688 * The percentage gamma of history updates.
689 * Simply 1 - alpha - beta
696 * Identifier for the main task that runs periodically.
698 struct GNUNET_SCHEDULER_Task * do_round_task;
701 * Time inverval the do_round task runs in.
703 struct GNUNET_TIME_Relative round_interval;
708 * List to store peers received through pushes temporary.
710 struct GNUNET_PeerIdentity *push_list;
713 * Size of the push_list;
715 unsigned int push_list_size;
716 //size_t push_list_size;
719 * List to store peers received through pulls temporary.
721 struct GNUNET_PeerIdentity *pull_list;
724 * Size of the pull_list;
726 unsigned int pull_list_size;
727 //size_t pull_list_size;
733 struct GNUNET_NSE_Handle *nse;
738 struct GNUNET_CADET_Handle *cadet_handle;
741 /***********************************************************************
743 ***********************************************************************/
746 * Get random peer from the gossip list.
748 struct GNUNET_PeerIdentity *
749 get_rand_peer(struct GNUNET_PeerIdentity *peer_list, unsigned int size)
752 struct GNUNET_PeerIdentity *peer;
754 // TODO find a better solution.
755 // FIXME if we have only own ID in gossip list this will block
756 // but then we might have a problem nevertheless ?
761 * Choose the r_index of the peer we want to return
762 * at random from the interval of the gossip list
764 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
767 peer = &(peer_list[r_index]);
768 } while ( own_identity == peer || NULL == peer );
774 * Make sure the context of a given peer exists.
777 touch_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
779 struct peer_context *ctx;
781 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) )
783 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer);
787 ctx = GNUNET_malloc(sizeof(struct peer_context));
790 ctx->to_channel = NULL;
791 ctx->from_channel = NULL;
792 GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
797 * Get the context of a peer. If not existing, create.
799 struct peer_context *
800 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
802 struct peer_context *ctx;
804 touch_peer_ctx(peer_map, peer);
805 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer);
810 * Get the channel of a peer. If not existing, create.
813 touch_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
815 struct peer_context *ctx;
817 ctx = get_peer_ctx (peer_map, peer);
818 if (NULL == ctx->to_channel)
820 ctx->to_channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer,
821 GNUNET_RPS_CADET_PORT,
822 GNUNET_CADET_OPTION_RELIABLE);
823 //TODO do I have to explicitly put it in the peer_map?
828 * Get the channel of a peer. If not existing, create.
830 struct GNUNET_CADET_Channel *
831 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
833 struct peer_context *ctx;
835 ctx = get_peer_ctx (peer_map, peer);
836 touch_channel(peer_map, peer);
837 return ctx->to_channel;
841 * Make sure the mq for a given peer exists.
843 * If we already have a message queue open to this client,
844 * simply return it, otherways create one.
847 touch_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer_id)
849 struct peer_context *ctx;
851 ctx = get_peer_ctx(peer_map, peer_id);
854 touch_channel(peer_map, peer_id);
855 ctx->mq = GNUNET_CADET_mq_create(ctx->to_channel);
856 //TODO do I have to explicitly put it in the peer_map?
857 //GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
858 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
863 * Get the message queue of a specific peer.
865 * If we already have a message queue open to this client,
866 * simply return it, otherways create one.
868 struct GNUNET_MQ_Handle *
869 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer_id)
871 struct peer_context *ctx;
873 ctx = get_peer_ctx(peer_map, peer_id);
874 touch_mq(peer_map, peer_id);
879 /***********************************************************************
881 ***********************************************************************/
884 * Function called by NSE.
886 * Updates sizes of sampler list and gossip list and adapt those lists
890 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
893 //double scale; // TODO this might go gloabal/config
895 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
897 estimate = 1 << (uint64_t) round(logestimate);
898 // GNUNET_NSE_log_estimate_to_n (logestimate);
899 estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add
900 if ( 0 < estimate ) {
901 LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
904 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
909 * Handle RPS request from the client.
912 * @param client identification of the client
913 * @param message the actual message
917 handle_cs_request (void *cls,
918 struct GNUNET_SERVER_Client *client,
919 const struct GNUNET_MessageHeader *message)
921 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
923 struct GNUNET_RPS_CS_RequestMessage *msg;
924 //unsigned int n_arr[sampler_list->size];// =
925 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
926 //struct GNUNET_MQ_Handle *mq;
927 struct client_ctx *cli_ctx;
928 struct GNUNET_MQ_Envelope *ev;
929 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
934 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
935 // Does not work because the compiler seems not to find it.
936 cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx);
937 if ( NULL == cli_ctx ) {
938 cli_ctx = GNUNET_new(struct client_ctx);
939 cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client);
940 GNUNET_SERVER_client_set_user_context(client, cli_ctx);
943 // TODO How many peers do we give back?
944 // Wait until we have enough random peers?
946 ev = GNUNET_MQ_msg_extra(out_msg,
947 GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity),
948 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
949 out_msg->num_peers = msg->num_peers; // No conversion between network and host order
951 num_peers = GNUNET_ntohll(msg->num_peers);
952 //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers);
954 SAMPLER_get_n_rand_peers(sampler_list, num_peers),
955 num_peers * sizeof(struct GNUNET_PeerIdentity));
957 GNUNET_MQ_send(cli_ctx->mq, ev);
958 //GNUNET_MQ_destroy(mq);
960 GNUNET_SERVER_receive_done (client,
965 * Handle a PUSH message from another peer.
967 * Check the proof of work and store the PeerID
968 * in the temporary list for pushed PeerIDs.
971 * @param channel The channel the PUSH was received over
972 * @param channel_ctx The context associated with this channel
973 * @param msg The message header
976 handle_peer_push (void *cls,
977 struct GNUNET_CADET_Channel *channel,
979 const struct GNUNET_MessageHeader *msg)
981 LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
983 struct GNUNET_PeerIdentity *peer;
985 // TODO check the proof of work
986 // and check limit for PUSHes
987 // IF we count per peer PUSHes
988 // maybe remove from gossip/sampler list
990 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
992 /* Add the sending peer to the push_list */
993 LOG(GNUNET_ERROR_TYPE_DEBUG, "Adding peer to push_list of size %u\n", push_list_size);
994 GNUNET_array_append(push_list, push_list_size, *peer);
995 LOG(GNUNET_ERROR_TYPE_DEBUG, "Size of push_list is now %u\n", push_list_size);
1001 * Handle PULL REQUEST request message from another peer.
1003 * Reply with the gossip list of PeerIDs.
1005 * @param cls Closure
1006 * @param channel The channel the PUSH was received over
1007 * @param channel_ctx The context associated with this channel
1008 * @param msg The message header
1011 handle_peer_pull_request (void *cls,
1012 struct GNUNET_CADET_Channel *channel,
1014 const struct GNUNET_MessageHeader *msg)
1017 struct GNUNET_PeerIdentity *peer;
1018 struct GNUNET_MQ_Handle *mq;
1019 //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg;
1020 struct GNUNET_MQ_Envelope *ev;
1021 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1023 // TODO find some way to keep one peer from spamming with pull requests
1024 // allow only one request per time interval ?
1025 // otherwise remove from peerlist?
1027 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER);
1028 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer));
1030 mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
1031 //mq = get_mq(peer_map, peer);
1033 //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg;
1034 // TODO how many peers do we actually send?
1035 // GNUNET_ntohll(in_msg->num_peers)
1036 ev = GNUNET_MQ_msg_extra(out_msg,
1037 gossip_list_size * sizeof(struct GNUNET_PeerIdentity),
1038 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1039 out_msg->num_peers = GNUNET_htonll(gossip_list_size);
1040 memcpy(&out_msg[1], gossip_list,
1041 gossip_list_size * sizeof(struct GNUNET_PeerIdentity));
1043 GNUNET_MQ_send(mq, ev);
1045 GNUNET_MQ_destroy(mq);
1052 * Handle PULL REPLY message from another peer.
1054 * Check whether we sent a corresponding request and
1055 * whether this reply is the first one.
1057 * @param cls Closure
1058 * @param channel The channel the PUSH was received over
1059 * @param channel_ctx The context associated with this channel
1060 * @param msg The message header
1063 handle_peer_pull_reply (void *cls,
1064 struct GNUNET_CADET_Channel *channel,
1066 const struct GNUNET_MessageHeader *msg)
1068 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1070 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1071 struct GNUNET_PeerIdentity *peers;
1074 // TODO check that we sent a request and that it is the first reply
1076 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1077 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1078 for ( i = 0 ; i < GNUNET_ntohll(in_msg->num_peers) ; i++ ) {
1079 GNUNET_array_append(pull_list, pull_list_size, peers[i]);
1082 // TODO maybe a disconnect happens here
1089 * Send out PUSHes and PULLs.
1091 * This is executed regylary.
1094 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1096 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
1099 //unsigned int *n_arr;
1100 struct GNUNET_RPS_P2P_PushMessage *push_msg;
1101 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
1102 struct GNUNET_MQ_Envelope *ev;
1103 struct GNUNET_PeerIdentity *peer;
1105 // TODO print lists, ...
1106 // TODO cleanup peer_map
1109 /* If the NSE has changed adapt the lists accordingly */
1110 if ( sampler_list->size != est_size )
1111 SAMPLER_samplers_resize(sampler_list, est_size, own_identity);
1113 GNUNET_array_grow(gossip_list, gossip_list_size, est_size);
1116 /* Would it make sense to have one shuffeled gossip list and then
1117 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1118 * use the rest to update sampler?
1119 * in essence get random peers with consumption */
1122 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
1123 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %f (%f * %u) peers.\n",
1124 alpha * gossip_list_size, alpha, gossip_list_size);
1125 for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO compute length
1126 peer = get_rand_peer(gossip_list, gossip_list_size);
1127 // TODO check NULL == peer
1128 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s(peer));
1130 ev = GNUNET_MQ_msg(push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1131 //ev = GNUNET_MQ_msg_extra();
1132 /* TODO Compute proof of work here
1134 push_msg->placeholder = 0;
1135 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
1137 // TODO modify in_flags of respective peer?
1141 /* Send PULL requests */
1143 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1144 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %f (%f * %u) peers.\n",
1145 beta * gossip_list_size, beta, gossip_list_size);
1146 for ( i = 0 ; i < beta * gossip_list_size ; i++ ){ // TODO compute length
1147 peer = get_rand_peer(gossip_list, gossip_list_size);
1148 // TODO check NULL == peer
1149 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s(peer));
1151 ev = GNUNET_MQ_msg(pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1152 //ev = GNUNET_MQ_msg_extra();
1153 pull_msg->placeholder = 0;
1154 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
1155 // TODO modify in_flags of respective peer?
1159 /* Update gossip list */
1162 if ( push_list_size <= alpha * gossip_list_size &&
1163 push_list_size != 0 &&
1164 pull_list_size != 0 )
1166 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
1168 uint64_t first_border;
1169 uint64_t second_border;
1171 first_border = round(alpha * gossip_list_size);
1172 for ( i = 0 ; i < first_border ; i++ )
1173 { // TODO use SAMPLER_get_n_rand_peers
1174 /* Update gossip list with peers received through PUSHes */
1175 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1177 gossip_list[i] = push_list[r_index];
1178 // TODO change the in_flags accordingly
1181 second_border = first_border + round(beta * gossip_list_size);
1182 for ( i = first_border ; i < second_border ; i++ )
1184 /* Update gossip list with peers received through PULLs */
1185 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1187 gossip_list[i] = pull_list[r_index];
1188 // TODO change the in_flags accordingly
1191 for ( i = second_border ; i < gossip_list_size ; i++ )
1193 /* Update gossip list with peers from history */
1194 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1195 sampler_list->size);
1196 gossip_list[i] = sampler_list->peer_ids[r_index];
1197 // TODO change the in_flags accordingly
1203 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1205 // TODO independent of that also get some peers from CADET_get_peers()?
1208 /* Update samplers */
1210 for ( i = 0 ; i < push_list_size ; i++ )
1212 SAMPLER_update_list(sampler_list, &push_list[i]);
1213 // TODO set in_flag?
1216 for ( i = 0 ; i < pull_list_size ; i++ )
1218 SAMPLER_update_list(sampler_list, &pull_list[i]);
1219 // TODO set in_flag?
1223 /* Empty push/pull lists */
1224 GNUNET_array_grow(push_list, push_list_size, 0);
1225 push_list_size = 0; // I guess that's not necessary but doesn't hurt
1226 GNUNET_array_grow(pull_list, pull_list_size, 0);
1227 pull_list_size = 0; // I guess that's not necessary but doesn't hurt
1230 /* Schedule next round */
1231 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1232 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1236 * Open a connection to given peer and store channel and mq.
1239 insertCB (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
1241 touch_mq(peer_map, id);
1245 * Close the connection to given peer and delete channel and mq.
1248 removeCB (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
1251 struct peer_context *ctx;
1253 s = SAMPLER_count_id(sampler_list, id);
1255 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(peer_map, id))
1257 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, id);
1258 if (NULL != ctx->to_channel)
1260 if (NULL != ctx->mq)
1262 GNUNET_MQ_destroy(ctx->mq);
1264 GNUNET_CADET_channel_destroy(ctx->to_channel);
1266 // TODO cleanup peer
1267 GNUNET_CONTAINER_multipeermap_remove_all(peer_map, id);
1273 rps_start (struct GNUNET_SERVER_Handle *server);
1276 * This is called from GNUNET_CADET_get_peers().
1278 * It is called on every peer(ID) that cadet somehow has contact with.
1279 * We use those to initialise the sampler.
1282 init_peer_cb (void *cls,
1283 const struct GNUNET_PeerIdentity *peer,
1284 int tunnel, // "Do we have a tunnel towards this peer?"
1285 unsigned int n_paths, // "Number of known paths towards this peer"
1286 unsigned int best_path) // "How long is the best path?
1287 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1291 LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s (at %p) from CADET\n", GNUNET_i2s(peer), peer);
1292 SAMPLER_update_list(sampler_list, peer);
1293 touch_peer_ctx(peer_map, peer);
1296 i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size);
1297 gossip_list[i] = *peer;
1298 // TODO send push/pull to each of those peers?
1302 rps_start( (struct GNUNET_SERVER_Handle *) cls);
1310 * Task run during shutdown.
1316 shutdown_task (void *cls,
1317 const struct GNUNET_SCHEDULER_TaskContext *tc)
1319 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1321 if ( NULL != do_round_task )
1323 GNUNET_SCHEDULER_cancel (do_round_task);
1324 do_round_task = NULL;
1327 GNUNET_NSE_disconnect(nse);
1328 GNUNET_CADET_disconnect(cadet_handle);
1329 GNUNET_free(own_identity);
1330 //GNUNET_free(round_interval);
1331 //GNUNET_free(gossip_list);
1332 SAMPLER_samplers_destroy(sampler_list);
1333 GNUNET_array_grow(gossip_list, gossip_list_size, 0);
1334 GNUNET_array_grow(push_list, push_list_size, 0);
1335 GNUNET_array_grow(pull_list, pull_list_size, 0);
1336 // TODO delete global data
1341 * A client disconnected. Remove all of its data structure entries.
1343 * @param cls closure, NULL
1344 * @param client identification of the client
1347 handle_client_disconnect (void *cls,
1348 struct GNUNET_SERVER_Client * client)
1350 // TODO reinitialise that sampler
1354 * Handle the channel a peer opens to us.
1356 * @param cls The closure
1357 * @param channel The channel the peer wants to establish
1358 * @param initiator The peer's peer ID
1359 * @param port The port the channel is being established over
1360 * @param options Further options
1363 handle_inbound_channel (void *cls,
1364 struct GNUNET_CADET_Channel *channel,
1365 const struct GNUNET_PeerIdentity *initiator,
1367 enum GNUNET_CADET_ChannelOption options)
1369 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1371 GNUNET_assert( NULL != channel );
1373 // TODO we might not even store the from_channel
1375 if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) {
1376 ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel;
1377 // FIXME there might already be an established channel
1379 struct peer_context *ctx;
1381 ctx = GNUNET_malloc( sizeof(struct peer_context));
1382 ctx->in_flags = in_other_gossip_list;
1383 ctx->mq = NULL; // TODO create mq?
1384 ctx->from_channel = channel;
1386 GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1388 return NULL; // TODO
1392 * This is called when a remote peer destroys a channel.
1394 * @param cls The closure
1395 * @param channel The channel being closed
1396 * @param channel_ctx The context associated with this channel
1399 cleanup_channel(void *cls,
1400 const struct GNUNET_CADET_Channel *channel,
1403 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1404 // TODO test whether that was a peer in the samplers/a peer we opened a connection to
1405 // and if so, reinitialise the sampler
1409 * Actually start the service.
1412 rps_start (struct GNUNET_SERVER_Handle *server)
1414 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1415 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1419 GNUNET_SERVER_add_handlers (server, handlers);
1420 GNUNET_SERVER_disconnect_notify (server,
1421 &handle_client_disconnect,
1423 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1427 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1428 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1430 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1438 * Process statistics requests.
1440 * @param cls closure
1441 * @param server the initialized server
1442 * @param c configuration to use
1446 struct GNUNET_SERVER_Handle *server,
1447 const struct GNUNET_CONFIGURATION_Handle *c)
1449 // TODO check what this does -- copied from gnunet-boss
1450 // - seems to work as expected
1451 GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL);
1453 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1458 own_identity = GNUNET_new(struct GNUNET_PeerIdentity); // needed?
1460 GNUNET_CRYPTO_get_peer_identity(cfg, own_identity); // TODO check return value
1462 GNUNET_assert(NULL != own_identity);
1464 LOG(GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1468 /* Get time interval from the configuration */
1469 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1473 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1474 GNUNET_SCHEDULER_shutdown();
1478 /* Get initial size of sampler/gossip list from the configuration */
1479 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1481 (long long unsigned int *) &est_size))
1483 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1484 GNUNET_SCHEDULER_shutdown();
1487 LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1489 //gossip_list_size = est_size; // TODO rename est_size
1493 GNUNET_array_grow(gossip_list, gossip_list_size, est_size);
1496 /* connect to NSE */
1497 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1498 // TODO check whether that was successful
1499 // TODO disconnect on shutdown
1500 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1505 // TODO initialise thresholds - ?
1507 ///* Get alpha from the configuration */
1508 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1512 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1514 //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1516 ///* Get beta from the configuration */
1517 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1521 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1523 //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1526 peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1529 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1530 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1531 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1532 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1536 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1537 cadet_handle = GNUNET_CADET_connect(cfg,
1539 &handle_inbound_channel,
1543 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1546 /* Initialise sampler and gossip list */
1548 sampler_list = SAMPLER_samplers_init(est_size, own_identity, insertCB, NULL, removeCB, NULL);
1556 LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1557 GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server);
1558 // FIXME use magic 0000 PeerID to _start_ the service
1560 // TODO send push/pull to each of those peers?
1565 * The main function for the rps service.
1567 * @param argc number of arguments from the command line
1568 * @param argv command line arguments
1569 * @return 0 ok, 1 on error
1572 main (int argc, char *const *argv)
1574 return (GNUNET_OK ==
1575 GNUNET_SERVICE_run (argc,
1578 GNUNET_SERVICE_OPTION_NONE,
1579 &run, NULL)) ? 0 : 1;
1582 /* end of gnunet-service-rps.c */