2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file fs/gnunet-service-fs_cp.c
22 * @brief API to handle 'connected peers'
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
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"
33 #include "gnunet_peerstore_service.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 struct GNUNET_SCHEDULER_Task *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;
101 * Closure for @e gmc.
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 struct GNUNET_SCHEDULER_Task *delay_task;
160 * Size of the message.
168 * Information per peer and request.
174 * Handle to generic request (generic: from peer or local client).
176 struct GSF_PendingRequest *pr;
179 * Which specific peer issued this request?
181 struct GSF_ConnectedPeer *cp;
184 * Task for asynchronous stopping of this request.
186 struct GNUNET_SCHEDULER_Task *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 struct GNUNET_SCHEDULER_Task *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 struct GNUNET_SCHEDULER_Task *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 @e cth is
272 * NULL, we should not call notify_transmit_ready for this
275 unsigned int cth_in_progress;
278 * Number of entries in @e delayed_head DLL.
280 unsigned int delay_queue_size;
283 * Respect rating for this peer on disk.
285 uint32_t disk_respect;
288 * Which offset in @e last_p2p_replies will be updated next?
289 * (we go round-robin).
291 unsigned int last_p2p_replies_woff;
294 * Which offset in @e last_client_replies will be updated next?
295 * (we go round-robin).
297 unsigned int last_client_replies_woff;
300 * Current offset into @e last_request_times ring buffer.
302 unsigned int last_request_times_off;
305 * #GNUNET_YES if we did successfully reserve 32k bandwidth,
311 * Function called when the creation of this record is complete.
313 GSF_ConnectedPeerCreationCallback creation_cb;
316 * Closure for @e creation_cb
318 void *creation_cb_cls;
321 * Handle to the PEERSTORE iterate request for peer respect value
323 struct GNUNET_PEERSTORE_IterateContext *respect_iterate_req;
329 * Map from peer identities to `struct GSF_ConnectPeer` entries.
331 static struct GNUNET_CONTAINER_MultiPeerMap *cp_map;
334 * Handle to peerstore service.
336 static struct GNUNET_PEERSTORE_Handle *peerstore;
339 * Task used to flush respect values to disk.
341 static struct GNUNET_SCHEDULER_Task *fr_task;
345 * Update the latency information kept for the given peer.
347 * @param id peer record to update
348 * @param latency current latency value
351 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
352 struct GNUNET_TIME_Relative latency)
354 struct GSF_ConnectedPeer *cp;
356 cp = GSF_peer_get_ (id);
358 return; /* we're not yet connected at the core level, ignore */
359 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay,
365 * Return the performance data record for the given peer
367 * @param cp peer to query
368 * @return performance data record for the peer
370 struct GSF_PeerPerformanceData *
371 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
378 * Core is ready to transmit to a peer, get the message.
380 * @param cls the `struct GSF_PeerTransmitHandle` of the message
381 * @param size number of bytes core is willing to take
382 * @param buf where to copy the message
383 * @return number of bytes copied to @a buf
386 peer_transmit_ready_cb (void *cls,
392 * Function called by core upon success or failure of our bandwidth reservation request.
394 * @param cls the `struct GSF_ConnectedPeer` of the peer for which we made the request
395 * @param peer identifies the peer
396 * @param amount set to the amount that was actually reserved or unreserved;
397 * either the full requested amount or zero (no partial reservations)
398 * @param res_delay if the reservation could not be satisfied (amount was 0), how
399 * long should the client wait until re-trying?
402 ats_reserve_callback (void *cls,
403 const struct GNUNET_PeerIdentity *peer,
405 struct GNUNET_TIME_Relative res_delay);
409 * If ready (bandwidth reserved), try to schedule transmission via
410 * core for the given handle.
412 * @param pth transmission handle to schedule
415 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
417 struct GSF_ConnectedPeer *cp;
418 struct GNUNET_PeerIdentity target;
421 if ((NULL != cp->cth) || (0 != cp->cth_in_progress))
422 return; /* already done */
423 GNUNET_assert (0 != cp->ppd.pid);
424 GNUNET_PEER_resolve (cp->ppd.pid, &target);
426 if (0 != cp->inc_preference)
428 GNUNET_ATS_performance_change_preference (GSF_ats,
430 GNUNET_ATS_PREFERENCE_BANDWIDTH,
431 (double) cp->inc_preference,
432 GNUNET_ATS_PREFERENCE_END);
433 cp->inc_preference = 0;
436 if ( (GNUNET_YES == pth->is_query) &&
437 (GNUNET_YES != pth->was_reserved) )
439 /* query, need reservation */
440 if (GNUNET_YES != cp->did_reserve)
441 return; /* not ready */
442 cp->did_reserve = GNUNET_NO;
443 /* reservation already done! */
444 pth->was_reserved = GNUNET_YES;
445 cp->rc = GNUNET_ATS_reserve_bandwidth (GSF_ats,
448 &ats_reserve_callback,
452 GNUNET_assert (NULL == cp->cth);
453 cp->cth_in_progress++;
455 GNUNET_CORE_notify_transmit_ready (GSF_core,
457 GNUNET_CORE_PRIO_BACKGROUND,
458 GNUNET_TIME_absolute_get_remaining (pth->timeout),
461 &peer_transmit_ready_cb, cp);
462 GNUNET_assert (NULL != cp->cth);
463 GNUNET_assert (0 < cp->cth_in_progress--);
468 * Core is ready to transmit to a peer, get the message.
470 * @param cls the `struct GSF_PeerTransmitHandle` of the message
471 * @param size number of bytes core is willing to take
472 * @param buf where to copy the message
473 * @return number of bytes copied to @a buf
476 peer_transmit_ready_cb (void *cls,
480 struct GSF_ConnectedPeer *cp = cls;
481 struct GSF_PeerTransmitHandle *pth = cp->pth_head;
482 struct GSF_PeerTransmitHandle *pos;
488 if (pth->size > size)
490 schedule_transmission (pth);
493 if (NULL != pth->timeout_task)
495 GNUNET_SCHEDULER_cancel (pth->timeout_task);
496 pth->timeout_task = NULL;
498 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
501 if (GNUNET_YES == pth->is_query)
503 cp->ppd.last_request_times[(cp->last_request_times_off++) %
504 MAX_QUEUE_PER_PEER] =
505 GNUNET_TIME_absolute_get ();
506 GNUNET_assert (0 < cp->ppd.pending_queries--);
508 else if (GNUNET_NO == pth->is_query)
510 GNUNET_assert (0 < cp->ppd.pending_replies--);
512 GNUNET_LOAD_update (cp->ppd.transmission_delay,
513 GNUNET_TIME_absolute_get_duration
514 (pth->transmission_request_start_time).rel_value_us);
515 ret = pth->gmc (pth->gmc_cls, size, buf);
516 if (NULL != (pos = cp->pth_head))
518 GNUNET_assert (pos != pth);
519 schedule_transmission (pos);
527 * (re)try to reserve bandwidth from the given peer.
529 * @param cls the `struct GSF_ConnectedPeer` to reserve from
532 retry_reservation (void *cls)
534 struct GSF_ConnectedPeer *cp = cls;
535 struct GNUNET_PeerIdentity target;
537 GNUNET_PEER_resolve (cp->ppd.pid, &target);
538 cp->rc_delay_task = NULL;
540 GNUNET_ATS_reserve_bandwidth (GSF_ats,
543 &ats_reserve_callback, cp);
548 * Function called by core upon success or failure of our bandwidth reservation request.
550 * @param cls the `struct GSF_ConnectedPeer` of the peer for which we made the request
551 * @param peer identifies the peer
552 * @param amount set to the amount that was actually reserved or unreserved;
553 * either the full requested amount or zero (no partial reservations)
554 * @param res_delay if the reservation could not be satisfied (amount was 0), how
555 * long should the client wait until re-trying?
558 ats_reserve_callback (void *cls,
559 const struct GNUNET_PeerIdentity *peer,
561 struct GNUNET_TIME_Relative res_delay)
563 struct GSF_ConnectedPeer *cp = cls;
564 struct GSF_PeerTransmitHandle *pth;
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567 "Reserved %d bytes / need to wait %s for reservation\n",
569 GNUNET_STRINGS_relative_time_to_string (res_delay, GNUNET_YES));
574 GNUNET_SCHEDULER_add_delayed (res_delay,
579 cp->did_reserve = GNUNET_YES;
581 if ( (NULL != pth) &&
583 (0 == cp->cth_in_progress) )
585 /* reservation success, try transmission now! */
586 cp->cth_in_progress++;
588 GNUNET_CORE_notify_transmit_ready (GSF_core,
590 GNUNET_CORE_PRIO_BACKGROUND,
591 GNUNET_TIME_absolute_get_remaining (pth->timeout),
594 &peer_transmit_ready_cb,
596 GNUNET_assert (NULL != cp->cth);
597 GNUNET_assert (0 < cp->cth_in_progress--);
603 * Function called by PEERSTORE with peer respect record
605 * @param cls handle to connected peer entry
606 * @param record peerstore record information
607 * @param emsg error message, or NULL if no errors
610 peer_respect_cb (void *cls,
611 const struct GNUNET_PEERSTORE_Record *record,
614 struct GSF_ConnectedPeer *cp = cls;
616 GNUNET_assert (NULL != cp->respect_iterate_req);
617 if ((NULL != record) && (sizeof (cp->disk_respect) == record->value_size))
618 cp->disk_respect = cp->ppd.respect = *((uint32_t *)record->value);
619 GSF_push_start_ (cp);
620 if (NULL != cp->creation_cb)
621 cp->creation_cb (cp->creation_cb_cls, cp);
623 GNUNET_PEERSTORE_iterate_cancel (cp->respect_iterate_req);
624 cp->respect_iterate_req = NULL;
629 * A peer connected to us. Setup the connected peer
632 * @param peer identity of peer that connected
633 * @param creation_cb callback function when the record is created.
634 * @param creation_cb_cls closure for @creation_cb
637 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
638 GSF_ConnectedPeerCreationCallback creation_cb,
639 void *creation_cb_cls)
641 struct GSF_ConnectedPeer *cp;
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Connected to peer %s\n",
646 cp = GNUNET_new (struct GSF_ConnectedPeer);
647 cp->ppd.pid = GNUNET_PEER_intern (peer);
648 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
650 GNUNET_ATS_reserve_bandwidth (GSF_ats,
653 &ats_reserve_callback, cp);
654 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128,
656 GNUNET_break (GNUNET_OK ==
657 GNUNET_CONTAINER_multipeermap_put (cp_map,
658 GSF_connected_peer_get_identity2_ (cp),
660 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
661 GNUNET_STATISTICS_set (GSF_stats,
662 gettext_noop ("# peers connected"),
663 GNUNET_CONTAINER_multipeermap_size (cp_map),
665 cp->creation_cb = creation_cb;
666 cp->creation_cb_cls = creation_cb_cls;
667 cp->respect_iterate_req =
668 GNUNET_PEERSTORE_iterate (peerstore, "fs",
670 GNUNET_TIME_UNIT_FOREVER_REL,
677 * It may be time to re-start migrating content to this
678 * peer. Check, and if so, restart migration.
680 * @param cls the `struct GSF_ConnectedPeer`
683 revive_migration (void *cls)
685 struct GSF_ConnectedPeer *cp = cls;
686 struct GNUNET_TIME_Relative bt;
688 cp->mig_revive_task = NULL;
689 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
690 if (0 != bt.rel_value_us)
692 /* still time left... */
693 cp->mig_revive_task =
694 GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
697 GSF_push_start_ (cp);
702 * Get a handle for a connected peer.
704 * @param peer peer's identity
705 * @return NULL if the peer is not currently connected
707 struct GSF_ConnectedPeer *
708 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
712 return GNUNET_CONTAINER_multipeermap_get (cp_map, peer);
717 * Handle P2P "MIGRATION_STOP" message.
719 * @param cls closure, always NULL
720 * @param other the other peer involved (sender or receiver, NULL
721 * for loopback messages where we are both sender and receiver)
722 * @param message the actual message
723 * @return #GNUNET_OK to keep the connection open,
724 * #GNUNET_SYSERR to close it (signal serious error)
727 GSF_handle_p2p_migration_stop_ (void *cls,
728 const struct GNUNET_PeerIdentity *other,
729 const struct GNUNET_MessageHeader *message)
731 struct GSF_ConnectedPeer *cp;
732 const struct MigrationStopMessage *msm;
733 struct GNUNET_TIME_Relative bt;
735 msm = (const struct MigrationStopMessage *) message;
736 cp = GSF_peer_get_ (other);
742 GNUNET_STATISTICS_update (GSF_stats,
743 gettext_noop ("# migration stop messages received"),
745 bt = GNUNET_TIME_relative_ntoh (msm->duration);
746 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
747 _("Migration of content to peer `%s' blocked for %s\n"),
749 GNUNET_STRINGS_relative_time_to_string (bt, GNUNET_YES));
750 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
751 if ( (NULL == cp->mig_revive_task) &&
752 (NULL == cp->respect_iterate_req) )
755 cp->mig_revive_task =
756 GNUNET_SCHEDULER_add_delayed (bt,
757 &revive_migration, cp);
764 * Copy reply and free put message.
766 * @param cls the `struct PutMessage`
767 * @param buf_size number of bytes available in @a buf
768 * @param buf where to copy the message, NULL on error (peer disconnect)
769 * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
772 copy_reply (void *cls,
776 struct PutMessage *pm = cls;
781 GNUNET_assert (buf_size >= ntohs (pm->header.size));
782 size = ntohs (pm->header.size);
783 memcpy (buf, pm, size);
784 GNUNET_STATISTICS_update (GSF_stats,
785 gettext_noop ("# replies transmitted to other peers"),
792 GNUNET_STATISTICS_update (GSF_stats,
793 gettext_noop ("# replies dropped"),
803 * Free resources associated with the given peer request.
805 * @param peerreq request to free
808 free_pending_request (struct PeerRequest *peerreq)
810 struct GSF_ConnectedPeer *cp = peerreq->cp;
811 struct GSF_PendingRequestData *prd;
813 prd = GSF_pending_request_get_data_ (peerreq->pr);
814 if (NULL != peerreq->kill_task)
816 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
817 peerreq->kill_task = NULL;
819 GNUNET_STATISTICS_update (GSF_stats,
820 gettext_noop ("# P2P searches active"),
823 GNUNET_break (GNUNET_YES ==
824 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
827 GNUNET_free (peerreq);
832 * Cancel all requests associated with the peer.
835 * @param query hash code of the request
836 * @param value the `struct GSF_PendingRequest`
837 * @return #GNUNET_YES (continue to iterate)
840 cancel_pending_request (void *cls,
841 const struct GNUNET_HashCode *query,
844 struct PeerRequest *peerreq = value;
845 struct GSF_PendingRequest *pr = peerreq->pr;
847 free_pending_request (peerreq);
848 GSF_pending_request_cancel_ (pr,
855 * Free the given request.
857 * @param cls the request to free
860 peer_request_destroy (void *cls)
862 struct PeerRequest *peerreq = cls;
863 struct GSF_PendingRequest *pr = peerreq->pr;
864 struct GSF_PendingRequestData *prd;
866 peerreq->kill_task = NULL;
867 prd = GSF_pending_request_get_data_ (pr);
868 cancel_pending_request (NULL,
875 * The artificial delay is over, transmit the message now.
877 * @param cls the `struct GSF_DelayedHandle` with the message
880 transmit_delayed_now (void *cls)
882 struct GSF_DelayedHandle *dh = cls;
883 struct GSF_ConnectedPeer *cp = dh->cp;
885 GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
888 cp->delay_queue_size--;
889 (void) GSF_peer_transmit_ (cp,
901 * Get the randomized delay a response should be subjected to.
903 * @return desired delay
905 static struct GNUNET_TIME_Relative
906 get_randomized_delay ()
908 struct GNUNET_TIME_Relative ret;
911 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
912 GNUNET_CRYPTO_random_u32
913 (GNUNET_CRYPTO_QUALITY_WEAK,
914 2 * GSF_avg_latency.rel_value_us + 1));
915 #if INSANE_STATISTICS
916 GNUNET_STATISTICS_update (GSF_stats,
918 ("# artificial delays introduced (ms)"),
919 ret.rel_value_us / 1000LL, GNUNET_NO);
926 * Handle a reply to a pending request. Also called if a request
927 * expires (then with data == NULL). The handler may be called
928 * many times (depending on the request type), but will not be
929 * called during or after a call to GSF_pending_request_cancel
930 * and will also not be called anymore after a call signalling
933 * @param cls `struct PeerRequest` this is an answer for
934 * @param eval evaluation of the result
935 * @param pr handle to the original pending request
936 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
937 * @param expiration when does @a data expire?
938 * @param last_transmission when did we last transmit a request for this block
939 * @param type type of the block
940 * @param data response data, NULL on request expiration
941 * @param data_len number of bytes in @a data
944 handle_p2p_reply (void *cls,
945 enum GNUNET_BLOCK_EvaluationResult eval,
946 struct GSF_PendingRequest *pr,
947 uint32_t reply_anonymity_level,
948 struct GNUNET_TIME_Absolute expiration,
949 struct GNUNET_TIME_Absolute last_transmission,
950 enum GNUNET_BLOCK_Type type,
954 struct PeerRequest *peerreq = cls;
955 struct GSF_ConnectedPeer *cp = peerreq->cp;
956 struct GSF_PendingRequestData *prd;
957 struct PutMessage *pm;
960 GNUNET_assert (data_len + sizeof (struct PutMessage) <
961 GNUNET_SERVER_MAX_MESSAGE_SIZE);
962 GNUNET_assert (peerreq->pr == pr);
963 prd = GSF_pending_request_get_data_ (pr);
966 free_pending_request (peerreq);
969 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
970 if ((prd->type != type) && (GNUNET_BLOCK_TYPE_ANY != prd->type))
972 GNUNET_STATISTICS_update (GSF_stats,
974 ("# replies dropped due to type mismatch"),
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Transmitting result for query `%s' to peer\n",
980 GNUNET_h2s (&prd->query));
981 GNUNET_STATISTICS_update (GSF_stats,
982 gettext_noop ("# replies received for other peers"),
984 msize = sizeof (struct PutMessage) + data_len;
985 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
990 if ((UINT32_MAX != reply_anonymity_level) && (reply_anonymity_level > 1))
992 if (reply_anonymity_level - 1 > GSF_cover_content_count)
994 GNUNET_STATISTICS_update (GSF_stats,
996 ("# replies dropped due to insufficient cover traffic"),
1000 GSF_cover_content_count -= (reply_anonymity_level - 1);
1003 pm = GNUNET_malloc (msize);
1004 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1005 pm->header.size = htons (msize);
1006 pm->type = htonl (type);
1007 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
1008 memcpy (&pm[1], data, data_len);
1009 if ( (UINT32_MAX != reply_anonymity_level) &&
1010 (0 != reply_anonymity_level) &&
1011 (GNUNET_YES == GSF_enable_randomized_delays) )
1013 struct GSF_DelayedHandle *dh;
1015 dh = GNUNET_new (struct GSF_DelayedHandle);
1019 GNUNET_CONTAINER_DLL_insert (cp->delayed_head,
1022 cp->delay_queue_size++;
1024 GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
1025 &transmit_delayed_now,
1030 (void) GSF_peer_transmit_ (cp,
1038 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
1040 if (NULL == peerreq->kill_task)
1042 GNUNET_STATISTICS_update (GSF_stats,
1044 ("# P2P searches destroyed due to ultimate reply"),
1047 peerreq->kill_task =
1048 GNUNET_SCHEDULER_add_now (&peer_request_destroy,
1055 * Increase the peer's respect by a value.
1057 * @param cp which peer to change the respect value on
1058 * @param value is the int value by which the
1059 * peer's credit is to be increased or decreased
1060 * @returns the actual change in respect (positive or negative)
1063 change_peer_respect (struct GSF_ConnectedPeer *cp, int value)
1067 GNUNET_assert (NULL != cp);
1070 if (cp->ppd.respect + value < cp->ppd.respect)
1072 value = UINT32_MAX - cp->ppd.respect;
1073 cp->ppd.respect = UINT32_MAX;
1076 cp->ppd.respect += value;
1080 if (cp->ppd.respect < -value)
1082 value = -cp->ppd.respect;
1083 cp->ppd.respect = 0;
1086 cp->ppd.respect += value;
1093 * We've received a request with the specified priority. Bound it
1094 * according to how much we respect the given peer.
1096 * @param prio_in requested priority
1097 * @param cp the peer making the request
1098 * @return effective priority
1101 bound_priority (uint32_t prio_in,
1102 struct GSF_ConnectedPeer *cp)
1104 #define N ((double)128.0)
1109 ld = GSF_test_get_load_too_high_ (0);
1110 if (GNUNET_SYSERR == ld)
1112 #if INSANE_STATISTICS
1113 GNUNET_STATISTICS_update (GSF_stats,
1115 ("# requests done for free (low load)"), 1,
1118 return 0; /* excess resources */
1120 if (prio_in > INT32_MAX)
1121 prio_in = INT32_MAX;
1122 ret = -change_peer_respect (cp, -(int) prio_in);
1125 if (ret > GSF_current_priorities + N)
1126 rret = GSF_current_priorities + N;
1129 GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
1131 if ((GNUNET_YES == ld) && (ret > 0))
1133 /* try with charging */
1134 ld = GSF_test_get_load_too_high_ (ret);
1136 if (GNUNET_YES == ld)
1138 GNUNET_STATISTICS_update (GSF_stats,
1140 ("# request dropped, priority insufficient"), 1,
1143 change_peer_respect (cp, (int) ret);
1144 return -1; /* not enough resources */
1148 GNUNET_STATISTICS_update (GSF_stats,
1150 ("# requests done for a price (normal load)"), 1,
1159 * The priority level imposes a bound on the maximum
1160 * value for the ttl that can be requested.
1162 * @param ttl_in requested ttl
1163 * @param prio given priority
1164 * @return @a ttl_in if @a ttl_in is below the limit,
1165 * otherwise the ttl-limit for the given @a prio
1168 bound_ttl (int32_t ttl_in,
1171 unsigned long long allowed;
1175 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1176 if (ttl_in > allowed)
1178 if (allowed >= (1 << 30))
1187 * Closure for #test_exist_cb().
1189 struct TestExistClosure
1193 * Priority of the incoming request.
1198 * Relative TTL of the incoming request.
1203 * Type of the incoming request.
1205 enum GNUNET_BLOCK_Type type;
1208 * Set to #GNUNET_YES if we are done handling the query.
1216 * Test if the query already exists. If so, merge it, otherwise
1217 * keep `finished` at #GNUNET_NO.
1219 * @param cls our `struct TestExistClosure`
1220 * @param hc the key of the query
1221 * @param value the existing `struct PeerRequest`.
1222 * @return #GNUNET_YES to continue to iterate,
1223 * #GNUNET_NO if we successfully merged
1226 test_exist_cb (void *cls,
1227 const struct GNUNET_HashCode *hc,
1230 struct TestExistClosure *tec = cls;
1231 struct PeerRequest *peerreq = value;
1232 struct GSF_PendingRequest *pr;
1233 struct GSF_PendingRequestData *prd;
1236 prd = GSF_pending_request_get_data_ (pr);
1237 if (prd->type != tec->type)
1239 if (prd->ttl.abs_value_us >=
1240 GNUNET_TIME_absolute_get ().abs_value_us + tec->ttl * 1000LL)
1242 /* existing request has higher TTL, drop new one! */
1243 prd->priority += tec->priority;
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Have existing request with higher TTL, dropping new request.\n");
1246 GNUNET_STATISTICS_update (GSF_stats,
1248 ("# requests dropped due to higher-TTL request"),
1250 tec->finished = GNUNET_YES;
1253 /* existing request has lower TTL, drop old one! */
1254 tec->priority += prd->priority;
1255 free_pending_request (peerreq);
1256 GSF_pending_request_cancel_ (pr,
1263 * Handle P2P "QUERY" message. Creates the pending request entry
1264 * and sets up all of the data structures to that we will
1265 * process replies properly. Does not initiate forwarding or
1266 * local database lookups.
1268 * @param other the other peer involved (sender or receiver, NULL
1269 * for loopback messages where we are both sender and receiver)
1270 * @param message the actual message
1271 * @return pending request handle, NULL on error
1273 struct GSF_PendingRequest *
1274 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1275 const struct GNUNET_MessageHeader *message)
1277 struct PeerRequest *peerreq;
1278 struct GSF_PendingRequest *pr;
1279 struct GSF_ConnectedPeer *cp;
1280 struct GSF_ConnectedPeer *cps;
1281 const struct GNUNET_PeerIdentity *target;
1282 enum GSF_PendingRequestOptions options;
1284 const struct GetMessage *gm;
1286 const struct GNUNET_PeerIdentity *opt;
1289 uint32_t ttl_decrement;
1290 struct TestExistClosure tec;
1291 GNUNET_PEER_Id spid;
1292 const struct GSF_PendingRequestData *prd;
1294 msize = ntohs (message->size);
1295 if (msize < sizeof (struct GetMessage))
1297 GNUNET_break_op (0);
1300 GNUNET_STATISTICS_update (GSF_stats,
1302 ("# GET requests received (from other peers)"),
1305 gm = (const struct GetMessage *) message;
1306 tec.type = ntohl (gm->type);
1307 bm = ntohl (gm->hash_bitmap);
1315 if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_PeerIdentity))
1317 GNUNET_break_op (0);
1320 opt = (const struct GNUNET_PeerIdentity *) &gm[1];
1321 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_PeerIdentity);
1322 /* bfsize must be power of 2, check! */
1323 if (0 != ((bfsize - 1) & bfsize))
1325 GNUNET_break_op (0);
1328 GSF_cover_query_count++;
1329 bm = ntohl (gm->hash_bitmap);
1331 cps = GSF_peer_get_ (other);
1334 /* peer must have just disconnected */
1335 GNUNET_STATISTICS_update (GSF_stats,
1337 ("# requests dropped due to initiator not being connected"),
1341 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1342 cp = GSF_peer_get_ (&opt[bits++]);
1347 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Failed to find RETURN-TO peer `%s' in connection set. Dropping query.\n",
1350 GNUNET_i2s (&opt[bits - 1]));
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "Failed to find peer `%s' in connection set. Dropping query.\n",
1355 GNUNET_i2s (other));
1356 GNUNET_STATISTICS_update (GSF_stats,
1358 ("# requests dropped due to missing reverse route"),
1363 if (cp->ppd.pending_replies + cp->delay_queue_size > MAX_QUEUE_PER_PEER)
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366 "Peer `%s' has too many replies queued already. Dropping query.\n",
1367 GNUNET_i2s (other));
1368 GNUNET_STATISTICS_update (GSF_stats,
1369 gettext_noop ("# requests dropped due to full reply queue"),
1374 /* note that we can really only check load here since otherwise
1375 * peers could find out that we are overloaded by not being
1376 * disconnected after sending us a malformed query... */
1377 tec.priority = bound_priority (ntohl (gm->priority),
1379 if (tec.priority < 0)
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "Dropping query from `%s', this peer is too busy.\n",
1383 GNUNET_i2s (other));
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1387 "Received request for `%s' of type %u from peer `%s' with flags %u\n",
1388 GNUNET_h2s (&gm->query),
1389 (unsigned int) tec.type,
1394 (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? (&opt[bits++]) : NULL;
1395 options = GSF_PRO_DEFAULTS;
1397 if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + tec.priority))
1398 || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1399 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value_us * 2 +
1400 GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
1402 /* don't have BW to send to peer, or would likely take longer than we have for it,
1403 * so at best indirect the query */
1405 options |= GSF_PRO_FORWARD_ONLY;
1406 spid = GNUNET_PEER_intern (other);
1407 GNUNET_assert (0 != spid);
1409 tec.ttl = bound_ttl (ntohl (gm->ttl),
1411 /* decrement ttl (always) */
1413 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1415 if ((tec.ttl < 0) && (((int32_t) (tec.ttl - ttl_decrement)) > 0))
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1422 GNUNET_STATISTICS_update (GSF_stats,
1424 ("# requests dropped due TTL underflow"), 1,
1426 /* integer underflow => drop (should be very rare)! */
1429 tec.ttl -= ttl_decrement;
1431 /* test if the request already exists */
1432 tec.finished = GNUNET_NO;
1433 GNUNET_CONTAINER_multihashmap_get_multiple (cp->request_map,
1437 if (GNUNET_YES == tec.finished)
1438 return NULL; /* merged into existing request, we're done */
1440 peerreq = GNUNET_new (struct PeerRequest);
1442 pr = GSF_pending_request_create_ (options,
1447 ? (const char *) &opt[bits]
1450 ntohl (gm->filter_mutator),
1452 (uint32_t) tec.priority,
1455 GNUNET_PEER_intern (other),
1456 NULL, 0, /* replies_seen */
1459 GNUNET_assert (NULL != pr);
1460 prd = GSF_pending_request_get_data_ (pr);
1462 GNUNET_break (GNUNET_OK ==
1463 GNUNET_CONTAINER_multihashmap_put (cp->request_map,
1466 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1467 GNUNET_STATISTICS_update (GSF_stats,
1468 gettext_noop ("# P2P query messages received and processed"),
1471 GNUNET_STATISTICS_update (GSF_stats,
1472 gettext_noop ("# P2P searches active"),
1480 * Function called if there has been a timeout trying to satisfy
1481 * a transmission request.
1483 * @param cls the `struct GSF_PeerTransmitHandle` of the request
1486 peer_transmit_timeout (void *cls)
1488 struct GSF_PeerTransmitHandle *pth = cls;
1489 struct GSF_ConnectedPeer *cp;
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "Timeout trying to transmit to other peer\n");
1493 pth->timeout_task = NULL;
1495 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1498 if (GNUNET_YES == pth->is_query)
1499 GNUNET_assert (0 < cp->ppd.pending_queries--);
1500 else if (GNUNET_NO == pth->is_query)
1501 GNUNET_assert (0 < cp->ppd.pending_replies--);
1502 GNUNET_LOAD_update (cp->ppd.transmission_delay,
1504 if (NULL != cp->cth)
1506 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1509 pth->gmc (pth->gmc_cls, 0, NULL);
1510 GNUNET_assert (0 == cp->cth_in_progress);
1516 * Transmit a message to the given peer as soon as possible.
1517 * If the peer disconnects before the transmission can happen,
1518 * the callback is invoked with a `NULL` @a buffer.
1520 * @param cp target peer
1521 * @param is_query is this a query (#GNUNET_YES) or content (#GNUNET_NO) or neither (#GNUNET_SYSERR)
1522 * @param priority how important is this request?
1523 * @param timeout when does this request timeout (call gmc with error)
1524 * @param size number of bytes we would like to send to the peer
1525 * @param gmc function to call to get the message
1526 * @param gmc_cls closure for @a gmc
1527 * @return handle to cancel request
1529 struct GSF_PeerTransmitHandle *
1530 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
1533 struct GNUNET_TIME_Relative timeout,
1535 GSF_GetMessageCallback gmc, void *gmc_cls)
1537 struct GSF_PeerTransmitHandle *pth;
1538 struct GSF_PeerTransmitHandle *pos;
1539 struct GSF_PeerTransmitHandle *prev;
1541 pth = GNUNET_new (struct GSF_PeerTransmitHandle);
1542 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1543 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1545 pth->gmc_cls = gmc_cls;
1547 pth->is_query = is_query;
1548 pth->priority = priority;
1550 /* insertion sort (by priority, descending) */
1553 while ((NULL != pos) && (pos->priority > priority))
1558 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head,
1562 if (GNUNET_YES == is_query)
1563 cp->ppd.pending_queries++;
1564 else if (GNUNET_NO == is_query)
1565 cp->ppd.pending_replies++;
1567 = GNUNET_SCHEDULER_add_delayed (timeout,
1568 &peer_transmit_timeout,
1570 schedule_transmission (pth);
1576 * Cancel an earlier request for transmission.
1578 * @param pth request to cancel
1581 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1583 struct GSF_ConnectedPeer *cp;
1585 if (NULL != pth->timeout_task)
1587 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1588 pth->timeout_task = NULL;
1591 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1594 if (GNUNET_YES == pth->is_query)
1595 GNUNET_assert (0 < cp->ppd.pending_queries--);
1596 else if (GNUNET_NO == pth->is_query)
1597 GNUNET_assert (0 < cp->ppd.pending_replies--);
1603 * Report on receiving a reply; update the performance record of the given peer.
1605 * @param cp responding peer (will be updated)
1606 * @param request_time time at which the original query was transmitted
1607 * @param request_priority priority of the original request
1610 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1611 struct GNUNET_TIME_Absolute request_time,
1612 uint32_t request_priority)
1614 struct GNUNET_TIME_Relative delay;
1616 delay = GNUNET_TIME_absolute_get_duration (request_time);
1617 cp->ppd.avg_reply_delay.rel_value_us =
1618 (cp->ppd.avg_reply_delay.rel_value_us * (RUNAVG_DELAY_N - 1) +
1619 delay.rel_value_us) / RUNAVG_DELAY_N;
1620 cp->ppd.avg_priority =
1621 (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
1622 request_priority) / RUNAVG_DELAY_N;
1627 * Report on receiving a reply in response to an initiating client.
1628 * Remember that this peer is good for this client.
1630 * @param cp responding peer (will be updated)
1631 * @param initiator_client local client on responsible for query
1634 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1635 struct GSF_LocalClient *initiator_client)
1637 cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
1638 CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1643 * Report on receiving a reply in response to an initiating peer.
1644 * Remember that this peer is good for this initiating peer.
1646 * @param cp responding peer (will be updated)
1647 * @param initiator_peer other peer responsible for query
1650 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1651 const struct GSF_ConnectedPeer *initiator_peer)
1655 woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
1656 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
1657 cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
1658 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1659 cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
1664 * Write peer-respect information to a file - flush the buffer entry!
1667 * @param key peer identity
1668 * @param value the `struct GSF_ConnectedPeer` to flush
1669 * @return #GNUNET_OK to continue iteration
1672 flush_respect (void *cls,
1673 const struct GNUNET_PeerIdentity *key,
1676 struct GSF_ConnectedPeer *cp = value;
1677 struct GNUNET_PeerIdentity pid;
1679 if (cp->ppd.respect == cp->disk_respect)
1680 return GNUNET_OK; /* unchanged */
1681 GNUNET_assert (0 != cp->ppd.pid);
1682 GNUNET_PEER_resolve (cp->ppd.pid, &pid);
1683 GNUNET_PEERSTORE_store (peerstore, "fs", &pid, "respect", &cp->ppd.respect,
1684 sizeof (cp->ppd.respect),
1685 GNUNET_TIME_UNIT_FOREVER_ABS,
1686 GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL);
1692 * A peer disconnected from us. Tear down the connected peer
1696 * @param peer identity of peer that connected
1699 GSF_peer_disconnect_handler_ (void *cls,
1700 const struct GNUNET_PeerIdentity *peer)
1702 struct GSF_ConnectedPeer *cp;
1703 struct GSF_PeerTransmitHandle *pth;
1704 struct GSF_DelayedHandle *dh;
1706 cp = GSF_peer_get_ (peer);
1708 return; /* must have been disconnect from core with
1709 * 'peer' == my_id, ignore */
1710 flush_respect (NULL, peer, cp);
1711 GNUNET_assert (GNUNET_YES ==
1712 GNUNET_CONTAINER_multipeermap_remove (cp_map,
1715 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
1716 GNUNET_CONTAINER_multipeermap_size (cp_map),
1718 if (NULL != cp->respect_iterate_req)
1720 GNUNET_PEERSTORE_iterate_cancel (cp->respect_iterate_req);
1721 cp->respect_iterate_req = NULL;
1723 if (NULL != cp->migration_pth)
1725 GSF_peer_transmit_cancel_ (cp->migration_pth);
1726 cp->migration_pth = NULL;
1730 GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
1733 if (NULL != cp->rc_delay_task)
1735 GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
1736 cp->rc_delay_task = NULL;
1738 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1739 &cancel_pending_request,
1741 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1742 cp->request_map = NULL;
1743 GSF_plan_notify_peer_disconnect_ (cp);
1744 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1745 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies,
1746 P2P_SUCCESS_LIST_SIZE);
1747 memset (cp->ppd.last_p2p_replies,
1749 sizeof (cp->ppd.last_p2p_replies));
1750 GSF_push_stop_ (cp);
1751 if (NULL != cp->cth)
1753 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1756 GNUNET_assert (0 == cp->cth_in_progress);
1757 while (NULL != (pth = cp->pth_head))
1759 if (pth->timeout_task != NULL)
1761 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1762 pth->timeout_task = NULL;
1764 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1767 if (GNUNET_YES == pth->is_query)
1768 GNUNET_assert (0 < cp->ppd.pending_queries--);
1769 else if (GNUNET_NO == pth->is_query)
1770 GNUNET_assert (0 < cp->ppd.pending_replies--);
1771 pth->gmc (pth->gmc_cls, 0, NULL);
1774 while (NULL != (dh = cp->delayed_head))
1776 GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
1779 cp->delay_queue_size--;
1780 GNUNET_SCHEDULER_cancel (dh->delay_task);
1781 GNUNET_free (dh->pm);
1784 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1785 if (NULL != cp->mig_revive_task)
1787 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1788 cp->mig_revive_task = NULL;
1790 GNUNET_break (0 == cp->ppd.pending_queries);
1791 GNUNET_break (0 == cp->ppd.pending_replies);
1797 * Closure for #call_iterator().
1799 struct IterationContext
1802 * Function to call on each entry.
1804 GSF_ConnectedPeerIterator it;
1807 * Closure for @e it.
1814 * Function that calls the callback for each peer.
1816 * @param cls the `struct IterationContext *`
1817 * @param key identity of the peer
1818 * @param value the `struct GSF_ConnectedPeer *`
1819 * @return #GNUNET_YES to continue iteration
1822 call_iterator (void *cls,
1823 const struct GNUNET_PeerIdentity *key,
1826 struct IterationContext *ic = cls;
1827 struct GSF_ConnectedPeer *cp = value;
1837 * Iterate over all connected peers.
1839 * @param it function to call for each peer
1840 * @param it_cls closure for @a it
1843 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1846 struct IterationContext ic;
1850 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
1857 * Obtain the identity of a connected peer.
1859 * @param cp peer to get identity of
1860 * @param id identity to set (written to)
1863 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1864 struct GNUNET_PeerIdentity *id)
1866 GNUNET_assert (0 != cp->ppd.pid);
1867 GNUNET_PEER_resolve (cp->ppd.pid, id);
1872 * Obtain the identity of a connected peer.
1874 * @param cp peer to get identity of
1875 * @return reference to peer identity, valid until peer disconnects (!)
1877 const struct GNUNET_PeerIdentity *
1878 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp)
1880 GNUNET_assert (0 != cp->ppd.pid);
1881 return GNUNET_PEER_resolve2 (cp->ppd.pid);
1886 * Assemble a migration stop message for transmission.
1888 * @param cls the `struct GSF_ConnectedPeer` to use
1889 * @param size number of bytes we're allowed to write to @a buf
1890 * @param buf where to copy the message
1891 * @return number of bytes copied to @a buf
1894 create_migration_stop_message (void *cls,
1898 struct GSF_ConnectedPeer *cp = cls;
1899 struct MigrationStopMessage msm;
1901 cp->migration_pth = NULL;
1904 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1905 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1906 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1907 msm.reserved = htonl (0);
1909 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1910 (cp->last_migration_block));
1911 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1912 GNUNET_STATISTICS_update (GSF_stats,
1913 gettext_noop ("# migration stop messages sent"),
1915 return sizeof (struct MigrationStopMessage);
1920 * Ask a peer to stop migrating data to us until the given point
1923 * @param cp peer to ask
1924 * @param block_time until when to block
1927 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1928 struct GNUNET_TIME_Absolute block_time)
1930 if (cp->last_migration_block.abs_value_us > block_time.abs_value_us)
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1933 "Migration already blocked for another %s\n",
1934 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining
1935 (cp->last_migration_block), GNUNET_YES));
1936 return; /* already blocked */
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %s\n",
1939 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (block_time),
1941 cp->last_migration_block = block_time;
1942 if (NULL != cp->migration_pth)
1943 GSF_peer_transmit_cancel_ (cp->migration_pth);
1945 GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
1946 GNUNET_TIME_UNIT_FOREVER_REL,
1947 sizeof (struct MigrationStopMessage),
1948 &create_migration_stop_message, cp);
1953 * Notify core about a preference we have for the given peer
1954 * (to allocate more resources towards it). The change will
1955 * be communicated the next time we reserve bandwidth with
1956 * core (not instantly).
1958 * @param cp peer to reserve bandwidth from
1959 * @param pref preference change
1962 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1965 cp->inc_preference += pref;
1970 * Call this method periodically to flush respect information to disk.
1972 * @param cls closure, not used
1975 cron_flush_respect (void *cls)
1978 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
1981 fr_task = GNUNET_SCHEDULER_add_delayed_with_priority (RESPECT_FLUSH_FREQ,
1982 GNUNET_SCHEDULER_PRIORITY_HIGH,
1983 &cron_flush_respect, NULL);
1988 * Initialize peer management subsystem.
1991 GSF_connected_peer_init_ ()
1993 cp_map = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1994 peerstore = GNUNET_PEERSTORE_connect (GSF_cfg);
1995 fr_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1996 &cron_flush_respect, NULL);
2001 * Iterator to free peer entries.
2003 * @param cls closure, unused
2004 * @param key current key code
2005 * @param value value in the hash map (peer entry)
2006 * @return #GNUNET_YES (we should continue to iterate)
2009 clean_peer (void *cls,
2010 const struct GNUNET_PeerIdentity *key,
2013 GSF_peer_disconnect_handler_ (NULL, key);
2019 * Shutdown peer management subsystem.
2022 GSF_connected_peer_done_ ()
2024 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
2027 GNUNET_CONTAINER_multipeermap_iterate (cp_map,
2030 GNUNET_SCHEDULER_cancel (fr_task);
2032 GNUNET_CONTAINER_multipeermap_destroy (cp_map);
2034 GNUNET_PEERSTORE_disconnect (peerstore,
2041 * Iterator to remove references to LC entry.
2043 * @param cls the `struct GSF_LocalClient *` to look for
2044 * @param key current key code
2045 * @param value value in the hash map (peer entry)
2046 * @return #GNUNET_YES (we should continue to iterate)
2049 clean_local_client (void *cls,
2050 const struct GNUNET_PeerIdentity *key,
2053 const struct GSF_LocalClient *lc = cls;
2054 struct GSF_ConnectedPeer *cp = value;
2057 for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
2058 if (cp->ppd.last_client_replies[i] == lc)
2059 cp->ppd.last_client_replies[i] = NULL;
2065 * Notification that a local client disconnected. Clean up all of our
2066 * references to the given handle.
2068 * @param lc handle to the local client (henceforth invalid)
2071 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
2074 return; /* already cleaned up */
2075 GNUNET_CONTAINER_multipeermap_iterate (cp_map, &clean_local_client,
2080 /* end of gnunet-service-fs_cp.c */