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