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_MultiPeerMap *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_us > expiration.abs_value_us)
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)
254 if (NULL == networkIdDirectory)
256 GNUNET_asprintf (&fn, "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR,
257 GNUNET_i2s_full (id));
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,
278 "Notifying all clients about peer `%s'\n",
279 GNUNET_i2s(&entry->identity));
280 for (cur = nc_head; NULL != cur; cur = cur->next)
282 if (GNUNET_NO == cur->include_friend_only)
284 GNUNET_SERVER_notification_context_unicast (notify_list,
289 if (GNUNET_YES == cur->include_friend_only)
291 GNUNET_SERVER_notification_context_unicast (notify_list,
297 GNUNET_free (msg_pub);
298 GNUNET_free (msg_friend);
303 * Bind a host address (hello) to a hostId.
305 * @param peer the peer for which this is a hello
306 * @param hello the verified (!) hello message
309 update_hello (const struct GNUNET_PeerIdentity *peer,
310 const struct GNUNET_HELLO_Message *hello);
314 * Try to read the HELLOs in the given filename and discard expired
315 * addresses. Removes the file if one the HELLO is mal-formed. If all
316 * addresses are expired, the HELLO is also removed (but the HELLO
317 * with the public key is still returned if it was found and valid).
319 * The file can contain multiple HELLO messages, but onlu a public and a friend only
320 * HELLO should be included
322 * @param fn name of the file
323 * @param unlink_garbage if #GNUNET_YES, try to remove useless files
324 * @param r ReadHostFileContext to store the resutl
327 read_host_file (const char *fn,
329 struct ReadHostFileContext *r)
331 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
332 unsigned int size_total;
333 struct GNUNET_TIME_Absolute now;
336 const struct GNUNET_HELLO_Message *hello;
337 struct GNUNET_HELLO_Message *hello_clean;
341 r->friend_only_hello = NULL;
344 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
346 size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Read %u bytes from `%s'\n",
351 if (size_total < sizeof (struct GNUNET_MessageHeader))
353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
354 _("Failed to parse HELLO in file `%s': %s\n"),
355 fn, "Fail has invalid size");
356 if ( (GNUNET_YES == unlink_garbage) &&
357 (0 != UNLINK (fn)) &&
359 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
366 while (read_pos < size_total)
368 hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
369 size_hello = GNUNET_HELLO_size (hello);
372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
373 _("Failed to parse HELLO in file `%s'\n"),
375 if ((GNUNET_YES == unlink_garbage) &&
376 (0 != UNLINK (fn)) &&
378 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
384 now = GNUNET_TIME_absolute_get ();
385 hello_clean = GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES,
386 &discard_expired, &now);
387 if (NULL == hello_clean)
389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
390 _("Failed to parse HELLO in file `%s'\n"),
392 if ((GNUNET_YES == unlink_garbage) &&
393 (0 != UNLINK (fn)) &&
395 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
401 (void) GNUNET_HELLO_iterate_addresses (hello_clean, GNUNET_NO,
402 &count_addresses, &left);
406 GNUNET_free (hello_clean);
410 if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello_clean))
412 if (NULL == r->hello)
413 r->hello = hello_clean;
417 GNUNET_free (r->hello);
418 r->hello = hello_clean;
423 if (NULL == r->friend_only_hello)
424 r->friend_only_hello = hello_clean;
428 GNUNET_free (r->friend_only_hello);
429 r->friend_only_hello = hello_clean;
432 read_pos += size_hello;
437 /* no addresses left, remove from disk */
438 if ( (GNUNET_YES == unlink_garbage) &&
440 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Found `%s' and `%s' HELLO message in file\n",
446 (NULL != r->hello) ? "public" : "NO public",
447 (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
452 * Add a host to the list and notify clients about this event
454 * @param identity the identity of the host
455 * @return the HostEntry
457 static struct HostEntry *
458 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
460 struct HostEntry *entry;
461 struct ReadHostFileContext r;
464 entry = GNUNET_CONTAINER_multipeermap_get (hostmap, identity);
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n", GNUNET_i2s (identity));
468 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
470 entry = GNUNET_new (struct HostEntry);
471 entry->identity = *identity;
472 GNUNET_assert (GNUNET_OK ==
473 GNUNET_CONTAINER_multipeermap_put (hostmap, &entry->identity, entry,
474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
476 fn = get_host_filename (identity);
479 read_host_file (fn, GNUNET_YES, &r);
481 update_hello (identity, r.hello);
482 if (NULL != r.friend_only_hello)
483 update_hello (identity, r.friend_only_hello);
484 GNUNET_free_non_null (r.hello);
485 GNUNET_free_non_null (r.friend_only_hello);
494 * Remove a file that should not be there. LOG
495 * success or failure.
497 * @param fullname name of the file to remove
500 remove_garbage (const char *fullname)
502 if (0 == UNLINK (fullname))
503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
505 ("File `%s' in directory `%s' does not match naming convention. "
506 "Removed.\n"), fullname, networkIdDirectory);
508 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
514 * Closure for 'hosts_directory_scan_callback'.
516 struct DirScanContext
519 * GNUNET_YES if we should remove files that are broken,
520 * GNUNET_NO if the directory we are iterating over should
521 * be treated as read-only by us.
526 * Counter for the number of (valid) entries found, incremented
527 * by one for each match.
529 unsigned int matched;
534 * Function that is called on each HELLO file in a particular directory.
535 * Try to parse the file and add the HELLO to our list.
537 * @param cls pointer to 'unsigned int' to increment for each file, or NULL
538 * if the file is from a read-only, read-once resource directory
539 * @param fullname name of the file to parse
540 * @return #GNUNET_OK (continue iteration)
543 hosts_directory_scan_callback (void *cls, const char *fullname)
545 struct DirScanContext *dsc = cls;
546 struct GNUNET_PeerIdentity identity;
547 struct ReadHostFileContext r;
548 const char *filename;
549 struct GNUNET_PeerIdentity id_public;
550 struct GNUNET_PeerIdentity id_friend;
551 struct GNUNET_PeerIdentity id;
553 if (GNUNET_YES != GNUNET_DISK_file_test (fullname))
554 return GNUNET_OK; /* ignore non-files */
556 filename = strrchr (fullname, DIR_SEPARATOR);
557 if ((NULL == filename) || (1 > strlen (filename)))
562 read_host_file (fullname, dsc->remove_files, &r);
563 if ( (NULL == r.hello) && (NULL == r.friend_only_hello))
565 if (NULL != r.friend_only_hello)
567 if (GNUNET_OK != GNUNET_HELLO_get_id (r.friend_only_hello, &id_friend))
568 if (GNUNET_YES == dsc->remove_files)
570 remove_garbage (fullname);
577 if (GNUNET_OK != GNUNET_HELLO_get_id (r.hello, &id_public))
578 if (GNUNET_YES == dsc->remove_files)
580 remove_garbage (fullname);
586 if ( (NULL != r.hello) && (NULL != r.friend_only_hello) &&
587 (0 != memcmp (&id_friend, &id_public, sizeof (id_friend))) )
589 /* HELLOs are not for the same peer */
591 if (GNUNET_YES == dsc->remove_files)
592 remove_garbage (fullname);
595 if (GNUNET_OK == GNUNET_CRYPTO_eddsa_public_key_from_string (filename,
597 &identity.public_key))
599 if (0 != memcmp (&id, &identity, sizeof (id_friend)))
601 /* HELLOs are not for the same peer */
603 if (GNUNET_YES == dsc->remove_files)
604 remove_garbage (fullname);
609 /* ok, found something valid, remember HELLO */
610 add_host_to_known_hosts (&id);
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' public HELLO \n",
615 update_hello (&id, r.hello);
616 GNUNET_free (r.hello);
618 if (NULL != r.friend_only_hello)
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' friend only HELLO \n",
622 update_hello (&id, r.friend_only_hello);
623 GNUNET_free (r.friend_only_hello);
631 * Call this method periodically to scan data/hosts for new hosts.
634 * @param tc scheduler context, aborted if reason is shutdown
637 cron_scan_directory_data_hosts (void *cls,
638 const struct GNUNET_SCHEDULER_TaskContext *tc)
640 static unsigned int retries;
641 struct DirScanContext dsc;
643 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
645 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
647 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
648 GNUNET_SCHEDULER_PRIORITY_IDLE,
649 &cron_scan_directory_data_hosts, NULL);
653 dsc.remove_files = GNUNET_YES;
654 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
655 _("Scanning directory `%s'\n"), networkIdDirectory);
656 GNUNET_DISK_directory_scan (networkIdDirectory,
657 &hosts_directory_scan_callback, &dsc);
658 if ((0 == dsc.matched) && (0 == (++retries & 31)))
659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
660 _("Still no peers found in `%s'!\n"), networkIdDirectory);
661 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
662 GNUNET_SCHEDULER_PRIORITY_IDLE,
663 &cron_scan_directory_data_hosts,
668 static struct GNUNET_HELLO_Message *
669 update_friend_hello (const struct GNUNET_HELLO_Message *hello,
670 const struct GNUNET_HELLO_Message *friend_hello)
672 struct GNUNET_HELLO_Message * res;
673 struct GNUNET_HELLO_Message * tmp;
674 struct GNUNET_CRYPTO_EddsaPublicKey pk;
676 if (NULL != friend_hello)
678 res = GNUNET_HELLO_merge (hello, friend_hello);
679 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
684 GNUNET_HELLO_get_key (hello, &pk))
689 tmp = GNUNET_HELLO_create (&pk, NULL, NULL, GNUNET_YES);
690 res = GNUNET_HELLO_merge (hello, tmp);
692 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
698 * Bind a host address (hello) to a hostId.
700 * @param peer the peer for which this is a hello
701 * @param hello the verified (!) hello message
704 update_hello (const struct GNUNET_PeerIdentity *peer,
705 const struct GNUNET_HELLO_Message *hello)
708 struct HostEntry *host;
709 struct GNUNET_HELLO_Message *mrg;
710 struct GNUNET_HELLO_Message **dest;
711 struct GNUNET_TIME_Absolute delta;
714 int friend_hello_type;
716 int store_friend_hello;
720 host = GNUNET_CONTAINER_multipeermap_get (hostmap, peer);
721 GNUNET_assert (NULL != host);
723 friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating %s HELLO for `%s'\n",
725 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
729 if (GNUNET_YES == friend_hello_type)
731 dest = &host->friend_only_hello;
740 (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
741 memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
745 mrg = GNUNET_HELLO_merge ((*dest), hello);
746 delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ());
747 if (delta.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
749 /* no differences, just ignore the update */
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No change in %s HELLO for `%s'\n",
751 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
756 GNUNET_free ((*dest));
760 if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type))
762 /* Update friend only hello */
763 mrg = update_friend_hello (host->hello, host->friend_only_hello);
764 if (NULL != host->friend_only_hello)
765 GNUNET_free (host->friend_only_hello);
766 host->friend_only_hello = mrg;
769 if (NULL != host->hello)
770 GNUNET_assert ((GNUNET_NO == GNUNET_HELLO_is_friend_only (host->hello)));
771 if (NULL != host->friend_only_hello)
772 GNUNET_assert ((GNUNET_YES == GNUNET_HELLO_is_friend_only(host->friend_only_hello)));
774 fn = get_host_filename (peer);
776 (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn)) )
778 store_hello = GNUNET_NO;
781 if (NULL != host->hello)
782 (void) GNUNET_HELLO_iterate_addresses (host->hello,
783 GNUNET_NO, &count_addresses, &cnt);
786 store_hello = GNUNET_YES;
787 size += GNUNET_HELLO_size (host->hello);
790 if (NULL != host->friend_only_hello)
791 (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello, GNUNET_NO,
792 &count_addresses, &cnt);
793 store_friend_hello = GNUNET_NO;
796 store_friend_hello = GNUNET_YES;
797 size += GNUNET_HELLO_size (host->friend_only_hello);
800 if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello))
802 /* no valid addresses, don't put HELLO on disk; in fact,
803 if one exists on disk, remove it */
808 buffer = GNUNET_malloc (size);
811 if (GNUNET_YES == store_hello)
813 memcpy (buffer, host->hello, GNUNET_HELLO_size (host->hello));
814 pos += GNUNET_HELLO_size (host->hello);
816 if (GNUNET_YES == store_friend_hello)
818 memcpy (&buffer[pos], host->friend_only_hello, GNUNET_HELLO_size (host->friend_only_hello));
819 pos += GNUNET_HELLO_size (host->friend_only_hello);
821 GNUNET_assert (pos == size);
823 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
824 GNUNET_DISK_PERM_USER_READ |
825 GNUNET_DISK_PERM_USER_WRITE |
826 GNUNET_DISK_PERM_GROUP_READ |
827 GNUNET_DISK_PERM_OTHER_READ))
828 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stored %s %s HELLO in %s with total size %u\n",
831 (GNUNET_YES == store_friend_hello) ? "friend-only": "",
832 (GNUNET_YES == store_hello) ? "public": "",
834 GNUNET_free (buffer);
837 GNUNET_free_non_null (fn);
843 * Do transmit info about peer to given host.
845 * @param cls NULL to hit all hosts, otherwise specifies a particular target
847 * @param value information to transmit
848 * @return GNUNET_YES (continue to iterate)
851 add_to_tc (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
853 struct TransmitContext *tc = cls;
854 struct HostEntry *pos = value;
855 struct InfoMessage *im;
857 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
860 im = (struct InfoMessage *) buf;
862 if ((pos->hello != NULL) && (GNUNET_NO == tc->friend_only))
864 /* Copy public HELLO */
865 hs = GNUNET_HELLO_size (pos->hello);
866 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
867 sizeof (struct InfoMessage));
868 memcpy (&im[1], pos->hello, hs);
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Sending public HELLO with size %u for peer `%4s'\n",
871 hs, GNUNET_i2s (key));
873 else if ((pos->friend_only_hello != NULL) && (GNUNET_YES == tc->friend_only))
875 /* Copy friend only HELLO */
876 hs = GNUNET_HELLO_size (pos->friend_only_hello);
877 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
878 sizeof (struct InfoMessage));
879 memcpy (&im[1], pos->friend_only_hello, hs);
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Sending friend-only HELLO with size %u for peer `%4s'\n",
882 hs, GNUNET_i2s (key));
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 "Adding no HELLO for peer `%s'\n",
891 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
892 im->header.size = htons (sizeof (struct InfoMessage) + hs);
893 im->reserved = htonl (0);
894 im->peer = pos->identity;
895 GNUNET_SERVER_transmit_context_append_message (tc->tc, &im->header);
901 * @brief delete expired HELLO entries in directory
903 * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
904 * @param fn filename to test to see if the HELLO expired
905 * @return GNUNET_OK (continue iteration)
908 discard_hosts_helper (void *cls, const char *fn)
910 struct GNUNET_TIME_Absolute *now = cls;
911 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
912 const struct GNUNET_HELLO_Message *hello;
913 struct GNUNET_HELLO_Message *new_hello;
915 unsigned int cur_hello_size;
916 unsigned int new_hello_size;
923 read_size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
924 if (read_size < sizeof (struct GNUNET_MessageHeader))
926 if (0 != UNLINK (fn))
927 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
928 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
932 writebuffer = GNUNET_malloc (read_size);
935 while (read_pos < read_size)
937 /* Check each HELLO */
938 hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
939 cur_hello_size = GNUNET_HELLO_size (hello);
940 if (0 == cur_hello_size)
942 /* Invalid data, discard */
943 if (0 != UNLINK (fn))
944 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
945 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
948 new_hello = GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
950 if (NULL != new_hello)
951 (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses, &cnt);
952 if ( (NULL != new_hello) && (0 < cnt) )
954 /* Store new HELLO to write it when done */
955 new_hello_size = GNUNET_HELLO_size (new_hello);
956 memcpy (&writebuffer[write_pos], new_hello, new_hello_size);
957 write_pos += new_hello_size;
959 read_pos += cur_hello_size;
960 GNUNET_free_non_null (new_hello);
965 GNUNET_DISK_fn_write (fn, writebuffer,write_pos,
966 GNUNET_DISK_PERM_USER_READ |
967 GNUNET_DISK_PERM_USER_WRITE |
968 GNUNET_DISK_PERM_GROUP_READ |
969 GNUNET_DISK_PERM_OTHER_READ);
971 else if (0 != UNLINK (fn))
972 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
973 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
975 GNUNET_free (writebuffer);
981 * Call this method periodically to scan peerinfo/ for ancient
985 * @param tc scheduler context, aborted if reason is shutdown
988 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
990 struct GNUNET_TIME_Absolute now;
992 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
994 now = GNUNET_TIME_absolute_get ();
995 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
996 _("Cleaning up directory `%s'\n"), networkIdDirectory);
997 GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
998 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
1004 * Handle HELLO-message.
1006 * @param cls closure
1007 * @param client identification of the client
1008 * @param message the actual message
1011 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
1012 const struct GNUNET_MessageHeader *message)
1014 const struct GNUNET_HELLO_Message *hello;
1015 struct GNUNET_PeerIdentity pid;
1017 hello = (const struct GNUNET_HELLO_Message *) message;
1018 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
1021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1025 "HELLO", GNUNET_i2s (&pid));
1026 add_host_to_known_hosts (&pid);
1027 update_hello (&pid, hello);
1028 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1033 * Handle GET-message.
1035 * @param cls closure
1036 * @param client identification of the client
1037 * @param message the actual message
1040 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
1041 const struct GNUNET_MessageHeader *message)
1043 const struct ListPeerMessage *lpm;
1044 struct TransmitContext tcx;
1046 lpm = (const struct ListPeerMessage *) message;
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1048 "GET", GNUNET_i2s (&lpm->peer));
1049 tcx.friend_only = ntohl (lpm->include_friend_only);
1050 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1051 GNUNET_CONTAINER_multipeermap_get_multiple (hostmap, &lpm->peer,
1053 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1054 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1055 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1060 * Handle GET-ALL-message.
1062 * @param cls closure
1063 * @param client identification of the client
1064 * @param message the actual message
1067 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
1068 const struct GNUNET_MessageHeader *message)
1070 const struct ListAllPeersMessage *lapm;
1071 struct TransmitContext tcx;
1073 lapm = (const struct ListAllPeersMessage *) message;
1074 tcx.friend_only = ntohl (lapm->include_friend_only);
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
1076 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1077 GNUNET_CONTAINER_multipeermap_iterate (hostmap, &add_to_tc, &tcx);
1078 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1079 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1080 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1086 * Pass the given client the information we have in the respective
1087 * host entry; the client is already in the notification context.
1089 * @param cls the 'struct GNUNET_SERVER_Client' to notify
1090 * @param key key for the value (unused)
1091 * @param value the 'struct HostEntry' to notify the client about
1092 * @return GNUNET_YES (always, continue to iterate)
1095 do_notify_entry (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1097 struct NotificationContext *nc = cls;
1098 struct HostEntry *he = value;
1099 struct InfoMessage *msg;
1101 if ((NULL == he->hello) && (GNUNET_NO == nc->include_friend_only))
1103 /* We have no public hello */
1107 if ( (NULL == he->friend_only_hello) &&
1108 (GNUNET_YES == nc->include_friend_only) )
1110 /* We have no friend hello */
1114 msg = make_info_message (he, nc->include_friend_only);
1115 GNUNET_SERVER_notification_context_unicast (notify_list,
1125 * Handle NOTIFY-message.
1127 * @param cls closure
1128 * @param client identification of the client
1129 * @param message the actual message
1132 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
1133 const struct GNUNET_MessageHeader *message)
1135 struct NotifyMessage *nm = (struct NotifyMessage *) message;
1136 struct NotificationContext *nc;
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 "`%s' message received\n",
1141 nc = GNUNET_new (struct NotificationContext);
1142 nc->client = client;
1143 nc->include_friend_only = ntohl (nm->include_friend_only);
1145 GNUNET_CONTAINER_DLL_insert (nc_head, nc_tail, nc);
1146 GNUNET_SERVER_client_mark_monitor (client);
1147 GNUNET_SERVER_notification_context_add (notify_list, client);
1148 GNUNET_CONTAINER_multipeermap_iterate (hostmap, &do_notify_entry, nc);
1149 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1154 * Client disconnect callback
1157 * @param client server client
1160 disconnect_cb (void *cls,struct GNUNET_SERVER_Client *client)
1162 struct NotificationContext *cur;
1164 for (cur = nc_head; NULL != cur; cur = cur->next)
1165 if (cur->client == client)
1169 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1175 * Release memory taken by a host entry.
1178 * @param key key of the host entry
1179 * @param value the 'struct HostEntry' to free
1180 * @return GNUNET_YES (continue to iterate)
1183 free_host_entry (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1185 struct HostEntry *he = value;
1187 GNUNET_free_non_null (he->hello);
1188 GNUNET_free_non_null (he->friend_only_hello);
1195 * Clean up our state. Called during shutdown.
1198 * @param tc scheduler task context, unused
1201 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1203 struct NotificationContext *cur;
1204 struct NotificationContext *next;
1206 GNUNET_SERVER_notification_context_destroy (notify_list);
1209 for (cur = nc_head; NULL != cur; cur = next)
1212 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1215 GNUNET_CONTAINER_multipeermap_iterate (hostmap, &free_host_entry, NULL);
1216 GNUNET_CONTAINER_multipeermap_destroy (hostmap);
1219 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1226 * Start up peerinfo service.
1228 * @param cls closure
1229 * @param server the initialized server
1230 * @param cfg configuration to use
1233 run (void *cls, struct GNUNET_SERVER_Handle *server,
1234 const struct GNUNET_CONFIGURATION_Handle *cfg)
1236 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1237 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
1238 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
1239 sizeof (struct ListPeerMessage)},
1240 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
1241 sizeof (struct ListAllPeersMessage)},
1242 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
1243 sizeof (struct NotifyMessage)},
1248 struct DirScanContext dsc;
1252 hostmap = GNUNET_CONTAINER_multipeermap_create (1024, GNUNET_YES);
1253 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
1254 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
1255 noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO");
1256 use_included = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "USE_INCLUDED_HELLOS");
1257 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1259 if (GNUNET_YES != noio)
1261 GNUNET_assert (GNUNET_OK ==
1262 GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
1264 &networkIdDirectory));
1266 GNUNET_DISK_directory_create (networkIdDirectory))
1268 GNUNET_SCHEDULER_shutdown ();
1272 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1273 &cron_scan_directory_data_hosts, NULL);
1275 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1276 &cron_clean_data_hosts, NULL);
1277 if (GNUNET_YES == use_included)
1279 ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1280 GNUNET_asprintf (&peerdir,
1285 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1286 _("Importing HELLOs from `%s'\n"),
1289 dsc.remove_files = GNUNET_NO;
1291 GNUNET_DISK_directory_scan (peerdir,
1292 &hosts_directory_scan_callback, &dsc);
1294 GNUNET_free (peerdir);
1297 GNUNET_SERVER_add_handlers (server, handlers);
1298 GNUNET_SERVER_disconnect_notify (server, &disconnect_cb, NULL) ;
1303 * The main function for the peerinfo service.
1305 * @param argc number of arguments from the command line
1306 * @param argv command line arguments
1307 * @return 0 ok, 1 on error
1310 main (int argc, char *const *argv)
1316 GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
1317 &run, NULL)) ? 0 : 1;
1318 GNUNET_free_non_null (networkIdDirectory);
1323 /* end of gnunet-service-peerinfo.c */