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