(no commit message)
[oweals/gnunet.git] / src / transport / plugin_transport_http.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 2, 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.c
23  * @brief Implementation of the HTTP transport service
24  * @author Matthias Wachs
25  */
26
27 #include "platform.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_transport_service.h"
34 #include "plugin_transport.h"
35 #include "microhttpd.h"
36 #include <curl/curl.h>
37
38 #define VERBOSE GNUNET_YES
39 #define DEBUG GNUNET_YES
40
41 /**
42  * After how long do we expire an address that we
43  * learned from another peer if it is not reconfirmed
44  * by anyone?
45  */
46 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
47
48 #define HTTP_TIMEOUT 600
49
50 /**
51  * Encapsulation of all of the state of the plugin.
52  */
53 struct Plugin;
54
55
56 /**
57  * Session handle for connections.
58  */
59 struct Session
60 {
61
62   /**
63    * Stored in a linked list.
64    */
65   struct Session *next;
66
67   /**
68    * Pointer to the global plugin struct.
69    */
70   struct Plugin *plugin;
71
72   /**
73    * The client (used to identify this connection)
74    */
75   /* void *client; */
76
77   /**
78    * Continuation function to call once the transmission buffer
79    * has again space available.  NULL if there is no
80    * continuation to call.
81    */
82   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
83
84   /**
85    * Closure for transmit_cont.
86    */
87   void *transmit_cont_cls;
88
89   /**
90    * To whom are we talking to (set to our identity
91    * if we are still waiting for the welcome message)
92    */
93   struct GNUNET_PeerIdentity sender;
94
95   /**
96    * At what time did we reset last_received last?
97    */
98   struct GNUNET_TIME_Absolute last_quota_update;
99
100   /**
101    * How many bytes have we received since the "last_quota_update"
102    * timestamp?
103    */
104   uint64_t last_received;
105
106   /**
107    * Number of bytes per ms that this peer is allowed
108    * to send to us.
109    */
110   uint32_t quota;
111
112 };
113
114 /**
115  * Encapsulation of all of the state of the plugin.
116  */
117 struct Plugin
118 {
119   /**
120    * Our environment.
121    */
122   struct GNUNET_TRANSPORT_PluginEnvironment *env;
123
124   /**
125    * List of open sessions.
126    */
127   struct Session *sessions;
128
129   /**
130    * Handle for the statistics service.
131    */
132   struct GNUNET_STATISTICS_Handle *statistics;
133
134 };
135
136 /**
137  * Daemon for listening for new connections.
138  */
139 static struct MHD_Daemon *http_daemon;
140
141 /**
142  * Curl multi for managing client operations.
143  */
144 static CURLM *curl_multi;
145
146 /**
147  * Function that can be used by the transport service to transmit
148  * a message using the plugin.
149  *
150  * @param cls closure
151  * @param target who should receive this message
152  * @param priority how important is the message
153  * @param msgbuf the message to transmit
154  * @param msgbuf_size number of bytes in 'msgbuf'
155  * @param timeout when should we time out 
156  * @param session which session must be used (or NULL for "any")
157  * @param addr the address to use (can be NULL if the plugin
158  *                is "on its own" (i.e. re-use existing TCP connection))
159  * @param addrlen length of the address in bytes
160  * @param force_address GNUNET_YES if the plugin MUST use the given address,
161  *                otherwise the plugin may use other addresses or
162  *                existing connections (if available)
163  * @param cont continuation to call once the message has
164  *        been transmitted (or if the transport is ready
165  *        for the next transmission call; or if the
166  *        peer disconnected...)
167  * @param cont_cls closure for cont
168  * @return number of bytes used (on the physical network, with overheads);
169  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
170  *         and does NOT mean that the message was not transmitted (DV)
171  */
172 static ssize_t
173 http_plugin_send (void *cls,
174                       const struct GNUNET_PeerIdentity *
175                       target,
176                       const char *msgbuf,
177                       size_t msgbuf_size,
178                       unsigned int priority,
179                       struct GNUNET_TIME_Relative timeout,
180                       struct Session *session,
181                       const void *addr,
182                       size_t addrlen,
183                       int force_address,
184                       GNUNET_TRANSPORT_TransmitContinuation
185                       cont, void *cont_cls)
186 {
187   int bytes_sent = 0;
188   /*  struct Plugin *plugin = cls; */
189   return bytes_sent;
190 }
191
192
193
194 /**
195  * Function that can be used to force the plugin to disconnect
196  * from the given peer and cancel all previous transmissions
197  * (and their continuationc).
198  *
199  * @param cls closure
200  * @param target peer from which to disconnect
201  */
202 void
203 http_plugin_disconnect (void *cls,
204                             const struct GNUNET_PeerIdentity *target)
205 {
206   // struct Plugin *plugin = cls;
207   // FIXME
208   return;
209 }
210
211
212 /**
213  * Convert the transports address to a nice, human-readable
214  * format.
215  *
216  * @param cls closure
217  * @param type name of the transport that generated the address
218  * @param addr one of the addresses of the host, NULL for the last address
219  *        the specific address format depends on the transport
220  * @param addrlen length of the address
221  * @param numeric should (IP) addresses be displayed in numeric form?
222  * @param timeout after how long should we give up?
223  * @param asc function to call on each string
224  * @param asc_cls closure for asc
225  */
226 static void
227 http_plugin_address_pretty_printer (void *cls,
228                                     const char *type,
229                                     const void *addr,
230                                     size_t addrlen,
231                                     int numeric,
232                                     struct GNUNET_TIME_Relative timeout,
233                                     GNUNET_TRANSPORT_AddressStringCallback
234                                     asc, void *asc_cls)
235 {
236   asc (asc_cls, NULL);
237 }
238
239
240
241 /**
242  * Another peer has suggested an address for this
243  * peer and transport plugin.  Check that this could be a valid
244  * address.  If so, consider adding it to the list
245  * of addresses.
246  *
247  * @param cls closure
248  * @param addr pointer to the address
249  * @param addrlen length of addr
250  * @return GNUNET_OK if this is a plausible address for this peer
251  *         and transport
252  */
253 static int
254 http_plugin_address_suggested (void *cls,
255                                   void *addr, size_t addrlen)
256 {
257   /* struct Plugin *plugin = cls; */
258
259   /* check if the address is plausible; if so,
260      add it to our list! */
261   return GNUNET_OK;
262 }
263
264 /**
265  * Check if we are allowed to connect to the given IP.
266  */
267 static int
268 acceptPolicyCallback (void *cls,
269                       const struct sockaddr *addr, socklen_t addr_len)
270 {
271   return MHD_YES;
272 }
273
274 /**
275  * Process GET or PUT request received via MHD.  For
276  * GET, queue response that will send back our pending
277  * messages.  For PUT, process incoming data and send
278  * to GNUnet core.  In either case, check if a session
279  * already exists and create a new one if not.
280  */
281 static int
282 accessHandlerCallback (void *cls,
283                        struct MHD_Connection *session,
284                        const char *url,
285                        const char *method,
286                        const char *version,
287                        const char *upload_data,
288                        size_t * upload_data_size, void **httpSessionCache)
289 {
290   return MHD_YES;
291 }
292
293 /**
294  * MHD is done handling a request.  Cleanup
295  * the respective transport state.
296  */
297 static void
298 requestCompletedCallback (void *unused,
299                           struct MHD_Connection *session,
300                           void **httpSessionCache)
301 {
302
303 }
304
305 /**
306  * Entry point for the plugin.
307  */
308 void *
309 libgnunet_plugin_transport_http_init (void *cls)
310 {
311   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
312   struct GNUNET_TRANSPORT_PluginFunctions *api;
313   struct Plugin *plugin;
314   long long unsigned int port;
315   int use_ipv6;
316
317   plugin = GNUNET_malloc (sizeof (struct Plugin));
318   plugin->env = env;
319   plugin->statistics = NULL;
320   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
321   api->cls = plugin;
322   api->send = &http_plugin_send;
323   api->disconnect = &http_plugin_disconnect;
324   api->address_pretty_printer = &http_plugin_address_pretty_printer;
325   api->check_address = &http_plugin_address_suggested;
326
327   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
328   /* Reading port number from config file */
329   if ((GNUNET_OK !=
330        GNUNET_CONFIGURATION_get_value_number (env->cfg,
331                                               "transport-http",
332                                               "PORT",
333                                               &port)) ||
334       (port > 65535) )
335     {
336       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
337                        "http",
338                        _
339                        ("Require valid port number for service `%s' in configuration!\n"),
340                        "transport-http");
341       return NULL;
342     }
343   use_ipv6 = GNUNET_YES;
344   use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno  (env->cfg, "transport-http","USE_IPV6");
345   if ((http_daemon == NULL) && (port != 0))
346     {
347       if ( use_ipv6 == GNUNET_YES)
348         {
349           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon could not started, http plugin not working\n");
350           http_daemon = MHD_start_daemon (MHD_USE_IPv6,
351                                          port,
352                                          &acceptPolicyCallback,
353                                          NULL, &accessHandlerCallback, NULL,
354                                          MHD_OPTION_CONNECTION_TIMEOUT,
355                                          (unsigned int) HTTP_TIMEOUT,
356                                          MHD_OPTION_CONNECTION_MEMORY_LIMIT,
357                                          (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
358                                          MHD_OPTION_CONNECTION_LIMIT,
359                                          (unsigned int) 128,
360                                          MHD_OPTION_PER_IP_CONNECTION_LIMIT,
361                                          (unsigned int) 8,
362                                          MHD_OPTION_NOTIFY_COMPLETED,
363                                          &requestCompletedCallback, NULL,
364                                          MHD_OPTION_END);
365         }
366       else
367         {
368           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 disabled\n",port);
369           http_daemon = MHD_start_daemon (MHD_NO_FLAG,
370                                          port,
371                                          &acceptPolicyCallback,
372                                          NULL, &accessHandlerCallback, NULL,
373                                          MHD_OPTION_CONNECTION_TIMEOUT,
374                                          (unsigned int) HTTP_TIMEOUT,
375                                          MHD_OPTION_CONNECTION_MEMORY_LIMIT,
376                                          (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
377                                          MHD_OPTION_CONNECTION_LIMIT,
378                                          (unsigned int) 128,
379                                          MHD_OPTION_PER_IP_CONNECTION_LIMIT,
380                                          (unsigned int) 8,
381                                          MHD_OPTION_NOTIFY_COMPLETED,
382                                          &requestCompletedCallback, NULL,
383                                          MHD_OPTION_END);
384         }
385     }
386
387   curl_multi = curl_multi_init ();
388
389   if ( (NULL != http_daemon) && (NULL != curl_multi))
390     return api;
391   else
392   {
393     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Initializing http plugin failed\n");
394     return NULL;
395   }
396 }
397
398
399 /**
400  * Exit point from the plugin.
401  */
402 void *
403 libgnunet_plugin_transport_http_done (void *cls)
404 {
405   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
406   struct Plugin *plugin = api->cls;
407
408   if (http_daemon != NULL)
409   {
410     MHD_stop_daemon (http_daemon);
411     http_daemon = NULL;
412   }
413
414   curl_multi_cleanup (curl_multi);
415   curl_multi = NULL;
416
417   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
418
419   GNUNET_free (plugin);
420   GNUNET_free (api);
421   return NULL;
422 }
423
424 /* end of plugin_transport_http.c */