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_crypto_lib.h"
36 #include "gnunet_container_lib.h"
37 #include "gnunet_disk_lib.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_protocols.h"
40 #include "gnunet_service_lib.h"
41 #include "gnunet_statistics_service.h"
45 * How often do we scan the HOST_DIR for new entries?
47 #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
50 * How often do we discard old entries in data/hosts/?
52 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
55 * In-memory cache of known hosts.
61 * Identity of the peer.
63 struct GNUNET_PeerIdentity identity;
66 * Hello for the peer (can be NULL)
68 struct GNUNET_HELLO_Message *hello;
74 * The in-memory list of known hosts, mapping of
75 * host IDs to 'struct HostEntry*' values.
77 static struct GNUNET_CONTAINER_MultiHashMap *hostmap;
80 * Clients to immediately notify about all changes.
82 static struct GNUNET_SERVER_NotificationContext *notify_list;
85 * Directory where the hellos are stored in (data/hosts)
87 static char *networkIdDirectory;
90 * Handle for reporting statistics.
92 static struct GNUNET_STATISTICS_Handle *stats;
96 * Notify all clients in the notify list about the
97 * given host entry changing.
99 static struct InfoMessage *
100 make_info_message (const struct HostEntry *he)
102 struct InfoMessage *im;
105 hs = (he->hello == NULL) ? 0 : GNUNET_HELLO_size (he->hello);
106 im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
107 im->header.size = htons (hs + sizeof (struct InfoMessage));
108 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
109 im->peer = he->identity;
110 if (he->hello != NULL)
111 memcpy (&im[1], he->hello, hs);
117 * Address iterator that causes expired entries to be discarded.
119 * @param cls pointer to the current time
120 * @param tname name of the transport
121 * @param expiration expiration time for the address
122 * @param addr the address
123 * @param addrlen length of addr in bytes
124 * @return GNUNET_NO if expiration smaller than the current time
127 discard_expired (void *cls,
129 struct GNUNET_TIME_Absolute expiration,
130 const void *addr, uint16_t addrlen)
132 const struct GNUNET_TIME_Absolute *now = cls;
133 if (now->abs_value > expiration.abs_value)
135 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
136 _("Removing expired address of transport `%s'\n"),
145 * Get the filename under which we would store the GNUNET_HELLO_Message
146 * for the given host and protocol.
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,
157 "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
163 * Broadcast information about the given entry to all
166 * @param entry entry to broadcast about
169 notify_all (struct HostEntry *entry)
171 struct InfoMessage *msg;
173 msg = make_info_message (entry);
174 GNUNET_SERVER_notification_context_broadcast (notify_list,
182 * Add a host to the list.
184 * @param identity the identity of the host
187 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
189 struct HostEntry *entry;
190 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
191 const struct GNUNET_HELLO_Message *hello;
192 struct GNUNET_HELLO_Message *hello_clean;
194 struct GNUNET_TIME_Absolute now;
197 entry = GNUNET_CONTAINER_multihashmap_get (hostmap,
198 &identity->hashPubKey);
201 GNUNET_STATISTICS_update (stats,
202 gettext_noop ("# peers known"),
205 entry = GNUNET_malloc (sizeof (struct HostEntry));
206 entry->identity = *identity;
208 fn = get_host_filename (identity);
209 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
211 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
212 hello = (const struct GNUNET_HELLO_Message *) buffer;
213 if ( (size < sizeof (struct GNUNET_MessageHeader)) ||
214 (size != ntohs((((const struct GNUNET_MessageHeader*) hello)->size))) ||
215 (size != GNUNET_HELLO_size (hello)) )
218 if (0 != UNLINK (fn))
219 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
225 now = GNUNET_TIME_absolute_get ();
226 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
228 &discard_expired, &now);
229 entry->hello = hello_clean;
233 GNUNET_CONTAINER_multihashmap_put (hostmap,
234 &identity->hashPubKey,
236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
242 * Remove a file that should not be there. LOG
243 * success or failure.
246 remove_garbage (const char *fullname)
248 if (0 == UNLINK (fullname))
249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
251 ("File `%s' in directory `%s' does not match naming convention. "
252 "Removed.\n"), fullname, networkIdDirectory);
254 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
255 GNUNET_ERROR_TYPE_BULK, "unlink", fullname);
260 hosts_directory_scan_callback (void *cls,
261 const char *fullname)
263 unsigned int *matched = cls;
264 struct GNUNET_PeerIdentity identity;
265 const char *filename;
267 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
268 return GNUNET_OK; /* ignore non-files */
269 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
271 remove_garbage (fullname);
275 &fullname[strlen (fullname) -
276 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
277 if (filename[-1] != DIR_SEPARATOR)
279 remove_garbage (fullname);
282 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (filename,
283 &identity.hashPubKey))
285 remove_garbage (fullname);
289 add_host_to_known_hosts (&identity);
295 * Call this method periodically to scan data/hosts for new hosts.
298 cron_scan_directory_data_hosts (void *cls,
299 const struct GNUNET_SCHEDULER_TaskContext *tc)
301 static unsigned int retries;
304 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
307 GNUNET_DISK_directory_create (networkIdDirectory);
308 GNUNET_DISK_directory_scan (networkIdDirectory,
309 &hosts_directory_scan_callback, &count);
310 if ((0 == count) && (0 == (++retries & 31)))
311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
312 GNUNET_ERROR_TYPE_BULK,
313 _("Still no peers found in `%s'!\n"), networkIdDirectory);
314 GNUNET_SCHEDULER_add_delayed (DATA_HOST_FREQ,
315 &cron_scan_directory_data_hosts, NULL);
320 * Bind a host address (hello) to a hostId.
322 * @param peer the peer for which this is a hello
323 * @param hello the verified (!) hello message
326 bind_address (const struct GNUNET_PeerIdentity *peer,
327 const struct GNUNET_HELLO_Message *hello)
330 struct HostEntry *host;
331 struct GNUNET_HELLO_Message *mrg;
332 struct GNUNET_TIME_Absolute delta;
334 add_host_to_known_hosts (peer);
335 host = GNUNET_CONTAINER_multihashmap_get (hostmap,
337 GNUNET_assert (host != NULL);
338 if (host->hello == NULL)
340 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
341 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
345 mrg = GNUNET_HELLO_merge (host->hello, hello);
346 delta = GNUNET_HELLO_equals (mrg,
348 GNUNET_TIME_absolute_get ());
349 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
354 GNUNET_free (host->hello);
357 fn = get_host_filename (peer);
358 GNUNET_DISK_directory_create_for_file (fn);
359 GNUNET_DISK_fn_write (fn,
361 GNUNET_HELLO_size (host->hello),
362 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
363 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
371 * Do transmit info about peer to given host.
373 * @param cls NULL to hit all hosts, otherwise specifies a particular target
375 * @param value information to transmit
376 * @return GNUNET_YES (continue to iterate)
379 add_to_tc (void *cls,
380 const GNUNET_HashCode *key,
383 struct GNUNET_SERVER_TransmitContext *tc = cls;
384 struct HostEntry *pos = value;
385 struct InfoMessage *im;
387 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
390 im = (struct InfoMessage *) buf;
391 if (pos->hello != NULL)
393 hs = GNUNET_HELLO_size (pos->hello);
395 GNUNET_SERVER_MAX_MESSAGE_SIZE -
396 sizeof (struct InfoMessage));
397 memcpy (&im[1], pos->hello, hs);
399 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
400 im->header.size = htons (sizeof (struct InfoMessage) + hs);
401 im->reserved = htonl (0);
402 im->peer = pos->identity;
403 GNUNET_SERVER_transmit_context_append_message (tc,
410 * @brief delete expired HELLO entries in data/hosts/
413 discard_hosts_helper (void *cls, const char *fn)
415 struct GNUNET_TIME_Absolute *now = cls;
416 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
417 const struct GNUNET_HELLO_Message *hello;
418 struct GNUNET_HELLO_Message *new_hello;
421 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
422 if (size < sizeof (struct GNUNET_MessageHeader))
424 if (0 != UNLINK (fn))
425 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
426 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
429 hello = (const struct GNUNET_HELLO_Message *) buffer;
430 new_hello = GNUNET_HELLO_iterate_addresses (hello,
432 &discard_expired, now);
433 if (new_hello != NULL)
435 GNUNET_DISK_fn_write (fn,
437 GNUNET_HELLO_size (new_hello),
438 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
439 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
440 GNUNET_free (new_hello);
444 if (0 != UNLINK (fn))
445 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
446 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
453 * Call this method periodically to scan data/hosts for new hosts.
456 cron_clean_data_hosts (void *cls,
457 const struct GNUNET_SCHEDULER_TaskContext *tc)
459 struct GNUNET_TIME_Absolute now;
461 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
463 now = GNUNET_TIME_absolute_get ();
464 GNUNET_DISK_directory_scan (networkIdDirectory,
465 &discard_hosts_helper, &now);
466 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ,
467 &cron_clean_data_hosts, NULL);
472 * Handle HELLO-message.
475 * @param client identification of the client
476 * @param message the actual message
479 handle_hello (void *cls,
480 struct GNUNET_SERVER_Client *client,
481 const struct GNUNET_MessageHeader *message)
483 const struct GNUNET_HELLO_Message *hello;
484 struct GNUNET_PeerIdentity pid;
486 hello = (const struct GNUNET_HELLO_Message *) message;
487 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
490 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "`%s' message received for peer `%4s'\n",
499 bind_address (&pid, hello);
500 GNUNET_SERVER_receive_done (client, GNUNET_OK);
505 * Handle GET-message.
508 * @param client identification of the client
509 * @param message the actual message
512 handle_get (void *cls,
513 struct GNUNET_SERVER_Client *client,
514 const struct GNUNET_MessageHeader *message)
516 const struct ListPeerMessage *lpm;
517 struct GNUNET_SERVER_TransmitContext *tc;
519 lpm = (const struct ListPeerMessage *) message;
520 GNUNET_break (0 == ntohl (lpm->reserved));
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "`%s' message received for peer `%4s'\n",
525 GNUNET_i2s (&lpm->peer));
527 tc = GNUNET_SERVER_transmit_context_create (client);
528 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap,
529 &lpm->peer.hashPubKey,
532 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
533 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
534 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
539 * Handle GET-ALL-message.
542 * @param client identification of the client
543 * @param message the actual message
546 handle_get_all (void *cls,
547 struct GNUNET_SERVER_Client *client,
548 const struct GNUNET_MessageHeader *message)
550 struct GNUNET_SERVER_TransmitContext *tc;
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "`%s' message received\n",
557 tc = GNUNET_SERVER_transmit_context_create (client);
558 GNUNET_CONTAINER_multihashmap_iterate (hostmap,
561 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
562 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
563 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
568 do_notify_entry (void *cls,
569 const GNUNET_HashCode *key,
572 struct GNUNET_SERVER_Client *client = cls;
573 struct HostEntry *he = value;
574 struct InfoMessage *msg;
576 msg = make_info_message (he);
577 GNUNET_SERVER_notification_context_unicast (notify_list,
587 * Handle NOTIFY-message.
590 * @param client identification of the client
591 * @param message the actual message
594 handle_notify (void *cls,
595 struct GNUNET_SERVER_Client *client,
596 const struct GNUNET_MessageHeader *message)
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600 "`%s' message received\n",
603 GNUNET_SERVER_notification_context_add (notify_list,
605 GNUNET_CONTAINER_multihashmap_iterate (hostmap,
612 free_host_entry (void *cls,
613 const GNUNET_HashCode *key,
616 struct HostEntry *he = value;
618 GNUNET_free_non_null (he->hello);
624 * Clean up our state. Called during shutdown.
627 * @param tc scheduler task context, unused
630 shutdown_task (void *cls,
631 const struct GNUNET_SCHEDULER_TaskContext *tc)
633 GNUNET_SERVER_notification_context_destroy (notify_list);
635 GNUNET_CONTAINER_multihashmap_iterate (hostmap,
638 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
641 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
648 * Process statistics requests.
651 * @param server the initialized server
652 * @param cfg configuration to use
656 struct GNUNET_SERVER_Handle *server,
657 const struct GNUNET_CONFIGURATION_Handle *cfg)
659 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
660 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
661 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
662 sizeof (struct ListPeerMessage)},
663 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
664 sizeof (struct GNUNET_MessageHeader)},
665 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
666 sizeof (struct GNUNET_MessageHeader)},
670 hostmap = GNUNET_CONTAINER_multihashmap_create (1024);
671 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
672 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
673 GNUNET_assert (GNUNET_OK ==
674 GNUNET_CONFIGURATION_get_value_filename (cfg,
677 &networkIdDirectory));
678 GNUNET_DISK_directory_create (networkIdDirectory);
679 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
680 &cron_scan_directory_data_hosts, NULL);
681 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
682 &cron_clean_data_hosts, NULL);
683 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
684 &shutdown_task, NULL);
685 GNUNET_SERVER_add_handlers (server, handlers);
690 * The main function for the statistics service.
692 * @param argc number of arguments from the command line
693 * @param argv command line arguments
694 * @return 0 ok, 1 on error
697 main (int argc, char *const *argv)
702 GNUNET_SERVICE_run (argc,
705 GNUNET_SERVICE_OPTION_NONE,
706 &run, NULL)) ? 0 : 1;
707 GNUNET_free_non_null (networkIdDirectory);
712 /* end of gnunet-service-peerinfo.c */