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 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 * Size of the linke list used to store hostlists
195 static unsigned int linked_list_size;
198 * Value saying if preconfigured is used
200 static unsigned int use_preconfigured_list;
203 * Process downloaded bits by calling callback on each HELLO.
205 * @param ptr buffer with downloaded data
206 * @param size size of a record
207 * @param nmemb number of records downloaded
209 * @return number of bytes that were processed (always size*nmemb)
212 download_hostlist_processor (void *ptr,
217 const char * cbuf = ptr;
218 const struct GNUNET_MessageHeader *msg;
224 total = size * nmemb;
225 if ( (total == 0) || (bogus_url) )
227 return total; /* ok, no data or bogus data */
229 GNUNET_STATISTICS_update (stats,
230 gettext_noop ("# bytes downloaded from hostlist servers"),
234 while ( (left > 0) ||
237 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
238 memcpy (&download_buffer[download_pos],
244 if (download_pos < sizeof(struct GNUNET_MessageHeader))
246 GNUNET_assert (left == 0);
249 msg = (const struct GNUNET_MessageHeader *) download_buffer;
250 msize = ntohs(msg->size);
251 if (msize < sizeof(struct GNUNET_MessageHeader))
253 GNUNET_STATISTICS_update (stats,
254 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258 _("Invalid `%s' message received from hostlist at `%s'\n"),
264 if (download_pos < msize)
266 GNUNET_assert (left == 0);
269 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
271 #if DEBUG_HOSTLIST_CLIENT
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Received valid `%s' message from hostlist server.\n",
276 GNUNET_STATISTICS_update (stats,
277 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
280 GNUNET_TRANSPORT_offer_hello (transport, msg);
284 GNUNET_STATISTICS_update (stats,
285 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
288 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
289 _("Invalid `%s' message received from hostlist at `%s'\n"),
292 bogus_url = GNUNET_YES;
295 memmove (download_buffer,
296 &download_buffer[msize],
297 download_pos - msize);
298 download_pos -= msize;
305 * Obtain a hostlist URL that we should use.
307 * @return NULL if there is no URL available
318 GNUNET_CONFIGURATION_get_value_string (cfg,
323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
324 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
325 "SERVERS", "HOSTLIST");
330 if (strlen (servers) > 0)
333 pos = strlen (servers) - 1;
336 if (servers[pos] == ' ')
343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
344 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
345 "SERVERS", "HOSTLIST");
346 GNUNET_free (servers);
350 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
351 pos = strlen (servers) - 1;
354 if (servers[pos] == ' ')
366 ret = GNUNET_strdup (&servers[pos]);
367 GNUNET_free (servers);
372 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
373 * @return uri to use, NULL if there is no URL available
379 unsigned int counter;
380 struct Hostlist * pos;
382 if ( GNUNET_YES == use_preconfigured_list)
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Using preconfigured bootstrap server\n");
386 use_preconfigured_list = GNUNET_NO;
387 return get_bootstrap_url();
389 if (linked_list_size == 0)
391 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
393 pos = linked_list_head;
394 while ( counter < index )
399 use_preconfigured_list = GNUNET_YES;
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "Using learned hostlist `%s'\n", pos->hostlist_uri);
402 return strdup(pos->hostlist_uri);
406 #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);
410 * Schedule the background task that will (possibly)
411 * download a hostlist.
414 schedule_hostlist_task (void);
417 * Method to load persistent hostlist file during hostlist client shutdown
418 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
420 static void save_hostlist_file ( int shutdown );
423 * Clean up the state from the task that downloaded the
424 * hostlist and schedule the next task.
433 mret = curl_multi_remove_handle (multi, curl);
434 if (mret != CURLM_OK)
436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
437 _("%s failed at %s:%d: `%s'\n"),
438 "curl_multi_remove_handle", __FILE__, __LINE__,
439 curl_multi_strerror (mret));
441 mret = curl_multi_cleanup (multi);
442 if (mret != CURLM_OK)
443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
444 _("%s failed at %s:%d: `%s'\n"),
445 "curl_multi_cleanup", __FILE__, __LINE__,
446 curl_multi_strerror (mret));
451 curl_easy_cleanup (curl);
454 GNUNET_free_non_null (current_url);
456 schedule_hostlist_task ();
461 * Ask CURL for the select set and then schedule the
462 * receiving task with the scheduler.
469 * Task that is run when we are ready to receive more data from the hostlist
472 * @param cls closure, unused
473 * @param tc task context, unused
476 multi_ready (void *cls,
477 const struct GNUNET_SCHEDULER_TaskContext *tc)
483 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
485 #if DEBUG_HOSTLIST_CLIENT
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Shutdown requested while trying to download hostlist from `%s'\n",
493 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
496 _("Timeout trying to download hostlist from `%s'\n"),
501 #if DEBUG_HOSTLIST_CLIENT
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Ready for processing hostlist client request\n");
508 mret = curl_multi_perform (multi, &running);
513 msg = curl_multi_info_read (multi, &running);
514 GNUNET_break (msg != NULL);
520 if ( (msg->data.result != CURLE_OK) &&
521 (msg->data.result != CURLE_GOT_NOTHING) )
522 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
523 _("%s failed for `%s' at %s:%d: `%s'\n"),
524 "curl_multi_perform",
528 curl_easy_strerror (msg->data.result));
530 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
531 _("Download of hostlist `%s' completed.\n"),
542 while (mret == CURLM_CALL_MULTI_PERFORM);
543 if (mret != CURLM_OK)
545 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
546 _("%s failed at %s:%d: `%s'\n"),
547 "curl_multi_perform", __FILE__, __LINE__,
548 curl_multi_strerror (mret));
556 * Ask CURL for the select set and then schedule the
557 * receiving task with the scheduler.
567 struct GNUNET_NETWORK_FDSet *grs;
568 struct GNUNET_NETWORK_FDSet *gws;
570 struct GNUNET_TIME_Relative rtime;
576 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
577 if (mret != CURLM_OK)
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 _("%s failed at %s:%d: `%s'\n"),
581 "curl_multi_fdset", __FILE__, __LINE__,
582 curl_multi_strerror (mret));
586 mret = curl_multi_timeout (multi, &timeout);
587 if (mret != CURLM_OK)
589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
590 _("%s failed at %s:%d: `%s'\n"),
591 "curl_multi_timeout", __FILE__, __LINE__,
592 curl_multi_strerror (mret));
596 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
597 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
599 grs = GNUNET_NETWORK_fdset_create ();
600 gws = GNUNET_NETWORK_fdset_create ();
601 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
602 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
603 #if DEBUG_HOSTLIST_CLIENT
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Scheduling task for hostlist download using cURL\n");
608 = GNUNET_SCHEDULER_add_select (sched,
609 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
610 GNUNET_SCHEDULER_NO_TASK,
616 GNUNET_NETWORK_fdset_destroy (gws);
617 GNUNET_NETWORK_fdset_destroy (grs);
622 * Main function that will download a hostlist and process its
631 curl = curl_easy_init ();
639 current_url = get_list_url ();
640 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
641 _("Bootstrapping using hostlist at `%s'.\n"),
643 GNUNET_STATISTICS_update (stats,
644 gettext_noop ("# hostlist downloads initiated"),
648 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
651 CURL_EASY_SETOPT (curl,
652 CURLOPT_WRITEFUNCTION,
653 &download_hostlist_processor);
659 CURL_EASY_SETOPT (curl,
667 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
668 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
669 /* no need to abort if the above failed */
670 CURL_EASY_SETOPT (curl,
678 CURL_EASY_SETOPT (curl,
682 CURL_EASY_SETOPT (curl,
686 CURL_EASY_SETOPT (curl,
688 GNUNET_SERVER_MAX_MESSAGE_SIZE);
689 if (0 == strncmp (current_url, "http", 4))
690 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
691 CURL_EASY_SETOPT (curl,
692 CURLOPT_CONNECTTIMEOUT,
694 CURL_EASY_SETOPT (curl,
698 /* this should no longer be needed; we're now single-threaded! */
699 CURL_EASY_SETOPT (curl,
703 multi = curl_multi_init ();
710 mret = curl_multi_add_handle (multi, curl);
711 if (mret != CURLM_OK)
713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
714 _("%s failed at %s:%d: `%s'\n"),
715 "curl_multi_add_handle", __FILE__, __LINE__,
716 curl_multi_strerror (mret));
717 mret = curl_multi_cleanup (multi);
718 if (mret != CURLM_OK)
719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
720 _("%s failed at %s:%d: `%s'\n"),
721 "curl_multi_cleanup", __FILE__, __LINE__,
722 curl_multi_strerror (mret));
727 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
733 * Task that checks if we should try to download a hostlist.
734 * If so, we initiate the download, otherwise we schedule
735 * this task again for a later time.
738 check_task (void *cls,
739 const struct GNUNET_SCHEDULER_TaskContext *tc)
741 current_task = GNUNET_SCHEDULER_NO_TASK;
742 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
744 if (connection_count < MIN_CONNECTIONS)
745 download_hostlist ();
747 schedule_hostlist_task ();
752 * Compute when we should check the next time about downloading
753 * a hostlist; then schedule the task accordingly.
756 schedule_hostlist_task ()
759 struct GNUNET_TIME_Relative delay;
763 curl_global_cleanup ();
764 return; /* in shutdown */
766 delay = hostlist_delay;
767 if (hostlist_delay.value == 0)
768 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
770 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
771 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
772 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
773 (1 + connection_count));
774 GNUNET_STATISTICS_set (stats,
775 gettext_noop("# seconds between hostlist downloads"),
776 hostlist_delay.value,
780 delay = GNUNET_TIME_UNIT_ZERO;
783 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
784 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
787 (unsigned long long) delay.value);
788 current_task = GNUNET_SCHEDULER_add_delayed (sched,
795 * Task that writes hostlist entries to a file on a regular base
800 hostlist_saving_task (void *cls,
801 const struct GNUNET_SCHEDULER_TaskContext *tc)
803 saving_task = GNUNET_SCHEDULER_NO_TASK;
804 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
806 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
807 _("Scheduled saving of hostlists\n"));
808 save_hostlist_file ( GNUNET_NO );
810 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
811 _("Hostlists will be saved to file again in %llums\n"),
812 (unsigned long long) SAVING_INTERVALL.value);
813 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
815 &hostlist_saving_task,
820 * Method called whenever a given peer connects.
823 * @param peer peer identity this notification is about
824 * @param latency reported latency of the connection with 'other'
825 * @param distance reported distance (DV) to 'other'
828 connect_handler (void *cls,
830 GNUNET_PeerIdentity * peer,
831 struct GNUNET_TIME_Relative latency,
835 GNUNET_STATISTICS_update (stats,
836 gettext_noop ("# active connections"),
843 * Method called whenever a given peer disconnects.
846 * @param peer peer identity this notification is about
849 disconnect_handler (void *cls,
851 GNUNET_PeerIdentity * peer)
854 GNUNET_STATISTICS_update (stats,
855 gettext_noop ("# active connections"),
862 * Method to check if URI is in hostlist linked list
863 * @param uri uri to check
864 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
867 linked_list_contains (const char * uri)
869 struct Hostlist * pos;
871 pos = linked_list_head;
874 if (0 == strcmp(pos->hostlist_uri, uri) )
883 static struct Hostlist *
884 linked_list_get_lowest_quality ( )
886 struct Hostlist * pos;
887 struct Hostlist * lowest;
889 if (linked_list_size == 0)
891 lowest = linked_list_head;
892 pos = linked_list_head->next;
895 if (pos->quality < lowest->quality)
904 * Method called whenever an advertisement message arrives.
906 * @param cls closure (always NULL)
907 * @param client identification of the client
908 * @param message the actual message
909 * @return GNUNET_OK to keep the connection open,
910 * GNUNET_SYSERR to close it (signal serious error)
913 advertisement_handler (void *cls,
914 const struct GNUNET_PeerIdentity * peer,
915 const struct GNUNET_MessageHeader * message,
916 struct GNUNET_TIME_Relative latency,
921 const struct GNUNET_MessageHeader * incoming;
923 struct Hostlist * hostlist;
925 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
926 size = ntohs (message->size);
927 if (size <= sizeof(struct GNUNET_MessageHeader))
930 return GNUNET_SYSERR;
932 incoming = (const struct GNUNET_MessageHeader *) message;
933 uri = (const char*) &incoming[1];
934 uri_size = size - sizeof (struct GNUNET_MessageHeader);
935 if (uri [uri_size - 1] != '\0')
938 return GNUNET_SYSERR;
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
944 if (GNUNET_NO != linked_list_contains (uri))
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "URI `%s' is already known\n",
951 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
952 hostlist->hostlist_uri = (const char*) &hostlist[1];
953 memcpy (&hostlist[1], uri, uri_size);
954 hostlist->time_creation = GNUNET_TIME_absolute_get();
955 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
956 hostlist->quality = HOSTLIST_INITIAL;
958 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
961 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
964 /* No free entries available, replace existing entry */
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "Removing lowest quality entry\n" );
967 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
970 lowest_quality->hostlist_uri,
971 (unsigned long long) lowest_quality->quality);
972 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
974 GNUNET_free (lowest_quality);
980 * Continuation called by the statistics code once
981 * we go the stat. Initiates hostlist download scheduling.
984 * @param success GNUNET_OK if statistics were
985 * successfully obtained, GNUNET_SYSERR if not.
988 primary_task (void *cls, int success)
991 return; /* in shutdown */
992 #if DEBUG_HOSTLIST_CLIENT
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Statistics request done, scheduling hostlist download\n");
996 schedule_hostlist_task ();
1001 process_stat (void *cls,
1002 const char *subsystem,
1007 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1008 _("Initial time between hostlist downloads is %llums\n"),
1009 (unsigned long long) value);
1010 hostlist_delay.value = value;
1015 * Method to load persistent hostlist file during hostlist client startup
1018 load_hostlist_file ()
1023 struct Hostlist * hostlist;
1025 uint32_t times_used;
1026 uint32_t hellos_returned;
1033 GNUNET_CONFIGURATION_get_value_string (cfg,
1038 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1039 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1040 "HOSTLISTFILE", "HOSTLIST");
1044 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1045 _("Loading saved hostlist entries from file `%s' \n"), filename);
1047 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1050 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1051 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1054 GNUNET_free (filename);
1059 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1060 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1061 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1062 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1063 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1064 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1066 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1067 hostlist->hello_count = hellos_returned;
1068 hostlist->hostlist_uri = (const char *) &hostlist[1];
1069 memcpy (&hostlist[1], uri, strlen(uri)+1);
1070 hostlist->quality = quality;
1071 hostlist->time_creation.value = created;
1072 hostlist->time_last_usage.value = last_used;
1073 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1080 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1083 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1084 _("%u hostlist URIs loaded from file\n"), counter);
1085 GNUNET_STATISTICS_set (stats,
1086 gettext_noop("# hostlis URIs read from file"),
1090 GNUNET_free_non_null (uri);
1092 GNUNET_BIO_read_close (rh, &emsg);
1095 GNUNET_free (filename);
1100 * Method to load persistent hostlist file during hostlist client shutdown
1101 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1103 static void save_hostlist_file ( int shutdown )
1106 struct Hostlist *pos;
1107 struct GNUNET_BIO_WriteHandle * wh;
1112 GNUNET_CONFIGURATION_get_value_string (cfg,
1117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1118 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1119 "HOSTLISTFILE", "HOSTLIST");
1120 GNUNET_free (filename);
1123 wh = GNUNET_BIO_write_open (filename);
1126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1127 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1130 GNUNET_free (filename);
1133 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1134 _("Writing %u hostlist URIs to `%s'\n" ),
1135 linked_list_size, filename);
1137 /* add code to write hostlists to file using bio */
1140 while (NULL != (pos = linked_list_head))
1142 if ( GNUNET_YES == shutdown)
1144 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1147 if (GNUNET_YES == ok)
1150 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1152 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1154 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1156 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1158 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1160 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1163 _("Error writing hostlist URIs to file `%s'\n"),
1169 if ( GNUNET_YES == shutdown)
1172 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1174 GNUNET_STATISTICS_set (stats,
1175 gettext_noop("# hostlist URIs written to file"),
1179 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1181 _("Error writing hostlist URIs to file `%s'\n"),
1183 GNUNET_free (filename);
1187 * Start downloading hostlists from hostlist servers as necessary.
1190 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1191 struct GNUNET_SCHEDULER_Handle *s,
1192 struct GNUNET_STATISTICS_Handle *st,
1193 GNUNET_CORE_ConnectEventHandler *ch,
1194 GNUNET_CORE_DisconnectEventHandler *dh,
1195 GNUNET_CORE_MessageCallback *msgh,
1198 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1201 return GNUNET_SYSERR;
1203 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1204 if (NULL == transport)
1206 curl_global_cleanup ();
1207 return GNUNET_SYSERR;
1213 GNUNET_CONFIGURATION_get_value_string (cfg,
1218 *ch = &connect_handler;
1219 *dh = &disconnect_handler;
1221 *msgh = &advertisement_handler;
1224 linked_list_head = NULL;
1225 linked_list_tail = NULL;
1226 use_preconfigured_list = GNUNET_YES;
1227 load_hostlist_file ();
1229 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1230 _("Hostlists will be saved to file again in %llums\n"),
1231 (unsigned long long) SAVING_INTERVALL.value);
1232 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1234 &hostlist_saving_task,
1237 GNUNET_STATISTICS_get (stats,
1239 gettext_noop("# seconds between hostlist downloads"),
1240 GNUNET_TIME_UNIT_MINUTES,
1249 * Stop downloading hostlists from hostlist servers as necessary.
1252 GNUNET_HOSTLIST_client_stop ()
1254 #if DEBUG_HOSTLIST_CLIENT
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Hostlist client shutdown\n");
1258 save_hostlist_file ( GNUNET_YES );
1260 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1262 GNUNET_SCHEDULER_cancel (sched,
1264 curl_global_cleanup ();
1266 if (transport != NULL)
1268 GNUNET_TRANSPORT_disconnect (transport);
1271 GNUNET_assert (NULL == transport);
1272 GNUNET_free_non_null (proxy);
1278 /* end of hostlist-client.c */