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 * Add a host to the list.
182 * @param identity the identity of the host
185 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
187 struct HostEntry *entry;
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;
195 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
198 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
200 entry = GNUNET_malloc (sizeof (struct HostEntry));
201 entry->identity = *identity;
203 fn = get_host_filename (identity);
204 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
206 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
207 hello = (const struct GNUNET_HELLO_Message *) buffer;
208 if ((size < sizeof (struct GNUNET_MessageHeader)) ||
209 (size != ntohs ((((const struct GNUNET_MessageHeader *) hello)->size)))
210 || (size != GNUNET_HELLO_size (hello)))
213 if (0 != UNLINK (fn))
214 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
218 now = GNUNET_TIME_absolute_get ();
220 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired,
222 entry->hello = hello_clean;
226 GNUNET_CONTAINER_multihashmap_put (hostmap, &identity->hashPubKey, entry,
227 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
233 * Remove a file that should not be there. LOG
234 * success or failure.
236 * @param fullname name of the file to remove
239 remove_garbage (const char *fullname)
241 if (0 == UNLINK (fullname))
242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
244 ("File `%s' in directory `%s' does not match naming convention. "
245 "Removed.\n"), fullname, networkIdDirectory);
247 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
253 * Function that is called on each HELLO file in a particular directory.
254 * Try to parse the file and add the HELLO to our list.
256 * @param cls pointer to 'unsigned int' to increment for each file, or NULL
257 * if the file is from a read-only, read-once resource directory
258 * @param fullname name of the file to parse
259 * @return GNUNET_OK (continue iteration)
262 hosts_directory_scan_callback (void *cls, const char *fullname)
264 unsigned int *matched = cls;
265 struct GNUNET_PeerIdentity identity;
266 const char *filename;
268 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
269 return GNUNET_OK; /* ignore non-files */
270 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
273 remove_garbage (fullname);
277 &fullname[strlen (fullname) -
278 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
279 if (filename[-1] != DIR_SEPARATOR)
282 remove_garbage (fullname);
286 GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
289 remove_garbage (fullname);
294 add_host_to_known_hosts (&identity);
300 * Call this method periodically to scan data/hosts for new hosts.
303 * @param tc scheduler context, aborted if reason is shutdown
306 cron_scan_directory_data_hosts (void *cls,
307 const struct GNUNET_SCHEDULER_TaskContext *tc)
309 static unsigned int retries;
312 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
315 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
317 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
318 GNUNET_SCHEDULER_PRIORITY_IDLE,
319 &cron_scan_directory_data_hosts, NULL);
322 GNUNET_DISK_directory_scan (networkIdDirectory,
323 &hosts_directory_scan_callback, &count);
324 if ((0 == count) && (0 == (++retries & 31)))
325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
326 _("Still no peers found in `%s'!\n"), networkIdDirectory);
327 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
328 GNUNET_SCHEDULER_PRIORITY_IDLE,
329 &cron_scan_directory_data_hosts,
335 * Bind a host address (hello) to a hostId.
337 * @param peer the peer for which this is a hello
338 * @param hello the verified (!) hello message
341 bind_address (const struct GNUNET_PeerIdentity *peer,
342 const struct GNUNET_HELLO_Message *hello)
345 struct HostEntry *host;
346 struct GNUNET_HELLO_Message *mrg;
347 struct GNUNET_TIME_Absolute delta;
349 add_host_to_known_hosts (peer);
350 host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
351 GNUNET_assert (host != NULL);
352 if (host->hello == NULL)
354 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
355 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
359 mrg = GNUNET_HELLO_merge (host->hello, hello);
360 delta = GNUNET_HELLO_equals (mrg, host->hello, GNUNET_TIME_absolute_get ());
361 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
366 GNUNET_free (host->hello);
369 fn = get_host_filename (peer);
370 if (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn))
373 GNUNET_DISK_fn_write (fn, host->hello, GNUNET_HELLO_size (host->hello),
374 GNUNET_DISK_PERM_USER_READ |
375 GNUNET_DISK_PERM_USER_WRITE |
376 GNUNET_DISK_PERM_GROUP_READ |
377 GNUNET_DISK_PERM_OTHER_READ))
378 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
387 * Do transmit info about peer to given host.
389 * @param cls NULL to hit all hosts, otherwise specifies a particular target
391 * @param value information to transmit
392 * @return GNUNET_YES (continue to iterate)
395 add_to_tc (void *cls, const GNUNET_HashCode * key, void *value)
397 struct GNUNET_SERVER_TransmitContext *tc = cls;
398 struct HostEntry *pos = value;
399 struct InfoMessage *im;
401 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
404 im = (struct InfoMessage *) buf;
405 if (pos->hello != NULL)
407 hs = GNUNET_HELLO_size (pos->hello);
409 GNUNET_SERVER_MAX_MESSAGE_SIZE -
410 sizeof (struct InfoMessage));
411 memcpy (&im[1], pos->hello, hs);
413 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
414 im->header.size = htons (sizeof (struct InfoMessage) + hs);
415 im->reserved = htonl (0);
416 im->peer = pos->identity;
417 GNUNET_SERVER_transmit_context_append_message (tc, &im->header);
423 * @brief delete expired HELLO entries in data/hosts/
425 * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
426 * @param fn filename to test to see if the HELLO expired
427 * @return GNUNET_OK (continue iteration)
430 discard_hosts_helper (void *cls, const char *fn)
432 struct GNUNET_TIME_Absolute *now = cls;
433 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
434 const struct GNUNET_HELLO_Message *hello;
435 struct GNUNET_HELLO_Message *new_hello;
438 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
439 if (size < sizeof (struct GNUNET_MessageHeader))
441 if (0 != UNLINK (fn))
442 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
443 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
446 hello = (const struct GNUNET_HELLO_Message *) buffer;
448 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
449 if (new_hello != NULL)
451 GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
452 GNUNET_DISK_PERM_USER_READ |
453 GNUNET_DISK_PERM_USER_WRITE |
454 GNUNET_DISK_PERM_GROUP_READ |
455 GNUNET_DISK_PERM_OTHER_READ);
456 GNUNET_free (new_hello);
460 if (0 != UNLINK (fn))
461 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
462 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
469 * Call this method periodically to scan data/hosts for ancient
473 * @param tc scheduler context, aborted if reason is shutdown
476 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
478 struct GNUNET_TIME_Absolute now;
480 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
482 now = GNUNET_TIME_absolute_get ();
483 GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
484 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
490 * Handle HELLO-message.
493 * @param client identification of the client
494 * @param message the actual message
497 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
498 const struct GNUNET_MessageHeader *message)
500 const struct GNUNET_HELLO_Message *hello;
501 struct GNUNET_PeerIdentity pid;
503 hello = (const struct GNUNET_HELLO_Message *) message;
504 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
512 "HELLO", GNUNET_i2s (&pid));
514 bind_address (&pid, hello);
515 GNUNET_SERVER_receive_done (client, GNUNET_OK);
520 * Handle GET-message.
523 * @param client identification of the client
524 * @param message the actual message
527 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
528 const struct GNUNET_MessageHeader *message)
530 const struct ListPeerMessage *lpm;
531 struct GNUNET_SERVER_TransmitContext *tc;
533 lpm = (const struct ListPeerMessage *) message;
534 GNUNET_break (0 == ntohl (lpm->reserved));
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
537 "GET", GNUNET_i2s (&lpm->peer));
539 tc = GNUNET_SERVER_transmit_context_create (client);
540 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
542 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
543 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
544 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
549 * Handle GET-ALL-message.
552 * @param client identification of the client
553 * @param message the actual message
556 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
557 const struct GNUNET_MessageHeader *message)
559 struct GNUNET_SERVER_TransmitContext *tc;
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
564 tc = GNUNET_SERVER_transmit_context_create (client);
565 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, tc);
566 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
567 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
568 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
576 do_notify_entry (void *cls, const GNUNET_HashCode * key, void *value)
578 struct GNUNET_SERVER_Client *client = cls;
579 struct HostEntry *he = value;
580 struct InfoMessage *msg;
582 msg = make_info_message (he);
583 GNUNET_SERVER_notification_context_unicast (notify_list, client, &msg->header,
591 * Handle NOTIFY-message.
594 * @param client identification of the client
595 * @param message the actual message
598 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
599 const struct GNUNET_MessageHeader *message)
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
604 GNUNET_SERVER_notification_context_add (notify_list, client);
605 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, client);
606 GNUNET_SERVER_receive_done (client, GNUNET_OK);
614 free_host_entry (void *cls, const GNUNET_HashCode * key, void *value)
616 struct HostEntry *he = value;
618 GNUNET_free_non_null (he->hello);
625 * Clean up our state. Called during shutdown.
628 * @param tc scheduler task context, unused
631 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
633 GNUNET_SERVER_notification_context_destroy (notify_list);
635 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
636 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
639 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
646 * Start up peerinfo service.
649 * @param server the initialized server
650 * @param cfg configuration to use
653 run (void *cls, struct GNUNET_SERVER_Handle *server,
654 const struct GNUNET_CONFIGURATION_Handle *cfg)
656 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
657 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
658 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
659 sizeof (struct ListPeerMessage)},
660 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
661 sizeof (struct GNUNET_MessageHeader)},
662 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
663 sizeof (struct GNUNET_MessageHeader)},
669 hostmap = GNUNET_CONTAINER_multihashmap_create (1024);
670 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
671 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
672 GNUNET_assert (GNUNET_OK ==
673 GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
675 &networkIdDirectory));
676 GNUNET_DISK_directory_create (networkIdDirectory);
677 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
678 &cron_scan_directory_data_hosts, NULL);
679 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
680 &cron_clean_data_hosts, NULL);
681 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
683 GNUNET_SERVER_add_handlers (server, handlers);
684 ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
685 GNUNET_asprintf (&peerdir,
689 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
690 _("Importing HELLOs from `%s'\n"),
692 GNUNET_DISK_directory_scan (peerdir,
693 &hosts_directory_scan_callback, NULL);
694 GNUNET_free (peerdir);
699 * The main function for the peerinfo service.
701 * @param argc number of arguments from the command line
702 * @param argv command line arguments
703 * @return 0 ok, 1 on error
706 main (int argc, char *const *argv)
712 GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
713 &run, NULL)) ? 0 : 1;
714 GNUNET_free_non_null (networkIdDirectory);
719 /* end of gnunet-service-peerinfo.c */