2 This file is part of GNUnet.
3 Copyright (C) 2013-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file rps/gnunet-service-rps.c
21 * @brief rps service implementation
22 * @author Julius Bünger
25 #include "gnunet_applications.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_cadet_service.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_nse_service.h"
30 #include "gnunet_statistics_service.h"
32 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler.h"
34 #include "gnunet-service-rps_custommap.h"
35 #include "gnunet-service-rps_view.h"
40 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
42 // TODO modify @brief in every file
44 // TODO check for overflows
46 // TODO align message structs
48 // TODO connect to friends
50 // TODO store peers somewhere persistent
52 // TODO blacklist? (-> mal peer detection on top of brahms)
54 // hist_size_init, hist_size_max
59 static const struct GNUNET_CONFIGURATION_Handle *cfg;
62 * Handle to the statistics service.
64 static struct GNUNET_STATISTICS_Handle *stats;
69 static struct GNUNET_PeerIdentity own_identity;
73 /***********************************************************************
74 * Old gnunet-service-rps_peers.c
75 ***********************************************************************/
78 * Set a peer flag of given peer context.
80 #define set_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask))
83 * Get peer flag of given peer context.
85 #define check_peer_flag_set(peer_ctx, mask)\
86 ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
89 * Unset flag of given peer context.
91 #define unset_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask))
94 * Set a channel flag of given channel context.
96 #define set_channel_flag(channel_flags, mask) ((*channel_flags) |= (mask))
99 * Get channel flag of given channel context.
101 #define check_channel_flag_set(channel_flags, mask)\
102 ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
105 * Unset flag of given channel context.
107 #define unset_channel_flag(channel_flags, mask) ((*channel_flags) &= ~(mask))
112 * Pending operation on peer consisting of callback and closure
114 * When an operation cannot be executed right now this struct is used to store
115 * the callback and closure for later execution.
131 * List containing all messages that are yet to be send
133 * This is used to keep track of all messages that have not been sent yet. When
134 * a peer is to be removed the pending messages can be removed properly.
136 struct PendingMessage
141 struct PendingMessage *next;
142 struct PendingMessage *prev;
145 * The envelope to the corresponding message
147 struct GNUNET_MQ_Envelope *ev;
150 * The corresponding context
152 struct PeerContext *peer_ctx;
161 * Struct used to keep track of other peer's status
163 * This is stored in a multipeermap.
164 * It contains information such as cadet channels, a message queue for sending,
165 * status about the channels, the pending operations on this peer and some flags
166 * about the status of the peer itself. (live, valid, ...)
171 * Message queue open to client
173 struct GNUNET_MQ_Handle *mq;
176 * Channel open to client.
178 struct GNUNET_CADET_Channel *send_channel;
181 * Flags to the sending channel
183 uint32_t *send_channel_flags;
186 * Channel open from client.
188 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
191 * Flags to the receiving channel
193 uint32_t *recv_channel_flags;
196 * Array of pending operations on this peer.
198 struct PeerPendingOp *pending_ops;
201 * Handle to the callback given to cadet_ntfy_tmt_rdy()
203 * To be canceled on shutdown.
205 struct PendingMessage *liveliness_check_pending;
208 * Number of pending operations.
210 unsigned int num_pending_ops;
213 * Identity of the peer
215 struct GNUNET_PeerIdentity peer_id;
218 * Flags indicating status of peer
223 * Last time we received something from that peer.
225 struct GNUNET_TIME_Absolute last_message_recv;
228 * Last time we received a keepalive message.
230 struct GNUNET_TIME_Absolute last_keepalive;
233 * DLL with all messages that are yet to be sent
235 struct PendingMessage *pending_messages_head;
236 struct PendingMessage *pending_messages_tail;
239 * This is pobably followed by 'statistical' data (when we first saw
240 * him, how did we get his ID, how many pushes (in a timeinterval),
246 * @brief Closure to #valid_peer_iterator
248 struct PeersIteratorCls
253 PeersIterator iterator;
256 * Closure to iterator
262 * @brief Hashmap of valid peers.
264 static struct GNUNET_CONTAINER_MultiPeerMap *valid_peers;
267 * @brief Maximum number of valid peers to keep.
268 * TODO read from config
270 static uint32_t num_valid_peers_max = UINT32_MAX;
273 * @brief Filename of the file that stores the valid peers persistently.
275 static char *filename_valid_peers;
278 * Set of all peers to keep track of them.
280 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
285 static struct GNUNET_CADET_Handle *cadet_handle;
290 * @brief Get the #PeerContext associated with a peer
292 * @param peer the peer id
294 * @return the #PeerContext
296 static struct PeerContext *
297 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
299 struct PeerContext *ctx;
302 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
303 GNUNET_assert (GNUNET_YES == ret);
304 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
305 GNUNET_assert (NULL != ctx);
310 Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer);
313 * @brief Create a new #PeerContext and insert it into the peer map
315 * @param peer the peer to create the #PeerContext for
317 * @return the #PeerContext
319 static struct PeerContext *
320 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
322 struct PeerContext *ctx;
325 GNUNET_assert (GNUNET_NO == Peers_check_peer_known (peer));
327 ctx = GNUNET_new (struct PeerContext);
328 ctx->peer_id = *peer;
329 ctx->send_channel_flags = GNUNET_new (uint32_t);
330 ctx->recv_channel_flags = GNUNET_new (uint32_t);
331 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
332 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
333 GNUNET_assert (GNUNET_OK == ret);
339 * @brief Create or get a #PeerContext
341 * @param peer the peer to get the associated context to
343 * @return the context
345 static struct PeerContext *
346 create_or_get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
348 if (GNUNET_NO == Peers_check_peer_known (peer))
350 return create_peer_ctx (peer);
352 return get_peer_ctx (peer);
356 Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
359 Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
362 * @brief Check whether we have a connection to this @a peer
364 * Also sets the #Peers_ONLINE flag accordingly
366 * @param peer the peer in question
368 * @return #GNUNET_YES if we are connected
369 * #GNUNET_NO otherwise
372 Peers_check_connected (const struct GNUNET_PeerIdentity *peer)
374 const struct PeerContext *peer_ctx;
376 /* If we don't know about this peer we don't know whether it's online */
377 if (GNUNET_NO == Peers_check_peer_known (peer))
381 /* Get the context */
382 peer_ctx = get_peer_ctx (peer);
383 /* If we have no channel to this peer we don't know whether it's online */
384 if ( (NULL == peer_ctx->send_channel) &&
385 (NULL == peer_ctx->recv_channel) )
387 Peers_unset_peer_flag (peer, Peers_ONLINE);
390 /* Otherwise (if we have a channel, we know that it's online */
391 Peers_set_peer_flag (peer, Peers_ONLINE);
397 * @brief The closure to #get_rand_peer_iterator.
399 struct GetRandPeerIteratorCls
402 * @brief The index of the peer to return.
403 * Will be decreased until 0.
404 * Then current peer is returned.
409 * @brief Pointer to peer to return.
411 const struct GNUNET_PeerIdentity *peer;
416 * @brief Iterator function for #get_random_peer_from_peermap.
418 * Implements #GNUNET_CONTAINER_PeerMapIterator.
419 * Decreases the index until the index is null.
420 * Then returns the current peer.
422 * @param cls the #GetRandPeerIteratorCls containing index and peer
423 * @param peer current peer
424 * @param value unused
426 * @return #GNUNET_YES if we should continue to
431 get_rand_peer_iterator (void *cls,
432 const struct GNUNET_PeerIdentity *peer,
435 struct GetRandPeerIteratorCls *iterator_cls = cls;
436 if (0 >= iterator_cls->index)
438 iterator_cls->peer = peer;
441 iterator_cls->index--;
447 * @brief Get a random peer from @a peer_map
449 * @param peer_map the peer_map to get the peer from
451 * @return a random peer
453 static const struct GNUNET_PeerIdentity *
454 get_random_peer_from_peermap (const struct
455 GNUNET_CONTAINER_MultiPeerMap *peer_map)
457 struct GetRandPeerIteratorCls *iterator_cls;
458 const struct GNUNET_PeerIdentity *ret;
460 iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls);
461 iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
462 GNUNET_CONTAINER_multipeermap_size (peer_map));
463 (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
464 get_rand_peer_iterator,
466 ret = iterator_cls->peer;
467 GNUNET_free (iterator_cls);
473 * @brief Add a given @a peer to valid peers.
475 * If valid peers are already #num_valid_peers_max, delete a peer previously.
477 * @param peer the peer that is added to the valid peers.
479 * @return #GNUNET_YES if no other peer had to be removed
480 * #GNUNET_NO otherwise
483 add_valid_peer (const struct GNUNET_PeerIdentity *peer)
485 const struct GNUNET_PeerIdentity *rand_peer;
489 while (GNUNET_CONTAINER_multipeermap_size (valid_peers) >= num_valid_peers_max)
491 rand_peer = get_random_peer_from_peermap (valid_peers);
492 GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer);
495 (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL,
496 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
502 * @brief Set the peer flag to living and
503 * call the pending operations on this peer.
505 * Also adds peer to #valid_peers.
507 * @param peer_ctx the #PeerContext of the peer to set live
510 set_peer_live (struct PeerContext *peer_ctx)
512 struct GNUNET_PeerIdentity *peer;
515 peer = &peer_ctx->peer_id;
516 LOG (GNUNET_ERROR_TYPE_DEBUG,
517 "Peer %s is live and valid, calling %i pending operations on it\n",
519 peer_ctx->num_pending_ops);
521 if (NULL != peer_ctx->liveliness_check_pending)
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 "Removing pending liveliness check for peer %s\n",
525 GNUNET_i2s (&peer_ctx->peer_id));
526 // TODO wait until cadet sets mq->cancel_impl
527 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
528 GNUNET_free (peer_ctx->liveliness_check_pending);
529 peer_ctx->liveliness_check_pending = NULL;
532 (void) add_valid_peer (peer);
533 set_peer_flag (peer_ctx, Peers_ONLINE);
535 /* Call pending operations */
536 for (i = 0; i < peer_ctx->num_pending_ops; i++)
538 peer_ctx->pending_ops[i].op (peer_ctx->pending_ops[i].op_cls, peer);
540 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
544 cleanup_destroyed_channel (void *cls,
545 const struct GNUNET_CADET_Channel *channel);
547 /* Declaration of handlers */
549 handle_peer_check (void *cls,
550 const struct GNUNET_MessageHeader *msg);
553 handle_peer_push (void *cls,
554 const struct GNUNET_MessageHeader *msg);
557 handle_peer_pull_request (void *cls,
558 const struct GNUNET_MessageHeader *msg);
561 check_peer_pull_reply (void *cls,
562 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
565 handle_peer_pull_reply (void *cls,
566 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
568 /* End declaration of handlers */
572 * @brief Get the channel of a peer. If not existing, create.
574 * @param peer the peer id
575 * @return the #GNUNET_CADET_Channel used to send data to @a peer
577 struct GNUNET_CADET_Channel *
578 get_channel (const struct GNUNET_PeerIdentity *peer)
580 struct PeerContext *peer_ctx;
581 struct GNUNET_HashCode port;
582 struct GNUNET_PeerIdentity *ctx_peer;
583 /* There exists a copy-paste-clone in run() */
584 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
585 GNUNET_MQ_hd_fixed_size (peer_check,
586 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
587 struct GNUNET_MessageHeader,
589 GNUNET_MQ_hd_fixed_size (peer_push,
590 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
591 struct GNUNET_MessageHeader,
593 GNUNET_MQ_hd_fixed_size (peer_pull_request,
594 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
595 struct GNUNET_MessageHeader,
597 GNUNET_MQ_hd_var_size (peer_pull_reply,
598 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
599 struct GNUNET_RPS_P2P_PullReplyMessage,
601 GNUNET_MQ_handler_end ()
605 peer_ctx = get_peer_ctx (peer);
606 if (NULL == peer_ctx->send_channel)
608 LOG (GNUNET_ERROR_TYPE_DEBUG,
609 "Trying to establish channel to peer %s\n",
611 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
612 strlen (GNUNET_APPLICATION_PORT_RPS),
614 ctx_peer = GNUNET_new (struct GNUNET_PeerIdentity);
616 peer_ctx->send_channel =
617 GNUNET_CADET_channel_create (cadet_handle,
618 (struct GNUNET_PeerIdentity *) ctx_peer, /* context */
621 GNUNET_CADET_OPTION_RELIABLE,
622 NULL, /* WindowSize handler */
623 cleanup_destroyed_channel, /* Disconnect handler */
626 GNUNET_assert (NULL != peer_ctx->send_channel);
627 return peer_ctx->send_channel;
632 * Get the message queue (#GNUNET_MQ_Handle) of a specific peer.
634 * If we already have a message queue open to this client,
635 * simply return it, otherways create one.
637 * @param peer the peer to get the mq to
638 * @return the #GNUNET_MQ_Handle
640 static struct GNUNET_MQ_Handle *
641 get_mq (const struct GNUNET_PeerIdentity *peer)
643 struct PeerContext *peer_ctx;
645 peer_ctx = get_peer_ctx (peer);
647 if (NULL == peer_ctx->mq)
649 peer_ctx->mq = GNUNET_CADET_get_mq (get_channel (peer));
656 * @brief This is called in response to the first message we sent as a
659 * @param cls #PeerContext of peer with pending liveliness check
662 mq_liveliness_check_successful (void *cls)
664 struct PeerContext *peer_ctx = cls;
666 if (NULL != peer_ctx->liveliness_check_pending)
668 LOG (GNUNET_ERROR_TYPE_DEBUG,
669 "Liveliness check for peer %s was successfull\n",
670 GNUNET_i2s (&peer_ctx->peer_id));
671 GNUNET_free (peer_ctx->liveliness_check_pending);
672 peer_ctx->liveliness_check_pending = NULL;
673 set_peer_live (peer_ctx);
678 * Issue a check whether peer is live
680 * @param peer_ctx the context of the peer
683 check_peer_live (struct PeerContext *peer_ctx)
685 LOG (GNUNET_ERROR_TYPE_DEBUG,
686 "Get informed about peer %s getting live\n",
687 GNUNET_i2s (&peer_ctx->peer_id));
689 struct GNUNET_MQ_Handle *mq;
690 struct GNUNET_MQ_Envelope *ev;
692 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE);
693 peer_ctx->liveliness_check_pending = GNUNET_new (struct PendingMessage);
694 peer_ctx->liveliness_check_pending->ev = ev;
695 peer_ctx->liveliness_check_pending->peer_ctx = peer_ctx;
696 peer_ctx->liveliness_check_pending->type = "Check liveliness";
697 mq = get_mq (&peer_ctx->peer_id);
698 GNUNET_MQ_notify_sent (ev,
699 mq_liveliness_check_successful,
701 GNUNET_MQ_send (mq, ev);
705 * @brief Add an envelope to a message passed to mq to list of pending messages
707 * @param peer peer the message was sent to
708 * @param ev envelope to the message
709 * @param type type of the message to be sent
710 * @return pointer to pending message
712 static struct PendingMessage *
713 insert_pending_message (const struct GNUNET_PeerIdentity *peer,
714 struct GNUNET_MQ_Envelope *ev,
717 struct PendingMessage *pending_msg;
718 struct PeerContext *peer_ctx;
720 peer_ctx = get_peer_ctx (peer);
721 pending_msg = GNUNET_new (struct PendingMessage);
722 pending_msg->ev = ev;
723 pending_msg->peer_ctx = peer_ctx;
724 pending_msg->type = type;
725 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
726 peer_ctx->pending_messages_tail,
733 * @brief Remove a pending message from the respective DLL
735 * @param pending_msg the pending message to remove
736 * @param cancel cancel the pending message, too
739 remove_pending_message (struct PendingMessage *pending_msg, int cancel)
741 struct PeerContext *peer_ctx;
743 peer_ctx = pending_msg->peer_ctx;
744 GNUNET_assert (NULL != peer_ctx);
745 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
746 peer_ctx->pending_messages_tail,
748 // TODO wait for the cadet implementation of message cancellation
749 //if (GNUNET_YES == cancel)
751 // GNUNET_MQ_send_cancel (pending_msg->ev);
753 GNUNET_free (pending_msg);
758 * @brief Check whether function of type #PeerOp was already scheduled
760 * The array with pending operations will probably never grow really big, so
761 * iterating over it should be ok.
763 * @param peer the peer to check
764 * @param peer_op the operation (#PeerOp) on the peer
766 * @return #GNUNET_YES if this operation is scheduled on that peer
767 * #GNUNET_NO otherwise
770 check_operation_scheduled (const struct GNUNET_PeerIdentity *peer,
771 const PeerOp peer_op)
773 const struct PeerContext *peer_ctx;
776 peer_ctx = get_peer_ctx (peer);
777 for (i = 0; i < peer_ctx->num_pending_ops; i++)
778 if (peer_op == peer_ctx->pending_ops[i].op)
784 Peers_remove_peer (const struct GNUNET_PeerIdentity *peer);
787 * Iterator over hash map entries. Deletes all contexts of peers.
790 * @param key current public key
791 * @param value value in the hash map
792 * @return #GNUNET_YES if we should continue to iterate,
796 peermap_clear_iterator (void *cls,
797 const struct GNUNET_PeerIdentity *key,
800 Peers_remove_peer (key);
806 * @brief This is called once a message is sent.
808 * Removes the pending message
810 * @param cls type of the message that was sent
813 mq_notify_sent_cb (void *cls)
815 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
816 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 if (0 == strncmp ("PULL REPLY", pending_msg->type, 10))
820 GNUNET_STATISTICS_update(stats, "# pull replys sent", 1, GNUNET_NO);
821 if (0 == strncmp ("PULL REQUEST", pending_msg->type, 12))
822 GNUNET_STATISTICS_update(stats, "# pull requests sent", 1, GNUNET_NO);
823 if (0 == strncmp ("PUSH", pending_msg->type, 4))
824 GNUNET_STATISTICS_update(stats, "# pushes sent", 1, GNUNET_NO);
825 /* Do not cancle message */
826 remove_pending_message (pending_msg, GNUNET_NO);
831 * @brief Iterator function for #store_valid_peers.
833 * Implements #GNUNET_CONTAINER_PeerMapIterator.
834 * Writes single peer to disk.
836 * @param cls the file handle to write to.
837 * @param peer current peer
838 * @param value unused
840 * @return #GNUNET_YES if we should continue to
845 store_peer_presistently_iterator (void *cls,
846 const struct GNUNET_PeerIdentity *peer,
849 const struct GNUNET_DISK_FileHandle *fh = cls;
850 char peer_string[128];
858 size = GNUNET_snprintf (peer_string,
859 sizeof (peer_string),
861 GNUNET_i2s_full (peer));
862 GNUNET_assert (53 == size);
863 ret = GNUNET_DISK_file_write (fh,
866 GNUNET_assert (size == ret);
872 * @brief Store the peers currently in #valid_peers to disk.
877 struct GNUNET_DISK_FileHandle *fh;
878 uint32_t number_written_peers;
881 if (0 == strncmp ("DISABLE", filename_valid_peers, 7))
886 ret = GNUNET_DISK_directory_create_for_file (filename_valid_peers);
887 if (GNUNET_SYSERR == ret)
889 LOG (GNUNET_ERROR_TYPE_WARNING,
890 "Not able to create directory for file `%s'\n",
891 filename_valid_peers);
894 else if (GNUNET_NO == ret)
896 LOG (GNUNET_ERROR_TYPE_WARNING,
897 "Directory for file `%s' exists but is not writable for us\n",
898 filename_valid_peers);
901 fh = GNUNET_DISK_file_open (filename_valid_peers,
902 GNUNET_DISK_OPEN_WRITE |
903 GNUNET_DISK_OPEN_CREATE,
904 GNUNET_DISK_PERM_USER_READ |
905 GNUNET_DISK_PERM_USER_WRITE);
908 LOG (GNUNET_ERROR_TYPE_WARNING,
909 "Not able to write valid peers to file `%s'\n",
910 filename_valid_peers);
913 LOG (GNUNET_ERROR_TYPE_DEBUG,
914 "Writing %u valid peers to disk\n",
915 GNUNET_CONTAINER_multipeermap_size (valid_peers));
916 number_written_peers =
917 GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
918 store_peer_presistently_iterator,
920 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
921 GNUNET_assert (number_written_peers ==
922 GNUNET_CONTAINER_multipeermap_size (valid_peers));
927 * @brief Convert string representation of peer id to peer id.
929 * Counterpart to #GNUNET_i2s_full.
931 * @param string_repr The string representation of the peer id
933 * @return The peer id
935 static const struct GNUNET_PeerIdentity *
936 s2i_full (const char *string_repr)
938 struct GNUNET_PeerIdentity *peer;
942 peer = GNUNET_new (struct GNUNET_PeerIdentity);
943 len = strlen (string_repr);
946 LOG (GNUNET_ERROR_TYPE_WARNING,
947 "Not able to convert string representation of PeerID to PeerID\n"
948 "Sting representation: %s (len %lu) - too short\n",
957 ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr,
960 if (GNUNET_OK != ret)
962 LOG (GNUNET_ERROR_TYPE_WARNING,
963 "Not able to convert string representation of PeerID to PeerID\n"
964 "Sting representation: %s\n",
973 * @brief Restore the peers on disk to #valid_peers.
976 restore_valid_peers ()
980 struct GNUNET_DISK_FileHandle *fh;
985 const struct GNUNET_PeerIdentity *peer;
987 if (0 == strncmp ("DISABLE", filename_valid_peers, 7))
992 if (GNUNET_OK != GNUNET_DISK_file_test (filename_valid_peers))
996 fh = GNUNET_DISK_file_open (filename_valid_peers,
997 GNUNET_DISK_OPEN_READ,
998 GNUNET_DISK_PERM_NONE);
999 GNUNET_assert (NULL != fh);
1000 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size));
1001 num_peers = file_size / 53;
1002 buf = GNUNET_malloc (file_size);
1003 size_read = GNUNET_DISK_file_read (fh, buf, file_size);
1004 GNUNET_assert (size_read == file_size);
1005 LOG (GNUNET_ERROR_TYPE_DEBUG,
1006 "Restoring %" PRIu32 " peers from file `%s'\n",
1008 filename_valid_peers);
1009 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53)
1011 str_repr = GNUNET_strndup (iter_buf, 53);
1012 peer = s2i_full (str_repr);
1013 GNUNET_free (str_repr);
1014 add_valid_peer (peer);
1015 LOG (GNUNET_ERROR_TYPE_DEBUG,
1016 "Restored valid peer %s from disk\n",
1017 GNUNET_i2s_full (peer));
1021 LOG (GNUNET_ERROR_TYPE_DEBUG,
1022 "num_peers: %" PRIu32 ", _size (valid_peers): %u\n",
1024 GNUNET_CONTAINER_multipeermap_size (valid_peers));
1025 if (num_peers != GNUNET_CONTAINER_multipeermap_size (valid_peers))
1027 LOG (GNUNET_ERROR_TYPE_WARNING,
1028 "Number of restored peers does not match file size. Have probably duplicates.\n");
1030 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1031 LOG (GNUNET_ERROR_TYPE_DEBUG,
1032 "Restored %u valid peers from disk\n",
1033 GNUNET_CONTAINER_multipeermap_size (valid_peers));
1038 * @brief Initialise storage of peers
1040 * @param fn_valid_peers filename of the file used to store valid peer ids
1041 * @param cadet_h cadet handle
1042 * @param own_id own peer identity
1045 Peers_initialise (char* fn_valid_peers,
1046 struct GNUNET_CADET_Handle *cadet_h,
1047 const struct GNUNET_PeerIdentity *own_id)
1049 filename_valid_peers = GNUNET_strdup (fn_valid_peers);
1050 cadet_handle = cadet_h;
1051 own_identity = *own_id;
1052 peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
1053 valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
1054 restore_valid_peers ();
1059 * @brief Delete storage of peers that was created with #Peers_initialise ()
1064 if (GNUNET_SYSERR ==
1065 GNUNET_CONTAINER_multipeermap_iterate (peer_map,
1066 peermap_clear_iterator,
1069 LOG (GNUNET_ERROR_TYPE_WARNING,
1070 "Iteration destroying peers was aborted.\n");
1072 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1074 store_valid_peers ();
1075 GNUNET_free (filename_valid_peers);
1076 GNUNET_CONTAINER_multipeermap_destroy (valid_peers);
1081 * Iterator over #valid_peers hash map entries.
1083 * @param cls closure - unused
1084 * @param peer current peer id
1085 * @param value value in the hash map - unused
1086 * @return #GNUNET_YES if we should continue to
1088 * #GNUNET_NO if not.
1091 valid_peer_iterator (void *cls,
1092 const struct GNUNET_PeerIdentity *peer,
1095 struct PeersIteratorCls *it_cls = cls;
1097 return it_cls->iterator (it_cls->cls,
1103 * @brief Get all currently known, valid peer ids.
1105 * @param it function to call on each peer id
1106 * @param it_cls extra argument to @a it
1107 * @return the number of key value pairs processed,
1108 * #GNUNET_SYSERR if it aborted iteration
1111 Peers_get_valid_peers (PeersIterator iterator,
1114 struct PeersIteratorCls *cls;
1117 cls = GNUNET_new (struct PeersIteratorCls);
1118 cls->iterator = iterator;
1120 ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
1121 valid_peer_iterator,
1129 * @brief Add peer to known peers.
1131 * This function is called on new peer_ids from 'external' sources
1132 * (client seed, cadet get_peers(), ...)
1134 * @param peer the new #GNUNET_PeerIdentity
1136 * @return #GNUNET_YES if peer was inserted
1137 * #GNUNET_NO otherwise (if peer was already known or
1138 * peer was #own_identity)
1141 Peers_insert_peer (const struct GNUNET_PeerIdentity *peer)
1143 if ( (GNUNET_YES == Peers_check_peer_known (peer)) ||
1144 (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity)) )
1146 return GNUNET_NO; /* We already know this peer - nothing to do */
1148 (void) create_peer_ctx (peer);
1153 Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
1156 * @brief Try connecting to a peer to see whether it is online
1158 * If not known yet, insert into known peers
1160 * @param peer the peer whose liveliness is to be checked
1161 * @return #GNUNET_YES if peer had to be inserted
1162 * #GNUNET_NO otherwise (if peer was already known or
1163 * peer was #own_identity)
1166 Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer)
1168 struct PeerContext *peer_ctx;
1171 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity))
1175 ret = Peers_insert_peer (peer);
1176 peer_ctx = get_peer_ctx (peer);
1177 if (GNUNET_NO == Peers_check_peer_flag (peer, Peers_ONLINE))
1179 check_peer_live (peer_ctx);
1186 * @brief Check if peer is removable.
1189 * - a recv channel exists
1190 * - there are pending messages
1191 * - there is no pending pull reply
1193 * @param peer the peer in question
1194 * @return #GNUNET_YES if peer is removable
1195 * #GNUNET_NO if peer is NOT removable
1196 * #GNUNET_SYSERR if peer is not known
1199 Peers_check_removable (const struct GNUNET_PeerIdentity *peer)
1201 struct PeerContext *peer_ctx;
1203 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1205 return GNUNET_SYSERR;
1208 peer_ctx = get_peer_ctx (peer);
1209 if ( (NULL != peer_ctx->recv_channel) ||
1210 (NULL != peer_ctx->pending_messages_head) ||
1211 (GNUNET_NO == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)) )
1219 Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1220 enum Peers_ChannelRole role);
1223 Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags);
1226 * @brief Remove peer
1228 * @param peer the peer to clean
1229 * @return #GNUNET_YES if peer was removed
1230 * #GNUNET_NO otherwise
1233 Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1235 struct PeerContext *peer_ctx;
1236 uint32_t *channel_flag;
1238 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1243 peer_ctx = get_peer_ctx (peer);
1244 set_peer_flag (peer_ctx, Peers_TO_DESTROY);
1245 LOG (GNUNET_ERROR_TYPE_DEBUG,
1246 "Going to remove peer %s\n",
1247 GNUNET_i2s (&peer_ctx->peer_id));
1248 Peers_unset_peer_flag (peer, Peers_ONLINE);
1250 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
1251 while (NULL != peer_ctx->pending_messages_head)
1253 LOG (GNUNET_ERROR_TYPE_DEBUG,
1254 "Removing unsent %s\n",
1255 peer_ctx->pending_messages_head->type);
1256 /* Cancle pending message, too */
1257 remove_pending_message (peer_ctx->pending_messages_head, GNUNET_YES);
1259 /* If we are still waiting for notification whether this peer is live
1260 * cancel the according task */
1261 if (NULL != peer_ctx->liveliness_check_pending)
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "Removing pending liveliness check for peer %s\n",
1265 GNUNET_i2s (&peer_ctx->peer_id));
1266 // TODO wait until cadet sets mq->cancel_impl
1267 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
1268 GNUNET_free (peer_ctx->liveliness_check_pending);
1269 peer_ctx->liveliness_check_pending = NULL;
1271 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
1272 if (NULL != peer_ctx->send_channel &&
1273 GNUNET_YES != Peers_check_channel_flag (channel_flag, Peers_CHANNEL_DESTROING))
1275 LOG (GNUNET_ERROR_TYPE_DEBUG,
1276 "Destroying send channel\n");
1277 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1278 peer_ctx->send_channel = NULL;
1279 peer_ctx->mq = NULL;
1281 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
1282 if (NULL != peer_ctx->recv_channel &&
1283 GNUNET_YES != Peers_check_channel_flag (channel_flag, Peers_CHANNEL_DESTROING))
1285 LOG (GNUNET_ERROR_TYPE_DEBUG,
1286 "Destroying recv channel\n");
1287 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1288 peer_ctx->recv_channel = NULL;
1291 GNUNET_free (peer_ctx->send_channel_flags);
1292 GNUNET_free (peer_ctx->recv_channel_flags);
1294 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, &peer_ctx->peer_id))
1296 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1298 GNUNET_free (peer_ctx);
1304 * @brief set flags on a given peer.
1306 * @param peer the peer to set flags on
1307 * @param flags the flags
1310 Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1312 struct PeerContext *peer_ctx;
1314 peer_ctx = get_peer_ctx (peer);
1315 set_peer_flag (peer_ctx, flags);
1320 * @brief unset flags on a given peer.
1322 * @param peer the peer to unset flags on
1323 * @param flags the flags
1326 Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1328 struct PeerContext *peer_ctx;
1330 peer_ctx = get_peer_ctx (peer);
1331 unset_peer_flag (peer_ctx, flags);
1336 * @brief Check whether flags on a peer are set.
1338 * @param peer the peer to check the flag of
1339 * @param flags the flags to check
1341 * @return #GNUNET_SYSERR if peer is not known
1342 * #GNUNET_YES if all given flags are set
1343 * #GNUNET_NO otherwise
1346 Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1348 struct PeerContext *peer_ctx;
1350 if (GNUNET_NO == Peers_check_peer_known (peer))
1352 return GNUNET_SYSERR;
1354 peer_ctx = get_peer_ctx (peer);
1355 return check_peer_flag_set (peer_ctx, flags);
1360 * @brief set flags on a given channel.
1362 * @param channel the channel to set flags on
1363 * @param flags the flags
1366 Peers_set_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1368 set_channel_flag (channel_flags, flags);
1373 * @brief unset flags on a given channel.
1375 * @param channel the channel to unset flags on
1376 * @param flags the flags
1379 Peers_unset_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1381 unset_channel_flag (channel_flags, flags);
1386 * @brief Check whether flags on a channel are set.
1388 * @param channel the channel to check the flag of
1389 * @param flags the flags to check
1391 * @return #GNUNET_YES if all given flags are set
1392 * #GNUNET_NO otherwise
1395 Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1397 return check_channel_flag_set (channel_flags, flags);
1401 * @brief Get the flags for the channel in @a role for @a peer.
1403 * @param peer Peer to get the channel flags for.
1404 * @param role Role of channel to get flags for
1406 * @return The flags.
1409 Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1410 enum Peers_ChannelRole role)
1412 const struct PeerContext *peer_ctx;
1414 peer_ctx = get_peer_ctx (peer);
1415 if (Peers_CHANNEL_ROLE_SENDING == role)
1417 return peer_ctx->send_channel_flags;
1419 else if (Peers_CHANNEL_ROLE_RECEIVING == role)
1421 return peer_ctx->recv_channel_flags;
1430 * @brief Check whether we have information about the given peer.
1432 * FIXME probably deprecated. Make this the new _online.
1434 * @param peer peer in question
1436 * @return #GNUNET_YES if peer is known
1437 * #GNUNET_NO if peer is not knwon
1440 Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer)
1442 if (NULL != peer_map)
1444 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
1453 * @brief Check whether @a peer is actually a peer.
1455 * A valid peer is a peer that we know exists eg. we were connected to once.
1457 * @param peer peer in question
1459 * @return #GNUNET_YES if peer is valid
1460 * #GNUNET_NO if peer is not valid
1463 Peers_check_peer_valid (const struct GNUNET_PeerIdentity *peer)
1465 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer);
1470 * @brief Indicate that we want to send to the other peer
1472 * This establishes a sending channel
1474 * @param peer the peer to establish channel to
1477 Peers_indicate_sending_intention (const struct GNUNET_PeerIdentity *peer)
1479 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1480 (void) get_channel (peer);
1485 * @brief Check whether other peer has the intention to send/opened channel
1488 * @param peer the peer in question
1490 * @return #GNUNET_YES if peer has the intention to send
1491 * #GNUNET_NO otherwise
1494 Peers_check_peer_send_intention (const struct GNUNET_PeerIdentity *peer)
1496 const struct PeerContext *peer_ctx;
1498 peer_ctx = get_peer_ctx (peer);
1499 if (NULL != peer_ctx->recv_channel)
1508 * Handle the channel a peer opens to us.
1510 * @param cls The closure
1511 * @param channel The channel the peer wants to establish
1512 * @param initiator The peer's peer ID
1514 * @return initial channel context for the channel
1515 * (can be NULL -- that's not an error)
1518 Peers_handle_inbound_channel (void *cls,
1519 struct GNUNET_CADET_Channel *channel,
1520 const struct GNUNET_PeerIdentity *initiator)
1522 struct PeerContext *peer_ctx;
1523 struct GNUNET_PeerIdentity *ctx_peer;
1525 LOG (GNUNET_ERROR_TYPE_DEBUG,
1526 "New channel was established to us (Peer %s).\n",
1527 GNUNET_i2s (initiator));
1528 GNUNET_assert (NULL != channel); /* according to cadet API */
1529 /* Make sure we 'know' about this peer */
1530 peer_ctx = create_or_get_peer_ctx (initiator);
1531 set_peer_live (peer_ctx);
1532 ctx_peer = GNUNET_new (struct GNUNET_PeerIdentity);
1533 *ctx_peer = *initiator;
1534 /* We only accept one incoming channel per peer */
1535 if (GNUNET_YES == Peers_check_peer_send_intention (initiator))
1537 set_channel_flag (peer_ctx->recv_channel_flags,
1538 Peers_CHANNEL_ESTABLISHED_TWICE);
1539 //GNUNET_CADET_channel_destroy (channel);
1540 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1541 peer_ctx->recv_channel = channel;
1542 /* return the channel context */
1545 peer_ctx->recv_channel = channel;
1551 * @brief Check whether a sending channel towards the given peer exists
1553 * @param peer the peer to check for
1555 * @return #GNUNET_YES if a sending channel towards that peer exists
1556 * #GNUNET_NO otherwise
1559 Peers_check_sending_channel_exists (const struct GNUNET_PeerIdentity *peer)
1561 struct PeerContext *peer_ctx;
1563 if (GNUNET_NO == Peers_check_peer_known (peer))
1564 { /* If no such peer exists, there is no channel */
1567 peer_ctx = get_peer_ctx (peer);
1568 if (NULL == peer_ctx->send_channel)
1577 * @brief check whether the given channel is the sending channel of the given
1580 * @param peer the peer in question
1581 * @param channel the channel to check for
1582 * @param role either #Peers_CHANNEL_ROLE_SENDING, or
1583 * #Peers_CHANNEL_ROLE_RECEIVING
1585 * @return #GNUNET_YES if the given chennel is the sending channel of the peer
1586 * #GNUNET_NO otherwise
1589 Peers_check_channel_role (const struct GNUNET_PeerIdentity *peer,
1590 const struct GNUNET_CADET_Channel *channel,
1591 enum Peers_ChannelRole role)
1593 const struct PeerContext *peer_ctx;
1595 if (GNUNET_NO == Peers_check_peer_known (peer))
1599 peer_ctx = get_peer_ctx (peer);
1600 if ( (Peers_CHANNEL_ROLE_SENDING == role) &&
1601 (channel == peer_ctx->send_channel) )
1605 if ( (Peers_CHANNEL_ROLE_RECEIVING == role) &&
1606 (channel == peer_ctx->recv_channel) )
1615 * @brief Destroy the send channel of a peer e.g. stop indicating a sending
1616 * intention to another peer
1618 * If there is also no channel to receive messages from that peer, remove it
1622 * @peer the peer identity of the peer whose sending channel to destroy
1623 * @return #GNUNET_YES if channel was destroyed
1624 * #GNUNET_NO otherwise
1627 Peers_destroy_sending_channel (const struct GNUNET_PeerIdentity *peer)
1629 struct PeerContext *peer_ctx;
1631 if (GNUNET_NO == Peers_check_peer_known (peer))
1635 peer_ctx = get_peer_ctx (peer);
1636 if (NULL != peer_ctx->send_channel)
1638 set_channel_flag (peer_ctx->send_channel_flags, Peers_CHANNEL_CLEAN);
1639 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1640 peer_ctx->send_channel = NULL;
1641 peer_ctx->mq = NULL;
1642 (void) Peers_check_connected (peer);
1649 * This is called when a channel is destroyed.
1651 * @param cls The closure
1652 * @param channel The channel being closed
1655 Peers_cleanup_destroyed_channel (void *cls,
1656 const struct GNUNET_CADET_Channel *channel)
1658 struct GNUNET_PeerIdentity *peer = cls;
1659 struct PeerContext *peer_ctx;
1661 if (GNUNET_NO == Peers_check_peer_known (peer))
1662 {/* We don't want to implicitly create a context that we're about to kill */
1663 LOG (GNUNET_ERROR_TYPE_DEBUG,
1664 "channel (%s) without associated context was destroyed\n",
1668 peer_ctx = get_peer_ctx (peer);
1670 /* If our peer issued the destruction of the channel, the #Peers_TO_DESTROY
1671 * flag will be set. In this case simply make sure that the channels are
1673 /* FIXME This distinction seems to be redundant */
1674 if (Peers_check_peer_flag (peer, Peers_TO_DESTROY))
1675 {/* We initiatad the destruction of this particular peer */
1676 LOG (GNUNET_ERROR_TYPE_DEBUG,
1677 "Peer is in the process of being destroyed\n");
1678 if (channel == peer_ctx->send_channel)
1680 peer_ctx->send_channel = NULL;
1681 peer_ctx->mq = NULL;
1683 else if (channel == peer_ctx->recv_channel)
1685 peer_ctx->recv_channel = NULL;
1688 if (NULL != peer_ctx->send_channel)
1690 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1691 peer_ctx->send_channel = NULL;
1692 peer_ctx->mq = NULL;
1694 if (NULL != peer_ctx->recv_channel)
1696 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1697 peer_ctx->recv_channel = NULL;
1699 /* Set the #Peers_ONLINE flag accordingly */
1700 (void) Peers_check_connected (peer);
1705 { /* We did not initiate the destruction of this peer */
1706 LOG (GNUNET_ERROR_TYPE_DEBUG,
1707 "Peer is NOT in the process of being destroyed\n");
1708 if (channel == peer_ctx->send_channel)
1709 { /* Something (but us) killd the channel - clean up peer */
1710 LOG (GNUNET_ERROR_TYPE_DEBUG,
1711 "send channel (%s) was destroyed - cleaning up\n",
1713 peer_ctx->send_channel = NULL;
1714 peer_ctx->mq = NULL;
1716 else if (channel == peer_ctx->recv_channel)
1717 { /* Other peer doesn't want to send us messages anymore */
1718 LOG (GNUNET_ERROR_TYPE_DEBUG,
1719 "Peer %s destroyed recv channel - cleaning up channel\n",
1721 peer_ctx->recv_channel = NULL;
1725 LOG (GNUNET_ERROR_TYPE_WARNING,
1726 "unknown channel (%s) was destroyed\n",
1730 (void) Peers_check_connected (peer);
1734 * @brief Send a message to another peer.
1736 * Keeps track about pending messages so they can be properly removed when the
1737 * peer is destroyed.
1739 * @param peer receeiver of the message
1740 * @param ev envelope of the message
1741 * @param type type of the message
1744 Peers_send_message (const struct GNUNET_PeerIdentity *peer,
1745 struct GNUNET_MQ_Envelope *ev,
1748 struct PendingMessage *pending_msg;
1749 struct GNUNET_MQ_Handle *mq;
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1752 "Sending message to %s of type %s\n",
1755 pending_msg = insert_pending_message (peer, ev, type);
1757 GNUNET_MQ_notify_sent (ev,
1760 GNUNET_MQ_send (mq, ev);
1764 * @brief Schedule a operation on given peer
1766 * Avoids scheduling an operation twice.
1768 * @param peer the peer we want to schedule the operation for once it gets live
1770 * @return #GNUNET_YES if the operation was scheduled
1771 * #GNUNET_NO otherwise
1774 Peers_schedule_operation (const struct GNUNET_PeerIdentity *peer,
1775 const PeerOp peer_op)
1777 struct PeerPendingOp pending_op;
1778 struct PeerContext *peer_ctx;
1780 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity))
1784 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1786 //TODO if LIVE/ONLINE execute immediately
1788 if (GNUNET_NO == check_operation_scheduled (peer, peer_op))
1790 peer_ctx = get_peer_ctx (peer);
1791 pending_op.op = peer_op;
1792 pending_op.op_cls = NULL;
1793 GNUNET_array_append (peer_ctx->pending_ops,
1794 peer_ctx->num_pending_ops,
1802 * @brief Get the recv_channel of @a peer.
1803 * Needed to correctly handle (call #GNUNET_CADET_receive_done()) incoming
1806 * @param peer The peer to get the recv_channel from.
1808 * @return The recv_channel.
1810 struct GNUNET_CADET_Channel *
1811 Peers_get_recv_channel (const struct GNUNET_PeerIdentity *peer)
1813 struct PeerContext *peer_ctx;
1815 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1816 peer_ctx = get_peer_ctx (peer);
1817 return peer_ctx->recv_channel;
1819 /***********************************************************************
1820 * /Old gnunet-service-rps_peers.c
1821 ***********************************************************************/
1824 /***********************************************************************
1825 * Housekeeping with clients
1826 ***********************************************************************/
1829 * Closure used to pass the client and the id to the callback
1830 * that replies to a client's request
1837 struct ReplyCls *next;
1838 struct ReplyCls *prev;
1841 * The identifier of the request
1846 * The handle to the request
1848 struct RPS_SamplerRequestHandle *req_handle;
1851 * The client handle to send the reply to
1853 struct ClientContext *cli_ctx;
1858 * Struct used to store the context of a connected client.
1860 struct ClientContext
1865 struct ClientContext *next;
1866 struct ClientContext *prev;
1869 * The message queue to communicate with the client.
1871 struct GNUNET_MQ_Handle *mq;
1874 * DLL with handles to single requests from the client
1876 struct ReplyCls *rep_cls_head;
1877 struct ReplyCls *rep_cls_tail;
1880 * @brief How many updates this client expects to receive.
1882 int64_t view_updates_left;
1885 * The client handle to send the reply to
1887 struct GNUNET_SERVICE_Client *client;
1891 * DLL with all clients currently connected to us
1893 struct ClientContext *cli_ctx_head;
1894 struct ClientContext *cli_ctx_tail;
1896 /***********************************************************************
1897 * /Housekeeping with clients
1898 ***********************************************************************/
1904 /***********************************************************************
1906 ***********************************************************************/
1909 * Sampler used for the Brahms protocol itself.
1911 static struct RPS_Sampler *prot_sampler;
1914 * Sampler used for the clients.
1916 static struct RPS_Sampler *client_sampler;
1919 * Name to log view to
1921 static const char *file_name_view_log;
1925 * Name to log number of observed peers to
1927 static const char *file_name_observed_log;
1930 * @brief Count the observed peers
1932 static uint32_t num_observed_peers;
1935 * @brief Multipeermap (ab-) used to count unique peer_ids
1937 static struct GNUNET_CONTAINER_MultiPeerMap *observed_unique_peers;
1938 #endif /* TO_FILE */
1941 * The size of sampler we need to be able to satisfy the client's need
1944 static unsigned int sampler_size_client_need;
1947 * The size of sampler we need to be able to satisfy the Brahms protocol's
1948 * need of random peers.
1950 * This is one minimum size the sampler grows to.
1952 static unsigned int sampler_size_est_need;
1955 * @brief This is the minimum estimate used as sampler size.
1957 * It is configured by the user.
1959 static unsigned int sampler_size_est_min;
1962 * @brief This is the estimate used as view size.
1964 * It is initialised with the minimum
1966 static unsigned int view_size_est_need;
1969 * @brief This is the minimum estimate used as view size.
1971 * It is configured by the user.
1973 static unsigned int view_size_est_min;
1976 * Percentage of total peer number in the view
1977 * to send random PUSHes to
1982 * Percentage of total peer number in the view
1983 * to send random PULLs to
1988 * Identifier for the main task that runs periodically.
1990 static struct GNUNET_SCHEDULER_Task *do_round_task;
1993 * Time inverval the do_round task runs in.
1995 static struct GNUNET_TIME_Relative round_interval;
1998 * List to store peers received through pushes temporary.
2000 static struct CustomPeerMap *push_map;
2003 * List to store peers received through pulls temporary.
2005 static struct CustomPeerMap *pull_map;
2010 static struct GNUNET_NSE_Handle *nse;
2015 static struct GNUNET_CADET_Handle *cadet_handle;
2018 * @brief Port to communicate to other peers.
2020 static struct GNUNET_CADET_Port *cadet_port;
2023 * Handler to PEERINFO.
2025 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
2028 * Handle for cancellation of iteration over peers.
2030 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
2035 * Counts how many requets clients already issued.
2036 * Only needed in the beginning to check how many of the 64 deltas
2039 static unsigned int req_counter;
2042 * Time of the last request we received.
2044 * Used to compute the expected request rate.
2046 static struct GNUNET_TIME_Absolute last_request;
2049 * Size of #request_deltas.
2051 #define REQUEST_DELTAS_SIZE 64
2052 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
2055 * Last 64 deltas between requests
2057 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
2060 * The prediction of the rate of requests
2062 static struct GNUNET_TIME_Relative request_rate;
2065 #ifdef ENABLE_MALICIOUS
2067 * Type of malicious peer
2069 * 0 Don't act malicious at all - Default
2070 * 1 Try to maximise representation
2071 * 2 Try to partition the network
2074 static uint32_t mal_type;
2077 * Other malicious peers
2079 static struct GNUNET_PeerIdentity *mal_peers;
2082 * Hashmap of malicious peers used as set.
2083 * Used to more efficiently check whether we know that peer.
2085 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set;
2088 * Number of other malicious peers
2090 static uint32_t num_mal_peers;
2094 * If type is 2 This struct is used to store the attacked peers in a DLL
2101 struct AttackedPeer *next;
2102 struct AttackedPeer *prev;
2107 struct GNUNET_PeerIdentity peer_id;
2111 * If type is 2 this is the DLL of attacked peers
2113 static struct AttackedPeer *att_peers_head;
2114 static struct AttackedPeer *att_peers_tail;
2117 * This index is used to point to an attacked peer to
2118 * implement the round-robin-ish way to select attacked peers.
2120 static struct AttackedPeer *att_peer_index;
2123 * Hashmap of attacked peers used as set.
2124 * Used to more efficiently check whether we know that peer.
2126 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set;
2129 * Number of attacked peers
2131 static uint32_t num_attacked_peers;
2134 * If type is 1 this is the attacked peer
2136 static struct GNUNET_PeerIdentity attacked_peer;
2139 * The limit of PUSHes we can send in one round.
2140 * This is an assumption of the Brahms protocol and either implemented
2143 * assumend to be the bandwidth limitation.
2145 static uint32_t push_limit = 10000;
2146 #endif /* ENABLE_MALICIOUS */
2149 /***********************************************************************
2151 ***********************************************************************/
2154 /***********************************************************************
2156 ***********************************************************************/
2160 * Print peerlist to log.
2163 print_peer_list (struct GNUNET_PeerIdentity *list,
2168 LOG (GNUNET_ERROR_TYPE_DEBUG,
2169 "Printing peer list of length %u at %p:\n",
2172 for (i = 0 ; i < len ; i++)
2174 LOG (GNUNET_ERROR_TYPE_DEBUG,
2176 i, GNUNET_i2s (&list[i]));
2182 * Remove peer from list.
2185 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
2186 unsigned int *list_size,
2187 const struct GNUNET_PeerIdentity *peer)
2190 struct GNUNET_PeerIdentity *tmp;
2194 LOG (GNUNET_ERROR_TYPE_DEBUG,
2195 "Removing peer %s from list at %p\n",
2199 for ( i = 0 ; i < *list_size ; i++ )
2201 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
2203 if (i < *list_size -1)
2204 { /* Not at the last entry -- shift peers left */
2205 memmove (&tmp[i], &tmp[i +1],
2206 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
2208 /* Remove last entry (should be now useless PeerID) */
2209 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
2217 * Sum all time relatives of an array.
2219 static struct GNUNET_TIME_Relative
2220 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array,
2223 struct GNUNET_TIME_Relative sum;
2226 sum = GNUNET_TIME_UNIT_ZERO;
2227 for ( i = 0 ; i < arr_size ; i++ )
2229 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
2236 * Compute the average of given time relatives.
2238 static struct GNUNET_TIME_Relative
2239 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array,
2242 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array,
2249 * Insert PeerID in #view
2251 * Called once we know a peer is live.
2252 * Implements #PeerOp
2254 * @return GNUNET_OK if peer was actually inserted
2255 * GNUNET_NO if peer was not inserted
2258 insert_in_view_op (void *cls,
2259 const struct GNUNET_PeerIdentity *peer);
2262 * Insert PeerID in #view
2264 * Called once we know a peer is live.
2266 * @return GNUNET_OK if peer was actually inserted
2267 * GNUNET_NO if peer was not inserted
2270 insert_in_view (const struct GNUNET_PeerIdentity *peer)
2274 online = Peers_check_peer_flag (peer, Peers_ONLINE);
2275 if ( (GNUNET_NO == online) ||
2276 (GNUNET_SYSERR == online) ) /* peer is not even known */
2278 (void) Peers_issue_peer_liveliness_check (peer);
2279 (void) Peers_schedule_operation (peer, insert_in_view_op);
2282 /* Open channel towards peer to keep connection open */
2283 Peers_indicate_sending_intention (peer);
2284 return View_put (peer);
2288 * @brief sends updates to clients that are interested
2291 clients_notify_view_update (void);
2294 * Put random peer from sampler into the view as history update.
2297 hist_update (void *cls,
2298 struct GNUNET_PeerIdentity *ids,
2303 for (i = 0; i < num_peers; i++)
2305 (void) insert_in_view (&ids[i]);
2306 to_file (file_name_view_log,
2308 GNUNET_i2s_full (ids));
2310 clients_notify_view_update();
2315 * Wrapper around #RPS_sampler_resize()
2317 * If we do not have enough sampler elements, double current sampler size
2318 * If we have more than enough sampler elements, halv current sampler size
2321 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
2323 unsigned int sampler_size;
2326 // TODO respect the min, max
2327 sampler_size = RPS_sampler_get_size (sampler);
2328 if (sampler_size > new_size * 4)
2330 RPS_sampler_resize (sampler, sampler_size / 2);
2332 else if (sampler_size < new_size)
2334 RPS_sampler_resize (sampler, sampler_size * 2);
2336 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
2341 * Wrapper around #RPS_sampler_resize() resizing the client sampler
2344 client_resize_wrapper ()
2346 uint32_t bigger_size;
2350 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
2352 // TODO respect the min, max
2353 resize_wrapper (client_sampler, bigger_size);
2354 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
2360 * Estimate request rate
2362 * Called every time we receive a request from the client.
2367 struct GNUNET_TIME_Relative max_round_duration;
2369 if (request_deltas_size > req_counter)
2371 if ( 1 < req_counter)
2373 /* Shift last request deltas to the right */
2374 memmove (&request_deltas[1],
2376 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
2378 /* Add current delta to beginning */
2380 GNUNET_TIME_absolute_get_difference (last_request,
2381 GNUNET_TIME_absolute_get ());
2382 request_rate = T_relative_avg (request_deltas, req_counter);
2383 request_rate = (request_rate.rel_value_us < 1) ?
2384 GNUNET_TIME_relative_get_unit_ () : request_rate;
2386 /* Compute the duration a round will maximally take */
2387 max_round_duration =
2388 GNUNET_TIME_relative_add (round_interval,
2389 GNUNET_TIME_relative_divide (round_interval, 2));
2391 /* Set the estimated size the sampler has to have to
2392 * satisfy the current client request rate */
2393 sampler_size_client_need =
2394 max_round_duration.rel_value_us / request_rate.rel_value_us;
2396 /* Resize the sampler */
2397 client_resize_wrapper ();
2399 last_request = GNUNET_TIME_absolute_get ();
2404 * Add all peers in @a peer_array to @a peer_map used as set.
2406 * @param peer_array array containing the peers
2407 * @param num_peers number of peers in @peer_array
2408 * @param peer_map the peermap to use as set
2411 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
2412 unsigned int num_peers,
2413 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
2416 if (NULL == peer_map)
2418 LOG (GNUNET_ERROR_TYPE_WARNING,
2419 "Trying to add peers to non-existing peermap.\n");
2423 for (i = 0; i < num_peers; i++)
2425 GNUNET_CONTAINER_multipeermap_put (peer_map,
2428 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2434 * Send a PULL REPLY to @a peer_id
2436 * @param peer_id the peer to send the reply to.
2437 * @param peer_ids the peers to send to @a peer_id
2438 * @param num_peer_ids the number of peers to send to @a peer_id
2441 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
2442 const struct GNUNET_PeerIdentity *peer_ids,
2443 unsigned int num_peer_ids)
2446 struct GNUNET_MQ_Envelope *ev;
2447 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
2449 /* Compute actual size */
2450 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
2451 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
2453 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
2454 /* Compute number of peers to send
2455 * If too long, simply truncate */
2456 // TODO select random ones via permutation
2457 // or even better: do good protocol design
2459 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
2460 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
2461 sizeof (struct GNUNET_PeerIdentity);
2463 send_size = num_peer_ids;
2465 LOG (GNUNET_ERROR_TYPE_DEBUG,
2466 "Going to send PULL REPLY with %u peers to %s\n",
2467 send_size, GNUNET_i2s (peer_id));
2469 ev = GNUNET_MQ_msg_extra (out_msg,
2470 send_size * sizeof (struct GNUNET_PeerIdentity),
2471 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
2472 out_msg->num_peers = htonl (send_size);
2473 GNUNET_memcpy (&out_msg[1], peer_ids,
2474 send_size * sizeof (struct GNUNET_PeerIdentity));
2476 Peers_send_message (peer_id, ev, "PULL REPLY");
2477 GNUNET_STATISTICS_update(stats, "# pull reply send issued", 1, GNUNET_NO);
2482 * Insert PeerID in #pull_map
2484 * Called once we know a peer is live.
2487 insert_in_pull_map (void *cls,
2488 const struct GNUNET_PeerIdentity *peer)
2490 CustomPeerMap_put (pull_map, peer);
2495 * Insert PeerID in #view
2497 * Called once we know a peer is live.
2498 * Implements #PeerOp
2501 insert_in_view_op (void *cls,
2502 const struct GNUNET_PeerIdentity *peer)
2504 (void) insert_in_view (peer);
2509 * Update sampler with given PeerID.
2510 * Implements #PeerOp
2513 insert_in_sampler (void *cls,
2514 const struct GNUNET_PeerIdentity *peer)
2516 LOG (GNUNET_ERROR_TYPE_DEBUG,
2517 "Updating samplers with peer %s from insert_in_sampler()\n",
2519 RPS_sampler_update (prot_sampler, peer);
2520 RPS_sampler_update (client_sampler, peer);
2521 if (0 < RPS_sampler_count_id (prot_sampler, peer))
2523 /* Make sure we 'know' about this peer */
2524 (void) Peers_issue_peer_liveliness_check (peer);
2525 /* Establish a channel towards that peer to indicate we are going to send
2527 //Peers_indicate_sending_intention (peer);
2530 num_observed_peers++;
2531 GNUNET_CONTAINER_multipeermap_put
2532 (observed_unique_peers,
2535 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2536 uint32_t num_observed_unique_peers = GNUNET_CONTAINER_multipeermap_size (
2537 observed_unique_peers);
2538 to_file (file_name_observed_log,
2539 "%" PRIu32 " %" PRIu32 " %f\n",
2541 num_observed_unique_peers,
2542 1.0*num_observed_unique_peers/num_observed_peers)
2543 #endif /* TO_FILE */
2547 * @brief This is called on peers from external sources (cadet, peerinfo, ...)
2548 * If the peer is not known, liveliness check is issued and it is
2549 * scheduled to be inserted in sampler and view.
2551 * "External sources" refer to every source except the gossip.
2553 * @param peer peer to insert
2556 got_peer (const struct GNUNET_PeerIdentity *peer)
2558 /* If we did not know this peer already, insert it into sampler and view */
2559 if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer))
2561 Peers_schedule_operation (peer, insert_in_sampler);
2562 Peers_schedule_operation (peer, insert_in_view_op);
2567 * @brief Checks if there is a sending channel and if it is needed
2569 * @param peer the peer whose sending channel is checked
2570 * @return GNUNET_YES if sending channel exists and is still needed
2571 * GNUNET_NO otherwise
2574 check_sending_channel_needed (const struct GNUNET_PeerIdentity *peer)
2576 /* struct GNUNET_CADET_Channel *channel; */
2577 if (GNUNET_NO == Peers_check_peer_known (peer))
2581 if (GNUNET_YES == Peers_check_sending_channel_exists (peer))
2583 if ( (0 < RPS_sampler_count_id (prot_sampler, peer)) ||
2584 (GNUNET_YES == View_contains_peer (peer)) ||
2585 (GNUNET_YES == CustomPeerMap_contains_peer (push_map, peer)) ||
2586 (GNUNET_YES == CustomPeerMap_contains_peer (pull_map, peer)) ||
2587 (GNUNET_YES == Peers_check_peer_flag (peer, Peers_PULL_REPLY_PENDING)))
2588 { /* If we want to keep the connection to peer open */
2597 * @brief remove peer from our knowledge, the view, push and pull maps and
2600 * @param peer the peer to remove
2603 remove_peer (const struct GNUNET_PeerIdentity *peer)
2605 (void) View_remove_peer (peer);
2606 CustomPeerMap_remove_peer (pull_map, peer);
2607 CustomPeerMap_remove_peer (push_map, peer);
2608 RPS_sampler_reinitialise_by_value (prot_sampler, peer);
2609 RPS_sampler_reinitialise_by_value (client_sampler, peer);
2610 Peers_remove_peer (peer);
2615 * @brief Remove data that is not needed anymore.
2617 * If the sending channel is no longer needed it is destroyed.
2619 * @param peer the peer whose data is about to be cleaned
2622 clean_peer (const struct GNUNET_PeerIdentity *peer)
2624 if (GNUNET_NO == check_sending_channel_needed (peer))
2626 LOG (GNUNET_ERROR_TYPE_DEBUG,
2627 "Going to remove send channel to peer %s\n",
2629 #ifdef ENABLE_MALICIOUS
2630 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2631 (void) Peers_destroy_sending_channel (peer);
2632 #else /* ENABLE_MALICIOUS */
2633 (void) Peers_destroy_sending_channel (peer);
2634 #endif /* ENABLE_MALICIOUS */
2637 if ( (GNUNET_NO == Peers_check_peer_send_intention (peer)) &&
2638 (GNUNET_NO == View_contains_peer (peer)) &&
2639 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2640 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2641 (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2642 (0 == RPS_sampler_count_id (client_sampler, peer)) &&
2643 (GNUNET_NO != Peers_check_removable (peer)) )
2644 { /* We can safely remove this peer */
2645 LOG (GNUNET_ERROR_TYPE_DEBUG,
2646 "Going to remove peer %s\n",
2654 * @brief This is called when a channel is destroyed.
2656 * Removes peer completely from our knowledge if the send_channel was destroyed
2657 * Otherwise simply delete the recv_channel
2658 * Also check if the knowledge about this peer is still needed.
2659 * If not, remove this peer from our knowledge.
2661 * @param cls The closure
2662 * @param channel The channel being closed
2663 * @param channel_ctx The context associated with this channel
2666 cleanup_destroyed_channel (void *cls,
2667 const struct GNUNET_CADET_Channel *channel)
2669 struct GNUNET_PeerIdentity *peer = cls;
2670 uint32_t *channel_flag;
2671 struct PeerContext *peer_ctx;
2673 GNUNET_assert (NULL != peer);
2675 if (GNUNET_NO == Peers_check_peer_known (peer))
2676 { /* We don't know a context to that peer */
2677 LOG (GNUNET_ERROR_TYPE_WARNING,
2678 "channel (%s) without associated context was destroyed\n",
2684 peer_ctx = get_peer_ctx (peer);
2685 if (GNUNET_YES == Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
2687 LOG (GNUNET_ERROR_TYPE_DEBUG,
2688 "Callback on destruction of recv-channel was called (%s)\n",
2690 set_channel_flag (peer_ctx->recv_channel_flags, Peers_CHANNEL_DESTROING);
2691 } else if (GNUNET_YES == Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
2693 LOG (GNUNET_ERROR_TYPE_DEBUG,
2694 "Callback on destruction of send-channel was called (%s)\n",
2696 set_channel_flag (peer_ctx->send_channel_flags, Peers_CHANNEL_DESTROING);
2698 LOG (GNUNET_ERROR_TYPE_ERROR,
2699 "Channel to be destroyed has is neither sending nor receiving role\n");
2702 if (GNUNET_YES == Peers_check_peer_flag (peer, Peers_TO_DESTROY))
2703 { /* We are in the middle of removing that peer from our knowledge. In this
2704 case simply make sure that the channels are cleaned. */
2705 Peers_cleanup_destroyed_channel (cls, channel);
2706 to_file (file_name_view_log,
2707 "-%s\t(cleanup channel, ourself)",
2708 GNUNET_i2s_full (peer));
2714 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
2715 { /* Channel used for sending was destroyed */
2716 /* Possible causes of channel destruction:
2717 * - ourselves -> cleaning send channel -> clean context
2718 * - other peer -> peer probably went down -> remove
2720 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
2721 if (GNUNET_YES == Peers_check_channel_flag (channel_flag, Peers_CHANNEL_CLEAN))
2722 { /* We are about to clean the sending channel. Clean the respective
2724 Peers_cleanup_destroyed_channel (cls, channel);
2729 { /* Other peer destroyed our sending channel that he is supposed to keep
2730 * open. It probably went down. Remove it from our knowledge. */
2731 Peers_cleanup_destroyed_channel (cls, channel);
2737 else if (GNUNET_YES ==
2738 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
2739 { /* Channel used for receiving was destroyed */
2740 /* Possible causes of channel destruction:
2741 * - ourselves -> peer tried to establish channel twice -> clean context
2742 * - other peer -> peer doesn't want to send us data -> clean
2744 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
2746 Peers_check_channel_flag (channel_flag, Peers_CHANNEL_ESTABLISHED_TWICE))
2747 { /* Other peer tried to establish a channel to us twice. We do not accept
2748 * that. Clean the context. */
2749 Peers_cleanup_destroyed_channel (cls, channel);
2754 { /* Other peer doesn't want to send us data anymore. We are free to clean
2756 Peers_cleanup_destroyed_channel (cls, channel);
2764 LOG (GNUNET_ERROR_TYPE_WARNING,
2765 "Destroyed channel is neither sending nor receiving channel\n");
2770 /***********************************************************************
2772 ***********************************************************************/
2775 destroy_reply_cls (struct ReplyCls *rep_cls)
2777 struct ClientContext *cli_ctx;
2779 cli_ctx = rep_cls->cli_ctx;
2780 GNUNET_assert (NULL != cli_ctx);
2781 if (NULL != rep_cls->req_handle)
2783 RPS_sampler_request_cancel (rep_cls->req_handle);
2785 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
2786 cli_ctx->rep_cls_tail,
2788 GNUNET_free (rep_cls);
2793 destroy_cli_ctx (struct ClientContext *cli_ctx)
2795 GNUNET_assert (NULL != cli_ctx);
2796 if (NULL != cli_ctx->rep_cls_head)
2798 LOG (GNUNET_ERROR_TYPE_WARNING,
2799 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
2800 while (NULL != cli_ctx->rep_cls_head)
2801 destroy_reply_cls (cli_ctx->rep_cls_head);
2803 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
2806 GNUNET_free (cli_ctx);
2811 * Function called by NSE.
2813 * Updates sizes of sampler list and view and adapt those lists
2817 nse_callback (void *cls,
2818 struct GNUNET_TIME_Absolute timestamp,
2819 double logestimate, double std_dev)
2822 //double scale; // TODO this might go gloabal/config
2824 LOG (GNUNET_ERROR_TYPE_DEBUG,
2825 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
2826 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
2828 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
2829 // GNUNET_NSE_log_estimate_to_n (logestimate);
2830 estimate = pow (estimate, 1.0 / 3);
2831 // TODO add if std_dev is a number
2832 // estimate += (std_dev * scale);
2833 if (view_size_est_min < ceil (estimate))
2835 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
2836 sampler_size_est_need = estimate;
2837 view_size_est_need = estimate;
2840 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
2841 //sampler_size_est_need = view_size_est_min;
2842 view_size_est_need = view_size_est_min;
2845 /* If the NSE has changed adapt the lists accordingly */
2846 resize_wrapper (prot_sampler, sampler_size_est_need);
2847 client_resize_wrapper ();
2852 * Callback called once the requested PeerIDs are ready.
2854 * Sends those to the requesting client.
2857 client_respond (void *cls,
2858 struct GNUNET_PeerIdentity *peer_ids,
2861 struct ReplyCls *reply_cls = cls;
2863 struct GNUNET_MQ_Envelope *ev;
2864 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
2865 uint32_t size_needed;
2866 struct ClientContext *cli_ctx;
2868 GNUNET_assert (NULL != reply_cls);
2869 LOG (GNUNET_ERROR_TYPE_DEBUG,
2870 "sampler returned %" PRIu32 " peers:\n",
2872 for (i = 0; i < num_peers; i++)
2874 LOG (GNUNET_ERROR_TYPE_DEBUG,
2875 " %" PRIu32 ": %s\n",
2877 GNUNET_i2s (&peer_ids[i]));
2880 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
2881 num_peers * sizeof (struct GNUNET_PeerIdentity);
2883 GNUNET_assert (GNUNET_MAX_MESSAGE_SIZE >= size_needed);
2885 ev = GNUNET_MQ_msg_extra (out_msg,
2886 num_peers * sizeof (struct GNUNET_PeerIdentity),
2887 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
2888 out_msg->num_peers = htonl (num_peers);
2889 out_msg->id = htonl (reply_cls->id);
2891 GNUNET_memcpy (&out_msg[1],
2893 num_peers * sizeof (struct GNUNET_PeerIdentity));
2894 GNUNET_free (peer_ids);
2896 cli_ctx = reply_cls->cli_ctx;
2897 GNUNET_assert (NULL != cli_ctx);
2898 reply_cls->req_handle = NULL;
2899 destroy_reply_cls (reply_cls);
2900 GNUNET_MQ_send (cli_ctx->mq, ev);
2905 * Handle RPS request from the client.
2907 * @param cls closure
2908 * @param message the actual message
2911 handle_client_request (void *cls,
2912 const struct GNUNET_RPS_CS_RequestMessage *msg)
2914 struct ClientContext *cli_ctx = cls;
2916 uint32_t size_needed;
2917 struct ReplyCls *reply_cls;
2920 num_peers = ntohl (msg->num_peers);
2921 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
2922 num_peers * sizeof (struct GNUNET_PeerIdentity);
2924 if (GNUNET_MAX_MESSAGE_SIZE < size_needed)
2926 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2927 "Message received from client has size larger than expected\n");
2928 GNUNET_SERVICE_client_drop (cli_ctx->client);
2932 for (i = 0 ; i < num_peers ; i++)
2935 LOG (GNUNET_ERROR_TYPE_DEBUG,
2936 "Client requested %" PRIu32 " random peer(s).\n",
2939 reply_cls = GNUNET_new (struct ReplyCls);
2940 reply_cls->id = ntohl (msg->id);
2941 reply_cls->cli_ctx = cli_ctx;
2942 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
2947 GNUNET_assert (NULL != cli_ctx);
2948 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
2949 cli_ctx->rep_cls_tail,
2951 GNUNET_SERVICE_client_continue (cli_ctx->client);
2956 * @brief Handle a message that requests the cancellation of a request
2959 * @param message the message containing the id of the request
2962 handle_client_request_cancel (void *cls,
2963 const struct GNUNET_RPS_CS_RequestCancelMessage *msg)
2965 struct ClientContext *cli_ctx = cls;
2966 struct ReplyCls *rep_cls;
2968 GNUNET_assert (NULL != cli_ctx);
2969 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
2970 rep_cls = cli_ctx->rep_cls_head;
2971 LOG (GNUNET_ERROR_TYPE_DEBUG,
2972 "Client cancels request with id %" PRIu32 "\n",
2974 while ( (NULL != rep_cls->next) &&
2975 (rep_cls->id != ntohl (msg->id)) )
2976 rep_cls = rep_cls->next;
2977 GNUNET_assert (rep_cls->id == ntohl (msg->id));
2978 destroy_reply_cls (rep_cls);
2979 GNUNET_SERVICE_client_continue (cli_ctx->client);
2984 * @brief This function is called, when the client seeds peers.
2985 * It verifies that @a msg is well-formed.
2987 * @param cls the closure (#ClientContext)
2988 * @param msg the message
2989 * @return #GNUNET_OK if @a msg is well-formed
2992 check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
2994 struct ClientContext *cli_ctx = cls;
2995 uint16_t msize = ntohs (msg->header.size);
2996 uint32_t num_peers = ntohl (msg->num_peers);
2998 msize -= sizeof (struct GNUNET_RPS_CS_SeedMessage);
2999 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
3000 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
3003 GNUNET_SERVICE_client_drop (cli_ctx->client);
3004 return GNUNET_SYSERR;
3011 * Handle seed from the client.
3013 * @param cls closure
3014 * @param message the actual message
3017 handle_client_seed (void *cls,
3018 const struct GNUNET_RPS_CS_SeedMessage *msg)
3020 struct ClientContext *cli_ctx = cls;
3021 struct GNUNET_PeerIdentity *peers;
3025 num_peers = ntohl (msg->num_peers);
3026 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3027 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
3028 //GNUNET_memcpy (peers, &msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
3030 LOG (GNUNET_ERROR_TYPE_DEBUG,
3031 "Client seeded peers:\n");
3032 print_peer_list (peers, num_peers);
3034 for (i = 0; i < num_peers; i++)
3036 LOG (GNUNET_ERROR_TYPE_DEBUG,
3037 "Updating samplers with seed %" PRIu32 ": %s\n",
3039 GNUNET_i2s (&peers[i]));
3041 got_peer (&peers[i]);
3044 ////GNUNET_free (peers);
3046 GNUNET_SERVICE_client_continue (cli_ctx->client);
3050 * @brief Send view to client
3052 * @param cli_ctx the context of the client
3053 * @param view_array the peerids of the view as array (can be empty)
3054 * @param view_size the size of the view array (can be 0)
3057 send_view (const struct ClientContext *cli_ctx,
3058 const struct GNUNET_PeerIdentity *view_array,
3061 struct GNUNET_MQ_Envelope *ev;
3062 struct GNUNET_RPS_CS_DEBUG_ViewReply *out_msg;
3064 if (NULL == view_array)
3066 view_size = View_size ();
3067 view_array = View_get_as_array();
3070 ev = GNUNET_MQ_msg_extra (out_msg,
3071 view_size * sizeof (struct GNUNET_PeerIdentity),
3072 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY);
3073 out_msg->num_peers = htonl (view_size);
3075 GNUNET_memcpy (&out_msg[1],
3077 view_size * sizeof (struct GNUNET_PeerIdentity));
3078 GNUNET_MQ_send (cli_ctx->mq, ev);
3082 * @brief sends updates to clients that are interested
3085 clients_notify_view_update (void)
3087 struct ClientContext *cli_ctx_iter;
3089 const struct GNUNET_PeerIdentity *view_array;
3091 num_peers = View_size ();
3092 view_array = View_get_as_array();
3093 /* check size of view is small enough */
3094 if (GNUNET_MAX_MESSAGE_SIZE < num_peers)
3096 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3097 "View is too big to send\n");
3101 for (cli_ctx_iter = cli_ctx_head;
3102 NULL != cli_ctx_iter;
3103 cli_ctx_iter = cli_ctx_head->next)
3105 if (1 < cli_ctx_iter->view_updates_left)
3107 /* Client wants to receive limited amount of updates */
3108 cli_ctx_iter->view_updates_left -= 1;
3109 } else if (1 == cli_ctx_iter->view_updates_left)
3111 /* Last update of view for client */
3112 cli_ctx_iter->view_updates_left = -1;
3113 } else if (0 > cli_ctx_iter->view_updates_left) {
3114 /* Client is not interested in updates */
3117 /* else _updates_left == 0 - infinite amount of updates */
3120 send_view (cli_ctx_iter, view_array, num_peers);
3126 * Handle RPS request from the client.
3128 * @param cls closure
3129 * @param message the actual message
3132 handle_client_view_request (void *cls,
3133 const struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg)
3135 struct ClientContext *cli_ctx = cls;
3136 uint64_t num_updates;
3138 num_updates = ntohl (msg->num_updates);
3140 LOG (GNUNET_ERROR_TYPE_DEBUG,
3141 "Client requested %" PRIu64 " updates of view.\n",
3144 GNUNET_assert (NULL != cli_ctx);
3145 cli_ctx->view_updates_left = num_updates;
3146 send_view (cli_ctx, NULL, 0);
3147 GNUNET_SERVICE_client_continue (cli_ctx->client);
3151 * Handle a CHECK_LIVE message from another peer.
3153 * This does nothing. But without calling #GNUNET_CADET_receive_done()
3154 * the channel is blocked for all other communication.
3156 * @param cls Closure
3157 * @param msg The message header
3160 handle_peer_check (void *cls,
3161 const struct GNUNET_MessageHeader *msg)
3163 const struct GNUNET_PeerIdentity *peer = cls;
3164 LOG (GNUNET_ERROR_TYPE_DEBUG,
3165 "Received CHECK_LIVE (%s)\n", GNUNET_i2s (peer));
3167 GNUNET_break_op (Peers_check_peer_known (peer));
3168 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
3172 * Handle a PUSH message from another peer.
3174 * Check the proof of work and store the PeerID
3175 * in the temporary list for pushed PeerIDs.
3177 * @param cls Closure
3178 * @param msg The message header
3181 handle_peer_push (void *cls,
3182 const struct GNUNET_MessageHeader *msg)
3184 const struct GNUNET_PeerIdentity *peer = cls;
3186 // (check the proof of work (?))
3188 LOG (GNUNET_ERROR_TYPE_DEBUG,
3189 "Received PUSH (%s)\n",
3191 GNUNET_STATISTICS_update(stats, "# push message received", 1, GNUNET_NO);
3193 #ifdef ENABLE_MALICIOUS
3194 struct AttackedPeer *tmp_att_peer;
3196 if ( (1 == mal_type) ||
3198 { /* Try to maximise representation */
3199 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3200 tmp_att_peer->peer_id = *peer;
3201 if (NULL == att_peer_set)
3202 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
3204 GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3207 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3210 add_peer_array_to_set (peer, 1, att_peer_set);
3215 else if (2 == mal_type)
3217 /* We attack one single well-known peer - simply ignore */
3219 #endif /* ENABLE_MALICIOUS */
3221 /* Add the sending peer to the push_map */
3222 CustomPeerMap_put (push_map, peer);
3224 GNUNET_break_op (Peers_check_peer_known (peer));
3225 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
3230 * Handle PULL REQUEST request message from another peer.
3232 * Reply with the view of PeerIDs.
3234 * @param cls Closure
3235 * @param msg The message header
3238 handle_peer_pull_request (void *cls,
3239 const struct GNUNET_MessageHeader *msg)
3241 struct GNUNET_PeerIdentity *peer = cls;
3242 const struct GNUNET_PeerIdentity *view_array;
3244 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
3245 GNUNET_STATISTICS_update(stats, "# pull request message received", 1, GNUNET_NO);
3247 #ifdef ENABLE_MALICIOUS
3250 { /* Try to maximise representation */
3251 send_pull_reply (peer, mal_peers, num_mal_peers);
3254 else if (2 == mal_type)
3255 { /* Try to partition network */
3256 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
3258 send_pull_reply (peer, mal_peers, num_mal_peers);
3261 #endif /* ENABLE_MALICIOUS */
3263 GNUNET_break_op (Peers_check_peer_known (peer));
3264 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
3265 view_array = View_get_as_array ();
3266 send_pull_reply (peer, view_array, View_size ());
3271 * Check whether we sent a corresponding request and
3272 * whether this reply is the first one.
3274 * @param cls Closure
3275 * @param msg The message header
3278 check_peer_pull_reply (void *cls,
3279 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3281 struct GNUNET_PeerIdentity *sender = cls;
3283 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
3285 GNUNET_break_op (0);
3286 return GNUNET_SYSERR;
3289 if ((ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
3290 sizeof (struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
3292 LOG (GNUNET_ERROR_TYPE_ERROR,
3293 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3294 ntohl (msg->num_peers),
3295 (ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
3296 sizeof (struct GNUNET_PeerIdentity));
3297 GNUNET_break_op (0);
3298 return GNUNET_SYSERR;
3301 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
3303 LOG (GNUNET_ERROR_TYPE_WARNING,
3304 "Received a pull reply from a peer we didn't request one from!\n");
3305 GNUNET_break_op (0);
3306 return GNUNET_SYSERR;
3312 * Handle PULL REPLY message from another peer.
3314 * @param cls Closure
3315 * @param msg The message header
3318 handle_peer_pull_reply (void *cls,
3319 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3321 const struct GNUNET_PeerIdentity *peers;
3322 struct GNUNET_PeerIdentity *sender = cls;
3324 #ifdef ENABLE_MALICIOUS
3325 struct AttackedPeer *tmp_att_peer;
3326 #endif /* ENABLE_MALICIOUS */
3328 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
3329 GNUNET_STATISTICS_update(stats, "# pull reply messages received", 1, GNUNET_NO);
3331 #ifdef ENABLE_MALICIOUS
3332 // We shouldn't even receive pull replies as we're not sending
3336 #endif /* ENABLE_MALICIOUS */
3338 /* Do actual logic */
3339 peers = (const struct GNUNET_PeerIdentity *) &msg[1];
3341 LOG (GNUNET_ERROR_TYPE_DEBUG,
3342 "PULL REPLY received, got following %u peers:\n",
3343 ntohl (msg->num_peers));
3345 for (i = 0; i < ntohl (msg->num_peers); i++)
3347 LOG (GNUNET_ERROR_TYPE_DEBUG,
3350 GNUNET_i2s (&peers[i]));
3352 #ifdef ENABLE_MALICIOUS
3353 if ((NULL != att_peer_set) &&
3354 (1 == mal_type || 3 == mal_type))
3355 { /* Add attacked peer to local list */
3356 // TODO check if we sent a request and this was the first reply
3357 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3359 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
3361 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
3364 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3365 tmp_att_peer->peer_id = peers[i];
3366 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3369 add_peer_array_to_set (&peers[i], 1, att_peer_set);
3373 #endif /* ENABLE_MALICIOUS */
3374 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
3377 /* Make sure we 'know' about this peer */
3378 (void) Peers_insert_peer (&peers[i]);
3380 if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
3382 CustomPeerMap_put (pull_map, &peers[i]);
3386 Peers_schedule_operation (&peers[i], insert_in_pull_map);
3387 (void) Peers_issue_peer_liveliness_check (&peers[i]);
3392 Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING);
3393 clean_peer (sender);
3395 GNUNET_break_op (Peers_check_peer_known (sender));
3396 GNUNET_CADET_receive_done (Peers_get_recv_channel (sender));
3401 * Compute a random delay.
3402 * A uniformly distributed value between mean + spread and mean - spread.
3404 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
3405 * It would return a random value between 2 and 6 min.
3407 * @param mean the mean
3408 * @param spread the inverse amount of deviation from the mean
3410 static struct GNUNET_TIME_Relative
3411 compute_rand_delay (struct GNUNET_TIME_Relative mean,
3412 unsigned int spread)
3414 struct GNUNET_TIME_Relative half_interval;
3415 struct GNUNET_TIME_Relative ret;
3416 unsigned int rand_delay;
3417 unsigned int max_rand_delay;
3421 LOG (GNUNET_ERROR_TYPE_WARNING,
3422 "Not accepting spread of 0\n");
3426 GNUNET_assert (0 != mean.rel_value_us);
3428 /* Compute random time value between spread * mean and spread * mean */
3429 half_interval = GNUNET_TIME_relative_divide (mean, spread);
3431 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
3433 * Compute random value between (0 and 1) * round_interval
3434 * via multiplying round_interval with a 'fraction' (0 to value)/value
3436 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
3437 ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay);
3438 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
3439 ret = GNUNET_TIME_relative_add (ret, half_interval);
3441 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
3442 LOG (GNUNET_ERROR_TYPE_WARNING,
3443 "Returning FOREVER_REL\n");
3450 * Send single pull request
3452 * @param peer_id the peer to send the pull request to.
3455 send_pull_request (const struct GNUNET_PeerIdentity *peer)
3457 struct GNUNET_MQ_Envelope *ev;
3459 GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer,
3460 Peers_PULL_REPLY_PENDING));
3461 Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING);
3463 LOG (GNUNET_ERROR_TYPE_DEBUG,
3464 "Going to send PULL REQUEST to peer %s.\n",
3467 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
3468 Peers_send_message (peer, ev, "PULL REQUEST");
3469 GNUNET_STATISTICS_update(stats, "# pull request send issued", 1, GNUNET_NO);
3476 * @param peer_id the peer to send the push to.
3479 send_push (const struct GNUNET_PeerIdentity *peer_id)
3481 struct GNUNET_MQ_Envelope *ev;
3483 LOG (GNUNET_ERROR_TYPE_DEBUG,
3484 "Going to send PUSH to peer %s.\n",
3485 GNUNET_i2s (peer_id));
3487 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
3488 Peers_send_message (peer_id, ev, "PUSH");
3489 GNUNET_STATISTICS_update(stats, "# push send issued", 1, GNUNET_NO);
3494 do_round (void *cls);
3497 do_mal_round (void *cls);
3499 #ifdef ENABLE_MALICIOUS
3503 * @brief This function is called, when the client tells us to act malicious.
3504 * It verifies that @a msg is well-formed.
3506 * @param cls the closure (#ClientContext)
3507 * @param msg the message
3508 * @return #GNUNET_OK if @a msg is well-formed
3511 check_client_act_malicious (void *cls,
3512 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
3514 struct ClientContext *cli_ctx = cls;
3515 uint16_t msize = ntohs (msg->header.size);
3516 uint32_t num_peers = ntohl (msg->num_peers);
3518 msize -= sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage);
3519 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
3520 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
3522 LOG (GNUNET_ERROR_TYPE_ERROR,
3523 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3524 ntohl (msg->num_peers),
3525 (msize / sizeof (struct GNUNET_PeerIdentity)));
3527 GNUNET_SERVICE_client_drop (cli_ctx->client);
3528 return GNUNET_SYSERR;
3534 * Turn RPS service to act malicious.
3536 * @param cls Closure
3537 * @param client The client that sent the message
3538 * @param msg The message header
3541 handle_client_act_malicious (void *cls,
3542 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
3544 struct ClientContext *cli_ctx = cls;
3545 struct GNUNET_PeerIdentity *peers;
3546 uint32_t num_mal_peers_sent;
3547 uint32_t num_mal_peers_old;
3549 /* Do actual logic */
3550 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3551 mal_type = ntohl (msg->type);
3552 if (NULL == mal_peer_set)
3553 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
3555 LOG (GNUNET_ERROR_TYPE_DEBUG,
3556 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
3558 ntohl (msg->num_peers));
3561 { /* Try to maximise representation */
3562 /* Add other malicious peers to those we already know */
3564 num_mal_peers_sent = ntohl (msg->num_peers);
3565 num_mal_peers_old = num_mal_peers;
3566 GNUNET_array_grow (mal_peers,
3568 num_mal_peers + num_mal_peers_sent);
3569 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
3571 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
3573 /* Add all mal peers to mal_peer_set */
3574 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
3578 /* Substitute do_round () with do_mal_round () */
3579 GNUNET_SCHEDULER_cancel (do_round_task);
3580 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
3583 else if ( (2 == mal_type) ||
3585 { /* Try to partition the network */
3586 /* Add other malicious peers to those we already know */
3588 num_mal_peers_sent = ntohl (msg->num_peers) - 1;
3589 num_mal_peers_old = num_mal_peers;
3590 GNUNET_array_grow (mal_peers,
3592 num_mal_peers + num_mal_peers_sent);
3593 if (NULL != mal_peers &&
3596 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
3598 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
3600 /* Add all mal peers to mal_peer_set */
3601 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
3606 /* Store the one attacked peer */
3607 GNUNET_memcpy (&attacked_peer,
3608 &msg->attacked_peer,
3609 sizeof (struct GNUNET_PeerIdentity));
3610 /* Set the flag of the attacked peer to valid to avoid problems */
3611 if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
3613 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3616 LOG (GNUNET_ERROR_TYPE_DEBUG,
3617 "Attacked peer is %s\n",
3618 GNUNET_i2s (&attacked_peer));
3620 /* Substitute do_round () with do_mal_round () */
3621 GNUNET_SCHEDULER_cancel (do_round_task);
3622 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
3624 else if (0 == mal_type)
3625 { /* Stop acting malicious */
3626 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
3628 /* Substitute do_mal_round () with do_round () */
3629 GNUNET_SCHEDULER_cancel (do_round_task);
3630 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
3635 GNUNET_SERVICE_client_continue (cli_ctx->client);
3637 GNUNET_SERVICE_client_continue (cli_ctx->client);
3642 * Send out PUSHes and PULLs maliciously.
3644 * This is executed regylary.
3647 do_mal_round (void *cls)
3649 uint32_t num_pushes;
3651 struct GNUNET_TIME_Relative time_next_round;
3652 struct AttackedPeer *tmp_att_peer;
3654 LOG (GNUNET_ERROR_TYPE_DEBUG,
3655 "Going to execute next round maliciously type %" PRIu32 ".\n",
3657 do_round_task = NULL;
3658 GNUNET_assert (mal_type <= 3);
3659 /* Do malicious actions */
3661 { /* Try to maximise representation */
3663 /* The maximum of pushes we're going to send this round */
3664 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
3665 num_attacked_peers),
3666 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
3668 LOG (GNUNET_ERROR_TYPE_DEBUG,
3669 "Going to send %" PRIu32 " pushes\n",
3672 /* Send PUSHes to attacked peers */
3673 for (i = 0 ; i < num_pushes ; i++)
3675 if (att_peers_tail == att_peer_index)
3676 att_peer_index = att_peers_head;
3678 att_peer_index = att_peer_index->next;
3680 send_push (&att_peer_index->peer_id);
3683 /* Send PULLs to some peers to learn about additional peers to attack */
3684 tmp_att_peer = att_peer_index;
3685 for (i = 0 ; i < num_pushes * alpha ; i++)
3687 if (att_peers_tail == tmp_att_peer)
3688 tmp_att_peer = att_peers_head;
3690 att_peer_index = tmp_att_peer->next;
3692 send_pull_request (&tmp_att_peer->peer_id);
3697 else if (2 == mal_type)
3699 * Try to partition the network
3700 * Send as many pushes to the attacked peer as possible
3701 * That is one push per round as it will ignore more.
3703 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3704 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
3705 send_push (&attacked_peer);
3710 { /* Combined attack */
3712 /* Send PUSH to attacked peers */
3713 if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
3715 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3716 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
3718 LOG (GNUNET_ERROR_TYPE_DEBUG,
3719 "Goding to send push to attacked peer (%s)\n",
3720 GNUNET_i2s (&attacked_peer));
3721 send_push (&attacked_peer);
3724 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3726 /* The maximum of pushes we're going to send this round */
3727 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
3728 num_attacked_peers),
3729 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
3731 LOG (GNUNET_ERROR_TYPE_DEBUG,
3732 "Going to send %" PRIu32 " pushes\n",
3735 for (i = 0; i < num_pushes; i++)
3737 if (att_peers_tail == att_peer_index)
3738 att_peer_index = att_peers_head;
3740 att_peer_index = att_peer_index->next;
3742 send_push (&att_peer_index->peer_id);
3745 /* Send PULLs to some peers to learn about additional peers to attack */
3746 tmp_att_peer = att_peer_index;
3747 for (i = 0; i < num_pushes * alpha; i++)
3749 if (att_peers_tail == tmp_att_peer)
3750 tmp_att_peer = att_peers_head;
3752 att_peer_index = tmp_att_peer->next;
3754 send_pull_request (&tmp_att_peer->peer_id);
3758 /* Schedule next round */
3759 time_next_round = compute_rand_delay (round_interval, 2);
3761 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
3763 GNUNET_assert (NULL == do_round_task);
3764 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
3765 &do_mal_round, NULL);
3766 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
3768 #endif /* ENABLE_MALICIOUS */
3771 * Send out PUSHes and PULLs, possibly update #view, samplers.
3773 * This is executed regylary.
3776 do_round (void *cls)
3779 const struct GNUNET_PeerIdentity *view_array;
3780 unsigned int *permut;
3781 unsigned int a_peers; /* Number of peers we send pushes to */
3782 unsigned int b_peers; /* Number of peers we send pull requests to */
3783 uint32_t first_border;
3784 uint32_t second_border;
3785 struct GNUNET_PeerIdentity peer;
3786 struct GNUNET_PeerIdentity *update_peer;
3788 LOG (GNUNET_ERROR_TYPE_DEBUG,
3789 "Going to execute next round.\n");
3790 GNUNET_STATISTICS_update(stats, "# rounds", 1, GNUNET_NO);
3791 do_round_task = NULL;
3792 LOG (GNUNET_ERROR_TYPE_DEBUG,
3793 "Printing view:\n");
3794 to_file (file_name_view_log,
3795 "___ new round ___");
3796 view_array = View_get_as_array ();
3797 for (i = 0; i < View_size (); i++)
3799 LOG (GNUNET_ERROR_TYPE_DEBUG,
3800 "\t%s\n", GNUNET_i2s (&view_array[i]));
3801 to_file (file_name_view_log,
3803 GNUNET_i2s_full (&view_array[i]));
3807 /* Send pushes and pull requests */
3808 if (0 < View_size ())
3810 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3814 a_peers = ceil (alpha * View_size ());
3816 LOG (GNUNET_ERROR_TYPE_DEBUG,
3817 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
3818 a_peers, alpha, View_size ());
3819 for (i = 0; i < a_peers; i++)
3821 peer = view_array[permut[i]];
3822 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
3823 { // FIXME if this fails schedule/loop this for later
3828 /* Send PULL requests */
3829 b_peers = ceil (beta * View_size ());
3830 first_border = a_peers;
3831 second_border = a_peers + b_peers;
3832 if (second_border > View_size ())
3834 first_border = View_size () - b_peers;
3835 second_border = View_size ();
3837 LOG (GNUNET_ERROR_TYPE_DEBUG,
3838 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
3839 b_peers, beta, View_size ());
3840 for (i = first_border; i < second_border; i++)
3842 peer = view_array[permut[i]];
3843 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
3844 GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
3845 { // FIXME if this fails schedule/loop this for later
3846 send_pull_request (&peer);
3850 GNUNET_free (permut);
3856 /* TODO see how many peers are in push-/pull- list! */
3858 if ((CustomPeerMap_size (push_map) <= alpha * view_size_est_need) &&
3859 (0 < CustomPeerMap_size (push_map)) &&
3860 (0 < CustomPeerMap_size (pull_map)))
3861 //if (GNUNET_YES) // disable blocking temporarily
3862 { /* If conditions for update are fulfilled, update */
3863 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
3865 uint32_t final_size;
3866 uint32_t peers_to_clean_size;
3867 struct GNUNET_PeerIdentity *peers_to_clean;
3869 peers_to_clean = NULL;
3870 peers_to_clean_size = 0;
3871 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
3872 GNUNET_memcpy (peers_to_clean,
3874 View_size () * sizeof (struct GNUNET_PeerIdentity));
3876 /* Seems like recreating is the easiest way of emptying the peermap */
3878 to_file (file_name_view_log,
3881 first_border = GNUNET_MIN (ceil (alpha * view_size_est_need),
3882 CustomPeerMap_size (push_map));
3883 second_border = first_border +
3884 GNUNET_MIN (floor (beta * view_size_est_need),
3885 CustomPeerMap_size (pull_map));
3886 final_size = second_border +
3887 ceil ((1 - (alpha + beta)) * view_size_est_need);
3888 LOG (GNUNET_ERROR_TYPE_DEBUG,
3889 "first border: %" PRIu32 ", second border: %" PRIu32 ", final size: %"PRIu32 "\n",
3894 /* Update view with peers received through PUSHes */
3895 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3896 CustomPeerMap_size (push_map));
3897 for (i = 0; i < first_border; i++)
3899 (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
3901 to_file (file_name_view_log,
3903 GNUNET_i2s_full (&view_array[i]));
3904 // TODO change the peer_flags accordingly
3906 GNUNET_free (permut);
3909 /* Update view with peers received through PULLs */
3910 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3911 CustomPeerMap_size (pull_map));
3912 for (i = first_border; i < second_border; i++)
3914 (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
3915 permut[i - first_border]));
3916 to_file (file_name_view_log,
3918 GNUNET_i2s_full (&view_array[i]));
3919 // TODO change the peer_flags accordingly
3921 GNUNET_free (permut);
3924 /* Update view with peers from history */
3925 RPS_sampler_get_n_rand_peers (prot_sampler,
3928 final_size - second_border);
3929 // TODO change the peer_flags accordingly
3931 for (i = 0; i < View_size (); i++)
3932 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
3934 /* Clean peers that were removed from the view */
3935 for (i = 0; i < peers_to_clean_size; i++)
3937 to_file (file_name_view_log,
3939 GNUNET_i2s_full (&peers_to_clean[i]));
3940 clean_peer (&peers_to_clean[i]);
3941 //peer_destroy_channel_send (sender);
3944 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
3945 clients_notify_view_update();
3947 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
3948 GNUNET_STATISTICS_update(stats, "# rounds blocked", 1, GNUNET_NO);
3949 if (CustomPeerMap_size (push_map) > alpha * View_size () &&
3950 !(0 >= CustomPeerMap_size (pull_map)))
3951 GNUNET_STATISTICS_update(stats, "# rounds blocked - too many pushes", 1, GNUNET_NO);
3952 if (CustomPeerMap_size (push_map) > alpha * View_size () &&
3953 (0 >= CustomPeerMap_size (pull_map)))
3954 GNUNET_STATISTICS_update(stats, "# rounds blocked - too many pushes, no pull replies", 1, GNUNET_NO);
3955 if (0 >= CustomPeerMap_size (push_map) &&
3956 !(0 >= CustomPeerMap_size (pull_map)))
3957 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pushes", 1, GNUNET_NO);
3958 if (0 >= CustomPeerMap_size (push_map) &&
3959 (0 >= CustomPeerMap_size (pull_map)))
3960 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pushes, no pull replies", 1, GNUNET_NO);
3961 if (0 >= CustomPeerMap_size (pull_map) &&
3962 CustomPeerMap_size (push_map) > alpha * View_size () &&
3963 0 >= CustomPeerMap_size (push_map))
3964 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pull replies", 1, GNUNET_NO);
3966 // TODO independent of that also get some peers from CADET_get_peers()?
3967 GNUNET_STATISTICS_set (stats,
3968 "# peers in push map at end of round",
3969 CustomPeerMap_size (push_map),
3971 GNUNET_STATISTICS_set (stats,
3972 "# peers in pull map at end of round",
3973 CustomPeerMap_size (pull_map),
3975 GNUNET_STATISTICS_set (stats,
3976 "# peers in view at end of round",
3980 LOG (GNUNET_ERROR_TYPE_DEBUG,
3981 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
3982 CustomPeerMap_size (push_map),
3983 CustomPeerMap_size (pull_map),
3986 alpha * View_size ());
3988 /* Update samplers */
3989 for (i = 0; i < CustomPeerMap_size (push_map); i++)
3991 update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
3992 LOG (GNUNET_ERROR_TYPE_DEBUG,
3993 "Updating with peer %s from push list\n",
3994 GNUNET_i2s (update_peer));
3995 insert_in_sampler (NULL, update_peer);
3996 clean_peer (update_peer); /* This cleans only if it is not in the view */
3997 //peer_destroy_channel_send (sender);
4000 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
4002 LOG (GNUNET_ERROR_TYPE_DEBUG,
4003 "Updating with peer %s from pull list\n",
4004 GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
4005 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
4006 /* This cleans only if it is not in the view */
4007 clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
4008 //peer_destroy_channel_send (sender);
4012 /* Empty push/pull lists */
4013 CustomPeerMap_clear (push_map);
4014 CustomPeerMap_clear (pull_map);
4016 struct GNUNET_TIME_Relative time_next_round;
4018 time_next_round = compute_rand_delay (round_interval, 2);
4020 /* Schedule next round */
4021 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
4023 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
4028 * This is called from GNUNET_CADET_get_peers().
4030 * It is called on every peer(ID) that cadet somehow has contact with.
4031 * We use those to initialise the sampler.
4034 init_peer_cb (void *cls,
4035 const struct GNUNET_PeerIdentity *peer,
4036 int tunnel, // "Do we have a tunnel towards this peer?"
4037 unsigned int n_paths, // "Number of known paths towards this peer"
4038 unsigned int best_path) // "How long is the best path?
4039 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
4043 LOG (GNUNET_ERROR_TYPE_DEBUG,
4044 "Got peer_id %s from cadet\n",
4051 * @brief Iterator function over stored, valid peers.
4053 * We initialise the sampler with those.
4055 * @param cls the closure
4056 * @param peer the peer id
4057 * @return #GNUNET_YES if we should continue to
4059 * #GNUNET_NO if not.
4062 valid_peers_iterator (void *cls,
4063 const struct GNUNET_PeerIdentity *peer)
4067 LOG (GNUNET_ERROR_TYPE_DEBUG,
4068 "Got stored, valid peer %s\n",
4077 * Iterator over peers from peerinfo.
4079 * @param cls closure
4080 * @param peer id of the peer, NULL for last call
4081 * @param hello hello message for the peer (can be NULL)
4082 * @param error message
4085 process_peerinfo_peers (void *cls,
4086 const struct GNUNET_PeerIdentity *peer,
4087 const struct GNUNET_HELLO_Message *hello,
4088 const char *err_msg)
4092 LOG (GNUNET_ERROR_TYPE_DEBUG,
4093 "Got peer_id %s from peerinfo\n",
4101 * Task run during shutdown.
4106 shutdown_task (void *cls)
4108 struct ClientContext *client_ctx;
4109 struct ReplyCls *reply_cls;
4111 LOG (GNUNET_ERROR_TYPE_DEBUG,
4112 "RPS is going down\n");
4114 /* Clean all clients */
4115 for (client_ctx = cli_ctx_head;
4116 NULL != cli_ctx_head;
4117 client_ctx = cli_ctx_head)
4119 /* Clean pending requests to the sampler */
4120 for (reply_cls = client_ctx->rep_cls_head;
4121 NULL != client_ctx->rep_cls_head;
4122 reply_cls = client_ctx->rep_cls_head)
4124 RPS_sampler_request_cancel (reply_cls->req_handle);
4125 GNUNET_CONTAINER_DLL_remove (client_ctx->rep_cls_head,
4126 client_ctx->rep_cls_tail,
4128 GNUNET_free (reply_cls);
4130 GNUNET_CONTAINER_DLL_remove (cli_ctx_head, cli_ctx_tail, client_ctx);
4131 GNUNET_free (client_ctx);
4133 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
4134 GNUNET_PEERINFO_disconnect (peerinfo_handle);
4136 if (NULL != do_round_task)
4138 GNUNET_SCHEDULER_cancel (do_round_task);
4139 do_round_task = NULL;
4144 GNUNET_NSE_disconnect (nse);
4145 RPS_sampler_destroy (prot_sampler);
4146 RPS_sampler_destroy (client_sampler);
4147 GNUNET_CADET_close_port (cadet_port);
4148 GNUNET_CADET_disconnect (cadet_handle);
4150 CustomPeerMap_destroy (push_map);
4151 CustomPeerMap_destroy (pull_map);
4154 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4157 #ifdef ENABLE_MALICIOUS
4158 struct AttackedPeer *tmp_att_peer;
4159 /* it is ok to free this const during shutdown: */
4160 GNUNET_free ((char *) file_name_view_log);
4162 GNUNET_free ((char *) file_name_observed_log);
4163 GNUNET_CONTAINER_multipeermap_destroy (observed_unique_peers);
4164 #endif /* TO_FILE */
4165 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
4166 if (NULL != mal_peer_set)
4167 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
4168 if (NULL != att_peer_set)
4169 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
4170 while (NULL != att_peers_head)
4172 tmp_att_peer = att_peers_head;
4173 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
4175 #endif /* ENABLE_MALICIOUS */
4180 * Handle client connecting to the service.
4183 * @param client the new client
4184 * @param mq the message queue of @a client
4188 client_connect_cb (void *cls,
4189 struct GNUNET_SERVICE_Client *client,
4190 struct GNUNET_MQ_Handle *mq)
4192 struct ClientContext *cli_ctx;
4194 LOG (GNUNET_ERROR_TYPE_DEBUG,
4195 "Client connected\n");
4197 return client; /* Server was destroyed before a client connected. Shutting down */
4198 cli_ctx = GNUNET_new (struct ClientContext);
4199 cli_ctx->mq = GNUNET_SERVICE_client_get_mq (client);
4200 cli_ctx->view_updates_left = -1;
4201 cli_ctx->client = client;
4202 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
4209 * Callback called when a client disconnected from the service
4211 * @param cls closure for the service
4212 * @param c the client that disconnected
4213 * @param internal_cls should be equal to @a c
4216 client_disconnect_cb (void *cls,
4217 struct GNUNET_SERVICE_Client *client,
4220 struct ClientContext *cli_ctx = internal_cls;
4222 GNUNET_assert (client == cli_ctx->client);
4224 {/* shutdown task - destroy all clients */
4225 while (NULL != cli_ctx_head)
4226 destroy_cli_ctx (cli_ctx_head);
4229 { /* destroy this client */
4230 LOG (GNUNET_ERROR_TYPE_DEBUG,
4231 "Client disconnected. Destroy its context.\n");
4232 destroy_cli_ctx (cli_ctx);
4238 * Handle random peer sampling clients.
4240 * @param cls closure
4241 * @param c configuration to use
4242 * @param service the initialized service
4246 const struct GNUNET_CONFIGURATION_Handle *c,
4247 struct GNUNET_SERVICE_Handle *service)
4249 char* fn_valid_peers;
4250 struct GNUNET_HashCode port;
4252 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
4257 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
4258 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4259 "STARTING SERVICE (rps) for peer [%s]\n",
4260 GNUNET_i2s (&own_identity));
4261 #ifdef ENABLE_MALICIOUS
4262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4263 "Malicious execution compiled in.\n");
4264 #endif /* ENABLE_MALICIOUS */
4268 /* Get time interval from the configuration */
4269 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
4273 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4274 "RPS", "ROUNDINTERVAL");
4275 GNUNET_SCHEDULER_shutdown ();
4279 /* Get initial size of sampler/view from the configuration */
4281 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "MINSIZE",
4282 (long long unsigned int *) &sampler_size_est_min))
4284 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4286 GNUNET_SCHEDULER_shutdown ();
4289 sampler_size_est_need = sampler_size_est_min;
4290 view_size_est_min = sampler_size_est_min;
4291 LOG (GNUNET_ERROR_TYPE_DEBUG, "MINSIZE is %u\n", sampler_size_est_min);
4294 GNUNET_CONFIGURATION_get_value_filename (cfg,
4296 "FILENAME_VALID_PEERS",
4299 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4300 "rps", "FILENAME_VALID_PEERS");
4304 View_create (view_size_est_min);
4306 /* file_name_view_log */
4307 file_name_view_log = store_prefix_file_name (&own_identity, "view");
4309 file_name_observed_log = store_prefix_file_name (&own_identity, "observed");
4310 observed_unique_peers = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
4311 #endif /* TO_FILE */
4313 /* connect to NSE */
4314 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
4321 /* Initialise cadet */
4322 /* There exists a copy-paste-clone in get_channel() */
4323 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
4324 GNUNET_MQ_hd_fixed_size (peer_check,
4325 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
4326 struct GNUNET_MessageHeader,
4328 GNUNET_MQ_hd_fixed_size (peer_push,
4329 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
4330 struct GNUNET_MessageHeader,
4332 GNUNET_MQ_hd_fixed_size (peer_pull_request,
4333 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
4334 struct GNUNET_MessageHeader,
4336 GNUNET_MQ_hd_var_size (peer_pull_reply,
4337 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
4338 struct GNUNET_RPS_P2P_PullReplyMessage,
4340 GNUNET_MQ_handler_end ()
4343 cadet_handle = GNUNET_CADET_connect (cfg);
4344 GNUNET_assert (NULL != cadet_handle);
4345 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
4346 strlen (GNUNET_APPLICATION_PORT_RPS),
4348 cadet_port = GNUNET_CADET_open_port (cadet_handle,
4350 &Peers_handle_inbound_channel, /* Connect handler */
4352 NULL, /* WindowSize handler */
4353 cleanup_destroyed_channel, /* Disconnect handler */
4357 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
4358 Peers_initialise (fn_valid_peers, cadet_handle, &own_identity);
4359 GNUNET_free (fn_valid_peers);
4361 /* Initialise sampler */
4362 struct GNUNET_TIME_Relative half_round_interval;
4363 struct GNUNET_TIME_Relative max_round_interval;
4365 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
4366 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
4368 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
4369 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
4371 /* Initialise push and pull maps */
4372 push_map = CustomPeerMap_create (4);
4373 pull_map = CustomPeerMap_create (4);
4376 //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
4377 //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
4378 // TODO send push/pull to each of those peers?
4379 // TODO read stored valid peers from last run
4380 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
4381 Peers_get_valid_peers (valid_peers_iterator, NULL);
4383 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
4385 process_peerinfo_peers,
4388 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
4390 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
4391 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
4393 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
4394 stats = GNUNET_STATISTICS_create ("rps", cfg);
4400 * Define "main" method using service macro.
4404 GNUNET_SERVICE_OPTION_NONE,
4407 &client_disconnect_cb,
4409 GNUNET_MQ_hd_fixed_size (client_request,
4410 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
4411 struct GNUNET_RPS_CS_RequestMessage,
4413 GNUNET_MQ_hd_fixed_size (client_request_cancel,
4414 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
4415 struct GNUNET_RPS_CS_RequestCancelMessage,
4417 GNUNET_MQ_hd_var_size (client_seed,
4418 GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
4419 struct GNUNET_RPS_CS_SeedMessage,
4421 #ifdef ENABLE_MALICIOUS
4422 GNUNET_MQ_hd_var_size (client_act_malicious,
4423 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
4424 struct GNUNET_RPS_CS_ActMaliciousMessage,
4426 #endif /* ENABLE_MALICIOUS */
4427 GNUNET_MQ_hd_fixed_size (client_view_request,
4428 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST,
4429 struct GNUNET_RPS_CS_DEBUG_ViewRequest,
4431 GNUNET_MQ_handler_end());
4433 /* end of gnunet-service-rps.c */