2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 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 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: %X - %s", cls, text);
60 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http",
61 "Client: %X - %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);
77 * Function setting up file descriptors and scheduling task to run
79 * @param plugin plugin as closure
80 * @param now schedule task in 1ms, regardless of what curl may say
81 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
84 client_schedule (struct Plugin *plugin, int now)
90 struct GNUNET_NETWORK_FDSet *grs;
91 struct GNUNET_NETWORK_FDSet *gws;
94 struct GNUNET_TIME_Relative timeout;
96 /* Cancel previous scheduled task */
97 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
99 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
100 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 (s->msg_head, s->msg_tail, msg);
152 if (GNUNET_YES != exist_session(p, s))
155 return GNUNET_SYSERR;
158 if (s->client_put_paused == GNUNET_YES)
160 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
161 "Client: %X was suspended, unpausing\n", s->client_put);
162 s->client_put_paused = GNUNET_NO;
163 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
166 client_schedule (s->plugin, GNUNET_YES);
174 * Task performing curl operations
175 * @param cls plugin as closure
176 * @param tc gnunet scheduler task context
179 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 struct Plugin *plugin = cls;
185 GNUNET_assert (cls != NULL);
187 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
188 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
194 mret = curl_multi_perform (plugin->client_mh, &running);
199 while ((msg = curl_multi_info_read (plugin->client_mh, &msgs_left)))
201 CURL *easy_h = msg->easy_handle;
202 struct Session *s = NULL;
203 char *d = (char *) s;
206 //GNUNET_assert (easy_h != NULL);
209 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
210 "Client: connection to ended with reason %i: `%s', %i handles running\n",
212 curl_easy_strerror (msg->data.result), running);
216 GNUNET_assert (CURLE_OK ==
217 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
218 s = (struct Session *) d;
220 if (GNUNET_YES != exist_session(plugin, s))
226 GNUNET_assert (s != NULL);
228 if (msg->msg == CURLMSG_DONE)
230 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
231 "Client: %X connection to '%s' %s ended with reason %i: `%s'\n",
232 msg->easy_handle, GNUNET_i2s (&s->target),
233 http_plugin_address_to_string (NULL, s->addr,
236 curl_easy_strerror (msg->data.result));
238 client_disconnect (s);
239 //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));
240 notify_session_end (plugin, &s->target, s);
244 while (mret == CURLM_CALL_MULTI_PERFORM);
245 client_schedule (plugin, GNUNET_NO);
249 client_disconnect (struct Session *s)
253 struct Plugin *plugin = s->plugin;
254 struct HTTP_Message *msg;
255 struct HTTP_Message *t;
257 if (GNUNET_YES != exist_session(plugin, s))
260 return GNUNET_SYSERR;
263 if (s->client_put != NULL)
265 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
266 "Client: %X Deleting outbound PUT session to peer `%s'\n",
267 s->client_put, GNUNET_i2s (&s->target));
269 mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
270 if (mret != CURLM_OK)
272 curl_easy_cleanup (s->client_put);
276 curl_easy_cleanup (s->client_put);
277 s->client_put = NULL;
281 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
283 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
284 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
287 if (s->client_get != NULL)
289 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
290 "Client: %X Deleting outbound GET session to peer `%s'\n",
291 s->client_get, GNUNET_i2s (&s->target));
293 mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
294 if (mret != CURLM_OK)
296 curl_easy_cleanup (s->client_get);
300 curl_easy_cleanup (s->client_get);
301 s->client_get = NULL;
308 if (NULL != msg->transmit_cont)
309 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
310 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
315 plugin->cur_connections -= 2;
317 GNUNET_assert (plugin->outbound_sessions > 0);
318 plugin->outbound_sessions --;
319 GNUNET_STATISTICS_set (plugin->env->stats,
320 "# HTTP outbound sessions",
321 plugin->outbound_sessions,
324 /* Re-schedule since handles have changed */
325 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
327 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
328 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),
366 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
368 struct Session *s = cls;
370 if (GNUNET_YES != exist_session(p, s))
376 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
378 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
381 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
382 "Client: %X Waking up receive handle\n", s->client_get);
384 if (s->client_get != NULL)
385 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
390 * Callback method used with libcurl
391 * Method is called when libcurl needs to write data during sending
392 * @param stream pointer where to write data
393 * @param size size of an individual element
394 * @param nmemb count of elements that can be written to the buffer
395 * @param cls destination pointer, passed to the libcurl handle
396 * @return bytes read from stream
399 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
401 struct Session *s = cls;
402 struct GNUNET_TIME_Absolute now;
403 size_t len = size * nmemb;
404 struct Plugin *plugin = s->plugin;
406 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
407 "Client: Received %Zu bytes from peer `%s'\n", len,
408 GNUNET_i2s (&s->target));
409 now = GNUNET_TIME_absolute_get ();
410 if (now.abs_value < s->next_receive.abs_value)
412 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
413 struct GNUNET_TIME_Relative delta =
414 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
415 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
416 "Client: %X No inbound bandwidth available! Next read was delayed for %llu ms\n",
417 s->client_get, delta.rel_value);
418 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
420 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
421 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
423 s->recv_wakeup_task =
424 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
425 return CURLPAUSE_ALL;
429 if (s->msg_tk == NULL)
430 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
432 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
438 * Callback method used with libcurl
439 * Method is called when libcurl needs to read data during sending
440 * @param stream pointer where to write data
441 * @param size size of an individual element
442 * @param nmemb count of elements that can be written to the buffer
443 * @param cls source pointer, passed to the libcurl handle
444 * @return bytes written to stream, returning 0 will terminate connection!
447 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
449 struct Session *s = cls;
450 struct Plugin *plugin = s->plugin;
451 size_t bytes_sent = 0;
454 if (GNUNET_YES != exist_session(plugin, s))
457 return GNUNET_SYSERR;
460 struct HTTP_Message *msg = s->msg_head;
464 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
465 "Client: %X Nothing to send! Suspending PUT handle!\n",
467 s->client_put_paused = GNUNET_YES;
468 return CURL_READFUNC_PAUSE;
471 GNUNET_assert (msg != NULL);
473 if (msg->pos < msg->size)
475 /* data fit in buffer */
476 if ((msg->size - msg->pos) <= (size * nmemb))
478 len = (msg->size - msg->pos);
479 memcpy (stream, &msg->buf[msg->pos], len);
486 memcpy (stream, &msg->buf[msg->pos], len);
491 /* no data to send */
498 if (msg->pos == msg->size)
500 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
501 "Client: %X Message with %u bytes sent, removing message from queue\n",
502 s->client_put, msg->size, msg->pos);
503 /* Calling transmit continuation */
504 if (NULL != msg->transmit_cont)
505 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
506 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
513 client_connect (struct Session *s)
515 struct Plugin *plugin = s->plugin;
520 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
521 "Initiating outbound session peer `%s'\n",
522 GNUNET_i2s (&s->target));
523 s->inbound = GNUNET_NO;
526 GNUNET_asprintf (&url, "%s%s;%u",
527 http_plugin_address_to_string (plugin, s->addr, s->addrlen),
528 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
531 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "URL `%s'\n", url);
533 /* create get connection */
534 s->client_get = curl_easy_init ();
536 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
537 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
538 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
541 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
542 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
543 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
545 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
546 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
547 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
548 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
549 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
550 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
551 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
552 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
553 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
554 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
555 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
556 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
557 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
558 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
560 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
563 /* create put connection */
564 s->client_put = curl_easy_init ();
566 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
567 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
568 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
571 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
572 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
573 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
575 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
576 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
577 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
578 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
579 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
580 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
581 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
582 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
583 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
584 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
585 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
586 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
587 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
588 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
589 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
591 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
596 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
597 if (mret != CURLM_OK)
599 curl_easy_cleanup (s->client_get);
604 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
605 if (mret != CURLM_OK)
607 curl_multi_remove_handle (plugin->client_mh, s->client_get);
608 curl_easy_cleanup (s->client_get);
609 curl_easy_cleanup (s->client_put);
614 /* Perform connect */
615 plugin->cur_connections += 2;
617 plugin->outbound_sessions ++;
618 GNUNET_STATISTICS_set (plugin->env->stats,
619 "# HTTP outbound sessions",
620 plugin->outbound_sessions,
623 /* Re-schedule since handles have changed */
624 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
626 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
627 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
629 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
635 client_start (struct Plugin *plugin)
640 curl_global_init (CURL_GLOBAL_ALL);
641 plugin->client_mh = curl_multi_init ();
643 if (NULL == plugin->client_mh)
645 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
647 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
655 client_stop (struct Plugin *plugin)
658 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
660 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
661 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
664 curl_multi_cleanup (plugin->client_mh);
665 curl_global_cleanup ();
670 /* end of plugin_transport_http_client.c */