2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009 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;
184 while ( (pos != NULL) &&
185 (left >= sizeof (struct InfoMessage) + (hs = GNUNET_HELLO_size (pos->he->hello))) )
188 im.header.size = htons (hs + sizeof (struct InfoMessage));
189 im.header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
190 im.trust = htonl (pos->he->trust);
191 im.peer = pos->he->identity;
192 memcpy (&buf[size - left], &im, sizeof (struct InfoMessage));
193 memcpy (&buf[size - left + sizeof (struct InfoMessage)], pos->he->hello, hs);
194 left -= hs + sizeof (struct InfoMessage);
199 if (nl->pending != NULL)
202 = GNUNET_SERVER_notify_transmit_ready (nl->client,
203 sizeof (struct InfoMessage) + hs,
204 GNUNET_TIME_UNIT_FOREVER_REL,
205 &transmit_pending_notification,
214 * Notify client about host change. Checks if the
215 * respective host entry is already in the list of things
216 * to send to the client, and if not, adds it. Also
217 * triggers a new request for transmission if the pending
218 * list was previously empty.
220 * @param nl client to notify
221 * @param hc entry to notify about
224 do_notify (struct NotifyList *nl,
225 struct HostEntry *he)
227 struct PendingEntry *pe;
233 return; /* already in list */
236 pe = GNUNET_malloc (sizeof (struct PendingEntry));
237 pe->next = nl->pending;
240 if (nl->transmit_ctx != NULL)
241 return; /* already trying to transmit */
242 nl->transmit_ctx = GNUNET_SERVER_notify_transmit_ready (nl->client,
243 sizeof (struct InfoMessage) + GNUNET_HELLO_size (he->hello),
244 GNUNET_TIME_UNIT_FOREVER_REL,
245 &transmit_pending_notification,
251 * Notify all clients in the notify list about the
252 * given host entry changing.
255 notify_all (struct HostEntry *he)
257 struct NotifyList *nl;
269 * Address iterator that causes expired entries to be discarded.
271 * @param cls pointer to the current time
272 * @param tname name of the transport
273 * @param expiration expiration time for the address
274 * @param addr the address
275 * @param addrlen length of addr in bytes
276 * @return GNUNET_NO if expiration smaller than the current time
279 discard_expired (void *cls,
281 struct GNUNET_TIME_Absolute expiration,
282 const void *addr, size_t addrlen)
284 const struct GNUNET_TIME_Absolute *now = cls;
285 if (now->value > expiration.value)
292 * Get the filename under which we would store the GNUNET_HELLO_Message
293 * for the given host and protocol.
294 * @return filename of the form DIRECTORY/HOSTID
297 get_host_filename (const struct GNUNET_PeerIdentity *id)
299 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
302 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
303 GNUNET_asprintf (&fn,
304 "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
310 * Get the filename under which we would store the GNUNET_HELLO_Message
311 * for the given host and protocol.
312 * @return filename of the form DIRECTORY/HOSTID
315 get_trust_filename (const struct GNUNET_PeerIdentity *id)
317 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
320 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
321 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
326 * Find the host entry for the given peer. Call
327 * only when synchronized!
328 * @return NULL if not found
330 static struct HostEntry *
331 lookup_host_entry (const struct GNUNET_PeerIdentity *id)
333 struct HostEntry *pos;
336 while ((pos != NULL) &&
338 memcmp (id, &pos->identity, sizeof (struct GNUNET_PeerIdentity))))
345 * Add a host to the list.
347 * @param identity the identity of the host
350 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
352 struct HostEntry *entry;
355 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
356 const struct GNUNET_HELLO_Message *hello;
357 struct GNUNET_HELLO_Message *hello_clean;
359 struct GNUNET_TIME_Absolute now;
361 entry = lookup_host_entry (identity);
364 entry = GNUNET_malloc (sizeof (struct HostEntry));
365 entry->identity = *identity;
366 fn = get_trust_filename (identity);
367 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
368 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
369 entry->disk_trust = entry->trust = ntohl (trust);
372 fn = get_host_filename (identity);
373 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
375 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
376 hello = (const struct GNUNET_HELLO_Message *) buffer;
377 if ( (size < sizeof (struct GNUNET_MessageHeader)) ||
378 (size != ntohs((((const struct GNUNET_MessageHeader*) hello)->size))) ||
379 (size != GNUNET_HELLO_size (hello)) )
382 if (0 != UNLINK (fn))
383 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
389 now = GNUNET_TIME_absolute_get ();
390 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
392 &discard_expired, &now);
393 entry->hello = hello_clean;
404 * Increase the host credit by a value.
406 * @param hostId is the identity of the host
407 * @param value is the int value by which the
408 * host credit is to be increased or decreased
409 * @returns the actual change in trust (positive or negative)
412 change_host_trust (const struct GNUNET_PeerIdentity *hostId, int value)
414 struct HostEntry *host;
415 unsigned int old_trust;
419 host = lookup_host_entry (hostId);
422 add_host_to_known_hosts (hostId);
423 host = lookup_host_entry (hostId);
425 GNUNET_assert (host != NULL);
426 old_trust = host->trust;
429 if (host->trust + value < host->trust)
431 value = ((uint32_t) - 1) - host->trust;
432 host->trust = (uint32_t) - 1; /* maximized */
435 host->trust += value;
439 if (host->trust < -value)
441 value = -host->trust;
445 host->trust += value;
447 if (host->trust != old_trust)
454 * Remove a file that should not be there. LOG
455 * success or failure.
458 remove_garbage (const char *fullname)
460 if (0 == UNLINK (fullname))
461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
463 ("File `%s' in directory `%s' does not match naming convention. "
464 "Removed.\n"), fullname, networkIdDirectory);
466 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
467 GNUNET_ERROR_TYPE_BULK, "unlink", fullname);
472 hosts_directory_scan_callback (void *cls, const char *fullname)
474 unsigned int *matched = cls;
475 struct GNUNET_PeerIdentity identity;
476 const char *filename;
478 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
479 return GNUNET_OK; /* ignore non-files */
480 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
482 remove_garbage (fullname);
486 &fullname[strlen (fullname) -
487 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
488 if (filename[-1] != DIR_SEPARATOR)
490 remove_garbage (fullname);
493 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (filename,
494 &identity.hashPubKey))
496 remove_garbage (fullname);
500 add_host_to_known_hosts (&identity);
506 * Call this method periodically to scan data/hosts for new hosts.
509 cron_scan_directory_data_hosts (void *cls,
510 const struct GNUNET_SCHEDULER_TaskContext *tc)
512 static unsigned int retries;
515 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
518 GNUNET_DISK_directory_create (networkIdDirectory);
519 GNUNET_DISK_directory_scan (networkIdDirectory,
520 &hosts_directory_scan_callback, &count);
521 if ((0 == count) && (0 == (++retries & 31)))
522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
523 GNUNET_ERROR_TYPE_BULK,
524 _("Still no peers found in `%s'!\n"), networkIdDirectory);
525 GNUNET_SCHEDULER_add_delayed (tc->sched,
527 &cron_scan_directory_data_hosts, NULL);
532 * Bind a host address (hello) to a hostId.
534 * @param peer the peer for which this is a hello
535 * @param hello the verified (!) hello message
538 bind_address (const struct GNUNET_PeerIdentity *peer,
539 const struct GNUNET_HELLO_Message *hello)
542 struct HostEntry *host;
543 struct GNUNET_HELLO_Message *mrg;
545 add_host_to_known_hosts (peer);
546 host = lookup_host_entry (peer);
547 GNUNET_assert (host != NULL);
548 if (host->hello == NULL)
550 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
551 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
555 mrg = GNUNET_HELLO_merge (host->hello, hello);
556 /* FIXME: check if old and merged hello are equal,
557 and if so, bail out early... */
558 GNUNET_free (host->hello);
561 fn = get_host_filename (peer);
562 GNUNET_DISK_fn_write (fn,
564 GNUNET_HELLO_size (host->hello),
565 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
566 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
573 * Do transmit info either for only the host matching the given
574 * argument or for all known hosts and change their trust values by
577 * @param only NULL to hit all hosts, otherwise specifies a particular target
578 * @param trust_change how much should the trust be changed
579 * @param client who is making the request (and will thus receive our confirmation)
582 send_to_each_host (const struct GNUNET_PeerIdentity *only,
583 int trust_change, struct GNUNET_SERVER_Client *client)
585 struct HostEntry *pos;
586 struct InfoMessage *im;
587 const struct GNUNET_MessageHeader *end;
589 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
590 struct GNUNET_SERVER_TransmitContext *tc;
592 tc = GNUNET_SERVER_transmit_context_create (client);
596 if ((only == NULL) ||
598 memcmp (only, &pos->identity,
599 sizeof (struct GNUNET_PeerIdentity))))
601 change_host_trust (&pos->identity, trust_change);
603 im = (struct InfoMessage *) buf;
604 if (pos->hello != NULL)
606 hs = GNUNET_HELLO_size (pos->hello);
608 GNUNET_SERVER_MAX_MESSAGE_SIZE -
609 sizeof (struct InfoMessage));
610 memcpy (&im[1], pos->hello, hs);
612 im->trust = htonl (pos->trust);
613 im->peer = pos->identity;
615 GNUNET_SERVER_transmit_context_append (tc,
618 sizeof (struct InfoMessage) -
620 GNUNET_MessageHeader),
621 GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
625 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
626 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
627 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
632 * Write host-trust information to a file - flush the buffer entry!
633 * Assumes synchronized access.
636 flush_trust (struct HostEntry *host)
641 if (host->trust == host->disk_trust)
642 return; /* unchanged */
643 fn = get_trust_filename (&host->identity);
644 if (host->trust == 0)
646 if ((0 != UNLINK (fn)) && (errno != ENOENT))
647 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
648 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
652 trust = htonl (host->trust);
653 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
655 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
656 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
657 host->disk_trust = host->trust;
663 * Call this method periodically to scan data/hosts for new hosts.
666 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
668 struct HostEntry *pos;
676 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
678 GNUNET_SCHEDULER_add_delayed (tc->sched,
679 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
684 * @brief delete expired HELLO entries in data/hosts/
687 discard_hosts_helper (void *cls, const char *fn)
689 struct GNUNET_TIME_Absolute *now = cls;
690 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
691 const struct GNUNET_HELLO_Message *hello;
692 struct GNUNET_HELLO_Message *new_hello;
695 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
696 if ((size < sizeof (struct GNUNET_MessageHeader)) && (0 != UNLINK (fn)))
698 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
699 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
702 hello = (const struct GNUNET_HELLO_Message *) buffer;
703 new_hello = GNUNET_HELLO_iterate_addresses (hello,
705 &discard_expired, now);
706 if ((new_hello == NULL) && (0 != UNLINK (fn)))
707 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
708 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
709 if (new_hello != NULL)
711 GNUNET_DISK_fn_write (fn,
713 GNUNET_HELLO_size (new_hello),
714 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
715 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
716 GNUNET_free (new_hello);
723 * Call this method periodically to scan data/hosts for new hosts.
726 cron_clean_data_hosts (void *cls,
727 const struct GNUNET_SCHEDULER_TaskContext *tc)
729 struct GNUNET_TIME_Absolute now;
731 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
733 now = GNUNET_TIME_absolute_get ();
734 GNUNET_DISK_directory_scan (networkIdDirectory,
735 &discard_hosts_helper, &now);
737 GNUNET_SCHEDULER_add_delayed (tc->sched,
738 DATA_HOST_CLEAN_FREQ,
739 &cron_clean_data_hosts, NULL);
744 * Handle ADD-message.
747 * @param client identification of the client
748 * @param message the actual message
751 handle_add (void *cls,
752 struct GNUNET_SERVER_Client *client,
753 const struct GNUNET_MessageHeader *message)
755 const struct PeerAddMessage *pam;
756 const struct GNUNET_MessageHeader *hello;
759 size = ntohs (message->size);
761 sizeof (struct PeerAddMessage) + sizeof (struct GNUNET_MessageHeader))
764 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
767 pam = (const struct PeerAddMessage *) message;
768 hello = (const struct GNUNET_MessageHeader *) &pam[1];
769 if (size != sizeof (struct PeerAddMessage) + ntohs (hello->size))
772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
775 bind_address (&pam->peer, (const struct GNUNET_HELLO_Message *) hello);
776 GNUNET_SERVER_receive_done (client, GNUNET_OK);
781 * Handle GET-message.
784 * @param client identification of the client
785 * @param message the actual message
788 handle_get (void *cls,
789 struct GNUNET_SERVER_Client *client,
790 const struct GNUNET_MessageHeader *message)
792 const struct ListPeerMessage *lpm;
794 lpm = (const struct ListPeerMessage *) message;
795 send_to_each_host (&lpm->peer, ntohl (lpm->trust_change), client);
800 * Handle GET-ALL-message.
803 * @param client identification of the client
804 * @param message the actual message
807 handle_get_all (void *cls,
808 struct GNUNET_SERVER_Client *client,
809 const struct GNUNET_MessageHeader *message)
811 const struct ListAllPeersMessage *lpm;
813 lpm = (const struct ListAllPeersMessage *) message;
814 send_to_each_host (NULL, ntohl (lpm->trust_change), client);
819 * Handle NOTIFY-message.
822 * @param client identification of the client
823 * @param message the actual message
826 handle_notify (void *cls,
827 struct GNUNET_SERVER_Client *client,
828 const struct GNUNET_MessageHeader *message)
830 struct NotifyList *nl;
831 struct HostEntry *pos;
833 nl = GNUNET_malloc (sizeof (struct NotifyList));
834 nl->next = notify_list;
836 GNUNET_SERVER_client_keep (client);
848 * List of handlers for the messages understood by this
851 static struct GNUNET_SERVER_MessageHandler handlers[] = {
852 {&handle_add, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_ADD, 0},
853 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
854 sizeof (struct ListPeerMessage)},
855 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
856 sizeof (struct ListAllPeersMessage)},
857 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
858 sizeof (struct GNUNET_MessageHeader)},
864 * Function that is called when a client disconnects.
867 notify_disconnect (void *cls,
868 struct GNUNET_SERVER_Client *client)
870 struct NotifyList *pos;
871 struct NotifyList *prev;
872 struct NotifyList *next;
873 struct PendingEntry *p;
880 if (pos->client == client)
882 while (NULL != (p = pos->pending))
884 pos->pending = p->next;
887 if (pos->transmit_ctx != NULL)
889 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->transmit_ctx);
890 pos->transmit_ctx = NULL;
896 GNUNET_SERVER_client_drop (client);
910 * Process statistics requests.
913 * @param sched scheduler to use
914 * @param server the initialized server
915 * @param cfg configuration to use
919 struct GNUNET_SCHEDULER_Handle *sched,
920 struct GNUNET_SERVER_Handle *server,
921 const struct GNUNET_CONFIGURATION_Handle *cfg)
923 GNUNET_assert (GNUNET_OK ==
924 GNUNET_CONFIGURATION_get_value_filename (cfg,
927 &networkIdDirectory));
928 GNUNET_assert (GNUNET_OK ==
929 GNUNET_CONFIGURATION_get_value_filename (cfg,
933 GNUNET_DISK_directory_create (networkIdDirectory);
934 GNUNET_DISK_directory_create (trustDirectory);
935 GNUNET_SCHEDULER_add_with_priority (sched,
936 GNUNET_SCHEDULER_PRIORITY_IDLE,
937 &cron_scan_directory_data_hosts, NULL);
938 GNUNET_SCHEDULER_add_with_priority (sched,
939 GNUNET_SCHEDULER_PRIORITY_HIGH,
940 &cron_flush_trust, NULL);
941 GNUNET_SCHEDULER_add_with_priority (sched,
942 GNUNET_SCHEDULER_PRIORITY_IDLE,
943 &cron_clean_data_hosts, NULL);
944 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, NULL);
945 GNUNET_SERVER_add_handlers (server, handlers);
950 * The main function for the statistics service.
952 * @param argc number of arguments from the command line
953 * @param argv command line arguments
954 * @return 0 ok, 1 on error
957 main (int argc, char *const *argv)
962 GNUNET_SERVICE_run (argc,
965 GNUNET_SERVICE_OPTION_NONE,
966 &run, NULL)) ? 0 : 1;
967 GNUNET_free_non_null (networkIdDirectory);
968 GNUNET_free_non_null (trustDirectory);
973 /* end of gnunet-service-peerinfo.c */