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;
199 * Size of the linke list used to store hostlists
201 static unsigned int linked_list_size;
204 * Value saying if preconfigured is used
206 static unsigned int use_preconfigured_list;
209 * Set if we are allowed to learn new hostlists and use them
214 * Value saying how many valid HELLO messages were obtained during download
216 static unsigned int hellos_obtained;
219 * Value saying how many valid HELLO messages were obtained during download
221 static unsigned int download_successful;
224 * Process downloaded bits by calling callback on each HELLO.
226 * @param ptr buffer with downloaded data
227 * @param size size of a record
228 * @param nmemb number of records downloaded
230 * @return number of bytes that were processed (always size*nmemb)
233 download_hostlist_processor (void *ptr,
238 const char * cbuf = ptr;
239 const struct GNUNET_MessageHeader *msg;
245 total = size * nmemb;
246 if ( (total == 0) || (bogus_url) )
248 return total; /* ok, no data or bogus data */
250 GNUNET_STATISTICS_update (stats,
251 gettext_noop ("# bytes downloaded from hostlist servers"),
255 while ( (left > 0) ||
258 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
259 memcpy (&download_buffer[download_pos],
265 if (download_pos < sizeof(struct GNUNET_MessageHeader))
267 GNUNET_assert (left == 0);
270 msg = (const struct GNUNET_MessageHeader *) download_buffer;
271 msize = ntohs(msg->size);
272 if (msize < sizeof(struct GNUNET_MessageHeader))
274 GNUNET_STATISTICS_update (stats,
275 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
279 _("Invalid `%s' message received from hostlist at `%s'\n"),
285 if (download_pos < msize)
287 GNUNET_assert (left == 0);
290 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
292 #if DEBUG_HOSTLIST_CLIENT
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Received valid `%s' message from hostlist server.\n",
297 GNUNET_STATISTICS_update (stats,
298 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
302 GNUNET_TRANSPORT_offer_hello (transport, msg);
306 GNUNET_STATISTICS_update (stats,
307 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311 _("Invalid `%s' message received from hostlist at `%s'\n"),
314 bogus_url = GNUNET_YES;
317 memmove (download_buffer,
318 &download_buffer[msize],
319 download_pos - msize);
320 download_pos -= msize;
327 * Obtain a hostlist URL that we should use.
329 * @return NULL if there is no URL available
340 GNUNET_CONFIGURATION_get_value_string (cfg,
345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
346 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
347 "SERVERS", "HOSTLIST");
352 if (strlen (servers) > 0)
355 pos = strlen (servers) - 1;
358 if (servers[pos] == ' ')
365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
367 "SERVERS", "HOSTLIST");
368 GNUNET_free (servers);
372 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
373 pos = strlen (servers) - 1;
376 if (servers[pos] == ' ')
388 ret = GNUNET_strdup (&servers[pos]);
389 GNUNET_free (servers);
394 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
395 * @return uri to use, NULL if there is no URL available
401 unsigned int counter;
402 struct Hostlist * pos;
404 if ( GNUNET_NO == learning)
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Using preconfigured bootstrap server\n");
408 current_hostlist = NULL;
409 return get_bootstrap_url();
411 if ( (GNUNET_YES == use_preconfigured_list) ||
412 (linked_list_size == 0) )
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Using preconfigured bootstrap server\n");
416 current_hostlist = NULL;
417 return get_bootstrap_url();
419 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
421 pos = linked_list_head;
422 while ( counter < index )
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Using learned hostlist `%s'\n", pos->hostlist_uri);
429 current_hostlist = pos;
430 return strdup(pos->hostlist_uri);
434 #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);
438 * Schedule the background task that will (possibly)
439 * download a hostlist.
442 schedule_hostlist_task (void);
445 * Method to load persistent hostlist file during hostlist client shutdown
446 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
448 static void save_hostlist_file ( int shutdown );
451 * add val2 to val1 with overflow check
452 * return = val1 + val2
457 static uint64_t checked_add (uint64_t val1, uint64_t val2)
459 static uint64_t temp;
460 static uint64_t maxv;
473 * substract val2 from val1 with underflow check
474 * return = val1 - val2
479 static uint64_t checked_sub (uint64_t val1, uint64_t val2)
489 * Method updating hostlist statistics
491 static void update_hostlist ( )
493 if ( (use_preconfigured_list == GNUNET_NO) && ( NULL != current_hostlist ) )
495 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
496 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
497 current_hostlist->hello_count = hellos_obtained;
498 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
499 current_hostlist->quality = checked_add ( current_hostlist->quality, (hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
500 if ( GNUNET_YES == download_successful )
502 current_hostlist->times_used++;
503 current_hostlist->quality = checked_add( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
506 current_hostlist->quality = checked_sub ( current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD );
508 current_hostlist = NULL;
509 /* Alternating the usage of preconfigured and learned hostlists */
511 if ( GNUNET_YES == learning)
513 if (use_preconfigured_list == GNUNET_YES)
514 use_preconfigured_list = GNUNET_NO;
516 use_preconfigured_list = GNUNET_YES;
519 use_preconfigured_list = GNUNET_YES;
523 * Clean up the state from the task that downloaded the
524 * hostlist and schedule the next task.
533 mret = curl_multi_remove_handle (multi, curl);
534 if (mret != CURLM_OK)
536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
537 _("%s failed at %s:%d: `%s'\n"),
538 "curl_multi_remove_handle", __FILE__, __LINE__,
539 curl_multi_strerror (mret));
541 mret = curl_multi_cleanup (multi);
542 if (mret != CURLM_OK)
543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
544 _("%s failed at %s:%d: `%s'\n"),
545 "curl_multi_cleanup", __FILE__, __LINE__,
546 curl_multi_strerror (mret));
551 curl_easy_cleanup (curl);
554 GNUNET_free_non_null (current_url);
557 schedule_hostlist_task ();
562 * Ask CURL for the select set and then schedule the
563 * receiving task with the scheduler.
570 * Task that is run when we are ready to receive more data from the hostlist
573 * @param cls closure, unused
574 * @param tc task context, unused
577 multi_ready (void *cls,
578 const struct GNUNET_SCHEDULER_TaskContext *tc)
584 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
586 #if DEBUG_HOSTLIST_CLIENT
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Shutdown requested while trying to download hostlist from `%s'\n",
595 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
598 _("Timeout trying to download hostlist from `%s'\n"),
604 #if DEBUG_HOSTLIST_CLIENT
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Ready for processing hostlist client request\n");
611 mret = curl_multi_perform (multi, &running);
616 msg = curl_multi_info_read (multi, &running);
617 GNUNET_break (msg != NULL);
623 if ( (msg->data.result != CURLE_OK) &&
624 (msg->data.result != CURLE_GOT_NOTHING) )
625 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
626 _("%s failed for `%s' at %s:%d: `%s'\n"),
627 "curl_multi_perform",
631 curl_easy_strerror (msg->data.result));
634 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
635 _("Download of hostlist `%s' completed.\n"),
637 download_successful = GNUNET_YES;
649 while (mret == CURLM_CALL_MULTI_PERFORM);
650 if (mret != CURLM_OK)
652 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
653 _("%s failed at %s:%d: `%s'\n"),
654 "curl_multi_perform", __FILE__, __LINE__,
655 curl_multi_strerror (mret));
663 * Ask CURL for the select set and then schedule the
664 * receiving task with the scheduler.
674 struct GNUNET_NETWORK_FDSet *grs;
675 struct GNUNET_NETWORK_FDSet *gws;
677 struct GNUNET_TIME_Relative rtime;
683 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
684 if (mret != CURLM_OK)
686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
687 _("%s failed at %s:%d: `%s'\n"),
688 "curl_multi_fdset", __FILE__, __LINE__,
689 curl_multi_strerror (mret));
693 mret = curl_multi_timeout (multi, &timeout);
694 if (mret != CURLM_OK)
696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
697 _("%s failed at %s:%d: `%s'\n"),
698 "curl_multi_timeout", __FILE__, __LINE__,
699 curl_multi_strerror (mret));
703 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
704 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
706 grs = GNUNET_NETWORK_fdset_create ();
707 gws = GNUNET_NETWORK_fdset_create ();
708 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
709 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
710 #if DEBUG_HOSTLIST_CLIENT
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Scheduling task for hostlist download using cURL\n");
715 = GNUNET_SCHEDULER_add_select (sched,
716 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
717 GNUNET_SCHEDULER_NO_TASK,
723 GNUNET_NETWORK_fdset_destroy (gws);
724 GNUNET_NETWORK_fdset_destroy (grs);
729 * Main function that will download a hostlist and process its
738 current_url = get_list_url ();
739 if (current_url == NULL)
741 curl = curl_easy_init ();
749 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
750 _("Bootstrapping using hostlist at `%s'.\n"),
753 download_successful = GNUNET_NO;
754 GNUNET_STATISTICS_update (stats,
755 gettext_noop ("# hostlist downloads initiated"),
759 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
762 CURL_EASY_SETOPT (curl,
763 CURLOPT_WRITEFUNCTION,
764 &download_hostlist_processor);
770 CURL_EASY_SETOPT (curl,
778 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
779 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
780 /* no need to abort if the above failed */
781 CURL_EASY_SETOPT (curl,
789 CURL_EASY_SETOPT (curl,
793 CURL_EASY_SETOPT (curl,
797 CURL_EASY_SETOPT (curl,
799 GNUNET_SERVER_MAX_MESSAGE_SIZE);
800 if (0 == strncmp (current_url, "http", 4))
801 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
802 CURL_EASY_SETOPT (curl,
803 CURLOPT_CONNECTTIMEOUT,
805 CURL_EASY_SETOPT (curl,
809 /* this should no longer be needed; we're now single-threaded! */
810 CURL_EASY_SETOPT (curl,
814 multi = curl_multi_init ();
821 mret = curl_multi_add_handle (multi, curl);
822 if (mret != CURLM_OK)
824 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
825 _("%s failed at %s:%d: `%s'\n"),
826 "curl_multi_add_handle", __FILE__, __LINE__,
827 curl_multi_strerror (mret));
828 mret = curl_multi_cleanup (multi);
829 if (mret != CURLM_OK)
830 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
831 _("%s failed at %s:%d: `%s'\n"),
832 "curl_multi_cleanup", __FILE__, __LINE__,
833 curl_multi_strerror (mret));
838 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
844 * Task that checks if we should try to download a hostlist.
845 * If so, we initiate the download, otherwise we schedule
846 * this task again for a later time.
849 check_task (void *cls,
850 const struct GNUNET_SCHEDULER_TaskContext *tc)
852 current_task = GNUNET_SCHEDULER_NO_TASK;
853 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
855 if (connection_count < MIN_CONNECTIONS)
856 download_hostlist ();
858 schedule_hostlist_task ();
863 * Compute when we should check the next time about downloading
864 * a hostlist; then schedule the task accordingly.
867 schedule_hostlist_task ()
870 struct GNUNET_TIME_Relative delay;
874 curl_global_cleanup ();
875 return; /* in shutdown */
877 delay = hostlist_delay;
878 if (hostlist_delay.value == 0)
879 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
881 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
882 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
883 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
884 (1 + connection_count));
885 GNUNET_STATISTICS_set (stats,
886 gettext_noop("# seconds between hostlist downloads"),
887 hostlist_delay.value,
891 delay = GNUNET_TIME_UNIT_ZERO;
894 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
895 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
898 (unsigned long long) delay.value);
899 current_task = GNUNET_SCHEDULER_add_delayed (sched,
906 * Task that writes hostlist entries to a file on a regular base
911 hostlist_saving_task (void *cls,
912 const struct GNUNET_SCHEDULER_TaskContext *tc)
914 saving_task = GNUNET_SCHEDULER_NO_TASK;
915 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
917 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
918 _("Scheduled saving of hostlists\n"));
919 save_hostlist_file ( GNUNET_NO );
921 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
922 _("Hostlists will be saved to file again in %llums\n"),
923 (unsigned long long) SAVING_INTERVALL.value);
924 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
926 &hostlist_saving_task,
931 * Method called whenever a given peer connects.
934 * @param peer peer identity this notification is about
935 * @param latency reported latency of the connection with 'other'
936 * @param distance reported distance (DV) to 'other'
939 connect_handler (void *cls,
941 GNUNET_PeerIdentity * peer,
942 struct GNUNET_TIME_Relative latency,
946 GNUNET_STATISTICS_update (stats,
947 gettext_noop ("# active connections"),
954 * Method called whenever a given peer disconnects.
957 * @param peer peer identity this notification is about
960 disconnect_handler (void *cls,
962 GNUNET_PeerIdentity * peer)
965 GNUNET_STATISTICS_update (stats,
966 gettext_noop ("# active connections"),
973 * Method to check if URI is in hostlist linked list
974 * @param uri uri to check
975 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
978 linked_list_contains (const char * uri)
980 struct Hostlist * pos;
982 pos = linked_list_head;
985 if (0 == strcmp(pos->hostlist_uri, uri) )
994 static struct Hostlist *
995 linked_list_get_lowest_quality ( )
997 struct Hostlist * pos;
998 struct Hostlist * lowest;
1000 if (linked_list_size == 0)
1002 lowest = linked_list_head;
1003 pos = linked_list_head->next;
1006 if (pos->quality < lowest->quality)
1015 * Method called whenever an advertisement message arrives.
1017 * @param cls closure (always NULL)
1018 * @param client identification of the client
1019 * @param message the actual message
1020 * @return GNUNET_OK to keep the connection open,
1021 * GNUNET_SYSERR to close it (signal serious error)
1024 advertisement_handler (void *cls,
1025 const struct GNUNET_PeerIdentity * peer,
1026 const struct GNUNET_MessageHeader * message,
1027 struct GNUNET_TIME_Relative latency,
1032 const struct GNUNET_MessageHeader * incoming;
1034 struct Hostlist * hostlist;
1036 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1037 size = ntohs (message->size);
1038 if (size <= sizeof(struct GNUNET_MessageHeader))
1040 GNUNET_break_op (0);
1041 return GNUNET_SYSERR;
1043 incoming = (const struct GNUNET_MessageHeader *) message;
1044 uri = (const char*) &incoming[1];
1045 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1046 if (uri [uri_size - 1] != '\0')
1048 GNUNET_break_op (0);
1049 return GNUNET_SYSERR;
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1055 if (GNUNET_NO != linked_list_contains (uri))
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "URI `%s' is already known\n",
1062 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1063 hostlist->hostlist_uri = (const char*) &hostlist[1];
1064 memcpy (&hostlist[1], uri, uri_size);
1065 hostlist->time_creation = GNUNET_TIME_absolute_get();
1066 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1067 hostlist->quality = HOSTLIST_INITIAL;
1069 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1072 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
1075 /* No free entries available, replace existing entry */
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "Removing lowest quality entry\n" );
1078 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
1081 lowest_quality->hostlist_uri,
1082 (unsigned long long) lowest_quality->quality);
1083 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
1085 GNUNET_free (lowest_quality);
1091 * Continuation called by the statistics code once
1092 * we go the stat. Initiates hostlist download scheduling.
1094 * @param cls closure
1095 * @param success GNUNET_OK if statistics were
1096 * successfully obtained, GNUNET_SYSERR if not.
1099 primary_task (void *cls, int success)
1102 return; /* in shutdown */
1103 #if DEBUG_HOSTLIST_CLIENT
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 "Statistics request done, scheduling hostlist download\n");
1107 schedule_hostlist_task ();
1112 process_stat (void *cls,
1113 const char *subsystem,
1118 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1119 _("Initial time between hostlist downloads is %llums\n"),
1120 (unsigned long long) value);
1121 hostlist_delay.value = value;
1126 * Method to load persistent hostlist file during hostlist client startup
1129 load_hostlist_file ()
1134 struct Hostlist * hostlist;
1136 uint32_t times_used;
1137 uint32_t hellos_returned;
1144 GNUNET_CONFIGURATION_get_value_string (cfg,
1149 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1150 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1151 "HOSTLISTFILE", "HOSTLIST");
1155 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1156 _("Loading saved hostlist entries from file `%s' \n"), filename);
1158 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1161 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1162 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1165 GNUNET_free (filename);
1170 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1171 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1172 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1173 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1174 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1175 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1177 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1178 hostlist->hello_count = hellos_returned;
1179 hostlist->hostlist_uri = (const char *) &hostlist[1];
1180 memcpy (&hostlist[1], uri, strlen(uri)+1);
1181 hostlist->quality = quality;
1182 hostlist->time_creation.value = created;
1183 hostlist->time_last_usage.value = last_used;
1184 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1191 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1194 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1195 _("%u hostlist URIs loaded from file\n"), counter);
1196 GNUNET_STATISTICS_set (stats,
1197 gettext_noop("# hostlis URIs read from file"),
1201 GNUNET_free_non_null (uri);
1203 GNUNET_BIO_read_close (rh, &emsg);
1206 GNUNET_free (filename);
1211 * Method to load persistent hostlist file during hostlist client shutdown
1212 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1214 static void save_hostlist_file ( int shutdown )
1217 struct Hostlist *pos;
1218 struct GNUNET_BIO_WriteHandle * wh;
1223 GNUNET_CONFIGURATION_get_value_string (cfg,
1228 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1229 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1230 "HOSTLISTFILE", "HOSTLIST");
1231 GNUNET_free (filename);
1234 wh = GNUNET_BIO_write_open (filename);
1237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1238 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1241 GNUNET_free (filename);
1244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1245 _("Writing %u hostlist URIs to `%s'\n" ),
1246 linked_list_size, filename);
1248 /* add code to write hostlists to file using bio */
1251 while (NULL != (pos = linked_list_head))
1253 if ( GNUNET_YES == shutdown)
1255 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1258 if (GNUNET_YES == ok)
1261 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1263 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1265 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1267 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1269 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1271 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1274 _("Error writing hostlist URIs to file `%s'\n"),
1280 if ( GNUNET_YES == shutdown)
1283 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1285 GNUNET_STATISTICS_set (stats,
1286 gettext_noop("# hostlist URIs written to file"),
1290 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1292 _("Error writing hostlist URIs to file `%s'\n"),
1294 GNUNET_free (filename);
1298 * Start downloading hostlists from hostlist servers as necessary.
1301 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1302 struct GNUNET_SCHEDULER_Handle *s,
1303 struct GNUNET_STATISTICS_Handle *st,
1304 GNUNET_CORE_ConnectEventHandler *ch,
1305 GNUNET_CORE_DisconnectEventHandler *dh,
1306 GNUNET_CORE_MessageCallback *msgh,
1309 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1312 return GNUNET_SYSERR;
1314 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1315 if (NULL == transport)
1317 curl_global_cleanup ();
1318 return GNUNET_SYSERR;
1324 GNUNET_CONFIGURATION_get_value_string (cfg,
1329 *ch = &connect_handler;
1330 *dh = &disconnect_handler;
1332 *msgh = &advertisement_handler;
1335 linked_list_head = NULL;
1336 linked_list_tail = NULL;
1337 use_preconfigured_list = GNUNET_YES;
1339 if ( GNUNET_YES == learning )
1341 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1342 _("Learning is enabled on this peer\n"));
1343 load_hostlist_file ();
1344 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1345 _("Hostlists will be saved to file again in %llums\n"),
1346 (unsigned long long) SAVING_INTERVALL.value);
1347 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1349 &hostlist_saving_task,
1354 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1355 _("Learning is not enabled on this peer\n"));
1357 GNUNET_STATISTICS_get (stats,
1359 gettext_noop("# seconds between hostlist downloads"),
1360 GNUNET_TIME_UNIT_MINUTES,
1369 * Stop downloading hostlists from hostlist servers as necessary.
1372 GNUNET_HOSTLIST_client_stop ()
1374 #if DEBUG_HOSTLIST_CLIENT
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Hostlist client shutdown\n");
1378 save_hostlist_file ( GNUNET_YES );
1380 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1382 GNUNET_SCHEDULER_cancel (sched,
1384 curl_global_cleanup ();
1386 if (transport != NULL)
1388 GNUNET_TRANSPORT_disconnect (transport);
1391 GNUNET_assert (NULL == transport);
1392 GNUNET_free_non_null (proxy);
1398 /* end of hostlist-client.c */