2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs_cp.c
23 * @brief API to handle 'connected peers'
24 * @author Christian Grothoff
27 #include "gnunet_load_lib.h"
28 #include "gnunet-service-fs.h"
29 #include "gnunet-service-fs_cp.h"
30 #include "gnunet-service-fs_pe.h"
31 #include "gnunet-service-fs_pr.h"
32 #include "gnunet-service-fs_push.h"
35 * How often do we flush trust values to disk?
37 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
40 * After how long do we discard a reply?
42 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
46 * Handle to cancel a transmission request.
48 struct GSF_PeerTransmitHandle
52 * Kept in a doubly-linked list.
54 struct GSF_PeerTransmitHandle *next;
57 * Kept in a doubly-linked list.
59 struct GSF_PeerTransmitHandle *prev;
62 * Handle for an active request for transmission to this
63 * peer, or NULL (if core queue was full).
65 struct GNUNET_CORE_TransmitHandle *cth;
68 * Time when this transmission request was issued.
70 struct GNUNET_TIME_Absolute transmission_request_start_time;
73 * Timeout for this request.
75 struct GNUNET_TIME_Absolute timeout;
78 * Task called on timeout, or 0 for none.
80 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
83 * Function to call to get the actual message.
85 GSF_GetMessageCallback gmc;
88 * Peer this request targets.
90 struct GSF_ConnectedPeer *cp;
98 * Size of the message to be transmitted.
103 * Set to 1 if we're currently in the process of calling
104 * 'GNUNET_CORE_notify_transmit_ready' (so while cth is
105 * NULL, we should not call notify_transmit_ready for this
108 unsigned int cth_in_progress;
111 * GNUNET_YES if this is a query, GNUNET_NO for content.
116 * Did we get a reservation already?
121 * Priority of this request.
129 * Handle for an entry in our delay list.
131 struct GSF_DelayedHandle
135 * Kept in a doubly-linked list.
137 struct GSF_DelayedHandle *next;
140 * Kept in a doubly-linked list.
142 struct GSF_DelayedHandle *prev;
145 * Peer this transmission belongs to.
147 struct GSF_ConnectedPeer *cp;
150 * The PUT that was delayed.
152 struct PutMessage *pm;
155 * Task for the delay.
157 GNUNET_SCHEDULER_TaskIdentifier delay_task;
160 * Size of the message.
168 * Information per peer and request.
174 * Handle to generic request.
176 struct GSF_PendingRequest *pr;
179 * Handle to specific peer.
181 struct GSF_ConnectedPeer *cp;
184 * Task for asynchronous stopping of this request.
186 GNUNET_SCHEDULER_TaskIdentifier kill_task;
194 struct GSF_ConnectedPeer
198 * Performance data for this peer.
200 struct GSF_PeerPerformanceData ppd;
203 * Time until when we blocked this peer from migrating
206 struct GNUNET_TIME_Absolute last_migration_block;
209 * Task scheduled to revive migration to this peer.
211 GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
214 * Messages (replies, queries, content migration) we would like to
215 * send to this peer in the near future. Sorted by priority, head.
217 struct GSF_PeerTransmitHandle *pth_head;
220 * Messages (replies, queries, content migration) we would like to
221 * send to this peer in the near future. Sorted by priority, tail.
223 struct GSF_PeerTransmitHandle *pth_tail;
226 * Messages (replies, queries, content migration) we would like to
227 * send to this peer in the near future. Sorted by priority, head.
229 struct GSF_DelayedHandle *delayed_head;
232 * Messages (replies, queries, content migration) we would like to
233 * send to this peer in the near future. Sorted by priority, tail.
235 struct GSF_DelayedHandle *delayed_tail;
238 * Migration stop message in our queue, or NULL if we have none pending.
240 struct GSF_PeerTransmitHandle *migration_pth;
243 * Context of our GNUNET_CORE_peer_change_preference call (or NULL).
245 struct GNUNET_CORE_InformationRequestContext *irc;
248 * Task scheduled if we need to retry bandwidth reservation later.
250 GNUNET_SCHEDULER_TaskIdentifier irc_delay_task;
253 * Active requests from this neighbour, map of query to 'struct PeerRequest'.
255 struct GNUNET_CONTAINER_MultiHashMap *request_map;
258 * Increase in traffic preference still to be submitted
259 * to the core service for this peer.
261 uint64_t inc_preference;
264 * Trust rating for this peer on disk.
269 * Which offset in "last_p2p_replies" will be updated next?
270 * (we go round-robin).
272 unsigned int last_p2p_replies_woff;
275 * Which offset in "last_client_replies" will be updated next?
276 * (we go round-robin).
278 unsigned int last_client_replies_woff;
281 * Current offset into 'last_request_times' ring buffer.
283 unsigned int last_request_times_off;
286 * GNUNET_YES if we did successfully reserve 32k bandwidth,
295 * Map from peer identities to 'struct GSF_ConnectPeer' entries.
297 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
300 * Where do we store trust information?
302 static char *trustDirectory;
306 * Get the filename under which we would store the GNUNET_HELLO_Message
307 * for the given host and protocol.
308 * @return filename of the form DIRECTORY/HOSTID
311 get_trust_filename (const struct GNUNET_PeerIdentity *id)
313 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
316 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
317 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
323 * Find latency information in 'atsi'.
325 * @param atsi performance data
326 * @return connection latency
328 static struct GNUNET_TIME_Relative
329 get_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
332 return GNUNET_TIME_UNIT_SECONDS;
333 while ( (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
334 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY) )
336 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
339 /* how can we not have latency data? */
340 return GNUNET_TIME_UNIT_SECONDS;
342 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
343 ntohl (atsi->value));
348 * Update the performance information kept for the given peer.
350 * @param cp peer record to update
351 * @param atsi transport performance data
354 update_atsi (struct GSF_ConnectedPeer *cp,
355 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
357 struct GNUNET_TIME_Relative latency;
359 latency = get_latency (atsi);
360 GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay,
362 /* LATER: merge atsi into cp's performance data (if we ever care...) */
367 * Return the performance data record for the given peer
369 * @param cp peer to query
370 * @return performance data record for the peer
372 struct GSF_PeerPerformanceData *
373 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
380 * Core is ready to transmit to a peer, get the message.
382 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
383 * @param size number of bytes core is willing to take
384 * @param buf where to copy the message
385 * @return number of bytes copied to buf
388 peer_transmit_ready_cb (void *cls,
396 * Function called by core upon success or failure of our bandwidth reservation request.
398 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
399 * @param peer identifies the peer
400 * @param bandwidth_out available amount of outbound bandwidth
401 * @param amount set to the amount that was actually reserved or unreserved;
402 * either the full requested amount or zero (no partial reservations)
403 * @param res_delay if the reservation could not be satisfied (amount was 0), how
404 * long should the client wait until re-trying?
405 * @param preference current traffic preference for the given peer
408 core_reserve_callback (void *cls,
409 const struct GNUNET_PeerIdentity *peer,
410 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
412 struct GNUNET_TIME_Relative res_delay,
413 uint64_t preference);
417 * If ready (bandwidth reserved), try to schedule transmission via
418 * core for the given handle.
420 * @param pth transmission handle to schedule
423 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
425 struct GSF_ConnectedPeer *cp;
426 struct GNUNET_PeerIdentity target;
429 if ( (NULL != pth->cth) ||
430 (0 != pth->cth_in_progress) )
431 return; /* already done */
433 GNUNET_assert (0 != cp->ppd.pid);
434 GNUNET_PEER_resolve (cp->ppd.pid,
436 if ( (GNUNET_YES == pth->is_query) &&
437 (GNUNET_YES != pth->was_reserved) )
439 /* query, need reservation */
440 if (GNUNET_YES != cp->did_reserve)
441 return; /* not ready */
442 cp->did_reserve = GNUNET_NO;
443 /* reservation already done! */
444 pth->was_reserved = GNUNET_YES;
445 ip = cp->inc_preference;
446 cp->inc_preference = 0;
447 cp->irc = GNUNET_CORE_peer_change_preference (GSF_core,
449 GNUNET_TIME_UNIT_FOREVER_REL,
450 GNUNET_BANDWIDTH_VALUE_MAX,
453 &core_reserve_callback,
456 GNUNET_assert (pth->cth == NULL);
457 pth->cth_in_progress++;
458 pth->cth = GNUNET_CORE_notify_transmit_ready (GSF_core,
461 GNUNET_TIME_absolute_get_remaining (pth->timeout),
464 &peer_transmit_ready_cb,
466 GNUNET_assert (0 < pth->cth_in_progress--);
471 * Core is ready to transmit to a peer, get the message.
473 * @param cls the 'struct GSF_PeerTransmitHandle' of the message
474 * @param size number of bytes core is willing to take
475 * @param buf where to copy the message
476 * @return number of bytes copied to buf
479 peer_transmit_ready_cb (void *cls,
483 struct GSF_PeerTransmitHandle *pth = cls;
484 struct GSF_PeerTransmitHandle *pos;
485 struct GSF_ConnectedPeer *cp;
488 GNUNET_assert ( (NULL == buf) ||
489 (pth->size <= size) );
491 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
493 GNUNET_SCHEDULER_cancel (pth->timeout_task);
494 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
497 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
500 if (GNUNET_YES == pth->is_query)
502 cp->ppd.last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
503 GNUNET_assert (0 < cp->ppd.pending_queries--);
505 else if (GNUNET_NO == pth->is_query)
507 GNUNET_assert (0 < cp->ppd.pending_replies--);
509 GNUNET_LOAD_update (cp->ppd.transmission_delay,
510 GNUNET_TIME_absolute_get_duration (pth->transmission_request_start_time).rel_value);
511 ret = pth->gmc (pth->gmc_cls,
513 GNUNET_assert (NULL == pth->cth);
514 for (pos = cp->pth_head; pos != NULL; pos = pos->next)
516 GNUNET_assert (pos != pth);
517 schedule_transmission (pos);
519 GNUNET_assert (pth->cth == NULL);
520 GNUNET_assert (pth->cth_in_progress == 0);
527 * (re)try to reserve bandwidth from the given peer.
529 * @param cls the 'struct GSF_ConnectedPeer' to reserve from
530 * @param tc scheduler context
533 retry_reservation (void *cls,
534 const struct GNUNET_SCHEDULER_TaskContext *tc)
536 struct GSF_ConnectedPeer *cp = cls;
538 struct GNUNET_PeerIdentity target;
540 GNUNET_PEER_resolve (cp->ppd.pid,
542 cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
543 ip = cp->inc_preference;
544 cp->inc_preference = 0;
545 cp->irc = GNUNET_CORE_peer_change_preference (GSF_core,
547 GNUNET_TIME_UNIT_FOREVER_REL,
548 GNUNET_BANDWIDTH_VALUE_MAX,
551 &core_reserve_callback,
557 * Function called by core upon success or failure of our bandwidth reservation request.
559 * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
560 * @param peer identifies the peer
561 * @param bandwidth_out available amount of outbound bandwidth
562 * @param amount set to the amount that was actually reserved or unreserved;
563 * either the full requested amount or zero (no partial reservations)
564 * @param res_delay if the reservation could not be satisfied (amount was 0), how
565 * long should the client wait until re-trying?
566 * @param preference current traffic preference for the given peer
569 core_reserve_callback (void *cls,
570 const struct GNUNET_PeerIdentity *peer,
571 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
573 struct GNUNET_TIME_Relative res_delay,
576 struct GSF_ConnectedPeer *cp = cls;
577 struct GSF_PeerTransmitHandle *pth;
582 cp->irc_delay_task = GNUNET_SCHEDULER_add_delayed (res_delay,
587 cp->did_reserve = GNUNET_YES;
589 if ( (NULL != pth) &&
592 /* reservation success, try transmission now! */
593 pth->cth_in_progress++;
594 pth->cth = GNUNET_CORE_notify_transmit_ready (GSF_core,
597 GNUNET_TIME_absolute_get_remaining (pth->timeout),
600 &peer_transmit_ready_cb,
602 GNUNET_assert (0 < pth->cth_in_progress--);
608 * A peer connected to us. Setup the connected peer
611 * @param peer identity of peer that connected
612 * @param atsi performance data for the connection
613 * @return handle to connected peer entry
615 struct GSF_ConnectedPeer *
616 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
617 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
619 struct GSF_ConnectedPeer *cp;
623 cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
624 cp->ppd.pid = GNUNET_PEER_intern (peer);
625 cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
626 cp->irc = GNUNET_CORE_peer_change_preference (GSF_core,
628 GNUNET_TIME_UNIT_FOREVER_REL,
629 GNUNET_BANDWIDTH_VALUE_MAX,
632 &core_reserve_callback,
634 fn = get_trust_filename (peer);
635 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
636 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
637 cp->disk_trust = cp->ppd.trust = ntohl (trust);
639 cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
640 GNUNET_break (GNUNET_OK ==
641 GNUNET_CONTAINER_multihashmap_put (cp_map,
644 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
645 update_atsi (cp, atsi);
646 GSF_push_start_ (cp);
652 * It may be time to re-start migrating content to this
653 * peer. Check, and if so, restart migration.
655 * @param cls the 'struct GSF_ConnectedPeer'
656 * @param tc scheduler context
659 revive_migration (void *cls,
660 const struct GNUNET_SCHEDULER_TaskContext *tc)
662 struct GSF_ConnectedPeer *cp = cls;
663 struct GNUNET_TIME_Relative bt;
665 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
666 bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
667 if (0 != bt.rel_value)
669 /* still time left... */
671 = GNUNET_SCHEDULER_add_delayed (bt,
676 GSF_push_start_ (cp);
681 * Get a handle for a connected peer.
683 * @param peer peer's identity
684 * @return NULL if the peer is not currently connected
686 struct GSF_ConnectedPeer *
687 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
689 return GNUNET_CONTAINER_multihashmap_get (cp_map,
695 * Handle P2P "MIGRATION_STOP" message.
697 * @param cls closure, always NULL
698 * @param other the other peer involved (sender or receiver, NULL
699 * for loopback messages where we are both sender and receiver)
700 * @param message the actual message
701 * @param atsi performance information
702 * @return GNUNET_OK to keep the connection open,
703 * GNUNET_SYSERR to close it (signal serious error)
706 GSF_handle_p2p_migration_stop_ (void *cls,
707 const struct GNUNET_PeerIdentity *other,
708 const struct GNUNET_MessageHeader *message,
709 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
711 struct GSF_ConnectedPeer *cp;
712 const struct MigrationStopMessage *msm;
713 struct GNUNET_TIME_Relative bt;
715 msm = (const struct MigrationStopMessage*) message;
716 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
723 bt = GNUNET_TIME_relative_ntoh (msm->duration);
724 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
725 _("Migration of content to peer `%s' blocked for %llu ms\n"),
727 (unsigned long long) bt.rel_value);
728 cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
729 if (cp->mig_revive_task == GNUNET_SCHEDULER_NO_TASK)
733 = GNUNET_SCHEDULER_add_delayed (bt,
737 update_atsi (cp, atsi);
743 * Copy reply and free put message.
745 * @param cls the 'struct PutMessage'
746 * @param buf_size number of bytes available in buf
747 * @param buf where to copy the message, NULL on error (peer disconnect)
748 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
751 copy_reply (void *cls,
755 struct PutMessage *pm = cls;
760 GNUNET_assert (buf_size >= ntohs (pm->header.size));
761 size = ntohs (pm->header.size);
762 memcpy (buf, pm, size);
763 GNUNET_STATISTICS_update (GSF_stats,
764 gettext_noop ("# replies transmitted to other peers"),
771 GNUNET_STATISTICS_update (GSF_stats,
772 gettext_noop ("# replies dropped"),
782 * Cancel all requests associated with the peer.
785 * @param query hash code of the request
786 * @param value the 'struct GSF_PendingRequest'
787 * @return GNUNET_YES (continue to iterate)
790 cancel_pending_request (void *cls,
791 const GNUNET_HashCode *query,
794 struct PeerRequest *peerreq = value;
795 struct GSF_PendingRequest *pr = peerreq->pr;
796 struct GSF_ConnectedPeer *cp = peerreq->cp;
797 struct GSF_PendingRequestData *prd;
799 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
801 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
802 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
804 GNUNET_STATISTICS_update (GSF_stats,
805 gettext_noop ("# P2P searches active"),
808 prd = GSF_pending_request_get_data_ (pr);
809 GNUNET_break (GNUNET_OK ==
810 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
813 GSF_pending_request_cancel_ (pr);
814 GNUNET_free (peerreq);
820 * Free the given request.
822 * @param cls the request to free
823 * @param tc task context
826 peer_request_destroy (void *cls,
827 const struct GNUNET_SCHEDULER_TaskContext *tc)
829 struct PeerRequest *peerreq = cls;
830 struct GSF_PendingRequest *pr = peerreq->pr;
831 struct GSF_PendingRequestData *prd;
833 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
834 prd = GSF_pending_request_get_data_ (pr);
835 cancel_pending_request (NULL,
842 * The artificial delay is over, transmit the message now.
844 * @param cls the 'struct GSF_DelayedHandle' with the message
845 * @param tc scheduler context
848 transmit_delayed_now (void *cls,
849 const struct GNUNET_SCHEDULER_TaskContext *tc)
851 struct GSF_DelayedHandle *dh = cls;
852 struct GSF_ConnectedPeer *cp = dh->cp;
854 GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
857 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
859 GNUNET_free (dh->pm);
863 (void) GSF_peer_transmit_ (cp, GNUNET_NO,
874 * Get the randomized delay a response should be subjected to.
876 * @return desired delay
878 static struct GNUNET_TIME_Relative
879 get_randomized_delay ()
881 struct GNUNET_TIME_Relative ret;
883 /* FIXME: replace 5000 with something relating to current observed P2P message latency */
884 ret = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
885 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
887 GNUNET_STATISTICS_update (GSF_stats,
888 gettext_noop ("# artificial delays introduced (ms)"),
897 * Handle a reply to a pending request. Also called if a request
898 * expires (then with data == NULL). The handler may be called
899 * many times (depending on the request type), but will not be
900 * called during or after a call to GSF_pending_request_cancel
901 * and will also not be called anymore after a call signalling
904 * @param cls 'struct PeerRequest' this is an answer for
905 * @param eval evaluation of the result
906 * @param pr handle to the original pending request
907 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
908 * @param expiration when does 'data' expire?
909 * @param type type of the block
910 * @param data response data, NULL on request expiration
911 * @param data_len number of bytes in data
914 handle_p2p_reply (void *cls,
915 enum GNUNET_BLOCK_EvaluationResult eval,
916 struct GSF_PendingRequest *pr,
917 uint32_t reply_anonymity_level,
918 struct GNUNET_TIME_Absolute expiration,
919 enum GNUNET_BLOCK_Type type,
923 struct PeerRequest *peerreq = cls;
924 struct GSF_ConnectedPeer *cp = peerreq->cp;
925 struct GSF_PendingRequestData *prd;
926 struct PutMessage *pm;
929 GNUNET_assert (data_len + sizeof (struct PutMessage) < GNUNET_SERVER_MAX_MESSAGE_SIZE);
930 GNUNET_assert (peerreq->pr == pr);
931 prd = GSF_pending_request_get_data_ (pr);
934 GNUNET_STATISTICS_update (GSF_stats,
935 gettext_noop ("# P2P searches active"),
938 GNUNET_break (GNUNET_OK ==
939 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
942 GNUNET_free (peerreq);
945 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
946 if ( (prd->type != type) &&
947 (prd->type != GNUNET_BLOCK_TYPE_ANY) )
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "Transmitting result for query `%s' to peer\n",
955 GNUNET_h2s (&prd->query));
957 GNUNET_STATISTICS_update (GSF_stats,
958 gettext_noop ("# replies received for other peers"),
961 msize = sizeof (struct PutMessage) + data_len;
962 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
967 if ( (reply_anonymity_level != UINT32_MAX) &&
968 (reply_anonymity_level > 1) )
970 if (reply_anonymity_level - 1 > GSF_cover_content_count)
972 GNUNET_STATISTICS_update (GSF_stats,
973 gettext_noop ("# replies dropped due to insufficient cover traffic"),
978 GSF_cover_content_count -= (reply_anonymity_level - 1);
981 pm = GNUNET_malloc (msize);
982 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
983 pm->header.size = htons (msize);
984 pm->type = htonl (type);
985 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
986 memcpy (&pm[1], data, data_len);
987 if ( (reply_anonymity_level != UINT32_MAX) &&
988 (reply_anonymity_level != 0) &&
989 (GSF_enable_randomized_delays == GNUNET_YES) )
991 struct GSF_DelayedHandle *dh;
993 dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
997 GNUNET_CONTAINER_DLL_insert (cp->delayed_head,
1000 dh->delay_task = GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
1001 &transmit_delayed_now,
1006 (void) GSF_peer_transmit_ (cp, GNUNET_NO,
1013 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1015 if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
1017 GNUNET_STATISTICS_update (GSF_stats,
1018 gettext_noop ("# P2P searches destroyed due to ultimate reply"),
1021 peerreq->kill_task = GNUNET_SCHEDULER_add_now (&peer_request_destroy,
1028 * Increase the host credit by a value.
1030 * @param cp which peer to change the trust value on
1031 * @param value is the int value by which the
1032 * host credit is to be increased or decreased
1033 * @returns the actual change in trust (positive or negative)
1036 change_host_trust (struct GSF_ConnectedPeer *cp, int value)
1040 GNUNET_assert (cp != NULL);
1043 if (cp->ppd.trust + value < cp->ppd.trust)
1045 value = UINT32_MAX - cp->ppd.trust;
1046 cp->ppd.trust = UINT32_MAX;
1049 cp->ppd.trust += value;
1053 if (cp->ppd.trust < -value)
1055 value = -cp->ppd.trust;
1059 cp->ppd.trust += value;
1066 * We've received a request with the specified priority. Bound it
1067 * according to how much we trust the given peer.
1069 * @param prio_in requested priority
1070 * @param cp the peer making the request
1071 * @return effective priority
1074 bound_priority (uint32_t prio_in,
1075 struct GSF_ConnectedPeer *cp)
1077 #define N ((double)128.0)
1082 ld = GSF_test_get_load_too_high_ (0);
1083 if (ld == GNUNET_SYSERR)
1085 GNUNET_STATISTICS_update (GSF_stats,
1086 gettext_noop ("# requests done for free (low load)"),
1089 return 0; /* excess resources */
1091 if (prio_in > INT32_MAX)
1092 prio_in = INT32_MAX;
1093 ret = - change_host_trust (cp, - (int) prio_in);
1096 if (ret > GSF_current_priorities + N)
1097 rret = GSF_current_priorities + N;
1100 GSF_current_priorities
1101 = (GSF_current_priorities * (N-1) + rret)/N;
1103 if ( (ld == GNUNET_YES) && (ret > 0) )
1105 /* try with charging */
1106 ld = GSF_test_get_load_too_high_ (ret);
1108 if (ld == GNUNET_YES)
1110 GNUNET_STATISTICS_update (GSF_stats,
1111 gettext_noop ("# request dropped, priority insufficient"),
1115 change_host_trust (cp, (int) ret);
1116 return -1; /* not enough resources */
1120 GNUNET_STATISTICS_update (GSF_stats,
1121 gettext_noop ("# requests done for a price (normal load)"),
1131 * The priority level imposes a bound on the maximum
1132 * value for the ttl that can be requested.
1134 * @param ttl_in requested ttl
1135 * @param prio given priority
1136 * @return ttl_in if ttl_in is below the limit,
1137 * otherwise the ttl-limit for the given priority
1140 bound_ttl (int32_t ttl_in, uint32_t prio)
1142 unsigned long long allowed;
1146 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1147 if (ttl_in > allowed)
1149 if (allowed >= (1 << 30))
1158 * Handle P2P "QUERY" message. Creates the pending request entry
1159 * and sets up all of the data structures to that we will
1160 * process replies properly. Does not initiate forwarding or
1161 * local database lookups.
1163 * @param other the other peer involved (sender or receiver, NULL
1164 * for loopback messages where we are both sender and receiver)
1165 * @param message the actual message
1166 * @return pending request handle, NULL on error
1168 struct GSF_PendingRequest *
1169 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1170 const struct GNUNET_MessageHeader *message)
1172 struct PeerRequest *peerreq;
1173 struct GSF_PendingRequest *pr;
1174 struct GSF_PendingRequestData *prd;
1175 struct GSF_ConnectedPeer *cp;
1176 struct GSF_ConnectedPeer *cps;
1177 const GNUNET_HashCode *namespace;
1178 const struct GNUNET_PeerIdentity *target;
1179 enum GSF_PendingRequestOptions options;
1181 const struct GetMessage *gm;
1183 const GNUNET_HashCode *opt;
1186 uint32_t ttl_decrement;
1189 enum GNUNET_BLOCK_Type type;
1190 GNUNET_PEER_Id spid;
1192 msize = ntohs(message->size);
1193 if (msize < sizeof (struct GetMessage))
1195 GNUNET_break_op (0);
1198 GNUNET_STATISTICS_update (GSF_stats,
1199 gettext_noop ("# GET requests received (from other peers)"),
1202 gm = (const struct GetMessage*) message;
1203 type = ntohl (gm->type);
1204 bm = ntohl (gm->hash_bitmap);
1212 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
1214 GNUNET_break_op (0);
1217 opt = (const GNUNET_HashCode*) &gm[1];
1218 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
1219 /* bfsize must be power of 2, check! */
1220 if (0 != ( (bfsize - 1) & bfsize))
1222 GNUNET_break_op (0);
1225 GSF_cover_query_count++;
1226 bm = ntohl (gm->hash_bitmap);
1228 cps = GNUNET_CONTAINER_multihashmap_get (cp_map,
1229 &other->hashPubKey);
1232 /* peer must have just disconnected */
1233 GNUNET_STATISTICS_update (GSF_stats,
1234 gettext_noop ("# requests dropped due to initiator not being connected"),
1239 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1240 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1247 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1250 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Failed to find peer `%4s' in connection set. Dropping query.\n",
1255 GNUNET_i2s (other));
1257 GNUNET_STATISTICS_update (GSF_stats,
1258 gettext_noop ("# requests dropped due to missing reverse route"),
1263 /* note that we can really only check load here since otherwise
1264 peers could find out that we are overloaded by not being
1265 disconnected after sending us a malformed query... */
1266 priority = bound_priority (ntohl (gm->priority), cps);
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 "Dropping query from `%s', this peer is too busy.\n",
1272 GNUNET_i2s (other));
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1279 GNUNET_h2s (&gm->query),
1280 (unsigned int) type,
1284 namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1285 if ( (type == GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
1286 (namespace == NULL) )
1288 GNUNET_break_op (0);
1291 if ( (type != GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
1292 (namespace != NULL) )
1294 GNUNET_break_op (0);
1297 target = (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity*) &opt[bits++]) : NULL;
1300 if ( (GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority)) ||
1301 (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
1302 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 + GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)) )
1304 /* don't have BW to send to peer, or would likely take longer than we have for it,
1305 so at best indirect the query */
1307 options |= GSF_PRO_FORWARD_ONLY;
1308 spid = GNUNET_PEER_intern (other);
1310 ttl = bound_ttl (ntohl (gm->ttl), priority);
1311 /* decrement ttl (always) */
1312 ttl_decrement = 2 * TTL_DECREMENT +
1313 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1316 (((int32_t)(ttl - ttl_decrement)) > 0) )
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1325 GNUNET_STATISTICS_update (GSF_stats,
1326 gettext_noop ("# requests dropped due TTL underflow"),
1329 /* integer underflow => drop (should be very rare)! */
1332 ttl -= ttl_decrement;
1334 /* test if the request already exists */
1335 peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map,
1337 if (peerreq != NULL)
1340 prd = GSF_pending_request_get_data_ (pr);
1341 if ( (prd->type == type) &&
1342 ( (type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1343 (0 == memcmp (&prd->namespace,
1345 sizeof (GNUNET_HashCode))) ) )
1347 if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get().abs_value + ttl)
1349 /* existing request has higher TTL, drop new one! */
1350 prd->priority += priority;
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Have existing request with higher TTL, dropping new request.\n",
1354 GNUNET_i2s (other));
1356 GNUNET_STATISTICS_update (GSF_stats,
1357 gettext_noop ("# requests dropped due to higher-TTL request"),
1362 /* existing request has lower TTL, drop old one! */
1363 GNUNET_STATISTICS_update (GSF_stats,
1364 gettext_noop ("# P2P searches active"),
1367 priority += prd->priority;
1368 GSF_pending_request_cancel_ (pr);
1369 GNUNET_assert (GNUNET_YES ==
1370 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
1373 if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
1375 GNUNET_SCHEDULER_cancel (peerreq->kill_task);
1376 peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
1378 GNUNET_free (peerreq);
1382 peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1384 pr = GSF_pending_request_create_ (options,
1389 (bfsize > 0) ? (const char*)&opt[bits] : NULL,
1391 ntohl (gm->filter_mutator),
1393 (uint32_t) priority,
1396 NULL, 0, /* replies_seen */
1399 GNUNET_assert (NULL != pr);
1401 GNUNET_break (GNUNET_OK ==
1402 GNUNET_CONTAINER_multihashmap_put (cp->request_map,
1405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1406 GNUNET_STATISTICS_update (GSF_stats,
1407 gettext_noop ("# P2P query messages received and processed"),
1410 GNUNET_STATISTICS_update (GSF_stats,
1411 gettext_noop ("# P2P searches active"),
1419 * Function called if there has been a timeout trying to satisfy
1420 * a transmission request.
1422 * @param cls the 'struct GSF_PeerTransmitHandle' of the request
1423 * @param tc scheduler context
1426 peer_transmit_timeout (void *cls,
1427 const struct GNUNET_SCHEDULER_TaskContext *tc)
1429 struct GSF_PeerTransmitHandle *pth = cls;
1430 struct GSF_ConnectedPeer *cp;
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1434 "Timeout trying to transmit to other peer\n");
1436 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1438 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1441 if (GNUNET_YES == pth->is_query)
1442 GNUNET_assert (0 < cp->ppd.pending_queries--);
1443 else if (GNUNET_NO == pth->is_query)
1444 GNUNET_assert (0 < cp->ppd.pending_replies--);
1445 GNUNET_LOAD_update (cp->ppd.transmission_delay,
1447 if (NULL != pth->cth)
1449 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1452 pth->gmc (pth->gmc_cls,
1454 GNUNET_assert (0 == pth->cth_in_progress);
1460 * Transmit a message to the given peer as soon as possible.
1461 * If the peer disconnects before the transmission can happen,
1462 * the callback is invoked with a 'NULL' buffer.
1464 * @param cp target peer
1465 * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1466 * @param priority how important is this request?
1467 * @param timeout when does this request timeout (call gmc with error)
1468 * @param size number of bytes we would like to send to the peer
1469 * @param gmc function to call to get the message
1470 * @param gmc_cls closure for gmc
1471 * @return handle to cancel request
1473 struct GSF_PeerTransmitHandle *
1474 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
1477 struct GNUNET_TIME_Relative timeout,
1479 GSF_GetMessageCallback gmc,
1482 struct GSF_PeerTransmitHandle *pth;
1483 struct GSF_PeerTransmitHandle *pos;
1484 struct GSF_PeerTransmitHandle *prev;
1486 pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1487 pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1488 pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1490 pth->gmc_cls = gmc_cls;
1492 pth->is_query = is_query;
1493 pth->priority = priority;
1495 /* insertion sort (by priority, descending) */
1498 while ( (pos != NULL) &&
1499 (pos->priority > priority) )
1505 GNUNET_CONTAINER_DLL_insert (cp->pth_head,
1509 GNUNET_CONTAINER_DLL_insert_after (cp->pth_head,
1513 if (GNUNET_YES == is_query)
1514 cp->ppd.pending_queries++;
1515 else if (GNUNET_NO == is_query)
1516 cp->ppd.pending_replies++;
1517 pth->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1518 &peer_transmit_timeout,
1520 schedule_transmission (pth);
1526 * Cancel an earlier request for transmission.
1528 * @param pth request to cancel
1531 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1533 struct GSF_ConnectedPeer *cp;
1535 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1537 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1538 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1540 if (NULL != pth->cth)
1542 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1546 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1549 if (GNUNET_YES == pth->is_query)
1550 GNUNET_assert (0 < cp->ppd.pending_queries--);
1551 else if (GNUNET_NO == pth->is_query)
1552 GNUNET_assert (0 < cp->ppd.pending_replies--);
1553 GNUNET_assert (0 == pth->cth_in_progress);
1559 * Report on receiving a reply; update the performance record of the given peer.
1561 * @param cp responding peer (will be updated)
1562 * @param request_time time at which the original query was transmitted
1563 * @param request_priority priority of the original request
1566 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1567 struct GNUNET_TIME_Absolute request_time,
1568 uint32_t request_priority)
1570 struct GNUNET_TIME_Relative delay;
1572 delay = GNUNET_TIME_absolute_get_duration (request_time);
1573 cp->ppd.avg_reply_delay.rel_value = (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N-1) + delay.rel_value) / RUNAVG_DELAY_N;
1574 cp->ppd.avg_priority = (cp->ppd.avg_priority * (RUNAVG_DELAY_N-1) + request_priority) / RUNAVG_DELAY_N;
1579 * Report on receiving a reply in response to an initiating client.
1580 * Remember that this peer is good for this client.
1582 * @param cp responding peer (will be updated)
1583 * @param initiator_client local client on responsible for query
1586 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1587 struct GSF_LocalClient *initiator_client)
1589 cp->ppd.last_client_replies[cp->last_client_replies_woff++ % CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1594 * Report on receiving a reply in response to an initiating peer.
1595 * Remember that this peer is good for this initiating peer.
1597 * @param cp responding peer (will be updated)
1598 * @param initiator_peer other peer responsible for query
1601 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1602 const struct GSF_ConnectedPeer *initiator_peer)
1604 GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE], -1);
1605 cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff++ % P2P_SUCCESS_LIST_SIZE] = initiator_peer->ppd.pid;
1606 GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1611 * Method called whenever a given peer has a status change.
1613 * @param cls closure
1614 * @param peer peer identity this notification is about
1615 * @param bandwidth_in available amount of inbound bandwidth
1616 * @param bandwidth_out available amount of outbound bandwidth
1617 * @param timeout absolute time when this peer will time out
1618 * unless we see some further activity from it
1619 * @param atsi status information
1622 GSF_peer_status_handler_ (void *cls,
1623 const struct GNUNET_PeerIdentity *peer,
1624 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1625 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1626 struct GNUNET_TIME_Absolute timeout,
1627 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1629 struct GSF_ConnectedPeer *cp;
1631 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1633 GNUNET_assert (NULL != cp);
1634 update_atsi (cp, atsi);
1639 * A peer disconnected from us. Tear down the connected peer
1643 * @param peer identity of peer that connected
1646 GSF_peer_disconnect_handler_ (void *cls,
1647 const struct GNUNET_PeerIdentity *peer)
1649 struct GSF_ConnectedPeer *cp;
1650 struct GSF_PeerTransmitHandle *pth;
1651 struct GSF_DelayedHandle *dh;
1653 cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1656 return; /* must have been disconnect from core with
1657 'peer' == my_id, ignore */
1658 GNUNET_CONTAINER_multihashmap_remove (cp_map,
1661 if (NULL != cp->migration_pth)
1663 GSF_peer_transmit_cancel_ (cp->migration_pth);
1664 cp->migration_pth = NULL;
1666 if (NULL != cp->irc)
1668 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1671 if (GNUNET_SCHEDULER_NO_TASK != cp->irc_delay_task)
1673 GNUNET_SCHEDULER_cancel (cp->irc_delay_task);
1674 cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1676 GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1677 &cancel_pending_request,
1679 GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1680 cp->request_map = NULL;
1681 GSF_plan_notify_peer_disconnect_ (cp);
1682 GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1683 GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1684 GSF_push_stop_ (cp);
1685 while (NULL != (pth = cp->pth_head))
1687 if (NULL != pth->cth)
1689 GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1692 if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1694 GNUNET_SCHEDULER_cancel (pth->timeout_task);
1695 pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1697 GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1700 GNUNET_assert (0 == pth->cth_in_progress);
1703 while (NULL != (dh = cp->delayed_head))
1705 GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
1708 GNUNET_SCHEDULER_cancel (dh->delay_task);
1709 GNUNET_free (dh->pm);
1712 GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1713 if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1715 GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1716 cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1723 * Closure for 'call_iterator'.
1725 struct IterationContext
1728 * Function to call on each entry.
1730 GSF_ConnectedPeerIterator it;
1740 * Function that calls the callback for each peer.
1742 * @param cls the 'struct IterationContext*'
1743 * @param key identity of the peer
1744 * @param value the 'struct GSF_ConnectedPeer*'
1745 * @return GNUNET_YES to continue iteration
1748 call_iterator (void *cls,
1749 const GNUNET_HashCode *key,
1752 struct IterationContext *ic = cls;
1753 struct GSF_ConnectedPeer *cp = value;
1756 (const struct GNUNET_PeerIdentity*) key,
1764 * Iterate over all connected peers.
1766 * @param it function to call for each peer
1767 * @param it_cls closure for it
1770 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1773 struct IterationContext ic;
1777 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1784 * Obtain the identity of a connected peer.
1786 * @param cp peer to reserve bandwidth from
1787 * @param id identity to set (written to)
1790 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1791 struct GNUNET_PeerIdentity *id)
1793 GNUNET_assert (0 != cp->ppd.pid);
1794 GNUNET_PEER_resolve (cp->ppd.pid,
1800 * Assemble a migration stop message for transmission.
1802 * @param cls the 'struct GSF_ConnectedPeer' to use
1803 * @param size number of bytes we're allowed to write to buf
1804 * @param buf where to copy the message
1805 * @return number of bytes copied to buf
1808 create_migration_stop_message (void *cls,
1812 struct GSF_ConnectedPeer *cp = cls;
1813 struct MigrationStopMessage msm;
1815 cp->migration_pth = NULL;
1818 GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1819 msm.header.size = htons (sizeof (struct MigrationStopMessage));
1820 msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1821 msm.reserved = htonl (0);
1822 msm.duration = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block));
1823 memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1824 return sizeof (struct MigrationStopMessage);
1829 * Ask a peer to stop migrating data to us until the given point
1832 * @param cp peer to ask
1833 * @param block_time until when to block
1836 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1837 struct GNUNET_TIME_Relative block_time)
1839 if (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value > block_time.rel_value)
1842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 "Migration already blocked for another %llu ms\n",
1844 (unsigned long long) GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value);
1846 return; /* already blocked */
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "Asking to stop migration for %llu ms\n",
1851 (unsigned long long) block_time.rel_value);
1853 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1854 if (cp->migration_pth != NULL)
1855 GSF_peer_transmit_cancel_ (cp->migration_pth);
1857 = GSF_peer_transmit_ (cp,
1860 GNUNET_TIME_UNIT_FOREVER_REL,
1861 sizeof (struct MigrationStopMessage),
1862 &create_migration_stop_message,
1868 * Write host-trust information to a file - flush the buffer entry!
1870 * @param cls closure, not used
1871 * @param key host identity
1872 * @param value the 'struct GSF_ConnectedPeer' to flush
1873 * @return GNUNET_OK to continue iteration
1876 flush_trust (void *cls,
1877 const GNUNET_HashCode *key,
1880 struct GSF_ConnectedPeer *cp = value;
1883 struct GNUNET_PeerIdentity pid;
1885 if (cp->ppd.trust == cp->disk_trust)
1886 return GNUNET_OK; /* unchanged */
1887 GNUNET_assert (0 != cp->ppd.pid);
1888 GNUNET_PEER_resolve (cp->ppd.pid,
1890 fn = get_trust_filename (&pid);
1891 if (cp->ppd.trust == 0)
1893 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1894 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1895 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1899 trust = htonl (cp->ppd.trust);
1900 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1902 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1903 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1904 cp->disk_trust = cp->ppd.trust;
1912 * Notify core about a preference we have for the given peer
1913 * (to allocate more resources towards it). The change will
1914 * be communicated the next time we reserve bandwidth with
1915 * core (not instantly).
1917 * @param cp peer to reserve bandwidth from
1918 * @param pref preference change
1921 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1924 cp->inc_preference += pref;
1929 * Call this method periodically to flush trust information to disk.
1931 * @param cls closure, not used
1932 * @param tc task context, not used
1935 cron_flush_trust (void *cls,
1936 const struct GNUNET_SCHEDULER_TaskContext *tc)
1941 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1946 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1948 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ,
1955 * Initialize peer management subsystem.
1958 GSF_connected_peer_init_ ()
1960 cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1961 GNUNET_assert (GNUNET_OK ==
1962 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg,
1966 GNUNET_DISK_directory_create (trustDirectory);
1967 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1968 &cron_flush_trust, NULL);
1973 * Iterator to free peer entries.
1975 * @param cls closure, unused
1976 * @param key current key code
1977 * @param value value in the hash map (peer entry)
1978 * @return GNUNET_YES (we should continue to iterate)
1981 clean_peer (void *cls,
1982 const GNUNET_HashCode * key,
1985 GSF_peer_disconnect_handler_ (NULL,
1986 (const struct GNUNET_PeerIdentity*) key);
1992 * Shutdown peer management subsystem.
1995 GSF_connected_peer_done_ ()
1997 cron_flush_trust (NULL, NULL);
1998 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
2001 GNUNET_CONTAINER_multihashmap_destroy (cp_map);
2003 GNUNET_free (trustDirectory);
2004 trustDirectory = NULL;
2009 * Iterator to remove references to LC entry.
2011 * @param cls the 'struct GSF_LocalClient*' to look for
2012 * @param key current key code
2013 * @param value value in the hash map (peer entry)
2014 * @return GNUNET_YES (we should continue to iterate)
2017 clean_local_client (void *cls,
2018 const GNUNET_HashCode * key,
2021 const struct GSF_LocalClient *lc = cls;
2022 struct GSF_ConnectedPeer *cp = value;
2025 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2026 if (cp->ppd.last_client_replies[i] == lc)
2027 cp->ppd.last_client_replies[i] = NULL;
2033 * Notification that a local client disconnected. Clean up all of our
2034 * references to the given handle.
2036 * @param lc handle to the local client (henceforth invalid)
2039 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
2042 return; /* already cleaned up */
2043 GNUNET_CONTAINER_multihashmap_iterate (cp_map,
2044 &clean_local_client,
2049 /* end of gnunet-service-fs_cp.c */