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 trust values to disk?
46 #define TRUST_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 * Handle for an active request for transmission to this
72 * peer, or NULL (if core queue was full).
74 struct GNUNET_CORE_TransmitHandle *cth;
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 GNUNET_SCHEDULER_TaskIdentifier 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;
107 * Size of the message to be transmitted.
112 * Set to 1 if we're currently in the process of calling
113 * 'GNUNET_CORE_notify_transmit_ready' (so while cth is
114 * NULL, we should not call notify_transmit_ready for this
117 unsigned int cth_in_progress;
120 * GNUNET_YES if this is a query, GNUNET_NO for content.
125 * Did we get a reservation already?
130 * Priority of this request.
138 * Handle for an entry in our delay list.
140 struct GSF_DelayedHandle
144 * Kept in a doubly-linked list.
146 struct GSF_DelayedHandle *next;
149 * Kept in a doubly-linked list.
151 struct GSF_DelayedHandle *prev;
154 * Peer this transmission belongs to.
156 struct GSF_ConnectedPeer *cp;
159 * The PUT that was delayed.
161 struct PutMessage *pm;
164 * Task for the delay.
166 GNUNET_SCHEDULER_TaskIdentifier delay_task;
169 * Size of the message.
177 * Information per peer and request.
183 * Handle to generic request.
185 struct GSF_PendingRequest *pr;
188 * Handle to specific peer.
190 struct GSF_ConnectedPeer *cp;
193 * Task for asynchronous stopping of this request.
195 GNUNET_SCHEDULER_TaskIdentifier kill_task;
203 struct GSF_ConnectedPeer
207 * Performance data for this peer.
209 struct GSF_PeerPerformanceData ppd;
212 * Time until when we blocked this peer from migrating
215 struct GNUNET_TIME_Absolute last_migration_block;
218 * Task scheduled to revive migration to this peer.
220 GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
223 * Messages (replies, queries, content migration) we would like to
224 * send to this peer in the near future. Sorted by priority, head.
226 struct GSF_PeerTransmitHandle *pth_head;
229 * Messages (replies, queries, content migration) we would like to
230 * send to this peer in the near future. Sorted by priority, tail.
232 struct GSF_PeerTransmitHandle *pth_tail;
235 * Messages (replies, queries, content migration) we would like to
236 * send to this peer in the near future. Sorted by priority, head.
238 struct GSF_DelayedHandle *delayed_head;
241 * Messages (replies, queries, content migration) we would like to
242 * send to this peer in the near future. Sorted by priority, tail.
244 struct GSF_DelayedHandle *delayed_tail;
247 * Migration stop message in our queue, or NULL if we have none pending.
249 struct GSF_PeerTransmitHandle *migration_pth;
252 * Context of our GNUNET_ATS_reserve_bandwidth call (or NULL).
254 struct GNUNET_ATS_ReservationContext *rc;
257 * Task scheduled if we need to retry bandwidth reservation later.
259 GNUNET_SCHEDULER_TaskIdentifier rc_delay_task;
262 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
264 struct GNUNET_CONTAINER_MultiHashMap *request_map;
267 * Increase in traffic preference still to be submitted
268 * to the core service for this peer.
270 uint64_t inc_preference;
273 * Trust rating for this peer on disk.
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 trust information?
311 static char *trustDirectory;
314 * Handle to ATS service.
316 static struct GNUNET_ATS_PerformanceHandle *ats;
319 * Get the filename under which we would store the GNUNET_HELLO_Message
320 * for the given host and protocol.
321 * @return filename of the form DIRECTORY/HOSTID
324 get_trust_filename (const struct GNUNET_PeerIdentity *id)
326 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
329 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
330 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
336 * Find latency information in 'atsi'.
338 * @param atsi performance data
339 * @param atsi_count number of records in 'atsi'
340 * @return connection latency
342 static struct GNUNET_TIME_Relative
343 get_latency (const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
347 for (i = 0; i < atsi_count; i++)
348 if (ntohl (atsi->type) == GNUNET_ATS_QUALITY_NET_DELAY)
349 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
350 ntohl (atsi->value));
351 return GNUNET_TIME_UNIT_SECONDS;
356 * Update the performance information kept for the given peer.
358 * @param cp peer record to update
359 * @param atsi transport performance data
360 * @param atsi_count number of records in 'atsi'
363 update_atsi (struct GSF_ConnectedPeer *cp,
364 const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
366 struct GNUNET_TIME_Relative latency;
368 latency = get_latency (atsi, atsi_count);
369 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
370 /* LATER: merge atsi into cp's performance data (if we ever care...) */
375 * Return the performance data record for the given peer
377 * @param cp peer to query
378 * @return performance data record for the peer
380 struct GSF_PeerPerformanceData *
381 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
388 * Core is ready to transmit to a peer, get the message.
390 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
391 * @param size number of bytes core is willing to take
392 * @param buf where to copy the message
393 * @return number of bytes copied to buf
396 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
400 * Function called by core upon success or failure of our bandwidth reservation request.
402 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
403 * @param peer identifies the peer
404 * @param amount set to the amount that was actually reserved or unreserved;
405 * either the full requested amount or zero (no partial reservations)
406 * @param res_delay if the reservation could not be satisfied (amount was 0), how
407 * long should the client wait until re-trying?
410 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
411 int32_t amount, struct GNUNET_TIME_Relative res_delay);
415 * If ready (bandwidth reserved), try to schedule transmission via
416 * core for the given handle.
418 * @param pth transmission handle to schedule
421 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
423 struct GSF_ConnectedPeer *cp;
424 struct GNUNET_PeerIdentity target;
426 if ((NULL != pth->cth) || (0 != pth->cth_in_progress))
427 return; /* already done */
429 GNUNET_assert (0 != cp->ppd.pid);
430 GNUNET_PEER_resolve (cp->ppd.pid, &target);
432 if (0 != cp->inc_preference)
434 GNUNET_ATS_change_preference (ats, &target, GNUNET_ATS_PREFERENCE_BANDWIDTH,
435 (double) cp->inc_preference,
436 GNUNET_ATS_PREFERENCE_END);
437 cp->inc_preference = 0;
440 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
442 /* query, need reservation */
443 if (GNUNET_YES != cp->did_reserve)
444 return; /* not ready */
445 cp->did_reserve = GNUNET_NO;
446 /* reservation already done! */
447 pth->was_reserved = GNUNET_YES;
449 GNUNET_ATS_reserve_bandwidth (ats, &target, DBLOCK_SIZE,
450 &ats_reserve_callback, cp);
452 GNUNET_assert (pth->cth == NULL);
453 pth->cth_in_progress++;
455 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
456 GNUNET_TIME_absolute_get_remaining
457 (pth->timeout), &target, pth->size,
458 &peer_transmit_ready_cb, pth);
459 GNUNET_assert (0 < pth->cth_in_progress--);
464 * Core is ready to transmit to a peer, get the message.
466 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
467 * @param size number of bytes core is willing to take
468 * @param buf where to copy the message
469 * @return number of bytes copied to buf
472 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
474 struct GSF_PeerTransmitHandle *pth = cls;
475 struct GSF_PeerTransmitHandle *pos;
476 struct GSF_ConnectedPeer *cp;
479 GNUNET_assert ((NULL == buf) || (pth->size <= size));
481 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
483 GNUNET_SCHEDULER_cancel (pth->timeout_task);
484 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
487 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
488 if (GNUNET_YES == pth->is_query)
490 cp->ppd.last_request_times[(cp->last_request_times_off++) %
491 MAX_QUEUE_PER_PEER] =
492 GNUNET_TIME_absolute_get ();
493 GNUNET_assert (0 < cp->ppd.pending_queries--);
495 else if (GNUNET_NO == pth->is_query)
497 GNUNET_assert (0 < cp->ppd.pending_replies--);
499 GNUNET_LOAD_update (cp->ppd.transmission_delay,
500 GNUNET_TIME_absolute_get_duration
501 (pth->transmission_request_start_time).rel_value);
502 ret = pth->gmc (pth->gmc_cls, size, buf);
503 GNUNET_assert (NULL == pth->cth);
504 for (pos = cp->pth_head; pos != NULL; pos = pos->next)
506 GNUNET_assert (pos != pth);
507 schedule_transmission (pos);
509 GNUNET_assert (pth->cth == NULL);
510 GNUNET_assert (pth->cth_in_progress == 0);
517 * (re)try to reserve bandwidth from the given peer.
519 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
520 * @param tc scheduler context
523 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
525 struct GSF_ConnectedPeer *cp = cls;
526 struct GNUNET_PeerIdentity target;
528 GNUNET_PEER_resolve (cp->ppd.pid, &target);
529 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
531 GNUNET_ATS_reserve_bandwidth (ats, &target, DBLOCK_SIZE,
532 &ats_reserve_callback, cp);
537 * Function called by core upon success or failure of our bandwidth reservation request.
539 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
540 * @param peer identifies the peer
541 * @param amount set to the amount that was actually reserved or unreserved;
542 * either the full requested amount or zero (no partial reservations)
543 * @param res_delay if the reservation could not be satisfied (amount was 0), how
544 * long should the client wait until re-trying?
547 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
548 int32_t amount, struct GNUNET_TIME_Relative res_delay)
550 struct GSF_ConnectedPeer *cp = cls;
551 struct GSF_PeerTransmitHandle *pth;
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "Reserved %d bytes / need to wait %llu ms for reservation\n",
555 (int) amount, (unsigned long long) res_delay.rel_value);
560 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
563 cp->did_reserve = GNUNET_YES;
565 if ((NULL != pth) && (NULL == pth->cth))
567 /* reservation success, try transmission now! */
568 pth->cth_in_progress++;
570 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
571 GNUNET_TIME_absolute_get_remaining
572 (pth->timeout), peer, pth->size,
573 &peer_transmit_ready_cb, pth);
574 GNUNET_assert (0 < pth->cth_in_progress--);
580 * A peer connected to us. Setup the connected peer
583 * @param peer identity of peer that connected
584 * @param atsi performance data for the connection
585 * @param atsi_count number of records in 'atsi'
586 * @return handle to connected peer entry
588 struct GSF_ConnectedPeer *
589 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
590 const struct GNUNET_ATS_Information *atsi,
591 unsigned int atsi_count)
593 struct GSF_ConnectedPeer *cp;
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n",
599 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
600 cp->ppd.pid = GNUNET_PEER_intern (peer);
601 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
603 GNUNET_ATS_reserve_bandwidth (ats, peer, DBLOCK_SIZE,
604 &ats_reserve_callback, cp);
605 fn = get_trust_filename (peer);
606 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
607 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
608 cp->disk_trust = cp->ppd.trust = ntohl (trust);
610 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
611 GNUNET_break (GNUNET_OK ==
612 GNUNET_CONTAINER_multihashmap_put (cp_map, &peer->hashPubKey,
614 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
615 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
616 GNUNET_CONTAINER_multihashmap_size (cp_map),
618 update_atsi (cp, atsi, atsi_count);
619 GSF_push_start_ (cp);
625 * It may be time to re-start migrating content to this
626 * peer. Check, and if so, restart migration.
628 * @param cls the 'struct GSF_ConnectedPeer'
629 * @param tc scheduler context
632 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
634 struct GSF_ConnectedPeer *cp = cls;
635 struct GNUNET_TIME_Relative bt;
637 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
638 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
639 if (0 != bt.rel_value)
641 /* still time left... */
642 cp->mig_revive_task =
643 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
646 GSF_push_start_ (cp);
651 * Get a handle for a connected peer.
653 * @param peer peer's identity
654 * @return NULL if the peer is not currently connected
656 struct GSF_ConnectedPeer *
657 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
661 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
666 * Handle P2P "MIGRATION_STOP" message.
668 * @param cls closure, always NULL
669 * @param other the other peer involved (sender or receiver, NULL
670 * for loopback messages where we are both sender and receiver)
671 * @param message the actual message
672 * @param atsi performance information
673 * @param atsi_count number of records in 'atsi'
674 * @return GNUNET_OK to keep the connection open,
675 * GNUNET_SYSERR to close it (signal serious error)
678 GSF_handle_p2p_migration_stop_ (void *cls,
679 const struct GNUNET_PeerIdentity *other,
680 const struct GNUNET_MessageHeader *message,
681 const struct GNUNET_ATS_Information *atsi,
682 unsigned int atsi_count)
684 struct GSF_ConnectedPeer *cp;
685 const struct MigrationStopMessage *msm;
686 struct GNUNET_TIME_Relative bt;
688 msm = (const struct MigrationStopMessage *) message;
689 cp = GSF_peer_get_ (other);
695 GNUNET_STATISTICS_update (GSF_stats,
696 gettext_noop ("# migration stop messages received"),
698 bt = GNUNET_TIME_relative_ntoh (msm->duration);
699 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
700 _("Migration of content to peer `%s' blocked for %llu ms\n"),
701 GNUNET_i2s (other), (unsigned long long) bt.rel_value);
702 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
703 if (cp->mig_revive_task == GNUNET_SCHEDULER_NO_TASK)
706 cp->mig_revive_task =
707 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
709 update_atsi (cp, atsi, atsi_count);
715 * Copy reply and free put message.
717 * @param cls the 'struct PutMessage'
718 * @param buf_size number of bytes available in buf
719 * @param buf where to copy the message, NULL on error (peer disconnect)
720 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
723 copy_reply (void *cls, size_t buf_size, void *buf)
725 struct PutMessage *pm = cls;
730 GNUNET_assert (buf_size >= ntohs (pm->header.size));
731 size = ntohs (pm->header.size);
732 memcpy (buf, pm, size);
733 GNUNET_STATISTICS_update (GSF_stats,
735 ("# replies transmitted to other peers"), 1,
741 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
750 * Cancel all requests associated with the peer.
753 * @param query hash code of the request
754 * @param value the 'struct GSF_PendingRequest'
755 * @return GNUNET_YES (continue to iterate)
758 cancel_pending_request (void *cls, const GNUNET_HashCode * query, void *value)
760 struct PeerRequest *peerreq = value;
761 struct GSF_PendingRequest *pr = peerreq->pr;
762 struct GSF_ConnectedPeer *cp = peerreq->cp;
763 struct GSF_PendingRequestData *prd;
765 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
767 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
768 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
770 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
772 prd = GSF_pending_request_get_data_ (pr);
773 GNUNET_break (GNUNET_YES ==
774 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
775 &prd->query, peerreq));
776 GSF_pending_request_cancel_ (pr, GNUNET_NO);
777 GNUNET_free (peerreq);
783 * Free the given request.
785 * @param cls the request to free
786 * @param tc task context
789 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
791 struct PeerRequest *peerreq = cls;
792 struct GSF_PendingRequest *pr = peerreq->pr;
793 struct GSF_PendingRequestData *prd;
795 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
796 prd = GSF_pending_request_get_data_ (pr);
797 cancel_pending_request (NULL, &prd->query, peerreq);
802 * The artificial delay is over, transmit the message now.
804 * @param cls the 'struct GSF_DelayedHandle' with the message
805 * @param tc scheduler context
808 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
810 struct GSF_DelayedHandle *dh = cls;
811 struct GSF_ConnectedPeer *cp = dh->cp;
813 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
814 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
816 GNUNET_free (dh->pm);
820 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
821 dh->msize, ©_reply, dh->pm);
827 * Get the randomized delay a response should be subjected to.
829 * @return desired delay
831 static struct GNUNET_TIME_Relative
832 get_randomized_delay ()
834 struct GNUNET_TIME_Relative ret;
837 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
838 GNUNET_CRYPTO_random_u32
839 (GNUNET_CRYPTO_QUALITY_WEAK,
840 2 * GSF_avg_latency.rel_value + 1));
841 GNUNET_STATISTICS_update (GSF_stats,
843 ("# artificial delays introduced (ms)"),
844 ret.rel_value, GNUNET_NO);
851 * Handle a reply to a pending request. Also called if a request
852 * expires (then with data == NULL). The handler may be called
853 * many times (depending on the request type), but will not be
854 * called during or after a call to GSF_pending_request_cancel
855 * and will also not be called anymore after a call signalling
858 * @param cls 'struct PeerRequest' this is an answer for
859 * @param eval evaluation of the result
860 * @param pr handle to the original pending request
861 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
862 * @param expiration when does 'data' expire?
863 * @param type type of the block
864 * @param data response data, NULL on request expiration
865 * @param data_len number of bytes in data
868 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
869 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
870 struct GNUNET_TIME_Absolute expiration,
871 enum GNUNET_BLOCK_Type type, const void *data,
874 struct PeerRequest *peerreq = cls;
875 struct GSF_ConnectedPeer *cp = peerreq->cp;
876 struct GSF_PendingRequestData *prd;
877 struct PutMessage *pm;
880 GNUNET_assert (data_len + sizeof (struct PutMessage) <
881 GNUNET_SERVER_MAX_MESSAGE_SIZE);
882 GNUNET_assert (peerreq->pr == pr);
883 prd = GSF_pending_request_get_data_ (pr);
886 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
888 GNUNET_break (GNUNET_YES ==
889 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
890 &prd->query, peerreq));
891 GNUNET_free (peerreq);
894 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
895 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "Transmitting result for query `%s' to peer\n",
903 GNUNET_h2s (&prd->query));
905 GNUNET_STATISTICS_update (GSF_stats,
906 gettext_noop ("# replies received for other peers"),
908 msize = sizeof (struct PutMessage) + data_len;
909 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
914 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level > 1))
916 if (reply_anonymity_level - 1 > GSF_cover_content_count)
918 GNUNET_STATISTICS_update (GSF_stats,
920 ("# replies dropped due to insufficient cover traffic"),
924 GSF_cover_content_count -= (reply_anonymity_level - 1);
927 pm = GNUNET_malloc (msize);
928 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
929 pm->header.size = htons (msize);
930 pm->type = htonl (type);
931 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
932 memcpy (&pm[1], data, data_len);
933 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level != 0) &&
934 (GSF_enable_randomized_delays == GNUNET_YES))
936 struct GSF_DelayedHandle *dh;
938 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
942 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
944 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
945 &transmit_delayed_now, dh);
949 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
952 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
954 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
956 GNUNET_STATISTICS_update (GSF_stats,
958 ("# P2P searches destroyed due to ultimate reply"),
961 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
967 * Increase the host credit by a value.
969 * @param cp which peer to change the trust value on
970 * @param value is the int value by which the
971 * host credit is to be increased or decreased
972 * @returns the actual change in trust (positive or negative)
975 change_host_trust (struct GSF_ConnectedPeer *cp, int value)
979 GNUNET_assert (cp != NULL);
982 if (cp->ppd.trust + value < cp->ppd.trust)
984 value = UINT32_MAX - cp->ppd.trust;
985 cp->ppd.trust = UINT32_MAX;
988 cp->ppd.trust += value;
992 if (cp->ppd.trust < -value)
994 value = -cp->ppd.trust;
998 cp->ppd.trust += value;
1005 * We've received a request with the specified priority. Bound it
1006 * according to how much we trust the given peer.
1008 * @param prio_in requested priority
1009 * @param cp the peer making the request
1010 * @return effective priority
1013 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1015 #define N ((double)128.0)
1020 ld = GSF_test_get_load_too_high_ (0);
1021 if (ld == GNUNET_SYSERR)
1023 GNUNET_STATISTICS_update (GSF_stats,
1025 ("# requests done for free (low load)"), 1,
1027 return 0; /* excess resources */
1029 if (prio_in > INT32_MAX)
1030 prio_in = INT32_MAX;
1031 ret = -change_host_trust (cp, -(int) prio_in);
1034 if (ret > GSF_current_priorities + N)
1035 rret = GSF_current_priorities + N;
1038 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1040 if ((ld == GNUNET_YES) && (ret > 0))
1042 /* try with charging */
1043 ld = GSF_test_get_load_too_high_ (ret);
1045 if (ld == GNUNET_YES)
1047 GNUNET_STATISTICS_update (GSF_stats,
1049 ("# request dropped, priority insufficient"), 1,
1052 change_host_trust (cp, (int) ret);
1053 return -1; /* not enough resources */
1057 GNUNET_STATISTICS_update (GSF_stats,
1059 ("# requests done for a price (normal load)"), 1,
1068 * The priority level imposes a bound on the maximum
1069 * value for the ttl that can be requested.
1071 * @param ttl_in requested ttl
1072 * @param prio given priority
1073 * @return ttl_in if ttl_in is below the limit,
1074 * otherwise the ttl-limit for the given priority
1077 bound_ttl (int32_t ttl_in, uint32_t prio)
1079 unsigned long long allowed;
1083 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1084 if (ttl_in > allowed)
1086 if (allowed >= (1 << 30))
1095 * Handle P2P "QUERY" message. Creates the pending request entry
1096 * and sets up all of the data structures to that we will
1097 * process replies properly. Does not initiate forwarding or
1098 * local database lookups.
1100 * @param other the other peer involved (sender or receiver, NULL
1101 * for loopback messages where we are both sender and receiver)
1102 * @param message the actual message
1103 * @return pending request handle, NULL on error
1105 struct GSF_PendingRequest *
1106 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1107 const struct GNUNET_MessageHeader *message)
1109 struct PeerRequest *peerreq;
1110 struct GSF_PendingRequest *pr;
1111 struct GSF_PendingRequestData *prd;
1112 struct GSF_ConnectedPeer *cp;
1113 struct GSF_ConnectedPeer *cps;
1114 const GNUNET_HashCode *namespace;
1115 const struct GNUNET_PeerIdentity *target;
1116 enum GSF_PendingRequestOptions options;
1118 const struct GetMessage *gm;
1120 const GNUNET_HashCode *opt;
1123 uint32_t ttl_decrement;
1126 enum GNUNET_BLOCK_Type type;
1127 GNUNET_PEER_Id spid;
1129 GNUNET_assert (other != NULL);
1130 msize = ntohs (message->size);
1131 if (msize < sizeof (struct GetMessage))
1133 GNUNET_break_op (0);
1136 GNUNET_STATISTICS_update (GSF_stats,
1138 ("# GET requests received (from other peers)"), 1,
1140 gm = (const struct GetMessage *) message;
1141 type = ntohl (gm->type);
1142 bm = ntohl (gm->hash_bitmap);
1150 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
1152 GNUNET_break_op (0);
1155 opt = (const GNUNET_HashCode *) &gm[1];
1156 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
1157 /* bfsize must be power of 2, check! */
1158 if (0 != ((bfsize - 1) & bfsize))
1160 GNUNET_break_op (0);
1163 GSF_cover_query_count++;
1164 bm = ntohl (gm->hash_bitmap);
1166 cps = GSF_peer_get_ (other);
1169 /* peer must have just disconnected */
1170 GNUNET_STATISTICS_update (GSF_stats,
1172 ("# requests dropped due to initiator not being connected"),
1176 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1177 cp = GSF_peer_get_ ((const struct GNUNET_PeerIdentity *) &opt[bits++]);
1183 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1186 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1192 GNUNET_i2s (other));
1194 GNUNET_STATISTICS_update (GSF_stats,
1196 ("# requests dropped due to missing reverse route"),
1200 /* note that we can really only check load here since otherwise
1201 * peers could find out that we are overloaded by not being
1202 * disconnected after sending us a malformed query... */
1203 priority = bound_priority (ntohl (gm->priority), cps);
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Dropping query from `%s', this peer is too busy.\n",
1209 GNUNET_i2s (other));
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1216 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1219 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1220 if ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace == NULL))
1222 GNUNET_break_op (0);
1225 if ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace != NULL))
1227 GNUNET_break_op (0);
1232 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1233 *) &opt[bits++]) : NULL;
1236 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1237 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1238 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1239 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1241 /* don't have BW to send to peer, or would likely take longer than we have for it,
1242 * so at best indirect the query */
1244 options |= GSF_PRO_FORWARD_ONLY;
1245 spid = GNUNET_PEER_intern (other);
1246 GNUNET_assert (0 != spid);
1248 ttl = bound_ttl (ntohl (gm->ttl), priority);
1249 /* decrement ttl (always) */
1251 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1253 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1258 GNUNET_i2s (other), ttl, ttl_decrement);
1260 GNUNET_STATISTICS_update (GSF_stats,
1262 ("# requests dropped due TTL underflow"), 1,
1264 /* integer underflow => drop (should be very rare)! */
1267 ttl -= ttl_decrement;
1269 /* test if the request already exists */
1270 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1271 if (peerreq != NULL)
1274 prd = GSF_pending_request_get_data_ (pr);
1275 if ((prd->type == type) &&
1276 ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1277 (0 == memcmp (&prd->namespace, namespace, sizeof (GNUNET_HashCode)))))
1279 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1281 /* existing request has higher TTL, drop new one! */
1282 prd->priority += priority;
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "Have existing request with higher TTL, dropping new request.\n",
1286 GNUNET_i2s (other));
1288 GNUNET_STATISTICS_update (GSF_stats,
1290 ("# requests dropped due to higher-TTL request"),
1294 /* existing request has lower TTL, drop old one! */
1295 GNUNET_STATISTICS_update (GSF_stats,
1296 gettext_noop ("# P2P searches active"), -1,
1298 priority += prd->priority;
1299 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1300 GNUNET_assert (GNUNET_YES ==
1301 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
1304 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
1306 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
1307 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
1309 GNUNET_free (peerreq);
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;
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "Timeout trying to transmit to other peer\n");
1356 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1358 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1359 if (GNUNET_YES == pth->is_query)
1360 GNUNET_assert (0 < cp->ppd.pending_queries--);
1361 else if (GNUNET_NO == pth->is_query)
1362 GNUNET_assert (0 < cp->ppd.pending_replies--);
1363 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1364 if (NULL != pth->cth)
1366 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1369 pth->gmc (pth->gmc_cls, 0, NULL);
1370 GNUNET_assert (0 == pth->cth_in_progress);
1376 * Transmit a message to the given peer as soon as possible.
1377 * If the peer disconnects before the transmission can happen,
1378 * the callback is invoked with a 'NULL' buffer.
1380 * @param cp target peer
1381 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1382 * @param priority how important is this request?
1383 * @param timeout when does this request timeout (call gmc with error)
1384 * @param size number of bytes we would like to send to the peer
1385 * @param gmc function to call to get the message
1386 * @param gmc_cls closure for gmc
1387 * @return handle to cancel request
1389 struct GSF_PeerTransmitHandle *
1390 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1391 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1392 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1394 struct GSF_PeerTransmitHandle *pth;
1395 struct GSF_PeerTransmitHandle *pos;
1396 struct GSF_PeerTransmitHandle *prev;
1398 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1399 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1400 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1402 pth->gmc_cls = gmc_cls;
1404 pth->is_query = is_query;
1405 pth->priority = priority;
1407 /* insertion sort (by priority, descending) */
1410 while ((pos != NULL) && (pos->priority > priority))
1416 GNUNET_CONTAINER_DLL_insert (cp->pth_head, cp->pth_tail, pth);
1418 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1419 if (GNUNET_YES == is_query)
1420 cp->ppd.pending_queries++;
1421 else if (GNUNET_NO == is_query)
1422 cp->ppd.pending_replies++;
1424 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1425 schedule_transmission (pth);
1431 * Cancel an earlier request for transmission.
1433 * @param pth request to cancel
1436 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1438 struct GSF_ConnectedPeer *cp;
1440 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1442 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1443 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1445 if (NULL != pth->cth)
1447 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1451 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1452 if (GNUNET_YES == pth->is_query)
1453 GNUNET_assert (0 < cp->ppd.pending_queries--);
1454 else if (GNUNET_NO == pth->is_query)
1455 GNUNET_assert (0 < cp->ppd.pending_replies--);
1456 GNUNET_assert (0 == pth->cth_in_progress);
1462 * Report on receiving a reply; update the performance record of the given peer.
1464 * @param cp responding peer (will be updated)
1465 * @param request_time time at which the original query was transmitted
1466 * @param request_priority priority of the original request
1469 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1470 struct GNUNET_TIME_Absolute request_time,
1471 uint32_t request_priority)
1473 struct GNUNET_TIME_Relative delay;
1475 delay = GNUNET_TIME_absolute_get_duration (request_time);
1476 cp->ppd.avg_reply_delay.rel_value =
1477 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1478 delay.rel_value) / RUNAVG_DELAY_N;
1479 cp->ppd.avg_priority =
1480 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1481 request_priority) / RUNAVG_DELAY_N;
1486 * Report on receiving a reply in response to an initiating client.
1487 * Remember that this peer is good for this client.
1489 * @param cp responding peer (will be updated)
1490 * @param initiator_client local client on responsible for query
1493 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1494 struct GSF_LocalClient *initiator_client)
1496 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1497 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1502 * Report on receiving a reply in response to an initiating peer.
1503 * Remember that this peer is good for this initiating peer.
1505 * @param cp responding peer (will be updated)
1506 * @param initiator_peer other peer responsible for query
1509 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1510 const struct GSF_ConnectedPeer *initiator_peer)
1514 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1515 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1516 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1517 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1518 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1523 * A peer disconnected from us. Tear down the connected peer
1527 * @param peer identity of peer that connected
1530 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1532 struct GSF_ConnectedPeer *cp;
1533 struct GSF_PeerTransmitHandle *pth;
1534 struct GSF_DelayedHandle *dh;
1536 cp = GSF_peer_get_ (peer);
1538 return; /* must have been disconnect from core with
1539 * 'peer' == my_id, ignore */
1540 GNUNET_assert (GNUNET_YES ==
1541 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1542 &peer->hashPubKey, cp));
1543 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
1544 GNUNET_CONTAINER_multihashmap_size (cp_map),
1546 if (NULL != cp->migration_pth)
1548 GSF_peer_transmit_cancel_ (cp->migration_pth);
1549 cp->migration_pth = NULL;
1553 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1556 if (GNUNET_SCHEDULER_NO_TASK != cp->rc_delay_task)
1558 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1559 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1561 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1562 &cancel_pending_request, cp);
1563 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1564 cp->request_map = NULL;
1565 GSF_plan_notify_peer_disconnect_ (cp);
1566 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1567 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1568 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1569 GSF_push_stop_ (cp);
1570 while (NULL != (pth = cp->pth_head))
1572 if (NULL != pth->cth)
1574 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1577 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1579 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1580 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1582 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1583 GNUNET_assert (0 == pth->cth_in_progress);
1584 pth->gmc (pth->gmc_cls, 0, NULL);
1587 while (NULL != (dh = cp->delayed_head))
1589 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1590 GNUNET_SCHEDULER_cancel (dh->delay_task);
1591 GNUNET_free (dh->pm);
1594 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1595 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1597 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1598 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1605 * Closure for 'call_iterator'.
1607 struct IterationContext
1610 * Function to call on each entry.
1612 GSF_ConnectedPeerIterator it;
1622 * Function that calls the callback for each peer.
1624 * @param cls the 'struct IterationContext*'
1625 * @param key identity of the peer
1626 * @param value the 'struct GSF_ConnectedPeer*'
1627 * @return GNUNET_YES to continue iteration
1630 call_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1632 struct IterationContext *ic = cls;
1633 struct GSF_ConnectedPeer *cp = value;
1635 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1641 * Iterate over all connected peers.
1643 * @param it function to call for each peer
1644 * @param it_cls closure for it
1647 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1649 struct IterationContext ic;
1653 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1658 * Obtain the identity of a connected peer.
1660 * @param cp peer to reserve bandwidth from
1661 * @param id identity to set (written to)
1664 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1665 struct GNUNET_PeerIdentity *id)
1667 GNUNET_assert (0 != cp->ppd.pid);
1668 GNUNET_PEER_resolve (cp->ppd.pid, id);
1673 * Assemble a migration stop message for transmission.
1675 * @param cls the 'struct GSF_ConnectedPeer' to use
1676 * @param size number of bytes we're allowed to write to buf
1677 * @param buf where to copy the message
1678 * @return number of bytes copied to buf
1681 create_migration_stop_message (void *cls, size_t size, void *buf)
1683 struct GSF_ConnectedPeer *cp = cls;
1684 struct MigrationStopMessage msm;
1686 cp->migration_pth = NULL;
1689 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1690 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1691 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1692 msm.reserved = htonl (0);
1694 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1695 (cp->last_migration_block));
1696 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1697 return sizeof (struct MigrationStopMessage);
1702 * Ask a peer to stop migrating data to us until the given point
1705 * @param cp peer to ask
1706 * @param block_time until when to block
1709 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1710 struct GNUNET_TIME_Relative block_time)
1712 if (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value >
1713 block_time.rel_value)
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 "Migration already blocked for another %llu ms\n",
1718 (unsigned long long)
1719 GNUNET_TIME_absolute_get_remaining
1720 (cp->last_migration_block).rel_value);
1722 return; /* already blocked */
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1726 (unsigned long long) block_time.rel_value);
1728 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1729 if (cp->migration_pth != NULL)
1730 GSF_peer_transmit_cancel_ (cp->migration_pth);
1732 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1733 GNUNET_TIME_UNIT_FOREVER_REL,
1734 sizeof (struct MigrationStopMessage),
1735 &create_migration_stop_message, cp);
1740 * Write host-trust information to a file - flush the buffer entry!
1742 * @param cls closure, not used
1743 * @param key host identity
1744 * @param value the 'struct GSF_ConnectedPeer' to flush
1745 * @return GNUNET_OK to continue iteration
1748 flush_trust (void *cls, const GNUNET_HashCode * key, void *value)
1750 struct GSF_ConnectedPeer *cp = value;
1753 struct GNUNET_PeerIdentity pid;
1755 if (cp->ppd.trust == cp->disk_trust)
1756 return GNUNET_OK; /* unchanged */
1757 GNUNET_assert (0 != cp->ppd.pid);
1758 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1759 fn = get_trust_filename (&pid);
1760 if (cp->ppd.trust == 0)
1762 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1763 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1764 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1768 trust = htonl (cp->ppd.trust);
1769 if (sizeof (uint32_t) ==
1770 GNUNET_DISK_fn_write (fn, &trust, sizeof (uint32_t),
1771 GNUNET_DISK_PERM_USER_READ |
1772 GNUNET_DISK_PERM_USER_WRITE |
1773 GNUNET_DISK_PERM_GROUP_READ |
1774 GNUNET_DISK_PERM_OTHER_READ))
1775 cp->disk_trust = cp->ppd.trust;
1783 * Notify core about a preference we have for the given peer
1784 * (to allocate more resources towards it). The change will
1785 * be communicated the next time we reserve bandwidth with
1786 * core (not instantly).
1788 * @param cp peer to reserve bandwidth from
1789 * @param pref preference change
1792 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1795 cp->inc_preference += pref;
1800 * Call this method periodically to flush trust information to disk.
1802 * @param cls closure, not used
1803 * @param tc task context, not used
1806 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1811 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_trust, NULL);
1814 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1816 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1821 * Initialize peer management subsystem.
1824 GSF_connected_peer_init_ ()
1826 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1827 ats = GNUNET_ATS_performance_init (GSF_cfg, NULL, NULL);
1828 GNUNET_assert (GNUNET_OK ==
1829 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1832 GNUNET_DISK_directory_create (trustDirectory);
1833 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1834 &cron_flush_trust, NULL);
1839 * Iterator to free peer entries.
1841 * @param cls closure, unused
1842 * @param key current key code
1843 * @param value value in the hash map (peer entry)
1844 * @return GNUNET_YES (we should continue to iterate)
1847 clean_peer (void *cls, const GNUNET_HashCode * key, void *value)
1849 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1855 * Shutdown peer management subsystem.
1858 GSF_connected_peer_done_ ()
1860 cron_flush_trust (NULL, NULL);
1861 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1862 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1864 GNUNET_free (trustDirectory);
1865 trustDirectory = NULL;
1866 GNUNET_ATS_performance_done (ats);
1872 * Iterator to remove references to LC entry.
1874 * @param cls the 'struct GSF_LocalClient*' to look for
1875 * @param key current key code
1876 * @param value value in the hash map (peer entry)
1877 * @return GNUNET_YES (we should continue to iterate)
1880 clean_local_client (void *cls, const GNUNET_HashCode * key, void *value)
1882 const struct GSF_LocalClient *lc = cls;
1883 struct GSF_ConnectedPeer *cp = value;
1886 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1887 if (cp->ppd.last_client_replies[i] == lc)
1888 cp->ppd.last_client_replies[i] = NULL;
1894 * Notification that a local client disconnected. Clean up all of our
1895 * references to the given handle.
1897 * @param lc handle to the local client (henceforth invalid)
1900 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1903 return; /* already cleaned up */
1904 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1909 /* end of gnunet-service-fs_cp.c */