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