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 * Value saying if preconfigured is used
204 static unsigned int use_preconfigured_list;
207 * Set if we are allowed to learn new hostlists and use them
212 * Value saying how many valid HELLO messages were obtained during download
214 static unsigned int hellos_obtained;
217 * Value saying if hostlist download was successful
219 static unsigned int download_successful;
222 * Process downloaded bits by calling callback on each HELLO.
224 * @param ptr buffer with downloaded data
225 * @param size size of a record
226 * @param nmemb number of records downloaded
228 * @return number of bytes that were processed (always size*nmemb)
231 download_hostlist_processor (void *ptr,
236 const char * cbuf = ptr;
237 const struct GNUNET_MessageHeader *msg;
243 total = size * nmemb;
244 if ( (total == 0) || (bogus_url) )
246 return total; /* ok, no data or bogus data */
248 GNUNET_STATISTICS_update (stats,
249 gettext_noop ("# bytes downloaded from hostlist servers"),
253 while ( (left > 0) ||
256 cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
257 memcpy (&download_buffer[download_pos],
263 if (download_pos < sizeof(struct GNUNET_MessageHeader))
265 GNUNET_assert (left == 0);
268 msg = (const struct GNUNET_MessageHeader *) download_buffer;
269 msize = ntohs(msg->size);
270 if (msize < sizeof(struct GNUNET_MessageHeader))
272 GNUNET_STATISTICS_update (stats,
273 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
276 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
277 _("Invalid `%s' message received from hostlist at `%s'\n"),
283 if (download_pos < msize)
285 GNUNET_assert (left == 0);
288 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
290 #if DEBUG_HOSTLIST_CLIENT
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Received valid `%s' message from hostlist server.\n",
295 GNUNET_STATISTICS_update (stats,
296 gettext_noop ("# valid HELLOs downloaded from hostlist servers"),
300 GNUNET_TRANSPORT_offer_hello (transport, msg);
304 GNUNET_STATISTICS_update (stats,
305 gettext_noop ("# invalid HELLOs downloaded from hostlist servers"),
308 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
309 _("Invalid `%s' message received from hostlist at `%s'\n"),
312 bogus_url = GNUNET_YES;
315 memmove (download_buffer,
316 &download_buffer[msize],
317 download_pos - msize);
318 download_pos -= msize;
325 * Obtain a hostlist URL that we should use.
327 * @return NULL if there is no URL available
338 GNUNET_CONFIGURATION_get_value_string (cfg,
343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
344 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
345 "SERVERS", "HOSTLIST");
350 if (strlen (servers) > 0)
353 pos = strlen (servers) - 1;
356 if (servers[pos] == ' ')
363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
364 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
365 "SERVERS", "HOSTLIST");
366 GNUNET_free (servers);
370 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
371 pos = strlen (servers) - 1;
374 if (servers[pos] == ' ')
386 ret = GNUNET_strdup (&servers[pos]);
387 GNUNET_free (servers);
392 * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
393 * @return uri to use, NULL if there is no URL available
399 unsigned int counter;
400 struct Hostlist * pos;
402 if ( GNUNET_NO == learning)
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Using preconfigured bootstrap server\n");
406 current_hostlist = NULL;
407 return get_bootstrap_url();
409 if ( (GNUNET_YES == use_preconfigured_list) ||
410 (linked_list_size == 0) )
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Using preconfigured bootstrap server\n");
414 current_hostlist = NULL;
415 return get_bootstrap_url();
417 index = GNUNET_CRYPTO_random_u32 ( GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
419 pos = linked_list_head;
420 while ( counter < index )
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Using learned hostlist `%s'\n", pos->hostlist_uri);
427 current_hostlist = pos;
428 return strdup(pos->hostlist_uri);
432 #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);
436 * Schedule the background task that will (possibly)
437 * download a hostlist.
440 schedule_hostlist_task (void);
443 * Method to load persistent hostlist file during hostlist client shutdown
444 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
446 static void save_hostlist_file ( int shutdown );
449 * add val2 to val1 with overflow check
450 * @param val1 value 1
451 * @param val2 value 2
454 static uint64_t checked_add (uint64_t val1, uint64_t val2)
456 static uint64_t temp;
457 static uint64_t maxv;
470 * Subtract val2 from val1 with underflow check
471 * @param val1 value 1
472 * @param val2 value 2
475 static uint64_t checked_sub (uint64_t val1, uint64_t val2)
485 * Method updating hostlist statistics
487 static void update_hostlist ( )
490 if ( (use_preconfigured_list == GNUNET_NO) && ( NULL != current_hostlist ) )
492 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
493 "Updating hostlist statics for URI `%s'\n",current_hostlist->hostlist_uri );
494 current_hostlist->hello_count = hellos_obtained;
495 current_hostlist->time_last_usage = GNUNET_TIME_absolute_get();
496 current_hostlist->quality = checked_add ( current_hostlist->quality, (hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
497 if ( GNUNET_YES == download_successful )
499 current_hostlist->times_used++;
500 current_hostlist->quality = checked_add ( current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
501 GNUNET_asprintf (&stat,
502 gettext_noop("# advertised URI `%s' downloaded"),
503 current_hostlist->hostlist_uri);
505 GNUNET_STATISTICS_update ( stats,
512 current_hostlist->quality = checked_sub ( current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD );
514 current_hostlist = NULL;
515 /* Alternating the usage of preconfigured and learned hostlists */
517 if ( GNUNET_YES == learning)
519 if (use_preconfigured_list == GNUNET_YES)
520 use_preconfigured_list = GNUNET_NO;
522 use_preconfigured_list = GNUNET_YES;
525 use_preconfigured_list = GNUNET_YES;
529 * Clean up the state from the task that downloaded the
530 * hostlist and schedule the next task.
539 mret = curl_multi_remove_handle (multi, curl);
540 if (mret != CURLM_OK)
542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
543 _("%s failed at %s:%d: `%s'\n"),
544 "curl_multi_remove_handle", __FILE__, __LINE__,
545 curl_multi_strerror (mret));
547 mret = curl_multi_cleanup (multi);
548 if (mret != CURLM_OK)
549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550 _("%s failed at %s:%d: `%s'\n"),
551 "curl_multi_cleanup", __FILE__, __LINE__,
552 curl_multi_strerror (mret));
557 curl_easy_cleanup (curl);
560 GNUNET_free_non_null (current_url);
563 schedule_hostlist_task ();
568 * Ask CURL for the select set and then schedule the
569 * receiving task with the scheduler.
576 * Task that is run when we are ready to receive more data from the hostlist
579 * @param cls closure, unused
580 * @param tc task context, unused
583 multi_ready (void *cls,
584 const struct GNUNET_SCHEDULER_TaskContext *tc)
590 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
592 #if DEBUG_HOSTLIST_CLIENT
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594 "Shutdown requested while trying to download hostlist from `%s'\n",
601 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
604 _("Timeout trying to download hostlist from `%s'\n"),
610 #if DEBUG_HOSTLIST_CLIENT
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "Ready for processing hostlist client request\n");
617 mret = curl_multi_perform (multi, &running);
622 msg = curl_multi_info_read (multi, &running);
623 GNUNET_break (msg != NULL);
629 if ( (msg->data.result != CURLE_OK) &&
630 (msg->data.result != CURLE_GOT_NOTHING) )
631 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
632 _("%s failed for `%s' at %s:%d: `%s'\n"),
633 "curl_multi_perform",
637 curl_easy_strerror (msg->data.result));
640 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
641 _("Download of hostlist `%s' completed.\n"),
643 download_successful = GNUNET_YES;
655 while (mret == CURLM_CALL_MULTI_PERFORM);
656 if (mret != CURLM_OK)
658 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
659 _("%s failed at %s:%d: `%s'\n"),
660 "curl_multi_perform", __FILE__, __LINE__,
661 curl_multi_strerror (mret));
669 * Ask CURL for the select set and then schedule the
670 * receiving task with the scheduler.
680 struct GNUNET_NETWORK_FDSet *grs;
681 struct GNUNET_NETWORK_FDSet *gws;
683 struct GNUNET_TIME_Relative rtime;
689 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
690 if (mret != CURLM_OK)
692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
693 _("%s failed at %s:%d: `%s'\n"),
694 "curl_multi_fdset", __FILE__, __LINE__,
695 curl_multi_strerror (mret));
699 mret = curl_multi_timeout (multi, &timeout);
700 if (mret != CURLM_OK)
702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
703 _("%s failed at %s:%d: `%s'\n"),
704 "curl_multi_timeout", __FILE__, __LINE__,
705 curl_multi_strerror (mret));
709 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
710 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
712 grs = GNUNET_NETWORK_fdset_create ();
713 gws = GNUNET_NETWORK_fdset_create ();
714 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
715 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
716 #if DEBUG_HOSTLIST_CLIENT
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Scheduling task for hostlist download using cURL\n");
721 = GNUNET_SCHEDULER_add_select (sched,
722 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
723 GNUNET_SCHEDULER_NO_TASK,
729 GNUNET_NETWORK_fdset_destroy (gws);
730 GNUNET_NETWORK_fdset_destroy (grs);
735 * Main function that will download a hostlist and process its
744 current_url = get_list_url ();
745 if (current_url == NULL)
747 curl = curl_easy_init ();
755 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
756 _("Bootstrapping using hostlist at `%s'.\n"),
759 download_successful = GNUNET_NO;
760 GNUNET_STATISTICS_update (stats,
761 gettext_noop ("# hostlist downloads initiated"),
765 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
768 CURL_EASY_SETOPT (curl,
769 CURLOPT_WRITEFUNCTION,
770 &download_hostlist_processor);
776 CURL_EASY_SETOPT (curl,
784 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
785 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
786 /* no need to abort if the above failed */
787 CURL_EASY_SETOPT (curl,
795 CURL_EASY_SETOPT (curl,
799 CURL_EASY_SETOPT (curl,
803 CURL_EASY_SETOPT (curl,
805 GNUNET_SERVER_MAX_MESSAGE_SIZE);
806 if (0 == strncmp (current_url, "http", 4))
807 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
808 CURL_EASY_SETOPT (curl,
809 CURLOPT_CONNECTTIMEOUT,
811 CURL_EASY_SETOPT (curl,
815 /* this should no longer be needed; we're now single-threaded! */
816 CURL_EASY_SETOPT (curl,
820 multi = curl_multi_init ();
827 mret = curl_multi_add_handle (multi, curl);
828 if (mret != CURLM_OK)
830 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
831 _("%s failed at %s:%d: `%s'\n"),
832 "curl_multi_add_handle", __FILE__, __LINE__,
833 curl_multi_strerror (mret));
834 mret = curl_multi_cleanup (multi);
835 if (mret != CURLM_OK)
836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
837 _("%s failed at %s:%d: `%s'\n"),
838 "curl_multi_cleanup", __FILE__, __LINE__,
839 curl_multi_strerror (mret));
844 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
850 * Task that checks if we should try to download a hostlist.
851 * If so, we initiate the download, otherwise we schedule
852 * this task again for a later time.
855 check_task (void *cls,
856 const struct GNUNET_SCHEDULER_TaskContext *tc)
858 current_task = GNUNET_SCHEDULER_NO_TASK;
859 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
861 if (connection_count < MIN_CONNECTIONS)
862 download_hostlist ();
864 schedule_hostlist_task ();
869 * Compute when we should check the next time about downloading
870 * a hostlist; then schedule the task accordingly.
873 schedule_hostlist_task ()
876 struct GNUNET_TIME_Relative delay;
880 curl_global_cleanup ();
881 return; /* in shutdown */
883 delay = hostlist_delay;
884 if (hostlist_delay.value == 0)
885 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
887 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
888 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
889 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
890 (1 + connection_count));
891 GNUNET_STATISTICS_set (stats,
892 gettext_noop("# seconds between hostlist downloads"),
893 hostlist_delay.value,
897 delay = GNUNET_TIME_UNIT_ZERO;
900 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
901 _("Have %u/%u connections. Will consider downloading hostlist in %llums\n"),
904 (unsigned long long) delay.value);
905 current_task = GNUNET_SCHEDULER_add_delayed (sched,
912 * Task that writes hostlist entries to a file on a regular base
917 hostlist_saving_task (void *cls,
918 const struct GNUNET_SCHEDULER_TaskContext *tc)
920 saving_task = GNUNET_SCHEDULER_NO_TASK;
921 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
923 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
924 _("Scheduled saving of hostlists\n"));
925 save_hostlist_file ( GNUNET_NO );
927 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
928 _("Hostlists will be saved to file again in %llums\n"),
929 (unsigned long long) SAVING_INTERVALL.value);
930 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
932 &hostlist_saving_task,
937 * Method called whenever a given peer connects.
940 * @param peer peer identity this notification is about
941 * @param latency reported latency of the connection with 'other'
942 * @param distance reported distance (DV) to 'other'
945 connect_handler (void *cls,
947 GNUNET_PeerIdentity * peer,
948 struct GNUNET_TIME_Relative latency,
952 GNUNET_STATISTICS_update (stats,
953 gettext_noop ("# active connections"),
960 * Method called whenever a given peer disconnects.
963 * @param peer peer identity this notification is about
966 disconnect_handler (void *cls,
968 GNUNET_PeerIdentity * peer)
971 GNUNET_STATISTICS_update (stats,
972 gettext_noop ("# active connections"),
979 * Method to check if URI is in hostlist linked list
980 * @param uri uri to check
981 * @return GNUNET_YES if existing in linked list, GNUNET_NO if not
984 linked_list_contains (const char * uri)
986 struct Hostlist * pos;
988 pos = linked_list_head;
991 if (0 == strcmp(pos->hostlist_uri, uri) )
1000 static struct Hostlist *
1001 linked_list_get_lowest_quality ( )
1003 struct Hostlist * pos;
1004 struct Hostlist * lowest;
1006 if (linked_list_size == 0)
1008 lowest = linked_list_head;
1009 pos = linked_list_head->next;
1012 if (pos->quality < lowest->quality)
1021 * Method called whenever an advertisement message arrives.
1023 * @param cls closure (always NULL)
1024 * @param peer the peer sending the message
1025 * @param message the actual message
1026 * @param latency latency
1027 * @param distance distance
1028 * @return GNUNET_OK to keep the connection open,
1029 * GNUNET_SYSERR to close it (signal serious error)
1032 advertisement_handler (void *cls,
1033 const struct GNUNET_PeerIdentity * peer,
1034 const struct GNUNET_MessageHeader * message,
1035 struct GNUNET_TIME_Relative latency,
1040 const struct GNUNET_MessageHeader * incoming;
1042 struct Hostlist * hostlist;
1044 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
1045 size = ntohs (message->size);
1046 if (size <= sizeof(struct GNUNET_MessageHeader))
1048 GNUNET_break_op (0);
1049 return GNUNET_SYSERR;
1051 incoming = (const struct GNUNET_MessageHeader *) message;
1052 uri = (const char*) &incoming[1];
1053 uri_size = size - sizeof (struct GNUNET_MessageHeader);
1054 if (uri [uri_size - 1] != '\0')
1056 GNUNET_break_op (0);
1057 return GNUNET_SYSERR;
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
1063 if (GNUNET_NO != linked_list_contains (uri))
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "URI `%s' is already known\n",
1070 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
1071 hostlist->hostlist_uri = (const char*) &hostlist[1];
1072 memcpy (&hostlist[1], uri, uri_size);
1073 hostlist->time_creation = GNUNET_TIME_absolute_get();
1074 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
1075 hostlist->quality = HOSTLIST_INITIAL;
1077 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1080 GNUNET_STATISTICS_set (stats,
1081 gettext_noop("# advertised hostlist URIs"),
1085 if (MAX_NUMBER_HOSTLISTS >= linked_list_size)
1088 /* No free entries available, replace existing entry */
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Removing lowest quality entry\n" );
1091 struct Hostlist * lowest_quality = linked_list_get_lowest_quality();
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 "Hostlist with URI `%s' has the worst quality of all with value %llu\n",
1094 lowest_quality->hostlist_uri,
1095 (unsigned long long) lowest_quality->quality);
1096 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, lowest_quality);
1099 GNUNET_STATISTICS_set (stats,
1100 gettext_noop("# advertised hostlist URIs"),
1104 GNUNET_free (lowest_quality);
1110 * Continuation called by the statistics code once
1111 * we go the stat. Initiates hostlist download scheduling.
1113 * @param cls closure
1114 * @param success GNUNET_OK if statistics were
1115 * successfully obtained, GNUNET_SYSERR if not.
1118 primary_task (void *cls, int success)
1121 return; /* in shutdown */
1122 #if DEBUG_HOSTLIST_CLIENT
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "Statistics request done, scheduling hostlist download\n");
1126 schedule_hostlist_task ();
1131 process_stat (void *cls,
1132 const char *subsystem,
1137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1138 _("Initial time between hostlist downloads is %llums\n"),
1139 (unsigned long long) value);
1140 hostlist_delay.value = value;
1145 * Method to load persistent hostlist file during hostlist client startup
1148 load_hostlist_file ()
1153 struct Hostlist * hostlist;
1155 uint32_t times_used;
1156 uint32_t hellos_returned;
1163 GNUNET_CONFIGURATION_get_value_string (cfg,
1168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1169 _("No `%s' specified in `%s' configuration, cannot load hostlists from file.\n"),
1170 "HOSTLISTFILE", "HOSTLIST");
1174 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1175 _("Loading saved hostlist entries from file `%s' \n"), filename);
1176 if ( GNUNET_NO == GNUNET_DISK_file_test (filename) )
1178 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1179 _("Hostlist file `%s' is not existing\n"), filename);
1180 GNUNET_free ( filename );
1184 struct GNUNET_BIO_ReadHandle * rh = GNUNET_BIO_read_open (filename);
1187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1188 _("Could not open file `%s' for reading to load hostlists: %s\n"),
1191 GNUNET_free (filename);
1196 while ( (GNUNET_OK == GNUNET_BIO_read_string (rh, "url" , &uri, MAX_URL_LEN)) &&
1197 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used)) &&
1198 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
1199 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
1200 (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
1201 (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)) )
1203 hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
1204 hostlist->hello_count = hellos_returned;
1205 hostlist->hostlist_uri = (const char *) &hostlist[1];
1206 memcpy (&hostlist[1], uri, strlen(uri)+1);
1207 hostlist->quality = quality;
1208 hostlist->time_creation.value = created;
1209 hostlist->time_last_usage.value = last_used;
1210 GNUNET_CONTAINER_DLL_insert(linked_list_head, linked_list_tail, hostlist);
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Added hostlist entry eith URI `%s' \n", hostlist->hostlist_uri);
1217 if ( counter >= MAX_NUMBER_HOSTLISTS ) break;
1220 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1221 _("%u hostlist URIs loaded from file\n"), counter);
1222 GNUNET_STATISTICS_set (stats,
1223 gettext_noop("# hostlist URIs read from file"),
1226 GNUNET_STATISTICS_set (stats,
1227 gettext_noop("# advertised hostlist URIs"),
1231 GNUNET_free_non_null (uri);
1233 GNUNET_BIO_read_close (rh, &emsg);
1236 GNUNET_free (filename);
1241 * Method to save persistent hostlist file during hostlist client shutdown
1242 * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
1244 static void save_hostlist_file ( int shutdown )
1247 struct Hostlist *pos;
1248 struct GNUNET_BIO_WriteHandle * wh;
1253 GNUNET_CONFIGURATION_get_value_string (cfg,
1258 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1259 _("No `%s' specified in `%s' configuration, cannot save hostlists to file.\n"),
1260 "HOSTLISTFILE", "HOSTLIST");
1263 wh = GNUNET_BIO_write_open (filename);
1266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1267 _("Could not open file `%s' for writing to save hostlists: %s\n"),
1270 GNUNET_free (filename);
1273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1274 _("Writing %u hostlist URIs to `%s'\n" ),
1275 linked_list_size, filename);
1277 /* add code to write hostlists to file using bio */
1280 while (NULL != (pos = linked_list_head))
1282 if ( GNUNET_YES == shutdown)
1284 GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
1287 if (GNUNET_YES == ok)
1290 GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
1292 GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
1294 GNUNET_BIO_write_int64 (wh, pos->quality)) ||
1296 GNUNET_BIO_write_int64 (wh, pos->time_last_usage.value)) ||
1298 GNUNET_BIO_write_int64 (wh, pos->time_creation.value)) ||
1300 GNUNET_BIO_write_int32 (wh, pos->hello_count)))
1302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1303 _("Error writing hostlist URIs to file `%s'\n"),
1309 if ( GNUNET_YES == shutdown)
1312 if ( counter >= MAX_NUMBER_HOSTLISTS) break;
1314 GNUNET_STATISTICS_set (stats,
1315 gettext_noop("# hostlist URIs written to file"),
1319 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
1320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1321 _("Error writing hostlist URIs to file `%s'\n"),
1323 GNUNET_free (filename);
1327 * Start downloading hostlists from hostlist servers as necessary.
1330 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
1331 struct GNUNET_SCHEDULER_Handle *s,
1332 struct GNUNET_STATISTICS_Handle *st,
1333 GNUNET_CORE_ConnectEventHandler *ch,
1334 GNUNET_CORE_DisconnectEventHandler *dh,
1335 GNUNET_CORE_MessageCallback *msgh,
1341 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1344 return GNUNET_SYSERR;
1346 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
1347 if (NULL == transport)
1349 curl_global_cleanup ();
1350 return GNUNET_SYSERR;
1356 GNUNET_CONFIGURATION_get_value_string (cfg,
1362 *ch = &connect_handler;
1363 *dh = &disconnect_handler;
1364 linked_list_head = NULL;
1365 linked_list_tail = NULL;
1366 use_preconfigured_list = GNUNET_YES;
1368 if ( GNUNET_YES == learning )
1370 *msgh = &advertisement_handler;
1371 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1372 _("Learning is enabled on this peer\n"));
1373 load_hostlist_file ();
1374 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1375 _("Hostlists will be saved to file again in %llums\n"),
1376 (unsigned long long) SAVING_INTERVALL.value);
1377 saving_task = GNUNET_SCHEDULER_add_delayed (sched,
1379 &hostlist_saving_task,
1384 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1385 _("Learning is not enabled on this peer\n"));
1387 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1392 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
1394 result = remove (filename);
1396 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1397 _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),filename);
1399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1400 _("Hostlist file `%s' could not be removed\n"),filename);
1403 GNUNET_free ( filename );
1405 GNUNET_STATISTICS_get (stats,
1407 gettext_noop("# seconds between hostlist downloads"),
1408 GNUNET_TIME_UNIT_MINUTES,
1417 * Stop downloading hostlists from hostlist servers as necessary.
1420 GNUNET_HOSTLIST_client_stop ()
1422 #if DEBUG_HOSTLIST_CLIENT
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Hostlist client shutdown\n");
1426 if ( GNUNET_YES == learning )
1427 save_hostlist_file ( GNUNET_YES );
1429 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1431 GNUNET_SCHEDULER_cancel (sched,
1433 curl_global_cleanup ();
1435 if (transport != NULL)
1437 GNUNET_TRANSPORT_disconnect (transport);
1440 GNUNET_assert (NULL == transport);
1441 GNUNET_free_non_null (proxy);
1447 /* end of hostlist-client.c */