a2a66bd6272404d024d91d1652503e323bf334de
[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, 2012 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  * - notify clients when addresses in HELLO expire (#1933)
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    * Friend only hello for the peer (can be NULL)
70    */
71   struct GNUNET_HELLO_Message *friend_only_hello;
72
73 };
74
75 /**
76  * Transmit context for GET requests
77  */
78 struct TransmitContext
79 {
80         /**
81          * Server transmit context
82          */
83         struct GNUNET_SERVER_TransmitContext *tc;
84
85         /**
86                 * Include friend only HELLOs GNUNET_YES or _NO
87                 */
88         int friend_only;
89 };
90
91 /**
92  * Result of reading a file
93  */
94 struct ReadHostFileContext
95 {
96   /**
97    * Hello for the peer (can be NULL)
98    */
99   struct GNUNET_HELLO_Message *hello;
100
101   /**
102    * Friend only hello for the peer (can be NULL)
103    */
104   struct GNUNET_HELLO_Message *friend_only_hello;
105 };
106
107
108 /**
109  * Client notification context
110  */
111 struct NotificationContext
112 {
113         /**
114          * Next in DLL
115          */
116         struct NotificationContext *prev;
117
118         /**
119          * Previous in DLL
120          */
121         struct NotificationContext *next;
122
123         /**
124          * Server client
125          */
126         struct GNUNET_SERVER_Client *client;
127
128         /**
129          * Interested in friend only HELLO?
130          */
131         int include_friend_only;
132 };
133
134
135 /**
136  * The in-memory list of known hosts, mapping of
137  * host IDs to 'struct HostEntry*' values.
138  */
139 static struct GNUNET_CONTAINER_MultiHashMap *hostmap;
140
141 /**
142  * Clients to immediately notify about all changes.
143  */
144 static struct GNUNET_SERVER_NotificationContext *notify_list;
145
146 /**
147  * Directory where the hellos are stored in (peerinfo/)
148  */
149 static char *networkIdDirectory;
150
151 /**
152  * Handle for reporting statistics.
153  */
154 static struct GNUNET_STATISTICS_Handle *stats;
155
156 /**
157  * DLL of notification contexts: head
158  */
159 static struct NotificationContext *nc_head;
160
161 /**
162  * DLL of notification contexts: tail
163  */
164 static struct NotificationContext *nc_tail;
165
166
167 /**
168  * Notify all clients in the notify list about the
169  * given host entry changing.
170  *
171  * @param he entry of the host for which we generate a notification
172  * @return generated notification message
173  */
174 static struct InfoMessage *
175 make_info_message (const struct HostEntry *he, int include_friend_only)
176 {
177   struct InfoMessage *im;
178   struct GNUNET_HELLO_Message *src;
179   size_t hs;
180
181   if (GNUNET_YES == include_friend_only)
182         src = he->friend_only_hello;
183   else
184         src = he->hello;
185
186   hs = (NULL == src) ? 0 : GNUNET_HELLO_size (src);
187   im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
188   im->header.size = htons (hs + sizeof (struct InfoMessage));
189   im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
190   im->peer = he->identity;
191   if (NULL != src)
192     memcpy (&im[1], src, hs);
193   return im;
194 }
195
196
197 /**
198  * Address iterator that causes expired entries to be discarded.
199  *
200  * @param cls pointer to the current time
201  * @param address the address
202  * @param expiration expiration time for the address
203  * @return GNUNET_NO if expiration smaller than the current time
204  */
205 static int
206 discard_expired (void *cls, const struct GNUNET_HELLO_Address *address,
207                  struct GNUNET_TIME_Absolute expiration)
208 {
209   const struct GNUNET_TIME_Absolute *now = cls;
210
211   if (now->abs_value > expiration.abs_value)
212   {
213     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
214                 _("Removing expired address of transport `%s'\n"),
215                 address->transport_name);
216     return GNUNET_NO;
217   }
218   return GNUNET_OK;
219 }
220
221
222 /**
223  * Address iterator that counts the remaining addresses.
224  *
225  * @param cls pointer to the counter
226  * @param address the address
227  * @param expiration expiration time for the address
228  * @return GNUNET_OK (always)
229  */
230 static int
231 count_addresses (void *cls, const struct GNUNET_HELLO_Address *address,
232                  struct GNUNET_TIME_Absolute expiration)
233 {
234   unsigned int *cnt = cls;
235
236   (*cnt)++;
237   return GNUNET_OK;
238 }
239
240
241 /**
242  * Get the filename under which we would store the GNUNET_HELLO_Message
243  * for the given host and protocol.
244  *
245  * @param id peer for which we need the filename for the HELLO
246  * @return filename of the form DIRECTORY/HOSTID
247  */
248 static char *
249 get_host_filename (const struct GNUNET_PeerIdentity *id)
250 {
251   struct GNUNET_CRYPTO_HashAsciiEncoded fil;
252   char *fn;
253
254   if (NULL == networkIdDirectory)
255     return NULL;
256   GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
257   GNUNET_asprintf (&fn, "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
258   return fn;
259 }
260
261
262 /**
263  * Broadcast information about the given entry to all
264  * clients that care.
265  *
266  * @param entry entry to broadcast about
267  */
268 static void
269 notify_all (struct HostEntry *entry)
270 {
271   struct InfoMessage *msg_pub;
272   struct InfoMessage *msg_friend;
273   struct NotificationContext *cur;
274
275   msg_pub = make_info_message (entry, GNUNET_NO);
276   msg_friend = make_info_message (entry, GNUNET_YES);
277   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Notifying all clients about peer `%s'\n",
278                 GNUNET_i2s(&entry->identity));
279         for (cur = nc_head; NULL != cur; cur = cur->next)
280         {
281                 if (GNUNET_NO == cur->include_friend_only)
282                 {
283                         GNUNET_SERVER_notification_context_unicast (notify_list,
284                                                                                                                                                                                                         cur->client,
285                                                                                                                                                                                                         &msg_pub->header,
286                                                                                                                                                                                                         GNUNET_NO);
287                 }
288                 if (GNUNET_YES == cur->include_friend_only)
289                 {
290                         GNUNET_SERVER_notification_context_unicast (notify_list,
291                                                                                                                                                                                                         cur->client,
292                                                                                                                                                                                                         &msg_friend->header,
293                                                                                                                                                                                                         GNUNET_NO);
294                 }
295         }
296   GNUNET_free (msg_pub);
297   GNUNET_free (msg_friend);
298 }
299
300
301 /**
302  * Bind a host address (hello) to a hostId.
303  *
304  * @param peer the peer for which this is a hello
305  * @param hello the verified (!) hello message
306  */
307 static void
308 update_hello (const struct GNUNET_PeerIdentity *peer,
309               const struct GNUNET_HELLO_Message *hello);
310
311
312 /**
313  * Try to read the HELLOs in the given filename and discard expired
314  * addresses.  Removes the file if one the HELLO is mal-formed.  If all
315  * addresses are expired, the HELLO is also removed (but the HELLO
316  * with the public key is still returned if it was found and valid).
317  * 
318  * The file can contain up to two HELLO messages, a public and a friend only
319  * HELLO
320  *
321  * @param fn name of the file
322  * @param unlink_garbage if GNUNET_YES, try to remove useless files
323  */
324 static void
325 read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r)
326 {
327   char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
328   const struct GNUNET_HELLO_Message *hello_1st;
329   const struct GNUNET_HELLO_Message *hello_2nd;
330   struct GNUNET_HELLO_Message *hello_clean_1st;
331   struct GNUNET_HELLO_Message *hello_clean_2nd;
332   int size_1st;
333   int size_2nd;
334   int size_total;
335   struct GNUNET_TIME_Absolute now;
336   unsigned int left;
337
338   hello_1st = NULL;
339   hello_2nd = NULL;
340   hello_clean_1st = NULL;
341   hello_clean_2nd = NULL;
342   size_1st = 0;
343   size_2nd = 0;
344   size_total = 0;
345   r->friend_only_hello = NULL;
346   r->hello = NULL;
347
348   if (GNUNET_YES != GNUNET_DISK_file_test (fn))
349   {
350     return;
351   }
352
353   size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
354   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from `%s'\n", size_total, fn);
355   if (size_total < sizeof (struct GNUNET_MessageHeader))
356   {
357             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
358                                                                         _("Failed to parse HELLO in file `%s': %s\n"),
359                                                                         fn, "Fail has invalid size");
360     if ( (GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) )
361       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
362     return;
363   }
364
365   hello_1st = (const struct GNUNET_HELLO_Message *) buffer;
366   size_1st = ntohs (((struct GNUNET_MessageHeader *) &buffer)->size);
367   if (size_1st < sizeof (struct GNUNET_MessageHeader))
368   {
369             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370                                                                         _("Failed to parse HELLO in file `%s': %s %u \n"),
371                                                                         fn, "1st HELLO has invalid size of ", size_1st);
372     if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
373       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
374     return;
375   }
376         if (size_1st != GNUNET_HELLO_size (hello_1st))
377         {
378     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
379                                                                 _("Failed to parse HELLO in file `%s': %s \n"),
380                                                                 fn, "1st HELLO is invalid");
381     if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
382         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
383     return;
384         }
385
386   if (size_total > size_1st)
387   {
388                 hello_2nd = (const struct GNUNET_HELLO_Message *) &buffer[size_1st];
389                 size_2nd = ntohs (((const struct GNUNET_MessageHeader *) hello_2nd)->size);
390           if ((size_2nd < sizeof (struct GNUNET_MessageHeader)) ||
391                         (size_2nd != GNUNET_HELLO_size (hello_2nd)))
392           {
393             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
394                                                                                 _("Failed to parse HELLO in file `%s': %s\n"),
395                                                                                 fn, "2nd HELLO has wrong size");
396             if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
397               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
398             return;
399           }
400   }
401
402   if (size_total != (size_1st + size_2nd))
403   {
404             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405                                                         _("Failed to parse HELLO in file `%s': %s\n"),
406                                                         fn, "Multiple HELLOs but total size is wrong");
407             if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
408               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
409             return;
410   }
411
412   now = GNUNET_TIME_absolute_get ();
413   hello_clean_1st = GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_YES,
414                                                                                                                                                                                                   &discard_expired, &now);
415   left = 0;
416   (void) GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_NO,
417                                                                                                                                                          &count_addresses, &left);
418   if (0 == left)
419   {
420     /* no addresses left, remove from disk */
421     if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
422       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
423   }
424
425   if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_1st))
426   {
427         if (NULL == r->hello)
428                 r->hello = hello_clean_1st;
429         else
430         {
431                         GNUNET_break (0);
432                         GNUNET_free (r->hello);
433                         r->hello = hello_clean_1st;
434         }
435   }
436   else
437   {
438         if (NULL == r->friend_only_hello)
439                 r->friend_only_hello = hello_clean_1st;
440         else
441         {
442                         GNUNET_break (0);
443                         GNUNET_free (r->friend_only_hello);
444                         r->friend_only_hello = hello_clean_1st;
445         }
446   }
447
448   if (NULL != hello_2nd)
449   {
450           hello_clean_2nd = GNUNET_HELLO_iterate_addresses (hello_2nd, GNUNET_YES,
451                                                                                                                                                                                                           &discard_expired, &now);
452           left = 0;
453           (void) GNUNET_HELLO_iterate_addresses (hello_clean_2nd, GNUNET_NO,
454                                                                                                                                                                  &count_addresses, &left);
455           if (0 == left)
456           {
457             /* no addresses left, remove from disk */
458             if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
459               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
460           }
461
462           if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_2nd))
463           {
464                 if (NULL == r->hello)
465                         r->hello = hello_clean_2nd;
466                 else
467                 {
468                                 GNUNET_break (0);
469                                 GNUNET_free (r->hello);
470                                 r->hello = hello_clean_2nd;
471                 }
472           }
473           else
474           {
475                 if (NULL == r->friend_only_hello)
476                         r->friend_only_hello = hello_clean_2nd;
477                 else
478                 {
479                                 GNUNET_break (0);
480                                 GNUNET_free (r->friend_only_hello);
481                                 r->friend_only_hello = hello_clean_2nd;
482                 }
483           }
484   }
485
486   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' and `%s' HELLO message in file\n",
487                 (NULL != r->hello) ? "public" : "NO public",
488                         (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
489
490
491 }
492
493
494 /**
495  * Add a host to the list and notify clients about this event
496  *
497  * @param identity the identity of the host
498  * @return the HostEntry
499  */
500 static struct HostEntry *
501 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
502 {
503   struct HostEntry *entry;
504   struct ReadHostFileContext r;
505   char *fn;
506
507   entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
508   if (NULL == entry)
509   {
510                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n", GNUNET_i2s (identity));
511         GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
512                             GNUNET_NO);
513         entry = GNUNET_malloc (sizeof (struct HostEntry));
514         entry->identity = *identity;
515         GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
516                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
517     notify_all (entry);
518     fn = get_host_filename (identity);
519     if (NULL != fn)
520     {
521       read_host_file (fn, GNUNET_YES, &r);
522       if (NULL != r.hello)
523         update_hello (identity, r.hello);
524       if (NULL != r.friend_only_hello)
525         update_hello (identity, r.friend_only_hello);
526       GNUNET_free_non_null (r.hello);
527       GNUNET_free_non_null (r.friend_only_hello);
528       GNUNET_free (fn);
529     }
530   }
531   return entry;
532 }
533
534
535 /**
536  * Remove a file that should not be there.  LOG
537  * success or failure.
538  *
539  * @param fullname name of the file to remove
540  */
541 static void
542 remove_garbage (const char *fullname)
543 {
544   if (0 == UNLINK (fullname))
545     GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
546                 _
547                 ("File `%s' in directory `%s' does not match naming convention. "
548                  "Removed.\n"), fullname, networkIdDirectory);
549   else
550     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
551                               "unlink", fullname);
552 }
553
554
555 /**
556  * Closure for 'hosts_directory_scan_callback'.
557  */
558 struct DirScanContext
559 {
560   /**
561    * GNUNET_YES if we should remove files that are broken,
562    * GNUNET_NO if the directory we are iterating over should
563    * be treated as read-only by us.
564    */ 
565   int remove_files;
566
567   /**
568    * Counter for the number of (valid) entries found, incremented
569    * by one for each match.
570    */
571   unsigned int matched;
572 };
573
574
575 /**
576  * Function that is called on each HELLO file in a particular directory.
577  * Try to parse the file and add the HELLO to our list.
578  *
579  * @param cls pointer to 'unsigned int' to increment for each file, or NULL
580  *            if the file is from a read-only, read-once resource directory
581  * @param fullname name of the file to parse
582  * @return GNUNET_OK (continue iteration)
583  */
584 static int
585 hosts_directory_scan_callback (void *cls, const char *fullname)
586 {
587   struct DirScanContext *dsc = cls;
588   struct GNUNET_PeerIdentity identity;
589   struct ReadHostFileContext r;
590   const char *filename;
591   struct GNUNET_PeerIdentity id_public;
592   struct GNUNET_PeerIdentity id_friend;
593   struct GNUNET_PeerIdentity id;
594
595   if (GNUNET_YES != GNUNET_DISK_file_test (fullname))
596     return GNUNET_OK;           /* ignore non-files */
597
598   filename = strrchr (fullname, DIR_SEPARATOR);
599   if ((NULL == filename) || (1 > strlen (filename)))
600         filename = fullname;
601   else
602     filename ++;
603
604   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reading %s `%s'\n", fullname, filename);
605   read_host_file (fullname, dsc->remove_files, &r);
606
607         if ( (NULL == r.hello) && (NULL == r.friend_only_hello))
608         {
609     if (GNUNET_YES == dsc->remove_files)
610       remove_garbage (fullname);
611     return GNUNET_OK;
612         }
613
614         if (NULL != r.friend_only_hello)
615         {
616                 if (GNUNET_OK != GNUNET_HELLO_get_id (r.friend_only_hello, &id_friend))
617                         if (GNUNET_YES == dsc->remove_files)
618                         {
619                                 remove_garbage (fullname);
620                                 return GNUNET_OK;
621                         }
622                 id = id_friend;
623         }
624         if (NULL != r.hello)
625         {
626                 if (GNUNET_OK != GNUNET_HELLO_get_id (r.hello, &id_public))
627                         if (GNUNET_YES == dsc->remove_files)
628                         {
629                                 remove_garbage (fullname);
630                                 return GNUNET_OK;
631                         }
632                 id = id_public;
633         }
634
635         if ( (NULL != r.hello) && (NULL != r.friend_only_hello) &&
636                         (0 != memcmp (&id_friend, &id_public, sizeof (id_friend))) )
637         {
638                 /* HELLOs are not for the same peer */
639                 GNUNET_break (0);
640                 if (GNUNET_YES == dsc->remove_files)
641                         remove_garbage (fullname);
642                 return GNUNET_OK;
643         }
644   if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
645   {
646                 if (0 != memcmp (&id, &identity, sizeof (id_friend)))
647                 {
648                         /* HELLOs are not for the same peer */
649                         GNUNET_break (0);
650                         if (GNUNET_YES == dsc->remove_files)
651                                 remove_garbage (fullname);
652                         return GNUNET_OK;
653                 }
654   }
655         /* ok, found something valid, remember HELLO */
656   add_host_to_known_hosts (&id);
657   if (NULL != r.hello)
658   {
659                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' public HELLO \n",
660                                         GNUNET_i2s (&id));
661         update_hello (&id, r.hello);
662         GNUNET_free (r.hello);
663   }
664   if (NULL != r.friend_only_hello)
665   {
666                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' friend only HELLO \n",
667                                         GNUNET_i2s (&id));
668         update_hello (&id, r.friend_only_hello);
669         GNUNET_free (r.friend_only_hello);
670   }
671         dsc->matched++;
672         return GNUNET_OK;
673 }
674
675
676 /**
677  * Call this method periodically to scan data/hosts for new hosts.
678  *
679  * @param cls unused
680  * @param tc scheduler context, aborted if reason is shutdown
681  */
682 static void
683 cron_scan_directory_data_hosts (void *cls,
684                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
685 {
686   static unsigned int retries;
687   struct DirScanContext dsc;
688
689   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
690     return;
691   if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
692   {
693     GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
694                                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
695                                                 &cron_scan_directory_data_hosts, NULL);
696     return;
697   }
698   dsc.matched = 0;
699   dsc.remove_files = GNUNET_YES;
700   GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
701               _("Scanning directory `%s'\n"), networkIdDirectory);
702   GNUNET_DISK_directory_scan (networkIdDirectory,
703                               &hosts_directory_scan_callback, &dsc);
704   if ((0 == dsc.matched) && (0 == (++retries & 31)))
705     GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
706                 _("Still no peers found in `%s'!\n"), networkIdDirectory);
707   GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ, 
708                                               GNUNET_SCHEDULER_PRIORITY_IDLE,
709                                               &cron_scan_directory_data_hosts,
710                                               NULL);
711 }
712
713
714 static struct GNUNET_HELLO_Message *
715 update_friend_hello (const struct GNUNET_HELLO_Message *hello,
716                                                                                  const struct GNUNET_HELLO_Message *friend_hello)
717 {
718         struct GNUNET_HELLO_Message * res;
719         struct GNUNET_HELLO_Message * tmp;
720         struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
721
722         if (NULL != friend_hello)
723         {
724                 res = GNUNET_HELLO_merge (hello, friend_hello);
725                 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
726                 return res;
727         }
728
729         GNUNET_HELLO_get_key (hello, &pk);
730         tmp = GNUNET_HELLO_create (&pk, NULL, NULL, GNUNET_YES);
731         res = GNUNET_HELLO_merge (hello, tmp);
732         GNUNET_free (tmp);
733         GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
734         return res;
735 }
736
737
738
739 /**
740  * Bind a host address (hello) to a hostId.
741  *
742  * @param peer the peer for which this is a hello
743  * @param hello the verified (!) hello message
744  */
745 static void
746 update_hello (const struct GNUNET_PeerIdentity *peer,
747               const struct GNUNET_HELLO_Message *hello)
748 {
749   char *fn;
750   struct HostEntry *host;
751   struct GNUNET_HELLO_Message *mrg;
752   struct GNUNET_HELLO_Message **dest;
753   struct GNUNET_TIME_Absolute delta;
754   unsigned int cnt;
755   unsigned int size;
756   int friend_hello_type;
757   int store_hello;
758   int store_friend_hello;
759   int pos;
760   char *buffer;
761
762   host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
763   GNUNET_assert (NULL != host);
764
765   friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
766         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating %s HELLO for `%s'\n",
767                         (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
768                         GNUNET_i2s (peer));
769
770   dest = NULL;
771   if (GNUNET_YES == friend_hello_type)
772   {
773         dest = &host->friend_only_hello;
774   }
775   else
776   {
777         dest = &host->hello;
778   }
779
780   if (NULL == (*dest))
781   {
782         (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
783     memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
784   }
785   else
786   {
787     mrg = GNUNET_HELLO_merge ((*dest), hello);
788     delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ());
789     if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
790     {
791       /* no differences, just ignore the update */
792         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No change in %s HELLO for `%s'\n",
793                         (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
794                         GNUNET_i2s (peer));
795       GNUNET_free (mrg);
796       return;
797     }
798     GNUNET_free ((*dest));
799     (*dest) = mrg;
800   }
801
802   if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type))
803   {
804                 /* Update friend only hello */
805                 mrg = update_friend_hello (host->hello, host->friend_only_hello);
806                 if (NULL != host->friend_only_hello)
807                         GNUNET_free (host->friend_only_hello);
808                 host->friend_only_hello = mrg;
809   }
810
811   if (NULL != host->hello)
812         GNUNET_assert ((GNUNET_NO == GNUNET_HELLO_is_friend_only (host->hello)));
813   if (NULL != host->friend_only_hello)
814         GNUNET_assert ((GNUNET_YES == GNUNET_HELLO_is_friend_only(host->friend_only_hello)));
815
816   store_hello = GNUNET_NO;
817   store_friend_hello = GNUNET_NO;
818         fn = get_host_filename (peer);
819         if ( (NULL != fn) &&
820                          (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn)) )
821                 {
822
823                         store_hello = GNUNET_NO;
824                         size = 0;
825                         cnt = 0;
826                         if (NULL != host->hello)
827                                 (void) GNUNET_HELLO_iterate_addresses (host->hello,
828                                                                         GNUNET_NO, &count_addresses, &cnt);
829                         if (cnt > 0)
830                         {
831                                 store_hello = GNUNET_YES;
832                                 size += GNUNET_HELLO_size (host->hello);
833                         }
834                         cnt = 0;
835                         if (NULL != host->friend_only_hello)
836                                 (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello, GNUNET_NO,
837                                                                                 &count_addresses, &cnt);
838                         if (0 < cnt)
839                         {
840                                 store_friend_hello = GNUNET_YES;
841                                 size += GNUNET_HELLO_size (host->friend_only_hello);
842                         }
843
844                         if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello))
845                         {
846                                 /* no valid addresses, don't put HELLO on disk; in fact,
847                                          if one exists on disk, remove it */
848                                 (void) UNLINK (fn);
849                         }
850                         else
851                         {
852                                 buffer = GNUNET_malloc (size);
853                                 pos = 0;
854
855                                 if (GNUNET_YES == store_hello)
856                                 {
857                                         memcpy (buffer, host->hello, GNUNET_HELLO_size (host->hello));
858                                         pos += GNUNET_HELLO_size (host->hello);
859                                 }
860                                 if (GNUNET_YES == store_friend_hello)
861                                 {
862                                         memcpy (&buffer[pos], host->friend_only_hello, GNUNET_HELLO_size (host->friend_only_hello));
863                                         pos += GNUNET_HELLO_size (host->friend_only_hello);
864                                 }
865                                 GNUNET_assert (pos == size);
866
867                                 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
868                                         GNUNET_DISK_PERM_USER_READ |
869                                         GNUNET_DISK_PERM_USER_WRITE |
870                                         GNUNET_DISK_PERM_GROUP_READ |
871                                         GNUNET_DISK_PERM_OTHER_READ))
872                                         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
873                                 else
874                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stored %s %s HELLO in %s  with total size %u\n",
875                                                         (GNUNET_YES == store_friend_hello) ? "friend-only": "",
876                                                         (GNUNET_YES == store_hello) ? "public": "",
877                                                         fn, size);
878                                 GNUNET_free (buffer);
879                         }
880   }
881         GNUNET_free_non_null (fn);
882   notify_all (host);
883 }
884
885
886 /**
887  * Do transmit info about peer to given host.
888  *
889  * @param cls NULL to hit all hosts, otherwise specifies a particular target
890  * @param key hostID
891  * @param value information to transmit
892  * @return GNUNET_YES (continue to iterate)
893  */
894 static int
895 add_to_tc (void *cls, const struct GNUNET_HashCode * key, void *value)
896 {
897   struct TransmitContext *tc = cls;
898   struct HostEntry *pos = value;
899   struct InfoMessage *im;
900   uint16_t hs;
901   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
902
903   hs = 0;
904   im = (struct InfoMessage *) buf;
905
906   if ((pos->hello != NULL) && (GNUNET_NO == tc->friend_only))
907   {
908         /* Copy public HELLO */
909     hs = GNUNET_HELLO_size (pos->hello);
910     GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
911                    sizeof (struct InfoMessage));
912     memcpy (&im[1], pos->hello, hs);
913     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending public HELLO with size %u for peer `%4s'\n",
914                 hs, GNUNET_h2s (key));
915   }
916   else if ((pos->friend_only_hello != NULL) && (GNUNET_YES == tc->friend_only))
917   {
918         /* Copy friend only HELLO */
919     hs = GNUNET_HELLO_size (pos->friend_only_hello);
920     GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
921                    sizeof (struct InfoMessage));
922     memcpy (&im[1], pos->friend_only_hello, hs);
923     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending friend-only HELLO with size %u for peer `%4s'\n",
924                 hs, GNUNET_h2s (key));
925   }
926   else
927   {
928       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding no HELLO for peer `%s'\n",
929                  GNUNET_h2s (key));
930   }
931
932   im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
933   im->header.size = htons (sizeof (struct InfoMessage) + hs);
934   im->reserved = htonl (0);
935   im->peer = pos->identity;
936   GNUNET_SERVER_transmit_context_append_message (tc->tc, &im->header);
937   return GNUNET_YES;
938 }
939
940
941 /**
942  * @brief delete expired HELLO entries in directory
943  *
944  * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
945  * @param fn filename to test to see if the HELLO expired
946  * @return GNUNET_OK (continue iteration)
947  */
948 static int
949 discard_hosts_helper (void *cls, const char *fn)
950 {
951   struct GNUNET_TIME_Absolute *now = cls;
952   char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
953   const struct GNUNET_HELLO_Message *hello;
954   struct GNUNET_HELLO_Message *new_hello;
955   int size;
956   unsigned int cnt;
957
958   size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
959   if (size < sizeof (struct GNUNET_MessageHeader))
960   {
961     if (0 != UNLINK (fn))
962       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
963                                 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
964     return GNUNET_OK;
965   }
966   hello = (const struct GNUNET_HELLO_Message *) buffer;
967   new_hello =
968     GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
969   cnt = 0;
970   if (NULL != new_hello)
971     (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses, &cnt);
972   if ( (NULL != new_hello) && (0 < cnt) )
973   {
974     GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
975                           GNUNET_DISK_PERM_USER_READ |
976                           GNUNET_DISK_PERM_USER_WRITE |
977                           GNUNET_DISK_PERM_GROUP_READ |
978                           GNUNET_DISK_PERM_OTHER_READ);
979   }
980   else
981   {
982     if (0 != UNLINK (fn))
983       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
984                                 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
985   }
986   GNUNET_free_non_null (new_hello);
987   return GNUNET_OK;
988 }
989
990
991 /**
992  * Call this method periodically to scan data/hosts for ancient
993  * HELLOs to expire.
994  *
995  * @param cls unused
996  * @param tc scheduler context, aborted if reason is shutdown
997  */
998 static void
999 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1000 {
1001   struct GNUNET_TIME_Absolute now;
1002
1003   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1004     return;
1005   now = GNUNET_TIME_absolute_get ();
1006   GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
1007   GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
1008                                 NULL);
1009 }
1010
1011
1012 /**
1013  * Handle HELLO-message.
1014  *
1015  * @param cls closure
1016  * @param client identification of the client
1017  * @param message the actual message
1018  */
1019 static void
1020 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
1021               const struct GNUNET_MessageHeader *message)
1022 {
1023   const struct GNUNET_HELLO_Message *hello;
1024   struct GNUNET_PeerIdentity pid;
1025
1026   hello = (const struct GNUNET_HELLO_Message *) message;
1027   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
1028   {
1029     GNUNET_break (0);
1030     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1031     return;
1032   }
1033   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1034               "HELLO", GNUNET_i2s (&pid));
1035   add_host_to_known_hosts (&pid);
1036   update_hello (&pid, hello);
1037   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1038 }
1039
1040
1041 /**
1042  * Handle GET-message.
1043  *
1044  * @param cls closure
1045  * @param client identification of the client
1046  * @param message the actual message
1047  */
1048 static void
1049 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
1050             const struct GNUNET_MessageHeader *message)
1051 {
1052   const struct ListPeerMessage *lpm;
1053   struct TransmitContext tcx;
1054
1055   lpm = (const struct ListPeerMessage *) message;
1056   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1057               "GET", GNUNET_i2s (&lpm->peer));
1058   tcx.friend_only = ntohl (lpm->include_friend_only);
1059   tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1060   GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
1061                                               &add_to_tc, &tcx);
1062   GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1063                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1064   GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1065 }
1066
1067
1068 /**
1069  * Handle GET-ALL-message.
1070  *
1071  * @param cls closure
1072  * @param client identification of the client
1073  * @param message the actual message
1074  */
1075 static void
1076 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
1077                 const struct GNUNET_MessageHeader *message)
1078 {
1079   const struct ListAllPeersMessage *lapm;
1080   struct TransmitContext tcx;
1081
1082   lapm = (const struct ListAllPeersMessage *) message;
1083   tcx.friend_only = ntohl (lapm->include_friend_only);
1084   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
1085   tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1086   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, &tcx);
1087   GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1088                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1089   GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1090 }
1091
1092
1093
1094 /**
1095  * Pass the given client the information we have in the respective
1096  * host entry; the client is already in the notification context.
1097  *
1098  * @param cls the 'struct GNUNET_SERVER_Client' to notify
1099  * @param key key for the value (unused)
1100  * @param value the 'struct HostEntry' to notify the client about
1101  * @return GNUNET_YES (always, continue to iterate)
1102  */
1103 static int
1104 do_notify_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1105 {
1106         struct NotificationContext *nc = cls;
1107   struct HostEntry *he = value;
1108   struct InfoMessage *msg;
1109
1110         if ((NULL == he->hello) && (GNUNET_NO == nc->include_friend_only))
1111         {
1112                 /* We have no public hello  */
1113           return GNUNET_YES;
1114         }
1115
1116
1117         if ((NULL == he->friend_only_hello) && GNUNET_YES == nc->include_friend_only)
1118         {
1119                 /* We have no friend hello */
1120           return GNUNET_YES;
1121         }
1122
1123         msg = make_info_message (he, nc->include_friend_only);
1124         GNUNET_SERVER_notification_context_unicast (notify_list,
1125                         nc->client,
1126                         &msg->header,
1127                         GNUNET_NO);
1128   GNUNET_free (msg);
1129   return GNUNET_YES;
1130 }
1131
1132
1133 /**
1134  * Handle NOTIFY-message.
1135  *
1136  * @param cls closure
1137  * @param client identification of the client
1138  * @param message the actual message
1139  */
1140 static void
1141 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
1142                const struct GNUNET_MessageHeader *message)
1143 {
1144   struct NotifyMessage *nm = (struct NotifyMessage *) message;
1145   struct NotificationContext *nc;
1146
1147         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
1148
1149         nc = GNUNET_malloc (sizeof (struct NotificationContext));
1150         nc->client = client;
1151         nc->include_friend_only = ntohl (nm->include_friend_only);
1152
1153         GNUNET_CONTAINER_DLL_insert (nc_head, nc_tail, nc);
1154   GNUNET_SERVER_client_mark_monitor (client);
1155         GNUNET_SERVER_notification_context_add (notify_list, client);
1156   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, nc);
1157   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1158 }
1159
1160
1161 /**
1162  * Client disconnect callback
1163  *
1164  * @param cls unused
1165  * @param client server client
1166  */
1167 static void disconnect_cb (void *cls,struct GNUNET_SERVER_Client *client)
1168 {
1169         struct NotificationContext *cur;
1170
1171         for (cur = nc_head; NULL != cur; cur = cur->next)
1172                 if (cur->client == client)
1173                         break;
1174
1175         if (NULL == cur)
1176                 return;
1177
1178         GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1179         GNUNET_free (cur);
1180 }
1181
1182
1183 /**
1184  * Release memory taken by a host entry.
1185  *
1186  * @param cls NULL
1187  * @param key key of the host entry
1188  * @param value the 'struct HostEntry' to free
1189  * @return GNUNET_YES (continue to iterate)
1190  */
1191 static int
1192 free_host_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1193 {
1194   struct HostEntry *he = value;
1195
1196   GNUNET_free_non_null (he->hello);
1197   GNUNET_free_non_null (he->friend_only_hello);
1198   GNUNET_free (he);
1199   return GNUNET_YES;
1200 }
1201
1202
1203 /**
1204  * Clean up our state.  Called during shutdown.
1205  *
1206  * @param cls unused
1207  * @param tc scheduler task context, unused
1208  */
1209 static void
1210 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1211 {
1212         struct NotificationContext *cur;
1213         struct NotificationContext *next;
1214         GNUNET_SERVER_notification_context_destroy (notify_list);
1215   notify_list = NULL;
1216
1217         for (cur = nc_head; NULL != cur; cur = next)
1218         {
1219                         next = cur->next;
1220                         GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1221                         GNUNET_free (cur);
1222         }
1223
1224   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
1225   GNUNET_CONTAINER_multihashmap_destroy (hostmap);
1226   if (NULL != stats)
1227   {
1228     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1229     stats = NULL;
1230   }
1231 }
1232
1233
1234 /**
1235  * Start up peerinfo service.
1236  *
1237  * @param cls closure
1238  * @param server the initialized server
1239  * @param cfg configuration to use
1240  */
1241 static void
1242 run (void *cls, struct GNUNET_SERVER_Handle *server,
1243      const struct GNUNET_CONFIGURATION_Handle *cfg)
1244 {
1245   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1246     {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
1247     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
1248      sizeof (struct ListPeerMessage)},
1249     {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
1250      sizeof (struct ListAllPeersMessage)},
1251     {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
1252      sizeof (struct NotifyMessage)},
1253     {NULL, NULL, 0, 0}
1254   };
1255   char *peerdir;
1256   char *ip;
1257   struct DirScanContext dsc;
1258   int noio;
1259
1260   hostmap = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1261   stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
1262   notify_list = GNUNET_SERVER_notification_context_create (server, 0);
1263   noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO");
1264   if (GNUNET_YES != noio)
1265   {
1266     GNUNET_assert (GNUNET_OK ==
1267                    GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
1268                                                             "HOSTS",
1269                                                             &networkIdDirectory));
1270     GNUNET_DISK_directory_create (networkIdDirectory);
1271
1272     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1273                                         &cron_scan_directory_data_hosts, NULL); /* CHECK */
1274
1275     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1276                                         &cron_clean_data_hosts, NULL); /* CHECK */
1277
1278     ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1279     GNUNET_asprintf (&peerdir,
1280                      "%shellos",
1281                      ip);
1282     GNUNET_free (ip);
1283
1284     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1285                 _("Importing HELLOs from `%s'\n"),
1286                 peerdir);
1287     dsc.matched = 0;
1288     dsc.remove_files = GNUNET_NO;
1289
1290     GNUNET_DISK_directory_scan (peerdir,
1291                                 &hosts_directory_scan_callback, &dsc);
1292
1293     GNUNET_free (peerdir);
1294   }
1295   GNUNET_SERVER_add_handlers (server, handlers);
1296   GNUNET_SERVER_disconnect_notify (server, &disconnect_cb, NULL) ;
1297   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1298                                 NULL);
1299
1300 }
1301
1302
1303 /**
1304  * The main function for the peerinfo service.
1305  *
1306  * @param argc number of arguments from the command line
1307  * @param argv command line arguments
1308  * @return 0 ok, 1 on error
1309  */
1310 int
1311 main (int argc, char *const *argv)
1312 {
1313   int ret;
1314
1315   ret =
1316       (GNUNET_OK ==
1317        GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
1318                            &run, NULL)) ? 0 : 1;
1319   GNUNET_free_non_null (networkIdDirectory);
1320   return ret;
1321 }
1322
1323
1324 /* end of gnunet-service-peerinfo.c */