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;
110 * Clients to notify of changes to the peer information.
116 * This is a linked list.
118 struct NotifyList *next;
123 struct GNUNET_SERVER_Client *client;
126 * Notifications pending for this entry.
128 struct PendingEntry *pending;
131 * Handle for a transmit ready request.
133 struct GNUNET_CONNECTION_TransmitHandle *transmit_ctx;
138 * The in-memory list of known hosts.
140 static struct HostEntry *hosts;
143 * Clients to immediately notify about all changes.
145 static struct NotifyList *notify_list;
148 * Directory where the hellos are stored in (data/hosts)
150 static char *networkIdDirectory;
153 * Where do we store trust information?
155 static char *trustDirectory;
159 * Transmit peer information messages from the pending queue
162 * @param cls the 'struct NotifyList' that we are processing
163 * @param size number of bytes we can transmit
164 * @param vbuf where to write the messages
165 * @return number of bytes written to vbuf
168 transmit_pending_notification (void *cls,
172 struct NotifyList *nl = cls;
174 struct PendingEntry *pos;
175 struct PendingEntry *next;
176 struct InfoMessage im;
180 nl->transmit_ctx = NULL;
186 hs = (pos->he->hello == NULL) ? 0 : GNUNET_HELLO_size (pos->he->hello);
187 if (left < sizeof (struct InfoMessage) + hs)
190 im.header.size = htons (hs + sizeof (struct InfoMessage));
191 im.header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
192 im.trust = htonl (pos->he->trust);
193 im.peer = pos->he->identity;
194 memcpy (&buf[size - left], &im, sizeof (struct InfoMessage));
195 memcpy (&buf[size - left + sizeof (struct InfoMessage)], pos->he->hello, hs);
196 left -= hs + sizeof (struct InfoMessage);
201 if (nl->pending != NULL)
204 = GNUNET_SERVER_notify_transmit_ready (nl->client,
205 sizeof (struct InfoMessage) + hs,
206 GNUNET_TIME_UNIT_FOREVER_REL,
207 &transmit_pending_notification,
216 * Notify client about host change. Checks if the
217 * respective host entry is already in the list of things
218 * to send to the client, and if not, adds it. Also
219 * triggers a new request for transmission if the pending
220 * list was previously empty.
222 * @param nl client to notify
223 * @param hc entry to notify about
226 do_notify (struct NotifyList *nl,
227 struct HostEntry *he)
229 struct PendingEntry *pe;
236 return; /* already in list */
239 pe = GNUNET_malloc (sizeof (struct PendingEntry));
240 pe->next = nl->pending;
243 if (nl->transmit_ctx != NULL)
244 return; /* already trying to transmit */
245 hsize = (he->hello == NULL) ? 0 : GNUNET_HELLO_size (he->hello);
246 nl->transmit_ctx = GNUNET_SERVER_notify_transmit_ready (nl->client,
247 sizeof (struct InfoMessage) + hsize,
248 GNUNET_TIME_UNIT_FOREVER_REL,
249 &transmit_pending_notification,
255 * Notify all clients in the notify list about the
256 * given host entry changing.
259 notify_all (struct HostEntry *he)
261 struct NotifyList *nl;
273 * Address iterator that causes expired entries to be discarded.
275 * @param cls pointer to the current time
276 * @param tname name of the transport
277 * @param expiration expiration time for the address
278 * @param addr the address
279 * @param addrlen length of addr in bytes
280 * @return GNUNET_NO if expiration smaller than the current time
283 discard_expired (void *cls,
285 struct GNUNET_TIME_Absolute expiration,
286 const void *addr, size_t addrlen)
288 const struct GNUNET_TIME_Absolute *now = cls;
289 if (now->value > expiration.value)
296 * Get the filename under which we would store the GNUNET_HELLO_Message
297 * for the given host and protocol.
298 * @return filename of the form DIRECTORY/HOSTID
301 get_host_filename (const struct GNUNET_PeerIdentity *id)
303 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
306 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
307 GNUNET_asprintf (&fn,
308 "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
314 * Get the filename under which we would store the GNUNET_HELLO_Message
315 * for the given host and protocol.
316 * @return filename of the form DIRECTORY/HOSTID
319 get_trust_filename (const struct GNUNET_PeerIdentity *id)
321 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
324 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
325 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
330 * Find the host entry for the given peer. Call
331 * only when synchronized!
332 * @return NULL if not found
334 static struct HostEntry *
335 lookup_host_entry (const struct GNUNET_PeerIdentity *id)
337 struct HostEntry *pos;
340 while ((pos != NULL) &&
342 memcmp (id, &pos->identity, sizeof (struct GNUNET_PeerIdentity))))
349 * Add a host to the list.
351 * @param identity the identity of the host
354 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
356 struct HostEntry *entry;
359 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
360 const struct GNUNET_HELLO_Message *hello;
361 struct GNUNET_HELLO_Message *hello_clean;
363 struct GNUNET_TIME_Absolute now;
365 entry = lookup_host_entry (identity);
368 entry = GNUNET_malloc (sizeof (struct HostEntry));
369 entry->identity = *identity;
370 fn = get_trust_filename (identity);
371 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
372 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
373 entry->disk_trust = entry->trust = ntohl (trust);
376 fn = get_host_filename (identity);
377 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
379 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
380 hello = (const struct GNUNET_HELLO_Message *) buffer;
381 if ( (size < sizeof (struct GNUNET_MessageHeader)) ||
382 (size != ntohs((((const struct GNUNET_MessageHeader*) hello)->size))) ||
383 (size != GNUNET_HELLO_size (hello)) )
386 if (0 != UNLINK (fn))
387 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
393 now = GNUNET_TIME_absolute_get ();
394 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
396 &discard_expired, &now);
397 entry->hello = hello_clean;
408 * Increase the host credit by a value.
410 * @param hostId is the identity of the host
411 * @param value is the int value by which the
412 * host credit is to be increased or decreased
413 * @returns the actual change in trust (positive or negative)
416 change_host_trust (const struct GNUNET_PeerIdentity *hostId, int value)
418 struct HostEntry *host;
419 unsigned int old_trust;
423 host = lookup_host_entry (hostId);
426 add_host_to_known_hosts (hostId);
427 host = lookup_host_entry (hostId);
429 GNUNET_assert (host != NULL);
430 old_trust = host->trust;
433 if (host->trust + value < host->trust)
435 value = ((uint32_t) - 1) - host->trust;
436 host->trust = (uint32_t) - 1; /* maximized */
439 host->trust += value;
443 if (host->trust < -value)
445 value = -host->trust;
449 host->trust += value;
451 if (host->trust != old_trust)
458 * Remove a file that should not be there. LOG
459 * success or failure.
462 remove_garbage (const char *fullname)
464 if (0 == UNLINK (fullname))
465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
467 ("File `%s' in directory `%s' does not match naming convention. "
468 "Removed.\n"), fullname, networkIdDirectory);
470 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
471 GNUNET_ERROR_TYPE_BULK, "unlink", fullname);
476 hosts_directory_scan_callback (void *cls, const char *fullname)
478 unsigned int *matched = cls;
479 struct GNUNET_PeerIdentity identity;
480 const char *filename;
482 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
483 return GNUNET_OK; /* ignore non-files */
484 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
486 remove_garbage (fullname);
490 &fullname[strlen (fullname) -
491 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
492 if (filename[-1] != DIR_SEPARATOR)
494 remove_garbage (fullname);
497 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (filename,
498 &identity.hashPubKey))
500 remove_garbage (fullname);
504 add_host_to_known_hosts (&identity);
510 * Call this method periodically to scan data/hosts for new hosts.
513 cron_scan_directory_data_hosts (void *cls,
514 const struct GNUNET_SCHEDULER_TaskContext *tc)
516 static unsigned int retries;
519 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
522 GNUNET_DISK_directory_create (networkIdDirectory);
523 GNUNET_DISK_directory_scan (networkIdDirectory,
524 &hosts_directory_scan_callback, &count);
525 if ((0 == count) && (0 == (++retries & 31)))
526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
527 GNUNET_ERROR_TYPE_BULK,
528 _("Still no peers found in `%s'!\n"), networkIdDirectory);
529 GNUNET_SCHEDULER_add_delayed (tc->sched,
531 &cron_scan_directory_data_hosts, NULL);
536 * Bind a host address (hello) to a hostId.
538 * @param peer the peer for which this is a hello
539 * @param hello the verified (!) hello message
542 bind_address (const struct GNUNET_PeerIdentity *peer,
543 const struct GNUNET_HELLO_Message *hello)
546 struct HostEntry *host;
547 struct GNUNET_HELLO_Message *mrg;
549 add_host_to_known_hosts (peer);
550 host = lookup_host_entry (peer);
551 GNUNET_assert (host != NULL);
552 if (host->hello == NULL)
554 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
555 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
559 mrg = GNUNET_HELLO_merge (host->hello, hello);
560 /* FIXME: check if old and merged hello are equal,
561 and if so, bail out early... */
562 GNUNET_free (host->hello);
565 fn = get_host_filename (peer);
566 GNUNET_DISK_fn_write (fn,
568 GNUNET_HELLO_size (host->hello),
569 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
570 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
577 * Do transmit info either for only the host matching the given
578 * argument or for all known hosts and change their trust values by
581 * @param only NULL to hit all hosts, otherwise specifies a particular target
582 * @param trust_change how much should the trust be changed
583 * @param client who is making the request (and will thus receive our confirmation)
586 send_to_each_host (const struct GNUNET_PeerIdentity *only,
587 int trust_change, struct GNUNET_SERVER_Client *client)
589 struct HostEntry *pos;
590 struct InfoMessage *im;
591 const struct GNUNET_MessageHeader *end;
593 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
594 struct GNUNET_SERVER_TransmitContext *tc;
596 tc = GNUNET_SERVER_transmit_context_create (client);
600 if ((only == NULL) ||
602 memcmp (only, &pos->identity,
603 sizeof (struct GNUNET_PeerIdentity))))
605 change_host_trust (&pos->identity, trust_change);
607 im = (struct InfoMessage *) buf;
608 if (pos->hello != NULL)
610 hs = GNUNET_HELLO_size (pos->hello);
612 GNUNET_SERVER_MAX_MESSAGE_SIZE -
613 sizeof (struct InfoMessage));
614 memcpy (&im[1], pos->hello, hs);
616 im->trust = htonl (pos->trust);
617 im->peer = pos->identity;
619 GNUNET_SERVER_transmit_context_append (tc,
622 sizeof (struct InfoMessage) -
624 GNUNET_MessageHeader),
625 GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
629 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
630 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
631 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
636 * Write host-trust information to a file - flush the buffer entry!
637 * Assumes synchronized access.
640 flush_trust (struct HostEntry *host)
645 if (host->trust == host->disk_trust)
646 return; /* unchanged */
647 fn = get_trust_filename (&host->identity);
648 if (host->trust == 0)
650 if ((0 != UNLINK (fn)) && (errno != ENOENT))
651 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
652 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
656 trust = htonl (host->trust);
657 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
659 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
660 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
661 host->disk_trust = host->trust;
667 * Call this method periodically to scan data/hosts for new hosts.
670 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
672 struct HostEntry *pos;
680 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
682 GNUNET_SCHEDULER_add_delayed (tc->sched,
683 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
688 * @brief delete expired HELLO entries in data/hosts/
691 discard_hosts_helper (void *cls, const char *fn)
693 struct GNUNET_TIME_Absolute *now = cls;
694 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
695 const struct GNUNET_HELLO_Message *hello;
696 struct GNUNET_HELLO_Message *new_hello;
699 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
700 if ((size < sizeof (struct GNUNET_MessageHeader)) && (0 != UNLINK (fn)))
702 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
703 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
706 hello = (const struct GNUNET_HELLO_Message *) buffer;
707 new_hello = GNUNET_HELLO_iterate_addresses (hello,
709 &discard_expired, now);
710 if ((new_hello == NULL) && (0 != UNLINK (fn)))
711 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
712 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
713 if (new_hello != NULL)
715 GNUNET_DISK_fn_write (fn,
717 GNUNET_HELLO_size (new_hello),
718 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
719 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
720 GNUNET_free (new_hello);
727 * Call this method periodically to scan data/hosts for new hosts.
730 cron_clean_data_hosts (void *cls,
731 const struct GNUNET_SCHEDULER_TaskContext *tc)
733 struct GNUNET_TIME_Absolute now;
735 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
737 now = GNUNET_TIME_absolute_get ();
738 GNUNET_DISK_directory_scan (networkIdDirectory,
739 &discard_hosts_helper, &now);
741 GNUNET_SCHEDULER_add_delayed (tc->sched,
742 DATA_HOST_CLEAN_FREQ,
743 &cron_clean_data_hosts, NULL);
748 * Handle ADD-message.
751 * @param client identification of the client
752 * @param message the actual message
755 handle_add (void *cls,
756 struct GNUNET_SERVER_Client *client,
757 const struct GNUNET_MessageHeader *message)
759 const struct PeerAddMessage *pam;
760 const struct GNUNET_MessageHeader *hello;
763 size = ntohs (message->size);
765 sizeof (struct PeerAddMessage) + sizeof (struct GNUNET_MessageHeader))
768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
771 pam = (const struct PeerAddMessage *) message;
772 hello = (const struct GNUNET_MessageHeader *) &pam[1];
773 if (size != sizeof (struct PeerAddMessage) + ntohs (hello->size))
776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
779 bind_address (&pam->peer, (const struct GNUNET_HELLO_Message *) hello);
780 GNUNET_SERVER_receive_done (client, GNUNET_OK);
785 * Handle GET-message.
788 * @param client identification of the client
789 * @param message the actual message
792 handle_get (void *cls,
793 struct GNUNET_SERVER_Client *client,
794 const struct GNUNET_MessageHeader *message)
796 const struct ListPeerMessage *lpm;
798 lpm = (const struct ListPeerMessage *) message;
799 send_to_each_host (&lpm->peer, ntohl (lpm->trust_change), client);
804 * Handle GET-ALL-message.
807 * @param client identification of the client
808 * @param message the actual message
811 handle_get_all (void *cls,
812 struct GNUNET_SERVER_Client *client,
813 const struct GNUNET_MessageHeader *message)
815 const struct ListAllPeersMessage *lpm;
817 lpm = (const struct ListAllPeersMessage *) message;
818 send_to_each_host (NULL, ntohl (lpm->trust_change), client);
823 * Handle NOTIFY-message.
826 * @param client identification of the client
827 * @param message the actual message
830 handle_notify (void *cls,
831 struct GNUNET_SERVER_Client *client,
832 const struct GNUNET_MessageHeader *message)
834 struct NotifyList *nl;
835 struct HostEntry *pos;
837 nl = GNUNET_malloc (sizeof (struct NotifyList));
838 nl->next = notify_list;
840 GNUNET_SERVER_client_keep (client);
852 * List of handlers for the messages understood by this
855 static struct GNUNET_SERVER_MessageHandler handlers[] = {
856 {&handle_add, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_ADD, 0},
857 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
858 sizeof (struct ListPeerMessage)},
859 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
860 sizeof (struct ListAllPeersMessage)},
861 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
862 sizeof (struct GNUNET_MessageHeader)},
868 * Function that is called when a client disconnects.
871 notify_disconnect (void *cls,
872 struct GNUNET_SERVER_Client *client)
874 struct NotifyList *pos;
875 struct NotifyList *prev;
876 struct NotifyList *next;
877 struct PendingEntry *p;
884 if (pos->client == client)
886 while (NULL != (p = pos->pending))
888 pos->pending = p->next;
891 if (pos->transmit_ctx != NULL)
893 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->transmit_ctx);
894 pos->transmit_ctx = NULL;
900 GNUNET_SERVER_client_drop (client);
914 * Process statistics requests.
917 * @param sched scheduler to use
918 * @param server the initialized server
919 * @param cfg configuration to use
923 struct GNUNET_SCHEDULER_Handle *sched,
924 struct GNUNET_SERVER_Handle *server,
925 const struct GNUNET_CONFIGURATION_Handle *cfg)
927 GNUNET_assert (GNUNET_OK ==
928 GNUNET_CONFIGURATION_get_value_filename (cfg,
931 &networkIdDirectory));
932 GNUNET_assert (GNUNET_OK ==
933 GNUNET_CONFIGURATION_get_value_filename (cfg,
937 GNUNET_DISK_directory_create (networkIdDirectory);
938 GNUNET_DISK_directory_create (trustDirectory);
939 GNUNET_SCHEDULER_add_with_priority (sched,
940 GNUNET_SCHEDULER_PRIORITY_IDLE,
941 &cron_scan_directory_data_hosts, NULL);
942 GNUNET_SCHEDULER_add_with_priority (sched,
943 GNUNET_SCHEDULER_PRIORITY_HIGH,
944 &cron_flush_trust, NULL);
945 GNUNET_SCHEDULER_add_with_priority (sched,
946 GNUNET_SCHEDULER_PRIORITY_IDLE,
947 &cron_clean_data_hosts, NULL);
948 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, NULL);
949 GNUNET_SERVER_add_handlers (server, handlers);
954 * The main function for the statistics service.
956 * @param argc number of arguments from the command line
957 * @param argv command line arguments
958 * @return 0 ok, 1 on error
961 main (int argc, char *const *argv)
966 GNUNET_SERVICE_run (argc,
969 GNUNET_SERVICE_OPTION_NONE,
970 &run, NULL)) ? 0 : 1;
971 GNUNET_free_non_null (networkIdDirectory);
972 GNUNET_free_non_null (trustDirectory);
977 /* end of gnunet-service-peerinfo.c */