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 (data/hosts)
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)
281 GNUNET_SERVER_notification_context_unicast (notify_list,
285 if (GNUNET_YES == cur->include_friend_only)
286 GNUNET_SERVER_notification_context_unicast (notify_list,
291 GNUNET_free (msg_pub);
292 GNUNET_free (msg_friend);
297 * Try to read the HELLOs in the given filename and discard expired
298 * addresses. Removes the file if one the HELLO is mal-formed. If all
299 * addresses are expired, the HELLO is also removed (but the HELLO
300 * with the public key is still returned if it was found and valid).
302 * @param fn name of the file
303 * @param unlink_garbage if GNUNET_YES, try to remove useless files
306 read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r)
308 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
309 const struct GNUNET_HELLO_Message *hello_1st;
310 const struct GNUNET_HELLO_Message *hello_2nd;
311 struct GNUNET_HELLO_Message *hello_clean_1st;
312 struct GNUNET_HELLO_Message *hello_clean_2nd;
316 struct GNUNET_TIME_Absolute now;
321 hello_clean_1st = NULL;
322 hello_clean_2nd = NULL;
326 r->friend_only_hello = NULL;
329 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
332 size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
333 if (size_total < sizeof (struct GNUNET_MessageHeader))
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336 _("Failed to parse HELLO in file `%s': %s\n"),
337 fn, "Fail has invalid size");
338 if ( (GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) )
339 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
343 hello_1st = (const struct GNUNET_HELLO_Message *) buffer;
344 size_1st = ntohs (((const struct GNUNET_MessageHeader *) hello_1st)->size);
345 if ((size_1st < sizeof (struct GNUNET_MessageHeader)) ||
346 (size_1st != GNUNET_HELLO_size (hello_1st)))
348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349 _("Failed to parse HELLO in file `%s': %s\n"),
350 fn, "1nd HELLO has wrong size");
351 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
352 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
356 if (size_total > size_1st)
358 hello_2nd = (const struct GNUNET_HELLO_Message *) &buffer[size_1st];
359 size_2nd = ntohs (((const struct GNUNET_MessageHeader *) hello_2nd)->size);
360 if ((size_2nd < sizeof (struct GNUNET_MessageHeader)) ||
361 (size_2nd != GNUNET_HELLO_size (hello_2nd)))
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364 _("Failed to parse HELLO in file `%s': %s\n"),
365 fn, "2nd HELLO has wrong size");
366 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
367 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
372 if (size_total != (size_1st + size_2nd))
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375 _("Failed to parse HELLO in file `%s': %s\n"),
376 fn, "Multiple HELLOs but total size is wrong");
377 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
378 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
382 now = GNUNET_TIME_absolute_get ();
384 hello_clean_1st = GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_YES,
385 &discard_expired, &now);
387 (void) GNUNET_HELLO_iterate_addresses (hello_1st, GNUNET_NO,
388 &count_addresses, &left);
391 /* no addresses left, remove from disk */
392 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
393 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
396 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_1st))
397 r->hello = hello_clean_1st;
399 r->friend_only_hello = hello_clean_1st;
401 if (NULL != hello_2nd)
403 hello_clean_2nd = GNUNET_HELLO_iterate_addresses (hello_2nd, GNUNET_YES,
404 &discard_expired, &now);
406 (void) GNUNET_HELLO_iterate_addresses (hello_2nd, GNUNET_NO,
407 &count_addresses, &left);
410 /* no addresses left, remove from disk */
411 if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)))
412 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
415 if (GNUNET_NO == GNUNET_HELLO_is_friend_only(hello_clean_2nd))
416 r->hello = hello_clean_2nd;
418 r->friend_only_hello = hello_clean_2nd;
424 * Add a host to the list.
426 * @param identity the identity of the host
429 add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
431 struct HostEntry *entry;
432 struct ReadHostFileContext r;
435 entry = GNUNET_CONTAINER_multihashmap_get (hostmap, &identity->hashPubKey);
438 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers known"), 1,
440 entry = GNUNET_malloc (sizeof (struct HostEntry));
441 entry->identity = *identity;
442 GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
443 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
444 fn = get_host_filename (identity);
447 read_host_file (fn, GNUNET_YES, &r);
448 entry->hello = r.hello;
449 entry->hello = r.friend_only_hello;
457 * Remove a file that should not be there. LOG
458 * success or failure.
460 * @param fullname name of the file to remove
463 remove_garbage (const char *fullname)
465 if (0 == UNLINK (fullname))
466 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
468 ("File `%s' in directory `%s' does not match naming convention. "
469 "Removed.\n"), fullname, networkIdDirectory);
471 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
477 * Closure for 'hosts_directory_scan_callback'.
479 struct DirScanContext
482 * GNUNET_YES if we should remove files that are broken,
483 * GNUNET_NO if the directory we are iterating over should
484 * be treated as read-only by us.
489 * Counter for the number of (valid) entries found, incremented
490 * by one for each match.
492 unsigned int matched;
497 * Function that is called on each HELLO file in a particular directory.
498 * Try to parse the file and add the HELLO to our list.
500 * @param cls pointer to 'unsigned int' to increment for each file, or NULL
501 * if the file is from a read-only, read-once resource directory
502 * @param fullname name of the file to parse
503 * @return GNUNET_OK (continue iteration)
506 hosts_directory_scan_callback (void *cls, const char *fullname)
508 struct DirScanContext *dsc = cls;
509 struct GNUNET_PeerIdentity identity;
510 struct ReadHostFileContext r;
511 const char *filename;
512 struct HostEntry *entry;
513 struct GNUNET_PeerIdentity id_public;
514 struct GNUNET_PeerIdentity id_friend;
516 if (GNUNET_YES != GNUNET_DISK_file_test (fullname))
517 return GNUNET_OK; /* ignore non-files */
518 if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
520 if (GNUNET_YES == dsc->remove_files)
521 remove_garbage (fullname);
525 &fullname[strlen (fullname) -
526 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1];
527 if (DIR_SEPARATOR != filename[-1])
529 if (GNUNET_YES == dsc->remove_files)
530 remove_garbage (fullname);
534 GNUNET_CRYPTO_hash_from_string (filename, &identity.hashPubKey))
536 /* odd filename, but might still be valid, try getting identity from HELLO */
537 read_host_file (filename, dsc->remove_files, &r);
538 if ( ((NULL != r.hello) &&
539 (GNUNET_OK == GNUNET_HELLO_get_id (r.hello, &id_public))) ||
540 ((NULL != r.friend_only_hello) &&
541 (GNUNET_OK == GNUNET_HELLO_get_id (r.friend_only_hello, &id_friend))) )
543 if ( (NULL != r.hello) && (NULL != r.friend_only_hello) &&
544 (0 != memcmp (&id_friend, &id_public, sizeof (id_friend))) )
546 /* HELLOs are not for the same peer */
548 if (GNUNET_YES == dsc->remove_files)
549 remove_garbage (fullname);
553 /* ok, found something valid, remember HELLO */
554 entry = GNUNET_malloc (sizeof (struct HostEntry));
555 entry->identity = id_friend;
556 GNUNET_CONTAINER_multihashmap_put (hostmap, &entry->identity.hashPubKey, entry,
557 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
558 entry->hello = r.hello;
559 entry->hello = r.friend_only_hello;
565 if (GNUNET_YES == dsc->remove_files)
566 remove_garbage (fullname);
570 add_host_to_known_hosts (&identity);
576 * Call this method periodically to scan data/hosts for new hosts.
579 * @param tc scheduler context, aborted if reason is shutdown
582 cron_scan_directory_data_hosts (void *cls,
583 const struct GNUNET_SCHEDULER_TaskContext *tc)
585 static unsigned int retries;
586 struct DirScanContext dsc;
588 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
590 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (networkIdDirectory))
592 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
593 GNUNET_SCHEDULER_PRIORITY_IDLE,
594 &cron_scan_directory_data_hosts, NULL);
598 dsc.remove_files = GNUNET_YES;
599 GNUNET_DISK_directory_scan (networkIdDirectory,
600 &hosts_directory_scan_callback, &dsc);
601 if ((0 == dsc.matched) && (0 == (++retries & 31)))
602 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
603 _("Still no peers found in `%s'!\n"), networkIdDirectory);
604 GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
605 GNUNET_SCHEDULER_PRIORITY_IDLE,
606 &cron_scan_directory_data_hosts,
611 static struct GNUNET_HELLO_Message *
612 update_friend_hello (const struct GNUNET_HELLO_Message *hello,
613 const struct GNUNET_HELLO_Message *friend_hello)
615 struct GNUNET_HELLO_Message * res;
616 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
618 if (NULL != friend_hello)
620 res = GNUNET_HELLO_merge (hello, friend_hello);
621 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
625 GNUNET_HELLO_get_key (hello, &pk);
626 res = GNUNET_HELLO_create (&pk, NULL, NULL, GNUNET_YES);
627 res = GNUNET_HELLO_merge (hello, res);
628 GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
635 * Bind a host address (hello) to a hostId.
637 * @param peer the peer for which this is a hello
638 * @param hello the verified (!) hello message
641 bind_address (const struct GNUNET_PeerIdentity *peer,
642 const struct GNUNET_HELLO_Message *hello)
645 struct HostEntry *host;
646 struct GNUNET_HELLO_Message *mrg;
647 struct GNUNET_HELLO_Message **dest;
648 struct GNUNET_TIME_Absolute delta;
650 int friend_hello_type;
652 add_host_to_known_hosts (peer);
653 host = GNUNET_CONTAINER_multihashmap_get (hostmap, &peer->hashPubKey);
654 GNUNET_assert (NULL != host);
655 friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
658 if (GNUNET_YES == friend_hello_type)
660 dest = &host->friend_only_hello;
669 (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
670 memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
674 mrg = GNUNET_HELLO_merge ((*dest), hello);
675 delta = GNUNET_HELLO_equals (mrg, (*dest), GNUNET_TIME_absolute_get ());
676 if (delta.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
678 /* no differences, just ignore the update */
682 GNUNET_free ((*dest));
686 if ((NULL != (host->hello)) && (GNUNET_NO == friend_hello_type))
687 host->friend_only_hello = update_friend_hello (host->hello, host->friend_only_hello);
689 fn = get_host_filename (peer);
691 (GNUNET_OK == GNUNET_DISK_directory_create_for_file (fn)) )
695 (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses,
699 /* no valid addresses, don't put HELLO on disk; in fact,
700 if one exists on disk, remove it */
706 GNUNET_DISK_fn_write (fn, host->hello, GNUNET_HELLO_size (host->hello),
707 GNUNET_DISK_PERM_USER_READ |
708 GNUNET_DISK_PERM_USER_WRITE |
709 GNUNET_DISK_PERM_GROUP_READ |
710 GNUNET_DISK_PERM_OTHER_READ))
711 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
715 GNUNET_free_non_null (fn);
723 * Do transmit info about peer to given host.
725 * @param cls NULL to hit all hosts, otherwise specifies a particular target
727 * @param value information to transmit
728 * @return GNUNET_YES (continue to iterate)
731 add_to_tc (void *cls, const struct GNUNET_HashCode * key, void *value)
733 struct TransmitContext *tc = cls;
734 struct HostEntry *pos = value;
735 struct InfoMessage *im;
737 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
740 im = (struct InfoMessage *) buf;
742 if ((pos->hello != NULL) && (GNUNET_NO == tc->friend_only))
744 /* Copy public HELLO */
745 hs = GNUNET_HELLO_size (pos->hello);
746 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
747 sizeof (struct InfoMessage));
748 memcpy (&im[1], pos->hello, hs);
752 if ((pos->friend_only_hello != NULL) && (GNUNET_YES == tc->friend_only))
754 /* Copy friend only HELLO */
755 hs = GNUNET_HELLO_size (pos->friend_only_hello);
756 GNUNET_assert (hs < GNUNET_SERVER_MAX_MESSAGE_SIZE -
757 sizeof (struct InfoMessage));
758 memcpy (&im[1], pos->friend_only_hello, hs);
761 im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
762 im->header.size = htons (sizeof (struct InfoMessage) + hs);
763 im->reserved = htonl (0);
764 im->peer = pos->identity;
765 GNUNET_SERVER_transmit_context_append_message (tc->tc, &im->header);
771 * @brief delete expired HELLO entries in directory
773 * @param cls pointer to current time (struct GNUNET_TIME_Absolute)
774 * @param fn filename to test to see if the HELLO expired
775 * @return GNUNET_OK (continue iteration)
778 discard_hosts_helper (void *cls, const char *fn)
780 struct GNUNET_TIME_Absolute *now = cls;
781 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
782 const struct GNUNET_HELLO_Message *hello;
783 struct GNUNET_HELLO_Message *new_hello;
787 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
788 if (size < sizeof (struct GNUNET_MessageHeader))
790 if (0 != UNLINK (fn))
791 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
792 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
795 hello = (const struct GNUNET_HELLO_Message *) buffer;
797 GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, now);
799 if (NULL != new_hello)
800 (void) GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_addresses, &cnt);
801 if ( (NULL != new_hello) && (0 < cnt) )
803 GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
804 GNUNET_DISK_PERM_USER_READ |
805 GNUNET_DISK_PERM_USER_WRITE |
806 GNUNET_DISK_PERM_GROUP_READ |
807 GNUNET_DISK_PERM_OTHER_READ);
811 if (0 != UNLINK (fn))
812 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
813 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
815 GNUNET_free_non_null (new_hello);
821 * Call this method periodically to scan data/hosts for ancient
825 * @param tc scheduler context, aborted if reason is shutdown
828 cron_clean_data_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
830 struct GNUNET_TIME_Absolute now;
832 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
834 now = GNUNET_TIME_absolute_get ();
835 GNUNET_DISK_directory_scan (networkIdDirectory, &discard_hosts_helper, &now);
836 GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ, &cron_clean_data_hosts,
842 * Handle HELLO-message.
845 * @param client identification of the client
846 * @param message the actual message
849 handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
850 const struct GNUNET_MessageHeader *message)
852 const struct GNUNET_HELLO_Message *hello;
853 struct GNUNET_PeerIdentity pid;
855 hello = (const struct GNUNET_HELLO_Message *) message;
856 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
859 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
863 "HELLO", GNUNET_i2s (&pid));
864 bind_address (&pid, hello);
865 GNUNET_SERVER_receive_done (client, GNUNET_OK);
870 * Handle GET-message.
873 * @param client identification of the client
874 * @param message the actual message
877 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
878 const struct GNUNET_MessageHeader *message)
880 const struct ListPeerMessage *lpm;
881 struct TransmitContext tcx;
883 lpm = (const struct ListPeerMessage *) message;
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
885 "GET", GNUNET_i2s (&lpm->peer));
886 tcx.friend_only = ntohl (lpm->include_friend_only);
887 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
888 GNUNET_CONTAINER_multihashmap_get_multiple (hostmap, &lpm->peer.hashPubKey,
890 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
891 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
892 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
897 * Handle GET-ALL-message.
900 * @param client identification of the client
901 * @param message the actual message
904 handle_get_all (void *cls, struct GNUNET_SERVER_Client *client,
905 const struct GNUNET_MessageHeader *message)
907 const struct ListAllPeersMessage *lapm;
908 struct TransmitContext tcx;
910 lapm = (const struct ListAllPeersMessage *) message;
911 tcx.friend_only = ntohl (lapm->include_friend_only);
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "GET_ALL");
913 tcx.tc = GNUNET_SERVER_transmit_context_create (client);
914 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &add_to_tc, &tcx);
915 GNUNET_SERVER_transmit_context_append_data (tcx.tc, NULL, 0,
916 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
917 GNUNET_SERVER_transmit_context_run (tcx.tc, GNUNET_TIME_UNIT_FOREVER_REL);
923 * Pass the given client the information we have in the respective
924 * host entry; the client is already in the notification context.
926 * @param cls the 'struct GNUNET_SERVER_Client' to notify
927 * @param key key for the value (unused)
928 * @param value the 'struct HostEntry' to notify the client about
929 * @return GNUNET_YES (always, continue to iterate)
932 do_notify_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
934 struct NotificationContext *nc = cls;
935 struct HostEntry *he = value;
936 struct InfoMessage *msg;
939 if ((NULL == he->hello) && (GNUNET_NO == nc->include_friend_only))
941 /* We have no public hello */
946 if ((NULL == he->friend_only_hello) && GNUNET_YES == nc->include_friend_only)
948 /* We have no friend hello */
952 msg = make_info_message (he, nc->include_friend_only);
953 GNUNET_SERVER_notification_context_unicast (notify_list,
963 * Handle NOTIFY-message.
966 * @param client identification of the client
967 * @param message the actual message
970 handle_notify (void *cls, struct GNUNET_SERVER_Client *client,
971 const struct GNUNET_MessageHeader *message)
973 struct NotifyMessage *nm = (struct NotifyMessage *) message;
974 struct NotificationContext *nc;
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received\n", "NOTIFY");
978 nc = GNUNET_malloc (sizeof (struct NotificationContext));
980 nc->include_friend_only = ntohl (nm->include_friend_only);
982 GNUNET_CONTAINER_DLL_insert (nc_head, nc_tail, nc);
983 GNUNET_SERVER_client_mark_monitor (client);
984 GNUNET_SERVER_notification_context_add (notify_list, client);
985 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &do_notify_entry, nc);
986 GNUNET_SERVER_receive_done (client, GNUNET_OK);
991 * Client disconnect callback
994 * @param client server client
996 static void disconnect_cb (void *cls,struct GNUNET_SERVER_Client *client)
998 struct NotificationContext *cur;
1000 for (cur = nc_head; NULL != cur; cur = cur->next)
1001 if (cur->client == client)
1007 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1013 * Release memory taken by a host entry.
1016 * @param key key of the host entry
1017 * @param value the 'struct HostEntry' to free
1018 * @return GNUNET_YES (continue to iterate)
1021 free_host_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
1023 struct HostEntry *he = value;
1025 GNUNET_free_non_null (he->hello);
1026 GNUNET_free_non_null (he->friend_only_hello);
1033 * Clean up our state. Called during shutdown.
1036 * @param tc scheduler task context, unused
1039 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1041 struct NotificationContext *cur;
1042 struct NotificationContext *next;
1043 GNUNET_SERVER_notification_context_destroy (notify_list);
1046 for (cur = nc_head; NULL != cur; cur = next)
1049 GNUNET_CONTAINER_DLL_remove (nc_head, nc_tail, cur);
1053 GNUNET_CONTAINER_multihashmap_iterate (hostmap, &free_host_entry, NULL);
1054 GNUNET_CONTAINER_multihashmap_destroy (hostmap);
1057 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1064 * Start up peerinfo service.
1066 * @param cls closure
1067 * @param server the initialized server
1068 * @param cfg configuration to use
1071 run (void *cls, struct GNUNET_SERVER_Handle *server,
1072 const struct GNUNET_CONFIGURATION_Handle *cfg)
1074 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1075 {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0},
1076 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET,
1077 sizeof (struct ListPeerMessage)},
1078 {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
1079 sizeof (struct ListAllPeersMessage)},
1080 {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
1081 sizeof (struct NotifyMessage)},
1086 struct DirScanContext dsc;
1089 hostmap = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1090 stats = GNUNET_STATISTICS_create ("peerinfo", cfg);
1091 notify_list = GNUNET_SERVER_notification_context_create (server, 0);
1092 noio = GNUNET_CONFIGURATION_get_value_yesno (cfg, "peerinfo", "NO_IO");
1093 if (GNUNET_YES != noio)
1095 GNUNET_assert (GNUNET_OK ==
1096 GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo",
1098 &networkIdDirectory));
1099 GNUNET_DISK_directory_create (networkIdDirectory);
1100 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1101 &cron_scan_directory_data_hosts, NULL);
1102 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1103 &cron_clean_data_hosts, NULL);
1104 ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1105 GNUNET_asprintf (&peerdir,
1109 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1110 _("Importing HELLOs from `%s'\n"),
1113 dsc.remove_files = GNUNET_NO;
1114 GNUNET_DISK_directory_scan (peerdir,
1115 &hosts_directory_scan_callback, &dsc);
1116 GNUNET_free (peerdir);
1118 GNUNET_SERVER_add_handlers (server, handlers);
1119 GNUNET_SERVER_disconnect_notify (server, &disconnect_cb, NULL) ;
1120 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1127 * The main function for the peerinfo service.
1129 * @param argc number of arguments from the command line
1130 * @param argv command line arguments
1131 * @return 0 ok, 1 on error
1134 main (int argc, char *const *argv)
1140 GNUNET_SERVICE_run (argc, argv, "peerinfo", GNUNET_SERVICE_OPTION_NONE,
1141 &run, NULL)) ? 0 : 1;
1142 GNUNET_free_non_null (networkIdDirectory);
1147 /* end of gnunet-service-peerinfo.c */