2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2010 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 hostlist/hostlist-client.c
23 * @brief hostlist support. Downloads HELLOs via HTTP.
24 * @author Christian Grothoff
25 * @author Matthias Wachs
29 #include "hostlist-client.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet-daemon-hostlist.h"
35 #include <curl/curl.h>
36 #include "gnunet_common.h"
37 #include "gnunet_bio_lib.h"
39 #define DEBUG_HOSTLIST_CLIENT GNUNET_NO
43 * Number of connections that we must have to NOT download
46 #define MIN_CONNECTIONS 4
49 * Interval between two advertised hostlist tests
51 #define TESTING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
54 * A single hostlist obtained by hostlist advertisements
59 * previous entry, used to manage entries in a double linked list
61 struct Hostlist * prev;
64 * next entry, used to manage entries in a double linked list
66 struct Hostlist * next;
69 * URI where hostlist can be obtained
71 const char *hostlist_uri;
74 * Value describing the quality of the hostlist, the bigger the better but (should) never < 0
75 * used for deciding which hostlist is replaced if MAX_NUMBER_HOSTLISTS in data structure is reached
76 * intial value = HOSTLIST_INITIAL
77 * increased every successful download by HOSTLIST_SUCCESSFULL_DOWNLOAD
78 * increased every successful download by number of obtained HELLO messages
79 * decreased every failed download by HOSTLIST_SUCCESSFULL_DOWNLOAD
84 * Time the hostlist advertisement was recieved and the entry was created
86 struct GNUNET_TIME_Absolute time_creation;
89 * Last time the hostlist was obtained
91 struct GNUNET_TIME_Absolute time_last_usage;
94 * Number of HELLO messages obtained during last download
99 * Number of times the hostlist was successfully obtained
109 static const struct GNUNET_CONFIGURATION_Handle *cfg;
114 static struct GNUNET_SCHEDULER_Handle *sched;
119 static struct GNUNET_STATISTICS_Handle *stats;
124 static struct GNUNET_TRANSPORT_Handle *transport;
127 * Proxy that we are using (can be NULL).
132 * Number of bytes valid in 'download_buffer'.
134 static size_t download_pos;
137 * Current URL that we are using.
139 static char *current_url;
142 * Current CURL handle.
147 * Current multi-CURL handle.
152 * How many bytes did we download from the current hostlist URL?
154 static uint32_t stat_bytes_downloaded;
156 * Amount of time we wait between hostlist downloads.
158 static struct GNUNET_TIME_Relative hostlist_delay;
161 * ID of the task, checking if hostlist download should take plate
163 static GNUNET_SCHEDULER_TaskIdentifier ti_check_download;
166 * ID of the task downloading the hostlist
168 static GNUNET_SCHEDULER_TaskIdentifier ti_download;
171 * ID of the task saving the hostlsit in a regular intervall
173 static GNUNET_SCHEDULER_TaskIdentifier ti_saving_task;
176 * ID of the task called to initiate a download
178 static GNUNET_SCHEDULER_TaskIdentifier ti_download_dispatcher_task;
181 * ID of the task controlling the locking between two hostlist tests
183 static GNUNET_SCHEDULER_TaskIdentifier ti_testing_intervall_task;
186 * At what time MUST the current hostlist request be done?
188 static struct GNUNET_TIME_Absolute end_time;
191 * Head of the linked list used to store hostlists
193 static struct Hostlist * linked_list_head;
196 * Tail of the linked list used to store hostlists
198 static struct Hostlist * linked_list_tail;
201 * Current hostlist used for downloading
203 static struct Hostlist * current_hostlist;
206 * Size of the linke list used to store hostlists
208 static unsigned int linked_list_size;
211 * Head of the linked list used to store hostlists
213 static struct Hostlist * hostlist_to_test;
216 * Set to GNUNET_YES if the current URL had some problems.
218 static int stat_bogus_url;
221 * Value controlling if a hostlist is tested at the moment
223 static int stat_testing_hostlist;
226 * Value controlling if a hostlist testing is allowed at the moment
228 static int stat_testing_allowed;
231 * Value controlling if a hostlist download is running at the moment
233 static int stat_download_in_progress;
236 * Value saying if a preconfigured bootstrap server is used
238 static unsigned int stat_use_bootstrap;
240 * Set if we are allowed to learn new hostlists and use them
242 static int stat_learning;
245 * Value saying if hostlist download was successful
247 static unsigned int stat_download_successful;
250 * Value saying how many valid HELLO messages were obtained during download
252 static unsigned int stat_hellos_obtained;
255 * Number of active connections (according to core service).
257 static unsigned int stat_connection_count;
261 * Process downloaded bits by calling callback on each HELLO.
263 * @param ptr buffer with downloaded data
264 * @param size size of a record
265 * @param nmemb number of records downloaded
267 * @return number of bytes that were processed (always size*nmemb)
270 callback_download (void *ptr,
275 static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
276 const char * cbuf = ptr;
277 const struct GNUNET_MessageHeader *msg;
283 total = size * nmemb;
284 stat_bytes_downloaded += total;
285 if ( (total == 0) || (stat_bogus_url) )
287 return total; /* ok, no data or bogus data */
290 GNUNET_STATISTICS_update (stats,
291 gettext_noop ("# bytes downloaded from hostlist servers"),
295 while ( (left > 0) ||
298 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - download_pos);
299 memcpy (&download_buffer[download_pos],
305 if (download_pos < sizeof(struct GNUNET_MessageHeader))
307 GNUNET_assert (left == 0);
310 msg = (const struct GNUNET_MessageHeader *) download_buffer;
311 msize = ntohs(msg->size);
312 if (msize < sizeof(struct GNUNET_MessageHeader))
314 GNUNET_STATISTICS_update (stats,
315 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
318 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
319 _("Invalid `%s' message received from hostlist at `%s'\n"),
322 stat_hellos_obtained++;
326 if (download_pos < msize)
328 GNUNET_assert (left == 0);
331 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
333 #if DEBUG_HOSTLIST_CLIENT
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Received valid `%s' message from hostlist server.\n",
338 GNUNET_STATISTICS_update (stats,
339 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
342 stat_hellos_obtained++;
343 GNUNET_TRANSPORT_offer_hello (transport, msg);
347 GNUNET_STATISTICS_update (stats,
348 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
352 _("Invalid `%s' message received from hostlist at `%s'\n"),
355 stat_bogus_url = GNUNET_YES;
356 stat_hellos_obtained++;
359 memmove (download_buffer,
360 &download_buffer[msize],
361 download_pos - msize);
362 download_pos -= msize;
369 * Obtain a hostlist URL that we should use.
371 * @return NULL if there is no URL available
374 get_bootstrap_server ()
382 GNUNET_CONFIGURATION_get_value_string (cfg,
387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
388 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
389 "SERVERS", "HOSTLIST");
394 if (strlen (servers) > 0)
397 pos = strlen (servers) - 1;
400 if (servers[pos] == ' ')
407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
408 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
409 "SERVERS", "HOSTLIST");
410 GNUNET_free (servers);
414 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
415 pos = strlen (servers) - 1;
418 if (servers[pos] == ' ')
430 ret = GNUNET_strdup (&servers[pos]);
431 GNUNET_free (servers);
436 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
437 * @return uri to use, NULL if there is no URL available
443 unsigned int counter;
444 struct Hostlist * pos;
446 if ( GNUNET_NO == stat_learning)
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Using preconfigured bootstrap server\n");
450 current_hostlist = NULL;
451 return get_bootstrap_server();
454 if ( ( GNUNET_YES == stat_testing_hostlist) && (NULL != hostlist_to_test) )
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Testing new advertised hostlist if it is obtainable\n");
458 current_hostlist = hostlist_to_test;
459 return strdup(hostlist_to_test->hostlist_uri);
462 if ( (GNUNET_YES == stat_use_bootstrap) ||
463 (linked_list_size == 0) )
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Using preconfigured bootstrap server\n");
467 current_hostlist = NULL;
468 return get_bootstrap_server();
470 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
472 pos = linked_list_head;
473 while ( counter < index )
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Using learned hostlist `%s'\n", pos->hostlist_uri);
480 current_hostlist = pos;
481 return strdup(pos->hostlist_uri);
485 #define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt(c, a, b); if (ret != CURLE_OK) GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret)); } while (0)
488 * Method to save hostlist to a file during hostlist client shutdown
489 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
491 static void save_hostlist_file ( int shutdown );
494 * add val2 to val1 with overflow check
495 * @param val1 value 1
496 * @param val2 value 2
499 static uint64_t checked_add (uint64_t val1, uint64_t val2)
501 static uint64_t temp;
502 static uint64_t maxv;
515 * Subtract val2 from val1 with underflow check
516 * @param val1 value 1
517 * @param val2 value 2
520 static uint64_t checked_sub (uint64_t val1, uint64_t val2)
529 * Method to check if a URI is in hostlist linked list
530 * @param uri uri to check
531 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
534 linked_list_contains (const char * uri)
536 struct Hostlist * pos;
538 pos = linked_list_head;
541 if (0 == strcmp(pos->hostlist_uri, uri) )
550 * Method returning the hostlist element with the lowest quality in the datastore
551 * @return hostlist with lowest quality
553 static struct Hostlist *
554 linked_list_get_lowest_quality ( )
556 struct Hostlist * pos;
557 struct Hostlist * lowest;
559 if (linked_list_size == 0)
561 lowest = linked_list_head;
562 pos = linked_list_head->next;
565 if (pos->quality < lowest->quality)
574 * Method to insert a hostlist into the datastore. If datastore contains maximum number of elements, the elements with lowest quality is dismissed
579 struct Hostlist * lowest_quality;
581 if (MAX_NUMBER_HOSTLISTS <= linked_list_size)
583 /* No free entries available, replace existing entry */
584 lowest_quality = linked_list_get_lowest_quality();
585 GNUNET_assert (lowest_quality != NULL);
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Removing hostlist with URI `%s' which has the worst quality of all (%llu)\n",
588 lowest_quality->hostlist_uri,
589 (unsigned long long) lowest_quality->quality);
590 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
592 GNUNET_free (lowest_quality);
594 GNUNET_CONTAINER_DLL_insert(linked_list_head,
598 GNUNET_STATISTICS_set (stats,
599 gettext_noop("# advertised hostlist URIs"),
602 stat_testing_hostlist = GNUNET_NO;
607 * Method updating hostlist statistics
609 static void update_hostlist ( )
612 if ( ((stat_use_bootstrap == GNUNET_NO) && ( NULL != current_hostlist )) ||
613 ((stat_testing_hostlist == GNUNET_YES) && ( NULL != current_hostlist )) )
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
616 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
617 current_hostlist->hello_count = stat_hellos_obtained;
618 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
619 current_hostlist->quality = checked_add ( current_hostlist->quality, (stat_hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
620 if ( GNUNET_YES == stat_download_successful )
622 current_hostlist->times_used++;
623 current_hostlist->quality = checked_add ( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
624 GNUNET_asprintf (&stat,
625 gettext_noop("# advertised URI `%s' downloaded"),
626 current_hostlist->hostlist_uri);
628 GNUNET_STATISTICS_update ( stats,
635 current_hostlist->quality = checked_sub ( current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD );
637 current_hostlist = NULL;
638 /* Alternating the usage of preconfigured and learned hostlists */
640 if (stat_testing_hostlist == GNUNET_YES)
643 if ( GNUNET_YES == stat_learning)
645 if (stat_use_bootstrap == GNUNET_YES)
646 stat_use_bootstrap = GNUNET_NO;
648 stat_use_bootstrap = GNUNET_YES;
651 stat_use_bootstrap = GNUNET_YES;
655 * Clean up the state from the task that downloaded the
656 * hostlist and schedule the next task.
663 if ( ( stat_testing_hostlist == GNUNET_YES ) && ( GNUNET_NO == stat_download_successful) && (NULL != hostlist_to_test))
665 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
666 _("Advertised hostlist with URI `%s' could not be downloaded. Advertised URI gets dismissed.\n"),hostlist_to_test->hostlist_uri);
669 if ( stat_testing_hostlist == GNUNET_YES )
671 stat_testing_hostlist = GNUNET_NO;
673 if ( NULL != hostlist_to_test)
675 GNUNET_free (hostlist_to_test);
676 hostlist_to_test = NULL;
681 mret = curl_multi_remove_handle (multi, curl);
682 if (mret != CURLM_OK)
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685 _("%s failed at %s:%d: `%s'\n"),
686 "curl_multi_remove_handle", __FILE__, __LINE__,
687 curl_multi_strerror (mret));
689 mret = curl_multi_cleanup (multi);
690 if (mret != CURLM_OK)
691 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
692 _("%s failed at %s:%d: `%s'\n"),
693 "curl_multi_cleanup", __FILE__, __LINE__,
694 curl_multi_strerror (mret));
699 curl_easy_cleanup (curl);
702 GNUNET_free_non_null (current_url);
704 stat_bytes_downloaded = 0;
705 stat_download_in_progress = GNUNET_NO;
709 * Task that is run when we are ready to receive more data from the hostlist
712 * @param cls closure, unused
713 * @param tc task context, unused
716 task_download (void *cls,
717 const struct GNUNET_SCHEDULER_TaskContext *tc);
720 * Ask CURL for the select set and then schedule the
721 * receiving task with the scheduler.
731 struct GNUNET_NETWORK_FDSet *grs;
732 struct GNUNET_NETWORK_FDSet *gws;
734 struct GNUNET_TIME_Relative rtime;
740 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
741 if (mret != CURLM_OK)
743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
744 _("%s failed at %s:%d: `%s'\n"),
745 "curl_multi_fdset", __FILE__, __LINE__,
746 curl_multi_strerror (mret));
750 mret = curl_multi_timeout (multi, &timeout);
751 if (mret != CURLM_OK)
753 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
754 _("%s failed at %s:%d: `%s'\n"),
755 "curl_multi_timeout", __FILE__, __LINE__,
756 curl_multi_strerror (mret));
760 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
761 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
763 grs = GNUNET_NETWORK_fdset_create ();
764 gws = GNUNET_NETWORK_fdset_create ();
765 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
766 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
767 #if DEBUG_HOSTLIST_CLIENT
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Scheduling task for hostlist download using cURL\n");
772 = GNUNET_SCHEDULER_add_select (sched,
773 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
774 GNUNET_SCHEDULER_NO_TASK,
780 GNUNET_NETWORK_fdset_destroy (gws);
781 GNUNET_NETWORK_fdset_destroy (grs);
786 * Task that is run when we are ready to receive more data from the hostlist
789 * @param cls closure, unused
790 * @param tc task context, unused
793 task_download (void *cls,
794 const struct GNUNET_SCHEDULER_TaskContext *tc)
801 ti_download = GNUNET_SCHEDULER_NO_TASK;
802 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
804 #if DEBUG_HOSTLIST_CLIENT
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Shutdown requested while trying to download hostlist from `%s'\n",
813 if (GNUNET_TIME_absolute_get_remaining (end_time).rel_value == 0)
815 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
816 _("Timeout trying to download hostlist from `%s'\n"),
822 #if DEBUG_HOSTLIST_CLIENT
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824 "Ready for processing hostlist client request\n");
830 if (stat_bytes_downloaded > MAX_BYTES_PER_HOSTLISTS)
832 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
833 _("Download limit of %u bytes exceeded, stopping download\n"),MAX_BYTES_PER_HOSTLISTS);
837 mret = curl_multi_perform (multi, &running);
844 msg = curl_multi_info_read (multi, &running);
845 GNUNET_break (msg != NULL);
851 if ( (msg->data.result != CURLE_OK) &&
852 (msg->data.result != CURLE_GOT_NOTHING) )
853 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
854 _("%s failed for `%s' at %s:%d: `%s'\n"),
855 "curl_multi_perform",
859 curl_easy_strerror (msg->data.result));
862 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
863 _("Download of hostlist `%s' completed.\n"),
865 stat_download_successful = GNUNET_YES;
867 if (GNUNET_YES == stat_testing_hostlist)
869 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
870 _("Adding successfully tested hostlist `%s' datastore.\n"),current_url);
872 hostlist_to_test = NULL;
873 stat_testing_hostlist = GNUNET_NO;
883 while ( (running > 0) );
886 while (mret == CURLM_CALL_MULTI_PERFORM);
888 if (mret != CURLM_OK)
890 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
891 _("%s failed at %s:%d: `%s'\n"),
892 "curl_multi_perform", __FILE__, __LINE__,
893 curl_multi_strerror (mret));
901 * Main function that will download a hostlist and process its
911 current_url = download_get_url ();
912 if (current_url == NULL)
914 curl = curl_easy_init ();
922 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
923 _("Bootstrapping using hostlist at `%s'.\n"),
926 stat_download_in_progress = GNUNET_YES;
927 stat_download_successful = GNUNET_NO;
928 stat_hellos_obtained = 0;
929 stat_bytes_downloaded = 0;
931 GNUNET_STATISTICS_update (stats,
932 gettext_noop ("# hostlist downloads initiated"),
936 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
939 CURL_EASY_SETOPT (curl,
940 CURLOPT_WRITEFUNCTION,
947 CURL_EASY_SETOPT (curl,
955 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
956 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
957 /* no need to abort if the above failed */
958 CURL_EASY_SETOPT (curl,
966 CURL_EASY_SETOPT (curl,
970 CURL_EASY_SETOPT (curl,
974 CURL_EASY_SETOPT (curl,
976 GNUNET_SERVER_MAX_MESSAGE_SIZE);
977 if (0 == strncmp (current_url, "http", 4))
978 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
979 CURL_EASY_SETOPT (curl,
980 CURLOPT_CONNECTTIMEOUT,
982 CURL_EASY_SETOPT (curl,
986 /* this should no longer be needed; we're now single-threaded! */
987 CURL_EASY_SETOPT (curl,
991 multi = curl_multi_init ();
998 mret = curl_multi_add_handle (multi, curl);
999 if (mret != CURLM_OK)
1001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1002 _("%s failed at %s:%d: `%s'\n"),
1003 "curl_multi_add_handle", __FILE__, __LINE__,
1004 curl_multi_strerror (mret));
1005 mret = curl_multi_cleanup (multi);
1006 if (mret != CURLM_OK)
1007 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1008 _("%s failed at %s:%d: `%s'\n"),
1009 "curl_multi_cleanup", __FILE__, __LINE__,
1010 curl_multi_strerror (mret));
1015 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
1016 download_prepare ();
1021 task_download_dispatcher (void *cls,
1022 const struct GNUNET_SCHEDULER_TaskContext *tc)
1024 ti_download_dispatcher_task = GNUNET_SCHEDULER_NO_TASK;
1025 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Download is initiated...\n");
1029 if ( GNUNET_NO == stat_download_in_progress )
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Download can start immediately...\n");
1033 download_hostlist();
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "Download in progess, have to wait...\n");
1039 ti_download_dispatcher_task = GNUNET_SCHEDULER_add_delayed (sched,
1041 &task_download_dispatcher,
1047 * Task that checks if we should try to download a hostlist.
1048 * If so, we initiate the download, otherwise we schedule
1049 * this task again for a later time.
1052 task_check (void *cls,
1053 const struct GNUNET_SCHEDULER_TaskContext *tc)
1056 struct GNUNET_TIME_Relative delay;
1058 ti_check_download = GNUNET_SCHEDULER_NO_TASK;
1059 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1062 if (stat_connection_count < MIN_CONNECTIONS)
1064 ti_download_dispatcher_task = GNUNET_SCHEDULER_add_now ( sched,
1065 &task_download_dispatcher,
1071 curl_global_cleanup ();
1072 return; /* in shutdown */
1074 delay = hostlist_delay;
1075 if (hostlist_delay.rel_value == 0)
1076 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
1078 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
1079 if (hostlist_delay.rel_value > GNUNET_TIME_UNIT_HOURS.rel_value * (1 + stat_connection_count))
1080 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
1081 (1 + stat_connection_count));
1082 GNUNET_STATISTICS_set (stats,
1083 gettext_noop("# milliseconds between hostlist downloads"),
1084 hostlist_delay.rel_value,
1088 delay = GNUNET_TIME_UNIT_ZERO;
1091 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1092 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
1093 stat_connection_count,
1095 (unsigned long long) delay.rel_value);
1096 ti_check_download = GNUNET_SCHEDULER_add_delayed (sched,
1103 * This tasks sets hostlist testing to allowed after intervall between to testings is reached
1108 task_testing_intervall_reset (void *cls,
1109 const struct GNUNET_SCHEDULER_TaskContext *tc)
1111 ti_testing_intervall_task = GNUNET_SCHEDULER_NO_TASK;
1112 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1114 stat_testing_allowed = GNUNET_OK;
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "Testing new hostlist advertisements is allowed again\n");
1121 * Task that writes hostlist entries to a file on a regular base
1126 task_hostlist_saving (void *cls,
1127 const struct GNUNET_SCHEDULER_TaskContext *tc)
1129 ti_saving_task = GNUNET_SCHEDULER_NO_TASK;
1130 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1132 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1133 _("Scheduled saving of hostlists\n"));
1134 save_hostlist_file ( GNUNET_NO );
1136 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1137 _("Hostlists will be saved to file again in %llums\n"),
1138 (unsigned long long) SAVING_INTERVALL.rel_value);
1139 ti_saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1141 &task_hostlist_saving,
1146 * Method called whenever a given peer connects.
1148 * @param cls closure
1149 * @param peer peer identity this notification is about
1150 * @param latency reported latency of the connection with 'other'
1151 * @param distance reported distance (DV) to 'other'
1154 handler_connect (void *cls,
1156 GNUNET_PeerIdentity * peer,
1157 struct GNUNET_TIME_Relative latency,
1160 GNUNET_assert (stat_connection_count < UINT_MAX);
1161 stat_connection_count++;
1162 GNUNET_STATISTICS_update (stats,
1163 gettext_noop ("# active connections"),
1170 * Method called whenever a given peer disconnects.
1172 * @param cls closure
1173 * @param peer peer identity this notification is about
1176 handler_disconnect (void *cls,
1178 GNUNET_PeerIdentity * peer)
1180 /* GNUNET_assert (stat_connection_count > 0); */
1181 if (stat_connection_count == 0)
1183 GNUNET_break_op (0);
1187 stat_connection_count--;
1188 GNUNET_STATISTICS_update (stats,
1189 gettext_noop ("# active connections"),
1195 * Method called whenever an advertisement message arrives.
1197 * @param cls closure (always NULL)
1198 * @param peer the peer sending the message
1199 * @param message the actual message
1200 * @param latency latency
1201 * @param distance distance
1202 * @return GNUNET_OK to keep the connection open,
1203 * GNUNET_SYSERR to close it (signal serious error)
1206 handler_advertisement (void *cls,
1207 const struct GNUNET_PeerIdentity * peer,
1208 const struct GNUNET_MessageHeader * message,
1209 struct GNUNET_TIME_Relative latency,
1214 const struct GNUNET_MessageHeader * incoming;
1216 struct Hostlist * hostlist;
1218 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1219 size = ntohs (message->size);
1220 if (size <= sizeof(struct GNUNET_MessageHeader))
1222 GNUNET_break_op (0);
1223 return GNUNET_SYSERR;
1225 incoming = (const struct GNUNET_MessageHeader *) message;
1226 uri = (const char*) &incoming[1];
1227 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1228 if (uri [uri_size - 1] != '\0')
1230 GNUNET_break_op (0);
1231 return GNUNET_SYSERR;
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1237 if (GNUNET_NO != linked_list_contains (uri))
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1240 "URI `%s' is already known\n",
1245 if ( GNUNET_NO == stat_testing_allowed )
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Currently not accepting new advertisements: interval between to advertisements is not reached\n");
1249 return GNUNET_SYSERR;
1251 if ( GNUNET_YES == stat_testing_hostlist )
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Currently not accepting new advertisements: we are already testing a hostlist\n");
1255 return GNUNET_SYSERR;
1258 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1259 hostlist->hostlist_uri = (const char*) &hostlist[1];
1260 memcpy (&hostlist[1], uri, uri_size);
1261 hostlist->time_creation = GNUNET_TIME_absolute_get();
1262 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1263 hostlist->quality = HOSTLIST_INITIAL;
1264 hostlist_to_test = hostlist;
1266 stat_testing_hostlist = GNUNET_YES;
1267 stat_testing_allowed = GNUNET_NO;
1268 ti_testing_intervall_task = GNUNET_SCHEDULER_add_delayed (sched,
1270 &task_testing_intervall_reset,
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 "Testing new hostlist advertisements is locked for the next %u ms\n",
1275 TESTING_INTERVAL.rel_value);
1277 ti_download_dispatcher_task = GNUNET_SCHEDULER_add_now (sched,
1278 &task_download_dispatcher,
1287 * Continuation called by the statistics code once
1288 * we go the stat. Initiates hostlist download scheduling.
1290 * @param cls closure
1291 * @param success GNUNET_OK if statistics were
1292 * successfully obtained, GNUNET_SYSERR if not.
1295 primary_task (void *cls, int success)
1298 return; /* in shutdown */
1299 #if DEBUG_HOSTLIST_CLIENT
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "Statistics request done, scheduling hostlist download\n");
1303 ti_check_download = GNUNET_SCHEDULER_add_now (sched,
1310 process_stat (void *cls,
1311 const char *subsystem,
1316 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1317 _("Initial time between hostlist downloads is %llums\n"),
1318 (unsigned long long) value);
1319 hostlist_delay.rel_value = value;
1324 * Method to load persistent hostlist file during hostlist client startup
1327 load_hostlist_file ()
1332 struct Hostlist * hostlist;
1334 uint32_t times_used;
1335 uint32_t hellos_returned;
1342 GNUNET_CONFIGURATION_get_value_filename (cfg,
1347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1348 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1349 "HOSTLISTFILE", "HOSTLIST");
1353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1354 _("Loading saved hostlist entries from file `%s' \n"), filename);
1355 if ( GNUNET_NO == GNUNET_DISK_file_test (filename) )
1357 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1358 _("Hostlist file `%s' is not existing\n"), filename);
1359 GNUNET_free ( filename );
1363 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1366 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1367 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1370 GNUNET_free (filename);
1375 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1377 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1378 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1379 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1380 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1381 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1383 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1384 hostlist->hello_count = hellos_returned;
1385 hostlist->hostlist_uri = (const char *) &hostlist[1];
1386 memcpy (&hostlist[1], uri, strlen(uri)+1);
1387 hostlist->quality = quality;
1388 hostlist->time_creation.abs_value = created;
1389 hostlist->time_last_usage.abs_value = last_used;
1390 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1397 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1400 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1401 _("%u hostlist URIs loaded from file\n"), counter);
1402 GNUNET_STATISTICS_set (stats,
1403 gettext_noop("# hostlist URIs read from file"),
1406 GNUNET_STATISTICS_set (stats,
1407 gettext_noop("# advertised hostlist URIs"),
1411 GNUNET_free_non_null (uri);
1413 GNUNET_BIO_read_close (rh, &emsg);
1416 GNUNET_free (filename);
1421 * Method to save persistent hostlist file during hostlist client shutdown
1422 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1424 static void save_hostlist_file ( int shutdown )
1427 struct Hostlist *pos;
1428 struct GNUNET_BIO_WriteHandle * wh;
1433 GNUNET_CONFIGURATION_get_value_filename (cfg,
1438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1439 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1440 "HOSTLISTFILE", "HOSTLIST");
1443 wh = GNUNET_BIO_write_open (filename);
1446 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1447 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1450 GNUNET_free (filename);
1453 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1454 _("Writing %u hostlist URIs to `%s'\n" ),
1455 linked_list_size, filename);
1457 /* add code to write hostlists to file using bio */
1460 while (NULL != (pos = linked_list_head))
1462 if ( GNUNET_YES == shutdown)
1464 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1467 if (GNUNET_YES == ok)
1470 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1472 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1474 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1476 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.abs_value)) ||
1478 GNUNET_BIO_write_int64 (wh, pos->time_creation.abs_value)) ||
1480 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1483 _("Error writing hostlist URIs to file `%s'\n"),
1489 if ( GNUNET_YES == shutdown)
1492 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1494 GNUNET_STATISTICS_set (stats,
1495 gettext_noop("# hostlist URIs written to file"),
1499 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1501 _("Error writing hostlist URIs to file `%s'\n"),
1503 GNUNET_free (filename);
1507 * Start downloading hostlists from hostlist servers as necessary.
1510 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1511 struct GNUNET_SCHEDULER_Handle *s,
1512 struct GNUNET_STATISTICS_Handle *st,
1513 GNUNET_CORE_ConnectEventHandler *ch,
1514 GNUNET_CORE_DisconnectEventHandler *dh,
1515 GNUNET_CORE_MessageCallback *msgh,
1521 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1524 return GNUNET_SYSERR;
1526 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL, NULL);
1527 if (NULL == transport)
1529 curl_global_cleanup ();
1530 return GNUNET_SYSERR;
1536 GNUNET_CONFIGURATION_get_value_string (cfg,
1541 stat_learning = learn;
1542 *ch = &handler_connect;
1543 *dh = &handler_disconnect;
1544 linked_list_head = NULL;
1545 linked_list_tail = NULL;
1546 stat_use_bootstrap = GNUNET_YES;
1547 stat_testing_hostlist = GNUNET_NO;
1548 stat_testing_allowed = GNUNET_YES;
1550 if ( GNUNET_YES == stat_learning )
1552 *msgh = &handler_advertisement;
1553 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1554 _("Learning is enabled on this peer\n"));
1555 load_hostlist_file ();
1556 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1557 _("Hostlists will be saved to file again in %llums\n"),
1558 (unsigned long long) SAVING_INTERVALL.rel_value);
1559 ti_saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1561 &task_hostlist_saving,
1566 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1567 _("Learning is not enabled on this peer\n"));
1569 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
1574 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1576 result = remove (filename);
1578 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1579 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),filename);
1581 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1582 _("Hostlist file `%s' could not be removed\n"),filename);
1585 GNUNET_free ( filename );
1587 GNUNET_STATISTICS_get (stats,
1589 gettext_noop("# milliseconds between hostlist downloads"),
1590 GNUNET_TIME_UNIT_MINUTES,
1599 * Stop downloading hostlists from hostlist servers as necessary.
1602 GNUNET_HOSTLIST_client_stop ()
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1605 "Hostlist client shutdown\n");
1606 #if DEBUG_HOSTLIST_CLIENT
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Hostlist client shutdown\n");
1610 if ( GNUNET_YES == stat_learning )
1611 save_hostlist_file ( GNUNET_YES );
1613 if (ti_saving_task != GNUNET_SCHEDULER_NO_TASK)
1615 GNUNET_SCHEDULER_cancel (sched,
1619 if (ti_download_dispatcher_task != GNUNET_SCHEDULER_NO_TASK)
1621 GNUNET_SCHEDULER_cancel (sched,
1622 ti_download_dispatcher_task);
1624 if (ti_testing_intervall_task != GNUNET_SCHEDULER_NO_TASK)
1626 GNUNET_SCHEDULER_cancel (sched,
1627 ti_testing_intervall_task);
1629 if (ti_download != GNUNET_SCHEDULER_NO_TASK)
1631 GNUNET_SCHEDULER_cancel (sched,
1634 if (ti_check_download != GNUNET_SCHEDULER_NO_TASK)
1636 GNUNET_SCHEDULER_cancel (sched,
1638 curl_global_cleanup ();
1640 if (transport != NULL)
1642 GNUNET_TRANSPORT_disconnect (transport);
1645 GNUNET_assert (NULL == transport);
1646 GNUNET_free_non_null (proxy);
1652 /* end of hostlist-client.c */