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
32 #include "gnunet_crypto_lib.h"
33 #include "gnunet_disk_lib.h"
34 #include "gnunet_hello_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_service_lib.h"
40 * How often do we scan the HOST_DIR for new entries?
42 #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
45 * How often do we flush trust values to disk?
47 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
50 * How often do we discard old entries in data/hosts/?
52 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
55 * In-memory cache of known hosts.
61 * This is a linked list.
63 struct HostEntry *next;
66 * Identity of the peer.
68 struct GNUNET_PeerIdentity identity;
71 * Hello for the peer (can be NULL)
73 struct GNUNET_HELLO_Message *hello;
76 * Trust rating for this peer
81 * Trust rating for this peer on disk.
88 * The in-memory list of known hosts.
90 static struct HostEntry *hosts;
93 * Directory where the hellos are stored in (data/hosts)
95 static char *networkIdDirectory;
98 * Where do we store trust information?
100 static char *trustDirectory;
104 * Address iterator that causes expired entries to be discarded.
106 * @param cls pointer to the current time
107 * @return GNUNET_NO if expiration smaller than the current time
110 discard_expired (void *cls,
112 struct GNUNET_TIME_Absolute expiration,
113 const void *addr, size_t addrlen)
115 const struct GNUNET_TIME_Absolute *now = cls;
116 if (now->value > expiration.value)
123 * Get the filename under which we would store the GNUNET_HELLO_Message
124 * for the given host and protocol.
125 * @return filename of the form DIRECTORY/HOSTID
128 get_host_filename (const struct GNUNET_PeerIdentity *id)
130 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
133 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
134 GNUNET_asprintf (&fn,
135 "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
141 * Get the filename under which we would store the GNUNET_HELLO_Message
142 * for the given host and protocol.
143 * @return filename of the form DIRECTORY/HOSTID
146 get_trust_filename (const struct GNUNET_PeerIdentity *id)
148 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
151 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
152 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
157 * Find the host entry for the given peer. Call
158 * only when synchronized!
159 * @return NULL if not found
161 static struct HostEntry *
162 lookup_host_entry (const struct GNUNET_PeerIdentity *id)
164 struct HostEntry *pos;
167 while ((pos != NULL) &&
169 memcmp (id, &pos->identity, sizeof (struct GNUNET_PeerIdentity))))
176 * Add a host to the list.
178 * @param identity the identity of the host
179 * @param protocol the protocol for the host
182 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
184 struct HostEntry *entry;
187 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
188 const struct GNUNET_HELLO_Message *hello;
189 struct GNUNET_HELLO_Message *hello_clean;
191 struct GNUNET_TIME_Absolute now;
193 entry = lookup_host_entry (identity);
196 entry = GNUNET_malloc (sizeof (struct HostEntry));
197 entry->identity = *identity;
198 fn = get_trust_filename (identity);
199 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
200 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
201 entry->disk_trust = entry->trust = ntohl (trust);
204 fn = get_host_filename (identity);
205 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
207 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
208 hello = (const struct GNUNET_HELLO_Message *) buffer;
209 now = GNUNET_TIME_absolute_get ();
210 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
212 &discard_expired, &now);
213 entry->hello = hello_clean;
222 * Increase the host credit by a value.
224 * @param hostId is the identity of the host
225 * @param value is the int value by which the
226 * host credit is to be increased or decreased
227 * @returns the actual change in trust (positive or negative)
230 change_host_trust (const struct GNUNET_PeerIdentity *hostId, int value)
232 struct HostEntry *host;
236 host = lookup_host_entry (hostId);
239 add_host_to_known_hosts (hostId);
240 host = lookup_host_entry (hostId);
242 GNUNET_assert (host != NULL);
245 if (host->trust + value < host->trust)
247 value = ((uint32_t) - 1) - host->trust;
248 host->trust = (uint32_t) - 1; /* maximized */
251 host->trust += value;
255 if (host->trust < -value)
257 value = -host->trust;
261 host->trust += value;
268 * Remove a file that should not be there. LOG
269 * success or failure.
272 remove_garbage (const char *fullname)
274 if (0 == UNLINK (fullname))
275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
277 ("File `%s' in directory `%s' does not match naming convention. "
278 "Removed.\n"), fullname, networkIdDirectory);
280 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
281 GNUNET_ERROR_TYPE_BULK, "unlink", fullname);
286 hosts_directory_scan_callback (void *cls, const char *fullname)
288 unsigned int *matched = cls;
289 struct GNUNET_PeerIdentity identity;
290 const char *filename;
292 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
293 return GNUNET_OK; /* ignore non-files */
294 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
296 remove_garbage (fullname);
300 &fullname[strlen (fullname) -
301 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
302 if (filename[-1] != DIR_SEPARATOR)
304 remove_garbage (fullname);
307 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (filename,
308 &identity.hashPubKey))
310 remove_garbage (fullname);
314 add_host_to_known_hosts (&identity);
320 * Call this method periodically to scan data/hosts for new hosts.
323 cron_scan_directory_data_hosts (void *cls,
324 const struct GNUNET_SCHEDULER_TaskContext *tc)
326 static unsigned int retries;
330 GNUNET_DISK_directory_scan (networkIdDirectory,
331 &hosts_directory_scan_callback, &count);
332 if ((0 == count) && (0 == (++retries & 31)))
333 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
334 GNUNET_ERROR_TYPE_BULK,
335 _("Still no peers found in `%s'!\n"), networkIdDirectory);
336 GNUNET_SCHEDULER_add_delayed (tc->sched,
338 GNUNET_SCHEDULER_PRIORITY_KEEP,
339 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
341 &cron_scan_directory_data_hosts, NULL);
346 * Bind a host address (hello) to a hostId.
348 * @param peer the peer for which this is a hello
349 * @param hello the verified (!) hello message
352 bind_address (const struct GNUNET_PeerIdentity *peer,
353 const struct GNUNET_HELLO_Message *hello)
356 struct HostEntry *host;
357 struct GNUNET_HELLO_Message *mrg;
359 add_host_to_known_hosts (peer);
360 host = lookup_host_entry (peer);
361 GNUNET_assert (host != NULL);
362 if (host->hello == NULL)
364 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
365 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
369 mrg = GNUNET_HELLO_merge (host->hello, hello);
370 GNUNET_free (host->hello);
373 fn = get_host_filename (peer);
374 GNUNET_DISK_fn_write (fn,
376 GNUNET_HELLO_size (host->hello),
377 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
378 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
384 * Do transmit info either for only the host matching the given
385 * argument or for all known hosts and change their trust values by
388 * @param only NULL to hit all hosts
391 send_to_each_host (const struct GNUNET_PeerIdentity *only,
392 int trust_change, struct GNUNET_SERVER_Client *client)
394 struct HostEntry *pos;
395 struct InfoMessage *im;
396 const struct GNUNET_MessageHeader *end;
398 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
399 struct GNUNET_SERVER_TransmitContext *tc;
401 tc = GNUNET_SERVER_transmit_context_create (client);
405 if ((only == NULL) ||
407 memcmp (only, &pos->identity,
408 sizeof (struct GNUNET_PeerIdentity))))
410 change_host_trust (&pos->identity, trust_change);
412 im = (struct InfoMessage *) buf;
413 if (pos->hello != NULL)
415 hs = GNUNET_HELLO_size (pos->hello);
417 GNUNET_SERVER_MAX_MESSAGE_SIZE -
418 sizeof (struct InfoMessage));
419 memcpy (&im[1], pos->hello, hs);
421 im->trust = htonl (pos->trust);
422 im->peer = pos->identity;
424 GNUNET_SERVER_transmit_context_append (tc,
427 sizeof (struct InfoMessage) -
429 GNUNET_MessageHeader),
430 GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
434 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
435 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
436 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
441 * Write host-trust information to a file - flush the buffer entry!
442 * Assumes synchronized access.
445 flush_trust (struct HostEntry *host)
450 if (host->trust == host->disk_trust)
451 return; /* unchanged */
452 fn = get_trust_filename (&host->identity);
453 if (host->trust == 0)
455 if ((0 != UNLINK (fn)) && (errno != ENOENT))
456 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
457 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
461 trust = htonl (host->trust);
462 if (GNUNET_OK == GNUNET_DISK_fn_write (fn, &trust, sizeof(uint32_t),
463 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
464 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
465 host->disk_trust = host->trust;
471 * Call this method periodically to scan data/hosts for new hosts.
474 cron_flush_trust (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
476 struct HostEntry *pos;
484 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
485 GNUNET_SCHEDULER_add_delayed (tc->sched,
487 GNUNET_SCHEDULER_PRIORITY_KEEP,
488 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
489 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
494 * @brief delete expired HELLO entries in data/hosts/
497 discard_hosts_helper (void *cls, const char *fn)
499 struct GNUNET_TIME_Absolute *now = cls;
500 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
501 const struct GNUNET_HELLO_Message *hello;
502 struct GNUNET_HELLO_Message *new_hello;
505 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
506 if ((size < sizeof (struct GNUNET_MessageHeader)) && (0 != UNLINK (fn)))
508 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
509 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
512 hello = (const struct GNUNET_HELLO_Message *) buffer;
513 new_hello = GNUNET_HELLO_iterate_addresses (hello,
515 &discard_expired, now);
516 if ((new_hello == NULL) && (0 != UNLINK (fn)))
517 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
518 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
519 if (new_hello != NULL)
521 GNUNET_DISK_fn_write (fn,
523 GNUNET_HELLO_size (new_hello),
524 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
525 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
526 GNUNET_free (new_hello);
533 * Call this method periodically to scan data/hosts for new hosts.
536 cron_clean_data_hosts (void *cls,
537 const struct GNUNET_SCHEDULER_TaskContext *tc)
539 struct GNUNET_TIME_Absolute now;
541 now = GNUNET_TIME_absolute_get ();
542 GNUNET_DISK_directory_scan (networkIdDirectory,
543 &discard_hosts_helper, &now);
545 GNUNET_SCHEDULER_add_delayed (tc->sched,
547 GNUNET_SCHEDULER_PRIORITY_KEEP,
548 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
549 DATA_HOST_CLEAN_FREQ,
550 &cron_clean_data_hosts, NULL);
555 * Handle ADD-message.
558 * @param client identification of the client
559 * @param message the actual message
562 handle_add (void *cls,
563 struct GNUNET_SERVER_Client *client,
564 const struct GNUNET_MessageHeader *message)
566 const struct PeerAddMessage *pam;
567 const struct GNUNET_MessageHeader *hello;
570 size = ntohs (message->size);
572 sizeof (struct PeerAddMessage) + sizeof (struct GNUNET_MessageHeader))
575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
578 pam = (const struct PeerAddMessage *) message;
579 hello = (const struct GNUNET_MessageHeader *) &pam[1];
580 if (size != sizeof (struct PeerAddMessage) + ntohs (hello->size))
583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
586 bind_address (&pam->peer, (const struct GNUNET_HELLO_Message *) hello);
587 GNUNET_SERVER_receive_done (client, GNUNET_OK);
592 * Handle GET-message.
595 * @param client identification of the client
596 * @param message the actual message
599 handle_get (void *cls,
600 struct GNUNET_SERVER_Client *client,
601 const struct GNUNET_MessageHeader *message)
603 const struct ListPeerMessage *lpm;
605 lpm = (const struct ListPeerMessage *) message;
606 send_to_each_host (&lpm->peer, ntohl (lpm->trust_change), client);
611 * Handle GET-ALL-message.
614 * @param client identification of the client
615 * @param message the actual message
618 handle_get_all (void *cls,
619 struct GNUNET_SERVER_Client *client,
620 const struct GNUNET_MessageHeader *message)
622 const struct ListAllPeersMessage *lpm;
624 lpm = (const struct ListAllPeersMessage *) message;
625 send_to_each_host (NULL, ntohl (lpm->trust_change), client);
630 * List of handlers for the messages understood by this
633 static struct GNUNET_SERVER_MessageHandler handlers[] = {
634 {&handle_add, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_ADD, 0},
635 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
636 sizeof (struct ListPeerMessage)},
637 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
638 sizeof (struct ListAllPeersMessage)},
644 * Process statistics requests.
647 * @param sched scheduler to use
648 * @param server the initialized server
649 * @param cfg configuration to use
653 struct GNUNET_SCHEDULER_Handle *sched,
654 struct GNUNET_SERVER_Handle *server,
655 struct GNUNET_CONFIGURATION_Handle *cfg)
657 GNUNET_assert (GNUNET_OK ==
658 GNUNET_CONFIGURATION_get_value_filename (cfg,
661 &networkIdDirectory));
662 GNUNET_assert (GNUNET_OK ==
663 GNUNET_CONFIGURATION_get_value_filename (cfg,
667 GNUNET_DISK_directory_create (networkIdDirectory);
668 GNUNET_DISK_directory_create (trustDirectory);
669 GNUNET_SCHEDULER_add_delayed (sched,
671 GNUNET_SCHEDULER_PRIORITY_IDLE,
672 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
673 GNUNET_TIME_UNIT_MILLISECONDS,
674 &cron_scan_directory_data_hosts, NULL);
675 GNUNET_SCHEDULER_add_delayed (sched,
677 GNUNET_SCHEDULER_PRIORITY_HIGH,
678 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
679 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
680 GNUNET_SCHEDULER_add_delayed (sched,
682 GNUNET_SCHEDULER_PRIORITY_IDLE,
683 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
684 DATA_HOST_CLEAN_FREQ,
685 &cron_clean_data_hosts, NULL);
686 GNUNET_SERVER_add_handlers (server, handlers);
691 * The main function for the statistics service.
693 * @param argc number of arguments from the command line
694 * @param argv command line arguments
695 * @return 0 ok, 1 on error
698 main (int argc, char *const *argv)
701 GNUNET_SERVICE_run (argc,
703 "peerinfo", &run, NULL, NULL, NULL)) ? 0 : 1;
707 /* end of gnunet-service-peerinfo.c */