checks
[oweals/gnunet.git] / src / fs / gnunet-service-fs_cp.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file fs/gnunet-service-fs_cp.c
23  * @brief API to handle 'connected peers'
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_load_lib.h"
28 #include "gnunet-service-fs.h"
29 #include "gnunet-service-fs_cp.h"
30 #include "gnunet-service-fs_pe.h"
31 #include "gnunet-service-fs_pr.h"
32 #include "gnunet-service-fs_push.h"
33
34 /**
35  * How often do we flush trust values to disk?
36  */
37 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
38
39 /**
40  * After how long do we discard a reply?
41  */
42 #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
43
44
45 /**
46  * Handle to cancel a transmission request.
47  */
48 struct GSF_PeerTransmitHandle
49 {
50
51   /**
52    * Kept in a doubly-linked list.
53    */
54   struct GSF_PeerTransmitHandle *next;
55
56   /**
57    * Kept in a doubly-linked list.
58    */
59   struct GSF_PeerTransmitHandle *prev;
60
61   /**
62    * Handle for an active request for transmission to this
63    * peer, or NULL (if core queue was full).
64    */
65   struct GNUNET_CORE_TransmitHandle *cth;
66
67   /**
68    * Time when this transmission request was issued.
69    */
70   struct GNUNET_TIME_Absolute transmission_request_start_time;
71
72   /**
73    * Timeout for this request.
74    */
75   struct GNUNET_TIME_Absolute timeout;
76
77   /**
78    * Task called on timeout, or 0 for none.
79    */
80   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
81
82   /**
83    * Function to call to get the actual message.
84    */
85   GSF_GetMessageCallback gmc;
86
87   /**
88    * Peer this request targets.
89    */
90   struct GSF_ConnectedPeer *cp;
91
92   /**
93    * Closure for 'gmc'.
94    */
95   void *gmc_cls;
96
97   /**
98    * Size of the message to be transmitted.
99    */
100   size_t size;
101
102   /**
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
106    * handle right now).
107    */
108   unsigned int cth_in_progress;
109
110   /**
111    * GNUNET_YES if this is a query, GNUNET_NO for content.
112    */
113   int is_query;
114
115   /**
116    * Did we get a reservation already?
117    */
118   int was_reserved;
119
120   /**
121    * Priority of this request.
122    */
123   uint32_t priority;
124
125 };
126
127
128 /**
129  * Handle for an entry in our delay list.
130  */
131 struct GSF_DelayedHandle
132 {
133
134   /**
135    * Kept in a doubly-linked list.
136    */
137   struct GSF_DelayedHandle *next;  
138
139   /**
140    * Kept in a doubly-linked list.
141    */
142   struct GSF_DelayedHandle *prev;
143
144   /**
145    * Peer this transmission belongs to.
146    */
147   struct GSF_ConnectedPeer *cp;
148
149   /**
150    * The PUT that was delayed.
151    */
152   struct PutMessage *pm;
153
154   /**
155    * Task for the delay.
156    */
157   GNUNET_SCHEDULER_TaskIdentifier delay_task;
158
159   /**
160    * Size of the message.
161    */
162   size_t msize;
163   
164 };
165
166
167 /**
168  * Information per peer and request.
169  */
170 struct PeerRequest
171 {
172
173   /**
174    * Handle to generic request.
175    */
176   struct GSF_PendingRequest *pr;
177   
178   /**
179    * Handle to specific peer.
180    */
181   struct GSF_ConnectedPeer *cp;
182
183   /**
184    * Task for asynchronous stopping of this request.
185    */
186   GNUNET_SCHEDULER_TaskIdentifier kill_task;
187
188 };
189
190
191 /**
192  * A connected peer.
193  */
194 struct GSF_ConnectedPeer 
195 {
196
197   /**
198    * Performance data for this peer.
199    */
200   struct GSF_PeerPerformanceData ppd;
201
202   /**
203    * Time until when we blocked this peer from migrating
204    * data to us.
205    */
206   struct GNUNET_TIME_Absolute last_migration_block;
207
208   /**
209    * Task scheduled to revive migration to this peer.
210    */
211   GNUNET_SCHEDULER_TaskIdentifier mig_revive_task;
212
213   /**
214    * Messages (replies, queries, content migration) we would like to
215    * send to this peer in the near future.  Sorted by priority, head.
216    */
217   struct GSF_PeerTransmitHandle *pth_head;
218
219   /**
220    * Messages (replies, queries, content migration) we would like to
221    * send to this peer in the near future.  Sorted by priority, tail.
222    */
223   struct GSF_PeerTransmitHandle *pth_tail;
224
225   /**
226    * Messages (replies, queries, content migration) we would like to
227    * send to this peer in the near future.  Sorted by priority, head.
228    */
229   struct GSF_DelayedHandle *delayed_head;
230
231   /**
232    * Messages (replies, queries, content migration) we would like to
233    * send to this peer in the near future.  Sorted by priority, tail.
234    */
235   struct GSF_DelayedHandle *delayed_tail;
236
237   /**
238    * Migration stop message in our queue, or NULL if we have none pending.
239    */
240   struct GSF_PeerTransmitHandle *migration_pth;
241
242   /**
243    * Context of our GNUNET_CORE_peer_change_preference call (or NULL).
244    */
245   struct GNUNET_CORE_InformationRequestContext *irc;
246
247   /**
248    * Task scheduled if we need to retry bandwidth reservation later.
249    */
250   GNUNET_SCHEDULER_TaskIdentifier irc_delay_task;
251
252   /**
253    * Active requests from this neighbour, map of query to 'struct PeerRequest'.
254    */
255   struct GNUNET_CONTAINER_MultiHashMap *request_map;
256
257   /**
258    * Increase in traffic preference still to be submitted
259    * to the core service for this peer.
260    */
261   uint64_t inc_preference;
262
263   /**
264    * Trust rating for this peer on disk.
265    */
266   uint32_t disk_trust;
267
268   /**
269    * Which offset in "last_p2p_replies" will be updated next?
270    * (we go round-robin).
271    */
272   unsigned int last_p2p_replies_woff;
273
274   /**
275    * Which offset in "last_client_replies" will be updated next?
276    * (we go round-robin).
277    */
278   unsigned int last_client_replies_woff;
279
280   /**
281    * Current offset into 'last_request_times' ring buffer.
282    */
283   unsigned int last_request_times_off;
284
285   /**
286    * GNUNET_YES if we did successfully reserve 32k bandwidth,
287    * GNUNET_NO if not.
288    */
289   int did_reserve;
290
291 };
292
293
294 /**
295  * Map from peer identities to 'struct GSF_ConnectPeer' entries.
296  */
297 static struct GNUNET_CONTAINER_MultiHashMap *cp_map;
298
299 /**
300  * Where do we store trust information?
301  */
302 static char *trustDirectory;
303
304
305 /**
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
309  */
310 static char *
311 get_trust_filename (const struct GNUNET_PeerIdentity *id)
312 {
313   struct GNUNET_CRYPTO_HashAsciiEncoded fil;
314   char *fn;
315
316   GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
317   GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
318   return fn;
319 }
320
321
322 /**
323  * Find latency information in 'atsi'.
324  *
325  * @param atsi performance data
326  * @return connection latency
327  */
328 static struct GNUNET_TIME_Relative
329 get_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
330 {
331   if (atsi == NULL)
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) )
335     atsi++;
336   if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) 
337     {
338       GNUNET_break (0);
339       /* how can we not have latency data? */
340       return GNUNET_TIME_UNIT_SECONDS;
341     }
342   return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
343                                         ntohl (atsi->value));
344 }
345
346
347 /**
348  * Update the performance information kept for the given peer.
349  *
350  * @param cp peer record to update
351  * @param atsi transport performance data
352  */
353 static void
354 update_atsi (struct GSF_ConnectedPeer *cp,
355              const struct GNUNET_TRANSPORT_ATS_Information *atsi)
356 {
357   struct GNUNET_TIME_Relative latency;
358
359   latency = get_latency (atsi);
360   GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay,
361                                  latency);
362   /* LATER: merge atsi into cp's performance data (if we ever care...) */
363 }
364
365
366 /**
367  * Return the performance data record for the given peer
368  * 
369  * @param cp peer to query
370  * @return performance data record for the peer
371  */
372 struct GSF_PeerPerformanceData *
373 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
374 {
375   return &cp->ppd;
376 }
377
378
379 /**
380  * Core is ready to transmit to a peer, get the message.
381  *
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
386  */
387 static size_t
388 peer_transmit_ready_cb (void *cls,
389                         size_t size,
390                         void *buf);
391
392
393
394
395 /**
396  * Function called by core upon success or failure of our bandwidth reservation request.
397  *
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
406  */
407 static void
408 core_reserve_callback (void *cls,
409                        const struct GNUNET_PeerIdentity *peer,
410                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
411                        int32_t amount,
412                        struct GNUNET_TIME_Relative res_delay,
413                        uint64_t preference);
414
415
416 /**
417  * If ready (bandwidth reserved), try to schedule transmission via
418  * core for the given handle.
419  *
420  * @param pth transmission handle to schedule
421  */
422 static void
423 schedule_transmission (struct GSF_PeerTransmitHandle *pth)
424 {
425   struct GSF_ConnectedPeer *cp;
426   struct GNUNET_PeerIdentity target;
427   uint64_t ip;
428
429   if ( (NULL != pth->cth) ||
430        (0 != pth->cth_in_progress) )
431     return; /* already done */
432   cp = pth->cp;
433   GNUNET_assert (0 != cp->ppd.pid);
434   GNUNET_PEER_resolve (cp->ppd.pid,
435                        &target);
436   if ( (GNUNET_YES == pth->is_query) &&
437        (GNUNET_YES != pth->was_reserved) )
438     {
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,
448                                                     &target,
449                                                     GNUNET_TIME_UNIT_FOREVER_REL,
450                                                     GNUNET_BANDWIDTH_VALUE_MAX,
451                                                     DBLOCK_SIZE,
452                                                     ip,
453                                                     &core_reserve_callback,
454                                                     cp);          
455     }
456   GNUNET_assert (pth->cth == NULL);
457   pth->cth_in_progress++;
458   pth->cth = GNUNET_CORE_notify_transmit_ready (GSF_core,
459                                                 GNUNET_YES,
460                                                 pth->priority,
461                                                 GNUNET_TIME_absolute_get_remaining (pth->timeout),
462                                                 &target,
463                                                 pth->size,
464                                                 &peer_transmit_ready_cb,
465                                                 pth);
466   GNUNET_assert (0 < pth->cth_in_progress--);
467 }
468
469
470 /**
471  * Core is ready to transmit to a peer, get the message.
472  *
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
477  */
478 static size_t
479 peer_transmit_ready_cb (void *cls,
480                         size_t size,
481                         void *buf)
482 {
483   struct GSF_PeerTransmitHandle *pth = cls;
484   struct GSF_PeerTransmitHandle *pos;
485   struct GSF_ConnectedPeer *cp;
486   size_t ret;
487
488   GNUNET_assert ( (NULL == buf) ||
489                   (pth->size <= size) );
490   pth->cth = NULL;
491   if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
492     {
493       GNUNET_SCHEDULER_cancel (pth->timeout_task);
494       pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
495     }
496   cp = pth->cp;
497   GNUNET_CONTAINER_DLL_remove (cp->pth_head,
498                                cp->pth_tail,
499                                pth);
500   if (GNUNET_YES == pth->is_query)
501     {
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--);    
504     }
505   else if (GNUNET_NO == pth->is_query)
506     {
507       GNUNET_assert (0 < cp->ppd.pending_replies--);
508     }
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, 
512                   size, buf);
513   GNUNET_assert (NULL == pth->cth);
514   for (pos = cp->pth_head; pos != NULL; pos = pos->next)
515     {
516       GNUNET_assert (pos != pth);
517       schedule_transmission (pos);
518     }
519   GNUNET_assert (pth->cth == NULL);
520   GNUNET_assert (pth->cth_in_progress == 0);
521   GNUNET_free (pth);
522   return ret;
523 }
524
525
526 /**
527  * (re)try to reserve bandwidth from the given peer.
528  *
529  * @param cls the 'struct GSF_ConnectedPeer' to reserve from
530  * @param tc scheduler context
531  */
532 static void
533 retry_reservation (void *cls,
534                    const struct GNUNET_SCHEDULER_TaskContext *tc)
535 {
536   struct GSF_ConnectedPeer *cp = cls;
537   uint64_t ip;
538   struct GNUNET_PeerIdentity target;
539
540   GNUNET_PEER_resolve (cp->ppd.pid,
541                        &target);
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,
546                                                 &target,
547                                                 GNUNET_TIME_UNIT_FOREVER_REL,
548                                                 GNUNET_BANDWIDTH_VALUE_MAX,
549                                                 DBLOCK_SIZE,
550                                                 ip,
551                                                 &core_reserve_callback,
552                                                 cp);
553 }
554
555
556 /**
557  * Function called by core upon success or failure of our bandwidth reservation request.
558  *
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
567  */
568 static void
569 core_reserve_callback (void *cls,
570                        const struct GNUNET_PeerIdentity *peer,
571                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
572                        int32_t amount,
573                        struct GNUNET_TIME_Relative res_delay,
574                        uint64_t preference)
575 {
576   struct GSF_ConnectedPeer *cp = cls;
577   struct GSF_PeerTransmitHandle *pth;
578
579   cp->irc = NULL;
580   if (0 == amount)
581     {
582       cp->irc_delay_task = GNUNET_SCHEDULER_add_delayed (res_delay,
583                                                          &retry_reservation,
584                                                          cp);
585       return;
586     }
587   cp->did_reserve = GNUNET_YES;
588   pth = cp->pth_head;
589   if ( (NULL != pth) &&
590        (NULL == pth->cth) )
591     {
592       /* reservation success, try transmission now! */
593       pth->cth_in_progress++;
594       pth->cth = GNUNET_CORE_notify_transmit_ready (GSF_core,
595                                                     GNUNET_YES,
596                                                     pth->priority,
597                                                     GNUNET_TIME_absolute_get_remaining (pth->timeout),
598                                                     peer,
599                                                     pth->size,
600                                                     &peer_transmit_ready_cb,
601                                                     pth);
602       GNUNET_assert (0 < pth->cth_in_progress--);
603     }
604 }
605
606
607 /**
608  * A peer connected to us.  Setup the connected peer
609  * records.
610  *
611  * @param peer identity of peer that connected
612  * @param atsi performance data for the connection
613  * @return handle to connected peer entry
614  */
615 struct GSF_ConnectedPeer *
616 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
617                            const struct GNUNET_TRANSPORT_ATS_Information *atsi)
618 {
619   struct GSF_ConnectedPeer *cp;
620   char *fn;
621   uint32_t trust;
622
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,
627                                                 peer,
628                                                 GNUNET_TIME_UNIT_FOREVER_REL,
629                                                 GNUNET_BANDWIDTH_VALUE_MAX,
630                                                 DBLOCK_SIZE,
631                                                 0,
632                                                 &core_reserve_callback,
633                                                 cp);
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);
638   GNUNET_free (fn);
639   cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
640   GNUNET_break (GNUNET_OK ==
641                 GNUNET_CONTAINER_multihashmap_put (cp_map,
642                                                    &peer->hashPubKey,
643                                                    cp,
644                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
645   update_atsi (cp, atsi);
646   GSF_push_start_ (cp);
647   return cp;
648 }
649
650
651 /**
652  * It may be time to re-start migrating content to this
653  * peer.  Check, and if so, restart migration.
654  *
655  * @param cls the 'struct GSF_ConnectedPeer'
656  * @param tc scheduler context
657  */
658 static void
659 revive_migration (void *cls,
660                   const struct GNUNET_SCHEDULER_TaskContext *tc)
661 {
662   struct GSF_ConnectedPeer *cp = cls;
663   struct GNUNET_TIME_Relative bt;
664   
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)
668     {
669       /* still time left... */
670       cp->mig_revive_task 
671         = GNUNET_SCHEDULER_add_delayed (bt,
672                                         &revive_migration,
673                                         cp);
674       return;
675     }
676   GSF_push_start_ (cp);
677 }
678
679
680 /**
681  * Get a handle for a connected peer.
682  *
683  * @param peer peer's identity
684  * @return NULL if the peer is not currently connected
685  */
686 struct GSF_ConnectedPeer *
687 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
688 {
689   return GNUNET_CONTAINER_multihashmap_get (cp_map,
690                                             &peer->hashPubKey);
691 }
692
693
694 /**
695  * Handle P2P "MIGRATION_STOP" message.
696  *
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)
704  */
705 int
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)
710 {
711   struct GSF_ConnectedPeer *cp; 
712   const struct MigrationStopMessage *msm;
713   struct GNUNET_TIME_Relative bt;
714
715   msm = (const struct MigrationStopMessage*) message;
716   cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
717                                           &other->hashPubKey);
718   if (cp == NULL)
719     {
720       GNUNET_break (0);
721       return GNUNET_OK;
722     }
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"),
726               GNUNET_i2s (other),
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)
730     {
731       GSF_push_stop_ (cp);
732       cp->mig_revive_task 
733         = GNUNET_SCHEDULER_add_delayed (bt,
734                                         &revive_migration,
735                                         cp);
736     }
737   update_atsi (cp, atsi);
738   return GNUNET_OK;
739 }
740
741
742 /**
743  * Copy reply and free put message.
744  *
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)
749  */
750 static size_t 
751 copy_reply (void *cls,
752             size_t buf_size,
753             void *buf)
754 {
755   struct PutMessage *pm = cls;
756   size_t size;
757
758   if (buf != NULL)
759     {
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"),
765                                 1,
766                                 GNUNET_NO); 
767     }
768   else
769     {
770       size = 0;
771       GNUNET_STATISTICS_update (GSF_stats,
772                                 gettext_noop ("# replies dropped"),
773                                 1,
774                                 GNUNET_NO); 
775     }
776   GNUNET_free (pm);
777   return size;
778 }
779
780
781 /**
782  * Cancel all requests associated with the peer.
783  *
784  * @param cls unused
785  * @param query hash code of the request
786  * @param value the 'struct GSF_PendingRequest'
787  * @return GNUNET_YES (continue to iterate)
788  */
789 static int
790 cancel_pending_request (void *cls,
791                         const GNUNET_HashCode *query,
792                         void *value)
793 {
794   struct PeerRequest *peerreq = value;
795   struct GSF_PendingRequest *pr = peerreq->pr;
796   struct GSF_ConnectedPeer *cp = peerreq->cp;
797   struct GSF_PendingRequestData *prd;
798
799   if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
800     {
801       GNUNET_SCHEDULER_cancel (peerreq->kill_task);
802       peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
803     }
804   GNUNET_STATISTICS_update (GSF_stats,
805                             gettext_noop ("# P2P searches active"),
806                             -1,
807                             GNUNET_NO);
808   prd = GSF_pending_request_get_data_ (pr);
809   GNUNET_break (GNUNET_OK ==
810                 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
811                                                       &prd->query,
812                                                       peerreq));
813   GSF_pending_request_cancel_ (pr);
814   GNUNET_free (peerreq);
815   return GNUNET_OK;
816 }
817
818
819 /**
820  * Free the given request.
821  *
822  * @param cls the request to free
823  * @param tc task context
824  */ 
825 static void
826 peer_request_destroy (void *cls,
827                       const struct GNUNET_SCHEDULER_TaskContext *tc)
828 {
829   struct PeerRequest *peerreq = cls;
830   struct GSF_PendingRequest *pr = peerreq->pr;
831   struct GSF_PendingRequestData *prd;
832
833   peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
834   prd = GSF_pending_request_get_data_ (pr);
835   cancel_pending_request (NULL,
836                           &prd->query,
837                           peerreq);
838 }
839
840
841 /**
842  * The artificial delay is over, transmit the message now.
843  *
844  * @param cls the 'struct GSF_DelayedHandle' with the message
845  * @param tc scheduler context
846  */
847 static void
848 transmit_delayed_now (void *cls,
849                       const struct GNUNET_SCHEDULER_TaskContext *tc)
850 {
851   struct GSF_DelayedHandle *dh = cls;
852   struct GSF_ConnectedPeer *cp = dh->cp;
853
854   GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
855                                cp->delayed_tail,
856                                dh);
857   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
858     {
859       GNUNET_free (dh->pm);
860       GNUNET_free (dh);
861       return;
862     }
863   (void) GSF_peer_transmit_ (cp, GNUNET_NO,
864                              UINT32_MAX,
865                              REPLY_TIMEOUT,
866                              dh->msize,
867                              &copy_reply,
868                              dh->pm);
869   GNUNET_free (dh);
870 }
871
872
873 /**
874  * Get the randomized delay a response should be subjected to.
875  * 
876  * @return desired delay
877  */
878 static struct GNUNET_TIME_Relative
879 get_randomized_delay ()
880 {
881   struct GNUNET_TIME_Relative ret;
882
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,
886                                                                  5000));
887   GNUNET_STATISTICS_update (GSF_stats,
888                             gettext_noop ("# artificial delays introduced (ms)"),
889                             ret.rel_value,
890                             GNUNET_NO);
891
892   return ret;
893 }
894
895
896 /**
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
902  * expiration.
903  *
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
912  */
913 static void
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,
920                   const void *data,
921                   size_t data_len)
922 {
923   struct PeerRequest *peerreq = cls;
924   struct GSF_ConnectedPeer *cp = peerreq->cp;
925   struct GSF_PendingRequestData *prd;
926   struct PutMessage *pm;
927   size_t msize;
928
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);
932   if (NULL == data)
933     {
934       GNUNET_STATISTICS_update (GSF_stats,
935                                 gettext_noop ("# P2P searches active"),
936                                 -1,
937                                 GNUNET_NO);
938       GNUNET_break (GNUNET_OK ==
939                     GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
940                                                           &prd->query,
941                                                           peerreq));
942       GNUNET_free (peerreq);
943       return;
944     }  
945   GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
946   if ( (prd->type != type) &&
947        (prd->type != GNUNET_BLOCK_TYPE_ANY) )
948     {
949       GNUNET_break (0);
950       return;
951     }
952 #if DEBUG_FS
953   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954               "Transmitting result for query `%s' to peer\n",
955               GNUNET_h2s (&prd->query));
956 #endif  
957   GNUNET_STATISTICS_update (GSF_stats,
958                             gettext_noop ("# replies received for other peers"),
959                             1,
960                             GNUNET_NO); 
961   msize = sizeof (struct PutMessage) + data_len;
962   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
963     {
964       GNUNET_break (0);
965       return;
966     }
967   if ( (reply_anonymity_level != UINT32_MAX) &&
968        (reply_anonymity_level > 1) )
969     {
970       if (reply_anonymity_level - 1 > GSF_cover_content_count) 
971         {
972           GNUNET_STATISTICS_update (GSF_stats,
973                                     gettext_noop ("# replies dropped due to insufficient cover traffic"),
974                                     1,
975                                     GNUNET_NO); 
976           return;
977         }
978       GSF_cover_content_count -= (reply_anonymity_level - 1);
979     }
980     
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) )
990     {
991       struct GSF_DelayedHandle *dh;
992
993       dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
994       dh->cp = cp;
995       dh->pm = pm;
996       dh->msize = msize;
997       GNUNET_CONTAINER_DLL_insert (cp->delayed_head,
998                                    cp->delayed_tail,
999                                    dh);
1000       dh->delay_task = GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
1001                                                      &transmit_delayed_now,
1002                                                      dh);
1003     }
1004   else
1005     {
1006       (void) GSF_peer_transmit_ (cp, GNUNET_NO,
1007                                  UINT32_MAX,
1008                                  REPLY_TIMEOUT,
1009                                  msize,
1010                                  &copy_reply,
1011                                  pm);
1012     }
1013   if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1014     return;
1015   if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
1016     {
1017       GNUNET_STATISTICS_update (GSF_stats,
1018                                 gettext_noop ("# P2P searches destroyed due to ultimate reply"),
1019                                 1,
1020                                 GNUNET_NO);
1021      peerreq->kill_task = GNUNET_SCHEDULER_add_now (&peer_request_destroy,
1022                                                      peerreq);
1023     }
1024 }
1025
1026
1027 /**
1028  * Increase the host credit by a value.
1029  *
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)
1034  */
1035 static int
1036 change_host_trust (struct GSF_ConnectedPeer *cp, int value)
1037 {
1038   if (value == 0)
1039     return 0;
1040   GNUNET_assert (cp != NULL);
1041   if (value > 0)
1042     {
1043       if (cp->ppd.trust + value < cp->ppd.trust)
1044         {
1045           value = UINT32_MAX - cp->ppd.trust;
1046           cp->ppd.trust = UINT32_MAX;
1047         }
1048       else
1049         cp->ppd.trust += value;
1050     }
1051   else
1052     {
1053       if (cp->ppd.trust < -value)
1054         {
1055           value = -cp->ppd.trust;
1056           cp->ppd.trust = 0;
1057         }
1058       else
1059         cp->ppd.trust += value;
1060     }
1061   return value;
1062 }
1063
1064
1065 /**
1066  * We've received a request with the specified priority.  Bound it
1067  * according to how much we trust the given peer.
1068  * 
1069  * @param prio_in requested priority
1070  * @param cp the peer making the request
1071  * @return effective priority
1072  */
1073 static int32_t
1074 bound_priority (uint32_t prio_in,
1075                 struct GSF_ConnectedPeer *cp)
1076 {
1077 #define N ((double)128.0)
1078   uint32_t ret;
1079   double rret;
1080   int ld;
1081
1082   ld = GSF_test_get_load_too_high_ (0);
1083   if (ld == GNUNET_SYSERR)
1084     {
1085       GNUNET_STATISTICS_update (GSF_stats,
1086                                 gettext_noop ("# requests done for free (low load)"),
1087                                 1,
1088                                 GNUNET_NO);
1089       return 0; /* excess resources */
1090     }
1091   if (prio_in > INT32_MAX)
1092     prio_in = INT32_MAX;
1093   ret = - change_host_trust (cp, - (int) prio_in);
1094   if (ret > 0)
1095     {
1096       if (ret > GSF_current_priorities + N)
1097         rret = GSF_current_priorities + N;
1098       else
1099         rret = ret;
1100       GSF_current_priorities 
1101         = (GSF_current_priorities * (N-1) + rret)/N;
1102     }
1103   if ( (ld == GNUNET_YES) && (ret > 0) )
1104     {
1105       /* try with charging */
1106       ld = GSF_test_get_load_too_high_ (ret);
1107     }
1108   if (ld == GNUNET_YES)
1109     {
1110       GNUNET_STATISTICS_update (GSF_stats,
1111                                 gettext_noop ("# request dropped, priority insufficient"),
1112                                 1,
1113                                 GNUNET_NO);
1114       /* undo charge */
1115       change_host_trust (cp, (int) ret);
1116       return -1; /* not enough resources */
1117     }
1118   else
1119     {
1120       GNUNET_STATISTICS_update (GSF_stats,
1121                                 gettext_noop ("# requests done for a price (normal load)"),
1122                                 1,
1123                                 GNUNET_NO);
1124     }
1125 #undef N
1126   return ret;
1127 }
1128
1129
1130 /**
1131  * The priority level imposes a bound on the maximum
1132  * value for the ttl that can be requested.
1133  *
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
1138  */
1139 static int32_t
1140 bound_ttl (int32_t ttl_in, uint32_t prio)
1141 {
1142   unsigned long long allowed;
1143
1144   if (ttl_in <= 0)
1145     return ttl_in;
1146   allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000; 
1147   if (ttl_in > allowed)      
1148     {
1149       if (allowed >= (1 << 30))
1150         return 1 << 30;
1151       return allowed;
1152     }
1153   return ttl_in;
1154 }
1155
1156
1157 /**
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.
1162  *
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
1167  */
1168 struct GSF_PendingRequest *
1169 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1170                        const struct GNUNET_MessageHeader *message)
1171 {
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;                            
1180   uint16_t msize;
1181   const struct GetMessage *gm;
1182   unsigned int bits;
1183   const GNUNET_HashCode *opt;
1184   uint32_t bm;
1185   size_t bfsize;
1186   uint32_t ttl_decrement;
1187   int32_t priority;
1188   int32_t ttl;
1189   enum GNUNET_BLOCK_Type type;
1190   GNUNET_PEER_Id spid;
1191
1192   GNUNET_assert (other != NULL);
1193   msize = ntohs(message->size);
1194   if (msize < sizeof (struct GetMessage))
1195     {
1196       GNUNET_break_op (0);
1197       return NULL;
1198     }
1199   GNUNET_STATISTICS_update (GSF_stats,
1200                             gettext_noop ("# GET requests received (from other peers)"),
1201                             1,
1202                             GNUNET_NO);
1203   gm = (const struct GetMessage*) message;
1204   type = ntohl (gm->type);
1205   bm = ntohl (gm->hash_bitmap);
1206   bits = 0;
1207   while (bm > 0)
1208     {
1209       if (1 == (bm & 1))
1210         bits++;
1211       bm >>= 1;
1212     }
1213   if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
1214     {
1215       GNUNET_break_op (0);
1216       return NULL;
1217     }  
1218   opt = (const GNUNET_HashCode*) &gm[1];
1219   bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
1220   /* bfsize must be power of 2, check! */
1221   if (0 != ( (bfsize - 1) & bfsize))
1222     {
1223       GNUNET_break_op (0);
1224       return NULL;
1225     }
1226   GSF_cover_query_count++;
1227   bm = ntohl (gm->hash_bitmap);
1228   bits = 0;
1229   cps = GNUNET_CONTAINER_multihashmap_get (cp_map,
1230                                            &other->hashPubKey);
1231   if (NULL == cps)
1232     {
1233       /* peer must have just disconnected */
1234       GNUNET_STATISTICS_update (GSF_stats,
1235                                 gettext_noop ("# requests dropped due to initiator not being connected"),
1236                                 1,
1237                                 GNUNET_NO);
1238       return NULL;
1239     }
1240   if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1241     cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1242                                             &opt[bits++]);
1243   else
1244     cp = cps;
1245   if (cp == NULL)
1246     {
1247 #if DEBUG_FS
1248       if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1249         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250                     "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1251                     GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
1252       
1253       else
1254         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255                     "Failed to find peer `%4s' in connection set. Dropping query.\n",
1256                     GNUNET_i2s (other));
1257 #endif
1258       GNUNET_STATISTICS_update (GSF_stats,
1259                                 gettext_noop ("# requests dropped due to missing reverse route"),
1260                                 1,
1261                                 GNUNET_NO);
1262       return NULL;
1263     }
1264   /* note that we can really only check load here since otherwise
1265      peers could find out that we are overloaded by not being
1266      disconnected after sending us a malformed query... */
1267   priority = bound_priority (ntohl (gm->priority), cps);
1268   if (priority < 0)
1269     {
1270 #if DEBUG_FS
1271       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1272                   "Dropping query from `%s', this peer is too busy.\n",
1273                   GNUNET_i2s (other));
1274 #endif
1275       return NULL;
1276     }
1277 #if DEBUG_FS 
1278   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279               "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1280               GNUNET_h2s (&gm->query),
1281               (unsigned int) type,
1282               GNUNET_i2s (other),
1283               (unsigned int) bm);
1284 #endif
1285   namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1286   if ( (type == GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
1287        (namespace == NULL) )
1288     {
1289       GNUNET_break_op (0);
1290       return NULL;
1291     }
1292   if ( (type != GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
1293        (namespace != NULL) )
1294     {
1295       GNUNET_break_op (0);
1296       return NULL;
1297     }
1298   target = (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity*) &opt[bits++]) : NULL;
1299   options = 0;
1300   spid = 0;
1301   if ( (GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority)) ||
1302        (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) > 
1303         GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 + GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)) ) 
1304     {
1305       /* don't have BW to send to peer, or would likely take longer than we have for it,
1306          so at best indirect the query */
1307       priority = 0;
1308       options |= GSF_PRO_FORWARD_ONLY;
1309       spid = GNUNET_PEER_intern (other);
1310       GNUNET_assert (0 != spid);
1311     }
1312   ttl = bound_ttl (ntohl (gm->ttl), priority);
1313   /* decrement ttl (always) */
1314   ttl_decrement = 2 * TTL_DECREMENT +
1315     GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1316                               TTL_DECREMENT);
1317   if ( (ttl < 0) &&
1318        (((int32_t)(ttl - ttl_decrement)) > 0) )
1319     {
1320 #if DEBUG_FS
1321       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322                   "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1323                   GNUNET_i2s (other),
1324                   ttl,
1325                   ttl_decrement);
1326 #endif
1327       GNUNET_STATISTICS_update (GSF_stats,
1328                                 gettext_noop ("# requests dropped due TTL underflow"),
1329                                 1,
1330                                 GNUNET_NO);
1331       /* integer underflow => drop (should be very rare)! */      
1332       return NULL;
1333     } 
1334   ttl -= ttl_decrement;
1335
1336   /* test if the request already exists */
1337   peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map,
1338                                                &gm->query);
1339   if (peerreq != NULL) 
1340     {      
1341       pr = peerreq->pr;
1342       prd = GSF_pending_request_get_data_ (pr);
1343       if ( (prd->type == type) &&
1344            ( (type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1345              (0 == memcmp (&prd->namespace,
1346                            namespace,
1347                            sizeof (GNUNET_HashCode))) ) )
1348         {
1349           if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get().abs_value + ttl)
1350             {
1351               /* existing request has higher TTL, drop new one! */
1352               prd->priority += priority;
1353 #if DEBUG_FS
1354               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355                           "Have existing request with higher TTL, dropping new request.\n",
1356                           GNUNET_i2s (other));
1357 #endif
1358               GNUNET_STATISTICS_update (GSF_stats,
1359                                         gettext_noop ("# requests dropped due to higher-TTL request"),
1360                                         1,
1361                                         GNUNET_NO);
1362               return NULL;
1363             }
1364           /* existing request has lower TTL, drop old one! */
1365           GNUNET_STATISTICS_update (GSF_stats,
1366                                     gettext_noop ("# P2P searches active"),
1367                                     -1,
1368                                     GNUNET_NO);
1369           priority += prd->priority;
1370           GSF_pending_request_cancel_ (pr);
1371           GNUNET_assert (GNUNET_YES ==
1372                          GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
1373                                                                &gm->query,
1374                                                                peerreq));
1375           if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
1376             {
1377               GNUNET_SCHEDULER_cancel (peerreq->kill_task);
1378               peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
1379             }
1380           GNUNET_free (peerreq);
1381         }
1382     }
1383   
1384   peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1385   peerreq->cp = cp; 
1386   pr = GSF_pending_request_create_ (options,
1387                                     type,
1388                                     &gm->query,
1389                                     namespace,
1390                                     target,
1391                                     (bfsize > 0) ? (const char*)&opt[bits] : NULL,
1392                                     bfsize,
1393                                     ntohl (gm->filter_mutator),
1394                                     1 /* anonymity */,
1395                                     (uint32_t) priority,
1396                                     ttl,
1397                                     spid,
1398                                     NULL, 0, /* replies_seen */
1399                                     &handle_p2p_reply,
1400                                     peerreq);
1401   GNUNET_assert (NULL != pr);
1402   peerreq->pr = pr;
1403   GNUNET_break (GNUNET_OK ==
1404                 GNUNET_CONTAINER_multihashmap_put (cp->request_map,
1405                                                    &gm->query,
1406                                                    peerreq,
1407                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1408   GNUNET_STATISTICS_update (GSF_stats,
1409                             gettext_noop ("# P2P query messages received and processed"),
1410                             1,
1411                             GNUNET_NO);
1412   GNUNET_STATISTICS_update (GSF_stats,
1413                             gettext_noop ("# P2P searches active"),
1414                             1,
1415                             GNUNET_NO);
1416   return pr;
1417 }
1418
1419
1420 /**
1421  * Function called if there has been a timeout trying to satisfy
1422  * a transmission request.
1423  *
1424  * @param cls the 'struct GSF_PeerTransmitHandle' of the request 
1425  * @param tc scheduler context
1426  */
1427 static void
1428 peer_transmit_timeout (void *cls,
1429                        const struct GNUNET_SCHEDULER_TaskContext *tc)
1430 {
1431   struct GSF_PeerTransmitHandle *pth = cls;
1432   struct GSF_ConnectedPeer *cp;
1433
1434 #if DEBUG_FS
1435   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436               "Timeout trying to transmit to other peer\n");
1437 #endif  
1438   pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1439   cp = pth->cp;
1440   GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1441                                cp->pth_tail,
1442                                pth);
1443   if (GNUNET_YES == pth->is_query)
1444     GNUNET_assert (0 < cp->ppd.pending_queries--);    
1445   else if (GNUNET_NO == pth->is_query)
1446     GNUNET_assert (0 < cp->ppd.pending_replies--);
1447   GNUNET_LOAD_update (cp->ppd.transmission_delay,
1448                       UINT64_MAX);
1449   if (NULL != pth->cth)
1450     {
1451       GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1452       pth->cth = NULL;
1453     }
1454   pth->gmc (pth->gmc_cls, 
1455             0, NULL);
1456   GNUNET_assert (0 == pth->cth_in_progress);
1457   GNUNET_free (pth);
1458 }
1459
1460
1461 /**
1462  * Transmit a message to the given peer as soon as possible.
1463  * If the peer disconnects before the transmission can happen,
1464  * the callback is invoked with a 'NULL' buffer.
1465  *
1466  * @param cp target peer
1467  * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1468  * @param priority how important is this request?
1469  * @param timeout when does this request timeout (call gmc with error)
1470  * @param size number of bytes we would like to send to the peer
1471  * @param gmc function to call to get the message
1472  * @param gmc_cls closure for gmc
1473  * @return handle to cancel request
1474  */
1475 struct GSF_PeerTransmitHandle *
1476 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
1477                     int is_query,
1478                     uint32_t priority,
1479                     struct GNUNET_TIME_Relative timeout,
1480                     size_t size,
1481                     GSF_GetMessageCallback gmc,
1482                     void *gmc_cls)
1483 {
1484   struct GSF_PeerTransmitHandle *pth;
1485   struct GSF_PeerTransmitHandle *pos;
1486   struct GSF_PeerTransmitHandle *prev;
1487
1488   pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1489   pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1490   pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1491   pth->gmc = gmc;
1492   pth->gmc_cls = gmc_cls;
1493   pth->size = size;
1494   pth->is_query = is_query;
1495   pth->priority = priority;
1496   pth->cp = cp;
1497   /* insertion sort (by priority, descending) */
1498   prev = NULL;
1499   pos = cp->pth_head;
1500   while ( (pos != NULL) &&
1501           (pos->priority > priority) )
1502     {
1503       prev = pos;
1504       pos = pos->next;
1505     }
1506   if (prev == NULL)
1507     GNUNET_CONTAINER_DLL_insert (cp->pth_head,
1508                                  cp->pth_tail,
1509                                  pth);
1510   else
1511     GNUNET_CONTAINER_DLL_insert_after (cp->pth_head,
1512                                        cp->pth_tail,
1513                                        prev,
1514                                        pth);
1515   if (GNUNET_YES == is_query)
1516     cp->ppd.pending_queries++;
1517   else if (GNUNET_NO == is_query)
1518     cp->ppd.pending_replies++;
1519   pth->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1520                                                     &peer_transmit_timeout,
1521                                                     pth);
1522   schedule_transmission (pth);
1523   return pth;
1524 }
1525
1526
1527 /**
1528  * Cancel an earlier request for transmission.
1529  *
1530  * @param pth request to cancel
1531  */
1532 void
1533 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1534 {
1535   struct GSF_ConnectedPeer *cp;
1536
1537   if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1538     {
1539       GNUNET_SCHEDULER_cancel (pth->timeout_task);
1540       pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1541     }
1542   if (NULL != pth->cth)
1543     {
1544       GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1545       pth->cth = NULL;
1546     }
1547   cp = pth->cp;
1548   GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1549                                cp->pth_tail,
1550                                pth);
1551   if (GNUNET_YES == pth->is_query)
1552     GNUNET_assert (0 < cp->ppd.pending_queries--);    
1553   else if (GNUNET_NO == pth->is_query)
1554     GNUNET_assert (0 < cp->ppd.pending_replies--);
1555   GNUNET_assert (0 == pth->cth_in_progress);
1556   GNUNET_free (pth);
1557 }
1558
1559
1560 /**
1561  * Report on receiving a reply; update the performance record of the given peer.
1562  *
1563  * @param cp responding peer (will be updated)
1564  * @param request_time time at which the original query was transmitted
1565  * @param request_priority priority of the original request
1566  */
1567 void
1568 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1569                               struct GNUNET_TIME_Absolute request_time,
1570                               uint32_t request_priority)
1571 {
1572   struct GNUNET_TIME_Relative delay;
1573
1574   delay = GNUNET_TIME_absolute_get_duration (request_time);  
1575   cp->ppd.avg_reply_delay.rel_value = (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N-1) + delay.rel_value) / RUNAVG_DELAY_N;
1576   cp->ppd.avg_priority = (cp->ppd.avg_priority * (RUNAVG_DELAY_N-1) + request_priority) / RUNAVG_DELAY_N;
1577 }
1578
1579
1580 /**
1581  * Report on receiving a reply in response to an initiating client.
1582  * Remember that this peer is good for this client.
1583  *
1584  * @param cp responding peer (will be updated)
1585  * @param initiator_client local client on responsible for query
1586  */
1587 void
1588 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1589                                    struct GSF_LocalClient *initiator_client)
1590 {
1591   cp->ppd.last_client_replies[cp->last_client_replies_woff++ % CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1592 }
1593
1594
1595 /**
1596  * Report on receiving a reply in response to an initiating peer.
1597  * Remember that this peer is good for this initiating peer.
1598  *
1599  * @param cp responding peer (will be updated)
1600  * @param initiator_peer other peer responsible for query
1601  */
1602 void
1603 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1604                                  const struct GSF_ConnectedPeer *initiator_peer)
1605 {
1606   GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE], -1);
1607   cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff++ % P2P_SUCCESS_LIST_SIZE] = initiator_peer->ppd.pid;
1608   GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1609 }
1610
1611
1612 /**
1613  * Method called whenever a given peer has a status change.
1614  *
1615  * @param cls closure
1616  * @param peer peer identity this notification is about
1617  * @param bandwidth_in available amount of inbound bandwidth
1618  * @param bandwidth_out available amount of outbound bandwidth
1619  * @param timeout absolute time when this peer will time out
1620  *        unless we see some further activity from it
1621  * @param atsi status information
1622  */
1623 void
1624 GSF_peer_status_handler_ (void *cls,
1625                           const struct GNUNET_PeerIdentity *peer,
1626                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1627                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1628                           struct GNUNET_TIME_Absolute timeout,
1629                           const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1630 {
1631   struct GSF_ConnectedPeer *cp;
1632
1633   cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1634                                           &peer->hashPubKey);
1635   GNUNET_assert (NULL != cp);
1636   update_atsi (cp, atsi);
1637 }
1638
1639
1640 /**
1641  * A peer disconnected from us.  Tear down the connected peer
1642  * record.
1643  *
1644  * @param cls unused
1645  * @param peer identity of peer that connected
1646  */
1647 void
1648 GSF_peer_disconnect_handler_ (void *cls,
1649                               const struct GNUNET_PeerIdentity *peer)
1650 {
1651   struct GSF_ConnectedPeer *cp;
1652   struct GSF_PeerTransmitHandle *pth;
1653   struct GSF_DelayedHandle *dh;
1654
1655   cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1656                                           &peer->hashPubKey);
1657   if (NULL == cp)
1658     return; /* must have been disconnect from core with
1659                'peer' == my_id, ignore */
1660   GNUNET_CONTAINER_multihashmap_remove (cp_map,
1661                                         &peer->hashPubKey,
1662                                         cp);
1663   if (NULL != cp->migration_pth)
1664     {
1665       GSF_peer_transmit_cancel_ (cp->migration_pth);
1666       cp->migration_pth = NULL;
1667     }
1668   if (NULL != cp->irc)
1669     {
1670       GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1671       cp->irc = NULL;
1672     }
1673   if (GNUNET_SCHEDULER_NO_TASK != cp->irc_delay_task)
1674     {
1675       GNUNET_SCHEDULER_cancel (cp->irc_delay_task);
1676       cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1677     }
1678   GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1679                                          &cancel_pending_request,
1680                                          cp);
1681   GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1682   cp->request_map = NULL;
1683   GSF_plan_notify_peer_disconnect_ (cp);
1684   GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1685   GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1686   GSF_push_stop_ (cp);
1687   while (NULL != (pth = cp->pth_head))
1688     {
1689       if (NULL != pth->cth)
1690         {
1691           GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1692           pth->cth = NULL;
1693         }
1694       if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1695         {
1696           GNUNET_SCHEDULER_cancel (pth->timeout_task);
1697           pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1698         }
1699       GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1700                                    cp->pth_tail,
1701                                    pth);
1702       GNUNET_assert (0 == pth->cth_in_progress);
1703       GNUNET_free (pth);
1704     }
1705   while (NULL != (dh = cp->delayed_head))
1706     {
1707       GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
1708                                    cp->delayed_tail,
1709                                    dh);
1710       GNUNET_SCHEDULER_cancel (dh->delay_task);
1711       GNUNET_free (dh->pm);
1712       GNUNET_free (dh);
1713     }
1714   GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1715   if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1716     {
1717       GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1718       cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1719     }
1720   GNUNET_free (cp);
1721 }
1722
1723
1724 /**
1725  * Closure for 'call_iterator'.
1726  */
1727 struct IterationContext
1728 {
1729   /**
1730    * Function to call on each entry.
1731    */
1732   GSF_ConnectedPeerIterator it;
1733
1734   /**
1735    * Closure for 'it'.
1736    */
1737   void *it_cls;
1738 };
1739
1740
1741 /**
1742  * Function that calls the callback for each peer.
1743  *
1744  * @param cls the 'struct IterationContext*'
1745  * @param key identity of the peer
1746  * @param value the 'struct GSF_ConnectedPeer*'
1747  * @return GNUNET_YES to continue iteration
1748  */
1749 static int
1750 call_iterator (void *cls,
1751                const GNUNET_HashCode *key,
1752                void *value)
1753 {
1754   struct IterationContext *ic = cls;
1755   struct GSF_ConnectedPeer *cp = value;
1756   
1757   ic->it (ic->it_cls,
1758           (const struct GNUNET_PeerIdentity*) key,
1759           cp,
1760           &cp->ppd);
1761   return GNUNET_YES;
1762 }
1763
1764
1765 /**
1766  * Iterate over all connected peers.
1767  *
1768  * @param it function to call for each peer
1769  * @param it_cls closure for it
1770  */
1771 void
1772 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1773                               void *it_cls)
1774 {
1775   struct IterationContext ic;
1776
1777   ic.it = it;
1778   ic.it_cls = it_cls;
1779   GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1780                                          &call_iterator,
1781                                          &ic);
1782 }
1783
1784
1785 /**
1786  * Obtain the identity of a connected peer.
1787  *
1788  * @param cp peer to reserve bandwidth from
1789  * @param id identity to set (written to)
1790  */
1791 void
1792 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1793                                   struct GNUNET_PeerIdentity *id)
1794 {
1795   GNUNET_assert (0 != cp->ppd.pid);
1796   GNUNET_PEER_resolve (cp->ppd.pid,
1797                        id);
1798 }
1799
1800
1801 /**
1802  * Assemble a migration stop message for transmission.
1803  *
1804  * @param cls the 'struct GSF_ConnectedPeer' to use
1805  * @param size number of bytes we're allowed to write to buf
1806  * @param buf where to copy the message
1807  * @return number of bytes copied to buf
1808  */
1809 static size_t
1810 create_migration_stop_message (void *cls,
1811                                size_t size,
1812                                void *buf)
1813 {
1814   struct GSF_ConnectedPeer *cp = cls;
1815   struct MigrationStopMessage msm;
1816
1817   cp->migration_pth = NULL;
1818   if (NULL == buf)
1819     return 0;
1820   GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1821   msm.header.size = htons (sizeof (struct MigrationStopMessage));
1822   msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1823   msm.reserved = htonl (0);
1824   msm.duration = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block));
1825   memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1826   return sizeof (struct MigrationStopMessage);
1827 }
1828
1829
1830 /**
1831  * Ask a peer to stop migrating data to us until the given point
1832  * in time.
1833  * 
1834  * @param cp peer to ask
1835  * @param block_time until when to block
1836  */
1837 void
1838 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1839                            struct GNUNET_TIME_Relative block_time)
1840 {
1841   if (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value > block_time.rel_value)
1842     {
1843 #if DEBUG_FS && 0
1844       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845           "Migration already blocked for another %llu ms\n",
1846                   (unsigned long long) GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value);
1847 #endif
1848       return; /* already blocked */
1849     }
1850 #if DEBUG_FS && 0
1851   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1852               "Asking to stop migration for %llu ms\n",
1853               (unsigned long long) block_time.rel_value);
1854 #endif
1855   cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1856   if (cp->migration_pth != NULL)
1857     GSF_peer_transmit_cancel_ (cp->migration_pth);
1858   cp->migration_pth 
1859     = GSF_peer_transmit_ (cp,
1860                           GNUNET_SYSERR,
1861                           UINT32_MAX,
1862                           GNUNET_TIME_UNIT_FOREVER_REL,
1863                           sizeof (struct MigrationStopMessage),
1864                           &create_migration_stop_message,
1865                           cp);
1866 }
1867
1868
1869 /**
1870  * Write host-trust information to a file - flush the buffer entry!
1871  *
1872  * @param cls closure, not used
1873  * @param key host identity
1874  * @param value the 'struct GSF_ConnectedPeer' to flush
1875  * @return GNUNET_OK to continue iteration
1876  */
1877 static int
1878 flush_trust (void *cls,
1879              const GNUNET_HashCode *key,
1880              void *value)
1881 {
1882   struct GSF_ConnectedPeer *cp = value;
1883   char *fn;
1884   uint32_t trust;
1885   struct GNUNET_PeerIdentity pid;
1886
1887   if (cp->ppd.trust == cp->disk_trust)
1888     return GNUNET_OK;                     /* unchanged */
1889   GNUNET_assert (0 != cp->ppd.pid);
1890   GNUNET_PEER_resolve (cp->ppd.pid,
1891                        &pid);
1892   fn = get_trust_filename (&pid);
1893   if (cp->ppd.trust == 0)
1894     {
1895       if ((0 != UNLINK (fn)) && (errno != ENOENT))
1896         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1897                                   GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1898     }
1899   else
1900     {
1901       trust = htonl (cp->ppd.trust);
1902       if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust, 
1903                                                     sizeof(uint32_t),
1904                                                     GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1905                                                     | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1906         cp->disk_trust = cp->ppd.trust;
1907     }
1908   GNUNET_free (fn);
1909   return GNUNET_OK;
1910 }
1911
1912
1913 /**
1914  * Notify core about a preference we have for the given peer
1915  * (to allocate more resources towards it).  The change will
1916  * be communicated the next time we reserve bandwidth with
1917  * core (not instantly).
1918  *
1919  * @param cp peer to reserve bandwidth from
1920  * @param pref preference change
1921  */
1922 void
1923 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1924                                        uint64_t pref)
1925 {
1926   cp->inc_preference += pref;
1927 }
1928
1929
1930 /**
1931  * Call this method periodically to flush trust information to disk.
1932  *
1933  * @param cls closure, not used
1934  * @param tc task context, not used
1935  */
1936 static void
1937 cron_flush_trust (void *cls,
1938                   const struct GNUNET_SCHEDULER_TaskContext *tc)
1939 {
1940
1941   if (NULL == cp_map)
1942     return;
1943   GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1944                                          &flush_trust,
1945                                          NULL);
1946   if (NULL == tc)
1947     return;
1948   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1949     return;
1950   GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, 
1951                                 &cron_flush_trust, 
1952                                 NULL);
1953 }
1954
1955
1956 /**
1957  * Initialize peer management subsystem.
1958  */
1959 void
1960 GSF_connected_peer_init_ ()
1961 {
1962   cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1963   GNUNET_assert (GNUNET_OK ==
1964                  GNUNET_CONFIGURATION_get_value_filename (GSF_cfg,
1965                                                           "fs",
1966                                                           "TRUST",
1967                                                           &trustDirectory));
1968   GNUNET_DISK_directory_create (trustDirectory);
1969   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1970                                       &cron_flush_trust, NULL);
1971 }
1972
1973
1974 /**
1975  * Iterator to free peer entries.
1976  *
1977  * @param cls closure, unused
1978  * @param key current key code
1979  * @param value value in the hash map (peer entry)
1980  * @return GNUNET_YES (we should continue to iterate)
1981  */
1982 static int 
1983 clean_peer (void *cls,
1984             const GNUNET_HashCode * key,
1985             void *value)
1986 {
1987   GSF_peer_disconnect_handler_ (NULL, 
1988                                 (const struct GNUNET_PeerIdentity*) key);
1989   return GNUNET_YES;
1990 }
1991
1992
1993 /**
1994  * Shutdown peer management subsystem.
1995  */
1996 void
1997 GSF_connected_peer_done_ ()
1998 {
1999   cron_flush_trust (NULL, NULL);
2000   GNUNET_CONTAINER_multihashmap_iterate (cp_map,
2001                                          &clean_peer,
2002                                          NULL);
2003   GNUNET_CONTAINER_multihashmap_destroy (cp_map);
2004   cp_map = NULL;
2005   GNUNET_free (trustDirectory);
2006   trustDirectory = NULL;
2007 }
2008
2009
2010 /**
2011  * Iterator to remove references to LC entry.
2012  *
2013  * @param cls the 'struct GSF_LocalClient*' to look for
2014  * @param key current key code
2015  * @param value value in the hash map (peer entry)
2016  * @return GNUNET_YES (we should continue to iterate)
2017  */
2018 static int 
2019 clean_local_client (void *cls,
2020                     const GNUNET_HashCode * key,
2021                     void *value)
2022 {
2023   const struct GSF_LocalClient *lc = cls;
2024   struct GSF_ConnectedPeer *cp = value;
2025   unsigned int i;
2026
2027   for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2028     if (cp->ppd.last_client_replies[i] == lc)
2029       cp->ppd.last_client_replies[i] = NULL;
2030   return GNUNET_YES;
2031 }
2032
2033
2034 /**
2035  * Notification that a local client disconnected.  Clean up all of our
2036  * references to the given handle.
2037  *
2038  * @param lc handle to the local client (henceforth invalid)
2039  */
2040 void
2041 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
2042 {
2043   if (NULL == cp_map)
2044     return; /* already cleaned up */
2045   GNUNET_CONTAINER_multihashmap_iterate (cp_map,
2046                                          &clean_local_client,
2047                                          (void*) lc);
2048 }
2049
2050
2051 /* end of gnunet-service-fs_cp.c */