2 This file is part of GNUnet.
3 Copyright (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs_cp.c
23 * @brief API to handle 'connected peers'
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_load_lib.h"
29 #include "gnunet-service-fs.h"
30 #include "gnunet-service-fs_cp.h"
31 #include "gnunet-service-fs_pe.h"
32 #include "gnunet-service-fs_pr.h"
33 #include "gnunet-service-fs_push.h"
34 #include "gnunet_peerstore_service.h"
38 * Ratio for moving average delay calculation. The previous
39 * average goes in with a factor of (n-1) into the calculation.
42 #define RUNAVG_DELAY_N 16
45 * How often do we flush respect values to disk?
47 #define RESPECT_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
50 * After how long do we discard a reply?
52 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
55 * Collect an instane number of statistics? May cause excessive IPC.
57 #define INSANE_STATISTICS GNUNET_NO
61 * Handle to cancel a transmission request.
63 struct GSF_PeerTransmitHandle
67 * Kept in a doubly-linked list.
69 struct GSF_PeerTransmitHandle *next;
72 * Kept in a doubly-linked list.
74 struct GSF_PeerTransmitHandle *prev;
77 * Time when this transmission request was issued.
79 struct GNUNET_TIME_Absolute transmission_request_start_time;
82 * Timeout for this request.
84 struct GNUNET_TIME_Absolute timeout;
87 * Task called on timeout, or 0 for none.
89 struct GNUNET_SCHEDULER_Task * timeout_task;
92 * Function to call to get the actual message.
94 GSF_GetMessageCallback gmc;
97 * Peer this request targets.
99 struct GSF_ConnectedPeer *cp;
102 * Closure for @e gmc.
107 * Size of the message to be transmitted.
112 * #GNUNET_YES if this is a query, #GNUNET_NO for content.
117 * Did we get a reservation already?
122 * Priority of this request.
130 * Handle for an entry in our delay list.
132 struct GSF_DelayedHandle
136 * Kept in a doubly-linked list.
138 struct GSF_DelayedHandle *next;
141 * Kept in a doubly-linked list.
143 struct GSF_DelayedHandle *prev;
146 * Peer this transmission belongs to.
148 struct GSF_ConnectedPeer *cp;
151 * The PUT that was delayed.
153 struct PutMessage *pm;
156 * Task for the delay.
158 struct GNUNET_SCHEDULER_Task * delay_task;
161 * Size of the message.
169 * Information per peer and request.
175 * Handle to generic request.
177 struct GSF_PendingRequest *pr;
180 * Handle to specific peer.
182 struct GSF_ConnectedPeer *cp;
185 * Task for asynchronous stopping of this request.
187 struct GNUNET_SCHEDULER_Task * kill_task;
195 struct GSF_ConnectedPeer
199 * Performance data for this peer.
201 struct GSF_PeerPerformanceData ppd;
204 * Time until when we blocked this peer from migrating
207 struct GNUNET_TIME_Absolute last_migration_block;
210 * Task scheduled to revive migration to this peer.
212 struct GNUNET_SCHEDULER_Task * mig_revive_task;
215 * Messages (replies, queries, content migration) we would like to
216 * send to this peer in the near future. Sorted by priority, head.
218 struct GSF_PeerTransmitHandle *pth_head;
221 * Messages (replies, queries, content migration) we would like to
222 * send to this peer in the near future. Sorted by priority, tail.
224 struct GSF_PeerTransmitHandle *pth_tail;
227 * Messages (replies, queries, content migration) we would like to
228 * send to this peer in the near future. Sorted by priority, head.
230 struct GSF_DelayedHandle *delayed_head;
233 * Messages (replies, queries, content migration) we would like to
234 * send to this peer in the near future. Sorted by priority, tail.
236 struct GSF_DelayedHandle *delayed_tail;
239 * Migration stop message in our queue, or NULL if we have none pending.
241 struct GSF_PeerTransmitHandle *migration_pth;
244 * Context of our GNUNET_ATS_reserve_bandwidth call (or NULL).
246 struct GNUNET_ATS_ReservationContext *rc;
249 * Task scheduled if we need to retry bandwidth reservation later.
251 struct GNUNET_SCHEDULER_Task * rc_delay_task;
254 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
256 struct GNUNET_CONTAINER_MultiHashMap *request_map;
259 * Handle for an active request for transmission to this
260 * peer, or NULL (if core queue was full).
262 struct GNUNET_CORE_TransmitHandle *cth;
265 * Increase in traffic preference still to be submitted
266 * to the core service for this peer.
268 uint64_t inc_preference;
271 * Set to 1 if we're currently in the process of calling
272 * #GNUNET_CORE_notify_transmit_ready() (so while @e cth is
273 * NULL, we should not call notify_transmit_ready for this
276 unsigned int cth_in_progress;
279 * Respect rating for this peer on disk.
281 uint32_t disk_respect;
284 * Which offset in "last_p2p_replies" will be updated next?
285 * (we go round-robin).
287 unsigned int last_p2p_replies_woff;
290 * Which offset in "last_client_replies" will be updated next?
291 * (we go round-robin).
293 unsigned int last_client_replies_woff;
296 * Current offset into 'last_request_times' ring buffer.
298 unsigned int last_request_times_off;
301 * GNUNET_YES if we did successfully reserve 32k bandwidth,
307 * Function called when the creation of this record is complete.
309 GSF_ConnectedPeerCreationCallback creation_cb;
312 * Closure for @e creation_cb
314 void *creation_cb_cls;
317 * Handle to the PEERSTORE iterate request for peer respect value
319 struct GNUNET_PEERSTORE_IterateContext *respect_iterate_req;
325 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
327 static struct GNUNET_CONTAINER_MultiPeerMap *cp_map;
330 * Handle to peerstore service.
332 static struct GNUNET_PEERSTORE_Handle *peerstore;
336 * Update the latency information kept for the given peer.
338 * @param id peer record to update
339 * @param latency current latency value
342 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
343 struct GNUNET_TIME_Relative latency)
345 struct GSF_ConnectedPeer *cp;
347 cp = GSF_peer_get_ (id);
349 return; /* we're not yet connected at the core level, ignore */
350 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
355 * Return the performance data record for the given peer
357 * @param cp peer to query
358 * @return performance data record for the peer
360 struct GSF_PeerPerformanceData *
361 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
368 * Core is ready to transmit to a peer, get the message.
370 * @param cls the `struct GSF_PeerTransmitHandle` of the message
371 * @param size number of bytes core is willing to take
372 * @param buf where to copy the message
373 * @return number of bytes copied to @a buf
376 peer_transmit_ready_cb (void *cls,
382 * Function called by core upon success or failure of our bandwidth reservation request.
384 * @param cls the `struct GSF_ConnectedPeer` of the peer for which we made the request
385 * @param peer identifies the peer
386 * @param amount set to the amount that was actually reserved or unreserved;
387 * either the full requested amount or zero (no partial reservations)
388 * @param res_delay if the reservation could not be satisfied (amount was 0), how
389 * long should the client wait until re-trying?
392 ats_reserve_callback (void *cls,
393 const struct GNUNET_PeerIdentity *peer,
395 struct GNUNET_TIME_Relative res_delay);
399 * If ready (bandwidth reserved), try to schedule transmission via
400 * core for the given handle.
402 * @param pth transmission handle to schedule
405 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
407 struct GSF_ConnectedPeer *cp;
408 struct GNUNET_PeerIdentity target;
411 if ((NULL != cp->cth) || (0 != cp->cth_in_progress))
412 return; /* already done */
413 GNUNET_assert (0 != cp->ppd.pid);
414 GNUNET_PEER_resolve (cp->ppd.pid, &target);
416 if (0 != cp->inc_preference)
418 GNUNET_ATS_performance_change_preference (GSF_ats,
420 GNUNET_ATS_PREFERENCE_BANDWIDTH,
421 (double) cp->inc_preference,
422 GNUNET_ATS_PREFERENCE_END);
423 cp->inc_preference = 0;
426 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
428 /* query, need reservation */
429 if (GNUNET_YES != cp->did_reserve)
430 return; /* not ready */
431 cp->did_reserve = GNUNET_NO;
432 /* reservation already done! */
433 pth->was_reserved = GNUNET_YES;
435 GNUNET_ATS_reserve_bandwidth (GSF_ats, &target, DBLOCK_SIZE,
436 &ats_reserve_callback, cp);
439 GNUNET_assert (NULL == cp->cth);
440 cp->cth_in_progress++;
442 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES,
443 GNUNET_CORE_PRIO_BACKGROUND,
444 GNUNET_TIME_absolute_get_remaining
445 (pth->timeout), &target, pth->size,
446 &peer_transmit_ready_cb, cp);
447 GNUNET_assert (NULL != cp->cth);
448 GNUNET_assert (0 < cp->cth_in_progress--);
453 * Core is ready to transmit to a peer, get the message.
455 * @param cls the `struct GSF_PeerTransmitHandle` of the message
456 * @param size number of bytes core is willing to take
457 * @param buf where to copy the message
458 * @return number of bytes copied to @a buf
461 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
463 struct GSF_ConnectedPeer *cp = cls;
464 struct GSF_PeerTransmitHandle *pth = cp->pth_head;
465 struct GSF_PeerTransmitHandle *pos;
471 if (pth->size > size)
473 schedule_transmission (pth);
476 if (NULL != pth->timeout_task)
478 GNUNET_SCHEDULER_cancel (pth->timeout_task);
479 pth->timeout_task = NULL;
481 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
482 if (GNUNET_YES == pth->is_query)
484 cp->ppd.last_request_times[(cp->last_request_times_off++) %
485 MAX_QUEUE_PER_PEER] =
486 GNUNET_TIME_absolute_get ();
487 GNUNET_assert (0 < cp->ppd.pending_queries--);
489 else if (GNUNET_NO == pth->is_query)
491 GNUNET_assert (0 < cp->ppd.pending_replies--);
493 GNUNET_LOAD_update (cp->ppd.transmission_delay,
494 GNUNET_TIME_absolute_get_duration
495 (pth->transmission_request_start_time).rel_value_us);
496 ret = pth->gmc (pth->gmc_cls, size, buf);
497 if (NULL != (pos = cp->pth_head))
499 GNUNET_assert (pos != pth);
500 schedule_transmission (pos);
508 * (re)try to reserve bandwidth from the given peer.
510 * @param cls the `struct GSF_ConnectedPeer` to reserve from
511 * @param tc scheduler context
514 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
516 struct GSF_ConnectedPeer *cp = cls;
517 struct GNUNET_PeerIdentity target;
519 GNUNET_PEER_resolve (cp->ppd.pid, &target);
520 cp->rc_delay_task = NULL;
522 GNUNET_ATS_reserve_bandwidth (GSF_ats, &target, DBLOCK_SIZE,
523 &ats_reserve_callback, cp);
528 * Function called by core upon success or failure of our bandwidth reservation request.
530 * @param cls the `struct GSF_ConnectedPeer` of the peer for which we made the request
531 * @param peer identifies the peer
532 * @param amount set to the amount that was actually reserved or unreserved;
533 * either the full requested amount or zero (no partial reservations)
534 * @param res_delay if the reservation could not be satisfied (amount was 0), how
535 * long should the client wait until re-trying?
538 ats_reserve_callback (void *cls,
539 const struct GNUNET_PeerIdentity *peer,
541 struct GNUNET_TIME_Relative res_delay)
543 struct GSF_ConnectedPeer *cp = cls;
544 struct GSF_PeerTransmitHandle *pth;
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Reserved %d bytes / need to wait %s for reservation\n",
549 GNUNET_STRINGS_relative_time_to_string (res_delay, GNUNET_YES));
554 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
557 cp->did_reserve = GNUNET_YES;
559 if ((NULL != pth) && (NULL == cp->cth) && (0 == cp->cth_in_progress))
561 /* reservation success, try transmission now! */
562 cp->cth_in_progress++;
564 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES,
565 GNUNET_CORE_PRIO_BACKGROUND,
566 GNUNET_TIME_absolute_get_remaining
567 (pth->timeout), peer, pth->size,
568 &peer_transmit_ready_cb, cp);
569 GNUNET_assert (NULL != cp->cth);
570 GNUNET_assert (0 < cp->cth_in_progress--);
576 * Function called by PEERSTORE with peer respect record
578 * @param cls handle to connected peer entry
579 * @param record peerstore record information
580 * @param emsg error message, or NULL if no errors
581 * @return #GNUNET_NO to stop iterating since we only expect 0 or 1 records
584 peer_respect_cb (void *cls,
585 const struct GNUNET_PEERSTORE_Record *record,
588 struct GSF_ConnectedPeer *cp = cls;
590 cp->respect_iterate_req = NULL;
591 if ((NULL != record) && (sizeof (cp->disk_respect) == record->value_size))
592 cp->disk_respect = cp->ppd.respect = *((uint32_t *)record->value);
593 GSF_push_start_ (cp);
594 if (NULL != cp->creation_cb)
595 cp->creation_cb (cp->creation_cb_cls, cp);
601 * A peer connected to us. Setup the connected peer
604 * @param peer identity of peer that connected
605 * @param creation_cb callback function when the record is created.
606 * @param creation_cb_cls closure for @creation_cb
609 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
610 GSF_ConnectedPeerCreationCallback creation_cb,
611 void *creation_cb_cls)
613 struct GSF_ConnectedPeer *cp;
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "Connected to peer %s\n",
618 cp = GNUNET_new (struct GSF_ConnectedPeer);
619 cp->ppd.pid = GNUNET_PEER_intern (peer);
620 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
622 GNUNET_ATS_reserve_bandwidth (GSF_ats, peer, DBLOCK_SIZE,
623 &ats_reserve_callback, cp);
624 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
625 GNUNET_break (GNUNET_OK ==
626 GNUNET_CONTAINER_multipeermap_put (cp_map,
627 GSF_connected_peer_get_identity2_ (cp),
629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
630 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
631 GNUNET_CONTAINER_multipeermap_size (cp_map),
633 cp->creation_cb = creation_cb;
634 cp->creation_cb_cls = creation_cb_cls;
635 cp->respect_iterate_req =
636 GNUNET_PEERSTORE_iterate (peerstore, "fs", peer, "respect",
637 GNUNET_TIME_UNIT_FOREVER_REL, &peer_respect_cb,
643 * It may be time to re-start migrating content to this
644 * peer. Check, and if so, restart migration.
646 * @param cls the `struct GSF_ConnectedPeer`
647 * @param tc scheduler context
650 revive_migration (void *cls,
651 const struct GNUNET_SCHEDULER_TaskContext *tc)
653 struct GSF_ConnectedPeer *cp = cls;
654 struct GNUNET_TIME_Relative bt;
656 cp->mig_revive_task = NULL;
657 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
658 if (0 != bt.rel_value_us)
660 /* still time left... */
661 cp->mig_revive_task =
662 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
665 GSF_push_start_ (cp);
670 * Get a handle for a connected peer.
672 * @param peer peer's identity
673 * @return NULL if the peer is not currently connected
675 struct GSF_ConnectedPeer *
676 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
680 return GNUNET_CONTAINER_multipeermap_get (cp_map, peer);
685 * Handle P2P "MIGRATION_STOP" message.
687 * @param cls closure, always NULL
688 * @param other the other peer involved (sender or receiver, NULL
689 * for loopback messages where we are both sender and receiver)
690 * @param message the actual message
691 * @return #GNUNET_OK to keep the connection open,
692 * #GNUNET_SYSERR to close it (signal serious error)
695 GSF_handle_p2p_migration_stop_ (void *cls,
696 const struct GNUNET_PeerIdentity *other,
697 const struct GNUNET_MessageHeader *message)
699 struct GSF_ConnectedPeer *cp;
700 const struct MigrationStopMessage *msm;
701 struct GNUNET_TIME_Relative bt;
703 msm = (const struct MigrationStopMessage *) message;
704 cp = GSF_peer_get_ (other);
710 GNUNET_STATISTICS_update (GSF_stats,
711 gettext_noop ("# migration stop messages received"),
713 bt = GNUNET_TIME_relative_ntoh (msm->duration);
714 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
715 _("Migration of content to peer `%s' blocked for %s\n"),
717 GNUNET_STRINGS_relative_time_to_string (bt, GNUNET_YES));
718 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
719 if (NULL == cp->mig_revive_task)
722 cp->mig_revive_task =
723 GNUNET_SCHEDULER_add_delayed (bt,
724 &revive_migration, cp);
731 * Copy reply and free put message.
733 * @param cls the `struct PutMessage`
734 * @param buf_size number of bytes available in @a buf
735 * @param buf where to copy the message, NULL on error (peer disconnect)
736 * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
739 copy_reply (void *cls, size_t buf_size, void *buf)
741 struct PutMessage *pm = cls;
746 GNUNET_assert (buf_size >= ntohs (pm->header.size));
747 size = ntohs (pm->header.size);
748 memcpy (buf, pm, size);
749 GNUNET_STATISTICS_update (GSF_stats,
751 ("# replies transmitted to other peers"), 1,
757 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
766 * Free resources associated with the given peer request.
768 * @param peerreq request to free
769 * @param query associated key for the request
772 free_pending_request (struct PeerRequest *peerreq,
773 const struct GNUNET_HashCode *query)
775 struct GSF_ConnectedPeer *cp = peerreq->cp;
777 if (NULL != peerreq->kill_task)
779 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
780 peerreq->kill_task = NULL;
782 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
784 GNUNET_break (GNUNET_YES ==
785 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
787 GNUNET_free (peerreq);
792 * Cancel all requests associated with the peer.
795 * @param query hash code of the request
796 * @param value the `struct GSF_PendingRequest`
797 * @return #GNUNET_YES (continue to iterate)
800 cancel_pending_request (void *cls,
801 const struct GNUNET_HashCode *query,
804 struct PeerRequest *peerreq = value;
805 struct GSF_PendingRequest *pr = peerreq->pr;
806 struct GSF_PendingRequestData *prd;
808 prd = GSF_pending_request_get_data_ (pr);
809 GSF_pending_request_cancel_ (pr, GNUNET_NO);
810 free_pending_request (peerreq, &prd->query);
816 * Free the given request.
818 * @param cls the request to free
819 * @param tc task context
822 peer_request_destroy (void *cls,
823 const struct GNUNET_SCHEDULER_TaskContext *tc)
825 struct PeerRequest *peerreq = cls;
826 struct GSF_PendingRequest *pr = peerreq->pr;
827 struct GSF_PendingRequestData *prd;
829 peerreq->kill_task = NULL;
830 prd = GSF_pending_request_get_data_ (pr);
831 cancel_pending_request (NULL, &prd->query, peerreq);
836 * The artificial delay is over, transmit the message now.
838 * @param cls the `struct GSF_DelayedHandle` with the message
839 * @param tc scheduler context
842 transmit_delayed_now (void *cls,
843 const struct GNUNET_SCHEDULER_TaskContext *tc)
845 struct GSF_DelayedHandle *dh = cls;
846 struct GSF_ConnectedPeer *cp = dh->cp;
848 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
849 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
851 GNUNET_free (dh->pm);
855 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
856 dh->msize, ©_reply, dh->pm);
862 * Get the randomized delay a response should be subjected to.
864 * @return desired delay
866 static struct GNUNET_TIME_Relative
867 get_randomized_delay ()
869 struct GNUNET_TIME_Relative ret;
872 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
873 GNUNET_CRYPTO_random_u32
874 (GNUNET_CRYPTO_QUALITY_WEAK,
875 2 * GSF_avg_latency.rel_value_us + 1));
876 #if INSANE_STATISTICS
877 GNUNET_STATISTICS_update (GSF_stats,
879 ("# artificial delays introduced (ms)"),
880 ret.rel_value_us / 1000LL, GNUNET_NO);
887 * Handle a reply to a pending request. Also called if a request
888 * expires (then with data == NULL). The handler may be called
889 * many times (depending on the request type), but will not be
890 * called during or after a call to GSF_pending_request_cancel
891 * and will also not be called anymore after a call signalling
894 * @param cls `struct PeerRequest` this is an answer for
895 * @param eval evaluation of the result
896 * @param pr handle to the original pending request
897 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
898 * @param expiration when does @a data expire?
899 * @param last_transmission when did we last transmit a request for this block
900 * @param type type of the block
901 * @param data response data, NULL on request expiration
902 * @param data_len number of bytes in @a data
905 handle_p2p_reply (void *cls,
906 enum GNUNET_BLOCK_EvaluationResult eval,
907 struct GSF_PendingRequest *pr,
908 uint32_t reply_anonymity_level,
909 struct GNUNET_TIME_Absolute expiration,
910 struct GNUNET_TIME_Absolute last_transmission,
911 enum GNUNET_BLOCK_Type type,
915 struct PeerRequest *peerreq = cls;
916 struct GSF_ConnectedPeer *cp = peerreq->cp;
917 struct GSF_PendingRequestData *prd;
918 struct PutMessage *pm;
921 GNUNET_assert (data_len + sizeof (struct PutMessage) <
922 GNUNET_SERVER_MAX_MESSAGE_SIZE);
923 GNUNET_assert (peerreq->pr == pr);
924 prd = GSF_pending_request_get_data_ (pr);
927 free_pending_request (peerreq, &prd->query);
930 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
931 if ((prd->type != type) && (GNUNET_BLOCK_TYPE_ANY != prd->type))
933 GNUNET_STATISTICS_update (GSF_stats,
935 ("# replies dropped due to type mismatch"),
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "Transmitting result for query `%s' to peer\n",
941 GNUNET_h2s (&prd->query));
942 GNUNET_STATISTICS_update (GSF_stats,
943 gettext_noop ("# replies received for other peers"),
945 msize = sizeof (struct PutMessage) + data_len;
946 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
951 if ((UINT32_MAX != reply_anonymity_level) && (reply_anonymity_level > 1))
953 if (reply_anonymity_level - 1 > GSF_cover_content_count)
955 GNUNET_STATISTICS_update (GSF_stats,
957 ("# replies dropped due to insufficient cover traffic"),
961 GSF_cover_content_count -= (reply_anonymity_level - 1);
964 pm = GNUNET_malloc (msize);
965 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
966 pm->header.size = htons (msize);
967 pm->type = htonl (type);
968 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
969 memcpy (&pm[1], data, data_len);
970 if ((UINT32_MAX != reply_anonymity_level) && (0 != reply_anonymity_level) &&
971 (GNUNET_YES == GSF_enable_randomized_delays))
973 struct GSF_DelayedHandle *dh;
975 dh = GNUNET_new (struct GSF_DelayedHandle);
979 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
981 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
982 &transmit_delayed_now, dh);
986 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
989 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
991 if (NULL == peerreq->kill_task)
993 GNUNET_STATISTICS_update (GSF_stats,
995 ("# P2P searches destroyed due to ultimate reply"),
998 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
1004 * Increase the peer's respect by a value.
1006 * @param cp which peer to change the respect value on
1007 * @param value is the int value by which the
1008 * peer's credit is to be increased or decreased
1009 * @returns the actual change in respect (positive or negative)
1012 change_peer_respect (struct GSF_ConnectedPeer *cp, int value)
1016 GNUNET_assert (NULL != cp);
1019 if (cp->ppd.respect + value < cp->ppd.respect)
1021 value = UINT32_MAX - cp->ppd.respect;
1022 cp->ppd.respect = UINT32_MAX;
1025 cp->ppd.respect += value;
1029 if (cp->ppd.respect < -value)
1031 value = -cp->ppd.respect;
1032 cp->ppd.respect = 0;
1035 cp->ppd.respect += value;
1042 * We've received a request with the specified priority. Bound it
1043 * according to how much we respect the given peer.
1045 * @param prio_in requested priority
1046 * @param cp the peer making the request
1047 * @return effective priority
1050 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1052 #define N ((double)128.0)
1057 ld = GSF_test_get_load_too_high_ (0);
1058 if (GNUNET_SYSERR == ld)
1060 #if INSANE_STATISTICS
1061 GNUNET_STATISTICS_update (GSF_stats,
1063 ("# requests done for free (low load)"), 1,
1066 return 0; /* excess resources */
1068 if (prio_in > INT32_MAX)
1069 prio_in = INT32_MAX;
1070 ret = -change_peer_respect (cp, -(int) prio_in);
1073 if (ret > GSF_current_priorities + N)
1074 rret = GSF_current_priorities + N;
1077 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1079 if ((GNUNET_YES == ld) && (ret > 0))
1081 /* try with charging */
1082 ld = GSF_test_get_load_too_high_ (ret);
1084 if (GNUNET_YES == ld)
1086 GNUNET_STATISTICS_update (GSF_stats,
1088 ("# request dropped, priority insufficient"), 1,
1091 change_peer_respect (cp, (int) ret);
1092 return -1; /* not enough resources */
1096 GNUNET_STATISTICS_update (GSF_stats,
1098 ("# requests done for a price (normal load)"), 1,
1107 * The priority level imposes a bound on the maximum
1108 * value for the ttl that can be requested.
1110 * @param ttl_in requested ttl
1111 * @param prio given priority
1112 * @return ttl_in if ttl_in is below the limit,
1113 * otherwise the ttl-limit for the given priority
1116 bound_ttl (int32_t ttl_in, uint32_t prio)
1118 unsigned long long allowed;
1122 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1123 if (ttl_in > allowed)
1125 if (allowed >= (1 << 30))
1134 * Handle P2P "QUERY" message. Creates the pending request entry
1135 * and sets up all of the data structures to that we will
1136 * process replies properly. Does not initiate forwarding or
1137 * local database lookups.
1139 * @param other the other peer involved (sender or receiver, NULL
1140 * for loopback messages where we are both sender and receiver)
1141 * @param message the actual message
1142 * @return pending request handle, NULL on error
1144 struct GSF_PendingRequest *
1145 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1146 const struct GNUNET_MessageHeader *message)
1148 struct PeerRequest *peerreq;
1149 struct GSF_PendingRequest *pr;
1150 struct GSF_PendingRequestData *prd;
1151 struct GSF_ConnectedPeer *cp;
1152 struct GSF_ConnectedPeer *cps;
1153 const struct GNUNET_PeerIdentity *target;
1154 enum GSF_PendingRequestOptions options;
1156 const struct GetMessage *gm;
1158 const struct GNUNET_PeerIdentity *opt;
1161 uint32_t ttl_decrement;
1164 enum GNUNET_BLOCK_Type type;
1165 GNUNET_PEER_Id spid;
1167 GNUNET_assert (other != NULL);
1168 msize = ntohs (message->size);
1169 if (msize < sizeof (struct GetMessage))
1171 GNUNET_break_op (0);
1174 GNUNET_STATISTICS_update (GSF_stats,
1176 ("# GET requests received (from other peers)"), 1,
1178 gm = (const struct GetMessage *) message;
1179 type = ntohl (gm->type);
1180 bm = ntohl (gm->hash_bitmap);
1188 if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_PeerIdentity))
1190 GNUNET_break_op (0);
1193 opt = (const struct GNUNET_PeerIdentity *) &gm[1];
1194 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_PeerIdentity);
1195 /* bfsize must be power of 2, check! */
1196 if (0 != ((bfsize - 1) & bfsize))
1198 GNUNET_break_op (0);
1201 GSF_cover_query_count++;
1202 bm = ntohl (gm->hash_bitmap);
1204 cps = GSF_peer_get_ (other);
1207 /* peer must have just disconnected */
1208 GNUNET_STATISTICS_update (GSF_stats,
1210 ("# requests dropped due to initiator not being connected"),
1214 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1215 cp = GSF_peer_get_ (&opt[bits++]);
1220 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1222 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1223 GNUNET_i2s (&opt[bits - 1]));
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1228 GNUNET_i2s (other));
1229 #if INSANE_STATISTICS
1230 GNUNET_STATISTICS_update (GSF_stats,
1232 ("# requests dropped due to missing reverse route"),
1237 /* note that we can really only check load here since otherwise
1238 * peers could find out that we are overloaded by not being
1239 * disconnected after sending us a malformed query... */
1240 priority = bound_priority (ntohl (gm->priority), cps);
1243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1244 "Dropping query from `%s', this peer is too busy.\n",
1245 GNUNET_i2s (other));
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1250 GNUNET_h2s (&gm->query),
1251 (unsigned int) type,
1256 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? (&opt[bits++]) : NULL;
1257 options = GSF_PRO_DEFAULTS;
1259 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1260 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1261 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value_us * 2 +
1262 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1264 /* don't have BW to send to peer, or would likely take longer than we have for it,
1265 * so at best indirect the query */
1267 options |= GSF_PRO_FORWARD_ONLY;
1268 spid = GNUNET_PEER_intern (other);
1269 GNUNET_assert (0 != spid);
1271 ttl = bound_ttl (ntohl (gm->ttl), priority);
1272 /* decrement ttl (always) */
1274 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1276 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1280 GNUNET_i2s (other), ttl, ttl_decrement);
1281 GNUNET_STATISTICS_update (GSF_stats,
1283 ("# requests dropped due TTL underflow"), 1,
1285 /* integer underflow => drop (should be very rare)! */
1288 ttl -= ttl_decrement;
1290 /* test if the request already exists */
1291 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1292 if (peerreq != NULL)
1295 prd = GSF_pending_request_get_data_ (pr);
1296 if (prd->type == type)
1298 if (prd->ttl.abs_value_us >= GNUNET_TIME_absolute_get ().abs_value_us + ttl * 1000LL)
1300 /* existing request has higher TTL, drop new one! */
1301 prd->priority += priority;
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Have existing request with higher TTL, dropping new request.\n",
1304 GNUNET_i2s (other));
1305 GNUNET_STATISTICS_update (GSF_stats,
1307 ("# requests dropped due to higher-TTL request"),
1311 /* existing request has lower TTL, drop old one! */
1312 priority += prd->priority;
1313 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1314 free_pending_request (peerreq, &gm->query);
1318 peerreq = GNUNET_new (struct PeerRequest);
1320 pr = GSF_pending_request_create_ (options, type, &gm->query,
1323 0) ? (const char *) &opt[bits] : NULL,
1324 bfsize, ntohl (gm->filter_mutator),
1326 (uint32_t) priority, ttl, spid, GNUNET_PEER_intern (other), NULL, 0, /* replies_seen */
1327 &handle_p2p_reply, peerreq);
1328 GNUNET_assert (NULL != pr);
1330 GNUNET_break (GNUNET_OK ==
1331 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1334 GNUNET_STATISTICS_update (GSF_stats,
1336 ("# P2P query messages received and processed"), 1,
1338 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1345 * Function called if there has been a timeout trying to satisfy
1346 * a transmission request.
1348 * @param cls the `struct GSF_PeerTransmitHandle` of the request
1349 * @param tc scheduler context
1352 peer_transmit_timeout (void *cls,
1353 const struct GNUNET_SCHEDULER_TaskContext *tc)
1355 struct GSF_PeerTransmitHandle *pth = cls;
1356 struct GSF_ConnectedPeer *cp;
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1359 "Timeout trying to transmit to other peer\n");
1360 pth->timeout_task = NULL;
1362 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1363 if (GNUNET_YES == pth->is_query)
1364 GNUNET_assert (0 < cp->ppd.pending_queries--);
1365 else if (GNUNET_NO == pth->is_query)
1366 GNUNET_assert (0 < cp->ppd.pending_replies--);
1367 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1368 if (NULL != cp->cth)
1370 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1373 pth->gmc (pth->gmc_cls, 0, NULL);
1374 GNUNET_assert (0 == cp->cth_in_progress);
1380 * Transmit a message to the given peer as soon as possible.
1381 * If the peer disconnects before the transmission can happen,
1382 * the callback is invoked with a `NULL` @a buffer.
1384 * @param cp target peer
1385 * @param is_query is this a query (#GNUNET_YES) or content (#GNUNET_NO) or neither (#GNUNET_SYSERR)
1386 * @param priority how important is this request?
1387 * @param timeout when does this request timeout (call gmc with error)
1388 * @param size number of bytes we would like to send to the peer
1389 * @param gmc function to call to get the message
1390 * @param gmc_cls closure for @a gmc
1391 * @return handle to cancel request
1393 struct GSF_PeerTransmitHandle *
1394 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
1397 struct GNUNET_TIME_Relative timeout,
1399 GSF_GetMessageCallback gmc, void *gmc_cls)
1401 struct GSF_PeerTransmitHandle *pth;
1402 struct GSF_PeerTransmitHandle *pos;
1403 struct GSF_PeerTransmitHandle *prev;
1405 pth = GNUNET_new (struct GSF_PeerTransmitHandle);
1406 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1407 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1409 pth->gmc_cls = gmc_cls;
1411 pth->is_query = is_query;
1412 pth->priority = priority;
1414 /* insertion sort (by priority, descending) */
1417 while ((NULL != pos) && (pos->priority > priority))
1422 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1423 if (GNUNET_YES == is_query)
1424 cp->ppd.pending_queries++;
1425 else if (GNUNET_NO == is_query)
1426 cp->ppd.pending_replies++;
1428 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1429 schedule_transmission (pth);
1435 * Cancel an earlier request for transmission.
1437 * @param pth request to cancel
1440 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1442 struct GSF_ConnectedPeer *cp;
1444 if (NULL != pth->timeout_task)
1446 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1447 pth->timeout_task = NULL;
1450 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1451 if (GNUNET_YES == pth->is_query)
1452 GNUNET_assert (0 < cp->ppd.pending_queries--);
1453 else if (GNUNET_NO == pth->is_query)
1454 GNUNET_assert (0 < cp->ppd.pending_replies--);
1460 * Report on receiving a reply; update the performance record of the given peer.
1462 * @param cp responding peer (will be updated)
1463 * @param request_time time at which the original query was transmitted
1464 * @param request_priority priority of the original request
1467 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1468 struct GNUNET_TIME_Absolute request_time,
1469 uint32_t request_priority)
1471 struct GNUNET_TIME_Relative delay;
1473 delay = GNUNET_TIME_absolute_get_duration (request_time);
1474 cp->ppd.avg_reply_delay.rel_value_us =
1475 (cp->ppd.avg_reply_delay.rel_value_us * (RUNAVG_DELAY_N - 1) +
1476 delay.rel_value_us) / RUNAVG_DELAY_N;
1477 cp->ppd.avg_priority =
1478 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1479 request_priority) / RUNAVG_DELAY_N;
1484 * Report on receiving a reply in response to an initiating client.
1485 * Remember that this peer is good for this client.
1487 * @param cp responding peer (will be updated)
1488 * @param initiator_client local client on responsible for query
1491 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1492 struct GSF_LocalClient *initiator_client)
1494 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1495 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1500 * Report on receiving a reply in response to an initiating peer.
1501 * Remember that this peer is good for this initiating peer.
1503 * @param cp responding peer (will be updated)
1504 * @param initiator_peer other peer responsible for query
1507 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1508 const struct GSF_ConnectedPeer *initiator_peer)
1512 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1513 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1514 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1515 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1516 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1521 * Write peer-respect information to a file - flush the buffer entry!
1524 * @param key peer identity
1525 * @param value the `struct GSF_ConnectedPeer` to flush
1526 * @return #GNUNET_OK to continue iteration
1529 flush_respect (void *cls,
1530 const struct GNUNET_PeerIdentity *key,
1533 struct GSF_ConnectedPeer *cp = value;
1534 struct GNUNET_PeerIdentity pid;
1536 if (cp->ppd.respect == cp->disk_respect)
1537 return GNUNET_OK; /* unchanged */
1538 GNUNET_assert (0 != cp->ppd.pid);
1539 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1540 GNUNET_PEERSTORE_store (peerstore, "fs", &pid, "respect", &cp->ppd.respect,
1541 sizeof (cp->ppd.respect),
1542 GNUNET_TIME_UNIT_FOREVER_ABS,
1543 GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL);
1549 * A peer disconnected from us. Tear down the connected peer
1553 * @param peer identity of peer that connected
1556 GSF_peer_disconnect_handler_ (void *cls,
1557 const struct GNUNET_PeerIdentity *peer)
1559 struct GSF_ConnectedPeer *cp;
1560 struct GSF_PeerTransmitHandle *pth;
1561 struct GSF_DelayedHandle *dh;
1563 cp = GSF_peer_get_ (peer);
1565 return; /* must have been disconnect from core with
1566 * 'peer' == my_id, ignore */
1567 flush_respect (NULL, peer, cp);
1568 GNUNET_assert (GNUNET_YES ==
1569 GNUNET_CONTAINER_multipeermap_remove (cp_map,
1572 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
1573 GNUNET_CONTAINER_multipeermap_size (cp_map),
1575 if (NULL != cp->respect_iterate_req)
1577 GNUNET_PEERSTORE_iterate_cancel (cp->respect_iterate_req);
1578 cp->respect_iterate_req = NULL;
1580 if (NULL != cp->migration_pth)
1582 GSF_peer_transmit_cancel_ (cp->migration_pth);
1583 cp->migration_pth = NULL;
1587 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1590 if (NULL != cp->rc_delay_task)
1592 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1593 cp->rc_delay_task = NULL;
1595 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1596 &cancel_pending_request, cp);
1597 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1598 cp->request_map = NULL;
1599 GSF_plan_notify_peer_disconnect_ (cp);
1600 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1601 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1602 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1603 GSF_push_stop_ (cp);
1604 if (NULL != cp->cth)
1606 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1609 GNUNET_assert (0 == cp->cth_in_progress);
1610 while (NULL != (pth = cp->pth_head))
1612 if (pth->timeout_task != NULL)
1614 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1615 pth->timeout_task = NULL;
1617 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1618 pth->gmc (pth->gmc_cls, 0, NULL);
1621 while (NULL != (dh = cp->delayed_head))
1623 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1624 GNUNET_SCHEDULER_cancel (dh->delay_task);
1625 GNUNET_free (dh->pm);
1628 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1629 if (NULL != cp->mig_revive_task)
1631 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1632 cp->mig_revive_task = NULL;
1639 * Closure for #call_iterator().
1641 struct IterationContext
1644 * Function to call on each entry.
1646 GSF_ConnectedPeerIterator it;
1649 * Closure for @e it.
1656 * Function that calls the callback for each peer.
1658 * @param cls the `struct IterationContext *`
1659 * @param key identity of the peer
1660 * @param value the `struct GSF_ConnectedPeer *`
1661 * @return #GNUNET_YES to continue iteration
1664 call_iterator (void *cls,
1665 const struct GNUNET_PeerIdentity *key,
1668 struct IterationContext *ic = cls;
1669 struct GSF_ConnectedPeer *cp = value;
1679 * Iterate over all connected peers.
1681 * @param it function to call for each peer
1682 * @param it_cls closure for @a it
1685 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1688 struct IterationContext ic;
1692 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
1699 * Obtain the identity of a connected peer.
1701 * @param cp peer to get identity of
1702 * @param id identity to set (written to)
1705 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1706 struct GNUNET_PeerIdentity *id)
1708 GNUNET_assert (0 != cp->ppd.pid);
1709 GNUNET_PEER_resolve (cp->ppd.pid, id);
1714 * Obtain the identity of a connected peer.
1716 * @param cp peer to get identity of
1717 * @return reference to peer identity, valid until peer disconnects (!)
1719 const struct GNUNET_PeerIdentity *
1720 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp)
1722 GNUNET_assert (0 != cp->ppd.pid);
1723 return GNUNET_PEER_resolve2 (cp->ppd.pid);
1728 * Assemble a migration stop message for transmission.
1730 * @param cls the `struct GSF_ConnectedPeer` to use
1731 * @param size number of bytes we're allowed to write to @a buf
1732 * @param buf where to copy the message
1733 * @return number of bytes copied to @a buf
1736 create_migration_stop_message (void *cls,
1740 struct GSF_ConnectedPeer *cp = cls;
1741 struct MigrationStopMessage msm;
1743 cp->migration_pth = NULL;
1746 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1747 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1748 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1749 msm.reserved = htonl (0);
1751 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1752 (cp->last_migration_block));
1753 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1754 GNUNET_STATISTICS_update (GSF_stats,
1755 gettext_noop ("# migration stop messages sent"),
1757 return sizeof (struct MigrationStopMessage);
1762 * Ask a peer to stop migrating data to us until the given point
1765 * @param cp peer to ask
1766 * @param block_time until when to block
1769 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1770 struct GNUNET_TIME_Absolute block_time)
1772 if (cp->last_migration_block.abs_value_us > block_time.abs_value_us)
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1775 "Migration already blocked for another %s\n",
1776 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining
1777 (cp->last_migration_block), GNUNET_YES));
1778 return; /* already blocked */
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %s\n",
1781 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (block_time),
1783 cp->last_migration_block = block_time;
1784 if (NULL != cp->migration_pth)
1785 GSF_peer_transmit_cancel_ (cp->migration_pth);
1787 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1788 GNUNET_TIME_UNIT_FOREVER_REL,
1789 sizeof (struct MigrationStopMessage),
1790 &create_migration_stop_message, cp);
1795 * Notify core about a preference we have for the given peer
1796 * (to allocate more resources towards it). The change will
1797 * be communicated the next time we reserve bandwidth with
1798 * core (not instantly).
1800 * @param cp peer to reserve bandwidth from
1801 * @param pref preference change
1804 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1807 cp->inc_preference += pref;
1812 * Call this method periodically to flush respect information to disk.
1814 * @param cls closure, not used
1815 * @param tc task context, not used
1818 cron_flush_respect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1823 GNUNET_CONTAINER_multipeermap_iterate (cp_map, &flush_respect, NULL);
1826 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1828 GNUNET_SCHEDULER_add_delayed_with_priority (RESPECT_FLUSH_FREQ,
1829 GNUNET_SCHEDULER_PRIORITY_HIGH,
1830 &cron_flush_respect, NULL);
1835 * Initialize peer management subsystem.
1838 GSF_connected_peer_init_ ()
1840 cp_map = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1841 peerstore = GNUNET_PEERSTORE_connect (GSF_cfg);
1842 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1843 &cron_flush_respect, NULL);
1848 * Iterator to free peer entries.
1850 * @param cls closure, unused
1851 * @param key current key code
1852 * @param value value in the hash map (peer entry)
1853 * @return #GNUNET_YES (we should continue to iterate)
1856 clean_peer (void *cls,
1857 const struct GNUNET_PeerIdentity *key,
1860 GSF_peer_disconnect_handler_ (NULL, key);
1866 * Shutdown peer management subsystem.
1869 GSF_connected_peer_done_ ()
1871 cron_flush_respect (NULL, NULL);
1872 GNUNET_CONTAINER_multipeermap_iterate (cp_map, &clean_peer, NULL);
1873 GNUNET_CONTAINER_multipeermap_destroy (cp_map);
1875 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
1880 * Iterator to remove references to LC entry.
1882 * @param cls the `struct GSF_LocalClient *` to look for
1883 * @param key current key code
1884 * @param value value in the hash map (peer entry)
1885 * @return #GNUNET_YES (we should continue to iterate)
1888 clean_local_client (void *cls,
1889 const struct GNUNET_PeerIdentity *key,
1892 const struct GSF_LocalClient *lc = cls;
1893 struct GSF_ConnectedPeer *cp = value;
1896 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1897 if (cp->ppd.last_client_replies[i] == lc)
1898 cp->ppd.last_client_replies[i] = NULL;
1904 * Notification that a local client disconnected. Clean up all of our
1905 * references to the given handle.
1907 * @param lc handle to the local client (henceforth invalid)
1910 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1913 return; /* already cleaned up */
1914 GNUNET_CONTAINER_multipeermap_iterate (cp_map, &clean_local_client,
1919 /* end of gnunet-service-fs_cp.c */