2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file peerinfo/gnunet-service-peerinfo.c
23 * @brief maintains list of known peers
25 * Code to maintain the list of currently known hosts (in memory
26 * structure of data/hosts/).
28 * @author Christian Grothoff
31 * - notify clients when addresses in HELLO expire (#1933)
35 #include "gnunet_util_lib.h"
36 #include "gnunet_hello_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_statistics_service.h"
42 * How often do we scan the HOST_DIR for new entries?
44 #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * How often do we discard old entries in data/hosts/?
49 #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
53 * In-memory cache of known hosts.
59 * Identity of the peer.
61 struct GNUNET_PeerIdentity identity;
64 * Hello for the peer (can be NULL)
66 struct GNUNET_HELLO_Message *hello;
69 * Friend only hello for the peer (can be NULL)
71 struct GNUNET_HELLO_Message *friend_only_hello;
76 * Transmit context for GET requests
78 struct TransmitContext
81 * Server transmit context
83 struct GNUNET_SERVER_TransmitContext *tc;
86 * Include friend only HELLOs GNUNET_YES or _NO
92 * Result of reading a file
94 struct ReadHostFileContext
97 * Hello for the peer (can be NULL)
99 struct GNUNET_HELLO_Message *hello;
102 * Friend only hello for the peer (can be NULL)
104 struct GNUNET_HELLO_Message *friend_only_hello;
109 * Client notification context
111 struct NotificationContext
116 struct NotificationContext *prev;
121 struct NotificationContext *next;
126 struct GNUNET_SERVER_Client *client;
129 * Interested in friend only HELLO?
131 int include_friend_only;
136 * The in-memory list of known hosts, mapping of
137 * host IDs to 'struct HostEntry*' values.
139 static struct GNUNET_CONTAINER_MultiHashMap *hostmap;
142 * Clients to immediately notify about all changes.
144 static struct GNUNET_SERVER_NotificationContext *notify_list;
147 * Directory where the hellos are stored in (peerinfo/)
149 static char *networkIdDirectory;
152 * Handle for reporting statistics.
154 static struct GNUNET_STATISTICS_Handle *stats;
157 * DLL of notification contexts: head
159 static struct NotificationContext *nc_head;
162 * DLL of notification contexts: tail
164 static struct NotificationContext *nc_tail;
168 * Notify all clients in the notify list about the
169 * given host entry changing.
171 * @param he entry of the host for which we generate a notification
172 * @return generated notification message
174 static struct InfoMessage *
175 make_info_message (const struct HostEntry *he, int include_friend_only)
177 struct InfoMessage *im;
178 struct GNUNET_HELLO_Message *src;
181 if (GNUNET_YES == include_friend_only)
182 src = he->friend_only_hello;
186 hs = (NULL == src) ? 0 : GNUNET_HELLO_size (src);
187 im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
188 im->header.size = htons (hs + sizeof (struct InfoMessage));
189 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
190 im->peer = he->identity;
192 memcpy (&im[1], src, hs);
198 * Address iterator that causes expired entries to be discarded.
200 * @param cls pointer to the current time
201 * @param address the address
202 * @param expiration expiration time for the address
203 * @return GNUNET_NO if expiration smaller than the current time
206 discard_expired (void *cls, const struct GNUNET_HELLO_Address *address,
207 struct GNUNET_TIME_Absolute expiration)
209 const struct GNUNET_TIME_Absolute *now = cls;
211 if (now->abs_value > expiration.abs_value)
213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
214 _("Removing expired address of transport `%s'\n"),
215 address->transport_name);
223 * Address iterator that counts the remaining addresses.
225 * @param cls pointer to the counter
226 * @param address the address
227 * @param expiration expiration time for the address
228 * @return GNUNET_OK (always)
231 count_addresses (void *cls, const struct GNUNET_HELLO_Address *address,
232 struct GNUNET_TIME_Absolute expiration)
234 unsigned int *cnt = cls;
242 * Get the filename under which we would store the GNUNET_HELLO_Message
243 * for the given host and protocol.
245 * @param id peer for which we need the filename for the HELLO
246 * @return filename of the form DIRECTORY/HOSTID
249 get_host_filename (const struct GNUNET_PeerIdentity *id)
251 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
254 if (NULL == networkIdDirectory)
256 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
257 GNUNET_asprintf (&fn, "%s%s%s", networkIdDirectory, DIR_SEPARATOR_STR, &fil);
263 * Broadcast information about the given entry to all
266 * @param entry entry to broadcast about
269 notify_all (struct HostEntry *entry)
271 struct InfoMessage *msg_pub;
272 struct InfoMessage *msg_friend;
273 struct NotificationContext *cur;
275 msg_pub = make_info_message (entry, GNUNET_NO);
276 msg_friend = make_info_message (entry, GNUNET_YES);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Notifying all clients about peer `%s'\n",
278 GNUNET_i2s(&entry->identity));
279 for (cur = nc_head; NULL != cur; cur = cur->next)
281 if (GNUNET_NO == cur->include_friend_only)
283 GNUNET_SERVER_notification_context_unicast (notify_list,
288 if (GNUNET_YES == cur->include_friend_only)
290 GNUNET_SERVER_notification_context_unicast (notify_list,
296 GNUNET_free (msg_pub);
297 GNUNET_free (msg_friend);
302 * Bind a host address (hello) to a hostId.
304 * @param peer the peer for which this is a hello
305 * @param hello the verified (!) hello message
308 update_hello (const struct GNUNET_PeerIdentity *peer,
309 const struct GNUNET_HELLO_Message *hello);
313 * Try to read the HELLOs in the given filename and discard expired
314 * addresses. Removes the file if one the HELLO is mal-formed. If all
315 * addresses are expired, the HELLO is also removed (but the HELLO
316 * with the public key is still returned if it was found and valid).
318 * The file can contain up to two HELLO messages, a public and a friend only
321 * @param fn name of the file
322 * @param unlink_garbage if GNUNET_YES, try to remove useless files
325 read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r)
327 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
330 struct GNUNET_TIME_Absolute now;
333 const struct GNUNET_HELLO_Message *hello;
334 struct GNUNET_HELLO_Message *hello_clean;
339 r->friend_only_hello = NULL;
342 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
347 size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from `%s'\n", size_total, fn);
349 if (size_total < sizeof (struct GNUNET_MessageHeader))
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 _("Failed to parse HELLO in file `%s': %s\n"),
353 fn, "Fail has invalid size");
354 if ( (GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) )
355 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
360 while (read_pos < size_total)
362 hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
363 size_hello = GNUNET_HELLO_size (hello);
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 _("Failed to parse HELLO in file `%s': %s %u \n"),
368 fn, "HELLO is invalid and has size of ", size_hello);
369 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
370 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
374 now = GNUNET_TIME_absolute_get ();
375 hello_clean = GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES,
376 &discard_expired, &now);
378 (void) GNUNET_HELLO_iterate_addresses (hello_clean, GNUNET_NO,
379 &count_addresses, &left);
383 GNUNET_free (hello_clean);
387 if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello_clean))
389 if (NULL == r->hello)
390 r->hello = hello_clean;
394 GNUNET_free (r->hello);
395 r->hello = hello_clean;
400 if (NULL == r->friend_only_hello)
401 r->friend_only_hello = hello_clean;
405 GNUNET_free (r->friend_only_hello);
406 r->friend_only_hello = hello_clean;
409 read_pos += size_hello;
414 /* no addresses left, remove from disk */
415 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
416 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' and `%s' HELLO message in file\n",
420 (NULL != r->hello) ? "public" : "NO public",
421 (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
426 * Add a host to the list and notify clients about this event
428 * @param identity the identity of the host
429 * @return the HostEntry
431 static struct HostEntry *
432 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
434 struct HostEntry *entry;
435 struct ReadHostFileContext r;
438 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n", GNUNET_i2s (identity));
442 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
444 entry = GNUNET_malloc (sizeof (struct HostEntry));
445 entry->identity = *identity;
446 GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
449 fn = get_host_filename (identity);
452 read_host_file (fn, GNUNET_YES, &r);
454 update_hello (identity, r.hello);
455 if (NULL != r.friend_only_hello)
456 update_hello (identity, r.friend_only_hello);
457 GNUNET_free_non_null (r.hello);
458 GNUNET_free_non_null (r.friend_only_hello);
467 * Remove a file that should not be there. LOG
468 * success or failure.
470 * @param fullname name of the file to remove
473 remove_garbage (const char *fullname)
475 if (0 == UNLINK (fullname))
476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
478 ("File `%s' in directory `%s' does not match naming convention. "
479 "Removed.\n"), fullname, networkIdDirectory);
481 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
487 * Closure for 'hosts_directory_scan_callback'.
489 struct DirScanContext
492 * GNUNET_YES if we should remove files that are broken,
493 * GNUNET_NO if the directory we are iterating over should
494 * be treated as read-only by us.
499 * Counter for the number of (valid) entries found, incremented
500 * by one for each match.
502 unsigned int matched;
507 * Function that is called on each HELLO file in a particular directory.
508 * Try to parse the file and add the HELLO to our list.
510 * @param cls pointer to 'unsigned int' to increment for each file, or NULL
511 * if the file is from a read-only, read-once resource directory
512 * @param fullname name of the file to parse
513 * @return GNUNET_OK (continue iteration)
516 hosts_directory_scan_callback (void *cls, const char *fullname)
518 struct DirScanContext *dsc = cls;
519 struct GNUNET_PeerIdentity identity;
520 struct ReadHostFileContext r;
521 const char *filename;
522 struct GNUNET_PeerIdentity id_public;
523 struct GNUNET_PeerIdentity id_friend;
524 struct GNUNET_PeerIdentity id;
526 if (GNUNET_YES != GNUNET_DISK_file_test (fullname))
527 return GNUNET_OK; /* ignore non-files */
529 filename = strrchr (fullname, DIR_SEPARATOR);
530 if ((NULL == filename) || (1 > strlen (filename)))
535 read_host_file (fullname, dsc->remove_files, &r);
536 if ( (NULL == r.hello) && (NULL == r.friend_only_hello))
538 if (GNUNET_YES == dsc->remove_files)
539 remove_garbage (fullname);
543 if (NULL != r.friend_only_hello)
545 if (GNUNET_OK != GNUNET_HELLO_get_id (r.friend_only_hello, &id_friend))
546 if (GNUNET_YES == dsc->remove_files)
548 remove_garbage (fullname);
555 if (GNUNET_OK != GNUNET_HELLO_get_id (r.hello, &id_public))
556 if (GNUNET_YES == dsc->remove_files)
558 remove_garbage (fullname);
564 if ( (NULL != r.hello) && (NULL != r.friend_only_hello) &&
565 (0 != memcmp (&id_friend, &id_public, sizeof (id_friend))) )
567 /* HELLOs are not for the same peer */
569 if (GNUNET_YES == dsc->remove_files)
570 remove_garbage (fullname);
573 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
575 if (0 != memcmp (&id, &identity, sizeof (id_friend)))
577 /* HELLOs are not for the same peer */
579 if (GNUNET_YES == dsc->remove_files)
580 remove_garbage (fullname);
584 /* ok, found something valid, remember HELLO */
585 add_host_to_known_hosts (&id);
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' public HELLO \n",
590 update_hello (&id, r.hello);
591 GNUNET_free (r.hello);
593 if (NULL != r.friend_only_hello)
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' friend only HELLO \n",
597 update_hello (&id, r.friend_only_hello);
598 GNUNET_free (r.friend_only_hello);
606 * Call this method periodically to scan data/hosts for new hosts.
609 * @param tc scheduler context, aborted if reason is shutdown
612 cron_scan_directory_data_hosts (void *cls,
613 const struct GNUNET_SCHEDULER_TaskContext *tc)
615 static unsigned int retries;
616 struct DirScanContext dsc;
618 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
620 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
622 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
623 GNUNET_SCHEDULER_PRIORITY_IDLE,
624 &cron_scan_directory_data_hosts, NULL);
628 dsc.remove_files = GNUNET_YES;
629 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
630 _("Scanning directory `%s'\n"), networkIdDirectory);
631 GNUNET_DISK_directory_scan (networkIdDirectory,
632 &hosts_directory_scan_callback, &dsc);
633 if ((0 == dsc.matched) && (0 == (++retries & 31)))
634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
635 _("Still no peers found in `%s'!\n"), networkIdDirectory);
636 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
637 GNUNET_SCHEDULER_PRIORITY_IDLE,
638 &cron_scan_directory_data_hosts,
643 static struct GNUNET_HELLO_Message *
644 update_friend_hello (const struct GNUNET_HELLO_Message *hello,
645 const struct GNUNET_HELLO_Message *friend_hello)
647 struct GNUNET_HELLO_Message * res;
648 struct GNUNET_HELLO_Message * tmp;
649 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
651 if (NULL != friend_hello)
653 res = GNUNET_HELLO_merge (hello, friend_hello);
654 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
658 GNUNET_HELLO_get_key (hello, &pk);
659 tmp = GNUNET_HELLO_create (&pk, NULL, NULL, GNUNET_YES);
660 res = GNUNET_HELLO_merge (hello, tmp);
662 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
669 * Bind a host address (hello) to a hostId.
671 * @param peer the peer for which this is a hello
672 * @param hello the verified (!) hello message
675 update_hello (const struct GNUNET_PeerIdentity *peer,
676 const struct GNUNET_HELLO_Message *hello)
679 struct HostEntry *host;
680 struct GNUNET_HELLO_Message *mrg;
681 struct GNUNET_HELLO_Message **dest;
682 struct GNUNET_TIME_Absolute delta;
685 int friend_hello_type;
687 int store_friend_hello;
691 host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
692 GNUNET_assert (NULL != host);
694 friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating %s HELLO for `%s'\n",
696 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
700 if (GNUNET_YES == friend_hello_type)
702 dest = &host->friend_only_hello;
711 (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
712 memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
716 mrg = GNUNET_HELLO_merge ((*dest), hello);
717 delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ());
718 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
720 /* no differences, just ignore the update */
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No change in %s HELLO for `%s'\n",
722 (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
727 GNUNET_free ((*dest));
731 if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type))
733 /* Update friend only hello */
734 mrg = update_friend_hello (host->hello, host->friend_only_hello);
735 if (NULL != host->friend_only_hello)
736 GNUNET_free (host->friend_only_hello);
737 host->friend_only_hello = mrg;
740 if (NULL != host->hello)
741 GNUNET_assert ((GNUNET_NO == GNUNET_HELLO_is_friend_only (host->hello)));
742 if (NULL != host->friend_only_hello)
743 GNUNET_assert ((GNUNET_YES == GNUNET_HELLO_is_friend_only(host->friend_only_hello)));
745 store_hello = GNUNET_NO;
746 store_friend_hello = GNUNET_NO;
747 fn = get_host_filename (peer);
749 (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn)) )
752 store_hello = GNUNET_NO;
755 if (NULL != host->hello)
756 (void) GNUNET_HELLO_iterate_addresses (host->hello,
757 GNUNET_NO, &count_addresses, &cnt);
760 store_hello = GNUNET_YES;
761 size += GNUNET_HELLO_size (host->hello);
764 if (NULL != host->friend_only_hello)
765 (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello, GNUNET_NO,
766 &count_addresses, &cnt);
769 store_friend_hello = GNUNET_YES;
770 size += GNUNET_HELLO_size (host->friend_only_hello);
773 if ((GNUNET_NO == store_hello) && (GNUNET_NO == store_friend_hello))
775 /* no valid addresses, don't put HELLO on disk; in fact,
776 if one exists on disk, remove it */
781 buffer = GNUNET_malloc (size);
784 if (GNUNET_YES == store_hello)
786 memcpy (buffer, host->hello, GNUNET_HELLO_size (host->hello));
787 pos += GNUNET_HELLO_size (host->hello);
789 if (GNUNET_YES == store_friend_hello)
791 memcpy (&buffer[pos], host->friend_only_hello, GNUNET_HELLO_size (host->friend_only_hello));
792 pos += GNUNET_HELLO_size (host->friend_only_hello);
794 GNUNET_assert (pos == size);
796 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
797 GNUNET_DISK_PERM_USER_READ |
798 GNUNET_DISK_PERM_USER_WRITE |
799 GNUNET_DISK_PERM_GROUP_READ |
800 GNUNET_DISK_PERM_OTHER_READ))
801 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stored %s %s HELLO in %s with total size %u\n",
804 (GNUNET_YES == store_friend_hello) ? "friend-only": "",
805 (GNUNET_YES == store_hello) ? "public": "",
807 GNUNET_free (buffer);
810 GNUNET_free_non_null (fn);
816 * Do transmit info about peer to given host.
818 * @param cls NULL to hit all hosts, otherwise specifies a particular target
820 * @param value information to transmit
821 * @return GNUNET_YES (continue to iterate)
824 add_to_tc (void *cls, const struct GNUNET_HashCode * key, void *value)
826 struct TransmitContext *tc = cls;
827 struct HostEntry *pos = value;
828 struct InfoMessage *im;
830 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
833 im = (struct InfoMessage *) buf;
835 if ((pos->hello != NULL) && (GNUNET_NO == tc->friend_only))
837 /* Copy public HELLO */
838 hs = GNUNET_HELLO_size (pos->hello);
839 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
840 sizeof (struct InfoMessage));
841 memcpy (&im[1], pos->hello, hs);
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending public HELLO with size %u for peer `%4s'\n",
843 hs, GNUNET_h2s (key));
845 else if ((pos->friend_only_hello != NULL) && (GNUNET_YES == tc->friend_only))
847 /* Copy friend only HELLO */
848 hs = GNUNET_HELLO_size (pos->friend_only_hello);
849 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
850 sizeof (struct InfoMessage));
851 memcpy (&im[1], pos->friend_only_hello, hs);
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending friend-only HELLO with size %u for peer `%4s'\n",
853 hs, GNUNET_h2s (key));
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding no HELLO for peer `%s'\n",
861 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
862 im->header.size = htons (sizeof (struct InfoMessage) + hs);
863 im->reserved = htonl (0);
864 im->peer = pos->identity;
865 GNUNET_SERVER_transmit_context_append_message (tc->tc, &im->header);
871 * @brief delete expired HELLO entries in directory
873 * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
874 * @param fn filename to test to see if the HELLO expired
875 * @return GNUNET_OK (continue iteration)
878 discard_hosts_helper (void *cls, const char *fn)
880 struct GNUNET_TIME_Absolute *now = cls;
881 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
882 const struct GNUNET_HELLO_Message *hello;
883 struct GNUNET_HELLO_Message *new_hello;
893 read_size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
894 if (read_size < sizeof (struct GNUNET_MessageHeader))
896 if (0 != UNLINK (fn))
897 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
898 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
902 writebuffer = GNUNET_malloc (read_size);
905 while (read_pos < read_size)
907 /* Check each HELLO */
908 hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
909 cur_hello_size = GNUNET_HELLO_size (hello);
911 if (0 == cur_hello_size)
913 /* Invalid data, discard */
914 if (0 != UNLINK (fn))
915 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
916 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
919 new_hello = GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
921 if (NULL != new_hello)
922 (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses, &cnt);
923 if ( (NULL != new_hello) && (0 < cnt) )
925 /* Store new HELLO to write it when done */
926 new_hello_size = GNUNET_HELLO_size(new_hello);
927 memcpy (&writebuffer[write_pos], new_hello, new_hello_size);
928 write_pos += new_hello_size;
930 read_pos += cur_hello_size;
931 GNUNET_free_non_null (new_hello);
936 GNUNET_DISK_fn_write (fn, writebuffer,write_pos,
937 GNUNET_DISK_PERM_USER_READ |
938 GNUNET_DISK_PERM_USER_WRITE |
939 GNUNET_DISK_PERM_GROUP_READ |
940 GNUNET_DISK_PERM_OTHER_READ);
942 else if (0 != UNLINK (fn))
943 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
944 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
946 GNUNET_free (writebuffer);
952 * Call this method periodically to scan peerinfo/ for ancient
956 * @param tc scheduler context, aborted if reason is shutdown
959 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
961 struct GNUNET_TIME_Absolute now;
963 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
965 now = GNUNET_TIME_absolute_get ();
966 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
967 _("Cleaning up directory `%s'\n"), networkIdDirectory);
968 GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
969 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
975 * Handle HELLO-message.
978 * @param client identification of the client
979 * @param message the actual message
982 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
983 const struct GNUNET_MessageHeader *message)
985 const struct GNUNET_HELLO_Message *hello;
986 struct GNUNET_PeerIdentity pid;
988 hello = (const struct GNUNET_HELLO_Message *) message;
989 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
996 "HELLO", GNUNET_i2s (&pid));
997 add_host_to_known_hosts (&pid);
998 update_hello (&pid, hello);
999 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1004 * Handle GET-message.
1006 * @param cls closure
1007 * @param client identification of the client
1008 * @param message the actual message
1011 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
1012 const struct GNUNET_MessageHeader *message)
1014 const struct ListPeerMessage *lpm;
1015 struct TransmitContext tcx;
1017 lpm = (const struct ListPeerMessage *) message;
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
1019 "GET", GNUNET_i2s (&lpm->peer));
1020 tcx.friend_only = ntohl (lpm->include_friend_only);
1021 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1022 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
1024 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1025 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1026 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1031 * Handle GET-ALL-message.
1033 * @param cls closure
1034 * @param client identification of the client
1035 * @param message the actual message
1038 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
1039 const struct GNUNET_MessageHeader *message)
1041 const struct ListAllPeersMessage *lapm;
1042 struct TransmitContext tcx;
1044 lapm = (const struct ListAllPeersMessage *) message;
1045 tcx.friend_only = ntohl (lapm->include_friend_only);
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
1047 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
1048 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, &tcx);
1049 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
1050 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
1051 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
1057 * Pass the given client the information we have in the respective
1058 * host entry; the client is already in the notification context.
1060 * @param cls the 'struct GNUNET_SERVER_Client' to notify
1061 * @param key key for the value (unused)
1062 * @param value the 'struct HostEntry' to notify the client about
1063 * @return GNUNET_YES (always, continue to iterate)
1066 do_notify_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1068 struct NotificationContext *nc = cls;
1069 struct HostEntry *he = value;
1070 struct InfoMessage *msg;
1072 if ((NULL == he->hello) && (GNUNET_NO == nc->include_friend_only))
1074 /* We have no public hello */
1079 if ((NULL == he->friend_only_hello) && GNUNET_YES == nc->include_friend_only)
1081 /* We have no friend hello */
1085 msg = make_info_message (he, nc->include_friend_only);
1086 GNUNET_SERVER_notification_context_unicast (notify_list,
1096 * Handle NOTIFY-message.
1098 * @param cls closure
1099 * @param client identification of the client
1100 * @param message the actual message
1103 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
1104 const struct GNUNET_MessageHeader *message)
1106 struct NotifyMessage *nm = (struct NotifyMessage *) message;
1107 struct NotificationContext *nc;
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
1111 nc = GNUNET_malloc (sizeof (struct NotificationContext));
1112 nc->client = client;
1113 nc->include_friend_only = ntohl (nm->include_friend_only);
1115 GNUNET_CONTAINER_DLL_insert (nc_head, nc_tail, nc);
1116 GNUNET_SERVER_client_mark_monitor (client);
1117 GNUNET_SERVER_notification_context_add (notify_list, client);
1118 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, nc);
1119 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1124 * Client disconnect callback
1127 * @param client server client
1129 static void disconnect_cb (void *cls,struct GNUNET_SERVER_Client *client)
1131 struct NotificationContext *cur;
1133 for (cur = nc_head; NULL != cur; cur = cur->next)
1134 if (cur->client == client)
1140 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1146 * Release memory taken by a host entry.
1149 * @param key key of the host entry
1150 * @param value the 'struct HostEntry' to free
1151 * @return GNUNET_YES (continue to iterate)
1154 free_host_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1156 struct HostEntry *he = value;
1158 GNUNET_free_non_null (he->hello);
1159 GNUNET_free_non_null (he->friend_only_hello);
1166 * Clean up our state. Called during shutdown.
1169 * @param tc scheduler task context, unused
1172 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1174 struct NotificationContext *cur;
1175 struct NotificationContext *next;
1176 GNUNET_SERVER_notification_context_destroy (notify_list);
1179 for (cur = nc_head; NULL != cur; cur = next)
1182 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1186 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
1187 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
1190 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1197 * Start up peerinfo service.
1199 * @param cls closure
1200 * @param server the initialized server
1201 * @param cfg configuration to use
1204 run (void *cls, struct GNUNET_SERVER_Handle *server,
1205 const struct GNUNET_CONFIGURATION_Handle *cfg)
1207 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1208 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
1209 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
1210 sizeof (struct ListPeerMessage)},
1211 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
1212 sizeof (struct ListAllPeersMessage)},
1213 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
1214 sizeof (struct NotifyMessage)},
1219 struct DirScanContext dsc;
1222 hostmap = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1223 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
1224 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
1225 noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO");
1226 if (GNUNET_YES != noio)
1228 GNUNET_assert (GNUNET_OK ==
1229 GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
1231 &networkIdDirectory));
1232 GNUNET_DISK_directory_create (networkIdDirectory);
1234 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1235 &cron_scan_directory_data_hosts, NULL);
1237 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1238 &cron_clean_data_hosts, NULL);
1240 ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1241 GNUNET_asprintf (&peerdir,
1246 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1247 _("Importing HELLOs from `%s'\n"),
1250 dsc.remove_files = GNUNET_NO;
1252 GNUNET_DISK_directory_scan (peerdir,
1253 &hosts_directory_scan_callback, &dsc);
1255 GNUNET_free (peerdir);
1257 GNUNET_SERVER_add_handlers (server, handlers);
1258 GNUNET_SERVER_disconnect_notify (server, &disconnect_cb, NULL) ;
1259 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1266 * The main function for the peerinfo service.
1268 * @param argc number of arguments from the command line
1269 * @param argv command line arguments
1270 * @return 0 ok, 1 on error
1273 main (int argc, char *const *argv)
1279 GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
1280 &run, NULL)) ? 0 : 1;
1281 GNUNET_free_non_null (networkIdDirectory);
1286 /* end of gnunet-service-peerinfo.c */