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