2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 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>
35 * Number of connections that we must have to NOT download
38 #define MIN_CONNECTIONS 4
43 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48 static struct GNUNET_SCHEDULER_Handle *sched;
53 struct GNUNET_STATISTICS_Handle *stats;
58 struct GNUNET_TRANSPORT_Handle *transport;
61 * Proxy that we are using (can be NULL).
66 * Buffer for data downloaded via HTTP.
68 static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
71 * Number of bytes valid in 'download_buffer'.
73 static size_t download_pos;
76 * Current URL that we are using.
78 static char *current_url;
81 * Current CURL handle.
86 * Current multi-CURL handle.
91 * ID of the current task scheduled.
93 static GNUNET_SCHEDULER_TaskIdentifier current_task;
96 * Amount of time we wait between hostlist downloads.
98 static struct GNUNET_TIME_Relative hostlist_delay;
101 * Set to GNUNET_YES if the current URL had some problems.
103 static int bogus_url;
106 * Number of active connections (according to core service).
108 static unsigned int connection_count;
112 * Process downloaded bits by calling callback on each HELLO.
114 * @param ptr buffer with downloaded data
115 * @param size size of a record
116 * @param nmemb number of records downloaded
118 * @return number of bytes that were processed (always size*nmemb)
121 download_hostlist_processor (void *ptr,
126 const char * cbuf = ptr;
127 const struct GNUNET_MessageHeader *msg;
133 total = size * nmemb;
134 if ( (total == 0) || (bogus_url) )
135 return total; /* ok, no data or bogus data */
139 cpy = GNUNET_MIN (total, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
140 GNUNET_assert (cpy > 0);
141 memcpy (&download_buffer[download_pos],
147 if (download_pos < sizeof(struct GNUNET_MessageHeader))
149 msg = (const struct GNUNET_MessageHeader *) download_buffer;
150 msize = ntohs(msg->size);
151 if (msize < sizeof(struct GNUNET_MessageHeader))
153 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
154 _("Invalid `%s' message received from hostlist at `%s'\n"),
160 if (download_pos < msize)
162 if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
164 GNUNET_TRANSPORT_offer_hello (transport, msg);
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169 _("Invalid `%s' message received from hostlist at `%s'\n"),
175 memmove (download_buffer,
176 &download_buffer[msize],
177 download_pos - msize);
178 download_pos -= msize;
185 * Obtain a hostlist URL that we should use.
187 * @return NULL if there is no URL available
198 GNUNET_CONFIGURATION_get_value_string (cfg,
203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
204 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
205 "SERVERS", "HOSTLIST");
210 if (strlen (servers) > 0)
213 pos = strlen (servers) - 1;
216 if (servers[pos] == ' ')
223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
224 _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
225 "SERVERS", "HOSTLIST");
226 GNUNET_free (servers);
230 urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
231 pos = strlen (servers) - 1;
234 if (servers[pos] == ' ')
246 ret = GNUNET_strdup (&servers[pos]);
247 GNUNET_free (servers);
252 #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);
256 * Schedule the background task that will (possibly)
257 * download a hostlist.
260 schedule_hostlist_task (void);
264 * Clean up the state from the task that downloaded the
265 * hostlist and schedule the next task.
274 mret = curl_multi_remove_handle (multi, curl);
275 if (mret != CURLM_OK)
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
278 _("%s failed at %s:%d: `%s'\n"),
279 "curl_multi_remove_handle", __FILE__, __LINE__,
280 curl_multi_strerror (mret));
282 mret = curl_multi_cleanup (multi);
283 if (mret != CURLM_OK)
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
285 _("%s failed at %s:%d: `%s'\n"),
286 "curl_multi_cleanup", __FILE__, __LINE__,
287 curl_multi_strerror (mret));
292 curl_easy_cleanup (curl);
295 if (transport != NULL)
297 GNUNET_TRANSPORT_disconnect (transport);
300 GNUNET_free_non_null (current_url);
302 schedule_hostlist_task ();
307 * Task that is run when we are ready to receive more data from the hostlist
310 * @param cls closure, unused
311 * @param tc task context, unused
314 multi_ready (void *cls,
315 const struct GNUNET_SCHEDULER_TaskContext *tc)
321 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
324 _("Timeout trying to download hostlist from `%s'\n"),
332 mret = curl_multi_perform (multi, &running);
337 msg = curl_multi_info_read (multi, &running);
338 GNUNET_break (msg != NULL);
344 if (msg->data.result != CURLE_OK)
345 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
346 _("%s failed at %s:%d: `%s'\n"),
347 "curl_multi_perform", __FILE__,
349 curl_easy_strerror (msg->data.result));
358 while (mret == CURLM_CALL_MULTI_PERFORM);
359 if (mret != CURLM_OK)
361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
362 _("%s failed at %s:%d: `%s'\n"),
363 "curl_multi_perform", __FILE__, __LINE__,
364 curl_multi_strerror (mret));
373 * Ask CURL for the select set and then schedule the
374 * receiving task with the scheduler.
384 struct GNUNET_NETWORK_FDSet *grs;
385 struct GNUNET_NETWORK_FDSet *gws;
386 struct GNUNET_NETWORK_FDSet *ges;
392 grs = GNUNET_NETWORK_fdset_create ();
393 gws = GNUNET_NETWORK_fdset_create ();
394 ges = GNUNET_NETWORK_fdset_create ();
395 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
396 if (mret != CURLM_OK)
398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
399 _("%s failed at %s:%d: `%s'\n"),
400 "curl_multi_fdset", __FILE__, __LINE__,
401 curl_multi_strerror (mret));
405 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max);
406 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max);
407 GNUNET_NETWORK_fdset_copy_native (ges, &es, max);
409 = GNUNET_SCHEDULER_add_select (sched,
411 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
412 GNUNET_SCHEDULER_NO_TASK,
413 GNUNET_TIME_UNIT_MINUTES,
418 GNUNET_NETWORK_fdset_destroy (ges);
419 GNUNET_NETWORK_fdset_destroy (gws);
420 GNUNET_NETWORK_fdset_destroy (grs);
425 * Main function that will download a hostlist and process its
434 curl = curl_easy_init ();
442 transport = GNUNET_TRANSPORT_connect (sched, cfg, NULL, NULL, NULL, NULL);
443 current_url = get_url ();
444 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
445 _("Bootstrapping using hostlist at `%s'.\n"),
449 CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
452 CURL_EASY_SETOPT (curl,
453 CURLOPT_WRITEFUNCTION,
454 &download_hostlist_processor);
460 CURL_EASY_SETOPT (curl,
468 CURL_EASY_SETOPT (curl,
476 CURL_EASY_SETOPT (curl,
479 CURL_EASY_SETOPT (curl,
481 GNUNET_SERVER_MAX_MESSAGE_SIZE);
482 if (0 == strncmp (current_url, "http", 4))
483 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
484 CURL_EASY_SETOPT (curl,
485 CURLOPT_CONNECTTIMEOUT,
487 CURL_EASY_SETOPT (curl,
490 multi = curl_multi_init ();
497 mret = curl_multi_add_handle (multi, curl);
498 if (mret != CURLM_OK)
500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
501 _("%s failed at %s:%d: `%s'\n"),
502 "curl_multi_add_handle", __FILE__, __LINE__,
503 curl_multi_strerror (mret));
504 mret = curl_multi_cleanup (multi);
505 if (mret != CURLM_OK)
506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
507 _("%s failed at %s:%d: `%s'\n"),
508 "curl_multi_cleanup", __FILE__, __LINE__,
509 curl_multi_strerror (mret));
519 * Task that checks if we should try to download a hostlist.
520 * If so, we initiate the download, otherwise we schedule
521 * this task again for a later time.
524 check_task (void *cls,
525 const struct GNUNET_SCHEDULER_TaskContext *tc)
527 if (connection_count < MIN_CONNECTIONS)
528 download_hostlist ();
530 schedule_hostlist_task ();
535 * Compute when we should check the next time about downloading
536 * a hostlist; then schedule the task accordingly.
539 schedule_hostlist_task ()
541 struct GNUNET_TIME_Relative delay;
543 delay = hostlist_delay;
544 if (hostlist_delay.value == 0)
545 hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
547 hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
548 if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * connection_count)
549 hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS,
551 GNUNET_STATISTICS_set (stats,
552 gettext_noop("Minimum time between hostlist downloads"),
553 hostlist_delay.value,
555 current_task = GNUNET_SCHEDULER_add_delayed (sched,
557 GNUNET_SCHEDULER_PRIORITY_IDLE,
558 GNUNET_SCHEDULER_NO_TASK,
566 * Method called whenever a given peer connects.
569 * @param peer peer identity this notification is about
572 connect_handler (void *cls,
574 GNUNET_PeerIdentity * peer)
581 * Method called whenever a given peer connects.
584 * @param peer peer identity this notification is about
587 disconnect_handler (void *cls,
589 GNUNET_PeerIdentity * peer)
596 * Continuation called by the statistics code once
597 * we go the stat. Initiates hostlist download scheduling.
600 * @param success GNUNET_OK if statistics were
601 * successfully obtained, GNUNET_SYSERR if not.
604 primary_task (void *cls, int success)
606 schedule_hostlist_task ();
611 process_stat (void *cls,
612 const char *subsystem,
614 unsigned long long value,
617 hostlist_delay.value = (uint64_t) value;
623 * Start downloading hostlists from hostlist servers as necessary.
626 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
627 struct GNUNET_SCHEDULER_Handle *s,
628 struct GNUNET_STATISTICS_Handle *st,
629 GNUNET_CORE_ClientEventHandler *ch,
630 GNUNET_CORE_ClientEventHandler *dh)
632 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
635 return GNUNET_SYSERR;
641 GNUNET_CONFIGURATION_get_value_string (cfg,
646 *ch = &connect_handler;
647 *dh = &disconnect_handler;
648 GNUNET_STATISTICS_get (stats,
650 gettext_noop("Minimum time between hostlist downloads"),
651 GNUNET_TIME_UNIT_MINUTES,
660 * Stop downloading hostlists from hostlist servers as necessary.
663 GNUNET_HOSTLIST_client_stop ()
665 GNUNET_SCHEDULER_cancel (sched,
667 GNUNET_free_non_null (proxy);
670 curl_global_cleanup ();
675 /* end of hostlist-client.c */