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)
54 #define WRITE_TO_DISK GNUNET_NO
57 * In-memory cache of known hosts.
63 * Identity of the peer.
65 struct GNUNET_PeerIdentity identity;
68 * Hello for the peer (can be NULL)
70 struct GNUNET_HELLO_Message *hello;
76 * The in-memory list of known hosts, mapping of
77 * host IDs to 'struct HostEntry*' values.
79 static struct GNUNET_CONTAINER_MultiHashMap *hostmap;
82 * Clients to immediately notify about all changes.
84 static struct GNUNET_SERVER_NotificationContext *notify_list;
87 * Directory where the hellos are stored in (data/hosts)
89 static char *networkIdDirectory;
92 * Handle for reporting statistics.
94 static struct GNUNET_STATISTICS_Handle *stats;
98 * Notify all clients in the notify list about the
99 * given host entry changing.
101 static struct InfoMessage *
102 make_info_message (const struct HostEntry *he)
104 struct InfoMessage *im;
107 hs = (he->hello == NULL) ? 0 : GNUNET_HELLO_size (he->hello);
108 im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
109 im->header.size = htons (hs + sizeof (struct InfoMessage));
110 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
111 im->peer = he->identity;
112 if (he->hello != NULL)
113 memcpy (&im[1], he->hello, hs);
120 * Address iterator that causes expired entries to be discarded.
122 * @param cls pointer to the current time
123 * @param tname name of the transport
124 * @param expiration expiration time for the address
125 * @param addr the address
126 * @param addrlen length of addr in bytes
127 * @return GNUNET_NO if expiration smaller than the current time
130 discard_expired (void *cls,
132 struct GNUNET_TIME_Absolute expiration,
133 const void *addr, uint16_t addrlen)
135 const struct GNUNET_TIME_Absolute *now = cls;
136 if (now->abs_value > expiration.abs_value)
138 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
139 _("Removing expired address of transport `%s'\n"),
148 * Get the filename under which we would store the GNUNET_HELLO_Message
149 * for the given host and protocol.
150 * @return filename of the form DIRECTORY/HOSTID
153 get_host_filename (const struct GNUNET_PeerIdentity *id)
155 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
158 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
159 GNUNET_asprintf (&fn,
160 "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
167 * Broadcast information about the given entry to all
170 * @param entry entry to broadcast about
173 notify_all (struct HostEntry *entry)
175 struct InfoMessage *msg;
177 msg = make_info_message (entry);
178 GNUNET_SERVER_notification_context_broadcast (notify_list,
186 * Add a host to the list.
188 * @param identity the identity of the host
191 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
193 struct HostEntry *entry;
195 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
196 const struct GNUNET_HELLO_Message *hello;
197 struct GNUNET_HELLO_Message *hello_clean;
199 struct GNUNET_TIME_Absolute now;
203 entry = GNUNET_CONTAINER_multihashmap_get (hostmap,
204 &identity->hashPubKey);
207 GNUNET_STATISTICS_update (stats,
208 gettext_noop ("# peers known"),
211 entry = GNUNET_malloc (sizeof (struct HostEntry));
212 entry->identity = *identity;
214 fn = get_host_filename (identity);
215 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
217 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
218 hello = (const struct GNUNET_HELLO_Message *) buffer;
219 if ( (size < sizeof (struct GNUNET_MessageHeader)) ||
220 (size != ntohs((((const struct GNUNET_MessageHeader*) hello)->size))) ||
221 (size != GNUNET_HELLO_size (hello)) )
224 if (0 != UNLINK (fn))
225 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
231 now = GNUNET_TIME_absolute_get ();
232 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
234 &discard_expired, &now);
235 entry->hello = hello_clean;
240 GNUNET_CONTAINER_multihashmap_put (hostmap,
241 &identity->hashPubKey,
243 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
249 * Remove a file that should not be there. LOG
250 * success or failure.
253 remove_garbage (const char *fullname)
255 if (0 == UNLINK (fullname))
256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
258 ("File `%s' in directory `%s' does not match naming convention. "
259 "Removed.\n"), fullname, networkIdDirectory);
261 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
262 GNUNET_ERROR_TYPE_BULK, "unlink", fullname);
267 hosts_directory_scan_callback (void *cls,
268 const char *fullname)
270 unsigned int *matched = cls;
271 struct GNUNET_PeerIdentity identity;
272 const char *filename;
274 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
275 return GNUNET_OK; /* ignore non-files */
276 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
278 remove_garbage (fullname);
282 &fullname[strlen (fullname) -
283 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
284 if (filename[-1] != DIR_SEPARATOR)
286 remove_garbage (fullname);
289 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (filename,
290 &identity.hashPubKey))
292 remove_garbage (fullname);
296 add_host_to_known_hosts (&identity);
302 * Call this method periodically to scan data/hosts for new hosts.
305 cron_scan_directory_data_hosts (void *cls,
306 const struct GNUNET_SCHEDULER_TaskContext *tc)
308 static unsigned int retries;
311 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
314 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
316 GNUNET_SCHEDULER_add_delayed (DATA_HOST_FREQ,
317 &cron_scan_directory_data_hosts, NULL);
320 GNUNET_DISK_directory_scan (networkIdDirectory,
321 &hosts_directory_scan_callback, &count);
322 if ((0 == count) && (0 == (++retries & 31)))
323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
324 GNUNET_ERROR_TYPE_BULK,
325 _("Still no peers found in `%s'!\n"), networkIdDirectory);
326 GNUNET_SCHEDULER_add_delayed (DATA_HOST_FREQ,
327 &cron_scan_directory_data_hosts, NULL);
332 * Bind a host address (hello) to a hostId.
334 * @param peer the peer for which this is a hello
335 * @param hello the verified (!) hello message
338 bind_address (const struct GNUNET_PeerIdentity *peer,
339 const struct GNUNET_HELLO_Message *hello)
344 struct HostEntry *host;
345 struct GNUNET_HELLO_Message *mrg;
346 struct GNUNET_TIME_Absolute delta;
348 add_host_to_known_hosts (peer);
349 host = GNUNET_CONTAINER_multihashmap_get (hostmap,
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,
362 GNUNET_TIME_absolute_get ());
363 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
368 GNUNET_free (host->hello);
372 fn = get_host_filename (peer);
373 if (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn))
376 GNUNET_DISK_fn_write (fn,
378 GNUNET_HELLO_size (host->hello),
379 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
380 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
381 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
394 * Do transmit info about peer to given host.
396 * @param cls NULL to hit all hosts, otherwise specifies a particular target
398 * @param value information to transmit
399 * @return GNUNET_YES (continue to iterate)
402 add_to_tc (void *cls,
403 const GNUNET_HashCode *key,
406 struct GNUNET_SERVER_TransmitContext *tc = cls;
407 struct HostEntry *pos = value;
408 struct InfoMessage *im;
410 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
413 im = (struct InfoMessage *) buf;
414 if (pos->hello != NULL)
416 hs = GNUNET_HELLO_size (pos->hello);
418 GNUNET_SERVER_MAX_MESSAGE_SIZE -
419 sizeof (struct InfoMessage));
420 memcpy (&im[1], pos->hello, hs);
422 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
423 im->header.size = htons (sizeof (struct InfoMessage) + hs);
424 im->reserved = htonl (0);
425 im->peer = pos->identity;
426 GNUNET_SERVER_transmit_context_append_message (tc,
433 * @brief delete expired HELLO entries in data/hosts/
436 discard_hosts_helper (void *cls, const char *fn)
438 struct GNUNET_TIME_Absolute *now = cls;
439 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
440 const struct GNUNET_HELLO_Message *hello;
441 struct GNUNET_HELLO_Message *new_hello;
444 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
445 if (size < sizeof (struct GNUNET_MessageHeader))
447 if (0 != UNLINK (fn))
448 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
449 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
452 hello = (const struct GNUNET_HELLO_Message *) buffer;
453 new_hello = GNUNET_HELLO_iterate_addresses (hello,
455 &discard_expired, now);
456 if (new_hello != NULL)
458 GNUNET_DISK_fn_write (fn,
460 GNUNET_HELLO_size (new_hello),
461 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
462 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
463 GNUNET_free (new_hello);
467 if (0 != UNLINK (fn))
468 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
469 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
476 * Call this method periodically to scan data/hosts for new hosts.
479 cron_clean_data_hosts (void *cls,
480 const struct GNUNET_SCHEDULER_TaskContext *tc)
482 struct GNUNET_TIME_Absolute now;
484 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
486 now = GNUNET_TIME_absolute_get ();
487 GNUNET_DISK_directory_scan (networkIdDirectory,
488 &discard_hosts_helper, &now);
489 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ,
490 &cron_clean_data_hosts, NULL);
496 * Handle HELLO-message.
499 * @param client identification of the client
500 * @param message the actual message
503 handle_hello (void *cls,
504 struct GNUNET_SERVER_Client *client,
505 const struct GNUNET_MessageHeader *message)
507 const struct GNUNET_HELLO_Message *hello;
508 struct GNUNET_PeerIdentity pid;
510 hello = (const struct GNUNET_HELLO_Message *) message;
511 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
514 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "`%s' message received for peer `%4s'\n",
523 bind_address (&pid, hello);
524 GNUNET_SERVER_receive_done (client, GNUNET_OK);
529 * Handle GET-message.
532 * @param client identification of the client
533 * @param message the actual message
536 handle_get (void *cls,
537 struct GNUNET_SERVER_Client *client,
538 const struct GNUNET_MessageHeader *message)
540 const struct ListPeerMessage *lpm;
541 struct GNUNET_SERVER_TransmitContext *tc;
543 lpm = (const struct ListPeerMessage *) message;
544 GNUNET_break (0 == ntohl (lpm->reserved));
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "`%s' message received for peer `%4s'\n",
549 GNUNET_i2s (&lpm->peer));
551 tc = GNUNET_SERVER_transmit_context_create (client);
552 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap,
553 &lpm->peer.hashPubKey,
556 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
557 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
558 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
563 * Handle GET-ALL-message.
566 * @param client identification of the client
567 * @param message the actual message
570 handle_get_all (void *cls,
571 struct GNUNET_SERVER_Client *client,
572 const struct GNUNET_MessageHeader *message)
574 struct GNUNET_SERVER_TransmitContext *tc;
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
578 "`%s' message received\n",
581 tc = GNUNET_SERVER_transmit_context_create (client);
582 GNUNET_CONTAINER_multihashmap_iterate (hostmap,
585 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
586 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
587 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
592 do_notify_entry (void *cls,
593 const GNUNET_HashCode *key,
596 struct GNUNET_SERVER_Client *client = cls;
597 struct HostEntry *he = value;
598 struct InfoMessage *msg;
600 msg = make_info_message (he);
601 GNUNET_SERVER_notification_context_unicast (notify_list,
611 * Handle NOTIFY-message.
614 * @param client identification of the client
615 * @param message the actual message
618 handle_notify (void *cls,
619 struct GNUNET_SERVER_Client *client,
620 const struct GNUNET_MessageHeader *message)
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 "`%s' message received\n",
627 GNUNET_SERVER_notification_context_add (notify_list,
629 GNUNET_CONTAINER_multihashmap_iterate (hostmap,
632 GNUNET_SERVER_receive_done (client, GNUNET_OK);
637 free_host_entry (void *cls,
638 const GNUNET_HashCode *key,
641 struct HostEntry *he = value;
643 GNUNET_free_non_null (he->hello);
649 * Clean up our state. Called during shutdown.
652 * @param tc scheduler task context, unused
655 shutdown_task (void *cls,
656 const struct GNUNET_SCHEDULER_TaskContext *tc)
658 GNUNET_SERVER_notification_context_destroy (notify_list);
660 GNUNET_CONTAINER_multihashmap_iterate (hostmap,
663 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
666 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
673 * Process statistics requests.
676 * @param server the initialized server
677 * @param cfg configuration to use
681 struct GNUNET_SERVER_Handle *server,
682 const struct GNUNET_CONFIGURATION_Handle *cfg)
684 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
685 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
686 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
687 sizeof (struct ListPeerMessage)},
688 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
689 sizeof (struct GNUNET_MessageHeader)},
690 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
691 sizeof (struct GNUNET_MessageHeader)},
695 hostmap = GNUNET_CONTAINER_multihashmap_create (1024);
696 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
697 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
699 GNUNET_assert (GNUNET_OK ==
700 GNUNET_CONFIGURATION_get_value_filename (cfg,
703 &networkIdDirectory));
704 GNUNET_DISK_directory_create (networkIdDirectory);
705 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
706 &cron_scan_directory_data_hosts, NULL);
707 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
708 &cron_clean_data_hosts, NULL);
710 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
711 &shutdown_task, NULL);
712 GNUNET_SERVER_add_handlers (server, handlers);
717 * The main function for the statistics service.
719 * @param argc number of arguments from the command line
720 * @param argv command line arguments
721 * @return 0 ok, 1 on error
724 main (int argc, char *const *argv)
729 GNUNET_SERVICE_run (argc,
732 GNUNET_SERVICE_OPTION_NONE,
733 &run, NULL)) ? 0 : 1;
734 GNUNET_free_non_null (networkIdDirectory);
739 /* end of gnunet-service-peerinfo.c */