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';
54 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client: %X - %s", cls, text);
61 client_send (struct Session *s, struct HTTP_Message *msg)
63 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
68 * Task performing curl operations
69 * @param cls plugin as closure
70 * @param tc gnunet scheduler task context
73 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
76 * Function setting up file descriptors and scheduling task to run
78 * @param plugin plugin as closure
79 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
82 client_schedule (struct Plugin *plugin)
88 struct GNUNET_NETWORK_FDSet *grs;
89 struct GNUNET_NETWORK_FDSet *gws;
92 struct GNUNET_TIME_Relative timeout;
94 /* Cancel previous scheduled task */
95 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
97 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
98 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
105 mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
106 if (mret != CURLM_OK)
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;
113 mret = curl_multi_timeout (plugin->client_mh, &to);
115 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
117 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
118 if (mret != CURLM_OK)
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;
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);
131 plugin->client_perform_task =
132 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
133 GNUNET_SCHEDULER_NO_TASK,
139 GNUNET_NETWORK_fdset_destroy (gws);
140 GNUNET_NETWORK_fdset_destroy (grs);
146 * Task performing curl operations
147 * @param cls plugin as closure
148 * @param tc gnunet scheduler task context
151 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
153 struct Plugin *plugin = cls;
154 static unsigned int handles_last_run;
158 GNUNET_assert (cls != NULL);
160 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
161 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
167 mret = curl_multi_perform (plugin->client_mh, &running);
171 while ((msg = curl_multi_info_read(plugin->client_mh, &msgs_left)))
173 CURL *easy_h = msg->easy_handle;
175 GNUNET_assert (easy_h != NULL);
177 GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &s));
178 GNUNET_assert (s != NULL);
180 if (msg->msg == CURLMSG_DONE)
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));
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);
194 handles_last_run = running;
196 while (mret == CURLM_CALL_MULTI_PERFORM);
197 client_schedule (plugin);
201 client_disconnect (struct Session *s)
205 struct Plugin *plugin = s->plugin;
206 struct HTTP_Message * msg;
207 struct HTTP_Message * t;
210 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
211 "Deleting outbound PUT session to peer `%s'\n",
212 GNUNET_i2s (&s->target));
215 if (s->client_put != NULL)
217 mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
218 if (mret != CURLM_OK)
220 curl_easy_cleanup (s->client_put);
224 curl_easy_cleanup (s->client_put);
225 s->client_put = NULL;
229 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
230 "Deleting outbound GET session to peer `%s'\n",
231 GNUNET_i2s (&s->target));
234 if (s->client_get != NULL)
236 mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
237 if (mret != CURLM_OK)
239 curl_easy_cleanup (s->client_get);
243 curl_easy_cleanup (s->client_get);
244 s->client_get = NULL;
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);
258 plugin->cur_connections -= 2;
259 /* Re-schedule since handles have changed */
260 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
262 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
263 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
266 plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
272 client_receive_mst_cb (void *cls, void *client,
273 const struct GNUNET_MessageHeader *message)
275 struct Session *s = cls;
276 struct Plugin *plugin = s->plugin;
277 struct GNUNET_TIME_Relative delay;
279 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
281 s->delay = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
283 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
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);
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
302 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
304 struct Session *s = cls;
305 struct Plugin *plugin = s->plugin;
307 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
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);
317 if (s->msg_tk == NULL)
318 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
320 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, size * nmemb, GNUNET_NO,
324 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Received %u bytes from peer `%s'\n",
326 GNUNET_i2s (&s->target));
328 return (size * nmemb);
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
341 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
343 struct Session *s = cls;
344 //struct Plugin *plugin = s->plugin;
345 size_t bytes_sent = 0;
348 struct HTTP_Message *msg = s->msg_head;
350 if (s->put_paused == GNUNET_NO)
351 return CURL_READFUNC_PAUSE;
352 if ((s->msg_head == NULL) && (s->put_paused == GNUNET_YES))
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));
358 s->put_paused = GNUNET_NO;
359 return CURL_READFUNC_PAUSE;
364 GNUNET_assert (msg != NULL);
366 if (msg->pos < msg->size)
368 /* data fit in buffer */
369 if ((msg->size - msg->pos) <= (size * nmemb))
371 len = (msg->size - msg->pos);
372 memcpy (stream, &msg->buf[msg->pos], len);
379 memcpy (stream, &msg->buf[msg->pos], len);
384 /* no data to send */
390 if (msg->pos == msg->size)
392 #if DEBUG_CONNECTIONS
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Connection %X: Message with %u bytes sent, removing message from queue\n",
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);
407 client_connect (struct Session *s)
409 struct Plugin *plugin = s->plugin;
415 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
416 "Initiating outbound session peer `%s'\n",
417 GNUNET_i2s (&s->target));
420 s->inbound = GNUNET_NO;
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);
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
430 /* create get connection */
431 s->client_get = curl_easy_init ();
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);
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);
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);
457 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
460 /* create put connection */
461 s->client_put = curl_easy_init ();
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);
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);
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);
488 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
493 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
494 if (mret != CURLM_OK)
496 curl_easy_cleanup (s->client_get);
501 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
502 if (mret != CURLM_OK)
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);
511 /* Perform connect */
512 plugin->cur_connections += 2;
514 /* Re-schedule since handles have changed */
515 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
517 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
518 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
520 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
526 client_start (struct Plugin *plugin)
530 curl_global_init (CURL_GLOBAL_ALL);
531 plugin->client_mh = curl_multi_init ();
533 if (NULL == plugin->client_mh)
535 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
537 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
545 client_stop (struct Plugin *plugin)
547 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
549 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
550 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
553 curl_multi_cleanup (plugin->client_mh);
554 curl_global_cleanup ();
559 /* end of plugin_transport_http_client.c */