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 3, 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/).
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 discard old entries in data/hosts/?
51 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
54 * In-memory cache of known hosts.
60 * This is a linked list.
62 struct HostEntry *next;
65 * Identity of the peer.
67 struct GNUNET_PeerIdentity identity;
70 * Hello for the peer (can be NULL)
72 struct GNUNET_HELLO_Message *hello;
78 * The in-memory list of known hosts.
80 static struct HostEntry *hosts;
83 * Clients to immediately notify about all changes.
85 static struct GNUNET_SERVER_NotificationContext *notify_list;
88 * Directory where the hellos are stored in (data/hosts)
90 static char *networkIdDirectory;
93 * Handle for reporting statistics.
95 static struct GNUNET_STATISTICS_Handle *stats;
99 * Notify all clients in the notify list about the
100 * given host entry changing.
102 static struct InfoMessage *
103 make_info_message (const struct HostEntry *he)
105 struct InfoMessage *im;
108 hs = (he->hello == NULL) ? 0 : GNUNET_HELLO_size (he->hello);
109 im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
110 im->header.size = htons (hs + sizeof (struct InfoMessage));
111 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
112 im->peer = he->identity;
113 if (he->hello != NULL)
114 memcpy (&im[1], he->hello, hs);
120 * Address iterator that causes expired entries to be discarded.
122 * @param cls pointer to the current time
123 * @param tname name of the transport
124 * @param expiration expiration time for the address
125 * @param addr the address
126 * @param addrlen length of addr in bytes
127 * @return GNUNET_NO if expiration smaller than the current time
130 discard_expired (void *cls,
132 struct GNUNET_TIME_Absolute expiration,
133 const void *addr, uint16_t addrlen)
135 const struct GNUNET_TIME_Absolute *now = cls;
136 if (now->value > expiration.value)
138 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
139 _("Removing expired address of transport `%s'\n"),
148 * Get the filename under which we would store the GNUNET_HELLO_Message
149 * for the given host and protocol.
150 * @return filename of the form DIRECTORY/HOSTID
153 get_host_filename (const struct GNUNET_PeerIdentity *id)
155 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
158 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
159 GNUNET_asprintf (&fn,
160 "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
166 * Find the host entry for the given peer. FIXME: replace by hash map!
167 * @return NULL if not found
169 static struct HostEntry *
170 lookup_host_entry (const struct GNUNET_PeerIdentity *id)
172 struct HostEntry *pos;
175 while ((pos != NULL) &&
177 memcmp (id, &pos->identity, sizeof (struct GNUNET_PeerIdentity))))
184 * Broadcast information about the given entry to all
187 * @param entry entry to broadcast about
190 notify_all (struct HostEntry *entry)
192 struct InfoMessage *msg;
194 msg = make_info_message (entry);
195 GNUNET_SERVER_notification_context_broadcast (notify_list,
203 * Add a host to the list.
205 * @param identity the identity of the host
208 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
210 struct HostEntry *entry;
211 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
212 const struct GNUNET_HELLO_Message *hello;
213 struct GNUNET_HELLO_Message *hello_clean;
215 struct GNUNET_TIME_Absolute now;
218 entry = lookup_host_entry (identity);
221 GNUNET_STATISTICS_update (stats,
222 gettext_noop ("# peers known"),
225 entry = GNUNET_malloc (sizeof (struct HostEntry));
226 entry->identity = *identity;
228 fn = get_host_filename (identity);
229 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
231 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
232 hello = (const struct GNUNET_HELLO_Message *) buffer;
233 if ( (size < sizeof (struct GNUNET_MessageHeader)) ||
234 (size != ntohs((((const struct GNUNET_MessageHeader*) hello)->size))) ||
235 (size != GNUNET_HELLO_size (hello)) )
238 if (0 != UNLINK (fn))
239 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
245 now = GNUNET_TIME_absolute_get ();
246 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
248 &discard_expired, &now);
249 entry->hello = hello_clean;
260 * Remove a file that should not be there. LOG
261 * success or failure.
264 remove_garbage (const char *fullname)
266 if (0 == UNLINK (fullname))
267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
269 ("File `%s' in directory `%s' does not match naming convention. "
270 "Removed.\n"), fullname, networkIdDirectory);
272 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
273 GNUNET_ERROR_TYPE_BULK, "unlink", fullname);
278 hosts_directory_scan_callback (void *cls, const char *fullname)
280 unsigned int *matched = cls;
281 struct GNUNET_PeerIdentity identity;
282 const char *filename;
284 if (GNUNET_DISK_file_test (fullname) != GNUNET_YES)
285 return GNUNET_OK; /* ignore non-files */
286 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
288 remove_garbage (fullname);
292 &fullname[strlen (fullname) -
293 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
294 if (filename[-1] != DIR_SEPARATOR)
296 remove_garbage (fullname);
299 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (filename,
300 &identity.hashPubKey))
302 remove_garbage (fullname);
306 add_host_to_known_hosts (&identity);
312 * Call this method periodically to scan data/hosts for new hosts.
315 cron_scan_directory_data_hosts (void *cls,
316 const struct GNUNET_SCHEDULER_TaskContext *tc)
318 static unsigned int retries;
321 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
324 GNUNET_DISK_directory_create (networkIdDirectory);
325 GNUNET_DISK_directory_scan (networkIdDirectory,
326 &hosts_directory_scan_callback, &count);
327 if ((0 == count) && (0 == (++retries & 31)))
328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
329 GNUNET_ERROR_TYPE_BULK,
330 _("Still no peers found in `%s'!\n"), networkIdDirectory);
331 GNUNET_SCHEDULER_add_delayed (tc->sched,
333 &cron_scan_directory_data_hosts, NULL);
338 * Bind a host address (hello) to a hostId.
340 * @param peer the peer for which this is a hello
341 * @param hello the verified (!) hello message
344 bind_address (const struct GNUNET_PeerIdentity *peer,
345 const struct GNUNET_HELLO_Message *hello)
348 struct HostEntry *host;
349 struct GNUNET_HELLO_Message *mrg;
350 struct GNUNET_TIME_Absolute delta;
352 add_host_to_known_hosts (peer);
353 host = lookup_host_entry (peer);
354 GNUNET_assert (host != NULL);
355 if (host->hello == NULL)
357 host->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
358 memcpy (host->hello, hello, GNUNET_HELLO_size (hello));
362 mrg = GNUNET_HELLO_merge (host->hello, hello);
363 delta = GNUNET_HELLO_equals (mrg,
365 GNUNET_TIME_absolute_get ());
366 if (delta.value == GNUNET_TIME_UNIT_FOREVER_ABS.value)
371 GNUNET_free (host->hello);
374 fn = get_host_filename (peer);
375 GNUNET_DISK_directory_create_for_file (fn);
376 GNUNET_DISK_fn_write (fn,
378 GNUNET_HELLO_size (host->hello),
379 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
380 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
387 * Do transmit info either for only the host matching the given
388 * argument or for all known hosts.
390 * @param only NULL to hit all hosts, otherwise specifies a particular target
391 * @param client who is making the request (and will thus receive our confirmation)
394 send_to_each_host (const struct GNUNET_PeerIdentity *only,
395 struct GNUNET_SERVER_Client *client)
397 struct HostEntry *pos;
398 struct InfoMessage *im;
400 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
401 struct GNUNET_SERVER_TransmitContext *tc;
404 tc = GNUNET_SERVER_transmit_context_create (client);
409 if ((only == NULL) ||
411 memcmp (only, &pos->identity,
412 sizeof (struct GNUNET_PeerIdentity))))
415 im = (struct InfoMessage *) buf;
416 if (pos->hello != NULL)
418 hs = GNUNET_HELLO_size (pos->hello);
420 GNUNET_SERVER_MAX_MESSAGE_SIZE -
421 sizeof (struct InfoMessage));
422 memcpy (&im[1], pos->hello, hs);
425 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
426 im->header.size = htons (sizeof (struct InfoMessage) + hs);
427 im->reserved = htonl (0);
428 im->peer = pos->identity;
429 GNUNET_SERVER_transmit_context_append_message (tc,
434 if ( (only != NULL) &&
435 (match == GNUNET_NO) )
436 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
437 "No `%s' message was found for peer `%4s'\n",
440 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
441 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
442 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
447 * @brief delete expired HELLO entries in data/hosts/
450 discard_hosts_helper (void *cls, const char *fn)
452 struct GNUNET_TIME_Absolute *now = cls;
453 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
454 const struct GNUNET_HELLO_Message *hello;
455 struct GNUNET_HELLO_Message *new_hello;
458 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
459 if (size < sizeof (struct GNUNET_MessageHeader))
461 if (0 != UNLINK (fn))
462 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
463 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
466 hello = (const struct GNUNET_HELLO_Message *) buffer;
467 new_hello = GNUNET_HELLO_iterate_addresses (hello,
469 &discard_expired, now);
470 if (new_hello != NULL)
472 GNUNET_DISK_fn_write (fn,
474 GNUNET_HELLO_size (new_hello),
475 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
476 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
477 GNUNET_free (new_hello);
481 if (0 != UNLINK (fn))
482 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
483 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
490 * Call this method periodically to scan data/hosts for new hosts.
493 cron_clean_data_hosts (void *cls,
494 const struct GNUNET_SCHEDULER_TaskContext *tc)
496 struct GNUNET_TIME_Absolute now;
498 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
500 now = GNUNET_TIME_absolute_get ();
501 GNUNET_DISK_directory_scan (networkIdDirectory,
502 &discard_hosts_helper, &now);
504 GNUNET_SCHEDULER_add_delayed (tc->sched,
505 DATA_HOST_CLEAN_FREQ,
506 &cron_clean_data_hosts, NULL);
511 * Handle HELLO-message.
514 * @param client identification of the client
515 * @param message the actual message
518 handle_hello (void *cls,
519 struct GNUNET_SERVER_Client *client,
520 const struct GNUNET_MessageHeader *message)
522 const struct GNUNET_HELLO_Message *hello;
523 struct GNUNET_PeerIdentity pid;
525 hello = (const struct GNUNET_HELLO_Message *) message;
526 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534 "`%s' message received for peer `%4s'\n",
538 bind_address (&pid, hello);
539 GNUNET_SERVER_receive_done (client, GNUNET_OK);
544 * Handle GET-message.
547 * @param client identification of the client
548 * @param message the actual message
551 handle_get (void *cls,
552 struct GNUNET_SERVER_Client *client,
553 const struct GNUNET_MessageHeader *message)
555 const struct ListPeerMessage *lpm;
557 lpm = (const struct ListPeerMessage *) message;
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "`%s' message received for peer `%4s'\n",
562 GNUNET_i2s (&lpm->peer));
564 send_to_each_host (&lpm->peer, client);
569 * Handle GET-ALL-message.
572 * @param client identification of the client
573 * @param message the actual message
576 handle_get_all (void *cls,
577 struct GNUNET_SERVER_Client *client,
578 const struct GNUNET_MessageHeader *message)
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "`%s' message received\n",
585 send_to_each_host (NULL, client);
590 * Handle NOTIFY-message.
593 * @param client identification of the client
594 * @param message the actual message
597 handle_notify (void *cls,
598 struct GNUNET_SERVER_Client *client,
599 const struct GNUNET_MessageHeader *message)
601 struct InfoMessage *msg;
602 struct HostEntry *pos;
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "`%s' message received\n",
609 GNUNET_SERVER_notification_context_add (notify_list,
614 msg = make_info_message (pos);
615 GNUNET_SERVER_notification_context_unicast (notify_list,
626 * Clean up our state. Called during shutdown.
629 * @param tc scheduler task context, unused
632 shutdown_task (void *cls,
633 const struct GNUNET_SCHEDULER_TaskContext *tc)
635 GNUNET_SERVER_notification_context_destroy (notify_list);
639 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
646 * Process statistics requests.
649 * @param sched scheduler to use
650 * @param server the initialized server
651 * @param cfg configuration to use
655 struct GNUNET_SCHEDULER_Handle *sched,
656 struct GNUNET_SERVER_Handle *server,
657 const struct GNUNET_CONFIGURATION_Handle *cfg)
659 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
660 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
661 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
662 sizeof (struct ListPeerMessage)},
663 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
664 sizeof (struct GNUNET_MessageHeader)},
665 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
666 sizeof (struct GNUNET_MessageHeader)},
669 stats = GNUNET_STATISTICS_create (sched, "peerinfo", cfg);
670 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
671 GNUNET_assert (GNUNET_OK ==
672 GNUNET_CONFIGURATION_get_value_filename (cfg,
675 &networkIdDirectory));
676 GNUNET_DISK_directory_create (networkIdDirectory);
677 GNUNET_SCHEDULER_add_with_priority (sched,
678 GNUNET_SCHEDULER_PRIORITY_IDLE,
679 &cron_scan_directory_data_hosts, NULL);
680 GNUNET_SCHEDULER_add_with_priority (sched,
681 GNUNET_SCHEDULER_PRIORITY_IDLE,
682 &cron_clean_data_hosts, NULL);
683 GNUNET_SCHEDULER_add_delayed (sched,
684 GNUNET_TIME_UNIT_FOREVER_REL,
685 &shutdown_task, 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)
703 GNUNET_SERVICE_run (argc,
706 GNUNET_SERVICE_OPTION_NONE,
707 &run, NULL)) ? 0 : 1;
708 GNUNET_free_non_null (networkIdDirectory);
713 /* end of gnunet-service-peerinfo.c */