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))
149 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name, "Client: %X was suspended, unpausing\n", s->client_put);
150 s->client_put_paused = GNUNET_NO;
151 curl_easy_pause(s->client_put, CURLPAUSE_CONT);
154 client_schedule (s->plugin);
162 * Task performing curl operations
163 * @param cls plugin as closure
164 * @param tc gnunet scheduler task context
167 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 struct Plugin *plugin = cls;
170 static unsigned int handles_last_run;
174 GNUNET_assert (cls != NULL);
176 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
177 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
183 mret = curl_multi_perform (plugin->client_mh, &running);
187 while ((msg = curl_multi_info_read(plugin->client_mh, &msgs_left)))
189 CURL *easy_h = msg->easy_handle;
190 struct Session *s = NULL;
191 char * d = (char *) s;
192 GNUNET_assert (easy_h != NULL);
194 GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &d));
195 s = (struct Session *) d;
196 GNUNET_assert (s != NULL);
198 if (msg->msg == CURLMSG_DONE)
201 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
202 "Client: %X connection to '%s' %s ended\n", msg->easy_handle, GNUNET_i2s(&s->target), GNUNET_a2s (s->addr, s->addrlen));
204 client_disconnect(s);
205 //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));
206 if (s->msg_tk != NULL)
207 GNUNET_SERVER_mst_destroy (s->msg_tk);
208 notify_session_end (plugin, &s->target, s);
212 handles_last_run = running;
214 while (mret == CURLM_CALL_MULTI_PERFORM);
215 client_schedule (plugin);
219 client_disconnect (struct Session *s)
223 struct Plugin *plugin = s->plugin;
224 struct HTTP_Message * msg;
225 struct HTTP_Message * t;
229 if (s->client_put != NULL)
232 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
233 "Client: %X Deleting outbound PUT session to peer `%s'\n",
235 GNUNET_i2s (&s->target));
238 mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
239 if (mret != CURLM_OK)
241 curl_easy_cleanup (s->client_put);
245 curl_easy_cleanup (s->client_put);
246 s->client_put = NULL;
250 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
252 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
253 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
256 if (s->client_get != NULL)
259 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
260 "Client: %X Deleting outbound GET session to peer `%s'\n",
262 GNUNET_i2s (&s->target));
265 mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
266 if (mret != CURLM_OK)
268 curl_easy_cleanup (s->client_get);
272 curl_easy_cleanup (s->client_get);
273 s->client_get = NULL;
280 if (NULL != msg->transmit_cont)
281 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
282 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
287 plugin->cur_connections -= 2;
288 /* Re-schedule since handles have changed */
289 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
291 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
292 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
295 plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
301 client_receive_mst_cb (void *cls, void *client,
302 const struct GNUNET_MessageHeader *message)
304 struct Session *s = cls;
305 struct Plugin *plugin = s->plugin;
306 struct GNUNET_TIME_Relative delay;
308 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
310 s->delay = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
312 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
315 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
316 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
321 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
323 struct Session *s = cls;
325 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
326 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
329 if (s->client_get != NULL)
330 curl_easy_pause(s->client_get, CURLPAUSE_CONT);
334 * Callback method used with libcurl
335 * Method is called when libcurl needs to write data during sending
336 * @param stream pointer where to write data
337 * @param size size of an individual element
338 * @param nmemb count of elements that can be written to the buffer
339 * @param ptr destination pointer, passed to the libcurl handle
340 * @return bytes read from stream
343 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
345 struct Session *s = cls;
346 struct Plugin *plugin = s->plugin;
347 struct GNUNET_TIME_Absolute now;
348 size_t len = size * nmemb;
352 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: Received %Zu bytes from peer `%s'\n",
354 GNUNET_i2s (&s->target));
357 now = GNUNET_TIME_absolute_get();
358 if (now.abs_value < s->delay.abs_value)
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "No inbound bandwidth available! Next read was delayed for %llu ms\n",
364 s, GNUNET_TIME_absolute_get_difference(s->delay, GNUNET_TIME_absolute_get()).rel_value);
366 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
368 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
369 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
371 s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed( GNUNET_TIME_absolute_get_difference(s->delay, now), &client_wake_up, s);
372 return CURLPAUSE_ALL;
377 if (s->msg_tk == NULL)
378 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
380 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO,
385 client_wake_up (NULL, NULL);
389 * Callback method used with libcurl
390 * Method is called when libcurl needs to read data during sending
391 * @param stream pointer where to write data
392 * @param size size of an individual element
393 * @param nmemb count of elements that can be written to the buffer
394 * @param ptr source pointer, passed to the libcurl handle
395 * @return bytes written to stream, returning 0 will terminate connection!
398 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
400 struct Session *s = cls;
401 struct Plugin *plugin = s->plugin;
402 size_t bytes_sent = 0;
405 struct HTTP_Message *msg = s->msg_head;
407 if (s->put_paused == GNUNET_NO)
408 return CURL_READFUNC_PAUSE;
409 if ((s->msg_head == NULL) && (s->put_paused == GNUNET_YES))
412 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Suspending handle `%s' `%s'\n",
413 GNUNET_i2s (&s->target),GNUNET_a2s (s->addr, s->addrlen));
415 s->put_paused = GNUNET_NO;
416 return CURL_READFUNC_PAUSE;
421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: %X Nothing to send! Suspending PUT handle!\n", s->client_put);
422 s->client_put_paused = GNUNET_YES;
423 return CURL_READFUNC_PAUSE;
426 GNUNET_assert (msg != NULL);
428 if (msg->pos < msg->size)
430 /* data fit in buffer */
431 if ((msg->size - msg->pos) <= (size * nmemb))
433 len = (msg->size - msg->pos);
434 memcpy (stream, &msg->buf[msg->pos], len);
441 memcpy (stream, &msg->buf[msg->pos], len);
446 /* no data to send */
453 if (msg->pos == msg->size)
456 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
457 "Client: %X Message with %u bytes sent, removing message from queue\n",
458 s->client_put, msg->size, msg->pos);
460 /* Calling transmit continuation */
461 if (NULL != msg->transmit_cont)
462 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
463 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
470 client_connect (struct Session *s)
472 struct Plugin *plugin = s->plugin;
478 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
479 "Initiating outbound session peer `%s'\n",
480 GNUNET_i2s (&s->target));
483 s->inbound = GNUNET_NO;
487 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);
489 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
493 /* create get connection */
494 s->client_get = curl_easy_init ();
496 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
497 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
498 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
501 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
502 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
503 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
505 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
506 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
507 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
508 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
509 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
510 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
511 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
512 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
513 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
514 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
515 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
516 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
517 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
518 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
520 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
523 /* create put connection */
524 s->client_put = curl_easy_init ();
526 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
527 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
528 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
531 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
532 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
533 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
535 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
536 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
537 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
538 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
539 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
540 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
541 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
542 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
543 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
544 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
545 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
546 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
547 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
548 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
549 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
551 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
556 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
557 if (mret != CURLM_OK)
559 curl_easy_cleanup (s->client_get);
564 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
565 if (mret != CURLM_OK)
567 curl_multi_remove_handle (plugin->client_mh, s->client_get);
568 curl_easy_cleanup (s->client_get);
569 curl_easy_cleanup (s->client_put);
574 /* Perform connect */
575 plugin->cur_connections += 2;
577 /* Re-schedule since handles have changed */
578 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
580 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
581 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
583 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
589 client_start (struct Plugin *plugin)
593 curl_global_init (CURL_GLOBAL_ALL);
594 plugin->client_mh = curl_multi_init ();
596 if (NULL == plugin->client_mh)
598 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
600 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
608 client_stop (struct Plugin *plugin)
610 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
612 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
613 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
616 curl_multi_cleanup (plugin->client_mh);
617 curl_global_cleanup ();
622 /* end of plugin_transport_http_client.c */