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 2, 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_YES
41 #define MAX_URL_LEN 1000
44 * Number of connections that we must have to NOT download
47 #define MIN_CONNECTIONS 4
50 * A single hostlist obtained by hostlist advertisements
55 * previous entry, used to manage entries in a double linked list
57 struct Hostlist * prev;
60 * next entry, used to manage entries in a double linked list
62 struct Hostlist * next;
65 * URI where hostlist can be obtained
67 const char *hostlist_uri;
70 * Value describing the quality of the hostlist, the bigger the better but (should) never < 0
71 * used for deciding which hostlist is replaced if MAX_NUMBER_HOSTLISTS in data structure is reached
72 * intial value = HOSTLIST_INITIAL
73 * increased every successful download by HOSTLIST_SUCCESSFULL_DOWNLOAD
74 * increased every successful download by number of obtained HELLO messages
75 * decreased every failed download by HOSTLIST_SUCCESSFULL_DOWNLOAD
80 * Time the hostlist advertisement was recieved and the entry was created
82 struct GNUNET_TIME_Absolute time_creation;
85 * Last time the hostlist was obtained
87 struct GNUNET_TIME_Absolute time_last_usage;
90 * Number of HELLO messages obtained during last download
95 * Number of times the hostlist was successfully obtained
105 static const struct GNUNET_CONFIGURATION_Handle *cfg;
110 static struct GNUNET_SCHEDULER_Handle *sched;
115 struct GNUNET_STATISTICS_Handle *stats;
120 struct GNUNET_TRANSPORT_Handle *transport;
123 * Proxy that we are using (can be NULL).
128 * Buffer for data downloaded via HTTP.
130 static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
133 * Number of bytes valid in 'download_buffer'.
135 static size_t download_pos;
138 * Current URL that we are using.
140 static char *current_url;
143 * Current CURL handle.
148 * Current multi-CURL handle.
153 * ID of the current task scheduled.
155 static GNUNET_SCHEDULER_TaskIdentifier current_task;
158 * ID of the current hostlist saving task scheduled.
160 static GNUNET_SCHEDULER_TaskIdentifier saving_task;
163 * Amount of time we wait between hostlist downloads.
165 static struct GNUNET_TIME_Relative hostlist_delay;
168 * Set to GNUNET_YES if the current URL had some problems.
170 static int bogus_url;
173 * Number of active connections (according to core service).
175 static unsigned int connection_count;
178 * At what time MUST the current hostlist request be done?
180 static struct GNUNET_TIME_Absolute end_time;
183 * Head of the linked list used to store hostlists
185 static struct Hostlist * linked_list_head;
188 * Tail of the linked list used to store hostlists
190 static struct Hostlist * linked_list_tail;
193 * Current hostlist used for downloading
195 static struct Hostlist * current_hostlist;
198 * Size of the linke list used to store hostlists
200 static unsigned int linked_list_size;
203 * Value saying if preconfigured is used
205 static unsigned int use_preconfigured_list;
208 * Set if we are allowed to learn new hostlists and use them
213 * Value saying how many valid HELLO messages were obtained during download
215 static unsigned int hellos_obtained;
218 * Value saying if hostlist download was successful
220 static unsigned int download_successful;
223 * Process downloaded bits by calling callback on each HELLO.
225 * @param ptr buffer with downloaded data
226 * @param size size of a record
227 * @param nmemb number of records downloaded
229 * @return number of bytes that were processed (always size*nmemb)
232 download_hostlist_processor (void *ptr,
237 const char * cbuf = ptr;
238 const struct GNUNET_MessageHeader *msg;
244 total = size * nmemb;
245 if ( (total == 0) || (bogus_url) )
247 return total; /* ok, no data or bogus data */
249 GNUNET_STATISTICS_update (stats,
250 gettext_noop ("# bytes downloaded from hostlist servers"),
254 while ( (left > 0) ||
257 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
258 memcpy (&download_buffer[download_pos],
264 if (download_pos < sizeof(struct GNUNET_MessageHeader))
266 GNUNET_assert (left == 0);
269 msg = (const struct GNUNET_MessageHeader *) download_buffer;
270 msize = ntohs(msg->size);
271 if (msize < sizeof(struct GNUNET_MessageHeader))
273 GNUNET_STATISTICS_update (stats,
274 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278 _("Invalid `%s' message received from hostlist at `%s'\n"),
284 if (download_pos < msize)
286 GNUNET_assert (left == 0);
289 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
291 #if DEBUG_HOSTLIST_CLIENT
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Received valid `%s' message from hostlist server.\n",
296 GNUNET_STATISTICS_update (stats,
297 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
301 GNUNET_TRANSPORT_offer_hello (transport, msg);
305 GNUNET_STATISTICS_update (stats,
306 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
309 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
310 _("Invalid `%s' message received from hostlist at `%s'\n"),
313 bogus_url = GNUNET_YES;
316 memmove (download_buffer,
317 &download_buffer[msize],
318 download_pos - msize);
319 download_pos -= msize;
326 * Obtain a hostlist URL that we should use.
328 * @return NULL if there is no URL available
339 GNUNET_CONFIGURATION_get_value_string (cfg,
344 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
345 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
346 "SERVERS", "HOSTLIST");
351 if (strlen (servers) > 0)
354 pos = strlen (servers) - 1;
357 if (servers[pos] == ' ')
364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
365 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
366 "SERVERS", "HOSTLIST");
367 GNUNET_free (servers);
371 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
372 pos = strlen (servers) - 1;
375 if (servers[pos] == ' ')
387 ret = GNUNET_strdup (&servers[pos]);
388 GNUNET_free (servers);
393 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
394 * @return uri to use, NULL if there is no URL available
400 unsigned int counter;
401 struct Hostlist * pos;
403 if ( GNUNET_NO == learning)
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Using preconfigured bootstrap server\n");
407 current_hostlist = NULL;
408 return get_bootstrap_url();
410 if ( (GNUNET_YES == use_preconfigured_list) ||
411 (linked_list_size == 0) )
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 "Using preconfigured bootstrap server\n");
415 current_hostlist = NULL;
416 return get_bootstrap_url();
418 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
420 pos = linked_list_head;
421 while ( counter < index )
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Using learned hostlist `%s'\n", pos->hostlist_uri);
428 current_hostlist = pos;
429 return strdup(pos->hostlist_uri);
433 #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);
437 * Schedule the background task that will (possibly)
438 * download a hostlist.
441 schedule_hostlist_task (void);
444 * Method to load persistent hostlist file during hostlist client shutdown
445 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
447 static void save_hostlist_file ( int shutdown );
450 * add val2 to val1 with overflow check
451 * @param val1 value 1
452 * @param val2 value 2
455 static uint64_t checked_add (uint64_t val1, uint64_t val2)
457 static uint64_t temp;
458 static uint64_t maxv;
471 * Subtract val2 from val1 with underflow check
472 * @param val1 value 1
473 * @param val2 value 2
476 static uint64_t checked_sub (uint64_t val1, uint64_t val2)
486 * Method updating hostlist statistics
488 static void update_hostlist ( )
490 if ( (use_preconfigured_list == GNUNET_NO) && ( NULL != current_hostlist ) )
492 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
493 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
494 current_hostlist->hello_count = hellos_obtained;
495 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
496 current_hostlist->quality = checked_add ( current_hostlist->quality, (hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
497 if ( GNUNET_YES == download_successful )
499 current_hostlist->times_used++;
500 current_hostlist->quality = checked_add ( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
503 current_hostlist->quality = checked_sub ( current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD );
505 current_hostlist = NULL;
506 /* Alternating the usage of preconfigured and learned hostlists */
508 if ( GNUNET_YES == learning)
510 if (use_preconfigured_list == GNUNET_YES)
511 use_preconfigured_list = GNUNET_NO;
513 use_preconfigured_list = GNUNET_YES;
516 use_preconfigured_list = GNUNET_YES;
520 * Clean up the state from the task that downloaded the
521 * hostlist and schedule the next task.
530 mret = curl_multi_remove_handle (multi, curl);
531 if (mret != CURLM_OK)
533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
534 _("%s failed at %s:%d: `%s'\n"),
535 "curl_multi_remove_handle", __FILE__, __LINE__,
536 curl_multi_strerror (mret));
538 mret = curl_multi_cleanup (multi);
539 if (mret != CURLM_OK)
540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
541 _("%s failed at %s:%d: `%s'\n"),
542 "curl_multi_cleanup", __FILE__, __LINE__,
543 curl_multi_strerror (mret));
548 curl_easy_cleanup (curl);
551 GNUNET_free_non_null (current_url);
554 schedule_hostlist_task ();
559 * Ask CURL for the select set and then schedule the
560 * receiving task with the scheduler.
567 * Task that is run when we are ready to receive more data from the hostlist
570 * @param cls closure, unused
571 * @param tc task context, unused
574 multi_ready (void *cls,
575 const struct GNUNET_SCHEDULER_TaskContext *tc)
581 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
583 #if DEBUG_HOSTLIST_CLIENT
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
585 "Shutdown requested while trying to download hostlist from `%s'\n",
592 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
595 _("Timeout trying to download hostlist from `%s'\n"),
601 #if DEBUG_HOSTLIST_CLIENT
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Ready for processing hostlist client request\n");
608 mret = curl_multi_perform (multi, &running);
613 msg = curl_multi_info_read (multi, &running);
614 GNUNET_break (msg != NULL);
620 if ( (msg->data.result != CURLE_OK) &&
621 (msg->data.result != CURLE_GOT_NOTHING) )
622 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
623 _("%s failed for `%s' at %s:%d: `%s'\n"),
624 "curl_multi_perform",
628 curl_easy_strerror (msg->data.result));
631 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
632 _("Download of hostlist `%s' completed.\n"),
634 download_successful = GNUNET_YES;
646 while (mret == CURLM_CALL_MULTI_PERFORM);
647 if (mret != CURLM_OK)
649 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
650 _("%s failed at %s:%d: `%s'\n"),
651 "curl_multi_perform", __FILE__, __LINE__,
652 curl_multi_strerror (mret));
660 * Ask CURL for the select set and then schedule the
661 * receiving task with the scheduler.
671 struct GNUNET_NETWORK_FDSet *grs;
672 struct GNUNET_NETWORK_FDSet *gws;
674 struct GNUNET_TIME_Relative rtime;
680 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
681 if (mret != CURLM_OK)
683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684 _("%s failed at %s:%d: `%s'\n"),
685 "curl_multi_fdset", __FILE__, __LINE__,
686 curl_multi_strerror (mret));
690 mret = curl_multi_timeout (multi, &timeout);
691 if (mret != CURLM_OK)
693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
694 _("%s failed at %s:%d: `%s'\n"),
695 "curl_multi_timeout", __FILE__, __LINE__,
696 curl_multi_strerror (mret));
700 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
701 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
703 grs = GNUNET_NETWORK_fdset_create ();
704 gws = GNUNET_NETWORK_fdset_create ();
705 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
706 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
707 #if DEBUG_HOSTLIST_CLIENT
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Scheduling task for hostlist download using cURL\n");
712 = GNUNET_SCHEDULER_add_select (sched,
713 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
714 GNUNET_SCHEDULER_NO_TASK,
720 GNUNET_NETWORK_fdset_destroy (gws);
721 GNUNET_NETWORK_fdset_destroy (grs);
726 * Main function that will download a hostlist and process its
735 current_url = get_list_url ();
736 if (current_url == NULL)
738 curl = curl_easy_init ();
746 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
747 _("Bootstrapping using hostlist at `%s'.\n"),
750 download_successful = GNUNET_NO;
751 GNUNET_STATISTICS_update (stats,
752 gettext_noop ("# hostlist downloads initiated"),
756 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
759 CURL_EASY_SETOPT (curl,
760 CURLOPT_WRITEFUNCTION,
761 &download_hostlist_processor);
767 CURL_EASY_SETOPT (curl,
775 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
776 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
777 /* no need to abort if the above failed */
778 CURL_EASY_SETOPT (curl,
786 CURL_EASY_SETOPT (curl,
790 CURL_EASY_SETOPT (curl,
794 CURL_EASY_SETOPT (curl,
796 GNUNET_SERVER_MAX_MESSAGE_SIZE);
797 if (0 == strncmp (current_url, "http", 4))
798 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
799 CURL_EASY_SETOPT (curl,
800 CURLOPT_CONNECTTIMEOUT,
802 CURL_EASY_SETOPT (curl,
806 /* this should no longer be needed; we're now single-threaded! */
807 CURL_EASY_SETOPT (curl,
811 multi = curl_multi_init ();
818 mret = curl_multi_add_handle (multi, curl);
819 if (mret != CURLM_OK)
821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
822 _("%s failed at %s:%d: `%s'\n"),
823 "curl_multi_add_handle", __FILE__, __LINE__,
824 curl_multi_strerror (mret));
825 mret = curl_multi_cleanup (multi);
826 if (mret != CURLM_OK)
827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
828 _("%s failed at %s:%d: `%s'\n"),
829 "curl_multi_cleanup", __FILE__, __LINE__,
830 curl_multi_strerror (mret));
835 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
841 * Task that checks if we should try to download a hostlist.
842 * If so, we initiate the download, otherwise we schedule
843 * this task again for a later time.
846 check_task (void *cls,
847 const struct GNUNET_SCHEDULER_TaskContext *tc)
849 current_task = GNUNET_SCHEDULER_NO_TASK;
850 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
852 if (connection_count < MIN_CONNECTIONS)
853 download_hostlist ();
855 schedule_hostlist_task ();
860 * Compute when we should check the next time about downloading
861 * a hostlist; then schedule the task accordingly.
864 schedule_hostlist_task ()
867 struct GNUNET_TIME_Relative delay;
871 curl_global_cleanup ();
872 return; /* in shutdown */
874 delay = hostlist_delay;
875 if (hostlist_delay.value == 0)
876 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
878 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
879 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
880 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
881 (1 + connection_count));
882 GNUNET_STATISTICS_set (stats,
883 gettext_noop("# seconds between hostlist downloads"),
884 hostlist_delay.value,
888 delay = GNUNET_TIME_UNIT_ZERO;
891 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
892 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
895 (unsigned long long) delay.value);
896 current_task = GNUNET_SCHEDULER_add_delayed (sched,
903 * Task that writes hostlist entries to a file on a regular base
908 hostlist_saving_task (void *cls,
909 const struct GNUNET_SCHEDULER_TaskContext *tc)
911 saving_task = GNUNET_SCHEDULER_NO_TASK;
912 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
914 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
915 _("Scheduled saving of hostlists\n"));
916 save_hostlist_file ( GNUNET_NO );
918 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
919 _("Hostlists will be saved to file again in %llums\n"),
920 (unsigned long long) SAVING_INTERVALL.value);
921 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
923 &hostlist_saving_task,
928 * Method called whenever a given peer connects.
931 * @param peer peer identity this notification is about
932 * @param latency reported latency of the connection with 'other'
933 * @param distance reported distance (DV) to 'other'
936 connect_handler (void *cls,
938 GNUNET_PeerIdentity * peer,
939 struct GNUNET_TIME_Relative latency,
943 GNUNET_STATISTICS_update (stats,
944 gettext_noop ("# active connections"),
951 * Method called whenever a given peer disconnects.
954 * @param peer peer identity this notification is about
957 disconnect_handler (void *cls,
959 GNUNET_PeerIdentity * peer)
962 GNUNET_STATISTICS_update (stats,
963 gettext_noop ("# active connections"),
970 * Method to check if URI is in hostlist linked list
971 * @param uri uri to check
972 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
975 linked_list_contains (const char * uri)
977 struct Hostlist * pos;
979 pos = linked_list_head;
982 if (0 == strcmp(pos->hostlist_uri, uri) )
991 static struct Hostlist *
992 linked_list_get_lowest_quality ( )
994 struct Hostlist * pos;
995 struct Hostlist * lowest;
997 if (linked_list_size == 0)
999 lowest = linked_list_head;
1000 pos = linked_list_head->next;
1003 if (pos->quality < lowest->quality)
1012 * Method called whenever an advertisement message arrives.
1014 * @param cls closure (always NULL)
1015 * @param peer the peer sending the message
1016 * @param message the actual message
1017 * @param latency latency
1018 * @param distance distance
1019 * @return GNUNET_OK to keep the connection open,
1020 * GNUNET_SYSERR to close it (signal serious error)
1023 advertisement_handler (void *cls,
1024 const struct GNUNET_PeerIdentity * peer,
1025 const struct GNUNET_MessageHeader * message,
1026 struct GNUNET_TIME_Relative latency,
1031 const struct GNUNET_MessageHeader * incoming;
1033 struct Hostlist * hostlist;
1035 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1036 size = ntohs (message->size);
1037 if (size <= sizeof(struct GNUNET_MessageHeader))
1039 GNUNET_break_op (0);
1040 return GNUNET_SYSERR;
1042 incoming = (const struct GNUNET_MessageHeader *) message;
1043 uri = (const char*) &incoming[1];
1044 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1045 if (uri [uri_size - 1] != '\0')
1047 GNUNET_break_op (0);
1048 return GNUNET_SYSERR;
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1054 if (GNUNET_NO != linked_list_contains (uri))
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "URI `%s' is already known\n",
1061 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1062 hostlist->hostlist_uri = (const char*) &hostlist[1];
1063 memcpy (&hostlist[1], uri, uri_size);
1064 hostlist->time_creation = GNUNET_TIME_absolute_get();
1065 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1066 hostlist->quality = HOSTLIST_INITIAL;
1068 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1071 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
1074 /* No free entries available, replace existing entry */
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Removing lowest quality entry\n" );
1077 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
1080 lowest_quality->hostlist_uri,
1081 (unsigned long long) lowest_quality->quality);
1082 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
1084 GNUNET_free (lowest_quality);
1090 * Continuation called by the statistics code once
1091 * we go the stat. Initiates hostlist download scheduling.
1093 * @param cls closure
1094 * @param success GNUNET_OK if statistics were
1095 * successfully obtained, GNUNET_SYSERR if not.
1098 primary_task (void *cls, int success)
1101 return; /* in shutdown */
1102 #if DEBUG_HOSTLIST_CLIENT
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Statistics request done, scheduling hostlist download\n");
1106 schedule_hostlist_task ();
1111 process_stat (void *cls,
1112 const char *subsystem,
1117 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1118 _("Initial time between hostlist downloads is %llums\n"),
1119 (unsigned long long) value);
1120 hostlist_delay.value = value;
1125 * Method to load persistent hostlist file during hostlist client startup
1128 load_hostlist_file ()
1133 struct Hostlist * hostlist;
1135 uint32_t times_used;
1136 uint32_t hellos_returned;
1143 GNUNET_CONFIGURATION_get_value_string (cfg,
1148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1149 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1150 "HOSTLISTFILE", "HOSTLIST");
1154 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1155 _("Loading saved hostlist entries from file `%s' \n"), filename);
1157 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1160 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1161 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1164 GNUNET_free (filename);
1169 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1170 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1171 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1172 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1173 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1174 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1176 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1177 hostlist->hello_count = hellos_returned;
1178 hostlist->hostlist_uri = (const char *) &hostlist[1];
1179 memcpy (&hostlist[1], uri, strlen(uri)+1);
1180 hostlist->quality = quality;
1181 hostlist->time_creation.value = created;
1182 hostlist->time_last_usage.value = last_used;
1183 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1190 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1193 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1194 _("%u hostlist URIs loaded from file\n"), counter);
1195 GNUNET_STATISTICS_set (stats,
1196 gettext_noop("# hostlis URIs read from file"),
1200 GNUNET_free_non_null (uri);
1202 GNUNET_BIO_read_close (rh, &emsg);
1205 GNUNET_free (filename);
1210 * Method to load persistent hostlist file during hostlist client shutdown
1211 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1213 static void save_hostlist_file ( int shutdown )
1216 struct Hostlist *pos;
1217 struct GNUNET_BIO_WriteHandle * wh;
1222 GNUNET_CONFIGURATION_get_value_string (cfg,
1227 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1228 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1229 "HOSTLISTFILE", "HOSTLIST");
1230 GNUNET_free (filename);
1233 wh = GNUNET_BIO_write_open (filename);
1236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1237 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1240 GNUNET_free (filename);
1243 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1244 _("Writing %u hostlist URIs to `%s'\n" ),
1245 linked_list_size, filename);
1247 /* add code to write hostlists to file using bio */
1250 while (NULL != (pos = linked_list_head))
1252 if ( GNUNET_YES == shutdown)
1254 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1257 if (GNUNET_YES == ok)
1260 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1262 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1264 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1266 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1268 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1270 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1273 _("Error writing hostlist URIs to file `%s'\n"),
1279 if ( GNUNET_YES == shutdown)
1282 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1284 GNUNET_STATISTICS_set (stats,
1285 gettext_noop("# hostlist URIs written to file"),
1289 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1291 _("Error writing hostlist URIs to file `%s'\n"),
1293 GNUNET_free (filename);
1297 * Start downloading hostlists from hostlist servers as necessary.
1300 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1301 struct GNUNET_SCHEDULER_Handle *s,
1302 struct GNUNET_STATISTICS_Handle *st,
1303 GNUNET_CORE_ConnectEventHandler *ch,
1304 GNUNET_CORE_DisconnectEventHandler *dh,
1305 GNUNET_CORE_MessageCallback *msgh,
1311 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1314 return GNUNET_SYSERR;
1316 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1317 if (NULL == transport)
1319 curl_global_cleanup ();
1320 return GNUNET_SYSERR;
1326 GNUNET_CONFIGURATION_get_value_string (cfg,
1331 *ch = &connect_handler;
1332 *dh = &disconnect_handler;
1334 *msgh = &advertisement_handler;
1337 linked_list_head = NULL;
1338 linked_list_tail = NULL;
1339 use_preconfigured_list = GNUNET_YES;
1341 if ( GNUNET_YES == learning )
1343 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1344 _("Learning is enabled on this peer\n"));
1345 load_hostlist_file ();
1346 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1347 _("Hostlists will be saved to file again in %llums\n"),
1348 (unsigned long long) SAVING_INTERVALL.value);
1349 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1351 &hostlist_saving_task,
1356 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1357 _("Learning is not enabled on this peer\n"));
1358 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1363 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1365 result = remove (filename);
1367 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1368 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),filename);
1370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1371 _("Hostlist file `%s' could not be removed\n"),filename);
1374 GNUNET_free ( filename );
1376 GNUNET_STATISTICS_get (stats,
1378 gettext_noop("# seconds between hostlist downloads"),
1379 GNUNET_TIME_UNIT_MINUTES,
1388 * Stop downloading hostlists from hostlist servers as necessary.
1391 GNUNET_HOSTLIST_client_stop ()
1393 #if DEBUG_HOSTLIST_CLIENT
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Hostlist client shutdown\n");
1397 save_hostlist_file ( GNUNET_YES );
1399 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1401 GNUNET_SCHEDULER_cancel (sched,
1403 curl_global_cleanup ();
1405 if (transport != NULL)
1407 GNUNET_TRANSPORT_disconnect (transport);
1410 GNUNET_assert (NULL == transport);
1411 GNUNET_free_non_null (proxy);
1417 /* end of hostlist-client.c */