(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 #define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt(c, a, b); if (ret != CURLE_OK) GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret)); } while (0);
51
52 /**
53  * Text of the response sent back after the last bytes of a PUT
54  * request have been received (just to formally obey the HTTP
55  * protocol).
56  */
57 #define HTTP_PUT_RESPONSE "Thank you!"
58
59 /**
60  * Encapsulation of all of the state of the plugin.
61  */
62 struct Plugin;
63
64
65 /**
66  * Session handle for connections.
67  */
68 struct Session
69 {
70
71   /**
72    * Stored in a linked list.
73    */
74   struct Session *next;
75
76   /**
77    * Pointer to the global plugin struct.
78    */
79   struct Plugin *plugin;
80
81   /**
82    * The client (used to identify this connection)
83    */
84   /* void *client; */
85
86   /**
87    * Continuation function to call once the transmission buffer
88    * has again space available.  NULL if there is no
89    * continuation to call.
90    */
91   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
92
93   /**
94    * Closure for transmit_cont.
95    */
96   void *transmit_cont_cls;
97
98   /**
99    * To whom are we talking to (set to our identity
100    * if we are still waiting for the welcome message)
101    */
102   struct GNUNET_PeerIdentity sender;
103
104   /**
105    * At what time did we reset last_received last?
106    */
107   struct GNUNET_TIME_Absolute last_quota_update;
108
109   /**
110    * How many bytes have we received since the "last_quota_update"
111    * timestamp?
112    */
113   uint64_t last_received;
114
115   /**
116    * Number of bytes per ms that this peer is allowed
117    * to send to us.
118    */
119   uint32_t quota;
120
121 };
122
123 /**
124  * Encapsulation of all of the state of the plugin.
125  */
126 struct Plugin
127 {
128   /**
129    * Our environment.
130    */
131   struct GNUNET_TRANSPORT_PluginEnvironment *env;
132
133   /**
134    * Handle to the network service.
135    */
136   struct GNUNET_SERVICE_Context *service;
137
138   /**
139    * List of open sessions.
140    */
141   struct Session *sessions;
142 };
143
144 static struct Plugin *plugin;
145
146 /**
147  * Daemon for listening for new IPv4 connections.
148  */
149 static struct MHD_Daemon *http_daemon_v4;
150
151 /**
152  * Daemon for listening for new IPv6connections.
153  */
154 static struct MHD_Daemon *http_daemon_v6;
155
156 /**
157  * Our primary task for http daemon handling IPv4 connections
158  */
159 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
160
161 /**
162  * Our primary task for http daemon handling IPv6 connections
163  */
164 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
165
166 /**
167  * ID of the task downloading the hostlist
168  */
169 static GNUNET_SCHEDULER_TaskIdentifier ti_download;
170
171
172
173 /**
174  * Curl multi for managing client operations.
175  */
176 static CURLM *curl_multi;
177
178 static char * get_url( const struct GNUNET_PeerIdentity * target)
179 {
180   return strdup("http://localhost:12389");
181 }
182
183 static size_t curl_read_function( void *ptr, size_t size, size_t nmemb, void *stream)
184 {
185   // strcpy ("Testmessa")
186   return 0;
187 }
188
189 /**
190  * Task that is run when we are ready to receive more data from the hostlist
191  * server.
192  *
193  * @param cls closure, unused
194  * @param tc task context, unused
195  */
196 static void
197 task_download (void *cls,
198              const struct GNUNET_SCHEDULER_TaskContext *tc)
199 {
200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Download!!!");
201 }
202
203 /**
204  * Function that can be used by the transport service to transmit
205  * a message using the plugin.
206  *
207  * @param cls closure
208  * @param target who should receive this message
209  * @param priority how important is the message
210  * @param msgbuf the message to transmit
211  * @param msgbuf_size number of bytes in 'msgbuf'
212  * @param timeout when should we time out 
213  * @param session which session must be used (or NULL for "any")
214  * @param addr the address to use (can be NULL if the plugin
215  *                is "on its own" (i.e. re-use existing TCP connection))
216  * @param addrlen length of the address in bytes
217  * @param force_address GNUNET_YES if the plugin MUST use the given address,
218  *                otherwise the plugin may use other addresses or
219  *                existing connections (if available)
220  * @param cont continuation to call once the message has
221  *        been transmitted (or if the transport is ready
222  *        for the next transmission call; or if the
223  *        peer disconnected...)
224  * @param cont_cls closure for cont
225  * @return number of bytes used (on the physical network, with overheads);
226  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
227  *         and does NOT mean that the message was not transmitted (DV)
228  */
229 static ssize_t
230 http_plugin_send (void *cls,
231                   const struct GNUNET_PeerIdentity * target,
232                   const char *msgbuf,
233                   size_t msgbuf_size,
234                   unsigned int priority,
235                   struct GNUNET_TIME_Relative timeout,
236                   struct Session *session,
237                   const void *addr,
238                   size_t addrlen,
239                   int force_address,
240                   GNUNET_TRANSPORT_TransmitContinuation cont,
241                   void *cont_cls)
242 {
243   char * peer_url = get_url( target );
244   CURL *curl;
245   CURLMcode mret;
246   CURLcode ret;
247
248   int bytes_sent = 0;
249   /*  struct Plugin *plugin = cls; */
250
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending %u bytes (`%s') to `%s'\n",msgbuf_size, msgbuf,GNUNET_i2s(target));
252   /* Insert code to send using cURL */
253   curl = curl_easy_init ();
254
255   CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
256   CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
257
258    /* setting put options */
259   CURL_EASY_SETOPT (curl, CURLOPT_UPLOAD, 1L);
260   CURL_EASY_SETOPT (curl, CURLOPT_PUT, 1L);
261   CURL_EASY_SETOPT (curl, CURLOPT_READDATA, msgbuf);
262
263
264   /* no need to abort if the above failed */
265   CURL_EASY_SETOPT (curl,
266                     CURLOPT_URL,
267                     peer_url);
268   if (ret != CURLE_OK)
269     {
270       /* clean_up (); */
271       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272                   "Peer URL is not correct\n");
273       return 0;
274     }
275   CURL_EASY_SETOPT (curl,
276                     CURLOPT_FAILONERROR,
277                     1);
278 #if 0
279   CURL_EASY_SETOPT (curl,
280                     CURLOPT_VERBOSE,
281                     1);
282 #endif
283   CURL_EASY_SETOPT (curl,
284                     CURLOPT_BUFFERSIZE,
285                     GNUNET_SERVER_MAX_MESSAGE_SIZE);
286   if (0 == strncmp (peer_url, "http", 4))
287     CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
288   CURL_EASY_SETOPT (curl,
289                     CURLOPT_CONNECTTIMEOUT,
290                     60L);
291   CURL_EASY_SETOPT (curl,
292                     CURLOPT_TIMEOUT,
293                     60L);
294
295   curl_multi = curl_multi_init ();
296   if (curl_multi == NULL)
297     {
298       GNUNET_break (0);
299       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300                   "curl multi is not correct\n");
301       /* clean_up (); */
302       return 0;
303     }
304   mret = curl_multi_add_handle (curl_multi, curl);
305   if (mret != CURLM_OK)
306     {
307       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308                   _("%s failed at %s:%d: `%s'\n"),
309                   "curl_multi_add_handle", __FILE__, __LINE__,
310                   curl_multi_strerror (mret));
311       mret = curl_multi_cleanup (curl_multi);
312       if (mret != CURLM_OK)
313         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314                     _("%s failed at %s:%d: `%s'\n"),
315                     "curl_multi_cleanup", __FILE__, __LINE__,
316                     curl_multi_strerror (mret));
317       curl_multi = NULL;
318       /* clean_up (); */
319       return 0;
320     }
321
322
323
324   fd_set rs;
325   fd_set ws;
326   fd_set es;
327   int max;
328   struct GNUNET_NETWORK_FDSet *grs;
329   struct GNUNET_NETWORK_FDSet *gws;
330   struct GNUNET_TIME_Relative rtime;
331   long timeout_curl;
332   max = -1;
333   FD_ZERO (&rs);
334   FD_ZERO (&ws);
335   FD_ZERO (&es);
336   mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
337   if (mret != CURLM_OK)
338     {
339       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340                   _("%s failed at %s:%d: `%s'\n"),
341                   "curl_multi_fdset", __FILE__, __LINE__,
342                   curl_multi_strerror (mret));
343       /* clean_up (); */
344       return 0;
345     }
346   mret = curl_multi_timeout (curl_multi, &timeout_curl);
347   if (mret != CURLM_OK)
348     {
349       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350                   _("%s failed at %s:%d: `%s'\n"),
351                   "curl_multi_timeout", __FILE__, __LINE__,
352                   curl_multi_strerror (mret));
353       /* clean_up (); */
354       return 0;
355     }
356   /*rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
357                                     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
358                                                                    timeout));*/
359   grs = GNUNET_NETWORK_fdset_create ();
360   gws = GNUNET_NETWORK_fdset_create ();
361   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
362   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
363   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364               "Scheduling task for hostlist download using cURL\n");
365
366   ti_download = GNUNET_SCHEDULER_add_select (plugin->env->sched,
367                                    GNUNET_SCHEDULER_PRIORITY_DEFAULT,
368                                    GNUNET_SCHEDULER_NO_TASK,
369                                    GNUNET_TIME_UNIT_FOREVER_REL,
370                                    grs,
371                                    gws,
372                                    &task_download,
373                                    curl_multi);
374   GNUNET_NETWORK_fdset_destroy (gws);
375   GNUNET_NETWORK_fdset_destroy (grs);
376
377   GNUNET_free(peer_url);
378   /* FIXME: */
379   bytes_sent = msgbuf_size;
380   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381               "Logging shutdown\n");
382   GNUNET_STATISTICS_set(plugin->env->stats,"shutdown",2, GNUNET_NO);
383   return bytes_sent;
384 }
385
386
387
388 /**
389  * Function that can be used to force the plugin to disconnect
390  * from the given peer and cancel all previous transmissions
391  * (and their continuationc).
392  *
393  * @param cls closure
394  * @param target peer from which to disconnect
395  */
396 void
397 http_plugin_disconnect (void *cls,
398                             const struct GNUNET_PeerIdentity *target)
399 {
400   // struct Plugin *plugin = cls;
401   // FIXME
402   return;
403 }
404
405
406 /**
407  * Convert the transports address to a nice, human-readable
408  * format.
409  *
410  * @param cls closure
411  * @param type name of the transport that generated the address
412  * @param addr one of the addresses of the host, NULL for the last address
413  *        the specific address format depends on the transport
414  * @param addrlen length of the address
415  * @param numeric should (IP) addresses be displayed in numeric form?
416  * @param timeout after how long should we give up?
417  * @param asc function to call on each string
418  * @param asc_cls closure for asc
419  */
420 static void
421 http_plugin_address_pretty_printer (void *cls,
422                                     const char *type,
423                                     const void *addr,
424                                     size_t addrlen,
425                                     int numeric,
426                                     struct GNUNET_TIME_Relative timeout,
427                                     GNUNET_TRANSPORT_AddressStringCallback
428                                     asc, void *asc_cls)
429 {
430   asc (asc_cls, NULL);
431 }
432
433
434
435 /**
436  * Another peer has suggested an address for this
437  * peer and transport plugin.  Check that this could be a valid
438  * address.  If so, consider adding it to the list
439  * of addresses.
440  *
441  * @param cls closure
442  * @param addr pointer to the address
443  * @param addrlen length of addr
444  * @return GNUNET_OK if this is a plausible address for this peer
445  *         and transport
446  */
447 static int
448 http_plugin_address_suggested (void *cls,
449                                   void *addr, size_t addrlen)
450 {
451   /* struct Plugin *plugin = cls; */
452
453   /* check if the address is plausible; if so,
454      add it to our list! */
455   return GNUNET_OK;
456 }
457
458 /**
459  * Check if we are allowed to connect to the given IP.
460  */
461 static int
462 acceptPolicyCallback (void *cls,
463                       const struct sockaddr *addr, socklen_t addr_len)
464 {
465   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming connection \n");
466   /* Currently all incoming connections are accepted, so nothing to do here */
467   return MHD_YES;
468 }
469
470 /**
471  * Process GET or PUT request received via MHD.  For
472  * GET, queue response that will send back our pending
473  * messages.  For PUT, process incoming data and send
474  * to GNUnet core.  In either case, check if a session
475  * already exists and create a new one if not.
476  */
477 static int
478 accessHandlerCallback (void *cls,
479                        struct MHD_Connection *session,
480                        const char *url,
481                        const char *method,
482                        const char *version,
483                        const char *upload_data,
484                        size_t * upload_data_size, void **httpSessionCache)
485 {
486   struct MHD_Response *response;
487
488   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from \n",method);
489
490   /* Find out if session exists, otherwise create one */
491
492   /* Is it a PUT or a GET request */
493   if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
494   {
495     /* PUT method here */
496     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %u \n",upload_data_size);
497     GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO);
498   }
499   if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
500   {
501     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request with size\n");
502     GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO);
503   }
504
505   response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
506                                    HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
507   MHD_queue_response (session, MHD_HTTP_OK, response);
508   MHD_destroy_response (response);
509
510   return MHD_YES;
511 }
512
513 /**
514  * Function that queries MHD's select sets and
515  * starts the task waiting for them.
516  */
517 static GNUNET_SCHEDULER_TaskIdentifier prepare_daemon (struct MHD_Daemon *daemon_handle);
518 /**
519  * Call MHD to process pending requests and then go back
520  * and schedule the next run.
521  */
522 static void
523 run_daemon (void *cls,
524             const struct GNUNET_SCHEDULER_TaskContext *tc)
525 {
526   struct MHD_Daemon *daemon_handle = cls;
527
528   if (daemon_handle == http_daemon_v4)
529     http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
530
531   if (daemon_handle == http_daemon_v6)
532     http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
533
534
535   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
536     return;
537   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
538   if (daemon_handle == http_daemon_v4)
539     http_task_v4 = prepare_daemon (daemon_handle);
540   if (daemon_handle == http_daemon_v6)
541     http_task_v6 = prepare_daemon (daemon_handle);
542 }
543
544 /**
545  * Function that queries MHD's select sets and
546  * starts the task waiting for them.
547  */
548 static GNUNET_SCHEDULER_TaskIdentifier
549 prepare_daemon (struct MHD_Daemon *daemon_handle)
550 {
551   GNUNET_SCHEDULER_TaskIdentifier ret;
552   fd_set rs;
553   fd_set ws;
554   fd_set es;
555   struct GNUNET_NETWORK_FDSet *wrs;
556   struct GNUNET_NETWORK_FDSet *wws;
557   struct GNUNET_NETWORK_FDSet *wes;
558   int max;
559   unsigned long long timeout;
560   int haveto;
561   struct GNUNET_TIME_Relative tv;
562
563   FD_ZERO(&rs);
564   FD_ZERO(&ws);
565   FD_ZERO(&es);
566   wrs = GNUNET_NETWORK_fdset_create ();
567   wes = GNUNET_NETWORK_fdset_create ();
568   wws = GNUNET_NETWORK_fdset_create ();
569   max = -1;
570   GNUNET_assert (MHD_YES ==
571                  MHD_get_fdset (daemon_handle,
572                                 &rs,
573                                 &ws,
574                                 &es,
575                                 &max));
576   haveto = MHD_get_timeout (daemon_handle, &timeout);
577   if (haveto == MHD_YES)
578     tv.value = (uint64_t) timeout;
579   else
580     tv = GNUNET_TIME_UNIT_FOREVER_REL;
581   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
582   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
583   GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
584   ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
585                                      GNUNET_SCHEDULER_PRIORITY_HIGH,
586                                      GNUNET_SCHEDULER_NO_TASK,
587                                      tv,
588                                      wrs,
589                                      wws,
590                                      &run_daemon,
591                                      daemon_handle);
592   GNUNET_NETWORK_fdset_destroy (wrs);
593   GNUNET_NETWORK_fdset_destroy (wws);
594   GNUNET_NETWORK_fdset_destroy (wes);
595   return ret;
596 }
597
598 /**
599  * Exit point from the plugin.
600  */
601 void *
602 libgnunet_plugin_transport_http_done (void *cls)
603 {
604   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
605   struct Plugin *plugin = api->cls;
606
607   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
608
609   if ( ti_download != GNUNET_SCHEDULER_NO_TASK)
610   {
611     GNUNET_SCHEDULER_cancel(plugin->env->sched, ti_download);
612     ti_download = GNUNET_SCHEDULER_NO_TASK;
613   }
614
615   if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
616   {
617     GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
618     http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
619   }
620
621   if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
622   {
623     GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
624     http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
625   }
626
627   if (http_daemon_v4 != NULL)
628   {
629     MHD_stop_daemon (http_daemon_v4);
630     http_daemon_v4 = NULL;
631   }
632   if (http_daemon_v6 != NULL)
633   {
634     MHD_stop_daemon (http_daemon_v6);
635     http_daemon_v6 = NULL;
636   }
637
638
639   if ( NULL != curl_multi)
640   {
641     curl_multi_cleanup (curl_multi);
642     curl_multi = NULL;
643   }
644   GNUNET_free (plugin);
645   GNUNET_free (api);
646   return NULL;
647 }
648
649 /**
650  * Entry point for the plugin.
651  */
652 void *
653 libgnunet_plugin_transport_http_init (void *cls)
654 {
655   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
656   struct GNUNET_TRANSPORT_PluginFunctions *api;
657   long long unsigned int port;
658
659   plugin = GNUNET_malloc (sizeof (struct Plugin));
660   plugin->env = env;
661
662   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
663   api->cls = plugin;
664   api->send = &http_plugin_send;
665   api->disconnect = &http_plugin_disconnect;
666   api->address_pretty_printer = &http_plugin_address_pretty_printer;
667   api->check_address = &http_plugin_address_suggested;
668
669
670   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
671   /* Reading port number from config file */
672   if ((GNUNET_OK !=
673        GNUNET_CONFIGURATION_get_value_number (env->cfg,
674                                               "transport-http",
675                                               "PORT",
676                                               &port)) ||
677       (port > 65535) )
678     {
679       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
680                        "http",
681                        _
682                        ("Require valid port number for service `%s' in configuration!\n"),
683                        "transport-http");
684       libgnunet_plugin_transport_http_done (api);
685       return NULL;
686     }
687   if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
688     {
689       http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
690                                          port,
691                                          &acceptPolicyCallback,
692                                          NULL, &accessHandlerCallback, NULL,
693                                          MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
694                                          MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
695                                          MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
696                                          MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
697                                          MHD_OPTION_END);
698       http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
699                                          port,
700                                          &acceptPolicyCallback,
701                                          NULL, &accessHandlerCallback, NULL,
702                                          MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
703                                          MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
704                                          MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
705                                          MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
706                                          MHD_OPTION_END);
707     }
708
709   curl_multi = curl_multi_init ();
710
711   if (http_daemon_v4 != NULL)
712     http_task_v4 = prepare_daemon (http_daemon_v4);
713   if (http_daemon_v6 != NULL)
714     http_task_v6 = prepare_daemon (http_daemon_v6);
715
716   if ((http_daemon_v4 == NULL) || (http_daemon_v6 != NULL))
717     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u\n",port);
718
719
720   if (NULL == plugin->env->stats)
721   {
722     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
723                 _("Failed to retrieve statistics handle\n"));
724     libgnunet_plugin_transport_http_done (api);
725     return NULL;
726   }
727
728   GNUNET_STATISTICS_set ( env->stats, "# PUT requests", 0, GNUNET_NO);
729   GNUNET_STATISTICS_set ( env->stats, "# GET requests", 0, GNUNET_NO);
730
731   if ( ((NULL == http_daemon_v4) && (NULL == http_daemon_v6)) || (NULL == curl_multi))
732   {
733     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Initializing http plugin failed\n");
734     libgnunet_plugin_transport_http_done (api);
735     return NULL;
736   }
737   else
738     return api;
739 }
740
741 /* end of plugin_transport_http.c */