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_STATISTICS_Handle *stats;
119 static struct GNUNET_TRANSPORT_Handle *transport;
122 * Proxy that we are using (can be NULL).
127 * Number of bytes valid in 'download_buffer'.
129 static size_t download_pos;
132 * Current URL that we are using.
134 static char *current_url;
137 * Current CURL handle.
142 * Current multi-CURL handle.
147 * How many bytes did we download from the current hostlist URL?
149 static uint32_t stat_bytes_downloaded;
152 * Amount of time we wait between hostlist downloads.
154 static struct GNUNET_TIME_Relative hostlist_delay;
157 * ID of the task, checking if hostlist download should take plate
159 static GNUNET_SCHEDULER_TaskIdentifier ti_check_download;
162 * ID of the task downloading the hostlist
164 static GNUNET_SCHEDULER_TaskIdentifier ti_download;
167 * ID of the task saving the hostlsit in a regular intervall
169 static GNUNET_SCHEDULER_TaskIdentifier ti_saving_task;
172 * ID of the task called to initiate a download
174 static GNUNET_SCHEDULER_TaskIdentifier ti_download_dispatcher_task;
177 * ID of the task controlling the locking between two hostlist tests
179 static GNUNET_SCHEDULER_TaskIdentifier ti_testing_intervall_task;
182 * At what time MUST the current hostlist request be done?
184 static struct GNUNET_TIME_Absolute end_time;
187 * Head of the linked list used to store hostlists
189 static struct Hostlist * linked_list_head;
192 * Tail of the linked list used to store hostlists
194 static struct Hostlist * linked_list_tail;
197 * Current hostlist used for downloading
199 static struct Hostlist * current_hostlist;
202 * Size of the linke list used to store hostlists
204 static unsigned int linked_list_size;
207 * Head of the linked list used to store hostlists
209 static struct Hostlist * hostlist_to_test;
212 * Set to GNUNET_YES if the current URL had some problems.
214 static int stat_bogus_url;
217 * Value controlling if a hostlist is tested at the moment
219 static int stat_testing_hostlist;
222 * Value controlling if a hostlist testing is allowed at the moment
224 static int stat_testing_allowed;
227 * Value controlling if a hostlist download is running at the moment
229 static int stat_download_in_progress;
232 * Value saying if a preconfigured bootstrap server is used
234 static unsigned int stat_use_bootstrap;
236 * Set if we are allowed to learn new hostlists and use them
238 static int stat_learning;
241 * Value saying if hostlist download was successful
243 static unsigned int stat_download_successful;
246 * Value saying how many valid HELLO messages were obtained during download
248 static unsigned int stat_hellos_obtained;
251 * Number of active connections (according to core service).
253 static unsigned int stat_connection_count;
257 * Process downloaded bits by calling callback on each HELLO.
259 * @param ptr buffer with downloaded data
260 * @param size size of a record
261 * @param nmemb number of records downloaded
263 * @return number of bytes that were processed (always size*nmemb)
266 callback_download (void *ptr,
271 static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
272 const char * cbuf = ptr;
273 const struct GNUNET_MessageHeader *msg;
279 total = size * nmemb;
280 stat_bytes_downloaded += total;
281 if ( (total == 0) || (stat_bogus_url) )
283 return total; /* ok, no data or bogus data */
286 GNUNET_STATISTICS_update (stats,
287 gettext_noop ("# bytes downloaded from hostlist servers"),
291 while ( (left > 0) ||
294 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - download_pos);
295 memcpy (&download_buffer[download_pos],
301 if (download_pos < sizeof(struct GNUNET_MessageHeader))
303 GNUNET_assert (left == 0);
306 msg = (const struct GNUNET_MessageHeader *) download_buffer;
307 msize = ntohs(msg->size);
308 if (msize < sizeof(struct GNUNET_MessageHeader))
310 GNUNET_STATISTICS_update (stats,
311 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
314 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
315 _("Invalid `%s' message received from hostlist at `%s'\n"),
318 stat_hellos_obtained++;
322 if (download_pos < msize)
324 GNUNET_assert (left == 0);
327 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
329 #if DEBUG_HOSTLIST_CLIENT
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Received valid `%s' message from hostlist server.\n",
334 GNUNET_STATISTICS_update (stats,
335 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
338 stat_hellos_obtained++;
339 GNUNET_TRANSPORT_offer_hello (transport, msg, NULL, NULL);
343 GNUNET_STATISTICS_update (stats,
344 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
348 _("Invalid `%s' message received from hostlist at `%s'\n"),
351 stat_bogus_url = GNUNET_YES;
352 stat_hellos_obtained++;
355 memmove (download_buffer,
356 &download_buffer[msize],
357 download_pos - msize);
358 download_pos -= msize;
365 * Obtain a hostlist URL that we should use.
367 * @return NULL if there is no URL available
370 get_bootstrap_server ()
378 GNUNET_CONFIGURATION_get_value_string (cfg,
383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
384 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
385 "SERVERS", "HOSTLIST");
390 if (strlen (servers) > 0)
393 pos = strlen (servers) - 1;
396 if (servers[pos] == ' ')
403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
404 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
405 "SERVERS", "HOSTLIST");
406 GNUNET_free (servers);
410 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
411 pos = strlen (servers) - 1;
414 if (servers[pos] == ' ')
426 ret = GNUNET_strdup (&servers[pos]);
427 GNUNET_free (servers);
432 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
433 * @return uri to use, NULL if there is no URL available
439 unsigned int counter;
440 struct Hostlist * pos;
442 if ( GNUNET_NO == stat_learning)
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Using preconfigured bootstrap server\n");
446 current_hostlist = NULL;
447 return get_bootstrap_server();
450 if ( ( GNUNET_YES == stat_testing_hostlist) && (NULL != hostlist_to_test) )
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Testing new advertised hostlist if it is obtainable\n");
454 current_hostlist = hostlist_to_test;
455 return strdup(hostlist_to_test->hostlist_uri);
458 if ( (GNUNET_YES == stat_use_bootstrap) ||
459 (linked_list_size == 0) )
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Using preconfigured bootstrap server\n");
463 current_hostlist = NULL;
464 return get_bootstrap_server();
466 index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
469 pos = linked_list_head;
470 while ( counter < index )
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Using learned hostlist `%s'\n", pos->hostlist_uri);
477 current_hostlist = pos;
478 return strdup(pos->hostlist_uri);
482 #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)
486 * Method to save hostlist to a file during hostlist client shutdown
487 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
489 static void save_hostlist_file ( int shutdown );
493 * add val2 to val1 with overflow check
494 * @param val1 value 1
495 * @param val2 value 2
498 static uint64_t checked_add (uint64_t val1, uint64_t val2)
500 static uint64_t temp;
501 static uint64_t maxv;
514 * Subtract val2 from val1 with underflow check
515 * @param val1 value 1
516 * @param val2 value 2
520 checked_sub (uint64_t val1,
530 * Method to check if a URI is in hostlist linked list
531 * @param uri uri to check
532 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
535 linked_list_contains (const char * uri)
537 struct Hostlist * pos;
539 pos = linked_list_head;
542 if (0 == strcmp(pos->hostlist_uri, uri) )
551 * Method returning the hostlist element with the lowest quality in the datastore
552 * @return hostlist with lowest quality
554 static struct Hostlist *
555 linked_list_get_lowest_quality ( )
557 struct Hostlist * pos;
558 struct Hostlist * lowest;
560 if (linked_list_size == 0)
562 lowest = linked_list_head;
563 pos = linked_list_head->next;
566 if (pos->quality < lowest->quality)
575 * Method to insert a hostlist into the datastore. If datastore
576 * contains maximum number of elements, the elements with lowest
577 * quality is dismissed
582 struct Hostlist * lowest_quality;
584 if (MAX_NUMBER_HOSTLISTS <= linked_list_size)
586 /* No free entries available, replace existing entry */
587 lowest_quality = linked_list_get_lowest_quality();
588 GNUNET_assert (lowest_quality != NULL);
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Removing hostlist with URI `%s' which has the worst quality of all (%llu)\n",
591 lowest_quality->hostlist_uri,
592 (unsigned long long) lowest_quality->quality);
593 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
595 GNUNET_free (lowest_quality);
597 GNUNET_CONTAINER_DLL_insert(linked_list_head,
601 GNUNET_STATISTICS_set (stats,
602 gettext_noop("# advertised hostlist URIs"),
605 stat_testing_hostlist = GNUNET_NO;
610 * Method updating hostlist statistics
612 static void update_hostlist ( )
615 if ( ((stat_use_bootstrap == GNUNET_NO) && ( NULL != current_hostlist )) ||
616 ((stat_testing_hostlist == GNUNET_YES) && ( NULL != current_hostlist )) )
618 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
619 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
620 current_hostlist->hello_count = stat_hellos_obtained;
621 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
622 current_hostlist->quality = checked_add ( current_hostlist->quality, (stat_hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
623 if ( GNUNET_YES == stat_download_successful )
625 current_hostlist->times_used++;
626 current_hostlist->quality = checked_add ( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
627 GNUNET_asprintf (&stat,
628 gettext_noop("# advertised URI `%s' downloaded"),
629 current_hostlist->hostlist_uri);
631 GNUNET_STATISTICS_update ( stats,
638 current_hostlist->quality = checked_sub ( current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD );
640 current_hostlist = NULL;
641 /* Alternating the usage of preconfigured and learned hostlists */
643 if (stat_testing_hostlist == GNUNET_YES)
646 if ( GNUNET_YES == stat_learning)
648 if (stat_use_bootstrap == GNUNET_YES)
649 stat_use_bootstrap = GNUNET_NO;
651 stat_use_bootstrap = GNUNET_YES;
654 stat_use_bootstrap = GNUNET_YES;
658 * Clean up the state from the task that downloaded the
659 * hostlist and schedule the next task.
666 if ( (stat_testing_hostlist == GNUNET_YES) &&
667 (GNUNET_NO == stat_download_successful) &&
668 (NULL != hostlist_to_test))
670 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
671 _("Advertised hostlist with URI `%s' could not be downloaded. Advertised URI gets dismissed.\n"),
672 hostlist_to_test->hostlist_uri);
675 if (stat_testing_hostlist == GNUNET_YES)
677 stat_testing_hostlist = GNUNET_NO;
679 if ( NULL != hostlist_to_test)
681 GNUNET_free (hostlist_to_test);
682 hostlist_to_test = NULL;
687 mret = curl_multi_remove_handle (multi, curl);
688 if (mret != CURLM_OK)
690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
691 _("%s failed at %s:%d: `%s'\n"),
692 "curl_multi_remove_handle", __FILE__, __LINE__,
693 curl_multi_strerror (mret));
695 mret = curl_multi_cleanup (multi);
696 if (mret != CURLM_OK)
697 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
698 _("%s failed at %s:%d: `%s'\n"),
699 "curl_multi_cleanup", __FILE__, __LINE__,
700 curl_multi_strerror (mret));
705 curl_easy_cleanup (curl);
708 GNUNET_free_non_null (current_url);
710 stat_bytes_downloaded = 0;
711 stat_download_in_progress = GNUNET_NO;
716 * Task that is run when we are ready to receive more data from the hostlist
719 * @param cls closure, unused
720 * @param tc task context, unused
723 task_download (void *cls,
724 const struct GNUNET_SCHEDULER_TaskContext *tc);
728 * Ask CURL for the select set and then schedule the
729 * receiving task with the scheduler.
739 struct GNUNET_NETWORK_FDSet *grs;
740 struct GNUNET_NETWORK_FDSet *gws;
742 struct GNUNET_TIME_Relative rtime;
748 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
749 if (mret != CURLM_OK)
751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
752 _("%s failed at %s:%d: `%s'\n"),
753 "curl_multi_fdset", __FILE__, __LINE__,
754 curl_multi_strerror (mret));
758 mret = curl_multi_timeout (multi, &timeout);
759 if (mret != CURLM_OK)
761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
762 _("%s failed at %s:%d: `%s'\n"),
763 "curl_multi_timeout", __FILE__, __LINE__,
764 curl_multi_strerror (mret));
768 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
769 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
771 grs = GNUNET_NETWORK_fdset_create ();
772 gws = GNUNET_NETWORK_fdset_create ();
773 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
774 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
775 #if DEBUG_HOSTLIST_CLIENT
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Scheduling task for hostlist download using cURL\n");
780 = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
781 GNUNET_SCHEDULER_NO_TASK,
787 GNUNET_NETWORK_fdset_destroy (gws);
788 GNUNET_NETWORK_fdset_destroy (grs);
793 * Task that is run when we are ready to receive more data from the hostlist
796 * @param cls closure, unused
797 * @param tc task context, unused
800 task_download (void *cls,
801 const struct GNUNET_SCHEDULER_TaskContext *tc)
807 ti_download = GNUNET_SCHEDULER_NO_TASK;
808 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
810 #if DEBUG_HOSTLIST_CLIENT
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Shutdown requested while trying to download hostlist from `%s'\n",
819 if (GNUNET_TIME_absolute_get_remaining (end_time).rel_value == 0)
821 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
822 _("Timeout trying to download hostlist from `%s'\n"),
828 #if DEBUG_HOSTLIST_CLIENT
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 "Ready for processing hostlist client request\n");
836 if (stat_bytes_downloaded > MAX_BYTES_PER_HOSTLISTS)
838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
839 _("Download limit of %u bytes exceeded, stopping download\n"),
840 MAX_BYTES_PER_HOSTLISTS);
844 mret = curl_multi_perform (multi, &running);
849 msg = curl_multi_info_read (multi, &running);
850 GNUNET_break (msg != NULL);
856 if ( (msg->data.result != CURLE_OK) &&
857 (msg->data.result != CURLE_GOT_NOTHING) )
858 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
859 _("%s failed for `%s' at %s:%d: `%s'\n"),
860 "curl_multi_perform",
864 curl_easy_strerror (msg->data.result));
867 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
868 _("Download of hostlist `%s' completed.\n"),
870 stat_download_successful = GNUNET_YES;
872 if (GNUNET_YES == stat_testing_hostlist)
874 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
875 _("Adding successfully tested hostlist `%s' datastore.\n"),
878 hostlist_to_test = NULL;
879 stat_testing_hostlist = GNUNET_NO;
889 while ( (running > 0) );
892 while (mret == CURLM_CALL_MULTI_PERFORM);
894 if (mret != CURLM_OK)
896 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
897 _("%s failed at %s:%d: `%s'\n"),
898 "curl_multi_perform", __FILE__, __LINE__,
899 curl_multi_strerror (mret));
907 * Main function that will download a hostlist and process its
917 current_url = download_get_url ();
918 if (current_url == NULL)
920 curl = curl_easy_init ();
928 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
929 _("Bootstrapping using hostlist at `%s'.\n"),
932 stat_download_in_progress = GNUNET_YES;
933 stat_download_successful = GNUNET_NO;
934 stat_hellos_obtained = 0;
935 stat_bytes_downloaded = 0;
937 GNUNET_STATISTICS_update (stats,
938 gettext_noop ("# hostlist downloads initiated"),
942 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
945 CURL_EASY_SETOPT (curl,
946 CURLOPT_WRITEFUNCTION,
953 CURL_EASY_SETOPT (curl,
961 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
962 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
963 /* no need to abort if the above failed */
964 CURL_EASY_SETOPT (curl,
972 CURL_EASY_SETOPT (curl,
976 CURL_EASY_SETOPT (curl,
980 CURL_EASY_SETOPT (curl,
982 GNUNET_SERVER_MAX_MESSAGE_SIZE);
983 if (0 == strncmp (current_url, "http", 4))
984 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
985 CURL_EASY_SETOPT (curl,
986 CURLOPT_CONNECTTIMEOUT,
988 CURL_EASY_SETOPT (curl,
992 /* this should no longer be needed; we're now single-threaded! */
993 CURL_EASY_SETOPT (curl,
997 multi = curl_multi_init ();
1004 mret = curl_multi_add_handle (multi, curl);
1005 if (mret != CURLM_OK)
1007 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1008 _("%s failed at %s:%d: `%s'\n"),
1009 "curl_multi_add_handle", __FILE__, __LINE__,
1010 curl_multi_strerror (mret));
1011 mret = curl_multi_cleanup (multi);
1012 if (mret != CURLM_OK)
1013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1014 _("%s failed at %s:%d: `%s'\n"),
1015 "curl_multi_cleanup", __FILE__, __LINE__,
1016 curl_multi_strerror (mret));
1021 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
1022 download_prepare ();
1027 task_download_dispatcher (void *cls,
1028 const struct GNUNET_SCHEDULER_TaskContext *tc)
1030 ti_download_dispatcher_task = GNUNET_SCHEDULER_NO_TASK;
1031 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "Download is initiated...\n");
1035 if ( GNUNET_NO == stat_download_in_progress )
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "Download can start immediately...\n");
1039 download_hostlist();
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1044 "Download in progess, have to wait...\n");
1045 ti_download_dispatcher_task = GNUNET_SCHEDULER_add_delayed (WAITING_INTERVALL,
1046 &task_download_dispatcher,
1052 * Task that checks if we should try to download a hostlist.
1053 * If so, we initiate the download, otherwise we schedule
1054 * this task again for a later time.
1057 task_check (void *cls,
1058 const struct GNUNET_SCHEDULER_TaskContext *tc)
1061 struct GNUNET_TIME_Relative delay;
1063 ti_check_download = GNUNET_SCHEDULER_NO_TASK;
1064 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1067 if (stat_connection_count < MIN_CONNECTIONS)
1069 ti_download_dispatcher_task = GNUNET_SCHEDULER_add_now (&task_download_dispatcher,
1075 curl_global_cleanup ();
1076 return; /* in shutdown */
1078 delay = hostlist_delay;
1079 if (hostlist_delay.rel_value == 0)
1080 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
1082 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
1083 if (hostlist_delay.rel_value > GNUNET_TIME_UNIT_HOURS.rel_value * (1 + stat_connection_count))
1084 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
1085 (1 + stat_connection_count));
1086 GNUNET_STATISTICS_set (stats,
1087 gettext_noop("# milliseconds between hostlist downloads"),
1088 hostlist_delay.rel_value,
1092 delay = GNUNET_TIME_UNIT_ZERO;
1095 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1096 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
1097 stat_connection_count,
1099 (unsigned long long) delay.rel_value);
1100 ti_check_download = GNUNET_SCHEDULER_add_delayed (delay,
1107 * This tasks sets hostlist testing to allowed after intervall between to testings is reached
1109 * @param cls closure
1110 * @param tc TaskContext
1113 task_testing_intervall_reset (void *cls,
1114 const struct GNUNET_SCHEDULER_TaskContext *tc)
1116 ti_testing_intervall_task = GNUNET_SCHEDULER_NO_TASK;
1117 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1119 stat_testing_allowed = GNUNET_OK;
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1121 "Testing new hostlist advertisements is allowed again\n");
1126 * Task that writes hostlist entries to a file on a regular base
1128 * @param cls closure
1129 * @param tc TaskContext
1132 task_hostlist_saving (void *cls,
1133 const struct GNUNET_SCHEDULER_TaskContext *tc)
1135 ti_saving_task = GNUNET_SCHEDULER_NO_TASK;
1136 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1138 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1139 _("Scheduled saving of hostlists\n"));
1140 save_hostlist_file ( GNUNET_NO );
1142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1143 _("Hostlists will be saved to file again in %llums\n"),
1144 (unsigned long long) SAVING_INTERVALL.rel_value);
1145 ti_saving_task = GNUNET_SCHEDULER_add_delayed (SAVING_INTERVALL,
1146 &task_hostlist_saving,
1152 * Method called whenever a given peer connects.
1154 * @param cls closure
1155 * @param peer peer identity this notification is about
1156 * @param atsi performance data
1159 handler_connect (void *cls,
1161 GNUNET_PeerIdentity * peer,
1162 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1164 GNUNET_assert (stat_connection_count < UINT_MAX);
1165 stat_connection_count++;
1166 GNUNET_STATISTICS_update (stats,
1167 gettext_noop ("# active connections"),
1174 * Method called whenever a given peer disconnects.
1176 * @param cls closure
1177 * @param peer peer identity this notification is about
1180 handler_disconnect (void *cls,
1182 GNUNET_PeerIdentity * peer)
1184 GNUNET_assert (stat_connection_count > 0);
1185 stat_connection_count--;
1186 GNUNET_STATISTICS_update (stats,
1187 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 atsi performance data
1200 * @return GNUNET_OK to keep the connection open,
1201 * GNUNET_SYSERR to close it (signal serious error)
1204 handler_advertisement (void *cls,
1205 const struct GNUNET_PeerIdentity * peer,
1206 const struct GNUNET_MessageHeader * message,
1207 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1211 const struct GNUNET_MessageHeader * incoming;
1213 struct Hostlist * hostlist;
1215 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1216 size = ntohs (message->size);
1217 if (size <= sizeof(struct GNUNET_MessageHeader))
1219 GNUNET_break_op (0);
1220 return GNUNET_SYSERR;
1222 incoming = (const struct GNUNET_MessageHeader *) message;
1223 uri = (const char*) &incoming[1];
1224 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1225 if (uri [uri_size - 1] != '\0')
1227 GNUNET_break_op (0);
1228 return GNUNET_SYSERR;
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1234 if (GNUNET_NO != linked_list_contains (uri))
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "URI `%s' is already known\n",
1242 if ( GNUNET_NO == stat_testing_allowed )
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Currently not accepting new advertisements: interval between to advertisements is not reached\n");
1246 return GNUNET_SYSERR;
1248 if ( GNUNET_YES == stat_testing_hostlist )
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1251 "Currently not accepting new advertisements: we are already testing a hostlist\n");
1252 return GNUNET_SYSERR;
1255 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1256 hostlist->hostlist_uri = (const char*) &hostlist[1];
1257 memcpy (&hostlist[1], uri, uri_size);
1258 hostlist->time_creation = GNUNET_TIME_absolute_get();
1259 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1260 hostlist->quality = HOSTLIST_INITIAL;
1261 hostlist_to_test = hostlist;
1263 stat_testing_hostlist = GNUNET_YES;
1264 stat_testing_allowed = GNUNET_NO;
1265 ti_testing_intervall_task = GNUNET_SCHEDULER_add_delayed (TESTING_INTERVAL,
1266 &task_testing_intervall_reset,
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Testing new hostlist advertisements is locked for the next %u ms\n",
1271 TESTING_INTERVAL.rel_value);
1273 ti_download_dispatcher_task = GNUNET_SCHEDULER_add_now (&task_download_dispatcher,
1282 * Continuation called by the statistics code once
1283 * we go the stat. Initiates hostlist download scheduling.
1285 * @param cls closure
1286 * @param success GNUNET_OK if statistics were
1287 * successfully obtained, GNUNET_SYSERR if not.
1290 primary_task (void *cls, int success)
1293 return; /* in shutdown */
1294 #if DEBUG_HOSTLIST_CLIENT
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Statistics request done, scheduling hostlist download\n");
1298 ti_check_download = GNUNET_SCHEDULER_add_now (&task_check,
1304 process_stat (void *cls,
1305 const char *subsystem,
1310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1311 _("Initial time between hostlist downloads is %llums\n"),
1312 (unsigned long long) value);
1313 hostlist_delay.rel_value = value;
1318 * Method to load persistent hostlist file during hostlist client startup
1321 load_hostlist_file ()
1326 struct Hostlist * hostlist;
1328 uint32_t times_used;
1329 uint32_t hellos_returned;
1336 GNUNET_CONFIGURATION_get_value_filename (cfg,
1341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1342 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1343 "HOSTLISTFILE", "HOSTLIST");
1347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1348 _("Loading saved hostlist entries from file `%s' \n"), filename);
1349 if ( GNUNET_NO == GNUNET_DISK_file_test (filename) )
1351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1352 _("Hostlist file `%s' is not existing\n"), filename);
1353 GNUNET_free ( filename );
1357 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1360 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1361 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1364 GNUNET_free (filename);
1369 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1371 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1372 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1373 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1374 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1375 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1377 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1378 hostlist->hello_count = hellos_returned;
1379 hostlist->hostlist_uri = (const char *) &hostlist[1];
1380 memcpy (&hostlist[1], uri, strlen(uri)+1);
1381 hostlist->quality = quality;
1382 hostlist->time_creation.abs_value = created;
1383 hostlist->time_last_usage.abs_value = last_used;
1384 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1387 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1391 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1394 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1395 _("%u hostlist URIs loaded from file\n"), counter);
1396 GNUNET_STATISTICS_set (stats,
1397 gettext_noop("# hostlist URIs read from file"),
1400 GNUNET_STATISTICS_set (stats,
1401 gettext_noop("# advertised hostlist URIs"),
1405 GNUNET_free_non_null (uri);
1407 GNUNET_BIO_read_close (rh, &emsg);
1410 GNUNET_free (filename);
1415 * Method to save persistent hostlist file during hostlist client shutdown
1416 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1418 static void save_hostlist_file ( int shutdown )
1421 struct Hostlist *pos;
1422 struct GNUNET_BIO_WriteHandle * wh;
1427 GNUNET_CONFIGURATION_get_value_filename (cfg,
1432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1433 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1434 "HOSTLISTFILE", "HOSTLIST");
1437 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
1439 GNUNET_free (filename);
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);
1455 /* add code to write hostlists to file using bio */
1458 while (NULL != (pos = linked_list_head))
1460 if ( GNUNET_YES == shutdown)
1462 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1465 if (GNUNET_YES == ok)
1468 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1470 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1472 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1474 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.abs_value)) ||
1476 GNUNET_BIO_write_int64 (wh, pos->time_creation.abs_value)) ||
1478 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1480 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1481 _("Error writing hostlist URIs to file `%s'\n"),
1487 if ( GNUNET_YES == shutdown)
1490 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1492 GNUNET_STATISTICS_set (stats,
1493 gettext_noop("# hostlist URIs written to file"),
1497 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1498 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1499 _("Error writing hostlist URIs to file `%s'\n"),
1501 GNUNET_free (filename);
1505 * Start downloading hostlists from hostlist servers as necessary.
1508 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1509 struct GNUNET_STATISTICS_Handle *st,
1510 GNUNET_CORE_ConnectEventHandler *ch,
1511 GNUNET_CORE_DisconnectEventHandler *dh,
1512 GNUNET_CORE_MessageCallback *msgh,
1518 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1521 return GNUNET_SYSERR;
1523 transport = GNUNET_TRANSPORT_connect (c, NULL, NULL, NULL, NULL, NULL);
1524 if (NULL == transport)
1526 curl_global_cleanup ();
1527 return GNUNET_SYSERR;
1532 GNUNET_CONFIGURATION_get_value_string (cfg,
1537 stat_learning = learn;
1538 *ch = &handler_connect;
1539 *dh = &handler_disconnect;
1540 linked_list_head = NULL;
1541 linked_list_tail = NULL;
1542 stat_use_bootstrap = GNUNET_YES;
1543 stat_testing_hostlist = GNUNET_NO;
1544 stat_testing_allowed = GNUNET_YES;
1546 if ( GNUNET_YES == stat_learning )
1548 *msgh = &handler_advertisement;
1549 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1550 _("Learning is enabled on this peer\n"));
1551 load_hostlist_file ();
1552 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1553 _("Hostlists will be saved to file again in %llums\n"),
1554 (unsigned long long) SAVING_INTERVALL.rel_value);
1555 ti_saving_task = GNUNET_SCHEDULER_add_delayed (SAVING_INTERVALL,
1556 &task_hostlist_saving,
1561 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1562 _("Learning is not enabled on this peer\n"));
1564 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
1569 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1571 result = remove (filename);
1573 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1574 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),
1577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1578 _("Hostlist file `%s' could not be removed\n"),
1582 GNUNET_free ( filename );
1584 GNUNET_STATISTICS_get (stats,
1586 gettext_noop("# milliseconds between hostlist downloads"),
1587 GNUNET_TIME_UNIT_MINUTES,
1596 * Stop downloading hostlists from hostlist servers as necessary.
1599 GNUNET_HOSTLIST_client_stop ()
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Hostlist client shutdown\n");
1603 #if DEBUG_HOSTLIST_CLIENT
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1605 "Hostlist client shutdown\n");
1607 if ( GNUNET_YES == stat_learning )
1608 save_hostlist_file ( GNUNET_YES );
1610 if (ti_saving_task != GNUNET_SCHEDULER_NO_TASK)
1612 GNUNET_SCHEDULER_cancel (ti_saving_task);
1615 if (ti_download_dispatcher_task != GNUNET_SCHEDULER_NO_TASK)
1617 GNUNET_SCHEDULER_cancel (ti_download_dispatcher_task);
1619 if (ti_testing_intervall_task != GNUNET_SCHEDULER_NO_TASK)
1621 GNUNET_SCHEDULER_cancel (ti_testing_intervall_task);
1623 if (ti_download != GNUNET_SCHEDULER_NO_TASK)
1625 GNUNET_SCHEDULER_cancel (ti_download);
1627 if (ti_check_download != GNUNET_SCHEDULER_NO_TASK)
1629 GNUNET_SCHEDULER_cancel (ti_check_download);
1630 curl_global_cleanup ();
1632 if (transport != NULL)
1634 GNUNET_TRANSPORT_disconnect (transport);
1637 GNUNET_assert (NULL == transport);
1638 GNUNET_free_non_null (proxy);
1643 /* end of hostlist-client.c */