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"
33 #include "rps-test_util.h"
34 #include "gnunet-service-rps_sampler.h"
35 #include "gnunet-service-rps_custommap.h"
36 #include "gnunet-service-rps_view.h"
41 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
43 // TODO modify @brief in every file
45 // TODO check for overflows
47 // TODO align message structs
49 // TODO connect to friends
51 // TODO store peers somewhere persistent
53 // TODO blacklist? (-> mal peer detection on top of brahms)
55 // hist_size_init, hist_size_max
60 static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 static struct GNUNET_PeerIdentity own_identity;
69 /***********************************************************************
70 * Old gnunet-service-rps_peers.c
71 ***********************************************************************/
74 * Set a peer flag of given peer context.
76 #define set_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask))
79 * Get peer flag of given peer context.
81 #define check_peer_flag_set(peer_ctx, mask)\
82 ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
85 * Unset flag of given peer context.
87 #define unset_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask))
90 * Set a channel flag of given channel context.
92 #define set_channel_flag(channel_flags, mask) ((*channel_flags) |= (mask))
95 * Get channel flag of given channel context.
97 #define check_channel_flag_set(channel_flags, mask)\
98 ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
101 * Unset flag of given channel context.
103 #define unset_channel_flag(channel_flags, mask) ((*channel_flags) &= ~(mask))
108 * Pending operation on peer consisting of callback and closure
110 * When an operation cannot be executed right now this struct is used to store
111 * the callback and closure for later execution.
127 * List containing all messages that are yet to be send
129 * This is used to keep track of all messages that have not been sent yet. When
130 * a peer is to be removed the pending messages can be removed properly.
132 struct PendingMessage
137 struct PendingMessage *next;
138 struct PendingMessage *prev;
141 * The envelope to the corresponding message
143 struct GNUNET_MQ_Envelope *ev;
146 * The corresponding context
148 struct PeerContext *peer_ctx;
157 * Struct used to keep track of other peer's status
159 * This is stored in a multipeermap.
160 * It contains information such as cadet channels, a message queue for sending,
161 * status about the channels, the pending operations on this peer and some flags
162 * about the status of the peer itself. (live, valid, ...)
167 * Message queue open to client
169 struct GNUNET_MQ_Handle *mq;
172 * Channel open to client.
174 struct GNUNET_CADET_Channel *send_channel;
177 * Flags to the sending channel
179 uint32_t *send_channel_flags;
182 * Channel open from client.
184 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
187 * Flags to the receiving channel
189 uint32_t *recv_channel_flags;
192 * Array of pending operations on this peer.
194 struct PeerPendingOp *pending_ops;
197 * Handle to the callback given to cadet_ntfy_tmt_rdy()
199 * To be canceled on shutdown.
201 struct PendingMessage *liveliness_check_pending;
204 * Number of pending operations.
206 unsigned int num_pending_ops;
209 * Identity of the peer
211 struct GNUNET_PeerIdentity peer_id;
214 * Flags indicating status of peer
219 * Last time we received something from that peer.
221 struct GNUNET_TIME_Absolute last_message_recv;
224 * Last time we received a keepalive message.
226 struct GNUNET_TIME_Absolute last_keepalive;
229 * DLL with all messages that are yet to be sent
231 struct PendingMessage *pending_messages_head;
232 struct PendingMessage *pending_messages_tail;
235 * This is pobably followed by 'statistical' data (when we first saw
236 * him, how did we get his ID, how many pushes (in a timeinterval),
242 * @brief Closure to #valid_peer_iterator
244 struct PeersIteratorCls
249 PeersIterator iterator;
252 * Closure to iterator
258 * @brief Hashmap of valid peers.
260 static struct GNUNET_CONTAINER_MultiPeerMap *valid_peers;
263 * @brief Maximum number of valid peers to keep.
264 * TODO read from config
266 static uint32_t num_valid_peers_max = UINT32_MAX;
269 * @brief Filename of the file that stores the valid peers persistently.
271 static char *filename_valid_peers;
274 * Set of all peers to keep track of them.
276 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
281 static struct GNUNET_CADET_Handle *cadet_handle;
286 * @brief Get the #PeerContext associated with a peer
288 * @param peer the peer id
290 * @return the #PeerContext
292 static struct PeerContext *
293 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
295 struct PeerContext *ctx;
298 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
299 GNUNET_assert (GNUNET_YES == ret);
300 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
301 GNUNET_assert (NULL != ctx);
306 Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer);
309 * @brief Create a new #PeerContext and insert it into the peer map
311 * @param peer the peer to create the #PeerContext for
313 * @return the #PeerContext
315 static struct PeerContext *
316 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
318 struct PeerContext *ctx;
321 GNUNET_assert (GNUNET_NO == Peers_check_peer_known (peer));
323 ctx = GNUNET_new (struct PeerContext);
324 ctx->peer_id = *peer;
325 ctx->send_channel_flags = GNUNET_new (uint32_t);
326 ctx->recv_channel_flags = GNUNET_new (uint32_t);
327 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
328 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
329 GNUNET_assert (GNUNET_OK == ret);
335 * @brief Create or get a #PeerContext
337 * @param peer the peer to get the associated context to
339 * @return the context
341 static struct PeerContext *
342 create_or_get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
344 if (GNUNET_NO == Peers_check_peer_known (peer))
346 return create_peer_ctx (peer);
348 return get_peer_ctx (peer);
352 Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
355 Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
358 * @brief Check whether we have a connection to this @a peer
360 * Also sets the #Peers_ONLINE flag accordingly
362 * @param peer the peer in question
364 * @return #GNUNET_YES if we are connected
365 * #GNUNET_NO otherwise
368 Peers_check_connected (const struct GNUNET_PeerIdentity *peer)
370 const struct PeerContext *peer_ctx;
372 /* If we don't know about this peer we don't know whether it's online */
373 if (GNUNET_NO == Peers_check_peer_known (peer))
377 /* Get the context */
378 peer_ctx = get_peer_ctx (peer);
379 /* If we have no channel to this peer we don't know whether it's online */
380 if ( (NULL == peer_ctx->send_channel) &&
381 (NULL == peer_ctx->recv_channel) )
383 Peers_unset_peer_flag (peer, Peers_ONLINE);
386 /* Otherwise (if we have a channel, we know that it's online */
387 Peers_set_peer_flag (peer, Peers_ONLINE);
393 * @brief The closure to #get_rand_peer_iterator.
395 struct GetRandPeerIteratorCls
398 * @brief The index of the peer to return.
399 * Will be decreased until 0.
400 * Then current peer is returned.
405 * @brief Pointer to peer to return.
407 const struct GNUNET_PeerIdentity *peer;
412 * @brief Iterator function for #get_random_peer_from_peermap.
414 * Implements #GNUNET_CONTAINER_PeerMapIterator.
415 * Decreases the index until the index is null.
416 * Then returns the current peer.
418 * @param cls the #GetRandPeerIteratorCls containing index and peer
419 * @param peer current peer
420 * @param value unused
422 * @return #GNUNET_YES if we should continue to
427 get_rand_peer_iterator (void *cls,
428 const struct GNUNET_PeerIdentity *peer,
431 struct GetRandPeerIteratorCls *iterator_cls = cls;
432 if (0 >= iterator_cls->index)
434 iterator_cls->peer = peer;
437 iterator_cls->index--;
443 * @brief Get a random peer from @a peer_map
445 * @param peer_map the peer_map to get the peer from
447 * @return a random peer
449 static const struct GNUNET_PeerIdentity *
450 get_random_peer_from_peermap (const struct
451 GNUNET_CONTAINER_MultiPeerMap *peer_map)
453 struct GetRandPeerIteratorCls *iterator_cls;
454 const struct GNUNET_PeerIdentity *ret;
456 iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls);
457 iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
458 GNUNET_CONTAINER_multipeermap_size (peer_map));
459 (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
460 get_rand_peer_iterator,
462 ret = iterator_cls->peer;
463 GNUNET_free (iterator_cls);
469 * @brief Add a given @a peer to valid peers.
471 * If valid peers are already #num_valid_peers_max, delete a peer previously.
473 * @param peer the peer that is added to the valid peers.
475 * @return #GNUNET_YES if no other peer had to be removed
476 * #GNUNET_NO otherwise
479 add_valid_peer (const struct GNUNET_PeerIdentity *peer)
481 const struct GNUNET_PeerIdentity *rand_peer;
485 while (GNUNET_CONTAINER_multipeermap_size (valid_peers) >= num_valid_peers_max)
487 rand_peer = get_random_peer_from_peermap (valid_peers);
488 GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer);
491 (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL,
492 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
498 * @brief Set the peer flag to living and
499 * call the pending operations on this peer.
501 * Also adds peer to #valid_peers.
503 * @param peer_ctx the #PeerContext of the peer to set live
506 set_peer_live (struct PeerContext *peer_ctx)
508 struct GNUNET_PeerIdentity *peer;
511 peer = &peer_ctx->peer_id;
512 LOG (GNUNET_ERROR_TYPE_DEBUG,
513 "Peer %s is live and valid, calling %i pending operations on it\n",
515 peer_ctx->num_pending_ops);
517 if (NULL != peer_ctx->liveliness_check_pending)
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 "Removing pending liveliness check for peer %s\n",
521 GNUNET_i2s (&peer_ctx->peer_id));
522 // TODO wait until cadet sets mq->cancel_impl
523 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
524 GNUNET_free (peer_ctx->liveliness_check_pending);
525 peer_ctx->liveliness_check_pending = NULL;
528 (void) add_valid_peer (peer);
529 set_peer_flag (peer_ctx, Peers_ONLINE);
531 /* Call pending operations */
532 for (i = 0; i < peer_ctx->num_pending_ops; i++)
534 peer_ctx->pending_ops[i].op (peer_ctx->pending_ops[i].op_cls, peer);
536 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
540 cleanup_destroyed_channel (void *cls,
541 const struct GNUNET_CADET_Channel *channel);
543 /* Declaration of handlers */
545 handle_peer_check (void *cls,
546 const struct GNUNET_MessageHeader *msg);
549 handle_peer_push (void *cls,
550 const struct GNUNET_MessageHeader *msg);
553 handle_peer_pull_request (void *cls,
554 const struct GNUNET_MessageHeader *msg);
557 check_peer_pull_reply (void *cls,
558 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
561 handle_peer_pull_reply (void *cls,
562 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
564 /* End declaration of handlers */
568 * @brief Get the channel of a peer. If not existing, create.
570 * @param peer the peer id
571 * @return the #GNUNET_CADET_Channel used to send data to @a peer
573 struct GNUNET_CADET_Channel *
574 get_channel (const struct GNUNET_PeerIdentity *peer)
576 struct PeerContext *peer_ctx;
577 struct GNUNET_HashCode port;
578 /* There exists a copy-paste-clone in run() */
579 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
580 GNUNET_MQ_hd_fixed_size (peer_check,
581 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
582 struct GNUNET_MessageHeader,
584 GNUNET_MQ_hd_fixed_size (peer_push,
585 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
586 struct GNUNET_MessageHeader,
588 GNUNET_MQ_hd_fixed_size (peer_pull_request,
589 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
590 struct GNUNET_MessageHeader,
592 GNUNET_MQ_hd_var_size (peer_pull_reply,
593 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
594 struct GNUNET_RPS_P2P_PullReplyMessage,
596 GNUNET_MQ_handler_end ()
600 peer_ctx = get_peer_ctx (peer);
601 if (NULL == peer_ctx->send_channel)
603 LOG (GNUNET_ERROR_TYPE_DEBUG,
604 "Trying to establish channel to peer %s\n",
606 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
607 strlen (GNUNET_APPLICATION_PORT_RPS),
609 peer_ctx->send_channel =
610 GNUNET_CADET_channel_create (cadet_handle,
611 (struct GNUNET_PeerIdentity *) peer, /* context */
614 GNUNET_CADET_OPTION_RELIABLE,
615 NULL, /* WindowSize handler */
616 cleanup_destroyed_channel, /* Disconnect handler */
619 GNUNET_assert (NULL != peer_ctx->send_channel);
620 return peer_ctx->send_channel;
625 * Get the message queue (#GNUNET_MQ_Handle) of a specific peer.
627 * If we already have a message queue open to this client,
628 * simply return it, otherways create one.
630 * @param peer the peer to get the mq to
631 * @return the #GNUNET_MQ_Handle
633 static struct GNUNET_MQ_Handle *
634 get_mq (const struct GNUNET_PeerIdentity *peer)
636 struct PeerContext *peer_ctx;
638 peer_ctx = get_peer_ctx (peer);
640 if (NULL == peer_ctx->mq)
642 (void) get_channel (peer);
643 peer_ctx->mq = GNUNET_CADET_get_mq (peer_ctx->send_channel);
650 * @brief This is called in response to the first message we sent as a
653 * @param cls #PeerContext of peer with pending liveliness check
656 mq_liveliness_check_successful (void *cls)
658 struct PeerContext *peer_ctx = cls;
660 if (NULL != peer_ctx->liveliness_check_pending)
662 LOG (GNUNET_ERROR_TYPE_DEBUG,
663 "Liveliness check for peer %s was successfull\n",
664 GNUNET_i2s (&peer_ctx->peer_id));
665 GNUNET_free (peer_ctx->liveliness_check_pending);
666 peer_ctx->liveliness_check_pending = NULL;
667 set_peer_live (peer_ctx);
672 * Issue a check whether peer is live
674 * @param peer_ctx the context of the peer
677 check_peer_live (struct PeerContext *peer_ctx)
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "Get informed about peer %s getting live\n",
681 GNUNET_i2s (&peer_ctx->peer_id));
683 struct GNUNET_MQ_Handle *mq;
684 struct GNUNET_MQ_Envelope *ev;
686 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE);
687 peer_ctx->liveliness_check_pending = GNUNET_new (struct PendingMessage);
688 peer_ctx->liveliness_check_pending->ev = ev;
689 peer_ctx->liveliness_check_pending->peer_ctx = peer_ctx;
690 peer_ctx->liveliness_check_pending->type = "Check liveliness";
691 mq = get_mq (&peer_ctx->peer_id);
692 GNUNET_MQ_notify_sent (ev,
693 mq_liveliness_check_successful,
695 GNUNET_MQ_send (mq, ev);
699 * @brief Add an envelope to a message passed to mq to list of pending messages
701 * @param peer peer the message was sent to
702 * @param ev envelope to the message
703 * @param type type of the message to be sent
704 * @return pointer to pending message
706 static struct PendingMessage *
707 insert_pending_message (const struct GNUNET_PeerIdentity *peer,
708 struct GNUNET_MQ_Envelope *ev,
711 struct PendingMessage *pending_msg;
712 struct PeerContext *peer_ctx;
714 peer_ctx = get_peer_ctx (peer);
715 pending_msg = GNUNET_new (struct PendingMessage);
716 pending_msg->ev = ev;
717 pending_msg->peer_ctx = peer_ctx;
718 pending_msg->type = type;
719 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
720 peer_ctx->pending_messages_tail,
727 * @brief Remove a pending message from the respective DLL
729 * @param pending_msg the pending message to remove
730 * @param cancel cancel the pending message, too
733 remove_pending_message (struct PendingMessage *pending_msg, int cancel)
735 struct PeerContext *peer_ctx;
737 peer_ctx = pending_msg->peer_ctx;
738 GNUNET_assert (NULL != peer_ctx);
739 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
740 peer_ctx->pending_messages_tail,
742 // TODO wait for the cadet implementation of message cancellation
743 //if (GNUNET_YES == cancel)
745 // GNUNET_MQ_send_cancel (pending_msg->ev);
747 GNUNET_free (pending_msg);
752 * @brief Check whether function of type #PeerOp was already scheduled
754 * The array with pending operations will probably never grow really big, so
755 * iterating over it should be ok.
757 * @param peer the peer to check
758 * @param peer_op the operation (#PeerOp) on the peer
760 * @return #GNUNET_YES if this operation is scheduled on that peer
761 * #GNUNET_NO otherwise
764 check_operation_scheduled (const struct GNUNET_PeerIdentity *peer,
765 const PeerOp peer_op)
767 const struct PeerContext *peer_ctx;
770 peer_ctx = get_peer_ctx (peer);
771 for (i = 0; i < peer_ctx->num_pending_ops; i++)
772 if (peer_op == peer_ctx->pending_ops[i].op)
778 Peers_remove_peer (const struct GNUNET_PeerIdentity *peer);
781 * Iterator over hash map entries. Deletes all contexts of peers.
784 * @param key current public key
785 * @param value value in the hash map
786 * @return #GNUNET_YES if we should continue to iterate,
790 peermap_clear_iterator (void *cls,
791 const struct GNUNET_PeerIdentity *key,
794 Peers_remove_peer (key);
800 * @brief This is called once a message is sent.
802 * Removes the pending message
804 * @param cls type of the message that was sent
807 mq_notify_sent_cb (void *cls)
809 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
810 LOG (GNUNET_ERROR_TYPE_DEBUG,
813 /* Do not cancle message */
814 remove_pending_message (pending_msg, GNUNET_NO);
819 * @brief Iterator function for #store_valid_peers.
821 * Implements #GNUNET_CONTAINER_PeerMapIterator.
822 * Writes single peer to disk.
824 * @param cls the file handle to write to.
825 * @param peer current peer
826 * @param value unused
828 * @return #GNUNET_YES if we should continue to
833 store_peer_presistently_iterator (void *cls,
834 const struct GNUNET_PeerIdentity *peer,
837 const struct GNUNET_DISK_FileHandle *fh = cls;
838 char peer_string[128];
846 size = GNUNET_snprintf (peer_string,
847 sizeof (peer_string),
849 GNUNET_i2s_full (peer));
850 GNUNET_assert (53 == size);
851 ret = GNUNET_DISK_file_write (fh,
854 GNUNET_assert (size == ret);
860 * @brief Store the peers currently in #valid_peers to disk.
865 struct GNUNET_DISK_FileHandle *fh;
866 uint32_t number_written_peers;
869 if (0 == strncmp ("DISABLE", filename_valid_peers, 7))
874 ret = GNUNET_DISK_directory_create_for_file (filename_valid_peers);
875 if (GNUNET_SYSERR == ret)
877 LOG (GNUNET_ERROR_TYPE_WARNING,
878 "Not able to create directory for file `%s'\n",
879 filename_valid_peers);
882 else if (GNUNET_NO == ret)
884 LOG (GNUNET_ERROR_TYPE_WARNING,
885 "Directory for file `%s' exists but is not writable for us\n",
886 filename_valid_peers);
889 fh = GNUNET_DISK_file_open (filename_valid_peers,
890 GNUNET_DISK_OPEN_WRITE |
891 GNUNET_DISK_OPEN_CREATE,
892 GNUNET_DISK_PERM_USER_READ |
893 GNUNET_DISK_PERM_USER_WRITE);
896 LOG (GNUNET_ERROR_TYPE_WARNING,
897 "Not able to write valid peers to file `%s'\n",
898 filename_valid_peers);
901 LOG (GNUNET_ERROR_TYPE_DEBUG,
902 "Writing %u valid peers to disk\n",
903 GNUNET_CONTAINER_multipeermap_size (valid_peers));
904 number_written_peers =
905 GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
906 store_peer_presistently_iterator,
908 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
909 GNUNET_assert (number_written_peers ==
910 GNUNET_CONTAINER_multipeermap_size (valid_peers));
915 * @brief Convert string representation of peer id to peer id.
917 * Counterpart to #GNUNET_i2s_full.
919 * @param string_repr The string representation of the peer id
921 * @return The peer id
923 static const struct GNUNET_PeerIdentity *
924 s2i_full (const char *string_repr)
926 struct GNUNET_PeerIdentity *peer;
930 peer = GNUNET_new (struct GNUNET_PeerIdentity);
931 len = strlen (string_repr);
934 LOG (GNUNET_ERROR_TYPE_WARNING,
935 "Not able to convert string representation of PeerID to PeerID\n"
936 "Sting representation: %s (len %lu) - too short\n",
945 ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr,
948 if (GNUNET_OK != ret)
950 LOG (GNUNET_ERROR_TYPE_WARNING,
951 "Not able to convert string representation of PeerID to PeerID\n"
952 "Sting representation: %s\n",
961 * @brief Restore the peers on disk to #valid_peers.
964 restore_valid_peers ()
968 struct GNUNET_DISK_FileHandle *fh;
973 const struct GNUNET_PeerIdentity *peer;
975 if (0 == strncmp ("DISABLE", filename_valid_peers, 7))
980 if (GNUNET_OK != GNUNET_DISK_file_test (filename_valid_peers))
984 fh = GNUNET_DISK_file_open (filename_valid_peers,
985 GNUNET_DISK_OPEN_READ,
986 GNUNET_DISK_PERM_NONE);
987 GNUNET_assert (NULL != fh);
988 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size));
989 num_peers = file_size / 53;
990 buf = GNUNET_malloc (file_size);
991 size_read = GNUNET_DISK_file_read (fh, buf, file_size);
992 GNUNET_assert (size_read == file_size);
993 LOG (GNUNET_ERROR_TYPE_DEBUG,
994 "Restoring %" PRIu32 " peers from file `%s'\n",
996 filename_valid_peers);
997 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53)
999 str_repr = GNUNET_strndup (iter_buf, 53);
1000 peer = s2i_full (str_repr);
1001 GNUNET_free (str_repr);
1002 add_valid_peer (peer);
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Restored valid peer %s from disk\n",
1005 GNUNET_i2s_full (peer));
1009 LOG (GNUNET_ERROR_TYPE_DEBUG,
1010 "num_peers: %" PRIu32 ", _size (valid_peers): %u\n",
1012 GNUNET_CONTAINER_multipeermap_size (valid_peers));
1013 if (num_peers != GNUNET_CONTAINER_multipeermap_size (valid_peers))
1015 LOG (GNUNET_ERROR_TYPE_WARNING,
1016 "Number of restored peers does not match file size. Have probably duplicates.\n");
1018 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1019 LOG (GNUNET_ERROR_TYPE_DEBUG,
1020 "Restored %u valid peers from disk\n",
1021 GNUNET_CONTAINER_multipeermap_size (valid_peers));
1026 * @brief Initialise storage of peers
1028 * @param fn_valid_peers filename of the file used to store valid peer ids
1029 * @param cadet_h cadet handle
1030 * @param disconnect_handler Disconnect handler
1031 * @param own_id own peer identity
1034 Peers_initialise (char* fn_valid_peers,
1035 struct GNUNET_CADET_Handle *cadet_h,
1036 GNUNET_CADET_DisconnectEventHandler disconnect_handler,
1037 const struct GNUNET_PeerIdentity *own_id)
1039 filename_valid_peers = GNUNET_strdup (fn_valid_peers);
1040 cadet_handle = cadet_h;
1041 own_identity = *own_id;
1042 peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
1043 valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
1044 restore_valid_peers ();
1049 * @brief Delete storage of peers that was created with #Peers_initialise ()
1054 if (GNUNET_SYSERR ==
1055 GNUNET_CONTAINER_multipeermap_iterate (peer_map,
1056 peermap_clear_iterator,
1059 LOG (GNUNET_ERROR_TYPE_WARNING,
1060 "Iteration destroying peers was aborted.\n");
1062 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1063 store_valid_peers ();
1064 GNUNET_free (filename_valid_peers);
1065 GNUNET_CONTAINER_multipeermap_destroy (valid_peers);
1070 * Iterator over #valid_peers hash map entries.
1072 * @param cls closure - unused
1073 * @param peer current peer id
1074 * @param value value in the hash map - unused
1075 * @return #GNUNET_YES if we should continue to
1077 * #GNUNET_NO if not.
1080 valid_peer_iterator (void *cls,
1081 const struct GNUNET_PeerIdentity *peer,
1084 struct PeersIteratorCls *it_cls = cls;
1086 return it_cls->iterator (it_cls->cls,
1092 * @brief Get all currently known, valid peer ids.
1094 * @param it function to call on each peer id
1095 * @param it_cls extra argument to @a it
1096 * @return the number of key value pairs processed,
1097 * #GNUNET_SYSERR if it aborted iteration
1100 Peers_get_valid_peers (PeersIterator iterator,
1103 struct PeersIteratorCls *cls;
1106 cls = GNUNET_new (struct PeersIteratorCls);
1107 cls->iterator = iterator;
1109 ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
1110 valid_peer_iterator,
1118 * @brief Add peer to known peers.
1120 * This function is called on new peer_ids from 'external' sources
1121 * (client seed, cadet get_peers(), ...)
1123 * @param peer the new #GNUNET_PeerIdentity
1125 * @return #GNUNET_YES if peer was inserted
1126 * #GNUNET_NO otherwise (if peer was already known or
1127 * peer was #own_identity)
1130 Peers_insert_peer (const struct GNUNET_PeerIdentity *peer)
1132 if ( (GNUNET_YES == Peers_check_peer_known (peer)) ||
1133 (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity)) )
1135 return GNUNET_NO; /* We already know this peer - nothing to do */
1137 (void) create_peer_ctx (peer);
1142 Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags);
1145 * @brief Try connecting to a peer to see whether it is online
1147 * If not known yet, insert into known peers
1149 * @param peer the peer whose liveliness is to be checked
1150 * @return #GNUNET_YES if peer had to be inserted
1151 * #GNUNET_NO otherwise (if peer was already known or
1152 * peer was #own_identity)
1155 Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer)
1157 struct PeerContext *peer_ctx;
1160 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity))
1164 ret = Peers_insert_peer (peer);
1165 peer_ctx = get_peer_ctx (peer);
1166 if (GNUNET_NO == Peers_check_peer_flag (peer, Peers_ONLINE))
1168 check_peer_live (peer_ctx);
1175 * @brief Check if peer is removable.
1178 * - a recv channel exists
1179 * - there are pending messages
1180 * - there is no pending pull reply
1182 * @param peer the peer in question
1183 * @return #GNUNET_YES if peer is removable
1184 * #GNUNET_NO if peer is NOT removable
1185 * #GNUNET_SYSERR if peer is not known
1188 Peers_check_removable (const struct GNUNET_PeerIdentity *peer)
1190 struct PeerContext *peer_ctx;
1192 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1194 return GNUNET_SYSERR;
1197 peer_ctx = get_peer_ctx (peer);
1198 if ( (NULL != peer_ctx->recv_channel) ||
1199 (NULL != peer_ctx->pending_messages_head) ||
1200 (GNUNET_NO == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)) )
1208 Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1209 enum Peers_ChannelRole role);
1212 Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags);
1215 * @brief Remove peer
1217 * @param peer the peer to clean
1218 * @return #GNUNET_YES if peer was removed
1219 * #GNUNET_NO otherwise
1222 Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1224 struct PeerContext *peer_ctx;
1225 uint32_t *channel_flag;
1227 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1232 peer_ctx = get_peer_ctx (peer);
1233 set_peer_flag (peer_ctx, Peers_TO_DESTROY);
1234 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 "Going to remove peer %s\n",
1236 GNUNET_i2s (&peer_ctx->peer_id));
1237 Peers_unset_peer_flag (peer, Peers_ONLINE);
1239 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
1240 while (NULL != peer_ctx->pending_messages_head)
1242 LOG (GNUNET_ERROR_TYPE_DEBUG,
1243 "Removing unsent %s\n",
1244 peer_ctx->pending_messages_head->type);
1245 /* Cancle pending message, too */
1246 remove_pending_message (peer_ctx->pending_messages_head, GNUNET_YES);
1248 /* If we are still waiting for notification whether this peer is live
1249 * cancel the according task */
1250 if (NULL != peer_ctx->liveliness_check_pending)
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Removing pending liveliness check for peer %s\n",
1254 GNUNET_i2s (&peer_ctx->peer_id));
1255 // TODO wait until cadet sets mq->cancel_impl
1256 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
1257 GNUNET_free (peer_ctx->liveliness_check_pending);
1258 peer_ctx->liveliness_check_pending = NULL;
1260 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
1261 if (NULL != peer_ctx->send_channel &&
1262 GNUNET_YES != Peers_check_channel_flag (channel_flag, Peers_CHANNEL_DESTROING))
1264 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "Destroying send channel\n");
1266 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1267 peer_ctx->send_channel = NULL;
1269 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
1270 if (NULL != peer_ctx->recv_channel &&
1271 GNUNET_YES != Peers_check_channel_flag (channel_flag, Peers_CHANNEL_DESTROING))
1273 LOG (GNUNET_ERROR_TYPE_DEBUG,
1274 "Destroying recv channel\n");
1275 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1276 peer_ctx->recv_channel = NULL;
1279 GNUNET_free (peer_ctx->send_channel_flags);
1280 GNUNET_free (peer_ctx->recv_channel_flags);
1282 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, &peer_ctx->peer_id))
1284 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1286 GNUNET_free (peer_ctx);
1292 * @brief set flags on a given peer.
1294 * @param peer the peer to set flags on
1295 * @param flags the flags
1298 Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1300 struct PeerContext *peer_ctx;
1302 peer_ctx = get_peer_ctx (peer);
1303 set_peer_flag (peer_ctx, flags);
1308 * @brief unset flags on a given peer.
1310 * @param peer the peer to unset flags on
1311 * @param flags the flags
1314 Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1316 struct PeerContext *peer_ctx;
1318 peer_ctx = get_peer_ctx (peer);
1319 unset_peer_flag (peer_ctx, flags);
1324 * @brief Check whether flags on a peer are set.
1326 * @param peer the peer to check the flag of
1327 * @param flags the flags to check
1329 * @return #GNUNET_SYSERR if peer is not known
1330 * #GNUNET_YES if all given flags are set
1331 * #GNUNET_NO otherwise
1334 Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1336 struct PeerContext *peer_ctx;
1338 if (GNUNET_NO == Peers_check_peer_known (peer))
1340 return GNUNET_SYSERR;
1342 peer_ctx = get_peer_ctx (peer);
1343 return check_peer_flag_set (peer_ctx, flags);
1348 * @brief set flags on a given channel.
1350 * @param channel the channel to set flags on
1351 * @param flags the flags
1354 Peers_set_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1356 set_channel_flag (channel_flags, flags);
1361 * @brief unset flags on a given channel.
1363 * @param channel the channel to unset flags on
1364 * @param flags the flags
1367 Peers_unset_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1369 unset_channel_flag (channel_flags, flags);
1374 * @brief Check whether flags on a channel are set.
1376 * @param channel the channel to check the flag of
1377 * @param flags the flags to check
1379 * @return #GNUNET_YES if all given flags are set
1380 * #GNUNET_NO otherwise
1383 Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1385 return check_channel_flag_set (channel_flags, flags);
1389 * @brief Get the flags for the channel in @a role for @a peer.
1391 * @param peer Peer to get the channel flags for.
1392 * @param role Role of channel to get flags for
1394 * @return The flags.
1397 Peers_get_channel_flag (const struct GNUNET_PeerIdentity *peer,
1398 enum Peers_ChannelRole role)
1400 const struct PeerContext *peer_ctx;
1402 peer_ctx = get_peer_ctx (peer);
1403 if (Peers_CHANNEL_ROLE_SENDING == role)
1405 return peer_ctx->send_channel_flags;
1407 else if (Peers_CHANNEL_ROLE_RECEIVING == role)
1409 return peer_ctx->recv_channel_flags;
1418 * @brief Check whether we have information about the given peer.
1420 * FIXME probably deprecated. Make this the new _online.
1422 * @param peer peer in question
1424 * @return #GNUNET_YES if peer is known
1425 * #GNUNET_NO if peer is not knwon
1428 Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer)
1430 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
1435 * @brief Check whether @a peer is actually a peer.
1437 * A valid peer is a peer that we know exists eg. we were connected to once.
1439 * @param peer peer in question
1441 * @return #GNUNET_YES if peer is valid
1442 * #GNUNET_NO if peer is not valid
1445 Peers_check_peer_valid (const struct GNUNET_PeerIdentity *peer)
1447 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer);
1452 * @brief Indicate that we want to send to the other peer
1454 * This establishes a sending channel
1456 * @param peer the peer to establish channel to
1459 Peers_indicate_sending_intention (const struct GNUNET_PeerIdentity *peer)
1461 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1462 (void) get_channel (peer);
1467 * @brief Check whether other peer has the intention to send/opened channel
1470 * @param peer the peer in question
1472 * @return #GNUNET_YES if peer has the intention to send
1473 * #GNUNET_NO otherwise
1476 Peers_check_peer_send_intention (const struct GNUNET_PeerIdentity *peer)
1478 const struct PeerContext *peer_ctx;
1480 peer_ctx = get_peer_ctx (peer);
1481 if (NULL != peer_ctx->recv_channel)
1490 * Handle the channel a peer opens to us.
1492 * @param cls The closure
1493 * @param channel The channel the peer wants to establish
1494 * @param initiator The peer's peer ID
1496 * @return initial channel context for the channel
1497 * (can be NULL -- that's not an error)
1500 Peers_handle_inbound_channel (void *cls,
1501 struct GNUNET_CADET_Channel *channel,
1502 const struct GNUNET_PeerIdentity *initiator)
1504 struct PeerContext *peer_ctx;
1506 LOG (GNUNET_ERROR_TYPE_DEBUG,
1507 "New channel was established to us (Peer %s).\n",
1508 GNUNET_i2s (initiator));
1509 GNUNET_assert (NULL != channel); /* according to cadet API */
1510 /* Make sure we 'know' about this peer */
1511 peer_ctx = create_or_get_peer_ctx (initiator);
1512 set_peer_live (peer_ctx);
1513 /* We only accept one incoming channel per peer */
1514 if (GNUNET_YES == Peers_check_peer_send_intention (initiator))
1516 set_channel_flag (peer_ctx->recv_channel_flags,
1517 Peers_CHANNEL_ESTABLISHED_TWICE);
1518 GNUNET_CADET_channel_destroy (channel);
1519 /* return the channel context */
1520 return &peer_ctx->peer_id;
1522 peer_ctx->recv_channel = channel;
1523 return &peer_ctx->peer_id;
1528 * @brief Check whether a sending channel towards the given peer exists
1530 * @param peer the peer to check for
1532 * @return #GNUNET_YES if a sending channel towards that peer exists
1533 * #GNUNET_NO otherwise
1536 Peers_check_sending_channel_exists (const struct GNUNET_PeerIdentity *peer)
1538 struct PeerContext *peer_ctx;
1540 if (GNUNET_NO == Peers_check_peer_known (peer))
1541 { /* If no such peer exists, there is no channel */
1544 peer_ctx = get_peer_ctx (peer);
1545 if (NULL == peer_ctx->send_channel)
1554 * @brief check whether the given channel is the sending channel of the given
1557 * @param peer the peer in question
1558 * @param channel the channel to check for
1559 * @param role either #Peers_CHANNEL_ROLE_SENDING, or
1560 * #Peers_CHANNEL_ROLE_RECEIVING
1562 * @return #GNUNET_YES if the given chennel is the sending channel of the peer
1563 * #GNUNET_NO otherwise
1566 Peers_check_channel_role (const struct GNUNET_PeerIdentity *peer,
1567 const struct GNUNET_CADET_Channel *channel,
1568 enum Peers_ChannelRole role)
1570 const struct PeerContext *peer_ctx;
1572 if (GNUNET_NO == Peers_check_peer_known (peer))
1576 peer_ctx = get_peer_ctx (peer);
1577 if ( (Peers_CHANNEL_ROLE_SENDING == role) &&
1578 (channel == peer_ctx->send_channel) )
1582 if ( (Peers_CHANNEL_ROLE_RECEIVING == role) &&
1583 (channel == peer_ctx->recv_channel) )
1592 * @brief Destroy the send channel of a peer e.g. stop indicating a sending
1593 * intention to another peer
1595 * If there is also no channel to receive messages from that peer, remove it
1599 * @peer the peer identity of the peer whose sending channel to destroy
1600 * @return #GNUNET_YES if channel was destroyed
1601 * #GNUNET_NO otherwise
1604 Peers_destroy_sending_channel (const struct GNUNET_PeerIdentity *peer)
1606 struct PeerContext *peer_ctx;
1608 if (GNUNET_NO == Peers_check_peer_known (peer))
1612 peer_ctx = get_peer_ctx (peer);
1613 if (NULL != peer_ctx->send_channel)
1615 set_channel_flag (peer_ctx->send_channel_flags, Peers_CHANNEL_CLEAN);
1616 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1617 peer_ctx->send_channel = NULL;
1618 (void) Peers_check_connected (peer);
1625 * This is called when a channel is destroyed.
1627 * @param cls The closure
1628 * @param channel The channel being closed
1629 * @param channel_ctx The context associated with this channel
1632 Peers_cleanup_destroyed_channel (void *cls,
1633 const struct GNUNET_CADET_Channel *channel)
1635 struct GNUNET_PeerIdentity *peer = cls;
1636 struct PeerContext *peer_ctx;
1638 if (GNUNET_NO == Peers_check_peer_known (peer))
1639 {/* We don't want to implicitly create a context that we're about to kill */
1640 LOG (GNUNET_ERROR_TYPE_DEBUG,
1641 "channel (%s) without associated context was destroyed\n",
1645 peer_ctx = get_peer_ctx (peer);
1647 /* If our peer issued the destruction of the channel, the #Peers_TO_DESTROY
1648 * flag will be set. In this case simply make sure that the channels are
1650 /* FIXME This distinction seems to be redundant */
1651 if (Peers_check_peer_flag (peer, Peers_TO_DESTROY))
1652 {/* We initiatad the destruction of this particular peer */
1653 if (channel == peer_ctx->send_channel)
1654 peer_ctx->send_channel = NULL;
1655 else if (channel == peer_ctx->recv_channel)
1656 peer_ctx->recv_channel = NULL;
1658 if (NULL != peer_ctx->send_channel)
1660 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1661 peer_ctx->send_channel = NULL;
1663 if (NULL != peer_ctx->recv_channel)
1665 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1666 peer_ctx->recv_channel = NULL;
1668 /* Set the #Peers_ONLINE flag accordingly */
1669 (void) Peers_check_connected (peer);
1674 { /* We did not initiate the destruction of this peer */
1675 if (channel == peer_ctx->send_channel)
1676 { /* Something (but us) killd the channel - clean up peer */
1677 LOG (GNUNET_ERROR_TYPE_DEBUG,
1678 "send channel (%s) was destroyed - cleaning up\n",
1680 peer_ctx->send_channel = NULL;
1682 else if (channel == peer_ctx->recv_channel)
1683 { /* Other peer doesn't want to send us messages anymore */
1684 LOG (GNUNET_ERROR_TYPE_DEBUG,
1685 "Peer %s destroyed recv channel - cleaning up channel\n",
1687 peer_ctx->recv_channel = NULL;
1691 LOG (GNUNET_ERROR_TYPE_WARNING,
1692 "unknown channel (%s) was destroyed\n",
1696 (void) Peers_check_connected (peer);
1700 * @brief Send a message to another peer.
1702 * Keeps track about pending messages so they can be properly removed when the
1703 * peer is destroyed.
1705 * @param peer receeiver of the message
1706 * @param ev envelope of the message
1707 * @param type type of the message
1710 Peers_send_message (const struct GNUNET_PeerIdentity *peer,
1711 struct GNUNET_MQ_Envelope *ev,
1714 struct PendingMessage *pending_msg;
1715 struct GNUNET_MQ_Handle *mq;
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1718 "Sending message to %s of type %s\n",
1721 pending_msg = insert_pending_message (peer, ev, type);
1723 GNUNET_MQ_notify_sent (ev,
1726 GNUNET_MQ_send (mq, ev);
1730 * @brief Schedule a operation on given peer
1732 * Avoids scheduling an operation twice.
1734 * @param peer the peer we want to schedule the operation for once it gets live
1736 * @return #GNUNET_YES if the operation was scheduled
1737 * #GNUNET_NO otherwise
1740 Peers_schedule_operation (const struct GNUNET_PeerIdentity *peer,
1741 const PeerOp peer_op)
1743 struct PeerPendingOp pending_op;
1744 struct PeerContext *peer_ctx;
1746 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, &own_identity))
1750 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1752 //TODO if LIVE/ONLINE execute immediately
1754 if (GNUNET_NO == check_operation_scheduled (peer, peer_op))
1756 peer_ctx = get_peer_ctx (peer);
1757 pending_op.op = peer_op;
1758 pending_op.op_cls = NULL;
1759 GNUNET_array_append (peer_ctx->pending_ops,
1760 peer_ctx->num_pending_ops,
1768 * @brief Get the recv_channel of @a peer.
1769 * Needed to correctly handle (call #GNUNET_CADET_receive_done()) incoming
1772 * @param peer The peer to get the recv_channel from.
1774 * @return The recv_channel.
1776 struct GNUNET_CADET_Channel *
1777 Peers_get_recv_channel (const struct GNUNET_PeerIdentity *peer)
1779 struct PeerContext *peer_ctx;
1781 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1782 peer_ctx = get_peer_ctx (peer);
1783 return peer_ctx->recv_channel;
1785 /***********************************************************************
1786 * /Old gnunet-service-rps_peers.c
1787 ***********************************************************************/
1790 /***********************************************************************
1791 * Housekeeping with clients
1792 ***********************************************************************/
1795 * Closure used to pass the client and the id to the callback
1796 * that replies to a client's request
1803 struct ReplyCls *next;
1804 struct ReplyCls *prev;
1807 * The identifier of the request
1812 * The handle to the request
1814 struct RPS_SamplerRequestHandle *req_handle;
1817 * The client handle to send the reply to
1819 struct ClientContext *cli_ctx;
1824 * Struct used to store the context of a connected client.
1826 struct ClientContext
1831 struct ClientContext *next;
1832 struct ClientContext *prev;
1835 * The message queue to communicate with the client.
1837 struct GNUNET_MQ_Handle *mq;
1840 * DLL with handles to single requests from the client
1842 struct ReplyCls *rep_cls_head;
1843 struct ReplyCls *rep_cls_tail;
1846 * The client handle to send the reply to
1848 struct GNUNET_SERVICE_Client *client;
1852 * DLL with all clients currently connected to us
1854 struct ClientContext *cli_ctx_head;
1855 struct ClientContext *cli_ctx_tail;
1857 /***********************************************************************
1858 * /Housekeeping with clients
1859 ***********************************************************************/
1865 /***********************************************************************
1867 ***********************************************************************/
1870 * Sampler used for the Brahms protocol itself.
1872 static struct RPS_Sampler *prot_sampler;
1875 * Sampler used for the clients.
1877 static struct RPS_Sampler *client_sampler;
1880 * Name to log view to
1882 static char *file_name_view_log;
1885 * The size of sampler we need to be able to satisfy the client's need
1888 static unsigned int sampler_size_client_need;
1891 * The size of sampler we need to be able to satisfy the Brahms protocol's
1892 * need of random peers.
1894 * This is one minimum size the sampler grows to.
1896 static unsigned int sampler_size_est_need;
1899 * Percentage of total peer number in the view
1900 * to send random PUSHes to
1905 * Percentage of total peer number in the view
1906 * to send random PULLs to
1911 * Identifier for the main task that runs periodically.
1913 static struct GNUNET_SCHEDULER_Task *do_round_task;
1916 * Time inverval the do_round task runs in.
1918 static struct GNUNET_TIME_Relative round_interval;
1921 * List to store peers received through pushes temporary.
1923 static struct CustomPeerMap *push_map;
1926 * List to store peers received through pulls temporary.
1928 static struct CustomPeerMap *pull_map;
1933 static struct GNUNET_NSE_Handle *nse;
1938 static struct GNUNET_CADET_Handle *cadet_handle;
1941 * @brief Port to communicate to other peers.
1943 static struct GNUNET_CADET_Port *cadet_port;
1946 * Handler to PEERINFO.
1948 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
1951 * Handle for cancellation of iteration over peers.
1953 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
1958 * Counts how many requets clients already issued.
1959 * Only needed in the beginning to check how many of the 64 deltas
1962 static unsigned int req_counter;
1965 * Time of the last request we received.
1967 * Used to compute the expected request rate.
1969 static struct GNUNET_TIME_Absolute last_request;
1972 * Size of #request_deltas.
1974 #define REQUEST_DELTAS_SIZE 64
1975 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
1978 * Last 64 deltas between requests
1980 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
1983 * The prediction of the rate of requests
1985 static struct GNUNET_TIME_Relative request_rate;
1988 #ifdef ENABLE_MALICIOUS
1990 * Type of malicious peer
1992 * 0 Don't act malicious at all - Default
1993 * 1 Try to maximise representation
1994 * 2 Try to partition the network
1997 static uint32_t mal_type;
2000 * Other malicious peers
2002 static struct GNUNET_PeerIdentity *mal_peers;
2005 * Hashmap of malicious peers used as set.
2006 * Used to more efficiently check whether we know that peer.
2008 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set;
2011 * Number of other malicious peers
2013 static uint32_t num_mal_peers;
2017 * If type is 2 This struct is used to store the attacked peers in a DLL
2024 struct AttackedPeer *next;
2025 struct AttackedPeer *prev;
2030 struct GNUNET_PeerIdentity peer_id;
2034 * If type is 2 this is the DLL of attacked peers
2036 static struct AttackedPeer *att_peers_head;
2037 static struct AttackedPeer *att_peers_tail;
2040 * This index is used to point to an attacked peer to
2041 * implement the round-robin-ish way to select attacked peers.
2043 static struct AttackedPeer *att_peer_index;
2046 * Hashmap of attacked peers used as set.
2047 * Used to more efficiently check whether we know that peer.
2049 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set;
2052 * Number of attacked peers
2054 static uint32_t num_attacked_peers;
2057 * If type is 1 this is the attacked peer
2059 static struct GNUNET_PeerIdentity attacked_peer;
2062 * The limit of PUSHes we can send in one round.
2063 * This is an assumption of the Brahms protocol and either implemented
2066 * assumend to be the bandwidth limitation.
2068 static uint32_t push_limit = 10000;
2069 #endif /* ENABLE_MALICIOUS */
2072 /***********************************************************************
2074 ***********************************************************************/
2077 /***********************************************************************
2079 ***********************************************************************/
2083 * Print peerlist to log.
2086 print_peer_list (struct GNUNET_PeerIdentity *list,
2091 LOG (GNUNET_ERROR_TYPE_DEBUG,
2092 "Printing peer list of length %u at %p:\n",
2095 for (i = 0 ; i < len ; i++)
2097 LOG (GNUNET_ERROR_TYPE_DEBUG,
2099 i, GNUNET_i2s (&list[i]));
2105 * Remove peer from list.
2108 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
2109 unsigned int *list_size,
2110 const struct GNUNET_PeerIdentity *peer)
2113 struct GNUNET_PeerIdentity *tmp;
2117 LOG (GNUNET_ERROR_TYPE_DEBUG,
2118 "Removing peer %s from list at %p\n",
2122 for ( i = 0 ; i < *list_size ; i++ )
2124 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
2126 if (i < *list_size -1)
2127 { /* Not at the last entry -- shift peers left */
2128 memmove (&tmp[i], &tmp[i +1],
2129 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
2131 /* Remove last entry (should be now useless PeerID) */
2132 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
2140 * Sum all time relatives of an array.
2142 static struct GNUNET_TIME_Relative
2143 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array,
2146 struct GNUNET_TIME_Relative sum;
2149 sum = GNUNET_TIME_UNIT_ZERO;
2150 for ( i = 0 ; i < arr_size ; i++ )
2152 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
2159 * Compute the average of given time relatives.
2161 static struct GNUNET_TIME_Relative
2162 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array,
2165 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array,
2172 * Insert PeerID in #view
2174 * Called once we know a peer is live.
2175 * Implements #PeerOp
2177 * @return GNUNET_OK if peer was actually inserted
2178 * GNUNET_NO if peer was not inserted
2181 insert_in_view_op (void *cls,
2182 const struct GNUNET_PeerIdentity *peer);
2185 * Insert PeerID in #view
2187 * Called once we know a peer is live.
2189 * @return GNUNET_OK if peer was actually inserted
2190 * GNUNET_NO if peer was not inserted
2193 insert_in_view (const struct GNUNET_PeerIdentity *peer)
2197 online = Peers_check_peer_flag (peer, Peers_ONLINE);
2198 if ( (GNUNET_NO == online) ||
2199 (GNUNET_SYSERR == online) ) /* peer is not even known */
2201 (void) Peers_issue_peer_liveliness_check (peer);
2202 (void) Peers_schedule_operation (peer, insert_in_view_op);
2205 /* Open channel towards peer to keep connection open */
2206 Peers_indicate_sending_intention (peer);
2207 return View_put (peer);
2211 * Put random peer from sampler into the view as history update.
2214 hist_update (void *cls,
2215 struct GNUNET_PeerIdentity *ids,
2220 for (i = 0; i < num_peers; i++)
2222 (void) insert_in_view (&ids[i]);
2223 to_file (file_name_view_log,
2225 GNUNET_i2s_full (ids));
2231 * Wrapper around #RPS_sampler_resize()
2233 * If we do not have enough sampler elements, double current sampler size
2234 * If we have more than enough sampler elements, halv current sampler size
2237 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
2239 unsigned int sampler_size;
2242 // TODO respect the min, max
2243 sampler_size = RPS_sampler_get_size (sampler);
2244 if (sampler_size > new_size * 4)
2246 RPS_sampler_resize (sampler, sampler_size / 2);
2248 else if (sampler_size < new_size)
2250 RPS_sampler_resize (sampler, sampler_size * 2);
2252 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
2257 * Wrapper around #RPS_sampler_resize() resizing the client sampler
2260 client_resize_wrapper ()
2262 uint32_t bigger_size;
2266 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
2268 // TODO respect the min, max
2269 resize_wrapper (client_sampler, bigger_size);
2270 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
2276 * Estimate request rate
2278 * Called every time we receive a request from the client.
2283 struct GNUNET_TIME_Relative max_round_duration;
2285 if (request_deltas_size > req_counter)
2287 if ( 1 < req_counter)
2289 /* Shift last request deltas to the right */
2290 memmove (&request_deltas[1],
2292 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
2294 /* Add current delta to beginning */
2296 GNUNET_TIME_absolute_get_difference (last_request,
2297 GNUNET_TIME_absolute_get ());
2298 request_rate = T_relative_avg (request_deltas, req_counter);
2299 request_rate = (request_rate.rel_value_us < 1) ?
2300 GNUNET_TIME_relative_get_unit_ () : request_rate;
2302 /* Compute the duration a round will maximally take */
2303 max_round_duration =
2304 GNUNET_TIME_relative_add (round_interval,
2305 GNUNET_TIME_relative_divide (round_interval, 2));
2307 /* Set the estimated size the sampler has to have to
2308 * satisfy the current client request rate */
2309 sampler_size_client_need =
2310 max_round_duration.rel_value_us / request_rate.rel_value_us;
2312 /* Resize the sampler */
2313 client_resize_wrapper ();
2315 last_request = GNUNET_TIME_absolute_get ();
2320 * Add all peers in @a peer_array to @a peer_map used as set.
2322 * @param peer_array array containing the peers
2323 * @param num_peers number of peers in @peer_array
2324 * @param peer_map the peermap to use as set
2327 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
2328 unsigned int num_peers,
2329 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
2332 if (NULL == peer_map)
2334 LOG (GNUNET_ERROR_TYPE_WARNING,
2335 "Trying to add peers to non-existing peermap.\n");
2339 for (i = 0; i < num_peers; i++)
2341 GNUNET_CONTAINER_multipeermap_put (peer_map,
2344 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2350 * Send a PULL REPLY to @a peer_id
2352 * @param peer_id the peer to send the reply to.
2353 * @param peer_ids the peers to send to @a peer_id
2354 * @param num_peer_ids the number of peers to send to @a peer_id
2357 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
2358 const struct GNUNET_PeerIdentity *peer_ids,
2359 unsigned int num_peer_ids)
2362 struct GNUNET_MQ_Envelope *ev;
2363 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
2365 /* Compute actual size */
2366 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
2367 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
2369 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
2370 /* Compute number of peers to send
2371 * If too long, simply truncate */
2372 // TODO select random ones via permutation
2373 // or even better: do good protocol design
2375 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
2376 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
2377 sizeof (struct GNUNET_PeerIdentity);
2379 send_size = num_peer_ids;
2381 LOG (GNUNET_ERROR_TYPE_DEBUG,
2382 "Going to send PULL REPLY with %u peers to %s\n",
2383 send_size, GNUNET_i2s (peer_id));
2385 ev = GNUNET_MQ_msg_extra (out_msg,
2386 send_size * sizeof (struct GNUNET_PeerIdentity),
2387 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
2388 out_msg->num_peers = htonl (send_size);
2389 GNUNET_memcpy (&out_msg[1], peer_ids,
2390 send_size * sizeof (struct GNUNET_PeerIdentity));
2392 Peers_send_message (peer_id, ev, "PULL REPLY");
2397 * Insert PeerID in #pull_map
2399 * Called once we know a peer is live.
2402 insert_in_pull_map (void *cls,
2403 const struct GNUNET_PeerIdentity *peer)
2405 CustomPeerMap_put (pull_map, peer);
2410 * Insert PeerID in #view
2412 * Called once we know a peer is live.
2413 * Implements #PeerOp
2416 insert_in_view_op (void *cls,
2417 const struct GNUNET_PeerIdentity *peer)
2419 (void) insert_in_view (peer);
2424 * Update sampler with given PeerID.
2425 * Implements #PeerOp
2428 insert_in_sampler (void *cls,
2429 const struct GNUNET_PeerIdentity *peer)
2431 LOG (GNUNET_ERROR_TYPE_DEBUG,
2432 "Updating samplers with peer %s from insert_in_sampler()\n",
2434 RPS_sampler_update (prot_sampler, peer);
2435 RPS_sampler_update (client_sampler, peer);
2436 if (0 < RPS_sampler_count_id (prot_sampler, peer))
2438 /* Make sure we 'know' about this peer */
2439 (void) Peers_issue_peer_liveliness_check (peer);
2440 /* Establish a channel towards that peer to indicate we are going to send
2442 //Peers_indicate_sending_intention (peer);
2447 * @brief This is called on peers from external sources (cadet, peerinfo, ...)
2448 * If the peer is not known, liveliness check is issued and it is
2449 * scheduled to be inserted in sampler and view.
2451 * "External sources" refer to every source except the gossip.
2453 * @param peer peer to insert
2456 got_peer (const struct GNUNET_PeerIdentity *peer)
2458 /* If we did not know this peer already, insert it into sampler and view */
2459 if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer))
2461 Peers_schedule_operation (peer, insert_in_sampler);
2462 Peers_schedule_operation (peer, insert_in_view_op);
2467 * @brief Checks if there is a sending channel and if it is needed
2469 * @param peer the peer whose sending channel is checked
2470 * @return GNUNET_YES if sending channel exists and is still needed
2471 * GNUNET_NO otherwise
2474 check_sending_channel_needed (const struct GNUNET_PeerIdentity *peer)
2476 /* struct GNUNET_CADET_Channel *channel; */
2477 if (GNUNET_NO == Peers_check_peer_known (peer))
2481 if (GNUNET_YES == Peers_check_sending_channel_exists (peer))
2483 if ( (0 < RPS_sampler_count_id (prot_sampler, peer)) ||
2484 (GNUNET_YES == View_contains_peer (peer)) ||
2485 (GNUNET_YES == CustomPeerMap_contains_peer (push_map, peer)) ||
2486 (GNUNET_YES == CustomPeerMap_contains_peer (pull_map, peer)) ||
2487 (GNUNET_YES == Peers_check_peer_flag (peer, Peers_PULL_REPLY_PENDING)))
2488 { /* If we want to keep the connection to peer open */
2497 * @brief remove peer from our knowledge, the view, push and pull maps and
2500 * @param peer the peer to remove
2503 remove_peer (const struct GNUNET_PeerIdentity *peer)
2505 (void) View_remove_peer (peer);
2506 CustomPeerMap_remove_peer (pull_map, peer);
2507 CustomPeerMap_remove_peer (push_map, peer);
2508 RPS_sampler_reinitialise_by_value (prot_sampler, peer);
2509 RPS_sampler_reinitialise_by_value (client_sampler, peer);
2510 Peers_remove_peer (peer);
2515 * @brief Remove data that is not needed anymore.
2517 * If the sending channel is no longer needed it is destroyed.
2519 * @param peer the peer whose data is about to be cleaned
2522 clean_peer (const struct GNUNET_PeerIdentity *peer)
2524 if (GNUNET_NO == check_sending_channel_needed (peer))
2526 LOG (GNUNET_ERROR_TYPE_DEBUG,
2527 "Going to remove send channel to peer %s\n",
2529 #ifdef ENABLE_MALICIOUS
2530 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2531 (void) Peers_destroy_sending_channel (peer);
2532 #else /* ENABLE_MALICIOUS */
2533 (void) Peers_destroy_sending_channel (peer);
2534 #endif /* ENABLE_MALICIOUS */
2537 if ( (GNUNET_NO == Peers_check_peer_send_intention (peer)) &&
2538 (GNUNET_NO == View_contains_peer (peer)) &&
2539 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2540 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2541 (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2542 (0 == RPS_sampler_count_id (client_sampler, peer)) &&
2543 (GNUNET_NO != Peers_check_removable (peer)) )
2544 { /* We can safely remove this peer */
2545 LOG (GNUNET_ERROR_TYPE_DEBUG,
2546 "Going to remove peer %s\n",
2554 * @brief This is called when a channel is destroyed.
2556 * Removes peer completely from our knowledge if the send_channel was destroyed
2557 * Otherwise simply delete the recv_channel
2558 * Also check if the knowledge about this peer is still needed.
2559 * If not, remove this peer from our knowledge.
2561 * @param cls The closure
2562 * @param channel The channel being closed
2563 * @param channel_ctx The context associated with this channel
2566 cleanup_destroyed_channel (void *cls,
2567 const struct GNUNET_CADET_Channel *channel)
2569 struct GNUNET_PeerIdentity *peer = cls;
2570 uint32_t *channel_flag;
2572 if (GNUNET_NO == Peers_check_peer_known (peer))
2573 { /* We don't know a context to that peer */
2574 LOG (GNUNET_ERROR_TYPE_WARNING,
2575 "channel (%s) without associated context was destroyed\n",
2580 if (GNUNET_YES == Peers_check_peer_flag (peer, Peers_TO_DESTROY))
2581 { /* We are in the middle of removing that peer from our knowledge. In this
2582 case simply make sure that the channels are cleaned. */
2583 Peers_cleanup_destroyed_channel (cls, channel);
2584 to_file (file_name_view_log,
2585 "-%s\t(cleanup channel, ourself)",
2586 GNUNET_i2s_full (peer));
2591 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
2592 { /* Channel used for sending was destroyed */
2593 /* Possible causes of channel destruction:
2594 * - ourselves -> cleaning send channel -> clean context
2595 * - other peer -> peer probably went down -> remove
2597 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
2598 if (GNUNET_YES == Peers_check_channel_flag (channel_flag, Peers_CHANNEL_CLEAN))
2599 { /* We are about to clean the sending channel. Clean the respective
2601 Peers_cleanup_destroyed_channel (cls, channel);
2605 { /* Other peer destroyed our sending channel that he is supposed to keep
2606 * open. It probably went down. Remove it from our knowledge. */
2607 Peers_cleanup_destroyed_channel (cls, channel);
2612 else if (GNUNET_YES ==
2613 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
2614 { /* Channel used for receiving was destroyed */
2615 /* Possible causes of channel destruction:
2616 * - ourselves -> peer tried to establish channel twice -> clean context
2617 * - other peer -> peer doesn't want to send us data -> clean
2619 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
2621 Peers_check_channel_flag (channel_flag, Peers_CHANNEL_ESTABLISHED_TWICE))
2622 { /* Other peer tried to establish a channel to us twice. We do not accept
2623 * that. Clean the context. */
2624 Peers_cleanup_destroyed_channel (cls, channel);
2628 { /* Other peer doesn't want to send us data anymore. We are free to clean
2630 Peers_cleanup_destroyed_channel (cls, channel);
2637 LOG (GNUNET_ERROR_TYPE_WARNING,
2638 "Destroyed channel is neither sending nor receiving channel\n");
2642 /***********************************************************************
2644 ***********************************************************************/
2647 destroy_reply_cls (struct ReplyCls *rep_cls)
2649 struct ClientContext *cli_ctx;
2651 cli_ctx = rep_cls->cli_ctx;
2652 GNUNET_assert (NULL != cli_ctx);
2653 if (NULL != rep_cls->req_handle)
2655 RPS_sampler_request_cancel (rep_cls->req_handle);
2657 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
2658 cli_ctx->rep_cls_tail,
2660 GNUNET_free (rep_cls);
2665 destroy_cli_ctx (struct ClientContext *cli_ctx)
2667 GNUNET_assert (NULL != cli_ctx);
2668 if (NULL != cli_ctx->rep_cls_head)
2670 LOG (GNUNET_ERROR_TYPE_WARNING,
2671 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
2672 while (NULL != cli_ctx->rep_cls_head)
2673 destroy_reply_cls (cli_ctx->rep_cls_head);
2675 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
2678 GNUNET_free (cli_ctx);
2683 * Function called by NSE.
2685 * Updates sizes of sampler list and view and adapt those lists
2689 nse_callback (void *cls,
2690 struct GNUNET_TIME_Absolute timestamp,
2691 double logestimate, double std_dev)
2694 //double scale; // TODO this might go gloabal/config
2696 LOG (GNUNET_ERROR_TYPE_DEBUG,
2697 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
2698 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
2700 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
2701 // GNUNET_NSE_log_estimate_to_n (logestimate);
2702 estimate = pow (estimate, 1.0 / 3);
2703 // TODO add if std_dev is a number
2704 // estimate += (std_dev * scale);
2705 if (2 < ceil (estimate))
2707 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
2708 sampler_size_est_need = estimate;
2710 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
2712 /* If the NSE has changed adapt the lists accordingly */
2713 resize_wrapper (prot_sampler, sampler_size_est_need);
2714 client_resize_wrapper ();
2719 * Callback called once the requested PeerIDs are ready.
2721 * Sends those to the requesting client.
2724 client_respond (void *cls,
2725 struct GNUNET_PeerIdentity *peer_ids,
2728 struct ReplyCls *reply_cls = cls;
2730 struct GNUNET_MQ_Envelope *ev;
2731 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
2732 uint32_t size_needed;
2733 struct ClientContext *cli_ctx;
2735 GNUNET_assert (NULL != reply_cls);
2736 LOG (GNUNET_ERROR_TYPE_DEBUG,
2737 "sampler returned %" PRIu32 " peers:\n",
2739 for (i = 0; i < num_peers; i++)
2741 LOG (GNUNET_ERROR_TYPE_DEBUG,
2742 " %" PRIu32 ": %s\n",
2744 GNUNET_i2s (&peer_ids[i]));
2747 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
2748 num_peers * sizeof (struct GNUNET_PeerIdentity);
2750 GNUNET_assert (GNUNET_MAX_MESSAGE_SIZE >= size_needed);
2752 ev = GNUNET_MQ_msg_extra (out_msg,
2753 num_peers * sizeof (struct GNUNET_PeerIdentity),
2754 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
2755 out_msg->num_peers = htonl (num_peers);
2756 out_msg->id = htonl (reply_cls->id);
2758 GNUNET_memcpy (&out_msg[1],
2760 num_peers * sizeof (struct GNUNET_PeerIdentity));
2761 GNUNET_free (peer_ids);
2763 cli_ctx = reply_cls->cli_ctx;
2764 GNUNET_assert (NULL != cli_ctx);
2765 reply_cls->req_handle = NULL;
2766 destroy_reply_cls (reply_cls);
2767 GNUNET_MQ_send (cli_ctx->mq, ev);
2772 * Handle RPS request from the client.
2774 * @param cls closure
2775 * @param message the actual message
2778 handle_client_request (void *cls,
2779 const struct GNUNET_RPS_CS_RequestMessage *msg)
2781 struct ClientContext *cli_ctx = cls;
2783 uint32_t size_needed;
2784 struct ReplyCls *reply_cls;
2787 num_peers = ntohl (msg->num_peers);
2788 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
2789 num_peers * sizeof (struct GNUNET_PeerIdentity);
2791 if (GNUNET_MAX_MESSAGE_SIZE < size_needed)
2793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2794 "Message received from client has size larger than expected\n");
2795 GNUNET_SERVICE_client_drop (cli_ctx->client);
2799 for (i = 0 ; i < num_peers ; i++)
2802 LOG (GNUNET_ERROR_TYPE_DEBUG,
2803 "Client requested %" PRIu32 " random peer(s).\n",
2806 reply_cls = GNUNET_new (struct ReplyCls);
2807 reply_cls->id = ntohl (msg->id);
2808 reply_cls->cli_ctx = cli_ctx;
2809 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
2814 GNUNET_assert (NULL != cli_ctx);
2815 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
2816 cli_ctx->rep_cls_tail,
2818 GNUNET_SERVICE_client_continue (cli_ctx->client);
2823 * @brief Handle a message that requests the cancellation of a request
2826 * @param message the message containing the id of the request
2829 handle_client_request_cancel (void *cls,
2830 const struct GNUNET_RPS_CS_RequestCancelMessage *msg)
2832 struct ClientContext *cli_ctx = cls;
2833 struct ReplyCls *rep_cls;
2835 GNUNET_assert (NULL != cli_ctx);
2836 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
2837 rep_cls = cli_ctx->rep_cls_head;
2838 LOG (GNUNET_ERROR_TYPE_DEBUG,
2839 "Client cancels request with id %" PRIu32 "\n",
2841 while ( (NULL != rep_cls->next) &&
2842 (rep_cls->id != ntohl (msg->id)) )
2843 rep_cls = rep_cls->next;
2844 GNUNET_assert (rep_cls->id == ntohl (msg->id));
2845 destroy_reply_cls (rep_cls);
2846 GNUNET_SERVICE_client_continue (cli_ctx->client);
2851 * @brief This function is called, when the client seeds peers.
2852 * It verifies that @a msg is well-formed.
2854 * @param cls the closure (#ClientContext)
2855 * @param msg the message
2856 * @return #GNUNET_OK if @a msg is well-formed
2859 check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
2861 struct ClientContext *cli_ctx = cls;
2862 uint16_t msize = ntohs (msg->header.size);
2863 uint32_t num_peers = ntohl (msg->num_peers);
2865 msize -= sizeof (struct GNUNET_RPS_CS_SeedMessage);
2866 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
2867 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
2870 GNUNET_SERVICE_client_drop (cli_ctx->client);
2871 return GNUNET_SYSERR;
2878 * Handle seed from the client.
2880 * @param cls closure
2881 * @param message the actual message
2884 handle_client_seed (void *cls,
2885 const struct GNUNET_RPS_CS_SeedMessage *msg)
2887 struct ClientContext *cli_ctx = cls;
2888 struct GNUNET_PeerIdentity *peers;
2892 num_peers = ntohl (msg->num_peers);
2893 peers = (struct GNUNET_PeerIdentity *) &msg[1];
2894 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
2895 //GNUNET_memcpy (peers, &msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
2897 LOG (GNUNET_ERROR_TYPE_DEBUG,
2898 "Client seeded peers:\n");
2899 print_peer_list (peers, num_peers);
2901 for (i = 0; i < num_peers; i++)
2903 LOG (GNUNET_ERROR_TYPE_DEBUG,
2904 "Updating samplers with seed %" PRIu32 ": %s\n",
2906 GNUNET_i2s (&peers[i]));
2908 got_peer (&peers[i]);
2911 ////GNUNET_free (peers);
2913 GNUNET_SERVICE_client_continue (cli_ctx->client);
2917 * Handle a CHECK_LIVE message from another peer.
2919 * This does nothing. But without calling #GNUNET_CADET_receive_done()
2920 * the channel is blocked for all other communication.
2922 * @param cls Closure
2923 * @param msg The message header
2926 handle_peer_check (void *cls,
2927 const struct GNUNET_MessageHeader *msg)
2929 const struct GNUNET_PeerIdentity *peer = cls;
2931 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
2935 * Handle a PUSH message from another peer.
2937 * Check the proof of work and store the PeerID
2938 * in the temporary list for pushed PeerIDs.
2940 * @param cls Closure
2941 * @param msg The message header
2944 handle_peer_push (void *cls,
2945 const struct GNUNET_MessageHeader *msg)
2947 const struct GNUNET_PeerIdentity *peer = cls;
2949 // (check the proof of work (?))
2951 LOG (GNUNET_ERROR_TYPE_DEBUG,
2952 "Received PUSH (%s)\n",
2955 #ifdef ENABLE_MALICIOUS
2956 struct AttackedPeer *tmp_att_peer;
2958 if ( (1 == mal_type) ||
2960 { /* Try to maximise representation */
2961 tmp_att_peer = GNUNET_new (struct AttackedPeer);
2962 tmp_att_peer->peer_id = *peer;
2963 if (NULL == att_peer_set)
2964 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
2966 GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
2969 GNUNET_CONTAINER_DLL_insert (att_peers_head,
2972 add_peer_array_to_set (peer, 1, att_peer_set);
2977 else if (2 == mal_type)
2979 /* We attack one single well-known peer - simply ignore */
2981 #endif /* ENABLE_MALICIOUS */
2983 /* Add the sending peer to the push_map */
2984 CustomPeerMap_put (push_map, peer);
2986 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
2991 * Handle PULL REQUEST request message from another peer.
2993 * Reply with the view of PeerIDs.
2995 * @param cls Closure
2996 * @param msg The message header
2999 handle_peer_pull_request (void *cls,
3000 const struct GNUNET_MessageHeader *msg)
3002 struct GNUNET_PeerIdentity *peer = cls;
3003 const struct GNUNET_PeerIdentity *view_array;
3005 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
3007 #ifdef ENABLE_MALICIOUS
3010 { /* Try to maximise representation */
3011 send_pull_reply (peer, mal_peers, num_mal_peers);
3014 else if (2 == mal_type)
3015 { /* Try to partition network */
3016 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
3018 send_pull_reply (peer, mal_peers, num_mal_peers);
3021 #endif /* ENABLE_MALICIOUS */
3023 view_array = View_get_as_array ();
3024 send_pull_reply (peer, view_array, View_size ());
3026 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
3031 * Check whether we sent a corresponding request and
3032 * whether this reply is the first one.
3034 * @param cls Closure
3035 * @param msg The message header
3038 check_peer_pull_reply (void *cls,
3039 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3041 struct GNUNET_PeerIdentity *sender = cls;
3043 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
3045 GNUNET_break_op (0);
3046 return GNUNET_SYSERR;
3049 if ((ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
3050 sizeof (struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
3052 LOG (GNUNET_ERROR_TYPE_ERROR,
3053 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3054 ntohl (msg->num_peers),
3055 (ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
3056 sizeof (struct GNUNET_PeerIdentity));
3057 GNUNET_break_op (0);
3058 return GNUNET_SYSERR;
3061 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
3063 LOG (GNUNET_ERROR_TYPE_WARNING,
3064 "Received a pull reply from a peer we didn't request one from!\n");
3065 GNUNET_break_op (0);
3066 return GNUNET_SYSERR;
3072 * Handle PULL REPLY message from another peer.
3074 * @param cls Closure
3075 * @param msg The message header
3078 handle_peer_pull_reply (void *cls,
3079 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3081 const struct GNUNET_PeerIdentity *peers;
3082 struct GNUNET_PeerIdentity *sender = cls;
3084 #ifdef ENABLE_MALICIOUS
3085 struct AttackedPeer *tmp_att_peer;
3086 #endif /* ENABLE_MALICIOUS */
3088 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
3090 #ifdef ENABLE_MALICIOUS
3091 // We shouldn't even receive pull replies as we're not sending
3095 #endif /* ENABLE_MALICIOUS */
3097 /* Do actual logic */
3098 peers = (const struct GNUNET_PeerIdentity *) &msg[1];
3100 LOG (GNUNET_ERROR_TYPE_DEBUG,
3101 "PULL REPLY received, got following %u peers:\n",
3102 ntohl (msg->num_peers));
3104 for (i = 0; i < ntohl (msg->num_peers); i++)
3106 LOG (GNUNET_ERROR_TYPE_DEBUG,
3109 GNUNET_i2s (&peers[i]));
3111 #ifdef ENABLE_MALICIOUS
3112 if ((NULL != att_peer_set) &&
3113 (1 == mal_type || 3 == mal_type))
3114 { /* Add attacked peer to local list */
3115 // TODO check if we sent a request and this was the first reply
3116 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3118 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
3120 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
3123 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3124 tmp_att_peer->peer_id = peers[i];
3125 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3128 add_peer_array_to_set (&peers[i], 1, att_peer_set);
3132 #endif /* ENABLE_MALICIOUS */
3133 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
3136 /* Make sure we 'know' about this peer */
3137 (void) Peers_insert_peer (&peers[i]);
3139 if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
3141 CustomPeerMap_put (pull_map, &peers[i]);
3145 Peers_schedule_operation (&peers[i], insert_in_pull_map);
3146 (void) Peers_issue_peer_liveliness_check (&peers[i]);
3151 Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING);
3152 clean_peer (sender);
3154 GNUNET_CADET_receive_done (Peers_get_recv_channel (sender));
3159 * Compute a random delay.
3160 * A uniformly distributed value between mean + spread and mean - spread.
3162 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
3163 * It would return a random value between 2 and 6 min.
3165 * @param mean the mean
3166 * @param spread the inverse amount of deviation from the mean
3168 static struct GNUNET_TIME_Relative
3169 compute_rand_delay (struct GNUNET_TIME_Relative mean,
3170 unsigned int spread)
3172 struct GNUNET_TIME_Relative half_interval;
3173 struct GNUNET_TIME_Relative ret;
3174 unsigned int rand_delay;
3175 unsigned int max_rand_delay;
3179 LOG (GNUNET_ERROR_TYPE_WARNING,
3180 "Not accepting spread of 0\n");
3184 GNUNET_assert (0 != mean.rel_value_us);
3186 /* Compute random time value between spread * mean and spread * mean */
3187 half_interval = GNUNET_TIME_relative_divide (mean, spread);
3189 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
3191 * Compute random value between (0 and 1) * round_interval
3192 * via multiplying round_interval with a 'fraction' (0 to value)/value
3194 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
3195 ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay);
3196 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
3197 ret = GNUNET_TIME_relative_add (ret, half_interval);
3199 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
3200 LOG (GNUNET_ERROR_TYPE_WARNING,
3201 "Returning FOREVER_REL\n");
3208 * Send single pull request
3210 * @param peer_id the peer to send the pull request to.
3213 send_pull_request (const struct GNUNET_PeerIdentity *peer)
3215 struct GNUNET_MQ_Envelope *ev;
3217 GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer,
3218 Peers_PULL_REPLY_PENDING));
3219 Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING);
3221 LOG (GNUNET_ERROR_TYPE_DEBUG,
3222 "Going to send PULL REQUEST to peer %s.\n",
3225 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
3226 Peers_send_message (peer, ev, "PULL REQUEST");
3233 * @param peer_id the peer to send the push to.
3236 send_push (const struct GNUNET_PeerIdentity *peer_id)
3238 struct GNUNET_MQ_Envelope *ev;
3240 LOG (GNUNET_ERROR_TYPE_DEBUG,
3241 "Going to send PUSH to peer %s.\n",
3242 GNUNET_i2s (peer_id));
3244 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
3245 Peers_send_message (peer_id, ev, "PUSH");
3250 do_round (void *cls);
3253 do_mal_round (void *cls);
3255 #ifdef ENABLE_MALICIOUS
3259 * @brief This function is called, when the client tells us to act malicious.
3260 * It verifies that @a msg is well-formed.
3262 * @param cls the closure (#ClientContext)
3263 * @param msg the message
3264 * @return #GNUNET_OK if @a msg is well-formed
3267 check_client_act_malicious (void *cls,
3268 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
3270 struct ClientContext *cli_ctx = cls;
3271 uint16_t msize = ntohs (msg->header.size);
3272 uint32_t num_peers = ntohl (msg->num_peers);
3274 msize -= sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage);
3275 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
3276 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
3278 LOG (GNUNET_ERROR_TYPE_ERROR,
3279 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3280 ntohl (msg->num_peers),
3281 (msize / sizeof (struct GNUNET_PeerIdentity)));
3283 GNUNET_SERVICE_client_drop (cli_ctx->client);
3284 return GNUNET_SYSERR;
3290 * Turn RPS service to act malicious.
3292 * @param cls Closure
3293 * @param client The client that sent the message
3294 * @param msg The message header
3297 handle_client_act_malicious (void *cls,
3298 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
3300 struct ClientContext *cli_ctx = cls;
3301 struct GNUNET_PeerIdentity *peers;
3302 uint32_t num_mal_peers_sent;
3303 uint32_t num_mal_peers_old;
3305 /* Do actual logic */
3306 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3307 mal_type = ntohl (msg->type);
3308 if (NULL == mal_peer_set)
3309 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
3311 LOG (GNUNET_ERROR_TYPE_DEBUG,
3312 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
3314 ntohl (msg->num_peers));
3317 { /* Try to maximise representation */
3318 /* Add other malicious peers to those we already know */
3320 num_mal_peers_sent = ntohl (msg->num_peers);
3321 num_mal_peers_old = num_mal_peers;
3322 GNUNET_array_grow (mal_peers,
3324 num_mal_peers + num_mal_peers_sent);
3325 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
3327 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
3329 /* Add all mal peers to mal_peer_set */
3330 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
3334 /* Substitute do_round () with do_mal_round () */
3335 GNUNET_SCHEDULER_cancel (do_round_task);
3336 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
3339 else if ( (2 == mal_type) ||
3341 { /* Try to partition the network */
3342 /* Add other malicious peers to those we already know */
3344 num_mal_peers_sent = ntohl (msg->num_peers) - 1;
3345 num_mal_peers_old = num_mal_peers;
3346 GNUNET_array_grow (mal_peers,
3348 num_mal_peers + num_mal_peers_sent);
3349 if (NULL != mal_peers &&
3352 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
3354 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
3356 /* Add all mal peers to mal_peer_set */
3357 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
3362 /* Store the one attacked peer */
3363 GNUNET_memcpy (&attacked_peer,
3364 &msg->attacked_peer,
3365 sizeof (struct GNUNET_PeerIdentity));
3366 /* Set the flag of the attacked peer to valid to avoid problems */
3367 if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
3369 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3372 LOG (GNUNET_ERROR_TYPE_DEBUG,
3373 "Attacked peer is %s\n",
3374 GNUNET_i2s (&attacked_peer));
3376 /* Substitute do_round () with do_mal_round () */
3377 GNUNET_SCHEDULER_cancel (do_round_task);
3378 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
3380 else if (0 == mal_type)
3381 { /* Stop acting malicious */
3382 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
3384 /* Substitute do_mal_round () with do_round () */
3385 GNUNET_SCHEDULER_cancel (do_round_task);
3386 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
3391 GNUNET_SERVICE_client_continue (cli_ctx->client);
3393 GNUNET_SERVICE_client_continue (cli_ctx->client);
3398 * Send out PUSHes and PULLs maliciously.
3400 * This is executed regylary.
3403 do_mal_round (void *cls)
3405 uint32_t num_pushes;
3407 struct GNUNET_TIME_Relative time_next_round;
3408 struct AttackedPeer *tmp_att_peer;
3410 LOG (GNUNET_ERROR_TYPE_DEBUG,
3411 "Going to execute next round maliciously type %" PRIu32 ".\n",
3413 do_round_task = NULL;
3414 GNUNET_assert (mal_type <= 3);
3415 /* Do malicious actions */
3417 { /* Try to maximise representation */
3419 /* The maximum of pushes we're going to send this round */
3420 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
3421 num_attacked_peers),
3422 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
3424 LOG (GNUNET_ERROR_TYPE_DEBUG,
3425 "Going to send %" PRIu32 " pushes\n",
3428 /* Send PUSHes to attacked peers */
3429 for (i = 0 ; i < num_pushes ; i++)
3431 if (att_peers_tail == att_peer_index)
3432 att_peer_index = att_peers_head;
3434 att_peer_index = att_peer_index->next;
3436 send_push (&att_peer_index->peer_id);
3439 /* Send PULLs to some peers to learn about additional peers to attack */
3440 tmp_att_peer = att_peer_index;
3441 for (i = 0 ; i < num_pushes * alpha ; i++)
3443 if (att_peers_tail == tmp_att_peer)
3444 tmp_att_peer = att_peers_head;
3446 att_peer_index = tmp_att_peer->next;
3448 send_pull_request (&tmp_att_peer->peer_id);
3453 else if (2 == mal_type)
3455 * Try to partition the network
3456 * Send as many pushes to the attacked peer as possible
3457 * That is one push per round as it will ignore more.
3459 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3460 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
3461 send_push (&attacked_peer);
3466 { /* Combined attack */
3468 /* Send PUSH to attacked peers */
3469 if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
3471 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3472 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
3474 LOG (GNUNET_ERROR_TYPE_DEBUG,
3475 "Goding to send push to attacked peer (%s)\n",
3476 GNUNET_i2s (&attacked_peer));
3477 send_push (&attacked_peer);
3480 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
3482 /* The maximum of pushes we're going to send this round */
3483 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
3484 num_attacked_peers),
3485 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
3487 LOG (GNUNET_ERROR_TYPE_DEBUG,
3488 "Going to send %" PRIu32 " pushes\n",
3491 for (i = 0; i < num_pushes; i++)
3493 if (att_peers_tail == att_peer_index)
3494 att_peer_index = att_peers_head;
3496 att_peer_index = att_peer_index->next;
3498 send_push (&att_peer_index->peer_id);
3501 /* Send PULLs to some peers to learn about additional peers to attack */
3502 tmp_att_peer = att_peer_index;
3503 for (i = 0; i < num_pushes * alpha; i++)
3505 if (att_peers_tail == tmp_att_peer)
3506 tmp_att_peer = att_peers_head;
3508 att_peer_index = tmp_att_peer->next;
3510 send_pull_request (&tmp_att_peer->peer_id);
3514 /* Schedule next round */
3515 time_next_round = compute_rand_delay (round_interval, 2);
3517 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
3519 GNUNET_assert (NULL == do_round_task);
3520 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
3521 &do_mal_round, NULL);
3522 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
3524 #endif /* ENABLE_MALICIOUS */
3528 * Send out PUSHes and PULLs, possibly update #view, samplers.
3530 * This is executed regylary.
3533 do_round (void *cls)
3536 const struct GNUNET_PeerIdentity *view_array;
3537 unsigned int *permut;
3538 unsigned int a_peers; /* Number of peers we send pushes to */
3539 unsigned int b_peers; /* Number of peers we send pull requests to */
3540 uint32_t first_border;
3541 uint32_t second_border;
3542 struct GNUNET_PeerIdentity peer;
3543 struct GNUNET_PeerIdentity *update_peer;
3545 LOG (GNUNET_ERROR_TYPE_DEBUG,
3546 "Going to execute next round.\n");
3547 do_round_task = NULL;
3548 LOG (GNUNET_ERROR_TYPE_DEBUG,
3549 "Printing view:\n");
3550 to_file (file_name_view_log,
3551 "___ new round ___");
3552 view_array = View_get_as_array ();
3553 for (i = 0; i < View_size (); i++)
3555 LOG (GNUNET_ERROR_TYPE_DEBUG,
3556 "\t%s\n", GNUNET_i2s (&view_array[i]));
3557 to_file (file_name_view_log,
3559 GNUNET_i2s_full (&view_array[i]));
3563 /* Send pushes and pull requests */
3564 if (0 < View_size ())
3566 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3570 a_peers = ceil (alpha * View_size ());
3572 LOG (GNUNET_ERROR_TYPE_DEBUG,
3573 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
3574 a_peers, alpha, View_size ());
3575 for (i = 0; i < a_peers; i++)
3577 peer = view_array[permut[i]];
3578 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
3579 { // FIXME if this fails schedule/loop this for later
3584 /* Send PULL requests */
3585 b_peers = ceil (beta * View_size ());
3586 first_border = a_peers;
3587 second_border = a_peers + b_peers;
3588 if (second_border > View_size ())
3590 first_border = View_size () - b_peers;
3591 second_border = View_size ();
3593 LOG (GNUNET_ERROR_TYPE_DEBUG,
3594 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
3595 b_peers, beta, View_size ());
3596 for (i = first_border; i < second_border; i++)
3598 peer = view_array[permut[i]];
3599 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
3600 GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
3601 { // FIXME if this fails schedule/loop this for later
3602 send_pull_request (&peer);
3606 GNUNET_free (permut);
3612 /* TODO see how many peers are in push-/pull- list! */
3614 if ((CustomPeerMap_size (push_map) <= alpha * View_size ()) &&
3615 (0 < CustomPeerMap_size (push_map)) &&
3616 (0 < CustomPeerMap_size (pull_map)))
3617 { /* If conditions for update are fulfilled, update */
3618 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
3620 uint32_t final_size;
3621 uint32_t peers_to_clean_size;
3622 struct GNUNET_PeerIdentity *peers_to_clean;
3624 peers_to_clean = NULL;
3625 peers_to_clean_size = 0;
3626 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
3627 GNUNET_memcpy (peers_to_clean,
3629 View_size () * sizeof (struct GNUNET_PeerIdentity));
3631 /* Seems like recreating is the easiest way of emptying the peermap */
3633 to_file (file_name_view_log,
3636 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
3637 CustomPeerMap_size (push_map));
3638 second_border = first_border +
3639 GNUNET_MIN (floor (beta * sampler_size_est_need),
3640 CustomPeerMap_size (pull_map));
3641 final_size = second_border +
3642 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
3644 /* Update view with peers received through PUSHes */
3645 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3646 CustomPeerMap_size (push_map));
3647 for (i = 0; i < first_border; i++)
3649 (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
3651 to_file (file_name_view_log,
3653 GNUNET_i2s_full (&view_array[i]));
3654 // TODO change the peer_flags accordingly
3656 GNUNET_free (permut);
3659 /* Update view with peers received through PULLs */
3660 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
3661 CustomPeerMap_size (pull_map));
3662 for (i = first_border; i < second_border; i++)
3664 (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
3665 permut[i - first_border]));
3666 to_file (file_name_view_log,
3668 GNUNET_i2s_full (&view_array[i]));
3669 // TODO change the peer_flags accordingly
3671 GNUNET_free (permut);
3674 /* Update view with peers from history */
3675 RPS_sampler_get_n_rand_peers (prot_sampler,
3678 final_size - second_border);
3679 // TODO change the peer_flags accordingly
3681 for (i = 0; i < View_size (); i++)
3682 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
3684 /* Clean peers that were removed from the view */
3685 for (i = 0; i < peers_to_clean_size; i++)
3687 to_file (file_name_view_log,
3689 GNUNET_i2s_full (&peers_to_clean[i]));
3690 clean_peer (&peers_to_clean[i]);
3691 //peer_destroy_channel_send (sender);
3694 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
3698 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
3700 // TODO independent of that also get some peers from CADET_get_peers()?
3702 LOG (GNUNET_ERROR_TYPE_DEBUG,
3703 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
3704 CustomPeerMap_size (push_map),
3705 CustomPeerMap_size (pull_map),
3708 alpha * View_size ());
3710 /* Update samplers */
3711 for (i = 0; i < CustomPeerMap_size (push_map); i++)
3713 update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
3714 LOG (GNUNET_ERROR_TYPE_DEBUG,
3715 "Updating with peer %s from push list\n",
3716 GNUNET_i2s (update_peer));
3717 insert_in_sampler (NULL, update_peer);
3718 clean_peer (update_peer); /* This cleans only if it is not in the view */
3719 //peer_destroy_channel_send (sender);
3722 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
3724 LOG (GNUNET_ERROR_TYPE_DEBUG,
3725 "Updating with peer %s from pull list\n",
3726 GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
3727 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
3728 /* This cleans only if it is not in the view */
3729 clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
3730 //peer_destroy_channel_send (sender);
3734 /* Empty push/pull lists */
3735 CustomPeerMap_clear (push_map);
3736 CustomPeerMap_clear (pull_map);
3738 struct GNUNET_TIME_Relative time_next_round;
3740 time_next_round = compute_rand_delay (round_interval, 2);
3742 /* Schedule next round */
3743 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
3745 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
3750 * This is called from GNUNET_CADET_get_peers().
3752 * It is called on every peer(ID) that cadet somehow has contact with.
3753 * We use those to initialise the sampler.
3756 init_peer_cb (void *cls,
3757 const struct GNUNET_PeerIdentity *peer,
3758 int tunnel, // "Do we have a tunnel towards this peer?"
3759 unsigned int n_paths, // "Number of known paths towards this peer"
3760 unsigned int best_path) // "How long is the best path?
3761 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
3765 LOG (GNUNET_ERROR_TYPE_DEBUG,
3766 "Got peer_id %s from cadet\n",
3773 * @brief Iterator function over stored, valid peers.
3775 * We initialise the sampler with those.
3777 * @param cls the closure
3778 * @param peer the peer id
3779 * @return #GNUNET_YES if we should continue to
3781 * #GNUNET_NO if not.
3784 valid_peers_iterator (void *cls,
3785 const struct GNUNET_PeerIdentity *peer)
3789 LOG (GNUNET_ERROR_TYPE_DEBUG,
3790 "Got stored, valid peer %s\n",
3799 * Iterator over peers from peerinfo.
3801 * @param cls closure
3802 * @param peer id of the peer, NULL for last call
3803 * @param hello hello message for the peer (can be NULL)
3804 * @param error message
3807 process_peerinfo_peers (void *cls,
3808 const struct GNUNET_PeerIdentity *peer,
3809 const struct GNUNET_HELLO_Message *hello,
3810 const char *err_msg)
3814 LOG (GNUNET_ERROR_TYPE_DEBUG,
3815 "Got peer_id %s from peerinfo\n",
3823 * Task run during shutdown.
3828 shutdown_task (void *cls)
3830 struct ClientContext *client_ctx;
3831 struct ReplyCls *reply_cls;
3833 LOG (GNUNET_ERROR_TYPE_DEBUG,
3834 "RPS is going down\n");
3836 /* Clean all clients */
3837 for (client_ctx = cli_ctx_head;
3838 NULL != cli_ctx_head;
3839 client_ctx = cli_ctx_head)
3841 /* Clean pending requests to the sampler */
3842 for (reply_cls = client_ctx->rep_cls_head;
3843 NULL != client_ctx->rep_cls_head;
3844 reply_cls = client_ctx->rep_cls_head)
3846 RPS_sampler_request_cancel (reply_cls->req_handle);
3847 GNUNET_CONTAINER_DLL_remove (client_ctx->rep_cls_head,
3848 client_ctx->rep_cls_tail,
3850 GNUNET_free (reply_cls);
3852 GNUNET_CONTAINER_DLL_remove (cli_ctx_head, cli_ctx_tail, client_ctx);
3853 GNUNET_free (client_ctx);
3855 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
3856 GNUNET_PEERINFO_disconnect (peerinfo_handle);
3858 if (NULL != do_round_task)
3860 GNUNET_SCHEDULER_cancel (do_round_task);
3861 do_round_task = NULL;
3866 GNUNET_NSE_disconnect (nse);
3867 RPS_sampler_destroy (prot_sampler);
3868 RPS_sampler_destroy (client_sampler);
3869 GNUNET_CADET_close_port (cadet_port);
3870 GNUNET_CADET_disconnect (cadet_handle);
3872 CustomPeerMap_destroy (push_map);
3873 CustomPeerMap_destroy (pull_map);
3874 #ifdef ENABLE_MALICIOUS
3875 struct AttackedPeer *tmp_att_peer;
3876 GNUNET_free (file_name_view_log);
3877 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
3878 if (NULL != mal_peer_set)
3879 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
3880 if (NULL != att_peer_set)
3881 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
3882 while (NULL != att_peers_head)
3884 tmp_att_peer = att_peers_head;
3885 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
3887 #endif /* ENABLE_MALICIOUS */
3892 * Handle client connecting to the service.
3895 * @param client the new client
3896 * @param mq the message queue of @a client
3900 client_connect_cb (void *cls,
3901 struct GNUNET_SERVICE_Client *client,
3902 struct GNUNET_MQ_Handle *mq)
3904 struct ClientContext *cli_ctx;
3906 LOG (GNUNET_ERROR_TYPE_DEBUG,
3907 "Client connected\n");
3909 return client; /* Server was destroyed before a client connected. Shutting down */
3910 cli_ctx = GNUNET_new (struct ClientContext);
3911 cli_ctx->mq = GNUNET_SERVICE_client_get_mq (client);
3912 cli_ctx->client = client;
3913 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
3920 * Callback called when a client disconnected from the service
3922 * @param cls closure for the service
3923 * @param c the client that disconnected
3924 * @param internal_cls should be equal to @a c
3927 client_disconnect_cb (void *cls,
3928 struct GNUNET_SERVICE_Client *client,
3931 struct ClientContext *cli_ctx = internal_cls;
3933 GNUNET_assert (client == cli_ctx->client);
3935 {/* shutdown task - destroy all clients */
3936 while (NULL != cli_ctx_head)
3937 destroy_cli_ctx (cli_ctx_head);
3940 { /* destroy this client */
3941 LOG (GNUNET_ERROR_TYPE_DEBUG,
3942 "Client disconnected. Destroy its context.\n");
3943 destroy_cli_ctx (cli_ctx);
3949 * Handle random peer sampling clients.
3951 * @param cls closure
3952 * @param c configuration to use
3953 * @param service the initialized service
3957 const struct GNUNET_CONFIGURATION_Handle *c,
3958 struct GNUNET_SERVICE_Handle *service)
3960 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
3961 GNUNET_MQ_hd_fixed_size (peer_check,
3962 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
3963 struct GNUNET_MessageHeader,
3965 GNUNET_MQ_hd_fixed_size (peer_push,
3966 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
3967 struct GNUNET_MessageHeader,
3969 GNUNET_MQ_hd_fixed_size (peer_pull_request,
3970 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
3971 struct GNUNET_MessageHeader,
3973 GNUNET_MQ_hd_var_size (peer_pull_reply,
3974 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
3975 struct GNUNET_RPS_P2P_PullReplyMessage,
3977 GNUNET_MQ_handler_end ()
3982 char* fn_valid_peers;
3983 struct GNUNET_HashCode port;
3985 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
3990 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
3991 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3992 "STARTING SERVICE (rps) for peer [%s]\n",
3993 GNUNET_i2s (&own_identity));
3994 #ifdef ENABLE_MALICIOUS
3995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3996 "Malicious execution compiled in.\n");
3997 #endif /* ENABLE_MALICIOUS */
4001 /* Get time interval from the configuration */
4002 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
4006 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4007 "RPS", "ROUNDINTERVAL");
4008 GNUNET_SCHEDULER_shutdown ();
4012 /* Get initial size of sampler/view from the configuration */
4014 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
4015 (long long unsigned int *) &sampler_size_est_need))
4017 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4019 GNUNET_SCHEDULER_shutdown ();
4022 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need);
4025 GNUNET_CONFIGURATION_get_value_filename (cfg,
4027 "FILENAME_VALID_PEERS",
4030 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4031 "rps", "FILENAME_VALID_PEERS");
4037 /* file_name_view_log */
4038 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
4040 LOG (GNUNET_ERROR_TYPE_WARNING,
4041 "Failed to create directory /tmp/rps/\n");
4044 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
4045 file_name_view_log = GNUNET_malloc (size);
4046 out_size = GNUNET_snprintf (file_name_view_log,
4049 GNUNET_i2s_full (&own_identity));
4050 if (size < out_size ||
4053 LOG (GNUNET_ERROR_TYPE_WARNING,
4054 "Failed to write string to buffer (size: %i, out_size: %i)\n",
4060 /* connect to NSE */
4061 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
4068 /* Initialise cadet */
4069 cadet_handle = GNUNET_CADET_connect (cfg);
4070 GNUNET_assert (NULL != cadet_handle);
4071 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
4072 strlen (GNUNET_APPLICATION_PORT_RPS),
4074 cadet_port = GNUNET_CADET_open_port (cadet_handle,
4076 &Peers_handle_inbound_channel, /* Connect handler */
4078 NULL, /* WindowSize handler */
4079 cleanup_destroyed_channel, /* Disconnect handler */
4083 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
4084 Peers_initialise (fn_valid_peers, cadet_handle, cleanup_destroyed_channel,
4085 cadet_handlers, &own_identity);
4086 GNUNET_free (fn_valid_peers);
4088 /* Initialise sampler */
4089 struct GNUNET_TIME_Relative half_round_interval;
4090 struct GNUNET_TIME_Relative max_round_interval;
4092 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
4093 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
4095 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
4096 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
4098 /* Initialise push and pull maps */
4099 push_map = CustomPeerMap_create (4);
4100 pull_map = CustomPeerMap_create (4);
4103 //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
4104 //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
4105 // TODO send push/pull to each of those peers?
4106 // TODO read stored valid peers from last run
4107 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
4108 Peers_get_valid_peers (valid_peers_iterator, NULL);
4110 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
4112 process_peerinfo_peers,
4115 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
4117 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
4118 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
4120 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
4125 * Define "main" method using service macro.
4129 GNUNET_SERVICE_OPTION_NONE,
4132 &client_disconnect_cb,
4134 GNUNET_MQ_hd_fixed_size (client_request,
4135 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
4136 struct GNUNET_RPS_CS_RequestMessage,
4138 GNUNET_MQ_hd_fixed_size (client_request_cancel,
4139 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
4140 struct GNUNET_RPS_CS_RequestCancelMessage,
4142 GNUNET_MQ_hd_var_size (client_seed,
4143 GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
4144 struct GNUNET_RPS_CS_SeedMessage,
4146 #ifdef ENABLE_MALICIOUS
4147 GNUNET_MQ_hd_var_size (client_act_malicious,
4148 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
4149 struct GNUNET_RPS_CS_ActMaliciousMessage,
4151 #endif /* ENABLE_MALICIOUS */
4152 GNUNET_MQ_handler_end());
4154 /* end of gnunet-service-rps.c */