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