fixing memory leak
[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 3, 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_http.c
23  * @brief http transport service plugin
24  * @author Matthias Wachs
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_connection_lib.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_container_lib.h"
38 #include "gnunet_transport_plugin.h"
39 #include "gnunet_os_lib.h"
40 #include "microhttpd.h"
41 #include <curl/curl.h>
42
43 #if BUILD_HTTPS
44 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
45 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
46 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_https
47 #define PROTOCOL_PREFIX "https"
48 #else
49 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
50 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
51 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_http
52 #define PROTOCOL_PREFIX "http"
53 #endif
54
55 #define DEBUG_HTTP GNUNET_NO
56 #define DEBUG_CURL GNUNET_NO
57 #define DEBUG_MHD GNUNET_NO
58 #define DEBUG_CONNECTIONS GNUNET_NO
59 #define DEBUG_SESSION_SELECTION GNUNET_NO
60 #define DEBUG_SCHEDULING GNUNET_NO
61 #define CURL_TCP_NODELAY GNUNET_YES
62
63 #define INBOUND GNUNET_NO
64 #define OUTBOUND GNUNET_YES
65
66
67
68 /**
69  * Text of the response sent back after the last bytes of a PUT
70  * request have been received (just to formally obey the HTTP
71  * protocol).
72  */
73 #define HTTP_PUT_RESPONSE "Thank you!"
74
75 /**
76  * After how long do we expire an address that we
77  * learned from another peer if it is not reconfirmed
78  * by anyone?
79  */
80 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
81
82 /**
83  * Page returned if request invalid
84  */
85 #define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
86
87 /**
88  * Timeout for a http connect
89  */
90 #define HTTP_CONNECT_TIMEOUT 30
91
92
93 /**
94  * Network format for IPv4 addresses.
95  */
96 struct IPv4HttpAddress
97 {
98   /**
99    * Linked list next
100    */
101   struct IPv4HttpAddress * next;
102
103   /**
104    * Linked list previous
105    */
106   struct IPv4HttpAddress * prev;
107
108   /**
109    * IPv4 address, in network byte order.
110    */
111   uint32_t ipv4_addr GNUNET_PACKED;
112
113   /**
114    * Port number, in network byte order.
115    */
116   uint16_t u_port GNUNET_PACKED;
117
118 };
119
120
121 /**
122  * Network format for IPv6 addresses.
123  */
124 struct IPv6HttpAddress
125 {
126   /**
127    * Linked list next
128    */
129   struct IPv6HttpAddress * next;
130
131   /**
132    * Linked list previous
133    */
134   struct IPv6HttpAddress * prev;
135
136   /**
137    * IPv6 address.
138    */
139   struct in6_addr ipv6_addr GNUNET_PACKED;
140
141   /**
142    * Port number, in network byte order.
143    */
144   uint16_t u6_port GNUNET_PACKED;
145
146 };
147
148
149 /**
150  *  Message to send using http
151  */
152 struct HTTP_Message
153 {
154   /**
155    * next pointer for double linked list
156    */
157   struct HTTP_Message * next;
158
159   /**
160    * previous pointer for double linked list
161    */
162   struct HTTP_Message * prev;
163
164   /**
165    * buffer containing data to send
166    */
167   char *buf;
168
169   /**
170    * amount of data already sent
171    */
172   size_t pos;
173
174   /**
175    * buffer length
176    */
177   size_t size;
178
179   /**
180    * Continuation function to call once the transmission buffer
181    * has again space available.  NULL if there is no
182    * continuation to call.
183    */
184   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
185
186   /**
187    * Closure for transmit_cont.
188    */
189   void *transmit_cont_cls;
190 };
191
192
193 struct HTTP_PeerContext
194 {
195   /**
196    * peer's identity
197    */
198   struct GNUNET_PeerIdentity identity;
199
200   /**
201    * Pointer to the global plugin struct.
202    */
203   struct Plugin *plugin;
204
205   /**
206    * Linked list of connections with this peer
207    * head
208    */
209   struct Session * head;
210
211   /**
212    * Linked list of connections with this peer
213    * tail
214    */
215   struct Session * tail;
216
217   /**
218    * id for next session
219    */
220   size_t session_id_counter;
221
222   /**
223    * Last session used to send data
224    */
225   struct Session * last_session;
226
227   /**
228    * The task resetting inbound quota delay
229    */
230   GNUNET_SCHEDULER_TaskIdentifier reset_task;
231
232   /**
233    * Delay from transport service inbound quota tracker when to receive data again
234    */
235   struct GNUNET_TIME_Relative delay;
236 };
237
238
239 struct Session
240 {
241   /**
242    * API requirement.
243    */
244   struct SessionHeader header;
245
246   /**
247    * next session in linked list
248    */
249   struct Session * next;
250
251   /**
252    * previous session in linked list
253    */
254   struct Session * prev;
255
256   /**
257    * address of this session
258    */
259   void * addr;
260
261   /**
262    * address length
263    */
264   size_t addrlen;
265
266   /**
267    * target url
268    */
269   char * url;
270
271   /**
272    * Message queue for outbound messages
273    * head of queue
274    */
275   struct HTTP_Message * pending_msgs_head;
276
277   /**
278    * Message queue for outbound messages
279    * tail of queue
280    */
281   struct HTTP_Message * pending_msgs_tail;
282
283   /**
284    * partner peer this connection belongs to
285    */
286   struct HTTP_PeerContext * peercontext;
287
288   /**
289    * message stream tokenizer for incoming data
290    */
291   struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
292
293   /**
294    * session direction
295    * outbound: OUTBOUND (GNUNET_YES)
296    * inbound : INBOUND (GNUNET_NO)
297    */
298   unsigned int direction;
299
300   /**
301    * is session connected to send data?
302    */
303   unsigned int send_connected;
304
305   /**
306    * is send connection active?
307    */
308   unsigned int send_active;
309
310   /**
311    * connection disconnect forced (e.g. from transport)
312    */
313   unsigned int send_force_disconnect;
314
315   /**
316    * is session connected to receive data?
317    */
318   unsigned int recv_connected;
319
320   /**
321    * is receive connection active?
322    */
323   unsigned int recv_active;
324
325   /**
326    * connection disconnect forced (e.g. from transport)
327    */
328   unsigned int recv_force_disconnect;
329
330
331   /**
332    * id for next session
333    * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
334    */
335   size_t session_id;
336
337   /**
338    * entity managing sending data
339    * outbound session: CURL *
340    * inbound session: mhd_connection *
341    */
342   void * send_endpoint;
343
344   /**
345    * entity managing recieving data
346    * outbound session: CURL *
347    * inbound session: mhd_connection *
348    */
349   void * recv_endpoint;
350
351   /**
352    * Current queue size
353    */
354   size_t queue_length_cur;
355
356   /**
357         * Max queue size
358         */
359   size_t queue_length_max;
360
361 };
362
363 /**
364  * Encapsulation of all of the state of the plugin.
365  */
366 struct Plugin
367 {
368   /**
369    * Our environment.
370    */
371   struct GNUNET_TRANSPORT_PluginEnvironment *env;
372
373   /**
374    * Handle for reporting statistics.
375    */
376   struct GNUNET_STATISTICS_Handle *stats;
377
378   /**
379    * Plugin Port
380    */
381   uint16_t port_inbound;
382
383   struct GNUNET_CONTAINER_MultiHashMap *peers;
384
385   /**
386    * Daemon for listening for new IPv4 connections.
387    */
388   struct MHD_Daemon *http_server_daemon_v4;
389
390   /**
391    * Daemon for listening for new IPv6connections.
392    */
393   struct MHD_Daemon *http_server_daemon_v6;
394
395   /**
396    * Our primary task for http daemon handling IPv4 connections
397    */
398   GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
399
400   /**
401    * Our primary task for http daemon handling IPv6 connections
402    */
403   GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
404
405   /**
406    * The task sending data
407    */
408   GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
409
410   /**
411    * cURL Multihandle
412    */
413   CURLM * multi_handle;
414
415   /**
416    * ipv4 DLL head
417    */
418   struct IPv4HttpAddress * ipv4_addr_head;
419
420   /**
421    * ipv4 DLL tail
422    */
423   struct IPv4HttpAddress * ipv4_addr_tail;
424
425   /**
426    * ipv6 DLL head
427    */
428   struct IPv6HttpAddress * ipv6_addr_head;
429
430   /**
431    * ipv6 DLL tail
432    */
433   struct IPv6HttpAddress * ipv6_addr_tail;
434
435   /**
436    * Our ASCII encoded, hashed peer identity
437    * This string is used to distinguish between connections and is added to the urls
438    */
439   struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
440
441   /**
442    * IPv4 Address the plugin binds to
443    */
444   struct sockaddr_in * bind4_address;
445
446   /**
447    * IPv6 Address the plugins binds to
448    */
449   struct sockaddr_in6 * bind6_address;
450
451   /**
452    * Hostname to bind to
453    */
454   char * bind_hostname;
455
456   /**
457    * Is IPv4 enabled?
458    */
459   int use_ipv6;
460
461   /**
462    * Is IPv6 enabled?
463    */
464   int use_ipv4;
465
466   /**
467    * use local addresses?
468    */
469   int use_localaddresses;
470
471   /**
472    * maximum number of connections
473    */
474   int max_connect_per_transport;
475
476   /**
477    * Current number of connections;
478    */
479   int current_connections;
480
481   /**
482    * Closure passed by MHD to the mhd_logger function
483    */
484   void * mhd_log;
485
486   /* only needed for HTTPS plugin */
487 #if BUILD_HTTPS
488   /* The certificate MHD uses as an \0 terminated string */
489   char * cert;
490
491   /* The private key MHD uses as an \0 terminated string */
492   char * key;
493
494   /* crypto init string */
495   char * crypto_init;
496 #endif
497 };
498
499
500 /**
501  * Function called for a quick conversion of the binary address to
502  * a numeric address.  Note that the caller must not free the
503  * address and that the next call to this function is allowed
504  * to override the address again.
505  *
506  * @param cls closure
507  * @param addr binary address
508  * @param addrlen length of the address
509  * @return string representing the same address
510  */
511 static const char*
512 http_plugin_address_to_string (void *cls,
513                                    const void *addr,
514                                    size_t addrlen);
515
516
517 /**
518  * Call MHD to process pending ipv4 requests and then go back
519  * and schedule the next run.
520  */
521 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
522 /**
523  * Call MHD to process pending ipv6 requests and then go back
524  * and schedule the next run.
525  */
526 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
527
528 /**
529  * Function setting up curl handle and selecting message to send
530  *
531  * @param plugin plugin
532  * @param ps session
533  * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
534  */
535 static int send_check_connections (struct Plugin *plugin, struct Session *ps);
536
537 /**
538  * Function setting up file descriptors and scheduling task to run
539  *
540  * @param  plugin plugin as closure
541  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
542  */
543 static int curl_schedule (struct Plugin *plugin);
544
545 /**
546  * Task scheduled to reset the inbound quota delay for a specific peer
547  * @param cls plugin as closure
548  * @param tc task context
549  */
550 static void reset_inbound_quota_delay (void *cls,
551                                        const struct GNUNET_SCHEDULER_TaskContext *tc)
552 {
553   struct HTTP_PeerContext * pc = cls;
554   
555   GNUNET_assert(cls != NULL);
556   pc->reset_task = GNUNET_SCHEDULER_NO_TASK;
557   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
558     return;
559   pc->delay = GNUNET_TIME_relative_get_zero ();
560 }
561
562
563 /**
564  * Creates a valid url from passed address and id
565  * @param plugin plugin
566  * @param addr address to create url from
567  * @param addrlen address lenth
568  * @param id session id
569  * @return the created url
570  */
571 static char * 
572 create_url(struct Plugin *plugin, 
573            const void * addr, size_t addrlen, 
574            size_t id)
575 {
576   char *url = NULL;
577   char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
578
579   GNUNET_assert ((addr!=NULL) && (addrlen != 0));
580   GNUNET_asprintf(&url,
581                   "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
582                   (char *) (&plugin->my_ascii_hash_ident),id);
583   return url;
584 }
585
586
587 /**
588  * Removes a message from the linked list of messages
589  * @param ps session
590  * @param msg message
591  * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
592  */
593 static int 
594 remove_http_message (struct Session * ps, 
595                      struct HTTP_Message * msg)
596 {
597   GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
598                               ps->pending_msgs_tail,
599                               msg);
600   GNUNET_free(msg);
601   return GNUNET_OK;
602 }
603
604 /**
605  * Iterator to remove peer context
606  * @param cls the plugin
607  * @param key the peers public key hashcode
608  * @param value the peer context
609  * @return GNUNET_YES on success
610  */
611 static int 
612 remove_peer_context_Iterator (void *cls,
613                               const GNUNET_HashCode *key, 
614                               void *value)
615 {
616   struct Plugin *plugin = cls;
617   struct HTTP_PeerContext * pc = value;
618   struct Session * ps = pc->head;
619   struct Session * tmp = NULL;
620   struct HTTP_Message * msg = NULL;
621   struct HTTP_Message * msg_tmp = NULL;
622
623 #if DEBUG_HTTP
624   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625               "Freeing context for peer `%s'\n",
626               GNUNET_i2s(&pc->identity));
627 #endif
628   GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
629   while (ps!=NULL)
630     {
631       plugin->env->session_end(plugin, &pc->identity, ps);
632       tmp = ps->next;
633       
634       GNUNET_free_non_null (ps->addr);
635       GNUNET_free(ps->url);
636       if (ps->msgtok != NULL)
637         GNUNET_SERVER_mst_destroy (ps->msgtok);
638       
639       msg = ps->pending_msgs_head;
640       while (msg!=NULL)
641         {
642           msg_tmp = msg->next;
643           GNUNET_free(msg);
644           msg = msg_tmp;
645         }
646       if (ps->direction==OUTBOUND)
647         {
648           if (ps->send_endpoint!=NULL)
649             curl_easy_cleanup(ps->send_endpoint);
650           if (ps->recv_endpoint!=NULL)
651             curl_easy_cleanup(ps->recv_endpoint);
652         }      
653       GNUNET_free(ps);
654       ps=tmp;
655     }
656   GNUNET_free(pc);
657   GNUNET_STATISTICS_update (plugin->env->stats,
658                             gettext_noop ("# HTTP peers active"),
659                             -1,
660                             GNUNET_NO);
661   return GNUNET_YES;
662 }
663
664
665 /**
666  * Removes a session from the linked list of sessions
667  * @param pc peer context
668  * @param ps session
669  * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
670  * @param call_msg_cont_result result to call message continuations with
671  * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
672  */
673 static int 
674 remove_session (struct HTTP_PeerContext * pc, 
675                 struct Session * ps,  
676                 int call_msg_cont, 
677                 int call_msg_cont_result)
678 {
679   struct HTTP_Message * msg;
680   struct Plugin * plugin = ps->peercontext->plugin;
681
682 #if DEBUG_CONNECTIONS
683   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684               "Connection %X: removing %s session %X with id %u\n", 
685               ps,
686               (ps->direction == INBOUND) 
687               ? "inbound" 
688               : "outbound", 
689               ps, ps->session_id);
690 #endif
691   plugin->env->session_end(plugin, &pc->identity, ps);
692   GNUNET_free_non_null (ps->addr);
693   GNUNET_SERVER_mst_destroy (ps->msgtok);
694   GNUNET_free(ps->url);
695   if (ps->direction==INBOUND)
696     {
697       if (ps->recv_endpoint != NULL)
698         {
699           curl_easy_cleanup(ps->recv_endpoint);
700           ps->recv_endpoint = NULL;
701         }
702       if (ps->send_endpoint != NULL)
703         {
704           curl_easy_cleanup(ps->send_endpoint);
705           ps->send_endpoint = NULL;
706         }
707     }
708   
709   msg = ps->pending_msgs_head;
710   while (msg!=NULL)
711     {
712       if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
713         {
714           msg->transmit_cont (msg->transmit_cont_cls,
715                               &pc->identity,
716                               call_msg_cont_result);
717         }
718       GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
719                                   ps->pending_msgs_head,
720                                   msg);
721       GNUNET_free(msg);
722       msg = ps->pending_msgs_head;
723     }
724   
725   GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
726   GNUNET_free(ps);
727   ps = NULL;
728
729   /* no sessions left remove peer */
730   if (pc->head==NULL)
731     {
732 #if DEBUG_HTTP
733       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734                   "No sessions left for peer `%s', removing context\n",
735                   GNUNET_i2s(&pc->identity));
736 #endif
737       remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
738     }
739   
740   return GNUNET_OK;
741 }
742
743 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
744 {
745         uint32_t res = 0;
746         int local = GNUNET_NO;
747         int af = addr->sa_family;
748     switch (af)
749     {
750       case AF_INET:
751       {
752           uint32_t netmask = 0x7F000000;
753           uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
754           res = (address >> 24) ^ (netmask >> 24);
755           if (res != 0)
756                   local = GNUNET_NO;
757           else
758                   local = GNUNET_YES;
759 #if DEBUG_HTTP
760             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761                           "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
762 #endif
763             break;
764       }
765       case AF_INET6:
766       {
767            if (IN6_IS_ADDR_LOOPBACK  (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
768                    IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
769                    local = GNUNET_YES;
770            else
771                    local = GNUNET_NO;
772 #if DEBUG_HTTP
773            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774                           "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
775 #endif
776            break;
777       }
778     }
779         return local;
780 }
781
782 /**
783  * Add the IP of our network interface to the list of
784  * our external IP addresses.
785  *
786  * @param cls the 'struct Plugin*'
787  * @param name name of the interface
788  * @param isDefault do we think this may be our default interface
789  * @param addr address of the interface
790  * @param addrlen number of bytes in addr
791  * @return GNUNET_OK to continue iterating
792  */
793 static int
794 process_interfaces (void *cls,
795                     const char *name,
796                     int isDefault,
797                     const struct sockaddr *addr, socklen_t addrlen)
798 {
799   struct Plugin *plugin = cls;
800   struct IPv4HttpAddress * t4;
801   struct IPv6HttpAddress * t6;
802   int af;
803
804   if (plugin->use_localaddresses == GNUNET_NO)
805   {
806           if (GNUNET_YES == check_localaddress (addr, addrlen))
807           {
808 #if DEBUG_HTTP
809           GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
810                    PROTOCOL_PREFIX,
811                            "Not notifying transport of address `%s' (local address)\n",
812                            GNUNET_a2s (addr, addrlen));
813 #endif
814                   return GNUNET_OK;
815           }
816   }
817
818
819   GNUNET_assert(cls !=NULL);
820   af = addr->sa_family;
821   if ((af == AF_INET) &&
822       (plugin->use_ipv4 == GNUNET_YES) &&
823       (plugin->bind6_address == NULL) ) {
824
825           struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
826       t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
827      // Not skipping loopback addresses
828
829
830       t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
831       t4->u_port = htons (plugin->port_inbound);
832       if (plugin->bind4_address != NULL) {
833         if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
834           {
835             GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
836                                         plugin->ipv4_addr_tail,t4);
837                   plugin->env->notify_address(plugin->env->cls,
838                                               GNUNET_YES,
839                                               t4, sizeof (struct IPv4HttpAddress));
840             return GNUNET_OK;
841           }
842         GNUNET_free (t4);
843         return GNUNET_OK;
844       }
845       else
846           {
847           GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
848                                        plugin->ipv4_addr_tail,
849                                        t4);
850           plugin->env->notify_address(plugin->env->cls,
851                                       GNUNET_YES,
852                                       t4, sizeof (struct IPv4HttpAddress));
853           return GNUNET_OK;
854           }
855    }
856    if ((af == AF_INET6) &&
857             (plugin->use_ipv6 == GNUNET_YES) && 
858             (plugin->bind4_address == NULL) ) {
859
860           struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
861
862       t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
863       GNUNET_assert(t6 != NULL);
864
865       if (plugin->bind6_address != NULL) {
866           if (0 == memcmp(&plugin->bind6_address->sin6_addr,
867                                                   &bnd_cmp6,
868                                                  sizeof (struct in6_addr))) {
869               memcpy (&t6->ipv6_addr,
870                       &((struct sockaddr_in6 *) addr)->sin6_addr,
871                       sizeof (struct in6_addr));
872               t6->u6_port = htons (plugin->port_inbound);
873               plugin->env->notify_address(plugin->env->cls,
874                                           GNUNET_YES,
875                                           t6, sizeof (struct IPv6HttpAddress));
876               GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
877                                           plugin->ipv6_addr_tail,
878                                           t6);
879               return GNUNET_OK;
880               }
881           GNUNET_free (t6);
882           return GNUNET_OK;
883           }
884       memcpy (&t6->ipv6_addr,
885                   &((struct sockaddr_in6 *) addr)->sin6_addr,
886                   sizeof (struct in6_addr));
887       t6->u6_port = htons (plugin->port_inbound);
888       GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
889
890       plugin->env->notify_address(plugin->env->cls,
891                                   GNUNET_YES,
892                                   t6, sizeof (struct IPv6HttpAddress));
893   }
894   return GNUNET_OK;
895 }
896
897
898 /**
899  * External logging function for MHD
900  * @param arg arguments
901  * @param fmt format string
902  * @param ap  list of arguments
903  */
904 static void 
905 mhd_logger (void * arg, 
906             const char * fmt, 
907             va_list ap)
908 {
909   char text[1024];
910
911   vsnprintf(text, sizeof(text), fmt, ap);
912   va_end(ap);
913   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
914               "MHD: %s\n", 
915               text);
916 }
917
918
919 static void 
920 mhd_termination_cb (void *cls, 
921                     struct MHD_Connection * connection, 
922                     void **httpSessionCache)
923 {
924   struct Session * ps = *httpSessionCache;
925   if (ps == NULL)
926     return;
927   struct HTTP_PeerContext * pc = ps->peercontext;
928   struct Plugin *plugin = cls;
929
930   GNUNET_assert (cls != NULL);
931     plugin->current_connections--;
932
933   if (connection==ps->recv_endpoint)
934     {
935 #if DEBUG_CONNECTIONS
936       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937                   "Connection %X: inbound connection from peer `%s' was terminated\n", 
938                   ps, 
939                   GNUNET_i2s(&pc->identity));
940 #endif
941       ps->recv_active = GNUNET_NO;
942       ps->recv_connected = GNUNET_NO;
943       ps->recv_endpoint = NULL;
944     }
945   if (connection==ps->send_endpoint)
946     {
947       ps->send_active = GNUNET_NO;
948       ps->send_connected = GNUNET_NO;
949       ps->send_endpoint = NULL;
950 #if DEBUG_CONNECTIONS
951       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952                   "Connection %X: outbound connection from peer `%s' was terminated\n",
953                   ps, 
954                   GNUNET_i2s(&pc->identity));
955 #endif
956     }
957
958   /* if both connections disconnected, remove session */
959   if ( (ps->send_connected == GNUNET_NO) && 
960        (ps->recv_connected == GNUNET_NO) )
961   {
962     GNUNET_STATISTICS_update (pc->plugin->env->stats,
963                               gettext_noop ("# HTTP inbound sessions for peers active"),
964                               -1,
965                               GNUNET_NO);
966     remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
967   }
968 }
969
970
971 /**
972  * Callback called by MessageStreamTokenizer when a message has arrived
973  * @param cls current session as closure
974  * @param client clien
975  * @param message the message to be forwarded to transport service
976  */
977 static void 
978 mhd_write_mst_cb (void *cls,
979                   void *client,
980                   const struct GNUNET_MessageHeader *message)
981 {
982   struct Session *ps  = cls; 
983   struct HTTP_PeerContext *pc = ps->peercontext;
984   struct GNUNET_TIME_Relative delay;
985
986   GNUNET_assert(ps != NULL);
987   GNUNET_assert(pc != NULL);
988 #if DEBUG_HTTP
989   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990               "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
991               ps,
992               ntohs(message->type),
993               ntohs(message->size),
994               GNUNET_i2s(&(ps->peercontext)->identity),
995               http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
996 #endif
997   struct GNUNET_TRANSPORT_ATS_Information distance[2];
998   distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
999   distance[0].value = htonl (1);
1000   distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1001   distance[1].value = htonl (0);
1002   delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1003                                     &pc->identity,
1004                                     message,
1005                                     (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1006                                     2,
1007                                     ps,
1008                                     NULL,
1009                                     0);
1010   pc->delay = delay;
1011   if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1012     GNUNET_SCHEDULER_cancel (pc->reset_task);
1013   
1014   if (delay.rel_value > 0)
1015     {
1016 #if DEBUG_HTTP
1017       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018                   "Connection %X: Inbound quota management: delay next read for %llu ms \n", 
1019                   ps,
1020                   delay.rel_value);
1021 #endif
1022       pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1023     }
1024 }
1025
1026
1027 /**
1028  * Check if incoming connection is accepted.
1029  * NOTE: Here every connection is accepted
1030  * @param cls plugin as closure
1031  * @param addr address of incoming connection
1032  * @param addr_len address length of incoming connection
1033  * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1034  *
1035  */
1036 static int
1037 mhd_accept_cb (void *cls,
1038                const struct sockaddr *addr, 
1039                socklen_t addr_len)
1040 {
1041   struct Plugin *plugin = cls;
1042   GNUNET_assert (cls != NULL);
1043
1044   if (plugin->max_connect_per_transport > plugin->current_connections)
1045   {
1046     plugin->current_connections ++;
1047     return MHD_YES;
1048   }
1049   else return MHD_NO;
1050 }
1051
1052
1053 /**
1054  * Callback called by MHD when it needs data to send
1055  * @param cls current session
1056  * @param pos position in buffer
1057  * @param buf the buffer to write data to
1058  * @param max max number of bytes available in buffer
1059  * @return bytes written to buffer
1060  */
1061 static ssize_t
1062 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1063 {
1064   struct Session * ps = cls;
1065   struct HTTP_PeerContext * pc;
1066   struct HTTP_Message * msg;
1067   int bytes_read = 0;
1068
1069   GNUNET_assert (ps!=NULL);
1070
1071   pc = ps->peercontext;
1072   msg = ps->pending_msgs_tail;
1073   if (ps->send_force_disconnect==GNUNET_YES)
1074     {
1075 #if DEBUG_CONNECTIONS
1076       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077                   "Connection %X: outbound forced to disconnect\n",
1078                   ps);
1079 #endif
1080       return -1;
1081     }
1082   
1083   if (msg!=NULL)
1084     {
1085       /* sending */
1086       if ((msg->size-msg->pos) <= max)
1087         {
1088           memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
1089           bytes_read = msg->size-msg->pos;
1090           msg->pos+=(msg->size-msg->pos);
1091         }
1092       else
1093         {
1094           memcpy(buf,&msg->buf[msg->pos],max);
1095           msg->pos+=max;
1096           bytes_read = max;
1097         }
1098       
1099       /* removing message */
1100       if (msg->pos==msg->size)
1101         {
1102           if (NULL!=msg->transmit_cont)
1103             msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1104           ps->queue_length_cur -= msg->size;
1105           remove_http_message(ps,msg);
1106         }
1107     }
1108 #if DEBUG_CONNECTIONS
1109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110               "Connection %X: MHD has sent %u bytes\n", 
1111               ps, 
1112               bytes_read);
1113 #endif
1114   return bytes_read;
1115 }
1116
1117
1118 /**
1119  * Process GET or PUT request received via MHD.  For
1120  * GET, queue response that will send back our pending
1121  * messages.  For PUT, process incoming data and send
1122  * to GNUnet core.  In either case, check if a session
1123  * already exists and create a new one if not.
1124  */
1125 static int
1126 mhd_access_cb (void *cls,
1127                struct MHD_Connection *mhd_connection,
1128                const char *url,
1129                const char *method,
1130                const char *version,
1131                const char *upload_data,
1132                size_t * upload_data_size,
1133                void **httpSessionCache)
1134 {
1135   struct Plugin *plugin = cls;
1136   struct MHD_Response *response;
1137   const union MHD_ConnectionInfo * conn_info;
1138   const struct sockaddr *client_addr;
1139   const struct sockaddr_in  *addrin;
1140   const struct sockaddr_in6 *addrin6;
1141   char address[INET6_ADDRSTRLEN+14];
1142   struct GNUNET_PeerIdentity pi_in;
1143   size_t id_num = 0;
1144   struct IPv4HttpAddress ipv4addr;
1145   struct IPv6HttpAddress ipv6addr;
1146   struct HTTP_PeerContext *pc = NULL;
1147   struct Session *ps = NULL;
1148   struct Session *ps_tmp = NULL;
1149   int res = GNUNET_NO;
1150   void * addr = NULL;
1151   size_t addr_len = 0 ;
1152
1153   GNUNET_assert(cls !=NULL);
1154
1155   if (NULL == *httpSessionCache)
1156     {
1157       /* check url for peer identity , if invalid send HTTP 404*/
1158       size_t len = strlen(&url[1]);
1159       char * peer = GNUNET_malloc(104+1);
1160       
1161       if ( (len>104) && (url[104]==';'))
1162         {
1163           char * id = GNUNET_malloc((len-104)+1);
1164           strcpy(id,&url[105]);
1165           memcpy(peer,&url[1],103);
1166           peer[103] = '\0';
1167         id_num = strtoul ( id, NULL , 10);
1168         GNUNET_free(id);
1169     }
1170     res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1171     GNUNET_free(peer);
1172     if ( GNUNET_SYSERR == res )
1173       {
1174         response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1175                                                   HTTP_ERROR_RESPONSE,
1176                                                   MHD_NO, MHD_NO);
1177         res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1178         MHD_destroy_response (response);
1179 #if DEBUG_CONNECTIONS
1180       if (res == MHD_YES)
1181         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182                     "Peer has no valid ident, sent HTTP 1.1/404\n");
1183       else
1184         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185                     "Peer has no valid ident, could not send error\n");
1186 #endif
1187       return res;
1188       }
1189     }
1190   else
1191     {
1192       ps = *httpSessionCache;
1193       pc = ps->peercontext;
1194     }
1195   
1196   if (NULL == *httpSessionCache)
1197     {
1198       /* get peer context */
1199       pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1200       /* Peer unknown */
1201       if (pc==NULL)
1202         {
1203           pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1204           pc->plugin = plugin;
1205           pc->session_id_counter=1;
1206           pc->last_session = NULL;
1207           memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1208           GNUNET_CONTAINER_multihashmap_put(plugin->peers, 
1209                                             &pc->identity.hashPubKey,
1210                                             pc, 
1211                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1212           GNUNET_STATISTICS_update (plugin->env->stats,
1213                                     gettext_noop ("# HTTP peers active"),
1214                                     1,
1215                                     GNUNET_NO);
1216         }
1217
1218       conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1219       /* Incoming IPv4 connection */
1220       /* cast required for legacy MHD API < 0.9.6 */
1221       client_addr = (const struct sockaddr *) conn_info->client_addr;
1222       if ( AF_INET == client_addr->sa_family)
1223         {
1224           addrin = (const struct sockaddr_in*) client_addr;
1225           inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1226           memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1227           ipv4addr.u_port = addrin->sin_port;
1228           addr = &ipv4addr;
1229           addr_len = sizeof(struct IPv4HttpAddress);
1230         }
1231       /* Incoming IPv6 connection */
1232       if ( AF_INET6 == client_addr->sa_family)
1233         {
1234           addrin6 = (const struct sockaddr_in6 *) client_addr;
1235           inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1236           memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1237           ipv6addr.u6_port = addrin6->sin6_port;
1238           addr = &ipv6addr;
1239           addr_len = sizeof(struct IPv6HttpAddress);
1240         }
1241       
1242       GNUNET_assert (addr != NULL);
1243       GNUNET_assert (addr_len != 0);
1244       
1245       ps = NULL;
1246       /* only inbound sessions here */
1247       
1248       ps_tmp = pc->head;
1249       while (ps_tmp!=NULL)
1250         {
1251           if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1252             {
1253               if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1254                 ps=ps_tmp;
1255               break;
1256             }
1257           ps_tmp=ps_tmp->next;
1258         }
1259       
1260       if (ps==NULL)
1261         {
1262           ps = GNUNET_malloc(sizeof (struct Session));
1263           ps->addr = GNUNET_malloc(addr_len);
1264           memcpy(ps->addr,addr,addr_len);
1265           ps->addrlen = addr_len;
1266           ps->direction=INBOUND;
1267           ps->pending_msgs_head = NULL;
1268           ps->pending_msgs_tail = NULL;
1269           ps->send_connected=GNUNET_NO;
1270           ps->send_active=GNUNET_NO;
1271           ps->recv_connected=GNUNET_NO;
1272           ps->recv_active=GNUNET_NO;
1273           ps->peercontext=pc;
1274           ps->session_id =id_num;
1275           ps->queue_length_cur = 0;
1276           ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1277           ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1278           GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1279           GNUNET_STATISTICS_update (plugin->env->stats,
1280                                     gettext_noop ("# HTTP inbound sessions for peers active"),
1281                                     1,
1282                                     GNUNET_NO);
1283         }
1284       
1285       *httpSessionCache = ps;
1286       if (ps->msgtok==NULL)
1287         ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1288 #if DEBUG_HTTP
1289       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290                   "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1291                   ps,
1292                   method,
1293                   GNUNET_i2s(&pc->identity),
1294                   http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1295 #endif
1296     }
1297   
1298   /* Is it a PUT or a GET request */
1299   if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1300     {
1301       if (ps->recv_force_disconnect == GNUNET_YES)
1302         {
1303 #if DEBUG_CONNECTIONS
1304           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305                       "Connection %X: inbound connection was forced to disconnect\n",ps);
1306 #endif
1307           ps->recv_active = GNUNET_NO;
1308           return MHD_NO;
1309         }
1310       if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1311         {
1312           ps->recv_endpoint = mhd_connection;
1313           ps->recv_connected = GNUNET_YES;
1314           ps->recv_active = GNUNET_YES;
1315           ps->recv_force_disconnect = GNUNET_NO;
1316 #if DEBUG_CONNECTIONS
1317           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318                       "Connection %X: inbound PUT connection connected\n",ps);
1319 #endif
1320           return MHD_YES;
1321         }
1322       
1323       /* Transmission of all data complete */
1324       if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1325         {
1326           response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1327                                                     HTTP_PUT_RESPONSE, 
1328                                                     MHD_NO, MHD_NO);
1329           res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1330 #if DEBUG_CONNECTIONS
1331           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332                       "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",
1333                       ps);
1334 #endif
1335           MHD_destroy_response (response);
1336           ps->recv_active=GNUNET_NO;
1337           return MHD_YES;
1338         }
1339       
1340       /* Recieving data */
1341       if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1342         {
1343           if (pc->delay.rel_value == 0)
1344             {
1345 #if DEBUG_HTTP
1346               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347                           "Connection %X: PUT with %u bytes forwarded to MST\n", 
1348                           ps, *upload_data_size);
1349 #endif
1350               res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, 
1351                                               upload_data, *upload_data_size, 
1352                                               GNUNET_NO, GNUNET_NO);
1353               (*upload_data_size) = 0;
1354             }
1355           else
1356             {
1357 #if DEBUG_HTTP
1358               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1359                           "Connection %X: no inbound bandwidth available! Next read was delayed for  %llu ms\n", 
1360                           ps, 
1361                           ps->peercontext->delay.rel_value);
1362 #endif
1363             }
1364           return MHD_YES;
1365         }
1366       else
1367         return MHD_NO;
1368     }
1369   if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1370     {
1371       if (ps->send_force_disconnect == GNUNET_YES)
1372         {
1373 #if DEBUG_CONNECTIONS
1374           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375                       "Connection %X: outbound connection was  forced to disconnect\n",
1376                       ps);
1377 #endif
1378           ps->send_active = GNUNET_NO;
1379           return MHD_NO;
1380         }
1381       ps->send_connected = GNUNET_YES;
1382       ps->send_active = GNUNET_YES;
1383       ps->send_endpoint = mhd_connection;
1384       ps->send_force_disconnect = GNUNET_NO;
1385 #if DEBUG_CONNECTIONS
1386       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1387                   "Connection %X: inbound GET connection connected\n",
1388                   ps);
1389 #endif
1390       response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1391       res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1392       MHD_destroy_response (response);
1393       return MHD_YES;
1394     }
1395   return MHD_NO;
1396 }
1397
1398
1399 /**
1400  * Function that queries MHD's select sets and
1401  * starts the task waiting for them.
1402  * @param plugin plugin
1403  * @param daemon_handle the MHD daemon handle
1404  * @return gnunet task identifier
1405  */
1406 static GNUNET_SCHEDULER_TaskIdentifier
1407 http_server_daemon_prepare (struct Plugin *plugin,
1408                             struct MHD_Daemon *daemon_handle)
1409 {
1410   GNUNET_SCHEDULER_TaskIdentifier ret;
1411   fd_set rs;
1412   fd_set ws;
1413   fd_set es;
1414   struct GNUNET_NETWORK_FDSet *wrs;
1415   struct GNUNET_NETWORK_FDSet *wws;
1416   struct GNUNET_NETWORK_FDSet *wes;
1417   int max;
1418   unsigned long long timeout;
1419   int haveto;
1420   struct GNUNET_TIME_Relative tv;
1421
1422   ret = GNUNET_SCHEDULER_NO_TASK;
1423   FD_ZERO(&rs);
1424   FD_ZERO(&ws);
1425   FD_ZERO(&es);
1426   wrs = GNUNET_NETWORK_fdset_create ();
1427   wes = GNUNET_NETWORK_fdset_create ();
1428   wws = GNUNET_NETWORK_fdset_create ();
1429   max = -1;
1430   GNUNET_assert (MHD_YES ==
1431                  MHD_get_fdset (daemon_handle,
1432                                 &rs,
1433                                 &ws,
1434                                 &es,
1435                                 &max));
1436   haveto = MHD_get_timeout (daemon_handle, &timeout);
1437   if (haveto == MHD_YES)
1438     tv.rel_value = (uint64_t) timeout;
1439   else
1440     tv = GNUNET_TIME_UNIT_SECONDS;
1441   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1442   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1443   GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1444   if (daemon_handle == plugin->http_server_daemon_v4)
1445     {
1446       if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1447         {
1448           GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1449           plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1450         }
1451       
1452       ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1453                                          GNUNET_SCHEDULER_NO_TASK,
1454                                          tv,
1455                                          wrs,
1456                                          wws,
1457                                          &http_server_daemon_v4_run,
1458                                          plugin);
1459     }
1460   if (daemon_handle == plugin->http_server_daemon_v6)
1461     {
1462       if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1463         {
1464           GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1465           plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1466         }
1467       
1468       ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1469                                          GNUNET_SCHEDULER_NO_TASK,
1470                                          tv,
1471                                          wrs,
1472                                          wws,
1473                                          &http_server_daemon_v6_run,
1474                                          plugin);
1475     }
1476   GNUNET_NETWORK_fdset_destroy (wrs);
1477   GNUNET_NETWORK_fdset_destroy (wws);
1478   GNUNET_NETWORK_fdset_destroy (wes);
1479   return ret;
1480 }
1481
1482
1483 /**
1484  * Call MHD IPv4 to process pending requests and then go back
1485  * and schedule the next run.
1486  * @param cls plugin as closure
1487  * @param tc task context
1488  */
1489 static void 
1490 http_server_daemon_v4_run (void *cls,
1491                            const struct GNUNET_SCHEDULER_TaskContext *tc)
1492 {
1493   struct Plugin *plugin = cls;
1494
1495 #if DEBUG_SCHEDULING
1496   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1497     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498                 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1499   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) 
1500       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1501                   "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1502   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1503       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504                   "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1505   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1506       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507                   "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1508   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1509       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510                   "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1511 #endif              
1512       
1513   GNUNET_assert(cls !=NULL);
1514   plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1515
1516   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1517     return;
1518
1519   GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1520   plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1521  }
1522
1523
1524 /**
1525  * Call MHD IPv6 to process pending requests and then go back
1526  * and schedule the next run.
1527  * @param cls plugin as closure
1528  * @param tc task context
1529  */
1530 static void 
1531 http_server_daemon_v6_run (void *cls,
1532                            const struct GNUNET_SCHEDULER_TaskContext *tc)
1533 {
1534   struct Plugin *plugin = cls;
1535   
1536 #if DEBUG_SCHEDULING  
1537   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1538       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539                   "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1540   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) 
1541       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542                   "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1543   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1544       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545                   "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1546   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))  
1547      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548                  "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1549   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))  
1550      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551                  "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1552 #endif                                            
1553
1554   GNUNET_assert(cls !=NULL);
1555   plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1556
1557   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1558     return;
1559
1560   GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1561   plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1562 }
1563
1564
1565 static size_t 
1566 curl_get_header_cb( void *ptr, 
1567                     size_t size, size_t nmemb, 
1568                     void *stream)
1569 {
1570   struct Session * ps = stream;
1571
1572   long http_result = 0;
1573   int res;
1574   /* Getting last http result code */
1575   GNUNET_assert(NULL!=ps);
1576   if (ps->recv_connected==GNUNET_NO)
1577     {
1578       res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1579       if (CURLE_OK == res)
1580         {
1581           if (http_result == 200)
1582             {
1583               ps->recv_connected = GNUNET_YES;
1584               ps->recv_active = GNUNET_YES;
1585 #if DEBUG_CONNECTIONS
1586               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1587 #endif
1588               // Calling send_check_connections again since receive is established
1589               send_check_connections (ps->peercontext->plugin, ps);
1590             }
1591         }
1592     }
1593   
1594 #if DEBUG_CURL
1595   char * tmp;
1596   size_t len = size * nmemb;
1597   tmp = NULL;
1598   if ((size * nmemb) < SIZE_MAX)
1599     tmp = GNUNET_malloc (len+1);
1600
1601   if ((tmp != NULL) && (len > 0))
1602     {
1603       memcpy(tmp,ptr,len);
1604       if (len>=2)
1605         {
1606           if (tmp[len-2] == 13)
1607             tmp[len-2]= '\0';
1608         }
1609       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610                   "Connection %X: Header: %s\n",
1611                   ps, tmp);
1612     }
1613   GNUNET_free_non_null (tmp);
1614 #endif
1615   
1616   return size * nmemb;
1617 }
1618
1619
1620 /**
1621  * Callback called by libcurl when new headers arrive
1622  * Used to get HTTP result for curl operations
1623  * @param ptr stream to read from
1624  * @param size size of one char element
1625  * @param nmemb number of char elements
1626  * @param stream closure set by user
1627  * @return bytes read by function
1628  */
1629 static size_t 
1630 curl_put_header_cb(void *ptr, 
1631                    size_t size, 
1632                    size_t nmemb, 
1633                    void *stream)
1634 {
1635   struct Session * ps = stream;
1636
1637   char * tmp;
1638   size_t len = size * nmemb;
1639   long http_result = 0;
1640   int res;
1641
1642   /* Getting last http result code */
1643   GNUNET_assert(NULL!=ps);
1644   res = curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1645   if (CURLE_OK == res)
1646     {
1647       if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1648         {
1649           ps->send_connected = GNUNET_YES;
1650           ps->send_active = GNUNET_YES;
1651 #if DEBUG_CONNECTIONS
1652           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1653                       "Connection %X: connected to send data\n",
1654                       ps);
1655 #endif
1656         }
1657       if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1658         {
1659           ps->send_connected = GNUNET_NO;
1660           ps->send_active = GNUNET_NO;
1661 #if DEBUG_CONNECTIONS
1662           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663                       "Connection %X: sending disconnected\n",
1664                       ps);
1665 #endif
1666         }
1667     }
1668   
1669   tmp = NULL;
1670   if ((size * nmemb) < SIZE_MAX)
1671     tmp = GNUNET_malloc (len+1);
1672   
1673   if ((tmp != NULL) && (len > 0))
1674     {
1675       memcpy(tmp,ptr,len);
1676       if (len>=2)
1677         {
1678           if (tmp[len-2] == 13)
1679             tmp[len-2]= '\0';
1680         }
1681     }
1682   GNUNET_free_non_null (tmp);
1683   return size * nmemb;
1684 }
1685
1686 /**
1687  * Callback method used with libcurl
1688  * Method is called when libcurl needs to read data during sending
1689  * @param stream pointer where to write data
1690  * @param size size of an individual element
1691  * @param nmemb count of elements that can be written to the buffer
1692  * @param ptr source pointer, passed to the libcurl handle
1693  * @return bytes written to stream
1694  */
1695 static size_t 
1696 curl_send_cb(void *stream, 
1697              size_t size, size_t nmemb, 
1698              void *ptr)
1699 {
1700   struct Session * ps = ptr;
1701   struct HTTP_Message * msg = ps->pending_msgs_tail;
1702   size_t bytes_sent;
1703   size_t len;
1704
1705   if (ps->send_active == GNUNET_NO)
1706     return CURL_READFUNC_PAUSE;
1707   if ( (ps->pending_msgs_tail == NULL) && 
1708        (ps->send_active == GNUNET_YES) )
1709     {
1710 #if DEBUG_CONNECTIONS
1711       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712                   "Connection %X: No Message to send, pausing connection\n",
1713                   ps);
1714 #endif
1715       ps->send_active = GNUNET_NO;
1716     return CURL_READFUNC_PAUSE;
1717     }
1718   
1719   GNUNET_assert (msg!=NULL);
1720   
1721   /* data to send */
1722   if (msg->pos < msg->size)
1723     {
1724       /* data fit in buffer */
1725       if ((msg->size - msg->pos) <= (size * nmemb))
1726         {
1727           len = (msg->size - msg->pos);
1728           memcpy(stream, &msg->buf[msg->pos], len);
1729           msg->pos += len;
1730           bytes_sent = len;
1731         }
1732       else
1733         {
1734           len = size*nmemb;
1735           memcpy(stream, &msg->buf[msg->pos], len);
1736           msg->pos += len;
1737           bytes_sent = len;
1738         }
1739     }
1740   /* no data to send */
1741   else
1742     {
1743       bytes_sent = 0;
1744     }
1745   
1746   if ( msg->pos == msg->size)
1747     {
1748 #if DEBUG_CONNECTIONS
1749       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1750                   "Connection %X: Message with %u bytes sent, removing message from queue\n",
1751                   ps, 
1752                   msg->pos);
1753 #endif
1754       /* Calling transmit continuation  */
1755       if (NULL != ps->pending_msgs_tail->transmit_cont)
1756         msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1757                             &(ps->peercontext)->identity,
1758                             GNUNET_OK);
1759       ps->queue_length_cur -= msg->size;
1760       remove_http_message(ps, msg);
1761     }
1762   return bytes_sent;
1763 }
1764
1765
1766 static void 
1767 curl_receive_mst_cb  (void *cls,
1768                       void *client,
1769                       const struct GNUNET_MessageHeader *message)
1770 {
1771   struct Session *ps  = cls;
1772   struct GNUNET_TIME_Relative delay;
1773   GNUNET_assert(ps != NULL);
1774
1775   struct HTTP_PeerContext *pc = ps->peercontext;
1776   GNUNET_assert(pc != NULL);
1777 #if DEBUG_HTTP
1778   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1779               "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1780               ps,
1781               ntohs(message->type),
1782               ntohs(message->size),
1783               GNUNET_i2s(&(pc->identity)),
1784               http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1785 #endif
1786   struct GNUNET_TRANSPORT_ATS_Information distance[2];
1787   distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1788   distance[0].value = htonl (1);
1789   distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1790   distance[1].value = htonl (0);
1791
1792   delay = pc->plugin->env->receive (pc->plugin->env->cls,
1793                                     &pc->identity,
1794                                     message,
1795                                     (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1796                                     ps,
1797                                     ps->addr,
1798                                     ps->addrlen);
1799   pc->delay = delay;
1800   if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1801         GNUNET_SCHEDULER_cancel (pc->reset_task);
1802
1803   if (delay.rel_value > 0)
1804     {
1805 #if DEBUG_HTTP
1806       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1807                   "Connection %X: Inbound quota management: delay next read for %llu ms\n", 
1808                   ps, delay.rel_value);
1809 #endif
1810       pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1811     }
1812 }
1813
1814
1815 /**
1816 * Callback method used with libcurl
1817 * Method is called when libcurl needs to write data during sending
1818 * @param stream pointer where to write data
1819 * @param size size of an individual element
1820 * @param nmemb count of elements that can be written to the buffer
1821 * @param ptr destination pointer, passed to the libcurl handle
1822 * @return bytes read from stream
1823 */
1824 static size_t 
1825 curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1826 {
1827   struct Session * ps = ptr;
1828
1829   if (ps->peercontext->delay.rel_value > 0)
1830     {
1831 #if DEBUG_HTTP
1832       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833                   "Connection %X: no inbound bandwidth available! Next read was delayed for  %llu ms\n",
1834                   ps, ps->peercontext->delay.rel_value);
1835 #endif
1836       return 0;
1837     }  
1838 #if DEBUG_CONNECTIONS
1839   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1840               "Connection %X: %u bytes received\n",
1841               ps, size*nmemb);
1842 #endif
1843   GNUNET_SERVER_mst_receive(ps->msgtok, ps, 
1844                             stream, size*nmemb, 
1845                             GNUNET_NO, GNUNET_NO);
1846   return (size * nmemb);
1847 }
1848
1849
1850 static void 
1851 curl_handle_finished (struct Plugin *plugin)
1852 {
1853   struct Session *ps = NULL;
1854   struct HTTP_PeerContext *pc = NULL;
1855   struct CURLMsg *msg;
1856   struct HTTP_Message * cur_msg = NULL;
1857   int msgs_in_queue;
1858   char * tmp;
1859   long http_result;
1860   
1861   do
1862     {
1863       msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1864       if ((msgs_in_queue == 0) || (msg == NULL))
1865         break;
1866       /* get session for affected curl handle */
1867       GNUNET_assert ( msg->easy_handle != NULL );
1868       curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1869       ps = (struct Session *) tmp;
1870       GNUNET_assert ( ps != NULL );
1871       pc = ps->peercontext;
1872       GNUNET_assert ( pc != NULL );
1873       switch (msg->msg)
1874         {
1875         case CURLMSG_DONE:
1876           if ( (msg->data.result != CURLE_OK) &&
1877                (msg->data.result != CURLE_GOT_NOTHING) )
1878             {
1879               /* sending msg failed*/
1880               if (msg->easy_handle == ps->send_endpoint)
1881                 {
1882 #if DEBUG_CONNECTIONS
1883                   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1884                              _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1885                              ps,
1886                              GNUNET_i2s(&pc->identity),
1887                              http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1888                              "curl_multi_perform",
1889                              curl_easy_strerror (msg->data.result));
1890 #endif
1891                   ps->send_connected = GNUNET_NO;
1892                   ps->send_active = GNUNET_NO;
1893                   curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1894                   while (ps->pending_msgs_tail != NULL)
1895                     {
1896                       cur_msg = ps->pending_msgs_tail;
1897                       if ( NULL != cur_msg->transmit_cont)
1898                         cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1899                       ps->queue_length_cur -= cur_msg->size;
1900                       remove_http_message(ps,cur_msg);
1901                     }
1902                 }
1903               /* GET connection failed */
1904               if (msg->easy_handle == ps->recv_endpoint)
1905                 {
1906 #if DEBUG_CONNECTIONS
1907                   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1908                              _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1909                              ps,
1910                              GNUNET_i2s(&pc->identity),
1911                              http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1912                              "curl_multi_perform",
1913                              curl_easy_strerror (msg->data.result));
1914 #endif
1915                   ps->recv_connected = GNUNET_NO;
1916                   ps->recv_active = GNUNET_NO;
1917                   curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1918                 }
1919             }
1920           else
1921             {
1922               if (msg->easy_handle == ps->send_endpoint)
1923                 {
1924                   GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1925 #if DEBUG_CONNECTIONS
1926                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1927                               "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1928                               ps,
1929                               GNUNET_i2s(&pc->identity),
1930                               http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1931                               http_result);
1932 #endif
1933                   /* Calling transmit continuation  */
1934                   while (ps->pending_msgs_tail != NULL)
1935                     {
1936                       cur_msg = ps->pending_msgs_tail;
1937                       if ( NULL != cur_msg->transmit_cont)
1938                         {
1939                           /* HTTP 1xx : Last message before here was informational */
1940                           if ((http_result >=100) && (http_result < 200))
1941                             cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1942                           /* HTTP 2xx: successful operations */
1943                           if ((http_result >=200) && (http_result < 300))
1944                             cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1945                           /* HTTP 3xx..5xx: error */
1946                           if ((http_result >=300) && (http_result < 600))
1947                             cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1948                         }
1949                       ps->queue_length_cur -= cur_msg->size;
1950                       remove_http_message(ps,cur_msg);
1951                     }
1952                   
1953                   ps->send_connected = GNUNET_NO;
1954                   ps->send_active = GNUNET_NO;
1955                   curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1956                 }
1957               if (msg->easy_handle == ps->recv_endpoint)
1958                 {
1959 #if DEBUG_CONNECTIONS
1960                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1961                               "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1962                               ps,
1963                               GNUNET_i2s(&pc->identity),
1964                               http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1965                               http_result);
1966 #endif
1967                   ps->recv_connected = GNUNET_NO;
1968                   ps->recv_active = GNUNET_NO;
1969                   curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1970                 }
1971               plugin->current_connections--;
1972             }
1973           if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1974             remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1975           break;
1976         default:
1977           break;
1978         }
1979     }
1980   while ( (msgs_in_queue > 0) );
1981 }
1982
1983
1984 /**
1985  * Task performing curl operations
1986  * @param cls plugin as closure
1987  * @param tc gnunet scheduler task context
1988  */
1989 static void curl_perform (void *cls,
1990                           const struct GNUNET_SCHEDULER_TaskContext *tc)
1991 {
1992   struct Plugin *plugin = cls;
1993   static unsigned int handles_last_run;
1994   int running;
1995   CURLMcode mret;
1996
1997   GNUNET_assert(cls !=NULL);
1998
1999   plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2000   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2001     return;
2002   do
2003     {
2004       running = 0;
2005       mret = curl_multi_perform (plugin->multi_handle, &running);
2006       if ((running < handles_last_run) && (running>0))
2007           curl_handle_finished(plugin);
2008       handles_last_run = running;
2009     }
2010   while (mret == CURLM_CALL_MULTI_PERFORM);
2011   curl_schedule(plugin);
2012 }
2013
2014
2015 /**
2016  * Function setting up file descriptors and scheduling task to run
2017  *
2018  * @param  plugin plugin as closure
2019  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2020  */
2021 static int 
2022 curl_schedule(struct Plugin *plugin)
2023 {
2024   fd_set rs;
2025   fd_set ws;
2026   fd_set es;
2027   int max;
2028   struct GNUNET_NETWORK_FDSet *grs;
2029   struct GNUNET_NETWORK_FDSet *gws;
2030   long to;
2031   CURLMcode mret;
2032
2033   /* Cancel previous scheduled task */
2034   if (plugin->http_curl_task !=  GNUNET_SCHEDULER_NO_TASK)
2035     {
2036       GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2037       plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2038     }
2039   
2040   max = -1;
2041   FD_ZERO (&rs);
2042   FD_ZERO (&ws);
2043   FD_ZERO (&es);
2044   mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2045   if (mret != CURLM_OK)
2046     {
2047       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2048                   _("%s failed at %s:%d: `%s'\n"),
2049                   "curl_multi_fdset", __FILE__, __LINE__,
2050                   curl_multi_strerror (mret));
2051       return GNUNET_SYSERR;
2052     }
2053   mret = curl_multi_timeout (plugin->multi_handle, &to);
2054   if (mret != CURLM_OK)
2055     {
2056       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2057                   _("%s failed at %s:%d: `%s'\n"),
2058                   "curl_multi_timeout", __FILE__, __LINE__,
2059                   curl_multi_strerror (mret));
2060       return GNUNET_SYSERR;
2061     }
2062
2063   grs = GNUNET_NETWORK_fdset_create ();
2064   gws = GNUNET_NETWORK_fdset_create ();
2065   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2066   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2067   plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2068                                                         GNUNET_SCHEDULER_NO_TASK,
2069                                                         (to == -1) 
2070                                                         ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
2071                                                         : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
2072                                                         grs,
2073                                                         gws,
2074                                                         &curl_perform,
2075                                                         plugin);
2076   GNUNET_NETWORK_fdset_destroy (gws);
2077   GNUNET_NETWORK_fdset_destroy (grs);
2078   return GNUNET_OK;
2079 }
2080
2081
2082 #if DEBUG_CURL
2083 /**
2084  * Function to log curl debug messages with GNUNET_log
2085  * @param curl handle
2086  * @param type curl_infotype
2087  * @param data data
2088  * @param size size
2089  * @param cls  closure
2090  * @return 0
2091  */
2092 static int 
2093 curl_logger (CURL * curl,
2094              curl_infotype type, 
2095              char * data, size_t size, 
2096              void * cls)
2097 {
2098   if (type == CURLINFO_TEXT)
2099     {
2100       char text[size+2];
2101       memcpy(text,data,size);
2102       if (text[size-1] == '\n')
2103         text[size] = '\0';
2104       else
2105         {
2106           text[size] = '\n';
2107           text[size+1] = '\0';
2108         }
2109       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110                   "CURL: Connection %X - %s", 
2111                   cls, 
2112                   text);
2113     }
2114   return 0;
2115 }
2116 #endif
2117
2118
2119 /**
2120  * Function setting up curl handle and selecting message to send
2121  *
2122  * @param plugin plugin
2123  * @param ps session
2124  * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2125  */
2126 static int 
2127 send_check_connections (struct Plugin *plugin, 
2128                         struct Session *ps)
2129 {
2130   CURLMcode mret;
2131   struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2132
2133   if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2134     {
2135       /* RECV DIRECTION */
2136       /* Check if session is connected to receive data, otherwise connect to peer */
2137
2138       if (ps->recv_connected == GNUNET_NO)
2139         {
2140           int fresh = GNUNET_NO;
2141           if (ps->recv_endpoint == NULL)
2142             {
2143               fresh = GNUNET_YES;
2144               ps->recv_endpoint = curl_easy_init();
2145             }
2146 #if DEBUG_CURL
2147           curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2148           curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2149           curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2150 #endif
2151 #if BUILD_HTTPS
2152           curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2153           curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2154           curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2155 #endif
2156           curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
2157           curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
2158           curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2159           curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2160           curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
2161           curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2162           curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2163           curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2164           curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2165           curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2166           curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
2167 #if CURL_TCP_NODELAY
2168           curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2169 #endif
2170           if (fresh==GNUNET_YES)
2171             {
2172               mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
2173               if (mret != CURLM_OK)
2174                 {
2175                   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2176                               _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2177                               ps,
2178                               "curl_multi_add_handle", __FILE__, __LINE__,
2179                               curl_multi_strerror (mret));
2180                   return GNUNET_SYSERR;
2181                 }
2182             }
2183           if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2184             {
2185               GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2186               plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2187             }
2188           plugin->current_connections ++;
2189           plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2190         }
2191       
2192       /* waiting for receive direction */
2193       if (ps->recv_connected==GNUNET_NO)
2194         return GNUNET_NO;
2195       
2196       /* SEND DIRECTION */
2197       /* Check if session is connected to send data, otherwise connect to peer */
2198       if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
2199         {
2200           if (ps->send_active == GNUNET_YES)
2201             {
2202 #if DEBUG_CONNECTIONS
2203               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204                           "Connection %X: outbound active, enqueueing message\n",
2205                           ps);
2206 #endif
2207               return GNUNET_YES;
2208             }
2209           if (ps->send_active == GNUNET_NO)
2210             {
2211 #if DEBUG_CONNECTIONS
2212               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2213                           "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2214                           ps);
2215 #endif
2216               if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2217                 {
2218                   ps->send_active=GNUNET_YES;
2219                   if (plugin->http_curl_task !=  GNUNET_SCHEDULER_NO_TASK)
2220                     {
2221                       GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2222                       plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2223                     }
2224                   plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2225                   return GNUNET_YES;
2226                 }
2227               else
2228                 return GNUNET_SYSERR;
2229             }
2230         }
2231       /* not connected, initiate connection */
2232       if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2233         {
2234           int fresh = GNUNET_NO;
2235           if (NULL == ps->send_endpoint)
2236             {
2237               ps->send_endpoint = curl_easy_init();
2238               fresh = GNUNET_YES;
2239             }
2240           GNUNET_assert (ps->send_endpoint != NULL);
2241           GNUNET_assert (NULL != ps->pending_msgs_tail);
2242 #if DEBUG_CONNECTIONS
2243           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244                       "Connection %X: outbound not connected, initiating connection\n",
2245                       ps);
2246 #endif
2247           ps->send_active = GNUNET_NO;
2248           
2249 #if DEBUG_CURL
2250           curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2251           curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2252           curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2253 #endif
2254 #if BUILD_HTTPS
2255           curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2256           curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2257           curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2258 #endif
2259           curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2260           curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2261           curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2262           curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2263           curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2264           curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2265           curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2266           curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2267           curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2268           curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2269           curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2270           curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2271 #if CURL_TCP_NODELAY
2272           curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2273 #endif
2274           if (fresh==GNUNET_YES)
2275             {
2276               mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2277               if (mret != CURLM_OK)
2278                 {
2279                   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2280                               _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2281                               ps,
2282                               "curl_multi_add_handle", __FILE__, __LINE__,
2283                               curl_multi_strerror (mret));
2284                   return GNUNET_SYSERR;
2285                 }
2286             }
2287         }
2288       if (plugin->http_curl_task !=  GNUNET_SCHEDULER_NO_TASK)
2289         {
2290           GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2291           plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2292         }
2293       plugin->current_connections++;
2294       plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2295       return GNUNET_YES;
2296     }
2297   if (ps->direction == INBOUND)
2298     {
2299       GNUNET_assert (NULL != ps->pending_msgs_tail);
2300       if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2301           (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2302         return GNUNET_YES;
2303     }
2304   return GNUNET_SYSERR;
2305 }
2306
2307
2308 /**
2309  * select best session to transmit data to peer
2310  *
2311  * @param pc peer context of target peer
2312  * @param addr address of target peer
2313  * @param addrlen address length
2314  * @param force_address does transport service enforce address?
2315  * @param session session passed by transport service
2316  * @return selected session
2317  *
2318  */
2319 static struct Session * 
2320 send_select_session (struct HTTP_PeerContext *pc, 
2321                      const void * addr, size_t addrlen, 
2322                      int force_address, 
2323                      struct Session * session)
2324 {
2325   struct Session * tmp = NULL;
2326   int addr_given = GNUNET_NO;
2327   
2328   if ((addr!=NULL) && (addrlen>0))
2329     addr_given = GNUNET_YES;
2330   
2331   if (force_address == GNUNET_YES)
2332     {
2333       /* check session given as argument */
2334       if ((session != NULL) && (addr_given == GNUNET_YES))
2335         {
2336           if (0 == memcmp(session->addr, addr, addrlen))
2337             {
2338               /* connection can not be used, since it is disconnected */
2339               if ( (session->recv_force_disconnect==GNUNET_NO) && 
2340                    (session->send_force_disconnect==GNUNET_NO) )
2341                 {
2342 #if DEBUG_SESSION_SELECTION
2343                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344                               "Session %X selected: Using session passed by transport to send to forced address \n", 
2345                               session);
2346 #endif
2347                   return session;
2348                 }
2349             }
2350         }
2351       /* check last session used */
2352       if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2353         {
2354           if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2355             {
2356               /* connection can not be used, since it is disconnected */
2357               if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) && 
2358                    (pc->last_session->send_force_disconnect==GNUNET_NO) )
2359                 {
2360 #if DEBUG_SESSION_SELECTION
2361                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362                               "Session %X selected: Using last session used to send to forced address \n", 
2363                               pc->last_session);
2364 #endif
2365                   return pc->last_session;
2366                 }
2367             }
2368         }
2369       /* find session in existing sessions */
2370       tmp = pc->head;
2371       while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2372         {
2373           if (0 == memcmp(tmp->addr, addr, addrlen))
2374             {
2375               /* connection can not be used, since it is disconnected */
2376               if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2377                    (tmp->send_force_disconnect==GNUNET_NO) )
2378                 {
2379 #if DEBUG_SESSION_SELECTION
2380                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2381                               "Session %X selected: Using existing session to send to forced address \n", 
2382                               session);
2383 #endif
2384                   return session;
2385                 }             
2386             }
2387           tmp=tmp->next;
2388         }
2389       /* no session to use */
2390       return NULL;
2391     }
2392   if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2393     {
2394       /* check session given as argument */
2395       if (session != NULL)
2396         {
2397           /* connection can not be used, since it is disconnected */
2398           if ( (session->recv_force_disconnect==GNUNET_NO) &&
2399                (session->send_force_disconnect==GNUNET_NO) )
2400             {
2401 #if DEBUG_SESSION_SELECTION
2402               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2403                           "Session %X selected: Using session passed by transport to send not-forced address\n", 
2404                           session);
2405 #endif
2406               return session;
2407             }     
2408         }
2409       /* check last session used */
2410       if (pc->last_session != NULL)
2411         {
2412           /* connection can not be used, since it is disconnected */
2413           if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2414                (pc->last_session->send_force_disconnect==GNUNET_NO) )
2415             {
2416 #if DEBUG_SESSION_SELECTION
2417               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2418                           "Session %X selected: Using last session to send to not-forced address\n", 
2419                           pc->last_session);
2420 #endif
2421               return pc->last_session;
2422             }
2423         }
2424       /* find session in existing sessions */
2425       tmp = pc->head;
2426       while (tmp!=NULL)
2427         {
2428           /* connection can not be used, since it is disconnected */
2429           if ( (tmp->recv_force_disconnect==GNUNET_NO) && 
2430                (tmp->send_force_disconnect==GNUNET_NO) )
2431             {
2432 #if DEBUG_SESSION_SELECTION
2433               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2434                           "Session %X selected: Using existing session to send to not-forced address\n",
2435                           tmp);
2436 #endif
2437               return tmp;
2438             }
2439           tmp=tmp->next;
2440         }
2441       return NULL;
2442     }
2443   return NULL;
2444 }
2445
2446
2447 /**
2448  * Function that can be used by the transport service to transmit
2449  * a message using the plugin.   Note that in the case of a
2450  * peer disconnecting, the continuation MUST be called
2451  * prior to the disconnect notification itself.  This function
2452  * will be called with this peer's HELLO message to initiate
2453  * a fresh connection to another peer.
2454  *
2455  * @param cls closure
2456  * @param target who should receive this message
2457  * @param msgbuf the message to transmit
2458  * @param msgbuf_size number of bytes in 'msgbuf'
2459  * @param priority how important is the message (most plugins will
2460  *                 ignore message priority and just FIFO)
2461  * @param to how long to wait at most for the transmission (does not
2462  *                require plugins to discard the message after the timeout,
2463  *                just advisory for the desired delay; most plugins will ignore
2464  *                this as well)
2465  * @param session which session must be used (or NULL for "any")
2466  * @param addr the address to use (can be NULL if the plugin
2467  *                is "on its own" (i.e. re-use existing TCP connection))
2468  * @param addrlen length of the address in bytes
2469  * @param force_address GNUNET_YES if the plugin MUST use the given address,
2470  *                GNUNET_NO means the plugin may use any other address and
2471  *                GNUNET_SYSERR means that only reliable existing
2472  *                bi-directional connections should be used (regardless
2473  *                of address)
2474  * @param cont continuation to call once the message has
2475  *        been transmitted (or if the transport is ready
2476  *        for the next transmission call; or if the
2477  *        peer disconnected...); can be NULL
2478  * @param cont_cls closure for cont
2479  * @return number of bytes used (on the physical network, with overheads);
2480  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
2481  *         and does NOT mean that the message was not transmitted (DV)
2482  */
2483 static ssize_t
2484 http_plugin_send (void *cls,
2485                   const struct GNUNET_PeerIdentity *target,
2486                   const char *msgbuf,
2487                   size_t msgbuf_size,
2488                   unsigned int priority,
2489                   struct GNUNET_TIME_Relative to,
2490                   struct Session *session,
2491                   const void *addr,
2492                   size_t addrlen,
2493                   int force_address,
2494                   GNUNET_TRANSPORT_TransmitContinuation cont,
2495                   void *cont_cls)
2496 {
2497   struct Plugin *plugin = cls;
2498   struct HTTP_Message *msg;
2499   struct HTTP_PeerContext * pc;
2500   struct Session * ps = NULL;
2501
2502   GNUNET_assert(cls !=NULL);
2503
2504 #if DEBUG_HTTP
2505   char * force;
2506
2507   if (force_address == GNUNET_YES)
2508     GNUNET_asprintf(&force, "forced addr.");
2509   else if (force_address == GNUNET_NO)
2510     GNUNET_asprintf(&force, "any addr.");
2511   else if (force_address == GNUNET_SYSERR)
2512     GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2513   else
2514     GNUNET_assert (0);
2515   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516               "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2517               msgbuf_size,
2518               GNUNET_i2s(target),
2519               force,
2520               http_plugin_address_to_string(NULL, addr, addrlen),
2521               session);
2522   GNUNET_free(force);
2523 #endif
2524
2525   pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2526   /* Peer unknown */
2527   if (pc==NULL)
2528     {
2529       pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2530       pc->plugin = plugin;
2531       pc->session_id_counter=1;
2532       pc->last_session = NULL;
2533       memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2534       GNUNET_CONTAINER_multihashmap_put (plugin->peers, 
2535                                          &pc->identity.hashPubKey, 
2536                                          pc, 
2537                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2538       GNUNET_STATISTICS_update (plugin->env->stats,
2539                                 gettext_noop ("# HTTP peers active"),
2540                                 1,
2541                                 GNUNET_NO);
2542     }
2543   ps = send_select_session (pc, addr, addrlen, force_address, session);
2544   /* session not existing, but address forced -> creating new session */
2545   if (ps==NULL)
2546     {
2547       if ((addr!=NULL) && (addrlen!=0))
2548         {
2549           ps = GNUNET_malloc(sizeof (struct Session));
2550 #if DEBUG_SESSION_SELECTION
2551           if (force_address == GNUNET_YES)
2552             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553                         "No existing connection & forced address: creating new session %X to peer %s\n", 
2554                         ps, GNUNET_i2s(target));
2555           if (force_address != GNUNET_YES)
2556             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2557                         "No existing connection: creating new session %X to peer %s\n", 
2558                         ps, GNUNET_i2s(target));
2559 #endif
2560           ps->addr = GNUNET_malloc(addrlen);
2561           memcpy(ps->addr,addr,addrlen);
2562           ps->addrlen = addrlen;
2563           ps->direction=OUTBOUND;
2564           ps->recv_connected = GNUNET_NO;
2565           ps->recv_force_disconnect = GNUNET_NO;
2566           ps->send_connected = GNUNET_NO;
2567           ps->send_force_disconnect = GNUNET_NO;
2568           ps->pending_msgs_head = NULL;
2569           ps->pending_msgs_tail = NULL;
2570           ps->peercontext=pc;
2571           ps->session_id = pc->session_id_counter;
2572           ps->queue_length_cur = 0;
2573           ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2574           pc->session_id_counter++;
2575           ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2576           if (ps->msgtok == NULL)
2577             ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2578           GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2579           GNUNET_STATISTICS_update (plugin->env->stats,
2580                                     gettext_noop ("# HTTP outbound sessions for peers active"),
2581                                     1,
2582                                     GNUNET_NO);
2583         }
2584       else
2585         {
2586 #if DEBUG_HTTP
2587           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2588                       "No existing session found & and no address given: no way to send this message to peer `%s'!\n", 
2589                       GNUNET_i2s(target));
2590 #endif
2591           return GNUNET_SYSERR;
2592         }
2593     }
2594   
2595   if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2596     {
2597       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2598                   "Queue %X full: %u bytes in queue available, message with %u is too big\n", 
2599                   ps, 
2600                   (ps->queue_length_max - ps->queue_length_cur), 
2601                   msgbuf_size);
2602       //return GNUNET_SYSERR;
2603     }
2604   
2605   /* create msg */
2606   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2607   msg->next = NULL;
2608   msg->size = msgbuf_size;
2609   msg->pos = 0;
2610   msg->buf = (char *) &msg[1];
2611   msg->transmit_cont = cont;
2612   msg->transmit_cont_cls = cont_cls;
2613   memcpy (msg->buf,msgbuf, msgbuf_size);  
2614   GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2615                                ps->pending_msgs_tail,
2616                                msg);
2617   ps->queue_length_cur += msgbuf_size;
2618   if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2619     return GNUNET_SYSERR;
2620   if (force_address != GNUNET_YES)
2621     pc->last_session = ps;
2622   if (pc->last_session==NULL)
2623     pc->last_session = ps;
2624   return msg->size;
2625 }
2626
2627
2628 /**
2629  * Function that can be used to force the plugin to disconnect
2630  * from the given peer and cancel all previous transmissions
2631  * (and their continuationc).
2632  *
2633  * @param cls closure
2634  * @param target peer from which to disconnect
2635  */
2636 static void
2637 http_plugin_disconnect (void *cls,
2638                             const struct GNUNET_PeerIdentity *target)
2639 {
2640   struct Plugin *plugin = cls;
2641   struct HTTP_PeerContext *pc = NULL;
2642   struct Session *ps = NULL;
2643
2644   pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2645   if (pc==NULL)
2646     return;
2647   ps = pc->head;
2648   while (ps!=NULL)
2649     {
2650       /* Telling transport that session is getting disconnected */
2651       plugin->env->session_end(plugin, target, ps);
2652       if (ps->direction==OUTBOUND)
2653         {
2654           if (ps->send_endpoint!=NULL)
2655             {
2656               //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2657               //curl_easy_cleanup(ps->send_endpoint);
2658               //ps->send_endpoint=NULL;
2659               ps->send_force_disconnect = GNUNET_YES;
2660             }
2661           if (ps->recv_endpoint!=NULL)
2662             {
2663               //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2664               //curl_easy_cleanup(ps->recv_endpoint);
2665               //ps->recv_endpoint=NULL;
2666               ps->recv_force_disconnect = GNUNET_YES;
2667             }
2668         }
2669       if (ps->direction==INBOUND)
2670         {
2671           ps->recv_force_disconnect = GNUNET_YES;
2672           ps->send_force_disconnect = GNUNET_YES;
2673         }      
2674       while (ps->pending_msgs_head!=NULL)
2675         remove_http_message(ps, ps->pending_msgs_head);
2676       ps->recv_active = GNUNET_NO;
2677       ps->send_active = GNUNET_NO;
2678       ps=ps->next;
2679     }
2680 }
2681
2682
2683 /**
2684  * Convert the transports address to a nice, human-readable
2685  * format.
2686  *
2687  * @param cls closure
2688  * @param type name of the transport that generated the address
2689  * @param addr one of the addresses of the host, NULL for the last address
2690  *        the specific address format depends on the transport
2691  * @param addrlen length of the address
2692  * @param numeric should (IP) addresses be displayed in numeric form?
2693  * @param timeout after how long should we give up?
2694  * @param asc function to call on each string
2695  * @param asc_cls closure for asc
2696  */
2697 static void
2698 http_plugin_address_pretty_printer (void *cls,
2699                                         const char *type,
2700                                         const void *addr,
2701                                         size_t addrlen,
2702                                         int numeric,
2703                                         struct GNUNET_TIME_Relative timeout,
2704                                         GNUNET_TRANSPORT_AddressStringCallback
2705                                         asc, void *asc_cls)
2706 {
2707   const struct IPv4HttpAddress *t4;
2708   const struct IPv6HttpAddress *t6;
2709   struct sockaddr_in a4;
2710   struct sockaddr_in6 a6;
2711   char * address;
2712   char * ret;
2713   unsigned int port;
2714   unsigned int res;
2715
2716   GNUNET_assert(cls !=NULL);
2717   if (addrlen == sizeof (struct IPv6HttpAddress))
2718     {
2719       address = GNUNET_malloc (INET6_ADDRSTRLEN);
2720       t6 = addr;
2721       a6.sin6_addr = t6->ipv6_addr;
2722       inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2723       port = ntohs(t6->u6_port);
2724     }
2725   else if (addrlen == sizeof (struct IPv4HttpAddress))
2726     {
2727       address = GNUNET_malloc (INET_ADDRSTRLEN);
2728       t4 = addr;
2729       a4.sin_addr.s_addr =  t4->ipv4_addr;
2730       inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2731       port = ntohs(t4->u_port);
2732     }
2733   else
2734     {
2735       /* invalid address */
2736       GNUNET_break_op (0);
2737       asc (asc_cls, NULL);
2738       return;
2739     }
2740   res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2741   GNUNET_free (address);
2742   GNUNET_assert(res != 0);
2743   asc (asc_cls, ret);
2744   GNUNET_free_non_null (ret);
2745 }
2746
2747
2748
2749 /**
2750  * Another peer has suggested an address for this
2751  * peer and transport plugin.  Check that this could be a valid
2752  * address.  If so, consider adding it to the list
2753  * of addresses.
2754  *
2755  * @param cls closure
2756  * @param addr pointer to the address
2757  * @param addrlen length of addr
2758  * @return GNUNET_OK if this is a plausible address for this peer
2759  *         and transport
2760  */
2761 static int
2762 http_plugin_address_suggested (void *cls,
2763                                const void *addr, size_t addrlen)
2764 {
2765   struct Plugin *plugin = cls;
2766   struct IPv4HttpAddress *v4;
2767   struct IPv6HttpAddress *v6;
2768   struct IPv4HttpAddress *tv4 = plugin->ipv4_addr_head;
2769   struct IPv6HttpAddress *tv6 = plugin->ipv6_addr_head;
2770
2771   GNUNET_assert(cls !=NULL);
2772   if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2773       (addrlen != sizeof (struct IPv6HttpAddress)))
2774     return GNUNET_SYSERR;
2775   if (addrlen == sizeof (struct IPv4HttpAddress))
2776     {
2777       v4 = (struct IPv4HttpAddress *) addr;
2778       if (plugin->bind4_address!=NULL)
2779         {
2780           if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2781             return GNUNET_OK;
2782           else
2783             return GNUNET_SYSERR;
2784         }
2785       while (tv4!=NULL)
2786         {
2787           if (0==memcmp (&tv4->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2788             break;
2789           tv4 = tv4->next;
2790         }
2791       if (tv4 != NULL)
2792         return GNUNET_OK;
2793       else
2794         return GNUNET_SYSERR;
2795     }
2796   if (addrlen == sizeof (struct IPv6HttpAddress))
2797     {
2798       v6 = (struct IPv6HttpAddress *) addr;
2799       if (plugin->bind6_address!=NULL)
2800         {
2801           if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2802             return GNUNET_OK;
2803           else
2804             return GNUNET_SYSERR;
2805         }
2806       while (tv6!=NULL)
2807         {
2808           if (0 == memcmp (&tv6->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2809             break;
2810           tv6 = tv6->next;
2811         }
2812       if (tv6 !=NULL)
2813         return GNUNET_OK;
2814       else
2815         return GNUNET_SYSERR;
2816     }
2817   return GNUNET_SYSERR;
2818 }
2819
2820
2821 /**
2822  * Function called for a quick conversion of the binary address to
2823  * a numeric address.  Note that the caller must not free the
2824  * address and that the next call to this function is allowed
2825  * to override the address again.
2826  *
2827  * @param cls closure
2828  * @param addr binary address
2829  * @param addrlen length of the address
2830  * @return string representing the same address
2831  */
2832 static const char*
2833 http_plugin_address_to_string (void *cls,
2834                                    const void *addr,
2835                                    size_t addrlen)
2836 {
2837   const struct IPv4HttpAddress *t4;
2838   const struct IPv6HttpAddress *t6;
2839   struct sockaddr_in a4;
2840   struct sockaddr_in6 a6;
2841   char * address;
2842   static char rbuf[INET6_ADDRSTRLEN + 13];
2843   uint16_t port;
2844   int res;
2845
2846   if (addrlen == sizeof (struct IPv6HttpAddress))
2847     {
2848       address = GNUNET_malloc (INET6_ADDRSTRLEN);
2849       t6 = addr;
2850       a6.sin6_addr = t6->ipv6_addr;
2851       inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2852       port = ntohs(t6->u6_port);
2853     }
2854   else if (addrlen == sizeof (struct IPv4HttpAddress))
2855     {
2856       address = GNUNET_malloc (INET_ADDRSTRLEN);
2857       t4 = addr;
2858       a4.sin_addr.s_addr =  t4->ipv4_addr;
2859       inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2860       port = ntohs(t4->u_port);
2861     }
2862   else
2863     {
2864       /* invalid address */
2865       return NULL;
2866     }
2867
2868   res = GNUNET_snprintf (rbuf,
2869                    sizeof (rbuf),
2870                    "%s:%u",
2871                    address,
2872                    port);
2873
2874   GNUNET_free (address);
2875   GNUNET_assert(res != 0);
2876   return rbuf;
2877 }
2878
2879 /**
2880  * Notify transport service about address
2881  *
2882  * @param cls the plugin
2883  * @param tc unused
2884  */
2885 static void
2886 address_notification (void *cls,
2887                     const struct GNUNET_SCHEDULER_TaskContext *tc)
2888 {
2889   struct Plugin *plugin = cls;
2890
2891   GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2892 }
2893
2894 /**
2895  * Exit point from the plugin.
2896  */
2897 void *
2898 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2899 {
2900   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2901   struct Plugin *plugin = api->cls;
2902   CURLMcode mret;
2903   struct IPv4HttpAddress * ipv4addr;
2904   struct IPv6HttpAddress * ipv6addr;
2905   GNUNET_assert(cls !=NULL);
2906
2907   if (plugin->http_server_daemon_v4 != NULL)
2908     {
2909       MHD_stop_daemon (plugin->http_server_daemon_v4);
2910       plugin->http_server_daemon_v4 = NULL;
2911     }
2912   if (plugin->http_server_daemon_v6 != NULL)
2913     {
2914       MHD_stop_daemon (plugin->http_server_daemon_v6);
2915       plugin->http_server_daemon_v6 = NULL;
2916     }
2917   if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2918     {
2919       GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
2920       plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2921     }
2922   if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2923     {
2924       GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
2925       plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2926     }
2927   
2928   while (plugin->ipv4_addr_head!=NULL)
2929     {
2930       ipv4addr = plugin->ipv4_addr_head;
2931       GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,ipv4addr);
2932       GNUNET_free(ipv4addr);
2933     }
2934   
2935   while (plugin->ipv6_addr_head!=NULL)
2936     {
2937       ipv6addr = plugin->ipv6_addr_head;
2938       GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,ipv6addr);
2939       GNUNET_free(ipv6addr);
2940     }
2941   
2942   /* free all peer information */
2943   if (plugin->peers!=NULL)
2944     {
2945       GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2946                                              &remove_peer_context_Iterator,
2947                                              plugin);
2948       GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2949     }
2950   if (plugin->multi_handle!=NULL)
2951     {
2952       mret = curl_multi_cleanup(plugin->multi_handle);
2953       if (CURLM_OK != mret)
2954         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2955                     "curl multihandle clean up failed\n");
2956       plugin->multi_handle = NULL;
2957     }
2958   curl_global_cleanup();
2959   
2960   if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2961     {
2962       GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2963       plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2964     }
2965   
2966   GNUNET_free_non_null (plugin->bind4_address);
2967   GNUNET_free_non_null (plugin->bind6_address);
2968   GNUNET_free_non_null(plugin->bind_hostname);
2969 #if BUILD_HTTPS
2970   GNUNET_free_non_null (plugin->crypto_init);
2971   GNUNET_free_non_null (plugin->cert);
2972   GNUNET_free_non_null (plugin->key);
2973 #endif
2974   GNUNET_free (plugin);
2975   GNUNET_free (api);
2976 #if DEBUG_HTTP
2977   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2978               "Unload %s plugin complete...\n", 
2979               PROTOCOL_PREFIX);
2980 #endif
2981   return NULL;
2982 }
2983
2984 #if BUILD_HTTPS
2985 static char *
2986 load_certificate( const char * file )
2987 {
2988   struct GNUNET_DISK_FileHandle * gn_file;
2989   struct stat fstat;
2990   char * text = NULL;
2991
2992   if (0!=STAT(file, &fstat))
2993           return NULL;
2994   text = GNUNET_malloc (fstat.st_size+1);
2995   gn_file = GNUNET_DISK_file_open(file, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2996   if (gn_file==NULL)
2997     {
2998       GNUNET_free(text);
2999       return NULL;
3000     }
3001   if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
3002     {
3003       GNUNET_free(text);
3004       GNUNET_DISK_file_close(gn_file);
3005       return NULL;
3006     }
3007   text[fstat.st_size] = '\0';
3008   GNUNET_DISK_file_close(gn_file);
3009   return text;
3010 }
3011 #endif
3012
3013
3014 /**
3015  * Entry point for the plugin.
3016  */
3017 void *
3018 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3019 {
3020   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3021   struct Plugin *plugin;
3022   struct GNUNET_TRANSPORT_PluginFunctions *api;
3023   struct GNUNET_TIME_Relative gn_timeout;
3024   long long unsigned int port;
3025   unsigned long long tneigh;
3026   char * component_name;
3027 #if BUILD_HTTPS
3028   char * key_file = NULL;
3029   char * cert_file = NULL;
3030 #endif
3031
3032   GNUNET_assert(cls !=NULL);
3033 #if DEBUG_HTTP
3034   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3035               "Starting %s plugin...\n", 
3036               PROTOCOL_PREFIX);
3037 #endif
3038   GNUNET_asprintf(&component_name,
3039                   "transport-%s",
3040                   PROTOCOL_PREFIX);
3041
3042   plugin = GNUNET_malloc (sizeof (struct Plugin));
3043   plugin->stats = env->stats;
3044   plugin->env = env;
3045   plugin->peers = NULL;
3046   plugin->bind4_address = NULL;
3047   plugin->bind6_address = NULL;
3048   plugin->use_ipv6  = GNUNET_YES;
3049   plugin->use_ipv4  = GNUNET_YES;
3050   plugin->use_localaddresses = GNUNET_NO;
3051
3052   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3053   api->cls = plugin;
3054   api->send = &http_plugin_send;
3055   api->disconnect = &http_plugin_disconnect;
3056   api->address_pretty_printer = &http_plugin_address_pretty_printer;
3057   api->check_address = &http_plugin_address_suggested;
3058   api->address_to_string = &http_plugin_address_to_string;
3059
3060   /* Hashing our identity to use it in URLs */
3061   GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey), 
3062                              &plugin->my_ascii_hash_ident);
3063
3064
3065   if (GNUNET_CONFIGURATION_have_value (env->cfg, "TRANSPORT", "NEIGHBOUR_LIMIT"))
3066   {
3067     GNUNET_CONFIGURATION_get_value_number (env->cfg,
3068                                          "TRANSPORT",
3069                                          "NEIGHBOUR_LIMIT",
3070                                          &tneigh);
3071   }
3072   else
3073   {
3074     tneigh = -1;
3075   }
3076   plugin->max_connect_per_transport = tneigh;
3077
3078
3079   /* Use IPv6? */
3080   if (GNUNET_CONFIGURATION_have_value (env->cfg,
3081                                        component_name, "USE_IPv6"))
3082     {
3083       plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3084                                                                component_name,
3085                                                                "USE_IPv6");
3086     }
3087   /* Use IPv4? */
3088   if (GNUNET_CONFIGURATION_have_value (env->cfg,
3089                                        component_name, "USE_IPv4"))
3090     {
3091       plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3092                                                                component_name,"USE_IPv4");
3093     }
3094   /* use local addresses? */
3095
3096   if (GNUNET_CONFIGURATION_have_value (env->cfg,
3097                                        component_name, "USE_LOCALADDR"))
3098     {
3099       plugin->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3100                                                                component_name,
3101                                                                "USE_LOCALADDR");
3102     }
3103   /* Reading port number from config file */
3104   if ((GNUNET_OK !=
3105        GNUNET_CONFIGURATION_get_value_number (env->cfg,
3106                                               component_name,
3107                                               "PORT",
3108                                               &port)) ||
3109       (port > 65535) )
3110     {
3111       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3112                        component_name,
3113                        _("Require valid port number for transport plugin `%s' in configuration!\n"),
3114                        PROTOCOL_PREFIX);
3115       GNUNET_free(component_name);
3116       LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3117       return NULL;
3118     }
3119
3120   /* Reading ipv4 addresse to bind to from config file */
3121   if ( (plugin->use_ipv4==GNUNET_YES) && 
3122        (GNUNET_CONFIGURATION_have_value (env->cfg,
3123                                          component_name, "BINDTO4")))
3124     {
3125       GNUNET_break (GNUNET_OK ==
3126                     GNUNET_CONFIGURATION_get_value_string (env->cfg,
3127                                                            component_name,
3128                                                            "BINDTO4",
3129                                                            &plugin->bind_hostname));
3130       plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
3131       plugin->bind4_address->sin_family = AF_INET;
3132       plugin->bind4_address->sin_port = htons (port);
3133       
3134       if (plugin->bind_hostname!=NULL)
3135         {
3136           if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
3137             {
3138               GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3139                                component_name,
3140                                _("Misconfigured address to bind to in configuration!\n"));
3141               GNUNET_free(plugin->bind4_address);
3142               GNUNET_free(plugin->bind_hostname);
3143               plugin->bind_hostname = NULL;
3144               plugin->bind4_address = NULL;
3145             }
3146         }
3147     }
3148   
3149   /* Reading ipv4 addresse to bind to from config file */
3150   if ( (plugin->use_ipv6==GNUNET_YES) && 
3151        (GNUNET_CONFIGURATION_have_value (env->cfg,
3152                                          component_name, "BINDTO6")))
3153     {
3154       if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3155                                                               component_name,
3156                                                               "BINDTO6",
3157                                                               &plugin->bind_hostname))
3158         {
3159           plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
3160           plugin->bind6_address->sin6_family = AF_INET6;
3161           plugin->bind6_address->sin6_port = htons (port);
3162           if (plugin->bind_hostname!=NULL)
3163             {
3164               if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
3165                 {
3166                   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3167                                    component_name,
3168                                    _("Misconfigured address to bind to in configuration!\n"));
3169                   GNUNET_free(plugin->bind6_address);
3170                   GNUNET_free(plugin->bind_hostname);
3171                   plugin->bind_hostname = NULL;
3172                   plugin->bind6_address = NULL;
3173                 }
3174             }
3175         }
3176     }
3177   
3178 #if BUILD_HTTPS
3179   /* Reading HTTPS crypto related configuration */
3180   /* Get crypto init string from config */
3181   if (GNUNET_CONFIGURATION_have_value (env->cfg,
3182                                        "transport-https", "CRYPTO_INIT"))
3183     {
3184       GNUNET_CONFIGURATION_get_value_string (env->cfg,
3185                                              "transport-https",
3186                                              "CRYPTO_INIT",
3187                                              &plugin->crypto_init);
3188     }
3189   else
3190     {
3191       GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
3192     }
3193   
3194   /* Get private key file from config */
3195   if (GNUNET_CONFIGURATION_have_value (env->cfg,
3196                                        "transport-https", "KEY_FILE"))
3197     {
3198     GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3199                                              "transport-https",
3200                                              "KEY_FILE",
3201                                              &key_file);
3202     }
3203   if (key_file==NULL)
3204     GNUNET_asprintf(&key_file,"https.key");
3205   
3206   /* Get private key file from config */
3207   if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
3208     {
3209     GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3210                                              "transport-https",
3211                                              "CERT_FILE",
3212                                              &cert_file);
3213     }
3214   if (cert_file==NULL)
3215     GNUNET_asprintf(&cert_file,"https.cert");
3216   
3217   /* read key & certificates from file */
3218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
3219               "Loading TLS certificate `%s' `%s'\n", 
3220               key_file, cert_file);
3221
3222   plugin->key = load_certificate( key_file );
3223   plugin->cert = load_certificate( cert_file );
3224
3225   if ((plugin->key==NULL) || (plugin->cert==NULL))
3226     {
3227       char * cmd;
3228       int ret = 0;
3229       GNUNET_asprintf(&cmd,
3230                       "gnunet-transport-certificate-creation %s %s", 
3231                       key_file, cert_file);
3232       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3233                   "No usable TLS certificate found, creating certificate \n");
3234       ret = system(cmd);
3235       if (ret != 0)
3236         {
3237           GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3238                            "https",
3239                            _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
3240                            "transport-https");
3241           GNUNET_free (key_file);
3242           GNUNET_free (cert_file);
3243           GNUNET_free (component_name);
3244           LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3245           GNUNET_free (cmd);
3246           return NULL;
3247         }
3248       GNUNET_free (cmd);
3249       plugin->key = load_certificate( key_file );
3250       plugin->cert = load_certificate( cert_file );
3251       if ((plugin->key==NULL) || (plugin->cert==NULL))
3252         {
3253           GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3254                            "https",
3255                            _("No usable TLS certificate found and creating one failed! \n"),
3256                            "transport-https");
3257           GNUNET_free (key_file);
3258           GNUNET_free (cert_file);
3259           GNUNET_free (component_name);
3260           
3261           LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3262           return NULL;
3263         }
3264     }
3265   GNUNET_free (key_file);
3266   GNUNET_free (cert_file);
3267   
3268   GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
3269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
3270 #endif
3271
3272   GNUNET_assert ((port > 0) && (port <= 65535));
3273   plugin->port_inbound = port;
3274   gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3275   unsigned int timeout = (gn_timeout.rel_value) / 1000;
3276   if ( (plugin->http_server_daemon_v6 == NULL) && 
3277        (plugin->use_ipv6 == GNUNET_YES) && 
3278        (port != 0) )
3279     {
3280       struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3281       plugin->http_server_daemon_v6 = MHD_start_daemon (
3282 #if DEBUG_MHD
3283                                                         MHD_USE_DEBUG |
3284 #endif
3285 #if BUILD_HTTPS
3286                                                         MHD_USE_SSL |
3287 #endif
3288                                                         MHD_USE_IPv6,
3289                                                         port,
3290                                                         &mhd_accept_cb, plugin,
3291                                                         &mhd_access_cb, plugin,
3292                                                         MHD_OPTION_SOCK_ADDR, tmp,
3293                                                         MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3294 #if BUILD_HTTPS
3295                                                         MHD_OPTION_HTTPS_PRIORITIES,  plugin->crypto_init,
3296                                                         MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3297                                                         MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3298 #endif
3299                                                         MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3300                                                         MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3301                                                         MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3302                                                         MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3303                                                         MHD_OPTION_END);
3304     }
3305   if ( (plugin->http_server_daemon_v4 == NULL) && 
3306        (plugin->use_ipv4 == GNUNET_YES) && 
3307        (port != 0) )
3308     {
3309       plugin->http_server_daemon_v4 = MHD_start_daemon (
3310 #if DEBUG_MHD
3311                                                         MHD_USE_DEBUG |
3312 #endif
3313 #if BUILD_HTTPS
3314                                                         MHD_USE_SSL |
3315 #endif
3316                                                         MHD_NO_FLAG,
3317                                                         port,
3318                                                         &mhd_accept_cb, plugin ,
3319                                                         &mhd_access_cb, plugin,
3320                                                         MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3321                                                         MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3322 #if BUILD_HTTPS
3323                                                         MHD_OPTION_HTTPS_PRIORITIES,  plugin->crypto_init,
3324                                                         MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3325                                                         MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3326 #endif
3327                                                         MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3328                                                         MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3329                                                         MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3330                                                         MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3331                                                         MHD_OPTION_END);
3332     }
3333   if (plugin->http_server_daemon_v4 != NULL)
3334     plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3335   if (plugin->http_server_daemon_v6 != NULL)
3336     plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3337   
3338   
3339   if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3340     {
3341 #if DEBUG_HTTP
3342       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3343                   "Starting MHD with IPv4 bound to %s with port %u\n",
3344                   (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
3345 #endif
3346     }
3347   else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && 
3348             (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK) )
3349     {
3350 #if DEBUG_HTTP
3351       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3352                   "Starting MHD with IPv6 bound to %s with port %u\n",
3353                   (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3354 #endif
3355     }
3356   else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && 
3357             (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK) )
3358     {
3359 #if DEBUG_HTTP
3360       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3361                   "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3362                   (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", 
3363                   port);
3364 #endif
3365     }
3366   else
3367     {
3368       char * tmp = NULL;
3369       if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3370         GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3371       if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3372         GNUNET_asprintf(&tmp,"with IPv4 enabled");
3373       if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3374         GNUNET_asprintf(&tmp,"with IPv6 enabled");
3375       if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3376         GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3377       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3378                   _("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3379                   tmp, port, PROTOCOL_PREFIX);
3380       GNUNET_free (tmp);
3381       GNUNET_free (component_name);
3382       LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3383       return NULL;
3384     }
3385   
3386   /* Initializing cURL */
3387   curl_global_init(CURL_GLOBAL_ALL);
3388   plugin->multi_handle = curl_multi_init();
3389   
3390   if ( NULL == plugin->multi_handle )
3391     {
3392       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3393                        component_name,
3394                        _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3395                        PROTOCOL_PREFIX);
3396       GNUNET_free(component_name);
3397       LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3398       return NULL;
3399     }
3400   
3401   plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3402   
3403   GNUNET_free(component_name);
3404   GNUNET_SCHEDULER_add_now(address_notification, plugin);
3405   return api;
3406 }
3407
3408 /* end of plugin_transport_http.c */