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 * ID of the task checking the intervall between to hostlist tests
164 static GNUNET_SCHEDULER_TaskIdentifier testing_intervall_task;
167 * Amount of time we wait between hostlist downloads.
169 static struct GNUNET_TIME_Relative hostlist_delay;
172 * Set to GNUNET_YES if the current URL had some problems.
174 static int bogus_url;
177 * Number of active connections (according to core service).
179 static unsigned int connection_count;
182 * At what time MUST the current hostlist request be done?
184 static struct GNUNET_TIME_Absolute end_time;
187 * Head of the linked list used to store hostlists
189 static struct Hostlist * linked_list_head;
192 * Tail of the linked list used to store hostlists
194 static struct Hostlist * linked_list_tail;
197 * Current hostlist used for downloading
199 static struct Hostlist * current_hostlist;
202 * Size of the linke list used to store hostlists
204 static unsigned int linked_list_size;
207 * Head of the linked list used to store hostlists
209 static struct Hostlist * hostlist_to_test;
211 static int testing_hostlist;
213 static int testing_allowed;
215 static int download_in_progress;
218 * Value saying if preconfigured is used
220 static unsigned int use_preconfigured_list;
223 * Set if we are allowed to learn new hostlists and use them
228 * Value saying how many valid HELLO messages were obtained during download
230 static unsigned int hellos_obtained;
233 * Value saying if hostlist download was successful
235 static unsigned int download_successful;
238 * Process downloaded bits by calling callback on each HELLO.
240 * @param ptr buffer with downloaded data
241 * @param size size of a record
242 * @param nmemb number of records downloaded
244 * @return number of bytes that were processed (always size*nmemb)
247 download_hostlist_processor (void *ptr,
252 const char * cbuf = ptr;
253 const struct GNUNET_MessageHeader *msg;
259 total = size * nmemb;
260 if ( (total == 0) || (bogus_url) )
262 return total; /* ok, no data or bogus data */
264 GNUNET_STATISTICS_update (stats,
265 gettext_noop ("# bytes downloaded from hostlist servers"),
269 while ( (left > 0) ||
272 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
273 memcpy (&download_buffer[download_pos],
279 if (download_pos < sizeof(struct GNUNET_MessageHeader))
281 GNUNET_assert (left == 0);
284 msg = (const struct GNUNET_MessageHeader *) download_buffer;
285 msize = ntohs(msg->size);
286 if (msize < sizeof(struct GNUNET_MessageHeader))
288 GNUNET_STATISTICS_update (stats,
289 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
292 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
293 _("Invalid `%s' message received from hostlist at `%s'\n"),
299 if (download_pos < msize)
301 GNUNET_assert (left == 0);
304 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
306 #if DEBUG_HOSTLIST_CLIENT
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Received valid `%s' message from hostlist server.\n",
311 GNUNET_STATISTICS_update (stats,
312 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
316 GNUNET_TRANSPORT_offer_hello (transport, msg);
320 GNUNET_STATISTICS_update (stats,
321 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
324 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
325 _("Invalid `%s' message received from hostlist at `%s'\n"),
328 bogus_url = GNUNET_YES;
331 memmove (download_buffer,
332 &download_buffer[msize],
333 download_pos - msize);
334 download_pos -= msize;
341 * Obtain a hostlist URL that we should use.
343 * @return NULL if there is no URL available
354 GNUNET_CONFIGURATION_get_value_string (cfg,
359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
360 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
361 "SERVERS", "HOSTLIST");
366 if (strlen (servers) > 0)
369 pos = strlen (servers) - 1;
372 if (servers[pos] == ' ')
379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
380 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
381 "SERVERS", "HOSTLIST");
382 GNUNET_free (servers);
386 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
387 pos = strlen (servers) - 1;
390 if (servers[pos] == ' ')
402 ret = GNUNET_strdup (&servers[pos]);
403 GNUNET_free (servers);
408 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
409 * @return uri to use, NULL if there is no URL available
415 unsigned int counter;
416 struct Hostlist * pos;
418 if ( GNUNET_NO == learning)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Using preconfigured bootstrap server\n");
422 current_hostlist = NULL;
423 return get_bootstrap_url();
426 if ( ( GNUNET_YES == testing_hostlist) && (NULL != hostlist_to_test) )
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Testing new advertised hostlist if it is obtainable\n");
430 current_hostlist = hostlist_to_test;
431 return strdup(hostlist_to_test->hostlist_uri);
434 if ( (GNUNET_YES == use_preconfigured_list) ||
435 (linked_list_size == 0) )
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "Using preconfigured bootstrap server\n");
439 current_hostlist = NULL;
440 return get_bootstrap_url();
442 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
444 pos = linked_list_head;
445 while ( counter < index )
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "Using learned hostlist `%s'\n", pos->hostlist_uri);
452 current_hostlist = pos;
453 return strdup(pos->hostlist_uri);
457 #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);
460 * Method to load persistent hostlist file during hostlist client shutdown
461 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
463 static void save_hostlist_file ( int shutdown );
466 * add val2 to val1 with overflow check
467 * @param val1 value 1
468 * @param val2 value 2
471 static uint64_t checked_add (uint64_t val1, uint64_t val2)
473 static uint64_t temp;
474 static uint64_t maxv;
487 * Subtract val2 from val1 with underflow check
488 * @param val1 value 1
489 * @param val2 value 2
492 static uint64_t checked_sub (uint64_t val1, uint64_t val2)
501 * Method to check if URI is in hostlist linked list
502 * @param uri uri to check
503 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
506 linked_list_contains (const char * uri)
508 struct Hostlist * pos;
510 pos = linked_list_head;
513 if (0 == strcmp(pos->hostlist_uri, uri) )
522 * Method returning the uri with the lowest quality in the datastore
523 * @return hostlist with lowest quality
525 static struct Hostlist *
526 linked_list_get_lowest_quality ( )
528 struct Hostlist * pos;
529 struct Hostlist * lowest;
531 if (linked_list_size == 0)
533 lowest = linked_list_head;
534 pos = linked_list_head->next;
537 if (pos->quality < lowest->quality)
546 * Method to insert a hostlist into the datastore. If datastore contains maximum number of elements, the elements with lowest quality is dismissed
549 insert_hostlist ( void )
551 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist_to_test);
554 GNUNET_STATISTICS_set (stats,
555 gettext_noop("# advertised hostlist URIs"),
559 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
562 /* No free entries available, replace existing entry */
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "Removing lowest quality entry\n" );
565 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
568 lowest_quality->hostlist_uri,
569 (unsigned long long) lowest_quality->quality);
570 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
573 GNUNET_STATISTICS_set (stats,
574 gettext_noop("# advertised hostlist URIs"),
578 GNUNET_free (lowest_quality);
580 testing_hostlist = GNUNET_NO;
586 * Method updating hostlist statistics
588 static void update_hostlist ( )
591 if ( ((use_preconfigured_list == GNUNET_NO) && ( NULL != current_hostlist )) ||
592 ((testing_hostlist == GNUNET_YES) && ( NULL != current_hostlist )) )
594 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
595 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
596 current_hostlist->hello_count = hellos_obtained;
597 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
598 current_hostlist->quality = checked_add ( current_hostlist->quality, (hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
599 if ( GNUNET_YES == download_successful )
601 current_hostlist->times_used++;
602 current_hostlist->quality = checked_add ( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
603 GNUNET_asprintf (&stat,
604 gettext_noop("# advertised URI `%s' downloaded"),
605 current_hostlist->hostlist_uri);
607 GNUNET_STATISTICS_update ( stats,
614 current_hostlist->quality = checked_sub ( current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD );
616 current_hostlist = NULL;
617 /* Alternating the usage of preconfigured and learned hostlists */
619 if (testing_hostlist == GNUNET_YES)
622 if ( GNUNET_YES == learning)
624 if (use_preconfigured_list == GNUNET_YES)
625 use_preconfigured_list = GNUNET_NO;
627 use_preconfigured_list = GNUNET_YES;
630 use_preconfigured_list = GNUNET_YES;
634 * Clean up the state from the task that downloaded the
635 * hostlist and schedule the next task.
642 if ( ( testing_hostlist == GNUNET_YES ) && ( GNUNET_NO == download_successful) && (NULL != hostlist_to_test))
644 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
645 _("Advertised hostlist with URI `%s' could not be downloaded. Advertised URI gets dismissed.\n"),hostlist_to_test->hostlist_uri);
648 if ( testing_hostlist == GNUNET_YES )
650 testing_hostlist = GNUNET_NO;
652 if ( NULL != hostlist_to_test)
654 GNUNET_free (hostlist_to_test);
655 hostlist_to_test = NULL;
661 mret = curl_multi_remove_handle (multi, curl);
662 if (mret != CURLM_OK)
664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
665 _("%s failed at %s:%d: `%s'\n"),
666 "curl_multi_remove_handle", __FILE__, __LINE__,
667 curl_multi_strerror (mret));
669 mret = curl_multi_cleanup (multi);
670 if (mret != CURLM_OK)
671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
672 _("%s failed at %s:%d: `%s'\n"),
673 "curl_multi_cleanup", __FILE__, __LINE__,
674 curl_multi_strerror (mret));
679 curl_easy_cleanup (curl);
682 GNUNET_free_non_null (current_url);
685 download_in_progress = GNUNET_NO;
689 * Task that is run when we are ready to receive more data from the hostlist
692 * @param cls closure, unused
693 * @param tc task context, unused
696 multi_ready (void *cls,
697 const struct GNUNET_SCHEDULER_TaskContext *tc);
700 * Ask CURL for the select set and then schedule the
701 * receiving task with the scheduler.
711 struct GNUNET_NETWORK_FDSet *grs;
712 struct GNUNET_NETWORK_FDSet *gws;
714 struct GNUNET_TIME_Relative rtime;
720 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
721 if (mret != CURLM_OK)
723 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
724 _("%s failed at %s:%d: `%s'\n"),
725 "curl_multi_fdset", __FILE__, __LINE__,
726 curl_multi_strerror (mret));
730 mret = curl_multi_timeout (multi, &timeout);
731 if (mret != CURLM_OK)
733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
734 _("%s failed at %s:%d: `%s'\n"),
735 "curl_multi_timeout", __FILE__, __LINE__,
736 curl_multi_strerror (mret));
740 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
741 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
743 grs = GNUNET_NETWORK_fdset_create ();
744 gws = GNUNET_NETWORK_fdset_create ();
745 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
746 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
747 #if DEBUG_HOSTLIST_CLIENT
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 "Scheduling task for hostlist download using cURL\n");
752 = GNUNET_SCHEDULER_add_select (sched,
753 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
754 GNUNET_SCHEDULER_NO_TASK,
760 GNUNET_NETWORK_fdset_destroy (gws);
761 GNUNET_NETWORK_fdset_destroy (grs);
766 * Task that is run when we are ready to receive more data from the hostlist
769 * @param cls closure, unused
770 * @param tc task context, unused
773 multi_ready (void *cls,
774 const struct GNUNET_SCHEDULER_TaskContext *tc)
776 unsigned int counter;
781 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
783 #if DEBUG_HOSTLIST_CLIENT
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Shutdown requested while trying to download hostlist from `%s'\n",
792 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
794 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
795 _("Timeout trying to download hostlist from `%s'\n"),
801 #if DEBUG_HOSTLIST_CLIENT
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Ready for processing hostlist client request\n");
809 mret = curl_multi_perform (multi, &running);
815 msg = curl_multi_info_read (multi, &running);
817 GNUNET_break (msg != NULL);
823 if ( (msg->data.result != CURLE_OK) &&
824 (msg->data.result != CURLE_GOT_NOTHING) )
825 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
826 _("%s failed for `%s' at %s:%d: `%s'\n"),
827 "curl_multi_perform",
831 curl_easy_strerror (msg->data.result));
834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
835 _("Download of hostlist `%s' completed.\n"),
837 download_successful = GNUNET_YES;
839 if (GNUNET_YES == testing_hostlist)
841 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
842 _("Adding successfully tested hostlist `%s' datastore.\n"),current_url);
844 hostlist_to_test = NULL;
845 testing_hostlist = GNUNET_NO;
858 while (mret == CURLM_CALL_MULTI_PERFORM);
859 if (mret != CURLM_OK)
861 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
862 _("%s failed at %s:%d: `%s'\n"),
863 "curl_multi_perform", __FILE__, __LINE__,
864 curl_multi_strerror (mret));
872 * Main function that will download a hostlist and process its
882 current_url = get_list_url ();
883 if (current_url == NULL)
885 curl = curl_easy_init ();
893 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
894 _("Bootstrapping using hostlist at `%s'.\n"),
897 download_in_progress = GNUNET_YES;
898 download_successful = GNUNET_NO;
901 GNUNET_STATISTICS_update (stats,
902 gettext_noop ("# hostlist downloads initiated"),
906 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
909 CURL_EASY_SETOPT (curl,
910 CURLOPT_WRITEFUNCTION,
911 &download_hostlist_processor);
917 CURL_EASY_SETOPT (curl,
925 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
926 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
927 /* no need to abort if the above failed */
928 CURL_EASY_SETOPT (curl,
936 CURL_EASY_SETOPT (curl,
940 CURL_EASY_SETOPT (curl,
944 CURL_EASY_SETOPT (curl,
946 GNUNET_SERVER_MAX_MESSAGE_SIZE);
947 if (0 == strncmp (current_url, "http", 4))
948 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
949 CURL_EASY_SETOPT (curl,
950 CURLOPT_CONNECTTIMEOUT,
952 CURL_EASY_SETOPT (curl,
956 /* this should no longer be needed; we're now single-threaded! */
957 CURL_EASY_SETOPT (curl,
961 multi = curl_multi_init ();
968 mret = curl_multi_add_handle (multi, curl);
969 if (mret != CURLM_OK)
971 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
972 _("%s failed at %s:%d: `%s'\n"),
973 "curl_multi_add_handle", __FILE__, __LINE__,
974 curl_multi_strerror (mret));
975 mret = curl_multi_cleanup (multi);
976 if (mret != CURLM_OK)
977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
978 _("%s failed at %s:%d: `%s'\n"),
979 "curl_multi_cleanup", __FILE__, __LINE__,
980 curl_multi_strerror (mret));
985 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
991 download_dispatcher (void *cls,
992 const struct GNUNET_SCHEDULER_TaskContext *tc)
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Download is initiated...\n");
996 if ( GNUNET_NO == download_in_progress )
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Download can start immediately...\n");
1000 download_hostlist();
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Download in progess, have to wait...\n");
1006 GNUNET_SCHEDULER_add_delayed (sched,
1008 &download_dispatcher,
1014 * Task that checks if we should try to download a hostlist.
1015 * If so, we initiate the download, otherwise we schedule
1016 * this task again for a later time.
1019 check_task (void *cls,
1020 const struct GNUNET_SCHEDULER_TaskContext *tc)
1022 current_task = GNUNET_SCHEDULER_NO_TASK;
1023 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1026 if (connection_count < MIN_CONNECTIONS)
1028 GNUNET_SCHEDULER_add_now (sched,
1029 &download_dispatcher,
1034 struct GNUNET_TIME_Relative delay;
1038 curl_global_cleanup ();
1039 return; /* in shutdown */
1041 delay = hostlist_delay;
1042 if (hostlist_delay.value == 0)
1043 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
1045 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
1046 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
1047 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
1048 (1 + connection_count));
1049 GNUNET_STATISTICS_set (stats,
1050 gettext_noop("# seconds between hostlist downloads"),
1051 hostlist_delay.value,
1055 delay = GNUNET_TIME_UNIT_ZERO;
1058 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1059 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
1062 (unsigned long long) delay.value);
1063 current_task = GNUNET_SCHEDULER_add_delayed (sched,
1070 * This tasks sets hostlist testing to allowed after intervall between to testings is reached
1075 testing_intervall_reset (void *cls,
1076 const struct GNUNET_SCHEDULER_TaskContext *tc)
1078 testing_allowed = GNUNET_OK;
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "Testing new hostlist advertisements is allowed again\n");
1085 * Task that writes hostlist entries to a file on a regular base
1090 hostlist_saving_task (void *cls,
1091 const struct GNUNET_SCHEDULER_TaskContext *tc)
1093 saving_task = GNUNET_SCHEDULER_NO_TASK;
1094 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1096 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1097 _("Scheduled saving of hostlists\n"));
1098 save_hostlist_file ( GNUNET_NO );
1100 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1101 _("Hostlists will be saved to file again in %llums\n"),
1102 (unsigned long long) SAVING_INTERVALL.value);
1103 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1105 &hostlist_saving_task,
1110 * Method called whenever a given peer connects.
1112 * @param cls closure
1113 * @param peer peer identity this notification is about
1114 * @param latency reported latency of the connection with 'other'
1115 * @param distance reported distance (DV) to 'other'
1118 connect_handler (void *cls,
1120 GNUNET_PeerIdentity * peer,
1121 struct GNUNET_TIME_Relative latency,
1125 GNUNET_STATISTICS_update (stats,
1126 gettext_noop ("# active connections"),
1133 * Method called whenever a given peer disconnects.
1135 * @param cls closure
1136 * @param peer peer identity this notification is about
1139 disconnect_handler (void *cls,
1141 GNUNET_PeerIdentity * peer)
1144 GNUNET_STATISTICS_update (stats,
1145 gettext_noop ("# active connections"),
1151 * Method called whenever an advertisement message arrives.
1153 * @param cls closure (always NULL)
1154 * @param peer the peer sending the message
1155 * @param message the actual message
1156 * @param latency latency
1157 * @param distance distance
1158 * @return GNUNET_OK to keep the connection open,
1159 * GNUNET_SYSERR to close it (signal serious error)
1162 advertisement_handler (void *cls,
1163 const struct GNUNET_PeerIdentity * peer,
1164 const struct GNUNET_MessageHeader * message,
1165 struct GNUNET_TIME_Relative latency,
1170 const struct GNUNET_MessageHeader * incoming;
1172 struct Hostlist * hostlist;
1174 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1175 size = ntohs (message->size);
1176 if (size <= sizeof(struct GNUNET_MessageHeader))
1178 GNUNET_break_op (0);
1179 return GNUNET_SYSERR;
1181 incoming = (const struct GNUNET_MessageHeader *) message;
1182 uri = (const char*) &incoming[1];
1183 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1184 if (uri [uri_size - 1] != '\0')
1186 GNUNET_break_op (0);
1187 return GNUNET_SYSERR;
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1193 if (GNUNET_NO != linked_list_contains (uri))
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "URI `%s' is already known\n",
1201 if ( GNUNET_NO == testing_allowed )
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Currently not accepting new advertisements: interval between to advertisements is not reached\n");
1205 return GNUNET_SYSERR;
1207 if ( GNUNET_YES == testing_hostlist )
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210 "Currently not accepting new advertisements: we are already testing a hostlist\n");
1211 return GNUNET_SYSERR;
1214 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1215 hostlist->hostlist_uri = (const char*) &hostlist[1];
1216 memcpy (&hostlist[1], uri, uri_size);
1217 hostlist->time_creation = GNUNET_TIME_absolute_get();
1218 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1219 hostlist->quality = HOSTLIST_INITIAL;
1220 hostlist_to_test = hostlist;
1222 testing_hostlist = GNUNET_YES;
1223 testing_allowed = GNUNET_NO;
1224 testing_intervall_task = GNUNET_SCHEDULER_add_delayed (sched,
1226 &testing_intervall_reset,
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Testing new hostlist advertisements is locked for the next %u ms\n",
1233 testing_intervall_task = GNUNET_SCHEDULER_add_now (sched,
1234 &download_dispatcher,
1243 * Continuation called by the statistics code once
1244 * we go the stat. Initiates hostlist download scheduling.
1246 * @param cls closure
1247 * @param success GNUNET_OK if statistics were
1248 * successfully obtained, GNUNET_SYSERR if not.
1251 primary_task (void *cls, int success)
1254 return; /* in shutdown */
1255 #if DEBUG_HOSTLIST_CLIENT
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Statistics request done, scheduling hostlist download\n");
1259 current_task = GNUNET_SCHEDULER_add_now (sched,
1266 process_stat (void *cls,
1267 const char *subsystem,
1272 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1273 _("Initial time between hostlist downloads is %llums\n"),
1274 (unsigned long long) value);
1275 hostlist_delay.value = value;
1280 * Method to load persistent hostlist file during hostlist client startup
1283 load_hostlist_file ()
1288 struct Hostlist * hostlist;
1290 uint32_t times_used;
1291 uint32_t hellos_returned;
1298 GNUNET_CONFIGURATION_get_value_string (cfg,
1303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1304 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1305 "HOSTLISTFILE", "HOSTLIST");
1309 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1310 _("Loading saved hostlist entries from file `%s' \n"), filename);
1311 if ( GNUNET_NO == GNUNET_DISK_file_test (filename) )
1313 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1314 _("Hostlist file `%s' is not existing\n"), filename);
1315 GNUNET_free ( filename );
1319 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1322 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1323 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1326 GNUNET_free (filename);
1331 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1332 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1333 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1334 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1335 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1336 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1338 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1339 hostlist->hello_count = hellos_returned;
1340 hostlist->hostlist_uri = (const char *) &hostlist[1];
1341 memcpy (&hostlist[1], uri, strlen(uri)+1);
1342 hostlist->quality = quality;
1343 hostlist->time_creation.value = created;
1344 hostlist->time_last_usage.value = last_used;
1345 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1352 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1356 _("%u hostlist URIs loaded from file\n"), counter);
1357 GNUNET_STATISTICS_set (stats,
1358 gettext_noop("# hostlist URIs read from file"),
1361 GNUNET_STATISTICS_set (stats,
1362 gettext_noop("# advertised hostlist URIs"),
1366 GNUNET_free_non_null (uri);
1368 GNUNET_BIO_read_close (rh, &emsg);
1371 GNUNET_free (filename);
1376 * Method to save persistent hostlist file during hostlist client shutdown
1377 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1379 static void save_hostlist_file ( int shutdown )
1382 struct Hostlist *pos;
1383 struct GNUNET_BIO_WriteHandle * wh;
1388 GNUNET_CONFIGURATION_get_value_string (cfg,
1393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1394 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1395 "HOSTLISTFILE", "HOSTLIST");
1398 wh = GNUNET_BIO_write_open (filename);
1401 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1402 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1405 GNUNET_free (filename);
1408 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1409 _("Writing %u hostlist URIs to `%s'\n" ),
1410 linked_list_size, filename);
1412 /* add code to write hostlists to file using bio */
1415 while (NULL != (pos = linked_list_head))
1417 if ( GNUNET_YES == shutdown)
1419 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1422 if (GNUNET_YES == ok)
1425 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1427 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1429 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1431 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1433 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1435 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1437 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1438 _("Error writing hostlist URIs to file `%s'\n"),
1444 if ( GNUNET_YES == shutdown)
1447 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1449 GNUNET_STATISTICS_set (stats,
1450 gettext_noop("# hostlist URIs written to file"),
1454 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1456 _("Error writing hostlist URIs to file `%s'\n"),
1458 GNUNET_free (filename);
1462 * Start downloading hostlists from hostlist servers as necessary.
1465 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1466 struct GNUNET_SCHEDULER_Handle *s,
1467 struct GNUNET_STATISTICS_Handle *st,
1468 GNUNET_CORE_ConnectEventHandler *ch,
1469 GNUNET_CORE_DisconnectEventHandler *dh,
1470 GNUNET_CORE_MessageCallback *msgh,
1476 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1479 return GNUNET_SYSERR;
1481 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1482 if (NULL == transport)
1484 curl_global_cleanup ();
1485 return GNUNET_SYSERR;
1491 GNUNET_CONFIGURATION_get_value_string (cfg,
1497 *ch = &connect_handler;
1498 *dh = &disconnect_handler;
1499 linked_list_head = NULL;
1500 linked_list_tail = NULL;
1501 use_preconfigured_list = GNUNET_YES;
1502 testing_hostlist = GNUNET_NO;
1503 testing_allowed = GNUNET_YES;
1505 if ( GNUNET_YES == learning )
1507 *msgh = &advertisement_handler;
1508 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1509 _("Learning is enabled on this peer\n"));
1510 load_hostlist_file ();
1511 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1512 _("Hostlists will be saved to file again in %llums\n"),
1513 (unsigned long long) SAVING_INTERVALL.value);
1514 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1516 &hostlist_saving_task,
1521 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1522 _("Learning is not enabled on this peer\n"));
1524 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1529 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1531 result = remove (filename);
1533 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1534 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),filename);
1536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1537 _("Hostlist file `%s' could not be removed\n"),filename);
1540 GNUNET_free ( filename );
1542 GNUNET_STATISTICS_get (stats,
1544 gettext_noop("# seconds between hostlist downloads"),
1545 GNUNET_TIME_UNIT_MINUTES,
1554 * Stop downloading hostlists from hostlist servers as necessary.
1557 GNUNET_HOSTLIST_client_stop ()
1559 #if DEBUG_HOSTLIST_CLIENT
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1561 "Hostlist client shutdown\n");
1563 if ( GNUNET_YES == learning )
1564 save_hostlist_file ( GNUNET_YES );
1566 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1568 GNUNET_SCHEDULER_cancel (sched,
1570 curl_global_cleanup ();
1572 if (transport != NULL)
1574 GNUNET_TRANSPORT_disconnect (transport);
1577 GNUNET_assert (NULL == transport);
1578 GNUNET_free_non_null (proxy);
1584 /* end of hostlist-client.c */