more functionality
[oweals/gnunet.git] / src / transport / plugin_transport_http_client.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/plugin_transport_http_client.c
23  * @brief http transport service plugin
24  * @author Matthias Wachs
25  */
26
27 #include "plugin_transport_http.h"
28
29 #if VERBOSE_CLIENT
30 /**
31  * Function to log curl debug messages with GNUNET_log
32  * @param curl handle
33  * @param type curl_infotype
34  * @param data data
35  * @param size size
36  * @param cls  closure
37  * @return 0
38  */
39 static int
40 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
41 {
42   if (type == CURLINFO_TEXT)
43   {
44     char text[size + 2];
45
46     memcpy (text, data, size);
47     if (text[size - 1] == '\n')
48       text[size] = '\0';
49     else
50     {
51       text[size] = '\n';
52       text[size + 1] = '\0';
53     }
54     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client: %X - %s", cls, text);
55   }
56   return 0;
57 }
58 #endif
59
60 int
61 client_disconnect (struct Session *s)
62 {
63   int res = GNUNET_OK;
64   CURLMcode mret;
65
66 #if DEBUG_HTTP
67   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
68                    "Deleting outbound session peer `%s'\n",
69                    GNUNET_i2s (&s->target));
70 #endif
71
72   mret = curl_multi_remove_handle (s->plugin->client_mh, s->client_put);
73   if (mret != CURLM_OK)
74   {
75     curl_easy_cleanup (s->client_put);
76     res = GNUNET_SYSERR;
77     GNUNET_break (0);
78   }
79   curl_easy_cleanup (s->client_put);
80
81   mret = curl_multi_remove_handle (s->plugin->client_mh, s->client_get);
82   if (mret != CURLM_OK)
83   {
84     curl_easy_cleanup (s->client_get);
85     res = GNUNET_SYSERR;
86     GNUNET_break (0);
87   }
88   curl_easy_cleanup (s->client_get);
89
90   return res;
91 }
92
93 int
94 client_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
95 {
96   return GNUNET_OK;
97 }
98
99 /**
100  * Task performing curl operations
101  * @param cls plugin as closure
102  * @param tc gnunet scheduler task context
103  */
104 static void
105 client_perform (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
106
107 /**
108  * Function setting up file descriptors and scheduling task to run
109  *
110  * @param  plugin plugin as closure
111  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
112  */
113 static int
114 client_schedule_next_perform (struct Plugin *plugin)
115 {
116   fd_set rs;
117   fd_set ws;
118   fd_set es;
119   int max;
120   struct GNUNET_NETWORK_FDSet *grs;
121   struct GNUNET_NETWORK_FDSet *gws;
122   long to;
123   CURLMcode mret;
124   struct GNUNET_TIME_Relative timeout;
125
126   /* Cancel previous scheduled task */
127   if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
128   {
129     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
130     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
131   }
132
133   max = -1;
134   FD_ZERO (&rs);
135   FD_ZERO (&ws);
136   FD_ZERO (&es);
137   mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
138   if (mret != CURLM_OK)
139   {
140     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
141                 "curl_multi_fdset", __FILE__, __LINE__,
142                 curl_multi_strerror (mret));
143     return GNUNET_SYSERR;
144   }
145   mret = curl_multi_timeout (plugin->client_mh, &to);
146   if (to == -1)
147     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
148   else
149     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
150   if (mret != CURLM_OK)
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
153                 "curl_multi_timeout", __FILE__, __LINE__,
154                 curl_multi_strerror (mret));
155     return GNUNET_SYSERR;
156   }
157
158   grs = GNUNET_NETWORK_fdset_create ();
159   gws = GNUNET_NETWORK_fdset_create ();
160   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
161   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
162   plugin->client_perform_task =
163       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
164                                    GNUNET_SCHEDULER_NO_TASK,
165                                    timeout,
166                                    grs,
167                                    gws,
168                                    &client_perform,
169                                    plugin);
170   GNUNET_NETWORK_fdset_destroy (gws);
171   GNUNET_NETWORK_fdset_destroy (grs);
172   return GNUNET_OK;
173 }
174
175
176 /**
177  * Task performing curl operations
178  * @param cls plugin as closure
179  * @param tc gnunet scheduler task context
180  */
181 static void
182 client_perform (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 {
184   struct Plugin *plugin = cls;
185   static unsigned int handles_last_run;
186   int running;
187   CURLMcode mret;
188
189   GNUNET_assert (cls != NULL);
190
191   plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
192   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
193     return;
194   do
195   {
196     running = 0;
197     mret = curl_multi_perform (plugin->client_mh, &running);
198     if ((running < handles_last_run) && (running > 0))
199       {
200
201       }
202       //curl_handle_finished (plugin);
203     handles_last_run = running;
204   }
205   while (mret == CURLM_CALL_MULTI_PERFORM);
206   client_schedule_next_perform (plugin);
207 }
208
209 int
210 client_connect (struct Session *s)
211 {
212   int res = GNUNET_OK;
213   char *url;
214   CURLMcode mret;
215
216 #if DEBUG_HTTP
217   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
218                    "Initiating outbound session peer `%s'\n",
219                    GNUNET_i2s (&s->target));
220 #endif
221
222   s->inbound = GNUNET_NO;
223
224   /* create url */
225   GNUNET_asprintf (&url, "%s://%s/", s->plugin->protocol,
226                    http_plugin_address_to_string (NULL, s->addr, s->addrlen));
227
228 #if DEBUG_HTTP
229   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name, "URL `%s'\n", url);
230 #endif
231
232   /* create get connection */
233   s->client_get = curl_easy_init ();
234 #if VERBOSE_CLIENT
235   curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
236   curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
237   curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
238 #endif
239 #if BUILD_HTTPS
240   curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
241   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
242   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
243 #endif
244   curl_easy_setopt (s->client_get, CURLOPT_URL, url);
245   //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
246   //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
247   //curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, curl_send_cb);
248   //curl_easy_setopt (s->client_get, CURLOPT_READDATA, ps);
249   //curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, curl_receive_cb);
250   //curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, ps);
251   curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
252                     (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
253   //curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, ps);
254   curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
255                     (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
256   curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
257                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
258 #if CURL_TCP_NODELAY
259   curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
260 #endif
261
262   /* create put connection */
263   s->client_put = curl_easy_init ();
264 #if VERBOSE_CLIENT
265   curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
266   curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
267   curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
268 #endif
269 #if BUILD_HTTPS
270   curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
271   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
272   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
273 #endif
274   curl_easy_setopt (s->client_put, CURLOPT_URL, url);
275   curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
276   //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
277   //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
278   //curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, curl_send_cb);
279   //curl_easy_setopt (s->client_put, CURLOPT_READDATA, ps);
280   //curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, curl_receive_cb);
281   //curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, ps);
282   curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
283                     (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
284   //curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, ps);
285   curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
286                     (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
287   curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
288                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
289 #if CURL_TCP_NODELAY
290   curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
291 #endif
292
293   GNUNET_free (url);
294
295   mret = curl_multi_add_handle (s->plugin->client_mh, s->client_get);
296   if (mret != CURLM_OK)
297   {
298     curl_easy_cleanup (s->client_get);
299     res = GNUNET_SYSERR;
300     GNUNET_break (0);
301   }
302
303   mret = curl_multi_add_handle (s->plugin->client_mh, s->client_put);
304   if (mret != CURLM_OK)
305   {
306     curl_multi_remove_handle (s->plugin->client_mh, s->client_get);
307     curl_easy_cleanup (s->client_get);
308     curl_easy_cleanup (s->client_put);
309     res = GNUNET_SYSERR;
310     GNUNET_break (0);
311   }
312
313   /* Perform connect */
314   s->plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_perform, s->plugin);
315
316   return res;
317 }
318
319 int
320 client_start (struct Plugin *plugin)
321 {
322   int res = GNUNET_OK;
323
324   curl_global_init (CURL_GLOBAL_ALL);
325   plugin->client_mh = curl_multi_init ();
326
327   if (NULL == plugin->client_mh)
328   {
329     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
330                      _
331                      ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
332                      plugin->name);
333     res = GNUNET_SYSERR;
334   }
335   return res;
336 }
337
338 void
339 client_stop (struct Plugin *plugin)
340 {
341   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
342   {
343     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
344     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
345   }
346
347   curl_multi_cleanup (plugin->client_mh);
348   curl_global_cleanup ();
349 }
350
351
352
353 /* end of plugin_transport_http_client.c */