3b3a4705b34bc033fc425b21dade8a321dc26519
[oweals/gnunet.git] / src / transport / plugin_transport_http_client.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 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 3, 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 transport/plugin_transport_http_client.c
23  * @brief http transport service plugin
24  * @author Matthias Wachs
25  */
26
27 #include "plugin_transport_http.h"
28
29 #if VERBOSE_CURL
30 /**
31  * Function to log curl debug messages with GNUNET_log
32  * @param curl handle
33  * @param type curl_infotype
34  * @param data data
35  * @param size size
36  * @param cls  closure
37  * @return 0
38  */
39 static int
40 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
41 {
42   if (type == CURLINFO_TEXT)
43   {
44     char text[size + 2];
45
46     memcpy (text, data, size);
47     if (text[size - 1] == '\n')
48       text[size] = '\0';
49     else
50     {
51       text[size] = '\n';
52       text[size + 1] = '\0';
53     }
54     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client: %X - %s", cls, text);
55   }
56   return 0;
57 }
58 #endif
59
60 int
61 client_send (struct Session *s, struct HTTP_Message *msg)
62 {
63   GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
64   return GNUNET_OK;
65 }
66
67 /**
68  * Task performing curl operations
69  * @param cls plugin as closure
70  * @param tc gnunet scheduler task context
71  */
72 static void
73 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
74
75 /**
76  * Function setting up file descriptors and scheduling task to run
77  *
78  * @param  plugin plugin as closure
79  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
80  */
81 static int
82 client_schedule (struct Plugin *plugin)
83 {
84   fd_set rs;
85   fd_set ws;
86   fd_set es;
87   int max;
88   struct GNUNET_NETWORK_FDSet *grs;
89   struct GNUNET_NETWORK_FDSet *gws;
90   long to;
91   CURLMcode mret;
92   struct GNUNET_TIME_Relative timeout;
93
94   /* Cancel previous scheduled task */
95   if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
96   {
97     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
98     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
99   }
100
101   max = -1;
102   FD_ZERO (&rs);
103   FD_ZERO (&ws);
104   FD_ZERO (&es);
105   mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
106   if (mret != CURLM_OK)
107   {
108     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
109                 "curl_multi_fdset", __FILE__, __LINE__,
110                 curl_multi_strerror (mret));
111     return GNUNET_SYSERR;
112   }
113   mret = curl_multi_timeout (plugin->client_mh, &to);
114   if (to == -1)
115     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
116   else
117     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
118   if (mret != CURLM_OK)
119   {
120     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
121                 "curl_multi_timeout", __FILE__, __LINE__,
122                 curl_multi_strerror (mret));
123     return GNUNET_SYSERR;
124   }
125
126   grs = GNUNET_NETWORK_fdset_create ();
127   gws = GNUNET_NETWORK_fdset_create ();
128   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
129   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
130
131   plugin->client_perform_task =
132       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
133                                    GNUNET_SCHEDULER_NO_TASK,
134                                    timeout,
135                                    grs,
136                                    gws,
137                                    &client_run,
138                                    plugin);
139   GNUNET_NETWORK_fdset_destroy (gws);
140   GNUNET_NETWORK_fdset_destroy (grs);
141   return GNUNET_OK;
142 }
143
144
145 /**
146  * Task performing curl operations
147  * @param cls plugin as closure
148  * @param tc gnunet scheduler task context
149  */
150 static void
151 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
152 {
153   struct Plugin *plugin = cls;
154   static unsigned int handles_last_run;
155   int running;
156   CURLMcode mret;
157
158   GNUNET_assert (cls != NULL);
159
160   plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
161   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
162     return;
163
164   do
165   {
166     running = 0;
167     mret = curl_multi_perform (plugin->client_mh, &running);
168
169     CURLMsg * msg;
170     int msgs_left;
171     while ((msg = curl_multi_info_read(plugin->client_mh, &msgs_left)))
172     {
173        CURL *easy_h  = msg->easy_handle;
174        struct Session *s;
175        GNUNET_assert (easy_h != NULL);
176
177        GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &s));
178        GNUNET_assert (s != NULL);
179
180        if (msg->msg == CURLMSG_DONE)
181        {
182 #if DEBUG_HTTP
183          GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
184                    "Connection to '%s'  %s ended\n", GNUNET_i2s(&s->target), http_plugin_address_to_string(plugin, s->addr, s->addrlen));
185 #endif
186          client_disconnect(s);
187          //GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,"Notifying about ended session to peer `%s' `%s'\n", GNUNET_i2s (&s->target), http_plugin_address_to_string (plugin, s->addr, s->addrlen));
188          if (s->msg_tk != NULL)
189            GNUNET_SERVER_mst_destroy (s->msg_tk);
190          notify_session_end (plugin, &s->target, s);
191        }
192     }
193
194     handles_last_run = running;
195   }
196   while (mret == CURLM_CALL_MULTI_PERFORM);
197   client_schedule (plugin);
198 }
199
200 int
201 client_disconnect (struct Session *s)
202 {
203   int res = GNUNET_OK;
204   CURLMcode mret;
205   struct Plugin *plugin = s->plugin;
206   struct HTTP_Message * msg;
207   struct HTTP_Message * t;
208
209 #if 0
210   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
211                    "Deleting outbound PUT session to peer `%s'\n",
212                    GNUNET_i2s (&s->target));
213 #endif
214
215   if (s->client_put != NULL)
216   {
217     mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
218     if (mret != CURLM_OK)
219     {
220       curl_easy_cleanup (s->client_put);
221       res = GNUNET_SYSERR;
222       GNUNET_break (0);
223     }
224     curl_easy_cleanup (s->client_put);
225     s->client_put = NULL;
226   }
227
228 #if DEBUG_HTTP
229   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
230                    "Deleting outbound GET session to peer `%s'\n",
231                    GNUNET_i2s (&s->target));
232 #endif
233
234   if (s->client_get != NULL)
235   {
236     mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
237     if (mret != CURLM_OK)
238     {
239       curl_easy_cleanup (s->client_get);
240       res = GNUNET_SYSERR;
241       GNUNET_break (0);
242     }
243     curl_easy_cleanup (s->client_get);
244     s->client_get = NULL;
245   }
246
247   msg = s->msg_head;
248   while (msg != NULL)
249   {
250     t = msg->next;
251     if (NULL != msg->transmit_cont)
252       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
253     GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
254     GNUNET_free (msg);
255     msg = t;
256   }
257
258   plugin->cur_connections -= 2;
259   /* Re-schedule since handles have changed */
260   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
261   {
262     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
263     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
264   }
265
266   plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
267
268   return res;
269 }
270
271 static void
272 client_receive_mst_cb (void *cls, void *client,
273                      const struct GNUNET_MessageHeader *message)
274 {
275   struct Session *s = cls;
276   struct Plugin *plugin = s->plugin;
277   struct GNUNET_TIME_Relative delay;
278
279   delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
280
281   s->delay = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
282
283   if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
284   {
285 #if VERBOSE_CLIENT
286     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
287                 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
288 #endif
289   }
290 }
291
292 /**
293 * Callback method used with libcurl
294 * Method is called when libcurl needs to write data during sending
295 * @param stream pointer where to write data
296 * @param size size of an individual element
297 * @param nmemb count of elements that can be written to the buffer
298 * @param ptr destination pointer, passed to the libcurl handle
299 * @return bytes read from stream
300 */
301 static size_t
302 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
303 {
304   struct Session *s = cls;
305   struct Plugin *plugin = s->plugin;
306
307   if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
308   {
309 #if DEBUG_CLIENT
310     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311                 "no inbound bandwidth available! Next read was delayed for  %llu ms\n",
312                 s, GNUNET_TIME_absolute_get_difference(s->delay, GNUNET_TIME_absolute_get()).rel_value);
313 #endif
314     return 0;
315   }
316
317   if (s->msg_tk == NULL)
318       s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
319
320   GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, size * nmemb, GNUNET_NO,
321                              GNUNET_NO);
322
323 #if VERBOSE_CLIENT
324   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Received %u bytes from peer `%s'\n",
325                    size * nmemb,
326                    GNUNET_i2s (&s->target));
327 #endif
328   return (size * nmemb);
329 }
330
331 /**
332  * Callback method used with libcurl
333  * Method is called when libcurl needs to read data during sending
334  * @param stream pointer where to write data
335  * @param size size of an individual element
336  * @param nmemb count of elements that can be written to the buffer
337  * @param ptr source pointer, passed to the libcurl handle
338  * @return bytes written to stream
339  */
340 static size_t
341 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
342 {
343   struct Session *s = cls;
344   //struct Plugin *plugin = s->plugin;
345   size_t bytes_sent = 0;
346   size_t len;
347
348   struct HTTP_Message *msg = s->msg_head;
349 /*
350   if (s->put_paused == GNUNET_NO)
351     return CURL_READFUNC_PAUSE;
352   if ((s->msg_head == NULL) && (s->put_paused == GNUNET_YES))
353   {
354 #if VERBOSE_CLIENT
355     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Suspending handle `%s' `%s'\n",
356                      GNUNET_i2s (&s->target),GNUNET_a2s (s->addr, s->addrlen));
357 #endif
358     s->put_paused = GNUNET_NO;
359     return CURL_READFUNC_PAUSE;
360   }
361 */
362   if (msg == NULL)
363     return bytes_sent;
364   GNUNET_assert (msg != NULL);
365   /* data to send */
366   if (msg->pos < msg->size)
367   {
368     /* data fit in buffer */
369     if ((msg->size - msg->pos) <= (size * nmemb))
370     {
371       len = (msg->size - msg->pos);
372       memcpy (stream, &msg->buf[msg->pos], len);
373       msg->pos += len;
374       bytes_sent = len;
375     }
376     else
377     {
378       len = size * nmemb;
379       memcpy (stream, &msg->buf[msg->pos], len);
380       msg->pos += len;
381       bytes_sent = len;
382     }
383   }
384   /* no data to send */
385   else
386   {
387     bytes_sent = 0;
388   }
389
390   if (msg->pos == msg->size)
391   {
392 #if DEBUG_CONNECTIONS
393     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394                 "Connection %X: Message with %u bytes sent, removing message from queue\n",
395                 s, msg->pos);
396 #endif
397     /* Calling transmit continuation  */
398     if (NULL != msg->transmit_cont)
399       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
400     GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
401     GNUNET_free (msg);
402   }
403   return bytes_sent;
404 }
405
406 int
407 client_connect (struct Session *s)
408 {
409   struct Plugin *plugin = s->plugin;
410   int res = GNUNET_OK;
411   char *url;
412   CURLMcode mret;
413
414 #if VERBOSE_CLIENT
415   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
416                    "Initiating outbound session peer `%s'\n",
417                    GNUNET_i2s (&s->target));
418 #endif
419
420   s->inbound = GNUNET_NO;
421
422   plugin->last_tag++;
423   /* create url */
424   GNUNET_asprintf (&url, "%s%s;%u", http_plugin_address_to_string (plugin, s->addr, s->addrlen), GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),plugin->last_tag);
425 #if 0
426   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
427                    "URL `%s'\n",
428                    url);
429 #endif
430   /* create get connection */
431   s->client_get = curl_easy_init ();
432 #if VERBOSE_CURL
433   curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
434   curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
435   curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
436 #endif
437 #if BUILD_HTTPS
438   curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
439   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
440   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
441 #endif
442   curl_easy_setopt (s->client_get, CURLOPT_URL, url);
443   //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
444   //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
445   curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
446   curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
447   curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
448   curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
449   curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
450                     (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
451   curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
452   curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
453                     (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
454   curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
455                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
456 #if CURL_TCP_NODELAY
457   curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
458 #endif
459
460   /* create put connection */
461   s->client_put = curl_easy_init ();
462 #if VERBOSE_CURL
463   curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
464   curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
465   curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
466 #endif
467 #if BUILD_HTTPS
468   curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
469   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
470   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
471 #endif
472   curl_easy_setopt (s->client_put, CURLOPT_URL, url);
473   curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
474   //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
475   //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
476   curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
477   curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
478   curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
479   curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
480   curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
481                     (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
482   curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
483   curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
484                     (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
485   curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
486                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
487 #if CURL_TCP_NODELAY
488   curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
489 #endif
490
491   GNUNET_free (url);
492
493   mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
494   if (mret != CURLM_OK)
495   {
496     curl_easy_cleanup (s->client_get);
497     res = GNUNET_SYSERR;
498     GNUNET_break (0);
499   }
500
501   mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
502   if (mret != CURLM_OK)
503   {
504     curl_multi_remove_handle (plugin->client_mh, s->client_get);
505     curl_easy_cleanup (s->client_get);
506     curl_easy_cleanup (s->client_put);
507     res = GNUNET_SYSERR;
508     GNUNET_break (0);
509   }
510
511   /* Perform connect */
512   plugin->cur_connections += 2;
513
514   /* Re-schedule since handles have changed */
515   if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
516   {
517     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
518     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
519   }
520   plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
521
522   return res;
523 }
524
525 int
526 client_start (struct Plugin *plugin)
527 {
528   int res = GNUNET_OK;
529
530   curl_global_init (CURL_GLOBAL_ALL);
531   plugin->client_mh = curl_multi_init ();
532
533   if (NULL == plugin->client_mh)
534   {
535     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
536                      _
537                      ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
538                      plugin->name);
539     res = GNUNET_SYSERR;
540   }
541   return res;
542 }
543
544 void
545 client_stop (struct Plugin *plugin)
546 {
547   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
548   {
549     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
550     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
551   }
552
553   curl_multi_cleanup (plugin->client_mh);
554   curl_global_cleanup ();
555 }
556
557
558
559 /* end of plugin_transport_http_client.c */