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"
31 * How often do we flush trust values to disk?
33 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
37 * Handle to cancel a transmission request.
39 struct GSF_PeerTransmitHandle
43 * Handle for an active request for transmission to this
44 * peer, or NULL (if core queue was full).
46 struct GNUNET_CORE_TransmitHandle *cth;
49 * Time when this transmission request was issued.
51 struct GNUNET_TIME_Absolute transmission_request_start_time;
54 * Timeout for this request.
56 struct GNUNET_TIME_Absolute timeout;
59 * Task called on timeout, or 0 for none.
61 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
64 * Function to call to get the actual message.
66 GSF_GetMessageCallback gmc;
69 * Peer this request targets.
71 struct GSF_ConnectedPeer *cp;
79 * Size of the message to be transmitted.
84 * GNUNET_YES if this is a query, GNUNET_NO for content.
89 * Priority of this request.
99 struct GSF_ConnectedPeer
103 * Performance data for this peer.
105 struct GSF_PeerPerformanceData ppd;
108 * Time until when we blocked this peer from migrating
111 struct GNUNET_TIME_Absolute last_migration_block;
114 * Messages (replies, queries, content migration) we would like to
115 * send to this peer in the near future. Sorted by priority, head.
117 struct GSF_PeerTransmitHandle *pth_head;
120 * Messages (replies, queries, content migration) we would like to
121 * send to this peer in the near future. Sorted by priority, tail.
123 struct GSF_PeerTransmitHandle *pth_tail;
126 * Migration stop message in our queue, or NULL if we have none pending.
128 struct GSF_PeerTransmitHandle *migration_pth;
131 * Context of our GNUNET_CORE_peer_change_preference call (or NULL).
132 * NULL if we have successfully reserved 32k, otherwise non-NULL.
134 struct GNUNET_CORE_InformationRequestContext *irc;
137 * Active requests from this neighbour.
139 struct GNUNET_CONTAINER_MulitHashMap *request_map;
142 * ID of delay task for scheduling transmission.
144 GNUNET_SCHEDULER_TaskIdentifier delayed_transmission_request_task; // FIXME: used in 'push' (ugh!)
147 * Increase in traffic preference still to be submitted
148 * to the core service for this peer.
150 uint64_t inc_preference;
153 * Trust rating for this peer on disk.
158 * The peer's identity.
163 * Which offset in "last_p2p_replies" will be updated next?
164 * (we go round-robin).
166 unsigned int last_p2p_replies_woff;
169 * Which offset in "last_client_replies" will be updated next?
170 * (we go round-robin).
172 unsigned int last_client_replies_woff;
175 * Current offset into 'last_request_times' ring buffer.
177 unsigned int last_request_times_off;
183 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
185 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
189 * Where do we store trust information?
191 static char *trustDirectory;
195 * Get the filename under which we would store the GNUNET_HELLO_Message
196 * for the given host and protocol.
197 * @return filename of the form DIRECTORY/HOSTID
200 get_trust_filename (const struct GNUNET_PeerIdentity *id)
202 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
205 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
206 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
212 * Find latency information in 'atsi'.
214 * @param atsi performance data
215 * @return connection latency
217 static struct GNUNET_TIME_Relative
218 get_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
221 return GNUNET_TIME_UNIT_SECONDS;
222 while ( (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
223 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY) )
225 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
228 /* how can we not have latency data? */
229 return GNUNET_TIME_UNIT_SECONDS;
231 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
232 ntohl (atsi->value));
237 * Update the performance information kept for the given peer.
239 * @param cp peer record to update
240 * @param atsi transport performance data
243 update_atsi (struct GSF_ConnectedPeer *cp,
244 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
246 struct GNUNET_TIME_Relative latency;
248 latency = get_latency (atsi);
249 GNUNET_LOAD_value_set_decline (cp->transmission_delay,
251 /* LATER: merge atsi into cp's performance data (if we ever care...) */
256 * Return the performance data record for the given peer
258 * @param cp peer to query
259 * @return performance data record for the peer
261 struct GSF_PeerPerformanceData *
262 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
269 * Core is ready to transmit to a peer, get the message.
271 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
272 * @param size number of bytes core is willing to take
273 * @param buf where to copy the message
274 * @return number of bytes copied to buf
277 peer_transmit_ready_cb (void *cls,
281 struct GSF_PeerTransmitHandle *pth = cls;
282 struct GSF_ConnectedPeer *cp;
285 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
287 GNUNET_SCHEDULER_cancel (pth->timeout_task);
288 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
291 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
294 if (GNUNET_YES == pth->is_query)
296 cp->ppd.last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
297 GNUNET_assert (0 < cp->ppd.pending_queries--);
299 else if (GNUNET_NO == pth->is_query)
301 GNUNET_assert (0 < cp->ppd.pending_replies--);
303 GNUNET_LOAD_update (cp->ppd.transmission_delay,
304 GNUNET_TIME_absolute_get_duration (pth->request_start_time).rel_value);
305 ret = pth->gmc (pth->gmc_cls,
313 * Function called by core upon success or failure of our bandwidth reservation request.
315 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
316 * @param peer identifies the peer
317 * @param bandwidth_out available amount of outbound bandwidth
318 * @param amount set to the amount that was actually reserved or unreserved;
319 * either the full requested amount or zero (no partial reservations)
320 * @param preference current traffic preference for the given peer
323 core_reserve_callback (void *cls,
324 const struct GNUNET_PeerIdentity * peer,
325 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
329 struct GSF_ConnectedPeer *cp = cls;
335 /* failed; retry! (how did we get here!?) */
336 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
337 _("Failed to reserve bandwidth to peer `%s'\n"),
339 ip = cp->inc_preference;
340 cp->inc_preference = 0;
341 cp->irc = GNUNET_CORE_peer_change_preference (core,
343 GNUNET_TIME_UNIT_FOREVER_REL,
344 GNUNET_BANDWIDTH_VALUE_MAX,
345 GNUNET_FS_DBLOCK_SIZE,
347 &core_reserve_callback,
352 if ( (NULL != pth) &&
355 /* reservation success, try transmission now! */
356 pth->cth = GNUNET_CORE_notify_transmit_ready (core,
358 GNUNET_TIME_absolute_get_remaining (pth->timeout),
361 &peer_transmit_ready_cb,
368 * A peer connected to us. Setup the connected peer
371 * @param peer identity of peer that connected
372 * @param atsi performance data for the connection
373 * @return handle to connected peer entry
375 struct GSF_ConnectedPeer *
376 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
377 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
379 struct GSF_ConnectedPeer *cp;
382 struct GNUNET_TIME_Relative latency;
384 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
385 cp->transmission_delay = GNUNET_LOAD_value_init (latency);
386 cp->pid = GNUNET_PEER_intern (peer);
387 cp->transmission_delay = GNUNET_LOAD_value_init (0);
388 cp->irc = GNUNET_CORE_peer_change_preference (core,
390 GNUNET_TIME_UNIT_FOREVER_REL,
391 GNUNET_BANDWIDTH_VALUE_MAX,
392 GNUNET_FS_DBLOCK_SIZE,
394 &core_reserve_callback,
396 fn = get_trust_filename (peer);
397 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
398 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
399 cp->disk_trust = cp->trust = ntohl (trust);
401 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
402 GNUNET_break (GNUNET_OK ==
403 GNUNET_CONTAINER_multihashmap_put (cp_map,
406 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
407 update_atsi (cp, atsi);
408 GSF_plan_notify_new_peer_ (cp);
414 * Handle P2P "MIGRATION_STOP" message.
416 * @param cls closure, always NULL
417 * @param other the other peer involved (sender or receiver, NULL
418 * for loopback messages where we are both sender and receiver)
419 * @param message the actual message
420 * @param atsi performance information
421 * @return GNUNET_OK to keep the connection open,
422 * GNUNET_SYSERR to close it (signal serious error)
425 GSF_handle_p2p_migration_stop_ (void *cls,
426 const struct GNUNET_PeerIdentity *other,
427 const struct GNUNET_MessageHeader *message,
428 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
430 struct GSF_ConnectedPeer *cp;
431 const struct MigrationStopMessage *msm;
433 msm = (const struct MigrationStopMessage*) message;
434 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
441 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (msm->duration));
442 update_atsi (cp, atsi);
448 * Handle a reply to a pending request. Also called if a request
449 * expires (then with data == NULL). The handler may be called
450 * many times (depending on the request type), but will not be
451 * called during or after a call to GSF_pending_request_cancel
452 * and will also not be called anymore after a call signalling
455 * @param cls 'struct GSF_ConnectedPeer' of the peer that would
456 * have liked an answer to the request
457 * @param pr handle to the original pending request
458 * @param data response data, NULL on request expiration
459 * @param data_len number of bytes in data
462 handle_p2p_reply (void *cls,
463 struct GSF_PendingRequest *pr,
467 struct GSF_ConnectedPeer *cp = cls;
470 struct GNUNET_TIME_Relative art_delay;
473 /* FIXME: adapt code fragments below to new API! */
476 /* FIXME: request expired! clean up! */
477 GNUNET_STATISTICS_update (stats,
478 gettext_noop ("# P2P searches active"),
484 /* reply will go over the network, check for cover traffic */
485 if ( (prq->anonymity_level > 1) &&
486 (cover_content_count < prq->anonymity_level - 1) )
488 /* insufficient cover traffic, skip */
489 GNUNET_STATISTICS_update (stats,
490 gettext_noop ("# replies suppressed due to lack of cover traffic"),
495 if (prq->anonymity_level > 1)
496 cover_content_count -= prq->anonymity_level - 1;
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Transmitting result for query `%s' to other peer (PID=%u)\n",
504 (unsigned int) cp->pid);
506 GNUNET_STATISTICS_update (stats,
507 gettext_noop ("# replies received for other peers"),
510 msize = sizeof (struct PutMessage) + prq->size;
511 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
512 reply->cont = &transmit_reply_continuation;
513 reply->cont_cls = pr;
515 art_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
516 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
519 = GNUNET_TIME_relative_to_absolute (art_delay);
520 GNUNET_STATISTICS_update (stats,
521 gettext_noop ("cummulative artificial delay introduced (ms)"),
525 reply->msize = msize;
526 reply->priority = UINT32_MAX; /* send replies first! */
527 pm = (struct PutMessage*) &reply[1];
528 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
529 pm->header.size = htons (msize);
530 pm->type = htonl (prq->type);
531 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
532 memcpy (&pm[1], prq->data, prq->size);
533 add_to_pending_messages_for_peer (cp, reply, pr);
540 * Handle P2P "QUERY" message. Creates the pending request entry
541 * and sets up all of the data structures to that we will
542 * process replies properly. Does not initiate forwarding or
543 * local database lookups.
545 * @param other the other peer involved (sender or receiver, NULL
546 * for loopback messages where we are both sender and receiver)
547 * @param message the actual message
548 * @return pending request handle, NULL on error
550 struct GSF_PendingRequest *
551 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
552 const struct GNUNET_MessageHeader *message)
554 struct GSF_PendingRequest *pr;
555 struct GSF_PendingRequestData *prd;
556 struct GSF_ConnectedPeer *cp;
557 struct GSF_ConnectedPeer *cps;
558 GNUNET_HashCode *namespace;
559 struct GNUNET_PeerIdentity *target;
560 enum GSF_PendingRequestOptions options;
561 struct GNUNET_TIME_Relative timeout;
563 const struct GetMessage *gm;
565 const GNUNET_HashCode *opt;
568 uint32_t ttl_decrement;
571 enum GNUNET_BLOCK_Type type;
574 msize = ntohs(message->size);
575 if (msize < sizeof (struct GetMessage))
578 return GNUNET_SYSERR;
580 gm = (const struct GetMessage*) message;
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 "Received request for `%s'\n",
584 GNUNET_h2s (&gm->query));
586 type = ntohl (gm->type);
587 bm = ntohl (gm->hash_bitmap);
595 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
598 return GNUNET_SYSERR;
600 opt = (const GNUNET_HashCode*) &gm[1];
601 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
602 /* bfsize must be power of 2, check! */
603 if (0 != ( (bfsize - 1) & bfsize))
606 return GNUNET_SYSERR;
609 bm = ntohl (gm->hash_bitmap);
611 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
615 /* peer must have just disconnected */
616 GNUNET_STATISTICS_update (stats,
617 gettext_noop ("# requests dropped due to initiator not being connected"),
620 return GNUNET_SYSERR;
622 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
623 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
630 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
633 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637 "Failed to find peer `%4s' in connection set. Dropping query.\n",
640 GNUNET_STATISTICS_update (stats,
641 gettext_noop ("# requests dropped due to missing reverse route"),
646 /* note that we can really only check load here since otherwise
647 peers could find out that we are overloaded by not being
648 disconnected after sending us a malformed query... */
649 priority = bound_priority (ntohl (gm->priority), cps);
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Dropping query from `%s', this peer is too busy.\n",
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
662 GNUNET_h2s (&gm->query),
667 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
668 target = (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity*) &opt[bits++]) : NULL;
670 if ( (GNUNET_LOAD_get_load (cp->transmission_delay) > 3 * (1 + priority)) ||
671 (GNUNET_LOAD_get_average (cp->transmission_delay) >
672 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 + GNUNET_LOAD_get_average (rt_entry_lifetime)) )
674 /* don't have BW to send to peer, or would likely take longer than we have for it,
675 so at best indirect the query */
677 options |= GSF_PRO_FORWARD_ONLY;
679 ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
680 /* decrement ttl (always) */
681 ttl_decrement = 2 * TTL_DECREMENT +
682 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
685 (((int32_t)(ttl - ttl_decrement)) > 0) )
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
694 GNUNET_STATISTICS_update (stats,
695 gettext_noop ("# requests dropped due TTL underflow"),
698 /* integer underflow => drop (should be very rare)! */
701 ttl -= ttl_decrement;
703 /* test if the request already exists */
704 pr = GNUNET_CONTAINER_multihashmap_get (cp->request_map,
708 prd = GSF_pending_request_get_data_ (pr);
709 if ( (prd->type == type) &&
710 ( (type != GNUNET_BLOCK_TYPE_SBLOCK) ||
711 (0 == memcmp (prd->namespace,
713 sizeof (GNUNET_HashCode))) ) )
715 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get().abs_value + ttl)
717 /* existing request has higher TTL, drop new one! */
718 prd->priority += priority;
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Have existing request with higher TTL, dropping new request.\n",
724 GNUNET_STATISTICS_update (stats,
725 gettext_noop ("# requests dropped due to higher-TTL request"),
730 /* existing request has lower TTL, drop old one! */
731 pr->priority += prd->priority;
732 GSF_pending_request_cancel_ (pr);
733 GNUNET_assert (GNUNET_YES ==
734 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
740 pr = GSF_pending_request_create (options,
745 (bf_size > 0) ? (const char*)&opt[bits] : NULL,
747 ntohl (gm->filter_mutator),
751 NULL, 0, /* replies_seen */
754 GNUNET_break (GNUNET_OK ==
755 GNUNET_CONTAINER_multihashmap_put (cp->request_map,
758 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
759 GNUNET_STATISTICS_update (stats,
760 gettext_noop ("# P2P searches received"),
763 GNUNET_STATISTICS_update (stats,
764 gettext_noop ("# P2P searches active"),
772 * Function called if there has been a timeout trying to satisfy
773 * a transmission request.
775 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
776 * @param tc scheduler context
779 peer_transmit_timeout (void *cls,
780 const struct GNUNET_SCHEDULER_TaskContext *tc)
782 struct GSF_PeerTransmitHandle *pth = cls;
783 struct GSF_ConnectedPeer *cp;
785 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
787 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
790 if (GNUNET_YES == pth->is_query)
791 GNUNET_assert (0 < cp->ppd.pending_queries--);
792 else if (GNUNET_NO == pth->is_query)
793 GNUNET_assert (0 < cp->ppd.pending_replies--);
794 GNUNET_LOAD_update (cp->ppd.transmission_delay,
796 pth->gmc (pth->gmc_cls,
803 * Transmit a message to the given peer as soon as possible.
804 * If the peer disconnects before the transmission can happen,
805 * the callback is invoked with a 'NULL' buffer.
807 * @param peer target peer
808 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
809 * @param priority how important is this request?
810 * @param timeout when does this request timeout (call gmc with error)
811 * @param size number of bytes we would like to send to the peer
812 * @param gmc function to call to get the message
813 * @param gmc_cls closure for gmc
814 * @return handle to cancel request
816 struct GSF_PeerTransmitHandle *
817 GSF_peer_transmit_ (struct GSF_ConnectedPeer *peer,
820 struct GNUNET_TIME_Relative timeout,
822 GSF_GetMessageCallback gmc,
825 struct GSF_ConnectedPeer *cp;
826 struct GSF_PeerTransmitHandle *pth;
827 struct GSF_PeerTransmitHandle *pos;
828 struct GSF_PeerTransmitHandle *prev;
829 struct GNUNET_PeerIdentity target;
833 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
835 GNUNET_assert (NULL != cp);
836 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
837 pth->transmission_request_start_time = GNUNET_TIME_absolute_now ();
838 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
840 pth->gmc_cls = gmc_cls;
842 pth->is_query = is_query;
843 pth->priority = priority;
845 /* insertion sort (by priority, descending) */
848 while ( (pos != NULL) &&
849 (pos->priority > priority) )
855 GNUNET_CONTAINER_DLL_insert_head (cp->pth_head,
859 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head,
863 GNUNET_PEER_resolve (cp->pid,
865 if (GNUNET_YES == is_query)
867 /* query, need reservation */
868 cp->ppd.pending_queries++;
871 /* reservation already done! */
872 is_ready = GNUNET_YES;
873 ip = cp->inc_preference;
874 cp->inc_preference = 0;
875 cp->irc = GNUNET_CORE_peer_change_preference (core,
877 GNUNET_TIME_UNIT_FOREVER_REL,
878 GNUNET_BANDWIDTH_VALUE_MAX,
879 GNUNET_FS_DBLOCK_SIZE,
881 &core_reserve_callback,
886 /* still waiting for reservation */
887 is_ready = GNUNET_NO;
890 else if (GNUNET_NO == is_query)
892 /* no reservation needed for content */
893 cp->ppd.pending_replies++;
894 is_ready = GNUNET_YES;
898 /* not a query or content, no reservation needed */
899 is_ready = GNUNET_YES;
903 pth->cth = GNUNET_CORE_notify_transmit_ready (core,
908 &peer_transmit_ready_cb,
910 /* pth->cth could be NULL here, that's OK, we'll try again
913 if (pth->cth == NULL)
915 /* if we're waiting for reservation OR if we could not do notify_transmit_ready,
916 install a timeout task to be on the safe side */
917 pth->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
918 &peer_transmit_timeout,
926 * Cancel an earlier request for transmission.
928 * @param pth request to cancel
931 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
933 struct GSF_PeerTransmitHandle *pth = cls;
934 struct GSF_ConnectedPeer *cp;
936 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
938 GNUNET_SCHEDULER_cancel (pth->timeout_task);
939 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
941 if (NULL != pth->cth)
943 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
947 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
950 if (GNUNET_YES == pth->is_query)
951 GNUNET_assert (0 < cp->ppd.pending_queries--);
952 else if (GNUNET_NO == pth->is_query)
953 GNUNET_assert (0 < cp->ppd.pending_replies--);
959 * Report on receiving a reply; update the performance record of the given peer.
961 * @param cp responding peer (will be updated)
962 * @param request_time time at which the original query was transmitted
963 * @param request_priority priority of the original request
964 * @param initiator_client local client on responsible for query (or NULL)
965 * @param initiator_peer other peer responsible for query (or NULL)
968 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
969 struct GNUNET_TIME_Absolute request_time,
970 uint32_t request_priority,
971 const struct GSF_LocalClient *initiator_client,
972 const struct GSF_ConnectedPeer *initiator_peer)
974 struct GNUNET_TIME_Relative delay;
977 delay = GNUNET_TIME_absolute_get_duration (request_time);
978 cp->ppd.avg_reply_delay = (cp->ppd.avg_reply_delay * (RUNAVG_DELAY_N-1) + delay.rel_value) / RUNAVG_DELAY_N;
979 cp->ppd.avg_priority = (cp->avg_priority * (RUNAVG_DELAY_N-1) + request_priority) / RUNAVG_DELAY_N;
980 if (NULL != initiator_client)
982 cp->ppd.last_client_replies[cp->last_client_replies_woff++ % CS2P_SUCCESS_LIST_SIZE] = initiator_client;
984 else if (NULL != initiator_peer)
986 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE], -1);
987 cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff++ % P2P_SUCCESS_LIST_SIZE] = initiator_peer->pid;
988 GNUNET_PEER_change_rc (initiator_peer->pid, 1);
996 * Method called whenever a given peer has a status change.
999 * @param peer peer identity this notification is about
1000 * @param bandwidth_in available amount of inbound bandwidth
1001 * @param bandwidth_out available amount of outbound bandwidth
1002 * @param timeout absolute time when this peer will time out
1003 * unless we see some further activity from it
1004 * @param atsi status information
1007 GSF_peer_status_handler_ (void *cls,
1008 const struct GNUNET_PeerIdentity *peer,
1009 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1010 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1011 struct GNUNET_TIME_Absolute timeout,
1012 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1014 struct GSF_ConnectedPeer *cp;
1016 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1018 GNUNET_assert (NULL != cp);
1019 update_atsi (cp, atsi);
1024 * Cancel all requests associated with the peer.
1027 * @param query hash code of the request
1028 * @param value the 'struct GSF_PendingRequest'
1029 * @return GNUNET_YES (continue to iterate)
1032 cancel_pending_request (void *cls,
1033 const GNUNET_HashCode *query,
1036 struct GSF_PendingRequest *pr = value;
1038 GSF_pending_request_cancel_ (pr);
1044 * A peer disconnected from us. Tear down the connected peer
1048 * @param peer identity of peer that connected
1051 GSF_peer_disconnect_handler_ (void *cls,
1052 const struct GNUNET_PeerIdentity *peer)
1054 struct GSF_ConnectedPeer *cp;
1055 struct GSF_PeerTransmitHandle *pth;
1057 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1059 GNUNET_assert (NULL != cp);
1060 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1063 if (NULL != cp->migration_pth)
1065 GSF_peer_transmit_cancel_ (cp->migration_pth);
1066 cp->migration_pth = NULL;
1068 if (NULL != cp->irc)
1070 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1073 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1074 &cancel_pending_request,
1076 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1077 cp->request_map = NULL;
1078 GSF_plan_notify_peer_disconnect_ (cp);
1079 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1080 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1081 while (NULL != (pth = cp->pth_head))
1083 if (NULL != pth->th)
1085 GNUNET_CORE_notify_transmit_ready_cancel (pth->th);
1088 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1093 GNUNET_PEER_change_rc (cp->pid, -1);
1099 * Closure for 'call_iterator'.
1101 struct IterationContext
1104 * Function to call on each entry.
1106 GSF_ConnectedPeerIterator it;
1116 * Function that calls the callback for each peer.
1118 * @param cls the 'struct IterationContext*'
1119 * @param key identity of the peer
1120 * @param value the 'struct GSF_ConnectedPeer*'
1121 * @return GNUNET_YES to continue iteration
1124 call_iterator (void *cls,
1125 const GNUNET_HashCode *key,
1128 struct IterationContext *ic = cls;
1129 struct GSF_ConnectedPeer *cp = value;
1132 (const struct GNUNET_PeerIdentity*) key,
1140 * Iterate over all connected peers.
1142 * @param it function to call for each peer
1143 * @param it_cls closure for it
1146 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1149 struct IterationContext ic;
1153 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1160 * Obtain the identity of a connected peer.
1162 * @param cp peer to reserve bandwidth from
1163 * @param id identity to set (written to)
1166 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1167 struct GNUNET_PeerIdentity *id)
1169 GNUNET_PEER_resolve (cp->pid,
1175 * Assemble a migration stop message for transmission.
1177 * @param cls the 'struct GSF_ConnectedPeer' to use
1178 * @param size number of bytes we're allowed to write to buf
1179 * @param buf where to copy the message
1180 * @return number of bytes copied to buf
1183 create_migration_stop_message (void *cls,
1187 struct GSF_ConnectedPeer *cp = cls;
1188 struct MigrationStopMessage msm;
1190 cp->migration_pth = NULL;
1193 GNUNET_assert (size > sizeof (struct MigrationStopMessage));
1194 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1195 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1196 msm.duration = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block));
1197 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1198 return sizeof (struct MigrationStopMessage);
1203 * Ask a peer to stop migrating data to us until the given point
1206 * @param cp peer to ask
1207 * @param block_time until when to block
1210 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1211 struct GNUNET_TIME_Relative block_time)
1213 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).rel_value > block_time.rel_value)
1214 return; /* already blocked */
1215 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1216 if (cp->migration_pth != NULL)
1217 GSF_peer_transmit_cancel_ (cp->migration_pth);
1219 = GSF_peer_transmit_ (cp,
1222 GNUNET_TIME_UNIT_FOREVER_REL,
1223 sizeof (struct MigrationStopMessage),
1224 &create_migration_stop_message,
1230 * Write host-trust information to a file - flush the buffer entry!
1232 * @param cls closure, not used
1233 * @param key host identity
1234 * @param value the 'struct GSF_ConnectedPeer' to flush
1235 * @return GNUNET_OK to continue iteration
1238 flush_trust (void *cls,
1239 const GNUNET_HashCode *key,
1242 struct GSF_ConnectedPeer *cp = value;
1245 struct GNUNET_PeerIdentity pid;
1247 if (cp->trust == cp->disk_trust)
1248 return GNUNET_OK; /* unchanged */
1249 GNUNET_PEER_resolve (cp->pid,
1251 fn = get_trust_filename (&pid);
1254 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1255 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1256 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1260 trust = htonl (cp->trust);
1261 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1263 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1264 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1265 cp->disk_trust = cp->trust;
1273 * Notify core about a preference we have for the given peer
1274 * (to allocate more resources towards it). The change will
1275 * be communicated the next time we reserve bandwidth with
1276 * core (not instantly).
1278 * @param cp peer to reserve bandwidth from
1279 * @param pref preference change
1282 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1285 cp->inc_preference += pref;
1290 * Call this method periodically to flush trust information to disk.
1292 * @param cls closure, not used
1293 * @param tc task context, not used
1296 cron_flush_trust (void *cls,
1297 const struct GNUNET_SCHEDULER_TaskContext *tc)
1302 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1307 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1309 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ,
1316 * Initialize peer management subsystem.
1318 * @param cfg configuration to use
1321 GSF_connected_peer_init_ (struct GNUNET_CONFIGURATION_Handle *cfg)
1323 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1324 GNUNET_assert (GNUNET_OK ==
1325 GNUNET_CONFIGURATION_get_value_filename (cfg,
1329 GNUNET_DISK_directory_create (trustDirectory);
1330 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1331 &cron_flush_trust, NULL);
1336 * Iterator to free peer entries.
1338 * @param cls closure, unused
1339 * @param key current key code
1340 * @param value value in the hash map (peer entry)
1341 * @return GNUNET_YES (we should continue to iterate)
1344 clean_peer (void *cls,
1345 const GNUNET_HashCode * key,
1348 GSF_peer_disconnect_handler_ (NULL,
1349 (const struct GNUNET_PeerIdentity*) key);
1355 * Shutdown peer management subsystem.
1358 GSF_connected_peer_done_ ()
1360 cron_flush_trust (NULL, NULL);
1361 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1364 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1366 GNUNET_free (trustDirectory);
1367 trustDirectory = NULL;
1372 * Iterator to remove references to LC entry.
1374 * @param the 'struct GSF_LocalClient*' to look for
1375 * @param key current key code
1376 * @param value value in the hash map (peer entry)
1377 * @return GNUNET_YES (we should continue to iterate)
1380 clean_local_client (void *cls,
1381 const GNUNET_HashCode * key,
1384 const struct GSF_LocalClient *lc = cls;
1385 struct GSF_ConnectedPeer *cp = value;
1388 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1389 if (cp->ppd.last_client_replies[i] == lc)
1390 cp->ppd.last_client_replies[i] = NULL;
1396 * Notification that a local client disconnected. Clean up all of our
1397 * references to the given handle.
1399 * @param lc handle to the local client (henceforth invalid)
1402 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1404 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1405 &clean_local_client,
1410 /* end of gnunet-service-fs_cp.c */