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;
174 struct Session *s = NULL;
175 char * d = (char *) s;
176 GNUNET_assert (easy_h != NULL);
178 GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &d));
179 s = (struct Session *) d;
180 GNUNET_assert (s != NULL);
182 if (msg->msg == CURLMSG_DONE)
185 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
186 "Connection to '%s' %s ended\n", GNUNET_i2s(&s->target), GNUNET_a2s (s->addr, s->addrlen));
188 client_disconnect(s);
189 //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));
190 if (s->msg_tk != NULL)
191 GNUNET_SERVER_mst_destroy (s->msg_tk);
192 notify_session_end (plugin, &s->target, s);
196 handles_last_run = running;
198 while (mret == CURLM_CALL_MULTI_PERFORM);
199 client_schedule (plugin);
203 client_disconnect (struct Session *s)
207 struct Plugin *plugin = s->plugin;
208 struct HTTP_Message * msg;
209 struct HTTP_Message * t;
212 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
213 "Client: Deleting outbound PUT session to peer `%s'\n",
214 GNUNET_i2s (&s->target));
217 if (s->client_put != NULL)
219 mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
220 if (mret != CURLM_OK)
222 curl_easy_cleanup (s->client_put);
226 curl_easy_cleanup (s->client_put);
227 s->client_put = NULL;
231 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
232 "Client: Deleting outbound GET session to peer `%s'\n",
233 GNUNET_i2s (&s->target));
236 if (s->client_get != NULL)
238 mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
239 if (mret != CURLM_OK)
241 curl_easy_cleanup (s->client_get);
245 curl_easy_cleanup (s->client_get);
246 s->client_get = NULL;
253 if (NULL != msg->transmit_cont)
254 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
255 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
260 plugin->cur_connections -= 2;
261 /* Re-schedule since handles have changed */
262 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
264 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
265 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
268 plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
274 client_receive_mst_cb (void *cls, void *client,
275 const struct GNUNET_MessageHeader *message)
277 struct Session *s = cls;
278 struct Plugin *plugin = s->plugin;
279 struct GNUNET_TIME_Relative delay;
281 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
283 s->delay = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
285 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
288 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
289 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
295 * Callback method used with libcurl
296 * Method is called when libcurl needs to write data during sending
297 * @param stream pointer where to write data
298 * @param size size of an individual element
299 * @param nmemb count of elements that can be written to the buffer
300 * @param ptr destination pointer, passed to the libcurl handle
301 * @return bytes read from stream
304 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
306 struct Session *s = cls;
307 struct Plugin *plugin = s->plugin;
310 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: Received %Zu bytes from peer `%s'\n",
312 GNUNET_i2s (&s->target));
315 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "no inbound bandwidth available! Next read was delayed for %llu ms\n",
320 s, GNUNET_TIME_absolute_get_difference(s->delay, GNUNET_TIME_absolute_get()).rel_value);
326 if (s->msg_tk == NULL)
327 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
329 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, size * nmemb, GNUNET_NO,
332 return (size * nmemb);
336 * Callback method used with libcurl
337 * Method is called when libcurl needs to read 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 source pointer, passed to the libcurl handle
342 * @return bytes written to stream
345 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
347 struct Session *s = cls;
348 //struct Plugin *plugin = s->plugin;
349 size_t bytes_sent = 0;
352 struct HTTP_Message *msg = s->msg_head;
354 if (s->put_paused == GNUNET_NO)
355 return CURL_READFUNC_PAUSE;
356 if ((s->msg_head == NULL) && (s->put_paused == GNUNET_YES))
359 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Suspending handle `%s' `%s'\n",
360 GNUNET_i2s (&s->target),GNUNET_a2s (s->addr, s->addrlen));
362 s->put_paused = GNUNET_NO;
363 return CURL_READFUNC_PAUSE;
368 GNUNET_assert (msg != NULL);
370 if (msg->pos < msg->size)
372 /* data fit in buffer */
373 if ((msg->size - msg->pos) <= (size * nmemb))
375 len = (msg->size - msg->pos);
376 memcpy (stream, &msg->buf[msg->pos], len);
383 memcpy (stream, &msg->buf[msg->pos], len);
388 /* no data to send */
394 if (msg->pos == msg->size)
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Message with %u bytes sent, removing message from queue\n",
401 /* Calling transmit continuation */
402 if (NULL != msg->transmit_cont)
403 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
404 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
411 client_connect (struct Session *s)
413 struct Plugin *plugin = s->plugin;
419 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
420 "Initiating outbound session peer `%s'\n",
421 GNUNET_i2s (&s->target));
424 s->inbound = GNUNET_NO;
428 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);
429 //GNUNET_asprintf (&url, "http://www.heise.de", http_plugin_address_to_string (plugin, s->addr, s->addrlen), GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),plugin->last_tag);
431 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
435 /* create get connection */
436 s->client_get = curl_easy_init ();
438 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
439 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
440 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
443 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
444 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
445 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
447 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
448 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
449 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
450 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
451 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
452 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
453 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
454 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
455 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
456 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
457 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
458 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
459 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
460 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
462 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
465 /* create put connection */
466 s->client_put = curl_easy_init ();
468 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
469 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
470 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
473 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
474 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
475 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
477 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
478 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
479 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
480 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
481 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
482 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
483 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
484 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
485 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
486 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
487 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
488 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
489 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
490 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
491 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
493 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
498 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
499 if (mret != CURLM_OK)
501 curl_easy_cleanup (s->client_get);
506 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
507 if (mret != CURLM_OK)
509 curl_multi_remove_handle (plugin->client_mh, s->client_get);
510 curl_easy_cleanup (s->client_get);
511 curl_easy_cleanup (s->client_put);
516 /* Perform connect */
517 plugin->cur_connections += 2;
519 /* Re-schedule since handles have changed */
520 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
522 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
523 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
525 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
531 client_start (struct Plugin *plugin)
535 curl_global_init (CURL_GLOBAL_ALL);
536 plugin->client_mh = curl_multi_init ();
538 if (NULL == plugin->client_mh)
540 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
542 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
550 client_stop (struct Plugin *plugin)
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;
558 curl_multi_cleanup (plugin->client_mh);
559 curl_global_cleanup ();
564 /* end of plugin_transport_http_client.c */