2 This file is part of GNUnet.
3 (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_load_lib.h"
28 #include "gnunet_ats_service.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"
37 * Ratio for moving average delay calculation. The previous
38 * average goes in with a factor of (n-1) into the calculation.
41 #define RUNAVG_DELAY_N 16
44 * How often do we flush respect values to disk?
46 #define RESPECT_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
49 * After how long do we discard a reply?
51 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
55 * Handle to cancel a transmission request.
57 struct GSF_PeerTransmitHandle
61 * Kept in a doubly-linked list.
63 struct GSF_PeerTransmitHandle *next;
66 * Kept in a doubly-linked list.
68 struct GSF_PeerTransmitHandle *prev;
71 * Time when this transmission request was issued.
73 struct GNUNET_TIME_Absolute transmission_request_start_time;
76 * Timeout for this request.
78 struct GNUNET_TIME_Absolute timeout;
81 * Task called on timeout, or 0 for none.
83 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
86 * Function to call to get the actual message.
88 GSF_GetMessageCallback gmc;
91 * Peer this request targets.
93 struct GSF_ConnectedPeer *cp;
101 * Size of the message to be transmitted.
106 * GNUNET_YES if this is a query, GNUNET_NO for content.
111 * Did we get a reservation already?
116 * Priority of this request.
124 * Handle for an entry in our delay list.
126 struct GSF_DelayedHandle
130 * Kept in a doubly-linked list.
132 struct GSF_DelayedHandle *next;
135 * Kept in a doubly-linked list.
137 struct GSF_DelayedHandle *prev;
140 * Peer this transmission belongs to.
142 struct GSF_ConnectedPeer *cp;
145 * The PUT that was delayed.
147 struct PutMessage *pm;
150 * Task for the delay.
152 GNUNET_SCHEDULER_TaskIdentifier delay_task;
155 * Size of the message.
163 * Information per peer and request.
169 * Handle to generic request.
171 struct GSF_PendingRequest *pr;
174 * Handle to specific peer.
176 struct GSF_ConnectedPeer *cp;
179 * Task for asynchronous stopping of this request.
181 GNUNET_SCHEDULER_TaskIdentifier kill_task;
189 struct GSF_ConnectedPeer
193 * Performance data for this peer.
195 struct GSF_PeerPerformanceData ppd;
198 * Time until when we blocked this peer from migrating
201 struct GNUNET_TIME_Absolute last_migration_block;
204 * Task scheduled to revive migration to this peer.
206 GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
209 * Messages (replies, queries, content migration) we would like to
210 * send to this peer in the near future. Sorted by priority, head.
212 struct GSF_PeerTransmitHandle *pth_head;
215 * Messages (replies, queries, content migration) we would like to
216 * send to this peer in the near future. Sorted by priority, tail.
218 struct GSF_PeerTransmitHandle *pth_tail;
221 * Messages (replies, queries, content migration) we would like to
222 * send to this peer in the near future. Sorted by priority, head.
224 struct GSF_DelayedHandle *delayed_head;
227 * Messages (replies, queries, content migration) we would like to
228 * send to this peer in the near future. Sorted by priority, tail.
230 struct GSF_DelayedHandle *delayed_tail;
233 * Migration stop message in our queue, or NULL if we have none pending.
235 struct GSF_PeerTransmitHandle *migration_pth;
238 * Context of our GNUNET_ATS_reserve_bandwidth call (or NULL).
240 struct GNUNET_ATS_ReservationContext *rc;
243 * Task scheduled if we need to retry bandwidth reservation later.
245 GNUNET_SCHEDULER_TaskIdentifier rc_delay_task;
248 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
250 struct GNUNET_CONTAINER_MultiHashMap *request_map;
253 * Handle for an active request for transmission to this
254 * peer, or NULL (if core queue was full).
256 struct GNUNET_CORE_TransmitHandle *cth;
259 * Increase in traffic preference still to be submitted
260 * to the core service for this peer.
262 uint64_t inc_preference;
265 * Set to 1 if we're currently in the process of calling
266 * 'GNUNET_CORE_notify_transmit_ready' (so while cth is
267 * NULL, we should not call notify_transmit_ready for this
270 unsigned int cth_in_progress;
273 * Respect rating for this peer on disk.
275 uint32_t disk_respect;
278 * Which offset in "last_p2p_replies" will be updated next?
279 * (we go round-robin).
281 unsigned int last_p2p_replies_woff;
284 * Which offset in "last_client_replies" will be updated next?
285 * (we go round-robin).
287 unsigned int last_client_replies_woff;
290 * Current offset into 'last_request_times' ring buffer.
292 unsigned int last_request_times_off;
295 * GNUNET_YES if we did successfully reserve 32k bandwidth,
304 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
306 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
309 * Where do we store respect information?
311 static char *respectDirectory;
314 * Handle to ATS service.
316 static struct GNUNET_ATS_PerformanceHandle *ats;
319 * Get the filename under which we would store respect
320 * for the given peer.
322 * @param id peer to get the filename for
323 * @return filename of the form DIRECTORY/PEERID
326 get_respect_filename (const struct GNUNET_PeerIdentity *id)
328 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
331 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
332 GNUNET_asprintf (&fn, "%s%s%s", respectDirectory, DIR_SEPARATOR_STR, &fil);
338 * Find latency information in 'atsi'.
340 * @param atsi performance data
341 * @param atsi_count number of records in 'atsi'
342 * @return connection latency
344 static struct GNUNET_TIME_Relative
345 get_latency (const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
349 for (i = 0; i < atsi_count; i++)
350 if (ntohl (atsi->type) == GNUNET_ATS_QUALITY_NET_DELAY)
351 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
352 ntohl (atsi->value));
353 return GNUNET_TIME_UNIT_SECONDS;
358 * Update the performance information kept for the given peer.
360 * @param cp peer record to update
361 * @param atsi transport performance data
362 * @param atsi_count number of records in 'atsi'
365 update_atsi (struct GSF_ConnectedPeer *cp,
366 const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
368 struct GNUNET_TIME_Relative latency;
370 latency = get_latency (atsi, atsi_count);
371 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
372 /* LATER: merge atsi into cp's performance data (if we ever care...) */
377 * Return the performance data record for the given peer
379 * @param cp peer to query
380 * @return performance data record for the peer
382 struct GSF_PeerPerformanceData *
383 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
390 * Core is ready to transmit to a peer, get the message.
392 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
393 * @param size number of bytes core is willing to take
394 * @param buf where to copy the message
395 * @return number of bytes copied to buf
398 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
402 * Function called by core upon success or failure of our bandwidth reservation request.
404 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
405 * @param peer identifies the peer
406 * @param amount set to the amount that was actually reserved or unreserved;
407 * either the full requested amount or zero (no partial reservations)
408 * @param res_delay if the reservation could not be satisfied (amount was 0), how
409 * long should the client wait until re-trying?
412 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
413 int32_t amount, struct GNUNET_TIME_Relative res_delay);
417 * If ready (bandwidth reserved), try to schedule transmission via
418 * core for the given handle.
420 * @param pth transmission handle to schedule
423 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
425 struct GSF_ConnectedPeer *cp;
426 struct GNUNET_PeerIdentity target;
429 if ((NULL != cp->cth) || (0 != cp->cth_in_progress))
430 return; /* already done */
431 GNUNET_assert (0 != cp->ppd.pid);
432 GNUNET_PEER_resolve (cp->ppd.pid, &target);
434 if (0 != cp->inc_preference)
436 GNUNET_ATS_change_preference (ats, &target, GNUNET_ATS_PREFERENCE_BANDWIDTH,
437 (double) cp->inc_preference,
438 GNUNET_ATS_PREFERENCE_END);
439 cp->inc_preference = 0;
442 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
444 /* query, need reservation */
445 if (GNUNET_YES != cp->did_reserve)
446 return; /* not ready */
447 cp->did_reserve = GNUNET_NO;
448 /* reservation already done! */
449 pth->was_reserved = GNUNET_YES;
451 GNUNET_ATS_reserve_bandwidth (ats, &target, DBLOCK_SIZE,
452 &ats_reserve_callback, cp);
455 GNUNET_assert (NULL == cp->cth);
456 cp->cth_in_progress++;
458 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
459 GNUNET_TIME_absolute_get_remaining
460 (pth->timeout), &target, pth->size,
461 &peer_transmit_ready_cb, cp);
462 GNUNET_assert (NULL != cp->cth);
463 GNUNET_assert (0 < cp->cth_in_progress--);
468 * Core is ready to transmit to a peer, get the message.
470 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
471 * @param size number of bytes core is willing to take
472 * @param buf where to copy the message
473 * @return number of bytes copied to buf
476 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
478 struct GSF_ConnectedPeer *cp = cls;
479 struct GSF_PeerTransmitHandle *pth = cp->pth_head;
480 struct GSF_PeerTransmitHandle *pos;
486 if (pth->size > size)
488 schedule_transmission (pth);
491 if (GNUNET_SCHEDULER_NO_TASK != pth->timeout_task)
493 GNUNET_SCHEDULER_cancel (pth->timeout_task);
494 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
496 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
497 if (GNUNET_YES == pth->is_query)
499 cp->ppd.last_request_times[(cp->last_request_times_off++) %
500 MAX_QUEUE_PER_PEER] =
501 GNUNET_TIME_absolute_get ();
502 GNUNET_assert (0 < cp->ppd.pending_queries--);
504 else if (GNUNET_NO == pth->is_query)
506 GNUNET_assert (0 < cp->ppd.pending_replies--);
508 GNUNET_LOAD_update (cp->ppd.transmission_delay,
509 GNUNET_TIME_absolute_get_duration
510 (pth->transmission_request_start_time).rel_value);
511 ret = pth->gmc (pth->gmc_cls, size, buf);
512 if (NULL != (pos = cp->pth_head))
514 GNUNET_assert (pos != pth);
515 schedule_transmission (pos);
523 * (re)try to reserve bandwidth from the given peer.
525 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
526 * @param tc scheduler context
529 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
531 struct GSF_ConnectedPeer *cp = cls;
532 struct GNUNET_PeerIdentity target;
534 GNUNET_PEER_resolve (cp->ppd.pid, &target);
535 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
537 GNUNET_ATS_reserve_bandwidth (ats, &target, DBLOCK_SIZE,
538 &ats_reserve_callback, cp);
543 * Function called by core upon success or failure of our bandwidth reservation request.
545 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
546 * @param peer identifies the peer
547 * @param amount set to the amount that was actually reserved or unreserved;
548 * either the full requested amount or zero (no partial reservations)
549 * @param res_delay if the reservation could not be satisfied (amount was 0), how
550 * long should the client wait until re-trying?
553 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
554 int32_t amount, struct GNUNET_TIME_Relative res_delay)
556 struct GSF_ConnectedPeer *cp = cls;
557 struct GSF_PeerTransmitHandle *pth;
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Reserved %d bytes / need to wait %s for reservation\n",
562 GNUNET_STRINGS_relative_time_to_string (res_delay, GNUNET_YES));
567 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
570 cp->did_reserve = GNUNET_YES;
572 if ((NULL != pth) && (NULL == cp->cth) && (0 == cp->cth_in_progress))
574 /* reservation success, try transmission now! */
575 cp->cth_in_progress++;
577 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
578 GNUNET_TIME_absolute_get_remaining
579 (pth->timeout), peer, pth->size,
580 &peer_transmit_ready_cb, cp);
581 GNUNET_assert (NULL != cp->cth);
582 GNUNET_assert (0 < cp->cth_in_progress--);
588 * A peer connected to us. Setup the connected peer
591 * @param peer identity of peer that connected
592 * @param atsi performance data for the connection
593 * @param atsi_count number of records in 'atsi'
594 * @return handle to connected peer entry
596 struct GSF_ConnectedPeer *
597 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
598 const struct GNUNET_ATS_Information *atsi,
599 unsigned int atsi_count)
601 struct GSF_ConnectedPeer *cp;
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n",
607 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
608 cp->ppd.pid = GNUNET_PEER_intern (peer);
609 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
611 GNUNET_ATS_reserve_bandwidth (ats, peer, DBLOCK_SIZE,
612 &ats_reserve_callback, cp);
613 fn = get_respect_filename (peer);
614 if ((GNUNET_YES == GNUNET_DISK_file_test (fn)) &&
615 (sizeof (respect) == GNUNET_DISK_fn_read (fn, &respect, sizeof (respect))))
616 cp->disk_respect = cp->ppd.respect = ntohl (respect);
618 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
619 GNUNET_break (GNUNET_OK ==
620 GNUNET_CONTAINER_multihashmap_put (cp_map, &peer->hashPubKey,
622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
623 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
624 GNUNET_CONTAINER_multihashmap_size (cp_map),
626 update_atsi (cp, atsi, atsi_count);
627 GSF_push_start_ (cp);
633 * It may be time to re-start migrating content to this
634 * peer. Check, and if so, restart migration.
636 * @param cls the 'struct GSF_ConnectedPeer'
637 * @param tc scheduler context
640 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
642 struct GSF_ConnectedPeer *cp = cls;
643 struct GNUNET_TIME_Relative bt;
645 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
646 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
647 if (0 != bt.rel_value)
649 /* still time left... */
650 cp->mig_revive_task =
651 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
654 GSF_push_start_ (cp);
659 * Get a handle for a connected peer.
661 * @param peer peer's identity
662 * @return NULL if the peer is not currently connected
664 struct GSF_ConnectedPeer *
665 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
669 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
674 * Handle P2P "MIGRATION_STOP" message.
676 * @param cls closure, always NULL
677 * @param other the other peer involved (sender or receiver, NULL
678 * for loopback messages where we are both sender and receiver)
679 * @param message the actual message
680 * @param atsi performance information
681 * @param atsi_count number of records in 'atsi'
682 * @return GNUNET_OK to keep the connection open,
683 * GNUNET_SYSERR to close it (signal serious error)
686 GSF_handle_p2p_migration_stop_ (void *cls,
687 const struct GNUNET_PeerIdentity *other,
688 const struct GNUNET_MessageHeader *message,
689 const struct GNUNET_ATS_Information *atsi,
690 unsigned int atsi_count)
692 struct GSF_ConnectedPeer *cp;
693 const struct MigrationStopMessage *msm;
694 struct GNUNET_TIME_Relative bt;
696 msm = (const struct MigrationStopMessage *) message;
697 cp = GSF_peer_get_ (other);
703 GNUNET_STATISTICS_update (GSF_stats,
704 gettext_noop ("# migration stop messages received"),
706 bt = GNUNET_TIME_relative_ntoh (msm->duration);
707 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
708 _("Migration of content to peer `%s' blocked for %s\n"),
710 GNUNET_STRINGS_relative_time_to_string (bt, GNUNET_YES));
711 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
712 if (GNUNET_SCHEDULER_NO_TASK == cp->mig_revive_task)
715 cp->mig_revive_task =
716 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
718 update_atsi (cp, atsi, atsi_count);
724 * Copy reply and free put message.
726 * @param cls the 'struct PutMessage'
727 * @param buf_size number of bytes available in buf
728 * @param buf where to copy the message, NULL on error (peer disconnect)
729 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
732 copy_reply (void *cls, size_t buf_size, void *buf)
734 struct PutMessage *pm = cls;
739 GNUNET_assert (buf_size >= ntohs (pm->header.size));
740 size = ntohs (pm->header.size);
741 memcpy (buf, pm, size);
742 GNUNET_STATISTICS_update (GSF_stats,
744 ("# replies transmitted to other peers"), 1,
750 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
759 * Free resources associated with the given peer request.
761 * @param peerreq request to free
762 * @param query associated key for the request
765 free_pending_request (struct PeerRequest *peerreq,
766 const struct GNUNET_HashCode *query)
768 struct GSF_ConnectedPeer *cp = peerreq->cp;
770 if (GNUNET_SCHEDULER_NO_TASK != peerreq->kill_task)
772 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
773 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
775 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
777 GNUNET_break (GNUNET_YES ==
778 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
780 GNUNET_free (peerreq);
785 * Cancel all requests associated with the peer.
788 * @param query hash code of the request
789 * @param value the 'struct GSF_PendingRequest'
790 * @return GNUNET_YES (continue to iterate)
793 cancel_pending_request (void *cls, const struct GNUNET_HashCode * query, void *value)
795 struct PeerRequest *peerreq = value;
796 struct GSF_PendingRequest *pr = peerreq->pr;
797 struct GSF_PendingRequestData *prd;
799 prd = GSF_pending_request_get_data_ (pr);
800 GSF_pending_request_cancel_ (pr, GNUNET_NO);
801 free_pending_request (peerreq, &prd->query);
807 * Free the given request.
809 * @param cls the request to free
810 * @param tc task context
813 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
815 struct PeerRequest *peerreq = cls;
816 struct GSF_PendingRequest *pr = peerreq->pr;
817 struct GSF_PendingRequestData *prd;
819 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
820 prd = GSF_pending_request_get_data_ (pr);
821 cancel_pending_request (NULL, &prd->query, peerreq);
826 * The artificial delay is over, transmit the message now.
828 * @param cls the 'struct GSF_DelayedHandle' with the message
829 * @param tc scheduler context
832 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
834 struct GSF_DelayedHandle *dh = cls;
835 struct GSF_ConnectedPeer *cp = dh->cp;
837 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
838 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
840 GNUNET_free (dh->pm);
844 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
845 dh->msize, ©_reply, dh->pm);
851 * Get the randomized delay a response should be subjected to.
853 * @return desired delay
855 static struct GNUNET_TIME_Relative
856 get_randomized_delay ()
858 struct GNUNET_TIME_Relative ret;
861 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
862 GNUNET_CRYPTO_random_u32
863 (GNUNET_CRYPTO_QUALITY_WEAK,
864 2 * GSF_avg_latency.rel_value + 1));
865 GNUNET_STATISTICS_update (GSF_stats,
867 ("# artificial delays introduced (ms)"),
868 ret.rel_value, GNUNET_NO);
875 * Handle a reply to a pending request. Also called if a request
876 * expires (then with data == NULL). The handler may be called
877 * many times (depending on the request type), but will not be
878 * called during or after a call to GSF_pending_request_cancel
879 * and will also not be called anymore after a call signalling
882 * @param cls 'struct PeerRequest' this is an answer for
883 * @param eval evaluation of the result
884 * @param pr handle to the original pending request
885 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
886 * @param expiration when does 'data' expire?
887 * @param last_transmission when did we last transmit a request for this block
888 * @param type type of the block
889 * @param data response data, NULL on request expiration
890 * @param data_len number of bytes in data
893 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
894 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
895 struct GNUNET_TIME_Absolute expiration,
896 struct GNUNET_TIME_Absolute last_transmission,
897 enum GNUNET_BLOCK_Type type, const void *data,
900 struct PeerRequest *peerreq = cls;
901 struct GSF_ConnectedPeer *cp = peerreq->cp;
902 struct GSF_PendingRequestData *prd;
903 struct PutMessage *pm;
906 GNUNET_assert (data_len + sizeof (struct PutMessage) <
907 GNUNET_SERVER_MAX_MESSAGE_SIZE);
908 GNUNET_assert (peerreq->pr == pr);
909 prd = GSF_pending_request_get_data_ (pr);
912 free_pending_request (peerreq, &prd->query);
915 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
916 if ((prd->type != type) && (GNUNET_BLOCK_TYPE_ANY != prd->type))
918 GNUNET_STATISTICS_update (GSF_stats,
920 ("# replies dropped due to type mismatch"),
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925 "Transmitting result for query `%s' to peer\n",
926 GNUNET_h2s (&prd->query));
927 GNUNET_STATISTICS_update (GSF_stats,
928 gettext_noop ("# replies received for other peers"),
930 msize = sizeof (struct PutMessage) + data_len;
931 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
936 if ((UINT32_MAX != reply_anonymity_level) && (reply_anonymity_level > 1))
938 if (reply_anonymity_level - 1 > GSF_cover_content_count)
940 GNUNET_STATISTICS_update (GSF_stats,
942 ("# replies dropped due to insufficient cover traffic"),
946 GSF_cover_content_count -= (reply_anonymity_level - 1);
949 pm = GNUNET_malloc (msize);
950 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
951 pm->header.size = htons (msize);
952 pm->type = htonl (type);
953 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
954 memcpy (&pm[1], data, data_len);
955 if ((UINT32_MAX != reply_anonymity_level) && (0 != reply_anonymity_level) &&
956 (GNUNET_YES == GSF_enable_randomized_delays))
958 struct GSF_DelayedHandle *dh;
960 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
964 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
966 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
967 &transmit_delayed_now, dh);
971 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
974 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
976 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
978 GNUNET_STATISTICS_update (GSF_stats,
980 ("# P2P searches destroyed due to ultimate reply"),
983 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
989 * Increase the peer's respect by a value.
991 * @param cp which peer to change the respect value on
992 * @param value is the int value by which the
993 * peer's credit is to be increased or decreased
994 * @returns the actual change in respect (positive or negative)
997 change_peer_respect (struct GSF_ConnectedPeer *cp, int value)
1001 GNUNET_assert (NULL != cp);
1004 if (cp->ppd.respect + value < cp->ppd.respect)
1006 value = UINT32_MAX - cp->ppd.respect;
1007 cp->ppd.respect = UINT32_MAX;
1010 cp->ppd.respect += value;
1014 if (cp->ppd.respect < -value)
1016 value = -cp->ppd.respect;
1017 cp->ppd.respect = 0;
1020 cp->ppd.respect += value;
1027 * We've received a request with the specified priority. Bound it
1028 * according to how much we respect the given peer.
1030 * @param prio_in requested priority
1031 * @param cp the peer making the request
1032 * @return effective priority
1035 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1037 #define N ((double)128.0)
1042 ld = GSF_test_get_load_too_high_ (0);
1043 if (GNUNET_SYSERR == ld)
1045 GNUNET_STATISTICS_update (GSF_stats,
1047 ("# requests done for free (low load)"), 1,
1049 return 0; /* excess resources */
1051 if (prio_in > INT32_MAX)
1052 prio_in = INT32_MAX;
1053 ret = -change_peer_respect (cp, -(int) prio_in);
1056 if (ret > GSF_current_priorities + N)
1057 rret = GSF_current_priorities + N;
1060 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1062 if ((GNUNET_YES == ld) && (ret > 0))
1064 /* try with charging */
1065 ld = GSF_test_get_load_too_high_ (ret);
1067 if (GNUNET_YES == ld)
1069 GNUNET_STATISTICS_update (GSF_stats,
1071 ("# request dropped, priority insufficient"), 1,
1074 change_peer_respect (cp, (int) ret);
1075 return -1; /* not enough resources */
1079 GNUNET_STATISTICS_update (GSF_stats,
1081 ("# requests done for a price (normal load)"), 1,
1090 * The priority level imposes a bound on the maximum
1091 * value for the ttl that can be requested.
1093 * @param ttl_in requested ttl
1094 * @param prio given priority
1095 * @return ttl_in if ttl_in is below the limit,
1096 * otherwise the ttl-limit for the given priority
1099 bound_ttl (int32_t ttl_in, uint32_t prio)
1101 unsigned long long allowed;
1105 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1106 if (ttl_in > allowed)
1108 if (allowed >= (1 << 30))
1117 * Handle P2P "QUERY" message. Creates the pending request entry
1118 * and sets up all of the data structures to that we will
1119 * process replies properly. Does not initiate forwarding or
1120 * local database lookups.
1122 * @param other the other peer involved (sender or receiver, NULL
1123 * for loopback messages where we are both sender and receiver)
1124 * @param message the actual message
1125 * @return pending request handle, NULL on error
1127 struct GSF_PendingRequest *
1128 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1129 const struct GNUNET_MessageHeader *message)
1131 struct PeerRequest *peerreq;
1132 struct GSF_PendingRequest *pr;
1133 struct GSF_PendingRequestData *prd;
1134 struct GSF_ConnectedPeer *cp;
1135 struct GSF_ConnectedPeer *cps;
1136 const struct GNUNET_HashCode *namespace;
1137 const struct GNUNET_PeerIdentity *target;
1138 enum GSF_PendingRequestOptions options;
1140 const struct GetMessage *gm;
1142 const struct GNUNET_HashCode *opt;
1145 uint32_t ttl_decrement;
1148 enum GNUNET_BLOCK_Type type;
1149 GNUNET_PEER_Id spid;
1151 GNUNET_assert (other != NULL);
1152 msize = ntohs (message->size);
1153 if (msize < sizeof (struct GetMessage))
1155 GNUNET_break_op (0);
1158 GNUNET_STATISTICS_update (GSF_stats,
1160 ("# GET requests received (from other peers)"), 1,
1162 gm = (const struct GetMessage *) message;
1163 type = ntohl (gm->type);
1164 bm = ntohl (gm->hash_bitmap);
1172 if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_HashCode))
1174 GNUNET_break_op (0);
1177 opt = (const struct GNUNET_HashCode *) &gm[1];
1178 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_HashCode);
1179 /* bfsize must be power of 2, check! */
1180 if (0 != ((bfsize - 1) & bfsize))
1182 GNUNET_break_op (0);
1185 GSF_cover_query_count++;
1186 bm = ntohl (gm->hash_bitmap);
1188 cps = GSF_peer_get_ (other);
1191 /* peer must have just disconnected */
1192 GNUNET_STATISTICS_update (GSF_stats,
1194 ("# requests dropped due to initiator not being connected"),
1198 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1199 cp = GSF_peer_get_ ((const struct GNUNET_PeerIdentity *) &opt[bits++]);
1204 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1207 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1213 GNUNET_i2s (other));
1214 GNUNET_STATISTICS_update (GSF_stats,
1216 ("# requests dropped due to missing reverse route"),
1220 /* note that we can really only check load here since otherwise
1221 * peers could find out that we are overloaded by not being
1222 * disconnected after sending us a malformed query... */
1223 priority = bound_priority (ntohl (gm->priority), cps);
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Dropping query from `%s', this peer is too busy.\n",
1228 GNUNET_i2s (other));
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1233 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1235 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1236 if ((GNUNET_BLOCK_TYPE_FS_SBLOCK == type) && (NULL == namespace))
1238 GNUNET_break_op (0);
1241 if ((GNUNET_BLOCK_TYPE_FS_SBLOCK != type) && (NULL != namespace))
1243 GNUNET_break_op (0);
1248 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1249 *) &opt[bits++]) : NULL;
1250 options = GSF_PRO_DEFAULTS;
1252 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1253 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1254 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1255 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1257 /* don't have BW to send to peer, or would likely take longer than we have for it,
1258 * so at best indirect the query */
1260 options |= GSF_PRO_FORWARD_ONLY;
1261 spid = GNUNET_PEER_intern (other);
1262 GNUNET_assert (0 != spid);
1264 ttl = bound_ttl (ntohl (gm->ttl), priority);
1265 /* decrement ttl (always) */
1267 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1269 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1272 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1273 GNUNET_i2s (other), ttl, ttl_decrement);
1274 GNUNET_STATISTICS_update (GSF_stats,
1276 ("# requests dropped due TTL underflow"), 1,
1278 /* integer underflow => drop (should be very rare)! */
1281 ttl -= ttl_decrement;
1283 /* test if the request already exists */
1284 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1285 if (peerreq != NULL)
1288 prd = GSF_pending_request_get_data_ (pr);
1289 if ((prd->type == type) &&
1290 ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1291 (0 == memcmp (&prd->namespace, namespace, sizeof (struct GNUNET_HashCode)))))
1293 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1295 /* existing request has higher TTL, drop new one! */
1296 prd->priority += priority;
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "Have existing request with higher TTL, dropping new request.\n",
1299 GNUNET_i2s (other));
1300 GNUNET_STATISTICS_update (GSF_stats,
1302 ("# requests dropped due to higher-TTL request"),
1306 /* existing request has lower TTL, drop old one! */
1307 priority += prd->priority;
1308 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1309 free_pending_request (peerreq, &gm->query);
1313 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1315 pr = GSF_pending_request_create_ (options, type, &gm->query, namespace,
1318 0) ? (const char *) &opt[bits] : NULL,
1319 bfsize, ntohl (gm->filter_mutator),
1321 (uint32_t) priority, ttl, spid, GNUNET_PEER_intern (other), NULL, 0, /* replies_seen */
1322 &handle_p2p_reply, peerreq);
1323 GNUNET_assert (NULL != pr);
1325 GNUNET_break (GNUNET_OK ==
1326 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1328 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1329 GNUNET_STATISTICS_update (GSF_stats,
1331 ("# P2P query messages received and processed"), 1,
1333 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1340 * Function called if there has been a timeout trying to satisfy
1341 * a transmission request.
1343 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1344 * @param tc scheduler context
1347 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1349 struct GSF_PeerTransmitHandle *pth = cls;
1350 struct GSF_ConnectedPeer *cp;
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Timeout trying to transmit to other peer\n");
1354 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1356 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1357 if (GNUNET_YES == pth->is_query)
1358 GNUNET_assert (0 < cp->ppd.pending_queries--);
1359 else if (GNUNET_NO == pth->is_query)
1360 GNUNET_assert (0 < cp->ppd.pending_replies--);
1361 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1362 if (NULL != cp->cth)
1364 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1367 pth->gmc (pth->gmc_cls, 0, NULL);
1368 GNUNET_assert (0 == cp->cth_in_progress);
1374 * Transmit a message to the given peer as soon as possible.
1375 * If the peer disconnects before the transmission can happen,
1376 * the callback is invoked with a 'NULL' buffer.
1378 * @param cp target peer
1379 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1380 * @param priority how important is this request?
1381 * @param timeout when does this request timeout (call gmc with error)
1382 * @param size number of bytes we would like to send to the peer
1383 * @param gmc function to call to get the message
1384 * @param gmc_cls closure for gmc
1385 * @return handle to cancel request
1387 struct GSF_PeerTransmitHandle *
1388 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1389 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1390 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1392 struct GSF_PeerTransmitHandle *pth;
1393 struct GSF_PeerTransmitHandle *pos;
1394 struct GSF_PeerTransmitHandle *prev;
1396 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1397 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1398 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1400 pth->gmc_cls = gmc_cls;
1402 pth->is_query = is_query;
1403 pth->priority = priority;
1405 /* insertion sort (by priority, descending) */
1408 while ((NULL != pos) && (pos->priority > priority))
1413 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1414 if (GNUNET_YES == is_query)
1415 cp->ppd.pending_queries++;
1416 else if (GNUNET_NO == is_query)
1417 cp->ppd.pending_replies++;
1419 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1420 schedule_transmission (pth);
1426 * Cancel an earlier request for transmission.
1428 * @param pth request to cancel
1431 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1433 struct GSF_ConnectedPeer *cp;
1435 if (GNUNET_SCHEDULER_NO_TASK != pth->timeout_task)
1437 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1438 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1441 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1442 if (GNUNET_YES == pth->is_query)
1443 GNUNET_assert (0 < cp->ppd.pending_queries--);
1444 else if (GNUNET_NO == pth->is_query)
1445 GNUNET_assert (0 < cp->ppd.pending_replies--);
1451 * Report on receiving a reply; update the performance record of the given peer.
1453 * @param cp responding peer (will be updated)
1454 * @param request_time time at which the original query was transmitted
1455 * @param request_priority priority of the original request
1458 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1459 struct GNUNET_TIME_Absolute request_time,
1460 uint32_t request_priority)
1462 struct GNUNET_TIME_Relative delay;
1464 delay = GNUNET_TIME_absolute_get_duration (request_time);
1465 cp->ppd.avg_reply_delay.rel_value =
1466 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1467 delay.rel_value) / RUNAVG_DELAY_N;
1468 cp->ppd.avg_priority =
1469 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1470 request_priority) / RUNAVG_DELAY_N;
1475 * Report on receiving a reply in response to an initiating client.
1476 * Remember that this peer is good for this client.
1478 * @param cp responding peer (will be updated)
1479 * @param initiator_client local client on responsible for query
1482 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1483 struct GSF_LocalClient *initiator_client)
1485 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1486 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1491 * Report on receiving a reply in response to an initiating peer.
1492 * Remember that this peer is good for this initiating peer.
1494 * @param cp responding peer (will be updated)
1495 * @param initiator_peer other peer responsible for query
1498 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1499 const struct GSF_ConnectedPeer *initiator_peer)
1503 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1504 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1505 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1506 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1507 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1512 * A peer disconnected from us. Tear down the connected peer
1516 * @param peer identity of peer that connected
1519 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1521 struct GSF_ConnectedPeer *cp;
1522 struct GSF_PeerTransmitHandle *pth;
1523 struct GSF_DelayedHandle *dh;
1525 cp = GSF_peer_get_ (peer);
1527 return; /* must have been disconnect from core with
1528 * 'peer' == my_id, ignore */
1529 GNUNET_assert (GNUNET_YES ==
1530 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1531 &peer->hashPubKey, cp));
1532 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
1533 GNUNET_CONTAINER_multihashmap_size (cp_map),
1535 if (NULL != cp->migration_pth)
1537 GSF_peer_transmit_cancel_ (cp->migration_pth);
1538 cp->migration_pth = NULL;
1542 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1545 if (GNUNET_SCHEDULER_NO_TASK != cp->rc_delay_task)
1547 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1548 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1550 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1551 &cancel_pending_request, cp);
1552 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1553 cp->request_map = NULL;
1554 GSF_plan_notify_peer_disconnect_ (cp);
1555 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1556 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1557 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1558 GSF_push_stop_ (cp);
1559 if (NULL != cp->cth)
1561 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1564 GNUNET_assert (0 == cp->cth_in_progress);
1565 while (NULL != (pth = cp->pth_head))
1567 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1569 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1570 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1572 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1573 pth->gmc (pth->gmc_cls, 0, NULL);
1576 while (NULL != (dh = cp->delayed_head))
1578 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1579 GNUNET_SCHEDULER_cancel (dh->delay_task);
1580 GNUNET_free (dh->pm);
1583 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1584 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1586 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1587 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1594 * Closure for 'call_iterator'.
1596 struct IterationContext
1599 * Function to call on each entry.
1601 GSF_ConnectedPeerIterator it;
1611 * Function that calls the callback for each peer.
1613 * @param cls the 'struct IterationContext*'
1614 * @param key identity of the peer
1615 * @param value the 'struct GSF_ConnectedPeer*'
1616 * @return GNUNET_YES to continue iteration
1619 call_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
1621 struct IterationContext *ic = cls;
1622 struct GSF_ConnectedPeer *cp = value;
1624 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1630 * Iterate over all connected peers.
1632 * @param it function to call for each peer
1633 * @param it_cls closure for it
1636 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1638 struct IterationContext ic;
1642 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1647 * Obtain the identity of a connected peer.
1649 * @param cp peer to reserve bandwidth from
1650 * @param id identity to set (written to)
1653 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1654 struct GNUNET_PeerIdentity *id)
1656 GNUNET_assert (0 != cp->ppd.pid);
1657 GNUNET_PEER_resolve (cp->ppd.pid, id);
1662 * Assemble a migration stop message for transmission.
1664 * @param cls the 'struct GSF_ConnectedPeer' to use
1665 * @param size number of bytes we're allowed to write to buf
1666 * @param buf where to copy the message
1667 * @return number of bytes copied to buf
1670 create_migration_stop_message (void *cls, size_t size, void *buf)
1672 struct GSF_ConnectedPeer *cp = cls;
1673 struct MigrationStopMessage msm;
1675 cp->migration_pth = NULL;
1678 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1679 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1680 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1681 msm.reserved = htonl (0);
1683 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1684 (cp->last_migration_block));
1685 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1686 GNUNET_STATISTICS_update (GSF_stats,
1687 gettext_noop ("# migration stop messages sent"),
1689 return sizeof (struct MigrationStopMessage);
1694 * Ask a peer to stop migrating data to us until the given point
1697 * @param cp peer to ask
1698 * @param block_time until when to block
1701 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1702 struct GNUNET_TIME_Absolute block_time)
1704 if (cp->last_migration_block.abs_value > block_time.abs_value)
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1707 "Migration already blocked for another %s\n",
1708 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining
1709 (cp->last_migration_block), GNUNET_YES));
1710 return; /* already blocked */
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1713 (unsigned long long) GNUNET_TIME_absolute_get_remaining (block_time).rel_value);
1714 cp->last_migration_block = block_time;
1715 if (NULL != cp->migration_pth)
1716 GSF_peer_transmit_cancel_ (cp->migration_pth);
1718 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1719 GNUNET_TIME_UNIT_FOREVER_REL,
1720 sizeof (struct MigrationStopMessage),
1721 &create_migration_stop_message, cp);
1726 * Write peer-respect information to a file - flush the buffer entry!
1729 * @param key peer identity
1730 * @param value the 'struct GSF_ConnectedPeer' to flush
1731 * @return GNUNET_OK to continue iteration
1734 flush_respect (void *cls, const struct GNUNET_HashCode * key, void *value)
1736 struct GSF_ConnectedPeer *cp = value;
1739 struct GNUNET_PeerIdentity pid;
1741 if (cp->ppd.respect == cp->disk_respect)
1742 return GNUNET_OK; /* unchanged */
1743 GNUNET_assert (0 != cp->ppd.pid);
1744 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1745 fn = get_respect_filename (&pid);
1746 if (cp->ppd.respect == 0)
1748 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1749 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1750 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1754 respect = htonl (cp->ppd.respect);
1755 if (sizeof (uint32_t) ==
1756 GNUNET_DISK_fn_write (fn, &respect, sizeof (uint32_t),
1757 GNUNET_DISK_PERM_USER_READ |
1758 GNUNET_DISK_PERM_USER_WRITE |
1759 GNUNET_DISK_PERM_GROUP_READ |
1760 GNUNET_DISK_PERM_OTHER_READ))
1761 cp->disk_respect = cp->ppd.respect;
1769 * Notify core about a preference we have for the given peer
1770 * (to allocate more resources towards it). The change will
1771 * be communicated the next time we reserve bandwidth with
1772 * core (not instantly).
1774 * @param cp peer to reserve bandwidth from
1775 * @param pref preference change
1778 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1781 cp->inc_preference += pref;
1786 * Call this method periodically to flush respect information to disk.
1788 * @param cls closure, not used
1789 * @param tc task context, not used
1792 cron_flush_respect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1797 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_respect, NULL);
1800 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1802 GNUNET_SCHEDULER_add_delayed_with_priority (RESPECT_FLUSH_FREQ,
1803 GNUNET_SCHEDULER_PRIORITY_HIGH,
1804 &cron_flush_respect, NULL);
1809 * Initialize peer management subsystem.
1812 GSF_connected_peer_init_ ()
1814 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1815 ats = GNUNET_ATS_performance_init (GSF_cfg, NULL, NULL);
1816 GNUNET_assert (GNUNET_OK ==
1817 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1819 &respectDirectory));
1820 GNUNET_DISK_directory_create (respectDirectory);
1821 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1822 &cron_flush_respect, NULL);
1827 * Iterator to free peer entries.
1829 * @param cls closure, unused
1830 * @param key current key code
1831 * @param value value in the hash map (peer entry)
1832 * @return GNUNET_YES (we should continue to iterate)
1835 clean_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
1837 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1843 * Shutdown peer management subsystem.
1846 GSF_connected_peer_done_ ()
1848 cron_flush_respect (NULL, NULL);
1849 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1850 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1852 GNUNET_free (respectDirectory);
1853 respectDirectory = NULL;
1854 GNUNET_ATS_performance_done (ats);
1860 * Iterator to remove references to LC entry.
1862 * @param cls the 'struct GSF_LocalClient*' to look for
1863 * @param key current key code
1864 * @param value value in the hash map (peer entry)
1865 * @return GNUNET_YES (we should continue to iterate)
1868 clean_local_client (void *cls, const struct GNUNET_HashCode * key, void *value)
1870 const struct GSF_LocalClient *lc = cls;
1871 struct GSF_ConnectedPeer *cp = value;
1874 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1875 if (cp->ppd.last_client_replies[i] == lc)
1876 cp->ppd.last_client_replies[i] = NULL;
1882 * Notification that a local client disconnected. Clean up all of our
1883 * references to the given handle.
1885 * @param lc handle to the local client (henceforth invalid)
1888 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1891 return; /* already cleaned up */
1892 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1897 /* end of gnunet-service-fs_cp.c */