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