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).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.value == 0)
1076 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
1078 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
1079 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.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.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.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.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 unsigned int max = 0 ;
1163 if (stat_connection_count < max)
1165 stat_connection_count++;
1166 GNUNET_STATISTICS_update (stats,
1167 gettext_noop ("# active connections"),
1175 * Method called whenever a given peer disconnects.
1177 * @param cls closure
1178 * @param peer peer identity this notification is about
1181 handler_disconnect (void *cls,
1183 GNUNET_PeerIdentity * peer)
1185 GNUNET_assert (stat_connection_count > 0);
1186 stat_connection_count--;
1187 GNUNET_STATISTICS_update (stats,
1188 gettext_noop ("# active connections"),
1194 * Method called whenever an advertisement message arrives.
1196 * @param cls closure (always NULL)
1197 * @param peer the peer sending the message
1198 * @param message the actual message
1199 * @param latency latency
1200 * @param distance distance
1201 * @return GNUNET_OK to keep the connection open,
1202 * GNUNET_SYSERR to close it (signal serious error)
1205 handler_advertisement (void *cls,
1206 const struct GNUNET_PeerIdentity * peer,
1207 const struct GNUNET_MessageHeader * message,
1208 struct GNUNET_TIME_Relative latency,
1213 const struct GNUNET_MessageHeader * incoming;
1215 struct Hostlist * hostlist;
1217 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1218 size = ntohs (message->size);
1219 if (size <= sizeof(struct GNUNET_MessageHeader))
1221 GNUNET_break_op (0);
1222 return GNUNET_SYSERR;
1224 incoming = (const struct GNUNET_MessageHeader *) message;
1225 uri = (const char*) &incoming[1];
1226 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1227 if (uri [uri_size - 1] != '\0')
1229 GNUNET_break_op (0);
1230 return GNUNET_SYSERR;
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1236 if (GNUNET_NO != linked_list_contains (uri))
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "URI `%s' is already known\n",
1244 if ( GNUNET_NO == stat_testing_allowed )
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "Currently not accepting new advertisements: interval between to advertisements is not reached\n");
1248 return GNUNET_SYSERR;
1250 if ( GNUNET_YES == stat_testing_hostlist )
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Currently not accepting new advertisements: we are already testing a hostlist\n");
1254 return GNUNET_SYSERR;
1257 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1258 hostlist->hostlist_uri = (const char*) &hostlist[1];
1259 memcpy (&hostlist[1], uri, uri_size);
1260 hostlist->time_creation = GNUNET_TIME_absolute_get();
1261 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1262 hostlist->quality = HOSTLIST_INITIAL;
1263 hostlist_to_test = hostlist;
1265 stat_testing_hostlist = GNUNET_YES;
1266 stat_testing_allowed = GNUNET_NO;
1267 ti_testing_intervall_task = GNUNET_SCHEDULER_add_delayed (sched,
1269 &task_testing_intervall_reset,
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Testing new hostlist advertisements is locked for the next %u ms\n",
1274 TESTING_INTERVAL.value);
1276 ti_download_dispatcher_task = GNUNET_SCHEDULER_add_now (sched,
1277 &task_download_dispatcher,
1286 * Continuation called by the statistics code once
1287 * we go the stat. Initiates hostlist download scheduling.
1289 * @param cls closure
1290 * @param success GNUNET_OK if statistics were
1291 * successfully obtained, GNUNET_SYSERR if not.
1294 primary_task (void *cls, int success)
1297 return; /* in shutdown */
1298 #if DEBUG_HOSTLIST_CLIENT
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "Statistics request done, scheduling hostlist download\n");
1302 ti_check_download = GNUNET_SCHEDULER_add_now (sched,
1309 process_stat (void *cls,
1310 const char *subsystem,
1315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1316 _("Initial time between hostlist downloads is %llums\n"),
1317 (unsigned long long) value);
1318 hostlist_delay.value = value;
1323 * Method to load persistent hostlist file during hostlist client startup
1326 load_hostlist_file ()
1331 struct Hostlist * hostlist;
1333 uint32_t times_used;
1334 uint32_t hellos_returned;
1341 GNUNET_CONFIGURATION_get_value_filename (cfg,
1346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1347 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1348 "HOSTLISTFILE", "HOSTLIST");
1352 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1353 _("Loading saved hostlist entries from file `%s' \n"), filename);
1354 if ( GNUNET_NO == GNUNET_DISK_file_test (filename) )
1356 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1357 _("Hostlist file `%s' is not existing\n"), filename);
1358 GNUNET_free ( filename );
1362 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1365 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1366 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1369 GNUNET_free (filename);
1374 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1376 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1377 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1378 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1379 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1380 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1382 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1383 hostlist->hello_count = hellos_returned;
1384 hostlist->hostlist_uri = (const char *) &hostlist[1];
1385 memcpy (&hostlist[1], uri, strlen(uri)+1);
1386 hostlist->quality = quality;
1387 hostlist->time_creation.value = created;
1388 hostlist->time_last_usage.value = last_used;
1389 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1396 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1399 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1400 _("%u hostlist URIs loaded from file\n"), counter);
1401 GNUNET_STATISTICS_set (stats,
1402 gettext_noop("# hostlist URIs read from file"),
1405 GNUNET_STATISTICS_set (stats,
1406 gettext_noop("# advertised hostlist URIs"),
1410 GNUNET_free_non_null (uri);
1412 GNUNET_BIO_read_close (rh, &emsg);
1415 GNUNET_free (filename);
1420 * Method to save persistent hostlist file during hostlist client shutdown
1421 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1423 static void save_hostlist_file ( int shutdown )
1426 struct Hostlist *pos;
1427 struct GNUNET_BIO_WriteHandle * wh;
1432 GNUNET_CONFIGURATION_get_value_filename (cfg,
1437 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1438 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1439 "HOSTLISTFILE", "HOSTLIST");
1442 wh = GNUNET_BIO_write_open (filename);
1445 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1446 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1449 GNUNET_free (filename);
1452 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1453 _("Writing %u hostlist URIs to `%s'\n" ),
1454 linked_list_size, filename);
1456 /* add code to write hostlists to file using bio */
1459 while (NULL != (pos = linked_list_head))
1461 if ( GNUNET_YES == shutdown)
1463 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1466 if (GNUNET_YES == ok)
1469 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1471 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1473 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1475 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1477 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1479 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1482 _("Error writing hostlist URIs to file `%s'\n"),
1488 if ( GNUNET_YES == shutdown)
1491 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1493 GNUNET_STATISTICS_set (stats,
1494 gettext_noop("# hostlist URIs written to file"),
1498 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1500 _("Error writing hostlist URIs to file `%s'\n"),
1502 GNUNET_free (filename);
1506 * Start downloading hostlists from hostlist servers as necessary.
1509 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1510 struct GNUNET_SCHEDULER_Handle *s,
1511 struct GNUNET_STATISTICS_Handle *st,
1512 GNUNET_CORE_ConnectEventHandler *ch,
1513 GNUNET_CORE_DisconnectEventHandler *dh,
1514 GNUNET_CORE_MessageCallback *msgh,
1520 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1523 return GNUNET_SYSERR;
1525 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL, NULL);
1526 if (NULL == transport)
1528 curl_global_cleanup ();
1529 return GNUNET_SYSERR;
1535 GNUNET_CONFIGURATION_get_value_string (cfg,
1540 stat_learning = learn;
1541 *ch = &handler_connect;
1542 *dh = &handler_disconnect;
1543 linked_list_head = NULL;
1544 linked_list_tail = NULL;
1545 stat_use_bootstrap = GNUNET_YES;
1546 stat_testing_hostlist = GNUNET_NO;
1547 stat_testing_allowed = GNUNET_YES;
1549 if ( GNUNET_YES == stat_learning )
1551 *msgh = &handler_advertisement;
1552 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1553 _("Learning is enabled on this peer\n"));
1554 load_hostlist_file ();
1555 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1556 _("Hostlists will be saved to file again in %llums\n"),
1557 (unsigned long long) SAVING_INTERVALL.value);
1558 ti_saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1560 &task_hostlist_saving,
1565 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1566 _("Learning is not enabled on this peer\n"));
1568 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
1573 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1575 result = remove (filename);
1577 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1578 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),filename);
1580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1581 _("Hostlist file `%s' could not be removed\n"),filename);
1584 GNUNET_free ( filename );
1586 GNUNET_STATISTICS_get (stats,
1588 gettext_noop("# milliseconds between hostlist downloads"),
1589 GNUNET_TIME_UNIT_MINUTES,
1598 * Stop downloading hostlists from hostlist servers as necessary.
1601 GNUNET_HOSTLIST_client_stop ()
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Hostlist client shutdown\n");
1605 #if DEBUG_HOSTLIST_CLIENT
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1607 "Hostlist client shutdown\n");
1609 if ( GNUNET_YES == stat_learning )
1610 save_hostlist_file ( GNUNET_YES );
1612 if (ti_saving_task != GNUNET_SCHEDULER_NO_TASK)
1614 GNUNET_SCHEDULER_cancel (sched,
1618 if (ti_download_dispatcher_task != GNUNET_SCHEDULER_NO_TASK)
1620 GNUNET_SCHEDULER_cancel (sched,
1621 ti_download_dispatcher_task);
1623 if (ti_testing_intervall_task != GNUNET_SCHEDULER_NO_TASK)
1625 GNUNET_SCHEDULER_cancel (sched,
1626 ti_testing_intervall_task);
1628 if (ti_download != GNUNET_SCHEDULER_NO_TASK)
1630 GNUNET_SCHEDULER_cancel (sched,
1633 if (ti_check_download != GNUNET_SCHEDULER_NO_TASK)
1635 GNUNET_SCHEDULER_cancel (sched,
1637 curl_global_cleanup ();
1639 if (transport != NULL)
1641 GNUNET_TRANSPORT_disconnect (transport);
1644 GNUNET_assert (NULL == transport);
1645 GNUNET_free_non_null (proxy);
1651 /* end of hostlist-client.c */