2 This file is part of GNUnet
3 (C) 2002--2012 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 3, 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 transport/plugin_transport_http_client.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
27 #include "plugin_transport_http.h"
29 static struct Plugin * p;
33 * Function to log curl debug messages with GNUNET_log
35 * @param type curl_infotype
42 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
44 if (type == CURLINFO_TEXT)
48 memcpy (text, data, size);
49 if (text[size - 1] == '\n')
54 text[size + 1] = '\0';
57 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https",
58 "Client: %p - %s", cls, text);
60 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http",
61 "Client: %p - %s", cls, text);
69 * Task performing curl operations
70 * @param cls plugin as closure
71 * @param tc gnunet scheduler task context
74 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
78 * Function setting up file descriptors and scheduling task to run
80 * @param plugin plugin as closure
81 * @param now schedule task in 1ms, regardless of what curl may say
82 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
85 client_schedule (struct Plugin *plugin, int now)
91 struct GNUNET_NETWORK_FDSet *grs;
92 struct GNUNET_NETWORK_FDSet *gws;
95 struct GNUNET_TIME_Relative timeout;
97 /* Cancel previous scheduled task */
98 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
100 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
101 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
107 mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
108 if (mret != CURLM_OK)
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
111 "curl_multi_fdset", __FILE__, __LINE__,
112 curl_multi_strerror (mret));
113 return GNUNET_SYSERR;
115 mret = curl_multi_timeout (plugin->client_mh, &to);
117 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
119 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
120 if (now == GNUNET_YES)
121 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
123 if (mret != CURLM_OK)
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
126 "curl_multi_timeout", __FILE__, __LINE__,
127 curl_multi_strerror (mret));
128 return GNUNET_SYSERR;
131 grs = GNUNET_NETWORK_fdset_create ();
132 gws = GNUNET_NETWORK_fdset_create ();
133 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
134 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
136 plugin->client_perform_task =
137 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
139 &client_run, plugin);
140 GNUNET_NETWORK_fdset_destroy (gws);
141 GNUNET_NETWORK_fdset_destroy (grs);
147 client_send (struct Session *s, struct HTTP_Message *msg)
149 GNUNET_assert (s != NULL);
150 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
152 if (GNUNET_YES != exist_session(p, s))
155 return GNUNET_SYSERR;
157 if (s->client_put_paused == GNUNET_YES)
159 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
160 "Client: %p was suspended, unpausing\n", s->client_put);
161 s->client_put_paused = GNUNET_NO;
162 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
164 client_schedule (s->plugin, GNUNET_YES);
171 * Task performing curl operations
173 * @param cls plugin as closure
174 * @param tc gnunet scheduler task context
177 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
179 struct Plugin *plugin = cls;
183 GNUNET_assert (cls != NULL);
185 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
186 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
192 mret = curl_multi_perform (plugin->client_mh, &running);
197 while ((msg = curl_multi_info_read (plugin->client_mh, &msgs_left)))
199 CURL *easy_h = msg->easy_handle;
200 struct Session *s = NULL;
201 char *d = (char *) s;
206 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
207 "Client: connection to ended with reason %i: `%s', %i handles running\n",
209 curl_easy_strerror (msg->data.result), running);
213 GNUNET_assert (CURLE_OK ==
214 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
215 s = (struct Session *) d;
217 if (GNUNET_YES != exist_session(plugin, s))
223 GNUNET_assert (s != NULL);
224 if (msg->msg == CURLMSG_DONE)
226 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
227 "Client: %p connection to '%s' %s ended with reason %i: `%s'\n",
228 msg->easy_handle, GNUNET_i2s (&s->target),
229 http_plugin_address_to_string (NULL, s->addr,
232 curl_easy_strerror (msg->data.result));
234 /* Disconnect other transmission direction and tell transport */
235 client_disconnect (s);
239 while (mret == CURLM_CALL_MULTI_PERFORM);
240 client_schedule (plugin, GNUNET_NO);
245 client_disconnect (struct Session *s)
249 struct Plugin *plugin = s->plugin;
250 struct HTTP_Message *msg;
251 struct HTTP_Message *t;
253 if (GNUNET_YES != exist_session(plugin, s))
256 return GNUNET_SYSERR;
259 if (s->client_put != NULL)
261 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
262 "Client: %p / %p Deleting outbound PUT session to peer `%s'\n",
263 s, s->client_put, GNUNET_i2s (&s->target));
265 /* remove curl handle from multi handle */
266 mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
267 if (mret != CURLM_OK)
269 /* clean up easy handle, handle is now invalid and free'd */
273 curl_easy_cleanup (s->client_put);
274 s->client_put = NULL;
278 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
280 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
281 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
284 if (s->client_get != NULL)
286 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
287 "Client: %p / %p Deleting outbound GET session to peer `%s'\n",
289 s->client_get, GNUNET_i2s (&s->target));
291 /* remove curl handle from multi handle */
292 mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
293 if (mret != CURLM_OK)
295 /* clean up easy handle, handle is now invalid and free'd */
299 curl_easy_cleanup (s->client_get);
300 s->client_get = NULL;
307 if (NULL != msg->transmit_cont)
308 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
309 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
314 plugin->cur_connections -= 2;
316 notify_session_end (plugin, &s->target, s);
318 GNUNET_assert (plugin->outbound_sessions > 0);
319 plugin->outbound_sessions --;
320 GNUNET_STATISTICS_set (plugin->env->stats,
321 "# HTTP outbound sessions",
322 plugin->outbound_sessions,
325 /* Re-schedule since handles have changed */
326 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
328 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
329 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
331 client_schedule (plugin, GNUNET_YES);
337 client_receive_mst_cb (void *cls, void *client,
338 const struct GNUNET_MessageHeader *message)
340 struct Session *s = cls;
341 struct GNUNET_TIME_Relative delay;
343 if (GNUNET_YES != exist_session(p, s))
349 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
351 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
353 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
355 struct Plugin *plugin = s->plugin;
357 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
358 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
359 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
367 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
369 struct Session *s = cls;
371 if (GNUNET_YES != exist_session(p, s))
376 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
377 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
379 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
380 "Client: %p Waking up receive handle\n", s->client_get);
381 if (s->client_get != NULL)
382 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
387 * Callback method used with libcurl
388 * Method is called when libcurl needs to write data during sending
390 * @param stream pointer where to write data
391 * @param size size of an individual element
392 * @param nmemb count of elements that can be written to the buffer
393 * @param cls destination pointer, passed to the libcurl handle
394 * @return bytes read from stream
397 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
399 struct Session *s = cls;
400 struct GNUNET_TIME_Absolute now;
401 size_t len = size * nmemb;
402 struct Plugin *plugin = s->plugin;
404 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
405 "Client: Received %u bytes from peer `%s'\n", len,
406 GNUNET_i2s (&s->target));
407 now = GNUNET_TIME_absolute_get ();
408 if (now.abs_value < s->next_receive.abs_value)
410 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
411 struct GNUNET_TIME_Relative delta =
412 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
414 "Client: %p No inbound bandwidth available! Next read was delayed for %llu ms\n",
415 s->client_get, delta.rel_value);
416 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
418 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
419 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
421 s->recv_wakeup_task =
422 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
423 return CURLPAUSE_ALL;
425 if (NULL == s->msg_tk)
426 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
427 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
433 * Callback method used with libcurl
434 * Method is called when libcurl needs to read data during sending
436 * @param stream pointer where to write data
437 * @param size size of an individual element
438 * @param nmemb count of elements that can be written to the buffer
439 * @param cls source pointer, passed to the libcurl handle
440 * @return bytes written to stream, returning 0 will terminate connection!
443 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
445 struct Session *s = cls;
446 struct Plugin *plugin = s->plugin;
447 struct HTTP_Message *msg = s->msg_head;
450 if (GNUNET_YES != exist_session(plugin, s))
457 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
458 "Client: %p Nothing to send! Suspending PUT handle!\n",
460 s->client_put_paused = GNUNET_YES;
461 return CURL_READFUNC_PAUSE;
464 GNUNET_assert (msg->pos < msg->size);
465 /* calculate how much fits in buffer */
466 len = GNUNET_MIN (msg->size - msg->pos,
468 memcpy (stream, &msg->buf[msg->pos], len);
470 if (msg->pos == msg->size)
472 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
473 "Client: %p Message with %u bytes sent, removing message from queue\n",
474 s->client_put, msg->size, msg->pos);
475 /* Calling transmit continuation */
476 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
477 if (NULL != msg->transmit_cont)
478 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
486 client_connect (struct Session *s)
488 struct Plugin *plugin = s->plugin;
493 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
494 "Initiating outbound session peer `%s'\n",
495 GNUNET_i2s (&s->target));
496 s->inbound = GNUNET_NO;
499 GNUNET_asprintf (&url, "%s%s;%u",
500 http_plugin_address_to_string (plugin, s->addr, s->addrlen),
501 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
504 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "URL `%s'\n", url);
506 /* create get connection */
507 s->client_get = curl_easy_init ();
509 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
510 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
511 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
514 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
515 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
516 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
518 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
519 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
520 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
521 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
522 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
523 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
524 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
525 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
526 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
527 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
528 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
529 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
530 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
531 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
533 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
536 /* create put connection */
537 s->client_put = curl_easy_init ();
539 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
540 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
541 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
544 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
545 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
546 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
548 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
549 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
550 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
551 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
552 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
553 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
554 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
555 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
556 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
557 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
558 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
559 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
560 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
561 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
562 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
564 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
569 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
570 if (mret != CURLM_OK)
572 curl_easy_cleanup (s->client_get);
577 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
578 if (mret != CURLM_OK)
580 curl_multi_remove_handle (plugin->client_mh, s->client_get);
581 curl_easy_cleanup (s->client_get);
582 curl_easy_cleanup (s->client_put);
587 /* Perform connect */
588 plugin->cur_connections += 2;
590 plugin->outbound_sessions ++;
591 GNUNET_STATISTICS_set (plugin->env->stats,
592 "# HTTP outbound sessions",
593 plugin->outbound_sessions,
596 /* Re-schedule since handles have changed */
597 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
599 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
600 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
602 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
609 client_start (struct Plugin *plugin)
614 curl_global_init (CURL_GLOBAL_ALL);
615 plugin->client_mh = curl_multi_init ();
617 if (NULL == plugin->client_mh)
619 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
621 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
630 client_stop (struct Plugin *plugin)
633 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
635 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
636 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
639 curl_multi_cleanup (plugin->client_mh);
640 curl_global_cleanup ();
645 /* end of plugin_transport_http_client.c */