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 * @param include_friend_only create public of friend-only message
173 * @return generated notification message
175 static struct InfoMessage *
176 make_info_message (const struct HostEntry *he, int include_friend_only)
178 struct InfoMessage *im;
179 struct GNUNET_HELLO_Message *src;
182 if (GNUNET_YES == include_friend_only)
183 src = he->friend_only_hello;
187 hs = (NULL == src) ? 0 : GNUNET_HELLO_size (src);
188 im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
189 im->header.size = htons (hs + sizeof (struct InfoMessage));
190 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
191 im->peer = he->identity;
193 memcpy (&im[1], src, hs);
199 * Address iterator that causes expired entries to be discarded.
201 * @param cls pointer to the current time
202 * @param address the address
203 * @param expiration expiration time for the address
204 * @return GNUNET_NO if expiration smaller than the current time
207 discard_expired (void *cls, const struct GNUNET_HELLO_Address *address,
208 struct GNUNET_TIME_Absolute expiration)
210 const struct GNUNET_TIME_Absolute *now = cls;
212 if (now->abs_value > expiration.abs_value)
214 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
215 _("Removing expired address of transport `%s'\n"),
216 address->transport_name);
224 * Address iterator that counts the remaining addresses.
226 * @param cls pointer to the counter
227 * @param address the address
228 * @param expiration expiration time for the address
229 * @return GNUNET_OK (always)
232 count_addresses (void *cls, const struct GNUNET_HELLO_Address *address,
233 struct GNUNET_TIME_Absolute expiration)
235 unsigned int *cnt = cls;
243 * Get the filename under which we would store the GNUNET_HELLO_Message
244 * for the given host and protocol.
246 * @param id peer for which we need the filename for the HELLO
247 * @return filename of the form DIRECTORY/HOSTID
250 get_host_filename (const struct GNUNET_PeerIdentity *id)
252 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
255 if (NULL == networkIdDirectory)
257 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
258 GNUNET_asprintf (&fn, "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
264 * Broadcast information about the given entry to all
267 * @param entry entry to broadcast about
270 notify_all (struct HostEntry *entry)
272 struct InfoMessage *msg_pub;
273 struct InfoMessage *msg_friend;
274 struct NotificationContext *cur;
276 msg_pub = make_info_message (entry, GNUNET_NO);
277 msg_friend = make_info_message (entry, GNUNET_YES);
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Notifying all clients about peer `%s'\n",
280 GNUNET_i2s(&entry->identity));
281 for (cur = nc_head; NULL != cur; cur = cur->next)
283 if (GNUNET_NO == cur->include_friend_only)
285 GNUNET_SERVER_notification_context_unicast (notify_list,
290 if (GNUNET_YES == cur->include_friend_only)
292 GNUNET_SERVER_notification_context_unicast (notify_list,
298 GNUNET_free (msg_pub);
299 GNUNET_free (msg_friend);
304 * Bind a host address (hello) to a hostId.
306 * @param peer the peer for which this is a hello
307 * @param hello the verified (!) hello message
310 update_hello (const struct GNUNET_PeerIdentity *peer,
311 const struct GNUNET_HELLO_Message *hello);
315 * Try to read the HELLOs in the given filename and discard expired
316 * addresses. Removes the file if one the HELLO is mal-formed. If all
317 * addresses are expired, the HELLO is also removed (but the HELLO
318 * with the public key is still returned if it was found and valid).
320 * The file can contain multiple HELLO messages, but onlu a public and a friend only
321 * HELLO should be included
323 * @param fn name of the file
324 * @param unlink_garbage if GNUNET_YES, try to remove useless files
325 * @param r ReadHostFileContext to store the resutl
328 read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r)
330 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
333 struct GNUNET_TIME_Absolute now;
336 const struct GNUNET_HELLO_Message *hello;
337 struct GNUNET_HELLO_Message *hello_clean;
342 r->friend_only_hello = NULL;
345 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
350 size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from `%s'\n", size_total, fn);
352 if (size_total < sizeof (struct GNUNET_MessageHeader))
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355 _("Failed to parse HELLO in file `%s': %s\n"),
356 fn, "Fail has invalid size");
357 if ( (GNUNET_YES == unlink_garbage) &&
358 (0 != UNLINK (fn)) &&
360 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
365 while (read_pos < size_total)
367 hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
368 size_hello = GNUNET_HELLO_size (hello);
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 _("Failed to parse HELLO in file `%s': %s %u \n"),
373 fn, "HELLO is invalid and has size of ", size_hello);
374 if ((GNUNET_YES == unlink_garbage) &&
375 (0 != UNLINK (fn)) &&
377 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
381 now = GNUNET_TIME_absolute_get ();
382 hello_clean = GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES,
383 &discard_expired, &now);
385 (void) GNUNET_HELLO_iterate_addresses (hello_clean, GNUNET_NO,
386 &count_addresses, &left);
390 GNUNET_free (hello_clean);
394 if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello_clean))
396 if (NULL == r->hello)
397 r->hello = hello_clean;
401 GNUNET_free (r->hello);
402 r->hello = hello_clean;
407 if (NULL == r->friend_only_hello)
408 r->friend_only_hello = hello_clean;
412 GNUNET_free (r->friend_only_hello);
413 r->friend_only_hello = hello_clean;
416 read_pos += size_hello;
421 /* no addresses left, remove from disk */
422 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
423 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Found `%s' and `%s' HELLO message in file\n",
428 (NULL != r->hello) ? "public" : "NO public",
429 (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
434 * Add a host to the list and notify clients about this event
436 * @param identity the identity of the host
437 * @return the HostEntry
439 static struct HostEntry *
440 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
442 struct HostEntry *entry;
443 struct ReadHostFileContext r;
446 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n", GNUNET_i2s (identity));
450 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
452 entry = GNUNET_malloc (sizeof (struct HostEntry));
453 entry->identity = *identity;
454 GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
457 fn = get_host_filename (identity);
460 read_host_file (fn, GNUNET_YES, &r);
462 update_hello (identity, r.hello);
463 if (NULL != r.friend_only_hello)
464 update_hello (identity, r.friend_only_hello);
465 GNUNET_free_non_null (r.hello);
466 GNUNET_free_non_null (r.friend_only_hello);
475 * Remove a file that should not be there. LOG
476 * success or failure.
478 * @param fullname name of the file to remove
481 remove_garbage (const char *fullname)
483 if (0 == UNLINK (fullname))
484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
486 ("File `%s' in directory `%s' does not match naming convention. "
487 "Removed.\n"), fullname, networkIdDirectory);
489 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
495 * Closure for 'hosts_directory_scan_callback'.
497 struct DirScanContext
500 * GNUNET_YES if we should remove files that are broken,
501 * GNUNET_NO if the directory we are iterating over should
502 * be treated as read-only by us.
507 * Counter for the number of (valid) entries found, incremented
508 * by one for each match.
510 unsigned int matched;
515 * Function that is called on each HELLO file in a particular directory.
516 * Try to parse the file and add the HELLO to our list.
518 * @param cls pointer to 'unsigned int' to increment for each file, or NULL
519 * if the file is from a read-only, read-once resource directory
520 * @param fullname name of the file to parse
521 * @return GNUNET_OK (continue iteration)
524 hosts_directory_scan_callback (void *cls, const char *fullname)
526 struct DirScanContext *dsc = cls;
527 struct GNUNET_PeerIdentity identity;
528 struct ReadHostFileContext r;
529 const char *filename;
530 struct GNUNET_PeerIdentity id_public;
531 struct GNUNET_PeerIdentity id_friend;
532 struct GNUNET_PeerIdentity id;
534 if (GNUNET_YES != GNUNET_DISK_file_test (fullname))
535 return GNUNET_OK; /* ignore non-files */
537 filename = strrchr (fullname, DIR_SEPARATOR);
538 if ((NULL == filename) || (1 > strlen (filename)))
543 read_host_file (fullname, dsc->remove_files, &r);
544 if ( (NULL == r.hello) && (NULL == r.friend_only_hello))
546 if (GNUNET_YES == dsc->remove_files)
547 remove_garbage (fullname);
551 if (NULL != r.friend_only_hello)
553 if (GNUNET_OK != GNUNET_HELLO_get_id (r.friend_only_hello, &id_friend))
554 if (GNUNET_YES == dsc->remove_files)
556 remove_garbage (fullname);
563 if (GNUNET_OK != GNUNET_HELLO_get_id (r.hello, &id_public))
564 if (GNUNET_YES == dsc->remove_files)
566 remove_garbage (fullname);
572 if ( (NULL != r.hello) && (NULL != r.friend_only_hello) &&
573 (0 != memcmp (&id_friend, &id_public, sizeof (id_friend))) )
575 /* HELLOs are not for the same peer */
577 if (GNUNET_YES == dsc->remove_files)
578 remove_garbage (fullname);
581 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
583 if (0 != memcmp (&id, &identity, sizeof (id_friend)))
585 /* HELLOs are not for the same peer */
587 if (GNUNET_YES == dsc->remove_files)
588 remove_garbage (fullname);
593 /* ok, found something valid, remember HELLO */
594 add_host_to_known_hosts (&id);
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' public HELLO \n",
599 update_hello (&id, r.hello);
600 GNUNET_free (r.hello);
602 if (NULL != r.friend_only_hello)
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' friend only HELLO \n",
606 update_hello (&id, r.friend_only_hello);
607 GNUNET_free (r.friend_only_hello);
615 * Call this method periodically to scan data/hosts for new hosts.
618 * @param tc scheduler context, aborted if reason is shutdown
621 cron_scan_directory_data_hosts (void *cls,
622 const struct GNUNET_SCHEDULER_TaskContext *tc)
624 static unsigned int retries;
625 struct DirScanContext dsc;
627 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
629 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
631 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
632 GNUNET_SCHEDULER_PRIORITY_IDLE,
633 &cron_scan_directory_data_hosts, NULL);
637 dsc.remove_files = GNUNET_YES;
638 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
639 _("Scanning directory `%s'\n"), networkIdDirectory);
640 GNUNET_DISK_directory_scan (networkIdDirectory,
641 &hosts_directory_scan_callback, &dsc);
642 if ((0 == dsc.matched) && (0 == (++retries & 31)))
643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
644 _("Still no peers found in `%s'!\n"), networkIdDirectory);
645 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
646 GNUNET_SCHEDULER_PRIORITY_IDLE,
647 &cron_scan_directory_data_hosts,
652 static struct GNUNET_HELLO_Message *
653 update_friend_hello (const struct GNUNET_HELLO_Message *hello,
654 const struct GNUNET_HELLO_Message *friend_hello)
656 struct GNUNET_HELLO_Message * res;
657 struct GNUNET_HELLO_Message * tmp;
658 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
660 if (NULL != friend_hello)
662 res = GNUNET_HELLO_merge (hello, friend_hello);
663 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
668 GNUNET_HELLO_get_key (hello, &pk))
673 tmp = GNUNET_HELLO_create (&pk, NULL, NULL, GNUNET_YES);
674 res = GNUNET_HELLO_merge (hello, tmp);
676 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
682 * Bind a host address (hello) to a hostId.
684 * @param peer the peer for which this is a hello
685 * @param hello the verified (!) hello message
688 update_hello (const struct GNUNET_PeerIdentity *peer,
689 const struct GNUNET_HELLO_Message *hello)
692 struct HostEntry *host;
693 struct GNUNET_HELLO_Message *mrg;
694 struct GNUNET_HELLO_Message **dest;
695 struct GNUNET_TIME_Absolute delta;
698 int friend_hello_type;
700 int store_friend_hello;
704 host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
705 GNUNET_assert (NULL != host);
707 friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating %s HELLO for `%s'\n",
709 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
713 if (GNUNET_YES == friend_hello_type)
715 dest = &host->friend_only_hello;
724 (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
725 memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
729 mrg = GNUNET_HELLO_merge ((*dest), hello);
730 delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ());
731 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
733 /* no differences, just ignore the update */
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No change in %s HELLO for `%s'\n",
735 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
740 GNUNET_free ((*dest));
744 if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type))
746 /* Update friend only hello */
747 mrg = update_friend_hello (host->hello, host->friend_only_hello);
748 if (NULL != host->friend_only_hello)
749 GNUNET_free (host->friend_only_hello);
750 host->friend_only_hello = mrg;
753 if (NULL != host->hello)
754 GNUNET_assert ((GNUNET_NO == GNUNET_HELLO_is_friend_only (host->hello)));
755 if (NULL != host->friend_only_hello)
756 GNUNET_assert ((GNUNET_YES == GNUNET_HELLO_is_friend_only(host->friend_only_hello)));
758 store_hello = GNUNET_NO;
759 store_friend_hello = GNUNET_NO;
760 fn = get_host_filename (peer);
762 (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn)) )
764 store_hello = GNUNET_NO;
767 if (NULL != host->hello)
768 (void) GNUNET_HELLO_iterate_addresses (host->hello,
769 GNUNET_NO, &count_addresses, &cnt);
772 store_hello = GNUNET_YES;
773 size += GNUNET_HELLO_size (host->hello);
776 if (NULL != host->friend_only_hello)
777 (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello, GNUNET_NO,
778 &count_addresses, &cnt);
781 store_friend_hello = GNUNET_YES;
782 size += GNUNET_HELLO_size (host->friend_only_hello);
785 if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello))
787 /* no valid addresses, don't put HELLO on disk; in fact,
788 if one exists on disk, remove it */
793 buffer = GNUNET_malloc (size);
796 if (GNUNET_YES == store_hello)
798 memcpy (buffer, host->hello, GNUNET_HELLO_size (host->hello));
799 pos += GNUNET_HELLO_size (host->hello);
801 if (GNUNET_YES == store_friend_hello)
803 memcpy (&buffer[pos], host->friend_only_hello, GNUNET_HELLO_size (host->friend_only_hello));
804 pos += GNUNET_HELLO_size (host->friend_only_hello);
806 GNUNET_assert (pos == size);
808 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
809 GNUNET_DISK_PERM_USER_READ |
810 GNUNET_DISK_PERM_USER_WRITE |
811 GNUNET_DISK_PERM_GROUP_READ |
812 GNUNET_DISK_PERM_OTHER_READ))
813 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stored %s %s HELLO in %s with total size %u\n",
816 (GNUNET_YES == store_friend_hello) ? "friend-only": "",
817 (GNUNET_YES == store_hello) ? "public": "",
819 GNUNET_free (buffer);
822 GNUNET_free_non_null (fn);
828 * Do transmit info about peer to given host.
830 * @param cls NULL to hit all hosts, otherwise specifies a particular target
832 * @param value information to transmit
833 * @return GNUNET_YES (continue to iterate)
836 add_to_tc (void *cls, const struct GNUNET_HashCode * key, void *value)
838 struct TransmitContext *tc = cls;
839 struct HostEntry *pos = value;
840 struct InfoMessage *im;
842 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
845 im = (struct InfoMessage *) buf;
847 if ((pos->hello != NULL) && (GNUNET_NO == tc->friend_only))
849 /* Copy public HELLO */
850 hs = GNUNET_HELLO_size (pos->hello);
851 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
852 sizeof (struct InfoMessage));
853 memcpy (&im[1], pos->hello, hs);
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending public HELLO with size %u for peer `%4s'\n",
855 hs, GNUNET_h2s (key));
857 else if ((pos->friend_only_hello != NULL) && (GNUNET_YES == tc->friend_only))
859 /* Copy friend only HELLO */
860 hs = GNUNET_HELLO_size (pos->friend_only_hello);
861 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
862 sizeof (struct InfoMessage));
863 memcpy (&im[1], pos->friend_only_hello, hs);
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending friend-only HELLO with size %u for peer `%4s'\n",
865 hs, GNUNET_h2s (key));
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding no HELLO for peer `%s'\n",
873 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
874 im->header.size = htons (sizeof (struct InfoMessage) + hs);
875 im->reserved = htonl (0);
876 im->peer = pos->identity;
877 GNUNET_SERVER_transmit_context_append_message (tc->tc, &im->header);
883 * @brief delete expired HELLO entries in directory
885 * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
886 * @param fn filename to test to see if the HELLO expired
887 * @return GNUNET_OK (continue iteration)
890 discard_hosts_helper (void *cls, const char *fn)
892 struct GNUNET_TIME_Absolute *now = cls;
893 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
894 const struct GNUNET_HELLO_Message *hello;
895 struct GNUNET_HELLO_Message *new_hello;
905 read_size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
906 if (read_size < sizeof (struct GNUNET_MessageHeader))
908 if (0 != UNLINK (fn))
909 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
910 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
914 writebuffer = GNUNET_malloc (read_size);
917 while (read_pos < read_size)
919 /* Check each HELLO */
920 hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
921 cur_hello_size = GNUNET_HELLO_size (hello);
923 if (0 == cur_hello_size)
925 /* Invalid data, discard */
926 if (0 != UNLINK (fn))
927 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
928 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
931 new_hello = GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
933 if (NULL != new_hello)
934 (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses, &cnt);
935 if ( (NULL != new_hello) && (0 < cnt) )
937 /* Store new HELLO to write it when done */
938 new_hello_size = GNUNET_HELLO_size(new_hello);
939 memcpy (&writebuffer[write_pos], new_hello, new_hello_size);
940 write_pos += new_hello_size;
942 read_pos += cur_hello_size;
943 GNUNET_free_non_null (new_hello);
948 GNUNET_DISK_fn_write (fn, writebuffer,write_pos,
949 GNUNET_DISK_PERM_USER_READ |
950 GNUNET_DISK_PERM_USER_WRITE |
951 GNUNET_DISK_PERM_GROUP_READ |
952 GNUNET_DISK_PERM_OTHER_READ);
954 else if (0 != UNLINK (fn))
955 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
956 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
958 GNUNET_free (writebuffer);
964 * Call this method periodically to scan peerinfo/ for ancient
968 * @param tc scheduler context, aborted if reason is shutdown
971 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
973 struct GNUNET_TIME_Absolute now;
975 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
977 now = GNUNET_TIME_absolute_get ();
978 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
979 _("Cleaning up directory `%s'\n"), networkIdDirectory);
980 GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
981 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
987 * Handle HELLO-message.
990 * @param client identification of the client
991 * @param message the actual message
994 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
995 const struct GNUNET_MessageHeader *message)
997 const struct GNUNET_HELLO_Message *hello;
998 struct GNUNET_PeerIdentity pid;
1000 hello = (const struct GNUNET_HELLO_Message *) message;
1001 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
1004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1008 "HELLO", GNUNET_i2s (&pid));
1009 add_host_to_known_hosts (&pid);
1010 update_hello (&pid, hello);
1011 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1016 * Handle GET-message.
1018 * @param cls closure
1019 * @param client identification of the client
1020 * @param message the actual message
1023 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
1024 const struct GNUNET_MessageHeader *message)
1026 const struct ListPeerMessage *lpm;
1027 struct TransmitContext tcx;
1029 lpm = (const struct ListPeerMessage *) message;
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1031 "GET", GNUNET_i2s (&lpm->peer));
1032 tcx.friend_only = ntohl (lpm->include_friend_only);
1033 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1034 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
1036 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1037 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1038 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1043 * Handle GET-ALL-message.
1045 * @param cls closure
1046 * @param client identification of the client
1047 * @param message the actual message
1050 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
1051 const struct GNUNET_MessageHeader *message)
1053 const struct ListAllPeersMessage *lapm;
1054 struct TransmitContext tcx;
1056 lapm = (const struct ListAllPeersMessage *) message;
1057 tcx.friend_only = ntohl (lapm->include_friend_only);
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
1059 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1060 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, &tcx);
1061 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1062 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1063 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1069 * Pass the given client the information we have in the respective
1070 * host entry; the client is already in the notification context.
1072 * @param cls the 'struct GNUNET_SERVER_Client' to notify
1073 * @param key key for the value (unused)
1074 * @param value the 'struct HostEntry' to notify the client about
1075 * @return GNUNET_YES (always, continue to iterate)
1078 do_notify_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1080 struct NotificationContext *nc = cls;
1081 struct HostEntry *he = value;
1082 struct InfoMessage *msg;
1084 if ((NULL == he->hello) && (GNUNET_NO == nc->include_friend_only))
1086 /* We have no public hello */
1090 if ( (NULL == he->friend_only_hello) &&
1091 (GNUNET_YES == nc->include_friend_only) )
1093 /* We have no friend hello */
1097 msg = make_info_message (he, nc->include_friend_only);
1098 GNUNET_SERVER_notification_context_unicast (notify_list,
1108 * Handle NOTIFY-message.
1110 * @param cls closure
1111 * @param client identification of the client
1112 * @param message the actual message
1115 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
1116 const struct GNUNET_MessageHeader *message)
1118 struct NotifyMessage *nm = (struct NotifyMessage *) message;
1119 struct NotificationContext *nc;
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "`%s' message received\n",
1124 nc = GNUNET_malloc (sizeof (struct NotificationContext));
1125 nc->client = client;
1126 nc->include_friend_only = ntohl (nm->include_friend_only);
1128 GNUNET_CONTAINER_DLL_insert (nc_head, nc_tail, nc);
1129 GNUNET_SERVER_client_mark_monitor (client);
1130 GNUNET_SERVER_notification_context_add (notify_list, client);
1131 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, nc);
1132 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1137 * Client disconnect callback
1140 * @param client server client
1143 disconnect_cb (void *cls,struct GNUNET_SERVER_Client *client)
1145 struct NotificationContext *cur;
1147 for (cur = nc_head; NULL != cur; cur = cur->next)
1148 if (cur->client == client)
1152 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1158 * Release memory taken by a host entry.
1161 * @param key key of the host entry
1162 * @param value the 'struct HostEntry' to free
1163 * @return GNUNET_YES (continue to iterate)
1166 free_host_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1168 struct HostEntry *he = value;
1170 GNUNET_free_non_null (he->hello);
1171 GNUNET_free_non_null (he->friend_only_hello);
1178 * Clean up our state. Called during shutdown.
1181 * @param tc scheduler task context, unused
1184 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1186 struct NotificationContext *cur;
1187 struct NotificationContext *next;
1188 GNUNET_SERVER_notification_context_destroy (notify_list);
1191 for (cur = nc_head; NULL != cur; cur = next)
1194 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1198 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
1199 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
1202 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1209 * Start up peerinfo service.
1211 * @param cls closure
1212 * @param server the initialized server
1213 * @param cfg configuration to use
1216 run (void *cls, struct GNUNET_SERVER_Handle *server,
1217 const struct GNUNET_CONFIGURATION_Handle *cfg)
1219 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1220 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
1221 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
1222 sizeof (struct ListPeerMessage)},
1223 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
1224 sizeof (struct ListAllPeersMessage)},
1225 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
1226 sizeof (struct NotifyMessage)},
1231 struct DirScanContext dsc;
1234 hostmap = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1235 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
1236 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
1237 noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO");
1238 if (GNUNET_YES != noio)
1240 GNUNET_assert (GNUNET_OK ==
1241 GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
1243 &networkIdDirectory));
1244 GNUNET_DISK_directory_create (networkIdDirectory);
1246 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1247 &cron_scan_directory_data_hosts, NULL);
1249 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1250 &cron_clean_data_hosts, NULL);
1252 ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1253 GNUNET_asprintf (&peerdir,
1258 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1259 _("Importing HELLOs from `%s'\n"),
1262 dsc.remove_files = GNUNET_NO;
1264 GNUNET_DISK_directory_scan (peerdir,
1265 &hosts_directory_scan_callback, &dsc);
1267 GNUNET_free (peerdir);
1269 GNUNET_SERVER_add_handlers (server, handlers);
1270 GNUNET_SERVER_disconnect_notify (server, &disconnect_cb, NULL) ;
1271 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1278 * The main function for the peerinfo service.
1280 * @param argc number of arguments from the command line
1281 * @param argv command line arguments
1282 * @return 0 ok, 1 on error
1285 main (int argc, char *const *argv)
1291 GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
1292 &run, NULL)) ? 0 : 1;
1293 GNUNET_free_non_null (networkIdDirectory);
1298 /* end of gnunet-service-peerinfo.c */