9a3a31dfef4ab2ca485b7b735db912e52706e78b
[oweals/gnunet.git] / src / hostlist / hostlist-client.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file hostlist/hostlist-client.c
23  * @brief hostlist support.  Downloads HELLOs via HTTP.
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
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>
33
34 /**
35  * Number of connections that we must have to NOT download
36  * hostlists anymore.
37  */
38 #define MIN_CONNECTIONS 4
39
40 /**
41  * Our configuration.
42  */
43 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 /**
46  * Our scheduler.
47  */
48 static struct GNUNET_SCHEDULER_Handle *sched;
49
50 /**
51  * Statistics handle.
52  */
53 struct GNUNET_STATISTICS_Handle *stats; 
54
55 /**
56  * Transport handle.
57  */
58 struct GNUNET_TRANSPORT_Handle *transport;
59                        
60 /**
61  * Proxy that we are using (can be NULL).
62  */
63 static char *proxy;
64
65 /**
66  * Buffer for data downloaded via HTTP.
67  */
68 static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
69
70 /**
71  * Number of bytes valid in 'download_buffer'.
72  */
73 static size_t download_pos;
74
75 /**
76  * Current URL that we are using.
77  */
78 static char *current_url;
79
80 /**
81  * Current CURL handle.
82  */
83 static CURL *curl;
84
85 /**
86  * Current multi-CURL handle.
87  */
88 static CURLM *multi;
89
90 /**
91  * ID of the current task scheduled.
92  */
93 static GNUNET_SCHEDULER_TaskIdentifier current_task;
94
95 /**
96  * Amount of time we wait between hostlist downloads.
97  */
98 static struct GNUNET_TIME_Relative hostlist_delay;
99
100 /**
101  * Set to GNUNET_YES if the current URL had some problems.
102  */ 
103 static int bogus_url;
104
105 /**
106  * Number of active connections (according to core service).
107  */
108 static unsigned int connection_count;
109
110
111 /**
112  * Process downloaded bits by calling callback on each HELLO.
113  *
114  * @param ptr buffer with downloaded data
115  * @param size size of a record
116  * @param nmemb number of records downloaded
117  * @param ctx unused
118  * @return number of bytes that were processed (always size*nmemb)
119  */
120 static size_t
121 download_hostlist_processor (void *ptr, 
122                              size_t size, 
123                              size_t nmemb, 
124                              void *ctx)
125 {
126   const char * cbuf = ptr;
127   const struct GNUNET_MessageHeader *msg;
128   size_t total;
129   size_t cpy;
130   size_t left;
131   uint16_t msize;
132
133   total = size * nmemb;
134   if ( (total == 0) || (bogus_url) )
135     return total;  /* ok, no data or bogus data */
136   left = total;
137   while (left > 0)
138     {
139       cpy = GNUNET_MIN (total, GNUNET_SERVER_MAX_MESSAGE_SIZE - download_pos);
140       GNUNET_assert (cpy > 0);
141       memcpy (&download_buffer[download_pos],
142               cbuf,
143               cpy);
144       cbuf += cpy;
145       download_pos += cpy;
146       left -= cpy;
147       if (download_pos < sizeof(struct GNUNET_MessageHeader))
148         break;
149       msg = (const struct GNUNET_MessageHeader *) download_buffer;
150       msize = ntohs(msg->size);
151       if (msize < sizeof(struct GNUNET_MessageHeader))
152         {        
153           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
154                       _("Invalid `%s' message received from hostlist at `%s'\n"),
155                       "HELLO",
156                       current_url); 
157           bogus_url = 1;
158           return total;
159         }
160       if (download_pos < msize)
161         break;
162       if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*)msg) == msize)
163         {
164           GNUNET_TRANSPORT_offer_hello (transport, msg);
165         }
166       else
167         {
168           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169                       _("Invalid `%s' message received from hostlist at `%s'\n"),
170                       "HELLO",
171                       current_url);
172           bogus_url = 1;
173           return total;
174         }
175       memmove (download_buffer,
176                &download_buffer[msize],
177                download_pos - msize);
178       download_pos -= msize;
179     }
180   return total;
181 }
182
183
184 /**
185  * Obtain a hostlist URL that we should use.
186  *
187  * @return NULL if there is no URL available
188  */
189 static char *
190 get_url ()
191 {
192   char *servers;
193   char *ret;
194   size_t urls;
195   size_t pos;
196
197   if (GNUNET_OK != 
198       GNUNET_CONFIGURATION_get_value_string (cfg,
199                                              "HOSTLIST",
200                                              "SERVERS",
201                                              &servers))
202     {
203       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
204                   _("No `%s' specified in `%s' configuration, will not bootstrap.\n"),
205                   "SERVERS", "HOSTLIST");
206       return NULL;
207     }
208
209   urls = 0;
210   if (strlen (servers) > 0)
211     {
212       urls++;
213       pos = strlen (servers) - 1;
214       while (pos > 0)
215         {
216           if (servers[pos] == ' ')
217             urls++;
218           pos--;
219         }
220     }
221   if (urls == 0)
222     {
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);
227       return NULL;
228     }
229
230   urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
231   pos = strlen (servers) - 1;
232   while (pos > 0)
233     {
234       if (servers[pos] == ' ')
235         {
236           urls--;
237           servers[pos] = '\0';
238         }
239       if (urls == 0)
240         {
241           pos++;
242           break;
243         }
244       pos--;    
245     }
246   ret = GNUNET_strdup (&servers[pos]);
247   GNUNET_free (servers);
248   return ret;
249 }
250
251
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);
253
254
255 /**
256  * Schedule the background task that will (possibly)
257  * download a hostlist.
258  */
259 static void
260 schedule_hostlist_task (void);
261
262
263 /**
264  * Clean up the state from the task that downloaded the
265  * hostlist and schedule the next task.
266  */
267 static void 
268 clean_up ()
269 {
270   CURLMcode mret;
271
272   if (multi != NULL)
273     {
274       mret = curl_multi_remove_handle (multi, curl);
275       if (mret != CURLM_OK)
276         {
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));
281         }
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));
288       multi = NULL;
289     }
290   if (curl != NULL)
291     {
292       curl_easy_cleanup (curl);
293       curl = NULL;
294     }  
295   if (transport != NULL)
296     {
297       GNUNET_TRANSPORT_disconnect (transport);
298       transport = NULL;
299     }
300   GNUNET_free_non_null (current_url);
301   current_url = NULL;
302   schedule_hostlist_task ();
303 }
304
305
306 /**
307  * Task that is run when we are ready to receive more data from the hostlist
308  * server. 
309  *
310  * @param cls closure, unused
311  * @param tc task context, unused
312  */
313 static void
314 multi_ready (void *cls,
315              const struct GNUNET_SCHEDULER_TaskContext *tc)
316 {
317   int running;
318   struct CURLMsg *msg;
319   CURLMcode mret;
320
321   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
322     {
323       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
324                   _("Timeout trying to download hostlist from `%s'\n"),
325                   current_url);
326       clean_up ();
327       return;
328     }
329   do 
330     {
331       running = 0;
332       mret = curl_multi_perform (multi, &running);
333       if (running == 0)
334         {
335           do
336             {
337               msg = curl_multi_info_read (multi, &running);
338               GNUNET_break (msg != NULL);
339               if (msg == NULL)
340                 break;
341               switch (msg->msg)
342                 {
343                 case CURLMSG_DONE:
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__,
348                                __LINE__,
349                                curl_easy_strerror (msg->data.result));
350                   break;
351                 default:
352                   break;
353                 }
354             }
355           while (running > 0);
356         }
357     }
358   while (mret == CURLM_CALL_MULTI_PERFORM);
359   if (mret != CURLM_OK)
360     {
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));
365       clean_up ();
366       return;
367     }
368   clean_up ();
369 }
370
371
372 /**
373  * Ask CURL for the select set and then schedule the
374  * receiving task with the scheduler.
375  */
376 static void
377 run_multi () 
378 {
379   CURLMcode mret;
380   fd_set rs;
381   fd_set ws;
382   fd_set es;
383   int max;
384   struct GNUNET_NETWORK_FDSet *grs;
385   struct GNUNET_NETWORK_FDSet *gws;
386   
387   max = 0;
388   FD_ZERO (&rs);
389   FD_ZERO (&ws);
390   FD_ZERO (&es);
391   mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
392   if (mret != CURLM_OK)
393     {
394       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
395                   _("%s failed at %s:%d: `%s'\n"),
396                   "curl_multi_fdset", __FILE__, __LINE__,
397                   curl_multi_strerror (mret));
398       clean_up ();
399       return;
400     }
401   grs = GNUNET_NETWORK_fdset_create ();
402   gws = GNUNET_NETWORK_fdset_create ();
403   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max);
404   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max);
405   current_task 
406     = GNUNET_SCHEDULER_add_select (sched,
407                                    GNUNET_SCHEDULER_PRIORITY_DEFAULT,
408                                    GNUNET_SCHEDULER_NO_TASK,
409                                    GNUNET_TIME_UNIT_MINUTES,
410                                    grs,
411                                    gws,
412                                    &multi_ready,
413                                    multi);
414   GNUNET_NETWORK_fdset_destroy (gws);
415   GNUNET_NETWORK_fdset_destroy (grs);
416 }
417
418
419 /**
420  * Main function that will download a hostlist and process its
421  * data.
422  */
423 static void
424 download_hostlist () 
425 {
426   CURLcode ret;
427   CURLMcode mret;
428
429   curl = curl_easy_init ();
430   multi = NULL;
431   if (curl == NULL)
432     {
433       GNUNET_break (0);
434       clean_up ();
435       return;
436     }
437   transport = GNUNET_TRANSPORT_connect (sched, cfg, NULL, NULL, NULL, NULL);
438   current_url = get_url ();
439   GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
440               _("Bootstrapping using hostlist at `%s'.\n"), 
441               current_url);
442
443   if (proxy != NULL)
444     CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);    
445   download_pos = 0;
446   bogus_url = 0;
447   CURL_EASY_SETOPT (curl,
448                     CURLOPT_WRITEFUNCTION, 
449                     &download_hostlist_processor);
450   if (ret != CURLE_OK)
451     {
452       clean_up ();
453       return;
454     }
455   CURL_EASY_SETOPT (curl,
456                     CURLOPT_WRITEDATA, 
457                     NULL);
458   if (ret != CURLE_OK)
459     {
460       clean_up ();
461       return;
462     }
463   CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
464   /* no need to abort if the above failed */
465   CURL_EASY_SETOPT (curl, 
466                     CURLOPT_URL, 
467                     current_url);
468   if (ret != CURLE_OK)
469     {
470       clean_up ();
471       return;
472     }
473   CURL_EASY_SETOPT (curl, 
474                     CURLOPT_FAILONERROR, 
475                     1);
476   CURL_EASY_SETOPT (curl, 
477                     CURLOPT_BUFFERSIZE, 
478                     GNUNET_SERVER_MAX_MESSAGE_SIZE);
479   if (0 == strncmp (current_url, "http", 4))
480     CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
481   CURL_EASY_SETOPT (curl, 
482                     CURLOPT_CONNECTTIMEOUT, 
483                     150L);
484   CURL_EASY_SETOPT (curl,
485                     CURLOPT_NOSIGNAL, 
486                     1);
487   multi = curl_multi_init ();
488   if (multi == NULL)
489     {
490       GNUNET_break (0);
491       clean_up ();
492       return;
493     }
494   mret = curl_multi_add_handle (multi, curl);
495   if (mret != CURLM_OK)
496     {
497       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
498                   _("%s failed at %s:%d: `%s'\n"),
499                   "curl_multi_add_handle", __FILE__, __LINE__,
500                   curl_multi_strerror (mret));
501       mret = curl_multi_cleanup (multi);
502       if (mret != CURLM_OK)
503         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
504                     _("%s failed at %s:%d: `%s'\n"),
505                     "curl_multi_cleanup", __FILE__, __LINE__,
506                     curl_multi_strerror (mret));
507       multi = NULL;
508       clean_up ();
509       return;
510     }
511   run_multi (multi);
512 }  
513
514
515 /**
516  * Task that checks if we should try to download a hostlist.
517  * If so, we initiate the download, otherwise we schedule
518  * this task again for a later time.
519  */
520 static void
521 check_task (void *cls,
522             const struct GNUNET_SCHEDULER_TaskContext *tc)
523 {
524   if (connection_count < MIN_CONNECTIONS)
525     download_hostlist ();
526   else
527     schedule_hostlist_task ();
528 }
529
530
531 /**
532  * Compute when we should check the next time about downloading
533  * a hostlist; then schedule the task accordingly.
534  */
535 static void
536 schedule_hostlist_task ()
537 {
538   struct GNUNET_TIME_Relative delay;
539
540   delay = hostlist_delay;
541   if (hostlist_delay.value == 0)
542     hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
543   else
544     hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
545   if (hostlist_delay.value > GNUNET_TIME_UNIT_HOURS.value * connection_count)
546     hostlist_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS,
547                                                     connection_count);
548   GNUNET_STATISTICS_set (stats,
549                          gettext_noop("Minimum time between hostlist downloads"),
550                          hostlist_delay.value,
551                          GNUNET_YES);
552   current_task = GNUNET_SCHEDULER_add_delayed (sched,
553                                                delay,
554                                                &check_task,
555                                                NULL);
556 }
557
558
559 /**
560  * Method called whenever a given peer connects.
561  *
562  * @param cls closure
563  * @param peer peer identity this notification is about
564  */
565 static void
566 connect_handler (void *cls,
567                  const struct
568                  GNUNET_PeerIdentity * peer)
569 {
570   connection_count++;
571 }
572
573
574 /**
575  * Method called whenever a given peer connects.
576  *
577  * @param cls closure
578  * @param peer peer identity this notification is about
579  */
580 static void
581 disconnect_handler (void *cls,
582                     const struct
583                     GNUNET_PeerIdentity * peer)
584 {
585   connection_count--;
586 }
587
588
589 /**
590  * Continuation called by the statistics code once 
591  * we go the stat.  Initiates hostlist download scheduling.
592  *
593  * @param cls closure
594  * @param success GNUNET_OK if statistics were
595  *        successfully obtained, GNUNET_SYSERR if not.
596  */
597 static void
598 primary_task (void *cls, int success)
599 {
600   schedule_hostlist_task ();
601 }
602
603
604 static int
605 process_stat (void *cls,
606               const char *subsystem,
607               const char *name,
608               uint64_t value,
609               int is_persistent)
610 {
611   hostlist_delay.value = value;
612   return GNUNET_OK;
613 }
614
615
616 /**
617  * Start downloading hostlists from hostlist servers as necessary.
618  */
619 int
620 GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
621                               struct GNUNET_SCHEDULER_Handle *s,
622                               struct GNUNET_STATISTICS_Handle *st,
623                               GNUNET_CORE_ClientEventHandler *ch,
624                               GNUNET_CORE_ClientEventHandler *dh)
625 {
626   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
627     {
628       GNUNET_break (0);
629       return GNUNET_SYSERR;
630     }
631   cfg = c;
632   sched = s;
633   stats = st;
634   if (GNUNET_OK !=
635       GNUNET_CONFIGURATION_get_value_string (cfg,
636                                              "HOSTLIST",
637                                              "HTTP-PROXY", 
638                                              &proxy))
639     proxy = NULL;
640   *ch = &connect_handler;
641   *dh = &disconnect_handler;
642   GNUNET_STATISTICS_get (stats,
643                          "hostlist",
644                          gettext_noop("Minimum time between hostlist downloads"),
645                          GNUNET_TIME_UNIT_MINUTES,
646                          &primary_task,
647                          &process_stat,
648                          NULL);
649   return GNUNET_OK;
650 }
651
652
653 /**
654  * Stop downloading hostlists from hostlist servers as necessary.
655  */
656 void
657 GNUNET_HOSTLIST_client_stop ()
658 {
659   GNUNET_SCHEDULER_cancel (sched,
660                            current_task);
661   GNUNET_free_non_null (proxy);
662   proxy = NULL;
663   if (sched != NULL)
664     curl_global_cleanup ();
665   cfg = NULL;
666   sched = NULL;
667 }
668
669 /* end of hostlist-client.c */