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)
338 /* We sometime have no latency data, i.e. if the address came from
339 peerinfo and we never had a chance to play transport-level
340 PING/PONG yet. Assume 1s in that case. */
341 return GNUNET_TIME_UNIT_SECONDS;
343 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
344 ntohl (atsi->value));
349 * Update the performance information kept for the given peer.
351 * @param cp peer record to update
352 * @param atsi transport performance data
355 update_atsi (struct GSF_ConnectedPeer *cp,
356 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
358 struct GNUNET_TIME_Relative latency;
360 latency = get_latency (atsi);
361 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
362 /* LATER: merge atsi into cp's performance data (if we ever care...) */
367 * Return the performance data record for the given peer
369 * @param cp peer to query
370 * @return performance data record for the peer
372 struct GSF_PeerPerformanceData *
373 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
380 * Core is ready to transmit to a peer, get the message.
382 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
383 * @param size number of bytes core is willing to take
384 * @param buf where to copy the message
385 * @return number of bytes copied to buf
388 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
394 * Function called by core upon success or failure of our bandwidth reservation request.
396 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
397 * @param peer identifies the peer
398 * @param bandwidth_out available amount of outbound bandwidth
399 * @param amount set to the amount that was actually reserved or unreserved;
400 * either the full requested amount or zero (no partial reservations)
401 * @param res_delay if the reservation could not be satisfied (amount was 0), how
402 * long should the client wait until re-trying?
403 * @param preference current traffic preference for the given peer
406 core_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
407 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
408 int32_t amount, struct GNUNET_TIME_Relative res_delay,
409 uint64_t preference);
413 * If ready (bandwidth reserved), try to schedule transmission via
414 * core for the given handle.
416 * @param pth transmission handle to schedule
419 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
421 struct GSF_ConnectedPeer *cp;
422 struct GNUNET_PeerIdentity target;
425 if ((NULL != pth->cth) || (0 != pth->cth_in_progress))
426 return; /* already done */
428 GNUNET_assert (0 != cp->ppd.pid);
429 GNUNET_PEER_resolve (cp->ppd.pid, &target);
430 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
432 /* query, need reservation */
433 if (GNUNET_YES != cp->did_reserve)
434 return; /* not ready */
435 cp->did_reserve = GNUNET_NO;
436 /* reservation already done! */
437 pth->was_reserved = GNUNET_YES;
438 ip = cp->inc_preference;
439 cp->inc_preference = 0;
441 GNUNET_CORE_peer_change_preference (GSF_core, &target,
442 GNUNET_TIME_UNIT_FOREVER_REL,
443 GNUNET_BANDWIDTH_VALUE_MAX,
445 &core_reserve_callback, cp);
447 GNUNET_assert (pth->cth == NULL);
448 pth->cth_in_progress++;
450 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
451 GNUNET_TIME_absolute_get_remaining
452 (pth->timeout), &target, pth->size,
453 &peer_transmit_ready_cb, pth);
454 GNUNET_assert (0 < pth->cth_in_progress--);
459 * Core is ready to transmit to a peer, get the message.
461 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
462 * @param size number of bytes core is willing to take
463 * @param buf where to copy the message
464 * @return number of bytes copied to buf
467 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
469 struct GSF_PeerTransmitHandle *pth = cls;
470 struct GSF_PeerTransmitHandle *pos;
471 struct GSF_ConnectedPeer *cp;
474 GNUNET_assert ((NULL == buf) || (pth->size <= size));
476 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
478 GNUNET_SCHEDULER_cancel (pth->timeout_task);
479 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
482 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
483 if (GNUNET_YES == pth->is_query)
485 cp->ppd.last_request_times[(cp->last_request_times_off++) %
486 MAX_QUEUE_PER_PEER] =
487 GNUNET_TIME_absolute_get ();
488 GNUNET_assert (0 < cp->ppd.pending_queries--);
490 else if (GNUNET_NO == pth->is_query)
492 GNUNET_assert (0 < cp->ppd.pending_replies--);
494 GNUNET_LOAD_update (cp->ppd.transmission_delay,
495 GNUNET_TIME_absolute_get_duration
496 (pth->transmission_request_start_time).rel_value);
497 ret = pth->gmc (pth->gmc_cls, size, buf);
498 GNUNET_assert (NULL == pth->cth);
499 for (pos = cp->pth_head; pos != NULL; pos = pos->next)
501 GNUNET_assert (pos != pth);
502 schedule_transmission (pos);
504 GNUNET_assert (pth->cth == NULL);
505 GNUNET_assert (pth->cth_in_progress == 0);
512 * (re)try to reserve bandwidth from the given peer.
514 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
515 * @param tc scheduler context
518 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
520 struct GSF_ConnectedPeer *cp = cls;
522 struct GNUNET_PeerIdentity target;
524 GNUNET_PEER_resolve (cp->ppd.pid, &target);
525 cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
526 ip = cp->inc_preference;
527 cp->inc_preference = 0;
529 GNUNET_CORE_peer_change_preference (GSF_core, &target,
530 GNUNET_TIME_UNIT_FOREVER_REL,
531 GNUNET_BANDWIDTH_VALUE_MAX,
533 &core_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 bandwidth_out available amount of outbound bandwidth
543 * @param amount set to the amount that was actually reserved or unreserved;
544 * either the full requested amount or zero (no partial reservations)
545 * @param res_delay if the reservation could not be satisfied (amount was 0), how
546 * long should the client wait until re-trying?
547 * @param preference current traffic preference for the given peer
550 core_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
551 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
552 int32_t amount, struct GNUNET_TIME_Relative res_delay,
555 struct GSF_ConnectedPeer *cp = cls;
556 struct GSF_PeerTransmitHandle *pth;
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_TRANSPORT_ATS_Information *atsi)
593 struct GSF_ConnectedPeer *cp;
597 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
598 cp->ppd.pid = GNUNET_PEER_intern (peer);
599 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
601 GNUNET_CORE_peer_change_preference (GSF_core, peer,
602 GNUNET_TIME_UNIT_FOREVER_REL,
603 GNUNET_BANDWIDTH_VALUE_MAX,
605 &core_reserve_callback, cp);
606 fn = get_trust_filename (peer);
607 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
608 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
609 cp->disk_trust = cp->ppd.trust = ntohl (trust);
611 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
612 GNUNET_break (GNUNET_OK ==
613 GNUNET_CONTAINER_multihashmap_put (cp_map, &peer->hashPubKey,
615 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
616 update_atsi (cp, atsi);
617 GSF_push_start_ (cp);
623 * It may be time to re-start migrating content to this
624 * peer. Check, and if so, restart migration.
626 * @param cls the 'struct GSF_ConnectedPeer'
627 * @param tc scheduler context
630 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
632 struct GSF_ConnectedPeer *cp = cls;
633 struct GNUNET_TIME_Relative bt;
635 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
636 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
637 if (0 != bt.rel_value)
639 /* still time left... */
640 cp->mig_revive_task =
641 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
644 GSF_push_start_ (cp);
649 * Get a handle for a connected peer.
651 * @param peer peer's identity
652 * @return NULL if the peer is not currently connected
654 struct GSF_ConnectedPeer *
655 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
659 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
664 * Handle P2P "MIGRATION_STOP" message.
666 * @param cls closure, always NULL
667 * @param other the other peer involved (sender or receiver, NULL
668 * for loopback messages where we are both sender and receiver)
669 * @param message the actual message
670 * @param atsi performance information
671 * @return GNUNET_OK to keep the connection open,
672 * GNUNET_SYSERR to close it (signal serious error)
675 GSF_handle_p2p_migration_stop_ (void *cls,
676 const struct GNUNET_PeerIdentity *other,
677 const struct GNUNET_MessageHeader *message,
678 const struct GNUNET_TRANSPORT_ATS_Information
681 struct GSF_ConnectedPeer *cp;
682 const struct MigrationStopMessage *msm;
683 struct GNUNET_TIME_Relative bt;
685 msm = (const struct MigrationStopMessage *) message;
686 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &other->hashPubKey);
692 bt = GNUNET_TIME_relative_ntoh (msm->duration);
693 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
694 _("Migration of content to peer `%s' blocked for %llu ms\n"),
695 GNUNET_i2s (other), (unsigned long long) bt.rel_value);
696 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
697 if (cp->mig_revive_task == GNUNET_SCHEDULER_NO_TASK)
700 cp->mig_revive_task =
701 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
703 update_atsi (cp, atsi);
709 * Copy reply and free put message.
711 * @param cls the 'struct PutMessage'
712 * @param buf_size number of bytes available in buf
713 * @param buf where to copy the message, NULL on error (peer disconnect)
714 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
717 copy_reply (void *cls, size_t buf_size, void *buf)
719 struct PutMessage *pm = cls;
724 GNUNET_assert (buf_size >= ntohs (pm->header.size));
725 size = ntohs (pm->header.size);
726 memcpy (buf, pm, size);
727 GNUNET_STATISTICS_update (GSF_stats,
729 ("# replies transmitted to other peers"), 1,
735 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
744 * Cancel all requests associated with the peer.
747 * @param query hash code of the request
748 * @param value the 'struct GSF_PendingRequest'
749 * @return GNUNET_YES (continue to iterate)
752 cancel_pending_request (void *cls, const GNUNET_HashCode * query, void *value)
754 struct PeerRequest *peerreq = value;
755 struct GSF_PendingRequest *pr = peerreq->pr;
756 struct GSF_ConnectedPeer *cp = peerreq->cp;
757 struct GSF_PendingRequestData *prd;
759 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
761 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
762 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
764 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
766 prd = GSF_pending_request_get_data_ (pr);
767 GNUNET_break (GNUNET_YES ==
768 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
769 &prd->query, peerreq));
770 GSF_pending_request_cancel_ (pr, GNUNET_NO);
771 GNUNET_free (peerreq);
777 * Free the given request.
779 * @param cls the request to free
780 * @param tc task context
783 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
785 struct PeerRequest *peerreq = cls;
786 struct GSF_PendingRequest *pr = peerreq->pr;
787 struct GSF_PendingRequestData *prd;
789 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
790 prd = GSF_pending_request_get_data_ (pr);
791 cancel_pending_request (NULL, &prd->query, peerreq);
796 * The artificial delay is over, transmit the message now.
798 * @param cls the 'struct GSF_DelayedHandle' with the message
799 * @param tc scheduler context
802 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
804 struct GSF_DelayedHandle *dh = cls;
805 struct GSF_ConnectedPeer *cp = dh->cp;
807 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
808 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
810 GNUNET_free (dh->pm);
814 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
815 dh->msize, ©_reply, dh->pm);
821 * Get the randomized delay a response should be subjected to.
823 * @return desired delay
825 static struct GNUNET_TIME_Relative
826 get_randomized_delay ()
828 struct GNUNET_TIME_Relative ret;
830 /* FIXME: replace 5000 with something relating to current observed P2P message latency */
832 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
833 GNUNET_CRYPTO_random_u32
834 (GNUNET_CRYPTO_QUALITY_WEAK, 5000));
835 GNUNET_STATISTICS_update (GSF_stats,
837 ("# artificial delays introduced (ms)"),
838 ret.rel_value, GNUNET_NO);
845 * Handle a reply to a pending request. Also called if a request
846 * expires (then with data == NULL). The handler may be called
847 * many times (depending on the request type), but will not be
848 * called during or after a call to GSF_pending_request_cancel
849 * and will also not be called anymore after a call signalling
852 * @param cls 'struct PeerRequest' this is an answer for
853 * @param eval evaluation of the result
854 * @param pr handle to the original pending request
855 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
856 * @param expiration when does 'data' expire?
857 * @param type type of the block
858 * @param data response data, NULL on request expiration
859 * @param data_len number of bytes in data
862 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
863 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
864 struct GNUNET_TIME_Absolute expiration,
865 enum GNUNET_BLOCK_Type type, const void *data,
868 struct PeerRequest *peerreq = cls;
869 struct GSF_ConnectedPeer *cp = peerreq->cp;
870 struct GSF_PendingRequestData *prd;
871 struct PutMessage *pm;
874 GNUNET_assert (data_len + sizeof (struct PutMessage) <
875 GNUNET_SERVER_MAX_MESSAGE_SIZE);
876 GNUNET_assert (peerreq->pr == pr);
877 prd = GSF_pending_request_get_data_ (pr);
880 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
882 GNUNET_break (GNUNET_YES ==
883 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
884 &prd->query, peerreq));
885 GNUNET_free (peerreq);
888 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
889 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Transmitting result for query `%s' to peer\n",
897 GNUNET_h2s (&prd->query));
899 GNUNET_STATISTICS_update (GSF_stats,
900 gettext_noop ("# replies received for other peers"),
902 msize = sizeof (struct PutMessage) + data_len;
903 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
908 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level > 1))
910 if (reply_anonymity_level - 1 > GSF_cover_content_count)
912 GNUNET_STATISTICS_update (GSF_stats,
914 ("# replies dropped due to insufficient cover traffic"),
918 GSF_cover_content_count -= (reply_anonymity_level - 1);
921 pm = GNUNET_malloc (msize);
922 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
923 pm->header.size = htons (msize);
924 pm->type = htonl (type);
925 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
926 memcpy (&pm[1], data, data_len);
927 if ((reply_anonymity_level != UINT32_MAX) && (reply_anonymity_level != 0) &&
928 (GSF_enable_randomized_delays == GNUNET_YES))
930 struct GSF_DelayedHandle *dh;
932 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
936 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
938 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
939 &transmit_delayed_now, dh);
943 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
946 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
948 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
950 GNUNET_STATISTICS_update (GSF_stats,
952 ("# P2P searches destroyed due to ultimate reply"),
955 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
961 * Increase the host credit by a value.
963 * @param cp which peer to change the trust value on
964 * @param value is the int value by which the
965 * host credit is to be increased or decreased
966 * @returns the actual change in trust (positive or negative)
969 change_host_trust (struct GSF_ConnectedPeer *cp, int value)
973 GNUNET_assert (cp != NULL);
976 if (cp->ppd.trust + value < cp->ppd.trust)
978 value = UINT32_MAX - cp->ppd.trust;
979 cp->ppd.trust = UINT32_MAX;
982 cp->ppd.trust += value;
986 if (cp->ppd.trust < -value)
988 value = -cp->ppd.trust;
992 cp->ppd.trust += value;
999 * We've received a request with the specified priority. Bound it
1000 * according to how much we trust the given peer.
1002 * @param prio_in requested priority
1003 * @param cp the peer making the request
1004 * @return effective priority
1007 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1009 #define N ((double)128.0)
1014 ld = GSF_test_get_load_too_high_ (0);
1015 if (ld == GNUNET_SYSERR)
1017 GNUNET_STATISTICS_update (GSF_stats,
1019 ("# requests done for free (low load)"), 1,
1021 return 0; /* excess resources */
1023 if (prio_in > INT32_MAX)
1024 prio_in = INT32_MAX;
1025 ret = -change_host_trust (cp, -(int) prio_in);
1028 if (ret > GSF_current_priorities + N)
1029 rret = GSF_current_priorities + N;
1032 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1034 if ((ld == GNUNET_YES) && (ret > 0))
1036 /* try with charging */
1037 ld = GSF_test_get_load_too_high_ (ret);
1039 if (ld == GNUNET_YES)
1041 GNUNET_STATISTICS_update (GSF_stats,
1043 ("# request dropped, priority insufficient"), 1,
1046 change_host_trust (cp, (int) ret);
1047 return -1; /* not enough resources */
1051 GNUNET_STATISTICS_update (GSF_stats,
1053 ("# requests done for a price (normal load)"), 1,
1062 * The priority level imposes a bound on the maximum
1063 * value for the ttl that can be requested.
1065 * @param ttl_in requested ttl
1066 * @param prio given priority
1067 * @return ttl_in if ttl_in is below the limit,
1068 * otherwise the ttl-limit for the given priority
1071 bound_ttl (int32_t ttl_in, uint32_t prio)
1073 unsigned long long allowed;
1077 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1078 if (ttl_in > allowed)
1080 if (allowed >= (1 << 30))
1089 * Handle P2P "QUERY" message. Creates the pending request entry
1090 * and sets up all of the data structures to that we will
1091 * process replies properly. Does not initiate forwarding or
1092 * local database lookups.
1094 * @param other the other peer involved (sender or receiver, NULL
1095 * for loopback messages where we are both sender and receiver)
1096 * @param message the actual message
1097 * @return pending request handle, NULL on error
1099 struct GSF_PendingRequest *
1100 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1101 const struct GNUNET_MessageHeader *message)
1103 struct PeerRequest *peerreq;
1104 struct GSF_PendingRequest *pr;
1105 struct GSF_PendingRequestData *prd;
1106 struct GSF_ConnectedPeer *cp;
1107 struct GSF_ConnectedPeer *cps;
1108 const GNUNET_HashCode *namespace;
1109 const struct GNUNET_PeerIdentity *target;
1110 enum GSF_PendingRequestOptions options;
1112 const struct GetMessage *gm;
1114 const GNUNET_HashCode *opt;
1117 uint32_t ttl_decrement;
1120 enum GNUNET_BLOCK_Type type;
1121 GNUNET_PEER_Id spid;
1123 GNUNET_assert (other != NULL);
1124 msize = ntohs (message->size);
1125 if (msize < sizeof (struct GetMessage))
1127 GNUNET_break_op (0);
1130 GNUNET_STATISTICS_update (GSF_stats,
1132 ("# GET requests received (from other peers)"), 1,
1134 gm = (const struct GetMessage *) message;
1135 type = ntohl (gm->type);
1136 bm = ntohl (gm->hash_bitmap);
1144 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
1146 GNUNET_break_op (0);
1149 opt = (const GNUNET_HashCode *) &gm[1];
1150 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
1151 /* bfsize must be power of 2, check! */
1152 if (0 != ((bfsize - 1) & bfsize))
1154 GNUNET_break_op (0);
1157 GSF_cover_query_count++;
1158 bm = ntohl (gm->hash_bitmap);
1160 cps = GNUNET_CONTAINER_multihashmap_get (cp_map, &other->hashPubKey);
1163 /* peer must have just disconnected */
1164 GNUNET_STATISTICS_update (GSF_stats,
1166 ("# requests dropped due to initiator not being connected"),
1170 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1171 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &opt[bits++]);
1177 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1180 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1186 GNUNET_i2s (other));
1188 GNUNET_STATISTICS_update (GSF_stats,
1190 ("# requests dropped due to missing reverse route"),
1194 /* note that we can really only check load here since otherwise
1195 * peers could find out that we are overloaded by not being
1196 * disconnected after sending us a malformed query... */
1197 priority = bound_priority (ntohl (gm->priority), cps);
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Dropping query from `%s', this peer is too busy.\n",
1203 GNUNET_i2s (other));
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1210 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1213 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1214 if ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace == NULL))
1216 GNUNET_break_op (0);
1219 if ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) && (namespace != NULL))
1221 GNUNET_break_op (0);
1226 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1227 *) &opt[bits++]) : NULL;
1230 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1231 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1232 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1233 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1235 /* don't have BW to send to peer, or would likely take longer than we have for it,
1236 * so at best indirect the query */
1238 options |= GSF_PRO_FORWARD_ONLY;
1239 spid = GNUNET_PEER_intern (other);
1240 GNUNET_assert (0 != spid);
1242 ttl = bound_ttl (ntohl (gm->ttl), priority);
1243 /* decrement ttl (always) */
1245 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1247 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1251 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1252 GNUNET_i2s (other), ttl, ttl_decrement);
1254 GNUNET_STATISTICS_update (GSF_stats,
1256 ("# requests dropped due TTL underflow"), 1,
1258 /* integer underflow => drop (should be very rare)! */
1261 ttl -= ttl_decrement;
1263 /* test if the request already exists */
1264 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1265 if (peerreq != NULL)
1268 prd = GSF_pending_request_get_data_ (pr);
1269 if ((prd->type == type) &&
1270 ((type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1271 (0 == memcmp (&prd->namespace, namespace, sizeof (GNUNET_HashCode)))))
1273 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1275 /* existing request has higher TTL, drop new one! */
1276 prd->priority += priority;
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Have existing request with higher TTL, dropping new request.\n",
1280 GNUNET_i2s (other));
1282 GNUNET_STATISTICS_update (GSF_stats,
1284 ("# requests dropped due to higher-TTL request"),
1288 /* existing request has lower TTL, drop old one! */
1289 GNUNET_STATISTICS_update (GSF_stats,
1290 gettext_noop ("# P2P searches active"), -1,
1292 priority += prd->priority;
1293 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1294 GNUNET_assert (GNUNET_YES ==
1295 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
1298 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
1300 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
1301 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
1303 GNUNET_free (peerreq);
1307 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1309 pr = GSF_pending_request_create_ (options, type, &gm->query, namespace,
1312 0) ? (const char *) &opt[bits] : NULL,
1313 bfsize, ntohl (gm->filter_mutator),
1315 (uint32_t) priority, ttl, spid, NULL, 0, /* replies_seen */
1316 &handle_p2p_reply, peerreq);
1317 GNUNET_assert (NULL != pr);
1319 GNUNET_break (GNUNET_OK ==
1320 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1322 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1323 GNUNET_STATISTICS_update (GSF_stats,
1325 ("# P2P query messages received and processed"), 1,
1327 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1334 * Function called if there has been a timeout trying to satisfy
1335 * a transmission request.
1337 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1338 * @param tc scheduler context
1341 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1343 struct GSF_PeerTransmitHandle *pth = cls;
1344 struct GSF_ConnectedPeer *cp;
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "Timeout trying to transmit to other peer\n");
1350 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1352 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1353 if (GNUNET_YES == pth->is_query)
1354 GNUNET_assert (0 < cp->ppd.pending_queries--);
1355 else if (GNUNET_NO == pth->is_query)
1356 GNUNET_assert (0 < cp->ppd.pending_replies--);
1357 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1358 if (NULL != pth->cth)
1360 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1363 pth->gmc (pth->gmc_cls, 0, NULL);
1364 GNUNET_assert (0 == pth->cth_in_progress);
1370 * Transmit a message to the given peer as soon as possible.
1371 * If the peer disconnects before the transmission can happen,
1372 * the callback is invoked with a 'NULL' buffer.
1374 * @param cp target peer
1375 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1376 * @param priority how important is this request?
1377 * @param timeout when does this request timeout (call gmc with error)
1378 * @param size number of bytes we would like to send to the peer
1379 * @param gmc function to call to get the message
1380 * @param gmc_cls closure for gmc
1381 * @return handle to cancel request
1383 struct GSF_PeerTransmitHandle *
1384 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1385 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1386 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1388 struct GSF_PeerTransmitHandle *pth;
1389 struct GSF_PeerTransmitHandle *pos;
1390 struct GSF_PeerTransmitHandle *prev;
1392 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1393 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1394 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1396 pth->gmc_cls = gmc_cls;
1398 pth->is_query = is_query;
1399 pth->priority = priority;
1401 /* insertion sort (by priority, descending) */
1404 while ((pos != NULL) && (pos->priority > priority))
1410 GNUNET_CONTAINER_DLL_insert (cp->pth_head, cp->pth_tail, pth);
1412 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1413 if (GNUNET_YES == is_query)
1414 cp->ppd.pending_queries++;
1415 else if (GNUNET_NO == is_query)
1416 cp->ppd.pending_replies++;
1418 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1419 schedule_transmission (pth);
1425 * Cancel an earlier request for transmission.
1427 * @param pth request to cancel
1430 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1432 struct GSF_ConnectedPeer *cp;
1434 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1436 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1437 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1439 if (NULL != pth->cth)
1441 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1445 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1446 if (GNUNET_YES == pth->is_query)
1447 GNUNET_assert (0 < cp->ppd.pending_queries--);
1448 else if (GNUNET_NO == pth->is_query)
1449 GNUNET_assert (0 < cp->ppd.pending_replies--);
1450 GNUNET_assert (0 == pth->cth_in_progress);
1456 * Report on receiving a reply; update the performance record of the given peer.
1458 * @param cp responding peer (will be updated)
1459 * @param request_time time at which the original query was transmitted
1460 * @param request_priority priority of the original request
1463 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1464 struct GNUNET_TIME_Absolute request_time,
1465 uint32_t request_priority)
1467 struct GNUNET_TIME_Relative delay;
1469 delay = GNUNET_TIME_absolute_get_duration (request_time);
1470 cp->ppd.avg_reply_delay.rel_value =
1471 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1472 delay.rel_value) / RUNAVG_DELAY_N;
1473 cp->ppd.avg_priority =
1474 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1475 request_priority) / RUNAVG_DELAY_N;
1480 * Report on receiving a reply in response to an initiating client.
1481 * Remember that this peer is good for this client.
1483 * @param cp responding peer (will be updated)
1484 * @param initiator_client local client on responsible for query
1487 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1488 struct GSF_LocalClient *initiator_client)
1490 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1491 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1496 * Report on receiving a reply in response to an initiating peer.
1497 * Remember that this peer is good for this initiating peer.
1499 * @param cp responding peer (will be updated)
1500 * @param initiator_peer other peer responsible for query
1503 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1504 const struct GSF_ConnectedPeer *initiator_peer)
1508 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1509 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1510 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1511 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1512 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1517 * Method called whenever a given peer has a status change.
1519 * @param cls closure
1520 * @param peer peer identity this notification is about
1521 * @param bandwidth_in available amount of inbound bandwidth
1522 * @param bandwidth_out available amount of outbound bandwidth
1523 * @param timeout absolute time when this peer will time out
1524 * unless we see some further activity from it
1525 * @param atsi status information
1528 GSF_peer_status_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer,
1529 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1530 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1531 struct GNUNET_TIME_Absolute timeout,
1532 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1534 struct GSF_ConnectedPeer *cp;
1536 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
1537 GNUNET_assert (NULL != cp);
1538 update_atsi (cp, atsi);
1543 * A peer disconnected from us. Tear down the connected peer
1547 * @param peer identity of peer that connected
1550 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1552 struct GSF_ConnectedPeer *cp;
1553 struct GSF_PeerTransmitHandle *pth;
1554 struct GSF_DelayedHandle *dh;
1556 cp = GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
1558 return; /* must have been disconnect from core with
1559 * 'peer' == my_id, ignore */
1560 GNUNET_assert (GNUNET_YES ==
1561 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1562 &peer->hashPubKey, cp));
1563 if (NULL != cp->migration_pth)
1565 GSF_peer_transmit_cancel_ (cp->migration_pth);
1566 cp->migration_pth = NULL;
1568 if (NULL != cp->irc)
1570 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1573 if (GNUNET_SCHEDULER_NO_TASK != cp->irc_delay_task)
1575 GNUNET_SCHEDULER_cancel (cp->irc_delay_task);
1576 cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1578 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1579 &cancel_pending_request, cp);
1580 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1581 cp->request_map = NULL;
1582 GSF_plan_notify_peer_disconnect_ (cp);
1583 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1584 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1585 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1586 GSF_push_stop_ (cp);
1587 while (NULL != (pth = cp->pth_head))
1589 if (NULL != pth->cth)
1591 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1594 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1596 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1597 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1599 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1600 GNUNET_assert (0 == pth->cth_in_progress);
1601 pth->gmc (pth->gmc_cls, 0, NULL);
1604 while (NULL != (dh = cp->delayed_head))
1606 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1607 GNUNET_SCHEDULER_cancel (dh->delay_task);
1608 GNUNET_free (dh->pm);
1611 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1612 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1614 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1615 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1622 * Closure for 'call_iterator'.
1624 struct IterationContext
1627 * Function to call on each entry.
1629 GSF_ConnectedPeerIterator it;
1639 * Function that calls the callback for each peer.
1641 * @param cls the 'struct IterationContext*'
1642 * @param key identity of the peer
1643 * @param value the 'struct GSF_ConnectedPeer*'
1644 * @return GNUNET_YES to continue iteration
1647 call_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1649 struct IterationContext *ic = cls;
1650 struct GSF_ConnectedPeer *cp = value;
1652 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1658 * Iterate over all connected peers.
1660 * @param it function to call for each peer
1661 * @param it_cls closure for it
1664 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1666 struct IterationContext ic;
1670 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1675 * Obtain the identity of a connected peer.
1677 * @param cp peer to reserve bandwidth from
1678 * @param id identity to set (written to)
1681 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1682 struct GNUNET_PeerIdentity *id)
1684 GNUNET_assert (0 != cp->ppd.pid);
1685 GNUNET_PEER_resolve (cp->ppd.pid, id);
1690 * Assemble a migration stop message for transmission.
1692 * @param cls the 'struct GSF_ConnectedPeer' to use
1693 * @param size number of bytes we're allowed to write to buf
1694 * @param buf where to copy the message
1695 * @return number of bytes copied to buf
1698 create_migration_stop_message (void *cls, size_t size, void *buf)
1700 struct GSF_ConnectedPeer *cp = cls;
1701 struct MigrationStopMessage msm;
1703 cp->migration_pth = NULL;
1706 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1707 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1708 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1709 msm.reserved = htonl (0);
1711 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1712 (cp->last_migration_block));
1713 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1714 return sizeof (struct MigrationStopMessage);
1719 * Ask a peer to stop migrating data to us until the given point
1722 * @param cp peer to ask
1723 * @param block_time until when to block
1726 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1727 struct GNUNET_TIME_Relative block_time)
1729 if (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value >
1730 block_time.rel_value)
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 "Migration already blocked for another %llu ms\n",
1735 (unsigned long long)
1736 GNUNET_TIME_absolute_get_remaining
1737 (cp->last_migration_block).rel_value);
1739 return; /* already blocked */
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1743 (unsigned long long) block_time.rel_value);
1745 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1746 if (cp->migration_pth != NULL)
1747 GSF_peer_transmit_cancel_ (cp->migration_pth);
1749 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1750 GNUNET_TIME_UNIT_FOREVER_REL,
1751 sizeof (struct MigrationStopMessage),
1752 &create_migration_stop_message, cp);
1757 * Write host-trust information to a file - flush the buffer entry!
1759 * @param cls closure, not used
1760 * @param key host identity
1761 * @param value the 'struct GSF_ConnectedPeer' to flush
1762 * @return GNUNET_OK to continue iteration
1765 flush_trust (void *cls, const GNUNET_HashCode * key, void *value)
1767 struct GSF_ConnectedPeer *cp = value;
1770 struct GNUNET_PeerIdentity pid;
1772 if (cp->ppd.trust == cp->disk_trust)
1773 return GNUNET_OK; /* unchanged */
1774 GNUNET_assert (0 != cp->ppd.pid);
1775 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1776 fn = get_trust_filename (&pid);
1777 if (cp->ppd.trust == 0)
1779 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1780 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1781 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1785 trust = htonl (cp->ppd.trust);
1786 if (sizeof (uint32_t) ==
1787 GNUNET_DISK_fn_write (fn, &trust, sizeof (uint32_t),
1788 GNUNET_DISK_PERM_USER_READ |
1789 GNUNET_DISK_PERM_USER_WRITE |
1790 GNUNET_DISK_PERM_GROUP_READ |
1791 GNUNET_DISK_PERM_OTHER_READ))
1792 cp->disk_trust = cp->ppd.trust;
1800 * Notify core about a preference we have for the given peer
1801 * (to allocate more resources towards it). The change will
1802 * be communicated the next time we reserve bandwidth with
1803 * core (not instantly).
1805 * @param cp peer to reserve bandwidth from
1806 * @param pref preference change
1809 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1812 cp->inc_preference += pref;
1817 * Call this method periodically to flush trust information to disk.
1819 * @param cls closure, not used
1820 * @param tc task context, not used
1823 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1828 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_trust, NULL);
1831 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1833 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1838 * Initialize peer management subsystem.
1841 GSF_connected_peer_init_ ()
1843 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1844 GNUNET_assert (GNUNET_OK ==
1845 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1848 GNUNET_DISK_directory_create (trustDirectory);
1849 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1850 &cron_flush_trust, NULL);
1855 * Iterator to free peer entries.
1857 * @param cls closure, unused
1858 * @param key current key code
1859 * @param value value in the hash map (peer entry)
1860 * @return GNUNET_YES (we should continue to iterate)
1863 clean_peer (void *cls, const GNUNET_HashCode * key, void *value)
1865 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1871 * Shutdown peer management subsystem.
1874 GSF_connected_peer_done_ ()
1876 cron_flush_trust (NULL, NULL);
1877 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1878 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1880 GNUNET_free (trustDirectory);
1881 trustDirectory = NULL;
1886 * Iterator to remove references to LC entry.
1888 * @param cls the 'struct GSF_LocalClient*' to look for
1889 * @param key current key code
1890 * @param value value in the hash map (peer entry)
1891 * @return GNUNET_YES (we should continue to iterate)
1894 clean_local_client (void *cls, const GNUNET_HashCode * key, void *value)
1896 const struct GSF_LocalClient *lc = cls;
1897 struct GSF_ConnectedPeer *cp = value;
1900 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1901 if (cp->ppd.last_client_replies[i] == lc)
1902 cp->ppd.last_client_replies[i] = NULL;
1908 * Notification that a local client disconnected. Clean up all of our
1909 * references to the given handle.
1911 * @param lc handle to the local client (henceforth invalid)
1914 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1917 return; /* already cleaned up */
1918 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1923 /* end of gnunet-service-fs_cp.c */