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