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