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"
43 * How often do we scan the HOST_DIR for new entries?
45 #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
48 * How often do we flush trust values to disk?
50 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
53 * How often do we discard old entries in data/hosts/?
55 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
58 * In-memory cache of known hosts.
64 * This is a linked list.
66 struct HostEntry *next;
69 * Identity of the peer.
71 struct GNUNET_PeerIdentity identity;
74 * Hello for the peer (can be NULL)
76 struct GNUNET_HELLO_Message *hello;
79 * Trust rating for this peer
84 * Trust rating for this peer on disk.
92 * Entries that we still need to tell the client about.
98 * This is a linked list.
100 struct PendingEntry *next;
103 * Entry to tell the client about.
105 struct HostEntry *he;
111 * Clients to notify of changes to the peer information.
117 * This is a linked list.
119 struct NotifyList *next;
124 struct GNUNET_SERVER_Client *client;
127 * Notifications pending for this entry.
129 struct PendingEntry *pending;
132 * Handle for a transmit ready request.
134 struct GNUNET_CONNECTION_TransmitHandle *transmit_ctx;
140 * The in-memory list of known hosts.
142 static struct HostEntry *hosts;
145 * Clients to immediately notify about all changes.
147 static struct NotifyList *notify_list;
150 * Directory where the hellos are stored in (data/hosts)
152 static char *networkIdDirectory;
155 * Where do we store trust information?
157 static char *trustDirectory;
161 * Transmit peer information messages from the pending queue
164 * @param cls the 'struct NotifyList' that we are processing
165 * @param size number of bytes we can transmit
166 * @param vbuf where to write the messages
167 * @return number of bytes written to vbuf
170 transmit_pending_notification (void *cls,
174 struct NotifyList *nl = cls;
176 struct PendingEntry *pos;
177 struct PendingEntry *next;
178 struct InfoMessage im;
182 nl->transmit_ctx = NULL;
188 hs = (pos->he->hello == NULL) ? 0 : GNUNET_HELLO_size (pos->he->hello);
189 if (left < sizeof (struct InfoMessage) + hs)
192 im.header.size = htons (hs + sizeof (struct InfoMessage));
193 im.header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
194 im.trust = htonl (pos->he->trust);
195 im.peer = pos->he->identity;
196 memcpy (&buf[size - left], &im, sizeof (struct InfoMessage));
197 memcpy (&buf[size - left + sizeof (struct InfoMessage)], pos->he->hello, hs);
198 left -= hs + sizeof (struct InfoMessage);
203 if (nl->pending != NULL)
206 = GNUNET_SERVER_notify_transmit_ready (nl->client,
207 sizeof (struct InfoMessage) + hs,
208 GNUNET_TIME_UNIT_FOREVER_REL,
209 &transmit_pending_notification,
218 * Notify client about host change. Checks if the
219 * respective host entry is already in the list of things
220 * to send to the client, and if not, adds it. Also
221 * triggers a new request for transmission if the pending
222 * list was previously empty.
224 * @param nl client to notify
225 * @param he entry to notify about
228 do_notify (struct NotifyList *nl,
229 struct HostEntry *he)
231 struct PendingEntry *pe;
238 return; /* already in list */
241 pe = GNUNET_malloc (sizeof (struct PendingEntry));
242 pe->next = nl->pending;
245 if (nl->transmit_ctx != NULL)
246 return; /* already trying to transmit */
247 hsize = (he->hello == NULL) ? 0 : GNUNET_HELLO_size (he->hello);
248 nl->transmit_ctx = GNUNET_SERVER_notify_transmit_ready (nl->client,
249 sizeof (struct InfoMessage) + hsize,
250 GNUNET_TIME_UNIT_FOREVER_REL,
251 &transmit_pending_notification,
257 * Notify all clients in the notify list about the
258 * given host entry changing.
261 notify_all (struct HostEntry *he)
263 struct NotifyList *nl;
275 * Address iterator that causes expired entries to be discarded.
277 * @param cls pointer to the current time
278 * @param tname name of the transport
279 * @param expiration expiration time for the address
280 * @param addr the address
281 * @param addrlen length of addr in bytes
282 * @return GNUNET_NO if expiration smaller than the current time
285 discard_expired (void *cls,
287 struct GNUNET_TIME_Absolute expiration,
288 const void *addr, size_t addrlen)
290 const struct GNUNET_TIME_Absolute *now = cls;
291 if (now->value > expiration.value)
298 * Get the filename under which we would store the GNUNET_HELLO_Message
299 * for the given host and protocol.
300 * @return filename of the form DIRECTORY/HOSTID
303 get_host_filename (const struct GNUNET_PeerIdentity *id)
305 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
308 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
309 GNUNET_asprintf (&fn,
310 "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
316 * Get the filename under which we would store the GNUNET_HELLO_Message
317 * for the given host and protocol.
318 * @return filename of the form DIRECTORY/HOSTID
321 get_trust_filename (const struct GNUNET_PeerIdentity *id)
323 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
326 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
327 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
332 * Find the host entry for the given peer. Call
333 * only when synchronized!
334 * @return NULL if not found
336 static struct HostEntry *
337 lookup_host_entry (const struct GNUNET_PeerIdentity *id)
339 struct HostEntry *pos;
342 while ((pos != NULL) &&
344 memcmp (id, &pos->identity, sizeof (struct GNUNET_PeerIdentity))))
351 * Add a host to the list.
353 * @param identity the identity of the host
356 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
358 struct HostEntry *entry;
361 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
362 const struct GNUNET_HELLO_Message *hello;
363 struct GNUNET_HELLO_Message *hello_clean;
365 struct GNUNET_TIME_Absolute now;
367 entry = lookup_host_entry (identity);
370 entry = GNUNET_malloc (sizeof (struct HostEntry));
371 entry->identity = *identity;
372 fn = get_trust_filename (identity);
373 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
374 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
375 entry->disk_trust = entry->trust = ntohl (trust);
378 fn = get_host_filename (identity);
379 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
381 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
382 hello = (const struct GNUNET_HELLO_Message *) buffer;
383 if ( (size < sizeof (struct GNUNET_MessageHeader)) ||
384 (size != ntohs((((const struct GNUNET_MessageHeader*) hello)->size))) ||
385 (size != GNUNET_HELLO_size (hello)) )
388 if (0 != UNLINK (fn))
389 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
395 now = GNUNET_TIME_absolute_get ();
396 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
398 &discard_expired, &now);
399 entry->hello = hello_clean;
410 * Increase the host credit by a value.
412 * @param hostId is the identity of the host
413 * @param value is the int value by which the
414 * host credit is to be increased or decreased
415 * @returns the actual change in trust (positive or negative)
418 change_host_trust (const struct GNUNET_PeerIdentity *hostId, int value)
420 struct HostEntry *host;
421 unsigned int old_trust;
425 host = lookup_host_entry (hostId);
428 add_host_to_known_hosts (hostId);
429 host = lookup_host_entry (hostId);
431 GNUNET_assert (host != NULL);
432 old_trust = host->trust;
435 if (host->trust + value < host->trust)
437 value = ((uint32_t) - 1) - host->trust;
438 host->trust = (uint32_t) - 1; /* maximized */
441 host->trust += value;
445 if (host->trust < -value)
447 value = -host->trust;
451 host->trust += value;
453 if (host->trust != old_trust)
460 * Remove a file that should not be there. LOG
461 * success or failure.
464 remove_garbage (const char *fullname)
466 if (0 == UNLINK (fullname))
467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
469 ("File `%s' in directory `%s' does not match naming convention. "
470 "Removed.\n"), fullname, networkIdDirectory);
472 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
473 GNUNET_ERROR_TYPE_BULK, "unlink", fullname);
478 hosts_directory_scan_callback (void *cls, const char *fullname)
480 unsigned int *matched = cls;
481 struct GNUNET_PeerIdentity identity;
482 const char *filename;
484 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
485 return GNUNET_OK; /* ignore non-files */
486 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
488 remove_garbage (fullname);
492 &fullname[strlen (fullname) -
493 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
494 if (filename[-1] != DIR_SEPARATOR)
496 remove_garbage (fullname);
499 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (filename,
500 &identity.hashPubKey))
502 remove_garbage (fullname);
506 add_host_to_known_hosts (&identity);
512 * Call this method periodically to scan data/hosts for new hosts.
515 cron_scan_directory_data_hosts (void *cls,
516 const struct GNUNET_SCHEDULER_TaskContext *tc)
518 static unsigned int retries;
521 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
524 GNUNET_DISK_directory_create (networkIdDirectory);
525 GNUNET_DISK_directory_scan (networkIdDirectory,
526 &hosts_directory_scan_callback, &count);
527 if ((0 == count) && (0 == (++retries & 31)))
528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
529 GNUNET_ERROR_TYPE_BULK,
530 _("Still no peers found in `%s'!\n"), networkIdDirectory);
531 GNUNET_SCHEDULER_add_delayed (tc->sched,
533 &cron_scan_directory_data_hosts, NULL);
538 * Bind a host address (hello) to a hostId.
540 * @param peer the peer for which this is a hello
541 * @param hello the verified (!) hello message
544 bind_address (const struct GNUNET_PeerIdentity *peer,
545 const struct GNUNET_HELLO_Message *hello)
548 struct HostEntry *host;
549 struct GNUNET_HELLO_Message *mrg;
551 add_host_to_known_hosts (peer);
552 host = lookup_host_entry (peer);
553 GNUNET_assert (host != NULL);
554 if (host->hello == NULL)
556 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
557 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
561 mrg = GNUNET_HELLO_merge (host->hello, hello);
562 /* FIXME: check if old and merged hello are equal,
563 and if so, bail out early... */
564 GNUNET_free (host->hello);
567 fn = get_host_filename (peer);
568 GNUNET_DISK_directory_create_for_file (fn);
569 GNUNET_DISK_fn_write (fn,
571 GNUNET_HELLO_size (host->hello),
572 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
573 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
580 * Do transmit info either for only the host matching the given
581 * argument or for all known hosts and change their trust values by
584 * @param only NULL to hit all hosts, otherwise specifies a particular target
585 * @param trust_change how much should the trust be changed
586 * @param client who is making the request (and will thus receive our confirmation)
589 send_to_each_host (const struct GNUNET_PeerIdentity *only,
590 int trust_change, struct GNUNET_SERVER_Client *client)
592 struct HostEntry *pos;
593 struct InfoMessage *im;
594 const struct GNUNET_MessageHeader *end;
596 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
597 struct GNUNET_SERVER_TransmitContext *tc;
599 tc = GNUNET_SERVER_transmit_context_create (client);
603 if ((only == NULL) ||
605 memcmp (only, &pos->identity,
606 sizeof (struct GNUNET_PeerIdentity))))
608 change_host_trust (&pos->identity, trust_change);
610 im = (struct InfoMessage *) buf;
611 if (pos->hello != NULL)
613 hs = GNUNET_HELLO_size (pos->hello);
615 GNUNET_SERVER_MAX_MESSAGE_SIZE -
616 sizeof (struct InfoMessage));
617 memcpy (&im[1], pos->hello, hs);
619 im->trust = htonl (pos->trust);
620 im->peer = pos->identity;
622 GNUNET_SERVER_transmit_context_append (tc,
625 sizeof (struct InfoMessage) -
627 GNUNET_MessageHeader),
628 GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
632 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
633 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
634 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
639 * Write host-trust information to a file - flush the buffer entry!
640 * Assumes synchronized access.
643 flush_trust (struct HostEntry *host)
648 if (host->trust == host->disk_trust)
649 return; /* unchanged */
650 fn = get_trust_filename (&host->identity);
651 if (host->trust == 0)
653 if ((0 != UNLINK (fn)) && (errno != ENOENT))
654 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
655 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
659 trust = htonl (host->trust);
660 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
662 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
663 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
664 host->disk_trust = host->trust;
670 * Call this method periodically to scan data/hosts for new hosts.
673 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
675 struct HostEntry *pos;
683 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
685 GNUNET_SCHEDULER_add_delayed (tc->sched,
686 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
691 * @brief delete expired HELLO entries in data/hosts/
694 discard_hosts_helper (void *cls, const char *fn)
696 struct GNUNET_TIME_Absolute *now = cls;
697 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
698 const struct GNUNET_HELLO_Message *hello;
699 struct GNUNET_HELLO_Message *new_hello;
702 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
703 if ((size < sizeof (struct GNUNET_MessageHeader)) && (0 != UNLINK (fn)))
705 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
706 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
709 hello = (const struct GNUNET_HELLO_Message *) buffer;
710 new_hello = GNUNET_HELLO_iterate_addresses (hello,
712 &discard_expired, now);
713 if ((new_hello == NULL) && (0 != UNLINK (fn)))
714 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
715 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
716 if (new_hello != NULL)
718 GNUNET_DISK_fn_write (fn,
720 GNUNET_HELLO_size (new_hello),
721 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
722 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
723 GNUNET_free (new_hello);
730 * Call this method periodically to scan data/hosts for new hosts.
733 cron_clean_data_hosts (void *cls,
734 const struct GNUNET_SCHEDULER_TaskContext *tc)
736 struct GNUNET_TIME_Absolute now;
738 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
740 now = GNUNET_TIME_absolute_get ();
741 GNUNET_DISK_directory_scan (networkIdDirectory,
742 &discard_hosts_helper, &now);
744 GNUNET_SCHEDULER_add_delayed (tc->sched,
745 DATA_HOST_CLEAN_FREQ,
746 &cron_clean_data_hosts, NULL);
751 * Handle ADD-message.
754 * @param client identification of the client
755 * @param message the actual message
758 handle_add (void *cls,
759 struct GNUNET_SERVER_Client *client,
760 const struct GNUNET_MessageHeader *message)
762 const struct PeerAddMessage *pam;
763 const struct GNUNET_MessageHeader *hello;
766 size = ntohs (message->size);
768 sizeof (struct PeerAddMessage) + sizeof (struct GNUNET_MessageHeader))
771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
774 pam = (const struct PeerAddMessage *) message;
775 hello = (const struct GNUNET_MessageHeader *) &pam[1];
776 if (size != sizeof (struct PeerAddMessage) + ntohs (hello->size))
779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
782 bind_address (&pam->peer, (const struct GNUNET_HELLO_Message *) hello);
783 GNUNET_SERVER_receive_done (client, GNUNET_OK);
788 * Handle GET-message.
791 * @param client identification of the client
792 * @param message the actual message
795 handle_get (void *cls,
796 struct GNUNET_SERVER_Client *client,
797 const struct GNUNET_MessageHeader *message)
799 const struct ListPeerMessage *lpm;
801 lpm = (const struct ListPeerMessage *) message;
802 send_to_each_host (&lpm->peer, ntohl (lpm->trust_change), client);
807 * Handle GET-ALL-message.
810 * @param client identification of the client
811 * @param message the actual message
814 handle_get_all (void *cls,
815 struct GNUNET_SERVER_Client *client,
816 const struct GNUNET_MessageHeader *message)
818 const struct ListAllPeersMessage *lpm;
820 lpm = (const struct ListAllPeersMessage *) message;
821 send_to_each_host (NULL, ntohl (lpm->trust_change), client);
826 * Handle NOTIFY-message.
829 * @param client identification of the client
830 * @param message the actual message
833 handle_notify (void *cls,
834 struct GNUNET_SERVER_Client *client,
835 const struct GNUNET_MessageHeader *message)
837 struct NotifyList *nl;
838 struct HostEntry *pos;
840 nl = GNUNET_malloc (sizeof (struct NotifyList));
841 nl->next = notify_list;
843 GNUNET_SERVER_client_keep (client);
855 * List of handlers for the messages understood by this
858 static struct GNUNET_SERVER_MessageHandler handlers[] = {
859 {&handle_add, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_ADD, 0},
860 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
861 sizeof (struct ListPeerMessage)},
862 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
863 sizeof (struct ListAllPeersMessage)},
864 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
865 sizeof (struct GNUNET_MessageHeader)},
871 * Function that is called when a client disconnects.
874 notify_disconnect (void *cls,
875 struct GNUNET_SERVER_Client *client)
877 struct NotifyList *pos;
878 struct NotifyList *prev;
879 struct NotifyList *next;
880 struct PendingEntry *p;
887 if (pos->client == client)
889 while (NULL != (p = pos->pending))
891 pos->pending = p->next;
894 if (pos->transmit_ctx != NULL)
896 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->transmit_ctx);
897 pos->transmit_ctx = NULL;
903 GNUNET_SERVER_client_drop (client);
917 * Process statistics requests.
920 * @param sched scheduler to use
921 * @param server the initialized server
922 * @param cfg configuration to use
926 struct GNUNET_SCHEDULER_Handle *sched,
927 struct GNUNET_SERVER_Handle *server,
928 const struct GNUNET_CONFIGURATION_Handle *cfg)
930 GNUNET_assert (GNUNET_OK ==
931 GNUNET_CONFIGURATION_get_value_filename (cfg,
934 &networkIdDirectory));
935 GNUNET_assert (GNUNET_OK ==
936 GNUNET_CONFIGURATION_get_value_filename (cfg,
940 GNUNET_DISK_directory_create (networkIdDirectory);
941 GNUNET_DISK_directory_create (trustDirectory);
942 GNUNET_SCHEDULER_add_with_priority (sched,
943 GNUNET_SCHEDULER_PRIORITY_IDLE,
944 &cron_scan_directory_data_hosts, NULL);
945 GNUNET_SCHEDULER_add_with_priority (sched,
946 GNUNET_SCHEDULER_PRIORITY_HIGH,
947 &cron_flush_trust, NULL);
948 GNUNET_SCHEDULER_add_with_priority (sched,
949 GNUNET_SCHEDULER_PRIORITY_IDLE,
950 &cron_clean_data_hosts, NULL);
951 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, NULL);
952 GNUNET_SERVER_add_handlers (server, handlers);
957 * The main function for the statistics service.
959 * @param argc number of arguments from the command line
960 * @param argv command line arguments
961 * @return 0 ok, 1 on error
964 main (int argc, char *const *argv)
969 GNUNET_SERVICE_run (argc,
972 GNUNET_SERVICE_OPTION_NONE,
973 &run, NULL)) ? 0 : 1;
974 GNUNET_free_non_null (networkIdDirectory);
975 GNUNET_free_non_null (trustDirectory);
980 /* end of gnunet-service-peerinfo.c */