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