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)
38 * Handle to cancel a transmission request.
40 struct GSF_PeerTransmitHandle
44 * Handle for an active request for transmission to this
45 * peer, or NULL (if core queue was full).
47 struct GNUNET_CORE_TransmitHandle *cth;
50 * Time when this transmission request was issued.
52 struct GNUNET_TIME_Absolute transmission_request_start_time;
55 * Timeout for this request.
57 struct GNUNET_TIME_Absolute timeout;
60 * Task called on timeout, or 0 for none.
62 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
65 * Function to call to get the actual message.
67 GSF_GetMessageCallback gmc;
70 * Peer this request targets.
72 struct GSF_ConnectedPeer *cp;
80 * Size of the message to be transmitted.
85 * GNUNET_YES if this is a query, GNUNET_NO for content.
90 * Priority of this request.
100 struct GSF_ConnectedPeer
104 * Performance data for this peer.
106 struct GSF_PeerPerformanceData ppd;
109 * Time until when we blocked this peer from migrating
112 struct GNUNET_TIME_Absolute last_migration_block;
115 * Messages (replies, queries, content migration) we would like to
116 * send to this peer in the near future. Sorted by priority, head.
118 struct GSF_PeerTransmitHandle *pth_head;
121 * Messages (replies, queries, content migration) we would like to
122 * send to this peer in the near future. Sorted by priority, tail.
124 struct GSF_PeerTransmitHandle *pth_tail;
127 * Context of our GNUNET_CORE_peer_change_preference call (or NULL).
128 * NULL if we have successfully reserved 32k, otherwise non-NULL.
130 struct GNUNET_CORE_InformationRequestContext *irc;
133 * ID of delay task for scheduling transmission.
135 GNUNET_SCHEDULER_TaskIdentifier delayed_transmission_request_task; // FIXME: unused!
138 * Increase in traffic preference still to be submitted
139 * to the core service for this peer.
141 uint64_t inc_preference;
144 * Trust rating for this peer on disk.
149 * The peer's identity.
154 * Which offset in "last_p2p_replies" will be updated next?
155 * (we go round-robin).
157 unsigned int last_p2p_replies_woff;
160 * Which offset in "last_client_replies" will be updated next?
161 * (we go round-robin).
163 unsigned int last_client_replies_woff;
166 * Current offset into 'last_request_times' ring buffer.
168 unsigned int last_request_times_off;
174 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
176 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
180 * Where do we store trust information?
182 static char *trustDirectory;
186 * Get the filename under which we would store the GNUNET_HELLO_Message
187 * for the given host and protocol.
188 * @return filename of the form DIRECTORY/HOSTID
191 get_trust_filename (const struct GNUNET_PeerIdentity *id)
193 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
196 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
197 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
203 * Find latency information in 'atsi'.
205 * @param atsi performance data
206 * @return connection latency
208 static struct GNUNET_TIME_Relative
209 get_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
212 return GNUNET_TIME_UNIT_SECONDS;
213 while ( (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
214 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY) )
216 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
219 /* how can we not have latency data? */
220 return GNUNET_TIME_UNIT_SECONDS;
222 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
223 ntohl (atsi->value));
228 * Update the performance information kept for the given peer.
230 * @param cp peer record to update
231 * @param atsi transport performance data
234 update_atsi (struct GSF_ConnectedPeer *cp,
235 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
237 struct GNUNET_TIME_Relative latency;
239 latency = get_latency (atsi);
240 GNUNET_LOAD_value_set_decline (cp->transmission_delay,
242 /* LATER: merge atsi into cp's performance data (if we ever care...) */
247 * Return the performance data record for the given peer
249 * @param cp peer to query
250 * @return performance data record for the peer
252 struct GSF_PeerPerformanceData *
253 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
260 * Core is ready to transmit to a peer, get the message.
262 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
263 * @param size number of bytes core is willing to take
264 * @param buf where to copy the message
265 * @return number of bytes copied to buf
268 peer_transmit_ready_cb (void *cls,
272 struct GSF_PeerTransmitHandle *pth = cls;
273 struct GSF_ConnectedPeer *cp;
276 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
278 GNUNET_SCHEDULER_cancel (pth->timeout_task);
279 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
282 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
287 cp->ppd.last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
288 GNUNET_assert (0 < cp->ppd.pending_queries--);
292 GNUNET_assert (0 < cp->ppd.pending_replies--);
294 GNUNET_LOAD_update (cp->ppd.transmission_delay,
295 GNUNET_TIME_absolute_get_duration (pth->request_start_time).rel_value);
296 ret = pth->gmc (pth->gmc_cls,
304 * Function called by core upon success or failure of our bandwidth reservation request.
306 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
307 * @param peer identifies the peer
308 * @param bandwidth_out available amount of outbound bandwidth
309 * @param amount set to the amount that was actually reserved or unreserved;
310 * either the full requested amount or zero (no partial reservations)
311 * @param preference current traffic preference for the given peer
314 core_reserve_callback (void *cls,
315 const struct GNUNET_PeerIdentity * peer,
316 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
320 struct GSF_ConnectedPeer *cp = cls;
326 /* failed; retry! (how did we get here!?) */
327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
328 _("Failed to reserve bandwidth to peer `%s'\n"),
330 ip = cp->inc_preference;
331 cp->inc_preference = 0;
332 cp->irc = GNUNET_CORE_peer_change_preference (core,
334 GNUNET_TIME_UNIT_FOREVER_REL,
335 GNUNET_BANDWIDTH_VALUE_MAX,
336 GNUNET_FS_DBLOCK_SIZE,
338 &core_reserve_callback,
343 if ( (NULL != pth) &&
346 /* reservation success, try transmission now! */
347 pth->cth = GNUNET_CORE_notify_transmit_ready (core,
349 GNUNET_TIME_absolute_get_remaining (pth->timeout),
352 &peer_transmit_ready_cb,
359 * A peer connected to us. Setup the connected peer
362 * @param peer identity of peer that connected
363 * @param atsi performance data for the connection
364 * @return handle to connected peer entry
366 struct GSF_ConnectedPeer *
367 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
368 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
370 struct GSF_ConnectedPeer *cp;
373 struct GNUNET_TIME_Relative latency;
375 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
376 cp->transmission_delay = GNUNET_LOAD_value_init (latency);
377 cp->pid = GNUNET_PEER_intern (peer);
378 cp->transmission_delay = GNUNET_LOAD_value_init (0);
379 cp->irc = GNUNET_CORE_peer_change_preference (core,
381 GNUNET_TIME_UNIT_FOREVER_REL,
382 GNUNET_BANDWIDTH_VALUE_MAX,
383 GNUNET_FS_DBLOCK_SIZE,
385 &core_reserve_callback,
387 fn = get_trust_filename (peer);
388 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
389 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
390 cp->disk_trust = cp->trust = ntohl (trust);
392 GNUNET_break (GNUNET_OK ==
393 GNUNET_CONTAINER_multihashmap_put (cp_map,
396 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
397 update_atsi (cp, atsi);
398 GSF_plan_notify_new_peer_ (cp);
404 * Handle P2P "MIGRATION_STOP" message.
406 * @param cls closure, always NULL
407 * @param other the other peer involved (sender or receiver, NULL
408 * for loopback messages where we are both sender and receiver)
409 * @param message the actual message
410 * @param atsi performance information
411 * @return GNUNET_OK to keep the connection open,
412 * GNUNET_SYSERR to close it (signal serious error)
415 GSF_handle_p2p_migration_stop_ (void *cls,
416 const struct GNUNET_PeerIdentity *other,
417 const struct GNUNET_MessageHeader *message,
418 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
420 struct GSF_ConnectedPeer *cp;
421 const struct MigrationStopMessage *msm;
423 msm = (const struct MigrationStopMessage*) message;
424 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
431 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (msm->duration));
432 update_atsi (cp, atsi);
438 * Handle a reply to a pending request. Also called if a request
439 * expires (then with data == NULL). The handler may be called
440 * many times (depending on the request type), but will not be
441 * called during or after a call to GSF_pending_request_cancel
442 * and will also not be called anymore after a call signalling
445 * @param cls user-specified closure
446 * @param pr handle to the original pending request
447 * @param data response data, NULL on request expiration
448 * @param data_len number of bytes in data
451 handle_p2p_reply (void *cls,
452 struct GSF_PendingRequest *pr,
457 struct GNUNET_TIME_Relative art_delay;
460 /* FIXME: adapt code fragments below to new API! */
463 /* reply will go over the network, check for cover traffic */
464 if ( (prq->anonymity_level > 1) &&
465 (cover_content_count < prq->anonymity_level - 1) )
467 /* insufficient cover traffic, skip */
468 GNUNET_STATISTICS_update (stats,
469 gettext_noop ("# replies suppressed due to lack of cover traffic"),
474 if (prq->anonymity_level > 1)
475 cover_content_count -= prq->anonymity_level - 1;
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Transmitting result for query `%s' to other peer (PID=%u)\n",
483 (unsigned int) cp->pid);
485 GNUNET_STATISTICS_update (stats,
486 gettext_noop ("# replies received for other peers"),
489 msize = sizeof (struct PutMessage) + prq->size;
490 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
491 reply->cont = &transmit_reply_continuation;
492 reply->cont_cls = pr;
494 art_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
495 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
498 = GNUNET_TIME_relative_to_absolute (art_delay);
499 GNUNET_STATISTICS_update (stats,
500 gettext_noop ("cummulative artificial delay introduced (ms)"),
504 reply->msize = msize;
505 reply->priority = UINT32_MAX; /* send replies first! */
506 pm = (struct PutMessage*) &reply[1];
507 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
508 pm->header.size = htons (msize);
509 pm->type = htonl (prq->type);
510 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
511 memcpy (&pm[1], prq->data, prq->size);
512 add_to_pending_messages_for_peer (cp, reply, pr);
520 * Handle P2P "QUERY" message.
522 * @param other the other peer involved (sender or receiver, NULL
523 * for loopback messages where we are both sender and receiver)
524 * @param message the actual message
525 * @return pending request handle, NULL on error
527 struct GSF_PendingRequest *
528 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
529 const struct GNUNET_MessageHeader *message)
531 /* FIXME: adapt old code to new API! */
532 struct PendingRequest *pr;
533 struct ConnectedPeer *cp;
534 struct ConnectedPeer *cps;
535 struct CheckDuplicateRequestClosure cdc;
536 struct GNUNET_TIME_Relative timeout;
538 const struct GetMessage *gm;
540 const GNUNET_HashCode *opt;
543 uint32_t ttl_decrement;
545 enum GNUNET_BLOCK_Type type;
548 msize = ntohs(message->size);
549 if (msize < sizeof (struct GetMessage))
552 return GNUNET_SYSERR;
554 gm = (const struct GetMessage*) message;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Received request for `%s'\n",
558 GNUNET_h2s (&gm->query));
560 type = ntohl (gm->type);
561 bm = ntohl (gm->hash_bitmap);
569 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
572 return GNUNET_SYSERR;
574 opt = (const GNUNET_HashCode*) &gm[1];
575 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
576 /* bfsize must be power of 2, check! */
577 if (0 != ( (bfsize - 1) & bfsize))
580 return GNUNET_SYSERR;
583 bm = ntohl (gm->hash_bitmap);
585 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
589 /* peer must have just disconnected */
590 GNUNET_STATISTICS_update (stats,
591 gettext_noop ("# requests dropped due to initiator not being connected"),
594 return GNUNET_SYSERR;
596 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
597 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
604 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
607 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 "Failed to find peer `%4s' in connection set. Dropping query.\n",
614 GNUNET_STATISTICS_update (stats,
615 gettext_noop ("# requests dropped due to missing reverse route"),
618 /* FIXME: try connect? */
621 /* note that we can really only check load here since otherwise
622 peers could find out that we are overloaded by not being
623 disconnected after sending us a malformed query... */
624 priority = bound_priority (ntohl (gm->priority), cps);
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Dropping query from `%s', this peer is too busy.\n",
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
637 GNUNET_h2s (&gm->query),
642 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
643 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
644 (have_ns ? sizeof(GNUNET_HashCode) : 0));
647 pr->namespace = (GNUNET_HashCode*) &pr[1];
648 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
650 if ( (GNUNET_LOAD_get_load (cp->transmission_delay) > 3 * (1 + priority)) ||
651 (GNUNET_LOAD_get_average (cp->transmission_delay) >
652 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 + GNUNET_LOAD_get_average (rt_entry_lifetime)) )
654 /* don't have BW to send to peer, or would likely take longer than we have for it,
655 so at best indirect the query */
657 pr->forward_only = GNUNET_YES;
660 pr->mingle = ntohl (gm->filter_mutator);
661 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
662 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
663 pr->anonymity_level = 1;
664 pr->priority = (uint32_t) priority;
665 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
666 pr->query = gm->query;
667 /* decrement ttl (always) */
668 ttl_decrement = 2 * TTL_DECREMENT +
669 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
671 if ( (pr->ttl < 0) &&
672 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
681 GNUNET_STATISTICS_update (stats,
682 gettext_noop ("# requests dropped due TTL underflow"),
685 /* integer underflow => drop (should be very rare)! */
689 pr->ttl -= ttl_decrement;
690 pr->start_time = GNUNET_TIME_absolute_get ();
692 /* get bloom filter */
695 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
698 pr->bf_size = bfsize;
702 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
704 &check_duplicate_request_peer,
706 if (cdc.have != NULL)
708 if (cdc.have->start_time.abs_value + cdc.have->ttl >=
709 pr->start_time.abs_value + pr->ttl)
711 /* existing request has higher TTL, drop new one! */
712 cdc.have->priority += pr->priority;
713 destroy_pending_request (pr);
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Have existing request with higher TTL, dropping new request.\n",
719 GNUNET_STATISTICS_update (stats,
720 gettext_noop ("# requests dropped due to higher-TTL request"),
727 /* existing request has lower TTL, drop old one! */
728 pr->priority += cdc.have->priority;
729 /* Possible optimization: if we have applicable pending
730 replies in 'cdc.have', we might want to move those over
731 (this is a really rare special-case, so it is not clear
732 that this would be worth it) */
733 destroy_pending_request (cdc.have);
734 /* keep processing 'pr'! */
739 GNUNET_break (GNUNET_OK ==
740 GNUNET_CONTAINER_multihashmap_put (query_request_map,
743 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
744 GNUNET_break (GNUNET_OK ==
745 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
748 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
750 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
752 pr->start_time.abs_value + pr->ttl);
754 GNUNET_STATISTICS_update (stats,
755 gettext_noop ("# P2P searches received"),
758 GNUNET_STATISTICS_update (stats,
759 gettext_noop ("# P2P searches active"),
763 /* calculate change in traffic preference */
764 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
765 /* process locally */
766 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
767 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
768 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
770 if (GNUNET_YES != pr->forward_only)
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "Handing request for `%s' to datastore\n",
775 GNUNET_h2s (&gm->query));
777 pr->qe = GNUNET_DATASTORE_get (dsh,
783 &process_local_reply,
787 GNUNET_STATISTICS_update (stats,
788 gettext_noop ("# requests dropped by datastore (queue length limit)"),
795 GNUNET_STATISTICS_update (stats,
796 gettext_noop ("# requests forwarded due to high load"),
801 /* Are multiple results possible (and did we look locally)? If so, start processing remotely now! */
804 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
805 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
806 /* only one result, wait for datastore */
807 if (GNUNET_YES != pr->forward_only)
809 GNUNET_STATISTICS_update (stats,
810 gettext_noop ("# requests not instantly forwarded (waiting for datastore)"),
816 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
817 pr->task = GNUNET_SCHEDULER_add_now (&forward_request_task,
821 /* make sure we don't track too many requests */
822 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
824 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
825 GNUNET_assert (pr != NULL);
826 destroy_pending_request (pr);
834 // setup pending request (use 'handle_p2p_reply')
835 // track pending request to cancel it on peer disconnect (!)
837 // (actual planning & execution up to caller!)
843 * Function called if there has been a timeout trying to satisfy
844 * a transmission request.
846 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
847 * @param tc scheduler context
850 peer_transmit_timeout (void *cls,
851 const struct GNUNET_SCHEDULER_TaskContext *tc)
853 struct GSF_PeerTransmitHandle *pth = cls;
854 struct GSF_ConnectedPeer *cp;
856 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
858 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
862 GNUNET_assert (0 < cp->ppd.pending_queries--);
864 GNUNET_assert (0 < cp->ppd.pending_replies--);
865 GNUNET_LOAD_update (cp->ppd.transmission_delay,
867 pth->gmc (pth->gmc_cls,
874 * Transmit a message to the given peer as soon as possible.
875 * If the peer disconnects before the transmission can happen,
876 * the callback is invoked with a 'NULL' buffer.
878 * @param peer target peer
879 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO)
880 * @param priority how important is this request?
881 * @param timeout when does this request timeout (call gmc with error)
882 * @param size number of bytes we would like to send to the peer
883 * @param gmc function to call to get the message
884 * @param gmc_cls closure for gmc
885 * @return handle to cancel request
887 struct GSF_PeerTransmitHandle *
888 GSF_peer_transmit_ (struct GSF_ConnectedPeer *peer,
891 struct GNUNET_TIME_Relative timeout,
893 GSF_GetMessageCallback gmc,
896 struct GSF_ConnectedPeer *cp;
897 struct GSF_PeerTransmitHandle *pth;
898 struct GSF_PeerTransmitHandle *pos;
899 struct GSF_PeerTransmitHandle *prev;
900 struct GNUNET_PeerIdentity target;
904 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
906 GNUNET_assert (NULL != cp);
907 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
908 pth->transmission_request_start_time = GNUNET_TIME_absolute_now ();
909 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
911 pth->gmc_cls = gmc_cls;
913 pth->is_query = is_query;
914 pth->priority = priority;
916 /* insertion sort (by priority, descending) */
919 while ( (pos != NULL) &&
920 (pos->priority > priority) )
926 GNUNET_CONTAINER_DLL_insert_head (cp->pth_head,
930 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head,
934 GNUNET_PEER_resolve (cp->pid,
938 /* query, need reservation */
941 /* reservation already done! */
942 is_ready = GNUNET_YES;
943 ip = cp->inc_preference;
944 cp->inc_preference = 0;
945 cp->irc = GNUNET_CORE_peer_change_preference (core,
947 GNUNET_TIME_UNIT_FOREVER_REL,
948 GNUNET_BANDWIDTH_VALUE_MAX,
949 GNUNET_FS_DBLOCK_SIZE,
951 &core_reserve_callback,
956 /* still waiting for reservation */
957 is_ready = GNUNET_NO;
962 /* no reservation needed for content */
963 is_ready = GNUNET_YES;
967 pth->cth = GNUNET_CORE_notify_transmit_ready (core,
972 &peer_transmit_ready_cb,
974 /* pth->cth could be NULL here, that's OK, we'll try again
977 if (pth->cth == NULL)
979 /* if we're waiting for reservation OR if we could not do notify_transmit_ready,
980 install a timeout task to be on the safe side */
981 pth->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
982 &peer_transmit_timeout,
990 * Cancel an earlier request for transmission.
992 * @param pth request to cancel
995 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
997 struct GSF_PeerTransmitHandle *pth = cls;
998 struct GSF_ConnectedPeer *cp;
1000 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1002 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1003 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1005 if (NULL != pth->cth)
1007 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1011 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1015 GNUNET_assert (0 < cp->ppd.pending_queries--);
1017 GNUNET_assert (0 < cp->ppd.pending_replies--);
1023 * Report on receiving a reply; update the performance record of the given peer.
1025 * @param cp responding peer (will be updated)
1026 * @param request_time time at which the original query was transmitted
1027 * @param request_priority priority of the original request
1028 * @param initiator_client local client on responsible for query (or NULL)
1029 * @param initiator_peer other peer responsible for query (or NULL)
1032 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1033 struct GNUNET_TIME_Absolute request_time,
1034 uint32_t request_priority,
1035 const struct GSF_LocalClient *initiator_client,
1036 const struct GSF_ConnectedPeer *initiator_peer)
1038 struct GNUNET_TIME_Relative delay;
1041 delay = GNUNET_TIME_absolute_get_duration (request_time);
1042 cp->ppd.avg_reply_delay = (cp->ppd.avg_reply_delay * (RUNAVG_DELAY_N-1) + delay.rel_value) / RUNAVG_DELAY_N;
1043 cp->ppd.avg_priority = (cp->avg_priority * (RUNAVG_DELAY_N-1) + request_priority) / RUNAVG_DELAY_N;
1044 if (NULL != initiator_client)
1046 cp->ppd.last_client_replies[cp->last_client_replies_woff++ % CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1048 else if (NULL != initiator_peer)
1050 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE], -1);
1051 cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff++ % P2P_SUCCESS_LIST_SIZE] = initiator_peer->pid;
1052 GNUNET_PEER_change_rc (initiator_peer->pid, 1);
1060 * Method called whenever a given peer has a status change.
1062 * @param cls closure
1063 * @param peer peer identity this notification is about
1064 * @param bandwidth_in available amount of inbound bandwidth
1065 * @param bandwidth_out available amount of outbound bandwidth
1066 * @param timeout absolute time when this peer will time out
1067 * unless we see some further activity from it
1068 * @param atsi status information
1071 GSF_peer_status_handler_ (void *cls,
1072 const struct GNUNET_PeerIdentity *peer,
1073 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1074 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1075 struct GNUNET_TIME_Absolute timeout,
1076 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1078 struct GSF_ConnectedPeer *cp;
1080 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1082 GNUNET_assert (NULL != cp);
1083 update_atsi (cp, atsi);
1088 * A peer disconnected from us. Tear down the connected peer
1092 * @param peer identity of peer that connected
1095 GSF_peer_disconnect_handler_ (void *cls,
1096 const struct GNUNET_PeerIdentity *peer)
1098 struct GSF_ConnectedPeer *cp;
1099 struct GSF_PeerTransmitHandle *pth;
1101 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1103 GNUNET_assert (NULL != cp);
1104 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1107 if (NULL != cp->irc)
1109 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1112 GSF_plan_notify_peer_disconnect_ (cp);
1113 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1114 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1115 while (NULL != (pth = cp->pth_head))
1117 if (NULL != pth->th)
1119 GNUNET_CORE_notify_transmit_ready_cancel (pth->th);
1122 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1127 GNUNET_PEER_change_rc (cp->pid, -1);
1133 * Closure for 'call_iterator'.
1135 struct IterationContext
1138 * Function to call on each entry.
1140 GSF_ConnectedPeerIterator it;
1150 * Function that calls the callback for each peer.
1152 * @param cls the 'struct IterationContext*'
1153 * @param key identity of the peer
1154 * @param value the 'struct GSF_ConnectedPeer*'
1155 * @return GNUNET_YES to continue iteration
1158 call_iterator (void *cls,
1159 const GNUNET_HashCode *key,
1162 struct IterationContext *ic = cls;
1163 struct GSF_ConnectedPeer *cp = value;
1166 (const struct GNUNET_PeerIdentity*) key,
1174 * Iterate over all connected peers.
1176 * @param it function to call for each peer
1177 * @param it_cls closure for it
1180 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1183 struct IterationContext ic;
1187 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1194 * Obtain the identity of a connected peer.
1196 * @param cp peer to reserve bandwidth from
1197 * @param id identity to set (written to)
1200 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1201 struct GNUNET_PeerIdentity *id)
1203 GNUNET_PEER_resolve (cp->pid,
1209 * Ask a peer to stop migrating data to us until the given point
1212 * @param cp peer to ask
1213 * @param block_time until when to block
1216 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1217 struct GNUNET_TIME_Relative block_time)
1219 struct PendingMessage *pm;
1220 struct MigrationStopMessage *msm;
1222 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).rel_value > block_time.rel_value)
1223 return; /* already blocked */
1224 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1226 /* FIXME: adapt old code below to new API! */
1227 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1228 sizeof (struct MigrationStopMessage));
1229 pm->msize = sizeof (struct MigrationStopMessage);
1230 pm->priority = UINT32_MAX;
1231 msm = (struct MigrationStopMessage*) &pm[1];
1232 msm->header.size = htons (sizeof (struct MigrationStopMessage));
1233 msm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1234 msm->duration = GNUNET_TIME_relative_hton (block_time);
1235 add_to_pending_messages_for_peer (cp,
1244 * Write host-trust information to a file - flush the buffer entry!
1246 * @param cls closure, not used
1247 * @param key host identity
1248 * @param value the 'struct GSF_ConnectedPeer' to flush
1249 * @return GNUNET_OK to continue iteration
1252 flush_trust (void *cls,
1253 const GNUNET_HashCode *key,
1256 struct GSF_ConnectedPeer *cp = value;
1259 struct GNUNET_PeerIdentity pid;
1261 if (cp->trust == cp->disk_trust)
1262 return GNUNET_OK; /* unchanged */
1263 GNUNET_PEER_resolve (cp->pid,
1265 fn = get_trust_filename (&pid);
1268 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1269 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1270 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1274 trust = htonl (cp->trust);
1275 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1277 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1278 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1279 cp->disk_trust = cp->trust;
1287 * Notify core about a preference we have for the given peer
1288 * (to allocate more resources towards it). The change will
1289 * be communicated the next time we reserve bandwidth with
1290 * core (not instantly).
1292 * @param cp peer to reserve bandwidth from
1293 * @param pref preference change
1296 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1299 cp->inc_preference += pref;
1304 * Call this method periodically to flush trust information to disk.
1306 * @param cls closure, not used
1307 * @param tc task context, not used
1310 cron_flush_trust (void *cls,
1311 const struct GNUNET_SCHEDULER_TaskContext *tc)
1316 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1321 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1323 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ,
1330 * Initialize peer management subsystem.
1332 * @param cfg configuration to use
1335 GSF_connected_peer_init_ (struct GNUNET_CONFIGURATION_Handle *cfg)
1337 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1338 GNUNET_assert (GNUNET_OK ==
1339 GNUNET_CONFIGURATION_get_value_filename (cfg,
1343 GNUNET_DISK_directory_create (trustDirectory);
1344 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1345 &cron_flush_trust, NULL);
1350 * Iterator to free peer entries.
1352 * @param cls closure, unused
1353 * @param key current key code
1354 * @param value value in the hash map (peer entry)
1355 * @return GNUNET_YES (we should continue to iterate)
1358 clean_peer (void *cls,
1359 const GNUNET_HashCode * key,
1362 GSF_peer_disconnect_handler_ (NULL,
1363 (const struct GNUNET_PeerIdentity*) key);
1369 * Shutdown peer management subsystem.
1372 GSF_connected_peer_done_ ()
1374 cron_flush_trust (NULL, NULL);
1375 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1378 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1380 GNUNET_free (trustDirectory);
1381 trustDirectory = NULL;
1386 * Iterator to remove references to LC entry.
1388 * @param the 'struct GSF_LocalClient*' to look for
1389 * @param key current key code
1390 * @param value value in the hash map (peer entry)
1391 * @return GNUNET_YES (we should continue to iterate)
1394 clean_local_client (void *cls,
1395 const GNUNET_HashCode * key,
1398 const struct GSF_LocalClient *lc = cls;
1399 struct GSF_ConnectedPeer *cp = value;
1402 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1403 if (cp->ppd.last_client_replies[i] == lc)
1404 cp->ppd.last_client_replies[i] = NULL;
1410 * Notification that a local client disconnected. Clean up all of our
1411 * references to the given handle.
1413 * @param lc handle to the local client (henceforth invalid)
1416 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
1418 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1419 &clean_local_client,
1424 /* end of gnunet-service-fs_cp.c */