2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file peerinfo/gnunet-service-peerinfo.c
23 * @brief maintains list of known peers
25 * Code to maintain the list of currently known hosts (in memory
26 * structure of data/hosts/).
28 * @author Christian Grothoff
31 * - HostEntries are never 'free'd (add expiration, upper bound?)
35 #include "gnunet_util_lib.h"
36 #include "gnunet_hello_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_statistics_service.h"
42 * How often do we scan the HOST_DIR for new entries?
44 #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * How often do we discard old entries in data/hosts/?
49 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
53 * In-memory cache of known hosts.
59 * Identity of the peer.
61 struct GNUNET_PeerIdentity identity;
64 * Hello for the peer (can be NULL)
66 struct GNUNET_HELLO_Message *hello;
72 * The in-memory list of known hosts, mapping of
73 * host IDs to 'struct HostEntry*' values.
75 static struct GNUNET_CONTAINER_MultiHashMap *hostmap;
78 * Clients to immediately notify about all changes.
80 static struct GNUNET_SERVER_NotificationContext *notify_list;
83 * Directory where the hellos are stored in (data/hosts)
85 static char *networkIdDirectory;
88 * Handle for reporting statistics.
90 static struct GNUNET_STATISTICS_Handle *stats;
94 * Notify all clients in the notify list about the
95 * given host entry changing.
97 * @param he entry of the host for which we generate a notification
98 * @return generated notification message
100 static struct InfoMessage *
101 make_info_message (const struct HostEntry *he)
103 struct InfoMessage *im;
106 hs = (he->hello == NULL) ? 0 : GNUNET_HELLO_size (he->hello);
107 im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
108 im->header.size = htons (hs + sizeof (struct InfoMessage));
109 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
110 im->peer = he->identity;
111 if (he->hello != NULL)
112 memcpy (&im[1], he->hello, hs);
118 * Address iterator that causes expired entries to be discarded.
120 * @param cls pointer to the current time
121 * @param address the address
122 * @param expiration expiration time for the address
123 * @return GNUNET_NO if expiration smaller than the current time
126 discard_expired (void *cls, const struct GNUNET_HELLO_Address *address,
127 struct GNUNET_TIME_Absolute expiration)
129 const struct GNUNET_TIME_Absolute *now = cls;
131 if (now->abs_value > expiration.abs_value)
133 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
134 _("Removing expired address of transport `%s'\n"),
135 address->transport_name);
143 * Get the filename under which we would store the GNUNET_HELLO_Message
144 * for the given host and protocol.
146 * @param id peer for which we need the filename for the HELLO
147 * @return filename of the form DIRECTORY/HOSTID
150 get_host_filename (const struct GNUNET_PeerIdentity *id)
152 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
155 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
156 GNUNET_asprintf (&fn, "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
162 * Broadcast information about the given entry to all
165 * @param entry entry to broadcast about
168 notify_all (struct HostEntry *entry)
170 struct InfoMessage *msg;
172 msg = make_info_message (entry);
173 GNUNET_SERVER_notification_context_broadcast (notify_list, &msg->header,
180 * Try to read the HELLO in the given filename and discard expired addresses.
182 * @param fn name of the file
183 * @return HELLO of the file, NULL on error
185 static struct GNUNET_HELLO_Message *
186 read_host_file (const char *fn)
188 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
189 const struct GNUNET_HELLO_Message *hello;
190 struct GNUNET_HELLO_Message *hello_clean;
192 struct GNUNET_TIME_Absolute now;
194 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
196 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
197 hello = (const struct GNUNET_HELLO_Message *) buffer;
198 if ((size < sizeof (struct GNUNET_MessageHeader)) ||
199 (size != ntohs ((((const struct GNUNET_MessageHeader *) hello)->size)))
200 || (size != GNUNET_HELLO_size (hello)))
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
203 _("Failed to parse HELLO in file `%s'\n"),
207 now = GNUNET_TIME_absolute_get ();
209 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired,
216 * Add a host to the list.
218 * @param identity the identity of the host
221 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
223 struct HostEntry *entry;
226 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
229 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
231 entry = GNUNET_malloc (sizeof (struct HostEntry));
232 entry->identity = *identity;
233 GNUNET_CONTAINER_multihashmap_put (hostmap, &identity->hashPubKey, entry,
234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
235 fn = get_host_filename (identity);
236 entry->hello = read_host_file (fn);
243 * Remove a file that should not be there. LOG
244 * success or failure.
246 * @param fullname name of the file to remove
249 remove_garbage (const char *fullname)
251 if (0 == UNLINK (fullname))
252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
254 ("File `%s' in directory `%s' does not match naming convention. "
255 "Removed.\n"), fullname, networkIdDirectory);
257 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
263 * Function that is called on each HELLO file in a particular directory.
264 * Try to parse the file and add the HELLO to our list.
266 * @param cls pointer to 'unsigned int' to increment for each file, or NULL
267 * if the file is from a read-only, read-once resource directory
268 * @param fullname name of the file to parse
269 * @return GNUNET_OK (continue iteration)
272 hosts_directory_scan_callback (void *cls, const char *fullname)
274 unsigned int *matched = cls;
275 struct GNUNET_PeerIdentity identity;
276 const char *filename;
277 struct HostEntry *entry;
278 struct GNUNET_HELLO_Message *hello;
280 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
281 return GNUNET_OK; /* ignore non-files */
282 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
285 remove_garbage (fullname);
289 &fullname[strlen (fullname) -
290 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
291 if (filename[-1] != DIR_SEPARATOR)
294 remove_garbage (fullname);
298 GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
300 if (NULL != (hello = read_host_file (filename)))
302 entry = GNUNET_malloc (sizeof (struct HostEntry));
304 GNUNET_HELLO_get_id (hello,
307 GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
309 entry->hello = hello;
316 remove_garbage (fullname);
321 add_host_to_known_hosts (&identity);
327 * Call this method periodically to scan data/hosts for new hosts.
330 * @param tc scheduler context, aborted if reason is shutdown
333 cron_scan_directory_data_hosts (void *cls,
334 const struct GNUNET_SCHEDULER_TaskContext *tc)
336 static unsigned int retries;
339 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
342 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
344 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
345 GNUNET_SCHEDULER_PRIORITY_IDLE,
346 &cron_scan_directory_data_hosts, NULL);
349 GNUNET_DISK_directory_scan (networkIdDirectory,
350 &hosts_directory_scan_callback, &count);
351 if ((0 == count) && (0 == (++retries & 31)))
352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
353 _("Still no peers found in `%s'!\n"), networkIdDirectory);
354 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
355 GNUNET_SCHEDULER_PRIORITY_IDLE,
356 &cron_scan_directory_data_hosts,
362 * Bind a host address (hello) to a hostId.
364 * @param peer the peer for which this is a hello
365 * @param hello the verified (!) hello message
368 bind_address (const struct GNUNET_PeerIdentity *peer,
369 const struct GNUNET_HELLO_Message *hello)
372 struct HostEntry *host;
373 struct GNUNET_HELLO_Message *mrg;
374 struct GNUNET_TIME_Absolute delta;
376 add_host_to_known_hosts (peer);
377 host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
378 GNUNET_assert (host != NULL);
379 if (host->hello == NULL)
381 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
382 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
386 mrg = GNUNET_HELLO_merge (host->hello, hello);
387 delta = GNUNET_HELLO_equals (mrg, host->hello, GNUNET_TIME_absolute_get ());
388 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
393 GNUNET_free (host->hello);
396 fn = get_host_filename (peer);
397 if (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn))
400 GNUNET_DISK_fn_write (fn, host->hello, GNUNET_HELLO_size (host->hello),
401 GNUNET_DISK_PERM_USER_READ |
402 GNUNET_DISK_PERM_USER_WRITE |
403 GNUNET_DISK_PERM_GROUP_READ |
404 GNUNET_DISK_PERM_OTHER_READ))
405 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
414 * Do transmit info about peer to given host.
416 * @param cls NULL to hit all hosts, otherwise specifies a particular target
418 * @param value information to transmit
419 * @return GNUNET_YES (continue to iterate)
422 add_to_tc (void *cls, const GNUNET_HashCode * key, void *value)
424 struct GNUNET_SERVER_TransmitContext *tc = cls;
425 struct HostEntry *pos = value;
426 struct InfoMessage *im;
428 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
431 im = (struct InfoMessage *) buf;
432 if (pos->hello != NULL)
434 hs = GNUNET_HELLO_size (pos->hello);
436 GNUNET_SERVER_MAX_MESSAGE_SIZE -
437 sizeof (struct InfoMessage));
438 memcpy (&im[1], pos->hello, hs);
440 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
441 im->header.size = htons (sizeof (struct InfoMessage) + hs);
442 im->reserved = htonl (0);
443 im->peer = pos->identity;
444 GNUNET_SERVER_transmit_context_append_message (tc, &im->header);
450 * @brief delete expired HELLO entries in data/hosts/
452 * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
453 * @param fn filename to test to see if the HELLO expired
454 * @return GNUNET_OK (continue iteration)
457 discard_hosts_helper (void *cls, const char *fn)
459 struct GNUNET_TIME_Absolute *now = cls;
460 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
461 const struct GNUNET_HELLO_Message *hello;
462 struct GNUNET_HELLO_Message *new_hello;
465 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
466 if (size < sizeof (struct GNUNET_MessageHeader))
468 if (0 != UNLINK (fn))
469 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
470 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
473 hello = (const struct GNUNET_HELLO_Message *) buffer;
475 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
476 if (new_hello != NULL)
478 GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
479 GNUNET_DISK_PERM_USER_READ |
480 GNUNET_DISK_PERM_USER_WRITE |
481 GNUNET_DISK_PERM_GROUP_READ |
482 GNUNET_DISK_PERM_OTHER_READ);
483 GNUNET_free (new_hello);
487 if (0 != UNLINK (fn))
488 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
489 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
496 * Call this method periodically to scan data/hosts for ancient
500 * @param tc scheduler context, aborted if reason is shutdown
503 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
505 struct GNUNET_TIME_Absolute now;
507 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
509 now = GNUNET_TIME_absolute_get ();
510 GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
511 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
517 * Handle HELLO-message.
520 * @param client identification of the client
521 * @param message the actual message
524 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
525 const struct GNUNET_MessageHeader *message)
527 const struct GNUNET_HELLO_Message *hello;
528 struct GNUNET_PeerIdentity pid;
530 hello = (const struct GNUNET_HELLO_Message *) message;
531 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
539 "HELLO", GNUNET_i2s (&pid));
541 bind_address (&pid, hello);
542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
547 * Handle GET-message.
550 * @param client identification of the client
551 * @param message the actual message
554 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
555 const struct GNUNET_MessageHeader *message)
557 const struct ListPeerMessage *lpm;
558 struct GNUNET_SERVER_TransmitContext *tc;
560 lpm = (const struct ListPeerMessage *) message;
561 GNUNET_break (0 == ntohl (lpm->reserved));
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
564 "GET", GNUNET_i2s (&lpm->peer));
566 tc = GNUNET_SERVER_transmit_context_create (client);
567 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
569 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
570 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
571 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
576 * Handle GET-ALL-message.
579 * @param client identification of the client
580 * @param message the actual message
583 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
584 const struct GNUNET_MessageHeader *message)
586 struct GNUNET_SERVER_TransmitContext *tc;
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
591 tc = GNUNET_SERVER_transmit_context_create (client);
592 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, tc);
593 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
594 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
595 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
603 do_notify_entry (void *cls, const GNUNET_HashCode * key, void *value)
605 struct GNUNET_SERVER_Client *client = cls;
606 struct HostEntry *he = value;
607 struct InfoMessage *msg;
609 msg = make_info_message (he);
610 GNUNET_SERVER_notification_context_unicast (notify_list, client, &msg->header,
618 * Handle NOTIFY-message.
621 * @param client identification of the client
622 * @param message the actual message
625 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
626 const struct GNUNET_MessageHeader *message)
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
631 GNUNET_SERVER_notification_context_add (notify_list, client);
632 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, client);
633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
641 free_host_entry (void *cls, const GNUNET_HashCode * key, void *value)
643 struct HostEntry *he = value;
645 GNUNET_free_non_null (he->hello);
652 * Clean up our state. Called during shutdown.
655 * @param tc scheduler task context, unused
658 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
660 GNUNET_SERVER_notification_context_destroy (notify_list);
662 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
663 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
666 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
673 * Start up peerinfo service.
676 * @param server the initialized server
677 * @param cfg configuration to use
680 run (void *cls, struct GNUNET_SERVER_Handle *server,
681 const struct GNUNET_CONFIGURATION_Handle *cfg)
683 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
684 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
685 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
686 sizeof (struct ListPeerMessage)},
687 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
688 sizeof (struct GNUNET_MessageHeader)},
689 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
690 sizeof (struct GNUNET_MessageHeader)},
696 hostmap = GNUNET_CONTAINER_multihashmap_create (1024);
697 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
698 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
699 GNUNET_assert (GNUNET_OK ==
700 GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
702 &networkIdDirectory));
703 GNUNET_DISK_directory_create (networkIdDirectory);
704 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
705 &cron_scan_directory_data_hosts, NULL);
706 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
707 &cron_clean_data_hosts, NULL);
708 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
710 GNUNET_SERVER_add_handlers (server, handlers);
711 ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
712 GNUNET_asprintf (&peerdir,
716 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
717 _("Importing HELLOs from `%s'\n"),
719 GNUNET_DISK_directory_scan (peerdir,
720 &hosts_directory_scan_callback, NULL);
721 GNUNET_free (peerdir);
726 * The main function for the peerinfo service.
728 * @param argc number of arguments from the command line
729 * @param argv command line arguments
730 * @return 0 ok, 1 on error
733 main (int argc, char *const *argv)
739 GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
740 &run, NULL)) ? 0 : 1;
741 GNUNET_free_non_null (networkIdDirectory);
746 /* end of gnunet-service-peerinfo.c */