-fixes
[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 #if DEBUG_PEERINFO
538   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
539               "HELLO", GNUNET_i2s (&pid));
540 #endif
541   bind_address (&pid, hello);
542   GNUNET_SERVER_receive_done (client, GNUNET_OK);
543 }
544
545
546 /**
547  * Handle GET-message.
548  *
549  * @param cls closure
550  * @param client identification of the client
551  * @param message the actual message
552  */
553 static void
554 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
555             const struct GNUNET_MessageHeader *message)
556 {
557   const struct ListPeerMessage *lpm;
558   struct GNUNET_SERVER_TransmitContext *tc;
559
560   lpm = (const struct ListPeerMessage *) message;
561   GNUNET_break (0 == ntohl (lpm->reserved));
562 #if DEBUG_PEERINFO
563   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
564               "GET", GNUNET_i2s (&lpm->peer));
565 #endif
566   tc = GNUNET_SERVER_transmit_context_create (client);
567   GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
568                                               &add_to_tc, tc);
569   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
570                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
571   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
572 }
573
574
575 /**
576  * Handle GET-ALL-message.
577  *
578  * @param cls closure
579  * @param client identification of the client
580  * @param message the actual message
581  */
582 static void
583 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
584                 const struct GNUNET_MessageHeader *message)
585 {
586   struct GNUNET_SERVER_TransmitContext *tc;
587
588 #if DEBUG_PEERINFO
589   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
590 #endif
591   tc = GNUNET_SERVER_transmit_context_create (client);
592   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, tc);
593   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
594                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
595   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
596 }
597
598
599 /**
600  * FIXME.
601  */
602 static int
603 do_notify_entry (void *cls, const GNUNET_HashCode * key, void *value)
604 {
605   struct GNUNET_SERVER_Client *client = cls;
606   struct HostEntry *he = value;
607   struct InfoMessage *msg;
608
609   msg = make_info_message (he);
610   GNUNET_SERVER_notification_context_unicast (notify_list, client, &msg->header,
611                                               GNUNET_NO);
612   GNUNET_free (msg);
613   return GNUNET_YES;
614 }
615
616
617 /**
618  * Handle NOTIFY-message.
619  *
620  * @param cls closure
621  * @param client identification of the client
622  * @param message the actual message
623  */
624 static void
625 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
626                const struct GNUNET_MessageHeader *message)
627 {
628 #if DEBUG_PEERINFO
629   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
630 #endif
631   GNUNET_SERVER_notification_context_add (notify_list, client);
632   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, client);
633   GNUNET_SERVER_receive_done (client, GNUNET_OK);
634 }
635
636
637 /**
638  * FIXME.
639  */
640 static int
641 free_host_entry (void *cls, const GNUNET_HashCode * key, void *value)
642 {
643   struct HostEntry *he = value;
644
645   GNUNET_free_non_null (he->hello);
646   GNUNET_free (he);
647   return GNUNET_YES;
648 }
649
650
651 /**
652  * Clean up our state.  Called during shutdown.
653  *
654  * @param cls unused
655  * @param tc scheduler task context, unused
656  */
657 static void
658 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
659 {
660   GNUNET_SERVER_notification_context_destroy (notify_list);
661   notify_list = NULL;
662   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
663   GNUNET_CONTAINER_multihashmap_destroy (hostmap);
664   if (stats != NULL)
665   {
666     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
667     stats = NULL;
668   }
669 }
670
671
672 /**
673  * Start up peerinfo service.
674  *
675  * @param cls closure
676  * @param server the initialized server
677  * @param cfg configuration to use
678  */
679 static void
680 run (void *cls, struct GNUNET_SERVER_Handle *server,
681      const struct GNUNET_CONFIGURATION_Handle *cfg)
682 {
683   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
684     {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
685     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
686      sizeof (struct ListPeerMessage)},
687     {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
688      sizeof (struct GNUNET_MessageHeader)},
689     {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
690      sizeof (struct GNUNET_MessageHeader)},
691     {NULL, NULL, 0, 0}
692   };
693   char *peerdir;
694   char *ip;
695
696   hostmap = GNUNET_CONTAINER_multihashmap_create (1024);
697   stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
698   notify_list = GNUNET_SERVER_notification_context_create (server, 0);
699   GNUNET_assert (GNUNET_OK ==
700                  GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
701                                                           "HOSTS",
702                                                           &networkIdDirectory));
703   GNUNET_DISK_directory_create (networkIdDirectory);
704   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
705                                       &cron_scan_directory_data_hosts, NULL);
706   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
707                                       &cron_clean_data_hosts, NULL);
708   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
709                                 NULL);
710   GNUNET_SERVER_add_handlers (server, handlers);
711   ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
712   GNUNET_asprintf (&peerdir,
713                    "%shellos",
714                    ip);
715   GNUNET_free (ip);
716   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
717               _("Importing HELLOs from `%s'\n"),
718               peerdir);
719   GNUNET_DISK_directory_scan (peerdir,
720                               &hosts_directory_scan_callback, NULL);
721   GNUNET_free (peerdir);
722 }
723
724
725 /**
726  * The main function for the peerinfo service.
727  *
728  * @param argc number of arguments from the command line
729  * @param argv command line arguments
730  * @return 0 ok, 1 on error
731  */
732 int
733 main (int argc, char *const *argv)
734 {
735   int ret;
736
737   ret =
738       (GNUNET_OK ==
739        GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
740                            &run, NULL)) ? 0 : 1;
741   GNUNET_free_non_null (networkIdDirectory);
742   return ret;
743 }
744
745
746 /* end of gnunet-service-peerinfo.c */