flush peer respect value on disconnect
[oweals/gnunet.git] / src / fs / gnunet-service-fs.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009-2014 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.c
23  * @brief gnunet anonymity protocol implementation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include <float.h>
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_datastore_service.h"
32 #include "gnunet_load_lib.h"
33 #include "gnunet_peer_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet-service-fs_cp.h"
40 #include "gnunet-service-fs_indexing.h"
41 #include "gnunet-service-fs_lc.h"
42 #include "gnunet-service-fs_pe.h"
43 #include "gnunet-service-fs_pr.h"
44 #include "gnunet-service-fs_push.h"
45 #include "gnunet-service-fs_put.h"
46 #include "gnunet-service-fs_cadet.h"
47 #include "fs.h"
48 #include "fs_api.h"
49
50 /**
51  * Size for the hash map for DHT requests from the FS
52  * service.  Should be about the number of concurrent
53  * DHT requests we plan to make.
54  */
55 #define FS_DHT_HT_SIZE 1024
56
57
58 /**
59  * How quickly do we age cover traffic?  At the given
60  * time interval, remaining cover traffic counters are
61  * decremented by 1/16th.
62  */
63 #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
64
65 /**
66  * Collect an instane number of statistics?  May cause excessive IPC.
67  */
68 #define INSANE_STATISTICS GNUNET_NO
69
70
71 /* ****************************** globals ****************************** */
72
73 /**
74  * Our connection to the datastore.
75  */
76 struct GNUNET_DATASTORE_Handle *GSF_dsh;
77
78 /**
79  * Our configuration.
80  */
81 const struct GNUNET_CONFIGURATION_Handle *GSF_cfg;
82
83 /**
84  * Handle for reporting statistics.
85  */
86 struct GNUNET_STATISTICS_Handle *GSF_stats;
87
88 /**
89  * Handle for DHT operations.
90  */
91 struct GNUNET_DHT_Handle *GSF_dht;
92
93 /**
94  * How long do requests typically stay in the routing table?
95  */
96 struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime;
97
98 /**
99  * Running average of the observed latency to other peers (round trip).
100  * Initialized to 5s as the initial default.
101  */
102 struct GNUNET_TIME_Relative GSF_avg_latency = { 500 };
103
104 /**
105  * Handle to ATS service.
106  */
107 struct GNUNET_ATS_PerformanceHandle *GSF_ats;
108
109
110 /**
111  * Typical priorities we're seeing from other peers right now.  Since
112  * most priorities will be zero, this value is the weighted average of
113  * non-zero priorities seen "recently".  In order to ensure that new
114  * values do not dramatically change the ratio, values are first
115  * "capped" to a reasonable range (+N of the current value) and then
116  * averaged into the existing value by a ratio of 1:N.  Hence
117  * receiving the largest possible priority can still only raise our
118  * "current_priorities" by at most 1.
119  */
120 double GSF_current_priorities;
121
122 /**
123  * Size of the datastore queue we assume for common requests.
124  */
125 unsigned int GSF_datastore_queue_size;
126
127 /**
128  * How many query messages have we received 'recently' that
129  * have not yet been claimed as cover traffic?
130  */
131 unsigned int GSF_cover_query_count;
132
133 /**
134  * How many content messages have we received 'recently' that
135  * have not yet been claimed as cover traffic?
136  */
137 unsigned int GSF_cover_content_count;
138
139 /**
140  * Our block context.
141  */
142 struct GNUNET_BLOCK_Context *GSF_block_ctx;
143
144 /**
145  * Pointer to handle to the core service (points to NULL until we've
146  * connected to it).
147  */
148 struct GNUNET_CORE_Handle *GSF_core;
149
150 /**
151  * Are we introducing randomized delays for better anonymity?
152  */
153 int GSF_enable_randomized_delays;
154
155 /* ***************************** locals ******************************* */
156
157 /**
158  * Configuration for block library.
159  */
160 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
161
162 /**
163  * Private key of this peer.  Used to sign LOC URI requests.
164  */
165 static struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
166
167 /**
168  * ID of our task that we use to age the cover counters.
169  */
170 static GNUNET_SCHEDULER_TaskIdentifier cover_age_task;
171
172 /**
173  * Datastore 'GET' load tracking.
174  */
175 static struct GNUNET_LOAD_Value *datastore_get_load;
176
177 /**
178  * Identity of this peer.
179  */
180 static struct GNUNET_PeerIdentity my_id;
181
182
183 /**
184  * Task that periodically ages our cover traffic statistics.
185  *
186  * @param cls unused closure
187  * @param tc task context
188  */
189 static void
190 age_cover_counters (void *cls,
191                     const struct GNUNET_SCHEDULER_TaskContext *tc)
192 {
193   GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
194   GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
195   cover_age_task =
196       GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
197                                     NULL);
198 }
199
200
201 /**
202  * We've just now completed a datastore request.  Update our
203  * datastore load calculations.
204  *
205  * @param start time when the datastore request was issued
206  */
207 void
208 GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
209 {
210   struct GNUNET_TIME_Relative delay;
211
212   delay = GNUNET_TIME_absolute_get_duration (start);
213   GNUNET_LOAD_update (datastore_get_load, delay.rel_value_us);
214 }
215
216
217 /**
218  * Test if the DATABASE (GET) load on this peer is too high
219  * to even consider processing the query at
220  * all.
221  *
222  * @param priority priority of the request (used as a reference point to compare with the load)
223  * @return #GNUNET_YES if the load is too high to do anything (load high)
224  *         #GNUNET_NO to process normally (load normal)
225  *         #GNUNET_SYSERR to process for free (load low)
226  */
227 int
228 GSF_test_get_load_too_high_ (uint32_t priority)
229 {
230   double ld;
231
232   ld = GNUNET_LOAD_get_load (datastore_get_load);
233   if (ld < 1)
234     return GNUNET_SYSERR;
235   if (ld <= priority)
236     return GNUNET_NO;
237   return GNUNET_YES;
238 }
239
240
241 /**
242  * We've received peer performance information. Update
243  * our running average for the P2P latency.
244 *
245  * @param cls closure
246  * @param address the address
247  * @param active is this address in active use
248  * @param bandwidth_out assigned outbound bandwidth for the connection
249  * @param bandwidth_in assigned inbound bandwidth for the connection
250  * @param ats performance data for the address (as far as known)
251  * @param ats_count number of performance records in @a ats
252  */
253 static void
254 update_latencies (void *cls,
255                   const struct GNUNET_HELLO_Address *address,
256                   int active,
257                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
258                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
259                   const struct GNUNET_ATS_Information *ats,
260                   uint32_t ats_count)
261 {
262   unsigned int i;
263   struct GNUNET_TIME_Relative latency;
264
265   if (NULL == address)
266   {
267     /* ATS service temporarily disconnected */
268     return;
269   }
270
271   if (GNUNET_YES != active)
272     return;
273   for (i = 0; i < ats_count; i++)
274   {
275     if (GNUNET_ATS_QUALITY_NET_DELAY != ntohl (ats[i].type))
276       continue;
277     latency.rel_value_us = ntohl (ats[i].value);
278     GSF_update_peer_latency_ (&address->peer,
279                               latency);
280     GSF_avg_latency.rel_value_us =
281       (GSF_avg_latency.rel_value_us * 31 +
282        GNUNET_MIN (5000, ntohl (ats[i].value))) / 32;
283     GNUNET_STATISTICS_set (GSF_stats,
284                            gettext_noop
285                            ("# running average P2P latency (ms)"),
286                            GSF_avg_latency.rel_value_us / 1000LL, GNUNET_NO);
287     break;
288   }
289 }
290
291
292 /**
293  * Handle P2P "PUT" message.
294  *
295  * @param cls closure, always NULL
296  * @param other the other peer involved (sender or receiver, NULL
297  *        for loopback messages where we are both sender and receiver)
298  * @param message the actual message
299  * @return #GNUNET_OK to keep the connection open,
300  *         #GNUNET_SYSERR to close it (signal serious error)
301  */
302 static int
303 handle_p2p_put (void *cls,
304                 const struct GNUNET_PeerIdentity *other,
305                 const struct GNUNET_MessageHeader *message)
306 {
307   struct GSF_ConnectedPeer *cp;
308
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310               "Received P2P PUT from %s\n",
311               GNUNET_i2s (other));
312   cp = GSF_peer_get_ (other);
313   if (NULL == cp)
314   {
315     GNUNET_break (0);
316     return GNUNET_OK;
317   }
318   GSF_cover_content_count++;
319   return GSF_handle_p2p_content_ (cp, message);
320 }
321
322
323 /**
324  * We have a new request, consider forwarding it to the given
325  * peer.
326  *
327  * @param cls the `struct GSF_PendingRequest`
328  * @param peer identity of the peer
329  * @param cp handle to the connected peer record
330  * @param ppd peer performance data
331  */
332 static void
333 consider_request_for_forwarding (void *cls,
334                                  const struct GNUNET_PeerIdentity *peer,
335                                  struct GSF_ConnectedPeer *cp,
336                                  const struct GSF_PeerPerformanceData *ppd)
337 {
338   struct GSF_PendingRequest *pr = cls;
339
340   if (GNUNET_YES != GSF_pending_request_test_target_ (pr, peer))
341   {
342 #if INSANE_STATISTICS
343     GNUNET_STATISTICS_update (GSF_stats,
344                               gettext_noop ("# Loopback routes suppressed"), 1,
345                               GNUNET_NO);
346 #endif
347     return;
348   }
349   GSF_plan_add_ (cp, pr);
350 }
351
352
353 /**
354  * Function to be called after we're done processing
355  * replies from the local lookup.  If the result status
356  * code indicates that there may be more replies, plan
357  * forwarding the request.
358  *
359  * @param cls closure (NULL)
360  * @param pr the pending request we were processing
361  * @param result final datastore lookup result
362  */
363 static void
364 consider_forwarding (void *cls,
365                      struct GSF_PendingRequest *pr,
366                      enum GNUNET_BLOCK_EvaluationResult result)
367 {
368   if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
369     return;                     /* we're done... */
370   GSF_iterate_connected_peers_ (&consider_request_for_forwarding, pr);
371 }
372
373
374 /**
375  * Handle P2P "GET" request.
376  *
377  * @param cls closure, always NULL
378  * @param other the other peer involved (sender or receiver, NULL
379  *        for loopback messages where we are both sender and receiver)
380  * @param message the actual message
381  * @return #GNUNET_OK to keep the connection open,
382  *         #GNUNET_SYSERR to close it (signal serious error)
383  */
384 static int
385 handle_p2p_get (void *cls,
386                 const struct GNUNET_PeerIdentity *other,
387                 const struct GNUNET_MessageHeader *message)
388 {
389   struct GSF_PendingRequest *pr;
390
391   pr = GSF_handle_p2p_query_ (other, message);
392   if (NULL == pr)
393     return GNUNET_SYSERR;
394   GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
395   GSF_local_lookup_ (pr,
396                      &consider_forwarding, NULL);
397   return GNUNET_OK;
398 }
399
400
401 /**
402  * We're done with the local lookup, now consider
403  * P2P processing (depending on request options and
404  * result status).  Also signal that we can now
405  * receive more request information from the client.
406  *
407  * @param cls the client doing the request (`struct GNUNET_SERVER_Client`)
408  * @param pr the pending request we were processing
409  * @param result final datastore lookup result
410  */
411 static void
412 start_p2p_processing (void *cls,
413                       struct GSF_PendingRequest *pr,
414                       enum GNUNET_BLOCK_EvaluationResult result)
415 {
416   struct GNUNET_SERVER_Client *client = cls;
417   struct GSF_PendingRequestData *prd;
418
419   GNUNET_SERVER_receive_done (client,
420                               GNUNET_OK);
421   if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
422     return;                     /* we're done, 'pr' was already destroyed... */
423   prd = GSF_pending_request_get_data_ (pr);
424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425               "Finished database lookup for local request `%s' with result %d\n",
426               GNUNET_h2s (&prd->query), result);
427   if (0 == prd->anonymity_level)
428   {
429     switch (prd->type)
430     {
431     case GNUNET_BLOCK_TYPE_FS_DBLOCK:
432     case GNUNET_BLOCK_TYPE_FS_IBLOCK:
433       /* the above block types MAY be available via 'cadet' */
434       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435                   "Considering cadet-based download for block\n");
436       GSF_cadet_lookup_ (pr);
437       break;
438     case GNUNET_BLOCK_TYPE_FS_UBLOCK:
439       /* the above block types are in the DHT */
440       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441                   "Considering DHT-based search for block\n");
442       GSF_dht_lookup_ (pr);
443       break;
444     default:
445       GNUNET_break (0);
446       break;
447     }
448   }
449   consider_forwarding (NULL, pr, result);
450 }
451
452
453 /**
454  * Handle START_SEARCH-message (search request from client).
455  *
456  * @param cls closure
457  * @param client identification of the client
458  * @param message the actual message
459  */
460 static void
461 handle_start_search (void *cls,
462                      struct GNUNET_SERVER_Client *client,
463                      const struct GNUNET_MessageHeader *message)
464 {
465   struct GSF_PendingRequest *pr;
466   int ret;
467
468   pr = NULL;
469   ret = GSF_local_client_start_search_handler_ (client,
470                                                 message,
471                                                 &pr);
472   switch (ret)
473   {
474   case GNUNET_SYSERR:
475     GNUNET_SERVER_receive_done (client,
476                                 GNUNET_SYSERR);
477     break;
478   case GNUNET_NO:
479     GNUNET_SERVER_receive_done (client,
480                                 GNUNET_OK);
481     break;
482   case GNUNET_YES:
483     GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
484     GSF_local_lookup_ (pr,
485                        &start_p2p_processing,
486                        client);
487     break;
488   default:
489     GNUNET_assert (0);
490   }
491 }
492
493
494 /**
495  * Handle request to sign a LOC URI (from client).
496  *
497  * @param cls closure (NULL)
498  * @param client identification of the client
499  * @param message the actual message
500  */
501 static void
502 handle_loc_sign (void *cls,
503                  struct GNUNET_SERVER_Client *client,
504                  const struct GNUNET_MessageHeader *message)
505 {
506   const struct RequestLocSignatureMessage *msg;
507   struct GNUNET_FS_Uri base;
508   struct GNUNET_FS_Uri *loc;
509   struct ResponseLocSignatureMessage resp;
510   struct GSF_LocalClient *lc;
511
512   msg = (const struct RequestLocSignatureMessage *) message;
513   GNUNET_break (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT ==
514                 ntohl (msg->purpose));
515   base.type = GNUNET_FS_URI_CHK;
516   base.data.chk.chk = msg->chk;
517   base.data.chk.file_length = GNUNET_ntohll (msg->file_length);
518   loc = GNUNET_FS_uri_loc_create (&base,
519                                   pk,
520                                   GNUNET_TIME_absolute_ntoh (msg->expiration_time));
521   resp.header.size = htons (sizeof (struct ResponseLocSignatureMessage));
522   resp.header.type = htons (GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGNATURE);
523   resp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
524   resp.expiration_time = GNUNET_TIME_absolute_hton (loc->data.loc.expirationTime);
525   resp.signature = loc->data.loc.contentSignature;
526   resp.peer = loc->data.loc.peer;
527   GNUNET_FS_uri_destroy (loc);
528   lc = GSF_local_client_lookup_ (client);
529   GSF_local_client_transmit_ (lc,
530                               &resp.header);
531   GNUNET_SERVER_receive_done (client, GNUNET_OK);
532 }
533
534
535 /**
536  * Task run during shutdown.
537  *
538  * @param cls unused
539  * @param tc unused
540  */
541 static void
542 shutdown_task (void *cls,
543                const struct GNUNET_SCHEDULER_TaskContext *tc)
544 {
545   GSF_cadet_stop_client ();
546   GSF_cadet_stop_server ();
547   if (NULL != GSF_core)
548   {
549     GNUNET_CORE_disconnect (GSF_core);
550     GSF_core = NULL;
551   }
552   if (NULL != GSF_ats)
553   {
554     GNUNET_ATS_performance_done (GSF_ats);
555     GSF_ats = NULL;
556   }
557   GSF_put_done_ ();
558   GSF_push_done_ ();
559   GSF_pending_request_done_ ();
560   GSF_plan_done ();
561   GSF_connected_peer_done_ ();
562   GNUNET_DATASTORE_disconnect (GSF_dsh, GNUNET_NO);
563   GSF_dsh = NULL;
564   GNUNET_DHT_disconnect (GSF_dht);
565   GSF_dht = NULL;
566   GNUNET_BLOCK_context_destroy (GSF_block_ctx);
567   GSF_block_ctx = NULL;
568   GNUNET_CONFIGURATION_destroy (block_cfg);
569   block_cfg = NULL;
570   GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
571   GSF_stats = NULL;
572   if (GNUNET_SCHEDULER_NO_TASK != cover_age_task)
573   {
574     GNUNET_SCHEDULER_cancel (cover_age_task);
575     cover_age_task = GNUNET_SCHEDULER_NO_TASK;
576   }
577   GNUNET_FS_indexing_done ();
578   GNUNET_LOAD_value_free (datastore_get_load);
579   datastore_get_load = NULL;
580   GNUNET_LOAD_value_free (GSF_rt_entry_lifetime);
581   GSF_rt_entry_lifetime = NULL;
582 }
583
584
585 /**
586  * Function called for each pending request whenever a new
587  * peer connects, giving us a chance to decide about submitting
588  * the existing request to the new peer.
589  *
590  * @param cls the 'struct GSF_ConnectedPeer' of the new peer
591  * @param key query for the request
592  * @param pr handle to the pending request
593  * @return #GNUNET_YES to continue to iterate
594  */
595 static int
596 consider_peer_for_forwarding (void *cls,
597                               const struct GNUNET_HashCode *key,
598                               struct GSF_PendingRequest *pr)
599 {
600   struct GSF_ConnectedPeer *cp = cls;
601   struct GNUNET_PeerIdentity pid;
602
603   GSF_connected_peer_get_identity_ (cp, &pid);
604   if (GNUNET_YES != GSF_pending_request_test_target_ (pr, &pid))
605   {
606     GNUNET_STATISTICS_update (GSF_stats,
607                               gettext_noop ("# Loopback routes suppressed"), 1,
608                               GNUNET_NO);
609     return GNUNET_YES;
610   }
611   GSF_plan_add_ (cp, pr);
612   return GNUNET_YES;
613 }
614
615
616 /**
617  * Function called after the creation of a connected peer record is complete.
618  *
619  * @param cls closure (unused)
620  * @param cp handle to the newly created connected peer record
621  */
622 static void
623 connected_peer_cb (void *cls, struct GSF_ConnectedPeer *cp)
624 {
625   if (NULL == cp)
626     return;
627   GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, cp);
628 }
629
630
631 /**
632  * Method called whenever a given peer connects.
633  *
634  * @param cls closure, not used
635  * @param peer peer identity this notification is about
636  */
637 static void
638 peer_connect_handler (void *cls,
639                       const struct GNUNET_PeerIdentity *peer)
640 {
641   if (0 ==
642       GNUNET_CRYPTO_cmp_peer_identity (&my_id,
643                                        peer))
644     return;
645   GSF_peer_connect_handler_ (peer,
646                              &connected_peer_cb,
647                              NULL);
648 }
649
650
651 /**
652  * Function called after GNUNET_CORE_connect has succeeded
653  * (or failed for good).  Note that the private key of the
654  * peer is intentionally not exposed here; if you need it,
655  * your process should try to read the private key file
656  * directly (which should work if you are authorized...).
657  *
658  * @param cls closure
659  * @param my_identity ID of this peer, NULL if we failed
660  */
661 static void
662 peer_init_handler (void *cls,
663                    const struct GNUNET_PeerIdentity *my_identity)
664 {
665   if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_id,
666                                             my_identity))
667   {
668     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
669                 "Peer identity missmatch, refusing to start!\n");
670     GNUNET_SCHEDULER_shutdown ();
671   }
672 }
673
674
675 /**
676  * Process fs requests.
677  *
678  * @param server the initialized server
679  * @param c configuration to use
680  */
681 static int
682 main_init (struct GNUNET_SERVER_Handle *server,
683            const struct GNUNET_CONFIGURATION_Handle *c)
684 {
685   static const struct GNUNET_CORE_MessageHandler no_p2p_handlers[] = {
686     { NULL, 0, 0 }
687   };
688   static const struct GNUNET_CORE_MessageHandler p2p_handlers[] = {
689     { &handle_p2p_get,
690       GNUNET_MESSAGE_TYPE_FS_GET, 0 },
691     { &handle_p2p_put,
692       GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
693     { &GSF_handle_p2p_migration_stop_,
694       GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
695       sizeof (struct MigrationStopMessage) },
696     { NULL, 0, 0 }
697   };
698   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
699     { &GNUNET_FS_handle_index_start, NULL,
700       GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0 },
701     { &GNUNET_FS_handle_index_list_get, NULL,
702       GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET,
703       sizeof (struct GNUNET_MessageHeader) },
704     { &GNUNET_FS_handle_unindex, NULL,
705       GNUNET_MESSAGE_TYPE_FS_UNINDEX,
706       sizeof (struct UnindexMessage) },
707     { &handle_start_search, NULL,
708       GNUNET_MESSAGE_TYPE_FS_START_SEARCH, 0 },
709     { &handle_loc_sign, NULL,
710       GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGN,
711       sizeof (struct RequestLocSignatureMessage) },
712     {NULL, NULL, 0, 0}
713   };
714   int anon_p2p_off;
715   char *keyfile;
716
717   /* this option is really only for testcases that need to disable
718      _anonymous_ file-sharing for some reason */
719   anon_p2p_off = (GNUNET_YES ==
720                   GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
721                                                         "fs",
722                                                         "DISABLE_ANON_TRANSFER"));
723
724   if (GNUNET_OK !=
725       GNUNET_CONFIGURATION_get_value_filename (GSF_cfg,
726                                                "PEER",
727                                                "PRIVATE_KEY",
728                                                &keyfile))
729   {
730     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731                 _("FS service is lacking HOSTKEY configuration setting.  Exiting.\n"));
732     GNUNET_SCHEDULER_shutdown ();
733     return GNUNET_SYSERR;
734   }
735   pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
736   GNUNET_free (keyfile);
737   GNUNET_assert (NULL != pk);
738   GNUNET_CRYPTO_eddsa_key_get_public (pk,
739                                       &my_id.public_key);
740
741   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
742               "I am peer %s\n",
743               GNUNET_i2s (&my_id));
744   GSF_core
745     = GNUNET_CORE_connect (GSF_cfg, NULL,
746                            &peer_init_handler,
747                            &peer_connect_handler,
748                            &GSF_peer_disconnect_handler_,
749                            NULL, GNUNET_NO,
750                            NULL, GNUNET_NO,
751                            (GNUNET_YES == anon_p2p_off)
752                            ? no_p2p_handlers
753                            : p2p_handlers);
754   if (NULL == GSF_core)
755   {
756     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757                 _("Failed to connect to `%s' service.\n"), "core");
758     return GNUNET_SYSERR;
759   }
760   GNUNET_SERVER_disconnect_notify (server, &GSF_client_disconnect_handler_,
761                                    NULL);
762   GNUNET_SERVER_add_handlers (server, handlers);
763   cover_age_task =
764       GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
765                                     NULL);
766   datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
767   GSF_cadet_start_server ();
768   GSF_cadet_start_client ();
769   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
770                                 NULL);
771   return GNUNET_OK;
772 }
773
774
775 /**
776  * Process fs requests.
777  *
778  * @param cls closure
779  * @param server the initialized server
780  * @param cfg configuration to use
781  */
782 static void
783 run (void *cls, struct GNUNET_SERVER_Handle *server,
784      const struct GNUNET_CONFIGURATION_Handle *cfg)
785 {
786   unsigned long long dqs;
787
788   GSF_cfg = cfg;
789   if (GNUNET_OK !=
790       GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "fs", "DATASTORE_QUEUE_SIZE",
791                                            &dqs))
792   {
793     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
794                                "fs", "DATASTORE_QUEUE_SIZE");
795     dqs = 1024;
796   }
797   GSF_datastore_queue_size = (unsigned int) dqs;
798   GSF_enable_randomized_delays =
799       GNUNET_CONFIGURATION_get_value_yesno (cfg, "fs", "DELAY");
800   GSF_dsh = GNUNET_DATASTORE_connect (cfg);
801   if (NULL == GSF_dsh)
802   {
803     GNUNET_SCHEDULER_shutdown ();
804     return;
805   }
806   GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
807   GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
808   block_cfg = GNUNET_CONFIGURATION_create ();
809   GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
810   GNUNET_assert (NULL != GSF_block_ctx);
811   GSF_dht = GNUNET_DHT_connect (cfg, FS_DHT_HT_SIZE);
812   GSF_plan_init ();
813   GSF_pending_request_init_ ();
814   GSF_connected_peer_init_ ();
815   GSF_ats = GNUNET_ATS_performance_init (GSF_cfg, &update_latencies, NULL);
816   GSF_push_init_ ();
817   GSF_put_init_ ();
818   if ((GNUNET_OK != GNUNET_FS_indexing_init (cfg, GSF_dsh)) ||
819       (GNUNET_OK != main_init (server, cfg)))
820   {
821     GNUNET_SCHEDULER_shutdown ();
822     shutdown_task (NULL, NULL);
823     return;
824   }
825 }
826
827
828 /**
829  * The main function for the fs service.
830  *
831  * @param argc number of arguments from the command line
832  * @param argv command line arguments
833  * @return 0 ok, 1 on error
834  */
835 int
836 main (int argc, char *const *argv)
837 {
838   return (GNUNET_OK ==
839           GNUNET_SERVICE_run (argc, argv, "fs", GNUNET_SERVICE_OPTION_NONE,
840                               &run, NULL)) ? 0 : 1;
841 }
842
843 /* end of gnunet-service-fs.c */