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