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"
31 * Function to log curl debug messages with GNUNET_log
33 * @param type curl_infotype
40 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
42 if (type == CURLINFO_TEXT)
46 memcpy (text, data, size);
47 if (text[size - 1] == '\n')
52 text[size + 1] = '\0';
55 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https", "Client: %X - %s", cls, text);
57 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http", "Client: %X - %s", cls, text);
65 * Task performing curl operations
66 * @param cls plugin as closure
67 * @param tc gnunet scheduler task context
70 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
73 * Function setting up file descriptors and scheduling task to run
75 * @param plugin plugin as closure
76 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
79 client_schedule (struct Plugin *plugin)
85 struct GNUNET_NETWORK_FDSet *grs;
86 struct GNUNET_NETWORK_FDSet *gws;
89 struct GNUNET_TIME_Relative timeout;
91 /* Cancel previous scheduled task */
92 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
94 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
95 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
102 mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
103 if (mret != CURLM_OK)
105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
106 "curl_multi_fdset", __FILE__, __LINE__,
107 curl_multi_strerror (mret));
108 return GNUNET_SYSERR;
110 mret = curl_multi_timeout (plugin->client_mh, &to);
112 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
114 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
115 if (mret != CURLM_OK)
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
118 "curl_multi_timeout", __FILE__, __LINE__,
119 curl_multi_strerror (mret));
120 return GNUNET_SYSERR;
123 grs = GNUNET_NETWORK_fdset_create ();
124 gws = GNUNET_NETWORK_fdset_create ();
125 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
126 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
128 plugin->client_perform_task =
129 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
130 GNUNET_SCHEDULER_NO_TASK,
136 GNUNET_NETWORK_fdset_destroy (gws);
137 GNUNET_NETWORK_fdset_destroy (grs);
143 client_send (struct Session *s, struct HTTP_Message *msg)
145 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
147 if ((s != NULL) && (s->client_put_paused == GNUNET_YES))
150 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name, "Client: %X was suspended, unpausing\n", s->client_put);
152 s->client_put_paused = GNUNET_NO;
153 curl_easy_pause(s->client_put, CURLPAUSE_CONT);
156 client_schedule (s->plugin);
164 * Task performing curl operations
165 * @param cls plugin as closure
166 * @param tc gnunet scheduler task context
169 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 struct Plugin *plugin = cls;
175 GNUNET_assert (cls != NULL);
177 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
178 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
184 mret = curl_multi_perform (plugin->client_mh, &running);
188 while ((msg = curl_multi_info_read(plugin->client_mh, &msgs_left)))
190 CURL *easy_h = msg->easy_handle;
191 struct Session *s = NULL;
192 char * d = (char *) s;
193 GNUNET_assert (easy_h != NULL);
195 GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &d));
196 s = (struct Session *) d;
197 GNUNET_assert (s != NULL);
199 if (msg->msg == CURLMSG_DONE)
202 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
203 "Client: %X connection to '%s' %s ended\n", msg->easy_handle, GNUNET_i2s(&s->target), GNUNET_a2s (s->addr, s->addrlen));
205 client_disconnect(s);
206 //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));
207 notify_session_end (plugin, &s->target, s);
211 while (mret == CURLM_CALL_MULTI_PERFORM);
212 client_schedule (plugin);
216 client_disconnect (struct Session *s)
220 struct Plugin *plugin = s->plugin;
221 struct HTTP_Message * msg;
222 struct HTTP_Message * t;
226 if (s->client_put != NULL)
229 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
230 "Client: %X Deleting outbound PUT session to peer `%s'\n",
232 GNUNET_i2s (&s->target));
235 mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
236 if (mret != CURLM_OK)
238 curl_easy_cleanup (s->client_put);
242 curl_easy_cleanup (s->client_put);
243 s->client_put = NULL;
247 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
249 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
250 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
253 if (s->client_get != NULL)
256 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
257 "Client: %X Deleting outbound GET session to peer `%s'\n",
259 GNUNET_i2s (&s->target));
262 mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
263 if (mret != CURLM_OK)
265 curl_easy_cleanup (s->client_get);
269 curl_easy_cleanup (s->client_get);
270 s->client_get = NULL;
277 if (NULL != msg->transmit_cont)
278 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
279 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
284 plugin->cur_connections -= 2;
285 /* Re-schedule since handles have changed */
286 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
288 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
289 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
292 plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
298 client_receive_mst_cb (void *cls, void *client,
299 const struct GNUNET_MessageHeader *message)
301 struct Session *s = cls;
302 //struct Plugin *plugin = s->plugin;
303 struct GNUNET_TIME_Relative delay;
305 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
306 s->next_receive = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
308 if (GNUNET_TIME_absolute_get().abs_value < s->next_receive.abs_value)
311 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
312 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
317 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
319 struct Session *s = cls;
321 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
323 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
326 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
327 "Client: %X Waking up receive handle\n",
330 if (s->client_get != NULL)
331 curl_easy_pause(s->client_get, CURLPAUSE_CONT);
336 * Callback method used with libcurl
337 * Method is called when libcurl needs to write data during sending
338 * @param stream pointer where to write data
339 * @param size size of an individual element
340 * @param nmemb count of elements that can be written to the buffer
341 * @param ptr destination pointer, passed to the libcurl handle
342 * @return bytes read from stream
345 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
347 struct Session *s = cls;
348 struct GNUNET_TIME_Absolute now;
349 size_t len = size * nmemb;
353 struct Plugin *plugin = s->plugin;
354 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: Received %Zu bytes from peer `%s'\n",
356 GNUNET_i2s (&s->target));
359 now = GNUNET_TIME_absolute_get();
360 if (now.abs_value < s->next_receive.abs_value)
362 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
363 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(now, s->next_receive);
365 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
366 "Client: %X No inbound bandwidth available! Next read was delayed for %llu ms\n",
367 s->client_get, delta.rel_value);
369 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
371 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
372 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
374 s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
375 return CURLPAUSE_ALL;
379 if (s->msg_tk == NULL)
380 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
382 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO,
386 client_wake_up(NULL,NULL);
390 * Callback method used with libcurl
391 * Method is called when libcurl needs to read 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 ptr source pointer, passed to the libcurl handle
396 * @return bytes written to stream, returning 0 will terminate connection!
399 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
401 struct Session *s = cls;
403 struct Plugin *plugin = s->plugin;
405 size_t bytes_sent = 0;
408 struct HTTP_Message *msg = s->msg_head;
413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: %X Nothing to send! Suspending PUT handle!\n", s->client_put);
415 s->client_put_paused = GNUNET_YES;
416 return CURL_READFUNC_PAUSE;
419 GNUNET_assert (msg != NULL);
421 if (msg->pos < msg->size)
423 /* data fit in buffer */
424 if ((msg->size - msg->pos) <= (size * nmemb))
426 len = (msg->size - msg->pos);
427 memcpy (stream, &msg->buf[msg->pos], len);
434 memcpy (stream, &msg->buf[msg->pos], len);
439 /* no data to send */
446 if (msg->pos == msg->size)
449 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
450 "Client: %X Message with %u bytes sent, removing message from queue\n",
451 s->client_put, msg->size, msg->pos);
453 /* Calling transmit continuation */
454 if (NULL != msg->transmit_cont)
455 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
456 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
463 client_connect (struct Session *s)
465 struct Plugin *plugin = s->plugin;
471 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
472 "Initiating outbound session peer `%s'\n",
473 GNUNET_i2s (&s->target));
476 s->inbound = GNUNET_NO;
480 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);
482 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
486 /* create get connection */
487 s->client_get = curl_easy_init ();
489 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
490 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
491 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
494 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
495 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
496 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
498 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
499 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
500 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
501 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
502 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
503 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
504 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
505 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
506 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
507 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
508 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
509 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
510 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
511 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
513 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
516 /* create put connection */
517 s->client_put = curl_easy_init ();
519 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
520 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
521 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
524 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
525 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
526 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
528 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
529 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
530 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
531 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
532 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
533 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
534 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
535 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
536 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
537 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
538 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
539 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
540 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
541 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
542 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
544 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
549 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
550 if (mret != CURLM_OK)
552 curl_easy_cleanup (s->client_get);
557 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
558 if (mret != CURLM_OK)
560 curl_multi_remove_handle (plugin->client_mh, s->client_get);
561 curl_easy_cleanup (s->client_get);
562 curl_easy_cleanup (s->client_put);
567 /* Perform connect */
568 plugin->cur_connections += 2;
570 /* Re-schedule since handles have changed */
571 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
573 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
574 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
576 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
582 client_start (struct Plugin *plugin)
586 curl_global_init (CURL_GLOBAL_ALL);
587 plugin->client_mh = curl_multi_init ();
589 if (NULL == plugin->client_mh)
591 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
593 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
601 client_stop (struct Plugin *plugin)
603 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
605 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
606 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
609 curl_multi_cleanup (plugin->client_mh);
610 curl_global_cleanup ();
615 /* end of plugin_transport_http_client.c */