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-service-fs.h"
28 #include "gnunet-service-fs_cp.h"
29 #include "gnunet-service-fs_push.h"
32 * How often do we flush trust values to disk?
34 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
37 * After how long do we discard a reply?
39 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
43 * Handle to cancel a transmission request.
45 struct GSF_PeerTransmitHandle
49 * Handle for an active request for transmission to this
50 * peer, or NULL (if core queue was full).
52 struct GNUNET_CORE_TransmitHandle *cth;
55 * Time when this transmission request was issued.
57 struct GNUNET_TIME_Absolute transmission_request_start_time;
60 * Timeout for this request.
62 struct GNUNET_TIME_Absolute timeout;
65 * Task called on timeout, or 0 for none.
67 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
70 * Function to call to get the actual message.
72 GSF_GetMessageCallback gmc;
75 * Peer this request targets.
77 struct GSF_ConnectedPeer *cp;
85 * Size of the message to be transmitted.
90 * GNUNET_YES if this is a query, GNUNET_NO for content.
95 * Priority of this request.
105 struct GSF_ConnectedPeer
109 * Performance data for this peer.
111 struct GSF_PeerPerformanceData ppd;
114 * Time until when we blocked this peer from migrating
117 struct GNUNET_TIME_Absolute last_migration_block;
120 * Task scheduled to revive migration to this peer.
122 struct GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
125 * Messages (replies, queries, content migration) we would like to
126 * send to this peer in the near future. Sorted by priority, head.
128 struct GSF_PeerTransmitHandle *pth_head;
131 * Messages (replies, queries, content migration) we would like to
132 * send to this peer in the near future. Sorted by priority, tail.
134 struct GSF_PeerTransmitHandle *pth_tail;
137 * Migration stop message in our queue, or NULL if we have none pending.
139 struct GSF_PeerTransmitHandle *migration_pth;
142 * Context of our GNUNET_CORE_peer_change_preference call (or NULL).
143 * NULL if we have successfully reserved 32k, otherwise non-NULL.
145 struct GNUNET_CORE_InformationRequestContext *irc;
148 * Active requests from this neighbour.
150 struct GNUNET_CONTAINER_MulitHashMap *request_map;
153 * Increase in traffic preference still to be submitted
154 * to the core service for this peer.
156 uint64_t inc_preference;
159 * Trust rating for this peer on disk.
164 * Which offset in "last_p2p_replies" will be updated next?
165 * (we go round-robin).
167 unsigned int last_p2p_replies_woff;
170 * Which offset in "last_client_replies" will be updated next?
171 * (we go round-robin).
173 unsigned int last_client_replies_woff;
176 * Current offset into 'last_request_times' ring buffer.
178 unsigned int last_request_times_off;
184 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
186 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
190 * Where do we store trust information?
192 static char *trustDirectory;
196 * Get the filename under which we would store the GNUNET_HELLO_Message
197 * for the given host and protocol.
198 * @return filename of the form DIRECTORY/HOSTID
201 get_trust_filename (const struct GNUNET_PeerIdentity *id)
203 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
206 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
207 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
213 * Find latency information in 'atsi'.
215 * @param atsi performance data
216 * @return connection latency
218 static struct GNUNET_TIME_Relative
219 get_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
222 return GNUNET_TIME_UNIT_SECONDS;
223 while ( (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
224 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY) )
226 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
229 /* how can we not have latency data? */
230 return GNUNET_TIME_UNIT_SECONDS;
232 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
233 ntohl (atsi->value));
238 * Update the performance information kept for the given peer.
240 * @param cp peer record to update
241 * @param atsi transport performance data
244 update_atsi (struct GSF_ConnectedPeer *cp,
245 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
247 struct GNUNET_TIME_Relative latency;
249 latency = get_latency (atsi);
250 GNUNET_LOAD_value_set_decline (cp->transmission_delay,
252 /* LATER: merge atsi into cp's performance data (if we ever care...) */
257 * Return the performance data record for the given peer
259 * @param cp peer to query
260 * @return performance data record for the peer
262 struct GSF_PeerPerformanceData *
263 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
270 * Core is ready to transmit to a peer, get the message.
272 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
273 * @param size number of bytes core is willing to take
274 * @param buf where to copy the message
275 * @return number of bytes copied to buf
278 peer_transmit_ready_cb (void *cls,
282 struct GSF_PeerTransmitHandle *pth = cls;
283 struct GSF_ConnectedPeer *cp;
286 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
288 GNUNET_SCHEDULER_cancel (pth->timeout_task);
289 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
292 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
295 if (GNUNET_YES == pth->is_query)
297 cp->ppd.last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
298 GNUNET_assert (0 < cp->ppd.pending_queries--);
300 else if (GNUNET_NO == pth->is_query)
302 GNUNET_assert (0 < cp->ppd.pending_replies--);
304 GNUNET_LOAD_update (cp->ppd.transmission_delay,
305 GNUNET_TIME_absolute_get_duration (pth->request_start_time).rel_value);
306 ret = pth->gmc (pth->gmc_cls,
314 * Function called by core upon success or failure of our bandwidth reservation request.
316 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
317 * @param peer identifies the peer
318 * @param bandwidth_out available amount of outbound bandwidth
319 * @param amount set to the amount that was actually reserved or unreserved;
320 * either the full requested amount or zero (no partial reservations)
321 * @param preference current traffic preference for the given peer
324 core_reserve_callback (void *cls,
325 const struct GNUNET_PeerIdentity * peer,
326 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
330 struct GSF_ConnectedPeer *cp = cls;
336 /* failed; retry! (how did we get here!?) */
337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
338 _("Failed to reserve bandwidth to peer `%s'\n"),
340 ip = cp->inc_preference;
341 cp->inc_preference = 0;
342 cp->irc = GNUNET_CORE_peer_change_preference (core,
344 GNUNET_TIME_UNIT_FOREVER_REL,
345 GNUNET_BANDWIDTH_VALUE_MAX,
346 GNUNET_FS_DBLOCK_SIZE,
348 &core_reserve_callback,
353 if ( (NULL != pth) &&
356 /* reservation success, try transmission now! */
357 pth->cth = GNUNET_CORE_notify_transmit_ready (core,
359 GNUNET_TIME_absolute_get_remaining (pth->timeout),
362 &peer_transmit_ready_cb,
369 * A peer connected to us. Setup the connected peer
372 * @param peer identity of peer that connected
373 * @param atsi performance data for the connection
374 * @return handle to connected peer entry
376 struct GSF_ConnectedPeer *
377 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
378 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
380 struct GSF_ConnectedPeer *cp;
383 struct GNUNET_TIME_Relative latency;
385 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
386 cp->transmission_delay = GNUNET_LOAD_value_init (latency);
387 cp->ppd.pid = GNUNET_PEER_intern (peer);
388 cp->transmission_delay = GNUNET_LOAD_value_init (0);
389 cp->irc = GNUNET_CORE_peer_change_preference (core,
391 GNUNET_TIME_UNIT_FOREVER_REL,
392 GNUNET_BANDWIDTH_VALUE_MAX,
393 GNUNET_FS_DBLOCK_SIZE,
395 &core_reserve_callback,
397 fn = get_trust_filename (peer);
398 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
399 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
400 cp->disk_trust = cp->trust = ntohl (trust);
402 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
403 GNUNET_break (GNUNET_OK ==
404 GNUNET_CONTAINER_multihashmap_put (cp_map,
407 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
408 update_atsi (cp, atsi);
409 GSF_plan_notify_new_peer_ (cp);
410 GSF_push_start_ (cp);
416 * It may be time to re-start migrating content to this
417 * peer. Check, and if so, restart migration.
419 * @param cls the 'struct GSF_ConnectedPeer'
420 * @param tc scheduler context
423 revive_migration (void *cls,
424 const struct GNUNET_SCHEDULER_TaskContext *tc)
426 struct GSF_ConnectedPeer *cp = cls;
427 struct GNUNET_TIME_Relative bt;
429 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
430 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
431 if (0 != bt.rel_value)
433 /* still time left... */
435 = GNUNET_SCHEDULER_add_delayed (bt,
440 GSF_push_start_ (cp);
445 * Handle P2P "MIGRATION_STOP" message.
447 * @param cls closure, always NULL
448 * @param other the other peer involved (sender or receiver, NULL
449 * for loopback messages where we are both sender and receiver)
450 * @param message the actual message
451 * @param atsi performance information
452 * @return GNUNET_OK to keep the connection open,
453 * GNUNET_SYSERR to close it (signal serious error)
456 GSF_handle_p2p_migration_stop_ (void *cls,
457 const struct GNUNET_PeerIdentity *other,
458 const struct GNUNET_MessageHeader *message,
459 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
461 struct GSF_ConnectedPeer *cp;
462 const struct MigrationStopMessage *msm;
463 struct GNUNET_TIME_Relative bt;
465 msm = (const struct MigrationStopMessage*) message;
466 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
473 bt = GNUNET_TIME_relative_ntoh (msm->duration);
474 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
475 if (cp->mig_revive_task == GNUNET_SCHEDULER_NO_TASK)
479 = GNUNET_SCHEDULER_add_delayed (bt,
483 update_atsi (cp, atsi);
489 * Copy reply and free put message.
491 * @param cls the 'struct PutMessage'
492 * @param buf_size number of bytes available in buf
493 * @param buf where to copy the message, NULL on error (peer disconnect)
494 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
497 copy_reply (void *cls,
501 struct PutMessage *pm = cls;
505 GNUNET_assert (size >= ntohs (pm->header.size));
506 size = ntohs (pm->header.size);
507 memcpy (buf, pm, size);
508 GNUNET_STATISTICS_update (stats,
509 gettext_noop ("# replies transmitted to other peers"),
515 GNUNET_STATISTICS_update (stats,
516 gettext_noop ("# replies dropped"),
526 * Handle a reply to a pending request. Also called if a request
527 * expires (then with data == NULL). The handler may be called
528 * many times (depending on the request type), but will not be
529 * called during or after a call to GSF_pending_request_cancel
530 * and will also not be called anymore after a call signalling
533 * @param cls 'struct GSF_ConnectedPeer' of the peer that would
534 * have liked an answer to the request
535 * @param pr handle to the original pending request
536 * @param expiration when does 'data' expire?
537 * @param data response data, NULL on request expiration
538 * @param data_len number of bytes in data
539 * @param more GNUNET_YES if the request remains active (may call
540 * this function again), GNUNET_NO if the request is
541 * finished (client must not call GSF_pending_request_cancel_)
544 handle_p2p_reply (void *cls,
545 struct GSF_PendingRequest *pr,
546 struct GNUNET_TIME_Absolute expiration,
551 struct GSF_ConnectedPeer *cp = cls;
552 struct GSF_PendingRequest *prd;
553 struct PutMessage *pm;
556 prd = GSF_pending_request_get_data_ (pr);
559 GNUNET_assert (GNUNET_NO == more);
560 GNUNET_STATISTICS_update (stats,
561 gettext_noop ("# P2P searches active"),
564 GNUNET_break (GNUNET_OK ==
565 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572 "Transmitting result for query `%s'\n",
575 GNUNET_STATISTICS_update (stats,
576 gettext_noop ("# replies received for other peers"),
579 msize = sizeof (struct PutMessage) + data_len;
580 pm = GNUNET_malloc (sizeof (msize));
581 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
582 pm->header.size = htons (msize);
583 pm->type = htonl (prd->type);
584 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
585 memcpy (&pm[1], data, data_len);
586 (void) GSF_peer_transmit_ (cp, GNUNET_NO,
596 * Handle P2P "QUERY" message. Creates the pending request entry
597 * and sets up all of the data structures to that we will
598 * process replies properly. Does not initiate forwarding or
599 * local database lookups.
601 * @param other the other peer involved (sender or receiver, NULL
602 * for loopback messages where we are both sender and receiver)
603 * @param message the actual message
604 * @return pending request handle, NULL on error
606 struct GSF_PendingRequest *
607 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
608 const struct GNUNET_MessageHeader *message)
610 struct GSF_PendingRequest *pr;
611 struct GSF_PendingRequestData *prd;
612 struct GSF_ConnectedPeer *cp;
613 struct GSF_ConnectedPeer *cps;
614 GNUNET_HashCode *namespace;
615 struct GNUNET_PeerIdentity *target;
616 enum GSF_PendingRequestOptions options;
617 struct GNUNET_TIME_Relative timeout;
619 const struct GetMessage *gm;
621 const GNUNET_HashCode *opt;
624 uint32_t ttl_decrement;
627 enum GNUNET_BLOCK_Type type;
630 msize = ntohs(message->size);
631 if (msize < sizeof (struct GetMessage))
634 return GNUNET_SYSERR;
636 gm = (const struct GetMessage*) message;
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Received request for `%s'\n",
640 GNUNET_h2s (&gm->query));
642 type = ntohl (gm->type);
643 bm = ntohl (gm->hash_bitmap);
651 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
654 return GNUNET_SYSERR;
656 opt = (const GNUNET_HashCode*) &gm[1];
657 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
658 /* bfsize must be power of 2, check! */
659 if (0 != ( (bfsize - 1) & bfsize))
662 return GNUNET_SYSERR;
665 bm = ntohl (gm->hash_bitmap);
667 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
671 /* peer must have just disconnected */
672 GNUNET_STATISTICS_update (stats,
673 gettext_noop ("# requests dropped due to initiator not being connected"),
676 return GNUNET_SYSERR;
678 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
679 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
686 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
689 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Failed to find peer `%4s' in connection set. Dropping query.\n",
696 GNUNET_STATISTICS_update (stats,
697 gettext_noop ("# requests dropped due to missing reverse route"),
702 /* note that we can really only check load here since otherwise
703 peers could find out that we are overloaded by not being
704 disconnected after sending us a malformed query... */
705 priority = bound_priority (ntohl (gm->priority), cps);
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "Dropping query from `%s', this peer is too busy.\n",
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
717 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
718 GNUNET_h2s (&gm->query),
723 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
724 target = (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity*) &opt[bits++]) : NULL;
726 if ( (GNUNET_LOAD_get_load (cp->transmission_delay) > 3 * (1 + priority)) ||
727 (GNUNET_LOAD_get_average (cp->transmission_delay) >
728 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 + GNUNET_LOAD_get_average (rt_entry_lifetime)) )
730 /* don't have BW to send to peer, or would likely take longer than we have for it,
731 so at best indirect the query */
733 options |= GSF_PRO_FORWARD_ONLY;
735 ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
736 /* decrement ttl (always) */
737 ttl_decrement = 2 * TTL_DECREMENT +
738 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
741 (((int32_t)(ttl - ttl_decrement)) > 0) )
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
750 GNUNET_STATISTICS_update (stats,
751 gettext_noop ("# requests dropped due TTL underflow"),
754 /* integer underflow => drop (should be very rare)! */
757 ttl -= ttl_decrement;
759 /* test if the request already exists */
760 pr = GNUNET_CONTAINER_multihashmap_get (cp->request_map,
764 prd = GSF_pending_request_get_data_ (pr);
765 if ( (prd->type == type) &&
766 ( (type != GNUNET_BLOCK_TYPE_SBLOCK) ||
767 (0 == memcmp (prd->namespace,
769 sizeof (GNUNET_HashCode))) ) )
771 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get().abs_value + ttl)
773 /* existing request has higher TTL, drop new one! */
774 prd->priority += priority;
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Have existing request with higher TTL, dropping new request.\n",
780 GNUNET_STATISTICS_update (stats,
781 gettext_noop ("# requests dropped due to higher-TTL request"),
786 /* existing request has lower TTL, drop old one! */
787 pr->priority += prd->priority;
788 GSF_pending_request_cancel_ (pr);
789 GNUNET_assert (GNUNET_YES ==
790 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
796 pr = GSF_pending_request_create (options,
801 (bf_size > 0) ? (const char*)&opt[bits] : NULL,
803 ntohl (gm->filter_mutator),
807 NULL, 0, /* replies_seen */
810 GNUNET_break (GNUNET_OK ==
811 GNUNET_CONTAINER_multihashmap_put (cp->request_map,
814 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
815 GNUNET_STATISTICS_update (stats,
816 gettext_noop ("# P2P searches received"),
819 GNUNET_STATISTICS_update (stats,
820 gettext_noop ("# P2P searches active"),
828 * Function called if there has been a timeout trying to satisfy
829 * a transmission request.
831 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
832 * @param tc scheduler context
835 peer_transmit_timeout (void *cls,
836 const struct GNUNET_SCHEDULER_TaskContext *tc)
838 struct GSF_PeerTransmitHandle *pth = cls;
839 struct GSF_ConnectedPeer *cp;
841 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
843 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
846 if (GNUNET_YES == pth->is_query)
847 GNUNET_assert (0 < cp->ppd.pending_queries--);
848 else if (GNUNET_NO == pth->is_query)
849 GNUNET_assert (0 < cp->ppd.pending_replies--);
850 GNUNET_LOAD_update (cp->ppd.transmission_delay,
852 pth->gmc (pth->gmc_cls,
859 * Transmit a message to the given peer as soon as possible.
860 * If the peer disconnects before the transmission can happen,
861 * the callback is invoked with a 'NULL' buffer.
863 * @param peer target peer
864 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
865 * @param priority how important is this request?
866 * @param timeout when does this request timeout (call gmc with error)
867 * @param size number of bytes we would like to send to the peer
868 * @param gmc function to call to get the message
869 * @param gmc_cls closure for gmc
870 * @return handle to cancel request
872 struct GSF_PeerTransmitHandle *
873 GSF_peer_transmit_ (struct GSF_ConnectedPeer *peer,
876 struct GNUNET_TIME_Relative timeout,
878 GSF_GetMessageCallback gmc,
881 struct GSF_ConnectedPeer *cp;
882 struct GSF_PeerTransmitHandle *pth;
883 struct GSF_PeerTransmitHandle *pos;
884 struct GSF_PeerTransmitHandle *prev;
885 struct GNUNET_PeerIdentity target;
889 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
891 GNUNET_assert (NULL != cp);
892 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
893 pth->transmission_request_start_time = GNUNET_TIME_absolute_now ();
894 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
896 pth->gmc_cls = gmc_cls;
898 pth->is_query = is_query;
899 pth->priority = priority;
901 /* insertion sort (by priority, descending) */
904 while ( (pos != NULL) &&
905 (pos->priority > priority) )
911 GNUNET_CONTAINER_DLL_insert_head (cp->pth_head,
915 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head,
919 GNUNET_PEER_resolve (cp->ppd.pid,
921 if (GNUNET_YES == is_query)
923 /* query, need reservation */
924 cp->ppd.pending_queries++;
927 /* reservation already done! */
928 is_ready = GNUNET_YES;
929 ip = cp->inc_preference;
930 cp->inc_preference = 0;
931 cp->irc = GNUNET_CORE_peer_change_preference (core,
933 GNUNET_TIME_UNIT_FOREVER_REL,
934 GNUNET_BANDWIDTH_VALUE_MAX,
935 GNUNET_FS_DBLOCK_SIZE,
937 &core_reserve_callback,
942 /* still waiting for reservation */
943 is_ready = GNUNET_NO;
946 else if (GNUNET_NO == is_query)
948 /* no reservation needed for content */
949 cp->ppd.pending_replies++;
950 is_ready = GNUNET_YES;
954 /* not a query or content, no reservation needed */
955 is_ready = GNUNET_YES;
959 pth->cth = GNUNET_CORE_notify_transmit_ready (core,
964 &peer_transmit_ready_cb,
966 /* pth->cth could be NULL here, that's OK, we'll try again
969 if (pth->cth == NULL)
971 /* if we're waiting for reservation OR if we could not do notify_transmit_ready,
972 install a timeout task to be on the safe side */
973 pth->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
974 &peer_transmit_timeout,
982 * Cancel an earlier request for transmission.
984 * @param pth request to cancel
987 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
989 struct GSF_PeerTransmitHandle *pth = cls;
990 struct GSF_ConnectedPeer *cp;
992 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
994 GNUNET_SCHEDULER_cancel (pth->timeout_task);
995 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
997 if (NULL != pth->cth)
999 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1003 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1006 if (GNUNET_YES == pth->is_query)
1007 GNUNET_assert (0 < cp->ppd.pending_queries--);
1008 else if (GNUNET_NO == pth->is_query)
1009 GNUNET_assert (0 < cp->ppd.pending_replies--);
1015 * Report on receiving a reply; update the performance record of the given peer.
1017 * @param cp responding peer (will be updated)
1018 * @param request_time time at which the original query was transmitted
1019 * @param request_priority priority of the original request
1022 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1023 struct GNUNET_TIME_Absolute request_time,
1024 uint32_t request_priority)
1026 struct GNUNET_TIME_Relative delay;
1028 delay = GNUNET_TIME_absolute_get_duration (request_time);
1029 cp->ppd.avg_reply_delay = (cp->ppd.avg_reply_delay * (RUNAVG_DELAY_N-1) + delay.rel_value) / RUNAVG_DELAY_N;
1030 cp->ppd.avg_priority = (cp->avg_priority * (RUNAVG_DELAY_N-1) + request_priority) / RUNAVG_DELAY_N;
1035 * Report on receiving a reply in response to an initiating client.
1036 * Remember that this peer is good for this client.
1038 * @param cp responding peer (will be updated)
1039 * @param initiator_client local client on responsible for query
1042 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1043 const struct GSF_LocalClient *initiator_client)
1045 cp->ppd.last_client_replies[cp->last_client_replies_woff++ % CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1050 * Report on receiving a reply in response to an initiating peer.
1051 * Remember that this peer is good for this initiating peer.
1053 * @param cp responding peer (will be updated)
1054 * @param initiator_peer other peer responsible for query
1057 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1058 const struct GSF_ConnectedPeer *initiator_peer)
1060 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE], -1);
1061 cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff++ % P2P_SUCCESS_LIST_SIZE] = initiator_peer->ppd.pid;
1062 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1067 * Method called whenever a given peer has a status change.
1069 * @param cls closure
1070 * @param peer peer identity this notification is about
1071 * @param bandwidth_in available amount of inbound bandwidth
1072 * @param bandwidth_out available amount of outbound bandwidth
1073 * @param timeout absolute time when this peer will time out
1074 * unless we see some further activity from it
1075 * @param atsi status information
1078 GSF_peer_status_handler_ (void *cls,
1079 const struct GNUNET_PeerIdentity *peer,
1080 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1081 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1082 struct GNUNET_TIME_Absolute timeout,
1083 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1085 struct GSF_ConnectedPeer *cp;
1087 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1089 GNUNET_assert (NULL != cp);
1090 update_atsi (cp, atsi);
1095 * Cancel all requests associated with the peer.
1098 * @param query hash code of the request
1099 * @param value the 'struct GSF_PendingRequest'
1100 * @return GNUNET_YES (continue to iterate)
1103 cancel_pending_request (void *cls,
1104 const GNUNET_HashCode *query,
1107 struct GSF_PendingRequest *pr = value;
1109 GSF_pending_request_cancel_ (pr);
1115 * A peer disconnected from us. Tear down the connected peer
1119 * @param peer identity of peer that connected
1122 GSF_peer_disconnect_handler_ (void *cls,
1123 const struct GNUNET_PeerIdentity *peer)
1125 struct GSF_ConnectedPeer *cp;
1126 struct GSF_PeerTransmitHandle *pth;
1128 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1130 GNUNET_assert (NULL != cp);
1131 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1134 if (NULL != cp->migration_pth)
1136 GSF_peer_transmit_cancel_ (cp->migration_pth);
1137 cp->migration_pth = NULL;
1139 if (NULL != cp->irc)
1141 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1144 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1145 &cancel_pending_request,
1147 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1148 cp->request_map = NULL;
1149 GSF_plan_notify_peer_disconnect_ (cp);
1150 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1151 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1152 while (NULL != (pth = cp->pth_head))
1154 if (NULL != pth->th)
1156 GNUNET_CORE_notify_transmit_ready_cancel (pth->th);
1159 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1164 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1165 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1167 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1168 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1175 * Closure for 'call_iterator'.
1177 struct IterationContext
1180 * Function to call on each entry.
1182 GSF_ConnectedPeerIterator it;
1192 * Function that calls the callback for each peer.
1194 * @param cls the 'struct IterationContext*'
1195 * @param key identity of the peer
1196 * @param value the 'struct GSF_ConnectedPeer*'
1197 * @return GNUNET_YES to continue iteration
1200 call_iterator (void *cls,
1201 const GNUNET_HashCode *key,
1204 struct IterationContext *ic = cls;
1205 struct GSF_ConnectedPeer *cp = value;
1208 (const struct GNUNET_PeerIdentity*) key,
1216 * Iterate over all connected peers.
1218 * @param it function to call for each peer
1219 * @param it_cls closure for it
1222 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1225 struct IterationContext ic;
1229 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1236 * Obtain the identity of a connected peer.
1238 * @param cp peer to reserve bandwidth from
1239 * @param id identity to set (written to)
1242 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1243 struct GNUNET_PeerIdentity *id)
1245 GNUNET_PEER_resolve (cp->ppd.pid,
1251 * Assemble a migration stop message for transmission.
1253 * @param cls the 'struct GSF_ConnectedPeer' to use
1254 * @param size number of bytes we're allowed to write to buf
1255 * @param buf where to copy the message
1256 * @return number of bytes copied to buf
1259 create_migration_stop_message (void *cls,
1263 struct GSF_ConnectedPeer *cp = cls;
1264 struct MigrationStopMessage msm;
1266 cp->migration_pth = NULL;
1269 GNUNET_assert (size > sizeof (struct MigrationStopMessage));
1270 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1271 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1272 msm.duration = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block));
1273 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1274 return sizeof (struct MigrationStopMessage);
1279 * Ask a peer to stop migrating data to us until the given point
1282 * @param cp peer to ask
1283 * @param block_time until when to block
1286 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1287 struct GNUNET_TIME_Relative block_time)
1289 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).rel_value > block_time.rel_value)
1290 return; /* already blocked */
1291 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1292 if (cp->migration_pth != NULL)
1293 GSF_peer_transmit_cancel_ (cp->migration_pth);
1295 = GSF_peer_transmit_ (cp,
1298 GNUNET_TIME_UNIT_FOREVER_REL,
1299 sizeof (struct MigrationStopMessage),
1300 &create_migration_stop_message,
1306 * Write host-trust information to a file - flush the buffer entry!
1308 * @param cls closure, not used
1309 * @param key host identity
1310 * @param value the 'struct GSF_ConnectedPeer' to flush
1311 * @return GNUNET_OK to continue iteration
1314 flush_trust (void *cls,
1315 const GNUNET_HashCode *key,
1318 struct GSF_ConnectedPeer *cp = value;
1321 struct GNUNET_PeerIdentity pid;
1323 if (cp->trust == cp->disk_trust)
1324 return GNUNET_OK; /* unchanged */
1325 GNUNET_PEER_resolve (cp->ppd.pid,
1327 fn = get_trust_filename (&pid);
1330 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1331 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1332 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1336 trust = htonl (cp->trust);
1337 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1339 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1340 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1341 cp->disk_trust = cp->trust;
1349 * Notify core about a preference we have for the given peer
1350 * (to allocate more resources towards it). The change will
1351 * be communicated the next time we reserve bandwidth with
1352 * core (not instantly).
1354 * @param cp peer to reserve bandwidth from
1355 * @param pref preference change
1358 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1361 cp->inc_preference += pref;
1366 * Call this method periodically to flush trust information to disk.
1368 * @param cls closure, not used
1369 * @param tc task context, not used
1372 cron_flush_trust (void *cls,
1373 const struct GNUNET_SCHEDULER_TaskContext *tc)
1378 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1383 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1385 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ,
1392 * Initialize peer management subsystem.
1394 * @param cfg configuration to use
1397 GSF_connected_peer_init_ (struct GNUNET_CONFIGURATION_Handle *cfg)
1399 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1400 GNUNET_assert (GNUNET_OK ==
1401 GNUNET_CONFIGURATION_get_value_filename (cfg,
1405 GNUNET_DISK_directory_create (trustDirectory);
1406 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1407 &cron_flush_trust, NULL);
1412 * Iterator to free peer entries.
1414 * @param cls closure, unused
1415 * @param key current key code
1416 * @param value value in the hash map (peer entry)
1417 * @return GNUNET_YES (we should continue to iterate)
1420 clean_peer (void *cls,
1421 const GNUNET_HashCode * key,
1424 GSF_peer_disconnect_handler_ (NULL,
1425 (const struct GNUNET_PeerIdentity*) key);
1431 * Shutdown peer management subsystem.
1434 GSF_connected_peer_done_ ()
1436 cron_flush_trust (NULL, NULL);
1437 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1440 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1442 GNUNET_free (trustDirectory);
1443 trustDirectory = NULL;
1448 * Iterator to remove references to LC entry.
1450 * @param the 'struct GSF_LocalClient*' to look for
1451 * @param key current key code
1452 * @param value value in the hash map (peer entry)
1453 * @return GNUNET_YES (we should continue to iterate)
1456 clean_local_client (void *cls,
1457 const GNUNET_HashCode * key,
1460 const struct GSF_LocalClient *lc = cls;
1461 struct GSF_ConnectedPeer *cp = value;
1464 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1465 if (cp->ppd.last_client_replies[i] == lc)
1466 cp->ppd.last_client_replies[i] = NULL;
1472 * Notification that a local client disconnected. Clean up all of our
1473 * references to the given handle.
1475 * @param lc handle to the local client (henceforth invalid)
1478 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1480 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1481 &clean_local_client,
1486 /* end of gnunet-service-fs_cp.c */