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)
344 /* how can we not have latency data? */
345 return GNUNET_TIME_UNIT_SECONDS;
347 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
348 ntohl (atsi->value));
353 * Update the performance information kept for the given peer.
355 * @param cp peer record to update
356 * @param atsi transport performance data
359 update_atsi (struct GSF_ConnectedPeer *cp,
360 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
362 struct GNUNET_TIME_Relative latency;
364 latency = get_latency (atsi);
365 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
366 /* LATER: merge atsi into cp's performance data (if we ever care...) */
371 * Return the performance data record for the given peer
373 * @param cp peer to query
374 * @return performance data record for the peer
376 struct GSF_PeerPerformanceData *
377 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
384 * Core is ready to transmit to a peer, get the message.
386 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
387 * @param size number of bytes core is willing to take
388 * @param buf where to copy the message
389 * @return number of bytes copied to buf
392 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
398 * Function called by core upon success or failure of our bandwidth reservation request.
400 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
401 * @param peer identifies the peer
402 * @param bandwidth_out available amount of outbound bandwidth
403 * @param amount set to the amount that was actually reserved or unreserved;
404 * either the full requested amount or zero (no partial reservations)
405 * @param res_delay if the reservation could not be satisfied (amount was 0), how
406 * long should the client wait until re-trying?
407 * @param preference current traffic preference for the given peer
410 core_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
411 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
412 int32_t amount, struct GNUNET_TIME_Relative res_delay,
413 uint64_t preference);
417 * If ready (bandwidth reserved), try to schedule transmission via
418 * core for the given handle.
420 * @param pth transmission handle to schedule
423 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
425 struct GSF_ConnectedPeer *cp;
426 struct GNUNET_PeerIdentity target;
429 if ((NULL != pth->cth) || (0 != pth->cth_in_progress))
430 return; /* already done */
432 GNUNET_assert (0 != cp->ppd.pid);
433 GNUNET_PEER_resolve (cp->ppd.pid, &target);
434 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
436 /* query, need reservation */
437 if (GNUNET_YES != cp->did_reserve)
438 return; /* not ready */
439 cp->did_reserve = GNUNET_NO;
440 /* reservation already done! */
441 pth->was_reserved = GNUNET_YES;
442 ip = cp->inc_preference;
443 cp->inc_preference = 0;
445 GNUNET_CORE_peer_change_preference (GSF_core, &target,
446 GNUNET_TIME_UNIT_FOREVER_REL,
447 GNUNET_BANDWIDTH_VALUE_MAX,
449 &core_reserve_callback, cp);
451 GNUNET_assert (pth->cth == NULL);
452 pth->cth_in_progress++;
454 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
455 GNUNET_TIME_absolute_get_remaining
456 (pth->timeout), &target, pth->size,
457 &peer_transmit_ready_cb, pth);
458 GNUNET_assert (0 < pth->cth_in_progress--);
463 * Core is ready to transmit to a peer, get the message.
465 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
466 * @param size number of bytes core is willing to take
467 * @param buf where to copy the message
468 * @return number of bytes copied to buf
471 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
473 struct GSF_PeerTransmitHandle *pth = cls;
474 struct GSF_PeerTransmitHandle *pos;
475 struct GSF_ConnectedPeer *cp;
478 GNUNET_assert ((NULL == buf) || (pth->size <= size));
480 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
482 GNUNET_SCHEDULER_cancel (pth->timeout_task);
483 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
486 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
487 if (GNUNET_YES == pth->is_query)
489 cp->ppd.last_request_times[(cp->last_request_times_off++) %
490 MAX_QUEUE_PER_PEER] =
491 GNUNET_TIME_absolute_get ();
492 GNUNET_assert (0 < cp->ppd.pending_queries--);
494 else if (GNUNET_NO == pth->is_query)
496 GNUNET_assert (0 < cp->ppd.pending_replies--);
498 GNUNET_LOAD_update (cp->ppd.transmission_delay,
499 GNUNET_TIME_absolute_get_duration
500 (pth->transmission_request_start_time).rel_value);
501 ret = pth->gmc (pth->gmc_cls, size, buf);
502 GNUNET_assert (NULL == pth->cth);
503 for (pos = cp->pth_head; pos != NULL; pos = pos->next)
505 GNUNET_assert (pos != pth);
506 schedule_transmission (pos);
508 GNUNET_assert (pth->cth == NULL);
509 GNUNET_assert (pth->cth_in_progress == 0);
516 * (re)try to reserve bandwidth from the given peer.
518 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
519 * @param tc scheduler context
522 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
524 struct GSF_ConnectedPeer *cp = cls;
526 struct GNUNET_PeerIdentity target;
528 GNUNET_PEER_resolve (cp->ppd.pid, &target);
529 cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
530 ip = cp->inc_preference;
531 cp->inc_preference = 0;
533 GNUNET_CORE_peer_change_preference (GSF_core, &target,
534 GNUNET_TIME_UNIT_FOREVER_REL,
535 GNUNET_BANDWIDTH_VALUE_MAX,
537 &core_reserve_callback, cp);
542 * Function called by core upon success or failure of our bandwidth reservation request.
544 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
545 * @param peer identifies the peer
546 * @param bandwidth_out available amount of outbound bandwidth
547 * @param amount set to the amount that was actually reserved or unreserved;
548 * either the full requested amount or zero (no partial reservations)
549 * @param res_delay if the reservation could not be satisfied (amount was 0), how
550 * long should the client wait until re-trying?
551 * @param preference current traffic preference for the given peer
554 core_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
555 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
556 int32_t amount, struct GNUNET_TIME_Relative res_delay,
559 struct GSF_ConnectedPeer *cp = cls;
560 struct GSF_PeerTransmitHandle *pth;
566 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
569 cp->did_reserve = GNUNET_YES;
571 if ((NULL != pth) && (NULL == pth->cth))
573 /* reservation success, try transmission now! */
574 pth->cth_in_progress++;
576 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
577 GNUNET_TIME_absolute_get_remaining
578 (pth->timeout), peer, pth->size,
579 &peer_transmit_ready_cb, pth);
580 GNUNET_assert (0 < pth->cth_in_progress--);
586 * A peer connected to us. Setup the connected peer
589 * @param peer identity of peer that connected
590 * @param atsi performance data for the connection
591 * @return handle to connected peer entry
593 struct GSF_ConnectedPeer *
594 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
595 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
597 struct GSF_ConnectedPeer *cp;
601 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
602 cp->ppd.pid = GNUNET_PEER_intern (peer);
603 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
605 GNUNET_CORE_peer_change_preference (GSF_core, peer,
606 GNUNET_TIME_UNIT_FOREVER_REL,
607 GNUNET_BANDWIDTH_VALUE_MAX,
609 &core_reserve_callback, cp);
610 fn = get_trust_filename (peer);
611 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
612 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
613 cp->disk_trust = cp->ppd.trust = ntohl (trust);
615 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
616 GNUNET_break (GNUNET_OK ==
617 GNUNET_CONTAINER_multihashmap_put (cp_map, &peer->hashPubKey,
619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
620 update_atsi (cp, atsi);
621 GSF_push_start_ (cp);
627 * It may be time to re-start migrating content to this
628 * peer. Check, and if so, restart migration.
630 * @param cls the 'struct GSF_ConnectedPeer'
631 * @param tc scheduler context
634 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
636 struct GSF_ConnectedPeer *cp = cls;
637 struct GNUNET_TIME_Relative bt;
639 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
640 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
641 if (0 != bt.rel_value)
643 /* still time left... */
644 cp->mig_revive_task =
645 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
648 GSF_push_start_ (cp);
653 * Get a handle for a connected peer.
655 * @param peer peer's identity
656 * @return NULL if the peer is not currently connected
658 struct GSF_ConnectedPeer *
659 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
663 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
668 * Handle P2P "MIGRATION_STOP" message.
670 * @param cls closure, always NULL
671 * @param other the other peer involved (sender or receiver, NULL
672 * for loopback messages where we are both sender and receiver)
673 * @param message the actual message
674 * @param atsi performance information
675 * @return GNUNET_OK to keep the connection open,
676 * GNUNET_SYSERR to close it (signal serious error)
679 GSF_handle_p2p_migration_stop_ (void *cls,
680 const struct GNUNET_PeerIdentity *other,
681 const struct GNUNET_MessageHeader *message,
682 const struct GNUNET_TRANSPORT_ATS_Information
685 struct GSF_ConnectedPeer *cp;
686 const struct MigrationStopMessage *msm;
687 struct GNUNET_TIME_Relative bt;
689 msm = (const struct MigrationStopMessage *) message;
690 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &other->hashPubKey);
696 bt = GNUNET_TIME_relative_ntoh (msm->duration);
697 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
698 _("Migration of content to peer `%s' blocked for %llu ms\n"),
699 GNUNET_i2s (other), (unsigned long long) bt.rel_value);
700 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
701 if (cp->mig_revive_task == GNUNET_SCHEDULER_NO_TASK)
704 cp->mig_revive_task =
705 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
707 update_atsi (cp, atsi);
713 * Copy reply and free put message.
715 * @param cls the 'struct PutMessage'
716 * @param buf_size number of bytes available in buf
717 * @param buf where to copy the message, NULL on error (peer disconnect)
718 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
721 copy_reply (void *cls, size_t buf_size, void *buf)
723 struct PutMessage *pm = cls;
728 GNUNET_assert (buf_size >= ntohs (pm->header.size));
729 size = ntohs (pm->header.size);
730 memcpy (buf, pm, size);
731 GNUNET_STATISTICS_update (GSF_stats,
733 ("# replies transmitted to other peers"), 1,
739 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
748 * Cancel all requests associated with the peer.
751 * @param query hash code of the request
752 * @param value the 'struct GSF_PendingRequest'
753 * @return GNUNET_YES (continue to iterate)
756 cancel_pending_request (void *cls, const GNUNET_HashCode * query, void *value)
758 struct PeerRequest *peerreq = value;
759 struct GSF_PendingRequest *pr = peerreq->pr;
760 struct GSF_ConnectedPeer *cp = peerreq->cp;
761 struct GSF_PendingRequestData *prd;
763 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
765 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
766 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
768 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
770 prd = GSF_pending_request_get_data_ (pr);
771 GNUNET_break (GNUNET_YES ==
772 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
773 &prd->query, peerreq));
774 GSF_pending_request_cancel_ (pr, GNUNET_NO);
775 GNUNET_free (peerreq);
781 * Free the given request.
783 * @param cls the request to free
784 * @param tc task context
787 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
789 struct PeerRequest *peerreq = cls;
790 struct GSF_PendingRequest *pr = peerreq->pr;
791 struct GSF_PendingRequestData *prd;
793 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
794 prd = GSF_pending_request_get_data_ (pr);
795 cancel_pending_request (NULL, &prd->query, peerreq);
800 * The artificial delay is over, transmit the message now.
802 * @param cls the 'struct GSF_DelayedHandle' with the message
803 * @param tc scheduler context
806 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
808 struct GSF_DelayedHandle *dh = cls;
809 struct GSF_ConnectedPeer *cp = dh->cp;
811 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
812 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
814 GNUNET_free (dh->pm);
818 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
819 dh->msize, ©_reply, dh->pm);
825 * Get the randomized delay a response should be subjected to.
827 * @return desired delay
829 static struct GNUNET_TIME_Relative
830 get_randomized_delay ()
832 struct GNUNET_TIME_Relative ret;
834 /* FIXME: replace 5000 with something relating to current observed P2P message latency */
836 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
837 GNUNET_CRYPTO_random_u32
838 (GNUNET_CRYPTO_QUALITY_WEAK, 5000));
839 GNUNET_STATISTICS_update (GSF_stats,
841 ("# artificial delays introduced (ms)"),
842 ret.rel_value, GNUNET_NO);
849 * Handle a reply to a pending request. Also called if a request
850 * expires (then with data == NULL). The handler may be called
851 * many times (depending on the request type), but will not be
852 * called during or after a call to GSF_pending_request_cancel
853 * and will also not be called anymore after a call signalling
856 * @param cls 'struct PeerRequest' this is an answer for
857 * @param eval evaluation of the result
858 * @param pr handle to the original pending request
859 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
860 * @param expiration when does 'data' expire?
861 * @param type type of the block
862 * @param data response data, NULL on request expiration
863 * @param data_len number of bytes in data
866 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
867 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
868 struct GNUNET_TIME_Absolute expiration,
869 enum GNUNET_BLOCK_Type type, const void *data,
872 struct PeerRequest *peerreq = cls;
873 struct GSF_ConnectedPeer *cp = peerreq->cp;
874 struct GSF_PendingRequestData *prd;
875 struct PutMessage *pm;
878 GNUNET_assert (data_len + sizeof (struct PutMessage) <
879 GNUNET_SERVER_MAX_MESSAGE_SIZE);
880 GNUNET_assert (peerreq->pr == pr);
881 prd = GSF_pending_request_get_data_ (pr);
884 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
886 GNUNET_break (GNUNET_YES ==
887 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
888 &prd->query, peerreq));
889 GNUNET_free (peerreq);
892 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
893 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Transmitting result for query `%s' to peer\n",
901 GNUNET_h2s (&prd->query));
903 GNUNET_STATISTICS_update (GSF_stats,
904 gettext_noop ("# replies received for other peers"),
906 msize = sizeof (struct PutMessage) + data_len;
907 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
912 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level > 1))
914 if (reply_anonymity_level - 1 > GSF_cover_content_count)
916 GNUNET_STATISTICS_update (GSF_stats,
918 ("# replies dropped due to insufficient cover traffic"),
922 GSF_cover_content_count -= (reply_anonymity_level - 1);
925 pm = GNUNET_malloc (msize);
926 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
927 pm->header.size = htons (msize);
928 pm->type = htonl (type);
929 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
930 memcpy (&pm[1], data, data_len);
931 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level != 0) &&
932 (GSF_enable_randomized_delays == GNUNET_YES))
934 struct GSF_DelayedHandle *dh;
936 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
940 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
942 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
943 &transmit_delayed_now, dh);
947 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
950 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
952 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
954 GNUNET_STATISTICS_update (GSF_stats,
956 ("# P2P searches destroyed due to ultimate reply"),
959 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
965 * Increase the host credit by a value.
967 * @param cp which peer to change the trust value on
968 * @param value is the int value by which the
969 * host credit is to be increased or decreased
970 * @returns the actual change in trust (positive or negative)
973 change_host_trust (struct GSF_ConnectedPeer *cp, int value)
977 GNUNET_assert (cp != NULL);
980 if (cp->ppd.trust + value < cp->ppd.trust)
982 value = UINT32_MAX - cp->ppd.trust;
983 cp->ppd.trust = UINT32_MAX;
986 cp->ppd.trust += value;
990 if (cp->ppd.trust < -value)
992 value = -cp->ppd.trust;
996 cp->ppd.trust += value;
1003 * We've received a request with the specified priority. Bound it
1004 * according to how much we trust the given peer.
1006 * @param prio_in requested priority
1007 * @param cp the peer making the request
1008 * @return effective priority
1011 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1013 #define N ((double)128.0)
1018 ld = GSF_test_get_load_too_high_ (0);
1019 if (ld == GNUNET_SYSERR)
1021 GNUNET_STATISTICS_update (GSF_stats,
1023 ("# requests done for free (low load)"), 1,
1025 return 0; /* excess resources */
1027 if (prio_in > INT32_MAX)
1028 prio_in = INT32_MAX;
1029 ret = -change_host_trust (cp, -(int) prio_in);
1032 if (ret > GSF_current_priorities + N)
1033 rret = GSF_current_priorities + N;
1036 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1038 if ((ld == GNUNET_YES) && (ret > 0))
1040 /* try with charging */
1041 ld = GSF_test_get_load_too_high_ (ret);
1043 if (ld == GNUNET_YES)
1045 GNUNET_STATISTICS_update (GSF_stats,
1047 ("# request dropped, priority insufficient"), 1,
1050 change_host_trust (cp, (int) ret);
1051 return -1; /* not enough resources */
1055 GNUNET_STATISTICS_update (GSF_stats,
1057 ("# requests done for a price (normal load)"), 1,
1066 * The priority level imposes a bound on the maximum
1067 * value for the ttl that can be requested.
1069 * @param ttl_in requested ttl
1070 * @param prio given priority
1071 * @return ttl_in if ttl_in is below the limit,
1072 * otherwise the ttl-limit for the given priority
1075 bound_ttl (int32_t ttl_in, uint32_t prio)
1077 unsigned long long allowed;
1081 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1082 if (ttl_in > allowed)
1084 if (allowed >= (1 << 30))
1093 * Handle P2P "QUERY" message. Creates the pending request entry
1094 * and sets up all of the data structures to that we will
1095 * process replies properly. Does not initiate forwarding or
1096 * local database lookups.
1098 * @param other the other peer involved (sender or receiver, NULL
1099 * for loopback messages where we are both sender and receiver)
1100 * @param message the actual message
1101 * @return pending request handle, NULL on error
1103 struct GSF_PendingRequest *
1104 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1105 const struct GNUNET_MessageHeader *message)
1107 struct PeerRequest *peerreq;
1108 struct GSF_PendingRequest *pr;
1109 struct GSF_PendingRequestData *prd;
1110 struct GSF_ConnectedPeer *cp;
1111 struct GSF_ConnectedPeer *cps;
1112 const GNUNET_HashCode *namespace;
1113 const struct GNUNET_PeerIdentity *target;
1114 enum GSF_PendingRequestOptions options;
1116 const struct GetMessage *gm;
1118 const GNUNET_HashCode *opt;
1121 uint32_t ttl_decrement;
1124 enum GNUNET_BLOCK_Type type;
1125 GNUNET_PEER_Id spid;
1127 GNUNET_assert (other != NULL);
1128 msize = ntohs (message->size);
1129 if (msize < sizeof (struct GetMessage))
1131 GNUNET_break_op (0);
1134 GNUNET_STATISTICS_update (GSF_stats,
1136 ("# GET requests received (from other peers)"), 1,
1138 gm = (const struct GetMessage *) message;
1139 type = ntohl (gm->type);
1140 bm = ntohl (gm->hash_bitmap);
1148 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
1150 GNUNET_break_op (0);
1153 opt = (const GNUNET_HashCode *) &gm[1];
1154 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
1155 /* bfsize must be power of 2, check! */
1156 if (0 != ((bfsize - 1) & bfsize))
1158 GNUNET_break_op (0);
1161 GSF_cover_query_count++;
1162 bm = ntohl (gm->hash_bitmap);
1164 cps = GNUNET_CONTAINER_multihashmap_get (cp_map, &other->hashPubKey);
1167 /* peer must have just disconnected */
1168 GNUNET_STATISTICS_update (GSF_stats,
1170 ("# requests dropped due to initiator not being connected"),
1174 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1175 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &opt[bits++]);
1181 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1184 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1190 GNUNET_i2s (other));
1192 GNUNET_STATISTICS_update (GSF_stats,
1194 ("# requests dropped due to missing reverse route"),
1198 /* note that we can really only check load here since otherwise
1199 * peers could find out that we are overloaded by not being
1200 * disconnected after sending us a malformed query... */
1201 priority = bound_priority (ntohl (gm->priority), cps);
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "Dropping query from `%s', this peer is too busy.\n",
1207 GNUNET_i2s (other));
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1214 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1217 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1218 if ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace == NULL))
1220 GNUNET_break_op (0);
1223 if ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace != NULL))
1225 GNUNET_break_op (0);
1230 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1231 *) &opt[bits++]) : NULL;
1234 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1235 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1236 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1237 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1239 /* don't have BW to send to peer, or would likely take longer than we have for it,
1240 * so at best indirect the query */
1242 options |= GSF_PRO_FORWARD_ONLY;
1243 spid = GNUNET_PEER_intern (other);
1244 GNUNET_assert (0 != spid);
1246 ttl = bound_ttl (ntohl (gm->ttl), priority);
1247 /* decrement ttl (always) */
1249 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1251 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1256 GNUNET_i2s (other), ttl, ttl_decrement);
1258 GNUNET_STATISTICS_update (GSF_stats,
1260 ("# requests dropped due TTL underflow"), 1,
1262 /* integer underflow => drop (should be very rare)! */
1265 ttl -= ttl_decrement;
1267 /* test if the request already exists */
1268 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1269 if (peerreq != NULL)
1272 prd = GSF_pending_request_get_data_ (pr);
1273 if ((prd->type == type) &&
1274 ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1275 (0 == memcmp (&prd->namespace, namespace, sizeof (GNUNET_HashCode)))))
1277 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1279 /* existing request has higher TTL, drop new one! */
1280 prd->priority += priority;
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 "Have existing request with higher TTL, dropping new request.\n",
1284 GNUNET_i2s (other));
1286 GNUNET_STATISTICS_update (GSF_stats,
1288 ("# requests dropped due to higher-TTL request"),
1292 /* existing request has lower TTL, drop old one! */
1293 GNUNET_STATISTICS_update (GSF_stats,
1294 gettext_noop ("# P2P searches active"), -1,
1296 priority += prd->priority;
1297 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1298 GNUNET_assert (GNUNET_YES ==
1299 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
1302 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
1304 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
1305 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
1307 GNUNET_free (peerreq);
1311 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1313 pr = GSF_pending_request_create_ (options, type, &gm->query, namespace,
1316 0) ? (const char *) &opt[bits] : NULL,
1317 bfsize, ntohl (gm->filter_mutator),
1319 (uint32_t) priority, ttl, spid, NULL, 0, /* replies_seen */
1320 &handle_p2p_reply, peerreq);
1321 GNUNET_assert (NULL != pr);
1323 GNUNET_break (GNUNET_OK ==
1324 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1327 GNUNET_STATISTICS_update (GSF_stats,
1329 ("# P2P query messages received and processed"), 1,
1331 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1338 * Function called if there has been a timeout trying to satisfy
1339 * a transmission request.
1341 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1342 * @param tc scheduler context
1345 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1347 struct GSF_PeerTransmitHandle *pth = cls;
1348 struct GSF_ConnectedPeer *cp;
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 "Timeout trying to transmit to other peer\n");
1354 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1356 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1357 if (GNUNET_YES == pth->is_query)
1358 GNUNET_assert (0 < cp->ppd.pending_queries--);
1359 else if (GNUNET_NO == pth->is_query)
1360 GNUNET_assert (0 < cp->ppd.pending_replies--);
1361 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1362 if (NULL != pth->cth)
1364 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1367 pth->gmc (pth->gmc_cls, 0, NULL);
1368 GNUNET_assert (0 == pth->cth_in_progress);
1374 * Transmit a message to the given peer as soon as possible.
1375 * If the peer disconnects before the transmission can happen,
1376 * the callback is invoked with a 'NULL' buffer.
1378 * @param cp target peer
1379 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1380 * @param priority how important is this request?
1381 * @param timeout when does this request timeout (call gmc with error)
1382 * @param size number of bytes we would like to send to the peer
1383 * @param gmc function to call to get the message
1384 * @param gmc_cls closure for gmc
1385 * @return handle to cancel request
1387 struct GSF_PeerTransmitHandle *
1388 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1389 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1390 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1392 struct GSF_PeerTransmitHandle *pth;
1393 struct GSF_PeerTransmitHandle *pos;
1394 struct GSF_PeerTransmitHandle *prev;
1396 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1397 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1398 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1400 pth->gmc_cls = gmc_cls;
1402 pth->is_query = is_query;
1403 pth->priority = priority;
1405 /* insertion sort (by priority, descending) */
1408 while ((pos != NULL) && (pos->priority > priority))
1414 GNUNET_CONTAINER_DLL_insert (cp->pth_head, cp->pth_tail, pth);
1416 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1417 if (GNUNET_YES == is_query)
1418 cp->ppd.pending_queries++;
1419 else if (GNUNET_NO == is_query)
1420 cp->ppd.pending_replies++;
1422 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1423 schedule_transmission (pth);
1429 * Cancel an earlier request for transmission.
1431 * @param pth request to cancel
1434 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1436 struct GSF_ConnectedPeer *cp;
1438 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1440 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1441 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1443 if (NULL != pth->cth)
1445 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1449 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1450 if (GNUNET_YES == pth->is_query)
1451 GNUNET_assert (0 < cp->ppd.pending_queries--);
1452 else if (GNUNET_NO == pth->is_query)
1453 GNUNET_assert (0 < cp->ppd.pending_replies--);
1454 GNUNET_assert (0 == pth->cth_in_progress);
1460 * Report on receiving a reply; update the performance record of the given peer.
1462 * @param cp responding peer (will be updated)
1463 * @param request_time time at which the original query was transmitted
1464 * @param request_priority priority of the original request
1467 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1468 struct GNUNET_TIME_Absolute request_time,
1469 uint32_t request_priority)
1471 struct GNUNET_TIME_Relative delay;
1473 delay = GNUNET_TIME_absolute_get_duration (request_time);
1474 cp->ppd.avg_reply_delay.rel_value =
1475 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1476 delay.rel_value) / RUNAVG_DELAY_N;
1477 cp->ppd.avg_priority =
1478 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1479 request_priority) / RUNAVG_DELAY_N;
1484 * Report on receiving a reply in response to an initiating client.
1485 * Remember that this peer is good for this client.
1487 * @param cp responding peer (will be updated)
1488 * @param initiator_client local client on responsible for query
1491 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1492 struct GSF_LocalClient *initiator_client)
1494 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1495 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1500 * Report on receiving a reply in response to an initiating peer.
1501 * Remember that this peer is good for this initiating peer.
1503 * @param cp responding peer (will be updated)
1504 * @param initiator_peer other peer responsible for query
1507 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1508 const struct GSF_ConnectedPeer *initiator_peer)
1512 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1513 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1514 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1515 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1516 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1521 * Method called whenever a given peer has a status change.
1523 * @param cls closure
1524 * @param peer peer identity this notification is about
1525 * @param bandwidth_in available amount of inbound bandwidth
1526 * @param bandwidth_out available amount of outbound bandwidth
1527 * @param timeout absolute time when this peer will time out
1528 * unless we see some further activity from it
1529 * @param atsi status information
1532 GSF_peer_status_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer,
1533 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1534 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1535 struct GNUNET_TIME_Absolute timeout,
1536 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1538 struct GSF_ConnectedPeer *cp;
1540 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
1541 GNUNET_assert (NULL != cp);
1542 update_atsi (cp, atsi);
1547 * A peer disconnected from us. Tear down the connected peer
1551 * @param peer identity of peer that connected
1554 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1556 struct GSF_ConnectedPeer *cp;
1557 struct GSF_PeerTransmitHandle *pth;
1558 struct GSF_DelayedHandle *dh;
1560 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
1562 return; /* must have been disconnect from core with
1563 * 'peer' == my_id, ignore */
1564 GNUNET_assert (GNUNET_YES ==
1565 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1566 &peer->hashPubKey, cp));
1567 if (NULL != cp->migration_pth)
1569 GSF_peer_transmit_cancel_ (cp->migration_pth);
1570 cp->migration_pth = NULL;
1572 if (NULL != cp->irc)
1574 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1577 if (GNUNET_SCHEDULER_NO_TASK != cp->irc_delay_task)
1579 GNUNET_SCHEDULER_cancel (cp->irc_delay_task);
1580 cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1582 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1583 &cancel_pending_request, cp);
1584 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1585 cp->request_map = NULL;
1586 GSF_plan_notify_peer_disconnect_ (cp);
1587 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1588 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1589 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1590 GSF_push_stop_ (cp);
1591 while (NULL != (pth = cp->pth_head))
1593 if (NULL != pth->cth)
1595 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1598 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1600 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1601 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1603 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1604 GNUNET_assert (0 == pth->cth_in_progress);
1605 pth->gmc (pth->gmc_cls, 0, NULL);
1608 while (NULL != (dh = cp->delayed_head))
1610 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1611 GNUNET_SCHEDULER_cancel (dh->delay_task);
1612 GNUNET_free (dh->pm);
1615 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1616 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1618 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1619 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1626 * Closure for 'call_iterator'.
1628 struct IterationContext
1631 * Function to call on each entry.
1633 GSF_ConnectedPeerIterator it;
1643 * Function that calls the callback for each peer.
1645 * @param cls the 'struct IterationContext*'
1646 * @param key identity of the peer
1647 * @param value the 'struct GSF_ConnectedPeer*'
1648 * @return GNUNET_YES to continue iteration
1651 call_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1653 struct IterationContext *ic = cls;
1654 struct GSF_ConnectedPeer *cp = value;
1656 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1662 * Iterate over all connected peers.
1664 * @param it function to call for each peer
1665 * @param it_cls closure for it
1668 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1670 struct IterationContext ic;
1674 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1679 * Obtain the identity of a connected peer.
1681 * @param cp peer to reserve bandwidth from
1682 * @param id identity to set (written to)
1685 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1686 struct GNUNET_PeerIdentity *id)
1688 GNUNET_assert (0 != cp->ppd.pid);
1689 GNUNET_PEER_resolve (cp->ppd.pid, id);
1694 * Assemble a migration stop message for transmission.
1696 * @param cls the 'struct GSF_ConnectedPeer' to use
1697 * @param size number of bytes we're allowed to write to buf
1698 * @param buf where to copy the message
1699 * @return number of bytes copied to buf
1702 create_migration_stop_message (void *cls, size_t size, void *buf)
1704 struct GSF_ConnectedPeer *cp = cls;
1705 struct MigrationStopMessage msm;
1707 cp->migration_pth = NULL;
1710 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1711 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1712 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1713 msm.reserved = htonl (0);
1715 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1716 (cp->last_migration_block));
1717 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1718 return sizeof (struct MigrationStopMessage);
1723 * Ask a peer to stop migrating data to us until the given point
1726 * @param cp peer to ask
1727 * @param block_time until when to block
1730 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1731 struct GNUNET_TIME_Relative block_time)
1733 if (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value >
1734 block_time.rel_value)
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1738 "Migration already blocked for another %llu ms\n",
1739 (unsigned long long)
1740 GNUNET_TIME_absolute_get_remaining
1741 (cp->last_migration_block).rel_value);
1743 return; /* already blocked */
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1747 (unsigned long long) block_time.rel_value);
1749 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1750 if (cp->migration_pth != NULL)
1751 GSF_peer_transmit_cancel_ (cp->migration_pth);
1753 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1754 GNUNET_TIME_UNIT_FOREVER_REL,
1755 sizeof (struct MigrationStopMessage),
1756 &create_migration_stop_message, cp);
1761 * Write host-trust information to a file - flush the buffer entry!
1763 * @param cls closure, not used
1764 * @param key host identity
1765 * @param value the 'struct GSF_ConnectedPeer' to flush
1766 * @return GNUNET_OK to continue iteration
1769 flush_trust (void *cls, const GNUNET_HashCode * key, void *value)
1771 struct GSF_ConnectedPeer *cp = value;
1774 struct GNUNET_PeerIdentity pid;
1776 if (cp->ppd.trust == cp->disk_trust)
1777 return GNUNET_OK; /* unchanged */
1778 GNUNET_assert (0 != cp->ppd.pid);
1779 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1780 fn = get_trust_filename (&pid);
1781 if (cp->ppd.trust == 0)
1783 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1784 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1785 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1789 trust = htonl (cp->ppd.trust);
1790 if (sizeof (uint32_t) ==
1791 GNUNET_DISK_fn_write (fn, &trust, sizeof (uint32_t),
1792 GNUNET_DISK_PERM_USER_READ |
1793 GNUNET_DISK_PERM_USER_WRITE |
1794 GNUNET_DISK_PERM_GROUP_READ |
1795 GNUNET_DISK_PERM_OTHER_READ))
1796 cp->disk_trust = cp->ppd.trust;
1804 * Notify core about a preference we have for the given peer
1805 * (to allocate more resources towards it). The change will
1806 * be communicated the next time we reserve bandwidth with
1807 * core (not instantly).
1809 * @param cp peer to reserve bandwidth from
1810 * @param pref preference change
1813 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1816 cp->inc_preference += pref;
1821 * Call this method periodically to flush trust information to disk.
1823 * @param cls closure, not used
1824 * @param tc task context, not used
1827 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1832 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_trust, NULL);
1835 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1837 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1842 * Initialize peer management subsystem.
1845 GSF_connected_peer_init_ ()
1847 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1848 GNUNET_assert (GNUNET_OK ==
1849 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1852 GNUNET_DISK_directory_create (trustDirectory);
1853 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1854 &cron_flush_trust, NULL);
1859 * Iterator to free peer entries.
1861 * @param cls closure, unused
1862 * @param key current key code
1863 * @param value value in the hash map (peer entry)
1864 * @return GNUNET_YES (we should continue to iterate)
1867 clean_peer (void *cls, const GNUNET_HashCode * key, void *value)
1869 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1875 * Shutdown peer management subsystem.
1878 GSF_connected_peer_done_ ()
1880 cron_flush_trust (NULL, NULL);
1881 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1882 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1884 GNUNET_free (trustDirectory);
1885 trustDirectory = NULL;
1890 * Iterator to remove references to LC entry.
1892 * @param cls the 'struct GSF_LocalClient*' to look for
1893 * @param key current key code
1894 * @param value value in the hash map (peer entry)
1895 * @return GNUNET_YES (we should continue to iterate)
1898 clean_local_client (void *cls, const GNUNET_HashCode * key, void *value)
1900 const struct GSF_LocalClient *lc = cls;
1901 struct GSF_ConnectedPeer *cp = value;
1904 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1905 if (cp->ppd.last_client_replies[i] == lc)
1906 cp->ppd.last_client_replies[i] = NULL;
1912 * Notification that a local client disconnected. Clean up all of our
1913 * references to the given handle.
1915 * @param lc handle to the local client (henceforth invalid)
1918 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1921 return; /* already cleaned up */
1922 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1927 /* end of gnunet-service-fs_cp.c */