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);
278 for (cur = nc_head; NULL != cur; cur = cur->next)
280 if (GNUNET_NO == cur->include_friend_only)
282 GNUNET_SERVER_notification_context_unicast (notify_list,
287 if (GNUNET_YES == cur->include_friend_only)
289 GNUNET_SERVER_notification_context_unicast (notify_list,
295 GNUNET_free (msg_pub);
296 GNUNET_free (msg_friend);
301 * Bind a host address (hello) to a hostId.
303 * @param peer the peer for which this is a hello
304 * @param hello the verified (!) hello message
307 update_hello (const struct GNUNET_PeerIdentity *peer,
308 const struct GNUNET_HELLO_Message *hello);
312 * Try to read the HELLOs in the given filename and discard expired
313 * addresses. Removes the file if one the HELLO is mal-formed. If all
314 * addresses are expired, the HELLO is also removed (but the HELLO
315 * with the public key is still returned if it was found and valid).
317 * The file can contain up to two HELLO messages, a public and a friend only
320 * @param fn name of the file
321 * @param unlink_garbage if GNUNET_YES, try to remove useless files
324 read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r)
326 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
327 const struct GNUNET_HELLO_Message *hello_1st;
328 const struct GNUNET_HELLO_Message *hello_2nd;
329 struct GNUNET_HELLO_Message *hello_clean_1st;
330 struct GNUNET_HELLO_Message *hello_clean_2nd;
334 struct GNUNET_TIME_Absolute now;
339 hello_clean_1st = NULL;
340 hello_clean_2nd = NULL;
344 r->friend_only_hello = NULL;
347 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
352 size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from `%s'\n", size_total, fn);
354 if (size_total < sizeof (struct GNUNET_MessageHeader))
356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
357 _("Failed to parse HELLO in file `%s': %s\n"),
358 fn, "Fail has invalid size");
359 if ( (GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) )
360 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
364 hello_1st = (const struct GNUNET_HELLO_Message *) buffer;
365 size_1st = ntohs (((struct GNUNET_MessageHeader *) &buffer)->size);
366 if (size_1st < sizeof (struct GNUNET_MessageHeader))
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 _("Failed to parse HELLO in file `%s': %s %u \n"),
370 fn, "1st HELLO has invalid size of ", size_1st);
371 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
372 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
375 if (size_1st != GNUNET_HELLO_size (hello_1st))
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378 _("Failed to parse HELLO in file `%s': %s \n"),
379 fn, "1st HELLO is invalid");
380 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
381 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
385 if (size_total > size_1st)
387 hello_2nd = (const struct GNUNET_HELLO_Message *) &buffer[size_1st];
388 size_2nd = ntohs (((const struct GNUNET_MessageHeader *) hello_2nd)->size);
389 if ((size_2nd < sizeof (struct GNUNET_MessageHeader)) ||
390 (size_2nd != GNUNET_HELLO_size (hello_2nd)))
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 _("Failed to parse HELLO in file `%s': %s\n"),
394 fn, "2nd HELLO has wrong size");
395 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
396 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
401 if (size_total != (size_1st + size_2nd))
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404 _("Failed to parse HELLO in file `%s': %s\n"),
405 fn, "Multiple HELLOs but total size is wrong");
406 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
407 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
411 now = GNUNET_TIME_absolute_get ();
412 hello_clean_1st = GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_YES,
413 &discard_expired, &now);
415 (void) GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_NO,
416 &count_addresses, &left);
419 /* no addresses left, remove from disk */
420 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
421 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
424 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_1st))
426 if (NULL == r->hello)
427 r->hello = hello_clean_1st;
431 GNUNET_free (r->hello);
432 r->hello = hello_clean_1st;
437 if (NULL == r->friend_only_hello)
438 r->friend_only_hello = hello_clean_1st;
442 GNUNET_free (r->friend_only_hello);
443 r->friend_only_hello = hello_clean_1st;
447 if (NULL != hello_2nd)
449 hello_clean_2nd = GNUNET_HELLO_iterate_addresses (hello_2nd, GNUNET_YES,
450 &discard_expired, &now);
452 (void) GNUNET_HELLO_iterate_addresses (hello_clean_2nd, GNUNET_NO,
453 &count_addresses, &left);
456 /* no addresses left, remove from disk */
457 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
458 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
461 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_2nd))
463 if (NULL == r->hello)
464 r->hello = hello_clean_2nd;
468 GNUNET_free (r->hello);
469 r->hello = hello_clean_2nd;
474 if (NULL == r->friend_only_hello)
475 r->friend_only_hello = hello_clean_2nd;
479 GNUNET_free (r->friend_only_hello);
480 r->friend_only_hello = hello_clean_2nd;
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' and `%s' HELLO message in file\n",
486 (NULL != r->hello) ? "public" : "NO public",
487 (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
494 * Add a host to the list and notify clients about this event
496 * @param identity the identity of the host
497 * @return the HostEntry
499 static struct HostEntry *
500 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
502 struct HostEntry *entry;
503 struct ReadHostFileContext r;
506 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n", GNUNET_i2s (identity));
510 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
512 entry = GNUNET_malloc (sizeof (struct HostEntry));
513 entry->identity = *identity;
514 GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
515 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
517 fn = get_host_filename (identity);
520 read_host_file (fn, GNUNET_YES, &r);
522 update_hello (identity, r.hello);
523 if (NULL != r.friend_only_hello)
524 update_hello (identity, r.friend_only_hello);
525 GNUNET_free_non_null (r.hello);
526 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_DISK_directory_scan (networkIdDirectory,
701 &hosts_directory_scan_callback, &dsc);
702 if ((0 == dsc.matched) && (0 == (++retries & 31)))
703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
704 _("Still no peers found in `%s'!\n"), networkIdDirectory);
705 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
706 GNUNET_SCHEDULER_PRIORITY_IDLE,
707 &cron_scan_directory_data_hosts,
712 static struct GNUNET_HELLO_Message *
713 update_friend_hello (const struct GNUNET_HELLO_Message *hello,
714 const struct GNUNET_HELLO_Message *friend_hello)
716 struct GNUNET_HELLO_Message * res;
717 struct GNUNET_HELLO_Message * tmp;
718 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
720 if (NULL != friend_hello)
722 res = GNUNET_HELLO_merge (hello, friend_hello);
723 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
727 GNUNET_HELLO_get_key (hello, &pk);
728 tmp = GNUNET_HELLO_create (&pk, NULL, NULL, GNUNET_YES);
729 res = GNUNET_HELLO_merge (hello, tmp);
731 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
738 * Bind a host address (hello) to a hostId.
740 * @param peer the peer for which this is a hello
741 * @param hello the verified (!) hello message
744 update_hello (const struct GNUNET_PeerIdentity *peer,
745 const struct GNUNET_HELLO_Message *hello)
748 struct HostEntry *host;
749 struct GNUNET_HELLO_Message *mrg;
750 struct GNUNET_HELLO_Message **dest;
751 struct GNUNET_TIME_Absolute delta;
754 int friend_hello_type;
756 int store_friend_hello;
760 host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
761 GNUNET_assert (NULL != host);
763 friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating %s HELLO for `%s'\n",
765 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
769 if (GNUNET_YES == friend_hello_type)
771 dest = &host->friend_only_hello;
780 (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
781 memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
785 mrg = GNUNET_HELLO_merge ((*dest), hello);
786 delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ());
787 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
789 /* no differences, just ignore the update */
793 GNUNET_free ((*dest));
797 if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type))
799 /* Update friend only hello */
800 mrg = update_friend_hello (host->hello, host->friend_only_hello);
801 if (NULL != host->friend_only_hello)
802 GNUNET_free (host->friend_only_hello);
803 host->friend_only_hello = mrg;
806 if (NULL != host->hello)
807 GNUNET_assert ((GNUNET_NO == GNUNET_HELLO_is_friend_only (host->hello)));
808 if (NULL != host->friend_only_hello)
809 GNUNET_assert ((GNUNET_YES == GNUNET_HELLO_is_friend_only(host->friend_only_hello)));
811 store_hello = GNUNET_NO;
812 store_friend_hello = GNUNET_NO;
813 fn = get_host_filename (peer);
815 (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn)) )
818 store_hello = GNUNET_NO;
821 if (NULL != host->hello)
822 (void) GNUNET_HELLO_iterate_addresses (host->hello,
823 GNUNET_NO, &count_addresses, &cnt);
826 store_hello = GNUNET_YES;
827 size += GNUNET_HELLO_size (host->hello);
830 if (NULL != host->friend_only_hello)
831 (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello, GNUNET_NO,
832 &count_addresses, &cnt);
835 store_friend_hello = GNUNET_YES;
836 size += GNUNET_HELLO_size (host->friend_only_hello);
839 if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello))
841 /* no valid addresses, don't put HELLO on disk; in fact,
842 if one exists on disk, remove it */
847 buffer = GNUNET_malloc (size);
850 if (GNUNET_YES == store_hello)
852 memcpy (buffer, host->hello, GNUNET_HELLO_size (host->hello));
853 pos += GNUNET_HELLO_size (host->hello);
855 if (GNUNET_YES == store_friend_hello)
857 memcpy (&buffer[pos], host->friend_only_hello, GNUNET_HELLO_size (host->friend_only_hello));
858 pos += GNUNET_HELLO_size (host->friend_only_hello);
860 GNUNET_assert (pos == size);
862 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
863 GNUNET_DISK_PERM_USER_READ |
864 GNUNET_DISK_PERM_USER_WRITE |
865 GNUNET_DISK_PERM_GROUP_READ |
866 GNUNET_DISK_PERM_OTHER_READ))
867 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stored %s %s HELLO in %s with total size %u\n",
870 (GNUNET_YES == store_friend_hello) ? "friend-only": "",
871 (GNUNET_YES == store_hello) ? "public": "",
873 GNUNET_free (buffer);
876 GNUNET_free_non_null (fn);
882 * Do transmit info about peer to given host.
884 * @param cls NULL to hit all hosts, otherwise specifies a particular target
886 * @param value information to transmit
887 * @return GNUNET_YES (continue to iterate)
890 add_to_tc (void *cls, const struct GNUNET_HashCode * key, void *value)
892 struct TransmitContext *tc = cls;
893 struct HostEntry *pos = value;
894 struct InfoMessage *im;
896 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
899 im = (struct InfoMessage *) buf;
901 if ((pos->hello != NULL) && (GNUNET_NO == tc->friend_only))
903 /* Copy public HELLO */
904 hs = GNUNET_HELLO_size (pos->hello);
905 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
906 sizeof (struct InfoMessage));
907 memcpy (&im[1], pos->hello, hs);
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding public HELLO with size %u for peer `%s'\n",
909 hs, GNUNET_h2s (key));
911 else if ((pos->friend_only_hello != NULL) && (GNUNET_YES == tc->friend_only))
913 /* Copy friend only HELLO */
914 hs = GNUNET_HELLO_size (pos->friend_only_hello);
915 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
916 sizeof (struct InfoMessage));
917 memcpy (&im[1], pos->friend_only_hello, hs);
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding public HELLO with size %u for peer `%s'\n",
919 hs, GNUNET_h2s (key));
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding no HELLO for peer `%s'\n",
927 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
928 im->header.size = htons (sizeof (struct InfoMessage) + hs);
929 im->reserved = htonl (0);
930 im->peer = pos->identity;
931 GNUNET_SERVER_transmit_context_append_message (tc->tc, &im->header);
937 * @brief delete expired HELLO entries in directory
939 * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
940 * @param fn filename to test to see if the HELLO expired
941 * @return GNUNET_OK (continue iteration)
944 discard_hosts_helper (void *cls, const char *fn)
946 struct GNUNET_TIME_Absolute *now = cls;
947 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
948 const struct GNUNET_HELLO_Message *hello;
949 struct GNUNET_HELLO_Message *new_hello;
953 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
954 if (size < sizeof (struct GNUNET_MessageHeader))
956 if (0 != UNLINK (fn))
957 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
958 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
961 hello = (const struct GNUNET_HELLO_Message *) buffer;
963 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
965 if (NULL != new_hello)
966 (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses, &cnt);
967 if ( (NULL != new_hello) && (0 < cnt) )
969 GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
970 GNUNET_DISK_PERM_USER_READ |
971 GNUNET_DISK_PERM_USER_WRITE |
972 GNUNET_DISK_PERM_GROUP_READ |
973 GNUNET_DISK_PERM_OTHER_READ);
977 if (0 != UNLINK (fn))
978 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
979 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
981 GNUNET_free_non_null (new_hello);
987 * Call this method periodically to scan data/hosts for ancient
991 * @param tc scheduler context, aborted if reason is shutdown
994 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
996 struct GNUNET_TIME_Absolute now;
998 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1000 now = GNUNET_TIME_absolute_get ();
1001 GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
1002 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
1008 * Handle HELLO-message.
1010 * @param cls closure
1011 * @param client identification of the client
1012 * @param message the actual message
1015 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
1016 const struct GNUNET_MessageHeader *message)
1018 const struct GNUNET_HELLO_Message *hello;
1019 struct GNUNET_PeerIdentity pid;
1021 hello = (const struct GNUNET_HELLO_Message *) message;
1022 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
1025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1029 "HELLO", GNUNET_i2s (&pid));
1030 add_host_to_known_hosts (&pid);
1031 update_hello (&pid, hello);
1032 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1037 * Handle GET-message.
1039 * @param cls closure
1040 * @param client identification of the client
1041 * @param message the actual message
1044 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
1045 const struct GNUNET_MessageHeader *message)
1047 const struct ListPeerMessage *lpm;
1048 struct TransmitContext tcx;
1050 lpm = (const struct ListPeerMessage *) message;
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1052 "GET", GNUNET_i2s (&lpm->peer));
1053 tcx.friend_only = ntohl (lpm->include_friend_only);
1054 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1055 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
1057 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1058 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1059 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1064 * Handle GET-ALL-message.
1066 * @param cls closure
1067 * @param client identification of the client
1068 * @param message the actual message
1071 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
1072 const struct GNUNET_MessageHeader *message)
1074 const struct ListAllPeersMessage *lapm;
1075 struct TransmitContext tcx;
1077 lapm = (const struct ListAllPeersMessage *) message;
1078 tcx.friend_only = ntohl (lapm->include_friend_only);
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
1080 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1081 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, &tcx);
1082 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1083 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1084 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1090 * Pass the given client the information we have in the respective
1091 * host entry; the client is already in the notification context.
1093 * @param cls the 'struct GNUNET_SERVER_Client' to notify
1094 * @param key key for the value (unused)
1095 * @param value the 'struct HostEntry' to notify the client about
1096 * @return GNUNET_YES (always, continue to iterate)
1099 do_notify_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1101 struct NotificationContext *nc = cls;
1102 struct HostEntry *he = value;
1103 struct InfoMessage *msg;
1106 if ((NULL == he->hello) && (GNUNET_NO == nc->include_friend_only))
1108 /* We have no public hello */
1113 if ((NULL == he->friend_only_hello) && GNUNET_YES == nc->include_friend_only)
1115 /* We have no friend hello */
1119 msg = make_info_message (he, nc->include_friend_only);
1120 GNUNET_SERVER_notification_context_unicast (notify_list,
1130 * Handle NOTIFY-message.
1132 * @param cls closure
1133 * @param client identification of the client
1134 * @param message the actual message
1137 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
1138 const struct GNUNET_MessageHeader *message)
1140 struct NotifyMessage *nm = (struct NotifyMessage *) message;
1141 struct NotificationContext *nc;
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
1145 nc = GNUNET_malloc (sizeof (struct NotificationContext));
1146 nc->client = client;
1147 nc->include_friend_only = ntohl (nm->include_friend_only);
1149 GNUNET_CONTAINER_DLL_insert (nc_head, nc_tail, nc);
1150 GNUNET_SERVER_client_mark_monitor (client);
1151 GNUNET_SERVER_notification_context_add (notify_list, client);
1152 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, nc);
1153 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1158 * Client disconnect callback
1161 * @param client server client
1163 static void disconnect_cb (void *cls,struct GNUNET_SERVER_Client *client)
1165 struct NotificationContext *cur;
1167 for (cur = nc_head; NULL != cur; cur = cur->next)
1168 if (cur->client == client)
1174 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1180 * Release memory taken by a host entry.
1183 * @param key key of the host entry
1184 * @param value the 'struct HostEntry' to free
1185 * @return GNUNET_YES (continue to iterate)
1188 free_host_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1190 struct HostEntry *he = value;
1192 GNUNET_free_non_null (he->hello);
1193 GNUNET_free_non_null (he->friend_only_hello);
1200 * Clean up our state. Called during shutdown.
1203 * @param tc scheduler task context, unused
1206 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1208 struct NotificationContext *cur;
1209 struct NotificationContext *next;
1210 GNUNET_SERVER_notification_context_destroy (notify_list);
1213 for (cur = nc_head; NULL != cur; cur = next)
1216 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1220 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
1221 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
1224 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1231 * Start up peerinfo service.
1233 * @param cls closure
1234 * @param server the initialized server
1235 * @param cfg configuration to use
1238 run (void *cls, struct GNUNET_SERVER_Handle *server,
1239 const struct GNUNET_CONFIGURATION_Handle *cfg)
1241 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1242 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
1243 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
1244 sizeof (struct ListPeerMessage)},
1245 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
1246 sizeof (struct ListAllPeersMessage)},
1247 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
1248 sizeof (struct NotifyMessage)},
1253 struct DirScanContext dsc;
1256 hostmap = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1257 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
1258 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
1259 noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO");
1260 if (GNUNET_YES != noio)
1262 GNUNET_assert (GNUNET_OK ==
1263 GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
1265 &networkIdDirectory));
1266 GNUNET_DISK_directory_create (networkIdDirectory);
1268 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1269 &cron_scan_directory_data_hosts, NULL); /* CHECK */
1271 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1272 &cron_clean_data_hosts, NULL); /* CHECK */
1274 ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1275 GNUNET_asprintf (&peerdir,
1280 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1281 _("Importing HELLOs from `%s'\n"),
1284 dsc.remove_files = GNUNET_NO;
1286 GNUNET_DISK_directory_scan (peerdir,
1287 &hosts_directory_scan_callback, &dsc);
1289 GNUNET_free (peerdir);
1291 GNUNET_SERVER_add_handlers (server, handlers);
1292 GNUNET_SERVER_disconnect_notify (server, &disconnect_cb, NULL) ;
1293 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1300 * The main function for the peerinfo service.
1302 * @param argc number of arguments from the command line
1303 * @param argv command line arguments
1304 * @return 0 ok, 1 on error
1307 main (int argc, char *const *argv)
1313 GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
1314 &run, NULL)) ? 0 : 1;
1315 GNUNET_free_non_null (networkIdDirectory);
1320 /* end of gnunet-service-peerinfo.c */