2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file rps/gnunet-service-rps_peers.c
23 * @brief utilities for managing (information about) peers
24 * @author Julius Bünger
27 #include "gnunet_applications.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_cadet_service.h"
32 #include "gnunet-service-rps_peers.h"
36 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-peers",__VA_ARGS__)
40 * Set a peer flag of given peer context.
42 #define set_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask))
45 * Get peer flag of given peer context.
47 #define check_peer_flag_set(peer_ctx, mask)\
48 ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
51 * Unset flag of given peer context.
53 #define unset_peer_flag(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask))
56 * Set a channel flag of given channel context.
58 #define set_channel_flag(channel_flags, mask) ((*channel_flags) |= (mask))
61 * Get channel flag of given channel context.
63 #define check_channel_flag_set(channel_flags, mask)\
64 ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
67 * Unset flag of given channel context.
69 #define unset_channel_flag(channel_flags, mask) ((*channel_flags) &= ~(mask))
74 * Pending operation on peer consisting of callback and closure
76 * When an operation cannot be executed right now this struct is used to store
77 * the callback and closure for later execution.
93 * List containing all messages that are yet to be send
95 * This is used to keep track of all messages that have not been sent yet. When
96 * a peer is to be removed the pending messages can be removed properly.
103 struct PendingMessage *next;
104 struct PendingMessage *prev;
107 * The envelope to the corresponding message
109 struct GNUNET_MQ_Envelope *ev;
112 * The corresponding context
114 struct PeerContext *peer_ctx;
123 * Struct used to keep track of other peer's status
125 * This is stored in a multipeermap.
126 * It contains information such as cadet channels, a message queue for sending,
127 * status about the channels, the pending operations on this peer and some flags
128 * about the status of the peer itself. (live, valid, ...)
133 * Message queue open to client
135 struct GNUNET_MQ_Handle *mq;
138 * Channel open to client.
140 struct GNUNET_CADET_Channel *send_channel;
143 * Flags to the sending channel
145 uint32_t *send_channel_flags;
148 * Channel open from client.
150 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
153 * Flags to the receiving channel
155 uint32_t *recv_channel_flags;
158 * Array of pending operations on this peer.
160 struct PeerPendingOp *pending_ops;
163 * Handle to the callback given to cadet_ntfy_tmt_rdy()
165 * To be canceled on shutdown.
167 struct PendingMessage *liveliness_check_pending;
170 * Number of pending operations.
172 unsigned int num_pending_ops;
175 * Identity of the peer
177 struct GNUNET_PeerIdentity peer_id;
180 * Flags indicating status of peer
185 * Last time we received something from that peer.
187 struct GNUNET_TIME_Absolute last_message_recv;
190 * Last time we received a keepalive message.
192 struct GNUNET_TIME_Absolute last_keepalive;
195 * DLL with all messages that are yet to be sent
197 struct PendingMessage *pending_messages_head;
198 struct PendingMessage *pending_messages_tail;
201 * This is pobably followed by 'statistical' data (when we first saw
202 * him, how did we get his ID, how many pushes (in a timeinterval),
208 * @brief Closure to #valid_peer_iterator
210 struct PeersIteratorCls
215 PeersIterator iterator;
218 * Closure to iterator
224 * @brief Hashmap of valid peers.
226 static struct GNUNET_CONTAINER_MultiPeerMap *valid_peers;
229 * @brief Maximum number of valid peers to keep.
230 * TODO read from config
232 static uint32_t num_valid_peers_max = UINT32_MAX;
235 * @brief Filename of the file that stores the valid peers persistently.
237 static char *filename_valid_peers;
240 * Set of all peers to keep track of them.
242 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
245 * Own #GNUNET_PeerIdentity.
247 static const struct GNUNET_PeerIdentity *own_identity;
252 static struct GNUNET_CADET_Handle *cadet_handle;
256 * @brief Get the #PeerContext associated with a peer
258 * @param peer the peer id
260 * @return the #PeerContext
262 static struct PeerContext *
263 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
265 struct PeerContext *ctx;
268 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
269 GNUNET_assert (GNUNET_YES == ret);
270 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
271 GNUNET_assert (NULL != ctx);
277 * @brief Create a new #PeerContext and insert it into the peer map
279 * @param peer the peer to create the #PeerContext for
281 * @return the #PeerContext
283 static struct PeerContext *
284 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
286 struct PeerContext *ctx;
289 GNUNET_assert (GNUNET_NO == Peers_check_peer_known (peer));
291 ctx = GNUNET_new (struct PeerContext);
292 ctx->peer_id = *peer;
293 ctx->send_channel_flags = GNUNET_new (uint32_t);
294 ctx->recv_channel_flags = GNUNET_new (uint32_t);
295 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
296 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
297 GNUNET_assert (GNUNET_OK == ret);
303 * @brief Create or get a #PeerContext
305 * @param peer the peer to get the associated context to
307 * @return the context
309 static struct PeerContext *
310 create_or_get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
312 if (GNUNET_NO == Peers_check_peer_known (peer))
314 return create_peer_ctx (peer);
316 return get_peer_ctx (peer);
321 * @brief Check whether we have a connection to this @a peer
323 * Also sets the #Peers_ONLINE flag accordingly
325 * @param peer the peer in question
327 * @return #GNUNET_YES if we are connected
328 * #GNUNET_NO otherwise
331 Peers_check_connected (const struct GNUNET_PeerIdentity *peer)
333 const struct PeerContext *peer_ctx;
335 /* If we don't know about this peer we don't know whether it's online */
336 if (GNUNET_NO == Peers_check_peer_known (peer))
340 /* Get the context */
341 peer_ctx = get_peer_ctx (peer);
342 /* If we have no channel to this peer we don't know whether it's online */
343 if ( (NULL == peer_ctx->send_channel) &&
344 (NULL == peer_ctx->recv_channel) )
346 Peers_unset_peer_flag (peer, Peers_ONLINE);
349 /* Otherwise (if we have a channel, we know that it's online */
350 Peers_set_peer_flag (peer, Peers_ONLINE);
356 * @brief The closure to #get_rand_peer_iterator.
358 struct GetRandPeerIteratorCls
361 * @brief The index of the peer to return.
362 * Will be decreased until 0.
363 * Then current peer is returned.
368 * @brief Pointer to peer to return.
370 const struct GNUNET_PeerIdentity *peer;
375 * @brief Iterator function for #get_random_peer_from_peermap.
377 * Implements #GNUNET_CONTAINER_PeerMapIterator.
378 * Decreases the index until the index is null.
379 * Then returns the current peer.
381 * @param cls the #GetRandPeerIteratorCls containing index and peer
382 * @param peer current peer
383 * @param value unused
385 * @return #GNUNET_YES if we should continue to
390 get_rand_peer_iterator (void *cls,
391 const struct GNUNET_PeerIdentity *peer,
394 struct GetRandPeerIteratorCls *iterator_cls = cls;
395 if (0 >= iterator_cls->index)
397 iterator_cls->peer = peer;
400 iterator_cls->index--;
406 * @brief Get a random peer from @a peer_map
408 * @param peer_map the peer_map to get the peer from
410 * @return a random peer
412 static const struct GNUNET_PeerIdentity *
413 get_random_peer_from_peermap (const struct
414 GNUNET_CONTAINER_MultiPeerMap *peer_map)
416 struct GetRandPeerIteratorCls *iterator_cls;
417 const struct GNUNET_PeerIdentity *ret;
419 iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls);
420 iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
421 GNUNET_CONTAINER_multipeermap_size (peer_map));
422 (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
423 get_rand_peer_iterator,
425 ret = iterator_cls->peer;
426 GNUNET_free (iterator_cls);
432 * @brief Add a given @a peer to valid peers.
434 * If valid peers are already #num_valid_peers_max, delete a peer previously.
436 * @param peer the peer that is added to the valid peers.
438 * @return #GNUNET_YES if no other peer had to be removed
439 * #GNUNET_NO otherwise
442 add_valid_peer (const struct GNUNET_PeerIdentity *peer)
444 const struct GNUNET_PeerIdentity *rand_peer;
448 while (GNUNET_CONTAINER_multipeermap_size (valid_peers) >= num_valid_peers_max)
450 rand_peer = get_random_peer_from_peermap (valid_peers);
451 GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer);
454 (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL,
455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
461 * @brief Set the peer flag to living and
462 * call the pending operations on this peer.
464 * Also adds peer to #valid_peers.
466 * @param peer_ctx the #PeerContext of the peer to set live
469 set_peer_live (struct PeerContext *peer_ctx)
471 struct GNUNET_PeerIdentity *peer;
474 peer = &peer_ctx->peer_id;
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "Peer %s is live and valid, calling %i pending operations on it\n",
478 peer_ctx->num_pending_ops);
480 if (NULL != peer_ctx->liveliness_check_pending)
482 LOG (GNUNET_ERROR_TYPE_DEBUG,
483 "Removing pending liveliness check for peer %s\n",
484 GNUNET_i2s (&peer_ctx->peer_id));
485 // TODO wait until cadet sets mq->cancel_impl
486 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
487 GNUNET_free (peer_ctx->liveliness_check_pending);
488 peer_ctx->liveliness_check_pending = NULL;
491 (void) add_valid_peer (peer);
492 set_peer_flag (peer_ctx, Peers_ONLINE);
494 /* Call pending operations */
495 for (i = 0; i < peer_ctx->num_pending_ops; i++)
497 peer_ctx->pending_ops[i].op (peer_ctx->pending_ops[i].op_cls, peer);
499 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
504 * @brief Get the channel of a peer. If not existing, create.
506 * @param peer the peer id
507 * @return the #GNUNET_CADET_Channel used to send data to @a peer
509 struct GNUNET_CADET_Channel *
510 get_channel (const struct GNUNET_PeerIdentity *peer)
512 struct PeerContext *peer_ctx;
513 struct GNUNET_HashCode port;
515 peer_ctx = get_peer_ctx (peer);
516 if (NULL == peer_ctx->send_channel)
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Trying to establish channel to peer %s\n",
521 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
522 strlen (GNUNET_APPLICATION_PORT_RPS),
524 peer_ctx->send_channel =
525 GNUNET_CADET_channel_create (cadet_handle,
526 peer_ctx->send_channel_flags, /* context */
529 GNUNET_CADET_OPTION_RELIABLE);
531 GNUNET_assert (NULL != peer_ctx->send_channel);
532 return peer_ctx->send_channel;
537 * Get the message queue (#GNUNET_MQ_Handle) of a specific peer.
539 * If we already have a message queue open to this client,
540 * simply return it, otherways create one.
542 * @param peer the peer to get the mq to
543 * @return the #GNUNET_MQ_Handle
545 static struct GNUNET_MQ_Handle *
546 get_mq (const struct GNUNET_PeerIdentity *peer)
548 struct PeerContext *peer_ctx;
550 peer_ctx = get_peer_ctx (peer);
552 if (NULL == peer_ctx->mq)
554 (void) get_channel (peer);
555 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
562 * @brief This is called in response to the first message we sent as a
565 * @param cls #PeerContext of peer with pending liveliness check
568 mq_liveliness_check_successful (void *cls)
570 struct PeerContext *peer_ctx = cls;
572 if (NULL != peer_ctx->liveliness_check_pending)
574 LOG (GNUNET_ERROR_TYPE_DEBUG,
575 "Liveliness check for peer %s was successfull\n",
576 GNUNET_i2s (&peer_ctx->peer_id));
577 GNUNET_free (peer_ctx->liveliness_check_pending);
578 peer_ctx->liveliness_check_pending = NULL;
579 set_peer_live (peer_ctx);
584 * Issue a check whether peer is live
586 * @param peer_ctx the context of the peer
589 check_peer_live (struct PeerContext *peer_ctx)
591 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 "Get informed about peer %s getting live\n",
593 GNUNET_i2s (&peer_ctx->peer_id));
595 struct GNUNET_MQ_Handle *mq;
596 struct GNUNET_MQ_Envelope *ev;
598 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE);
599 peer_ctx->liveliness_check_pending = GNUNET_new (struct PendingMessage);
600 peer_ctx->liveliness_check_pending->ev = ev;
601 peer_ctx->liveliness_check_pending->peer_ctx = peer_ctx;
602 peer_ctx->liveliness_check_pending->type = "Check liveliness";
603 mq = get_mq (&peer_ctx->peer_id);
604 GNUNET_MQ_notify_sent (ev,
605 mq_liveliness_check_successful,
607 GNUNET_MQ_send (mq, ev);
611 * @brief Add an envelope to a message passed to mq to list of pending messages
613 * @param peer peer the message was sent to
614 * @param ev envelope to the message
615 * @param type type of the message to be sent
616 * @return pointer to pending message
618 static struct PendingMessage *
619 insert_pending_message (const struct GNUNET_PeerIdentity *peer,
620 struct GNUNET_MQ_Envelope *ev,
623 struct PendingMessage *pending_msg;
624 struct PeerContext *peer_ctx;
626 peer_ctx = get_peer_ctx (peer);
627 pending_msg = GNUNET_new (struct PendingMessage);
628 pending_msg->ev = ev;
629 pending_msg->peer_ctx = peer_ctx;
630 pending_msg->type = type;
631 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
632 peer_ctx->pending_messages_tail,
639 * @brief Remove a pending message from the respective DLL
641 * @param pending_msg the pending message to remove
644 remove_pending_message (struct PendingMessage *pending_msg)
646 struct PeerContext *peer_ctx;
648 peer_ctx = pending_msg->peer_ctx;
649 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
650 peer_ctx->pending_messages_tail,
652 /* FIXME We are not able to cancel messages as #GNUNET_CADET_mq_create () does
653 * not set a #GNUNET_MQ_CancelImpl */
654 /* GNUNET_MQ_send_cancel (peer_ctx->pending_messages_head->ev); */
655 GNUNET_free (pending_msg);
660 * @brief Check whether function of type #PeerOp was already scheduled
662 * The array with pending operations will probably never grow really big, so
663 * iterating over it should be ok.
665 * @param peer the peer to check
666 * @param peer_op the operation (#PeerOp) on the peer
668 * @return #GNUNET_YES if this operation is scheduled on that peer
669 * #GNUNET_NO otherwise
672 check_operation_scheduled (const struct GNUNET_PeerIdentity *peer,
673 const PeerOp peer_op)
675 const struct PeerContext *peer_ctx;
678 peer_ctx = get_peer_ctx (peer);
679 for (i = 0; i < peer_ctx->num_pending_ops; i++)
680 if (peer_op == peer_ctx->pending_ops[i].op)
687 * Iterator over hash map entries. Deletes all contexts of peers.
690 * @param key current public key
691 * @param value value in the hash map
692 * @return #GNUNET_YES if we should continue to iterate,
696 peermap_clear_iterator (void *cls,
697 const struct GNUNET_PeerIdentity *key,
700 Peers_remove_peer (key);
706 * @brief This is called once a message is sent.
708 * Removes the pending message
710 * @param cls type of the message that was sent
713 mq_notify_sent_cb (void *cls)
715 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
716 LOG (GNUNET_ERROR_TYPE_DEBUG,
719 remove_pending_message (pending_msg);
724 * @brief Iterator function for #store_valid_peers.
726 * Implements #GNUNET_CONTAINER_PeerMapIterator.
727 * Writes single peer to disk.
729 * @param cls the file handle to write to.
730 * @param peer current peer
731 * @param value unused
733 * @return #GNUNET_YES if we should continue to
738 store_peer_presistently_iterator (void *cls,
739 const struct GNUNET_PeerIdentity *peer,
742 const struct GNUNET_DISK_FileHandle *fh = cls;
743 char peer_string[128];
751 size = GNUNET_snprintf (peer_string,
752 sizeof (peer_string),
754 GNUNET_i2s_full (peer));
755 GNUNET_assert (53 == size);
756 ret = GNUNET_DISK_file_write (fh,
759 GNUNET_assert (size == ret);
765 * @brief Store the peers currently in #valid_peers to disk.
770 struct GNUNET_DISK_FileHandle *fh;
771 uint32_t number_written_peers;
774 if (0 == strncmp ("DISABLE", filename_valid_peers, 7))
779 ret = GNUNET_DISK_directory_create_for_file (filename_valid_peers);
780 if (GNUNET_SYSERR == ret)
782 LOG (GNUNET_ERROR_TYPE_WARNING,
783 "Not able to create directory for file `%s'\n",
784 filename_valid_peers);
787 else if (GNUNET_NO == ret)
789 LOG (GNUNET_ERROR_TYPE_WARNING,
790 "Directory for file `%s' exists but is not writable for us\n",
791 filename_valid_peers);
794 fh = GNUNET_DISK_file_open (filename_valid_peers,
795 GNUNET_DISK_OPEN_WRITE |
796 GNUNET_DISK_OPEN_CREATE,
797 GNUNET_DISK_PERM_USER_READ |
798 GNUNET_DISK_PERM_USER_WRITE);
801 LOG (GNUNET_ERROR_TYPE_WARNING,
802 "Not able to write valid peers to file `%s'\n",
803 filename_valid_peers);
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 "Writing %u valid peers to disk\n",
808 GNUNET_CONTAINER_multipeermap_size (valid_peers));
809 number_written_peers =
810 GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
811 store_peer_presistently_iterator,
813 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
814 GNUNET_assert (number_written_peers ==
815 GNUNET_CONTAINER_multipeermap_size (valid_peers));
820 * @brief Convert string representation of peer id to peer id.
822 * Counterpart to #GNUNET_i2s_full.
824 * @param string_repr The string representation of the peer id
826 * @return The peer id
828 static const struct GNUNET_PeerIdentity *
829 s2i_full (const char *string_repr)
831 struct GNUNET_PeerIdentity *peer;
835 peer = GNUNET_new (struct GNUNET_PeerIdentity);
836 len = strlen (string_repr);
839 LOG (GNUNET_ERROR_TYPE_WARNING,
840 "Not able to convert string representation of PeerID to PeerID\n"
841 "Sting representation: %s (len %u) - too short\n",
850 ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr,
853 if (GNUNET_OK != ret)
855 LOG (GNUNET_ERROR_TYPE_WARNING,
856 "Not able to convert string representation of PeerID to PeerID\n"
857 "Sting representation: %s\n",
866 * @brief Restore the peers on disk to #valid_peers.
869 restore_valid_peers ()
873 struct GNUNET_DISK_FileHandle *fh;
877 const char *str_repr;
878 const struct GNUNET_PeerIdentity *peer;
880 if (0 == strncmp ("DISABLE", filename_valid_peers, 7))
885 if (GNUNET_OK != GNUNET_DISK_file_test (filename_valid_peers))
889 fh = GNUNET_DISK_file_open (filename_valid_peers,
890 GNUNET_DISK_OPEN_READ,
891 GNUNET_DISK_PERM_NONE);
892 GNUNET_assert (NULL != fh);
893 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size));
894 num_peers = file_size / 53;
895 buf = GNUNET_malloc (file_size);
896 size_read = GNUNET_DISK_file_read (fh, buf, file_size);
897 GNUNET_assert (size_read == file_size);
898 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53)
900 str_repr = GNUNET_strndup (iter_buf, 53);
901 peer = s2i_full (str_repr);
902 add_valid_peer (peer);
903 LOG (GNUNET_ERROR_TYPE_DEBUG,
904 "Restored valid peer %s from disk\n",
905 GNUNET_i2s_full (peer));
907 LOG (GNUNET_ERROR_TYPE_DEBUG,
908 "num_peers: %" PRIu32 ", _size (valid_peers): %u\n",
910 GNUNET_CONTAINER_multipeermap_size (valid_peers));
911 GNUNET_assert (num_peers == GNUNET_CONTAINER_multipeermap_size (valid_peers));
912 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
913 LOG (GNUNET_ERROR_TYPE_DEBUG,
914 "Restored %u valid peers from disk\n",
920 * @brief Initialise storage of peers
922 * @param fn_valid_peers filename of the file used to store valid peer ids
923 * @param cadet_h cadet handle
924 * @param own_id own peer identity
927 Peers_initialise (char* fn_valid_peers,
928 struct GNUNET_CADET_Handle *cadet_h,
929 const struct GNUNET_PeerIdentity *own_id)
931 filename_valid_peers = GNUNET_strdup (fn_valid_peers);
932 cadet_handle = cadet_h;
933 own_identity = own_id;
934 peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
935 valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
936 restore_valid_peers ();
941 * @brief Delete storage of peers that was created with #Peers_initialise ()
947 GNUNET_CONTAINER_multipeermap_iterate (peer_map,
948 peermap_clear_iterator,
951 LOG (GNUNET_ERROR_TYPE_WARNING,
952 "Iteration destroying peers was aborted.\n");
954 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
955 store_valid_peers ();
956 GNUNET_free (filename_valid_peers);
957 GNUNET_CONTAINER_multipeermap_destroy (valid_peers);
962 * Iterator over #valid_peers hash map entries.
964 * @param cls closure - unused
965 * @param peer current peer id
966 * @param value value in the hash map - unused
967 * @return #GNUNET_YES if we should continue to
972 valid_peer_iterator (void *cls,
973 const struct GNUNET_PeerIdentity *peer,
976 struct PeersIteratorCls *it_cls = cls;
978 return it_cls->iterator (it_cls->cls,
984 * @brief Get all currently known, valid peer ids.
986 * @param it function to call on each peer id
987 * @param it_cls extra argument to @a it
988 * @return the number of key value pairs processed,
989 * #GNUNET_SYSERR if it aborted iteration
992 Peers_get_valid_peers (PeersIterator iterator,
995 struct PeersIteratorCls *cls;
998 cls = GNUNET_new (struct PeersIteratorCls);
999 cls->iterator = iterator;
1001 ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
1002 valid_peer_iterator,
1010 * @brief Add peer to known peers.
1012 * This function is called on new peer_ids from 'external' sources
1013 * (client seed, cadet get_peers(), ...)
1015 * @param peer the new #GNUNET_PeerIdentity
1017 * @return #GNUNET_YES if peer was inserted
1018 * #GNUNET_NO otherwise (if peer was already known or
1019 * peer was #own_identity)
1022 Peers_insert_peer (const struct GNUNET_PeerIdentity *peer)
1024 if ( (GNUNET_YES == Peers_check_peer_known (peer)) ||
1025 (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, own_identity)) )
1027 return GNUNET_NO; /* We already know this peer - nothing to do */
1029 (void) create_peer_ctx (peer);
1035 * @brief Try connecting to a peer to see whether it is online
1037 * If not known yet, insert into known peers
1039 * @param peer the peer whose liveliness is to be checked
1040 * @return #GNUNET_YES if peer had to be inserted
1041 * #GNUNET_NO otherwise (if peer was already known or
1042 * peer was #own_identity)
1045 Peers_issue_peer_liveliness_check (const struct GNUNET_PeerIdentity *peer)
1047 struct PeerContext *peer_ctx;
1050 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, own_identity))
1054 ret = Peers_insert_peer (peer);
1055 peer_ctx = get_peer_ctx (peer);
1056 if (GNUNET_NO == Peers_check_peer_flag (peer, Peers_ONLINE))
1058 check_peer_live (peer_ctx);
1065 * @brief Remove unecessary data
1067 * If the other peer is not intending to send messages, we have messages pending
1068 * to be sent to this peer and we are not waiting for a reply, remove the
1069 * information about it (its #PeerContext).
1071 * @param peer the peer to clean
1072 * @return #GNUNET_YES if peer was removed
1073 * #GNUNET_NO otherwise
1076 Peers_clean_peer (const struct GNUNET_PeerIdentity *peer)
1078 struct PeerContext *peer_ctx;
1080 // TODO actually remove unnecessary data
1082 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1087 peer_ctx = get_peer_ctx (peer);
1088 if ( (NULL != peer_ctx->recv_channel) ||
1089 (NULL != peer_ctx->pending_messages_head) ||
1090 (GNUNET_NO == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)) )
1094 Peers_remove_peer (peer);
1100 * @brief Remove peer
1102 * @param peer the peer to clean
1103 * @return #GNUNET_YES if peer was removed
1104 * #GNUNET_NO otherwise
1107 Peers_remove_peer (const struct GNUNET_PeerIdentity *peer)
1109 struct PeerContext *peer_ctx;
1111 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1116 peer_ctx = get_peer_ctx (peer);
1117 set_peer_flag (peer_ctx, Peers_TO_DESTROY);
1118 LOG (GNUNET_ERROR_TYPE_DEBUG,
1119 "Going to remove peer %s\n",
1120 GNUNET_i2s (&peer_ctx->peer_id));
1121 Peers_unset_peer_flag (peer, Peers_ONLINE);
1123 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
1124 while (NULL != peer_ctx->pending_messages_head)
1126 LOG (GNUNET_ERROR_TYPE_DEBUG,
1127 "Removing unsent %s\n",
1128 peer_ctx->pending_messages_head->type);
1129 remove_pending_message (peer_ctx->pending_messages_head);
1131 /* If we are still waiting for notification whether this peer is live
1132 * cancel the according task */
1133 if (NULL != peer_ctx->liveliness_check_pending)
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "Removing pending liveliness check for peer %s\n",
1137 GNUNET_i2s (&peer_ctx->peer_id));
1138 // TODO wait until cadet sets mq->cancel_impl
1139 //GNUNET_MQ_send_cancel (peer_ctx->liveliness_check_pending->ev);
1140 GNUNET_free (peer_ctx->liveliness_check_pending);
1141 peer_ctx->liveliness_check_pending = NULL;
1143 if (NULL != peer_ctx->send_channel)
1145 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1146 peer_ctx->send_channel = NULL;
1148 if (NULL != peer_ctx->recv_channel)
1150 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1151 peer_ctx->recv_channel = NULL;
1153 if (NULL != peer_ctx->mq)
1155 GNUNET_MQ_destroy (peer_ctx->mq);
1156 peer_ctx->mq = NULL;
1159 GNUNET_free (peer_ctx->send_channel_flags);
1160 GNUNET_free (peer_ctx->recv_channel_flags);
1162 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, &peer_ctx->peer_id))
1164 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1166 GNUNET_free (peer_ctx);
1172 * @brief set flags on a given peer.
1174 * @param peer the peer to set flags on
1175 * @param flags the flags
1178 Peers_set_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1180 struct PeerContext *peer_ctx;
1182 peer_ctx = get_peer_ctx (peer);
1183 set_peer_flag (peer_ctx, flags);
1188 * @brief unset flags on a given peer.
1190 * @param peer the peer to unset flags on
1191 * @param flags the flags
1194 Peers_unset_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1196 struct PeerContext *peer_ctx;
1198 peer_ctx = get_peer_ctx (peer);
1199 unset_peer_flag (peer_ctx, flags);
1204 * @brief Check whether flags on a peer are set.
1206 * @param peer the peer to check the flag of
1207 * @param flags the flags to check
1209 * @return #GNUNET_SYSERR if peer is not known
1210 * #GNUNET_YES if all given flags are set
1211 * #GNUNET_NO otherwise
1214 Peers_check_peer_flag (const struct GNUNET_PeerIdentity *peer, enum Peers_PeerFlags flags)
1216 struct PeerContext *peer_ctx;
1218 if (GNUNET_NO == Peers_check_peer_known (peer))
1220 return GNUNET_SYSERR;
1222 peer_ctx = get_peer_ctx (peer);
1223 return check_peer_flag_set (peer_ctx, flags);
1228 * @brief set flags on a given channel.
1230 * @param channel the channel to set flags on
1231 * @param flags the flags
1234 Peers_set_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1236 set_channel_flag (channel_flags, flags);
1241 * @brief unset flags on a given channel.
1243 * @param channel the channel to unset flags on
1244 * @param flags the flags
1247 Peers_unset_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1249 unset_channel_flag (channel_flags, flags);
1254 * @brief Check whether flags on a channel are set.
1256 * @param channel the channel to check the flag of
1257 * @param flags the flags to check
1259 * @return #GNUNET_YES if all given flags are set
1260 * #GNUNET_NO otherwise
1263 Peers_check_channel_flag (uint32_t *channel_flags, enum Peers_ChannelFlags flags)
1265 return check_channel_flag_set (channel_flags, flags);
1270 * @brief Check whether we have information about the given peer.
1272 * FIXME probably deprecated. Make this the new _online.
1274 * @param peer peer in question
1276 * @return #GNUNET_YES if peer is known
1277 * #GNUNET_NO if peer is not knwon
1280 Peers_check_peer_known (const struct GNUNET_PeerIdentity *peer)
1282 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
1287 * @brief Check whether @a peer is actually a peer.
1289 * A valid peer is a peer that we know exists eg. we were connected to once.
1291 * @param peer peer in question
1293 * @return #GNUNET_YES if peer is valid
1294 * #GNUNET_NO if peer is not valid
1297 Peers_check_peer_valid (const struct GNUNET_PeerIdentity *peer)
1299 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer);
1304 * @brief Indicate that we want to send to the other peer
1306 * This establishes a sending channel
1308 * @param peer the peer to establish channel to
1311 Peers_indicate_sending_intention (const struct GNUNET_PeerIdentity *peer)
1313 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1314 (void) get_channel (peer);
1319 * @brief Check whether other peer has the intention to send/opened channel
1322 * @param peer the peer in question
1324 * @return #GNUNET_YES if peer has the intention to send
1325 * #GNUNET_NO otherwise
1328 Peers_check_peer_send_intention (const struct GNUNET_PeerIdentity *peer)
1330 const struct PeerContext *peer_ctx;
1332 peer_ctx = get_peer_ctx (peer);
1333 if (NULL != peer_ctx->recv_channel)
1342 * Handle the channel a peer opens to us.
1344 * @param cls The closure
1345 * @param channel The channel the peer wants to establish
1346 * @param initiator The peer's peer ID
1347 * @param port The port the channel is being established over
1348 * @param options Further options
1350 * @return initial channel context for the channel
1351 * (can be NULL -- that's not an error)
1354 Peers_handle_inbound_channel (void *cls,
1355 struct GNUNET_CADET_Channel *channel,
1356 const struct GNUNET_PeerIdentity *initiator,
1357 const struct GNUNET_HashCode *port,
1358 enum GNUNET_CADET_ChannelOption options)
1360 struct PeerContext *peer_ctx;
1362 LOG (GNUNET_ERROR_TYPE_DEBUG,
1363 "New channel was established to us (Peer %s).\n",
1364 GNUNET_i2s (initiator));
1365 GNUNET_assert (NULL != channel); /* according to cadet API */
1366 /* Make sure we 'know' about this peer */
1367 peer_ctx = create_or_get_peer_ctx (initiator);
1368 set_peer_live (peer_ctx);
1369 /* We only accept one incoming channel per peer */
1370 if (GNUNET_YES == Peers_check_peer_send_intention (initiator))
1372 set_channel_flag (peer_ctx->recv_channel_flags,
1373 Peers_CHANNEL_ESTABLISHED_TWICE);
1374 GNUNET_CADET_channel_destroy (channel);
1375 /* return the channel context */
1376 return peer_ctx->recv_channel_flags;
1378 peer_ctx->recv_channel = channel;
1379 return peer_ctx->recv_channel_flags;
1384 * @brief Check whether a sending channel towards the given peer exists
1386 * @param peer the peer to check for
1388 * @return #GNUNET_YES if a sending channel towards that peer exists
1389 * #GNUNET_NO otherwise
1392 Peers_check_sending_channel_exists (const struct GNUNET_PeerIdentity *peer)
1394 struct PeerContext *peer_ctx;
1396 if (GNUNET_NO == Peers_check_peer_known (peer))
1397 { /* If no such peer exists, there is no channel */
1400 peer_ctx = get_peer_ctx (peer);
1401 if (NULL == peer_ctx->send_channel)
1410 * @brief check whether the given channel is the sending channel of the given
1413 * @param peer the peer in question
1414 * @param channel the channel to check for
1415 * @param role either #Peers_CHANNEL_ROLE_SENDING, or
1416 * #Peers_CHANNEL_ROLE_RECEIVING
1418 * @return #GNUNET_YES if the given chennel is the sending channel of the peer
1419 * #GNUNET_NO otherwise
1422 Peers_check_channel_role (const struct GNUNET_PeerIdentity *peer,
1423 const struct GNUNET_CADET_Channel *channel,
1424 enum Peers_ChannelRole role)
1426 const struct PeerContext *peer_ctx;
1428 if (GNUNET_NO == Peers_check_peer_known (peer))
1432 peer_ctx = get_peer_ctx (peer);
1433 if ( (Peers_CHANNEL_ROLE_SENDING == role) &&
1434 (channel == peer_ctx->send_channel) )
1438 if ( (Peers_CHANNEL_ROLE_RECEIVING == role) &&
1439 (channel == peer_ctx->recv_channel) )
1448 * @brief Destroy the send channel of a peer e.g. stop indicating a sending
1449 * intention to another peer
1451 * If there is also no channel to receive messages from that peer, remove it
1455 * @peer the peer identity of the peer whose sending channel to destroy
1456 * @return #GNUNET_YES if channel was destroyed
1457 * #GNUNET_NO otherwise
1460 Peers_destroy_sending_channel (const struct GNUNET_PeerIdentity *peer)
1462 struct PeerContext *peer_ctx;
1464 if (GNUNET_NO == Peers_check_peer_known (peer))
1468 peer_ctx = get_peer_ctx (peer);
1469 if (NULL != peer_ctx->send_channel)
1471 set_channel_flag (peer_ctx->send_channel_flags, Peers_CHANNEL_CLEAN);
1472 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1473 peer_ctx->send_channel = NULL;
1474 (void) Peers_check_connected (peer);
1481 * This is called when a channel is destroyed.
1483 * @param cls The closure
1484 * @param channel The channel being closed
1485 * @param channel_ctx The context associated with this channel
1488 Peers_cleanup_destroyed_channel (void *cls,
1489 const struct GNUNET_CADET_Channel *channel,
1492 struct GNUNET_PeerIdentity *peer;
1493 struct PeerContext *peer_ctx;
1495 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1496 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1497 // FIXME wait for cadet to change this function
1499 if (GNUNET_NO == Peers_check_peer_known (peer))
1500 {/* We don't want to implicitly create a context that we're about to kill */
1501 LOG (GNUNET_ERROR_TYPE_DEBUG,
1502 "channel (%s) without associated context was destroyed\n",
1506 peer_ctx = get_peer_ctx (peer);
1508 /* If our peer issued the destruction of the channel, the #Peers_TO_DESTROY
1509 * flag will be set. In this case simply make sure that the channels are
1511 /* FIXME This distinction seems to be redundant */
1512 if (Peers_check_peer_flag (peer, Peers_TO_DESTROY))
1513 {/* We initiatad the destruction of this particular peer */
1514 if (channel == peer_ctx->send_channel)
1515 peer_ctx->send_channel = NULL;
1516 else if (channel == peer_ctx->recv_channel)
1517 peer_ctx->recv_channel = NULL;
1519 if (NULL != peer_ctx->send_channel)
1521 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1522 peer_ctx->send_channel = NULL;
1524 if (NULL != peer_ctx->recv_channel)
1526 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1527 peer_ctx->recv_channel = NULL;
1529 /* Set the #Peers_ONLINE flag accordingly */
1530 (void) Peers_check_connected (peer);
1535 { /* We did not initiate the destruction of this peer */
1536 if (channel == peer_ctx->send_channel)
1537 { /* Something (but us) killd the channel - clean up peer */
1538 LOG (GNUNET_ERROR_TYPE_DEBUG,
1539 "send channel (%s) was destroyed - cleaning up\n",
1541 peer_ctx->send_channel = NULL;
1543 else if (channel == peer_ctx->recv_channel)
1544 { /* Other peer doesn't want to send us messages anymore */
1545 LOG (GNUNET_ERROR_TYPE_DEBUG,
1546 "Peer %s destroyed recv channel - cleaning up channel\n",
1548 peer_ctx->recv_channel = NULL;
1552 LOG (GNUNET_ERROR_TYPE_WARNING,
1553 "unknown channel (%s) was destroyed\n",
1557 (void) Peers_check_connected (peer);
1561 * @brief Send a message to another peer.
1563 * Keeps track about pending messages so they can be properly removed when the
1564 * peer is destroyed.
1566 * @param peer receeiver of the message
1567 * @param ev envelope of the message
1568 * @param type type of the message
1571 Peers_send_message (const struct GNUNET_PeerIdentity *peer,
1572 struct GNUNET_MQ_Envelope *ev,
1575 struct PendingMessage *pending_msg;
1576 struct GNUNET_MQ_Handle *mq;
1578 pending_msg = insert_pending_message (peer, ev, type);
1580 GNUNET_MQ_notify_sent (ev,
1583 GNUNET_MQ_send (mq, ev);
1587 * @brief Schedule a operation on given peer
1589 * Avoids scheduling an operation twice.
1591 * @param peer the peer we want to schedule the operation for once it gets live
1593 * @return #GNUNET_YES if the operation was scheduled
1594 * #GNUNET_NO otherwise
1597 Peers_schedule_operation (const struct GNUNET_PeerIdentity *peer,
1598 const PeerOp peer_op)
1600 struct PeerPendingOp pending_op;
1601 struct PeerContext *peer_ctx;
1603 if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, own_identity))
1607 GNUNET_assert (GNUNET_YES == Peers_check_peer_known (peer));
1609 //TODO if LIVE/ONLINE execute immediately
1611 if (GNUNET_NO == check_operation_scheduled (peer, peer_op))
1613 peer_ctx = get_peer_ctx (peer);
1614 pending_op.op = peer_op;
1615 pending_op.op_cls = NULL;
1616 GNUNET_array_append (peer_ctx->pending_ops,
1617 peer_ctx->num_pending_ops,
1624 /* end of gnunet-service-rps_peers.c */