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 2, 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/ and data/credit/).
28 * @author Christian Grothoff
31 * - HostEntries are never 'free'd (add expiration, upper bound?)
35 #include "gnunet_crypto_lib.h"
36 #include "gnunet_disk_lib.h"
37 #include "gnunet_hello_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_service_lib.h"
40 #include "gnunet_statistics_service.h"
44 * How often do we scan the HOST_DIR for new entries?
46 #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
49 * How often do we flush trust values to disk?
51 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
54 * How often do we discard old entries in data/hosts/?
56 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
59 * In-memory cache of known hosts.
65 * This is a linked list.
67 struct HostEntry *next;
70 * Identity of the peer.
72 struct GNUNET_PeerIdentity identity;
75 * Hello for the peer (can be NULL)
77 struct GNUNET_HELLO_Message *hello;
80 * Trust rating for this peer
85 * Trust rating for this peer on disk.
93 * The in-memory list of known hosts.
95 static struct HostEntry *hosts;
98 * Clients to immediately notify about all changes.
100 static struct GNUNET_SERVER_NotificationContext *notify_list;
103 * Directory where the hellos are stored in (data/hosts)
105 static char *networkIdDirectory;
108 * Where do we store trust information?
110 static char *trustDirectory;
113 * Handle for reporting statistics.
115 static struct GNUNET_STATISTICS_Handle *stats;
119 * Notify all clients in the notify list about the
120 * given host entry changing.
122 static struct InfoMessage *
123 make_info_message (const struct HostEntry *he)
125 struct InfoMessage *im;
128 hs = (he->hello == NULL) ? 0 : GNUNET_HELLO_size (he->hello);
129 im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
130 im->header.size = htons (hs + sizeof (struct InfoMessage));
131 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
132 im->trust = htonl (he->trust);
133 im->peer = he->identity;
134 if (he->hello != NULL)
135 memcpy (&im[1], he->hello, hs);
141 * Address iterator that causes expired entries to be discarded.
143 * @param cls pointer to the current time
144 * @param tname name of the transport
145 * @param expiration expiration time for the address
146 * @param addr the address
147 * @param addrlen length of addr in bytes
148 * @return GNUNET_NO if expiration smaller than the current time
151 discard_expired (void *cls,
153 struct GNUNET_TIME_Absolute expiration,
154 const void *addr, uint16_t addrlen)
156 const struct GNUNET_TIME_Absolute *now = cls;
157 if (now->value > expiration.value)
159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
160 _("Removing expired address of transport `%s'\n"),
169 * Get the filename under which we would store the GNUNET_HELLO_Message
170 * for the given host and protocol.
171 * @return filename of the form DIRECTORY/HOSTID
174 get_host_filename (const struct GNUNET_PeerIdentity *id)
176 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
179 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
180 GNUNET_asprintf (&fn,
181 "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
187 * Get the filename under which we would store the GNUNET_HELLO_Message
188 * for the given host and protocol.
189 * @return filename of the form DIRECTORY/HOSTID
192 get_trust_filename (const struct GNUNET_PeerIdentity *id)
194 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
197 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
198 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
204 * Find the host entry for the given peer. Call
205 * only when synchronized!
206 * @return NULL if not found
208 static struct HostEntry *
209 lookup_host_entry (const struct GNUNET_PeerIdentity *id)
211 struct HostEntry *pos;
214 while ((pos != NULL) &&
216 memcmp (id, &pos->identity, sizeof (struct GNUNET_PeerIdentity))))
223 * Broadcast information about the given entry to all
226 * @param entry entry to broadcast about
229 notify_all (struct HostEntry *entry)
231 struct InfoMessage *msg;
233 msg = make_info_message (entry);
234 GNUNET_SERVER_notification_context_broadcast (notify_list,
242 * Add a host to the list.
244 * @param identity the identity of the host
247 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
249 struct HostEntry *entry;
252 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
253 const struct GNUNET_HELLO_Message *hello;
254 struct GNUNET_HELLO_Message *hello_clean;
256 struct GNUNET_TIME_Absolute now;
258 entry = lookup_host_entry (identity);
261 GNUNET_STATISTICS_update (stats,
262 gettext_noop ("# peers known"),
265 entry = GNUNET_malloc (sizeof (struct HostEntry));
266 entry->identity = *identity;
267 fn = get_trust_filename (identity);
268 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
269 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
270 entry->disk_trust = entry->trust = ntohl (trust);
273 fn = get_host_filename (identity);
274 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
276 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
277 hello = (const struct GNUNET_HELLO_Message *) buffer;
278 if ( (size < sizeof (struct GNUNET_MessageHeader)) ||
279 (size != ntohs((((const struct GNUNET_MessageHeader*) hello)->size))) ||
280 (size != GNUNET_HELLO_size (hello)) )
283 if (0 != UNLINK (fn))
284 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
290 now = GNUNET_TIME_absolute_get ();
291 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
293 &discard_expired, &now);
294 entry->hello = hello_clean;
305 * Increase the host credit by a value.
307 * @param hostId is the identity of the host
308 * @param value is the int value by which the
309 * host credit is to be increased or decreased
310 * @returns the actual change in trust (positive or negative)
313 change_host_trust (const struct GNUNET_PeerIdentity *hostId, int value)
315 struct HostEntry *host;
316 unsigned int old_trust;
320 host = lookup_host_entry (hostId);
323 add_host_to_known_hosts (hostId);
324 host = lookup_host_entry (hostId);
326 GNUNET_assert (host != NULL);
327 old_trust = host->trust;
330 if (host->trust + value < host->trust)
332 value = UINT32_MAX - host->trust;
333 host->trust = UINT32_MAX;
336 host->trust += value;
340 if (host->trust < -value)
342 value = -host->trust;
346 host->trust += value;
348 if (host->trust != old_trust)
355 * Remove a file that should not be there. LOG
356 * success or failure.
359 remove_garbage (const char *fullname)
361 if (0 == UNLINK (fullname))
362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
364 ("File `%s' in directory `%s' does not match naming convention. "
365 "Removed.\n"), fullname, networkIdDirectory);
367 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
368 GNUNET_ERROR_TYPE_BULK, "unlink", fullname);
373 hosts_directory_scan_callback (void *cls, const char *fullname)
375 unsigned int *matched = cls;
376 struct GNUNET_PeerIdentity identity;
377 const char *filename;
379 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
380 return GNUNET_OK; /* ignore non-files */
381 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
383 remove_garbage (fullname);
387 &fullname[strlen (fullname) -
388 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
389 if (filename[-1] != DIR_SEPARATOR)
391 remove_garbage (fullname);
394 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (filename,
395 &identity.hashPubKey))
397 remove_garbage (fullname);
401 add_host_to_known_hosts (&identity);
407 * Call this method periodically to scan data/hosts for new hosts.
410 cron_scan_directory_data_hosts (void *cls,
411 const struct GNUNET_SCHEDULER_TaskContext *tc)
413 static unsigned int retries;
416 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
419 GNUNET_DISK_directory_create (networkIdDirectory);
420 GNUNET_DISK_directory_scan (networkIdDirectory,
421 &hosts_directory_scan_callback, &count);
422 if ((0 == count) && (0 == (++retries & 31)))
423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
424 GNUNET_ERROR_TYPE_BULK,
425 _("Still no peers found in `%s'!\n"), networkIdDirectory);
426 GNUNET_SCHEDULER_add_delayed (tc->sched,
428 &cron_scan_directory_data_hosts, NULL);
433 * Bind a host address (hello) to a hostId.
435 * @param peer the peer for which this is a hello
436 * @param hello the verified (!) hello message
439 bind_address (const struct GNUNET_PeerIdentity *peer,
440 const struct GNUNET_HELLO_Message *hello)
443 struct HostEntry *host;
444 struct GNUNET_HELLO_Message *mrg;
445 struct GNUNET_TIME_Absolute delta;
447 add_host_to_known_hosts (peer);
448 host = lookup_host_entry (peer);
449 GNUNET_assert (host != NULL);
450 if (host->hello == NULL)
452 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
453 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
457 mrg = GNUNET_HELLO_merge (host->hello, hello);
458 delta = GNUNET_HELLO_equals (mrg,
460 GNUNET_TIME_absolute_get ());
461 if (delta.value == GNUNET_TIME_UNIT_FOREVER_ABS.value)
466 GNUNET_free (host->hello);
469 fn = get_host_filename (peer);
470 GNUNET_DISK_directory_create_for_file (fn);
471 GNUNET_DISK_fn_write (fn,
473 GNUNET_HELLO_size (host->hello),
474 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
475 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
482 * Do transmit info either for only the host matching the given
483 * argument or for all known hosts and change their trust values by
486 * @param only NULL to hit all hosts, otherwise specifies a particular target
487 * @param trust_change how much should the trust be changed
488 * @param client who is making the request (and will thus receive our confirmation)
491 send_to_each_host (const struct GNUNET_PeerIdentity *only,
492 int trust_change, struct GNUNET_SERVER_Client *client)
494 struct HostEntry *pos;
495 struct InfoMessage *im;
497 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
498 struct GNUNET_SERVER_TransmitContext *tc;
501 tc = GNUNET_SERVER_transmit_context_create (client);
506 if ((only == NULL) ||
508 memcmp (only, &pos->identity,
509 sizeof (struct GNUNET_PeerIdentity))))
511 change_host_trust (&pos->identity, trust_change);
513 im = (struct InfoMessage *) buf;
514 if (pos->hello != NULL)
516 hs = GNUNET_HELLO_size (pos->hello);
518 GNUNET_SERVER_MAX_MESSAGE_SIZE -
519 sizeof (struct InfoMessage));
520 memcpy (&im[1], pos->hello, hs);
523 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
524 im->header.size = htons (sizeof (struct InfoMessage) + hs);
525 im->trust = htonl (pos->trust);
526 im->peer = pos->identity;
527 GNUNET_SERVER_transmit_context_append_message (tc,
532 if ( (only != NULL) &&
533 (match == GNUNET_NO) )
534 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
535 "No `%s' message was found for peer `%4s'\n",
538 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
539 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
540 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
545 * Write host-trust information to a file - flush the buffer entry!
546 * Assumes synchronized access.
549 flush_trust (struct HostEntry *host)
554 if (host->trust == host->disk_trust)
555 return; /* unchanged */
556 fn = get_trust_filename (&host->identity);
557 if (host->trust == 0)
559 if ((0 != UNLINK (fn)) && (errno != ENOENT))
560 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
561 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
565 trust = htonl (host->trust);
566 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
568 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
569 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
570 host->disk_trust = host->trust;
576 * Call this method periodically to scan data/hosts for new hosts.
579 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
581 struct HostEntry *pos;
589 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
591 GNUNET_SCHEDULER_add_delayed (tc->sched,
592 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
597 * @brief delete expired HELLO entries in data/hosts/
600 discard_hosts_helper (void *cls, const char *fn)
602 struct GNUNET_TIME_Absolute *now = cls;
603 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
604 const struct GNUNET_HELLO_Message *hello;
605 struct GNUNET_HELLO_Message *new_hello;
608 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
609 if (size < sizeof (struct GNUNET_MessageHeader))
611 if (0 != UNLINK (fn))
612 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
613 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
616 hello = (const struct GNUNET_HELLO_Message *) buffer;
617 new_hello = GNUNET_HELLO_iterate_addresses (hello,
619 &discard_expired, now);
620 if (new_hello != NULL)
622 GNUNET_DISK_fn_write (fn,
624 GNUNET_HELLO_size (new_hello),
625 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
626 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
627 GNUNET_free (new_hello);
631 if (0 != UNLINK (fn))
632 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
633 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
640 * Call this method periodically to scan data/hosts for new hosts.
643 cron_clean_data_hosts (void *cls,
644 const struct GNUNET_SCHEDULER_TaskContext *tc)
646 struct GNUNET_TIME_Absolute now;
648 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
650 now = GNUNET_TIME_absolute_get ();
651 GNUNET_DISK_directory_scan (networkIdDirectory,
652 &discard_hosts_helper, &now);
654 GNUNET_SCHEDULER_add_delayed (tc->sched,
655 DATA_HOST_CLEAN_FREQ,
656 &cron_clean_data_hosts, NULL);
661 * Handle HELLO-message.
664 * @param client identification of the client
665 * @param message the actual message
668 handle_hello (void *cls,
669 struct GNUNET_SERVER_Client *client,
670 const struct GNUNET_MessageHeader *message)
672 const struct GNUNET_HELLO_Message *hello;
673 struct GNUNET_PeerIdentity pid;
675 hello = (const struct GNUNET_HELLO_Message *) message;
676 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "`%s' message received for peer `%4s'\n",
688 bind_address (&pid, hello);
689 GNUNET_SERVER_receive_done (client, GNUNET_OK);
694 * Handle GET-message.
697 * @param client identification of the client
698 * @param message the actual message
701 handle_get (void *cls,
702 struct GNUNET_SERVER_Client *client,
703 const struct GNUNET_MessageHeader *message)
705 const struct ListPeerMessage *lpm;
707 lpm = (const struct ListPeerMessage *) message;
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "`%s' message received for peer `%4s'\n",
712 GNUNET_i2s (&lpm->peer));
714 send_to_each_host (&lpm->peer, ntohl (lpm->trust_change), client);
719 * Handle GET-ALL-message.
722 * @param client identification of the client
723 * @param message the actual message
726 handle_get_all (void *cls,
727 struct GNUNET_SERVER_Client *client,
728 const struct GNUNET_MessageHeader *message)
730 const struct ListAllPeersMessage *lpm;
732 lpm = (const struct ListAllPeersMessage *) message;
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "`%s' message received\n",
738 send_to_each_host (NULL, ntohl (lpm->trust_change), client);
743 * Handle NOTIFY-message.
746 * @param client identification of the client
747 * @param message the actual message
750 handle_notify (void *cls,
751 struct GNUNET_SERVER_Client *client,
752 const struct GNUNET_MessageHeader *message)
754 struct InfoMessage *msg;
755 struct HostEntry *pos;
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
759 "`%s' message received\n",
762 GNUNET_SERVER_notification_context_add (notify_list,
767 msg = make_info_message (pos);
768 GNUNET_SERVER_notification_context_unicast (notify_list,
779 * Clean up our state. Called during shutdown.
782 * @param tc scheduler task context, unused
785 shutdown_task (void *cls,
786 const struct GNUNET_SCHEDULER_TaskContext *tc)
788 GNUNET_SERVER_notification_context_destroy (notify_list);
792 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
799 * Process statistics requests.
802 * @param sched scheduler to use
803 * @param server the initialized server
804 * @param cfg configuration to use
808 struct GNUNET_SCHEDULER_Handle *sched,
809 struct GNUNET_SERVER_Handle *server,
810 const struct GNUNET_CONFIGURATION_Handle *cfg)
812 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
813 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
814 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
815 sizeof (struct ListPeerMessage)},
816 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
817 sizeof (struct ListAllPeersMessage)},
818 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
819 sizeof (struct GNUNET_MessageHeader)},
822 stats = GNUNET_STATISTICS_create (sched, "peerinfo", cfg);
823 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
824 GNUNET_assert (GNUNET_OK ==
825 GNUNET_CONFIGURATION_get_value_filename (cfg,
828 &networkIdDirectory));
829 GNUNET_assert (GNUNET_OK ==
830 GNUNET_CONFIGURATION_get_value_filename (cfg,
834 GNUNET_DISK_directory_create (networkIdDirectory);
835 GNUNET_DISK_directory_create (trustDirectory);
836 GNUNET_SCHEDULER_add_with_priority (sched,
837 GNUNET_SCHEDULER_PRIORITY_IDLE,
838 &cron_scan_directory_data_hosts, NULL);
839 GNUNET_SCHEDULER_add_with_priority (sched,
840 GNUNET_SCHEDULER_PRIORITY_HIGH,
841 &cron_flush_trust, NULL);
842 GNUNET_SCHEDULER_add_with_priority (sched,
843 GNUNET_SCHEDULER_PRIORITY_IDLE,
844 &cron_clean_data_hosts, NULL);
845 GNUNET_SCHEDULER_add_delayed (sched,
846 GNUNET_TIME_UNIT_FOREVER_REL,
847 &shutdown_task, NULL);
848 GNUNET_SERVER_add_handlers (server, handlers);
853 * The main function for the statistics service.
855 * @param argc number of arguments from the command line
856 * @param argv command line arguments
857 * @return 0 ok, 1 on error
860 main (int argc, char *const *argv)
865 GNUNET_SERVICE_run (argc,
868 GNUNET_SERVICE_OPTION_NONE,
869 &run, NULL)) ? 0 : 1;
870 GNUNET_free_non_null (networkIdDirectory);
871 GNUNET_free_non_null (trustDirectory);
876 /* end of gnunet-service-peerinfo.c */