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-service-fs.h"
29 #include "gnunet-service-fs_cp.h"
30 #include "gnunet-service-fs_pe.h"
31 #include "gnunet-service-fs_pr.h"
32 #include "gnunet-service-fs_push.h"
35 * How often do we flush trust values to disk?
37 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
40 * After how long do we discard a reply?
42 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
46 * Handle to cancel a transmission request.
48 struct GSF_PeerTransmitHandle
52 * Kept in a doubly-linked list.
54 struct GSF_PeerTransmitHandle *next;
57 * Kept in a doubly-linked list.
59 struct GSF_PeerTransmitHandle *prev;
62 * Handle for an active request for transmission to this
63 * peer, or NULL (if core queue was full).
65 struct GNUNET_CORE_TransmitHandle *cth;
68 * Time when this transmission request was issued.
70 struct GNUNET_TIME_Absolute transmission_request_start_time;
73 * Timeout for this request.
75 struct GNUNET_TIME_Absolute timeout;
78 * Task called on timeout, or 0 for none.
80 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
83 * Function to call to get the actual message.
85 GSF_GetMessageCallback gmc;
88 * Peer this request targets.
90 struct GSF_ConnectedPeer *cp;
98 * Size of the message to be transmitted.
103 * Set to 1 if we're currently in the process of calling
104 * 'GNUNET_CORE_notify_transmit_ready' (so while cth is
105 * NULL, we should not call notify_transmit_ready for this
108 unsigned int cth_in_progress;
111 * GNUNET_YES if this is a query, GNUNET_NO for content.
116 * Did we get a reservation already?
121 * Priority of this request.
129 * Handle for an entry in our delay list.
131 struct GSF_DelayedHandle
135 * Kept in a doubly-linked list.
137 struct GSF_DelayedHandle *next;
140 * Kept in a doubly-linked list.
142 struct GSF_DelayedHandle *prev;
145 * Peer this transmission belongs to.
147 struct GSF_ConnectedPeer *cp;
150 * The PUT that was delayed.
152 struct PutMessage *pm;
155 * Task for the delay.
157 GNUNET_SCHEDULER_TaskIdentifier delay_task;
160 * Size of the message.
168 * Information per peer and request.
174 * Handle to generic request.
176 struct GSF_PendingRequest *pr;
179 * Handle to specific peer.
181 struct GSF_ConnectedPeer *cp;
184 * Task for asynchronous stopping of this request.
186 GNUNET_SCHEDULER_TaskIdentifier kill_task;
194 struct GSF_ConnectedPeer
198 * Performance data for this peer.
200 struct GSF_PeerPerformanceData ppd;
203 * Time until when we blocked this peer from migrating
206 struct GNUNET_TIME_Absolute last_migration_block;
209 * Task scheduled to revive migration to this peer.
211 GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
214 * Messages (replies, queries, content migration) we would like to
215 * send to this peer in the near future. Sorted by priority, head.
217 struct GSF_PeerTransmitHandle *pth_head;
220 * Messages (replies, queries, content migration) we would like to
221 * send to this peer in the near future. Sorted by priority, tail.
223 struct GSF_PeerTransmitHandle *pth_tail;
226 * Messages (replies, queries, content migration) we would like to
227 * send to this peer in the near future. Sorted by priority, head.
229 struct GSF_DelayedHandle *delayed_head;
232 * Messages (replies, queries, content migration) we would like to
233 * send to this peer in the near future. Sorted by priority, tail.
235 struct GSF_DelayedHandle *delayed_tail;
238 * Migration stop message in our queue, or NULL if we have none pending.
240 struct GSF_PeerTransmitHandle *migration_pth;
243 * Context of our GNUNET_CORE_peer_change_preference call (or NULL).
245 struct GNUNET_CORE_InformationRequestContext *irc;
248 * Task scheduled if we need to retry bandwidth reservation later.
250 GNUNET_SCHEDULER_TaskIdentifier irc_delay_task;
253 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
255 struct GNUNET_CONTAINER_MultiHashMap *request_map;
258 * Increase in traffic preference still to be submitted
259 * to the core service for this peer.
261 uint64_t inc_preference;
264 * Trust rating for this peer on disk.
269 * Which offset in "last_p2p_replies" will be updated next?
270 * (we go round-robin).
272 unsigned int last_p2p_replies_woff;
275 * Which offset in "last_client_replies" will be updated next?
276 * (we go round-robin).
278 unsigned int last_client_replies_woff;
281 * Current offset into 'last_request_times' ring buffer.
283 unsigned int last_request_times_off;
286 * GNUNET_YES if we did successfully reserve 32k bandwidth,
295 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
297 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
300 * Where do we store trust information?
302 static char *trustDirectory;
306 * Get the filename under which we would store the GNUNET_HELLO_Message
307 * for the given host and protocol.
308 * @return filename of the form DIRECTORY/HOSTID
311 get_trust_filename (const struct GNUNET_PeerIdentity *id)
313 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
316 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
317 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
323 * Find latency information in 'atsi'.
325 * @param atsi performance data
326 * @return connection latency
328 static struct GNUNET_TIME_Relative
329 get_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
332 return GNUNET_TIME_UNIT_SECONDS;
333 while ((ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
334 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY))
336 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
339 /* how can we not have latency data? */
340 return GNUNET_TIME_UNIT_SECONDS;
342 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
343 ntohl (atsi->value));
348 * Update the performance information kept for the given peer.
350 * @param cp peer record to update
351 * @param atsi transport performance data
354 update_atsi (struct GSF_ConnectedPeer *cp,
355 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
357 struct GNUNET_TIME_Relative latency;
359 latency = get_latency (atsi);
360 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
361 /* LATER: merge atsi into cp's performance data (if we ever care...) */
366 * Return the performance data record for the given peer
368 * @param cp peer to query
369 * @return performance data record for the peer
371 struct GSF_PeerPerformanceData *
372 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
379 * Core is ready to transmit to a peer, get the message.
381 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
382 * @param size number of bytes core is willing to take
383 * @param buf where to copy the message
384 * @return number of bytes copied to buf
387 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
393 * Function called by core upon success or failure of our bandwidth reservation request.
395 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
396 * @param peer identifies the peer
397 * @param bandwidth_out available amount of outbound bandwidth
398 * @param amount set to the amount that was actually reserved or unreserved;
399 * either the full requested amount or zero (no partial reservations)
400 * @param res_delay if the reservation could not be satisfied (amount was 0), how
401 * long should the client wait until re-trying?
402 * @param preference current traffic preference for the given peer
405 core_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
406 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
407 int32_t amount, struct GNUNET_TIME_Relative res_delay,
408 uint64_t preference);
412 * If ready (bandwidth reserved), try to schedule transmission via
413 * core for the given handle.
415 * @param pth transmission handle to schedule
418 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
420 struct GSF_ConnectedPeer *cp;
421 struct GNUNET_PeerIdentity target;
424 if ((NULL != pth->cth) || (0 != pth->cth_in_progress))
425 return; /* already done */
427 GNUNET_assert (0 != cp->ppd.pid);
428 GNUNET_PEER_resolve (cp->ppd.pid, &target);
429 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
431 /* query, need reservation */
432 if (GNUNET_YES != cp->did_reserve)
433 return; /* not ready */
434 cp->did_reserve = GNUNET_NO;
435 /* reservation already done! */
436 pth->was_reserved = GNUNET_YES;
437 ip = cp->inc_preference;
438 cp->inc_preference = 0;
440 GNUNET_CORE_peer_change_preference (GSF_core, &target,
441 GNUNET_TIME_UNIT_FOREVER_REL,
442 GNUNET_BANDWIDTH_VALUE_MAX,
444 &core_reserve_callback, cp);
446 GNUNET_assert (pth->cth == NULL);
447 pth->cth_in_progress++;
449 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
450 GNUNET_TIME_absolute_get_remaining
451 (pth->timeout), &target, pth->size,
452 &peer_transmit_ready_cb, pth);
453 GNUNET_assert (0 < pth->cth_in_progress--);
458 * Core is ready to transmit to a peer, get the message.
460 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
461 * @param size number of bytes core is willing to take
462 * @param buf where to copy the message
463 * @return number of bytes copied to buf
466 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
468 struct GSF_PeerTransmitHandle *pth = cls;
469 struct GSF_PeerTransmitHandle *pos;
470 struct GSF_ConnectedPeer *cp;
473 GNUNET_assert ((NULL == buf) || (pth->size <= size));
475 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
477 GNUNET_SCHEDULER_cancel (pth->timeout_task);
478 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
481 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
482 if (GNUNET_YES == pth->is_query)
484 cp->ppd.last_request_times[(cp->last_request_times_off++) %
485 MAX_QUEUE_PER_PEER] =
486 GNUNET_TIME_absolute_get ();
487 GNUNET_assert (0 < cp->ppd.pending_queries--);
489 else if (GNUNET_NO == pth->is_query)
491 GNUNET_assert (0 < cp->ppd.pending_replies--);
493 GNUNET_LOAD_update (cp->ppd.transmission_delay,
494 GNUNET_TIME_absolute_get_duration
495 (pth->transmission_request_start_time).rel_value);
496 ret = pth->gmc (pth->gmc_cls, size, buf);
497 GNUNET_assert (NULL == pth->cth);
498 for (pos = cp->pth_head; pos != NULL; pos = pos->next)
500 GNUNET_assert (pos != pth);
501 schedule_transmission (pos);
503 GNUNET_assert (pth->cth == NULL);
504 GNUNET_assert (pth->cth_in_progress == 0);
511 * (re)try to reserve bandwidth from the given peer.
513 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
514 * @param tc scheduler context
517 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
519 struct GSF_ConnectedPeer *cp = cls;
521 struct GNUNET_PeerIdentity target;
523 GNUNET_PEER_resolve (cp->ppd.pid, &target);
524 cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
525 ip = cp->inc_preference;
526 cp->inc_preference = 0;
528 GNUNET_CORE_peer_change_preference (GSF_core, &target,
529 GNUNET_TIME_UNIT_FOREVER_REL,
530 GNUNET_BANDWIDTH_VALUE_MAX,
532 &core_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 bandwidth_out available amount of outbound bandwidth
542 * @param amount set to the amount that was actually reserved or unreserved;
543 * either the full requested amount or zero (no partial reservations)
544 * @param res_delay if the reservation could not be satisfied (amount was 0), how
545 * long should the client wait until re-trying?
546 * @param preference current traffic preference for the given peer
549 core_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
550 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
551 int32_t amount, struct GNUNET_TIME_Relative res_delay,
554 struct GSF_ConnectedPeer *cp = cls;
555 struct GSF_PeerTransmitHandle *pth;
561 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
564 cp->did_reserve = GNUNET_YES;
566 if ((NULL != pth) && (NULL == pth->cth))
568 /* reservation success, try transmission now! */
569 pth->cth_in_progress++;
571 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
572 GNUNET_TIME_absolute_get_remaining
573 (pth->timeout), peer, pth->size,
574 &peer_transmit_ready_cb, pth);
575 GNUNET_assert (0 < pth->cth_in_progress--);
581 * A peer connected to us. Setup the connected peer
584 * @param peer identity of peer that connected
585 * @param atsi performance data for the connection
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_TRANSPORT_ATS_Information *atsi)
592 struct GSF_ConnectedPeer *cp;
596 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
597 cp->ppd.pid = GNUNET_PEER_intern (peer);
598 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
600 GNUNET_CORE_peer_change_preference (GSF_core, peer,
601 GNUNET_TIME_UNIT_FOREVER_REL,
602 GNUNET_BANDWIDTH_VALUE_MAX,
604 &core_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 update_atsi (cp, atsi);
616 GSF_push_start_ (cp);
622 * It may be time to re-start migrating content to this
623 * peer. Check, and if so, restart migration.
625 * @param cls the 'struct GSF_ConnectedPeer'
626 * @param tc scheduler context
629 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
631 struct GSF_ConnectedPeer *cp = cls;
632 struct GNUNET_TIME_Relative bt;
634 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
635 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
636 if (0 != bt.rel_value)
638 /* still time left... */
639 cp->mig_revive_task =
640 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
643 GSF_push_start_ (cp);
648 * Get a handle for a connected peer.
650 * @param peer peer's identity
651 * @return NULL if the peer is not currently connected
653 struct GSF_ConnectedPeer *
654 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
658 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
663 * Handle P2P "MIGRATION_STOP" message.
665 * @param cls closure, always NULL
666 * @param other the other peer involved (sender or receiver, NULL
667 * for loopback messages where we are both sender and receiver)
668 * @param message the actual message
669 * @param atsi performance information
670 * @return GNUNET_OK to keep the connection open,
671 * GNUNET_SYSERR to close it (signal serious error)
674 GSF_handle_p2p_migration_stop_ (void *cls,
675 const struct GNUNET_PeerIdentity *other,
676 const struct GNUNET_MessageHeader *message,
677 const struct GNUNET_TRANSPORT_ATS_Information
680 struct GSF_ConnectedPeer *cp;
681 const struct MigrationStopMessage *msm;
682 struct GNUNET_TIME_Relative bt;
684 msm = (const struct MigrationStopMessage *) message;
685 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &other->hashPubKey);
691 bt = GNUNET_TIME_relative_ntoh (msm->duration);
692 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
693 _("Migration of content to peer `%s' blocked for %llu ms\n"),
694 GNUNET_i2s (other), (unsigned long long) bt.rel_value);
695 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
696 if (cp->mig_revive_task == GNUNET_SCHEDULER_NO_TASK)
699 cp->mig_revive_task =
700 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
702 update_atsi (cp, atsi);
708 * Copy reply and free put message.
710 * @param cls the 'struct PutMessage'
711 * @param buf_size number of bytes available in buf
712 * @param buf where to copy the message, NULL on error (peer disconnect)
713 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
716 copy_reply (void *cls, size_t buf_size, void *buf)
718 struct PutMessage *pm = cls;
723 GNUNET_assert (buf_size >= ntohs (pm->header.size));
724 size = ntohs (pm->header.size);
725 memcpy (buf, pm, size);
726 GNUNET_STATISTICS_update (GSF_stats,
728 ("# replies transmitted to other peers"), 1,
734 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
743 * Cancel all requests associated with the peer.
746 * @param query hash code of the request
747 * @param value the 'struct GSF_PendingRequest'
748 * @return GNUNET_YES (continue to iterate)
751 cancel_pending_request (void *cls, const GNUNET_HashCode * query, void *value)
753 struct PeerRequest *peerreq = value;
754 struct GSF_PendingRequest *pr = peerreq->pr;
755 struct GSF_ConnectedPeer *cp = peerreq->cp;
756 struct GSF_PendingRequestData *prd;
758 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
760 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
761 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
763 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
765 prd = GSF_pending_request_get_data_ (pr);
766 GNUNET_break (GNUNET_YES ==
767 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
768 &prd->query, peerreq));
769 GSF_pending_request_cancel_ (pr, GNUNET_NO);
770 GNUNET_free (peerreq);
776 * Free the given request.
778 * @param cls the request to free
779 * @param tc task context
782 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
784 struct PeerRequest *peerreq = cls;
785 struct GSF_PendingRequest *pr = peerreq->pr;
786 struct GSF_PendingRequestData *prd;
788 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
789 prd = GSF_pending_request_get_data_ (pr);
790 cancel_pending_request (NULL, &prd->query, peerreq);
795 * The artificial delay is over, transmit the message now.
797 * @param cls the 'struct GSF_DelayedHandle' with the message
798 * @param tc scheduler context
801 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
803 struct GSF_DelayedHandle *dh = cls;
804 struct GSF_ConnectedPeer *cp = dh->cp;
806 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
807 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
809 GNUNET_free (dh->pm);
813 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
814 dh->msize, ©_reply, dh->pm);
820 * Get the randomized delay a response should be subjected to.
822 * @return desired delay
824 static struct GNUNET_TIME_Relative
825 get_randomized_delay ()
827 struct GNUNET_TIME_Relative ret;
829 /* FIXME: replace 5000 with something relating to current observed P2P message latency */
831 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
832 GNUNET_CRYPTO_random_u32
833 (GNUNET_CRYPTO_QUALITY_WEAK, 5000));
834 GNUNET_STATISTICS_update (GSF_stats,
836 ("# artificial delays introduced (ms)"),
837 ret.rel_value, GNUNET_NO);
844 * Handle a reply to a pending request. Also called if a request
845 * expires (then with data == NULL). The handler may be called
846 * many times (depending on the request type), but will not be
847 * called during or after a call to GSF_pending_request_cancel
848 * and will also not be called anymore after a call signalling
851 * @param cls 'struct PeerRequest' this is an answer for
852 * @param eval evaluation of the result
853 * @param pr handle to the original pending request
854 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
855 * @param expiration when does 'data' expire?
856 * @param type type of the block
857 * @param data response data, NULL on request expiration
858 * @param data_len number of bytes in data
861 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
862 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
863 struct GNUNET_TIME_Absolute expiration,
864 enum GNUNET_BLOCK_Type type, const void *data,
867 struct PeerRequest *peerreq = cls;
868 struct GSF_ConnectedPeer *cp = peerreq->cp;
869 struct GSF_PendingRequestData *prd;
870 struct PutMessage *pm;
873 GNUNET_assert (data_len + sizeof (struct PutMessage) <
874 GNUNET_SERVER_MAX_MESSAGE_SIZE);
875 GNUNET_assert (peerreq->pr == pr);
876 prd = GSF_pending_request_get_data_ (pr);
879 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
881 GNUNET_break (GNUNET_YES ==
882 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
883 &prd->query, peerreq));
884 GNUNET_free (peerreq);
887 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
888 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Transmitting result for query `%s' to peer\n",
896 GNUNET_h2s (&prd->query));
898 GNUNET_STATISTICS_update (GSF_stats,
899 gettext_noop ("# replies received for other peers"),
901 msize = sizeof (struct PutMessage) + data_len;
902 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
907 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level > 1))
909 if (reply_anonymity_level - 1 > GSF_cover_content_count)
911 GNUNET_STATISTICS_update (GSF_stats,
913 ("# replies dropped due to insufficient cover traffic"),
917 GSF_cover_content_count -= (reply_anonymity_level - 1);
920 pm = GNUNET_malloc (msize);
921 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
922 pm->header.size = htons (msize);
923 pm->type = htonl (type);
924 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
925 memcpy (&pm[1], data, data_len);
926 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level != 0) &&
927 (GSF_enable_randomized_delays == GNUNET_YES))
929 struct GSF_DelayedHandle *dh;
931 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
935 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
937 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
938 &transmit_delayed_now, dh);
942 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
945 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
947 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
949 GNUNET_STATISTICS_update (GSF_stats,
951 ("# P2P searches destroyed due to ultimate reply"),
954 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
960 * Increase the host credit by a value.
962 * @param cp which peer to change the trust value on
963 * @param value is the int value by which the
964 * host credit is to be increased or decreased
965 * @returns the actual change in trust (positive or negative)
968 change_host_trust (struct GSF_ConnectedPeer *cp, int value)
972 GNUNET_assert (cp != NULL);
975 if (cp->ppd.trust + value < cp->ppd.trust)
977 value = UINT32_MAX - cp->ppd.trust;
978 cp->ppd.trust = UINT32_MAX;
981 cp->ppd.trust += value;
985 if (cp->ppd.trust < -value)
987 value = -cp->ppd.trust;
991 cp->ppd.trust += value;
998 * We've received a request with the specified priority. Bound it
999 * according to how much we trust the given peer.
1001 * @param prio_in requested priority
1002 * @param cp the peer making the request
1003 * @return effective priority
1006 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1008 #define N ((double)128.0)
1013 ld = GSF_test_get_load_too_high_ (0);
1014 if (ld == GNUNET_SYSERR)
1016 GNUNET_STATISTICS_update (GSF_stats,
1018 ("# requests done for free (low load)"), 1,
1020 return 0; /* excess resources */
1022 if (prio_in > INT32_MAX)
1023 prio_in = INT32_MAX;
1024 ret = -change_host_trust (cp, -(int) prio_in);
1027 if (ret > GSF_current_priorities + N)
1028 rret = GSF_current_priorities + N;
1031 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1033 if ((ld == GNUNET_YES) && (ret > 0))
1035 /* try with charging */
1036 ld = GSF_test_get_load_too_high_ (ret);
1038 if (ld == GNUNET_YES)
1040 GNUNET_STATISTICS_update (GSF_stats,
1042 ("# request dropped, priority insufficient"), 1,
1045 change_host_trust (cp, (int) ret);
1046 return -1; /* not enough resources */
1050 GNUNET_STATISTICS_update (GSF_stats,
1052 ("# requests done for a price (normal load)"), 1,
1061 * The priority level imposes a bound on the maximum
1062 * value for the ttl that can be requested.
1064 * @param ttl_in requested ttl
1065 * @param prio given priority
1066 * @return ttl_in if ttl_in is below the limit,
1067 * otherwise the ttl-limit for the given priority
1070 bound_ttl (int32_t ttl_in, uint32_t prio)
1072 unsigned long long allowed;
1076 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1077 if (ttl_in > allowed)
1079 if (allowed >= (1 << 30))
1088 * Handle P2P "QUERY" message. Creates the pending request entry
1089 * and sets up all of the data structures to that we will
1090 * process replies properly. Does not initiate forwarding or
1091 * local database lookups.
1093 * @param other the other peer involved (sender or receiver, NULL
1094 * for loopback messages where we are both sender and receiver)
1095 * @param message the actual message
1096 * @return pending request handle, NULL on error
1098 struct GSF_PendingRequest *
1099 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1100 const struct GNUNET_MessageHeader *message)
1102 struct PeerRequest *peerreq;
1103 struct GSF_PendingRequest *pr;
1104 struct GSF_PendingRequestData *prd;
1105 struct GSF_ConnectedPeer *cp;
1106 struct GSF_ConnectedPeer *cps;
1107 const GNUNET_HashCode *namespace;
1108 const struct GNUNET_PeerIdentity *target;
1109 enum GSF_PendingRequestOptions options;
1111 const struct GetMessage *gm;
1113 const GNUNET_HashCode *opt;
1116 uint32_t ttl_decrement;
1119 enum GNUNET_BLOCK_Type type;
1120 GNUNET_PEER_Id spid;
1122 GNUNET_assert (other != NULL);
1123 msize = ntohs (message->size);
1124 if (msize < sizeof (struct GetMessage))
1126 GNUNET_break_op (0);
1129 GNUNET_STATISTICS_update (GSF_stats,
1131 ("# GET requests received (from other peers)"), 1,
1133 gm = (const struct GetMessage *) message;
1134 type = ntohl (gm->type);
1135 bm = ntohl (gm->hash_bitmap);
1143 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
1145 GNUNET_break_op (0);
1148 opt = (const GNUNET_HashCode *) &gm[1];
1149 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
1150 /* bfsize must be power of 2, check! */
1151 if (0 != ((bfsize - 1) & bfsize))
1153 GNUNET_break_op (0);
1156 GSF_cover_query_count++;
1157 bm = ntohl (gm->hash_bitmap);
1159 cps = GNUNET_CONTAINER_multihashmap_get (cp_map, &other->hashPubKey);
1162 /* peer must have just disconnected */
1163 GNUNET_STATISTICS_update (GSF_stats,
1165 ("# requests dropped due to initiator not being connected"),
1169 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1170 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &opt[bits++]);
1176 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1179 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1185 GNUNET_i2s (other));
1187 GNUNET_STATISTICS_update (GSF_stats,
1189 ("# requests dropped due to missing reverse route"),
1193 /* note that we can really only check load here since otherwise
1194 * peers could find out that we are overloaded by not being
1195 * disconnected after sending us a malformed query... */
1196 priority = bound_priority (ntohl (gm->priority), cps);
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Dropping query from `%s', this peer is too busy.\n",
1202 GNUNET_i2s (other));
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1209 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1212 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1213 if ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace == NULL))
1215 GNUNET_break_op (0);
1218 if ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace != NULL))
1220 GNUNET_break_op (0);
1225 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1226 *) &opt[bits++]) : NULL;
1229 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1230 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1231 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1232 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1234 /* don't have BW to send to peer, or would likely take longer than we have for it,
1235 * so at best indirect the query */
1237 options |= GSF_PRO_FORWARD_ONLY;
1238 spid = GNUNET_PEER_intern (other);
1239 GNUNET_assert (0 != spid);
1241 ttl = bound_ttl (ntohl (gm->ttl), priority);
1242 /* decrement ttl (always) */
1244 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1246 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1251 GNUNET_i2s (other), ttl, ttl_decrement);
1253 GNUNET_STATISTICS_update (GSF_stats,
1255 ("# requests dropped due TTL underflow"), 1,
1257 /* integer underflow => drop (should be very rare)! */
1260 ttl -= ttl_decrement;
1262 /* test if the request already exists */
1263 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1264 if (peerreq != NULL)
1267 prd = GSF_pending_request_get_data_ (pr);
1268 if ((prd->type == type) &&
1269 ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1270 (0 == memcmp (&prd->namespace, namespace, sizeof (GNUNET_HashCode)))))
1272 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1274 /* existing request has higher TTL, drop new one! */
1275 prd->priority += priority;
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278 "Have existing request with higher TTL, dropping new request.\n",
1279 GNUNET_i2s (other));
1281 GNUNET_STATISTICS_update (GSF_stats,
1283 ("# requests dropped due to higher-TTL request"),
1287 /* existing request has lower TTL, drop old one! */
1288 GNUNET_STATISTICS_update (GSF_stats,
1289 gettext_noop ("# P2P searches active"), -1,
1291 priority += prd->priority;
1292 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1293 GNUNET_assert (GNUNET_YES ==
1294 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
1297 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
1299 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
1300 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
1302 GNUNET_free (peerreq);
1306 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1308 pr = GSF_pending_request_create_ (options, type, &gm->query, namespace,
1311 0) ? (const char *) &opt[bits] : NULL,
1312 bfsize, ntohl (gm->filter_mutator),
1314 (uint32_t) priority, ttl, spid, NULL, 0, /* replies_seen */
1315 &handle_p2p_reply, peerreq);
1316 GNUNET_assert (NULL != pr);
1318 GNUNET_break (GNUNET_OK ==
1319 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1321 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1322 GNUNET_STATISTICS_update (GSF_stats,
1324 ("# P2P query messages received and processed"), 1,
1326 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1333 * Function called if there has been a timeout trying to satisfy
1334 * a transmission request.
1336 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1337 * @param tc scheduler context
1340 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1342 struct GSF_PeerTransmitHandle *pth = cls;
1343 struct GSF_ConnectedPeer *cp;
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "Timeout trying to transmit to other peer\n");
1349 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1351 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1352 if (GNUNET_YES == pth->is_query)
1353 GNUNET_assert (0 < cp->ppd.pending_queries--);
1354 else if (GNUNET_NO == pth->is_query)
1355 GNUNET_assert (0 < cp->ppd.pending_replies--);
1356 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1357 if (NULL != pth->cth)
1359 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1362 pth->gmc (pth->gmc_cls, 0, NULL);
1363 GNUNET_assert (0 == pth->cth_in_progress);
1369 * Transmit a message to the given peer as soon as possible.
1370 * If the peer disconnects before the transmission can happen,
1371 * the callback is invoked with a 'NULL' buffer.
1373 * @param cp target peer
1374 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1375 * @param priority how important is this request?
1376 * @param timeout when does this request timeout (call gmc with error)
1377 * @param size number of bytes we would like to send to the peer
1378 * @param gmc function to call to get the message
1379 * @param gmc_cls closure for gmc
1380 * @return handle to cancel request
1382 struct GSF_PeerTransmitHandle *
1383 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1384 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1385 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1387 struct GSF_PeerTransmitHandle *pth;
1388 struct GSF_PeerTransmitHandle *pos;
1389 struct GSF_PeerTransmitHandle *prev;
1391 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1392 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1393 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1395 pth->gmc_cls = gmc_cls;
1397 pth->is_query = is_query;
1398 pth->priority = priority;
1400 /* insertion sort (by priority, descending) */
1403 while ((pos != NULL) && (pos->priority > priority))
1409 GNUNET_CONTAINER_DLL_insert (cp->pth_head, cp->pth_tail, pth);
1411 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1412 if (GNUNET_YES == is_query)
1413 cp->ppd.pending_queries++;
1414 else if (GNUNET_NO == is_query)
1415 cp->ppd.pending_replies++;
1417 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1418 schedule_transmission (pth);
1424 * Cancel an earlier request for transmission.
1426 * @param pth request to cancel
1429 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1431 struct GSF_ConnectedPeer *cp;
1433 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1435 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1436 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1438 if (NULL != pth->cth)
1440 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1444 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1445 if (GNUNET_YES == pth->is_query)
1446 GNUNET_assert (0 < cp->ppd.pending_queries--);
1447 else if (GNUNET_NO == pth->is_query)
1448 GNUNET_assert (0 < cp->ppd.pending_replies--);
1449 GNUNET_assert (0 == pth->cth_in_progress);
1455 * Report on receiving a reply; update the performance record of the given peer.
1457 * @param cp responding peer (will be updated)
1458 * @param request_time time at which the original query was transmitted
1459 * @param request_priority priority of the original request
1462 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1463 struct GNUNET_TIME_Absolute request_time,
1464 uint32_t request_priority)
1466 struct GNUNET_TIME_Relative delay;
1468 delay = GNUNET_TIME_absolute_get_duration (request_time);
1469 cp->ppd.avg_reply_delay.rel_value =
1470 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1471 delay.rel_value) / RUNAVG_DELAY_N;
1472 cp->ppd.avg_priority =
1473 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1474 request_priority) / RUNAVG_DELAY_N;
1479 * Report on receiving a reply in response to an initiating client.
1480 * Remember that this peer is good for this client.
1482 * @param cp responding peer (will be updated)
1483 * @param initiator_client local client on responsible for query
1486 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1487 struct GSF_LocalClient *initiator_client)
1489 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1490 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1495 * Report on receiving a reply in response to an initiating peer.
1496 * Remember that this peer is good for this initiating peer.
1498 * @param cp responding peer (will be updated)
1499 * @param initiator_peer other peer responsible for query
1502 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1503 const struct GSF_ConnectedPeer *initiator_peer)
1507 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1508 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1509 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1510 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1511 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1516 * Method called whenever a given peer has a status change.
1518 * @param cls closure
1519 * @param peer peer identity this notification is about
1520 * @param bandwidth_in available amount of inbound bandwidth
1521 * @param bandwidth_out available amount of outbound bandwidth
1522 * @param timeout absolute time when this peer will time out
1523 * unless we see some further activity from it
1524 * @param atsi status information
1527 GSF_peer_status_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer,
1528 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1529 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1530 struct GNUNET_TIME_Absolute timeout,
1531 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1533 struct GSF_ConnectedPeer *cp;
1535 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
1536 GNUNET_assert (NULL != cp);
1537 update_atsi (cp, atsi);
1542 * A peer disconnected from us. Tear down the connected peer
1546 * @param peer identity of peer that connected
1549 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1551 struct GSF_ConnectedPeer *cp;
1552 struct GSF_PeerTransmitHandle *pth;
1553 struct GSF_DelayedHandle *dh;
1555 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
1557 return; /* must have been disconnect from core with
1558 * 'peer' == my_id, ignore */
1559 GNUNET_assert (GNUNET_YES ==
1560 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1561 &peer->hashPubKey, cp));
1562 if (NULL != cp->migration_pth)
1564 GSF_peer_transmit_cancel_ (cp->migration_pth);
1565 cp->migration_pth = NULL;
1567 if (NULL != cp->irc)
1569 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1572 if (GNUNET_SCHEDULER_NO_TASK != cp->irc_delay_task)
1574 GNUNET_SCHEDULER_cancel (cp->irc_delay_task);
1575 cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1577 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1578 &cancel_pending_request, cp);
1579 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1580 cp->request_map = NULL;
1581 GSF_plan_notify_peer_disconnect_ (cp);
1582 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1583 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1584 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1585 GSF_push_stop_ (cp);
1586 while (NULL != (pth = cp->pth_head))
1588 if (NULL != pth->cth)
1590 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1593 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1595 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1596 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1598 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1599 GNUNET_assert (0 == pth->cth_in_progress);
1600 pth->gmc (pth->gmc_cls, 0, NULL);
1603 while (NULL != (dh = cp->delayed_head))
1605 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1606 GNUNET_SCHEDULER_cancel (dh->delay_task);
1607 GNUNET_free (dh->pm);
1610 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1611 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1613 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1614 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1621 * Closure for 'call_iterator'.
1623 struct IterationContext
1626 * Function to call on each entry.
1628 GSF_ConnectedPeerIterator it;
1638 * Function that calls the callback for each peer.
1640 * @param cls the 'struct IterationContext*'
1641 * @param key identity of the peer
1642 * @param value the 'struct GSF_ConnectedPeer*'
1643 * @return GNUNET_YES to continue iteration
1646 call_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1648 struct IterationContext *ic = cls;
1649 struct GSF_ConnectedPeer *cp = value;
1651 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1657 * Iterate over all connected peers.
1659 * @param it function to call for each peer
1660 * @param it_cls closure for it
1663 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1665 struct IterationContext ic;
1669 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1674 * Obtain the identity of a connected peer.
1676 * @param cp peer to reserve bandwidth from
1677 * @param id identity to set (written to)
1680 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1681 struct GNUNET_PeerIdentity *id)
1683 GNUNET_assert (0 != cp->ppd.pid);
1684 GNUNET_PEER_resolve (cp->ppd.pid, id);
1689 * Assemble a migration stop message for transmission.
1691 * @param cls the 'struct GSF_ConnectedPeer' to use
1692 * @param size number of bytes we're allowed to write to buf
1693 * @param buf where to copy the message
1694 * @return number of bytes copied to buf
1697 create_migration_stop_message (void *cls, size_t size, void *buf)
1699 struct GSF_ConnectedPeer *cp = cls;
1700 struct MigrationStopMessage msm;
1702 cp->migration_pth = NULL;
1705 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1706 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1707 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1708 msm.reserved = htonl (0);
1710 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1711 (cp->last_migration_block));
1712 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1713 return sizeof (struct MigrationStopMessage);
1718 * Ask a peer to stop migrating data to us until the given point
1721 * @param cp peer to ask
1722 * @param block_time until when to block
1725 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1726 struct GNUNET_TIME_Relative block_time)
1728 if (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value >
1729 block_time.rel_value)
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "Migration already blocked for another %llu ms\n",
1734 (unsigned long long)
1735 GNUNET_TIME_absolute_get_remaining
1736 (cp->last_migration_block).rel_value);
1738 return; /* already blocked */
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1742 (unsigned long long) block_time.rel_value);
1744 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1745 if (cp->migration_pth != NULL)
1746 GSF_peer_transmit_cancel_ (cp->migration_pth);
1748 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1749 GNUNET_TIME_UNIT_FOREVER_REL,
1750 sizeof (struct MigrationStopMessage),
1751 &create_migration_stop_message, cp);
1756 * Write host-trust information to a file - flush the buffer entry!
1758 * @param cls closure, not used
1759 * @param key host identity
1760 * @param value the 'struct GSF_ConnectedPeer' to flush
1761 * @return GNUNET_OK to continue iteration
1764 flush_trust (void *cls, const GNUNET_HashCode * key, void *value)
1766 struct GSF_ConnectedPeer *cp = value;
1769 struct GNUNET_PeerIdentity pid;
1771 if (cp->ppd.trust == cp->disk_trust)
1772 return GNUNET_OK; /* unchanged */
1773 GNUNET_assert (0 != cp->ppd.pid);
1774 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1775 fn = get_trust_filename (&pid);
1776 if (cp->ppd.trust == 0)
1778 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1779 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1780 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1784 trust = htonl (cp->ppd.trust);
1785 if (sizeof (uint32_t) ==
1786 GNUNET_DISK_fn_write (fn, &trust, sizeof (uint32_t),
1787 GNUNET_DISK_PERM_USER_READ |
1788 GNUNET_DISK_PERM_USER_WRITE |
1789 GNUNET_DISK_PERM_GROUP_READ |
1790 GNUNET_DISK_PERM_OTHER_READ))
1791 cp->disk_trust = cp->ppd.trust;
1799 * Notify core about a preference we have for the given peer
1800 * (to allocate more resources towards it). The change will
1801 * be communicated the next time we reserve bandwidth with
1802 * core (not instantly).
1804 * @param cp peer to reserve bandwidth from
1805 * @param pref preference change
1808 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1811 cp->inc_preference += pref;
1816 * Call this method periodically to flush trust information to disk.
1818 * @param cls closure, not used
1819 * @param tc task context, not used
1822 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1827 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_trust, NULL);
1830 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1832 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1837 * Initialize peer management subsystem.
1840 GSF_connected_peer_init_ ()
1842 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1843 GNUNET_assert (GNUNET_OK ==
1844 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1847 GNUNET_DISK_directory_create (trustDirectory);
1848 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1849 &cron_flush_trust, NULL);
1854 * Iterator to free peer entries.
1856 * @param cls closure, unused
1857 * @param key current key code
1858 * @param value value in the hash map (peer entry)
1859 * @return GNUNET_YES (we should continue to iterate)
1862 clean_peer (void *cls, const GNUNET_HashCode * key, void *value)
1864 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1870 * Shutdown peer management subsystem.
1873 GSF_connected_peer_done_ ()
1875 cron_flush_trust (NULL, NULL);
1876 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1877 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1879 GNUNET_free (trustDirectory);
1880 trustDirectory = NULL;
1885 * Iterator to remove references to LC entry.
1887 * @param cls the 'struct GSF_LocalClient*' to look for
1888 * @param key current key code
1889 * @param value value in the hash map (peer entry)
1890 * @return GNUNET_YES (we should continue to iterate)
1893 clean_local_client (void *cls, const GNUNET_HashCode * key, void *value)
1895 const struct GSF_LocalClient *lc = cls;
1896 struct GSF_ConnectedPeer *cp = value;
1899 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1900 if (cp->ppd.last_client_replies[i] == lc)
1901 cp->ppd.last_client_replies[i] = NULL;
1907 * Notification that a local client disconnected. Clean up all of our
1908 * references to the given handle.
1910 * @param lc handle to the local client (henceforth invalid)
1913 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1916 return; /* already cleaned up */
1917 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1922 /* end of gnunet-service-fs_cp.c */