-removing legacy ifdefs
[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  * Try to read the HELLO in the given filename and discard expired addresses.
181  * 
182  * @param fn name of the file
183  * @return HELLO of the file, NULL on error
184  */
185 static struct GNUNET_HELLO_Message *
186 read_host_file (const char *fn)
187 {
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
194   if (GNUNET_YES != GNUNET_DISK_file_test (fn))
195     return NULL;
196   size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
197   hello = (const struct GNUNET_HELLO_Message *) buffer;
198   if ((size < sizeof (struct GNUNET_MessageHeader)) ||
199       (size != ntohs ((((const struct GNUNET_MessageHeader *) hello)->size)))
200       || (size != GNUNET_HELLO_size (hello)))
201   {
202     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
203                 _("Failed to parse HELLO in file `%s'\n"),
204                 fn);
205     return NULL;
206   }
207   now = GNUNET_TIME_absolute_get ();
208   hello_clean =
209     GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired,
210                                     &now);
211   return hello_clean; 
212 }
213
214
215 /**
216  * Add a host to the list.
217  *
218  * @param identity the identity of the host
219  */
220 static void
221 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
222 {
223   struct HostEntry *entry;
224   char *fn;
225
226   entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
227   if (entry != NULL)
228     return;
229   GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
230                             GNUNET_NO);
231   entry = GNUNET_malloc (sizeof (struct HostEntry));
232   entry->identity = *identity;
233   GNUNET_CONTAINER_multihashmap_put (hostmap, &identity->hashPubKey, entry,
234                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
235   fn = get_host_filename (identity);
236   entry->hello = read_host_file (fn);
237   GNUNET_free (fn);
238   notify_all (entry);
239 }
240
241
242 /**
243  * Remove a file that should not be there.  LOG
244  * success or failure.
245  *
246  * @param fullname name of the file to remove
247  */
248 static void
249 remove_garbage (const char *fullname)
250 {
251   if (0 == UNLINK (fullname))
252     GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
253                 _
254                 ("File `%s' in directory `%s' does not match naming convention. "
255                  "Removed.\n"), fullname, networkIdDirectory);
256   else
257     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
258                               "unlink", fullname);
259 }
260
261
262 /**
263  * Function that is called on each HELLO file in a particular directory.
264  * Try to parse the file and add the HELLO to our list.
265  *
266  * @param cls pointer to 'unsigned int' to increment for each file, or NULL
267  *            if the file is from a read-only, read-once resource directory
268  * @param fullname name of the file to parse
269  * @return GNUNET_OK (continue iteration)
270  */
271 static int
272 hosts_directory_scan_callback (void *cls, const char *fullname)
273 {
274   unsigned int *matched = cls;
275   struct GNUNET_PeerIdentity identity;
276   const char *filename;
277   struct HostEntry *entry;
278   struct GNUNET_HELLO_Message *hello;
279
280   if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
281     return GNUNET_OK;           /* ignore non-files */
282   if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
283   {
284     if (NULL != matched)
285       remove_garbage (fullname);
286     return GNUNET_OK;
287   }
288   filename =
289       &fullname[strlen (fullname) -
290                 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
291   if (filename[-1] != DIR_SEPARATOR)
292   {
293     if (NULL != matched)
294       remove_garbage (fullname);
295     return GNUNET_OK;
296   }
297   if (GNUNET_OK !=
298       GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
299   {
300     if (NULL != (hello = read_host_file (filename)))
301     {
302       entry = GNUNET_malloc (sizeof (struct HostEntry));
303       if (GNUNET_OK ==
304           GNUNET_HELLO_get_id (hello,
305                                &entry->identity))
306       {
307         GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
308                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
309         entry->hello = hello;
310         notify_all (entry);
311         return GNUNET_OK;
312       }
313       GNUNET_free (entry);
314     }
315     if (NULL != matched)
316       remove_garbage (fullname);
317     return GNUNET_OK;
318   }
319   if (NULL != matched)
320     (*matched)++;
321   add_host_to_known_hosts (&identity);
322   return GNUNET_OK;
323 }
324
325
326 /**
327  * Call this method periodically to scan data/hosts for new hosts.
328  *
329  * @param cls unused
330  * @param tc scheduler context, aborted if reason is shutdown
331  */
332 static void
333 cron_scan_directory_data_hosts (void *cls,
334                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
335 {
336   static unsigned int retries;
337   unsigned int count;
338
339   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
340     return;
341   count = 0;
342   if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
343   {
344     GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
345                                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
346                                                 &cron_scan_directory_data_hosts, NULL);
347     return;
348   }
349   GNUNET_DISK_directory_scan (networkIdDirectory,
350                               &hosts_directory_scan_callback, &count);
351   if ((0 == count) && (0 == (++retries & 31)))
352     GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
353                 _("Still no peers found in `%s'!\n"), networkIdDirectory);
354   GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ, 
355                                               GNUNET_SCHEDULER_PRIORITY_IDLE,
356                                               &cron_scan_directory_data_hosts,
357                                               NULL);
358 }
359
360
361 /**
362  * Bind a host address (hello) to a hostId.
363  *
364  * @param peer the peer for which this is a hello
365  * @param hello the verified (!) hello message
366  */
367 static void
368 bind_address (const struct GNUNET_PeerIdentity *peer,
369               const struct GNUNET_HELLO_Message *hello)
370 {
371   char *fn;
372   struct HostEntry *host;
373   struct GNUNET_HELLO_Message *mrg;
374   struct GNUNET_TIME_Absolute delta;
375
376   add_host_to_known_hosts (peer);
377   host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
378   GNUNET_assert (host != NULL);
379   if (host->hello == NULL)
380   {
381     host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
382     memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
383   }
384   else
385   {
386     mrg = GNUNET_HELLO_merge (host->hello, hello);
387     delta = GNUNET_HELLO_equals (mrg, host->hello, GNUNET_TIME_absolute_get ());
388     if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
389     {
390       GNUNET_free (mrg);
391       return;
392     }
393     GNUNET_free (host->hello);
394     host->hello = mrg;
395   }
396   fn = get_host_filename (peer);
397   if (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn))
398   {
399     if (GNUNET_SYSERR ==
400         GNUNET_DISK_fn_write (fn, host->hello, GNUNET_HELLO_size (host->hello),
401                               GNUNET_DISK_PERM_USER_READ |
402                               GNUNET_DISK_PERM_USER_WRITE |
403                               GNUNET_DISK_PERM_GROUP_READ |
404                               GNUNET_DISK_PERM_OTHER_READ))
405       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
406
407   }
408   GNUNET_free (fn);
409   notify_all (host);
410 }
411
412
413 /**
414  * Do transmit info about peer to given host.
415  *
416  * @param cls NULL to hit all hosts, otherwise specifies a particular target
417  * @param key hostID
418  * @param value information to transmit
419  * @return GNUNET_YES (continue to iterate)
420  */
421 static int
422 add_to_tc (void *cls, const GNUNET_HashCode * key, void *value)
423 {
424   struct GNUNET_SERVER_TransmitContext *tc = cls;
425   struct HostEntry *pos = value;
426   struct InfoMessage *im;
427   uint16_t hs;
428   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
429
430   hs = 0;
431   im = (struct InfoMessage *) buf;
432   if (pos->hello != NULL)
433   {
434     hs = GNUNET_HELLO_size (pos->hello);
435     GNUNET_assert (hs <
436                    GNUNET_SERVER_MAX_MESSAGE_SIZE -
437                    sizeof (struct InfoMessage));
438     memcpy (&im[1], pos->hello, hs);
439   }
440   im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
441   im->header.size = htons (sizeof (struct InfoMessage) + hs);
442   im->reserved = htonl (0);
443   im->peer = pos->identity;
444   GNUNET_SERVER_transmit_context_append_message (tc, &im->header);
445   return GNUNET_YES;
446 }
447
448
449 /**
450  * @brief delete expired HELLO entries in data/hosts/
451  *
452  * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
453  * @param fn filename to test to see if the HELLO expired
454  * @return GNUNET_OK (continue iteration)
455  */
456 static int
457 discard_hosts_helper (void *cls, const char *fn)
458 {
459   struct GNUNET_TIME_Absolute *now = cls;
460   char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
461   const struct GNUNET_HELLO_Message *hello;
462   struct GNUNET_HELLO_Message *new_hello;
463   int size;
464
465   size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
466   if (size < sizeof (struct GNUNET_MessageHeader))
467   {
468     if (0 != UNLINK (fn))
469       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
470                                 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
471     return GNUNET_OK;
472   }
473   hello = (const struct GNUNET_HELLO_Message *) buffer;
474   new_hello =
475       GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
476   if (new_hello != NULL)
477   {
478     GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
479                           GNUNET_DISK_PERM_USER_READ |
480                           GNUNET_DISK_PERM_USER_WRITE |
481                           GNUNET_DISK_PERM_GROUP_READ |
482                           GNUNET_DISK_PERM_OTHER_READ);
483     GNUNET_free (new_hello);
484   }
485   else
486   {
487     if (0 != UNLINK (fn))
488       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
489                                 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
490   }
491   return GNUNET_OK;
492 }
493
494
495 /**
496  * Call this method periodically to scan data/hosts for ancient
497  * HELLOs to expire.
498  *
499  * @param cls unused
500  * @param tc scheduler context, aborted if reason is shutdown
501  */
502 static void
503 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
504 {
505   struct GNUNET_TIME_Absolute now;
506
507   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
508     return;
509   now = GNUNET_TIME_absolute_get ();
510   GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
511   GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
512                                 NULL);
513 }
514
515
516 /**
517  * Handle HELLO-message.
518  *
519  * @param cls closure
520  * @param client identification of the client
521  * @param message the actual message
522  */
523 static void
524 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
525               const struct GNUNET_MessageHeader *message)
526 {
527   const struct GNUNET_HELLO_Message *hello;
528   struct GNUNET_PeerIdentity pid;
529
530   hello = (const struct GNUNET_HELLO_Message *) message;
531   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
532   {
533     GNUNET_break (0);
534     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
535     return;
536   }
537   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
538               "HELLO", GNUNET_i2s (&pid));
539   bind_address (&pid, hello);
540   GNUNET_SERVER_receive_done (client, GNUNET_OK);
541 }
542
543
544 /**
545  * Handle GET-message.
546  *
547  * @param cls closure
548  * @param client identification of the client
549  * @param message the actual message
550  */
551 static void
552 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
553             const struct GNUNET_MessageHeader *message)
554 {
555   const struct ListPeerMessage *lpm;
556   struct GNUNET_SERVER_TransmitContext *tc;
557
558   lpm = (const struct ListPeerMessage *) message;
559   GNUNET_break (0 == ntohl (lpm->reserved));
560   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
561               "GET", GNUNET_i2s (&lpm->peer));
562   tc = GNUNET_SERVER_transmit_context_create (client);
563   GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
564                                               &add_to_tc, tc);
565   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
566                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
567   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
568 }
569
570
571 /**
572  * Handle GET-ALL-message.
573  *
574  * @param cls closure
575  * @param client identification of the client
576  * @param message the actual message
577  */
578 static void
579 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
580                 const struct GNUNET_MessageHeader *message)
581 {
582   struct GNUNET_SERVER_TransmitContext *tc;
583
584   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
585   tc = GNUNET_SERVER_transmit_context_create (client);
586   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, tc);
587   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
588                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
589   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
590 }
591
592
593 /**
594  * FIXME.
595  */
596 static int
597 do_notify_entry (void *cls, const GNUNET_HashCode * key, void *value)
598 {
599   struct GNUNET_SERVER_Client *client = cls;
600   struct HostEntry *he = value;
601   struct InfoMessage *msg;
602
603   msg = make_info_message (he);
604   GNUNET_SERVER_notification_context_unicast (notify_list, client, &msg->header,
605                                               GNUNET_NO);
606   GNUNET_free (msg);
607   return GNUNET_YES;
608 }
609
610
611 /**
612  * Handle NOTIFY-message.
613  *
614  * @param cls closure
615  * @param client identification of the client
616  * @param message the actual message
617  */
618 static void
619 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
620                const struct GNUNET_MessageHeader *message)
621 {
622   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
623   GNUNET_SERVER_notification_context_add (notify_list, client);
624   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, client);
625   GNUNET_SERVER_receive_done (client, GNUNET_OK);
626 }
627
628
629 /**
630  * FIXME.
631  */
632 static int
633 free_host_entry (void *cls, const GNUNET_HashCode * key, void *value)
634 {
635   struct HostEntry *he = value;
636
637   GNUNET_free_non_null (he->hello);
638   GNUNET_free (he);
639   return GNUNET_YES;
640 }
641
642
643 /**
644  * Clean up our state.  Called during shutdown.
645  *
646  * @param cls unused
647  * @param tc scheduler task context, unused
648  */
649 static void
650 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
651 {
652   GNUNET_SERVER_notification_context_destroy (notify_list);
653   notify_list = NULL;
654   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
655   GNUNET_CONTAINER_multihashmap_destroy (hostmap);
656   if (stats != NULL)
657   {
658     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
659     stats = NULL;
660   }
661 }
662
663
664 /**
665  * Start up peerinfo service.
666  *
667  * @param cls closure
668  * @param server the initialized server
669  * @param cfg configuration to use
670  */
671 static void
672 run (void *cls, struct GNUNET_SERVER_Handle *server,
673      const struct GNUNET_CONFIGURATION_Handle *cfg)
674 {
675   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
676     {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
677     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
678      sizeof (struct ListPeerMessage)},
679     {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
680      sizeof (struct GNUNET_MessageHeader)},
681     {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
682      sizeof (struct GNUNET_MessageHeader)},
683     {NULL, NULL, 0, 0}
684   };
685   char *peerdir;
686   char *ip;
687
688   hostmap = GNUNET_CONTAINER_multihashmap_create (1024);
689   stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
690   notify_list = GNUNET_SERVER_notification_context_create (server, 0);
691   GNUNET_assert (GNUNET_OK ==
692                  GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
693                                                           "HOSTS",
694                                                           &networkIdDirectory));
695   GNUNET_DISK_directory_create (networkIdDirectory);
696   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
697                                       &cron_scan_directory_data_hosts, NULL);
698   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
699                                       &cron_clean_data_hosts, NULL);
700   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
701                                 NULL);
702   GNUNET_SERVER_add_handlers (server, handlers);
703   ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
704   GNUNET_asprintf (&peerdir,
705                    "%shellos",
706                    ip);
707   GNUNET_free (ip);
708   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
709               _("Importing HELLOs from `%s'\n"),
710               peerdir);
711   GNUNET_DISK_directory_scan (peerdir,
712                               &hosts_directory_scan_callback, NULL);
713   GNUNET_free (peerdir);
714 }
715
716
717 /**
718  * The main function for the peerinfo service.
719  *
720  * @param argc number of arguments from the command line
721  * @param argv command line arguments
722  * @return 0 ok, 1 on error
723  */
724 int
725 main (int argc, char *const *argv)
726 {
727   int ret;
728
729   ret =
730       (GNUNET_OK ==
731        GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
732                            &run, NULL)) ? 0 : 1;
733   GNUNET_free_non_null (networkIdDirectory);
734   return ret;
735 }
736
737
738 /* end of gnunet-service-peerinfo.c */