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