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, size_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_t) - 1) - host->trust;
333 host->trust = (uint32_t) - 1; /* maximized */
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;
500 tc = GNUNET_SERVER_transmit_context_create (client);
504 if ((only == NULL) ||
506 memcmp (only, &pos->identity,
507 sizeof (struct GNUNET_PeerIdentity))))
509 change_host_trust (&pos->identity, trust_change);
511 im = (struct InfoMessage *) buf;
512 if (pos->hello != NULL)
514 hs = GNUNET_HELLO_size (pos->hello);
516 GNUNET_SERVER_MAX_MESSAGE_SIZE -
517 sizeof (struct InfoMessage));
518 memcpy (&im[1], pos->hello, hs);
520 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
521 im->header.size = htons (sizeof (struct InfoMessage) + hs);
522 im->trust = htonl (pos->trust);
523 im->peer = pos->identity;
524 GNUNET_SERVER_transmit_context_append_message (tc,
529 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
530 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
531 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
536 * Write host-trust information to a file - flush the buffer entry!
537 * Assumes synchronized access.
540 flush_trust (struct HostEntry *host)
545 if (host->trust == host->disk_trust)
546 return; /* unchanged */
547 fn = get_trust_filename (&host->identity);
548 if (host->trust == 0)
550 if ((0 != UNLINK (fn)) && (errno != ENOENT))
551 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
552 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
556 trust = htonl (host->trust);
557 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
559 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
560 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
561 host->disk_trust = host->trust;
567 * Call this method periodically to scan data/hosts for new hosts.
570 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
572 struct HostEntry *pos;
580 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
582 GNUNET_SCHEDULER_add_delayed (tc->sched,
583 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
588 * @brief delete expired HELLO entries in data/hosts/
591 discard_hosts_helper (void *cls, const char *fn)
593 struct GNUNET_TIME_Absolute *now = cls;
594 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
595 const struct GNUNET_HELLO_Message *hello;
596 struct GNUNET_HELLO_Message *new_hello;
599 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
600 if ((size < sizeof (struct GNUNET_MessageHeader)) && (0 != UNLINK (fn)))
602 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
603 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
606 hello = (const struct GNUNET_HELLO_Message *) buffer;
607 new_hello = GNUNET_HELLO_iterate_addresses (hello,
609 &discard_expired, now);
610 if (new_hello != NULL)
612 GNUNET_DISK_fn_write (fn,
614 GNUNET_HELLO_size (new_hello),
615 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
616 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
617 GNUNET_free (new_hello);
621 if (0 != UNLINK (fn))
622 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
623 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
630 * Call this method periodically to scan data/hosts for new hosts.
633 cron_clean_data_hosts (void *cls,
634 const struct GNUNET_SCHEDULER_TaskContext *tc)
636 struct GNUNET_TIME_Absolute now;
638 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
640 now = GNUNET_TIME_absolute_get ();
641 GNUNET_DISK_directory_scan (networkIdDirectory,
642 &discard_hosts_helper, &now);
644 GNUNET_SCHEDULER_add_delayed (tc->sched,
645 DATA_HOST_CLEAN_FREQ,
646 &cron_clean_data_hosts, NULL);
651 * Handle HELLO-message.
654 * @param client identification of the client
655 * @param message the actual message
658 handle_hello (void *cls,
659 struct GNUNET_SERVER_Client *client,
660 const struct GNUNET_MessageHeader *message)
662 const struct GNUNET_HELLO_Message *hello;
663 struct GNUNET_PeerIdentity pid;
665 hello = (const struct GNUNET_HELLO_Message *) message;
666 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
669 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
672 bind_address (&pid, hello);
673 GNUNET_SERVER_receive_done (client, GNUNET_OK);
678 * Handle GET-message.
681 * @param client identification of the client
682 * @param message the actual message
685 handle_get (void *cls,
686 struct GNUNET_SERVER_Client *client,
687 const struct GNUNET_MessageHeader *message)
689 const struct ListPeerMessage *lpm;
691 lpm = (const struct ListPeerMessage *) message;
692 send_to_each_host (&lpm->peer, ntohl (lpm->trust_change), client);
697 * Handle GET-ALL-message.
700 * @param client identification of the client
701 * @param message the actual message
704 handle_get_all (void *cls,
705 struct GNUNET_SERVER_Client *client,
706 const struct GNUNET_MessageHeader *message)
708 const struct ListAllPeersMessage *lpm;
710 lpm = (const struct ListAllPeersMessage *) message;
711 send_to_each_host (NULL, ntohl (lpm->trust_change), client);
716 * Handle NOTIFY-message.
719 * @param client identification of the client
720 * @param message the actual message
723 handle_notify (void *cls,
724 struct GNUNET_SERVER_Client *client,
725 const struct GNUNET_MessageHeader *message)
727 struct InfoMessage *msg;
728 struct HostEntry *pos;
730 GNUNET_SERVER_notification_context_add (notify_list,
735 msg = make_info_message (pos);
736 GNUNET_SERVER_notification_context_unicast (notify_list,
747 * List of handlers for the messages understood by this
750 static struct GNUNET_SERVER_MessageHandler handlers[] = {
751 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
752 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
753 sizeof (struct ListPeerMessage)},
754 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
755 sizeof (struct ListAllPeersMessage)},
756 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
757 sizeof (struct GNUNET_MessageHeader)},
763 * Clean up our state. Called during shutdown.
766 * @param tc scheduler task context, unused
769 shutdown_task (void *cls,
770 const struct GNUNET_SCHEDULER_TaskContext *tc)
772 GNUNET_SERVER_notification_context_destroy (notify_list);
776 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
783 * Process statistics requests.
786 * @param sched scheduler to use
787 * @param server the initialized server
788 * @param cfg configuration to use
792 struct GNUNET_SCHEDULER_Handle *sched,
793 struct GNUNET_SERVER_Handle *server,
794 const struct GNUNET_CONFIGURATION_Handle *cfg)
796 stats = GNUNET_STATISTICS_create (sched, "peerinfo", cfg);
797 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
798 GNUNET_assert (GNUNET_OK ==
799 GNUNET_CONFIGURATION_get_value_filename (cfg,
802 &networkIdDirectory));
803 GNUNET_assert (GNUNET_OK ==
804 GNUNET_CONFIGURATION_get_value_filename (cfg,
808 GNUNET_DISK_directory_create (networkIdDirectory);
809 GNUNET_DISK_directory_create (trustDirectory);
810 GNUNET_SCHEDULER_add_with_priority (sched,
811 GNUNET_SCHEDULER_PRIORITY_IDLE,
812 &cron_scan_directory_data_hosts, NULL);
813 GNUNET_SCHEDULER_add_with_priority (sched,
814 GNUNET_SCHEDULER_PRIORITY_HIGH,
815 &cron_flush_trust, NULL);
816 GNUNET_SCHEDULER_add_with_priority (sched,
817 GNUNET_SCHEDULER_PRIORITY_IDLE,
818 &cron_clean_data_hosts, NULL);
819 GNUNET_SCHEDULER_add_delayed (sched,
820 GNUNET_TIME_UNIT_FOREVER_REL,
821 &shutdown_task, NULL);
822 GNUNET_SERVER_add_handlers (server, handlers);
827 * The main function for the statistics service.
829 * @param argc number of arguments from the command line
830 * @param argv command line arguments
831 * @return 0 ok, 1 on error
834 main (int argc, char *const *argv)
839 GNUNET_SERVICE_run (argc,
842 GNUNET_SERVICE_OPTION_NONE,
843 &run, NULL)) ? 0 : 1;
844 GNUNET_free_non_null (networkIdDirectory);
845 GNUNET_free_non_null (trustDirectory);
850 /* end of gnunet-service-peerinfo.c */