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), GNUNET_a2s (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 "Client: 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 "Client: 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;
308 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: Received %Zu bytes from peer `%s'\n",
310 GNUNET_i2s (&s->target));
313 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "no inbound bandwidth available! Next read was delayed for %llu ms\n",
318 s, GNUNET_TIME_absolute_get_difference(s->delay, GNUNET_TIME_absolute_get()).rel_value);
324 if (s->msg_tk == NULL)
325 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
327 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, size * nmemb, GNUNET_NO,
330 return (size * nmemb);
334 * Callback method used with libcurl
335 * Method is called when libcurl needs to read 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 source pointer, passed to the libcurl handle
340 * @return bytes written to stream
343 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
345 struct Session *s = cls;
346 //struct Plugin *plugin = s->plugin;
347 size_t bytes_sent = 0;
350 struct HTTP_Message *msg = s->msg_head;
352 if (s->put_paused == GNUNET_NO)
353 return CURL_READFUNC_PAUSE;
354 if ((s->msg_head == NULL) && (s->put_paused == GNUNET_YES))
357 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Suspending handle `%s' `%s'\n",
358 GNUNET_i2s (&s->target),GNUNET_a2s (s->addr, s->addrlen));
360 s->put_paused = GNUNET_NO;
361 return CURL_READFUNC_PAUSE;
366 GNUNET_assert (msg != NULL);
368 if (msg->pos < msg->size)
370 /* data fit in buffer */
371 if ((msg->size - msg->pos) <= (size * nmemb))
373 len = (msg->size - msg->pos);
374 memcpy (stream, &msg->buf[msg->pos], len);
381 memcpy (stream, &msg->buf[msg->pos], len);
386 /* no data to send */
392 if (msg->pos == msg->size)
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Message with %u bytes sent, removing message from queue\n",
399 /* Calling transmit continuation */
400 if (NULL != msg->transmit_cont)
401 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
402 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
409 client_connect (struct Session *s)
411 struct Plugin *plugin = s->plugin;
417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
418 "Initiating outbound session peer `%s'\n",
419 GNUNET_i2s (&s->target));
422 s->inbound = GNUNET_NO;
426 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);
427 //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);
429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
433 /* create get connection */
434 s->client_get = curl_easy_init ();
436 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
437 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
438 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
441 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
442 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
443 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
445 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
446 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
447 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
448 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
449 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
450 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
451 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
452 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
453 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
454 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
455 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
456 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
457 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
458 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
460 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
463 /* create put connection */
464 s->client_put = curl_easy_init ();
466 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
467 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
468 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
471 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
472 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
473 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
475 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
476 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
477 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
478 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
479 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
480 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
481 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
482 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
483 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
484 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
485 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
486 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
487 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
488 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
489 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
491 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
496 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
497 if (mret != CURLM_OK)
499 curl_easy_cleanup (s->client_get);
504 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
505 if (mret != CURLM_OK)
507 curl_multi_remove_handle (plugin->client_mh, s->client_get);
508 curl_easy_cleanup (s->client_get);
509 curl_easy_cleanup (s->client_put);
514 /* Perform connect */
515 plugin->cur_connections += 2;
517 /* Re-schedule since handles have changed */
518 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
520 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
521 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
523 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
529 client_start (struct Plugin *plugin)
533 curl_global_init (CURL_GLOBAL_ALL);
534 plugin->client_mh = curl_multi_init ();
536 if (NULL == plugin->client_mh)
538 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
540 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
548 client_stop (struct Plugin *plugin)
550 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
552 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
553 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
556 curl_multi_cleanup (plugin->client_mh);
557 curl_global_cleanup ();
562 /* end of plugin_transport_http_client.c */