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