2 This file is part of GNUnet.
3 Copyright (C) 2001-2010, 2014, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file hostlist/gnunet-daemon-hostlist_client.c
22 * @brief hostlist support. Downloads HELLOs via HTTP.
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet-daemon-hostlist_client.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet-daemon-hostlist.h"
33 /* Just included for the right curl.h */
34 #include "gnunet_curl_lib.h"
38 * Number of connections that we must have to NOT download
41 #define MIN_CONNECTIONS 4
44 * Maximum number of hostlist that are saved
46 #define MAX_NUMBER_HOSTLISTS 30
49 * Time interval hostlists are saved to disk
51 #define SAVING_INTERVAL \
52 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
55 * Time interval between two hostlist tests
57 #define TESTING_INTERVAL \
58 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
61 * Time interval for download dispatcher before a download is re-scheduled
63 #define WAITING_INTERVAL \
64 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
67 * Defines concerning the hostlist quality metric
71 * Initial quality of a new created hostlist
73 #define HOSTLIST_INITIAL 10000
76 * Value subtracted each time a hostlist download fails
78 #define HOSTLIST_FAILED_DOWNLOAD 100
81 * Value added each time a hostlist download is successful
83 #define HOSTLIST_SUCCESSFUL_DOWNLOAD 100
86 * Value added for each valid HELLO recived during a hostlist download
88 #define HOSTLIST_SUCCESSFUL_HELLO 1
92 * A single hostlist obtained by hostlist advertisements
97 * previous entry, used to manage entries in a double linked list
99 struct Hostlist *prev;
102 * next entry, used to manage entries in a double linked list
104 struct Hostlist *next;
107 * URI where hostlist can be obtained
109 const char *hostlist_uri;
112 * Value describing the quality of the hostlist, the bigger the better but (should) never < 0
113 * used for deciding which hostlist is replaced if MAX_NUMBER_HOSTLISTS in data structure is reached
114 * intial value = HOSTLIST_INITIAL
115 * increased every successful download by HOSTLIST_SUCCESSFULL_DOWNLOAD
116 * increased every successful download by number of obtained HELLO messages
117 * decreased every failed download by HOSTLIST_SUCCESSFULL_DOWNLOAD
122 * Time the hostlist advertisement was recieved and the entry was created
124 struct GNUNET_TIME_Absolute time_creation;
127 * Last time the hostlist was obtained
129 struct GNUNET_TIME_Absolute time_last_usage;
132 * Number of HELLO messages obtained during last download
134 uint32_t hello_count;
137 * Number of times the hostlist was successfully obtained
146 static const struct GNUNET_CONFIGURATION_Handle *cfg;
151 static struct GNUNET_STATISTICS_Handle *stats;
154 * Proxy hostname or ip we are using (can be NULL).
159 * Proxy username we are using (can be NULL).
161 static char *proxy_username;
164 * Proxy password we are using (can be NULL).
166 static char *proxy_password;
169 * Proxy type we are using (can be NULL).
171 static curl_proxytype proxy_type;
174 * Number of bytes valid in 'download_buffer'.
176 static size_t download_pos;
179 * Current URL that we are using.
181 static char *current_url;
184 * Current CURL handle.
189 * Current multi-CURL handle.
194 * How many bytes did we download from the current hostlist URL?
196 static uint32_t stat_bytes_downloaded;
199 * Amount of time we wait between hostlist downloads.
201 static struct GNUNET_TIME_Relative hostlist_delay;
204 * ID of the task, checking if hostlist download should take plate
206 static struct GNUNET_SCHEDULER_Task *ti_check_download;
209 * ID of the task downloading the hostlist
211 static struct GNUNET_SCHEDULER_Task *ti_download;
214 * ID of the task saving the hostlsit in a regular intervall
216 static struct GNUNET_SCHEDULER_Task *ti_saving_task;
219 * ID of the task called to initiate a download
221 static struct GNUNET_SCHEDULER_Task *ti_download_dispatcher_task;
224 * ID of the task controlling the locking between two hostlist tests
226 static struct GNUNET_SCHEDULER_Task *ti_testing_intervall_task;
229 * At what time MUST the current hostlist request be done?
231 static struct GNUNET_TIME_Absolute end_time;
234 * Head of the linked list used to store hostlists
236 static struct Hostlist *linked_list_head;
239 * Tail of the linked list used to store hostlists
241 static struct Hostlist *linked_list_tail;
244 * Current hostlist used for downloading
246 static struct Hostlist *current_hostlist;
249 * Size of the linke list used to store hostlists
251 static unsigned int linked_list_size;
254 * Head of the linked list used to store hostlists
256 static struct Hostlist *hostlist_to_test;
259 * Handle for our statistics GET operation.
261 static struct GNUNET_STATISTICS_GetHandle *sget;
264 * Set to GNUNET_YES if the current URL had some problems.
266 static int stat_bogus_url;
269 * Value controlling if a hostlist is tested at the moment
271 static int stat_testing_hostlist;
274 * Value controlling if a hostlist testing is allowed at the moment
276 static int stat_testing_allowed;
279 * Value controlling if a hostlist download is running at the moment
281 static int stat_download_in_progress;
284 * Value saying if a preconfigured bootstrap server is used
286 static unsigned int stat_use_bootstrap;
289 * Set if we are allowed to learn new hostlists and use them
291 static int stat_learning;
294 * Value saying if hostlist download was successful
296 static unsigned int stat_download_successful;
299 * Value saying how many valid HELLO messages were obtained during download
301 static unsigned int stat_hellos_obtained;
304 * Number of active connections (according to core service).
306 static unsigned int stat_connection_count;
309 * Handle to peerinfo service.
311 static struct GNUNET_PEERINFO_Handle *pi;
315 * Process downloaded bits by calling callback on each HELLO.
317 * @param ptr buffer with downloaded data
318 * @param size size of a record
319 * @param nmemb number of records downloaded
321 * @return number of bytes that were processed (always size*nmemb)
324 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
326 static char download_buffer[GNUNET_MAX_MESSAGE_SIZE - 1];
327 const char *cbuf = ptr;
328 const struct GNUNET_MessageHeader *msg;
334 total = size * nmemb;
335 stat_bytes_downloaded += total;
336 if ((total == 0) || (stat_bogus_url))
338 return total; /* ok, no data or bogus data */
341 GNUNET_STATISTICS_update (stats,
343 "# bytes downloaded from hostlist servers"),
347 while ((left > 0) || (download_pos > 0))
349 cpy = GNUNET_MIN (left, GNUNET_MAX_MESSAGE_SIZE - 1 - download_pos);
350 GNUNET_memcpy (&download_buffer[download_pos], cbuf, cpy);
354 if (download_pos < sizeof(struct GNUNET_MessageHeader))
356 GNUNET_assert (0 == left);
359 msg = (const struct GNUNET_MessageHeader *) download_buffer;
360 msize = ntohs (msg->size);
361 if (msize < sizeof(struct GNUNET_MessageHeader))
363 GNUNET_STATISTICS_update (
365 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
368 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
369 _ ("Invalid `%s' message received from hostlist at `%s'\n"),
372 stat_hellos_obtained++;
376 if (download_pos < msize)
378 GNUNET_assert (left == 0);
381 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) msg) == msize)
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Received valid `%s' message from hostlist server.\n",
386 GNUNET_STATISTICS_update (
388 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
391 stat_hellos_obtained++;
393 GNUNET_PEERINFO_add_peer (pi,
394 (const struct GNUNET_HELLO_Message *) msg,
400 GNUNET_STATISTICS_update (
402 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
405 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
406 _ ("Invalid `%s' message received from hostlist at `%s'\n"),
409 stat_bogus_url = GNUNET_YES;
410 stat_hellos_obtained++;
413 memmove (download_buffer, &download_buffer[msize], download_pos - msize);
414 download_pos -= msize;
421 * Obtain a hostlist URL that we should use.
423 * @return NULL if there is no URL available
426 get_bootstrap_server ()
433 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
438 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
445 if (strlen (servers) > 0)
448 pos = strlen (servers) - 1;
451 if (servers[pos] == ' ')
458 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
461 GNUNET_free (servers);
465 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
466 pos = strlen (servers) - 1;
469 if (servers[pos] == ' ')
481 ret = GNUNET_strdup (&servers[pos]);
482 GNUNET_free (servers);
488 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
489 * @return uri to use, NULL if there is no URL available
495 unsigned int counter;
496 struct Hostlist *pos;
498 if (GNUNET_NO == stat_learning)
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Using preconfigured bootstrap server\n");
502 current_hostlist = NULL;
503 return get_bootstrap_server ();
506 if ((GNUNET_YES == stat_testing_hostlist) && (NULL != hostlist_to_test))
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Testing new advertised hostlist if it is obtainable\n");
510 current_hostlist = hostlist_to_test;
511 return GNUNET_strdup (hostlist_to_test->hostlist_uri);
514 if ((GNUNET_YES == stat_use_bootstrap) || (linked_list_size == 0))
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "Using preconfigured bootstrap server\n");
518 current_hostlist = NULL;
519 return get_bootstrap_server ();
522 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
524 pos = linked_list_head;
525 while (counter < index)
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531 "Using learned hostlist `%s'\n",
533 current_hostlist = pos;
534 return GNUNET_strdup (pos->hostlist_uri);
538 #define CURL_EASY_SETOPT(c, a, b) \
541 ret = curl_easy_setopt (c, a, b); \
542 if (CURLE_OK != ret) \
543 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, \
544 _ ("%s failed at %s:%d: `%s'\n"), \
545 "curl_easy_setopt", \
548 curl_easy_strerror (ret)); \
553 * Method to save hostlist to a file during hostlist client shutdown
555 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
558 save_hostlist_file (int shutdown);
562 * Add val2 to val1 with overflow check
564 * @param val1 value 1
565 * @param val2 value 2
569 checked_add (uint64_t val1, uint64_t val2)
571 static uint64_t temp;
572 static uint64_t maxv;
585 * Subtract val2 from val1 with underflow check
587 * @param val1 value 1
588 * @param val2 value 2
592 checked_sub (uint64_t val1, uint64_t val2)
601 * Method to check if a URI is in hostlist linked list
603 * @param uri uri to check
604 * @return #GNUNET_YES if existing in linked list, #GNUNET_NO if not
607 linked_list_contains (const char *uri)
609 struct Hostlist *pos;
611 pos = linked_list_head;
614 if (0 == strcmp (pos->hostlist_uri, uri))
623 * Method returning the hostlist element with the lowest quality in the datastore
624 * @return hostlist with lowest quality
626 static struct Hostlist *
627 linked_list_get_lowest_quality ()
629 struct Hostlist *pos;
630 struct Hostlist *lowest;
632 if (linked_list_size == 0)
634 lowest = linked_list_head;
635 pos = linked_list_head->next;
638 if (pos->quality < lowest->quality)
647 * Method to insert a hostlist into the datastore. If datastore
648 * contains maximum number of elements, the elements with lowest
649 * quality is dismissed
654 struct Hostlist *lowest_quality;
656 if (MAX_NUMBER_HOSTLISTS <= linked_list_size)
658 /* No free entries available, replace existing entry */
659 lowest_quality = linked_list_get_lowest_quality ();
660 GNUNET_assert (lowest_quality != NULL);
662 GNUNET_ERROR_TYPE_DEBUG,
663 "Removing hostlist with URI `%s' which has the worst quality of all (%llu)\n",
664 lowest_quality->hostlist_uri,
665 (unsigned long long) lowest_quality->quality);
666 GNUNET_CONTAINER_DLL_remove (linked_list_head,
670 GNUNET_free (lowest_quality);
672 GNUNET_CONTAINER_DLL_insert (linked_list_head,
676 GNUNET_STATISTICS_set (stats,
677 gettext_noop ("# advertised hostlist URIs"),
680 stat_testing_hostlist = GNUNET_NO;
685 * Method updating hostlist statistics
692 if (((stat_use_bootstrap == GNUNET_NO) && (NULL != current_hostlist)) ||
693 ((stat_testing_hostlist == GNUNET_YES) && (NULL != current_hostlist)))
695 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
696 "Updating hostlist statics for URI `%s'\n",
697 current_hostlist->hostlist_uri);
698 current_hostlist->hello_count = stat_hellos_obtained;
699 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get ();
700 current_hostlist->quality =
701 checked_add (current_hostlist->quality,
702 (stat_hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
703 if (GNUNET_YES == stat_download_successful)
705 current_hostlist->times_used++;
706 current_hostlist->quality =
707 checked_add (current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
708 GNUNET_asprintf (&stat,
709 gettext_noop ("# advertised URI `%s' downloaded"),
710 current_hostlist->hostlist_uri);
712 GNUNET_STATISTICS_update (stats, stat, 1, GNUNET_YES);
716 current_hostlist->quality =
717 checked_sub (current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD);
719 current_hostlist = NULL;
720 /* Alternating the usage of preconfigured and learned hostlists */
722 if (stat_testing_hostlist == GNUNET_YES)
725 if (GNUNET_YES == stat_learning)
727 if (stat_use_bootstrap == GNUNET_YES)
728 stat_use_bootstrap = GNUNET_NO;
730 stat_use_bootstrap = GNUNET_YES;
733 stat_use_bootstrap = GNUNET_YES;
738 * Clean up the state from the task that downloaded the
739 * hostlist and schedule the next task.
746 if ((stat_testing_hostlist == GNUNET_YES) &&
747 (GNUNET_NO == stat_download_successful) && (NULL != hostlist_to_test))
750 GNUNET_ERROR_TYPE_INFO,
752 "Advertised hostlist with URI `%s' could not be downloaded. Advertised URI gets dismissed.\n"),
753 hostlist_to_test->hostlist_uri);
756 if (stat_testing_hostlist == GNUNET_YES)
758 stat_testing_hostlist = GNUNET_NO;
760 if (NULL != hostlist_to_test)
762 GNUNET_free (hostlist_to_test);
763 hostlist_to_test = NULL;
768 mret = curl_multi_remove_handle (multi, curl);
769 if (mret != CURLM_OK)
771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
772 _ ("%s failed at %s:%d: `%s'\n"),
773 "curl_multi_remove_handle",
776 curl_multi_strerror (mret));
778 mret = curl_multi_cleanup (multi);
779 if (mret != CURLM_OK)
780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
781 _ ("%s failed at %s:%d: `%s'\n"),
782 "curl_multi_cleanup",
785 curl_multi_strerror (mret));
790 curl_easy_cleanup (curl);
793 GNUNET_free_non_null (current_url);
795 stat_bytes_downloaded = 0;
796 stat_download_in_progress = GNUNET_NO;
801 * Task that is run when we are ready to receive more data from the hostlist
804 * @param cls closure, unused
805 * @param tc task context, unused
808 task_download (void *cls);
812 * Ask CURL for the select set and then schedule the
813 * receiving task with the scheduler.
823 struct GNUNET_NETWORK_FDSet *grs;
824 struct GNUNET_NETWORK_FDSet *gws;
826 struct GNUNET_TIME_Relative rtime;
832 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
833 if (mret != CURLM_OK)
835 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
836 _ ("%s failed at %s:%d: `%s'\n"),
840 curl_multi_strerror (mret));
844 mret = curl_multi_timeout (multi, &timeout);
845 if (mret != CURLM_OK)
847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
848 _ ("%s failed at %s:%d: `%s'\n"),
849 "curl_multi_timeout",
852 curl_multi_strerror (mret));
856 rtime = GNUNET_TIME_relative_min (
857 GNUNET_TIME_absolute_get_remaining (end_time),
858 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, timeout));
859 grs = GNUNET_NETWORK_fdset_create ();
860 gws = GNUNET_NETWORK_fdset_create ();
861 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
862 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Scheduling task for hostlist download using cURL\n");
865 ti_download = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
871 GNUNET_NETWORK_fdset_destroy (gws);
872 GNUNET_NETWORK_fdset_destroy (grs);
877 * Task that is run when we are ready to receive more data from the hostlist
880 * @param cls closure, unused
883 task_download (void *cls)
890 if (0 == GNUNET_TIME_absolute_get_remaining (end_time).rel_value_us)
892 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
893 _ ("Timeout trying to download hostlist from `%s'\n"),
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Ready for processing hostlist client request\n");
904 if (stat_bytes_downloaded > MAX_BYTES_PER_HOSTLISTS)
906 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
908 "Download limit of %u bytes exceeded, stopping download\n"),
909 MAX_BYTES_PER_HOSTLISTS);
913 mret = curl_multi_perform (multi, &running);
918 msg = curl_multi_info_read (multi, &running);
919 GNUNET_break (msg != NULL);
925 if ((msg->data.result != CURLE_OK) &&
926 (msg->data.result != CURLE_GOT_NOTHING))
927 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
928 _ ("Download of hostlist from `%s' failed: `%s'\n"),
930 curl_easy_strerror (msg->data.result));
933 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
934 _ ("Download of hostlist `%s' completed.\n"),
936 stat_download_successful = GNUNET_YES;
938 if (GNUNET_YES == stat_testing_hostlist)
941 GNUNET_ERROR_TYPE_INFO,
942 _ ("Adding successfully tested hostlist `%s' datastore.\n"),
945 hostlist_to_test = NULL;
946 stat_testing_hostlist = GNUNET_NO;
956 while ((running > 0));
959 while (mret == CURLM_CALL_MULTI_PERFORM);
961 if (mret != CURLM_OK)
963 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
964 _ ("%s failed at %s:%d: `%s'\n"),
965 "curl_multi_perform",
968 curl_multi_strerror (mret));
976 * Main function that will download a hostlist and process its
986 current_url = download_get_url ();
987 if (current_url == NULL)
989 curl = curl_easy_init ();
997 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
998 _ ("Bootstrapping using hostlist at `%s'.\n"),
1001 stat_download_in_progress = GNUNET_YES;
1002 stat_download_successful = GNUNET_NO;
1003 stat_hellos_obtained = 0;
1004 stat_bytes_downloaded = 0;
1006 GNUNET_STATISTICS_update (stats,
1007 gettext_noop ("# hostlist downloads initiated"),
1012 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
1013 CURL_EASY_SETOPT (curl, CURLOPT_PROXYTYPE, proxy_type);
1014 if (NULL != proxy_username)
1015 CURL_EASY_SETOPT (curl, CURLOPT_PROXYUSERNAME, proxy_username);
1016 if (NULL != proxy_password)
1017 CURL_EASY_SETOPT (curl, CURLOPT_PROXYPASSWORD, proxy_password);
1021 CURL_EASY_SETOPT (curl, CURLOPT_WRITEFUNCTION, &callback_download);
1022 if (ret != CURLE_OK)
1027 CURL_EASY_SETOPT (curl, CURLOPT_WRITEDATA, NULL);
1028 if (ret != CURLE_OK)
1033 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
1034 CURL_EASY_SETOPT (curl,
1035 CURLOPT_REDIR_PROTOCOLS,
1036 CURLPROTO_HTTP | CURLPROTO_HTTPS);
1037 CURL_EASY_SETOPT (curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
1038 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
1039 /* no need to abort if the above failed */
1040 CURL_EASY_SETOPT (curl, CURLOPT_URL, current_url);
1041 if (ret != CURLE_OK)
1046 CURL_EASY_SETOPT (curl, CURLOPT_FAILONERROR, 1);
1048 CURL_EASY_SETOPT (curl, CURLOPT_VERBOSE, 1);
1050 CURL_EASY_SETOPT (curl, CURLOPT_BUFFERSIZE, GNUNET_MAX_MESSAGE_SIZE);
1051 if (0 == strncmp (current_url, "http", 4))
1052 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
1053 CURL_EASY_SETOPT (curl, CURLOPT_CONNECTTIMEOUT, 60L);
1054 CURL_EASY_SETOPT (curl, CURLOPT_TIMEOUT, 60L);
1055 multi = curl_multi_init ();
1062 mret = curl_multi_add_handle (multi, curl);
1063 if (mret != CURLM_OK)
1065 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1066 _ ("%s failed at %s:%d: `%s'\n"),
1067 "curl_multi_add_handle",
1070 curl_multi_strerror (mret));
1071 mret = curl_multi_cleanup (multi);
1072 if (mret != CURLM_OK)
1073 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1074 _ ("%s failed at %s:%d: `%s'\n"),
1075 "curl_multi_cleanup",
1078 curl_multi_strerror (mret));
1083 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
1084 download_prepare ();
1089 task_download_dispatcher (void *cls)
1091 ti_download_dispatcher_task = NULL;
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download is initiated...\n");
1093 if (GNUNET_NO == stat_download_in_progress)
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download can start immediately...\n");
1096 download_hostlist ();
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1101 "Download in progess, have to wait...\n");
1102 ti_download_dispatcher_task =
1103 GNUNET_SCHEDULER_add_delayed (WAITING_INTERVAL,
1104 &task_download_dispatcher,
1111 * Task that checks if we should try to download a hostlist.
1112 * If so, we initiate the download, otherwise we schedule
1113 * this task again for a later time.
1116 task_check (void *cls)
1119 struct GNUNET_TIME_Relative delay;
1121 ti_check_download = NULL;
1124 curl_global_cleanup ();
1125 return; /* in shutdown */
1127 if ((stat_connection_count < MIN_CONNECTIONS) &&
1128 (NULL == ti_download_dispatcher_task))
1129 ti_download_dispatcher_task =
1130 GNUNET_SCHEDULER_add_now (&task_download_dispatcher, NULL);
1132 delay = hostlist_delay;
1133 if (0 == hostlist_delay.rel_value_us)
1134 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
1136 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
1137 if (hostlist_delay.rel_value_us >
1138 GNUNET_TIME_UNIT_HOURS.rel_value_us * (1 + stat_connection_count))
1140 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
1141 (1 + stat_connection_count));
1142 GNUNET_STATISTICS_set (stats,
1144 "# milliseconds between hostlist downloads"),
1145 hostlist_delay.rel_value_us / 1000LL,
1149 delay = GNUNET_TIME_UNIT_ZERO;
1153 GNUNET_ERROR_TYPE_INFO,
1154 _ ("Have %u/%u connections. Will consider downloading hostlist in %s\n"),
1155 stat_connection_count,
1157 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1158 ti_check_download = GNUNET_SCHEDULER_add_delayed (delay, &task_check, NULL);
1163 * This tasks sets hostlist testing to allowed after intervall between to testings is reached
1165 * @param cls closure
1168 task_testing_intervall_reset (void *cls)
1170 ti_testing_intervall_task = NULL;
1171 stat_testing_allowed = GNUNET_OK;
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1173 "Testing new hostlist advertisements is allowed again\n");
1178 * Task that writes hostlist entries to a file on a regular base
1180 * @param cls closure
1183 task_hostlist_saving (void *cls)
1185 ti_saving_task = NULL;
1186 save_hostlist_file (GNUNET_NO);
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Hostlists will be saved to file again in %s\n",
1190 GNUNET_STRINGS_relative_time_to_string (SAVING_INTERVAL,
1193 GNUNET_SCHEDULER_add_delayed (SAVING_INTERVAL, &task_hostlist_saving, NULL);
1198 * Method called whenever a given peer connects.
1200 * @param cls closure
1201 * @param peer peer identity this notification is about
1202 * @param mq message queue for transmissions to @a peer
1205 handler_connect (void *cls,
1206 const struct GNUNET_PeerIdentity *peer,
1207 struct GNUNET_MQ_Handle *mq)
1209 GNUNET_assert (stat_connection_count < UINT_MAX);
1210 stat_connection_count++;
1211 GNUNET_STATISTICS_update (stats,
1212 gettext_noop ("# active connections"),
1220 * Method called whenever a given peer disconnects.
1222 * @param cls closure
1223 * @param peer peer identity this notification is about
1226 handler_disconnect (void *cls,
1227 const struct GNUNET_PeerIdentity *peer,
1230 GNUNET_assert (stat_connection_count > 0);
1231 stat_connection_count--;
1232 GNUNET_STATISTICS_update (stats,
1233 gettext_noop ("# active connections"),
1240 * Method called whenever an advertisement message arrives.
1242 * @param uri the advertised URI
1245 handler_advertisement (const char *uri)
1248 struct Hostlist *hostlist;
1250 uri_size = strlen (uri) + 1;
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 "Hostlist client recieved advertisement containing URI `%s'\n",
1254 if (GNUNET_NO != linked_list_contains (uri))
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URI `%s' is already known\n", uri);
1260 if (GNUNET_NO == stat_testing_allowed)
1263 GNUNET_ERROR_TYPE_DEBUG,
1264 "Currently not accepting new advertisements: interval between to advertisements is not reached\n");
1267 if (GNUNET_YES == stat_testing_hostlist)
1270 GNUNET_ERROR_TYPE_DEBUG,
1271 "Currently not accepting new advertisements: we are already testing a hostlist\n");
1275 hostlist = GNUNET_malloc (sizeof(struct Hostlist) + uri_size);
1276 hostlist->hostlist_uri = (const char *) &hostlist[1];
1277 GNUNET_memcpy (&hostlist[1], uri, uri_size);
1278 hostlist->time_creation = GNUNET_TIME_absolute_get ();
1279 hostlist->quality = HOSTLIST_INITIAL;
1280 hostlist_to_test = hostlist;
1282 stat_testing_hostlist = GNUNET_YES;
1283 stat_testing_allowed = GNUNET_NO;
1284 ti_testing_intervall_task =
1285 GNUNET_SCHEDULER_add_delayed (TESTING_INTERVAL,
1286 &task_testing_intervall_reset,
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Testing new hostlist advertisements is locked for the next %s\n",
1291 GNUNET_STRINGS_relative_time_to_string (TESTING_INTERVAL,
1294 ti_download_dispatcher_task =
1295 GNUNET_SCHEDULER_add_now (&task_download_dispatcher, NULL);
1300 * Continuation called by the statistics code once
1301 * we go the stat. Initiates hostlist download scheduling.
1303 * @param cls closure
1304 * @param success #GNUNET_OK if statistics were
1305 * successfully obtained, #GNUNET_SYSERR if not.
1308 primary_task (void *cls, int success)
1310 if (NULL != ti_check_download)
1312 GNUNET_SCHEDULER_cancel (ti_check_download);
1313 ti_check_download = NULL;
1316 GNUNET_assert (NULL != stats);
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318 "Statistics request done, scheduling hostlist download\n");
1319 ti_check_download = GNUNET_SCHEDULER_add_now (&task_check, NULL);
1324 * Continuation called by the statistics code once
1325 * we go the stat. Initiates hostlist download scheduling.
1327 * @param cls closure
1328 * @param success #GNUNET_OK if statistics were
1329 * successfully obtained, #GNUNET_SYSERR if not.
1332 stat_timeout_task (void *cls)
1334 GNUNET_STATISTICS_get_cancel (sget);
1336 ti_check_download = GNUNET_SCHEDULER_add_now (&task_check, NULL);
1341 * We've received the previous delay value from statistics. Remember it.
1343 * @param cls NULL, unused
1344 * @param subsystem should be "hostlist", unused
1345 * @param name will be "milliseconds between hostlist downloads", unused
1346 * @param value previous delay value, in milliseconds (!)
1347 * @param is_persistent unused, will be #GNUNET_YES
1350 process_stat (void *cls,
1351 const char *subsystem,
1356 hostlist_delay.rel_value_us = value * 1000LL;
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1358 "Initial time between hostlist downloads is %s\n",
1359 GNUNET_STRINGS_relative_time_to_string (hostlist_delay,
1366 * Method to load persistent hostlist file during hostlist client startup
1369 load_hostlist_file ()
1374 struct Hostlist *hostlist;
1375 uint32_t times_used;
1376 uint32_t hellos_returned;
1381 struct GNUNET_BIO_ReadHandle *rh;
1384 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
1389 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
1395 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1396 _ ("Loading saved hostlist entries from file `%s' \n"),
1398 if (GNUNET_NO == GNUNET_DISK_file_test (filename))
1400 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1401 _ ("Hostlist file `%s' does not exist\n"),
1403 GNUNET_free (filename);
1407 rh = GNUNET_BIO_read_open (filename);
1410 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1412 "Could not open file `%s' for reading to load hostlists: %s\n"),
1415 GNUNET_free (filename);
1420 while ((GNUNET_OK == GNUNET_BIO_read_string (rh, "url", &uri, MAX_URL_LEN)) &&
1422 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1423 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1424 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1425 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1426 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)))
1428 hostlist = GNUNET_malloc (sizeof(struct Hostlist) + strlen (uri) + 1);
1429 hostlist->hello_count = hellos_returned;
1430 hostlist->hostlist_uri = (const char *) &hostlist[1];
1431 GNUNET_memcpy (&hostlist[1], uri, strlen (uri) + 1);
1432 hostlist->quality = quality;
1433 hostlist->time_creation.abs_value_us = created;
1434 hostlist->time_last_usage.abs_value_us = last_used;
1435 GNUNET_CONTAINER_DLL_insert (linked_list_head, linked_list_tail, hostlist);
1437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1438 "Added hostlist entry eith URI `%s' \n",
1439 hostlist->hostlist_uri);
1443 if (counter >= MAX_NUMBER_HOSTLISTS)
1447 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1448 _ ("%u hostlist URIs loaded from file\n"),
1450 GNUNET_STATISTICS_set (stats,
1451 gettext_noop ("# hostlist URIs read from file"),
1454 GNUNET_STATISTICS_set (stats,
1455 gettext_noop ("# advertised hostlist URIs"),
1459 GNUNET_free_non_null (uri);
1461 (void) GNUNET_BIO_read_close (rh, &emsg);
1464 GNUNET_free (filename);
1469 * Method to save persistent hostlist file during hostlist client shutdown
1471 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1474 save_hostlist_file (int shutdown)
1477 struct Hostlist *pos;
1478 struct GNUNET_BIO_WriteHandle *wh;
1482 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
1487 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
1492 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
1494 GNUNET_free (filename);
1497 wh = GNUNET_BIO_write_open (filename);
1500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1502 "Could not open file `%s' for writing to save hostlists: %s\n"),
1505 GNUNET_free (filename);
1508 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1509 _ ("Writing %u hostlist URIs to `%s'\n"),
1512 /* add code to write hostlists to file using bio */
1515 while (NULL != (pos = linked_list_head))
1517 if (GNUNET_YES == shutdown)
1519 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1522 if (GNUNET_YES == ok)
1524 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1525 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1526 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1528 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.abs_value_us)) ||
1530 GNUNET_BIO_write_int64 (wh, pos->time_creation.abs_value_us)) ||
1531 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1534 _ ("Error writing hostlist URIs to file `%s'\n"),
1540 if (GNUNET_YES == shutdown)
1543 if (counter >= MAX_NUMBER_HOSTLISTS)
1546 GNUNET_STATISTICS_set (stats,
1547 gettext_noop ("# hostlist URIs written to file"),
1551 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1553 _ ("Error writing hostlist URIs to file `%s'\n"),
1555 GNUNET_free (filename);
1560 * Start downloading hostlists from hostlist servers as necessary.
1562 * @param c configuration to use
1563 * @param st statistics handle to use
1564 * @param[out] ch set to handler for CORE connect events
1565 * @param[out] dh set to handler for CORE disconnect events
1566 * @param[out] msgh set to handler for CORE advertisement messages
1567 * @param learn should we learn hostlist URLs from CORE
1568 * @return #GNUNET_OK on success
1571 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1572 struct GNUNET_STATISTICS_Handle *st,
1573 GNUNET_CORE_ConnectEventHandler *ch,
1574 GNUNET_CORE_DisconnectEventHandler *dh,
1575 GNUNET_HOSTLIST_UriHandler *msgh,
1579 char *proxytype_str;
1582 GNUNET_assert (NULL != st);
1583 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1586 return GNUNET_SYSERR;
1591 /* Read proxy configuration */
1592 pi = GNUNET_PEERINFO_connect (c);
1594 GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST", "PROXY", &proxy))
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found proxy host: `%s'\n", proxy);
1597 /* proxy username */
1598 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Found proxy username name: `%s'\n",
1608 /* proxy password */
1609 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 "Found proxy password name: `%s'\n",
1620 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1625 GNUNET_STRINGS_utf8_toupper (proxytype_str, proxytype_str);
1626 proxy_type = CURLPROXY_HTTP;
1627 if (0 == strcmp (proxytype_str, "HTTP"))
1628 proxy_type = CURLPROXY_HTTP;
1629 else if (0 == strcmp (proxytype_str, "HTTP_1_0"))
1630 proxy_type = CURLPROXY_HTTP_1_0;
1631 else if (0 == strcmp (proxytype_str, "SOCKS4"))
1632 proxy_type = CURLPROXY_SOCKS4;
1633 else if (0 == strcmp (proxytype_str, "SOCKS5"))
1634 proxy_type = CURLPROXY_SOCKS5;
1635 else if (0 == strcmp (proxytype_str, "SOCKS4A"))
1636 proxy_type = CURLPROXY_SOCKS4A;
1637 else if (0 == strcmp (proxytype_str, "SOCKS5_HOSTNAME"))
1638 proxy_type = CURLPROXY_SOCKS5_HOSTNAME;
1642 GNUNET_ERROR_TYPE_ERROR,
1644 "Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
1646 GNUNET_free (proxytype_str);
1647 GNUNET_free (proxy);
1649 GNUNET_free_non_null (proxy_username);
1650 proxy_username = NULL;
1651 GNUNET_free_non_null (proxy_password);
1652 proxy_password = NULL;
1654 return GNUNET_SYSERR;
1657 GNUNET_free_non_null (proxytype_str);
1660 stat_learning = learn;
1661 *ch = &handler_connect;
1662 *dh = &handler_disconnect;
1663 linked_list_head = NULL;
1664 linked_list_tail = NULL;
1665 stat_use_bootstrap = GNUNET_YES;
1666 stat_testing_hostlist = GNUNET_NO;
1667 stat_testing_allowed = GNUNET_YES;
1669 if (GNUNET_YES == stat_learning)
1671 *msgh = &handler_advertisement;
1672 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1673 _ ("Learning is enabled on this peer\n"));
1674 load_hostlist_file ();
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Hostlists will be saved to file again in %s\n",
1677 GNUNET_STRINGS_relative_time_to_string (SAVING_INTERVAL,
1679 ti_saving_task = GNUNET_SCHEDULER_add_delayed (SAVING_INTERVAL,
1680 &task_hostlist_saving,
1685 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1686 _ ("Learning is not enabled on this peer\n"));
1688 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
1693 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1695 result = remove (filename);
1698 GNUNET_ERROR_TYPE_INFO,
1700 "Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),
1703 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1708 GNUNET_free (filename);
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "Loading stats value on hostlist download frequency\n");
1712 sget = GNUNET_STATISTICS_get (stats,
1715 "# milliseconds between hostlist downloads"),
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1722 "Statistics request failed, scheduling hostlist download\n");
1723 ti_check_download = GNUNET_SCHEDULER_add_now (&task_check, NULL);
1727 ti_check_download = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1736 * Stop downloading hostlists from hostlist servers as necessary.
1739 GNUNET_HOSTLIST_client_stop ()
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist client shutdown\n");
1744 GNUNET_STATISTICS_get_cancel (sget);
1748 if (GNUNET_YES == stat_learning)
1749 save_hostlist_file (GNUNET_YES);
1750 if (NULL != ti_saving_task)
1752 GNUNET_SCHEDULER_cancel (ti_saving_task);
1753 ti_saving_task = NULL;
1755 if (NULL != ti_download_dispatcher_task)
1757 GNUNET_SCHEDULER_cancel (ti_download_dispatcher_task);
1758 ti_download_dispatcher_task = NULL;
1760 if (NULL != ti_testing_intervall_task)
1762 GNUNET_SCHEDULER_cancel (ti_testing_intervall_task);
1763 ti_testing_intervall_task = NULL;
1765 if (NULL != ti_download)
1767 GNUNET_SCHEDULER_cancel (ti_download);
1772 if (NULL != ti_check_download)
1774 GNUNET_SCHEDULER_cancel (ti_check_download);
1775 ti_check_download = NULL;
1776 curl_global_cleanup ();
1778 GNUNET_free_non_null (proxy);
1780 GNUNET_free_non_null (proxy_username);
1781 proxy_username = NULL;
1782 GNUNET_free_non_null (proxy_password);
1783 proxy_password = NULL;
1786 GNUNET_PEERINFO_disconnect (pi);
1793 /* end of gnunet-daemon-hostlist_client.c */