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
43 * Number of connections that we must have to NOT download
46 #define MIN_CONNECTIONS 4
49 * A single hostlist obtained by hostlist advertisements
54 * previous entry, used to manage entries in a double linked list
56 struct Hostlist * prev;
59 * next entry, used to manage entries in a double linked list
61 struct Hostlist * next;
64 * URI where hostlist can be obtained
66 const char *hostlist_uri;
69 * Value describing the quality of the hostlist, the bigger the better but (should) never < 0
70 * used for deciding which hostlist is replaced if MAX_NUMBER_HOSTLISTS in data structure is reached
71 * intial value = HOSTLIST_INITIAL
72 * increased every successful download by HOSTLIST_SUCCESSFULL_DOWNLOAD
73 * increased every successful download by number of obtained HELLO messages
74 * decreased every failed download by HOSTLIST_SUCCESSFULL_DOWNLOAD
79 * Time the hostlist advertisement was recieved and the entry was created
81 struct GNUNET_TIME_Absolute time_creation;
84 * Last time the hostlist was obtained
86 struct GNUNET_TIME_Absolute time_last_usage;
89 * Number of HELLO messages obtained during last download
94 * Number of times the hostlist was successfully obtained
104 static const struct GNUNET_CONFIGURATION_Handle *cfg;
109 static struct GNUNET_SCHEDULER_Handle *sched;
114 struct GNUNET_STATISTICS_Handle *stats;
119 struct GNUNET_TRANSPORT_Handle *transport;
122 * Proxy that we are using (can be NULL).
127 * Buffer for data downloaded via HTTP.
129 static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
132 * Number of bytes valid in 'download_buffer'.
134 static size_t download_pos;
137 * Current URL that we are using.
139 static char *current_url;
142 * Current CURL handle.
147 * Current multi-CURL handle.
152 * ID of the current task scheduled.
154 static GNUNET_SCHEDULER_TaskIdentifier current_task;
157 * ID of the current hostlist saving task scheduled.
159 static GNUNET_SCHEDULER_TaskIdentifier saving_task;
162 * Amount of time we wait between hostlist downloads.
164 static struct GNUNET_TIME_Relative hostlist_delay;
167 * Set to GNUNET_YES if the current URL had some problems.
169 static int bogus_url;
172 * Number of active connections (according to core service).
174 static unsigned int connection_count;
177 * At what time MUST the current hostlist request be done?
179 static struct GNUNET_TIME_Absolute end_time;
182 * Head of the linked list used to store hostlists
184 static struct Hostlist * linked_list_head;
187 * Tail of the linked list used to store hostlists
189 static struct Hostlist * linked_list_tail;
192 * Current hostlist used for downloading
194 static struct Hostlist * current_hostlist;
197 * Size of the linke list used to store hostlists
199 static unsigned int linked_list_size;
202 * Value saying if preconfigured is used
204 static unsigned int use_preconfigured_list;
207 * Set if we are allowed to learn new hostlists and use them
212 * Value saying how many valid HELLO messages were obtained during download
214 static unsigned int hellos_obtained;
217 * Value saying if hostlist download was successful
219 static unsigned int download_successful;
222 * Process downloaded bits by calling callback on each HELLO.
224 * @param ptr buffer with downloaded data
225 * @param size size of a record
226 * @param nmemb number of records downloaded
228 * @return number of bytes that were processed (always size*nmemb)
231 download_hostlist_processor (void *ptr,
236 const char * cbuf = ptr;
237 const struct GNUNET_MessageHeader *msg;
243 total = size * nmemb;
244 if ( (total == 0) || (bogus_url) )
246 return total; /* ok, no data or bogus data */
248 GNUNET_STATISTICS_update (stats,
249 gettext_noop ("# bytes downloaded from hostlist servers"),
253 while ( (left > 0) ||
256 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
257 memcpy (&download_buffer[download_pos],
263 if (download_pos < sizeof(struct GNUNET_MessageHeader))
265 GNUNET_assert (left == 0);
268 msg = (const struct GNUNET_MessageHeader *) download_buffer;
269 msize = ntohs(msg->size);
270 if (msize < sizeof(struct GNUNET_MessageHeader))
272 GNUNET_STATISTICS_update (stats,
273 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
276 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
277 _("Invalid `%s' message received from hostlist at `%s'\n"),
283 if (download_pos < msize)
285 GNUNET_assert (left == 0);
288 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
290 #if DEBUG_HOSTLIST_CLIENT
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Received valid `%s' message from hostlist server.\n",
295 GNUNET_STATISTICS_update (stats,
296 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
300 GNUNET_TRANSPORT_offer_hello (transport, msg);
304 GNUNET_STATISTICS_update (stats,
305 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
308 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
309 _("Invalid `%s' message received from hostlist at `%s'\n"),
312 bogus_url = GNUNET_YES;
315 memmove (download_buffer,
316 &download_buffer[msize],
317 download_pos - msize);
318 download_pos -= msize;
325 * Obtain a hostlist URL that we should use.
327 * @return NULL if there is no URL available
338 GNUNET_CONFIGURATION_get_value_string (cfg,
343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
344 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
345 "SERVERS", "HOSTLIST");
350 if (strlen (servers) > 0)
353 pos = strlen (servers) - 1;
356 if (servers[pos] == ' ')
363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
364 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
365 "SERVERS", "HOSTLIST");
366 GNUNET_free (servers);
370 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
371 pos = strlen (servers) - 1;
374 if (servers[pos] == ' ')
386 ret = GNUNET_strdup (&servers[pos]);
387 GNUNET_free (servers);
392 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
393 * @return uri to use, NULL if there is no URL available
399 unsigned int counter;
400 struct Hostlist * pos;
402 if ( GNUNET_NO == learning)
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Using preconfigured bootstrap server\n");
406 current_hostlist = NULL;
407 return get_bootstrap_url();
409 if ( (GNUNET_YES == use_preconfigured_list) ||
410 (linked_list_size == 0) )
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Using preconfigured bootstrap server\n");
414 current_hostlist = NULL;
415 return get_bootstrap_url();
417 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
419 pos = linked_list_head;
420 while ( counter < index )
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Using learned hostlist `%s'\n", pos->hostlist_uri);
427 current_hostlist = pos;
428 return strdup(pos->hostlist_uri);
432 #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);
436 * Schedule the background task that will (possibly)
437 * download a hostlist.
440 schedule_hostlist_task (void);
443 * Method to load persistent hostlist file during hostlist client shutdown
444 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
446 static void save_hostlist_file ( int shutdown );
449 * add val2 to val1 with overflow check
450 * @param val1 value 1
451 * @param val2 value 2
454 static uint64_t checked_add (uint64_t val1, uint64_t val2)
456 static uint64_t temp;
457 static uint64_t maxv;
470 * Subtract val2 from val1 with underflow check
471 * @param val1 value 1
472 * @param val2 value 2
475 static uint64_t checked_sub (uint64_t val1, uint64_t val2)
485 * Method updating hostlist statistics
487 static void update_hostlist ( )
489 if ( (use_preconfigured_list == GNUNET_NO) && ( NULL != current_hostlist ) )
491 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
492 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
493 current_hostlist->hello_count = hellos_obtained;
494 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
495 current_hostlist->quality = checked_add ( current_hostlist->quality, (hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
496 if ( GNUNET_YES == download_successful )
498 current_hostlist->times_used++;
499 current_hostlist->quality = checked_add ( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
502 current_hostlist->quality = checked_sub ( current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD );
504 current_hostlist = NULL;
505 /* Alternating the usage of preconfigured and learned hostlists */
507 if ( GNUNET_YES == learning)
509 if (use_preconfigured_list == GNUNET_YES)
510 use_preconfigured_list = GNUNET_NO;
512 use_preconfigured_list = GNUNET_YES;
515 use_preconfigured_list = GNUNET_YES;
519 * Clean up the state from the task that downloaded the
520 * hostlist and schedule the next task.
529 mret = curl_multi_remove_handle (multi, curl);
530 if (mret != CURLM_OK)
532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
533 _("%s failed at %s:%d: `%s'\n"),
534 "curl_multi_remove_handle", __FILE__, __LINE__,
535 curl_multi_strerror (mret));
537 mret = curl_multi_cleanup (multi);
538 if (mret != CURLM_OK)
539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
540 _("%s failed at %s:%d: `%s'\n"),
541 "curl_multi_cleanup", __FILE__, __LINE__,
542 curl_multi_strerror (mret));
547 curl_easy_cleanup (curl);
550 GNUNET_free_non_null (current_url);
553 schedule_hostlist_task ();
558 * Ask CURL for the select set and then schedule the
559 * receiving task with the scheduler.
566 * Task that is run when we are ready to receive more data from the hostlist
569 * @param cls closure, unused
570 * @param tc task context, unused
573 multi_ready (void *cls,
574 const struct GNUNET_SCHEDULER_TaskContext *tc)
580 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
582 #if DEBUG_HOSTLIST_CLIENT
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "Shutdown requested while trying to download hostlist from `%s'\n",
591 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
593 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
594 _("Timeout trying to download hostlist from `%s'\n"),
600 #if DEBUG_HOSTLIST_CLIENT
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602 "Ready for processing hostlist client request\n");
607 mret = curl_multi_perform (multi, &running);
612 msg = curl_multi_info_read (multi, &running);
613 GNUNET_break (msg != NULL);
619 if ( (msg->data.result != CURLE_OK) &&
620 (msg->data.result != CURLE_GOT_NOTHING) )
621 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
622 _("%s failed for `%s' at %s:%d: `%s'\n"),
623 "curl_multi_perform",
627 curl_easy_strerror (msg->data.result));
630 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
631 _("Download of hostlist `%s' completed.\n"),
633 download_successful = GNUNET_YES;
645 while (mret == CURLM_CALL_MULTI_PERFORM);
646 if (mret != CURLM_OK)
648 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
649 _("%s failed at %s:%d: `%s'\n"),
650 "curl_multi_perform", __FILE__, __LINE__,
651 curl_multi_strerror (mret));
659 * Ask CURL for the select set and then schedule the
660 * receiving task with the scheduler.
670 struct GNUNET_NETWORK_FDSet *grs;
671 struct GNUNET_NETWORK_FDSet *gws;
673 struct GNUNET_TIME_Relative rtime;
679 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
680 if (mret != CURLM_OK)
682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
683 _("%s failed at %s:%d: `%s'\n"),
684 "curl_multi_fdset", __FILE__, __LINE__,
685 curl_multi_strerror (mret));
689 mret = curl_multi_timeout (multi, &timeout);
690 if (mret != CURLM_OK)
692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
693 _("%s failed at %s:%d: `%s'\n"),
694 "curl_multi_timeout", __FILE__, __LINE__,
695 curl_multi_strerror (mret));
699 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
700 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
702 grs = GNUNET_NETWORK_fdset_create ();
703 gws = GNUNET_NETWORK_fdset_create ();
704 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
705 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
706 #if DEBUG_HOSTLIST_CLIENT
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Scheduling task for hostlist download using cURL\n");
711 = GNUNET_SCHEDULER_add_select (sched,
712 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
713 GNUNET_SCHEDULER_NO_TASK,
719 GNUNET_NETWORK_fdset_destroy (gws);
720 GNUNET_NETWORK_fdset_destroy (grs);
725 * Main function that will download a hostlist and process its
734 current_url = get_list_url ();
735 if (current_url == NULL)
737 curl = curl_easy_init ();
745 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
746 _("Bootstrapping using hostlist at `%s'.\n"),
749 download_successful = GNUNET_NO;
750 GNUNET_STATISTICS_update (stats,
751 gettext_noop ("# hostlist downloads initiated"),
755 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
758 CURL_EASY_SETOPT (curl,
759 CURLOPT_WRITEFUNCTION,
760 &download_hostlist_processor);
766 CURL_EASY_SETOPT (curl,
774 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
775 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
776 /* no need to abort if the above failed */
777 CURL_EASY_SETOPT (curl,
785 CURL_EASY_SETOPT (curl,
789 CURL_EASY_SETOPT (curl,
793 CURL_EASY_SETOPT (curl,
795 GNUNET_SERVER_MAX_MESSAGE_SIZE);
796 if (0 == strncmp (current_url, "http", 4))
797 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
798 CURL_EASY_SETOPT (curl,
799 CURLOPT_CONNECTTIMEOUT,
801 CURL_EASY_SETOPT (curl,
805 /* this should no longer be needed; we're now single-threaded! */
806 CURL_EASY_SETOPT (curl,
810 multi = curl_multi_init ();
817 mret = curl_multi_add_handle (multi, curl);
818 if (mret != CURLM_OK)
820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
821 _("%s failed at %s:%d: `%s'\n"),
822 "curl_multi_add_handle", __FILE__, __LINE__,
823 curl_multi_strerror (mret));
824 mret = curl_multi_cleanup (multi);
825 if (mret != CURLM_OK)
826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
827 _("%s failed at %s:%d: `%s'\n"),
828 "curl_multi_cleanup", __FILE__, __LINE__,
829 curl_multi_strerror (mret));
834 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
840 * Task that checks if we should try to download a hostlist.
841 * If so, we initiate the download, otherwise we schedule
842 * this task again for a later time.
845 check_task (void *cls,
846 const struct GNUNET_SCHEDULER_TaskContext *tc)
848 current_task = GNUNET_SCHEDULER_NO_TASK;
849 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
851 if (connection_count < MIN_CONNECTIONS)
852 download_hostlist ();
854 schedule_hostlist_task ();
859 * Compute when we should check the next time about downloading
860 * a hostlist; then schedule the task accordingly.
863 schedule_hostlist_task ()
866 struct GNUNET_TIME_Relative delay;
870 curl_global_cleanup ();
871 return; /* in shutdown */
873 delay = hostlist_delay;
874 if (hostlist_delay.value == 0)
875 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
877 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
878 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
879 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
880 (1 + connection_count));
881 GNUNET_STATISTICS_set (stats,
882 gettext_noop("# seconds between hostlist downloads"),
883 hostlist_delay.value,
887 delay = GNUNET_TIME_UNIT_ZERO;
890 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
891 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
894 (unsigned long long) delay.value);
895 current_task = GNUNET_SCHEDULER_add_delayed (sched,
902 * Task that writes hostlist entries to a file on a regular base
907 hostlist_saving_task (void *cls,
908 const struct GNUNET_SCHEDULER_TaskContext *tc)
910 saving_task = GNUNET_SCHEDULER_NO_TASK;
911 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
913 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
914 _("Scheduled saving of hostlists\n"));
915 save_hostlist_file ( GNUNET_NO );
917 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
918 _("Hostlists will be saved to file again in %llums\n"),
919 (unsigned long long) SAVING_INTERVALL.value);
920 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
922 &hostlist_saving_task,
927 * Method called whenever a given peer connects.
930 * @param peer peer identity this notification is about
931 * @param latency reported latency of the connection with 'other'
932 * @param distance reported distance (DV) to 'other'
935 connect_handler (void *cls,
937 GNUNET_PeerIdentity * peer,
938 struct GNUNET_TIME_Relative latency,
942 GNUNET_STATISTICS_update (stats,
943 gettext_noop ("# active connections"),
950 * Method called whenever a given peer disconnects.
953 * @param peer peer identity this notification is about
956 disconnect_handler (void *cls,
958 GNUNET_PeerIdentity * peer)
961 GNUNET_STATISTICS_update (stats,
962 gettext_noop ("# active connections"),
969 * Method to check if URI is in hostlist linked list
970 * @param uri uri to check
971 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
974 linked_list_contains (const char * uri)
976 struct Hostlist * pos;
978 pos = linked_list_head;
981 if (0 == strcmp(pos->hostlist_uri, uri) )
990 static struct Hostlist *
991 linked_list_get_lowest_quality ( )
993 struct Hostlist * pos;
994 struct Hostlist * lowest;
996 if (linked_list_size == 0)
998 lowest = linked_list_head;
999 pos = linked_list_head->next;
1002 if (pos->quality < lowest->quality)
1011 * Method called whenever an advertisement message arrives.
1013 * @param cls closure (always NULL)
1014 * @param peer the peer sending the message
1015 * @param message the actual message
1016 * @param latency latency
1017 * @param distance distance
1018 * @return GNUNET_OK to keep the connection open,
1019 * GNUNET_SYSERR to close it (signal serious error)
1022 advertisement_handler (void *cls,
1023 const struct GNUNET_PeerIdentity * peer,
1024 const struct GNUNET_MessageHeader * message,
1025 struct GNUNET_TIME_Relative latency,
1030 const struct GNUNET_MessageHeader * incoming;
1032 struct Hostlist * hostlist;
1034 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1035 size = ntohs (message->size);
1036 if (size <= sizeof(struct GNUNET_MessageHeader))
1038 GNUNET_break_op (0);
1039 return GNUNET_SYSERR;
1041 incoming = (const struct GNUNET_MessageHeader *) message;
1042 uri = (const char*) &incoming[1];
1043 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1044 if (uri [uri_size - 1] != '\0')
1046 GNUNET_break_op (0);
1047 return GNUNET_SYSERR;
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1053 if (GNUNET_NO != linked_list_contains (uri))
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "URI `%s' is already known\n",
1060 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1061 hostlist->hostlist_uri = (const char*) &hostlist[1];
1062 memcpy (&hostlist[1], uri, uri_size);
1063 hostlist->time_creation = GNUNET_TIME_absolute_get();
1064 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1065 hostlist->quality = HOSTLIST_INITIAL;
1067 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1070 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
1073 /* No free entries available, replace existing entry */
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "Removing lowest quality entry\n" );
1076 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
1079 lowest_quality->hostlist_uri,
1080 (unsigned long long) lowest_quality->quality);
1081 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
1083 GNUNET_free (lowest_quality);
1089 * Continuation called by the statistics code once
1090 * we go the stat. Initiates hostlist download scheduling.
1092 * @param cls closure
1093 * @param success GNUNET_OK if statistics were
1094 * successfully obtained, GNUNET_SYSERR if not.
1097 primary_task (void *cls, int success)
1100 return; /* in shutdown */
1101 #if DEBUG_HOSTLIST_CLIENT
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Statistics request done, scheduling hostlist download\n");
1105 schedule_hostlist_task ();
1110 process_stat (void *cls,
1111 const char *subsystem,
1116 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1117 _("Initial time between hostlist downloads is %llums\n"),
1118 (unsigned long long) value);
1119 hostlist_delay.value = value;
1124 * Method to load persistent hostlist file during hostlist client startup
1127 load_hostlist_file ()
1132 struct Hostlist * hostlist;
1134 uint32_t times_used;
1135 uint32_t hellos_returned;
1142 GNUNET_CONFIGURATION_get_value_string (cfg,
1147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1148 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1149 "HOSTLISTFILE", "HOSTLIST");
1153 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1154 _("Loading saved hostlist entries from file `%s' \n"), filename);
1156 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1160 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1163 GNUNET_free (filename);
1168 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1169 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1170 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1171 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1172 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1173 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1175 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1176 hostlist->hello_count = hellos_returned;
1177 hostlist->hostlist_uri = (const char *) &hostlist[1];
1178 memcpy (&hostlist[1], uri, strlen(uri)+1);
1179 hostlist->quality = quality;
1180 hostlist->time_creation.value = created;
1181 hostlist->time_last_usage.value = last_used;
1182 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1189 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1192 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1193 _("%u hostlist URIs loaded from file\n"), counter);
1194 GNUNET_STATISTICS_set (stats,
1195 gettext_noop("# hostlis URIs read from file"),
1199 GNUNET_free_non_null (uri);
1201 GNUNET_BIO_read_close (rh, &emsg);
1204 GNUNET_free (filename);
1209 * Method to load persistent hostlist file during hostlist client shutdown
1210 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1212 static void save_hostlist_file ( int shutdown )
1215 struct Hostlist *pos;
1216 struct GNUNET_BIO_WriteHandle * wh;
1221 GNUNET_CONFIGURATION_get_value_string (cfg,
1226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1227 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1228 "HOSTLISTFILE", "HOSTLIST");
1229 GNUNET_free (filename);
1232 wh = GNUNET_BIO_write_open (filename);
1235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1236 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1239 GNUNET_free (filename);
1242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1243 _("Writing %u hostlist URIs to `%s'\n" ),
1244 linked_list_size, filename);
1246 /* add code to write hostlists to file using bio */
1249 while (NULL != (pos = linked_list_head))
1251 if ( GNUNET_YES == shutdown)
1253 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1256 if (GNUNET_YES == ok)
1259 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1261 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1263 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1265 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1267 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1269 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1271 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1272 _("Error writing hostlist URIs to file `%s'\n"),
1278 if ( GNUNET_YES == shutdown)
1281 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1283 GNUNET_STATISTICS_set (stats,
1284 gettext_noop("# hostlist URIs written to file"),
1288 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1290 _("Error writing hostlist URIs to file `%s'\n"),
1292 GNUNET_free (filename);
1296 * Start downloading hostlists from hostlist servers as necessary.
1299 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1300 struct GNUNET_SCHEDULER_Handle *s,
1301 struct GNUNET_STATISTICS_Handle *st,
1302 GNUNET_CORE_ConnectEventHandler *ch,
1303 GNUNET_CORE_DisconnectEventHandler *dh,
1304 GNUNET_CORE_MessageCallback *msgh,
1310 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1313 return GNUNET_SYSERR;
1315 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1316 if (NULL == transport)
1318 curl_global_cleanup ();
1319 return GNUNET_SYSERR;
1325 GNUNET_CONFIGURATION_get_value_string (cfg,
1330 *ch = &connect_handler;
1331 *dh = &disconnect_handler;
1333 *msgh = &advertisement_handler;
1336 linked_list_head = NULL;
1337 linked_list_tail = NULL;
1338 use_preconfigured_list = GNUNET_YES;
1340 if ( GNUNET_YES == learning )
1342 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1343 _("Learning is enabled on this peer\n"));
1344 load_hostlist_file ();
1345 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1346 _("Hostlists will be saved to file again in %llums\n"),
1347 (unsigned long long) SAVING_INTERVALL.value);
1348 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1350 &hostlist_saving_task,
1355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1356 _("Learning is not enabled on this peer\n"));
1357 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1362 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1364 result = remove (filename);
1366 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1367 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),filename);
1369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1370 _("Hostlist file `%s' could not be removed\n"),filename);
1373 GNUNET_free ( filename );
1375 GNUNET_STATISTICS_get (stats,
1377 gettext_noop("# seconds between hostlist downloads"),
1378 GNUNET_TIME_UNIT_MINUTES,
1387 * Stop downloading hostlists from hostlist servers as necessary.
1390 GNUNET_HOSTLIST_client_stop ()
1392 #if DEBUG_HOSTLIST_CLIENT
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394 "Hostlist client shutdown\n");
1396 save_hostlist_file ( GNUNET_YES );
1398 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1400 GNUNET_SCHEDULER_cancel (sched,
1402 curl_global_cleanup ();
1404 if (transport != NULL)
1406 GNUNET_TRANSPORT_disconnect (transport);
1409 GNUNET_assert (NULL == transport);
1410 GNUNET_free_non_null (proxy);
1416 /* end of hostlist-client.c */