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