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