fix 1707
[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_PEER_resolve (cp->ppd.pid,
434                        &target);
435   if ( (GNUNET_YES == pth->is_query) &&
436        (GNUNET_YES != pth->was_reserved) )
437     {
438       /* query, need reservation */
439       if (GNUNET_YES != cp->did_reserve)
440         return; /* not ready */
441       cp->did_reserve = GNUNET_NO;
442       /* reservation already done! */
443       pth->was_reserved = GNUNET_YES;
444       ip = cp->inc_preference;
445       cp->inc_preference = 0;
446       cp->irc = GNUNET_CORE_peer_change_preference (GSF_core,
447                                                     &target,
448                                                     GNUNET_TIME_UNIT_FOREVER_REL,
449                                                     GNUNET_BANDWIDTH_VALUE_MAX,
450                                                     DBLOCK_SIZE,
451                                                     ip,
452                                                     &core_reserve_callback,
453                                                     cp);          
454     }
455   GNUNET_assert (pth->cth == NULL);
456   pth->cth_in_progress++;
457   pth->cth = GNUNET_CORE_notify_transmit_ready (GSF_core,
458                                                 GNUNET_YES,
459                                                 pth->priority,
460                                                 GNUNET_TIME_absolute_get_remaining (pth->timeout),
461                                                 &target,
462                                                 pth->size,
463                                                 &peer_transmit_ready_cb,
464                                                 pth);
465   GNUNET_assert (0 < pth->cth_in_progress--);
466 }
467
468
469 /**
470  * Core is ready to transmit to a peer, get the message.
471  *
472  * @param cls the 'struct GSF_PeerTransmitHandle' of the message
473  * @param size number of bytes core is willing to take
474  * @param buf where to copy the message
475  * @return number of bytes copied to buf
476  */
477 static size_t
478 peer_transmit_ready_cb (void *cls,
479                         size_t size,
480                         void *buf)
481 {
482   struct GSF_PeerTransmitHandle *pth = cls;
483   struct GSF_PeerTransmitHandle *pos;
484   struct GSF_ConnectedPeer *cp;
485   size_t ret;
486
487   GNUNET_assert ( (NULL == buf) ||
488                   (pth->size <= size) );
489   pth->cth = NULL;
490   if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
491     {
492       GNUNET_SCHEDULER_cancel (pth->timeout_task);
493       pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
494     }
495   cp = pth->cp;
496   GNUNET_CONTAINER_DLL_remove (cp->pth_head,
497                                cp->pth_tail,
498                                pth);
499   if (GNUNET_YES == pth->is_query)
500     {
501       cp->ppd.last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
502       GNUNET_assert (0 < cp->ppd.pending_queries--);    
503     }
504   else if (GNUNET_NO == pth->is_query)
505     {
506       GNUNET_assert (0 < cp->ppd.pending_replies--);
507     }
508   GNUNET_LOAD_update (cp->ppd.transmission_delay,
509                       GNUNET_TIME_absolute_get_duration (pth->transmission_request_start_time).rel_value);
510   ret = pth->gmc (pth->gmc_cls, 
511                   size, buf);
512   GNUNET_assert (NULL == pth->cth);
513   for (pos = cp->pth_head; pos != NULL; pos = pos->next)
514     {
515       GNUNET_assert (pos != pth);
516       schedule_transmission (pos);
517     }
518   GNUNET_assert (pth->cth == NULL);
519   GNUNET_assert (pth->cth_in_progress == 0);
520   GNUNET_free (pth);
521   return ret;
522 }
523
524
525 /**
526  * (re)try to reserve bandwidth from the given peer.
527  *
528  * @param cls the 'struct GSF_ConnectedPeer' to reserve from
529  * @param tc scheduler context
530  */
531 static void
532 retry_reservation (void *cls,
533                    const struct GNUNET_SCHEDULER_TaskContext *tc)
534 {
535   struct GSF_ConnectedPeer *cp = cls;
536   uint64_t ip;
537   struct GNUNET_PeerIdentity target;
538
539   GNUNET_PEER_resolve (cp->ppd.pid,
540                        &target);
541   cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
542   ip = cp->inc_preference;
543   cp->inc_preference = 0;
544   cp->irc = GNUNET_CORE_peer_change_preference (GSF_core,
545                                                 &target,
546                                                 GNUNET_TIME_UNIT_FOREVER_REL,
547                                                 GNUNET_BANDWIDTH_VALUE_MAX,
548                                                 DBLOCK_SIZE,
549                                                 ip,
550                                                 &core_reserve_callback,
551                                                 cp);
552 }
553
554
555 /**
556  * Function called by core upon success or failure of our bandwidth reservation request.
557  *
558  * @param cls the 'struct GSF_ConnectedPeer' of the peer for which we made the request
559  * @param peer identifies the peer
560  * @param bandwidth_out available amount of outbound bandwidth
561  * @param amount set to the amount that was actually reserved or unreserved;
562  *               either the full requested amount or zero (no partial reservations)
563  * @param res_delay if the reservation could not be satisfied (amount was 0), how
564  *        long should the client wait until re-trying?
565  * @param preference current traffic preference for the given peer
566  */
567 static void
568 core_reserve_callback (void *cls,
569                        const struct GNUNET_PeerIdentity *peer,
570                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
571                        int32_t amount,
572                        struct GNUNET_TIME_Relative res_delay,
573                        uint64_t preference)
574 {
575   struct GSF_ConnectedPeer *cp = cls;
576   struct GSF_PeerTransmitHandle *pth;
577
578   cp->irc = NULL;
579   if (0 == amount)
580     {
581       cp->irc_delay_task = GNUNET_SCHEDULER_add_delayed (res_delay,
582                                                          &retry_reservation,
583                                                          cp);
584       return;
585     }
586   cp->did_reserve = GNUNET_YES;
587   pth = cp->pth_head;
588   if ( (NULL != pth) &&
589        (NULL == pth->cth) )
590     {
591       /* reservation success, try transmission now! */
592       pth->cth_in_progress++;
593       pth->cth = GNUNET_CORE_notify_transmit_ready (GSF_core,
594                                                     GNUNET_YES,
595                                                     pth->priority,
596                                                     GNUNET_TIME_absolute_get_remaining (pth->timeout),
597                                                     peer,
598                                                     pth->size,
599                                                     &peer_transmit_ready_cb,
600                                                     pth);
601       GNUNET_assert (0 < pth->cth_in_progress--);
602     }
603 }
604
605
606 /**
607  * A peer connected to us.  Setup the connected peer
608  * records.
609  *
610  * @param peer identity of peer that connected
611  * @param atsi performance data for the connection
612  * @return handle to connected peer entry
613  */
614 struct GSF_ConnectedPeer *
615 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
616                            const struct GNUNET_TRANSPORT_ATS_Information *atsi)
617 {
618   struct GSF_ConnectedPeer *cp;
619   char *fn;
620   uint32_t trust;
621
622   cp = GNUNET_malloc (sizeof (struct GSF_ConnectedPeer));
623   cp->ppd.pid = GNUNET_PEER_intern (peer);
624   cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
625   cp->irc = GNUNET_CORE_peer_change_preference (GSF_core,
626                                                 peer,
627                                                 GNUNET_TIME_UNIT_FOREVER_REL,
628                                                 GNUNET_BANDWIDTH_VALUE_MAX,
629                                                 DBLOCK_SIZE,
630                                                 0,
631                                                 &core_reserve_callback,
632                                                 cp);
633   fn = get_trust_filename (peer);
634   if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
635       (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
636     cp->disk_trust = cp->ppd.trust = ntohl (trust);
637   GNUNET_free (fn);
638   cp->request_map = GNUNET_CONTAINER_multihashmap_create (128);
639   GNUNET_break (GNUNET_OK ==
640                 GNUNET_CONTAINER_multihashmap_put (cp_map,
641                                                    &peer->hashPubKey,
642                                                    cp,
643                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
644   update_atsi (cp, atsi);
645   GSF_push_start_ (cp);
646   return cp;
647 }
648
649
650 /**
651  * It may be time to re-start migrating content to this
652  * peer.  Check, and if so, restart migration.
653  *
654  * @param cls the 'struct GSF_ConnectedPeer'
655  * @param tc scheduler context
656  */
657 static void
658 revive_migration (void *cls,
659                   const struct GNUNET_SCHEDULER_TaskContext *tc)
660 {
661   struct GSF_ConnectedPeer *cp = cls;
662   struct GNUNET_TIME_Relative bt;
663   
664   cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
665   bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
666   if (0 != bt.rel_value)
667     {
668       /* still time left... */
669       cp->mig_revive_task 
670         = GNUNET_SCHEDULER_add_delayed (bt,
671                                         &revive_migration,
672                                         cp);
673       return;
674     }
675   GSF_push_start_ (cp);
676 }
677
678
679 /**
680  * Get a handle for a connected peer.
681  *
682  * @param peer peer's identity
683  * @return NULL if the peer is not currently connected
684  */
685 struct GSF_ConnectedPeer *
686 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
687 {
688   return GNUNET_CONTAINER_multihashmap_get (cp_map,
689                                             &peer->hashPubKey);
690 }
691
692
693 /**
694  * Handle P2P "MIGRATION_STOP" message.
695  *
696  * @param cls closure, always NULL
697  * @param other the other peer involved (sender or receiver, NULL
698  *        for loopback messages where we are both sender and receiver)
699  * @param message the actual message
700  * @param atsi performance information
701  * @return GNUNET_OK to keep the connection open,
702  *         GNUNET_SYSERR to close it (signal serious error)
703  */
704 int
705 GSF_handle_p2p_migration_stop_ (void *cls,
706                                 const struct GNUNET_PeerIdentity *other,
707                                 const struct GNUNET_MessageHeader *message,
708                                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
709 {
710   struct GSF_ConnectedPeer *cp; 
711   const struct MigrationStopMessage *msm;
712   struct GNUNET_TIME_Relative bt;
713
714   msm = (const struct MigrationStopMessage*) message;
715   cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
716                                           &other->hashPubKey);
717   if (cp == NULL)
718     {
719       GNUNET_break (0);
720       return GNUNET_OK;
721     }
722   bt = GNUNET_TIME_relative_ntoh (msm->duration);
723   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
724               _("Migration of content to peer `%s' blocked for %llu ms\n"),
725               GNUNET_i2s (other),
726               (unsigned long long) bt.rel_value);
727   cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
728   if (cp->mig_revive_task == GNUNET_SCHEDULER_NO_TASK)
729     {
730       GSF_push_stop_ (cp);
731       cp->mig_revive_task 
732         = GNUNET_SCHEDULER_add_delayed (bt,
733                                         &revive_migration,
734                                         cp);
735     }
736   update_atsi (cp, atsi);
737   return GNUNET_OK;
738 }
739
740
741 /**
742  * Copy reply and free put message.
743  *
744  * @param cls the 'struct PutMessage'
745  * @param buf_size number of bytes available in buf
746  * @param buf where to copy the message, NULL on error (peer disconnect)
747  * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
748  */
749 static size_t 
750 copy_reply (void *cls,
751             size_t buf_size,
752             void *buf)
753 {
754   struct PutMessage *pm = cls;
755   size_t size;
756
757   if (buf != NULL)
758     {
759       GNUNET_assert (buf_size >= ntohs (pm->header.size));
760       size = ntohs (pm->header.size);
761       memcpy (buf, pm, size); 
762       GNUNET_STATISTICS_update (GSF_stats,
763                                 gettext_noop ("# replies transmitted to other peers"),
764                                 1,
765                                 GNUNET_NO); 
766     }
767   else
768     {
769       size = 0;
770       GNUNET_STATISTICS_update (GSF_stats,
771                                 gettext_noop ("# replies dropped"),
772                                 1,
773                                 GNUNET_NO); 
774     }
775   GNUNET_free (pm);
776   return size;
777 }
778
779
780 /**
781  * Cancel all requests associated with the peer.
782  *
783  * @param cls unused
784  * @param query hash code of the request
785  * @param value the 'struct GSF_PendingRequest'
786  * @return GNUNET_YES (continue to iterate)
787  */
788 static int
789 cancel_pending_request (void *cls,
790                         const GNUNET_HashCode *query,
791                         void *value)
792 {
793   struct PeerRequest *peerreq = value;
794   struct GSF_PendingRequest *pr = peerreq->pr;
795   struct GSF_ConnectedPeer *cp = peerreq->cp;
796   struct GSF_PendingRequestData *prd;
797
798   if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
799     {
800       GNUNET_SCHEDULER_cancel (peerreq->kill_task);
801       peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
802     }
803   GNUNET_STATISTICS_update (GSF_stats,
804                             gettext_noop ("# P2P searches active"),
805                             -1,
806                             GNUNET_NO);
807   prd = GSF_pending_request_get_data_ (pr);
808   GNUNET_break (GNUNET_OK ==
809                 GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
810                                                       &prd->query,
811                                                       peerreq));
812   GSF_pending_request_cancel_ (pr);
813   GNUNET_free (peerreq);
814   return GNUNET_OK;
815 }
816
817
818 /**
819  * Free the given request.
820  *
821  * @param cls the request to free
822  * @param tc task context
823  */ 
824 static void
825 peer_request_destroy (void *cls,
826                       const struct GNUNET_SCHEDULER_TaskContext *tc)
827 {
828   struct PeerRequest *peerreq = cls;
829   struct GSF_PendingRequest *pr = peerreq->pr;
830   struct GSF_PendingRequestData *prd;
831
832   peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
833   prd = GSF_pending_request_get_data_ (pr);
834   cancel_pending_request (NULL,
835                           &prd->query,
836                           peerreq);
837 }
838
839
840 /**
841  * The artificial delay is over, transmit the message now.
842  *
843  * @param cls the 'struct GSF_DelayedHandle' with the message
844  * @param tc scheduler context
845  */
846 static void
847 transmit_delayed_now (void *cls,
848                       const struct GNUNET_SCHEDULER_TaskContext *tc)
849 {
850   struct GSF_DelayedHandle *dh = cls;
851   struct GSF_ConnectedPeer *cp = dh->cp;
852
853   GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
854                                cp->delayed_tail,
855                                dh);
856   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
857     {
858       GNUNET_free (dh->pm);
859       GNUNET_free (dh);
860       return;
861     }
862   (void) GSF_peer_transmit_ (cp, GNUNET_NO,
863                              UINT32_MAX,
864                              REPLY_TIMEOUT,
865                              dh->msize,
866                              &copy_reply,
867                              dh->pm);
868   GNUNET_free (dh);
869 }
870
871
872 /**
873  * Get the randomized delay a response should be subjected to.
874  * 
875  * @return desired delay
876  */
877 static struct GNUNET_TIME_Relative
878 get_randomized_delay ()
879 {
880   struct GNUNET_TIME_Relative ret;
881
882   /* FIXME: replace 5000 with something relating to current observed P2P message latency */
883   ret = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
884                                        GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
885                                                                  5000));
886   GNUNET_STATISTICS_update (GSF_stats,
887                             gettext_noop ("# artificial delays introduced (ms)"),
888                             ret.rel_value,
889                             GNUNET_NO);
890
891   return ret;
892 }
893
894
895 /**
896  * Handle a reply to a pending request.  Also called if a request
897  * expires (then with data == NULL).  The handler may be called
898  * many times (depending on the request type), but will not be
899  * called during or after a call to GSF_pending_request_cancel 
900  * and will also not be called anymore after a call signalling
901  * expiration.
902  *
903  * @param cls 'struct PeerRequest' this is an answer for
904  * @param eval evaluation of the result
905  * @param pr handle to the original pending request
906  * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
907  * @param expiration when does 'data' expire?
908  * @param type type of the block
909  * @param data response data, NULL on request expiration
910  * @param data_len number of bytes in data
911  */
912 static void
913 handle_p2p_reply (void *cls,
914                   enum GNUNET_BLOCK_EvaluationResult eval,
915                   struct GSF_PendingRequest *pr,
916                   uint32_t reply_anonymity_level,
917                   struct GNUNET_TIME_Absolute expiration,
918                   enum GNUNET_BLOCK_Type type,
919                   const void *data,
920                   size_t data_len)
921 {
922   struct PeerRequest *peerreq = cls;
923   struct GSF_ConnectedPeer *cp = peerreq->cp;
924   struct GSF_PendingRequestData *prd;
925   struct PutMessage *pm;
926   size_t msize;
927
928   GNUNET_assert (data_len + sizeof (struct PutMessage) < GNUNET_SERVER_MAX_MESSAGE_SIZE);
929   GNUNET_assert (peerreq->pr == pr);
930   prd = GSF_pending_request_get_data_ (pr);
931   if (NULL == data)
932     {
933       GNUNET_STATISTICS_update (GSF_stats,
934                                 gettext_noop ("# P2P searches active"),
935                                 -1,
936                                 GNUNET_NO);
937       GNUNET_break (GNUNET_OK ==
938                     GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
939                                                           &prd->query,
940                                                           peerreq));
941       GNUNET_free (peerreq);
942       return;
943     }  
944   GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
945   if ( (prd->type != type) &&
946        (prd->type != GNUNET_BLOCK_TYPE_ANY) )
947     {
948       GNUNET_break (0);
949       return;
950     }
951 #if DEBUG_FS
952   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953               "Transmitting result for query `%s' to peer\n",
954               GNUNET_h2s (&prd->query));
955 #endif  
956   GNUNET_STATISTICS_update (GSF_stats,
957                             gettext_noop ("# replies received for other peers"),
958                             1,
959                             GNUNET_NO); 
960   msize = sizeof (struct PutMessage) + data_len;
961   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
962     {
963       GNUNET_break (0);
964       return;
965     }
966   if ( (reply_anonymity_level != UINT32_MAX) &&
967        (reply_anonymity_level > 1) )
968     {
969       if (reply_anonymity_level - 1 > GSF_cover_content_count) 
970         {
971           GNUNET_STATISTICS_update (GSF_stats,
972                                     gettext_noop ("# replies dropped due to insufficient cover traffic"),
973                                     1,
974                                     GNUNET_NO); 
975           return;
976         }
977       GSF_cover_content_count -= (reply_anonymity_level - 1);
978     }
979     
980   pm = GNUNET_malloc (msize);
981   pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
982   pm->header.size = htons (msize);
983   pm->type = htonl (type);
984   pm->expiration = GNUNET_TIME_absolute_hton (expiration);
985   memcpy (&pm[1], data, data_len);
986   if ( (reply_anonymity_level != UINT32_MAX) &&
987        (reply_anonymity_level != 0) &&
988        (GSF_enable_randomized_delays == GNUNET_YES) )
989     {
990       struct GSF_DelayedHandle *dh;
991
992       dh = GNUNET_malloc (sizeof (struct GSF_DelayedHandle));
993       dh->cp = cp;
994       dh->pm = pm;
995       dh->msize = msize;
996       GNUNET_CONTAINER_DLL_insert (cp->delayed_head,
997                                    cp->delayed_tail,
998                                    dh);
999       dh->delay_task = GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
1000                                                      &transmit_delayed_now,
1001                                                      dh);
1002     }
1003   else
1004     {
1005       (void) GSF_peer_transmit_ (cp, GNUNET_NO,
1006                                  UINT32_MAX,
1007                                  REPLY_TIMEOUT,
1008                                  msize,
1009                                  &copy_reply,
1010                                  pm);
1011     }
1012   if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1013     return;
1014   if (GNUNET_SCHEDULER_NO_TASK == peerreq->kill_task)
1015     {
1016       GNUNET_STATISTICS_update (GSF_stats,
1017                                 gettext_noop ("# P2P searches destroyed due to ultimate reply"),
1018                                 1,
1019                                 GNUNET_NO);
1020      peerreq->kill_task = GNUNET_SCHEDULER_add_now (&peer_request_destroy,
1021                                                      peerreq);
1022     }
1023 }
1024
1025
1026 /**
1027  * Increase the host credit by a value.
1028  *
1029  * @param cp which peer to change the trust value on
1030  * @param value is the int value by which the
1031  *  host credit is to be increased or decreased
1032  * @returns the actual change in trust (positive or negative)
1033  */
1034 static int
1035 change_host_trust (struct GSF_ConnectedPeer *cp, int value)
1036 {
1037   if (value == 0)
1038     return 0;
1039   GNUNET_assert (cp != NULL);
1040   if (value > 0)
1041     {
1042       if (cp->ppd.trust + value < cp->ppd.trust)
1043         {
1044           value = UINT32_MAX - cp->ppd.trust;
1045           cp->ppd.trust = UINT32_MAX;
1046         }
1047       else
1048         cp->ppd.trust += value;
1049     }
1050   else
1051     {
1052       if (cp->ppd.trust < -value)
1053         {
1054           value = -cp->ppd.trust;
1055           cp->ppd.trust = 0;
1056         }
1057       else
1058         cp->ppd.trust += value;
1059     }
1060   return value;
1061 }
1062
1063
1064 /**
1065  * We've received a request with the specified priority.  Bound it
1066  * according to how much we trust the given peer.
1067  * 
1068  * @param prio_in requested priority
1069  * @param cp the peer making the request
1070  * @return effective priority
1071  */
1072 static int32_t
1073 bound_priority (uint32_t prio_in,
1074                 struct GSF_ConnectedPeer *cp)
1075 {
1076 #define N ((double)128.0)
1077   uint32_t ret;
1078   double rret;
1079   int ld;
1080
1081   ld = GSF_test_get_load_too_high_ (0);
1082   if (ld == GNUNET_SYSERR)
1083     {
1084       GNUNET_STATISTICS_update (GSF_stats,
1085                                 gettext_noop ("# requests done for free (low load)"),
1086                                 1,
1087                                 GNUNET_NO);
1088       return 0; /* excess resources */
1089     }
1090   if (prio_in > INT32_MAX)
1091     prio_in = INT32_MAX;
1092   ret = - change_host_trust (cp, - (int) prio_in);
1093   if (ret > 0)
1094     {
1095       if (ret > GSF_current_priorities + N)
1096         rret = GSF_current_priorities + N;
1097       else
1098         rret = ret;
1099       GSF_current_priorities 
1100         = (GSF_current_priorities * (N-1) + rret)/N;
1101     }
1102   if ( (ld == GNUNET_YES) && (ret > 0) )
1103     {
1104       /* try with charging */
1105       ld = GSF_test_get_load_too_high_ (ret);
1106     }
1107   if (ld == GNUNET_YES)
1108     {
1109       GNUNET_STATISTICS_update (GSF_stats,
1110                                 gettext_noop ("# request dropped, priority insufficient"),
1111                                 1,
1112                                 GNUNET_NO);
1113       /* undo charge */
1114       change_host_trust (cp, (int) ret);
1115       return -1; /* not enough resources */
1116     }
1117   else
1118     {
1119       GNUNET_STATISTICS_update (GSF_stats,
1120                                 gettext_noop ("# requests done for a price (normal load)"),
1121                                 1,
1122                                 GNUNET_NO);
1123     }
1124 #undef N
1125   return ret;
1126 }
1127
1128
1129 /**
1130  * The priority level imposes a bound on the maximum
1131  * value for the ttl that can be requested.
1132  *
1133  * @param ttl_in requested ttl
1134  * @param prio given priority
1135  * @return ttl_in if ttl_in is below the limit,
1136  *         otherwise the ttl-limit for the given priority
1137  */
1138 static int32_t
1139 bound_ttl (int32_t ttl_in, uint32_t prio)
1140 {
1141   unsigned long long allowed;
1142
1143   if (ttl_in <= 0)
1144     return ttl_in;
1145   allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000; 
1146   if (ttl_in > allowed)      
1147     {
1148       if (allowed >= (1 << 30))
1149         return 1 << 30;
1150       return allowed;
1151     }
1152   return ttl_in;
1153 }
1154
1155
1156 /**
1157  * Handle P2P "QUERY" message.  Creates the pending request entry
1158  * and sets up all of the data structures to that we will
1159  * process replies properly.  Does not initiate forwarding or
1160  * local database lookups.
1161  *
1162  * @param other the other peer involved (sender or receiver, NULL
1163  *        for loopback messages where we are both sender and receiver)
1164  * @param message the actual message
1165  * @return pending request handle, NULL on error
1166  */
1167 struct GSF_PendingRequest *
1168 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
1169                        const struct GNUNET_MessageHeader *message)
1170 {
1171   struct PeerRequest *peerreq;
1172   struct GSF_PendingRequest *pr;
1173   struct GSF_PendingRequestData *prd;
1174   struct GSF_ConnectedPeer *cp;
1175   struct GSF_ConnectedPeer *cps;
1176   const GNUNET_HashCode *namespace;
1177   const struct GNUNET_PeerIdentity *target;
1178   enum GSF_PendingRequestOptions options;                            
1179   uint16_t msize;
1180   const struct GetMessage *gm;
1181   unsigned int bits;
1182   const GNUNET_HashCode *opt;
1183   uint32_t bm;
1184   size_t bfsize;
1185   uint32_t ttl_decrement;
1186   int32_t priority;
1187   int32_t ttl;
1188   enum GNUNET_BLOCK_Type type;
1189   GNUNET_PEER_Id spid;
1190
1191   msize = ntohs(message->size);
1192   if (msize < sizeof (struct GetMessage))
1193     {
1194       GNUNET_break_op (0);
1195       return NULL;
1196     }
1197   GNUNET_STATISTICS_update (GSF_stats,
1198                             gettext_noop ("# GET requests received (from other peers)"),
1199                             1,
1200                             GNUNET_NO);
1201   gm = (const struct GetMessage*) message;
1202   type = ntohl (gm->type);
1203   bm = ntohl (gm->hash_bitmap);
1204   bits = 0;
1205   while (bm > 0)
1206     {
1207       if (1 == (bm & 1))
1208         bits++;
1209       bm >>= 1;
1210     }
1211   if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
1212     {
1213       GNUNET_break_op (0);
1214       return NULL;
1215     }  
1216   opt = (const GNUNET_HashCode*) &gm[1];
1217   bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
1218   /* bfsize must be power of 2, check! */
1219   if (0 != ( (bfsize - 1) & bfsize))
1220     {
1221       GNUNET_break_op (0);
1222       return NULL;
1223     }
1224   GSF_cover_query_count++;
1225   bm = ntohl (gm->hash_bitmap);
1226   bits = 0;
1227   cps = GNUNET_CONTAINER_multihashmap_get (cp_map,
1228                                            &other->hashPubKey);
1229   if (NULL == cps)
1230     {
1231       /* peer must have just disconnected */
1232       GNUNET_STATISTICS_update (GSF_stats,
1233                                 gettext_noop ("# requests dropped due to initiator not being connected"),
1234                                 1,
1235                                 GNUNET_NO);
1236       return NULL;
1237     }
1238   if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1239     cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1240                                             &opt[bits++]);
1241   else
1242     cp = cps;
1243   if (cp == NULL)
1244     {
1245 #if DEBUG_FS
1246       if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
1247         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248                     "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
1249                     GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
1250       
1251       else
1252         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253                     "Failed to find peer `%4s' in connection set. Dropping query.\n",
1254                     GNUNET_i2s (other));
1255 #endif
1256       GNUNET_STATISTICS_update (GSF_stats,
1257                                 gettext_noop ("# requests dropped due to missing reverse route"),
1258                                 1,
1259                                 GNUNET_NO);
1260       return NULL;
1261     }
1262   /* note that we can really only check load here since otherwise
1263      peers could find out that we are overloaded by not being
1264      disconnected after sending us a malformed query... */
1265   priority = bound_priority (ntohl (gm->priority), cps);
1266   if (priority < 0)
1267     {
1268 #if DEBUG_FS
1269       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270                   "Dropping query from `%s', this peer is too busy.\n",
1271                   GNUNET_i2s (other));
1272 #endif
1273       return NULL;
1274     }
1275 #if DEBUG_FS 
1276   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277               "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
1278               GNUNET_h2s (&gm->query),
1279               (unsigned int) type,
1280               GNUNET_i2s (other),
1281               (unsigned int) bm);
1282 #endif
1283   namespace = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) ? &opt[bits++] : NULL;
1284   if ( (type == GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
1285        (namespace == NULL) )
1286     {
1287       GNUNET_break_op (0);
1288       return NULL;
1289     }
1290   if ( (type != GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
1291        (namespace != NULL) )
1292     {
1293       GNUNET_break_op (0);
1294       return NULL;
1295     }
1296   target = (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? ((const struct GNUNET_PeerIdentity*) &opt[bits++]) : NULL;
1297   options = 0;
1298   spid = 0;
1299   if ( (GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority)) ||
1300        (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) > 
1301         GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 + GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)) )
1302     {
1303       /* don't have BW to send to peer, or would likely take longer than we have for it,
1304          so at best indirect the query */
1305       priority = 0;
1306       options |= GSF_PRO_FORWARD_ONLY;
1307       spid = GNUNET_PEER_intern (other);
1308     }
1309   ttl = bound_ttl (ntohl (gm->ttl), priority);
1310   /* decrement ttl (always) */
1311   ttl_decrement = 2 * TTL_DECREMENT +
1312     GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1313                               TTL_DECREMENT);
1314   if ( (ttl < 0) &&
1315        (((int32_t)(ttl - ttl_decrement)) > 0) )
1316     {
1317 #if DEBUG_FS
1318       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319                   "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
1320                   GNUNET_i2s (other),
1321                   ttl,
1322                   ttl_decrement);
1323 #endif
1324       GNUNET_STATISTICS_update (GSF_stats,
1325                                 gettext_noop ("# requests dropped due TTL underflow"),
1326                                 1,
1327                                 GNUNET_NO);
1328       /* integer underflow => drop (should be very rare)! */      
1329       return NULL;
1330     } 
1331   ttl -= ttl_decrement;
1332
1333   /* test if the request already exists */
1334   peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map,
1335                                                &gm->query);
1336   if (peerreq != NULL) 
1337     {      
1338       pr = peerreq->pr;
1339       prd = GSF_pending_request_get_data_ (pr);
1340       if ( (prd->type == type) &&
1341            ( (type != GNUNET_BLOCK_TYPE_FS_SBLOCK) ||
1342              (0 == memcmp (&prd->namespace,
1343                            namespace,
1344                            sizeof (GNUNET_HashCode))) ) )
1345         {
1346           if (prd->ttl.abs_value >= GNUNET_TIME_absolute_get().abs_value + ttl)
1347             {
1348               /* existing request has higher TTL, drop new one! */
1349               prd->priority += priority;
1350 #if DEBUG_FS
1351               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352                           "Have existing request with higher TTL, dropping new request.\n",
1353                           GNUNET_i2s (other));
1354 #endif
1355               GNUNET_STATISTICS_update (GSF_stats,
1356                                         gettext_noop ("# requests dropped due to higher-TTL request"),
1357                                         1,
1358                                         GNUNET_NO);
1359               return NULL;
1360             }
1361           /* existing request has lower TTL, drop old one! */
1362           GNUNET_STATISTICS_update (GSF_stats,
1363                                     gettext_noop ("# P2P searches active"),
1364                                     -1,
1365                                     GNUNET_NO);
1366           priority += prd->priority;
1367           GSF_pending_request_cancel_ (pr);
1368           GNUNET_assert (GNUNET_YES ==
1369                          GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
1370                                                                &gm->query,
1371                                                                peerreq));
1372           if (peerreq->kill_task != GNUNET_SCHEDULER_NO_TASK)
1373             {
1374               GNUNET_SCHEDULER_cancel (peerreq->kill_task);
1375               peerreq->kill_task = GNUNET_SCHEDULER_NO_TASK;
1376             }
1377           GNUNET_free (peerreq);
1378         }
1379     }
1380   
1381   peerreq = GNUNET_malloc (sizeof (struct PeerRequest));
1382   peerreq->cp = cp; 
1383   pr = GSF_pending_request_create_ (options,
1384                                     type,
1385                                     &gm->query,
1386                                     namespace,
1387                                     target,
1388                                     (bfsize > 0) ? (const char*)&opt[bits] : NULL,
1389                                     bfsize,
1390                                     ntohl (gm->filter_mutator),
1391                                     1 /* anonymity */,
1392                                     (uint32_t) priority,
1393                                     ttl,
1394                                     spid,
1395                                     NULL, 0, /* replies_seen */
1396                                     &handle_p2p_reply,
1397                                     peerreq);
1398   GNUNET_assert (NULL != pr);
1399   peerreq->pr = pr;
1400   GNUNET_break (GNUNET_OK ==
1401                 GNUNET_CONTAINER_multihashmap_put (cp->request_map,
1402                                                    &gm->query,
1403                                                    peerreq,
1404                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1405   GNUNET_STATISTICS_update (GSF_stats,
1406                             gettext_noop ("# P2P query messages received and processed"),
1407                             1,
1408                             GNUNET_NO);
1409   GNUNET_STATISTICS_update (GSF_stats,
1410                             gettext_noop ("# P2P searches active"),
1411                             1,
1412                             GNUNET_NO);
1413   return pr;
1414 }
1415
1416
1417 /**
1418  * Function called if there has been a timeout trying to satisfy
1419  * a transmission request.
1420  *
1421  * @param cls the 'struct GSF_PeerTransmitHandle' of the request 
1422  * @param tc scheduler context
1423  */
1424 static void
1425 peer_transmit_timeout (void *cls,
1426                        const struct GNUNET_SCHEDULER_TaskContext *tc)
1427 {
1428   struct GSF_PeerTransmitHandle *pth = cls;
1429   struct GSF_ConnectedPeer *cp;
1430
1431 #if DEBUG_FS
1432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1433               "Timeout trying to transmit to other peer\n");
1434 #endif  
1435   pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1436   cp = pth->cp;
1437   GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1438                                cp->pth_tail,
1439                                pth);
1440   if (GNUNET_YES == pth->is_query)
1441     GNUNET_assert (0 < cp->ppd.pending_queries--);    
1442   else if (GNUNET_NO == pth->is_query)
1443     GNUNET_assert (0 < cp->ppd.pending_replies--);
1444   GNUNET_LOAD_update (cp->ppd.transmission_delay,
1445                       UINT64_MAX);
1446   if (NULL != pth->cth)
1447     {
1448       GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1449       pth->cth = NULL;
1450     }
1451   pth->gmc (pth->gmc_cls, 
1452             0, NULL);
1453   GNUNET_assert (0 == pth->cth_in_progress);
1454   GNUNET_free (pth);
1455 }
1456
1457
1458 /**
1459  * Transmit a message to the given peer as soon as possible.
1460  * If the peer disconnects before the transmission can happen,
1461  * the callback is invoked with a 'NULL' buffer.
1462  *
1463  * @param cp target peer
1464  * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO) or neither (GNUNET_SYSERR)
1465  * @param priority how important is this request?
1466  * @param timeout when does this request timeout (call gmc with error)
1467  * @param size number of bytes we would like to send to the peer
1468  * @param gmc function to call to get the message
1469  * @param gmc_cls closure for gmc
1470  * @return handle to cancel request
1471  */
1472 struct GSF_PeerTransmitHandle *
1473 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
1474                     int is_query,
1475                     uint32_t priority,
1476                     struct GNUNET_TIME_Relative timeout,
1477                     size_t size,
1478                     GSF_GetMessageCallback gmc,
1479                     void *gmc_cls)
1480 {
1481   struct GSF_PeerTransmitHandle *pth;
1482   struct GSF_PeerTransmitHandle *pos;
1483   struct GSF_PeerTransmitHandle *prev;
1484
1485   pth = GNUNET_malloc (sizeof (struct GSF_PeerTransmitHandle));
1486   pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
1487   pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1488   pth->gmc = gmc;
1489   pth->gmc_cls = gmc_cls;
1490   pth->size = size;
1491   pth->is_query = is_query;
1492   pth->priority = priority;
1493   pth->cp = cp;
1494   /* insertion sort (by priority, descending) */
1495   prev = NULL;
1496   pos = cp->pth_head;
1497   while ( (pos != NULL) &&
1498           (pos->priority > priority) )
1499     {
1500       prev = pos;
1501       pos = pos->next;
1502     }
1503   if (prev == NULL)
1504     GNUNET_CONTAINER_DLL_insert (cp->pth_head,
1505                                  cp->pth_tail,
1506                                  pth);
1507   else
1508     GNUNET_CONTAINER_DLL_insert_after (cp->pth_head,
1509                                        cp->pth_tail,
1510                                        prev,
1511                                        pth);
1512   if (GNUNET_YES == is_query)
1513     cp->ppd.pending_queries++;
1514   else if (GNUNET_NO == is_query)
1515     cp->ppd.pending_replies++;
1516   pth->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1517                                                     &peer_transmit_timeout,
1518                                                     pth);
1519   schedule_transmission (pth);
1520   return pth;
1521 }
1522
1523
1524 /**
1525  * Cancel an earlier request for transmission.
1526  *
1527  * @param pth request to cancel
1528  */
1529 void
1530 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
1531 {
1532   struct GSF_ConnectedPeer *cp;
1533
1534   if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1535     {
1536       GNUNET_SCHEDULER_cancel (pth->timeout_task);
1537       pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1538     }
1539   if (NULL != pth->cth)
1540     {
1541       GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1542       pth->cth = NULL;
1543     }
1544   cp = pth->cp;
1545   GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1546                                cp->pth_tail,
1547                                pth);
1548   if (GNUNET_YES == pth->is_query)
1549     GNUNET_assert (0 < cp->ppd.pending_queries--);    
1550   else if (GNUNET_NO == pth->is_query)
1551     GNUNET_assert (0 < cp->ppd.pending_replies--);
1552   GNUNET_assert (0 == pth->cth_in_progress);
1553   GNUNET_free (pth);
1554 }
1555
1556
1557 /**
1558  * Report on receiving a reply; update the performance record of the given peer.
1559  *
1560  * @param cp responding peer (will be updated)
1561  * @param request_time time at which the original query was transmitted
1562  * @param request_priority priority of the original request
1563  */
1564 void
1565 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
1566                               struct GNUNET_TIME_Absolute request_time,
1567                               uint32_t request_priority)
1568 {
1569   struct GNUNET_TIME_Relative delay;
1570
1571   delay = GNUNET_TIME_absolute_get_duration (request_time);  
1572   cp->ppd.avg_reply_delay.rel_value = (cp->ppd.avg_reply_delay.rel_value * (RUNAVG_DELAY_N-1) + delay.rel_value) / RUNAVG_DELAY_N;
1573   cp->ppd.avg_priority = (cp->ppd.avg_priority * (RUNAVG_DELAY_N-1) + request_priority) / RUNAVG_DELAY_N;
1574 }
1575
1576
1577 /**
1578  * Report on receiving a reply in response to an initiating client.
1579  * Remember that this peer is good for this client.
1580  *
1581  * @param cp responding peer (will be updated)
1582  * @param initiator_client local client on responsible for query
1583  */
1584 void
1585 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
1586                                    struct GSF_LocalClient *initiator_client)
1587 {
1588   cp->ppd.last_client_replies[cp->last_client_replies_woff++ % CS2P_SUCCESS_LIST_SIZE] = initiator_client;
1589 }
1590
1591
1592 /**
1593  * Report on receiving a reply in response to an initiating peer.
1594  * Remember that this peer is good for this initiating peer.
1595  *
1596  * @param cp responding peer (will be updated)
1597  * @param initiator_peer other peer responsible for query
1598  */
1599 void
1600 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
1601                                  const struct GSF_ConnectedPeer *initiator_peer)
1602 {
1603   GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE], -1);
1604   cp->ppd.last_p2p_replies[cp->last_p2p_replies_woff++ % P2P_SUCCESS_LIST_SIZE] = initiator_peer->ppd.pid;
1605   GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
1606 }
1607
1608
1609 /**
1610  * Method called whenever a given peer has a status change.
1611  *
1612  * @param cls closure
1613  * @param peer peer identity this notification is about
1614  * @param bandwidth_in available amount of inbound bandwidth
1615  * @param bandwidth_out available amount of outbound bandwidth
1616  * @param timeout absolute time when this peer will time out
1617  *        unless we see some further activity from it
1618  * @param atsi status information
1619  */
1620 void
1621 GSF_peer_status_handler_ (void *cls,
1622                           const struct GNUNET_PeerIdentity *peer,
1623                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1624                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1625                           struct GNUNET_TIME_Absolute timeout,
1626                           const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1627 {
1628   struct GSF_ConnectedPeer *cp;
1629
1630   cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1631                                           &peer->hashPubKey);
1632   GNUNET_assert (NULL != cp);
1633   update_atsi (cp, atsi);
1634 }
1635
1636
1637 /**
1638  * A peer disconnected from us.  Tear down the connected peer
1639  * record.
1640  *
1641  * @param cls unused
1642  * @param peer identity of peer that connected
1643  */
1644 void
1645 GSF_peer_disconnect_handler_ (void *cls,
1646                               const struct GNUNET_PeerIdentity *peer)
1647 {
1648   struct GSF_ConnectedPeer *cp;
1649   struct GSF_PeerTransmitHandle *pth;
1650   struct GSF_DelayedHandle *dh;
1651
1652   cp = GNUNET_CONTAINER_multihashmap_get (cp_map,
1653                                           &peer->hashPubKey);
1654   if (NULL == cp)
1655     return; /* must have been disconnect from core with
1656                'peer' == my_id, ignore */
1657   GNUNET_CONTAINER_multihashmap_remove (cp_map,
1658                                         &peer->hashPubKey,
1659                                         cp);
1660   if (NULL != cp->migration_pth)
1661     {
1662       GSF_peer_transmit_cancel_ (cp->migration_pth);
1663       cp->migration_pth = NULL;
1664     }
1665   if (NULL != cp->irc)
1666     {
1667       GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1668       cp->irc = NULL;
1669     }
1670   if (GNUNET_SCHEDULER_NO_TASK != cp->irc_delay_task)
1671     {
1672       GNUNET_SCHEDULER_cancel (cp->irc_delay_task);
1673       cp->irc_delay_task = GNUNET_SCHEDULER_NO_TASK;
1674     }
1675   GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
1676                                          &cancel_pending_request,
1677                                          cp);
1678   GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
1679   cp->request_map = NULL;
1680   GSF_plan_notify_peer_disconnect_ (cp);
1681   GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
1682   GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1683   while (NULL != (pth = cp->pth_head))
1684     {
1685       if (NULL != pth->cth)
1686         {
1687           GNUNET_CORE_notify_transmit_ready_cancel (pth->cth);
1688           pth->cth = NULL;
1689         }
1690       if (pth->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1691         {
1692           GNUNET_SCHEDULER_cancel (pth->timeout_task);
1693           pth->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1694         }
1695       GNUNET_CONTAINER_DLL_remove (cp->pth_head,
1696                                    cp->pth_tail,
1697                                    pth);
1698       GNUNET_assert (0 == pth->cth_in_progress);
1699       GNUNET_free (pth);
1700     }
1701   while (NULL != (dh = cp->delayed_head))
1702     {
1703       GNUNET_CONTAINER_DLL_remove (cp->delayed_head,
1704                                    cp->delayed_tail,
1705                                    dh);
1706       GNUNET_SCHEDULER_cancel (dh->delay_task);
1707       GNUNET_free (dh->pm);
1708       GNUNET_free (dh);
1709     }
1710   GNUNET_PEER_change_rc (cp->ppd.pid, -1);
1711   GSF_push_stop_ (cp);
1712   if (GNUNET_SCHEDULER_NO_TASK != cp->mig_revive_task)
1713     {
1714       GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
1715       cp->mig_revive_task = GNUNET_SCHEDULER_NO_TASK;
1716     }
1717   GNUNET_free (cp);
1718 }
1719
1720
1721 /**
1722  * Closure for 'call_iterator'.
1723  */
1724 struct IterationContext
1725 {
1726   /**
1727    * Function to call on each entry.
1728    */
1729   GSF_ConnectedPeerIterator it;
1730
1731   /**
1732    * Closure for 'it'.
1733    */
1734   void *it_cls;
1735 };
1736
1737
1738 /**
1739  * Function that calls the callback for each peer.
1740  *
1741  * @param cls the 'struct IterationContext*'
1742  * @param key identity of the peer
1743  * @param value the 'struct GSF_ConnectedPeer*'
1744  * @return GNUNET_YES to continue iteration
1745  */
1746 static int
1747 call_iterator (void *cls,
1748                const GNUNET_HashCode *key,
1749                void *value)
1750 {
1751   struct IterationContext *ic = cls;
1752   struct GSF_ConnectedPeer *cp = value;
1753   
1754   ic->it (ic->it_cls,
1755           (const struct GNUNET_PeerIdentity*) key,
1756           cp,
1757           &cp->ppd);
1758   return GNUNET_YES;
1759 }
1760
1761
1762 /**
1763  * Iterate over all connected peers.
1764  *
1765  * @param it function to call for each peer
1766  * @param it_cls closure for it
1767  */
1768 void
1769 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
1770                               void *it_cls)
1771 {
1772   struct IterationContext ic;
1773
1774   ic.it = it;
1775   ic.it_cls = it_cls;
1776   GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1777                                          &call_iterator,
1778                                          &ic);
1779 }
1780
1781
1782 /**
1783  * Obtain the identity of a connected peer.
1784  *
1785  * @param cp peer to reserve bandwidth from
1786  * @param id identity to set (written to)
1787  */
1788 void
1789 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
1790                                   struct GNUNET_PeerIdentity *id)
1791 {
1792   GNUNET_PEER_resolve (cp->ppd.pid,
1793                        id);
1794 }
1795
1796
1797 /**
1798  * Assemble a migration stop message for transmission.
1799  *
1800  * @param cls the 'struct GSF_ConnectedPeer' to use
1801  * @param size number of bytes we're allowed to write to buf
1802  * @param buf where to copy the message
1803  * @return number of bytes copied to buf
1804  */
1805 static size_t
1806 create_migration_stop_message (void *cls,
1807                                size_t size,
1808                                void *buf)
1809 {
1810   struct GSF_ConnectedPeer *cp = cls;
1811   struct MigrationStopMessage msm;
1812
1813   cp->migration_pth = NULL;
1814   if (NULL == buf)
1815     return 0;
1816   GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
1817   msm.header.size = htons (sizeof (struct MigrationStopMessage));
1818   msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
1819   msm.reserved = htonl (0);
1820   msm.duration = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block));
1821   memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
1822   return sizeof (struct MigrationStopMessage);
1823 }
1824
1825
1826 /**
1827  * Ask a peer to stop migrating data to us until the given point
1828  * in time.
1829  * 
1830  * @param cp peer to ask
1831  * @param block_time until when to block
1832  */
1833 void
1834 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
1835                            struct GNUNET_TIME_Relative block_time)
1836 {
1837   if (GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value > block_time.rel_value)
1838     {
1839 #if DEBUG_FS && 0
1840       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841           "Migration already blocked for another %llu ms\n",
1842                   (unsigned long long) GNUNET_TIME_absolute_get_remaining (cp->last_migration_block).rel_value);
1843 #endif
1844       return; /* already blocked */
1845     }
1846 #if DEBUG_FS && 0
1847   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1848               "Asking to stop migration for %llu ms\n",
1849               (unsigned long long) block_time.rel_value);
1850 #endif
1851   cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
1852   if (cp->migration_pth != NULL)
1853     GSF_peer_transmit_cancel_ (cp->migration_pth);
1854   cp->migration_pth 
1855     = GSF_peer_transmit_ (cp,
1856                           GNUNET_SYSERR,
1857                           UINT32_MAX,
1858                           GNUNET_TIME_UNIT_FOREVER_REL,
1859                           sizeof (struct MigrationStopMessage),
1860                           &create_migration_stop_message,
1861                           cp);
1862 }
1863
1864
1865 /**
1866  * Write host-trust information to a file - flush the buffer entry!
1867  *
1868  * @param cls closure, not used
1869  * @param key host identity
1870  * @param value the 'struct GSF_ConnectedPeer' to flush
1871  * @return GNUNET_OK to continue iteration
1872  */
1873 static int
1874 flush_trust (void *cls,
1875              const GNUNET_HashCode *key,
1876              void *value)
1877 {
1878   struct GSF_ConnectedPeer *cp = value;
1879   char *fn;
1880   uint32_t trust;
1881   struct GNUNET_PeerIdentity pid;
1882
1883   if (cp->ppd.trust == cp->disk_trust)
1884     return GNUNET_OK;                     /* unchanged */
1885   GNUNET_PEER_resolve (cp->ppd.pid,
1886                        &pid);
1887   fn = get_trust_filename (&pid);
1888   if (cp->ppd.trust == 0)
1889     {
1890       if ((0 != UNLINK (fn)) && (errno != ENOENT))
1891         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1892                                   GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1893     }
1894   else
1895     {
1896       trust = htonl (cp->ppd.trust);
1897       if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust, 
1898                                                     sizeof(uint32_t),
1899                                                     GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1900                                                     | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1901         cp->disk_trust = cp->ppd.trust;
1902     }
1903   GNUNET_free (fn);
1904   return GNUNET_OK;
1905 }
1906
1907
1908 /**
1909  * Notify core about a preference we have for the given peer
1910  * (to allocate more resources towards it).  The change will
1911  * be communicated the next time we reserve bandwidth with
1912  * core (not instantly).
1913  *
1914  * @param cp peer to reserve bandwidth from
1915  * @param pref preference change
1916  */
1917 void
1918 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
1919                                        uint64_t pref)
1920 {
1921   cp->inc_preference += pref;
1922 }
1923
1924
1925 /**
1926  * Call this method periodically to flush trust information to disk.
1927  *
1928  * @param cls closure, not used
1929  * @param tc task context, not used
1930  */
1931 static void
1932 cron_flush_trust (void *cls,
1933                   const struct GNUNET_SCHEDULER_TaskContext *tc)
1934 {
1935
1936   if (NULL == cp_map)
1937     return;
1938   GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1939                                          &flush_trust,
1940                                          NULL);
1941   if (NULL == tc)
1942     return;
1943   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1944     return;
1945   GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, 
1946                                 &cron_flush_trust, 
1947                                 NULL);
1948 }
1949
1950
1951 /**
1952  * Initialize peer management subsystem.
1953  */
1954 void
1955 GSF_connected_peer_init_ ()
1956 {
1957   cp_map = GNUNET_CONTAINER_multihashmap_create (128);
1958   GNUNET_assert (GNUNET_OK ==
1959                  GNUNET_CONFIGURATION_get_value_filename (GSF_cfg,
1960                                                           "fs",
1961                                                           "TRUST",
1962                                                           &trustDirectory));
1963   GNUNET_DISK_directory_create (trustDirectory);
1964   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
1965                                       &cron_flush_trust, NULL);
1966 }
1967
1968
1969 /**
1970  * Iterator to free peer entries.
1971  *
1972  * @param cls closure, unused
1973  * @param key current key code
1974  * @param value value in the hash map (peer entry)
1975  * @return GNUNET_YES (we should continue to iterate)
1976  */
1977 static int 
1978 clean_peer (void *cls,
1979             const GNUNET_HashCode * key,
1980             void *value)
1981 {
1982   GSF_peer_disconnect_handler_ (NULL, 
1983                                 (const struct GNUNET_PeerIdentity*) key);
1984   return GNUNET_YES;
1985 }
1986
1987
1988 /**
1989  * Shutdown peer management subsystem.
1990  */
1991 void
1992 GSF_connected_peer_done_ ()
1993 {
1994   cron_flush_trust (NULL, NULL);
1995   GNUNET_CONTAINER_multihashmap_iterate (cp_map,
1996                                          &clean_peer,
1997                                          NULL);
1998   GNUNET_CONTAINER_multihashmap_destroy (cp_map);
1999   cp_map = NULL;
2000   GNUNET_free (trustDirectory);
2001   trustDirectory = NULL;
2002 }
2003
2004
2005 /**
2006  * Iterator to remove references to LC entry.
2007  *
2008  * @param cls the 'struct GSF_LocalClient*' to look for
2009  * @param key current key code
2010  * @param value value in the hash map (peer entry)
2011  * @return GNUNET_YES (we should continue to iterate)
2012  */
2013 static int 
2014 clean_local_client (void *cls,
2015                     const GNUNET_HashCode * key,
2016                     void *value)
2017 {
2018   const struct GSF_LocalClient *lc = cls;
2019   struct GSF_ConnectedPeer *cp = value;
2020   unsigned int i;
2021
2022   for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2023     if (cp->ppd.last_client_replies[i] == lc)
2024       cp->ppd.last_client_replies[i] = NULL;
2025   return GNUNET_YES;
2026 }
2027
2028
2029 /**
2030  * Notification that a local client disconnected.  Clean up all of our
2031  * references to the given handle.
2032  *
2033  * @param lc handle to the local client (henceforth invalid)
2034  */
2035 void
2036 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
2037 {
2038   if (NULL == cp_map)
2039     return; /* already cleaned up */
2040   GNUNET_CONTAINER_multihashmap_iterate (cp_map,
2041                                          &clean_local_client,
2042                                          (void*) lc);
2043 }
2044
2045
2046 /* end of gnunet-service-fs_cp.c */