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 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
391 pos = linked_list_head;
392 while ( counter < index )
397 use_preconfigured_list = GNUNET_YES;
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Using learned hostlist `%s'\n", pos->hostlist_uri);
400 return strdup(pos->hostlist_uri);
404 #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);
408 * Schedule the background task that will (possibly)
409 * download a hostlist.
412 schedule_hostlist_task (void);
415 * Method to load persistent hostlist file during hostlist client shutdown
416 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
418 static void save_hostlist_file ( int shutdown );
421 * Clean up the state from the task that downloaded the
422 * hostlist and schedule the next task.
431 mret = curl_multi_remove_handle (multi, curl);
432 if (mret != CURLM_OK)
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
435 _("%s failed at %s:%d: `%s'\n"),
436 "curl_multi_remove_handle", __FILE__, __LINE__,
437 curl_multi_strerror (mret));
439 mret = curl_multi_cleanup (multi);
440 if (mret != CURLM_OK)
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 _("%s failed at %s:%d: `%s'\n"),
443 "curl_multi_cleanup", __FILE__, __LINE__,
444 curl_multi_strerror (mret));
449 curl_easy_cleanup (curl);
452 GNUNET_free_non_null (current_url);
454 schedule_hostlist_task ();
459 * Ask CURL for the select set and then schedule the
460 * receiving task with the scheduler.
467 * Task that is run when we are ready to receive more data from the hostlist
470 * @param cls closure, unused
471 * @param tc task context, unused
474 multi_ready (void *cls,
475 const struct GNUNET_SCHEDULER_TaskContext *tc)
481 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
483 #if DEBUG_HOSTLIST_CLIENT
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Shutdown requested while trying to download hostlist from `%s'\n",
491 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
494 _("Timeout trying to download hostlist from `%s'\n"),
499 #if DEBUG_HOSTLIST_CLIENT
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Ready for processing hostlist client request\n");
506 mret = curl_multi_perform (multi, &running);
511 msg = curl_multi_info_read (multi, &running);
512 GNUNET_break (msg != NULL);
518 if ( (msg->data.result != CURLE_OK) &&
519 (msg->data.result != CURLE_GOT_NOTHING) )
520 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
521 _("%s failed for `%s' at %s:%d: `%s'\n"),
522 "curl_multi_perform",
526 curl_easy_strerror (msg->data.result));
528 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
529 _("Download of hostlist `%s' completed.\n"),
540 while (mret == CURLM_CALL_MULTI_PERFORM);
541 if (mret != CURLM_OK)
543 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
544 _("%s failed at %s:%d: `%s'\n"),
545 "curl_multi_perform", __FILE__, __LINE__,
546 curl_multi_strerror (mret));
554 * Ask CURL for the select set and then schedule the
555 * receiving task with the scheduler.
565 struct GNUNET_NETWORK_FDSet *grs;
566 struct GNUNET_NETWORK_FDSet *gws;
568 struct GNUNET_TIME_Relative rtime;
574 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
575 if (mret != CURLM_OK)
577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
578 _("%s failed at %s:%d: `%s'\n"),
579 "curl_multi_fdset", __FILE__, __LINE__,
580 curl_multi_strerror (mret));
584 mret = curl_multi_timeout (multi, &timeout);
585 if (mret != CURLM_OK)
587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
588 _("%s failed at %s:%d: `%s'\n"),
589 "curl_multi_timeout", __FILE__, __LINE__,
590 curl_multi_strerror (mret));
594 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
595 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
597 grs = GNUNET_NETWORK_fdset_create ();
598 gws = GNUNET_NETWORK_fdset_create ();
599 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
600 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
601 #if DEBUG_HOSTLIST_CLIENT
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Scheduling task for hostlist download using cURL\n");
606 = GNUNET_SCHEDULER_add_select (sched,
607 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
608 GNUNET_SCHEDULER_NO_TASK,
614 GNUNET_NETWORK_fdset_destroy (gws);
615 GNUNET_NETWORK_fdset_destroy (grs);
620 * Main function that will download a hostlist and process its
629 curl = curl_easy_init ();
637 current_url = get_list_url ();
638 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
639 _("Bootstrapping using hostlist at `%s'.\n"),
641 GNUNET_STATISTICS_update (stats,
642 gettext_noop ("# hostlist downloads initiated"),
646 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
649 CURL_EASY_SETOPT (curl,
650 CURLOPT_WRITEFUNCTION,
651 &download_hostlist_processor);
657 CURL_EASY_SETOPT (curl,
665 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
666 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
667 /* no need to abort if the above failed */
668 CURL_EASY_SETOPT (curl,
676 CURL_EASY_SETOPT (curl,
680 CURL_EASY_SETOPT (curl,
684 CURL_EASY_SETOPT (curl,
686 GNUNET_SERVER_MAX_MESSAGE_SIZE);
687 if (0 == strncmp (current_url, "http", 4))
688 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
689 CURL_EASY_SETOPT (curl,
690 CURLOPT_CONNECTTIMEOUT,
692 CURL_EASY_SETOPT (curl,
696 /* this should no longer be needed; we're now single-threaded! */
697 CURL_EASY_SETOPT (curl,
701 multi = curl_multi_init ();
708 mret = curl_multi_add_handle (multi, curl);
709 if (mret != CURLM_OK)
711 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
712 _("%s failed at %s:%d: `%s'\n"),
713 "curl_multi_add_handle", __FILE__, __LINE__,
714 curl_multi_strerror (mret));
715 mret = curl_multi_cleanup (multi);
716 if (mret != CURLM_OK)
717 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
718 _("%s failed at %s:%d: `%s'\n"),
719 "curl_multi_cleanup", __FILE__, __LINE__,
720 curl_multi_strerror (mret));
725 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
731 * Task that checks if we should try to download a hostlist.
732 * If so, we initiate the download, otherwise we schedule
733 * this task again for a later time.
736 check_task (void *cls,
737 const struct GNUNET_SCHEDULER_TaskContext *tc)
739 current_task = GNUNET_SCHEDULER_NO_TASK;
740 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
742 if (connection_count < MIN_CONNECTIONS)
743 download_hostlist ();
745 schedule_hostlist_task ();
750 * Compute when we should check the next time about downloading
751 * a hostlist; then schedule the task accordingly.
754 schedule_hostlist_task ()
757 struct GNUNET_TIME_Relative delay;
761 curl_global_cleanup ();
762 return; /* in shutdown */
764 delay = hostlist_delay;
765 if (hostlist_delay.value == 0)
766 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
768 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
769 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
770 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
771 (1 + connection_count));
772 GNUNET_STATISTICS_set (stats,
773 gettext_noop("# seconds between hostlist downloads"),
774 hostlist_delay.value,
778 delay = GNUNET_TIME_UNIT_ZERO;
781 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
782 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
785 (unsigned long long) delay.value);
786 current_task = GNUNET_SCHEDULER_add_delayed (sched,
793 * Task that writes hostlist entries to a file on a regular base
798 hostlist_saving_task (void *cls,
799 const struct GNUNET_SCHEDULER_TaskContext *tc)
801 saving_task = GNUNET_SCHEDULER_NO_TASK;
802 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
805 _("Scheduled saving of hostlists\n"));
806 save_hostlist_file ( GNUNET_NO );
808 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
809 _("Hostlists will be saved to file again in %llums\n"),
810 (unsigned long long) SAVING_INTERVALL.value);
811 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
813 &hostlist_saving_task,
818 * Method called whenever a given peer connects.
821 * @param peer peer identity this notification is about
822 * @param latency reported latency of the connection with 'other'
823 * @param distance reported distance (DV) to 'other'
826 connect_handler (void *cls,
828 GNUNET_PeerIdentity * peer,
829 struct GNUNET_TIME_Relative latency,
833 GNUNET_STATISTICS_update (stats,
834 gettext_noop ("# active connections"),
841 * Method called whenever a given peer disconnects.
844 * @param peer peer identity this notification is about
847 disconnect_handler (void *cls,
849 GNUNET_PeerIdentity * peer)
852 GNUNET_STATISTICS_update (stats,
853 gettext_noop ("# active connections"),
860 * Method to check if URI is in hostlist linked list
861 * @param uri uri to check
862 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
865 linked_list_contains (const char * uri)
867 struct Hostlist * pos;
869 pos = linked_list_head;
872 if (0 == strcmp(pos->hostlist_uri, uri) )
881 static struct Hostlist *
882 linked_list_get_lowest_quality ( )
884 struct Hostlist * pos;
885 struct Hostlist * lowest;
887 if (linked_list_size == 0)
889 lowest = linked_list_head;
890 pos = linked_list_head->next;
893 if (pos->quality < lowest->quality)
902 * Method called whenever an advertisement message arrives.
904 * @param cls closure (always NULL)
905 * @param client identification of the client
906 * @param message the actual message
907 * @return GNUNET_OK to keep the connection open,
908 * GNUNET_SYSERR to close it (signal serious error)
911 advertisement_handler (void *cls,
912 const struct GNUNET_PeerIdentity * peer,
913 const struct GNUNET_MessageHeader * message,
914 struct GNUNET_TIME_Relative latency,
919 const struct GNUNET_MessageHeader * incoming;
921 struct Hostlist * hostlist;
923 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
924 size = ntohs (message->size);
925 if (size <= sizeof(struct GNUNET_MessageHeader))
928 return GNUNET_SYSERR;
930 incoming = (const struct GNUNET_MessageHeader *) message;
931 uri = (const char*) &incoming[1];
932 uri_size = size - sizeof (struct GNUNET_MessageHeader);
933 if (uri [uri_size - 1] != '\0')
936 return GNUNET_SYSERR;
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
942 if (GNUNET_NO != linked_list_contains (uri))
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "URI `%s' is already known\n",
949 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
950 hostlist->hostlist_uri = (const char*) &hostlist[1];
951 memcpy (&hostlist[1], uri, uri_size);
952 hostlist->time_creation = GNUNET_TIME_absolute_get();
953 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
954 hostlist->quality = HOSTLIST_INITIAL;
956 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
959 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
962 /* No free entries available, replace existing entry */
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Removing lowest quality entry\n" );
965 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
968 lowest_quality->hostlist_uri,
969 (unsigned long long) lowest_quality->quality);
970 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
972 GNUNET_free (lowest_quality);
978 * Continuation called by the statistics code once
979 * we go the stat. Initiates hostlist download scheduling.
982 * @param success GNUNET_OK if statistics were
983 * successfully obtained, GNUNET_SYSERR if not.
986 primary_task (void *cls, int success)
989 return; /* in shutdown */
990 #if DEBUG_HOSTLIST_CLIENT
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992 "Statistics request done, scheduling hostlist download\n");
994 schedule_hostlist_task ();
999 process_stat (void *cls,
1000 const char *subsystem,
1005 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1006 _("Initial time between hostlist downloads is %llums\n"),
1007 (unsigned long long) value);
1008 hostlist_delay.value = value;
1013 * Method to load persistent hostlist file during hostlist client startup
1016 load_hostlist_file ()
1021 struct Hostlist * hostlist;
1023 uint32_t times_used;
1024 uint32_t hellos_returned;
1031 GNUNET_CONFIGURATION_get_value_string (cfg,
1036 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1037 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1038 "HOSTLISTFILE", "HOSTLIST");
1042 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1043 _("Loading saved hostlist entries from file `%s' \n"), filename);
1045 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1048 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1049 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1052 GNUNET_free (filename);
1057 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1058 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1059 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1060 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1061 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1062 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1064 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1065 hostlist->hello_count = hellos_returned;
1066 hostlist->hostlist_uri = (const char *) &hostlist[1];
1067 memcpy (&hostlist[1], uri, strlen(uri)+1);
1068 hostlist->quality = quality;
1069 hostlist->time_creation.value = created;
1070 hostlist->time_last_usage.value = last_used;
1071 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1077 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1080 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1081 _("%u hostlist URIs loaded from file\n"), counter);
1082 GNUNET_STATISTICS_set (stats,
1083 gettext_noop("# hostlis URIs read from file"),
1087 GNUNET_free_non_null (uri);
1089 GNUNET_BIO_read_close (rh, &emsg);
1092 GNUNET_free (filename);
1097 * Method to load persistent hostlist file during hostlist client shutdown
1098 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1100 static void save_hostlist_file ( int shutdown )
1103 struct Hostlist *pos;
1104 struct GNUNET_BIO_WriteHandle * wh;
1109 GNUNET_CONFIGURATION_get_value_string (cfg,
1114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1115 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1116 "HOSTLISTFILE", "HOSTLIST");
1119 wh = GNUNET_BIO_write_open (filename);
1122 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1123 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1129 _("Writing %u hostlist URIs to `%s'\n" ),
1130 linked_list_size, filename);
1132 /* add code to write hostlists to file using bio */
1135 while (NULL != (pos = linked_list_head))
1137 if ( GNUNET_YES == shutdown)
1139 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1142 if (GNUNET_YES == ok)
1145 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1147 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1149 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1151 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1153 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1155 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1158 _("Error writing hostlist URIs to file `%s'\n"),
1164 if ( GNUNET_YES == shutdown)
1167 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1169 GNUNET_STATISTICS_set (stats,
1170 gettext_noop("# hostlist URIs written to file"),
1174 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1175 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1176 _("Error writing hostlist URIs to file `%s'\n"),
1178 GNUNET_free (filename);
1182 * Start downloading hostlists from hostlist servers as necessary.
1185 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1186 struct GNUNET_SCHEDULER_Handle *s,
1187 struct GNUNET_STATISTICS_Handle *st,
1188 GNUNET_CORE_ConnectEventHandler *ch,
1189 GNUNET_CORE_DisconnectEventHandler *dh,
1190 GNUNET_CORE_MessageCallback *msgh,
1193 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1196 return GNUNET_SYSERR;
1198 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1199 if (NULL == transport)
1201 curl_global_cleanup ();
1202 return GNUNET_SYSERR;
1208 GNUNET_CONFIGURATION_get_value_string (cfg,
1213 *ch = &connect_handler;
1214 *dh = &disconnect_handler;
1216 *msgh = &advertisement_handler;
1219 linked_list_head = NULL;
1220 linked_list_tail = NULL;
1221 use_preconfigured_list = GNUNET_YES;
1222 load_hostlist_file ();
1224 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1225 _("Hostlists will be saved to file again in %llums\n"),
1226 (unsigned long long) SAVING_INTERVALL.value);
1227 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1229 &hostlist_saving_task,
1232 GNUNET_STATISTICS_get (stats,
1234 gettext_noop("# seconds between hostlist downloads"),
1235 GNUNET_TIME_UNIT_MINUTES,
1244 * Stop downloading hostlists from hostlist servers as necessary.
1247 GNUNET_HOSTLIST_client_stop ()
1249 #if DEBUG_HOSTLIST_CLIENT
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1251 "Hostlist client shutdown\n");
1253 save_hostlist_file ( GNUNET_YES );
1255 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1257 GNUNET_SCHEDULER_cancel (sched,
1259 curl_global_cleanup ();
1261 if (transport != NULL)
1263 GNUNET_TRANSPORT_disconnect (transport);
1266 GNUNET_assert (NULL == transport);
1267 GNUNET_free_non_null (proxy);
1273 /* end of hostlist-client.c */