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, const char *msgbuf, size_t msgbuf_size)
67 * Task performing curl operations
68 * @param cls plugin as closure
69 * @param tc gnunet scheduler task context
72 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
75 * Function setting up file descriptors and scheduling task to run
77 * @param plugin plugin as closure
78 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
81 client_schedule (struct Plugin *plugin)
87 struct GNUNET_NETWORK_FDSet *grs;
88 struct GNUNET_NETWORK_FDSet *gws;
91 struct GNUNET_TIME_Relative timeout;
93 /* Cancel previous scheduled task */
94 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
96 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
97 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
104 mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
105 if (mret != CURLM_OK)
107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
108 "curl_multi_fdset", __FILE__, __LINE__,
109 curl_multi_strerror (mret));
110 return GNUNET_SYSERR;
112 mret = curl_multi_timeout (plugin->client_mh, &to);
114 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
116 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
117 if (mret != CURLM_OK)
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
120 "curl_multi_timeout", __FILE__, __LINE__,
121 curl_multi_strerror (mret));
122 return GNUNET_SYSERR;
125 grs = GNUNET_NETWORK_fdset_create ();
126 gws = GNUNET_NETWORK_fdset_create ();
127 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
128 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
130 plugin->client_perform_task =
131 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
132 GNUNET_SCHEDULER_NO_TASK,
138 GNUNET_NETWORK_fdset_destroy (gws);
139 GNUNET_NETWORK_fdset_destroy (grs);
145 * Task performing curl operations
146 * @param cls plugin as closure
147 * @param tc gnunet scheduler task context
150 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
152 struct Plugin *plugin = cls;
153 static unsigned int handles_last_run;
157 GNUNET_assert (cls != NULL);
159 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
160 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
166 mret = curl_multi_perform (plugin->client_mh, &running);
170 while ((msg = curl_multi_info_read(plugin->client_mh, &msgs_left)))
172 CURL *easy_h = msg->easy_handle;
174 GNUNET_assert (easy_h != NULL);
176 GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &s));
177 GNUNET_assert (s != NULL);
179 if (msg->msg == CURLMSG_DONE)
182 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
183 "Connection to '%s' %s ended\n", GNUNET_i2s(&s->target), http_plugin_address_to_string(plugin, s->addr, s->addrlen));
185 client_disconnect(s);
186 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,"Notifying about ended session to peer `%s' `%s'\n", GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
187 notify_session_end (plugin, &s->target, s);
191 handles_last_run = running;
193 while (mret == CURLM_CALL_MULTI_PERFORM);
194 client_schedule (plugin);
198 client_disconnect (struct Session *s)
202 struct Plugin *plugin = s->plugin;
205 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
206 "Deleting outbound PUT session to peer `%s'\n",
207 GNUNET_i2s (&s->target));
210 if (s->client_put != NULL)
212 mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
213 if (mret != CURLM_OK)
215 curl_easy_cleanup (s->client_put);
219 curl_easy_cleanup (s->client_put);
220 s->client_put = NULL;
224 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
225 "Deleting outbound GET session to peer `%s'\n",
226 GNUNET_i2s (&s->target));
229 if (s->client_get != NULL)
231 mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
232 if (mret != CURLM_OK)
234 curl_easy_cleanup (s->client_get);
238 curl_easy_cleanup (s->client_get);
239 s->client_get = NULL;
242 plugin->cur_connections -= 2;
243 /* Re-schedule since handles have changed */
244 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
246 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
247 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
250 plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
257 client_connect (struct Session *s)
259 struct Plugin *plugin = s->plugin;
265 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
266 "Initiating outbound session peer `%s'\n",
267 GNUNET_i2s (&s->target));
270 s->inbound = GNUNET_NO;
274 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);
276 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
280 /* create get connection */
281 s->client_get = curl_easy_init ();
283 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
284 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
285 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
288 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
289 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
290 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
292 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
293 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
294 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
295 //curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, curl_send_cb);
296 //curl_easy_setopt (s->client_get, CURLOPT_READDATA, ps);
297 //curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, curl_receive_cb);
298 //curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, ps);
299 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
300 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
301 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
302 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
303 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
304 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
305 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
307 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
310 /* create put connection */
311 s->client_put = curl_easy_init ();
313 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
314 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
315 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
318 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
319 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
320 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
322 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
323 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
324 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
325 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
326 //curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, curl_send_cb);
327 //curl_easy_setopt (s->client_put, CURLOPT_READDATA, ps);
328 //curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, curl_receive_cb);
329 //curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, ps);
330 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
331 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
332 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
333 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
334 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
335 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
336 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
338 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
343 mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
344 if (mret != CURLM_OK)
346 curl_easy_cleanup (s->client_get);
351 mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
352 if (mret != CURLM_OK)
354 curl_multi_remove_handle (plugin->client_mh, s->client_get);
355 curl_easy_cleanup (s->client_get);
356 curl_easy_cleanup (s->client_put);
361 /* Perform connect */
362 plugin->cur_connections += 2;
364 /* Re-schedule since handles have changed */
365 if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
367 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
368 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
370 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
376 client_start (struct Plugin *plugin)
380 curl_global_init (CURL_GLOBAL_ALL);
381 plugin->client_mh = curl_multi_init ();
383 if (NULL == plugin->client_mh)
385 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
387 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
395 client_stop (struct Plugin *plugin)
397 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
399 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
400 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
403 curl_multi_cleanup (plugin->client_mh);
404 curl_global_cleanup ();
409 /* end of plugin_transport_http_client.c */