90ab6d5bf87f132dd1118797ee8bce9bc390fc95
[oweals/gnunet.git] / src / peerinfo / gnunet-service-peerinfo.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010 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 peerinfo/gnunet-service-peerinfo.c
23  * @brief maintains list of known peers
24  *
25  * Code to maintain the list of currently known hosts (in memory
26  * structure of data/hosts/).
27  *
28  * @author Christian Grothoff
29  *
30  * TODO:
31  * - HostEntries are never 'free'd (add expiration, upper bound?)
32  */
33
34 #include "platform.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_hello_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_statistics_service.h"
39 #include "peerinfo.h"
40
41 /**
42  * How often do we scan the HOST_DIR for new entries?
43  */
44 #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
45
46 /**
47  * How often do we discard old entries in data/hosts/?
48  */
49 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
50
51
52 /**
53  * In-memory cache of known hosts.
54  */
55 struct HostEntry
56 {
57
58   /**
59    * Identity of the peer.
60    */
61   struct GNUNET_PeerIdentity identity;
62
63   /**
64    * Hello for the peer (can be NULL)
65    */
66   struct GNUNET_HELLO_Message *hello;
67
68 };
69
70
71 /**
72  * The in-memory list of known hosts, mapping of
73  * host IDs to 'struct HostEntry*' values.
74  */
75 static struct GNUNET_CONTAINER_MultiHashMap *hostmap;
76
77 /**
78  * Clients to immediately notify about all changes.
79  */
80 static struct GNUNET_SERVER_NotificationContext *notify_list;
81
82 /**
83  * Directory where the hellos are stored in (data/hosts)
84  */
85 static char *networkIdDirectory;
86
87 /**
88  * Handle for reporting statistics.
89  */
90 static struct GNUNET_STATISTICS_Handle *stats;
91
92
93 /**
94  * Notify all clients in the notify list about the
95  * given host entry changing.
96  *
97  * @param he entry of the host for which we generate a notification
98  * @return generated notification message
99  */
100 static struct InfoMessage *
101 make_info_message (const struct HostEntry *he)
102 {
103   struct InfoMessage *im;
104   size_t hs;
105
106   hs = (he->hello == NULL) ? 0 : GNUNET_HELLO_size (he->hello);
107   im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
108   im->header.size = htons (hs + sizeof (struct InfoMessage));
109   im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
110   im->peer = he->identity;
111   if (he->hello != NULL)
112     memcpy (&im[1], he->hello, hs);
113   return im;
114 }
115
116
117 /**
118  * Address iterator that causes expired entries to be discarded.
119  *
120  * @param cls pointer to the current time
121  * @param address the address
122  * @param expiration expiration time for the address
123  * @return GNUNET_NO if expiration smaller than the current time
124  */
125 static int
126 discard_expired (void *cls, const struct GNUNET_HELLO_Address *address,
127                  struct GNUNET_TIME_Absolute expiration)
128 {
129   const struct GNUNET_TIME_Absolute *now = cls;
130
131   if (now->abs_value > expiration.abs_value)
132   {
133     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
134                 _("Removing expired address of transport `%s'\n"),
135                 address->transport_name);
136     return GNUNET_NO;
137   }
138   return GNUNET_OK;
139 }
140
141
142 /**
143  * Get the filename under which we would store the GNUNET_HELLO_Message
144  * for the given host and protocol.
145  *
146  * @param id peer for which we need the filename for the HELLO
147  * @return filename of the form DIRECTORY/HOSTID
148  */
149 static char *
150 get_host_filename (const struct GNUNET_PeerIdentity *id)
151 {
152   struct GNUNET_CRYPTO_HashAsciiEncoded fil;
153   char *fn;
154
155   GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
156   GNUNET_asprintf (&fn, "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
157   return fn;
158 }
159
160
161 /**
162  * Broadcast information about the given entry to all
163  * clients that care.
164  *
165  * @param entry entry to broadcast about
166  */
167 static void
168 notify_all (struct HostEntry *entry)
169 {
170   struct InfoMessage *msg;
171
172   msg = make_info_message (entry);
173   GNUNET_SERVER_notification_context_broadcast (notify_list, &msg->header,
174                                                 GNUNET_NO);
175   GNUNET_free (msg);
176 }
177
178
179 /**
180  * Add a host to the list.
181  *
182  * @param identity the identity of the host
183  */
184 static void
185 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
186 {
187   struct HostEntry *entry;
188   char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
189   const struct GNUNET_HELLO_Message *hello;
190   struct GNUNET_HELLO_Message *hello_clean;
191   int size;
192   struct GNUNET_TIME_Absolute now;
193   char *fn;
194
195   entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
196   if (entry != NULL)
197     return;
198   GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
199                             GNUNET_NO);
200   entry = GNUNET_malloc (sizeof (struct HostEntry));
201   entry->identity = *identity;
202
203   fn = get_host_filename (identity);
204   if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
205   {
206     size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
207     hello = (const struct GNUNET_HELLO_Message *) buffer;
208     if ((size < sizeof (struct GNUNET_MessageHeader)) ||
209         (size != ntohs ((((const struct GNUNET_MessageHeader *) hello)->size)))
210         || (size != GNUNET_HELLO_size (hello)))
211     {
212       GNUNET_break (0);
213       if (0 != UNLINK (fn))
214         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
215     }
216     else
217     {
218       now = GNUNET_TIME_absolute_get ();
219       hello_clean =
220           GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired,
221                                           &now);
222       entry->hello = hello_clean;
223     }
224   }
225   GNUNET_free (fn);
226   GNUNET_CONTAINER_multihashmap_put (hostmap, &identity->hashPubKey, entry,
227                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
228   notify_all (entry);
229 }
230
231
232 /**
233  * Remove a file that should not be there.  LOG
234  * success or failure.
235  *
236  * @param fullname name of the file to remove
237  */
238 static void
239 remove_garbage (const char *fullname)
240 {
241   if (0 == UNLINK (fullname))
242     GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
243                 _
244                 ("File `%s' in directory `%s' does not match naming convention. "
245                  "Removed.\n"), fullname, networkIdDirectory);
246   else
247     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
248                               "unlink", fullname);
249 }
250
251
252 /**
253  * Function that is called on each HELLO file in a particular directory.
254  * Try to parse the file and add the HELLO to our list.
255  *
256  * @param cls pointer to 'unsigned int' to increment for each file, or NULL
257  *            if the file is from a read-only, read-once resource directory
258  * @param fullname name of the file to parse
259  * @return GNUNET_OK (continue iteration)
260  */
261 static int
262 hosts_directory_scan_callback (void *cls, const char *fullname)
263 {
264   unsigned int *matched = cls;
265   struct GNUNET_PeerIdentity identity;
266   const char *filename;
267
268   if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
269     return GNUNET_OK;           /* ignore non-files */
270   if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
271   {
272     if (NULL != matched)
273       remove_garbage (fullname);
274     return GNUNET_OK;
275   }
276   filename =
277       &fullname[strlen (fullname) -
278                 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
279   if (filename[-1] != DIR_SEPARATOR)
280   {
281     if (NULL != matched)
282       remove_garbage (fullname);
283     return GNUNET_OK;
284   }
285   if (GNUNET_OK !=
286       GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
287   {
288     if (NULL != matched)
289       remove_garbage (fullname);
290     return GNUNET_OK;
291   }
292   if (NULL != matched)
293     (*matched)++;
294   add_host_to_known_hosts (&identity);
295   return GNUNET_OK;
296 }
297
298
299 /**
300  * Call this method periodically to scan data/hosts for new hosts.
301  *
302  * @param cls unused
303  * @param tc scheduler context, aborted if reason is shutdown
304  */
305 static void
306 cron_scan_directory_data_hosts (void *cls,
307                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
308 {
309   static unsigned int retries;
310   unsigned int count;
311
312   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
313     return;
314   count = 0;
315   if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
316   {
317     GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
318                                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
319                                                 &cron_scan_directory_data_hosts, NULL);
320     return;
321   }
322   GNUNET_DISK_directory_scan (networkIdDirectory,
323                               &hosts_directory_scan_callback, &count);
324   if ((0 == count) && (0 == (++retries & 31)))
325     GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
326                 _("Still no peers found in `%s'!\n"), networkIdDirectory);
327   GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ, 
328                                               GNUNET_SCHEDULER_PRIORITY_IDLE,
329                                               &cron_scan_directory_data_hosts,
330                                               NULL);
331 }
332
333
334 /**
335  * Bind a host address (hello) to a hostId.
336  *
337  * @param peer the peer for which this is a hello
338  * @param hello the verified (!) hello message
339  */
340 static void
341 bind_address (const struct GNUNET_PeerIdentity *peer,
342               const struct GNUNET_HELLO_Message *hello)
343 {
344   char *fn;
345   struct HostEntry *host;
346   struct GNUNET_HELLO_Message *mrg;
347   struct GNUNET_TIME_Absolute delta;
348
349   add_host_to_known_hosts (peer);
350   host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
351   GNUNET_assert (host != NULL);
352   if (host->hello == NULL)
353   {
354     host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
355     memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
356   }
357   else
358   {
359     mrg = GNUNET_HELLO_merge (host->hello, hello);
360     delta = GNUNET_HELLO_equals (mrg, host->hello, GNUNET_TIME_absolute_get ());
361     if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
362     {
363       GNUNET_free (mrg);
364       return;
365     }
366     GNUNET_free (host->hello);
367     host->hello = mrg;
368   }
369   fn = get_host_filename (peer);
370   if (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn))
371   {
372     if (GNUNET_SYSERR ==
373         GNUNET_DISK_fn_write (fn, host->hello, GNUNET_HELLO_size (host->hello),
374                               GNUNET_DISK_PERM_USER_READ |
375                               GNUNET_DISK_PERM_USER_WRITE |
376                               GNUNET_DISK_PERM_GROUP_READ |
377                               GNUNET_DISK_PERM_OTHER_READ))
378       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
379
380   }
381   GNUNET_free (fn);
382   notify_all (host);
383 }
384
385
386 /**
387  * Do transmit info about peer to given host.
388  *
389  * @param cls NULL to hit all hosts, otherwise specifies a particular target
390  * @param key hostID
391  * @param value information to transmit
392  * @return GNUNET_YES (continue to iterate)
393  */
394 static int
395 add_to_tc (void *cls, const GNUNET_HashCode * key, void *value)
396 {
397   struct GNUNET_SERVER_TransmitContext *tc = cls;
398   struct HostEntry *pos = value;
399   struct InfoMessage *im;
400   uint16_t hs;
401   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
402
403   hs = 0;
404   im = (struct InfoMessage *) buf;
405   if (pos->hello != NULL)
406   {
407     hs = GNUNET_HELLO_size (pos->hello);
408     GNUNET_assert (hs <
409                    GNUNET_SERVER_MAX_MESSAGE_SIZE -
410                    sizeof (struct InfoMessage));
411     memcpy (&im[1], pos->hello, hs);
412   }
413   im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
414   im->header.size = htons (sizeof (struct InfoMessage) + hs);
415   im->reserved = htonl (0);
416   im->peer = pos->identity;
417   GNUNET_SERVER_transmit_context_append_message (tc, &im->header);
418   return GNUNET_YES;
419 }
420
421
422 /**
423  * @brief delete expired HELLO entries in data/hosts/
424  *
425  * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
426  * @param fn filename to test to see if the HELLO expired
427  * @return GNUNET_OK (continue iteration)
428  */
429 static int
430 discard_hosts_helper (void *cls, const char *fn)
431 {
432   struct GNUNET_TIME_Absolute *now = cls;
433   char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
434   const struct GNUNET_HELLO_Message *hello;
435   struct GNUNET_HELLO_Message *new_hello;
436   int size;
437
438   size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
439   if (size < sizeof (struct GNUNET_MessageHeader))
440   {
441     if (0 != UNLINK (fn))
442       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
443                                 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
444     return GNUNET_OK;
445   }
446   hello = (const struct GNUNET_HELLO_Message *) buffer;
447   new_hello =
448       GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
449   if (new_hello != NULL)
450   {
451     GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
452                           GNUNET_DISK_PERM_USER_READ |
453                           GNUNET_DISK_PERM_USER_WRITE |
454                           GNUNET_DISK_PERM_GROUP_READ |
455                           GNUNET_DISK_PERM_OTHER_READ);
456     GNUNET_free (new_hello);
457   }
458   else
459   {
460     if (0 != UNLINK (fn))
461       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
462                                 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
463   }
464   return GNUNET_OK;
465 }
466
467
468 /**
469  * Call this method periodically to scan data/hosts for ancient
470  * HELLOs to expire.
471  *
472  * @param cls unused
473  * @param tc scheduler context, aborted if reason is shutdown
474  */
475 static void
476 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
477 {
478   struct GNUNET_TIME_Absolute now;
479
480   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
481     return;
482   now = GNUNET_TIME_absolute_get ();
483   GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
484   GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
485                                 NULL);
486 }
487
488
489 /**
490  * Handle HELLO-message.
491  *
492  * @param cls closure
493  * @param client identification of the client
494  * @param message the actual message
495  */
496 static void
497 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
498               const struct GNUNET_MessageHeader *message)
499 {
500   const struct GNUNET_HELLO_Message *hello;
501   struct GNUNET_PeerIdentity pid;
502
503   hello = (const struct GNUNET_HELLO_Message *) message;
504   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
505   {
506     GNUNET_break (0);
507     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
508     return;
509   }
510 #if DEBUG_PEERINFO
511   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
512               "HELLO", GNUNET_i2s (&pid));
513 #endif
514   bind_address (&pid, hello);
515   GNUNET_SERVER_receive_done (client, GNUNET_OK);
516 }
517
518
519 /**
520  * Handle GET-message.
521  *
522  * @param cls closure
523  * @param client identification of the client
524  * @param message the actual message
525  */
526 static void
527 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
528             const struct GNUNET_MessageHeader *message)
529 {
530   const struct ListPeerMessage *lpm;
531   struct GNUNET_SERVER_TransmitContext *tc;
532
533   lpm = (const struct ListPeerMessage *) message;
534   GNUNET_break (0 == ntohl (lpm->reserved));
535 #if DEBUG_PEERINFO
536   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
537               "GET", GNUNET_i2s (&lpm->peer));
538 #endif
539   tc = GNUNET_SERVER_transmit_context_create (client);
540   GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
541                                               &add_to_tc, tc);
542   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
543                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
544   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
545 }
546
547
548 /**
549  * Handle GET-ALL-message.
550  *
551  * @param cls closure
552  * @param client identification of the client
553  * @param message the actual message
554  */
555 static void
556 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
557                 const struct GNUNET_MessageHeader *message)
558 {
559   struct GNUNET_SERVER_TransmitContext *tc;
560
561 #if DEBUG_PEERINFO
562   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
563 #endif
564   tc = GNUNET_SERVER_transmit_context_create (client);
565   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, tc);
566   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
567                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
568   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
569 }
570
571
572 /**
573  * FIXME.
574  */
575 static int
576 do_notify_entry (void *cls, const GNUNET_HashCode * key, void *value)
577 {
578   struct GNUNET_SERVER_Client *client = cls;
579   struct HostEntry *he = value;
580   struct InfoMessage *msg;
581
582   msg = make_info_message (he);
583   GNUNET_SERVER_notification_context_unicast (notify_list, client, &msg->header,
584                                               GNUNET_NO);
585   GNUNET_free (msg);
586   return GNUNET_YES;
587 }
588
589
590 /**
591  * Handle NOTIFY-message.
592  *
593  * @param cls closure
594  * @param client identification of the client
595  * @param message the actual message
596  */
597 static void
598 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
599                const struct GNUNET_MessageHeader *message)
600 {
601 #if DEBUG_PEERINFO
602   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
603 #endif
604   GNUNET_SERVER_notification_context_add (notify_list, client);
605   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, client);
606   GNUNET_SERVER_receive_done (client, GNUNET_OK);
607 }
608
609
610 /**
611  * FIXME.
612  */
613 static int
614 free_host_entry (void *cls, const GNUNET_HashCode * key, void *value)
615 {
616   struct HostEntry *he = value;
617
618   GNUNET_free_non_null (he->hello);
619   GNUNET_free (he);
620   return GNUNET_YES;
621 }
622
623
624 /**
625  * Clean up our state.  Called during shutdown.
626  *
627  * @param cls unused
628  * @param tc scheduler task context, unused
629  */
630 static void
631 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
632 {
633   GNUNET_SERVER_notification_context_destroy (notify_list);
634   notify_list = NULL;
635   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
636   GNUNET_CONTAINER_multihashmap_destroy (hostmap);
637   if (stats != NULL)
638   {
639     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
640     stats = NULL;
641   }
642 }
643
644
645 /**
646  * Start up peerinfo service.
647  *
648  * @param cls closure
649  * @param server the initialized server
650  * @param cfg configuration to use
651  */
652 static void
653 run (void *cls, struct GNUNET_SERVER_Handle *server,
654      const struct GNUNET_CONFIGURATION_Handle *cfg)
655 {
656   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
657     {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
658     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
659      sizeof (struct ListPeerMessage)},
660     {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
661      sizeof (struct GNUNET_MessageHeader)},
662     {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
663      sizeof (struct GNUNET_MessageHeader)},
664     {NULL, NULL, 0, 0}
665   };
666   char *peerdir;
667   char *ip;
668
669   hostmap = GNUNET_CONTAINER_multihashmap_create (1024);
670   stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
671   notify_list = GNUNET_SERVER_notification_context_create (server, 0);
672   GNUNET_assert (GNUNET_OK ==
673                  GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
674                                                           "HOSTS",
675                                                           &networkIdDirectory));
676   GNUNET_DISK_directory_create (networkIdDirectory);
677   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
678                                       &cron_scan_directory_data_hosts, NULL);
679   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
680                                       &cron_clean_data_hosts, NULL);
681   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
682                                 NULL);
683   GNUNET_SERVER_add_handlers (server, handlers);
684   ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
685   GNUNET_asprintf (&peerdir,
686                    "%shellos",
687                    ip);
688   GNUNET_free (ip);
689   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
690               _("Importing HELLOs from `%s'\n"),
691               peerdir);
692   GNUNET_DISK_directory_scan (peerdir,
693                               &hosts_directory_scan_callback, NULL);
694   GNUNET_free (peerdir);
695 }
696
697
698 /**
699  * The main function for the peerinfo service.
700  *
701  * @param argc number of arguments from the command line
702  * @param argv command line arguments
703  * @return 0 ok, 1 on error
704  */
705 int
706 main (int argc, char *const *argv)
707 {
708   int ret;
709
710   ret =
711       (GNUNET_OK ==
712        GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
713                            &run, NULL)) ? 0 : 1;
714   GNUNET_free_non_null (networkIdDirectory);
715   return ret;
716 }
717
718
719 /* end of gnunet-service-peerinfo.c */