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 * Head of the linked list used to store hostlists
204 static struct Hostlist * hostlist_to_test;
206 static int testing_hostlist;
209 * Value saying if preconfigured is used
211 static unsigned int use_preconfigured_list;
214 * Set if we are allowed to learn new hostlists and use them
219 * Value saying how many valid HELLO messages were obtained during download
221 static unsigned int hellos_obtained;
224 * Value saying if hostlist download was successful
226 static unsigned int download_successful;
229 * Process downloaded bits by calling callback on each HELLO.
231 * @param ptr buffer with downloaded data
232 * @param size size of a record
233 * @param nmemb number of records downloaded
235 * @return number of bytes that were processed (always size*nmemb)
238 download_hostlist_processor (void *ptr,
243 const char * cbuf = ptr;
244 const struct GNUNET_MessageHeader *msg;
250 total = size * nmemb;
251 if ( (total == 0) || (bogus_url) )
253 return total; /* ok, no data or bogus data */
255 GNUNET_STATISTICS_update (stats,
256 gettext_noop ("# bytes downloaded from hostlist servers"),
260 while ( (left > 0) ||
263 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
264 memcpy (&download_buffer[download_pos],
270 if (download_pos < sizeof(struct GNUNET_MessageHeader))
272 GNUNET_assert (left == 0);
275 msg = (const struct GNUNET_MessageHeader *) download_buffer;
276 msize = ntohs(msg->size);
277 if (msize < sizeof(struct GNUNET_MessageHeader))
279 GNUNET_STATISTICS_update (stats,
280 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
283 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
284 _("Invalid `%s' message received from hostlist at `%s'\n"),
290 if (download_pos < msize)
292 GNUNET_assert (left == 0);
295 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
297 #if DEBUG_HOSTLIST_CLIENT
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Received valid `%s' message from hostlist server.\n",
302 GNUNET_STATISTICS_update (stats,
303 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
307 GNUNET_TRANSPORT_offer_hello (transport, msg);
311 GNUNET_STATISTICS_update (stats,
312 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316 _("Invalid `%s' message received from hostlist at `%s'\n"),
319 bogus_url = GNUNET_YES;
322 memmove (download_buffer,
323 &download_buffer[msize],
324 download_pos - msize);
325 download_pos -= msize;
332 * Obtain a hostlist URL that we should use.
334 * @return NULL if there is no URL available
345 GNUNET_CONFIGURATION_get_value_string (cfg,
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
351 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
352 "SERVERS", "HOSTLIST");
357 if (strlen (servers) > 0)
360 pos = strlen (servers) - 1;
363 if (servers[pos] == ' ')
370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
371 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
372 "SERVERS", "HOSTLIST");
373 GNUNET_free (servers);
377 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
378 pos = strlen (servers) - 1;
381 if (servers[pos] == ' ')
393 ret = GNUNET_strdup (&servers[pos]);
394 GNUNET_free (servers);
399 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
400 * @return uri to use, NULL if there is no URL available
406 unsigned int counter;
407 struct Hostlist * pos;
409 if ( GNUNET_NO == learning)
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Using preconfigured bootstrap server\n");
413 current_hostlist = NULL;
414 return get_bootstrap_url();
417 if ( ( GNUNET_YES == testing_hostlist) && (NULL != hostlist_to_test) )
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Testing new advertised hostlist if it is obtainable\n");
421 current_hostlist = hostlist_to_test;
422 return strdup(hostlist_to_test->hostlist_uri);
425 if ( (GNUNET_YES == use_preconfigured_list) ||
426 (linked_list_size == 0) )
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Using preconfigured bootstrap server\n");
430 current_hostlist = NULL;
431 return get_bootstrap_url();
433 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
435 pos = linked_list_head;
436 while ( counter < index )
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Using learned hostlist `%s'\n", pos->hostlist_uri);
443 current_hostlist = pos;
444 return strdup(pos->hostlist_uri);
448 #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);
452 * Schedule the background task that will (possibly)
453 * download a hostlist.
456 schedule_hostlist_task (void);
459 * Method to load persistent hostlist file during hostlist client shutdown
460 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
462 static void save_hostlist_file ( int shutdown );
465 * add val2 to val1 with overflow check
466 * @param val1 value 1
467 * @param val2 value 2
470 static uint64_t checked_add (uint64_t val1, uint64_t val2)
472 static uint64_t temp;
473 static uint64_t maxv;
486 * Subtract val2 from val1 with underflow check
487 * @param val1 value 1
488 * @param val2 value 2
491 static uint64_t checked_sub (uint64_t val1, uint64_t val2)
500 * Method to check if URI is in hostlist linked list
501 * @param uri uri to check
502 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
505 linked_list_contains (const char * uri)
507 struct Hostlist * pos;
509 pos = linked_list_head;
512 if (0 == strcmp(pos->hostlist_uri, uri) )
521 static struct Hostlist *
522 linked_list_get_lowest_quality ( )
524 struct Hostlist * pos;
525 struct Hostlist * lowest;
527 if (linked_list_size == 0)
529 lowest = linked_list_head;
530 pos = linked_list_head->next;
533 if (pos->quality < lowest->quality)
542 * Task that checks if we should try to download a hostlist.
543 * If so, we initiate the download, otherwise we schedule
544 * this task again for a later time.
547 insert_hostlist ( void )
549 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist_to_test);
552 GNUNET_STATISTICS_set (stats,
553 gettext_noop("# advertised hostlist URIs"),
557 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
560 /* No free entries available, replace existing entry */
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "Removing lowest quality entry\n" );
563 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
566 lowest_quality->hostlist_uri,
567 (unsigned long long) lowest_quality->quality);
568 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
571 GNUNET_STATISTICS_set (stats,
572 gettext_noop("# advertised hostlist URIs"),
576 GNUNET_free (lowest_quality);
578 testing_hostlist = GNUNET_NO;
584 * Method updating hostlist statistics
586 static void update_hostlist ( )
589 if ( ((use_preconfigured_list == GNUNET_NO) && ( NULL != current_hostlist )) ||
590 ((testing_hostlist == GNUNET_YES) && ( NULL != current_hostlist )) )
592 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
593 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
594 current_hostlist->hello_count = hellos_obtained;
595 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
596 current_hostlist->quality = checked_add ( current_hostlist->quality, (hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
597 if ( GNUNET_YES == download_successful )
599 current_hostlist->times_used++;
600 current_hostlist->quality = checked_add ( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
601 GNUNET_asprintf (&stat,
602 gettext_noop("# advertised URI `%s' downloaded"),
603 current_hostlist->hostlist_uri);
604 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
605 "Updating downloaded statics for URI to value\n" );
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 schedule_hostlist_task ();
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 if (counter >= MAX_HELLO_PER_HOSTLISTS)
819 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
820 _("Maximum number of %u HELLOs per hostlist exceeded, stopping download...\n"),
821 MAX_HELLO_PER_HOSTLISTS);
826 GNUNET_break (msg != NULL);
832 if ( (msg->data.result != CURLE_OK) &&
833 (msg->data.result != CURLE_GOT_NOTHING) )
834 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
835 _("%s failed for `%s' at %s:%d: `%s'\n"),
836 "curl_multi_perform",
840 curl_easy_strerror (msg->data.result));
843 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
844 _("Download of hostlist `%s' completed.\n"),
846 download_successful = GNUNET_YES;
848 if (GNUNET_YES == testing_hostlist)
850 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
851 _("Adding successfully tested hostlist `%s' datastore.\n"),current_url);
853 hostlist_to_test = NULL;
854 testing_hostlist = GNUNET_NO;
867 while (mret == CURLM_CALL_MULTI_PERFORM);
868 if (mret != CURLM_OK)
870 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
871 _("%s failed at %s:%d: `%s'\n"),
872 "curl_multi_perform", __FILE__, __LINE__,
873 curl_multi_strerror (mret));
881 * Main function that will download a hostlist and process its
890 current_url = get_list_url ();
891 if (current_url == NULL)
893 curl = curl_easy_init ();
901 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
902 _("Bootstrapping using hostlist at `%s'.\n"),
905 download_successful = GNUNET_NO;
906 GNUNET_STATISTICS_update (stats,
907 gettext_noop ("# hostlist downloads initiated"),
911 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
914 CURL_EASY_SETOPT (curl,
915 CURLOPT_WRITEFUNCTION,
916 &download_hostlist_processor);
922 CURL_EASY_SETOPT (curl,
930 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
931 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
932 /* no need to abort if the above failed */
933 CURL_EASY_SETOPT (curl,
941 CURL_EASY_SETOPT (curl,
945 CURL_EASY_SETOPT (curl,
949 CURL_EASY_SETOPT (curl,
951 GNUNET_SERVER_MAX_MESSAGE_SIZE);
952 if (0 == strncmp (current_url, "http", 4))
953 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
954 CURL_EASY_SETOPT (curl,
955 CURLOPT_CONNECTTIMEOUT,
957 CURL_EASY_SETOPT (curl,
961 /* this should no longer be needed; we're now single-threaded! */
962 CURL_EASY_SETOPT (curl,
966 multi = curl_multi_init ();
973 mret = curl_multi_add_handle (multi, curl);
974 if (mret != CURLM_OK)
976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
977 _("%s failed at %s:%d: `%s'\n"),
978 "curl_multi_add_handle", __FILE__, __LINE__,
979 curl_multi_strerror (mret));
980 mret = curl_multi_cleanup (multi);
981 if (mret != CURLM_OK)
982 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
983 _("%s failed at %s:%d: `%s'\n"),
984 "curl_multi_cleanup", __FILE__, __LINE__,
985 curl_multi_strerror (mret));
990 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
996 * Task that checks if we should try to download a hostlist.
997 * If so, we initiate the download, otherwise we schedule
998 * this task again for a later time.
1001 check_task (void *cls,
1002 const struct GNUNET_SCHEDULER_TaskContext *tc)
1004 current_task = GNUNET_SCHEDULER_NO_TASK;
1005 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1007 if (connection_count < MIN_CONNECTIONS)
1008 download_hostlist ();
1010 schedule_hostlist_task ();
1015 * Compute when we should check the next time about downloading
1016 * a hostlist; then schedule the task accordingly.
1019 schedule_hostlist_task ()
1022 struct GNUNET_TIME_Relative delay;
1026 curl_global_cleanup ();
1027 return; /* in shutdown */
1029 delay = hostlist_delay;
1030 if (hostlist_delay.value == 0)
1031 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
1033 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
1034 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
1035 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
1036 (1 + connection_count));
1037 GNUNET_STATISTICS_set (stats,
1038 gettext_noop("# seconds between hostlist downloads"),
1039 hostlist_delay.value,
1043 delay = GNUNET_TIME_UNIT_ZERO;
1046 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1047 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
1050 (unsigned long long) delay.value);
1051 current_task = GNUNET_SCHEDULER_add_delayed (sched,
1058 * Task that writes hostlist entries to a file on a regular base
1063 hostlist_saving_task (void *cls,
1064 const struct GNUNET_SCHEDULER_TaskContext *tc)
1066 saving_task = GNUNET_SCHEDULER_NO_TASK;
1067 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1069 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1070 _("Scheduled saving of hostlists\n"));
1071 save_hostlist_file ( GNUNET_NO );
1073 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1074 _("Hostlists will be saved to file again in %llums\n"),
1075 (unsigned long long) SAVING_INTERVALL.value);
1076 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1078 &hostlist_saving_task,
1083 * Method called whenever a given peer connects.
1085 * @param cls closure
1086 * @param peer peer identity this notification is about
1087 * @param latency reported latency of the connection with 'other'
1088 * @param distance reported distance (DV) to 'other'
1091 connect_handler (void *cls,
1093 GNUNET_PeerIdentity * peer,
1094 struct GNUNET_TIME_Relative latency,
1098 GNUNET_STATISTICS_update (stats,
1099 gettext_noop ("# active connections"),
1106 * Method called whenever a given peer disconnects.
1108 * @param cls closure
1109 * @param peer peer identity this notification is about
1112 disconnect_handler (void *cls,
1114 GNUNET_PeerIdentity * peer)
1117 GNUNET_STATISTICS_update (stats,
1118 gettext_noop ("# active connections"),
1125 * Method called whenever an advertisement message arrives.
1127 * @param cls closure (always NULL)
1128 * @param peer the peer sending the message
1129 * @param message the actual message
1130 * @param latency latency
1131 * @param distance distance
1132 * @return GNUNET_OK to keep the connection open,
1133 * GNUNET_SYSERR to close it (signal serious error)
1136 advertisement_handler (void *cls,
1137 const struct GNUNET_PeerIdentity * peer,
1138 const struct GNUNET_MessageHeader * message,
1139 struct GNUNET_TIME_Relative latency,
1144 const struct GNUNET_MessageHeader * incoming;
1146 struct Hostlist * hostlist;
1148 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1149 size = ntohs (message->size);
1150 if (size <= sizeof(struct GNUNET_MessageHeader))
1152 GNUNET_break_op (0);
1153 return GNUNET_SYSERR;
1155 incoming = (const struct GNUNET_MessageHeader *) message;
1156 uri = (const char*) &incoming[1];
1157 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1158 if (uri [uri_size - 1] != '\0')
1160 GNUNET_break_op (0);
1161 return GNUNET_SYSERR;
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1167 if (GNUNET_NO != linked_list_contains (uri))
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "URI `%s' is already known\n",
1175 if ( GNUNET_YES == testing_hostlist )
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "Currently not accepting adverts\n");
1182 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1183 hostlist->hostlist_uri = (const char*) &hostlist[1];
1184 memcpy (&hostlist[1], uri, uri_size);
1185 hostlist->time_creation = GNUNET_TIME_absolute_get();
1186 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1187 hostlist->quality = HOSTLIST_INITIAL;
1188 hostlist_to_test = hostlist;
1190 testing_hostlist = GNUNET_YES;
1197 * Continuation called by the statistics code once
1198 * we go the stat. Initiates hostlist download scheduling.
1200 * @param cls closure
1201 * @param success GNUNET_OK if statistics were
1202 * successfully obtained, GNUNET_SYSERR if not.
1205 primary_task (void *cls, int success)
1208 return; /* in shutdown */
1209 #if DEBUG_HOSTLIST_CLIENT
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "Statistics request done, scheduling hostlist download\n");
1213 schedule_hostlist_task ();
1218 process_stat (void *cls,
1219 const char *subsystem,
1224 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1225 _("Initial time between hostlist downloads is %llums\n"),
1226 (unsigned long long) value);
1227 hostlist_delay.value = value;
1232 * Method to load persistent hostlist file during hostlist client startup
1235 load_hostlist_file ()
1240 struct Hostlist * hostlist;
1242 uint32_t times_used;
1243 uint32_t hellos_returned;
1250 GNUNET_CONFIGURATION_get_value_string (cfg,
1255 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1256 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1257 "HOSTLISTFILE", "HOSTLIST");
1261 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1262 _("Loading saved hostlist entries from file `%s' \n"), filename);
1263 if ( GNUNET_NO == GNUNET_DISK_file_test (filename) )
1265 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1266 _("Hostlist file `%s' is not existing\n"), filename);
1267 GNUNET_free ( filename );
1271 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1274 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1275 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1278 GNUNET_free (filename);
1283 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1284 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1285 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1286 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1287 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1288 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1290 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1291 hostlist->hello_count = hellos_returned;
1292 hostlist->hostlist_uri = (const char *) &hostlist[1];
1293 memcpy (&hostlist[1], uri, strlen(uri)+1);
1294 hostlist->quality = quality;
1295 hostlist->time_creation.value = created;
1296 hostlist->time_last_usage.value = last_used;
1297 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1304 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1307 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1308 _("%u hostlist URIs loaded from file\n"), counter);
1309 GNUNET_STATISTICS_set (stats,
1310 gettext_noop("# hostlist URIs read from file"),
1313 GNUNET_STATISTICS_set (stats,
1314 gettext_noop("# advertised hostlist URIs"),
1318 GNUNET_free_non_null (uri);
1320 GNUNET_BIO_read_close (rh, &emsg);
1323 GNUNET_free (filename);
1328 * Method to save persistent hostlist file during hostlist client shutdown
1329 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1331 static void save_hostlist_file ( int shutdown )
1334 struct Hostlist *pos;
1335 struct GNUNET_BIO_WriteHandle * wh;
1340 GNUNET_CONFIGURATION_get_value_string (cfg,
1345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1346 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1347 "HOSTLISTFILE", "HOSTLIST");
1350 wh = GNUNET_BIO_write_open (filename);
1353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1354 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1357 GNUNET_free (filename);
1360 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1361 _("Writing %u hostlist URIs to `%s'\n" ),
1362 linked_list_size, filename);
1364 /* add code to write hostlists to file using bio */
1367 while (NULL != (pos = linked_list_head))
1369 if ( GNUNET_YES == shutdown)
1371 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1374 if (GNUNET_YES == ok)
1377 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1379 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1381 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1383 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1385 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1387 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1390 _("Error writing hostlist URIs to file `%s'\n"),
1396 if ( GNUNET_YES == shutdown)
1399 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1401 GNUNET_STATISTICS_set (stats,
1402 gettext_noop("# hostlist URIs written to file"),
1406 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1408 _("Error writing hostlist URIs to file `%s'\n"),
1410 GNUNET_free (filename);
1414 * Start downloading hostlists from hostlist servers as necessary.
1417 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1418 struct GNUNET_SCHEDULER_Handle *s,
1419 struct GNUNET_STATISTICS_Handle *st,
1420 GNUNET_CORE_ConnectEventHandler *ch,
1421 GNUNET_CORE_DisconnectEventHandler *dh,
1422 GNUNET_CORE_MessageCallback *msgh,
1428 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1431 return GNUNET_SYSERR;
1433 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1434 if (NULL == transport)
1436 curl_global_cleanup ();
1437 return GNUNET_SYSERR;
1443 GNUNET_CONFIGURATION_get_value_string (cfg,
1449 *ch = &connect_handler;
1450 *dh = &disconnect_handler;
1451 linked_list_head = NULL;
1452 linked_list_tail = NULL;
1453 use_preconfigured_list = GNUNET_YES;
1454 testing_hostlist = GNUNET_NO;
1456 if ( GNUNET_YES == learning )
1458 *msgh = &advertisement_handler;
1459 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1460 _("Learning is enabled on this peer\n"));
1461 load_hostlist_file ();
1462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1463 _("Hostlists will be saved to file again in %llums\n"),
1464 (unsigned long long) SAVING_INTERVALL.value);
1465 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1467 &hostlist_saving_task,
1472 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1473 _("Learning is not enabled on this peer\n"));
1475 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1480 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1482 result = remove (filename);
1484 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1485 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),filename);
1487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1488 _("Hostlist file `%s' could not be removed\n"),filename);
1491 GNUNET_free ( filename );
1493 GNUNET_STATISTICS_get (stats,
1495 gettext_noop("# seconds between hostlist downloads"),
1496 GNUNET_TIME_UNIT_MINUTES,
1505 * Stop downloading hostlists from hostlist servers as necessary.
1508 GNUNET_HOSTLIST_client_stop ()
1510 #if DEBUG_HOSTLIST_CLIENT
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "Hostlist client shutdown\n");
1514 if ( GNUNET_YES == learning )
1515 save_hostlist_file ( GNUNET_YES );
1517 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1519 GNUNET_SCHEDULER_cancel (sched,
1521 curl_global_cleanup ();
1523 if (transport != NULL)
1525 GNUNET_TRANSPORT_disconnect (transport);
1528 GNUNET_assert (NULL == transport);
1529 GNUNET_free_non_null (proxy);
1535 /* end of hostlist-client.c */