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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file rps/gnunet-service-rps.c
23 * @brief rps service implementation
24 * @author Julius Bünger
27 #include "gnunet_applications.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_cadet_service.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet_nse_service.h"
32 #include "gnunet_statistics_service.h"
34 #include "rps-test_util.h"
35 #include "gnunet-service-rps_sampler.h"
36 #include "gnunet-service-rps_custommap.h"
37 #include "gnunet-service-rps_view.h"
42 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
44 // TODO modify @brief in every file
46 // TODO check for overflows
48 // TODO align message structs
50 // TODO connect to friends
52 // TODO store peers somewhere persistent
54 // TODO blacklist? (-> mal peer detection on top of brahms)
56 // hist_size_init, hist_size_max
61 static const struct GNUNET_CONFIGURATION_Handle *cfg;
64 * Handle to the statistics service.
66 static struct GNUNET_STATISTICS_Handle *stats;
71 static struct GNUNET_PeerIdentity own_identity;
75 /***********************************************************************
76 * Old gnunet-service-rps_peers.c
77 ***********************************************************************/
80 * Set a peer flag of given peer context.
82 #define set_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask))
85 * Get peer flag of given peer context.
87 #define check_peer_flag_set(peer_ctx, mask)\
88 ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
91 * Unset flag of given peer context.
93 #define unset_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask))
96 * Set a channel flag of given channel context.
98 #define set_channel_flag(channel_flags, mask) ((*channel_flags) |= (mask))
101 * Get channel flag of given channel context.
103 #define check_channel_flag_set(channel_flags, mask)\
104 ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
107 * Unset flag of given channel context.
109 #define unset_channel_flag(channel_flags, mask) ((*channel_flags) &= ~(mask))
114 * Pending operation on peer consisting of callback and closure
116 * When an operation cannot be executed right now this struct is used to store
117 * the callback and closure for later execution.
133 * List containing all messages that are yet to be send
135 * This is used to keep track of all messages that have not been sent yet. When
136 * a peer is to be removed the pending messages can be removed properly.
138 struct PendingMessage
143 struct PendingMessage *next;
144 struct PendingMessage *prev;
147 * The envelope to the corresponding message
149 struct GNUNET_MQ_Envelope *ev;
152 * The corresponding context
154 struct PeerContext *peer_ctx;
163 * Struct used to keep track of other peer's status
165 * This is stored in a multipeermap.
166 * It contains information such as cadet channels, a message queue for sending,
167 * status about the channels, the pending operations on this peer and some flags
168 * about the status of the peer itself. (live, valid, ...)
173 * Message queue open to client
175 struct GNUNET_MQ_Handle *mq;
178 * Channel open to client.
180 struct GNUNET_CADET_Channel *send_channel;
183 * Flags to the sending channel
185 uint32_t *send_channel_flags;
188 * Channel open from client.
190 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
193 * Flags to the receiving channel
195 uint32_t *recv_channel_flags;
198 * Array of pending operations on this peer.
200 struct PeerPendingOp *pending_ops;
203 * Handle to the callback given to cadet_ntfy_tmt_rdy()
205 * To be canceled on shutdown.
207 struct PendingMessage *liveliness_check_pending;
210 * Number of pending operations.
212 unsigned int num_pending_ops;
215 * Identity of the peer
217 struct GNUNET_PeerIdentity peer_id;
220 * Flags indicating status of peer
225 * Last time we received something from that peer.
227 struct GNUNET_TIME_Absolute last_message_recv;
230 * Last time we received a keepalive message.
232 struct GNUNET_TIME_Absolute last_keepalive;
235 * DLL with all messages that are yet to be sent
237 struct PendingMessage *pending_messages_head;
238 struct PendingMessage *pending_messages_tail;
241 * This is pobably followed by 'statistical' data (when we first saw
242 * him, how did we get his ID, how many pushes (in a timeinterval),
248 * @brief Closure to #valid_peer_iterator
250 struct PeersIteratorCls
255 PeersIterator iterator;
258 * Closure to iterator
264 * @brief Hashmap of valid peers.
266 static struct GNUNET_CONTAINER_MultiPeerMap *valid_peers;
269 * @brief Maximum number of valid peers to keep.
270 * TODO read from config
272 static uint32_t num_valid_peers_max = UINT32_MAX;
275 * @brief Filename of the file that stores the valid peers persistently.
277 static char *filename_valid_peers;
280 * Set of all peers to keep track of them.
282 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
287 static struct GNUNET_CADET_Handle *cadet_handle;
292 * @brief Get the #PeerContext associated with a peer
294 * @param peer the peer id
296 * @return the #PeerContext
298 static struct PeerContext *
299 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
301 struct PeerContext *ctx;
304 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
305 GNUNET_assert (GNUNET_YES == ret);
306 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
307 GNUNET_assert (NULL != ctx);
312 Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer);
315 * @brief Create a new #PeerContext and insert it into the peer map
317 * @param peer the peer to create the #PeerContext for
319 * @return the #PeerContext
321 static struct PeerContext *
322 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
324 struct PeerContext *ctx;
327 GNUNET_assert (GNUNET_NO == Peers_check_peer_known (peer));
329 ctx = GNUNET_new (struct PeerContext);
330 ctx->peer_id = *peer;
331 ctx->send_channel_flags = GNUNET_new (uint32_t);
332 ctx->recv_channel_flags = GNUNET_new (uint32_t);
333 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
334 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
335 GNUNET_assert (GNUNET_OK == ret);
341 * @brief Create or get a #PeerContext
343 * @param peer the peer to get the associated context to
345 * @return the context
347 static struct PeerContext *
348 create_or_get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
350 if (GNUNET_NO == Peers_check_peer_known (peer))
352 return create_peer_ctx (peer);
354 return get_peer_ctx (peer);
358 Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
361 Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
364 * @brief Check whether we have a connection to this @a peer
366 * Also sets the #Peers_ONLINE flag accordingly
368 * @param peer the peer in question
370 * @return #GNUNET_YES if we are connected
371 * #GNUNET_NO otherwise
374 Peers_check_connected (const struct GNUNET_PeerIdentity *peer)
376 const struct PeerContext *peer_ctx;
378 /* If we don't know about this peer we don't know whether it's online */
379 if (GNUNET_NO == Peers_check_peer_known (peer))
383 /* Get the context */
384 peer_ctx = get_peer_ctx (peer);
385 /* If we have no channel to this peer we don't know whether it's online */
386 if ( (NULL == peer_ctx->send_channel) &&
387 (NULL == peer_ctx->recv_channel) )
389 Peers_unset_peer_flag (peer, Peers_ONLINE);
392 /* Otherwise (if we have a channel, we know that it's online */
393 Peers_set_peer_flag (peer, Peers_ONLINE);
399 * @brief The closure to #get_rand_peer_iterator.
401 struct GetRandPeerIteratorCls
404 * @brief The index of the peer to return.
405 * Will be decreased until 0.
406 * Then current peer is returned.
411 * @brief Pointer to peer to return.
413 const struct GNUNET_PeerIdentity *peer;
418 * @brief Iterator function for #get_random_peer_from_peermap.
420 * Implements #GNUNET_CONTAINER_PeerMapIterator.
421 * Decreases the index until the index is null.
422 * Then returns the current peer.
424 * @param cls the #GetRandPeerIteratorCls containing index and peer
425 * @param peer current peer
426 * @param value unused
428 * @return #GNUNET_YES if we should continue to
433 get_rand_peer_iterator (void *cls,
434 const struct GNUNET_PeerIdentity *peer,
437 struct GetRandPeerIteratorCls *iterator_cls = cls;
438 if (0 >= iterator_cls->index)
440 iterator_cls->peer = peer;
443 iterator_cls->index--;
449 * @brief Get a random peer from @a peer_map
451 * @param peer_map the peer_map to get the peer from
453 * @return a random peer
455 static const struct GNUNET_PeerIdentity *
456 get_random_peer_from_peermap (const struct
457 GNUNET_CONTAINER_MultiPeerMap *peer_map)
459 struct GetRandPeerIteratorCls *iterator_cls;
460 const struct GNUNET_PeerIdentity *ret;
462 iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls);
463 iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
464 GNUNET_CONTAINER_multipeermap_size (peer_map));
465 (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
466 get_rand_peer_iterator,
468 ret = iterator_cls->peer;
469 GNUNET_free (iterator_cls);
475 * @brief Add a given @a peer to valid peers.
477 * If valid peers are already #num_valid_peers_max, delete a peer previously.
479 * @param peer the peer that is added to the valid peers.
481 * @return #GNUNET_YES if no other peer had to be removed
482 * #GNUNET_NO otherwise
485 add_valid_peer (const struct GNUNET_PeerIdentity *peer)
487 const struct GNUNET_PeerIdentity *rand_peer;
491 while (GNUNET_CONTAINER_multipeermap_size (valid_peers) >= num_valid_peers_max)
493 rand_peer = get_random_peer_from_peermap (valid_peers);
494 GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer);
497 (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL,
498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
504 * @brief Set the peer flag to living and
505 * call the pending operations on this peer.
507 * Also adds peer to #valid_peers.
509 * @param peer_ctx the #PeerContext of the peer to set live
512 set_peer_live (struct PeerContext *peer_ctx)
514 struct GNUNET_PeerIdentity *peer;
517 peer = &peer_ctx->peer_id;
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Peer %s is live and valid, calling %i pending operations on it\n",
521 peer_ctx->num_pending_ops);
523 if (NULL != peer_ctx->liveliness_check_pending)
525 LOG (GNUNET_ERROR_TYPE_DEBUG,
526 "Removing pending liveliness check for peer %s\n",
527 GNUNET_i2s (&peer_ctx->peer_id));
528 // TODO wait until cadet sets mq->cancel_impl
529 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
530 GNUNET_free (peer_ctx->liveliness_check_pending);
531 peer_ctx->liveliness_check_pending = NULL;
534 (void) add_valid_peer (peer);
535 set_peer_flag (peer_ctx, Peers_ONLINE);
537 /* Call pending operations */
538 for (i = 0; i < peer_ctx->num_pending_ops; i++)
540 peer_ctx->pending_ops[i].op (peer_ctx->pending_ops[i].op_cls, peer);
542 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
546 cleanup_destroyed_channel (void *cls,
547 const struct GNUNET_CADET_Channel *channel);
549 /* Declaration of handlers */
551 handle_peer_check (void *cls,
552 const struct GNUNET_MessageHeader *msg);
555 handle_peer_push (void *cls,
556 const struct GNUNET_MessageHeader *msg);
559 handle_peer_pull_request (void *cls,
560 const struct GNUNET_MessageHeader *msg);
563 check_peer_pull_reply (void *cls,
564 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
567 handle_peer_pull_reply (void *cls,
568 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
570 /* End declaration of handlers */
574 * @brief Get the channel of a peer. If not existing, create.
576 * @param peer the peer id
577 * @return the #GNUNET_CADET_Channel used to send data to @a peer
579 struct GNUNET_CADET_Channel *
580 get_channel (const struct GNUNET_PeerIdentity *peer)
582 struct PeerContext *peer_ctx;
583 struct GNUNET_HashCode port;
584 struct GNUNET_PeerIdentity *ctx_peer;
585 /* There exists a copy-paste-clone in run() */
586 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
587 GNUNET_MQ_hd_fixed_size (peer_check,
588 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
589 struct GNUNET_MessageHeader,
591 GNUNET_MQ_hd_fixed_size (peer_push,
592 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
593 struct GNUNET_MessageHeader,
595 GNUNET_MQ_hd_fixed_size (peer_pull_request,
596 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
597 struct GNUNET_MessageHeader,
599 GNUNET_MQ_hd_var_size (peer_pull_reply,
600 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
601 struct GNUNET_RPS_P2P_PullReplyMessage,
603 GNUNET_MQ_handler_end ()
607 peer_ctx = get_peer_ctx (peer);
608 if (NULL == peer_ctx->send_channel)
610 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 "Trying to establish channel to peer %s\n",
613 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
614 strlen (GNUNET_APPLICATION_PORT_RPS),
616 ctx_peer = GNUNET_new (struct GNUNET_PeerIdentity);
618 peer_ctx->send_channel =
619 GNUNET_CADET_channel_create (cadet_handle,
620 (struct GNUNET_PeerIdentity *) ctx_peer, /* context */
623 GNUNET_CADET_OPTION_RELIABLE,
624 NULL, /* WindowSize handler */
625 cleanup_destroyed_channel, /* Disconnect handler */
628 GNUNET_assert (NULL != peer_ctx->send_channel);
629 return peer_ctx->send_channel;
634 * Get the message queue (#GNUNET_MQ_Handle) of a specific peer.
636 * If we already have a message queue open to this client,
637 * simply return it, otherways create one.
639 * @param peer the peer to get the mq to
640 * @return the #GNUNET_MQ_Handle
642 static struct GNUNET_MQ_Handle *
643 get_mq (const struct GNUNET_PeerIdentity *peer)
645 struct PeerContext *peer_ctx;
647 peer_ctx = get_peer_ctx (peer);
649 if (NULL == peer_ctx->mq)
651 peer_ctx->mq = GNUNET_CADET_get_mq (get_channel (peer));
658 * @brief This is called in response to the first message we sent as a
661 * @param cls #PeerContext of peer with pending liveliness check
664 mq_liveliness_check_successful (void *cls)
666 struct PeerContext *peer_ctx = cls;
668 if (NULL != peer_ctx->liveliness_check_pending)
670 LOG (GNUNET_ERROR_TYPE_DEBUG,
671 "Liveliness check for peer %s was successfull\n",
672 GNUNET_i2s (&peer_ctx->peer_id));
673 GNUNET_free (peer_ctx->liveliness_check_pending);
674 peer_ctx->liveliness_check_pending = NULL;
675 set_peer_live (peer_ctx);
680 * Issue a check whether peer is live
682 * @param peer_ctx the context of the peer
685 check_peer_live (struct PeerContext *peer_ctx)
687 LOG (GNUNET_ERROR_TYPE_DEBUG,
688 "Get informed about peer %s getting live\n",
689 GNUNET_i2s (&peer_ctx->peer_id));
691 struct GNUNET_MQ_Handle *mq;
692 struct GNUNET_MQ_Envelope *ev;
694 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE);
695 peer_ctx->liveliness_check_pending = GNUNET_new (struct PendingMessage);
696 peer_ctx->liveliness_check_pending->ev = ev;
697 peer_ctx->liveliness_check_pending->peer_ctx = peer_ctx;
698 peer_ctx->liveliness_check_pending->type = "Check liveliness";
699 mq = get_mq (&peer_ctx->peer_id);
700 GNUNET_MQ_notify_sent (ev,
701 mq_liveliness_check_successful,
703 GNUNET_MQ_send (mq, ev);
707 * @brief Add an envelope to a message passed to mq to list of pending messages
709 * @param peer peer the message was sent to
710 * @param ev envelope to the message
711 * @param type type of the message to be sent
712 * @return pointer to pending message
714 static struct PendingMessage *
715 insert_pending_message (const struct GNUNET_PeerIdentity *peer,
716 struct GNUNET_MQ_Envelope *ev,
719 struct PendingMessage *pending_msg;
720 struct PeerContext *peer_ctx;
722 peer_ctx = get_peer_ctx (peer);
723 pending_msg = GNUNET_new (struct PendingMessage);
724 pending_msg->ev = ev;
725 pending_msg->peer_ctx = peer_ctx;
726 pending_msg->type = type;
727 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
728 peer_ctx->pending_messages_tail,
735 * @brief Remove a pending message from the respective DLL
737 * @param pending_msg the pending message to remove
738 * @param cancel cancel the pending message, too
741 remove_pending_message (struct PendingMessage *pending_msg, int cancel)
743 struct PeerContext *peer_ctx;
745 peer_ctx = pending_msg->peer_ctx;
746 GNUNET_assert (NULL != peer_ctx);
747 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
748 peer_ctx->pending_messages_tail,
750 // TODO wait for the cadet implementation of message cancellation
751 //if (GNUNET_YES == cancel)
753 // GNUNET_MQ_send_cancel (pending_msg->ev);
755 GNUNET_free (pending_msg);
760 * @brief Check whether function of type #PeerOp was already scheduled
762 * The array with pending operations will probably never grow really big, so
763 * iterating over it should be ok.
765 * @param peer the peer to check
766 * @param peer_op the operation (#PeerOp) on the peer
768 * @return #GNUNET_YES if this operation is scheduled on that peer
769 * #GNUNET_NO otherwise
772 check_operation_scheduled (const struct GNUNET_PeerIdentity *peer,
773 const PeerOp peer_op)
775 const struct PeerContext *peer_ctx;
778 peer_ctx = get_peer_ctx (peer);
779 for (i = 0; i < peer_ctx->num_pending_ops; i++)
780 if (peer_op == peer_ctx->pending_ops[i].op)
786 Peers_remove_peer (const struct GNUNET_PeerIdentity *peer);
789 * Iterator over hash map entries. Deletes all contexts of peers.
792 * @param key current public key
793 * @param value value in the hash map
794 * @return #GNUNET_YES if we should continue to iterate,
798 peermap_clear_iterator (void *cls,
799 const struct GNUNET_PeerIdentity *key,
802 Peers_remove_peer (key);
808 * @brief This is called once a message is sent.
810 * Removes the pending message
812 * @param cls type of the message that was sent
815 mq_notify_sent_cb (void *cls)
817 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
821 if (0 == strncmp ("PULL REPLY", pending_msg->type, 10))
822 GNUNET_STATISTICS_update(stats, "# pull replys sent", 1, GNUNET_NO);
823 if (0 == strncmp ("PULL REQUEST", pending_msg->type, 12))
824 GNUNET_STATISTICS_update(stats, "# pull requests sent", 1, GNUNET_NO);
825 if (0 == strncmp ("PUSH", pending_msg->type, 4))
826 GNUNET_STATISTICS_update(stats, "# pushes sent", 1, GNUNET_NO);
827 /* Do not cancle message */
828 remove_pending_message (pending_msg, GNUNET_NO);
833 * @brief Iterator function for #store_valid_peers.
835 * Implements #GNUNET_CONTAINER_PeerMapIterator.
836 * Writes single peer to disk.
838 * @param cls the file handle to write to.
839 * @param peer current peer
840 * @param value unused
842 * @return #GNUNET_YES if we should continue to
847 store_peer_presistently_iterator (void *cls,
848 const struct GNUNET_PeerIdentity *peer,
851 const struct GNUNET_DISK_FileHandle *fh = cls;
852 char peer_string[128];
860 size = GNUNET_snprintf (peer_string,
861 sizeof (peer_string),
863 GNUNET_i2s_full (peer));
864 GNUNET_assert (53 == size);
865 ret = GNUNET_DISK_file_write (fh,
868 GNUNET_assert (size == ret);
874 * @brief Store the peers currently in #valid_peers to disk.
879 struct GNUNET_DISK_FileHandle *fh;
880 uint32_t number_written_peers;
883 if (0 == strncmp ("DISABLE", filename_valid_peers, 7))
888 ret = GNUNET_DISK_directory_create_for_file (filename_valid_peers);
889 if (GNUNET_SYSERR == ret)
891 LOG (GNUNET_ERROR_TYPE_WARNING,
892 "Not able to create directory for file `%s'\n",
893 filename_valid_peers);
896 else if (GNUNET_NO == ret)
898 LOG (GNUNET_ERROR_TYPE_WARNING,
899 "Directory for file `%s' exists but is not writable for us\n",
900 filename_valid_peers);
903 fh = GNUNET_DISK_file_open (filename_valid_peers,
904 GNUNET_DISK_OPEN_WRITE |
905 GNUNET_DISK_OPEN_CREATE,
906 GNUNET_DISK_PERM_USER_READ |
907 GNUNET_DISK_PERM_USER_WRITE);
910 LOG (GNUNET_ERROR_TYPE_WARNING,
911 "Not able to write valid peers to file `%s'\n",
912 filename_valid_peers);
915 LOG (GNUNET_ERROR_TYPE_DEBUG,
916 "Writing %u valid peers to disk\n",
917 GNUNET_CONTAINER_multipeermap_size (valid_peers));
918 number_written_peers =
919 GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
920 store_peer_presistently_iterator,
922 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
923 GNUNET_assert (number_written_peers ==
924 GNUNET_CONTAINER_multipeermap_size (valid_peers));
929 * @brief Convert string representation of peer id to peer id.
931 * Counterpart to #GNUNET_i2s_full.
933 * @param string_repr The string representation of the peer id
935 * @return The peer id
937 static const struct GNUNET_PeerIdentity *
938 s2i_full (const char *string_repr)
940 struct GNUNET_PeerIdentity *peer;
944 peer = GNUNET_new (struct GNUNET_PeerIdentity);
945 len = strlen (string_repr);
948 LOG (GNUNET_ERROR_TYPE_WARNING,
949 "Not able to convert string representation of PeerID to PeerID\n"
950 "Sting representation: %s (len %lu) - too short\n",
959 ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr,
962 if (GNUNET_OK != ret)
964 LOG (GNUNET_ERROR_TYPE_WARNING,
965 "Not able to convert string representation of PeerID to PeerID\n"
966 "Sting representation: %s\n",
975 * @brief Restore the peers on disk to #valid_peers.
978 restore_valid_peers ()
982 struct GNUNET_DISK_FileHandle *fh;
987 const struct GNUNET_PeerIdentity *peer;
989 if (0 == strncmp ("DISABLE", filename_valid_peers, 7))
994 if (GNUNET_OK != GNUNET_DISK_file_test (filename_valid_peers))
998 fh = GNUNET_DISK_file_open (filename_valid_peers,
999 GNUNET_DISK_OPEN_READ,
1000 GNUNET_DISK_PERM_NONE);
1001 GNUNET_assert (NULL != fh);
1002 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size));
1003 num_peers = file_size / 53;
1004 buf = GNUNET_malloc (file_size);
1005 size_read = GNUNET_DISK_file_read (fh, buf, file_size);
1006 GNUNET_assert (size_read == file_size);
1007 LOG (GNUNET_ERROR_TYPE_DEBUG,
1008 "Restoring %" PRIu32 " peers from file `%s'\n",
1010 filename_valid_peers);
1011 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53)
1013 str_repr = GNUNET_strndup (iter_buf, 53);
1014 peer = s2i_full (str_repr);
1015 GNUNET_free (str_repr);
1016 add_valid_peer (peer);
1017 LOG (GNUNET_ERROR_TYPE_DEBUG,
1018 "Restored valid peer %s from disk\n",
1019 GNUNET_i2s_full (peer));
1023 LOG (GNUNET_ERROR_TYPE_DEBUG,
1024 "num_peers: %" PRIu32 ", _size (valid_peers): %u\n",
1026 GNUNET_CONTAINER_multipeermap_size (valid_peers));
1027 if (num_peers != GNUNET_CONTAINER_multipeermap_size (valid_peers))
1029 LOG (GNUNET_ERROR_TYPE_WARNING,
1030 "Number of restored peers does not match file size. Have probably duplicates.\n");
1032 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1033 LOG (GNUNET_ERROR_TYPE_DEBUG,
1034 "Restored %u valid peers from disk\n",
1035 GNUNET_CONTAINER_multipeermap_size (valid_peers));
1040 * @brief Initialise storage of peers
1042 * @param fn_valid_peers filename of the file used to store valid peer ids
1043 * @param cadet_h cadet handle
1044 * @param own_id own peer identity
1047 Peers_initialise (char* fn_valid_peers,
1048 struct GNUNET_CADET_Handle *cadet_h,
1049 const struct GNUNET_PeerIdentity *own_id)
1051 filename_valid_peers = GNUNET_strdup (fn_valid_peers);
1052 cadet_handle = cadet_h;
1053 own_identity = *own_id;
1054 peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
1055 valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
1056 restore_valid_peers ();
1061 * @brief Delete storage of peers that was created with #Peers_initialise ()
1066 if (GNUNET_SYSERR ==
1067 GNUNET_CONTAINER_multipeermap_iterate (peer_map,
1068 peermap_clear_iterator,
1071 LOG (GNUNET_ERROR_TYPE_WARNING,
1072 "Iteration destroying peers was aborted.\n");
1074 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1076 store_valid_peers ();
1077 GNUNET_free (filename_valid_peers);
1078 GNUNET_CONTAINER_multipeermap_destroy (valid_peers);
1083 * Iterator over #valid_peers hash map entries.
1085 * @param cls closure - unused
1086 * @param peer current peer id
1087 * @param value value in the hash map - unused
1088 * @return #GNUNET_YES if we should continue to
1090 * #GNUNET_NO if not.
1093 valid_peer_iterator (void *cls,
1094 const struct GNUNET_PeerIdentity *peer,
1097 struct PeersIteratorCls *it_cls = cls;
1099 return it_cls->iterator (it_cls->cls,
1105 * @brief Get all currently known, valid peer ids.
1107 * @param it function to call on each peer id
1108 * @param it_cls extra argument to @a it
1109 * @return the number of key value pairs processed,
1110 * #GNUNET_SYSERR if it aborted iteration
1113 Peers_get_valid_peers (PeersIterator iterator,
1116 struct PeersIteratorCls *cls;
1119 cls = GNUNET_new (struct PeersIteratorCls);
1120 cls->iterator = iterator;
1122 ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
1123 valid_peer_iterator,
1131 * @brief Add peer to known peers.
1133 * This function is called on new peer_ids from 'external' sources
1134 * (client seed, cadet get_peers(), ...)
1136 * @param peer the new #GNUNET_PeerIdentity
1138 * @return #GNUNET_YES if peer was inserted
1139 * #GNUNET_NO otherwise (if peer was already known or
1140 * peer was #own_identity)
1143 Peers_insert_peer (const struct GNUNET_PeerIdentity *peer)
1145 if ( (GNUNET_YES == Peers_check_peer_known (peer)) ||
1146 (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity)) )
1148 return GNUNET_NO; /* We already know this peer - nothing to do */
1150 (void) create_peer_ctx (peer);
1155 Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
1158 * @brief Try connecting to a peer to see whether it is online
1160 * If not known yet, insert into known peers
1162 * @param peer the peer whose liveliness is to be checked
1163 * @return #GNUNET_YES if peer had to be inserted
1164 * #GNUNET_NO otherwise (if peer was already known or
1165 * peer was #own_identity)
1168 Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer)
1170 struct PeerContext *peer_ctx;
1173 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity))
1177 ret = Peers_insert_peer (peer);
1178 peer_ctx = get_peer_ctx (peer);
1179 if (GNUNET_NO == Peers_check_peer_flag (peer, Peers_ONLINE))
1181 check_peer_live (peer_ctx);
1188 * @brief Check if peer is removable.
1191 * - a recv channel exists
1192 * - there are pending messages
1193 * - there is no pending pull reply
1195 * @param peer the peer in question
1196 * @return #GNUNET_YES if peer is removable
1197 * #GNUNET_NO if peer is NOT removable
1198 * #GNUNET_SYSERR if peer is not known
1201 Peers_check_removable (const struct GNUNET_PeerIdentity *peer)
1203 struct PeerContext *peer_ctx;
1205 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1207 return GNUNET_SYSERR;
1210 peer_ctx = get_peer_ctx (peer);
1211 if ( (NULL != peer_ctx->recv_channel) ||
1212 (NULL != peer_ctx->pending_messages_head) ||
1213 (GNUNET_NO == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)) )
1221 Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1222 enum Peers_ChannelRole role);
1225 Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags);
1228 * @brief Remove peer
1230 * @param peer the peer to clean
1231 * @return #GNUNET_YES if peer was removed
1232 * #GNUNET_NO otherwise
1235 Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1237 struct PeerContext *peer_ctx;
1238 uint32_t *channel_flag;
1240 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1245 peer_ctx = get_peer_ctx (peer);
1246 set_peer_flag (peer_ctx, Peers_TO_DESTROY);
1247 LOG (GNUNET_ERROR_TYPE_DEBUG,
1248 "Going to remove peer %s\n",
1249 GNUNET_i2s (&peer_ctx->peer_id));
1250 Peers_unset_peer_flag (peer, Peers_ONLINE);
1252 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
1253 while (NULL != peer_ctx->pending_messages_head)
1255 LOG (GNUNET_ERROR_TYPE_DEBUG,
1256 "Removing unsent %s\n",
1257 peer_ctx->pending_messages_head->type);
1258 /* Cancle pending message, too */
1259 remove_pending_message (peer_ctx->pending_messages_head, GNUNET_YES);
1261 /* If we are still waiting for notification whether this peer is live
1262 * cancel the according task */
1263 if (NULL != peer_ctx->liveliness_check_pending)
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "Removing pending liveliness check for peer %s\n",
1267 GNUNET_i2s (&peer_ctx->peer_id));
1268 // TODO wait until cadet sets mq->cancel_impl
1269 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
1270 GNUNET_free (peer_ctx->liveliness_check_pending);
1271 peer_ctx->liveliness_check_pending = NULL;
1273 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
1274 if (NULL != peer_ctx->send_channel &&
1275 GNUNET_YES != Peers_check_channel_flag (channel_flag, Peers_CHANNEL_DESTROING))
1277 LOG (GNUNET_ERROR_TYPE_DEBUG,
1278 "Destroying send channel\n");
1279 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1280 peer_ctx->send_channel = NULL;
1281 peer_ctx->mq = NULL;
1283 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
1284 if (NULL != peer_ctx->recv_channel &&
1285 GNUNET_YES != Peers_check_channel_flag (channel_flag, Peers_CHANNEL_DESTROING))
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "Destroying recv channel\n");
1289 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1290 peer_ctx->recv_channel = NULL;
1293 GNUNET_free (peer_ctx->send_channel_flags);
1294 GNUNET_free (peer_ctx->recv_channel_flags);
1296 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, &peer_ctx->peer_id))
1298 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1300 GNUNET_free (peer_ctx);
1306 * @brief set flags on a given peer.
1308 * @param peer the peer to set flags on
1309 * @param flags the flags
1312 Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1314 struct PeerContext *peer_ctx;
1316 peer_ctx = get_peer_ctx (peer);
1317 set_peer_flag (peer_ctx, flags);
1322 * @brief unset flags on a given peer.
1324 * @param peer the peer to unset flags on
1325 * @param flags the flags
1328 Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1330 struct PeerContext *peer_ctx;
1332 peer_ctx = get_peer_ctx (peer);
1333 unset_peer_flag (peer_ctx, flags);
1338 * @brief Check whether flags on a peer are set.
1340 * @param peer the peer to check the flag of
1341 * @param flags the flags to check
1343 * @return #GNUNET_SYSERR if peer is not known
1344 * #GNUNET_YES if all given flags are set
1345 * #GNUNET_NO otherwise
1348 Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1350 struct PeerContext *peer_ctx;
1352 if (GNUNET_NO == Peers_check_peer_known (peer))
1354 return GNUNET_SYSERR;
1356 peer_ctx = get_peer_ctx (peer);
1357 return check_peer_flag_set (peer_ctx, flags);
1362 * @brief set flags on a given channel.
1364 * @param channel the channel to set flags on
1365 * @param flags the flags
1368 Peers_set_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1370 set_channel_flag (channel_flags, flags);
1375 * @brief unset flags on a given channel.
1377 * @param channel the channel to unset flags on
1378 * @param flags the flags
1381 Peers_unset_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1383 unset_channel_flag (channel_flags, flags);
1388 * @brief Check whether flags on a channel are set.
1390 * @param channel the channel to check the flag of
1391 * @param flags the flags to check
1393 * @return #GNUNET_YES if all given flags are set
1394 * #GNUNET_NO otherwise
1397 Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1399 return check_channel_flag_set (channel_flags, flags);
1403 * @brief Get the flags for the channel in @a role for @a peer.
1405 * @param peer Peer to get the channel flags for.
1406 * @param role Role of channel to get flags for
1408 * @return The flags.
1411 Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1412 enum Peers_ChannelRole role)
1414 const struct PeerContext *peer_ctx;
1416 peer_ctx = get_peer_ctx (peer);
1417 if (Peers_CHANNEL_ROLE_SENDING == role)
1419 return peer_ctx->send_channel_flags;
1421 else if (Peers_CHANNEL_ROLE_RECEIVING == role)
1423 return peer_ctx->recv_channel_flags;
1432 * @brief Check whether we have information about the given peer.
1434 * FIXME probably deprecated. Make this the new _online.
1436 * @param peer peer in question
1438 * @return #GNUNET_YES if peer is known
1439 * #GNUNET_NO if peer is not knwon
1442 Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer)
1444 if (NULL != peer_map)
1446 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
1455 * @brief Check whether @a peer is actually a peer.
1457 * A valid peer is a peer that we know exists eg. we were connected to once.
1459 * @param peer peer in question
1461 * @return #GNUNET_YES if peer is valid
1462 * #GNUNET_NO if peer is not valid
1465 Peers_check_peer_valid (const struct GNUNET_PeerIdentity *peer)
1467 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer);
1472 * @brief Indicate that we want to send to the other peer
1474 * This establishes a sending channel
1476 * @param peer the peer to establish channel to
1479 Peers_indicate_sending_intention (const struct GNUNET_PeerIdentity *peer)
1481 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1482 (void) get_channel (peer);
1487 * @brief Check whether other peer has the intention to send/opened channel
1490 * @param peer the peer in question
1492 * @return #GNUNET_YES if peer has the intention to send
1493 * #GNUNET_NO otherwise
1496 Peers_check_peer_send_intention (const struct GNUNET_PeerIdentity *peer)
1498 const struct PeerContext *peer_ctx;
1500 peer_ctx = get_peer_ctx (peer);
1501 if (NULL != peer_ctx->recv_channel)
1510 * Handle the channel a peer opens to us.
1512 * @param cls The closure
1513 * @param channel The channel the peer wants to establish
1514 * @param initiator The peer's peer ID
1516 * @return initial channel context for the channel
1517 * (can be NULL -- that's not an error)
1520 Peers_handle_inbound_channel (void *cls,
1521 struct GNUNET_CADET_Channel *channel,
1522 const struct GNUNET_PeerIdentity *initiator)
1524 struct PeerContext *peer_ctx;
1525 struct GNUNET_PeerIdentity *ctx_peer;
1527 LOG (GNUNET_ERROR_TYPE_DEBUG,
1528 "New channel was established to us (Peer %s).\n",
1529 GNUNET_i2s (initiator));
1530 GNUNET_assert (NULL != channel); /* according to cadet API */
1531 /* Make sure we 'know' about this peer */
1532 peer_ctx = create_or_get_peer_ctx (initiator);
1533 set_peer_live (peer_ctx);
1534 ctx_peer = GNUNET_new (struct GNUNET_PeerIdentity);
1535 *ctx_peer = *initiator;
1536 /* We only accept one incoming channel per peer */
1537 if (GNUNET_YES == Peers_check_peer_send_intention (initiator))
1539 set_channel_flag (peer_ctx->recv_channel_flags,
1540 Peers_CHANNEL_ESTABLISHED_TWICE);
1541 //GNUNET_CADET_channel_destroy (channel);
1542 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1543 peer_ctx->recv_channel = channel;
1544 /* return the channel context */
1547 peer_ctx->recv_channel = channel;
1553 * @brief Check whether a sending channel towards the given peer exists
1555 * @param peer the peer to check for
1557 * @return #GNUNET_YES if a sending channel towards that peer exists
1558 * #GNUNET_NO otherwise
1561 Peers_check_sending_channel_exists (const struct GNUNET_PeerIdentity *peer)
1563 struct PeerContext *peer_ctx;
1565 if (GNUNET_NO == Peers_check_peer_known (peer))
1566 { /* If no such peer exists, there is no channel */
1569 peer_ctx = get_peer_ctx (peer);
1570 if (NULL == peer_ctx->send_channel)
1579 * @brief check whether the given channel is the sending channel of the given
1582 * @param peer the peer in question
1583 * @param channel the channel to check for
1584 * @param role either #Peers_CHANNEL_ROLE_SENDING, or
1585 * #Peers_CHANNEL_ROLE_RECEIVING
1587 * @return #GNUNET_YES if the given chennel is the sending channel of the peer
1588 * #GNUNET_NO otherwise
1591 Peers_check_channel_role (const struct GNUNET_PeerIdentity *peer,
1592 const struct GNUNET_CADET_Channel *channel,
1593 enum Peers_ChannelRole role)
1595 const struct PeerContext *peer_ctx;
1597 if (GNUNET_NO == Peers_check_peer_known (peer))
1601 peer_ctx = get_peer_ctx (peer);
1602 if ( (Peers_CHANNEL_ROLE_SENDING == role) &&
1603 (channel == peer_ctx->send_channel) )
1607 if ( (Peers_CHANNEL_ROLE_RECEIVING == role) &&
1608 (channel == peer_ctx->recv_channel) )
1617 * @brief Destroy the send channel of a peer e.g. stop indicating a sending
1618 * intention to another peer
1620 * If there is also no channel to receive messages from that peer, remove it
1624 * @peer the peer identity of the peer whose sending channel to destroy
1625 * @return #GNUNET_YES if channel was destroyed
1626 * #GNUNET_NO otherwise
1629 Peers_destroy_sending_channel (const struct GNUNET_PeerIdentity *peer)
1631 struct PeerContext *peer_ctx;
1633 if (GNUNET_NO == Peers_check_peer_known (peer))
1637 peer_ctx = get_peer_ctx (peer);
1638 if (NULL != peer_ctx->send_channel)
1640 set_channel_flag (peer_ctx->send_channel_flags, Peers_CHANNEL_CLEAN);
1641 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1642 peer_ctx->send_channel = NULL;
1643 peer_ctx->mq = NULL;
1644 (void) Peers_check_connected (peer);
1651 * This is called when a channel is destroyed.
1653 * @param cls The closure
1654 * @param channel The channel being closed
1657 Peers_cleanup_destroyed_channel (void *cls,
1658 const struct GNUNET_CADET_Channel *channel)
1660 struct GNUNET_PeerIdentity *peer = cls;
1661 struct PeerContext *peer_ctx;
1663 if (GNUNET_NO == Peers_check_peer_known (peer))
1664 {/* We don't want to implicitly create a context that we're about to kill */
1665 LOG (GNUNET_ERROR_TYPE_DEBUG,
1666 "channel (%s) without associated context was destroyed\n",
1670 peer_ctx = get_peer_ctx (peer);
1672 /* If our peer issued the destruction of the channel, the #Peers_TO_DESTROY
1673 * flag will be set. In this case simply make sure that the channels are
1675 /* FIXME This distinction seems to be redundant */
1676 if (Peers_check_peer_flag (peer, Peers_TO_DESTROY))
1677 {/* We initiatad the destruction of this particular peer */
1678 LOG (GNUNET_ERROR_TYPE_DEBUG,
1679 "Peer is in the process of being destroyed\n");
1680 if (channel == peer_ctx->send_channel)
1682 peer_ctx->send_channel = NULL;
1683 peer_ctx->mq = NULL;
1685 else if (channel == peer_ctx->recv_channel)
1687 peer_ctx->recv_channel = NULL;
1690 if (NULL != peer_ctx->send_channel)
1692 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1693 peer_ctx->send_channel = NULL;
1694 peer_ctx->mq = NULL;
1696 if (NULL != peer_ctx->recv_channel)
1698 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1699 peer_ctx->recv_channel = NULL;
1701 /* Set the #Peers_ONLINE flag accordingly */
1702 (void) Peers_check_connected (peer);
1707 { /* We did not initiate the destruction of this peer */
1708 LOG (GNUNET_ERROR_TYPE_DEBUG,
1709 "Peer is NOT in the process of being destroyed\n");
1710 if (channel == peer_ctx->send_channel)
1711 { /* Something (but us) killd the channel - clean up peer */
1712 LOG (GNUNET_ERROR_TYPE_DEBUG,
1713 "send channel (%s) was destroyed - cleaning up\n",
1715 peer_ctx->send_channel = NULL;
1716 peer_ctx->mq = NULL;
1718 else if (channel == peer_ctx->recv_channel)
1719 { /* Other peer doesn't want to send us messages anymore */
1720 LOG (GNUNET_ERROR_TYPE_DEBUG,
1721 "Peer %s destroyed recv channel - cleaning up channel\n",
1723 peer_ctx->recv_channel = NULL;
1727 LOG (GNUNET_ERROR_TYPE_WARNING,
1728 "unknown channel (%s) was destroyed\n",
1732 (void) Peers_check_connected (peer);
1736 * @brief Send a message to another peer.
1738 * Keeps track about pending messages so they can be properly removed when the
1739 * peer is destroyed.
1741 * @param peer receeiver of the message
1742 * @param ev envelope of the message
1743 * @param type type of the message
1746 Peers_send_message (const struct GNUNET_PeerIdentity *peer,
1747 struct GNUNET_MQ_Envelope *ev,
1750 struct PendingMessage *pending_msg;
1751 struct GNUNET_MQ_Handle *mq;
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "Sending message to %s of type %s\n",
1757 pending_msg = insert_pending_message (peer, ev, type);
1759 GNUNET_MQ_notify_sent (ev,
1762 GNUNET_MQ_send (mq, ev);
1766 * @brief Schedule a operation on given peer
1768 * Avoids scheduling an operation twice.
1770 * @param peer the peer we want to schedule the operation for once it gets live
1772 * @return #GNUNET_YES if the operation was scheduled
1773 * #GNUNET_NO otherwise
1776 Peers_schedule_operation (const struct GNUNET_PeerIdentity *peer,
1777 const PeerOp peer_op)
1779 struct PeerPendingOp pending_op;
1780 struct PeerContext *peer_ctx;
1782 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity))
1786 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1788 //TODO if LIVE/ONLINE execute immediately
1790 if (GNUNET_NO == check_operation_scheduled (peer, peer_op))
1792 peer_ctx = get_peer_ctx (peer);
1793 pending_op.op = peer_op;
1794 pending_op.op_cls = NULL;
1795 GNUNET_array_append (peer_ctx->pending_ops,
1796 peer_ctx->num_pending_ops,
1804 * @brief Get the recv_channel of @a peer.
1805 * Needed to correctly handle (call #GNUNET_CADET_receive_done()) incoming
1808 * @param peer The peer to get the recv_channel from.
1810 * @return The recv_channel.
1812 struct GNUNET_CADET_Channel *
1813 Peers_get_recv_channel (const struct GNUNET_PeerIdentity *peer)
1815 struct PeerContext *peer_ctx;
1817 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1818 peer_ctx = get_peer_ctx (peer);
1819 return peer_ctx->recv_channel;
1821 /***********************************************************************
1822 * /Old gnunet-service-rps_peers.c
1823 ***********************************************************************/
1826 /***********************************************************************
1827 * Housekeeping with clients
1828 ***********************************************************************/
1831 * Closure used to pass the client and the id to the callback
1832 * that replies to a client's request
1839 struct ReplyCls *next;
1840 struct ReplyCls *prev;
1843 * The identifier of the request
1848 * The handle to the request
1850 struct RPS_SamplerRequestHandle *req_handle;
1853 * The client handle to send the reply to
1855 struct ClientContext *cli_ctx;
1860 * Struct used to store the context of a connected client.
1862 struct ClientContext
1867 struct ClientContext *next;
1868 struct ClientContext *prev;
1871 * The message queue to communicate with the client.
1873 struct GNUNET_MQ_Handle *mq;
1876 * DLL with handles to single requests from the client
1878 struct ReplyCls *rep_cls_head;
1879 struct ReplyCls *rep_cls_tail;
1882 * @brief How many updates this client expects to receive.
1884 int64_t view_updates_left;
1887 * The client handle to send the reply to
1889 struct GNUNET_SERVICE_Client *client;
1893 * DLL with all clients currently connected to us
1895 struct ClientContext *cli_ctx_head;
1896 struct ClientContext *cli_ctx_tail;
1898 /***********************************************************************
1899 * /Housekeeping with clients
1900 ***********************************************************************/
1906 /***********************************************************************
1908 ***********************************************************************/
1911 * Sampler used for the Brahms protocol itself.
1913 static struct RPS_Sampler *prot_sampler;
1916 * Sampler used for the clients.
1918 static struct RPS_Sampler *client_sampler;
1921 * Name to log view to
1923 static char *file_name_view_log;
1926 * The size of sampler we need to be able to satisfy the client's need
1929 static unsigned int sampler_size_client_need;
1932 * The size of sampler we need to be able to satisfy the Brahms protocol's
1933 * need of random peers.
1935 * This is one minimum size the sampler grows to.
1937 static unsigned int sampler_size_est_need;
1940 * @brief This is the minimum estimate used as sampler size.
1942 * It is configured by the user.
1944 static unsigned int sampler_size_est_min;
1947 * @brief This is the estimate used as view size.
1949 * It is initialised with the minimum
1951 static unsigned int view_size_est_need;
1954 * @brief This is the minimum estimate used as view size.
1956 * It is configured by the user.
1958 static unsigned int view_size_est_min;
1961 * Percentage of total peer number in the view
1962 * to send random PUSHes to
1967 * Percentage of total peer number in the view
1968 * to send random PULLs to
1973 * Identifier for the main task that runs periodically.
1975 static struct GNUNET_SCHEDULER_Task *do_round_task;
1978 * Time inverval the do_round task runs in.
1980 static struct GNUNET_TIME_Relative round_interval;
1983 * List to store peers received through pushes temporary.
1985 static struct CustomPeerMap *push_map;
1988 * List to store peers received through pulls temporary.
1990 static struct CustomPeerMap *pull_map;
1995 static struct GNUNET_NSE_Handle *nse;
2000 static struct GNUNET_CADET_Handle *cadet_handle;
2003 * @brief Port to communicate to other peers.
2005 static struct GNUNET_CADET_Port *cadet_port;
2008 * Handler to PEERINFO.
2010 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
2013 * Handle for cancellation of iteration over peers.
2015 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
2020 * Counts how many requets clients already issued.
2021 * Only needed in the beginning to check how many of the 64 deltas
2024 static unsigned int req_counter;
2027 * Time of the last request we received.
2029 * Used to compute the expected request rate.
2031 static struct GNUNET_TIME_Absolute last_request;
2034 * Size of #request_deltas.
2036 #define REQUEST_DELTAS_SIZE 64
2037 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
2040 * Last 64 deltas between requests
2042 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
2045 * The prediction of the rate of requests
2047 static struct GNUNET_TIME_Relative request_rate;
2050 #ifdef ENABLE_MALICIOUS
2052 * Type of malicious peer
2054 * 0 Don't act malicious at all - Default
2055 * 1 Try to maximise representation
2056 * 2 Try to partition the network
2059 static uint32_t mal_type;
2062 * Other malicious peers
2064 static struct GNUNET_PeerIdentity *mal_peers;
2067 * Hashmap of malicious peers used as set.
2068 * Used to more efficiently check whether we know that peer.
2070 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set;
2073 * Number of other malicious peers
2075 static uint32_t num_mal_peers;
2079 * If type is 2 This struct is used to store the attacked peers in a DLL
2086 struct AttackedPeer *next;
2087 struct AttackedPeer *prev;
2092 struct GNUNET_PeerIdentity peer_id;
2096 * If type is 2 this is the DLL of attacked peers
2098 static struct AttackedPeer *att_peers_head;
2099 static struct AttackedPeer *att_peers_tail;
2102 * This index is used to point to an attacked peer to
2103 * implement the round-robin-ish way to select attacked peers.
2105 static struct AttackedPeer *att_peer_index;
2108 * Hashmap of attacked peers used as set.
2109 * Used to more efficiently check whether we know that peer.
2111 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set;
2114 * Number of attacked peers
2116 static uint32_t num_attacked_peers;
2119 * If type is 1 this is the attacked peer
2121 static struct GNUNET_PeerIdentity attacked_peer;
2124 * The limit of PUSHes we can send in one round.
2125 * This is an assumption of the Brahms protocol and either implemented
2128 * assumend to be the bandwidth limitation.
2130 static uint32_t push_limit = 10000;
2131 #endif /* ENABLE_MALICIOUS */
2134 /***********************************************************************
2136 ***********************************************************************/
2139 /***********************************************************************
2141 ***********************************************************************/
2145 * Print peerlist to log.
2148 print_peer_list (struct GNUNET_PeerIdentity *list,
2153 LOG (GNUNET_ERROR_TYPE_DEBUG,
2154 "Printing peer list of length %u at %p:\n",
2157 for (i = 0 ; i < len ; i++)
2159 LOG (GNUNET_ERROR_TYPE_DEBUG,
2161 i, GNUNET_i2s (&list[i]));
2167 * Remove peer from list.
2170 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
2171 unsigned int *list_size,
2172 const struct GNUNET_PeerIdentity *peer)
2175 struct GNUNET_PeerIdentity *tmp;
2179 LOG (GNUNET_ERROR_TYPE_DEBUG,
2180 "Removing peer %s from list at %p\n",
2184 for ( i = 0 ; i < *list_size ; i++ )
2186 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
2188 if (i < *list_size -1)
2189 { /* Not at the last entry -- shift peers left */
2190 memmove (&tmp[i], &tmp[i +1],
2191 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
2193 /* Remove last entry (should be now useless PeerID) */
2194 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
2202 * Sum all time relatives of an array.
2204 static struct GNUNET_TIME_Relative
2205 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array,
2208 struct GNUNET_TIME_Relative sum;
2211 sum = GNUNET_TIME_UNIT_ZERO;
2212 for ( i = 0 ; i < arr_size ; i++ )
2214 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
2221 * Compute the average of given time relatives.
2223 static struct GNUNET_TIME_Relative
2224 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array,
2227 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array,
2234 * Insert PeerID in #view
2236 * Called once we know a peer is live.
2237 * Implements #PeerOp
2239 * @return GNUNET_OK if peer was actually inserted
2240 * GNUNET_NO if peer was not inserted
2243 insert_in_view_op (void *cls,
2244 const struct GNUNET_PeerIdentity *peer);
2247 * Insert PeerID in #view
2249 * Called once we know a peer is live.
2251 * @return GNUNET_OK if peer was actually inserted
2252 * GNUNET_NO if peer was not inserted
2255 insert_in_view (const struct GNUNET_PeerIdentity *peer)
2259 online = Peers_check_peer_flag (peer, Peers_ONLINE);
2260 if ( (GNUNET_NO == online) ||
2261 (GNUNET_SYSERR == online) ) /* peer is not even known */
2263 (void) Peers_issue_peer_liveliness_check (peer);
2264 (void) Peers_schedule_operation (peer, insert_in_view_op);
2267 /* Open channel towards peer to keep connection open */
2268 Peers_indicate_sending_intention (peer);
2269 return View_put (peer);
2273 * Put random peer from sampler into the view as history update.
2276 hist_update (void *cls,
2277 struct GNUNET_PeerIdentity *ids,
2282 for (i = 0; i < num_peers; i++)
2284 (void) insert_in_view (&ids[i]);
2285 to_file (file_name_view_log,
2287 GNUNET_i2s_full (ids));
2293 * Wrapper around #RPS_sampler_resize()
2295 * If we do not have enough sampler elements, double current sampler size
2296 * If we have more than enough sampler elements, halv current sampler size
2299 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
2301 unsigned int sampler_size;
2304 // TODO respect the min, max
2305 sampler_size = RPS_sampler_get_size (sampler);
2306 if (sampler_size > new_size * 4)
2308 RPS_sampler_resize (sampler, sampler_size / 2);
2310 else if (sampler_size < new_size)
2312 RPS_sampler_resize (sampler, sampler_size * 2);
2314 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
2319 * Wrapper around #RPS_sampler_resize() resizing the client sampler
2322 client_resize_wrapper ()
2324 uint32_t bigger_size;
2328 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
2330 // TODO respect the min, max
2331 resize_wrapper (client_sampler, bigger_size);
2332 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
2338 * Estimate request rate
2340 * Called every time we receive a request from the client.
2345 struct GNUNET_TIME_Relative max_round_duration;
2347 if (request_deltas_size > req_counter)
2349 if ( 1 < req_counter)
2351 /* Shift last request deltas to the right */
2352 memmove (&request_deltas[1],
2354 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
2356 /* Add current delta to beginning */
2358 GNUNET_TIME_absolute_get_difference (last_request,
2359 GNUNET_TIME_absolute_get ());
2360 request_rate = T_relative_avg (request_deltas, req_counter);
2361 request_rate = (request_rate.rel_value_us < 1) ?
2362 GNUNET_TIME_relative_get_unit_ () : request_rate;
2364 /* Compute the duration a round will maximally take */
2365 max_round_duration =
2366 GNUNET_TIME_relative_add (round_interval,
2367 GNUNET_TIME_relative_divide (round_interval, 2));
2369 /* Set the estimated size the sampler has to have to
2370 * satisfy the current client request rate */
2371 sampler_size_client_need =
2372 max_round_duration.rel_value_us / request_rate.rel_value_us;
2374 /* Resize the sampler */
2375 client_resize_wrapper ();
2377 last_request = GNUNET_TIME_absolute_get ();
2382 * Add all peers in @a peer_array to @a peer_map used as set.
2384 * @param peer_array array containing the peers
2385 * @param num_peers number of peers in @peer_array
2386 * @param peer_map the peermap to use as set
2389 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
2390 unsigned int num_peers,
2391 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
2394 if (NULL == peer_map)
2396 LOG (GNUNET_ERROR_TYPE_WARNING,
2397 "Trying to add peers to non-existing peermap.\n");
2401 for (i = 0; i < num_peers; i++)
2403 GNUNET_CONTAINER_multipeermap_put (peer_map,
2406 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2412 * Send a PULL REPLY to @a peer_id
2414 * @param peer_id the peer to send the reply to.
2415 * @param peer_ids the peers to send to @a peer_id
2416 * @param num_peer_ids the number of peers to send to @a peer_id
2419 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
2420 const struct GNUNET_PeerIdentity *peer_ids,
2421 unsigned int num_peer_ids)
2424 struct GNUNET_MQ_Envelope *ev;
2425 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
2427 /* Compute actual size */
2428 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
2429 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
2431 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
2432 /* Compute number of peers to send
2433 * If too long, simply truncate */
2434 // TODO select random ones via permutation
2435 // or even better: do good protocol design
2437 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
2438 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
2439 sizeof (struct GNUNET_PeerIdentity);
2441 send_size = num_peer_ids;
2443 LOG (GNUNET_ERROR_TYPE_DEBUG,
2444 "Going to send PULL REPLY with %u peers to %s\n",
2445 send_size, GNUNET_i2s (peer_id));
2447 ev = GNUNET_MQ_msg_extra (out_msg,
2448 send_size * sizeof (struct GNUNET_PeerIdentity),
2449 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
2450 out_msg->num_peers = htonl (send_size);
2451 GNUNET_memcpy (&out_msg[1], peer_ids,
2452 send_size * sizeof (struct GNUNET_PeerIdentity));
2454 Peers_send_message (peer_id, ev, "PULL REPLY");
2455 GNUNET_STATISTICS_update(stats, "# pull reply send issued", 1, GNUNET_NO);
2460 * Insert PeerID in #pull_map
2462 * Called once we know a peer is live.
2465 insert_in_pull_map (void *cls,
2466 const struct GNUNET_PeerIdentity *peer)
2468 CustomPeerMap_put (pull_map, peer);
2473 * Insert PeerID in #view
2475 * Called once we know a peer is live.
2476 * Implements #PeerOp
2479 insert_in_view_op (void *cls,
2480 const struct GNUNET_PeerIdentity *peer)
2482 (void) insert_in_view (peer);
2487 * Update sampler with given PeerID.
2488 * Implements #PeerOp
2491 insert_in_sampler (void *cls,
2492 const struct GNUNET_PeerIdentity *peer)
2494 LOG (GNUNET_ERROR_TYPE_DEBUG,
2495 "Updating samplers with peer %s from insert_in_sampler()\n",
2497 RPS_sampler_update (prot_sampler, peer);
2498 RPS_sampler_update (client_sampler, peer);
2499 if (0 < RPS_sampler_count_id (prot_sampler, peer))
2501 /* Make sure we 'know' about this peer */
2502 (void) Peers_issue_peer_liveliness_check (peer);
2503 /* Establish a channel towards that peer to indicate we are going to send
2505 //Peers_indicate_sending_intention (peer);
2510 * @brief This is called on peers from external sources (cadet, peerinfo, ...)
2511 * If the peer is not known, liveliness check is issued and it is
2512 * scheduled to be inserted in sampler and view.
2514 * "External sources" refer to every source except the gossip.
2516 * @param peer peer to insert
2519 got_peer (const struct GNUNET_PeerIdentity *peer)
2521 /* If we did not know this peer already, insert it into sampler and view */
2522 if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer))
2524 Peers_schedule_operation (peer, insert_in_sampler);
2525 Peers_schedule_operation (peer, insert_in_view_op);
2530 * @brief Checks if there is a sending channel and if it is needed
2532 * @param peer the peer whose sending channel is checked
2533 * @return GNUNET_YES if sending channel exists and is still needed
2534 * GNUNET_NO otherwise
2537 check_sending_channel_needed (const struct GNUNET_PeerIdentity *peer)
2539 /* struct GNUNET_CADET_Channel *channel; */
2540 if (GNUNET_NO == Peers_check_peer_known (peer))
2544 if (GNUNET_YES == Peers_check_sending_channel_exists (peer))
2546 if ( (0 < RPS_sampler_count_id (prot_sampler, peer)) ||
2547 (GNUNET_YES == View_contains_peer (peer)) ||
2548 (GNUNET_YES == CustomPeerMap_contains_peer (push_map, peer)) ||
2549 (GNUNET_YES == CustomPeerMap_contains_peer (pull_map, peer)) ||
2550 (GNUNET_YES == Peers_check_peer_flag (peer, Peers_PULL_REPLY_PENDING)))
2551 { /* If we want to keep the connection to peer open */
2560 * @brief remove peer from our knowledge, the view, push and pull maps and
2563 * @param peer the peer to remove
2566 remove_peer (const struct GNUNET_PeerIdentity *peer)
2568 (void) View_remove_peer (peer);
2569 CustomPeerMap_remove_peer (pull_map, peer);
2570 CustomPeerMap_remove_peer (push_map, peer);
2571 RPS_sampler_reinitialise_by_value (prot_sampler, peer);
2572 RPS_sampler_reinitialise_by_value (client_sampler, peer);
2573 Peers_remove_peer (peer);
2578 * @brief Remove data that is not needed anymore.
2580 * If the sending channel is no longer needed it is destroyed.
2582 * @param peer the peer whose data is about to be cleaned
2585 clean_peer (const struct GNUNET_PeerIdentity *peer)
2587 if (GNUNET_NO == check_sending_channel_needed (peer))
2589 LOG (GNUNET_ERROR_TYPE_DEBUG,
2590 "Going to remove send channel to peer %s\n",
2592 #ifdef ENABLE_MALICIOUS
2593 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2594 (void) Peers_destroy_sending_channel (peer);
2595 #else /* ENABLE_MALICIOUS */
2596 (void) Peers_destroy_sending_channel (peer);
2597 #endif /* ENABLE_MALICIOUS */
2600 if ( (GNUNET_NO == Peers_check_peer_send_intention (peer)) &&
2601 (GNUNET_NO == View_contains_peer (peer)) &&
2602 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2603 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2604 (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2605 (0 == RPS_sampler_count_id (client_sampler, peer)) &&
2606 (GNUNET_NO != Peers_check_removable (peer)) )
2607 { /* We can safely remove this peer */
2608 LOG (GNUNET_ERROR_TYPE_DEBUG,
2609 "Going to remove peer %s\n",
2617 * @brief This is called when a channel is destroyed.
2619 * Removes peer completely from our knowledge if the send_channel was destroyed
2620 * Otherwise simply delete the recv_channel
2621 * Also check if the knowledge about this peer is still needed.
2622 * If not, remove this peer from our knowledge.
2624 * @param cls The closure
2625 * @param channel The channel being closed
2626 * @param channel_ctx The context associated with this channel
2629 cleanup_destroyed_channel (void *cls,
2630 const struct GNUNET_CADET_Channel *channel)
2632 struct GNUNET_PeerIdentity *peer = cls;
2633 uint32_t *channel_flag;
2634 struct PeerContext *peer_ctx;
2636 GNUNET_assert (NULL != peer);
2638 if (GNUNET_NO == Peers_check_peer_known (peer))
2639 { /* We don't know a context to that peer */
2640 LOG (GNUNET_ERROR_TYPE_WARNING,
2641 "channel (%s) without associated context was destroyed\n",
2647 peer_ctx = get_peer_ctx (peer);
2648 if (GNUNET_YES == Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
2650 LOG (GNUNET_ERROR_TYPE_DEBUG,
2651 "Callback on destruction of recv-channel was called (%s)\n",
2653 set_channel_flag (peer_ctx->recv_channel_flags, Peers_CHANNEL_DESTROING);
2654 } else if (GNUNET_YES == Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
2656 LOG (GNUNET_ERROR_TYPE_DEBUG,
2657 "Callback on destruction of send-channel was called (%s)\n",
2659 set_channel_flag (peer_ctx->send_channel_flags, Peers_CHANNEL_DESTROING);
2661 LOG (GNUNET_ERROR_TYPE_ERROR,
2662 "Channel to be destroyed has is neither sending nor receiving role\n");
2665 if (GNUNET_YES == Peers_check_peer_flag (peer, Peers_TO_DESTROY))
2666 { /* We are in the middle of removing that peer from our knowledge. In this
2667 case simply make sure that the channels are cleaned. */
2668 Peers_cleanup_destroyed_channel (cls, channel);
2669 to_file (file_name_view_log,
2670 "-%s\t(cleanup channel, ourself)",
2671 GNUNET_i2s_full (peer));
2677 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
2678 { /* Channel used for sending was destroyed */
2679 /* Possible causes of channel destruction:
2680 * - ourselves -> cleaning send channel -> clean context
2681 * - other peer -> peer probably went down -> remove
2683 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
2684 if (GNUNET_YES == Peers_check_channel_flag (channel_flag, Peers_CHANNEL_CLEAN))
2685 { /* We are about to clean the sending channel. Clean the respective
2687 Peers_cleanup_destroyed_channel (cls, channel);
2692 { /* Other peer destroyed our sending channel that he is supposed to keep
2693 * open. It probably went down. Remove it from our knowledge. */
2694 Peers_cleanup_destroyed_channel (cls, channel);
2700 else if (GNUNET_YES ==
2701 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
2702 { /* Channel used for receiving was destroyed */
2703 /* Possible causes of channel destruction:
2704 * - ourselves -> peer tried to establish channel twice -> clean context
2705 * - other peer -> peer doesn't want to send us data -> clean
2707 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
2709 Peers_check_channel_flag (channel_flag, Peers_CHANNEL_ESTABLISHED_TWICE))
2710 { /* Other peer tried to establish a channel to us twice. We do not accept
2711 * that. Clean the context. */
2712 Peers_cleanup_destroyed_channel (cls, channel);
2717 { /* Other peer doesn't want to send us data anymore. We are free to clean
2719 Peers_cleanup_destroyed_channel (cls, channel);
2727 LOG (GNUNET_ERROR_TYPE_WARNING,
2728 "Destroyed channel is neither sending nor receiving channel\n");
2733 /***********************************************************************
2735 ***********************************************************************/
2738 destroy_reply_cls (struct ReplyCls *rep_cls)
2740 struct ClientContext *cli_ctx;
2742 cli_ctx = rep_cls->cli_ctx;
2743 GNUNET_assert (NULL != cli_ctx);
2744 if (NULL != rep_cls->req_handle)
2746 RPS_sampler_request_cancel (rep_cls->req_handle);
2748 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
2749 cli_ctx->rep_cls_tail,
2751 GNUNET_free (rep_cls);
2756 destroy_cli_ctx (struct ClientContext *cli_ctx)
2758 GNUNET_assert (NULL != cli_ctx);
2759 if (NULL != cli_ctx->rep_cls_head)
2761 LOG (GNUNET_ERROR_TYPE_WARNING,
2762 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
2763 while (NULL != cli_ctx->rep_cls_head)
2764 destroy_reply_cls (cli_ctx->rep_cls_head);
2766 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
2769 GNUNET_free (cli_ctx);
2774 * Function called by NSE.
2776 * Updates sizes of sampler list and view and adapt those lists
2780 nse_callback (void *cls,
2781 struct GNUNET_TIME_Absolute timestamp,
2782 double logestimate, double std_dev)
2785 //double scale; // TODO this might go gloabal/config
2787 LOG (GNUNET_ERROR_TYPE_DEBUG,
2788 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
2789 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
2791 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
2792 // GNUNET_NSE_log_estimate_to_n (logestimate);
2793 estimate = pow (estimate, 1.0 / 3);
2794 // TODO add if std_dev is a number
2795 // estimate += (std_dev * scale);
2796 if (view_size_est_min < ceil (estimate))
2798 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
2799 sampler_size_est_need = estimate;
2800 view_size_est_need = estimate;
2803 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
2804 //sampler_size_est_need = view_size_est_min;
2805 view_size_est_need = view_size_est_min;
2808 /* If the NSE has changed adapt the lists accordingly */
2809 resize_wrapper (prot_sampler, sampler_size_est_need);
2810 client_resize_wrapper ();
2815 * Callback called once the requested PeerIDs are ready.
2817 * Sends those to the requesting client.
2820 client_respond (void *cls,
2821 struct GNUNET_PeerIdentity *peer_ids,
2824 struct ReplyCls *reply_cls = cls;
2826 struct GNUNET_MQ_Envelope *ev;
2827 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
2828 uint32_t size_needed;
2829 struct ClientContext *cli_ctx;
2831 GNUNET_assert (NULL != reply_cls);
2832 LOG (GNUNET_ERROR_TYPE_DEBUG,
2833 "sampler returned %" PRIu32 " peers:\n",
2835 for (i = 0; i < num_peers; i++)
2837 LOG (GNUNET_ERROR_TYPE_DEBUG,
2838 " %" PRIu32 ": %s\n",
2840 GNUNET_i2s (&peer_ids[i]));
2843 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
2844 num_peers * sizeof (struct GNUNET_PeerIdentity);
2846 GNUNET_assert (GNUNET_MAX_MESSAGE_SIZE >= size_needed);
2848 ev = GNUNET_MQ_msg_extra (out_msg,
2849 num_peers * sizeof (struct GNUNET_PeerIdentity),
2850 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
2851 out_msg->num_peers = htonl (num_peers);
2852 out_msg->id = htonl (reply_cls->id);
2854 GNUNET_memcpy (&out_msg[1],
2856 num_peers * sizeof (struct GNUNET_PeerIdentity));
2857 GNUNET_free (peer_ids);
2859 cli_ctx = reply_cls->cli_ctx;
2860 GNUNET_assert (NULL != cli_ctx);
2861 reply_cls->req_handle = NULL;
2862 destroy_reply_cls (reply_cls);
2863 GNUNET_MQ_send (cli_ctx->mq, ev);
2868 * Handle RPS request from the client.
2870 * @param cls closure
2871 * @param message the actual message
2874 handle_client_request (void *cls,
2875 const struct GNUNET_RPS_CS_RequestMessage *msg)
2877 struct ClientContext *cli_ctx = cls;
2879 uint32_t size_needed;
2880 struct ReplyCls *reply_cls;
2883 num_peers = ntohl (msg->num_peers);
2884 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
2885 num_peers * sizeof (struct GNUNET_PeerIdentity);
2887 if (GNUNET_MAX_MESSAGE_SIZE < size_needed)
2889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2890 "Message received from client has size larger than expected\n");
2891 GNUNET_SERVICE_client_drop (cli_ctx->client);
2895 for (i = 0 ; i < num_peers ; i++)
2898 LOG (GNUNET_ERROR_TYPE_DEBUG,
2899 "Client requested %" PRIu32 " random peer(s).\n",
2902 reply_cls = GNUNET_new (struct ReplyCls);
2903 reply_cls->id = ntohl (msg->id);
2904 reply_cls->cli_ctx = cli_ctx;
2905 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
2910 GNUNET_assert (NULL != cli_ctx);
2911 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
2912 cli_ctx->rep_cls_tail,
2914 GNUNET_SERVICE_client_continue (cli_ctx->client);
2919 * @brief Handle a message that requests the cancellation of a request
2922 * @param message the message containing the id of the request
2925 handle_client_request_cancel (void *cls,
2926 const struct GNUNET_RPS_CS_RequestCancelMessage *msg)
2928 struct ClientContext *cli_ctx = cls;
2929 struct ReplyCls *rep_cls;
2931 GNUNET_assert (NULL != cli_ctx);
2932 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
2933 rep_cls = cli_ctx->rep_cls_head;
2934 LOG (GNUNET_ERROR_TYPE_DEBUG,
2935 "Client cancels request with id %" PRIu32 "\n",
2937 while ( (NULL != rep_cls->next) &&
2938 (rep_cls->id != ntohl (msg->id)) )
2939 rep_cls = rep_cls->next;
2940 GNUNET_assert (rep_cls->id == ntohl (msg->id));
2941 destroy_reply_cls (rep_cls);
2942 GNUNET_SERVICE_client_continue (cli_ctx->client);
2947 * @brief This function is called, when the client seeds peers.
2948 * It verifies that @a msg is well-formed.
2950 * @param cls the closure (#ClientContext)
2951 * @param msg the message
2952 * @return #GNUNET_OK if @a msg is well-formed
2955 check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
2957 struct ClientContext *cli_ctx = cls;
2958 uint16_t msize = ntohs (msg->header.size);
2959 uint32_t num_peers = ntohl (msg->num_peers);
2961 msize -= sizeof (struct GNUNET_RPS_CS_SeedMessage);
2962 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
2963 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
2966 GNUNET_SERVICE_client_drop (cli_ctx->client);
2967 return GNUNET_SYSERR;
2974 * Handle seed from the client.
2976 * @param cls closure
2977 * @param message the actual message
2980 handle_client_seed (void *cls,
2981 const struct GNUNET_RPS_CS_SeedMessage *msg)
2983 struct ClientContext *cli_ctx = cls;
2984 struct GNUNET_PeerIdentity *peers;
2988 num_peers = ntohl (msg->num_peers);
2989 peers = (struct GNUNET_PeerIdentity *) &msg[1];
2990 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
2991 //GNUNET_memcpy (peers, &msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
2993 LOG (GNUNET_ERROR_TYPE_DEBUG,
2994 "Client seeded peers:\n");
2995 print_peer_list (peers, num_peers);
2997 for (i = 0; i < num_peers; i++)
2999 LOG (GNUNET_ERROR_TYPE_DEBUG,
3000 "Updating samplers with seed %" PRIu32 ": %s\n",
3002 GNUNET_i2s (&peers[i]));
3004 got_peer (&peers[i]);
3007 ////GNUNET_free (peers);
3009 GNUNET_SERVICE_client_continue (cli_ctx->client);
3013 * @brief Send view to client
3015 * @param cli_ctx the context of the client
3016 * @param view_array the peerids of the view as array (can be empty)
3017 * @param view_size the size of the view array (can be 0)
3020 send_view (const struct ClientContext *cli_ctx,
3021 const struct GNUNET_PeerIdentity *view_array,
3024 struct GNUNET_MQ_Envelope *ev;
3025 struct GNUNET_RPS_CS_DEBUG_ViewReply *out_msg;
3027 if (NULL == view_array)
3029 view_size = View_size ();
3030 view_array = View_get_as_array();
3033 ev = GNUNET_MQ_msg_extra (out_msg,
3034 view_size * sizeof (struct GNUNET_PeerIdentity),
3035 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY);
3036 out_msg->num_peers = htonl (view_size);
3038 GNUNET_memcpy (&out_msg[1],
3040 view_size * sizeof (struct GNUNET_PeerIdentity));
3041 GNUNET_MQ_send (cli_ctx->mq, ev);
3045 * @brief sends updates to clients that are interested
3048 clients_notify_view_update (void)
3050 struct ClientContext *cli_ctx_iter;
3052 const struct GNUNET_PeerIdentity *view_array;
3054 num_peers = View_size ();
3055 view_array = View_get_as_array();
3056 /* check size of view is small enough */
3057 if (GNUNET_MAX_MESSAGE_SIZE < num_peers)
3059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3060 "View is too big to send\n");
3064 for (cli_ctx_iter = cli_ctx_head;
3065 NULL != cli_ctx_iter;
3066 cli_ctx_iter = cli_ctx_head->next)
3068 if (1 < cli_ctx_iter->view_updates_left)
3070 /* Client wants to receive limited amount of updates */
3071 cli_ctx_iter->view_updates_left -= 1;
3072 } else if (1 == cli_ctx_iter->view_updates_left)
3074 /* Last update of view for client */
3075 cli_ctx_iter->view_updates_left = -1;
3076 } else if (0 > cli_ctx_iter->view_updates_left) {
3077 /* Client is not interested in updates */
3080 /* else _updates_left == 0 - infinite amount of updates */
3083 send_view (cli_ctx_iter, view_array, num_peers);
3089 * Handle RPS request from the client.
3091 * @param cls closure
3092 * @param message the actual message
3095 handle_client_view_request (void *cls,
3096 const struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg)
3098 struct ClientContext *cli_ctx = cls;
3099 uint64_t num_updates;
3101 num_updates = ntohl (msg->num_updates);
3103 LOG (GNUNET_ERROR_TYPE_DEBUG,
3104 "Client requested %" PRIu64 " updates of view.\n",
3107 GNUNET_assert (NULL != cli_ctx);
3108 cli_ctx->view_updates_left = num_updates;
3109 send_view (cli_ctx, NULL, 0);
3110 GNUNET_SERVICE_client_continue (cli_ctx->client);
3114 * Handle a CHECK_LIVE message from another peer.
3116 * This does nothing. But without calling #GNUNET_CADET_receive_done()
3117 * the channel is blocked for all other communication.
3119 * @param cls Closure
3120 * @param msg The message header
3123 handle_peer_check (void *cls,
3124 const struct GNUNET_MessageHeader *msg)
3126 const struct GNUNET_PeerIdentity *peer = cls;
3127 LOG (GNUNET_ERROR_TYPE_DEBUG,
3128 "Received CHECK_LIVE (%s)\n", GNUNET_i2s (peer));
3130 GNUNET_break_op (Peers_check_peer_known (peer));
3131 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
3135 * Handle a PUSH message from another peer.
3137 * Check the proof of work and store the PeerID
3138 * in the temporary list for pushed PeerIDs.
3140 * @param cls Closure
3141 * @param msg The message header
3144 handle_peer_push (void *cls,
3145 const struct GNUNET_MessageHeader *msg)
3147 const struct GNUNET_PeerIdentity *peer = cls;
3149 // (check the proof of work (?))
3151 LOG (GNUNET_ERROR_TYPE_DEBUG,
3152 "Received PUSH (%s)\n",
3154 GNUNET_STATISTICS_update(stats, "# push message received", 1, GNUNET_NO);
3156 #ifdef ENABLE_MALICIOUS
3157 struct AttackedPeer *tmp_att_peer;
3159 if ( (1 == mal_type) ||
3161 { /* Try to maximise representation */
3162 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3163 tmp_att_peer->peer_id = *peer;
3164 if (NULL == att_peer_set)
3165 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
3167 GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3170 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3173 add_peer_array_to_set (peer, 1, att_peer_set);
3178 else if (2 == mal_type)
3180 /* We attack one single well-known peer - simply ignore */
3182 #endif /* ENABLE_MALICIOUS */
3184 /* Add the sending peer to the push_map */
3185 CustomPeerMap_put (push_map, peer);
3187 GNUNET_break_op (Peers_check_peer_known (peer));
3188 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
3193 * Handle PULL REQUEST request message from another peer.
3195 * Reply with the view of PeerIDs.
3197 * @param cls Closure
3198 * @param msg The message header
3201 handle_peer_pull_request (void *cls,
3202 const struct GNUNET_MessageHeader *msg)
3204 struct GNUNET_PeerIdentity *peer = cls;
3205 const struct GNUNET_PeerIdentity *view_array;
3207 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
3208 GNUNET_STATISTICS_update(stats, "# pull request message received", 1, GNUNET_NO);
3210 #ifdef ENABLE_MALICIOUS
3213 { /* Try to maximise representation */
3214 send_pull_reply (peer, mal_peers, num_mal_peers);
3217 else if (2 == mal_type)
3218 { /* Try to partition network */
3219 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
3221 send_pull_reply (peer, mal_peers, num_mal_peers);
3224 #endif /* ENABLE_MALICIOUS */
3226 GNUNET_break_op (Peers_check_peer_known (peer));
3227 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
3228 view_array = View_get_as_array ();
3229 send_pull_reply (peer, view_array, View_size ());
3234 * Check whether we sent a corresponding request and
3235 * whether this reply is the first one.
3237 * @param cls Closure
3238 * @param msg The message header
3241 check_peer_pull_reply (void *cls,
3242 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3244 struct GNUNET_PeerIdentity *sender = cls;
3246 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
3248 GNUNET_break_op (0);
3249 return GNUNET_SYSERR;
3252 if ((ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
3253 sizeof (struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
3255 LOG (GNUNET_ERROR_TYPE_ERROR,
3256 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3257 ntohl (msg->num_peers),
3258 (ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
3259 sizeof (struct GNUNET_PeerIdentity));
3260 GNUNET_break_op (0);
3261 return GNUNET_SYSERR;
3264 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
3266 LOG (GNUNET_ERROR_TYPE_WARNING,
3267 "Received a pull reply from a peer we didn't request one from!\n");
3268 GNUNET_break_op (0);
3269 return GNUNET_SYSERR;
3275 * Handle PULL REPLY message from another peer.
3277 * @param cls Closure
3278 * @param msg The message header
3281 handle_peer_pull_reply (void *cls,
3282 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3284 const struct GNUNET_PeerIdentity *peers;
3285 struct GNUNET_PeerIdentity *sender = cls;
3287 #ifdef ENABLE_MALICIOUS
3288 struct AttackedPeer *tmp_att_peer;
3289 #endif /* ENABLE_MALICIOUS */
3291 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
3292 GNUNET_STATISTICS_update(stats, "# pull reply messages received", 1, GNUNET_NO);
3294 #ifdef ENABLE_MALICIOUS
3295 // We shouldn't even receive pull replies as we're not sending
3299 #endif /* ENABLE_MALICIOUS */
3301 /* Do actual logic */
3302 peers = (const struct GNUNET_PeerIdentity *) &msg[1];
3304 LOG (GNUNET_ERROR_TYPE_DEBUG,
3305 "PULL REPLY received, got following %u peers:\n",
3306 ntohl (msg->num_peers));
3308 for (i = 0; i < ntohl (msg->num_peers); i++)
3310 LOG (GNUNET_ERROR_TYPE_DEBUG,
3313 GNUNET_i2s (&peers[i]));
3315 #ifdef ENABLE_MALICIOUS
3316 if ((NULL != att_peer_set) &&
3317 (1 == mal_type || 3 == mal_type))
3318 { /* Add attacked peer to local list */
3319 // TODO check if we sent a request and this was the first reply
3320 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3322 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
3324 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
3327 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3328 tmp_att_peer->peer_id = peers[i];
3329 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3332 add_peer_array_to_set (&peers[i], 1, att_peer_set);
3336 #endif /* ENABLE_MALICIOUS */
3337 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
3340 /* Make sure we 'know' about this peer */
3341 (void) Peers_insert_peer (&peers[i]);
3343 if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
3345 CustomPeerMap_put (pull_map, &peers[i]);
3349 Peers_schedule_operation (&peers[i], insert_in_pull_map);
3350 (void) Peers_issue_peer_liveliness_check (&peers[i]);
3355 Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING);
3356 clean_peer (sender);
3358 GNUNET_break_op (Peers_check_peer_known (sender));
3359 GNUNET_CADET_receive_done (Peers_get_recv_channel (sender));
3364 * Compute a random delay.
3365 * A uniformly distributed value between mean + spread and mean - spread.
3367 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
3368 * It would return a random value between 2 and 6 min.
3370 * @param mean the mean
3371 * @param spread the inverse amount of deviation from the mean
3373 static struct GNUNET_TIME_Relative
3374 compute_rand_delay (struct GNUNET_TIME_Relative mean,
3375 unsigned int spread)
3377 struct GNUNET_TIME_Relative half_interval;
3378 struct GNUNET_TIME_Relative ret;
3379 unsigned int rand_delay;
3380 unsigned int max_rand_delay;
3384 LOG (GNUNET_ERROR_TYPE_WARNING,
3385 "Not accepting spread of 0\n");
3389 GNUNET_assert (0 != mean.rel_value_us);
3391 /* Compute random time value between spread * mean and spread * mean */
3392 half_interval = GNUNET_TIME_relative_divide (mean, spread);
3394 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
3396 * Compute random value between (0 and 1) * round_interval
3397 * via multiplying round_interval with a 'fraction' (0 to value)/value
3399 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
3400 ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay);
3401 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
3402 ret = GNUNET_TIME_relative_add (ret, half_interval);
3404 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
3405 LOG (GNUNET_ERROR_TYPE_WARNING,
3406 "Returning FOREVER_REL\n");
3413 * Send single pull request
3415 * @param peer_id the peer to send the pull request to.
3418 send_pull_request (const struct GNUNET_PeerIdentity *peer)
3420 struct GNUNET_MQ_Envelope *ev;
3422 GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer,
3423 Peers_PULL_REPLY_PENDING));
3424 Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING);
3426 LOG (GNUNET_ERROR_TYPE_DEBUG,
3427 "Going to send PULL REQUEST to peer %s.\n",
3430 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
3431 Peers_send_message (peer, ev, "PULL REQUEST");
3432 GNUNET_STATISTICS_update(stats, "# pull request send issued", 1, GNUNET_NO);
3439 * @param peer_id the peer to send the push to.
3442 send_push (const struct GNUNET_PeerIdentity *peer_id)
3444 struct GNUNET_MQ_Envelope *ev;
3446 LOG (GNUNET_ERROR_TYPE_DEBUG,
3447 "Going to send PUSH to peer %s.\n",
3448 GNUNET_i2s (peer_id));
3450 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
3451 Peers_send_message (peer_id, ev, "PUSH");
3452 GNUNET_STATISTICS_update(stats, "# push send issued", 1, GNUNET_NO);
3457 do_round (void *cls);
3460 do_mal_round (void *cls);
3462 #ifdef ENABLE_MALICIOUS
3466 * @brief This function is called, when the client tells us to act malicious.
3467 * It verifies that @a msg is well-formed.
3469 * @param cls the closure (#ClientContext)
3470 * @param msg the message
3471 * @return #GNUNET_OK if @a msg is well-formed
3474 check_client_act_malicious (void *cls,
3475 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
3477 struct ClientContext *cli_ctx = cls;
3478 uint16_t msize = ntohs (msg->header.size);
3479 uint32_t num_peers = ntohl (msg->num_peers);
3481 msize -= sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage);
3482 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
3483 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
3485 LOG (GNUNET_ERROR_TYPE_ERROR,
3486 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3487 ntohl (msg->num_peers),
3488 (msize / sizeof (struct GNUNET_PeerIdentity)));
3490 GNUNET_SERVICE_client_drop (cli_ctx->client);
3491 return GNUNET_SYSERR;
3497 * Turn RPS service to act malicious.
3499 * @param cls Closure
3500 * @param client The client that sent the message
3501 * @param msg The message header
3504 handle_client_act_malicious (void *cls,
3505 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
3507 struct ClientContext *cli_ctx = cls;
3508 struct GNUNET_PeerIdentity *peers;
3509 uint32_t num_mal_peers_sent;
3510 uint32_t num_mal_peers_old;
3512 /* Do actual logic */
3513 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3514 mal_type = ntohl (msg->type);
3515 if (NULL == mal_peer_set)
3516 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
3518 LOG (GNUNET_ERROR_TYPE_DEBUG,
3519 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
3521 ntohl (msg->num_peers));
3524 { /* Try to maximise representation */
3525 /* Add other malicious peers to those we already know */
3527 num_mal_peers_sent = ntohl (msg->num_peers);
3528 num_mal_peers_old = num_mal_peers;
3529 GNUNET_array_grow (mal_peers,
3531 num_mal_peers + num_mal_peers_sent);
3532 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
3534 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
3536 /* Add all mal peers to mal_peer_set */
3537 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
3541 /* Substitute do_round () with do_mal_round () */
3542 GNUNET_SCHEDULER_cancel (do_round_task);
3543 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
3546 else if ( (2 == mal_type) ||
3548 { /* Try to partition the network */
3549 /* Add other malicious peers to those we already know */
3551 num_mal_peers_sent = ntohl (msg->num_peers) - 1;
3552 num_mal_peers_old = num_mal_peers;
3553 GNUNET_array_grow (mal_peers,
3555 num_mal_peers + num_mal_peers_sent);
3556 if (NULL != mal_peers &&
3559 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
3561 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
3563 /* Add all mal peers to mal_peer_set */
3564 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
3569 /* Store the one attacked peer */
3570 GNUNET_memcpy (&attacked_peer,
3571 &msg->attacked_peer,
3572 sizeof (struct GNUNET_PeerIdentity));
3573 /* Set the flag of the attacked peer to valid to avoid problems */
3574 if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
3576 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3579 LOG (GNUNET_ERROR_TYPE_DEBUG,
3580 "Attacked peer is %s\n",
3581 GNUNET_i2s (&attacked_peer));
3583 /* Substitute do_round () with do_mal_round () */
3584 GNUNET_SCHEDULER_cancel (do_round_task);
3585 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
3587 else if (0 == mal_type)
3588 { /* Stop acting malicious */
3589 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
3591 /* Substitute do_mal_round () with do_round () */
3592 GNUNET_SCHEDULER_cancel (do_round_task);
3593 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
3598 GNUNET_SERVICE_client_continue (cli_ctx->client);
3600 GNUNET_SERVICE_client_continue (cli_ctx->client);
3605 * Send out PUSHes and PULLs maliciously.
3607 * This is executed regylary.
3610 do_mal_round (void *cls)
3612 uint32_t num_pushes;
3614 struct GNUNET_TIME_Relative time_next_round;
3615 struct AttackedPeer *tmp_att_peer;
3617 LOG (GNUNET_ERROR_TYPE_DEBUG,
3618 "Going to execute next round maliciously type %" PRIu32 ".\n",
3620 do_round_task = NULL;
3621 GNUNET_assert (mal_type <= 3);
3622 /* Do malicious actions */
3624 { /* Try to maximise representation */
3626 /* The maximum of pushes we're going to send this round */
3627 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
3628 num_attacked_peers),
3629 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
3631 LOG (GNUNET_ERROR_TYPE_DEBUG,
3632 "Going to send %" PRIu32 " pushes\n",
3635 /* Send PUSHes to attacked peers */
3636 for (i = 0 ; i < num_pushes ; i++)
3638 if (att_peers_tail == att_peer_index)
3639 att_peer_index = att_peers_head;
3641 att_peer_index = att_peer_index->next;
3643 send_push (&att_peer_index->peer_id);
3646 /* Send PULLs to some peers to learn about additional peers to attack */
3647 tmp_att_peer = att_peer_index;
3648 for (i = 0 ; i < num_pushes * alpha ; i++)
3650 if (att_peers_tail == tmp_att_peer)
3651 tmp_att_peer = att_peers_head;
3653 att_peer_index = tmp_att_peer->next;
3655 send_pull_request (&tmp_att_peer->peer_id);
3660 else if (2 == mal_type)
3662 * Try to partition the network
3663 * Send as many pushes to the attacked peer as possible
3664 * That is one push per round as it will ignore more.
3666 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3667 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
3668 send_push (&attacked_peer);
3673 { /* Combined attack */
3675 /* Send PUSH to attacked peers */
3676 if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
3678 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3679 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
3681 LOG (GNUNET_ERROR_TYPE_DEBUG,
3682 "Goding to send push to attacked peer (%s)\n",
3683 GNUNET_i2s (&attacked_peer));
3684 send_push (&attacked_peer);
3687 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3689 /* The maximum of pushes we're going to send this round */
3690 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
3691 num_attacked_peers),
3692 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
3694 LOG (GNUNET_ERROR_TYPE_DEBUG,
3695 "Going to send %" PRIu32 " pushes\n",
3698 for (i = 0; i < num_pushes; i++)
3700 if (att_peers_tail == att_peer_index)
3701 att_peer_index = att_peers_head;
3703 att_peer_index = att_peer_index->next;
3705 send_push (&att_peer_index->peer_id);
3708 /* Send PULLs to some peers to learn about additional peers to attack */
3709 tmp_att_peer = att_peer_index;
3710 for (i = 0; i < num_pushes * alpha; i++)
3712 if (att_peers_tail == tmp_att_peer)
3713 tmp_att_peer = att_peers_head;
3715 att_peer_index = tmp_att_peer->next;
3717 send_pull_request (&tmp_att_peer->peer_id);
3721 /* Schedule next round */
3722 time_next_round = compute_rand_delay (round_interval, 2);
3724 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
3726 GNUNET_assert (NULL == do_round_task);
3727 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
3728 &do_mal_round, NULL);
3729 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
3731 #endif /* ENABLE_MALICIOUS */
3734 * Send out PUSHes and PULLs, possibly update #view, samplers.
3736 * This is executed regylary.
3739 do_round (void *cls)
3742 const struct GNUNET_PeerIdentity *view_array;
3743 unsigned int *permut;
3744 unsigned int a_peers; /* Number of peers we send pushes to */
3745 unsigned int b_peers; /* Number of peers we send pull requests to */
3746 uint32_t first_border;
3747 uint32_t second_border;
3748 struct GNUNET_PeerIdentity peer;
3749 struct GNUNET_PeerIdentity *update_peer;
3751 LOG (GNUNET_ERROR_TYPE_DEBUG,
3752 "Going to execute next round.\n");
3753 GNUNET_STATISTICS_update(stats, "# rounds", 1, GNUNET_NO);
3754 do_round_task = NULL;
3755 LOG (GNUNET_ERROR_TYPE_DEBUG,
3756 "Printing view:\n");
3757 to_file (file_name_view_log,
3758 "___ new round ___");
3759 view_array = View_get_as_array ();
3760 for (i = 0; i < View_size (); i++)
3762 LOG (GNUNET_ERROR_TYPE_DEBUG,
3763 "\t%s\n", GNUNET_i2s (&view_array[i]));
3764 to_file (file_name_view_log,
3766 GNUNET_i2s_full (&view_array[i]));
3770 /* Send pushes and pull requests */
3771 if (0 < View_size ())
3773 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3777 a_peers = ceil (alpha * View_size ());
3779 LOG (GNUNET_ERROR_TYPE_DEBUG,
3780 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
3781 a_peers, alpha, View_size ());
3782 for (i = 0; i < a_peers; i++)
3784 peer = view_array[permut[i]];
3785 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
3786 { // FIXME if this fails schedule/loop this for later
3791 /* Send PULL requests */
3792 b_peers = ceil (beta * View_size ());
3793 first_border = a_peers;
3794 second_border = a_peers + b_peers;
3795 if (second_border > View_size ())
3797 first_border = View_size () - b_peers;
3798 second_border = View_size ();
3800 LOG (GNUNET_ERROR_TYPE_DEBUG,
3801 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
3802 b_peers, beta, View_size ());
3803 for (i = first_border; i < second_border; i++)
3805 peer = view_array[permut[i]];
3806 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
3807 GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
3808 { // FIXME if this fails schedule/loop this for later
3809 send_pull_request (&peer);
3813 GNUNET_free (permut);
3819 /* TODO see how many peers are in push-/pull- list! */
3821 if ((CustomPeerMap_size (push_map) <= alpha * view_size_est_need) &&
3822 (0 < CustomPeerMap_size (push_map)) &&
3823 (0 < CustomPeerMap_size (pull_map)))
3824 //if (GNUNET_YES) // disable blocking temporarily
3825 { /* If conditions for update are fulfilled, update */
3826 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
3828 uint32_t final_size;
3829 uint32_t peers_to_clean_size;
3830 struct GNUNET_PeerIdentity *peers_to_clean;
3832 peers_to_clean = NULL;
3833 peers_to_clean_size = 0;
3834 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
3835 GNUNET_memcpy (peers_to_clean,
3837 View_size () * sizeof (struct GNUNET_PeerIdentity));
3839 /* Seems like recreating is the easiest way of emptying the peermap */
3841 to_file (file_name_view_log,
3844 first_border = GNUNET_MIN (ceil (alpha * view_size_est_need),
3845 CustomPeerMap_size (push_map));
3846 second_border = first_border +
3847 GNUNET_MIN (floor (beta * view_size_est_need),
3848 CustomPeerMap_size (pull_map));
3849 final_size = second_border +
3850 ceil ((1 - (alpha + beta)) * view_size_est_need);
3852 /* Update view with peers received through PUSHes */
3853 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3854 CustomPeerMap_size (push_map));
3855 for (i = 0; i < first_border; i++)
3857 (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
3859 to_file (file_name_view_log,
3861 GNUNET_i2s_full (&view_array[i]));
3862 // TODO change the peer_flags accordingly
3864 GNUNET_free (permut);
3867 /* Update view with peers received through PULLs */
3868 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3869 CustomPeerMap_size (pull_map));
3870 for (i = first_border; i < second_border; i++)
3872 (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
3873 permut[i - first_border]));
3874 to_file (file_name_view_log,
3876 GNUNET_i2s_full (&view_array[i]));
3877 // TODO change the peer_flags accordingly
3879 GNUNET_free (permut);
3882 /* Update view with peers from history */
3883 RPS_sampler_get_n_rand_peers (prot_sampler,
3886 final_size - second_border);
3887 // TODO change the peer_flags accordingly
3889 for (i = 0; i < View_size (); i++)
3890 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
3892 /* Clean peers that were removed from the view */
3893 for (i = 0; i < peers_to_clean_size; i++)
3895 to_file (file_name_view_log,
3897 GNUNET_i2s_full (&peers_to_clean[i]));
3898 clean_peer (&peers_to_clean[i]);
3899 //peer_destroy_channel_send (sender);
3902 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
3903 clients_notify_view_update();
3905 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
3906 GNUNET_STATISTICS_update(stats, "# rounds blocked", 1, GNUNET_NO);
3907 if (CustomPeerMap_size (push_map) > alpha * View_size () &&
3908 !(0 >= CustomPeerMap_size (pull_map)))
3909 GNUNET_STATISTICS_update(stats, "# rounds blocked - too many pushes", 1, GNUNET_NO);
3910 if (CustomPeerMap_size (push_map) > alpha * View_size () &&
3911 (0 >= CustomPeerMap_size (pull_map)))
3912 GNUNET_STATISTICS_update(stats, "# rounds blocked - too many pushes, no pull replies", 1, GNUNET_NO);
3913 if (0 >= CustomPeerMap_size (push_map) &&
3914 !(0 >= CustomPeerMap_size (pull_map)))
3915 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pushes", 1, GNUNET_NO);
3916 if (0 >= CustomPeerMap_size (push_map) &&
3917 (0 >= CustomPeerMap_size (pull_map)))
3918 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pushes, no pull replies", 1, GNUNET_NO);
3919 if (0 >= CustomPeerMap_size (pull_map) &&
3920 CustomPeerMap_size (push_map) > alpha * View_size () &&
3921 0 >= CustomPeerMap_size (push_map))
3922 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pull replies", 1, GNUNET_NO);
3924 // TODO independent of that also get some peers from CADET_get_peers()?
3925 GNUNET_STATISTICS_set (stats,
3926 "# peers in push map at end of round",
3927 CustomPeerMap_size (push_map),
3929 GNUNET_STATISTICS_set (stats,
3930 "# peers in pull map at end of round",
3931 CustomPeerMap_size (pull_map),
3933 GNUNET_STATISTICS_set (stats,
3934 "# peers in view at end of round",
3938 LOG (GNUNET_ERROR_TYPE_DEBUG,
3939 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
3940 CustomPeerMap_size (push_map),
3941 CustomPeerMap_size (pull_map),
3944 alpha * View_size ());
3946 /* Update samplers */
3947 for (i = 0; i < CustomPeerMap_size (push_map); i++)
3949 update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
3950 LOG (GNUNET_ERROR_TYPE_DEBUG,
3951 "Updating with peer %s from push list\n",
3952 GNUNET_i2s (update_peer));
3953 insert_in_sampler (NULL, update_peer);
3954 clean_peer (update_peer); /* This cleans only if it is not in the view */
3955 //peer_destroy_channel_send (sender);
3958 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
3960 LOG (GNUNET_ERROR_TYPE_DEBUG,
3961 "Updating with peer %s from pull list\n",
3962 GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
3963 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
3964 /* This cleans only if it is not in the view */
3965 clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
3966 //peer_destroy_channel_send (sender);
3970 /* Empty push/pull lists */
3971 CustomPeerMap_clear (push_map);
3972 CustomPeerMap_clear (pull_map);
3974 struct GNUNET_TIME_Relative time_next_round;
3976 time_next_round = compute_rand_delay (round_interval, 2);
3978 /* Schedule next round */
3979 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
3981 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
3986 * This is called from GNUNET_CADET_get_peers().
3988 * It is called on every peer(ID) that cadet somehow has contact with.
3989 * We use those to initialise the sampler.
3992 init_peer_cb (void *cls,
3993 const struct GNUNET_PeerIdentity *peer,
3994 int tunnel, // "Do we have a tunnel towards this peer?"
3995 unsigned int n_paths, // "Number of known paths towards this peer"
3996 unsigned int best_path) // "How long is the best path?
3997 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
4001 LOG (GNUNET_ERROR_TYPE_DEBUG,
4002 "Got peer_id %s from cadet\n",
4009 * @brief Iterator function over stored, valid peers.
4011 * We initialise the sampler with those.
4013 * @param cls the closure
4014 * @param peer the peer id
4015 * @return #GNUNET_YES if we should continue to
4017 * #GNUNET_NO if not.
4020 valid_peers_iterator (void *cls,
4021 const struct GNUNET_PeerIdentity *peer)
4025 LOG (GNUNET_ERROR_TYPE_DEBUG,
4026 "Got stored, valid peer %s\n",
4035 * Iterator over peers from peerinfo.
4037 * @param cls closure
4038 * @param peer id of the peer, NULL for last call
4039 * @param hello hello message for the peer (can be NULL)
4040 * @param error message
4043 process_peerinfo_peers (void *cls,
4044 const struct GNUNET_PeerIdentity *peer,
4045 const struct GNUNET_HELLO_Message *hello,
4046 const char *err_msg)
4050 LOG (GNUNET_ERROR_TYPE_DEBUG,
4051 "Got peer_id %s from peerinfo\n",
4059 * Task run during shutdown.
4064 shutdown_task (void *cls)
4066 struct ClientContext *client_ctx;
4067 struct ReplyCls *reply_cls;
4069 LOG (GNUNET_ERROR_TYPE_DEBUG,
4070 "RPS is going down\n");
4072 /* Clean all clients */
4073 for (client_ctx = cli_ctx_head;
4074 NULL != cli_ctx_head;
4075 client_ctx = cli_ctx_head)
4077 /* Clean pending requests to the sampler */
4078 for (reply_cls = client_ctx->rep_cls_head;
4079 NULL != client_ctx->rep_cls_head;
4080 reply_cls = client_ctx->rep_cls_head)
4082 RPS_sampler_request_cancel (reply_cls->req_handle);
4083 GNUNET_CONTAINER_DLL_remove (client_ctx->rep_cls_head,
4084 client_ctx->rep_cls_tail,
4086 GNUNET_free (reply_cls);
4088 GNUNET_CONTAINER_DLL_remove (cli_ctx_head, cli_ctx_tail, client_ctx);
4089 GNUNET_free (client_ctx);
4091 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
4092 GNUNET_PEERINFO_disconnect (peerinfo_handle);
4094 if (NULL != do_round_task)
4096 GNUNET_SCHEDULER_cancel (do_round_task);
4097 do_round_task = NULL;
4102 GNUNET_NSE_disconnect (nse);
4103 RPS_sampler_destroy (prot_sampler);
4104 RPS_sampler_destroy (client_sampler);
4105 GNUNET_CADET_close_port (cadet_port);
4106 GNUNET_CADET_disconnect (cadet_handle);
4108 CustomPeerMap_destroy (push_map);
4109 CustomPeerMap_destroy (pull_map);
4112 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4115 #ifdef ENABLE_MALICIOUS
4116 struct AttackedPeer *tmp_att_peer;
4117 GNUNET_free (file_name_view_log);
4118 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
4119 if (NULL != mal_peer_set)
4120 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
4121 if (NULL != att_peer_set)
4122 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
4123 while (NULL != att_peers_head)
4125 tmp_att_peer = att_peers_head;
4126 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
4128 #endif /* ENABLE_MALICIOUS */
4133 * Handle client connecting to the service.
4136 * @param client the new client
4137 * @param mq the message queue of @a client
4141 client_connect_cb (void *cls,
4142 struct GNUNET_SERVICE_Client *client,
4143 struct GNUNET_MQ_Handle *mq)
4145 struct ClientContext *cli_ctx;
4147 LOG (GNUNET_ERROR_TYPE_DEBUG,
4148 "Client connected\n");
4150 return client; /* Server was destroyed before a client connected. Shutting down */
4151 cli_ctx = GNUNET_new (struct ClientContext);
4152 cli_ctx->mq = GNUNET_SERVICE_client_get_mq (client);
4153 cli_ctx->view_updates_left = -1;
4154 cli_ctx->client = client;
4155 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
4162 * Callback called when a client disconnected from the service
4164 * @param cls closure for the service
4165 * @param c the client that disconnected
4166 * @param internal_cls should be equal to @a c
4169 client_disconnect_cb (void *cls,
4170 struct GNUNET_SERVICE_Client *client,
4173 struct ClientContext *cli_ctx = internal_cls;
4175 GNUNET_assert (client == cli_ctx->client);
4177 {/* shutdown task - destroy all clients */
4178 while (NULL != cli_ctx_head)
4179 destroy_cli_ctx (cli_ctx_head);
4182 { /* destroy this client */
4183 LOG (GNUNET_ERROR_TYPE_DEBUG,
4184 "Client disconnected. Destroy its context.\n");
4185 destroy_cli_ctx (cli_ctx);
4191 * Handle random peer sampling clients.
4193 * @param cls closure
4194 * @param c configuration to use
4195 * @param service the initialized service
4199 const struct GNUNET_CONFIGURATION_Handle *c,
4200 struct GNUNET_SERVICE_Handle *service)
4204 char* fn_valid_peers;
4205 struct GNUNET_HashCode port;
4207 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
4212 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
4213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4214 "STARTING SERVICE (rps) for peer [%s]\n",
4215 GNUNET_i2s (&own_identity));
4216 #ifdef ENABLE_MALICIOUS
4217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4218 "Malicious execution compiled in.\n");
4219 #endif /* ENABLE_MALICIOUS */
4223 /* Get time interval from the configuration */
4224 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
4228 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4229 "RPS", "ROUNDINTERVAL");
4230 GNUNET_SCHEDULER_shutdown ();
4234 /* Get initial size of sampler/view from the configuration */
4236 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "MINSIZE",
4237 (long long unsigned int *) &sampler_size_est_min))
4239 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4241 GNUNET_SCHEDULER_shutdown ();
4244 sampler_size_est_need = sampler_size_est_min;
4245 view_size_est_min = sampler_size_est_min;
4246 LOG (GNUNET_ERROR_TYPE_DEBUG, "MINSIZE is %u\n", sampler_size_est_min);
4249 GNUNET_CONFIGURATION_get_value_filename (cfg,
4251 "FILENAME_VALID_PEERS",
4254 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4255 "rps", "FILENAME_VALID_PEERS");
4261 /* file_name_view_log */
4262 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
4264 LOG (GNUNET_ERROR_TYPE_WARNING,
4265 "Failed to create directory /tmp/rps/\n");
4268 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
4269 file_name_view_log = GNUNET_malloc (size);
4270 out_size = GNUNET_snprintf (file_name_view_log,
4273 GNUNET_i2s_full (&own_identity));
4274 if (size < out_size ||
4277 LOG (GNUNET_ERROR_TYPE_WARNING,
4278 "Failed to write string to buffer (size: %i, out_size: %i)\n",
4284 /* connect to NSE */
4285 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
4292 /* Initialise cadet */
4293 /* There exists a copy-paste-clone in get_channel() */
4294 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
4295 GNUNET_MQ_hd_fixed_size (peer_check,
4296 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
4297 struct GNUNET_MessageHeader,
4299 GNUNET_MQ_hd_fixed_size (peer_push,
4300 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
4301 struct GNUNET_MessageHeader,
4303 GNUNET_MQ_hd_fixed_size (peer_pull_request,
4304 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
4305 struct GNUNET_MessageHeader,
4307 GNUNET_MQ_hd_var_size (peer_pull_reply,
4308 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
4309 struct GNUNET_RPS_P2P_PullReplyMessage,
4311 GNUNET_MQ_handler_end ()
4314 cadet_handle = GNUNET_CADET_connect (cfg);
4315 GNUNET_assert (NULL != cadet_handle);
4316 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
4317 strlen (GNUNET_APPLICATION_PORT_RPS),
4319 cadet_port = GNUNET_CADET_open_port (cadet_handle,
4321 &Peers_handle_inbound_channel, /* Connect handler */
4323 NULL, /* WindowSize handler */
4324 cleanup_destroyed_channel, /* Disconnect handler */
4328 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
4329 Peers_initialise (fn_valid_peers, cadet_handle, &own_identity);
4330 GNUNET_free (fn_valid_peers);
4332 /* Initialise sampler */
4333 struct GNUNET_TIME_Relative half_round_interval;
4334 struct GNUNET_TIME_Relative max_round_interval;
4336 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
4337 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
4339 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
4340 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
4342 /* Initialise push and pull maps */
4343 push_map = CustomPeerMap_create (4);
4344 pull_map = CustomPeerMap_create (4);
4347 //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
4348 //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
4349 // TODO send push/pull to each of those peers?
4350 // TODO read stored valid peers from last run
4351 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
4352 Peers_get_valid_peers (valid_peers_iterator, NULL);
4354 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
4356 process_peerinfo_peers,
4359 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
4361 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
4362 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
4364 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
4365 stats = GNUNET_STATISTICS_create ("rps", cfg);
4371 * Define "main" method using service macro.
4375 GNUNET_SERVICE_OPTION_NONE,
4378 &client_disconnect_cb,
4380 GNUNET_MQ_hd_fixed_size (client_request,
4381 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
4382 struct GNUNET_RPS_CS_RequestMessage,
4384 GNUNET_MQ_hd_fixed_size (client_request_cancel,
4385 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
4386 struct GNUNET_RPS_CS_RequestCancelMessage,
4388 GNUNET_MQ_hd_var_size (client_seed,
4389 GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
4390 struct GNUNET_RPS_CS_SeedMessage,
4392 #ifdef ENABLE_MALICIOUS
4393 GNUNET_MQ_hd_var_size (client_act_malicious,
4394 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
4395 struct GNUNET_RPS_CS_ActMaliciousMessage,
4397 #endif /* ENABLE_MALICIOUS */
4398 GNUNET_MQ_hd_fixed_size (client_view_request,
4399 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST,
4400 struct GNUNET_RPS_CS_DEBUG_ViewRequest,
4402 GNUNET_MQ_handler_end());
4404 /* end of gnunet-service-rps.c */