implemented disconnect notifications for outbound connections
[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_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
62 {
63   return GNUNET_OK;
64 }
65
66 /**
67  * Task performing curl operations
68  * @param cls plugin as closure
69  * @param tc gnunet scheduler task context
70  */
71 static void
72 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
73
74 /**
75  * Function setting up file descriptors and scheduling task to run
76  *
77  * @param  plugin plugin as closure
78  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
79  */
80 static int
81 client_schedule (struct Plugin *plugin)
82 {
83   fd_set rs;
84   fd_set ws;
85   fd_set es;
86   int max;
87   struct GNUNET_NETWORK_FDSet *grs;
88   struct GNUNET_NETWORK_FDSet *gws;
89   long to;
90   CURLMcode mret;
91   struct GNUNET_TIME_Relative timeout;
92
93   /* Cancel previous scheduled task */
94   if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
95   {
96     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
97     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
98   }
99
100   max = -1;
101   FD_ZERO (&rs);
102   FD_ZERO (&ws);
103   FD_ZERO (&es);
104   mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
105   if (mret != CURLM_OK)
106   {
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;
111   }
112   mret = curl_multi_timeout (plugin->client_mh, &to);
113   if (to == -1)
114     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
115   else
116     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
117   if (mret != CURLM_OK)
118   {
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;
123   }
124
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);
129   plugin->client_perform_task =
130       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
131                                    GNUNET_SCHEDULER_NO_TASK,
132                                    timeout,
133                                    grs,
134                                    gws,
135                                    &client_run,
136                                    plugin);
137   GNUNET_NETWORK_fdset_destroy (gws);
138   GNUNET_NETWORK_fdset_destroy (grs);
139   return GNUNET_OK;
140 }
141
142
143 /**
144  * Task performing curl operations
145  * @param cls plugin as closure
146  * @param tc gnunet scheduler task context
147  */
148 static void
149 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150 {
151   struct Plugin *plugin = cls;
152   static unsigned int handles_last_run;
153   int running;
154   CURLMcode mret;
155
156   GNUNET_assert (cls != NULL);
157
158   plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
159   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
160     return;
161
162   do
163   {
164     running = 0;
165     mret = curl_multi_perform (plugin->client_mh, &running);
166
167     CURLMsg * msg;
168     int msgs_left;
169     while ((msg = curl_multi_info_read(plugin->client_mh, &msgs_left)))
170     {
171        CURL *easy_h  = msg->easy_handle;
172        struct Session *s;
173        GNUNET_assert (easy_h != NULL);
174
175        GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &s));
176        GNUNET_assert (s != NULL);
177
178        if (msg->msg == CURLMSG_DONE)
179        {
180 #if DEBUG_HTTP
181          GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
182                    "Connection to '%s'  %s ended\n", GNUNET_i2s(&s->target), http_plugin_address_to_string(s->plugin, s->addr, s->addrlen));
183 #endif
184          client_disconnect(s);
185          notify_session_end (s->plugin, &s->target, s);
186        }
187     }
188
189     handles_last_run = running;
190   }
191   while (mret == CURLM_CALL_MULTI_PERFORM);
192   client_schedule (plugin);
193 }
194
195 int
196 client_disconnect (struct Session *s)
197 {
198   int res = GNUNET_OK;
199   CURLMcode mret;
200   struct Plugin *plugin = s->plugin;
201
202 #if DEBUG_HTTP
203   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
204                    "Deleting outbound PUT session to peer `%s'\n",
205                    GNUNET_i2s (&s->target));
206 #endif
207
208   mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
209   if (mret != CURLM_OK)
210   {
211     curl_easy_cleanup (s->client_put);
212     res = GNUNET_SYSERR;
213     GNUNET_break (0);
214   }
215   curl_easy_cleanup (s->client_put);
216
217 #if DEBUG_HTTP
218   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
219                    "Deleting outbound GET session to peer `%s'\n",
220                    GNUNET_i2s (&s->target));
221 #endif
222
223   mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
224   if (mret != CURLM_OK)
225   {
226     curl_easy_cleanup (s->client_get);
227     res = GNUNET_SYSERR;
228     GNUNET_break (0);
229   }
230   curl_easy_cleanup (s->client_get);
231
232   /* Re-schedule since handles have changed */
233   if (plugin->client_perform_task!= GNUNET_SCHEDULER_NO_TASK)
234   {
235     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
236     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
237   }
238
239   plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
240
241   return res;
242 }
243
244
245 int
246 client_connect (struct Session *s)
247 {
248   int res = GNUNET_OK;
249   char *url;
250   CURLMcode mret;
251
252 #if DEBUG_HTTP
253   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
254                    "Initiating outbound session peer `%s'\n",
255                    GNUNET_i2s (&s->target));
256 #endif
257
258   s->inbound = GNUNET_NO;
259
260   /* create url */
261   GNUNET_asprintf (&url, "%s", http_plugin_address_to_string (s->plugin, s->addr, s->addrlen));
262
263   /* create get connection */
264   s->client_get = curl_easy_init ();
265 #if VERBOSE_CLIENT
266   curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
267   curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
268   curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
269 #endif
270 #if BUILD_HTTPS
271   curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
272   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
273   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
274 #endif
275   curl_easy_setopt (s->client_get, CURLOPT_URL, url);
276   //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
277   //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
278   //curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, curl_send_cb);
279   //curl_easy_setopt (s->client_get, CURLOPT_READDATA, ps);
280   //curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, curl_receive_cb);
281   //curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, ps);
282   curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
283                     (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
284   curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
285   curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
286                     (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
287   curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
288                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
289 #if CURL_TCP_NODELAY
290   curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
291 #endif
292
293   /* create put connection */
294   s->client_put = curl_easy_init ();
295 #if VERBOSE_CLIENT
296   curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
297   curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
298   curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
299 #endif
300 #if BUILD_HTTPS
301   curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
302   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
303   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
304 #endif
305   curl_easy_setopt (s->client_put, CURLOPT_URL, url);
306   curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
307   //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
308   //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
309   //curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, curl_send_cb);
310   //curl_easy_setopt (s->client_put, CURLOPT_READDATA, ps);
311   //curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, curl_receive_cb);
312   //curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, ps);
313   curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
314                     (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
315   curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
316   curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
317                     (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
318   curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
319                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
320 #if CURL_TCP_NODELAY
321   curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
322 #endif
323
324   GNUNET_free (url);
325
326   mret = curl_multi_add_handle (s->plugin->client_mh, s->client_get);
327   if (mret != CURLM_OK)
328   {
329     curl_easy_cleanup (s->client_get);
330     res = GNUNET_SYSERR;
331     GNUNET_break (0);
332   }
333
334   mret = curl_multi_add_handle (s->plugin->client_mh, s->client_put);
335   if (mret != CURLM_OK)
336   {
337     curl_multi_remove_handle (s->plugin->client_mh, s->client_get);
338     curl_easy_cleanup (s->client_get);
339     curl_easy_cleanup (s->client_put);
340     res = GNUNET_SYSERR;
341     GNUNET_break (0);
342   }
343
344   /* Perform connect */
345   s->plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, s->plugin);
346
347   return res;
348 }
349
350 int
351 client_start (struct Plugin *plugin)
352 {
353   int res = GNUNET_OK;
354
355   curl_global_init (CURL_GLOBAL_ALL);
356   plugin->client_mh = curl_multi_init ();
357
358   if (NULL == plugin->client_mh)
359   {
360     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
361                      _
362                      ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
363                      plugin->name);
364     res = GNUNET_SYSERR;
365   }
366   return res;
367 }
368
369 void
370 client_stop (struct Plugin *plugin)
371 {
372   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
373   {
374     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
375     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
376   }
377
378   curl_multi_cleanup (plugin->client_mh);
379   curl_global_cleanup ();
380 }
381
382
383
384 /* end of plugin_transport_http_client.c */