2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs_cp.c
23 * @brief API to handle 'connected peers'
24 * @author Christian Grothoff
27 #include "gnunet_load_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-fs.h"
30 #include "gnunet-service-fs_cp.h"
31 #include "gnunet-service-fs_pe.h"
32 #include "gnunet-service-fs_pr.h"
33 #include "gnunet-service-fs_push.h"
36 * How often do we flush trust values to disk?
38 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
41 * After how long do we discard a reply?
43 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
47 * Handle to cancel a transmission request.
49 struct GSF_PeerTransmitHandle
53 * Kept in a doubly-linked list.
55 struct GSF_PeerTransmitHandle *next;
58 * Kept in a doubly-linked list.
60 struct GSF_PeerTransmitHandle *prev;
63 * Handle for an active request for transmission to this
64 * peer, or NULL (if core queue was full).
66 struct GNUNET_CORE_TransmitHandle *cth;
69 * Time when this transmission request was issued.
71 struct GNUNET_TIME_Absolute transmission_request_start_time;
74 * Timeout for this request.
76 struct GNUNET_TIME_Absolute timeout;
79 * Task called on timeout, or 0 for none.
81 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
84 * Function to call to get the actual message.
86 GSF_GetMessageCallback gmc;
89 * Peer this request targets.
91 struct GSF_ConnectedPeer *cp;
99 * Size of the message to be transmitted.
104 * Set to 1 if we're currently in the process of calling
105 * 'GNUNET_CORE_notify_transmit_ready' (so while cth is
106 * NULL, we should not call notify_transmit_ready for this
109 unsigned int cth_in_progress;
112 * GNUNET_YES if this is a query, GNUNET_NO for content.
117 * Did we get a reservation already?
122 * Priority of this request.
130 * Handle for an entry in our delay list.
132 struct GSF_DelayedHandle
136 * Kept in a doubly-linked list.
138 struct GSF_DelayedHandle *next;
141 * Kept in a doubly-linked list.
143 struct GSF_DelayedHandle *prev;
146 * Peer this transmission belongs to.
148 struct GSF_ConnectedPeer *cp;
151 * The PUT that was delayed.
153 struct PutMessage *pm;
156 * Task for the delay.
158 GNUNET_SCHEDULER_TaskIdentifier delay_task;
161 * Size of the message.
169 * Information per peer and request.
175 * Handle to generic request.
177 struct GSF_PendingRequest *pr;
180 * Handle to specific peer.
182 struct GSF_ConnectedPeer *cp;
185 * Task for asynchronous stopping of this request.
187 GNUNET_SCHEDULER_TaskIdentifier kill_task;
195 struct GSF_ConnectedPeer
199 * Performance data for this peer.
201 struct GSF_PeerPerformanceData ppd;
204 * Time until when we blocked this peer from migrating
207 struct GNUNET_TIME_Absolute last_migration_block;
210 * Task scheduled to revive migration to this peer.
212 GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
215 * Messages (replies, queries, content migration) we would like to
216 * send to this peer in the near future. Sorted by priority, head.
218 struct GSF_PeerTransmitHandle *pth_head;
221 * Messages (replies, queries, content migration) we would like to
222 * send to this peer in the near future. Sorted by priority, tail.
224 struct GSF_PeerTransmitHandle *pth_tail;
227 * Messages (replies, queries, content migration) we would like to
228 * send to this peer in the near future. Sorted by priority, head.
230 struct GSF_DelayedHandle *delayed_head;
233 * Messages (replies, queries, content migration) we would like to
234 * send to this peer in the near future. Sorted by priority, tail.
236 struct GSF_DelayedHandle *delayed_tail;
239 * Migration stop message in our queue, or NULL if we have none pending.
241 struct GSF_PeerTransmitHandle *migration_pth;
244 * Context of our GNUNET_ATS_reserve_bandwidth call (or NULL).
246 struct GNUNET_ATS_ReservationContext *rc;
249 * Task scheduled if we need to retry bandwidth reservation later.
251 GNUNET_SCHEDULER_TaskIdentifier rc_delay_task;
254 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
256 struct GNUNET_CONTAINER_MultiHashMap *request_map;
259 * Increase in traffic preference still to be submitted
260 * to the core service for this peer.
262 uint64_t inc_preference;
265 * Trust rating for this peer on disk.
270 * Which offset in "last_p2p_replies" will be updated next?
271 * (we go round-robin).
273 unsigned int last_p2p_replies_woff;
276 * Which offset in "last_client_replies" will be updated next?
277 * (we go round-robin).
279 unsigned int last_client_replies_woff;
282 * Current offset into 'last_request_times' ring buffer.
284 unsigned int last_request_times_off;
287 * GNUNET_YES if we did successfully reserve 32k bandwidth,
296 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
298 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
301 * Where do we store trust information?
303 static char *trustDirectory;
306 * Handle to ATS service.
308 static struct GNUNET_ATS_PerformanceHandle *ats;
311 * Get the filename under which we would store the GNUNET_HELLO_Message
312 * for the given host and protocol.
313 * @return filename of the form DIRECTORY/HOSTID
316 get_trust_filename (const struct GNUNET_PeerIdentity *id)
318 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
321 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
322 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
328 * Find latency information in 'atsi'.
330 * @param atsi performance data
331 * @return connection latency
333 static struct GNUNET_TIME_Relative
334 get_latency (const struct GNUNET_ATS_Information *atsi)
337 return GNUNET_TIME_UNIT_SECONDS;
338 while ((ntohl (atsi->type) != GNUNET_ATS_ARRAY_TERMINATOR) &&
339 (ntohl (atsi->type) != GNUNET_ATS_QUALITY_NET_DELAY))
341 if (ntohl (atsi->type) == GNUNET_ATS_ARRAY_TERMINATOR)
343 /* We sometime have no latency data, i.e. if the address came from
344 peerinfo and we never had a chance to play transport-level
345 PING/PONG yet. Assume 1s in that case. */
346 return GNUNET_TIME_UNIT_SECONDS;
348 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
349 ntohl (atsi->value));
354 * Update the performance information kept for the given peer.
356 * @param cp peer record to update
357 * @param atsi transport performance data
360 update_atsi (struct GSF_ConnectedPeer *cp,
361 const struct GNUNET_ATS_Information *atsi)
363 struct GNUNET_TIME_Relative latency;
365 latency = get_latency (atsi);
366 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
367 /* LATER: merge atsi into cp's performance data (if we ever care...) */
372 * Return the performance data record for the given peer
374 * @param cp peer to query
375 * @return performance data record for the peer
377 struct GSF_PeerPerformanceData *
378 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
385 * Core is ready to transmit to a peer, get the message.
387 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
388 * @param size number of bytes core is willing to take
389 * @param buf where to copy the message
390 * @return number of bytes copied to buf
393 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
397 * Function called by core upon success or failure of our bandwidth reservation request.
399 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
400 * @param peer identifies the peer
401 * @param amount set to the amount that was actually reserved or unreserved;
402 * either the full requested amount or zero (no partial reservations)
403 * @param res_delay if the reservation could not be satisfied (amount was 0), how
404 * long should the client wait until re-trying?
407 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
408 int32_t amount, struct GNUNET_TIME_Relative res_delay);
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;
423 if ((NULL != pth->cth) || (0 != pth->cth_in_progress))
424 return; /* already done */
426 GNUNET_assert (0 != cp->ppd.pid);
427 GNUNET_PEER_resolve (cp->ppd.pid, &target);
429 if (0 != cp->inc_preference)
431 GNUNET_ATS_change_preference (ats,
433 GNUNET_ATS_PREFERENCE_BANDWIDTH,
434 (double) cp->inc_preference,
435 GNUNET_ATS_PREFERENCE_END);
436 cp->inc_preference = 0;
439 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
441 /* query, need reservation */
442 if (GNUNET_YES != cp->did_reserve)
443 return; /* not ready */
444 cp->did_reserve = GNUNET_NO;
445 /* reservation already done! */
446 pth->was_reserved = GNUNET_YES;
448 GNUNET_ATS_reserve_bandwidth (ats, &target,
450 &ats_reserve_callback, cp);
452 GNUNET_assert (pth->cth == NULL);
453 pth->cth_in_progress++;
455 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
456 GNUNET_TIME_absolute_get_remaining
457 (pth->timeout), &target, pth->size,
458 &peer_transmit_ready_cb, pth);
459 GNUNET_assert (0 < pth->cth_in_progress--);
464 * Core is ready to transmit to a peer, get the message.
466 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
467 * @param size number of bytes core is willing to take
468 * @param buf where to copy the message
469 * @return number of bytes copied to buf
472 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
474 struct GSF_PeerTransmitHandle *pth = cls;
475 struct GSF_PeerTransmitHandle *pos;
476 struct GSF_ConnectedPeer *cp;
479 GNUNET_assert ((NULL == buf) || (pth->size <= size));
481 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
483 GNUNET_SCHEDULER_cancel (pth->timeout_task);
484 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
487 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
488 if (GNUNET_YES == pth->is_query)
490 cp->ppd.last_request_times[(cp->last_request_times_off++) %
491 MAX_QUEUE_PER_PEER] =
492 GNUNET_TIME_absolute_get ();
493 GNUNET_assert (0 < cp->ppd.pending_queries--);
495 else if (GNUNET_NO == pth->is_query)
497 GNUNET_assert (0 < cp->ppd.pending_replies--);
499 GNUNET_LOAD_update (cp->ppd.transmission_delay,
500 GNUNET_TIME_absolute_get_duration
501 (pth->transmission_request_start_time).rel_value);
502 ret = pth->gmc (pth->gmc_cls, size, buf);
503 GNUNET_assert (NULL == pth->cth);
504 for (pos = cp->pth_head; pos != NULL; pos = pos->next)
506 GNUNET_assert (pos != pth);
507 schedule_transmission (pos);
509 GNUNET_assert (pth->cth == NULL);
510 GNUNET_assert (pth->cth_in_progress == 0);
517 * (re)try to reserve bandwidth from the given peer.
519 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
520 * @param tc scheduler context
523 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
525 struct GSF_ConnectedPeer *cp = cls;
526 struct GNUNET_PeerIdentity target;
528 GNUNET_PEER_resolve (cp->ppd.pid, &target);
529 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
531 GNUNET_ATS_reserve_bandwidth (ats, &target,
533 &ats_reserve_callback, cp);
538 * Function called by core upon success or failure of our bandwidth reservation request.
540 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
541 * @param peer identifies the peer
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?
548 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
549 int32_t amount, struct GNUNET_TIME_Relative res_delay)
551 struct GSF_ConnectedPeer *cp = cls;
552 struct GSF_PeerTransmitHandle *pth;
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Reserved %d bytes / need to wait %llu ms for reservation\n",
557 (unsigned long long) res_delay.rel_value);
562 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
565 cp->did_reserve = GNUNET_YES;
567 if ((NULL != pth) && (NULL == pth->cth))
569 /* reservation success, try transmission now! */
570 pth->cth_in_progress++;
572 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
573 GNUNET_TIME_absolute_get_remaining
574 (pth->timeout), peer, pth->size,
575 &peer_transmit_ready_cb, pth);
576 GNUNET_assert (0 < pth->cth_in_progress--);
582 * A peer connected to us. Setup the connected peer
585 * @param peer identity of peer that connected
586 * @param atsi performance data for the connection
587 * @return handle to connected peer entry
589 struct GSF_ConnectedPeer *
590 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
591 const struct GNUNET_ATS_Information *atsi)
593 struct GSF_ConnectedPeer *cp;
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Connected to peer %s\n",
600 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
601 cp->ppd.pid = GNUNET_PEER_intern (peer);
602 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
604 GNUNET_ATS_reserve_bandwidth (ats, peer,
606 &ats_reserve_callback, cp);
607 fn = get_trust_filename (peer);
608 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
609 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
610 cp->disk_trust = cp->ppd.trust = ntohl (trust);
612 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
613 GNUNET_break (GNUNET_OK ==
614 GNUNET_CONTAINER_multihashmap_put (cp_map, &peer->hashPubKey,
616 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
617 GNUNET_STATISTICS_set (GSF_stats,
619 ("# connected peers"),
620 GNUNET_CONTAINER_multihashmap_size (cp_map),
622 update_atsi (cp, atsi);
623 GSF_push_start_ (cp);
629 * It may be time to re-start migrating content to this
630 * peer. Check, and if so, restart migration.
632 * @param cls the 'struct GSF_ConnectedPeer'
633 * @param tc scheduler context
636 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
638 struct GSF_ConnectedPeer *cp = cls;
639 struct GNUNET_TIME_Relative bt;
641 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
642 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
643 if (0 != bt.rel_value)
645 /* still time left... */
646 cp->mig_revive_task =
647 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
650 GSF_push_start_ (cp);
655 * Get a handle for a connected peer.
657 * @param peer peer's identity
658 * @return NULL if the peer is not currently connected
660 struct GSF_ConnectedPeer *
661 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
665 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
670 * Handle P2P "MIGRATION_STOP" message.
672 * @param cls closure, always NULL
673 * @param other the other peer involved (sender or receiver, NULL
674 * for loopback messages where we are both sender and receiver)
675 * @param message the actual message
676 * @param atsi performance information
677 * @return GNUNET_OK to keep the connection open,
678 * GNUNET_SYSERR to close it (signal serious error)
681 GSF_handle_p2p_migration_stop_ (void *cls,
682 const struct GNUNET_PeerIdentity *other,
683 const struct GNUNET_MessageHeader *message,
684 const struct GNUNET_ATS_Information
687 struct GSF_ConnectedPeer *cp;
688 const struct MigrationStopMessage *msm;
689 struct GNUNET_TIME_Relative bt;
691 msm = (const struct MigrationStopMessage *) message;
692 cp = GSF_peer_get_ (other);
698 GNUNET_STATISTICS_update (GSF_stats,
700 ("# migration stop messages received"),
702 bt = GNUNET_TIME_relative_ntoh (msm->duration);
703 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
704 _("Migration of content to peer `%s' blocked for %llu ms\n"),
705 GNUNET_i2s (other), (unsigned long long) bt.rel_value);
706 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
707 if (cp->mig_revive_task == GNUNET_SCHEDULER_NO_TASK)
710 cp->mig_revive_task =
711 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
713 update_atsi (cp, atsi);
719 * Copy reply and free put message.
721 * @param cls the 'struct PutMessage'
722 * @param buf_size number of bytes available in buf
723 * @param buf where to copy the message, NULL on error (peer disconnect)
724 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
727 copy_reply (void *cls, size_t buf_size, void *buf)
729 struct PutMessage *pm = cls;
734 GNUNET_assert (buf_size >= ntohs (pm->header.size));
735 size = ntohs (pm->header.size);
736 memcpy (buf, pm, size);
737 GNUNET_STATISTICS_update (GSF_stats,
739 ("# replies transmitted to other peers"), 1,
745 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
754 * Cancel all requests associated with the peer.
757 * @param query hash code of the request
758 * @param value the 'struct GSF_PendingRequest'
759 * @return GNUNET_YES (continue to iterate)
762 cancel_pending_request (void *cls, const GNUNET_HashCode * query, void *value)
764 struct PeerRequest *peerreq = value;
765 struct GSF_PendingRequest *pr = peerreq->pr;
766 struct GSF_ConnectedPeer *cp = peerreq->cp;
767 struct GSF_PendingRequestData *prd;
769 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
771 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
772 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
774 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
776 prd = GSF_pending_request_get_data_ (pr);
777 GNUNET_break (GNUNET_YES ==
778 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
779 &prd->query, peerreq));
780 GSF_pending_request_cancel_ (pr, GNUNET_NO);
781 GNUNET_free (peerreq);
787 * Free the given request.
789 * @param cls the request to free
790 * @param tc task context
793 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
795 struct PeerRequest *peerreq = cls;
796 struct GSF_PendingRequest *pr = peerreq->pr;
797 struct GSF_PendingRequestData *prd;
799 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
800 prd = GSF_pending_request_get_data_ (pr);
801 cancel_pending_request (NULL, &prd->query, peerreq);
806 * The artificial delay is over, transmit the message now.
808 * @param cls the 'struct GSF_DelayedHandle' with the message
809 * @param tc scheduler context
812 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
814 struct GSF_DelayedHandle *dh = cls;
815 struct GSF_ConnectedPeer *cp = dh->cp;
817 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
818 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
820 GNUNET_free (dh->pm);
824 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
825 dh->msize, ©_reply, dh->pm);
831 * Get the randomized delay a response should be subjected to.
833 * @return desired delay
835 static struct GNUNET_TIME_Relative
836 get_randomized_delay ()
838 struct GNUNET_TIME_Relative ret;
840 /* FIXME: replace 5000 with something relating to current observed P2P message latency */
842 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
843 GNUNET_CRYPTO_random_u32
844 (GNUNET_CRYPTO_QUALITY_WEAK, 5000));
845 GNUNET_STATISTICS_update (GSF_stats,
847 ("# artificial delays introduced (ms)"),
848 ret.rel_value, GNUNET_NO);
855 * Handle a reply to a pending request. Also called if a request
856 * expires (then with data == NULL). The handler may be called
857 * many times (depending on the request type), but will not be
858 * called during or after a call to GSF_pending_request_cancel
859 * and will also not be called anymore after a call signalling
862 * @param cls 'struct PeerRequest' this is an answer for
863 * @param eval evaluation of the result
864 * @param pr handle to the original pending request
865 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
866 * @param expiration when does 'data' expire?
867 * @param type type of the block
868 * @param data response data, NULL on request expiration
869 * @param data_len number of bytes in data
872 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
873 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
874 struct GNUNET_TIME_Absolute expiration,
875 enum GNUNET_BLOCK_Type type, const void *data,
878 struct PeerRequest *peerreq = cls;
879 struct GSF_ConnectedPeer *cp = peerreq->cp;
880 struct GSF_PendingRequestData *prd;
881 struct PutMessage *pm;
884 GNUNET_assert (data_len + sizeof (struct PutMessage) <
885 GNUNET_SERVER_MAX_MESSAGE_SIZE);
886 GNUNET_assert (peerreq->pr == pr);
887 prd = GSF_pending_request_get_data_ (pr);
890 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
892 GNUNET_break (GNUNET_YES ==
893 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
894 &prd->query, peerreq));
895 GNUNET_free (peerreq);
898 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
899 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Transmitting result for query `%s' to peer\n",
907 GNUNET_h2s (&prd->query));
909 GNUNET_STATISTICS_update (GSF_stats,
910 gettext_noop ("# replies received for other peers"),
912 msize = sizeof (struct PutMessage) + data_len;
913 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
918 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level > 1))
920 if (reply_anonymity_level - 1 > GSF_cover_content_count)
922 GNUNET_STATISTICS_update (GSF_stats,
924 ("# replies dropped due to insufficient cover traffic"),
928 GSF_cover_content_count -= (reply_anonymity_level - 1);
931 pm = GNUNET_malloc (msize);
932 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
933 pm->header.size = htons (msize);
934 pm->type = htonl (type);
935 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
936 memcpy (&pm[1], data, data_len);
937 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level != 0) &&
938 (GSF_enable_randomized_delays == GNUNET_YES))
940 struct GSF_DelayedHandle *dh;
942 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
946 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
948 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
949 &transmit_delayed_now, dh);
953 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
956 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
958 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
960 GNUNET_STATISTICS_update (GSF_stats,
962 ("# P2P searches destroyed due to ultimate reply"),
965 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
971 * Increase the host credit by a value.
973 * @param cp which peer to change the trust value on
974 * @param value is the int value by which the
975 * host credit is to be increased or decreased
976 * @returns the actual change in trust (positive or negative)
979 change_host_trust (struct GSF_ConnectedPeer *cp, int value)
983 GNUNET_assert (cp != NULL);
986 if (cp->ppd.trust + value < cp->ppd.trust)
988 value = UINT32_MAX - cp->ppd.trust;
989 cp->ppd.trust = UINT32_MAX;
992 cp->ppd.trust += value;
996 if (cp->ppd.trust < -value)
998 value = -cp->ppd.trust;
1002 cp->ppd.trust += value;
1009 * We've received a request with the specified priority. Bound it
1010 * according to how much we trust the given peer.
1012 * @param prio_in requested priority
1013 * @param cp the peer making the request
1014 * @return effective priority
1017 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1019 #define N ((double)128.0)
1024 ld = GSF_test_get_load_too_high_ (0);
1025 if (ld == GNUNET_SYSERR)
1027 GNUNET_STATISTICS_update (GSF_stats,
1029 ("# requests done for free (low load)"), 1,
1031 return 0; /* excess resources */
1033 if (prio_in > INT32_MAX)
1034 prio_in = INT32_MAX;
1035 ret = -change_host_trust (cp, -(int) prio_in);
1038 if (ret > GSF_current_priorities + N)
1039 rret = GSF_current_priorities + N;
1042 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1044 if ((ld == GNUNET_YES) && (ret > 0))
1046 /* try with charging */
1047 ld = GSF_test_get_load_too_high_ (ret);
1049 if (ld == GNUNET_YES)
1051 GNUNET_STATISTICS_update (GSF_stats,
1053 ("# request dropped, priority insufficient"), 1,
1056 change_host_trust (cp, (int) ret);
1057 return -1; /* not enough resources */
1061 GNUNET_STATISTICS_update (GSF_stats,
1063 ("# requests done for a price (normal load)"), 1,
1072 * The priority level imposes a bound on the maximum
1073 * value for the ttl that can be requested.
1075 * @param ttl_in requested ttl
1076 * @param prio given priority
1077 * @return ttl_in if ttl_in is below the limit,
1078 * otherwise the ttl-limit for the given priority
1081 bound_ttl (int32_t ttl_in, uint32_t prio)
1083 unsigned long long allowed;
1087 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1088 if (ttl_in > allowed)
1090 if (allowed >= (1 << 30))
1099 * Handle P2P "QUERY" message. Creates the pending request entry
1100 * and sets up all of the data structures to that we will
1101 * process replies properly. Does not initiate forwarding or
1102 * local database lookups.
1104 * @param other the other peer involved (sender or receiver, NULL
1105 * for loopback messages where we are both sender and receiver)
1106 * @param message the actual message
1107 * @return pending request handle, NULL on error
1109 struct GSF_PendingRequest *
1110 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1111 const struct GNUNET_MessageHeader *message)
1113 struct PeerRequest *peerreq;
1114 struct GSF_PendingRequest *pr;
1115 struct GSF_PendingRequestData *prd;
1116 struct GSF_ConnectedPeer *cp;
1117 struct GSF_ConnectedPeer *cps;
1118 const GNUNET_HashCode *namespace;
1119 const struct GNUNET_PeerIdentity *target;
1120 enum GSF_PendingRequestOptions options;
1122 const struct GetMessage *gm;
1124 const GNUNET_HashCode *opt;
1127 uint32_t ttl_decrement;
1130 enum GNUNET_BLOCK_Type type;
1131 GNUNET_PEER_Id spid;
1133 GNUNET_assert (other != NULL);
1134 msize = ntohs (message->size);
1135 if (msize < sizeof (struct GetMessage))
1137 GNUNET_break_op (0);
1140 GNUNET_STATISTICS_update (GSF_stats,
1142 ("# GET requests received (from other peers)"), 1,
1144 gm = (const struct GetMessage *) message;
1145 type = ntohl (gm->type);
1146 bm = ntohl (gm->hash_bitmap);
1154 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
1156 GNUNET_break_op (0);
1159 opt = (const GNUNET_HashCode *) &gm[1];
1160 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
1161 /* bfsize must be power of 2, check! */
1162 if (0 != ((bfsize - 1) & bfsize))
1164 GNUNET_break_op (0);
1167 GSF_cover_query_count++;
1168 bm = ntohl (gm->hash_bitmap);
1170 cps = GSF_peer_get_ (other);
1173 /* peer must have just disconnected */
1174 GNUNET_STATISTICS_update (GSF_stats,
1176 ("# requests dropped due to initiator not being connected"),
1180 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1181 cp = GSF_peer_get_ ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
1187 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1190 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1195 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1196 GNUNET_i2s (other));
1198 GNUNET_STATISTICS_update (GSF_stats,
1200 ("# requests dropped due to missing reverse route"),
1204 /* note that we can really only check load here since otherwise
1205 * peers could find out that we are overloaded by not being
1206 * disconnected after sending us a malformed query... */
1207 priority = bound_priority (ntohl (gm->priority), cps);
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "Dropping query from `%s', this peer is too busy.\n",
1213 GNUNET_i2s (other));
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1220 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1223 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1224 if ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace == NULL))
1226 GNUNET_break_op (0);
1229 if ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace != NULL))
1231 GNUNET_break_op (0);
1236 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1237 *) &opt[bits++]) : NULL;
1240 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1241 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1242 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1243 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1245 /* don't have BW to send to peer, or would likely take longer than we have for it,
1246 * so at best indirect the query */
1248 options |= GSF_PRO_FORWARD_ONLY;
1249 spid = GNUNET_PEER_intern (other);
1250 GNUNET_assert (0 != spid);
1252 ttl = bound_ttl (ntohl (gm->ttl), priority);
1253 /* decrement ttl (always) */
1255 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1257 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1262 GNUNET_i2s (other), ttl, ttl_decrement);
1264 GNUNET_STATISTICS_update (GSF_stats,
1266 ("# requests dropped due TTL underflow"), 1,
1268 /* integer underflow => drop (should be very rare)! */
1271 ttl -= ttl_decrement;
1273 /* test if the request already exists */
1274 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1275 if (peerreq != NULL)
1278 prd = GSF_pending_request_get_data_ (pr);
1279 if ((prd->type == type) &&
1280 ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1281 (0 == memcmp (&prd->namespace, namespace, sizeof (GNUNET_HashCode)))))
1283 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1285 /* existing request has higher TTL, drop new one! */
1286 prd->priority += priority;
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1289 "Have existing request with higher TTL, dropping new request.\n",
1290 GNUNET_i2s (other));
1292 GNUNET_STATISTICS_update (GSF_stats,
1294 ("# requests dropped due to higher-TTL request"),
1298 /* existing request has lower TTL, drop old one! */
1299 GNUNET_STATISTICS_update (GSF_stats,
1300 gettext_noop ("# P2P searches active"), -1,
1302 priority += prd->priority;
1303 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1304 GNUNET_assert (GNUNET_YES ==
1305 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
1308 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
1310 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
1311 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
1313 GNUNET_free (peerreq);
1317 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1319 pr = GSF_pending_request_create_ (options, type, &gm->query, namespace,
1322 0) ? (const char *) &opt[bits] : NULL,
1323 bfsize, ntohl (gm->filter_mutator),
1325 (uint32_t) priority, ttl, spid,
1326 GNUNET_PEER_intern (other),
1327 NULL, 0, /* replies_seen */
1328 &handle_p2p_reply, peerreq);
1329 GNUNET_assert (NULL != pr);
1331 GNUNET_break (GNUNET_OK ==
1332 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1334 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1335 GNUNET_STATISTICS_update (GSF_stats,
1337 ("# P2P query messages received and processed"), 1,
1339 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1346 * Function called if there has been a timeout trying to satisfy
1347 * a transmission request.
1349 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1350 * @param tc scheduler context
1353 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1355 struct GSF_PeerTransmitHandle *pth = cls;
1356 struct GSF_ConnectedPeer *cp;
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "Timeout trying to transmit to other peer\n");
1362 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1364 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1365 if (GNUNET_YES == pth->is_query)
1366 GNUNET_assert (0 < cp->ppd.pending_queries--);
1367 else if (GNUNET_NO == pth->is_query)
1368 GNUNET_assert (0 < cp->ppd.pending_replies--);
1369 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1370 if (NULL != pth->cth)
1372 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1375 pth->gmc (pth->gmc_cls, 0, NULL);
1376 GNUNET_assert (0 == pth->cth_in_progress);
1382 * Transmit a message to the given peer as soon as possible.
1383 * If the peer disconnects before the transmission can happen,
1384 * the callback is invoked with a 'NULL' buffer.
1386 * @param cp target peer
1387 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1388 * @param priority how important is this request?
1389 * @param timeout when does this request timeout (call gmc with error)
1390 * @param size number of bytes we would like to send to the peer
1391 * @param gmc function to call to get the message
1392 * @param gmc_cls closure for gmc
1393 * @return handle to cancel request
1395 struct GSF_PeerTransmitHandle *
1396 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1397 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1398 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1400 struct GSF_PeerTransmitHandle *pth;
1401 struct GSF_PeerTransmitHandle *pos;
1402 struct GSF_PeerTransmitHandle *prev;
1404 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1405 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1406 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1408 pth->gmc_cls = gmc_cls;
1410 pth->is_query = is_query;
1411 pth->priority = priority;
1413 /* insertion sort (by priority, descending) */
1416 while ((pos != NULL) && (pos->priority > priority))
1422 GNUNET_CONTAINER_DLL_insert (cp->pth_head, cp->pth_tail, pth);
1424 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1425 if (GNUNET_YES == is_query)
1426 cp->ppd.pending_queries++;
1427 else if (GNUNET_NO == is_query)
1428 cp->ppd.pending_replies++;
1430 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1431 schedule_transmission (pth);
1437 * Cancel an earlier request for transmission.
1439 * @param pth request to cancel
1442 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1444 struct GSF_ConnectedPeer *cp;
1446 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1448 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1449 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1451 if (NULL != pth->cth)
1453 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1457 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1458 if (GNUNET_YES == pth->is_query)
1459 GNUNET_assert (0 < cp->ppd.pending_queries--);
1460 else if (GNUNET_NO == pth->is_query)
1461 GNUNET_assert (0 < cp->ppd.pending_replies--);
1462 GNUNET_assert (0 == pth->cth_in_progress);
1468 * Report on receiving a reply; update the performance record of the given peer.
1470 * @param cp responding peer (will be updated)
1471 * @param request_time time at which the original query was transmitted
1472 * @param request_priority priority of the original request
1475 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1476 struct GNUNET_TIME_Absolute request_time,
1477 uint32_t request_priority)
1479 struct GNUNET_TIME_Relative delay;
1481 delay = GNUNET_TIME_absolute_get_duration (request_time);
1482 cp->ppd.avg_reply_delay.rel_value =
1483 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1484 delay.rel_value) / RUNAVG_DELAY_N;
1485 cp->ppd.avg_priority =
1486 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1487 request_priority) / RUNAVG_DELAY_N;
1492 * Report on receiving a reply in response to an initiating client.
1493 * Remember that this peer is good for this client.
1495 * @param cp responding peer (will be updated)
1496 * @param initiator_client local client on responsible for query
1499 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1500 struct GSF_LocalClient *initiator_client)
1502 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1503 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1508 * Report on receiving a reply in response to an initiating peer.
1509 * Remember that this peer is good for this initiating peer.
1511 * @param cp responding peer (will be updated)
1512 * @param initiator_peer other peer responsible for query
1515 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1516 const struct GSF_ConnectedPeer *initiator_peer)
1520 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1521 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1522 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1523 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1524 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1529 * A peer disconnected from us. Tear down the connected peer
1533 * @param peer identity of peer that connected
1536 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1538 struct GSF_ConnectedPeer *cp;
1539 struct GSF_PeerTransmitHandle *pth;
1540 struct GSF_DelayedHandle *dh;
1542 cp = GSF_peer_get_ (peer);
1544 return; /* must have been disconnect from core with
1545 * 'peer' == my_id, ignore */
1546 GNUNET_assert (GNUNET_YES ==
1547 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1548 &peer->hashPubKey, cp));
1549 GNUNET_STATISTICS_set (GSF_stats,
1551 ("# connected peers"),
1552 GNUNET_CONTAINER_multihashmap_size (cp_map),
1554 if (NULL != cp->migration_pth)
1556 GSF_peer_transmit_cancel_ (cp->migration_pth);
1557 cp->migration_pth = NULL;
1561 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1564 if (GNUNET_SCHEDULER_NO_TASK != cp->rc_delay_task)
1566 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1567 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1569 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1570 &cancel_pending_request, cp);
1571 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1572 cp->request_map = NULL;
1573 GSF_plan_notify_peer_disconnect_ (cp);
1574 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1575 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1576 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1577 GSF_push_stop_ (cp);
1578 while (NULL != (pth = cp->pth_head))
1580 if (NULL != pth->cth)
1582 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1585 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1587 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1588 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1590 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1591 GNUNET_assert (0 == pth->cth_in_progress);
1592 pth->gmc (pth->gmc_cls, 0, NULL);
1595 while (NULL != (dh = cp->delayed_head))
1597 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1598 GNUNET_SCHEDULER_cancel (dh->delay_task);
1599 GNUNET_free (dh->pm);
1602 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1603 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1605 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1606 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1613 * Closure for 'call_iterator'.
1615 struct IterationContext
1618 * Function to call on each entry.
1620 GSF_ConnectedPeerIterator it;
1630 * Function that calls the callback for each peer.
1632 * @param cls the 'struct IterationContext*'
1633 * @param key identity of the peer
1634 * @param value the 'struct GSF_ConnectedPeer*'
1635 * @return GNUNET_YES to continue iteration
1638 call_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1640 struct IterationContext *ic = cls;
1641 struct GSF_ConnectedPeer *cp = value;
1643 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1649 * Iterate over all connected peers.
1651 * @param it function to call for each peer
1652 * @param it_cls closure for it
1655 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1657 struct IterationContext ic;
1661 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1666 * Obtain the identity of a connected peer.
1668 * @param cp peer to reserve bandwidth from
1669 * @param id identity to set (written to)
1672 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1673 struct GNUNET_PeerIdentity *id)
1675 GNUNET_assert (0 != cp->ppd.pid);
1676 GNUNET_PEER_resolve (cp->ppd.pid, id);
1681 * Assemble a migration stop message for transmission.
1683 * @param cls the 'struct GSF_ConnectedPeer' to use
1684 * @param size number of bytes we're allowed to write to buf
1685 * @param buf where to copy the message
1686 * @return number of bytes copied to buf
1689 create_migration_stop_message (void *cls, size_t size, void *buf)
1691 struct GSF_ConnectedPeer *cp = cls;
1692 struct MigrationStopMessage msm;
1694 cp->migration_pth = NULL;
1697 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1698 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1699 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1700 msm.reserved = htonl (0);
1702 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1703 (cp->last_migration_block));
1704 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1705 return sizeof (struct MigrationStopMessage);
1710 * Ask a peer to stop migrating data to us until the given point
1713 * @param cp peer to ask
1714 * @param block_time until when to block
1717 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1718 struct GNUNET_TIME_Relative block_time)
1720 if (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value >
1721 block_time.rel_value)
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1725 "Migration already blocked for another %llu ms\n",
1726 (unsigned long long)
1727 GNUNET_TIME_absolute_get_remaining
1728 (cp->last_migration_block).rel_value);
1730 return; /* already blocked */
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1734 (unsigned long long) block_time.rel_value);
1736 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1737 if (cp->migration_pth != NULL)
1738 GSF_peer_transmit_cancel_ (cp->migration_pth);
1740 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1741 GNUNET_TIME_UNIT_FOREVER_REL,
1742 sizeof (struct MigrationStopMessage),
1743 &create_migration_stop_message, cp);
1748 * Write host-trust information to a file - flush the buffer entry!
1750 * @param cls closure, not used
1751 * @param key host identity
1752 * @param value the 'struct GSF_ConnectedPeer' to flush
1753 * @return GNUNET_OK to continue iteration
1756 flush_trust (void *cls, const GNUNET_HashCode * key, void *value)
1758 struct GSF_ConnectedPeer *cp = value;
1761 struct GNUNET_PeerIdentity pid;
1763 if (cp->ppd.trust == cp->disk_trust)
1764 return GNUNET_OK; /* unchanged */
1765 GNUNET_assert (0 != cp->ppd.pid);
1766 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1767 fn = get_trust_filename (&pid);
1768 if (cp->ppd.trust == 0)
1770 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1771 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1772 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1776 trust = htonl (cp->ppd.trust);
1777 if (sizeof (uint32_t) ==
1778 GNUNET_DISK_fn_write (fn, &trust, sizeof (uint32_t),
1779 GNUNET_DISK_PERM_USER_READ |
1780 GNUNET_DISK_PERM_USER_WRITE |
1781 GNUNET_DISK_PERM_GROUP_READ |
1782 GNUNET_DISK_PERM_OTHER_READ))
1783 cp->disk_trust = cp->ppd.trust;
1791 * Notify core about a preference we have for the given peer
1792 * (to allocate more resources towards it). The change will
1793 * be communicated the next time we reserve bandwidth with
1794 * core (not instantly).
1796 * @param cp peer to reserve bandwidth from
1797 * @param pref preference change
1800 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1803 cp->inc_preference += pref;
1808 * Call this method periodically to flush trust information to disk.
1810 * @param cls closure, not used
1811 * @param tc task context, not used
1814 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1819 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_trust, NULL);
1822 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1824 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1829 * Initialize peer management subsystem.
1832 GSF_connected_peer_init_ ()
1834 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1835 ats = GNUNET_ATS_performance_init (GSF_cfg, NULL, NULL);
1836 GNUNET_assert (GNUNET_OK ==
1837 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1840 GNUNET_DISK_directory_create (trustDirectory);
1841 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1842 &cron_flush_trust, NULL);
1847 * Iterator to free peer entries.
1849 * @param cls closure, unused
1850 * @param key current key code
1851 * @param value value in the hash map (peer entry)
1852 * @return GNUNET_YES (we should continue to iterate)
1855 clean_peer (void *cls, const GNUNET_HashCode * key, void *value)
1857 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1863 * Shutdown peer management subsystem.
1866 GSF_connected_peer_done_ ()
1868 cron_flush_trust (NULL, NULL);
1869 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1870 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1872 GNUNET_free (trustDirectory);
1873 trustDirectory = NULL;
1874 GNUNET_ATS_performance_done (ats);
1880 * Iterator to remove references to LC entry.
1882 * @param cls the 'struct GSF_LocalClient*' to look for
1883 * @param key current key code
1884 * @param value value in the hash map (peer entry)
1885 * @return GNUNET_YES (we should continue to iterate)
1888 clean_local_client (void *cls, const GNUNET_HashCode * key, void *value)
1890 const struct GSF_LocalClient *lc = cls;
1891 struct GSF_ConnectedPeer *cp = value;
1894 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1895 if (cp->ppd.last_client_replies[i] == lc)
1896 cp->ppd.last_client_replies[i] = NULL;
1902 * Notification that a local client disconnected. Clean up all of our
1903 * references to the given handle.
1905 * @param lc handle to the local client (henceforth invalid)
1908 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1911 return; /* already cleaned up */
1912 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1917 /* end of gnunet-service-fs_cp.c */