2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010, 2012 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 * - notify clients when addresses in HELLO expire (#1933)
35 #include "gnunet_util_lib.h"
36 #include "gnunet_hello_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_statistics_service.h"
42 * How often do we scan the HOST_DIR for new entries?
44 #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * How often do we discard old entries in data/hosts/?
49 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
53 * In-memory cache of known hosts.
59 * Identity of the peer.
61 struct GNUNET_PeerIdentity identity;
64 * Hello for the peer (can be NULL)
66 struct GNUNET_HELLO_Message *hello;
69 * Friend only hello for the peer (can be NULL)
71 struct GNUNET_HELLO_Message *friend_only_hello;
76 * Transmit context for GET requests
78 struct TransmitContext
81 * Server transmit context
83 struct GNUNET_SERVER_TransmitContext *tc;
86 * Include friend only HELLOs GNUNET_YES or _NO
92 * Result of reading a file
94 struct ReadHostFileContext
97 * Hello for the peer (can be NULL)
99 struct GNUNET_HELLO_Message *hello;
102 * Friend only hello for the peer (can be NULL)
104 struct GNUNET_HELLO_Message *friend_only_hello;
109 * Client notification context
111 struct NotificationContext
116 struct NotificationContext *prev;
121 struct NotificationContext *next;
126 struct GNUNET_SERVER_Client *client;
129 * Interested in friend only HELLO?
131 int include_friend_only;
136 * The in-memory list of known hosts, mapping of
137 * host IDs to 'struct HostEntry*' values.
139 static struct GNUNET_CONTAINER_MultiHashMap *hostmap;
142 * Clients to immediately notify about all changes.
144 static struct GNUNET_SERVER_NotificationContext *notify_list;
147 * Directory where the hellos are stored in (peerinfo/)
149 static char *networkIdDirectory;
152 * Handle for reporting statistics.
154 static struct GNUNET_STATISTICS_Handle *stats;
157 * DLL of notification contexts: head
159 static struct NotificationContext *nc_head;
162 * DLL of notification contexts: tail
164 static struct NotificationContext *nc_tail;
168 * Notify all clients in the notify list about the
169 * given host entry changing.
171 * @param he entry of the host for which we generate a notification
172 * @return generated notification message
174 static struct InfoMessage *
175 make_info_message (const struct HostEntry *he, int include_friend_only)
177 struct InfoMessage *im;
178 struct GNUNET_HELLO_Message *src;
181 if (GNUNET_YES == include_friend_only)
182 src = he->friend_only_hello;
186 hs = (NULL == src) ? 0 : GNUNET_HELLO_size (src);
187 im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
188 im->header.size = htons (hs + sizeof (struct InfoMessage));
189 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
190 im->peer = he->identity;
192 memcpy (&im[1], src, hs);
198 * Address iterator that causes expired entries to be discarded.
200 * @param cls pointer to the current time
201 * @param address the address
202 * @param expiration expiration time for the address
203 * @return GNUNET_NO if expiration smaller than the current time
206 discard_expired (void *cls, const struct GNUNET_HELLO_Address *address,
207 struct GNUNET_TIME_Absolute expiration)
209 const struct GNUNET_TIME_Absolute *now = cls;
211 if (now->abs_value > expiration.abs_value)
213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
214 _("Removing expired address of transport `%s'\n"),
215 address->transport_name);
223 * Address iterator that counts the remaining addresses.
225 * @param cls pointer to the counter
226 * @param address the address
227 * @param expiration expiration time for the address
228 * @return GNUNET_OK (always)
231 count_addresses (void *cls, const struct GNUNET_HELLO_Address *address,
232 struct GNUNET_TIME_Absolute expiration)
234 unsigned int *cnt = cls;
242 * Get the filename under which we would store the GNUNET_HELLO_Message
243 * for the given host and protocol.
245 * @param id peer for which we need the filename for the HELLO
246 * @return filename of the form DIRECTORY/HOSTID
249 get_host_filename (const struct GNUNET_PeerIdentity *id)
251 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
254 if (NULL == networkIdDirectory)
256 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
257 GNUNET_asprintf (&fn, "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
263 * Broadcast information about the given entry to all
266 * @param entry entry to broadcast about
269 notify_all (struct HostEntry *entry)
271 struct InfoMessage *msg_pub;
272 struct InfoMessage *msg_friend;
273 struct NotificationContext *cur;
275 msg_pub = make_info_message (entry, GNUNET_NO);
276 msg_friend = make_info_message (entry, GNUNET_YES);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Notifying all clients about peer `%s'\n",
278 GNUNET_i2s(&entry->identity));
279 for (cur = nc_head; NULL != cur; cur = cur->next)
281 if (GNUNET_NO == cur->include_friend_only)
283 GNUNET_SERVER_notification_context_unicast (notify_list,
288 if (GNUNET_YES == cur->include_friend_only)
290 GNUNET_SERVER_notification_context_unicast (notify_list,
296 GNUNET_free (msg_pub);
297 GNUNET_free (msg_friend);
302 * Bind a host address (hello) to a hostId.
304 * @param peer the peer for which this is a hello
305 * @param hello the verified (!) hello message
308 update_hello (const struct GNUNET_PeerIdentity *peer,
309 const struct GNUNET_HELLO_Message *hello);
313 * Try to read the HELLOs in the given filename and discard expired
314 * addresses. Removes the file if one the HELLO is mal-formed. If all
315 * addresses are expired, the HELLO is also removed (but the HELLO
316 * with the public key is still returned if it was found and valid).
318 * The file can contain up to two HELLO messages, a public and a friend only
321 * @param fn name of the file
322 * @param unlink_garbage if GNUNET_YES, try to remove useless files
325 read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r)
327 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
328 const struct GNUNET_HELLO_Message *hello_1st;
329 const struct GNUNET_HELLO_Message *hello_2nd;
330 struct GNUNET_HELLO_Message *hello_clean_1st;
331 struct GNUNET_HELLO_Message *hello_clean_2nd;
335 struct GNUNET_TIME_Absolute now;
340 hello_clean_1st = NULL;
341 hello_clean_2nd = NULL;
345 r->friend_only_hello = NULL;
348 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
353 size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from `%s'\n", size_total, fn);
355 if (size_total < sizeof (struct GNUNET_MessageHeader))
357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
358 _("Failed to parse HELLO in file `%s': %s\n"),
359 fn, "Fail has invalid size");
360 if ( (GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) )
361 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
365 hello_1st = (const struct GNUNET_HELLO_Message *) buffer;
366 size_1st = ntohs (((struct GNUNET_MessageHeader *) &buffer)->size);
367 if (size_1st < sizeof (struct GNUNET_MessageHeader))
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 _("Failed to parse HELLO in file `%s': %s %u \n"),
371 fn, "1st HELLO has invalid size of ", size_1st);
372 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
373 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
376 if (size_1st != GNUNET_HELLO_size (hello_1st))
378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
379 _("Failed to parse HELLO in file `%s': %s \n"),
380 fn, "1st HELLO is invalid");
381 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
382 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
386 if (size_total > size_1st)
388 hello_2nd = (const struct GNUNET_HELLO_Message *) &buffer[size_1st];
389 size_2nd = ntohs (((const struct GNUNET_MessageHeader *) hello_2nd)->size);
390 if ((size_2nd < sizeof (struct GNUNET_MessageHeader)) ||
391 (size_2nd != GNUNET_HELLO_size (hello_2nd)))
393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
394 _("Failed to parse HELLO in file `%s': %s\n"),
395 fn, "2nd HELLO has wrong size");
396 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
397 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
402 if (size_total != (size_1st + size_2nd))
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 _("Failed to parse HELLO in file `%s': %s\n"),
406 fn, "Multiple HELLOs but total size is wrong");
407 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
408 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
412 now = GNUNET_TIME_absolute_get ();
413 hello_clean_1st = GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_YES,
414 &discard_expired, &now);
416 (void) GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_NO,
417 &count_addresses, &left);
420 /* no addresses left, remove from disk */
421 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
422 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
425 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_1st))
427 if (NULL == r->hello)
428 r->hello = hello_clean_1st;
432 GNUNET_free (r->hello);
433 r->hello = hello_clean_1st;
438 if (NULL == r->friend_only_hello)
439 r->friend_only_hello = hello_clean_1st;
443 GNUNET_free (r->friend_only_hello);
444 r->friend_only_hello = hello_clean_1st;
448 if (NULL != hello_2nd)
450 hello_clean_2nd = GNUNET_HELLO_iterate_addresses (hello_2nd, GNUNET_YES,
451 &discard_expired, &now);
453 (void) GNUNET_HELLO_iterate_addresses (hello_clean_2nd, GNUNET_NO,
454 &count_addresses, &left);
457 /* no addresses left, remove from disk */
458 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
459 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
462 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_2nd))
464 if (NULL == r->hello)
465 r->hello = hello_clean_2nd;
469 GNUNET_free (r->hello);
470 r->hello = hello_clean_2nd;
475 if (NULL == r->friend_only_hello)
476 r->friend_only_hello = hello_clean_2nd;
480 GNUNET_free (r->friend_only_hello);
481 r->friend_only_hello = hello_clean_2nd;
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' and `%s' HELLO message in file\n",
487 (NULL != r->hello) ? "public" : "NO public",
488 (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
495 * Add a host to the list and notify clients about this event
497 * @param identity the identity of the host
498 * @return the HostEntry
500 static struct HostEntry *
501 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
503 struct HostEntry *entry;
504 struct ReadHostFileContext r;
507 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n", GNUNET_i2s (identity));
511 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
513 entry = GNUNET_malloc (sizeof (struct HostEntry));
514 entry->identity = *identity;
515 GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
516 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
518 fn = get_host_filename (identity);
521 read_host_file (fn, GNUNET_YES, &r);
523 update_hello (identity, r.hello);
524 if (NULL != r.friend_only_hello)
525 update_hello (identity, r.friend_only_hello);
526 GNUNET_free_non_null (r.hello);
527 GNUNET_free_non_null (r.friend_only_hello);
536 * Remove a file that should not be there. LOG
537 * success or failure.
539 * @param fullname name of the file to remove
542 remove_garbage (const char *fullname)
544 if (0 == UNLINK (fullname))
545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
547 ("File `%s' in directory `%s' does not match naming convention. "
548 "Removed.\n"), fullname, networkIdDirectory);
550 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
556 * Closure for 'hosts_directory_scan_callback'.
558 struct DirScanContext
561 * GNUNET_YES if we should remove files that are broken,
562 * GNUNET_NO if the directory we are iterating over should
563 * be treated as read-only by us.
568 * Counter for the number of (valid) entries found, incremented
569 * by one for each match.
571 unsigned int matched;
576 * Function that is called on each HELLO file in a particular directory.
577 * Try to parse the file and add the HELLO to our list.
579 * @param cls pointer to 'unsigned int' to increment for each file, or NULL
580 * if the file is from a read-only, read-once resource directory
581 * @param fullname name of the file to parse
582 * @return GNUNET_OK (continue iteration)
585 hosts_directory_scan_callback (void *cls, const char *fullname)
587 struct DirScanContext *dsc = cls;
588 struct GNUNET_PeerIdentity identity;
589 struct ReadHostFileContext r;
590 const char *filename;
591 struct GNUNET_PeerIdentity id_public;
592 struct GNUNET_PeerIdentity id_friend;
593 struct GNUNET_PeerIdentity id;
595 if (GNUNET_YES != GNUNET_DISK_file_test (fullname))
596 return GNUNET_OK; /* ignore non-files */
598 filename = strrchr (fullname, DIR_SEPARATOR);
599 if ((NULL == filename) || (1 > strlen (filename)))
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reading %s `%s'\n", fullname, filename);
605 read_host_file (fullname, dsc->remove_files, &r);
607 if ( (NULL == r.hello) && (NULL == r.friend_only_hello))
609 if (GNUNET_YES == dsc->remove_files)
610 remove_garbage (fullname);
614 if (NULL != r.friend_only_hello)
616 if (GNUNET_OK != GNUNET_HELLO_get_id (r.friend_only_hello, &id_friend))
617 if (GNUNET_YES == dsc->remove_files)
619 remove_garbage (fullname);
626 if (GNUNET_OK != GNUNET_HELLO_get_id (r.hello, &id_public))
627 if (GNUNET_YES == dsc->remove_files)
629 remove_garbage (fullname);
635 if ( (NULL != r.hello) && (NULL != r.friend_only_hello) &&
636 (0 != memcmp (&id_friend, &id_public, sizeof (id_friend))) )
638 /* HELLOs are not for the same peer */
640 if (GNUNET_YES == dsc->remove_files)
641 remove_garbage (fullname);
644 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
646 if (0 != memcmp (&id, &identity, sizeof (id_friend)))
648 /* HELLOs are not for the same peer */
650 if (GNUNET_YES == dsc->remove_files)
651 remove_garbage (fullname);
655 /* ok, found something valid, remember HELLO */
656 add_host_to_known_hosts (&id);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' public HELLO \n",
661 update_hello (&id, r.hello);
662 GNUNET_free (r.hello);
664 if (NULL != r.friend_only_hello)
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' friend only HELLO \n",
668 update_hello (&id, r.friend_only_hello);
669 GNUNET_free (r.friend_only_hello);
677 * Call this method periodically to scan data/hosts for new hosts.
680 * @param tc scheduler context, aborted if reason is shutdown
683 cron_scan_directory_data_hosts (void *cls,
684 const struct GNUNET_SCHEDULER_TaskContext *tc)
686 static unsigned int retries;
687 struct DirScanContext dsc;
689 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
691 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
693 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
694 GNUNET_SCHEDULER_PRIORITY_IDLE,
695 &cron_scan_directory_data_hosts, NULL);
699 dsc.remove_files = GNUNET_YES;
700 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
701 _("Scanning directory `%s'\n"), networkIdDirectory);
702 GNUNET_DISK_directory_scan (networkIdDirectory,
703 &hosts_directory_scan_callback, &dsc);
704 if ((0 == dsc.matched) && (0 == (++retries & 31)))
705 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
706 _("Still no peers found in `%s'!\n"), networkIdDirectory);
707 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
708 GNUNET_SCHEDULER_PRIORITY_IDLE,
709 &cron_scan_directory_data_hosts,
714 static struct GNUNET_HELLO_Message *
715 update_friend_hello (const struct GNUNET_HELLO_Message *hello,
716 const struct GNUNET_HELLO_Message *friend_hello)
718 struct GNUNET_HELLO_Message * res;
719 struct GNUNET_HELLO_Message * tmp;
720 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
722 if (NULL != friend_hello)
724 res = GNUNET_HELLO_merge (hello, friend_hello);
725 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
729 GNUNET_HELLO_get_key (hello, &pk);
730 tmp = GNUNET_HELLO_create (&pk, NULL, NULL, GNUNET_YES);
731 res = GNUNET_HELLO_merge (hello, tmp);
733 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
740 * Bind a host address (hello) to a hostId.
742 * @param peer the peer for which this is a hello
743 * @param hello the verified (!) hello message
746 update_hello (const struct GNUNET_PeerIdentity *peer,
747 const struct GNUNET_HELLO_Message *hello)
750 struct HostEntry *host;
751 struct GNUNET_HELLO_Message *mrg;
752 struct GNUNET_HELLO_Message **dest;
753 struct GNUNET_TIME_Absolute delta;
756 int friend_hello_type;
758 int store_friend_hello;
762 host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
763 GNUNET_assert (NULL != host);
765 friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating %s HELLO for `%s'\n",
767 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
771 if (GNUNET_YES == friend_hello_type)
773 dest = &host->friend_only_hello;
782 (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
783 memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
787 mrg = GNUNET_HELLO_merge ((*dest), hello);
788 delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ());
789 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
791 /* no differences, just ignore the update */
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No change in %s HELLO for `%s'\n",
793 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
798 GNUNET_free ((*dest));
802 if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type))
804 /* Update friend only hello */
805 mrg = update_friend_hello (host->hello, host->friend_only_hello);
806 if (NULL != host->friend_only_hello)
807 GNUNET_free (host->friend_only_hello);
808 host->friend_only_hello = mrg;
811 if (NULL != host->hello)
812 GNUNET_assert ((GNUNET_NO == GNUNET_HELLO_is_friend_only (host->hello)));
813 if (NULL != host->friend_only_hello)
814 GNUNET_assert ((GNUNET_YES == GNUNET_HELLO_is_friend_only(host->friend_only_hello)));
816 store_hello = GNUNET_NO;
817 store_friend_hello = GNUNET_NO;
818 fn = get_host_filename (peer);
820 (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn)) )
823 store_hello = GNUNET_NO;
826 if (NULL != host->hello)
827 (void) GNUNET_HELLO_iterate_addresses (host->hello,
828 GNUNET_NO, &count_addresses, &cnt);
831 store_hello = GNUNET_YES;
832 size += GNUNET_HELLO_size (host->hello);
835 if (NULL != host->friend_only_hello)
836 (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello, GNUNET_NO,
837 &count_addresses, &cnt);
840 store_friend_hello = GNUNET_YES;
841 size += GNUNET_HELLO_size (host->friend_only_hello);
844 if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello))
846 /* no valid addresses, don't put HELLO on disk; in fact,
847 if one exists on disk, remove it */
852 buffer = GNUNET_malloc (size);
855 if (GNUNET_YES == store_hello)
857 memcpy (buffer, host->hello, GNUNET_HELLO_size (host->hello));
858 pos += GNUNET_HELLO_size (host->hello);
860 if (GNUNET_YES == store_friend_hello)
862 memcpy (&buffer[pos], host->friend_only_hello, GNUNET_HELLO_size (host->friend_only_hello));
863 pos += GNUNET_HELLO_size (host->friend_only_hello);
865 GNUNET_assert (pos == size);
867 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
868 GNUNET_DISK_PERM_USER_READ |
869 GNUNET_DISK_PERM_USER_WRITE |
870 GNUNET_DISK_PERM_GROUP_READ |
871 GNUNET_DISK_PERM_OTHER_READ))
872 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stored %s %s HELLO in %s with total size %u\n",
875 (GNUNET_YES == store_friend_hello) ? "friend-only": "",
876 (GNUNET_YES == store_hello) ? "public": "",
878 GNUNET_free (buffer);
881 GNUNET_free_non_null (fn);
887 * Do transmit info about peer to given host.
889 * @param cls NULL to hit all hosts, otherwise specifies a particular target
891 * @param value information to transmit
892 * @return GNUNET_YES (continue to iterate)
895 add_to_tc (void *cls, const struct GNUNET_HashCode * key, void *value)
897 struct TransmitContext *tc = cls;
898 struct HostEntry *pos = value;
899 struct InfoMessage *im;
901 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
904 im = (struct InfoMessage *) buf;
906 if ((pos->hello != NULL) && (GNUNET_NO == tc->friend_only))
908 /* Copy public HELLO */
909 hs = GNUNET_HELLO_size (pos->hello);
910 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
911 sizeof (struct InfoMessage));
912 memcpy (&im[1], pos->hello, hs);
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending public HELLO with size %u for peer `%4s'\n",
914 hs, GNUNET_h2s (key));
916 else if ((pos->friend_only_hello != NULL) && (GNUNET_YES == tc->friend_only))
918 /* Copy friend only HELLO */
919 hs = GNUNET_HELLO_size (pos->friend_only_hello);
920 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
921 sizeof (struct InfoMessage));
922 memcpy (&im[1], pos->friend_only_hello, hs);
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending friend-only HELLO with size %u for peer `%4s'\n",
924 hs, GNUNET_h2s (key));
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding no HELLO for peer `%s'\n",
932 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
933 im->header.size = htons (sizeof (struct InfoMessage) + hs);
934 im->reserved = htonl (0);
935 im->peer = pos->identity;
936 GNUNET_SERVER_transmit_context_append_message (tc->tc, &im->header);
942 * @brief delete expired HELLO entries in directory
944 * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
945 * @param fn filename to test to see if the HELLO expired
946 * @return GNUNET_OK (continue iteration)
949 discard_hosts_helper (void *cls, const char *fn)
951 struct GNUNET_TIME_Absolute *now = cls;
952 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
953 const struct GNUNET_HELLO_Message *hello;
954 struct GNUNET_HELLO_Message *new_hello;
958 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
959 if (size < sizeof (struct GNUNET_MessageHeader))
961 if (0 != UNLINK (fn))
962 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
963 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
966 hello = (const struct GNUNET_HELLO_Message *) buffer;
968 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
970 if (NULL != new_hello)
971 (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses, &cnt);
972 if ( (NULL != new_hello) && (0 < cnt) )
974 GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
975 GNUNET_DISK_PERM_USER_READ |
976 GNUNET_DISK_PERM_USER_WRITE |
977 GNUNET_DISK_PERM_GROUP_READ |
978 GNUNET_DISK_PERM_OTHER_READ);
982 if (0 != UNLINK (fn))
983 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
984 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
986 GNUNET_free_non_null (new_hello);
992 * Call this method periodically to scan data/hosts for ancient
996 * @param tc scheduler context, aborted if reason is shutdown
999 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1001 struct GNUNET_TIME_Absolute now;
1003 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1005 now = GNUNET_TIME_absolute_get ();
1006 GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
1007 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
1013 * Handle HELLO-message.
1015 * @param cls closure
1016 * @param client identification of the client
1017 * @param message the actual message
1020 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
1021 const struct GNUNET_MessageHeader *message)
1023 const struct GNUNET_HELLO_Message *hello;
1024 struct GNUNET_PeerIdentity pid;
1026 hello = (const struct GNUNET_HELLO_Message *) message;
1027 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
1030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1034 "HELLO", GNUNET_i2s (&pid));
1035 add_host_to_known_hosts (&pid);
1036 update_hello (&pid, hello);
1037 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1042 * Handle GET-message.
1044 * @param cls closure
1045 * @param client identification of the client
1046 * @param message the actual message
1049 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
1050 const struct GNUNET_MessageHeader *message)
1052 const struct ListPeerMessage *lpm;
1053 struct TransmitContext tcx;
1055 lpm = (const struct ListPeerMessage *) message;
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1057 "GET", GNUNET_i2s (&lpm->peer));
1058 tcx.friend_only = ntohl (lpm->include_friend_only);
1059 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1060 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
1062 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1063 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1064 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1069 * Handle GET-ALL-message.
1071 * @param cls closure
1072 * @param client identification of the client
1073 * @param message the actual message
1076 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
1077 const struct GNUNET_MessageHeader *message)
1079 const struct ListAllPeersMessage *lapm;
1080 struct TransmitContext tcx;
1082 lapm = (const struct ListAllPeersMessage *) message;
1083 tcx.friend_only = ntohl (lapm->include_friend_only);
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
1085 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1086 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, &tcx);
1087 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1088 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1089 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1095 * Pass the given client the information we have in the respective
1096 * host entry; the client is already in the notification context.
1098 * @param cls the 'struct GNUNET_SERVER_Client' to notify
1099 * @param key key for the value (unused)
1100 * @param value the 'struct HostEntry' to notify the client about
1101 * @return GNUNET_YES (always, continue to iterate)
1104 do_notify_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1106 struct NotificationContext *nc = cls;
1107 struct HostEntry *he = value;
1108 struct InfoMessage *msg;
1110 if ((NULL == he->hello) && (GNUNET_NO == nc->include_friend_only))
1112 /* We have no public hello */
1117 if ((NULL == he->friend_only_hello) && GNUNET_YES == nc->include_friend_only)
1119 /* We have no friend hello */
1123 msg = make_info_message (he, nc->include_friend_only);
1124 GNUNET_SERVER_notification_context_unicast (notify_list,
1134 * Handle NOTIFY-message.
1136 * @param cls closure
1137 * @param client identification of the client
1138 * @param message the actual message
1141 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
1142 const struct GNUNET_MessageHeader *message)
1144 struct NotifyMessage *nm = (struct NotifyMessage *) message;
1145 struct NotificationContext *nc;
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
1149 nc = GNUNET_malloc (sizeof (struct NotificationContext));
1150 nc->client = client;
1151 nc->include_friend_only = ntohl (nm->include_friend_only);
1153 GNUNET_CONTAINER_DLL_insert (nc_head, nc_tail, nc);
1154 GNUNET_SERVER_client_mark_monitor (client);
1155 GNUNET_SERVER_notification_context_add (notify_list, client);
1156 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, nc);
1157 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1162 * Client disconnect callback
1165 * @param client server client
1167 static void disconnect_cb (void *cls,struct GNUNET_SERVER_Client *client)
1169 struct NotificationContext *cur;
1171 for (cur = nc_head; NULL != cur; cur = cur->next)
1172 if (cur->client == client)
1178 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1184 * Release memory taken by a host entry.
1187 * @param key key of the host entry
1188 * @param value the 'struct HostEntry' to free
1189 * @return GNUNET_YES (continue to iterate)
1192 free_host_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1194 struct HostEntry *he = value;
1196 GNUNET_free_non_null (he->hello);
1197 GNUNET_free_non_null (he->friend_only_hello);
1204 * Clean up our state. Called during shutdown.
1207 * @param tc scheduler task context, unused
1210 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1212 struct NotificationContext *cur;
1213 struct NotificationContext *next;
1214 GNUNET_SERVER_notification_context_destroy (notify_list);
1217 for (cur = nc_head; NULL != cur; cur = next)
1220 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1224 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
1225 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
1228 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1235 * Start up peerinfo service.
1237 * @param cls closure
1238 * @param server the initialized server
1239 * @param cfg configuration to use
1242 run (void *cls, struct GNUNET_SERVER_Handle *server,
1243 const struct GNUNET_CONFIGURATION_Handle *cfg)
1245 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1246 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
1247 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
1248 sizeof (struct ListPeerMessage)},
1249 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
1250 sizeof (struct ListAllPeersMessage)},
1251 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
1252 sizeof (struct NotifyMessage)},
1257 struct DirScanContext dsc;
1260 hostmap = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1261 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
1262 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
1263 noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO");
1264 if (GNUNET_YES != noio)
1266 GNUNET_assert (GNUNET_OK ==
1267 GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
1269 &networkIdDirectory));
1270 GNUNET_DISK_directory_create (networkIdDirectory);
1272 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1273 &cron_scan_directory_data_hosts, NULL); /* CHECK */
1275 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1276 &cron_clean_data_hosts, NULL); /* CHECK */
1278 ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1279 GNUNET_asprintf (&peerdir,
1284 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1285 _("Importing HELLOs from `%s'\n"),
1288 dsc.remove_files = GNUNET_NO;
1290 GNUNET_DISK_directory_scan (peerdir,
1291 &hosts_directory_scan_callback, &dsc);
1293 GNUNET_free (peerdir);
1295 GNUNET_SERVER_add_handlers (server, handlers);
1296 GNUNET_SERVER_disconnect_notify (server, &disconnect_cb, NULL) ;
1297 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1304 * The main function for the peerinfo service.
1306 * @param argc number of arguments from the command line
1307 * @param argv command line arguments
1308 * @return 0 ok, 1 on error
1311 main (int argc, char *const *argv)
1317 GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
1318 &run, NULL)) ? 0 : 1;
1319 GNUNET_free_non_null (networkIdDirectory);
1324 /* end of gnunet-service-peerinfo.c */