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 client_send (struct Session *s, struct HTTP_Message *msg)
67 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
69 curl_easy_pause(s->client_put, CURLPAUSE_CONT);
74 * Task performing curl operations
75 * @param cls plugin as closure
76 * @param tc gnunet scheduler task context
79 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
82 * Function setting up file descriptors and scheduling task to run
84 * @param plugin plugin as closure
85 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
88 client_schedule (struct Plugin *plugin)
94 struct GNUNET_NETWORK_FDSet *grs;
95 struct GNUNET_NETWORK_FDSet *gws;
98 struct GNUNET_TIME_Relative timeout;
100 /* Cancel previous scheduled task */
101 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
104 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
111 mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
112 if (mret != CURLM_OK)
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
115 "curl_multi_fdset", __FILE__, __LINE__,
116 curl_multi_strerror (mret));
117 return GNUNET_SYSERR;
119 mret = curl_multi_timeout (plugin->client_mh, &to);
121 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
123 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
124 if (mret != CURLM_OK)
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
127 "curl_multi_timeout", __FILE__, __LINE__,
128 curl_multi_strerror (mret));
129 return GNUNET_SYSERR;
132 grs = GNUNET_NETWORK_fdset_create ();
133 gws = GNUNET_NETWORK_fdset_create ();
134 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
135 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
137 plugin->client_perform_task =
138 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
139 GNUNET_SCHEDULER_NO_TASK,
145 GNUNET_NETWORK_fdset_destroy (gws);
146 GNUNET_NETWORK_fdset_destroy (grs);
152 * Task performing curl operations
153 * @param cls plugin as closure
154 * @param tc gnunet scheduler task context
157 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 struct Plugin *plugin = cls;
160 static unsigned int handles_last_run;
164 GNUNET_assert (cls != NULL);
166 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
167 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
173 mret = curl_multi_perform (plugin->client_mh, &running);
177 while ((msg = curl_multi_info_read(plugin->client_mh, &msgs_left)))
179 CURL *easy_h = msg->easy_handle;
180 struct Session *s = NULL;
181 char * d = (char *) s;
182 GNUNET_assert (easy_h != NULL);
184 GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &d));
185 s = (struct Session *) d;
186 GNUNET_assert (s != NULL);
188 if (msg->msg == CURLMSG_DONE)
191 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
192 "Client: %X connection to '%s' %s ended\n", s, GNUNET_i2s(&s->target), GNUNET_a2s (s->addr, s->addrlen));
194 client_disconnect(s);
195 //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));
196 if (s->msg_tk != NULL)
197 GNUNET_SERVER_mst_destroy (s->msg_tk);
198 notify_session_end (plugin, &s->target, s);
202 handles_last_run = running;
204 while (mret == CURLM_CALL_MULTI_PERFORM);
205 client_schedule (plugin);
209 client_disconnect (struct Session *s)
213 struct Plugin *plugin = s->plugin;
214 struct HTTP_Message * msg;
215 struct HTTP_Message * t;
218 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
219 "Client: %X Deleting outbound PUT session to peer `%s'\n",
221 GNUNET_i2s (&s->target));
224 if (s->client_put != NULL)
226 mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
227 if (mret != CURLM_OK)
229 curl_easy_cleanup (s->client_put);
233 curl_easy_cleanup (s->client_put);
234 s->client_put = NULL;
238 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
239 "Client: %X Deleting outbound GET session to peer `%s'\n",
241 GNUNET_i2s (&s->target));
243 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
245 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
246 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
249 if (s->client_get != NULL)
251 mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
252 if (mret != CURLM_OK)
254 curl_easy_cleanup (s->client_get);
258 curl_easy_cleanup (s->client_get);
259 s->client_get = NULL;
266 if (NULL != msg->transmit_cont)
267 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
268 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
273 plugin->cur_connections -= 2;
274 /* Re-schedule since handles have changed */
275 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
277 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
278 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
281 plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
287 client_receive_mst_cb (void *cls, void *client,
288 const struct GNUNET_MessageHeader *message)
290 struct Session *s = cls;
291 struct Plugin *plugin = s->plugin;
292 struct GNUNET_TIME_Relative delay;
294 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
296 s->delay = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
298 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
301 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
302 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
307 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
309 struct Session *s = cls;
311 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
312 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
315 if (s->client_get != NULL)
316 curl_easy_pause(s->client_get, CURLPAUSE_CONT);
320 * Callback method used with libcurl
321 * Method is called when libcurl needs to write data during sending
322 * @param stream pointer where to write data
323 * @param size size of an individual element
324 * @param nmemb count of elements that can be written to the buffer
325 * @param ptr destination pointer, passed to the libcurl handle
326 * @return bytes read from stream
329 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
331 struct Session *s = cls;
332 struct Plugin *plugin = s->plugin;
333 struct GNUNET_TIME_Absolute now;
336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: Received %Zu bytes from peer `%s'\n",
338 GNUNET_i2s (&s->target));
341 now = GNUNET_TIME_absolute_get();
342 if (now.abs_value < s->delay.abs_value)
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "No inbound bandwidth available! Next read was delayed for %llu ms\n",
347 s, GNUNET_TIME_absolute_get_difference(s->delay, GNUNET_TIME_absolute_get()).rel_value);
349 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
351 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
352 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
354 s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed( GNUNET_TIME_absolute_get_difference(s->delay, now), &client_wake_up, s);
355 return CURLPAUSE_ALL;
359 if (s->msg_tk == NULL)
360 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
362 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, size * nmemb, GNUNET_NO,
365 return (size * nmemb);
369 * Callback method used with libcurl
370 * Method is called when libcurl needs to read data during sending
371 * @param stream pointer where to write data
372 * @param size size of an individual element
373 * @param nmemb count of elements that can be written to the buffer
374 * @param ptr source pointer, passed to the libcurl handle
375 * @return bytes written to stream
378 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
380 struct Session *s = cls;
381 struct Plugin *plugin = s->plugin;
382 size_t bytes_sent = 0;
385 struct HTTP_Message *msg = s->msg_head;
387 if (s->put_paused == GNUNET_NO)
388 return CURL_READFUNC_PAUSE;
389 if ((s->msg_head == NULL) && (s->put_paused == GNUNET_YES))
392 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Suspending handle `%s' `%s'\n",
393 GNUNET_i2s (&s->target),GNUNET_a2s (s->addr, s->addrlen));
395 s->put_paused = GNUNET_NO;
396 return CURL_READFUNC_PAUSE;
401 GNUNET_assert (msg != NULL);
403 if (msg->pos < msg->size)
405 /* data fit in buffer */
406 if ((msg->size - msg->pos) <= (size * nmemb))
408 len = (msg->size - msg->pos);
409 memcpy (stream, &msg->buf[msg->pos], len);
416 memcpy (stream, &msg->buf[msg->pos], len);
421 /* no data to send */
427 if (msg->pos == msg->size)
430 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
431 "Client: %X Message with %u bytes sent, removing message from queue\n",
432 s, msg->size, msg->pos);
434 /* Calling transmit continuation */
435 if (NULL != msg->transmit_cont)
436 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
437 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
444 client_connect (struct Session *s)
446 struct Plugin *plugin = s->plugin;
452 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
453 "Initiating outbound session peer `%s'\n",
454 GNUNET_i2s (&s->target));
457 s->inbound = GNUNET_NO;
461 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);
463 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
467 /* create get connection */
468 s->client_get = curl_easy_init ();
470 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
471 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
472 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
475 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
476 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
477 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
479 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
480 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
481 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
482 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
483 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
484 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
485 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
486 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
487 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
488 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
489 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
490 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
491 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
492 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
494 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
497 /* create put connection */
498 s->client_put = curl_easy_init ();
500 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
501 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
502 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
505 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
506 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
507 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
509 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
510 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
511 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
512 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
513 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
514 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
515 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
516 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
517 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
518 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
519 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
520 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
521 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
522 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
523 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
525 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
530 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
531 if (mret != CURLM_OK)
533 curl_easy_cleanup (s->client_get);
538 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
539 if (mret != CURLM_OK)
541 curl_multi_remove_handle (plugin->client_mh, s->client_get);
542 curl_easy_cleanup (s->client_get);
543 curl_easy_cleanup (s->client_put);
548 /* Perform connect */
549 plugin->cur_connections += 2;
551 /* Re-schedule since handles have changed */
552 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
554 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
555 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
557 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
563 client_start (struct Plugin *plugin)
567 curl_global_init (CURL_GLOBAL_ALL);
568 plugin->client_mh = curl_multi_init ();
570 if (NULL == plugin->client_mh)
572 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
574 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
582 client_stop (struct Plugin *plugin)
584 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
586 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
587 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
590 curl_multi_cleanup (plugin->client_mh);
591 curl_global_cleanup ();
596 /* end of plugin_transport_http_client.c */