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