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
28 #include "hostlist-client.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_transport_service.h"
32 #include <curl/curl.h>
34 #define DEBUG_HOSTLIST_CLIENT GNUNET_NO
37 * Number of connections that we must have to NOT download
40 #define MIN_CONNECTIONS 4
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50 static struct GNUNET_SCHEDULER_Handle *sched;
55 struct GNUNET_STATISTICS_Handle *stats;
60 struct GNUNET_TRANSPORT_Handle *transport;
63 * Proxy that we are using (can be NULL).
68 * Buffer for data downloaded via HTTP.
70 static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
73 * Number of bytes valid in 'download_buffer'.
75 static size_t download_pos;
78 * Current URL that we are using.
80 static char *current_url;
83 * Current CURL handle.
88 * Current multi-CURL handle.
93 * ID of the current task scheduled.
95 static GNUNET_SCHEDULER_TaskIdentifier current_task;
98 * Amount of time we wait between hostlist downloads.
100 static struct GNUNET_TIME_Relative hostlist_delay;
103 * Set to GNUNET_YES if the current URL had some problems.
105 static int bogus_url;
108 * Number of active connections (according to core service).
110 static unsigned int connection_count;
113 * At what time MUST the current hostlist request be done?
115 static struct GNUNET_TIME_Absolute end_time;
119 * Process downloaded bits by calling callback on each HELLO.
121 * @param ptr buffer with downloaded data
122 * @param size size of a record
123 * @param nmemb number of records downloaded
125 * @return number of bytes that were processed (always size*nmemb)
128 download_hostlist_processor (void *ptr,
133 const char * cbuf = ptr;
134 const struct GNUNET_MessageHeader *msg;
140 total = size * nmemb;
141 if ( (total == 0) || (bogus_url) )
143 return total; /* ok, no data or bogus data */
148 cpy = GNUNET_MIN (total, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
149 GNUNET_assert (cpy > 0);
150 memcpy (&download_buffer[download_pos],
156 if (download_pos < sizeof(struct GNUNET_MessageHeader))
158 msg = (const struct GNUNET_MessageHeader *) download_buffer;
159 msize = ntohs(msg->size);
160 if (msize < sizeof(struct GNUNET_MessageHeader))
162 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
163 _("Invalid `%s' message received from hostlist at `%s'\n"),
169 if (download_pos < msize)
171 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
173 #if DEBUG_HOSTLIST_CLIENT
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Received valid `%s' message from hostlist server.\n",
178 GNUNET_TRANSPORT_offer_hello (transport, msg);
182 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
183 _("Invalid `%s' message received from hostlist at `%s'\n"),
189 memmove (download_buffer,
190 &download_buffer[msize],
191 download_pos - msize);
192 download_pos -= msize;
199 * Obtain a hostlist URL that we should use.
201 * @return NULL if there is no URL available
212 GNUNET_CONFIGURATION_get_value_string (cfg,
217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
218 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
219 "SERVERS", "HOSTLIST");
224 if (strlen (servers) > 0)
227 pos = strlen (servers) - 1;
230 if (servers[pos] == ' ')
237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
238 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
239 "SERVERS", "HOSTLIST");
240 GNUNET_free (servers);
244 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
245 pos = strlen (servers) - 1;
248 if (servers[pos] == ' ')
260 ret = GNUNET_strdup (&servers[pos]);
261 GNUNET_free (servers);
266 #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);
270 * Schedule the background task that will (possibly)
271 * download a hostlist.
274 schedule_hostlist_task (void);
278 * Clean up the state from the task that downloaded the
279 * hostlist and schedule the next task.
288 mret = curl_multi_remove_handle (multi, curl);
289 if (mret != CURLM_OK)
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 _("%s failed at %s:%d: `%s'\n"),
293 "curl_multi_remove_handle", __FILE__, __LINE__,
294 curl_multi_strerror (mret));
296 mret = curl_multi_cleanup (multi);
297 if (mret != CURLM_OK)
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 _("%s failed at %s:%d: `%s'\n"),
300 "curl_multi_cleanup", __FILE__, __LINE__,
301 curl_multi_strerror (mret));
306 curl_easy_cleanup (curl);
309 GNUNET_free_non_null (current_url);
311 schedule_hostlist_task ();
316 * Ask CURL for the select set and then schedule the
317 * receiving task with the scheduler.
324 * Task that is run when we are ready to receive more data from the hostlist
327 * @param cls closure, unused
328 * @param tc task context, unused
331 multi_ready (void *cls,
332 const struct GNUNET_SCHEDULER_TaskContext *tc)
338 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
340 #if DEBUG_HOSTLIST_CLIENT
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Shutdown requested while trying to download hostlist from `%s'\n",
348 if (GNUNET_TIME_absolute_get_remaining (end_time).value == 0)
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
351 _("Timeout trying to download hostlist from `%s'\n"),
356 #if DEBUG_HOSTLIST_CLIENT
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Ready for processing hostlist client request\n");
363 mret = curl_multi_perform (multi, &running);
368 msg = curl_multi_info_read (multi, &running);
369 GNUNET_break (msg != NULL);
375 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
376 _("Download of hostlist `%s' completed.\n"),
378 if ( (msg->data.result != CURLE_OK) &&
379 (msg->data.result != CURLE_GOT_NOTHING) )
380 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
381 _("%s failed at %s:%d: `%s'\n"),
382 "curl_multi_perform", __FILE__,
384 curl_easy_strerror (msg->data.result));
394 while (mret == CURLM_CALL_MULTI_PERFORM);
395 if (mret != CURLM_OK)
397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
398 _("%s failed at %s:%d: `%s'\n"),
399 "curl_multi_perform", __FILE__, __LINE__,
400 curl_multi_strerror (mret));
408 * Ask CURL for the select set and then schedule the
409 * receiving task with the scheduler.
419 struct GNUNET_NETWORK_FDSet *grs;
420 struct GNUNET_NETWORK_FDSet *gws;
422 struct GNUNET_TIME_Relative rtime;
428 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
429 if (mret != CURLM_OK)
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
432 _("%s failed at %s:%d: `%s'\n"),
433 "curl_multi_fdset", __FILE__, __LINE__,
434 curl_multi_strerror (mret));
438 mret = curl_multi_timeout (multi, &timeout);
439 if (mret != CURLM_OK)
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 _("%s failed at %s:%d: `%s'\n"),
443 "curl_multi_timeout", __FILE__, __LINE__,
444 curl_multi_strerror (mret));
448 rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
449 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
451 grs = GNUNET_NETWORK_fdset_create ();
452 gws = GNUNET_NETWORK_fdset_create ();
453 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
454 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
455 #if DEBUG_HOSTLIST_CLIENT
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Scheduling task for hostlist download using cURL\n");
460 = GNUNET_SCHEDULER_add_select (sched,
461 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
462 GNUNET_SCHEDULER_NO_TASK,
468 GNUNET_NETWORK_fdset_destroy (gws);
469 GNUNET_NETWORK_fdset_destroy (grs);
474 * Main function that will download a hostlist and process its
483 curl = curl_easy_init ();
491 current_url = get_url ();
492 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
493 _("Bootstrapping using hostlist at `%s'.\n"),
497 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
500 CURL_EASY_SETOPT (curl,
501 CURLOPT_WRITEFUNCTION,
502 &download_hostlist_processor);
508 CURL_EASY_SETOPT (curl,
516 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
517 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
518 /* no need to abort if the above failed */
519 CURL_EASY_SETOPT (curl,
527 CURL_EASY_SETOPT (curl,
531 CURL_EASY_SETOPT (curl,
535 CURL_EASY_SETOPT (curl,
537 GNUNET_SERVER_MAX_MESSAGE_SIZE);
538 if (0 == strncmp (current_url, "http", 4))
539 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
540 CURL_EASY_SETOPT (curl,
541 CURLOPT_CONNECTTIMEOUT,
543 CURL_EASY_SETOPT (curl,
547 /* this should no longer be needed; we're now single-threaded! */
548 CURL_EASY_SETOPT (curl,
552 multi = curl_multi_init ();
559 mret = curl_multi_add_handle (multi, curl);
560 if (mret != CURLM_OK)
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
563 _("%s failed at %s:%d: `%s'\n"),
564 "curl_multi_add_handle", __FILE__, __LINE__,
565 curl_multi_strerror (mret));
566 mret = curl_multi_cleanup (multi);
567 if (mret != CURLM_OK)
568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
569 _("%s failed at %s:%d: `%s'\n"),
570 "curl_multi_cleanup", __FILE__, __LINE__,
571 curl_multi_strerror (mret));
576 end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
582 * Task that checks if we should try to download a hostlist.
583 * If so, we initiate the download, otherwise we schedule
584 * this task again for a later time.
587 check_task (void *cls,
588 const struct GNUNET_SCHEDULER_TaskContext *tc)
590 current_task = GNUNET_SCHEDULER_NO_TASK;
591 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
593 if (connection_count < MIN_CONNECTIONS)
594 download_hostlist ();
596 schedule_hostlist_task ();
601 * Compute when we should check the next time about downloading
602 * a hostlist; then schedule the task accordingly.
605 schedule_hostlist_task ()
607 struct GNUNET_TIME_Relative delay;
611 curl_global_cleanup ();
612 return; /* in shutdown */
614 delay = hostlist_delay;
615 if (hostlist_delay.value == 0)
616 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
618 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
619 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * (1 + connection_count))
620 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
621 (1 + connection_count));
622 GNUNET_STATISTICS_set (stats,
623 gettext_noop("Minimum time between hostlist downloads"),
624 hostlist_delay.value,
626 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
627 _("Will consider downloading hostlist in %llums\n"),
628 (unsigned long long) delay.value);
629 current_task = GNUNET_SCHEDULER_add_delayed (sched,
637 * Method called whenever a given peer connects.
640 * @param peer peer identity this notification is about
641 * @param latency reported latency of the connection with 'other'
642 * @param distance reported distance (DV) to 'other'
645 connect_handler (void *cls,
647 GNUNET_PeerIdentity * peer,
648 struct GNUNET_TIME_Relative latency,
656 * Method called whenever a given peer connects.
659 * @param peer peer identity this notification is about
662 disconnect_handler (void *cls,
664 GNUNET_PeerIdentity * peer)
671 * Continuation called by the statistics code once
672 * we go the stat. Initiates hostlist download scheduling.
675 * @param success GNUNET_OK if statistics were
676 * successfully obtained, GNUNET_SYSERR if not.
679 primary_task (void *cls, int success)
682 return; /* in shutdown */
683 #if DEBUG_HOSTLIST_CLIENT
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Statistics request done, scheduling hostlist download\n");
687 schedule_hostlist_task ();
692 process_stat (void *cls,
693 const char *subsystem,
698 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
699 _("Initial time between hostlist downloads is %llums\n"),
700 (unsigned long long) value);
701 hostlist_delay.value = value;
707 * Start downloading hostlists from hostlist servers as necessary.
710 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
711 struct GNUNET_SCHEDULER_Handle *s,
712 struct GNUNET_STATISTICS_Handle *st,
713 GNUNET_CORE_ConnectEventHandler *ch,
714 GNUNET_CORE_DisconnectEventHandler *dh)
716 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
719 return GNUNET_SYSERR;
721 transport = GNUNET_TRANSPORT_connect (s, c, NULL, NULL, NULL, NULL);
722 if (NULL == transport)
724 curl_global_cleanup ();
725 return GNUNET_SYSERR;
731 GNUNET_CONFIGURATION_get_value_string (cfg,
736 *ch = &connect_handler;
737 *dh = &disconnect_handler;
738 GNUNET_STATISTICS_get (stats,
740 gettext_noop("Minimum time between hostlist downloads"),
741 GNUNET_TIME_UNIT_MINUTES,
750 * Stop downloading hostlists from hostlist servers as necessary.
753 GNUNET_HOSTLIST_client_stop ()
755 #if DEBUG_HOSTLIST_CLIENT
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Hostlist client shutdown\n");
759 if (current_task != GNUNET_SCHEDULER_NO_TASK)
761 GNUNET_SCHEDULER_cancel (sched,
763 curl_global_cleanup ();
765 if (transport != NULL)
767 GNUNET_TRANSPORT_disconnect (transport);
770 GNUNET_assert (NULL == transport);
771 GNUNET_free_non_null (proxy);
778 /* end of hostlist-client.c */