fix mem leak and counting valid addresses
[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
278         for (cur = nc_head; NULL != cur; cur = cur->next)
279         {
280                 if (GNUNET_NO == cur->include_friend_only)
281                 {
282                         GNUNET_SERVER_notification_context_unicast (notify_list,
283                                                                                                                                                                                                         cur->client,
284                                                                                                                                                                                                         &msg_pub->header,
285                                                                                                                                                                                                         GNUNET_NO);
286                 }
287                 if (GNUNET_YES == cur->include_friend_only)
288                 {
289                         GNUNET_SERVER_notification_context_unicast (notify_list,
290                                                                                                                                                                                                         cur->client,
291                                                                                                                                                                                                         &msg_friend->header,
292                                                                                                                                                                                                         GNUNET_NO);
293                 }
294         }
295   GNUNET_free (msg_pub);
296   GNUNET_free (msg_friend);
297 }
298
299
300 /**
301  * Bind a host address (hello) to a hostId.
302  *
303  * @param peer the peer for which this is a hello
304  * @param hello the verified (!) hello message
305  */
306 static void
307 update_hello (const struct GNUNET_PeerIdentity *peer,
308               const struct GNUNET_HELLO_Message *hello);
309
310
311 /**
312  * Try to read the HELLOs in the given filename and discard expired
313  * addresses.  Removes the file if one the HELLO is mal-formed.  If all
314  * addresses are expired, the HELLO is also removed (but the HELLO
315  * with the public key is still returned if it was found and valid).
316  * 
317  * The file can contain up to two HELLO messages, a public and a friend only
318  * HELLO
319  *
320  * @param fn name of the file
321  * @param unlink_garbage if GNUNET_YES, try to remove useless files
322  */
323 static void
324 read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r)
325 {
326   char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
327   const struct GNUNET_HELLO_Message *hello_1st;
328   const struct GNUNET_HELLO_Message *hello_2nd;
329   struct GNUNET_HELLO_Message *hello_clean_1st;
330   struct GNUNET_HELLO_Message *hello_clean_2nd;
331   int size_1st;
332   int size_2nd;
333   int size_total;
334   struct GNUNET_TIME_Absolute now;
335   unsigned int left;
336
337   hello_1st = NULL;
338   hello_2nd = NULL;
339   hello_clean_1st = NULL;
340   hello_clean_2nd = NULL;
341   size_1st = 0;
342   size_2nd = 0;
343   size_total = 0;
344   r->friend_only_hello = NULL;
345   r->hello = NULL;
346
347   if (GNUNET_YES != GNUNET_DISK_file_test (fn))
348   {
349     return;
350   }
351
352   size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from `%s'\n", size_total, fn);
354   if (size_total < sizeof (struct GNUNET_MessageHeader))
355   {
356             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
357                                                                         _("Failed to parse HELLO in file `%s': %s\n"),
358                                                                         fn, "Fail has invalid size");
359     if ( (GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) )
360       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
361     return;
362   }
363
364   hello_1st = (const struct GNUNET_HELLO_Message *) buffer;
365   size_1st = ntohs (((struct GNUNET_MessageHeader *) &buffer)->size);
366   if (size_1st < sizeof (struct GNUNET_MessageHeader))
367   {
368             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369                                                                         _("Failed to parse HELLO in file `%s': %s %u \n"),
370                                                                         fn, "1st HELLO has invalid size of ", size_1st);
371     if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
372       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
373     return;
374   }
375         if (size_1st != GNUNET_HELLO_size (hello_1st))
376         {
377     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378                                                                 _("Failed to parse HELLO in file `%s': %s \n"),
379                                                                 fn, "1st HELLO is invalid");
380     if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
381         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
382     return;
383         }
384
385   if (size_total > size_1st)
386   {
387                 hello_2nd = (const struct GNUNET_HELLO_Message *) &buffer[size_1st];
388                 size_2nd = ntohs (((const struct GNUNET_MessageHeader *) hello_2nd)->size);
389           if ((size_2nd < sizeof (struct GNUNET_MessageHeader)) ||
390                         (size_2nd != GNUNET_HELLO_size (hello_2nd)))
391           {
392             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393                                                                                 _("Failed to parse HELLO in file `%s': %s\n"),
394                                                                                 fn, "2nd HELLO has wrong size");
395             if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
396               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
397             return;
398           }
399   }
400
401   if (size_total != (size_1st + size_2nd))
402   {
403             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404                                                         _("Failed to parse HELLO in file `%s': %s\n"),
405                                                         fn, "Multiple HELLOs but total size is wrong");
406             if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
407               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
408             return;
409   }
410
411   now = GNUNET_TIME_absolute_get ();
412   hello_clean_1st = GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_YES,
413                                                                                                                                                                                                   &discard_expired, &now);
414   left = 0;
415   (void) GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_NO,
416                                                                                                                                                          &count_addresses, &left);
417   if (0 == left)
418   {
419     /* no addresses left, remove from disk */
420     if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
421       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
422   }
423
424   if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_1st))
425   {
426         if (NULL == r->hello)
427                 r->hello = hello_clean_1st;
428         else
429         {
430                         GNUNET_break (0);
431                         GNUNET_free (r->hello);
432                         r->hello = hello_clean_1st;
433         }
434   }
435   else
436   {
437         if (NULL == r->friend_only_hello)
438                 r->friend_only_hello = hello_clean_1st;
439         else
440         {
441                         GNUNET_break (0);
442                         GNUNET_free (r->friend_only_hello);
443                         r->friend_only_hello = hello_clean_1st;
444         }
445   }
446
447   if (NULL != hello_2nd)
448   {
449           hello_clean_2nd = GNUNET_HELLO_iterate_addresses (hello_2nd, GNUNET_YES,
450                                                                                                                                                                                                           &discard_expired, &now);
451           left = 0;
452           (void) GNUNET_HELLO_iterate_addresses (hello_clean_2nd, GNUNET_NO,
453                                                                                                                                                                  &count_addresses, &left);
454           if (0 == left)
455           {
456             /* no addresses left, remove from disk */
457             if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
458               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
459           }
460
461           if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_2nd))
462           {
463                 if (NULL == r->hello)
464                         r->hello = hello_clean_2nd;
465                 else
466                 {
467                                 GNUNET_break (0);
468                                 GNUNET_free (r->hello);
469                                 r->hello = hello_clean_2nd;
470                 }
471           }
472           else
473           {
474                 if (NULL == r->friend_only_hello)
475                         r->friend_only_hello = hello_clean_2nd;
476                 else
477                 {
478                                 GNUNET_break (0);
479                                 GNUNET_free (r->friend_only_hello);
480                                 r->friend_only_hello = hello_clean_2nd;
481                 }
482           }
483   }
484
485   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' and `%s' HELLO message in file\n",
486                 (NULL != r->hello) ? "public" : "NO public",
487                         (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
488
489
490 }
491
492
493 /**
494  * Add a host to the list and notify clients about this event
495  *
496  * @param identity the identity of the host
497  * @return the HostEntry
498  */
499 static struct HostEntry *
500 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
501 {
502   struct HostEntry *entry;
503   struct ReadHostFileContext r;
504   char *fn;
505
506   entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
507   if (NULL == entry)
508   {
509                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n", GNUNET_i2s (identity));
510         GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
511                             GNUNET_NO);
512         entry = GNUNET_malloc (sizeof (struct HostEntry));
513         entry->identity = *identity;
514         GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
515                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
516
517     fn = get_host_filename (identity);
518     if (NULL != fn)
519     {
520       read_host_file (fn, GNUNET_YES, &r);
521       if (NULL != r.hello)
522         update_hello (identity, r.hello);
523       if (NULL != r.friend_only_hello)
524         update_hello (identity, r.friend_only_hello);
525       GNUNET_free_non_null (r.hello);
526       GNUNET_free_non_null (r.friend_only_hello);
527       GNUNET_free (fn);
528     }
529     notify_all (entry);
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_DISK_directory_scan (networkIdDirectory,
701                               &hosts_directory_scan_callback, &dsc);
702   if ((0 == dsc.matched) && (0 == (++retries & 31)))
703     GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
704                 _("Still no peers found in `%s'!\n"), networkIdDirectory);
705   GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ, 
706                                               GNUNET_SCHEDULER_PRIORITY_IDLE,
707                                               &cron_scan_directory_data_hosts,
708                                               NULL);
709 }
710
711
712 static struct GNUNET_HELLO_Message *
713 update_friend_hello (const struct GNUNET_HELLO_Message *hello,
714                                                                                  const struct GNUNET_HELLO_Message *friend_hello)
715 {
716         struct GNUNET_HELLO_Message * res;
717         struct GNUNET_HELLO_Message * tmp;
718         struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
719
720         if (NULL != friend_hello)
721         {
722                 res = GNUNET_HELLO_merge (hello, friend_hello);
723                 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
724                 return res;
725         }
726
727         GNUNET_HELLO_get_key (hello, &pk);
728         tmp = GNUNET_HELLO_create (&pk, NULL, NULL, GNUNET_YES);
729         res = GNUNET_HELLO_merge (hello, tmp);
730         GNUNET_free (tmp);
731         GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
732         return res;
733 }
734
735
736
737 /**
738  * Bind a host address (hello) to a hostId.
739  *
740  * @param peer the peer for which this is a hello
741  * @param hello the verified (!) hello message
742  */
743 static void
744 update_hello (const struct GNUNET_PeerIdentity *peer,
745               const struct GNUNET_HELLO_Message *hello)
746 {
747   char *fn;
748   struct HostEntry *host;
749   struct GNUNET_HELLO_Message *mrg;
750   struct GNUNET_HELLO_Message **dest;
751   struct GNUNET_TIME_Absolute delta;
752   unsigned int cnt;
753   unsigned int size;
754   int friend_hello_type;
755   int store_hello;
756   int store_friend_hello;
757   int pos;
758   char *buffer;
759
760   host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
761   GNUNET_assert (NULL != host);
762
763   friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
764         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating %s HELLO for `%s'\n",
765                         (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
766                         GNUNET_i2s (peer));
767
768   dest = NULL;
769   if (GNUNET_YES == friend_hello_type)
770   {
771         dest = &host->friend_only_hello;
772   }
773   else
774   {
775         dest = &host->hello;
776   }
777
778   if (NULL == (*dest))
779   {
780         (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
781     memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
782   }
783   else
784   {
785     mrg = GNUNET_HELLO_merge ((*dest), hello);
786     delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ());
787     if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
788     {
789       /* no differences, just ignore the update */
790       GNUNET_free (mrg);
791       return;
792     }
793     GNUNET_free ((*dest));
794     (*dest) = mrg;
795   }
796
797   if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type))
798   {
799                 /* Update friend only hello */
800                 mrg = update_friend_hello (host->hello, host->friend_only_hello);
801                 if (NULL != host->friend_only_hello)
802                         GNUNET_free (host->friend_only_hello);
803                 host->friend_only_hello = mrg;
804   }
805
806   if (NULL != host->hello)
807         GNUNET_assert ((GNUNET_NO == GNUNET_HELLO_is_friend_only (host->hello)));
808   if (NULL != host->friend_only_hello)
809         GNUNET_assert ((GNUNET_YES == GNUNET_HELLO_is_friend_only(host->friend_only_hello)));
810
811   store_hello = GNUNET_NO;
812   store_friend_hello = GNUNET_NO;
813         fn = get_host_filename (peer);
814         if ( (NULL != fn) &&
815                          (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn)) )
816                 {
817
818                         store_hello = GNUNET_NO;
819                         size = 0;
820                         cnt = 0;
821                         if (NULL != host->hello)
822                                 (void) GNUNET_HELLO_iterate_addresses (host->hello,
823                                                                         GNUNET_NO, &count_addresses, &cnt);
824                         if (cnt > 0)
825                         {
826                                 store_hello = GNUNET_YES;
827                                 size += GNUNET_HELLO_size (host->hello);
828                         }
829                         cnt = 0;
830                         if (NULL != host->friend_only_hello)
831                                 (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello, GNUNET_NO,
832                                                                                 &count_addresses, &cnt);
833                         if (0 < cnt)
834                         {
835                                 store_friend_hello = GNUNET_YES;
836                                 size += GNUNET_HELLO_size (host->friend_only_hello);
837                         }
838
839                         if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello))
840                         {
841                                 /* no valid addresses, don't put HELLO on disk; in fact,
842                                          if one exists on disk, remove it */
843                                 (void) UNLINK (fn);
844                         }
845                         else
846                         {
847                                 buffer = GNUNET_malloc (size);
848                                 pos = 0;
849
850                                 if (GNUNET_YES == store_hello)
851                                 {
852                                         memcpy (buffer, host->hello, GNUNET_HELLO_size (host->hello));
853                                         pos += GNUNET_HELLO_size (host->hello);
854                                 }
855                                 if (GNUNET_YES == store_friend_hello)
856                                 {
857                                         memcpy (&buffer[pos], host->friend_only_hello, GNUNET_HELLO_size (host->friend_only_hello));
858                                         pos += GNUNET_HELLO_size (host->friend_only_hello);
859                                 }
860                                 GNUNET_assert (pos == size);
861
862                                 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
863                                         GNUNET_DISK_PERM_USER_READ |
864                                         GNUNET_DISK_PERM_USER_WRITE |
865                                         GNUNET_DISK_PERM_GROUP_READ |
866                                         GNUNET_DISK_PERM_OTHER_READ))
867                                         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
868                                 else
869                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stored %s %s HELLO in %s  with total size %u\n",
870                                                         (GNUNET_YES == store_friend_hello) ? "friend-only": "",
871                                                         (GNUNET_YES == store_hello) ? "public": "",
872                                                         fn, size);
873                                 GNUNET_free (buffer);
874                         }
875   }
876         GNUNET_free_non_null (fn);
877   notify_all (host);
878 }
879
880
881 /**
882  * Do transmit info about peer to given host.
883  *
884  * @param cls NULL to hit all hosts, otherwise specifies a particular target
885  * @param key hostID
886  * @param value information to transmit
887  * @return GNUNET_YES (continue to iterate)
888  */
889 static int
890 add_to_tc (void *cls, const struct GNUNET_HashCode * key, void *value)
891 {
892   struct TransmitContext *tc = cls;
893   struct HostEntry *pos = value;
894   struct InfoMessage *im;
895   uint16_t hs;
896   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
897
898   hs = 0;
899   im = (struct InfoMessage *) buf;
900
901   if ((pos->hello != NULL) && (GNUNET_NO == tc->friend_only))
902   {
903         /* Copy public HELLO */
904     hs = GNUNET_HELLO_size (pos->hello);
905     GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
906                    sizeof (struct InfoMessage));
907     memcpy (&im[1], pos->hello, hs);
908     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding public HELLO with size %u for peer `%s'\n",
909                 hs, GNUNET_h2s (key));
910   }
911   else if ((pos->friend_only_hello != NULL) && (GNUNET_YES == tc->friend_only))
912   {
913         /* Copy friend only HELLO */
914     hs = GNUNET_HELLO_size (pos->friend_only_hello);
915     GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
916                    sizeof (struct InfoMessage));
917     memcpy (&im[1], pos->friend_only_hello, hs);
918     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding public HELLO with size %u for peer `%s'\n",
919                 hs, GNUNET_h2s (key));
920   }
921   else
922   {
923       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding no HELLO for peer `%s'\n",
924                  GNUNET_h2s (key));
925   }
926
927   im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
928   im->header.size = htons (sizeof (struct InfoMessage) + hs);
929   im->reserved = htonl (0);
930   im->peer = pos->identity;
931   GNUNET_SERVER_transmit_context_append_message (tc->tc, &im->header);
932   return GNUNET_YES;
933 }
934
935
936 /**
937  * @brief delete expired HELLO entries in directory
938  *
939  * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
940  * @param fn filename to test to see if the HELLO expired
941  * @return GNUNET_OK (continue iteration)
942  */
943 static int
944 discard_hosts_helper (void *cls, const char *fn)
945 {
946   struct GNUNET_TIME_Absolute *now = cls;
947   char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
948   const struct GNUNET_HELLO_Message *hello;
949   struct GNUNET_HELLO_Message *new_hello;
950   int size;
951   unsigned int cnt;
952
953   size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
954   if (size < sizeof (struct GNUNET_MessageHeader))
955   {
956     if (0 != UNLINK (fn))
957       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
958                                 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
959     return GNUNET_OK;
960   }
961   hello = (const struct GNUNET_HELLO_Message *) buffer;
962   new_hello =
963     GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
964   cnt = 0;
965   if (NULL != new_hello)
966     (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses, &cnt);
967   if ( (NULL != new_hello) && (0 < cnt) )
968   {
969     GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
970                           GNUNET_DISK_PERM_USER_READ |
971                           GNUNET_DISK_PERM_USER_WRITE |
972                           GNUNET_DISK_PERM_GROUP_READ |
973                           GNUNET_DISK_PERM_OTHER_READ);
974   }
975   else
976   {
977     if (0 != UNLINK (fn))
978       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
979                                 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
980   }
981   GNUNET_free_non_null (new_hello);
982   return GNUNET_OK;
983 }
984
985
986 /**
987  * Call this method periodically to scan data/hosts for ancient
988  * HELLOs to expire.
989  *
990  * @param cls unused
991  * @param tc scheduler context, aborted if reason is shutdown
992  */
993 static void
994 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
995 {
996   struct GNUNET_TIME_Absolute now;
997
998   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
999     return;
1000   now = GNUNET_TIME_absolute_get ();
1001   GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
1002   GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
1003                                 NULL);
1004 }
1005
1006
1007 /**
1008  * Handle HELLO-message.
1009  *
1010  * @param cls closure
1011  * @param client identification of the client
1012  * @param message the actual message
1013  */
1014 static void
1015 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
1016               const struct GNUNET_MessageHeader *message)
1017 {
1018   const struct GNUNET_HELLO_Message *hello;
1019   struct GNUNET_PeerIdentity pid;
1020
1021   hello = (const struct GNUNET_HELLO_Message *) message;
1022   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
1023   {
1024     GNUNET_break (0);
1025     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1026     return;
1027   }
1028   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1029               "HELLO", GNUNET_i2s (&pid));
1030   add_host_to_known_hosts (&pid);
1031   update_hello (&pid, hello);
1032   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1033 }
1034
1035
1036 /**
1037  * Handle GET-message.
1038  *
1039  * @param cls closure
1040  * @param client identification of the client
1041  * @param message the actual message
1042  */
1043 static void
1044 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
1045             const struct GNUNET_MessageHeader *message)
1046 {
1047   const struct ListPeerMessage *lpm;
1048   struct TransmitContext tcx;
1049
1050   lpm = (const struct ListPeerMessage *) message;
1051   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1052               "GET", GNUNET_i2s (&lpm->peer));
1053   tcx.friend_only = ntohl (lpm->include_friend_only);
1054   tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1055   GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
1056                                               &add_to_tc, &tcx);
1057   GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1058                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1059   GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1060 }
1061
1062
1063 /**
1064  * Handle GET-ALL-message.
1065  *
1066  * @param cls closure
1067  * @param client identification of the client
1068  * @param message the actual message
1069  */
1070 static void
1071 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
1072                 const struct GNUNET_MessageHeader *message)
1073 {
1074   const struct ListAllPeersMessage *lapm;
1075   struct TransmitContext tcx;
1076
1077   lapm = (const struct ListAllPeersMessage *) message;
1078   tcx.friend_only = ntohl (lapm->include_friend_only);
1079   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
1080   tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1081   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, &tcx);
1082   GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1083                                               GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1084   GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1085 }
1086
1087
1088
1089 /**
1090  * Pass the given client the information we have in the respective
1091  * host entry; the client is already in the notification context.
1092  *
1093  * @param cls the 'struct GNUNET_SERVER_Client' to notify
1094  * @param key key for the value (unused)
1095  * @param value the 'struct HostEntry' to notify the client about
1096  * @return GNUNET_YES (always, continue to iterate)
1097  */
1098 static int
1099 do_notify_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1100 {
1101         struct NotificationContext *nc = cls;
1102   struct HostEntry *he = value;
1103   struct InfoMessage *msg;
1104
1105
1106         if ((NULL == he->hello) && (GNUNET_NO == nc->include_friend_only))
1107         {
1108                 /* We have no public hello  */
1109           return GNUNET_YES;
1110         }
1111
1112
1113         if ((NULL == he->friend_only_hello) && GNUNET_YES == nc->include_friend_only)
1114         {
1115                 /* We have no friend hello */
1116           return GNUNET_YES;
1117         }
1118
1119         msg = make_info_message (he, nc->include_friend_only);
1120         GNUNET_SERVER_notification_context_unicast (notify_list,
1121                         nc->client,
1122                         &msg->header,
1123                         GNUNET_NO);
1124   GNUNET_free (msg);
1125   return GNUNET_YES;
1126 }
1127
1128
1129 /**
1130  * Handle NOTIFY-message.
1131  *
1132  * @param cls closure
1133  * @param client identification of the client
1134  * @param message the actual message
1135  */
1136 static void
1137 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
1138                const struct GNUNET_MessageHeader *message)
1139 {
1140   struct NotifyMessage *nm = (struct NotifyMessage *) message;
1141   struct NotificationContext *nc;
1142
1143         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
1144
1145         nc = GNUNET_malloc (sizeof (struct NotificationContext));
1146         nc->client = client;
1147         nc->include_friend_only = ntohl (nm->include_friend_only);
1148
1149         GNUNET_CONTAINER_DLL_insert (nc_head, nc_tail, nc);
1150   GNUNET_SERVER_client_mark_monitor (client);
1151         GNUNET_SERVER_notification_context_add (notify_list, client);
1152   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, nc);
1153   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1154 }
1155
1156
1157 /**
1158  * Client disconnect callback
1159  *
1160  * @param cls unused
1161  * @param client server client
1162  */
1163 static void disconnect_cb (void *cls,struct GNUNET_SERVER_Client *client)
1164 {
1165         struct NotificationContext *cur;
1166
1167         for (cur = nc_head; NULL != cur; cur = cur->next)
1168                 if (cur->client == client)
1169                         break;
1170
1171         if (NULL == cur)
1172                 return;
1173
1174         GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1175         GNUNET_free (cur);
1176 }
1177
1178
1179 /**
1180  * Release memory taken by a host entry.
1181  *
1182  * @param cls NULL
1183  * @param key key of the host entry
1184  * @param value the 'struct HostEntry' to free
1185  * @return GNUNET_YES (continue to iterate)
1186  */
1187 static int
1188 free_host_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1189 {
1190   struct HostEntry *he = value;
1191
1192   GNUNET_free_non_null (he->hello);
1193   GNUNET_free_non_null (he->friend_only_hello);
1194   GNUNET_free (he);
1195   return GNUNET_YES;
1196 }
1197
1198
1199 /**
1200  * Clean up our state.  Called during shutdown.
1201  *
1202  * @param cls unused
1203  * @param tc scheduler task context, unused
1204  */
1205 static void
1206 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1207 {
1208         struct NotificationContext *cur;
1209         struct NotificationContext *next;
1210         GNUNET_SERVER_notification_context_destroy (notify_list);
1211   notify_list = NULL;
1212
1213         for (cur = nc_head; NULL != cur; cur = next)
1214         {
1215                         next = cur->next;
1216                         GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1217                         GNUNET_free (cur);
1218         }
1219
1220   GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
1221   GNUNET_CONTAINER_multihashmap_destroy (hostmap);
1222   if (NULL != stats)
1223   {
1224     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1225     stats = NULL;
1226   }
1227 }
1228
1229
1230 /**
1231  * Start up peerinfo service.
1232  *
1233  * @param cls closure
1234  * @param server the initialized server
1235  * @param cfg configuration to use
1236  */
1237 static void
1238 run (void *cls, struct GNUNET_SERVER_Handle *server,
1239      const struct GNUNET_CONFIGURATION_Handle *cfg)
1240 {
1241   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1242     {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
1243     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
1244      sizeof (struct ListPeerMessage)},
1245     {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
1246      sizeof (struct ListAllPeersMessage)},
1247     {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
1248      sizeof (struct NotifyMessage)},
1249     {NULL, NULL, 0, 0}
1250   };
1251   char *peerdir;
1252   char *ip;
1253   struct DirScanContext dsc;
1254   int noio;
1255
1256   hostmap = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1257   stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
1258   notify_list = GNUNET_SERVER_notification_context_create (server, 0);
1259   noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO");
1260   if (GNUNET_YES != noio)
1261   {
1262     GNUNET_assert (GNUNET_OK ==
1263                    GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
1264                                                             "HOSTS",
1265                                                             &networkIdDirectory));
1266     GNUNET_DISK_directory_create (networkIdDirectory);
1267 #if 0
1268     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1269                                         &cron_scan_directory_data_hosts, NULL); /* CHECK */
1270
1271     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1272                                         &cron_clean_data_hosts, NULL); /* CHECK */
1273 #endif
1274     ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1275     GNUNET_asprintf (&peerdir,
1276                      "%shellos",
1277                      ip);
1278     GNUNET_free (ip);
1279
1280     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1281                 _("Importing HELLOs from `%s'\n"),
1282                 peerdir);
1283     dsc.matched = 0;
1284     dsc.remove_files = GNUNET_NO;
1285
1286     GNUNET_DISK_directory_scan (peerdir,
1287                                 &hosts_directory_scan_callback, &dsc);
1288
1289     GNUNET_free (peerdir);
1290   }
1291   GNUNET_SERVER_add_handlers (server, handlers);
1292   GNUNET_SERVER_disconnect_notify (server, &disconnect_cb, NULL) ;
1293   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1294                                 NULL);
1295
1296 }
1297
1298
1299 /**
1300  * The main function for the peerinfo service.
1301  *
1302  * @param argc number of arguments from the command line
1303  * @param argv command line arguments
1304  * @return 0 ok, 1 on error
1305  */
1306 int
1307 main (int argc, char *const *argv)
1308 {
1309   int ret;
1310
1311   ret =
1312       (GNUNET_OK ==
1313        GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
1314                            &run, NULL)) ? 0 : 1;
1315   GNUNET_free_non_null (networkIdDirectory);
1316   return ret;
1317 }
1318
1319
1320 /* end of gnunet-service-peerinfo.c */