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 test_locked;
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();
416 if ( (GNUNET_YES == use_preconfigured_list) ||
417 (linked_list_size == 0) )
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Using preconfigured bootstrap server\n");
421 current_hostlist = NULL;
422 return get_bootstrap_url();
424 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
426 pos = linked_list_head;
427 while ( counter < index )
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Using learned hostlist `%s'\n", pos->hostlist_uri);
434 current_hostlist = pos;
435 return strdup(pos->hostlist_uri);
439 #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);
443 * Schedule the background task that will (possibly)
444 * download a hostlist.
447 schedule_hostlist_task (void);
450 * Method to load persistent hostlist file during hostlist client shutdown
451 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
453 static void save_hostlist_file ( int shutdown );
456 * add val2 to val1 with overflow check
457 * @param val1 value 1
458 * @param val2 value 2
461 static uint64_t checked_add (uint64_t val1, uint64_t val2)
463 static uint64_t temp;
464 static uint64_t maxv;
477 * Subtract val2 from val1 with underflow check
478 * @param val1 value 1
479 * @param val2 value 2
482 static uint64_t checked_sub (uint64_t val1, uint64_t val2)
492 * Method updating hostlist statistics
494 static void update_hostlist ( )
497 if ( (use_preconfigured_list == GNUNET_NO) && ( NULL != current_hostlist ) )
499 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
500 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
501 current_hostlist->hello_count = hellos_obtained;
502 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
503 current_hostlist->quality = checked_add ( current_hostlist->quality, (hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
504 if ( GNUNET_YES == download_successful )
506 current_hostlist->times_used++;
507 current_hostlist->quality = checked_add ( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
508 GNUNET_asprintf (&stat,
509 gettext_noop("# advertised URI `%s' downloaded"),
510 current_hostlist->hostlist_uri);
512 GNUNET_STATISTICS_update ( stats,
519 current_hostlist->quality = checked_sub ( current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD );
521 current_hostlist = NULL;
522 /* Alternating the usage of preconfigured and learned hostlists */
524 if ( GNUNET_YES == learning)
526 if (use_preconfigured_list == GNUNET_YES)
527 use_preconfigured_list = GNUNET_NO;
529 use_preconfigured_list = GNUNET_YES;
532 use_preconfigured_list = GNUNET_YES;
536 * Clean up the state from the task that downloaded the
537 * hostlist and schedule the next task.
546 mret = curl_multi_remove_handle (multi, curl);
547 if (mret != CURLM_OK)
549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550 _("%s failed at %s:%d: `%s'\n"),
551 "curl_multi_remove_handle", __FILE__, __LINE__,
552 curl_multi_strerror (mret));
554 mret = curl_multi_cleanup (multi);
555 if (mret != CURLM_OK)
556 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
557 _("%s failed at %s:%d: `%s'\n"),
558 "curl_multi_cleanup", __FILE__, __LINE__,
559 curl_multi_strerror (mret));
564 curl_easy_cleanup (curl);
567 GNUNET_free_non_null (current_url);
570 schedule_hostlist_task ();
574 * Task that is run when we are ready to receive more data from the hostlist
577 * @param cls closure, unused
578 * @param tc task context, unused
581 multi_ready (void *cls,
582 const struct GNUNET_SCHEDULER_TaskContext *tc);
585 * Ask CURL for the select set and then schedule the
586 * receiving task with the scheduler.
596 struct GNUNET_NETWORK_FDSet *grs;
597 struct GNUNET_NETWORK_FDSet *gws;
599 struct GNUNET_TIME_Relative rtime;
605 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
606 if (mret != CURLM_OK)
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 _("%s failed at %s:%d: `%s'\n"),
610 "curl_multi_fdset", __FILE__, __LINE__,
611 curl_multi_strerror (mret));
615 mret = curl_multi_timeout (multi, &timeout);
616 if (mret != CURLM_OK)
618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619 _("%s failed at %s:%d: `%s'\n"),
620 "curl_multi_timeout", __FILE__, __LINE__,
621 curl_multi_strerror (mret));
625 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
626 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
628 grs = GNUNET_NETWORK_fdset_create ();
629 gws = GNUNET_NETWORK_fdset_create ();
630 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
631 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
632 #if DEBUG_HOSTLIST_CLIENT
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "Scheduling task for hostlist download using cURL\n");
637 = GNUNET_SCHEDULER_add_select (sched,
638 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
639 GNUNET_SCHEDULER_NO_TASK,
645 GNUNET_NETWORK_fdset_destroy (gws);
646 GNUNET_NETWORK_fdset_destroy (grs);
651 * Task that is run when we are ready to receive more data from the hostlist
654 * @param cls closure, unused
655 * @param tc task context, unused
658 multi_ready (void *cls,
659 const struct GNUNET_SCHEDULER_TaskContext *tc)
661 unsigned int counter;
666 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
668 #if DEBUG_HOSTLIST_CLIENT
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Shutdown requested while trying to download hostlist from `%s'\n",
677 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
680 _("Timeout trying to download hostlist from `%s'\n"),
686 #if DEBUG_HOSTLIST_CLIENT
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Ready for processing hostlist client request\n");
694 mret = curl_multi_perform (multi, &running);
700 msg = curl_multi_info_read (multi, &running);
702 if (counter >= MAX_HELLO_PER_HOSTLISTS)
704 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
705 _("Maximum number of %u HELLOs per hostlist exceeded, stopping download...\n"),
706 MAX_HELLO_PER_HOSTLISTS);
711 GNUNET_break (msg != NULL);
717 if ( (msg->data.result != CURLE_OK) &&
718 (msg->data.result != CURLE_GOT_NOTHING) )
719 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
720 _("%s failed for `%s' at %s:%d: `%s'\n"),
721 "curl_multi_perform",
725 curl_easy_strerror (msg->data.result));
728 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
729 _("Download of hostlist `%s' completed.\n"),
731 download_successful = GNUNET_YES;
744 while (mret == CURLM_CALL_MULTI_PERFORM);
745 if (mret != CURLM_OK)
747 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
748 _("%s failed at %s:%d: `%s'\n"),
749 "curl_multi_perform", __FILE__, __LINE__,
750 curl_multi_strerror (mret));
758 * Main function that will download a hostlist and process its
767 current_url = get_list_url ();
768 if (current_url == NULL)
770 curl = curl_easy_init ();
778 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
779 _("Bootstrapping using hostlist at `%s'.\n"),
782 download_successful = GNUNET_NO;
783 GNUNET_STATISTICS_update (stats,
784 gettext_noop ("# hostlist downloads initiated"),
788 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
791 CURL_EASY_SETOPT (curl,
792 CURLOPT_WRITEFUNCTION,
793 &download_hostlist_processor);
799 CURL_EASY_SETOPT (curl,
807 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
808 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
809 /* no need to abort if the above failed */
810 CURL_EASY_SETOPT (curl,
818 CURL_EASY_SETOPT (curl,
822 CURL_EASY_SETOPT (curl,
826 CURL_EASY_SETOPT (curl,
828 GNUNET_SERVER_MAX_MESSAGE_SIZE);
829 if (0 == strncmp (current_url, "http", 4))
830 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
831 CURL_EASY_SETOPT (curl,
832 CURLOPT_CONNECTTIMEOUT,
834 CURL_EASY_SETOPT (curl,
838 /* this should no longer be needed; we're now single-threaded! */
839 CURL_EASY_SETOPT (curl,
843 multi = curl_multi_init ();
850 mret = curl_multi_add_handle (multi, curl);
851 if (mret != CURLM_OK)
853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
854 _("%s failed at %s:%d: `%s'\n"),
855 "curl_multi_add_handle", __FILE__, __LINE__,
856 curl_multi_strerror (mret));
857 mret = curl_multi_cleanup (multi);
858 if (mret != CURLM_OK)
859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
860 _("%s failed at %s:%d: `%s'\n"),
861 "curl_multi_cleanup", __FILE__, __LINE__,
862 curl_multi_strerror (mret));
867 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
873 * Task that checks if we should try to download a hostlist.
874 * If so, we initiate the download, otherwise we schedule
875 * this task again for a later time.
878 check_task (void *cls,
879 const struct GNUNET_SCHEDULER_TaskContext *tc)
881 current_task = GNUNET_SCHEDULER_NO_TASK;
882 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
884 if (connection_count < MIN_CONNECTIONS)
885 download_hostlist ();
887 schedule_hostlist_task ();
892 * Compute when we should check the next time about downloading
893 * a hostlist; then schedule the task accordingly.
896 schedule_hostlist_task ()
899 struct GNUNET_TIME_Relative delay;
903 curl_global_cleanup ();
904 return; /* in shutdown */
906 delay = hostlist_delay;
907 if (hostlist_delay.value == 0)
908 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
910 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
911 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
912 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
913 (1 + connection_count));
914 GNUNET_STATISTICS_set (stats,
915 gettext_noop("# seconds between hostlist downloads"),
916 hostlist_delay.value,
920 delay = GNUNET_TIME_UNIT_ZERO;
923 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
924 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
927 (unsigned long long) delay.value);
928 current_task = GNUNET_SCHEDULER_add_delayed (sched,
935 * Task that writes hostlist entries to a file on a regular base
940 hostlist_saving_task (void *cls,
941 const struct GNUNET_SCHEDULER_TaskContext *tc)
943 saving_task = GNUNET_SCHEDULER_NO_TASK;
944 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
946 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
947 _("Scheduled saving of hostlists\n"));
948 save_hostlist_file ( GNUNET_NO );
950 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
951 _("Hostlists will be saved to file again in %llums\n"),
952 (unsigned long long) SAVING_INTERVALL.value);
953 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
955 &hostlist_saving_task,
960 * Method called whenever a given peer connects.
963 * @param peer peer identity this notification is about
964 * @param latency reported latency of the connection with 'other'
965 * @param distance reported distance (DV) to 'other'
968 connect_handler (void *cls,
970 GNUNET_PeerIdentity * peer,
971 struct GNUNET_TIME_Relative latency,
975 GNUNET_STATISTICS_update (stats,
976 gettext_noop ("# active connections"),
983 * Method called whenever a given peer disconnects.
986 * @param peer peer identity this notification is about
989 disconnect_handler (void *cls,
991 GNUNET_PeerIdentity * peer)
994 GNUNET_STATISTICS_update (stats,
995 gettext_noop ("# active connections"),
1002 * Method to check if URI is in hostlist linked list
1003 * @param uri uri to check
1004 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
1007 linked_list_contains (const char * uri)
1009 struct Hostlist * pos;
1011 pos = linked_list_head;
1014 if (0 == strcmp(pos->hostlist_uri, uri) )
1023 static struct Hostlist *
1024 linked_list_get_lowest_quality ( )
1026 struct Hostlist * pos;
1027 struct Hostlist * lowest;
1029 if (linked_list_size == 0)
1031 lowest = linked_list_head;
1032 pos = linked_list_head->next;
1035 if (pos->quality < lowest->quality)
1043 * Task that checks if we should try to download a hostlist.
1044 * If so, we initiate the download, otherwise we schedule
1045 * this task again for a later time.
1048 check_hostlist_task (void *cls,
1049 const struct GNUNET_SCHEDULER_TaskContext *tc)
1051 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist_to_test);
1054 GNUNET_STATISTICS_set (stats,
1055 gettext_noop("# advertised hostlist URIs"),
1059 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
1062 /* No free entries available, replace existing entry */
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Removing lowest quality entry\n" );
1065 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
1068 lowest_quality->hostlist_uri,
1069 (unsigned long long) lowest_quality->quality);
1070 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
1073 GNUNET_STATISTICS_set (stats,
1074 gettext_noop("# advertised hostlist URIs"),
1078 GNUNET_free (lowest_quality);
1080 test_locked = GNUNET_NO;
1085 * Method called whenever an advertisement message arrives.
1087 * @param cls closure (always NULL)
1088 * @param peer the peer sending the message
1089 * @param message the actual message
1090 * @param latency latency
1091 * @param distance distance
1092 * @return GNUNET_OK to keep the connection open,
1093 * GNUNET_SYSERR to close it (signal serious error)
1096 advertisement_handler (void *cls,
1097 const struct GNUNET_PeerIdentity * peer,
1098 const struct GNUNET_MessageHeader * message,
1099 struct GNUNET_TIME_Relative latency,
1104 const struct GNUNET_MessageHeader * incoming;
1106 struct Hostlist * hostlist;
1108 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1109 size = ntohs (message->size);
1110 if (size <= sizeof(struct GNUNET_MessageHeader))
1112 GNUNET_break_op (0);
1113 return GNUNET_SYSERR;
1115 incoming = (const struct GNUNET_MessageHeader *) message;
1116 uri = (const char*) &incoming[1];
1117 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1118 if (uri [uri_size - 1] != '\0')
1120 GNUNET_break_op (0);
1121 return GNUNET_SYSERR;
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1127 if (GNUNET_NO != linked_list_contains (uri))
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "URI `%s' is already known\n",
1135 if ( GNUNET_YES == test_locked )
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "Currently not accepting adverts\n");
1142 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1143 hostlist->hostlist_uri = (const char*) &hostlist[1];
1144 memcpy (&hostlist[1], uri, uri_size);
1145 hostlist->time_creation = GNUNET_TIME_absolute_get();
1146 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1147 hostlist->quality = HOSTLIST_INITIAL;
1148 hostlist_to_test = hostlist;
1150 test_locked = GNUNET_YES;
1153 GNUNET_SCHEDULER_add_now (sched,
1154 &check_hostlist_task,
1161 * Continuation called by the statistics code once
1162 * we go the stat. Initiates hostlist download scheduling.
1164 * @param cls closure
1165 * @param success GNUNET_OK if statistics were
1166 * successfully obtained, GNUNET_SYSERR if not.
1169 primary_task (void *cls, int success)
1172 return; /* in shutdown */
1173 #if DEBUG_HOSTLIST_CLIENT
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "Statistics request done, scheduling hostlist download\n");
1177 schedule_hostlist_task ();
1182 process_stat (void *cls,
1183 const char *subsystem,
1188 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1189 _("Initial time between hostlist downloads is %llums\n"),
1190 (unsigned long long) value);
1191 hostlist_delay.value = value;
1196 * Method to load persistent hostlist file during hostlist client startup
1199 load_hostlist_file ()
1204 struct Hostlist * hostlist;
1206 uint32_t times_used;
1207 uint32_t hellos_returned;
1214 GNUNET_CONFIGURATION_get_value_string (cfg,
1219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1220 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1221 "HOSTLISTFILE", "HOSTLIST");
1225 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1226 _("Loading saved hostlist entries from file `%s' \n"), filename);
1227 if ( GNUNET_NO == GNUNET_DISK_file_test (filename) )
1229 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1230 _("Hostlist file `%s' is not existing\n"), filename);
1231 GNUNET_free ( filename );
1235 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1238 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1239 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1242 GNUNET_free (filename);
1247 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1248 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1249 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1250 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1251 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1252 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1254 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1255 hostlist->hello_count = hellos_returned;
1256 hostlist->hostlist_uri = (const char *) &hostlist[1];
1257 memcpy (&hostlist[1], uri, strlen(uri)+1);
1258 hostlist->quality = quality;
1259 hostlist->time_creation.value = created;
1260 hostlist->time_last_usage.value = last_used;
1261 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1268 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1271 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1272 _("%u hostlist URIs loaded from file\n"), counter);
1273 GNUNET_STATISTICS_set (stats,
1274 gettext_noop("# hostlist URIs read from file"),
1277 GNUNET_STATISTICS_set (stats,
1278 gettext_noop("# advertised hostlist URIs"),
1282 GNUNET_free_non_null (uri);
1284 GNUNET_BIO_read_close (rh, &emsg);
1287 GNUNET_free (filename);
1292 * Method to save persistent hostlist file during hostlist client shutdown
1293 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1295 static void save_hostlist_file ( int shutdown )
1298 struct Hostlist *pos;
1299 struct GNUNET_BIO_WriteHandle * wh;
1304 GNUNET_CONFIGURATION_get_value_string (cfg,
1309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1310 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1311 "HOSTLISTFILE", "HOSTLIST");
1314 wh = GNUNET_BIO_write_open (filename);
1317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1318 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1321 GNUNET_free (filename);
1324 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1325 _("Writing %u hostlist URIs to `%s'\n" ),
1326 linked_list_size, filename);
1328 /* add code to write hostlists to file using bio */
1331 while (NULL != (pos = linked_list_head))
1333 if ( GNUNET_YES == shutdown)
1335 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1338 if (GNUNET_YES == ok)
1341 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1343 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1345 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1347 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1349 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1351 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1354 _("Error writing hostlist URIs to file `%s'\n"),
1360 if ( GNUNET_YES == shutdown)
1363 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1365 GNUNET_STATISTICS_set (stats,
1366 gettext_noop("# hostlist URIs written to file"),
1370 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1372 _("Error writing hostlist URIs to file `%s'\n"),
1374 GNUNET_free (filename);
1378 * Start downloading hostlists from hostlist servers as necessary.
1381 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1382 struct GNUNET_SCHEDULER_Handle *s,
1383 struct GNUNET_STATISTICS_Handle *st,
1384 GNUNET_CORE_ConnectEventHandler *ch,
1385 GNUNET_CORE_DisconnectEventHandler *dh,
1386 GNUNET_CORE_MessageCallback *msgh,
1392 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1395 return GNUNET_SYSERR;
1397 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1398 if (NULL == transport)
1400 curl_global_cleanup ();
1401 return GNUNET_SYSERR;
1407 GNUNET_CONFIGURATION_get_value_string (cfg,
1413 *ch = &connect_handler;
1414 *dh = &disconnect_handler;
1415 linked_list_head = NULL;
1416 linked_list_tail = NULL;
1417 use_preconfigured_list = GNUNET_YES;
1418 test_locked = GNUNET_NO;
1420 if ( GNUNET_YES == learning )
1422 *msgh = &advertisement_handler;
1423 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1424 _("Learning is enabled on this peer\n"));
1425 load_hostlist_file ();
1426 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1427 _("Hostlists will be saved to file again in %llums\n"),
1428 (unsigned long long) SAVING_INTERVALL.value);
1429 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1431 &hostlist_saving_task,
1436 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1437 _("Learning is not enabled on this peer\n"));
1439 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1444 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1446 result = remove (filename);
1448 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1449 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),filename);
1451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1452 _("Hostlist file `%s' could not be removed\n"),filename);
1455 GNUNET_free ( filename );
1457 GNUNET_STATISTICS_get (stats,
1459 gettext_noop("# seconds between hostlist downloads"),
1460 GNUNET_TIME_UNIT_MINUTES,
1469 * Stop downloading hostlists from hostlist servers as necessary.
1472 GNUNET_HOSTLIST_client_stop ()
1474 #if DEBUG_HOSTLIST_CLIENT
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "Hostlist client shutdown\n");
1478 if ( GNUNET_YES == learning )
1479 save_hostlist_file ( GNUNET_YES );
1481 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1483 GNUNET_SCHEDULER_cancel (sched,
1485 curl_global_cleanup ();
1487 if (transport != NULL)
1489 GNUNET_TRANSPORT_disconnect (transport);
1492 GNUNET_assert (NULL == transport);
1493 GNUNET_free_non_null (proxy);
1499 /* end of hostlist-client.c */