2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs_cp.c
23 * @brief API to handle 'connected peers'
24 * @author Christian Grothoff
27 #include "gnunet_load_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-fs.h"
30 #include "gnunet-service-fs_cp.h"
31 #include "gnunet-service-fs_pe.h"
32 #include "gnunet-service-fs_pr.h"
33 #include "gnunet-service-fs_push.h"
37 * Ratio for moving average delay calculation. The previous
38 * average goes in with a factor of (n-1) into the calculation.
41 #define RUNAVG_DELAY_N 16
44 * How often do we flush respect values to disk?
46 #define RESPECT_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
49 * After how long do we discard a reply?
51 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
54 * Collect an instane number of statistics? May cause excessive IPC.
56 #define INSANE_STATISTICS GNUNET_NO
60 * Handle to cancel a transmission request.
62 struct GSF_PeerTransmitHandle
66 * Kept in a doubly-linked list.
68 struct GSF_PeerTransmitHandle *next;
71 * Kept in a doubly-linked list.
73 struct GSF_PeerTransmitHandle *prev;
76 * Time when this transmission request was issued.
78 struct GNUNET_TIME_Absolute transmission_request_start_time;
81 * Timeout for this request.
83 struct GNUNET_TIME_Absolute timeout;
86 * Task called on timeout, or 0 for none.
88 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
91 * Function to call to get the actual message.
93 GSF_GetMessageCallback gmc;
96 * Peer this request targets.
98 struct GSF_ConnectedPeer *cp;
106 * Size of the message to be transmitted.
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_ATS_reserve_bandwidth call (or NULL).
245 struct GNUNET_ATS_ReservationContext *rc;
248 * Task scheduled if we need to retry bandwidth reservation later.
250 GNUNET_SCHEDULER_TaskIdentifier rc_delay_task;
253 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
255 struct GNUNET_CONTAINER_MultiHashMap *request_map;
258 * Handle for an active request for transmission to this
259 * peer, or NULL (if core queue was full).
261 struct GNUNET_CORE_TransmitHandle *cth;
264 * Increase in traffic preference still to be submitted
265 * to the core service for this peer.
267 uint64_t inc_preference;
270 * Set to 1 if we're currently in the process of calling
271 * 'GNUNET_CORE_notify_transmit_ready' (so while cth is
272 * NULL, we should not call notify_transmit_ready for this
275 unsigned int cth_in_progress;
278 * Respect rating for this peer on disk.
280 uint32_t disk_respect;
283 * Which offset in "last_p2p_replies" will be updated next?
284 * (we go round-robin).
286 unsigned int last_p2p_replies_woff;
289 * Which offset in "last_client_replies" will be updated next?
290 * (we go round-robin).
292 unsigned int last_client_replies_woff;
295 * Current offset into 'last_request_times' ring buffer.
297 unsigned int last_request_times_off;
300 * GNUNET_YES if we did successfully reserve 32k bandwidth,
309 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
311 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
314 * Where do we store respect information?
316 static char *respectDirectory;
319 * Handle to ATS service.
321 static struct GNUNET_ATS_PerformanceHandle *ats;
325 * Get the filename under which we would store respect
326 * for the given peer.
328 * @param id peer to get the filename for
329 * @return filename of the form DIRECTORY/PEERID
332 get_respect_filename (const struct GNUNET_PeerIdentity *id)
334 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
337 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
338 GNUNET_asprintf (&fn, "%s%s%s", respectDirectory, DIR_SEPARATOR_STR, &fil);
344 * Find latency information in 'atsi'.
346 * @param atsi performance data
347 * @param atsi_count number of records in 'atsi'
348 * @return connection latency
350 static struct GNUNET_TIME_Relative
351 get_latency (const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
355 for (i = 0; i < atsi_count; i++)
356 if (ntohl (atsi->type) == GNUNET_ATS_QUALITY_NET_DELAY)
357 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
358 ntohl (atsi->value));
359 return GNUNET_TIME_UNIT_SECONDS;
364 * Update the performance information kept for the given peer.
366 * @param cp peer record to update
367 * @param atsi transport performance data
368 * @param atsi_count number of records in 'atsi'
371 update_atsi (struct GSF_ConnectedPeer *cp,
372 const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
374 struct GNUNET_TIME_Relative latency;
376 latency = get_latency (atsi, atsi_count);
377 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
378 /* LATER: merge atsi into cp's performance data (if we ever care...) */
383 * Return the performance data record for the given peer
385 * @param cp peer to query
386 * @return performance data record for the peer
388 struct GSF_PeerPerformanceData *
389 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
396 * Core is ready to transmit to a peer, get the message.
398 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
399 * @param size number of bytes core is willing to take
400 * @param buf where to copy the message
401 * @return number of bytes copied to buf
404 peer_transmit_ready_cb (void *cls, size_t size, void *buf);
408 * Function called by core upon success or failure of our bandwidth reservation request.
410 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
411 * @param peer identifies the peer
412 * @param amount set to the amount that was actually reserved or unreserved;
413 * either the full requested amount or zero (no partial reservations)
414 * @param res_delay if the reservation could not be satisfied (amount was 0), how
415 * long should the client wait until re-trying?
418 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
419 int32_t amount, struct GNUNET_TIME_Relative res_delay);
423 * If ready (bandwidth reserved), try to schedule transmission via
424 * core for the given handle.
426 * @param pth transmission handle to schedule
429 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
431 struct GSF_ConnectedPeer *cp;
432 struct GNUNET_PeerIdentity target;
435 if ((NULL != cp->cth) || (0 != cp->cth_in_progress))
436 return; /* already done */
437 GNUNET_assert (0 != cp->ppd.pid);
438 GNUNET_PEER_resolve (cp->ppd.pid, &target);
440 if (0 != cp->inc_preference)
442 GNUNET_ATS_change_preference (ats, &target, GNUNET_ATS_PREFERENCE_BANDWIDTH,
443 (double) cp->inc_preference,
444 GNUNET_ATS_PREFERENCE_END);
445 cp->inc_preference = 0;
448 if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
450 /* query, need reservation */
451 if (GNUNET_YES != cp->did_reserve)
452 return; /* not ready */
453 cp->did_reserve = GNUNET_NO;
454 /* reservation already done! */
455 pth->was_reserved = GNUNET_YES;
457 GNUNET_ATS_reserve_bandwidth (ats, &target, DBLOCK_SIZE,
458 &ats_reserve_callback, cp);
461 GNUNET_assert (NULL == cp->cth);
462 cp->cth_in_progress++;
464 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
465 GNUNET_TIME_absolute_get_remaining
466 (pth->timeout), &target, pth->size,
467 &peer_transmit_ready_cb, cp);
468 GNUNET_assert (NULL != cp->cth);
469 GNUNET_assert (0 < cp->cth_in_progress--);
474 * Core is ready to transmit to a peer, get the message.
476 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
477 * @param size number of bytes core is willing to take
478 * @param buf where to copy the message
479 * @return number of bytes copied to buf
482 peer_transmit_ready_cb (void *cls, size_t size, void *buf)
484 struct GSF_ConnectedPeer *cp = cls;
485 struct GSF_PeerTransmitHandle *pth = cp->pth_head;
486 struct GSF_PeerTransmitHandle *pos;
492 if (pth->size > size)
494 schedule_transmission (pth);
497 if (GNUNET_SCHEDULER_NO_TASK != pth->timeout_task)
499 GNUNET_SCHEDULER_cancel (pth->timeout_task);
500 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
502 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
503 if (GNUNET_YES == pth->is_query)
505 cp->ppd.last_request_times[(cp->last_request_times_off++) %
506 MAX_QUEUE_PER_PEER] =
507 GNUNET_TIME_absolute_get ();
508 GNUNET_assert (0 < cp->ppd.pending_queries--);
510 else if (GNUNET_NO == pth->is_query)
512 GNUNET_assert (0 < cp->ppd.pending_replies--);
514 GNUNET_LOAD_update (cp->ppd.transmission_delay,
515 GNUNET_TIME_absolute_get_duration
516 (pth->transmission_request_start_time).rel_value);
517 ret = pth->gmc (pth->gmc_cls, size, buf);
518 if (NULL != (pos = cp->pth_head))
520 GNUNET_assert (pos != pth);
521 schedule_transmission (pos);
529 * (re)try to reserve bandwidth from the given peer.
531 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
532 * @param tc scheduler context
535 retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
537 struct GSF_ConnectedPeer *cp = cls;
538 struct GNUNET_PeerIdentity target;
540 GNUNET_PEER_resolve (cp->ppd.pid, &target);
541 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
543 GNUNET_ATS_reserve_bandwidth (ats, &target, DBLOCK_SIZE,
544 &ats_reserve_callback, cp);
549 * Function called by core upon success or failure of our bandwidth reservation request.
551 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
552 * @param peer identifies the peer
553 * @param amount set to the amount that was actually reserved or unreserved;
554 * either the full requested amount or zero (no partial reservations)
555 * @param res_delay if the reservation could not be satisfied (amount was 0), how
556 * long should the client wait until re-trying?
559 ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
560 int32_t amount, struct GNUNET_TIME_Relative res_delay)
562 struct GSF_ConnectedPeer *cp = cls;
563 struct GSF_PeerTransmitHandle *pth;
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Reserved %d bytes / need to wait %s for reservation\n",
568 GNUNET_STRINGS_relative_time_to_string (res_delay, GNUNET_YES));
573 GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
576 cp->did_reserve = GNUNET_YES;
578 if ((NULL != pth) && (NULL == cp->cth) && (0 == cp->cth_in_progress))
580 /* reservation success, try transmission now! */
581 cp->cth_in_progress++;
583 GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES, pth->priority,
584 GNUNET_TIME_absolute_get_remaining
585 (pth->timeout), peer, pth->size,
586 &peer_transmit_ready_cb, cp);
587 GNUNET_assert (NULL != cp->cth);
588 GNUNET_assert (0 < cp->cth_in_progress--);
594 * A peer connected to us. Setup the connected peer
597 * @param peer identity of peer that connected
598 * @param atsi performance data for the connection
599 * @param atsi_count number of records in 'atsi'
600 * @return handle to connected peer entry
602 struct GSF_ConnectedPeer *
603 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
604 const struct GNUNET_ATS_Information *atsi,
605 unsigned int atsi_count)
607 struct GSF_ConnectedPeer *cp;
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n",
613 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
614 cp->ppd.pid = GNUNET_PEER_intern (peer);
615 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
617 GNUNET_ATS_reserve_bandwidth (ats, peer, DBLOCK_SIZE,
618 &ats_reserve_callback, cp);
619 fn = get_respect_filename (peer);
620 if ((GNUNET_YES == GNUNET_DISK_file_test (fn)) &&
621 (sizeof (respect) == GNUNET_DISK_fn_read (fn, &respect, sizeof (respect))))
622 cp->disk_respect = cp->ppd.respect = ntohl (respect);
624 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
625 GNUNET_break (GNUNET_OK ==
626 GNUNET_CONTAINER_multihashmap_put (cp_map,
627 &GSF_connected_peer_get_identity2_ (cp)->hashPubKey,
629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
630 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
631 GNUNET_CONTAINER_multihashmap_size (cp_map),
633 update_atsi (cp, atsi, atsi_count);
634 GSF_push_start_ (cp);
640 * It may be time to re-start migrating content to this
641 * peer. Check, and if so, restart migration.
643 * @param cls the 'struct GSF_ConnectedPeer'
644 * @param tc scheduler context
647 revive_migration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
649 struct GSF_ConnectedPeer *cp = cls;
650 struct GNUNET_TIME_Relative bt;
652 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
653 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
654 if (0 != bt.rel_value)
656 /* still time left... */
657 cp->mig_revive_task =
658 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
661 GSF_push_start_ (cp);
666 * Get a handle for a connected peer.
668 * @param peer peer's identity
669 * @return NULL if the peer is not currently connected
671 struct GSF_ConnectedPeer *
672 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
676 return GNUNET_CONTAINER_multihashmap_get (cp_map, &peer->hashPubKey);
681 * Handle P2P "MIGRATION_STOP" message.
683 * @param cls closure, always NULL
684 * @param other the other peer involved (sender or receiver, NULL
685 * for loopback messages where we are both sender and receiver)
686 * @param message the actual message
687 * @param atsi performance information
688 * @param atsi_count number of records in 'atsi'
689 * @return GNUNET_OK to keep the connection open,
690 * GNUNET_SYSERR to close it (signal serious error)
693 GSF_handle_p2p_migration_stop_ (void *cls,
694 const struct GNUNET_PeerIdentity *other,
695 const struct GNUNET_MessageHeader *message)
697 struct GSF_ConnectedPeer *cp;
698 const struct MigrationStopMessage *msm;
699 struct GNUNET_TIME_Relative bt;
701 msm = (const struct MigrationStopMessage *) message;
702 cp = GSF_peer_get_ (other);
708 GNUNET_STATISTICS_update (GSF_stats,
709 gettext_noop ("# migration stop messages received"),
711 bt = GNUNET_TIME_relative_ntoh (msm->duration);
712 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
713 _("Migration of content to peer `%s' blocked for %s\n"),
715 GNUNET_STRINGS_relative_time_to_string (bt, GNUNET_YES));
716 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
717 if (GNUNET_SCHEDULER_NO_TASK == cp->mig_revive_task)
720 cp->mig_revive_task =
721 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
723 fprintf (stderr, "FIX ATS DATA!\n");
724 update_atsi (cp, NULL, 0);
730 * Copy reply and free put message.
732 * @param cls the 'struct PutMessage'
733 * @param buf_size number of bytes available in buf
734 * @param buf where to copy the message, NULL on error (peer disconnect)
735 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
738 copy_reply (void *cls, size_t buf_size, void *buf)
740 struct PutMessage *pm = cls;
745 GNUNET_assert (buf_size >= ntohs (pm->header.size));
746 size = ntohs (pm->header.size);
747 memcpy (buf, pm, size);
748 GNUNET_STATISTICS_update (GSF_stats,
750 ("# replies transmitted to other peers"), 1,
756 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
765 * Free resources associated with the given peer request.
767 * @param peerreq request to free
768 * @param query associated key for the request
771 free_pending_request (struct PeerRequest *peerreq,
772 const struct GNUNET_HashCode *query)
774 struct GSF_ConnectedPeer *cp = peerreq->cp;
776 if (GNUNET_SCHEDULER_NO_TASK != peerreq->kill_task)
778 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
779 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
781 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
783 GNUNET_break (GNUNET_YES ==
784 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
786 GNUNET_free (peerreq);
791 * Cancel all requests associated with the peer.
794 * @param query hash code of the request
795 * @param value the 'struct GSF_PendingRequest'
796 * @return GNUNET_YES (continue to iterate)
799 cancel_pending_request (void *cls, const struct GNUNET_HashCode * query, void *value)
801 struct PeerRequest *peerreq = value;
802 struct GSF_PendingRequest *pr = peerreq->pr;
803 struct GSF_PendingRequestData *prd;
805 prd = GSF_pending_request_get_data_ (pr);
806 GSF_pending_request_cancel_ (pr, GNUNET_NO);
807 free_pending_request (peerreq, &prd->query);
813 * Free the given request.
815 * @param cls the request to free
816 * @param tc task context
819 peer_request_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
821 struct PeerRequest *peerreq = cls;
822 struct GSF_PendingRequest *pr = peerreq->pr;
823 struct GSF_PendingRequestData *prd;
825 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
826 prd = GSF_pending_request_get_data_ (pr);
827 cancel_pending_request (NULL, &prd->query, peerreq);
832 * The artificial delay is over, transmit the message now.
834 * @param cls the 'struct GSF_DelayedHandle' with the message
835 * @param tc scheduler context
838 transmit_delayed_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
840 struct GSF_DelayedHandle *dh = cls;
841 struct GSF_ConnectedPeer *cp = dh->cp;
843 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
844 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
846 GNUNET_free (dh->pm);
850 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
851 dh->msize, ©_reply, dh->pm);
857 * Get the randomized delay a response should be subjected to.
859 * @return desired delay
861 static struct GNUNET_TIME_Relative
862 get_randomized_delay ()
864 struct GNUNET_TIME_Relative ret;
867 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
868 GNUNET_CRYPTO_random_u32
869 (GNUNET_CRYPTO_QUALITY_WEAK,
870 2 * GSF_avg_latency.rel_value + 1));
871 #if INSANE_STATISTICS
872 GNUNET_STATISTICS_update (GSF_stats,
874 ("# artificial delays introduced (ms)"),
875 ret.rel_value, GNUNET_NO);
882 * Handle a reply to a pending request. Also called if a request
883 * expires (then with data == NULL). The handler may be called
884 * many times (depending on the request type), but will not be
885 * called during or after a call to GSF_pending_request_cancel
886 * and will also not be called anymore after a call signalling
889 * @param cls 'struct PeerRequest' this is an answer for
890 * @param eval evaluation of the result
891 * @param pr handle to the original pending request
892 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
893 * @param expiration when does 'data' expire?
894 * @param last_transmission when did we last transmit a request for this block
895 * @param type type of the block
896 * @param data response data, NULL on request expiration
897 * @param data_len number of bytes in data
900 handle_p2p_reply (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
901 struct GSF_PendingRequest *pr, uint32_t reply_anonymity_level,
902 struct GNUNET_TIME_Absolute expiration,
903 struct GNUNET_TIME_Absolute last_transmission,
904 enum GNUNET_BLOCK_Type type, const void *data,
907 struct PeerRequest *peerreq = cls;
908 struct GSF_ConnectedPeer *cp = peerreq->cp;
909 struct GSF_PendingRequestData *prd;
910 struct PutMessage *pm;
913 GNUNET_assert (data_len + sizeof (struct PutMessage) <
914 GNUNET_SERVER_MAX_MESSAGE_SIZE);
915 GNUNET_assert (peerreq->pr == pr);
916 prd = GSF_pending_request_get_data_ (pr);
919 free_pending_request (peerreq, &prd->query);
922 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
923 if ((prd->type != type) && (GNUNET_BLOCK_TYPE_ANY != prd->type))
925 GNUNET_STATISTICS_update (GSF_stats,
927 ("# replies dropped due to type mismatch"),
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Transmitting result for query `%s' to peer\n",
933 GNUNET_h2s (&prd->query));
934 GNUNET_STATISTICS_update (GSF_stats,
935 gettext_noop ("# replies received for other peers"),
937 msize = sizeof (struct PutMessage) + data_len;
938 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
943 if ((UINT32_MAX != reply_anonymity_level) && (reply_anonymity_level > 1))
945 if (reply_anonymity_level - 1 > GSF_cover_content_count)
947 GNUNET_STATISTICS_update (GSF_stats,
949 ("# replies dropped due to insufficient cover traffic"),
953 GSF_cover_content_count -= (reply_anonymity_level - 1);
956 pm = GNUNET_malloc (msize);
957 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
958 pm->header.size = htons (msize);
959 pm->type = htonl (type);
960 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
961 memcpy (&pm[1], data, data_len);
962 if ((UINT32_MAX != reply_anonymity_level) && (0 != reply_anonymity_level) &&
963 (GNUNET_YES == GSF_enable_randomized_delays))
965 struct GSF_DelayedHandle *dh;
967 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
971 GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
973 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
974 &transmit_delayed_now, dh);
978 (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
981 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
983 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
985 GNUNET_STATISTICS_update (GSF_stats,
987 ("# P2P searches destroyed due to ultimate reply"),
990 GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
996 * Increase the peer's respect by a value.
998 * @param cp which peer to change the respect value on
999 * @param value is the int value by which the
1000 * peer's credit is to be increased or decreased
1001 * @returns the actual change in respect (positive or negative)
1004 change_peer_respect (struct GSF_ConnectedPeer *cp, int value)
1008 GNUNET_assert (NULL != cp);
1011 if (cp->ppd.respect + value < cp->ppd.respect)
1013 value = UINT32_MAX - cp->ppd.respect;
1014 cp->ppd.respect = UINT32_MAX;
1017 cp->ppd.respect += value;
1021 if (cp->ppd.respect < -value)
1023 value = -cp->ppd.respect;
1024 cp->ppd.respect = 0;
1027 cp->ppd.respect += value;
1034 * We've received a request with the specified priority. Bound it
1035 * according to how much we respect the given peer.
1037 * @param prio_in requested priority
1038 * @param cp the peer making the request
1039 * @return effective priority
1042 bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
1044 #define N ((double)128.0)
1049 ld = GSF_test_get_load_too_high_ (0);
1050 if (GNUNET_SYSERR == ld)
1052 #if INSANE_STATISTICS
1053 GNUNET_STATISTICS_update (GSF_stats,
1055 ("# requests done for free (low load)"), 1,
1058 return 0; /* excess resources */
1060 if (prio_in > INT32_MAX)
1061 prio_in = INT32_MAX;
1062 ret = -change_peer_respect (cp, -(int) prio_in);
1065 if (ret > GSF_current_priorities + N)
1066 rret = GSF_current_priorities + N;
1069 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1071 if ((GNUNET_YES == ld) && (ret > 0))
1073 /* try with charging */
1074 ld = GSF_test_get_load_too_high_ (ret);
1076 if (GNUNET_YES == ld)
1078 GNUNET_STATISTICS_update (GSF_stats,
1080 ("# request dropped, priority insufficient"), 1,
1083 change_peer_respect (cp, (int) ret);
1084 return -1; /* not enough resources */
1088 GNUNET_STATISTICS_update (GSF_stats,
1090 ("# requests done for a price (normal load)"), 1,
1099 * The priority level imposes a bound on the maximum
1100 * value for the ttl that can be requested.
1102 * @param ttl_in requested ttl
1103 * @param prio given priority
1104 * @return ttl_in if ttl_in is below the limit,
1105 * otherwise the ttl-limit for the given priority
1108 bound_ttl (int32_t ttl_in, uint32_t prio)
1110 unsigned long long allowed;
1114 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1115 if (ttl_in > allowed)
1117 if (allowed >= (1 << 30))
1126 * Handle P2P "QUERY" message. Creates the pending request entry
1127 * and sets up all of the data structures to that we will
1128 * process replies properly. Does not initiate forwarding or
1129 * local database lookups.
1131 * @param other the other peer involved (sender or receiver, NULL
1132 * for loopback messages where we are both sender and receiver)
1133 * @param message the actual message
1134 * @return pending request handle, NULL on error
1136 struct GSF_PendingRequest *
1137 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1138 const struct GNUNET_MessageHeader *message)
1140 struct PeerRequest *peerreq;
1141 struct GSF_PendingRequest *pr;
1142 struct GSF_PendingRequestData *prd;
1143 struct GSF_ConnectedPeer *cp;
1144 struct GSF_ConnectedPeer *cps;
1145 const struct GNUNET_PeerIdentity *target;
1146 enum GSF_PendingRequestOptions options;
1148 const struct GetMessage *gm;
1150 const struct GNUNET_HashCode *opt;
1153 uint32_t ttl_decrement;
1156 enum GNUNET_BLOCK_Type type;
1157 GNUNET_PEER_Id spid;
1159 GNUNET_assert (other != NULL);
1160 msize = ntohs (message->size);
1161 if (msize < sizeof (struct GetMessage))
1163 GNUNET_break_op (0);
1166 GNUNET_STATISTICS_update (GSF_stats,
1168 ("# GET requests received (from other peers)"), 1,
1170 gm = (const struct GetMessage *) message;
1171 type = ntohl (gm->type);
1172 bm = ntohl (gm->hash_bitmap);
1180 if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_HashCode))
1182 GNUNET_break_op (0);
1185 opt = (const struct GNUNET_HashCode *) &gm[1];
1186 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_HashCode);
1187 /* bfsize must be power of 2, check! */
1188 if (0 != ((bfsize - 1) & bfsize))
1190 GNUNET_break_op (0);
1193 GSF_cover_query_count++;
1194 bm = ntohl (gm->hash_bitmap);
1196 cps = GSF_peer_get_ (other);
1199 /* peer must have just disconnected */
1200 GNUNET_STATISTICS_update (GSF_stats,
1202 ("# requests dropped due to initiator not being connected"),
1206 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1207 cp = GSF_peer_get_ ((const struct GNUNET_PeerIdentity *) &opt[bits++]);
1212 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1215 GNUNET_i2s ((const struct GNUNET_PeerIdentity *)
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1221 GNUNET_i2s (other));
1222 #if INSANE_STATISTICS
1223 GNUNET_STATISTICS_update (GSF_stats,
1225 ("# requests dropped due to missing reverse route"),
1230 /* note that we can really only check load here since otherwise
1231 * peers could find out that we are overloaded by not being
1232 * disconnected after sending us a malformed query... */
1233 priority = bound_priority (ntohl (gm->priority), cps);
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Dropping query from `%s', this peer is too busy.\n",
1238 GNUNET_i2s (other));
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1243 GNUNET_h2s (&gm->query), (unsigned int) type, GNUNET_i2s (other),
1247 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity
1248 *) &opt[bits++]) : NULL;
1249 options = GSF_PRO_DEFAULTS;
1251 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
1252 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1253 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 +
1254 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1256 /* don't have BW to send to peer, or would likely take longer than we have for it,
1257 * so at best indirect the query */
1259 options |= GSF_PRO_FORWARD_ONLY;
1260 spid = GNUNET_PEER_intern (other);
1261 GNUNET_assert (0 != spid);
1263 ttl = bound_ttl (ntohl (gm->ttl), priority);
1264 /* decrement ttl (always) */
1266 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1268 if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1272 GNUNET_i2s (other), ttl, ttl_decrement);
1273 GNUNET_STATISTICS_update (GSF_stats,
1275 ("# requests dropped due TTL underflow"), 1,
1277 /* integer underflow => drop (should be very rare)! */
1280 ttl -= ttl_decrement;
1282 /* test if the request already exists */
1283 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
1284 if (peerreq != NULL)
1287 prd = GSF_pending_request_get_data_ (pr);
1288 if (prd->type == type)
1290 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get ().abs_value + ttl)
1292 /* existing request has higher TTL, drop new one! */
1293 prd->priority += priority;
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 "Have existing request with higher TTL, dropping new request.\n",
1296 GNUNET_i2s (other));
1297 GNUNET_STATISTICS_update (GSF_stats,
1299 ("# requests dropped due to higher-TTL request"),
1303 /* existing request has lower TTL, drop old one! */
1304 priority += prd->priority;
1305 GSF_pending_request_cancel_ (pr, GNUNET_YES);
1306 free_pending_request (peerreq, &gm->query);
1310 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1312 pr = GSF_pending_request_create_ (options, type, &gm->query,
1315 0) ? (const char *) &opt[bits] : NULL,
1316 bfsize, ntohl (gm->filter_mutator),
1318 (uint32_t) priority, ttl, spid, GNUNET_PEER_intern (other), NULL, 0, /* replies_seen */
1319 &handle_p2p_reply, peerreq);
1320 GNUNET_assert (NULL != pr);
1322 GNUNET_break (GNUNET_OK ==
1323 GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
1325 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1326 GNUNET_STATISTICS_update (GSF_stats,
1328 ("# P2P query messages received and processed"), 1,
1330 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
1337 * Function called if there has been a timeout trying to satisfy
1338 * a transmission request.
1340 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1341 * @param tc scheduler context
1344 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1346 struct GSF_PeerTransmitHandle *pth = cls;
1347 struct GSF_ConnectedPeer *cp;
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 "Timeout trying to transmit to other peer\n");
1351 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1353 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1354 if (GNUNET_YES == pth->is_query)
1355 GNUNET_assert (0 < cp->ppd.pending_queries--);
1356 else if (GNUNET_NO == pth->is_query)
1357 GNUNET_assert (0 < cp->ppd.pending_replies--);
1358 GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
1359 if (NULL != cp->cth)
1361 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1364 pth->gmc (pth->gmc_cls, 0, NULL);
1365 GNUNET_assert (0 == cp->cth_in_progress);
1371 * Transmit a message to the given peer as soon as possible.
1372 * If the peer disconnects before the transmission can happen,
1373 * the callback is invoked with a 'NULL' buffer.
1375 * @param cp target peer
1376 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1377 * @param priority how important is this request?
1378 * @param timeout when does this request timeout (call gmc with error)
1379 * @param size number of bytes we would like to send to the peer
1380 * @param gmc function to call to get the message
1381 * @param gmc_cls closure for gmc
1382 * @return handle to cancel request
1384 struct GSF_PeerTransmitHandle *
1385 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
1386 uint32_t priority, struct GNUNET_TIME_Relative timeout,
1387 size_t size, GSF_GetMessageCallback gmc, void *gmc_cls)
1389 struct GSF_PeerTransmitHandle *pth;
1390 struct GSF_PeerTransmitHandle *pos;
1391 struct GSF_PeerTransmitHandle *prev;
1393 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1394 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1395 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1397 pth->gmc_cls = gmc_cls;
1399 pth->is_query = is_query;
1400 pth->priority = priority;
1402 /* insertion sort (by priority, descending) */
1405 while ((NULL != pos) && (pos->priority > priority))
1410 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
1411 if (GNUNET_YES == is_query)
1412 cp->ppd.pending_queries++;
1413 else if (GNUNET_NO == is_query)
1414 cp->ppd.pending_replies++;
1416 GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
1417 schedule_transmission (pth);
1423 * Cancel an earlier request for transmission.
1425 * @param pth request to cancel
1428 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1430 struct GSF_ConnectedPeer *cp;
1432 if (GNUNET_SCHEDULER_NO_TASK != pth->timeout_task)
1434 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1435 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1438 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1439 if (GNUNET_YES == pth->is_query)
1440 GNUNET_assert (0 < cp->ppd.pending_queries--);
1441 else if (GNUNET_NO == pth->is_query)
1442 GNUNET_assert (0 < cp->ppd.pending_replies--);
1448 * Report on receiving a reply; update the performance record of the given peer.
1450 * @param cp responding peer (will be updated)
1451 * @param request_time time at which the original query was transmitted
1452 * @param request_priority priority of the original request
1455 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1456 struct GNUNET_TIME_Absolute request_time,
1457 uint32_t request_priority)
1459 struct GNUNET_TIME_Relative delay;
1461 delay = GNUNET_TIME_absolute_get_duration (request_time);
1462 cp->ppd.avg_reply_delay.rel_value =
1463 (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N - 1) +
1464 delay.rel_value) / RUNAVG_DELAY_N;
1465 cp->ppd.avg_priority =
1466 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1467 request_priority) / RUNAVG_DELAY_N;
1472 * Report on receiving a reply in response to an initiating client.
1473 * Remember that this peer is good for this client.
1475 * @param cp responding peer (will be updated)
1476 * @param initiator_client local client on responsible for query
1479 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1480 struct GSF_LocalClient *initiator_client)
1482 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1483 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1488 * Report on receiving a reply in response to an initiating peer.
1489 * Remember that this peer is good for this initiating peer.
1491 * @param cp responding peer (will be updated)
1492 * @param initiator_peer other peer responsible for query
1495 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1496 const struct GSF_ConnectedPeer *initiator_peer)
1500 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1501 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1502 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1503 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1504 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1509 * A peer disconnected from us. Tear down the connected peer
1513 * @param peer identity of peer that connected
1516 GSF_peer_disconnect_handler_ (void *cls, const struct GNUNET_PeerIdentity *peer)
1518 struct GSF_ConnectedPeer *cp;
1519 struct GSF_PeerTransmitHandle *pth;
1520 struct GSF_DelayedHandle *dh;
1522 cp = GSF_peer_get_ (peer);
1524 return; /* must have been disconnect from core with
1525 * 'peer' == my_id, ignore */
1526 GNUNET_assert (GNUNET_YES ==
1527 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1528 &peer->hashPubKey, cp));
1529 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
1530 GNUNET_CONTAINER_multihashmap_size (cp_map),
1532 if (NULL != cp->migration_pth)
1534 GSF_peer_transmit_cancel_ (cp->migration_pth);
1535 cp->migration_pth = NULL;
1539 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1542 if (GNUNET_SCHEDULER_NO_TASK != cp->rc_delay_task)
1544 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1545 cp->rc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1547 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1548 &cancel_pending_request, cp);
1549 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1550 cp->request_map = NULL;
1551 GSF_plan_notify_peer_disconnect_ (cp);
1552 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1553 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1554 memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
1555 GSF_push_stop_ (cp);
1556 if (NULL != cp->cth)
1558 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1561 GNUNET_assert (0 == cp->cth_in_progress);
1562 while (NULL != (pth = cp->pth_head))
1564 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1566 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1567 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1569 GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
1570 pth->gmc (pth->gmc_cls, 0, NULL);
1573 while (NULL != (dh = cp->delayed_head))
1575 GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
1576 GNUNET_SCHEDULER_cancel (dh->delay_task);
1577 GNUNET_free (dh->pm);
1580 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1581 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1583 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1584 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1591 * Closure for 'call_iterator'.
1593 struct IterationContext
1596 * Function to call on each entry.
1598 GSF_ConnectedPeerIterator it;
1608 * Function that calls the callback for each peer.
1610 * @param cls the 'struct IterationContext*'
1611 * @param key identity of the peer
1612 * @param value the 'struct GSF_ConnectedPeer*'
1613 * @return GNUNET_YES to continue iteration
1616 call_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
1618 struct IterationContext *ic = cls;
1619 struct GSF_ConnectedPeer *cp = value;
1621 ic->it (ic->it_cls, (const struct GNUNET_PeerIdentity *) key, cp, &cp->ppd);
1627 * Iterate over all connected peers.
1629 * @param it function to call for each peer
1630 * @param it_cls closure for it
1633 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls)
1635 struct IterationContext ic;
1639 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &call_iterator, &ic);
1644 * Obtain the identity of a connected peer.
1646 * @param cp peer to get identity of
1647 * @param id identity to set (written to)
1650 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1651 struct GNUNET_PeerIdentity *id)
1653 GNUNET_assert (0 != cp->ppd.pid);
1654 GNUNET_PEER_resolve (cp->ppd.pid, id);
1659 * Obtain the identity of a connected peer.
1661 * @param cp peer to get identity of
1662 * @return reference to peer identity, valid until peer disconnects (!)
1664 const struct GNUNET_PeerIdentity *
1665 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp)
1667 GNUNET_assert (0 != cp->ppd.pid);
1668 return GNUNET_PEER_resolve2 (cp->ppd.pid);
1673 * Assemble a migration stop message for transmission.
1675 * @param cls the 'struct GSF_ConnectedPeer' to use
1676 * @param size number of bytes we're allowed to write to buf
1677 * @param buf where to copy the message
1678 * @return number of bytes copied to buf
1681 create_migration_stop_message (void *cls, size_t size, void *buf)
1683 struct GSF_ConnectedPeer *cp = cls;
1684 struct MigrationStopMessage msm;
1686 cp->migration_pth = NULL;
1689 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1690 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1691 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1692 msm.reserved = htonl (0);
1694 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1695 (cp->last_migration_block));
1696 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1697 GNUNET_STATISTICS_update (GSF_stats,
1698 gettext_noop ("# migration stop messages sent"),
1700 return sizeof (struct MigrationStopMessage);
1705 * Ask a peer to stop migrating data to us until the given point
1708 * @param cp peer to ask
1709 * @param block_time until when to block
1712 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1713 struct GNUNET_TIME_Absolute block_time)
1715 if (cp->last_migration_block.abs_value > block_time.abs_value)
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1718 "Migration already blocked for another %s\n",
1719 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining
1720 (cp->last_migration_block), GNUNET_YES));
1721 return; /* already blocked */
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %llu ms\n",
1724 (unsigned long long) GNUNET_TIME_absolute_get_remaining (block_time).rel_value);
1725 cp->last_migration_block = block_time;
1726 if (NULL != cp->migration_pth)
1727 GSF_peer_transmit_cancel_ (cp->migration_pth);
1729 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1730 GNUNET_TIME_UNIT_FOREVER_REL,
1731 sizeof (struct MigrationStopMessage),
1732 &create_migration_stop_message, cp);
1737 * Write peer-respect information to a file - flush the buffer entry!
1740 * @param key peer identity
1741 * @param value the 'struct GSF_ConnectedPeer' to flush
1742 * @return GNUNET_OK to continue iteration
1745 flush_respect (void *cls, const struct GNUNET_HashCode * key, void *value)
1747 struct GSF_ConnectedPeer *cp = value;
1750 struct GNUNET_PeerIdentity pid;
1752 if (cp->ppd.respect == cp->disk_respect)
1753 return GNUNET_OK; /* unchanged */
1754 GNUNET_assert (0 != cp->ppd.pid);
1755 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1756 fn = get_respect_filename (&pid);
1757 if (cp->ppd.respect == 0)
1759 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1760 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1761 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1765 respect = htonl (cp->ppd.respect);
1766 if (sizeof (uint32_t) ==
1767 GNUNET_DISK_fn_write (fn, &respect, sizeof (uint32_t),
1768 GNUNET_DISK_PERM_USER_READ |
1769 GNUNET_DISK_PERM_USER_WRITE |
1770 GNUNET_DISK_PERM_GROUP_READ |
1771 GNUNET_DISK_PERM_OTHER_READ))
1772 cp->disk_respect = cp->ppd.respect;
1780 * Notify core about a preference we have for the given peer
1781 * (to allocate more resources towards it). The change will
1782 * be communicated the next time we reserve bandwidth with
1783 * core (not instantly).
1785 * @param cp peer to reserve bandwidth from
1786 * @param pref preference change
1789 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1792 cp->inc_preference += pref;
1797 * Call this method periodically to flush respect information to disk.
1799 * @param cls closure, not used
1800 * @param tc task context, not used
1803 cron_flush_respect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1808 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &flush_respect, NULL);
1811 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1813 GNUNET_SCHEDULER_add_delayed_with_priority (RESPECT_FLUSH_FREQ,
1814 GNUNET_SCHEDULER_PRIORITY_HIGH,
1815 &cron_flush_respect, NULL);
1820 * Initialize peer management subsystem.
1823 GSF_connected_peer_init_ ()
1825 cp_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
1826 ats = GNUNET_ATS_performance_init (GSF_cfg, NULL, NULL);
1827 GNUNET_assert (GNUNET_OK ==
1828 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs",
1830 &respectDirectory));
1831 GNUNET_DISK_directory_create (respectDirectory);
1832 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1833 &cron_flush_respect, NULL);
1838 * Iterator to free peer entries.
1840 * @param cls closure, unused
1841 * @param key current key code
1842 * @param value value in the hash map (peer entry)
1843 * @return GNUNET_YES (we should continue to iterate)
1846 clean_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
1848 GSF_peer_disconnect_handler_ (NULL, (const struct GNUNET_PeerIdentity *) key);
1854 * Shutdown peer management subsystem.
1857 GSF_connected_peer_done_ ()
1859 cron_flush_respect (NULL, NULL);
1860 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_peer, NULL);
1861 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1863 GNUNET_free (respectDirectory);
1864 respectDirectory = NULL;
1865 GNUNET_ATS_performance_done (ats);
1871 * Iterator to remove references to LC entry.
1873 * @param cls the 'struct GSF_LocalClient*' to look for
1874 * @param key current key code
1875 * @param value value in the hash map (peer entry)
1876 * @return GNUNET_YES (we should continue to iterate)
1879 clean_local_client (void *cls, const struct GNUNET_HashCode * key, void *value)
1881 const struct GSF_LocalClient *lc = cls;
1882 struct GSF_ConnectedPeer *cp = value;
1885 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
1886 if (cp->ppd.last_client_replies[i] == lc)
1887 cp->ppd.last_client_replies[i] = NULL;
1893 * Notification that a local client disconnected. Clean up all of our
1894 * references to the given handle.
1896 * @param lc handle to the local client (henceforth invalid)
1899 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1902 return; /* already cleaned up */
1903 GNUNET_CONTAINER_multihashmap_iterate (cp_map, &clean_local_client,
1908 /* end of gnunet-service-fs_cp.c */