remove port from transport section because it was getting in the way of making sense...
[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
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  * Callback called by MHD when a connection is terminated
725  * @param cls closure
726  * @param connection the terminated connection
727  * @httpSessionCache the mhd session reference
728  */
729 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
730 {
731   struct Session * ps = *httpSessionCache;
732   if (ps == NULL)
733     return;
734   struct HTTP_PeerContext * pc = ps->peercontext;
735
736   if (connection==ps->recv_endpoint)
737   {
738 #if DEBUG_CONNECTIONS
739     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
740 #endif
741     ps->recv_active = GNUNET_NO;
742     ps->recv_connected = GNUNET_NO;
743     ps->recv_endpoint = NULL;
744   }
745   if (connection==ps->send_endpoint)
746   {
747
748     ps->send_active = GNUNET_NO;
749     ps->send_connected = GNUNET_NO;
750     ps->send_endpoint = NULL;
751 #if DEBUG_CONNECTIONS
752     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
753 #endif
754   }
755
756   /* if both connections disconnected, remove session */
757   if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
758   {
759       GNUNET_STATISTICS_update (pc->plugin->env->stats,
760                             gettext_noop ("# HTTP inbound sessions for peers active"),
761                             -1,
762                             GNUNET_NO);
763     remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
764   }
765 }
766
767 /**
768  * Callback called by MessageStreamTokenizer when a message has arrived
769  * @param cls current session as closure
770  * @param client clien
771  * @param message the message to be forwarded to transport service
772  */
773
774 static void mhd_write_mst_cb (void *cls,
775                               void *client,
776                               const struct GNUNET_MessageHeader *message)
777 {
778
779   struct Session *ps  = cls;
780   GNUNET_assert(ps != NULL);
781
782   struct HTTP_PeerContext *pc = ps->peercontext;
783   GNUNET_assert(pc != NULL);
784 #if DEBUG_HTTP
785   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786               "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
787               ps,
788               ntohs(message->type),
789               ntohs(message->size),
790               GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
791 #endif
792   pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
793                             &pc->identity,
794                             message, 1, ps,
795                             NULL,
796                             0);
797 }
798
799 /**
800  * Check if incoming connection is accepted.
801  * NOTE: Here every connection is accepted
802  * @param cls plugin as closure
803  * @param addr address of incoming connection
804  * @param addr_len address length of incoming connection
805  * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
806  *
807  */
808 static int
809 mhd_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
810 {
811 #if 0
812   struct Plugin *plugin = cls;
813 #endif
814   /* Every connection is accepted, nothing more to do here */
815   return MHD_YES;
816 }
817
818
819 /**
820  * Callback called by MHD when it needs data to send
821  * @param cls current session
822  * @param pos position in buffer
823  * @param buf the buffer to write data to
824  * @param max max number of bytes available in buffer
825  * @return bytes written to buffer
826  */
827 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
828 {
829   struct Session * ps = cls;
830   struct HTTP_PeerContext * pc;
831   struct HTTP_Message * msg;
832   int bytes_read = 0;
833
834   GNUNET_assert (ps!=NULL);
835
836   pc = ps->peercontext;
837   msg = ps->pending_msgs_tail;
838   if (ps->send_force_disconnect==GNUNET_YES)
839   {
840 #if DEBUG_CONNECTIONS
841     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
842 #endif
843     return -1;
844   }
845
846   if (msg!=NULL)
847   {
848     if ((msg->size-msg->pos) <= max)
849     {
850       memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
851       bytes_read = msg->size-msg->pos;
852       msg->pos+=(msg->size-msg->pos);
853     }
854     else
855     {
856       memcpy(buf,&msg->buf[msg->pos],max);
857       msg->pos+=max;
858       bytes_read = max;
859     }
860
861     if (msg->pos==msg->size)
862     {
863       if (NULL!=msg->transmit_cont)
864         msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
865       remove_http_message(ps,msg);
866     }
867   }
868   return bytes_read;
869 }
870
871 /**
872  * Process GET or PUT request received via MHD.  For
873  * GET, queue response that will send back our pending
874  * messages.  For PUT, process incoming data and send
875  * to GNUnet core.  In either case, check if a session
876  * already exists and create a new one if not.
877  */
878 static int
879 mdh_access_cb (void *cls,
880                            struct MHD_Connection *mhd_connection,
881                            const char *url,
882                            const char *method,
883                            const char *version,
884                            const char *upload_data,
885                            size_t * upload_data_size, void **httpSessionCache)
886 {
887   struct Plugin *plugin = cls;
888   struct MHD_Response *response;
889   const union MHD_ConnectionInfo * conn_info;
890
891   struct sockaddr_in  *addrin;
892   struct sockaddr_in6 *addrin6;
893
894   char address[INET6_ADDRSTRLEN+14];
895   struct GNUNET_PeerIdentity pi_in;
896   size_t id_num = 0;
897
898   struct IPv4HttpAddress ipv4addr;
899   struct IPv6HttpAddress ipv6addr;
900
901   struct HTTP_PeerContext *pc;
902   struct Session *ps = NULL;
903   struct Session *ps_tmp = NULL;
904
905   int res = GNUNET_NO;
906   int send_error_to_client;
907   void * addr = NULL;
908   size_t addr_len = 0 ;
909
910   GNUNET_assert(cls !=NULL);
911   send_error_to_client = GNUNET_NO;
912
913   if (NULL == *httpSessionCache)
914   {
915     /* check url for peer identity , if invalid send HTTP 404*/
916     size_t len = strlen(&url[1]);
917     char * peer = GNUNET_malloc(104+1);
918
919     if ((len>104) && (url[104]==';'))
920     {
921         char * id = GNUNET_malloc((len-104)+1);
922         strcpy(id,&url[105]);
923         memcpy(peer,&url[1],103);
924         peer[103] = '\0';
925         id_num = strtoul ( id, NULL , 10);
926         GNUNET_free(id);
927     }
928     res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
929     GNUNET_free(peer);
930     if ( GNUNET_SYSERR == res )
931     {
932       response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
933       res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
934       MHD_destroy_response (response);
935 #if DEBUG_CONNECTIONS
936       if (res == MHD_YES)
937         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
938       else
939         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
940 #endif
941       return res;
942     }
943   }
944   else
945   {
946     ps = *httpSessionCache;
947     pc = ps->peercontext;
948   }
949
950   if (NULL == *httpSessionCache)
951   {
952     /* get peer context */
953     pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
954     /* Peer unknown */
955     if (pc==NULL)
956     {
957       pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
958       pc->plugin = plugin;
959       pc->session_id_counter=1;
960       pc->last_session = NULL;
961       memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
962       GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
963       GNUNET_STATISTICS_update (plugin->env->stats,
964                             gettext_noop ("# HTTP peers active"),
965                             1,
966                             GNUNET_NO);
967     }
968
969     conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
970     /* Incoming IPv4 connection */
971     if ( AF_INET == conn_info->client_addr->sin_family)
972     {
973       addrin = conn_info->client_addr;
974       inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
975       memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
976       ipv4addr.u_port = addrin->sin_port;
977       addr = &ipv4addr;
978       addr_len = sizeof(struct IPv4HttpAddress);
979     }
980     /* Incoming IPv6 connection */
981     if ( AF_INET6 == conn_info->client_addr->sin_family)
982     {
983       addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
984       inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
985       memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
986       ipv6addr.u6_port = addrin6->sin6_port;
987       addr = &ipv6addr;
988       addr_len = sizeof(struct IPv6HttpAddress);
989     }
990
991     GNUNET_assert (addr != NULL);
992     GNUNET_assert (addr_len != 0);
993
994     ps = NULL;
995     /* only inbound sessions here */
996
997     ps_tmp = pc->head;
998     while (ps_tmp!=NULL)
999     {
1000       if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1001       {
1002         if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1003         ps=ps_tmp;
1004         break;
1005       }
1006       ps_tmp=ps_tmp->next;
1007     }
1008
1009     if (ps==NULL)
1010     {
1011       ps = GNUNET_malloc(sizeof (struct Session));
1012       ps->addr = GNUNET_malloc(addr_len);
1013       memcpy(ps->addr,addr,addr_len);
1014       ps->addrlen = addr_len;
1015       ps->direction=INBOUND;
1016       ps->pending_msgs_head = NULL;
1017       ps->pending_msgs_tail = NULL;
1018       ps->send_connected=GNUNET_NO;
1019       ps->send_active=GNUNET_NO;
1020       ps->recv_connected=GNUNET_NO;
1021       ps->recv_active=GNUNET_NO;
1022       ps->peercontext=pc;
1023       ps->session_id =id_num;
1024       ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1025       GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1026       GNUNET_STATISTICS_update (plugin->env->stats,
1027                             gettext_noop ("# HTTP inbound sessions for peers active"),
1028                             1,
1029                             GNUNET_NO);
1030     }
1031
1032     *httpSessionCache = ps;
1033     if (ps->msgtok==NULL)
1034       ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1035 #if DEBUG_HTTP
1036     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1037                 ps,
1038                 method,
1039                 GNUNET_i2s(&pc->identity),
1040                 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1041 #endif
1042   }
1043
1044   /* Is it a PUT or a GET request */
1045   if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1046   {
1047     if (ps->recv_force_disconnect == GNUNET_YES)
1048     {
1049 #if DEBUG_CONNECTIONS
1050       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
1051 #endif
1052       ps->recv_active = GNUNET_NO;
1053       return MHD_NO;
1054     }
1055     if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1056     {
1057       ps->recv_endpoint = mhd_connection;
1058       ps->recv_connected = GNUNET_YES;
1059       ps->recv_active = GNUNET_YES;
1060       ps->recv_force_disconnect = GNUNET_NO;
1061 #if DEBUG_CONNECTIONS
1062       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
1063 #endif
1064       return MHD_YES;
1065     }
1066
1067     /* Transmission of all data complete */
1068     if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1069     {
1070       response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
1071       res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1072 #if DEBUG_CONNECTIONS
1073       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
1074 #endif
1075       MHD_destroy_response (response);
1076       ps->recv_active=GNUNET_NO;
1077       return MHD_YES;
1078     }
1079
1080     /* Recieving data */
1081     if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1082     {
1083       res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
1084       (*upload_data_size) = 0;
1085       return MHD_YES;
1086     }
1087     else
1088       return MHD_NO;
1089   }
1090   if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1091   {
1092     if (ps->send_force_disconnect == GNUNET_YES)
1093     {
1094 #if DEBUG_CONNECTIONS
1095       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was  forced to disconnect\n",ps);
1096 #endif
1097       ps->send_active = GNUNET_NO;
1098       return MHD_NO;
1099     }
1100           ps->send_connected = GNUNET_YES;
1101           ps->send_active = GNUNET_YES;
1102           ps->send_endpoint = mhd_connection;
1103           ps->send_force_disconnect = GNUNET_NO;
1104 #if DEBUG_CONNECTIONS
1105           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1106 #endif
1107           response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1108           res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1109           MHD_destroy_response (response);
1110           return MHD_YES;
1111   }
1112   return MHD_NO;
1113 }
1114
1115 /**
1116  * Function that queries MHD's select sets and
1117  * starts the task waiting for them.
1118  * @param plugin plugin
1119  * @param daemon_handle the MHD daemon handle
1120  * @return gnunet task identifier
1121  */
1122 static GNUNET_SCHEDULER_TaskIdentifier
1123 http_server_daemon_prepare (struct Plugin *plugin , struct MHD_Daemon *daemon_handle)
1124 {
1125   GNUNET_SCHEDULER_TaskIdentifier ret;
1126   fd_set rs;
1127   fd_set ws;
1128   fd_set es;
1129   struct GNUNET_NETWORK_FDSet *wrs;
1130   struct GNUNET_NETWORK_FDSet *wws;
1131   struct GNUNET_NETWORK_FDSet *wes;
1132   int max;
1133   unsigned long long timeout;
1134   int haveto;
1135   struct GNUNET_TIME_Relative tv;
1136
1137   ret = GNUNET_SCHEDULER_NO_TASK;
1138   FD_ZERO(&rs);
1139   FD_ZERO(&ws);
1140   FD_ZERO(&es);
1141   wrs = GNUNET_NETWORK_fdset_create ();
1142   wes = GNUNET_NETWORK_fdset_create ();
1143   wws = GNUNET_NETWORK_fdset_create ();
1144   max = -1;
1145   GNUNET_assert (MHD_YES ==
1146                  MHD_get_fdset (daemon_handle,
1147                                 &rs,
1148                                 &ws,
1149                                 &es,
1150                                 &max));
1151   haveto = MHD_get_timeout (daemon_handle, &timeout);
1152   if (haveto == MHD_YES)
1153     tv.value = (uint64_t) timeout;
1154   else
1155     tv = GNUNET_TIME_UNIT_FOREVER_REL;
1156   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1157   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1158   GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1159   if (daemon_handle == plugin->http_server_daemon_v4)
1160   {
1161         if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1162         {
1163                 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1164                 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1165         }
1166
1167     ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1168                                        GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1169                                        GNUNET_SCHEDULER_NO_TASK,
1170                                        tv,
1171                                        wrs,
1172                                        wws,
1173                                        &http_server_daemon_v4_run,
1174                                        plugin);
1175   }
1176   if (daemon_handle == plugin->http_server_daemon_v6)
1177   {
1178         if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1179         {
1180                 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1181                 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1182         }
1183
1184     ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1185                                        GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1186                                        GNUNET_SCHEDULER_NO_TASK,
1187                                        tv,
1188                                        wrs,
1189                                        wws,
1190                                        &http_server_daemon_v6_run,
1191                                        plugin);
1192   }
1193   GNUNET_NETWORK_fdset_destroy (wrs);
1194   GNUNET_NETWORK_fdset_destroy (wws);
1195   GNUNET_NETWORK_fdset_destroy (wes);
1196   return ret;
1197 }
1198
1199 /**
1200  * Call MHD IPv4 to process pending requests and then go back
1201  * and schedule the next run.
1202  * @param cls plugin as closure
1203  * @param tc task context
1204  */
1205 static void http_server_daemon_v4_run (void *cls,
1206                              const struct GNUNET_SCHEDULER_TaskContext *tc)
1207 {
1208   struct Plugin *plugin = cls;
1209
1210   GNUNET_assert(cls !=NULL);
1211   plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1212
1213   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1214     return;
1215
1216   GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1217   plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1218  }
1219
1220
1221 /**
1222  * Call MHD IPv6 to process pending requests and then go back
1223  * and schedule the next run.
1224  * @param cls plugin as closure
1225  * @param tc task context
1226  */
1227 static void http_server_daemon_v6_run (void *cls,
1228                              const struct GNUNET_SCHEDULER_TaskContext *tc)
1229 {
1230   struct Plugin *plugin = cls;
1231
1232   GNUNET_assert(cls !=NULL);
1233   plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1234
1235   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1236     return;
1237
1238   GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1239   plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1240 }
1241
1242 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1243 {
1244   struct Session * ps = stream;
1245
1246   long http_result = 0;
1247   int res;
1248   /* Getting last http result code */
1249   GNUNET_assert(NULL!=ps);
1250   if (ps->recv_connected==GNUNET_NO)
1251   {
1252     res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1253     if (CURLE_OK == res)
1254     {
1255       if (http_result == 200)
1256       {
1257         ps->recv_connected = GNUNET_YES;
1258         ps->recv_active = GNUNET_YES;
1259 #if DEBUG_CONNECTIONS
1260         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1261 #endif
1262         // Calling send_check_connections again since receive is established
1263         send_check_connections (ps->peercontext->plugin, ps);
1264       }
1265     }
1266   }
1267
1268 #if DEBUG_CURL
1269   char * tmp;
1270   size_t len = size * nmemb;
1271   tmp = NULL;
1272   if ((size * nmemb) < SIZE_MAX)
1273     tmp = GNUNET_malloc (len+1);
1274
1275   if ((tmp != NULL) && (len > 0))
1276   {
1277     memcpy(tmp,ptr,len);
1278     if (len>=2)
1279     {
1280       if (tmp[len-2] == 13)
1281         tmp[len-2]= '\0';
1282     }
1283     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1284   }
1285   GNUNET_free_non_null (tmp);
1286 #endif
1287
1288   return size * nmemb;
1289 }
1290
1291 /**
1292  * Callback called by libcurl when new headers arrive
1293  * Used to get HTTP result for curl operations
1294  * @param ptr stream to read from
1295  * @param size size of one char element
1296  * @param nmemb number of char elements
1297  * @param stream closure set by user
1298  * @return bytes read by function
1299  */
1300
1301 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1302 {
1303   struct Session * ps = stream;
1304
1305   char * tmp;
1306   size_t len = size * nmemb;
1307   long http_result = 0;
1308   int res;
1309
1310   /* Getting last http result code */
1311   GNUNET_assert(NULL!=ps);
1312   res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1313   if (CURLE_OK == res)
1314   {
1315     if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1316     {
1317       ps->send_connected = GNUNET_YES;
1318       ps->send_active = GNUNET_YES;
1319 #if DEBUG_CONNECTIONS
1320       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1321 #endif
1322     }
1323     if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1324     {
1325       ps->send_connected = GNUNET_NO;
1326       ps->send_active = GNUNET_NO;
1327 #if DEBUG_CONNECTIONS
1328       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1329 #endif
1330     }
1331   }
1332
1333   tmp = NULL;
1334   if ((size * nmemb) < SIZE_MAX)
1335     tmp = GNUNET_malloc (len+1);
1336
1337   if ((tmp != NULL) && (len > 0))
1338   {
1339     memcpy(tmp,ptr,len);
1340     if (len>=2)
1341     {
1342       if (tmp[len-2] == 13)
1343         tmp[len-2]= '\0';
1344     }
1345   }
1346
1347   GNUNET_free_non_null (tmp);
1348
1349   return size * nmemb;
1350 }
1351
1352 /**
1353  * Callback method used with libcurl
1354  * Method is called when libcurl needs to read data during sending
1355  * @param stream pointer where to write data
1356  * @param size size of an individual element
1357  * @param nmemb count of elements that can be written to the buffer
1358  * @param ptr source pointer, passed to the libcurl handle
1359  * @return bytes written to stream
1360  */
1361 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1362 {
1363   struct Session * ps = ptr;
1364   struct HTTP_Message * msg = ps->pending_msgs_tail;
1365   size_t bytes_sent;
1366   size_t len;
1367
1368   if (ps->send_active == GNUNET_NO)
1369   {
1370         return CURL_READFUNC_PAUSE;
1371   }
1372
1373   if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1374   {
1375 #if DEBUG_CONNECTIONS
1376     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1377 #endif
1378     ps->send_active = GNUNET_NO;
1379     return CURL_READFUNC_PAUSE;
1380   }
1381
1382   GNUNET_assert (msg!=NULL);
1383
1384   /* data to send */
1385   if (msg->pos < msg->size)
1386   {
1387     /* data fit in buffer */
1388     if ((msg->size - msg->pos) <= (size * nmemb))
1389     {
1390       len = (msg->size - msg->pos);
1391       memcpy(stream, &msg->buf[msg->pos], len);
1392       msg->pos += len;
1393       bytes_sent = len;
1394     }
1395     else
1396     {
1397       len = size*nmemb;
1398       memcpy(stream, &msg->buf[msg->pos], len);
1399       msg->pos += len;
1400       bytes_sent = len;
1401     }
1402   }
1403   /* no data to send */
1404   else
1405   {
1406     bytes_sent = 0;
1407   }
1408
1409   if ( msg->pos == msg->size)
1410   {
1411 #if DEBUG_CONNECTIONS
1412     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1413 #endif
1414     /* Calling transmit continuation  */
1415     if (NULL != ps->pending_msgs_tail->transmit_cont)
1416       msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1417     remove_http_message(ps, msg);
1418   }
1419   return bytes_sent;
1420 }
1421
1422 static void curl_receive_mst_cb  (void *cls,
1423                                 void *client,
1424                                 const struct GNUNET_MessageHeader *message)
1425 {
1426   struct Session *ps  = cls;
1427   GNUNET_assert(ps != NULL);
1428
1429   struct HTTP_PeerContext *pc = ps->peercontext;
1430   GNUNET_assert(pc != NULL);
1431 #if DEBUG_HTTP
1432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1433               "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1434               ps,
1435               ntohs(message->type),
1436               ntohs(message->size),
1437               GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1438 #endif
1439   pc->plugin->env->receive (pc->plugin->env->cls,
1440                             &pc->identity,
1441                             message, 1, ps,
1442                             ps->addr,
1443                             ps->addrlen);
1444 }
1445
1446
1447 /**
1448 * Callback method used with libcurl
1449 * Method is called when libcurl needs to write data during sending
1450 * @param stream pointer where to write data
1451 * @param size size of an individual element
1452 * @param nmemb count of elements that can be written to the buffer
1453 * @param ptr destination pointer, passed to the libcurl handle
1454 * @return bytes read from stream
1455 */
1456 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1457 {
1458   struct Session * ps = ptr;
1459 #if DEBUG_CONNECTIONS
1460   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1461 #endif
1462   GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1463   return (size * nmemb);
1464
1465 }
1466
1467 static void curl_handle_finished (struct Plugin *plugin)
1468 {
1469         struct Session *ps = NULL;
1470         struct HTTP_PeerContext *pc = NULL;
1471         struct CURLMsg *msg;
1472         struct HTTP_Message * cur_msg = NULL;
1473
1474         int msgs_in_queue;
1475         char * tmp;
1476         long http_result;
1477
1478         do
1479           {
1480                 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1481                 if ((msgs_in_queue == 0) || (msg == NULL))
1482                   break;
1483                 /* get session for affected curl handle */
1484                 GNUNET_assert ( msg->easy_handle != NULL );
1485                 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1486                 ps = (struct Session *) tmp;
1487                 GNUNET_assert ( ps != NULL );
1488                 pc = ps->peercontext;
1489                 GNUNET_assert ( pc != NULL );
1490                 switch (msg->msg)
1491                   {
1492
1493                   case CURLMSG_DONE:
1494                         if ( (msg->data.result != CURLE_OK) &&
1495                                  (msg->data.result != CURLE_GOT_NOTHING) )
1496                         {
1497                           /* sending msg failed*/
1498                           if (msg->easy_handle == ps->send_endpoint)
1499                           {
1500         #if DEBUG_CONNECTIONS
1501                                 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1502                                                    _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1503                                                    ps,
1504                                                    GNUNET_i2s(&pc->identity),
1505                                                    http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1506                                                    "curl_multi_perform",
1507                                                    curl_easy_strerror (msg->data.result));
1508         #endif
1509                                 ps->send_connected = GNUNET_NO;
1510                                 ps->send_active = GNUNET_NO;
1511                                 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1512                                 //curl_easy_cleanup(ps->send_endpoint);
1513                                 //ps->send_endpoint=NULL;
1514                                 cur_msg = ps->pending_msgs_tail;
1515                                 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1516                                   cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1517                           }
1518                           /* GET connection failed */
1519                           if (msg->easy_handle == ps->recv_endpoint)
1520                           {
1521         #if DEBUG_CONNECTIONS
1522                                 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1523                                          _("Connection %X: HTTP GET 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->recv_connected = GNUNET_NO;
1531                                 ps->recv_active = GNUNET_NO;
1532                                 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1533                                 //curl_easy_cleanup(ps->recv_endpoint);
1534                                 //ps->recv_endpoint=NULL;
1535                           }
1536                         }
1537                         else
1538                         {
1539                           if (msg->easy_handle == ps->send_endpoint)
1540                           {
1541                                 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1542         #if DEBUG_CONNECTIONS
1543                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544                                                         "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1545                                                          ps,
1546                                                          GNUNET_i2s(&pc->identity),
1547                                                          http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1548                                                          http_result);
1549         #endif
1550                                 /* Calling transmit continuation  */
1551                                 cur_msg = ps->pending_msgs_tail;
1552                                 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1553                                 {
1554                                   /* HTTP 1xx : Last message before here was informational */
1555                                   if ((http_result >=100) && (http_result < 200))
1556                                         cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1557                                   /* HTTP 2xx: successful operations */
1558                                   if ((http_result >=200) && (http_result < 300))
1559                                         cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1560                                   /* HTTP 3xx..5xx: error */
1561                                   if ((http_result >=300) && (http_result < 600))
1562                                         cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1563                                 }
1564                                 ps->send_connected = GNUNET_NO;
1565                                 ps->send_active = GNUNET_NO;
1566                                 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1567                                 //curl_easy_cleanup(ps->send_endpoint);
1568                                 //ps->send_endpoint =NULL;
1569                           }
1570                           if (msg->easy_handle == ps->recv_endpoint)
1571                           {
1572         #if DEBUG_CONNECTIONS
1573                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574                                                         "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1575                                                          ps,
1576                                                          GNUNET_i2s(&pc->identity),
1577                                                          http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1578                                                          http_result);
1579         #endif
1580                                 ps->recv_connected = GNUNET_NO;
1581                                 ps->recv_active = GNUNET_NO;
1582                                 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1583                                 //curl_easy_cleanup(ps->recv_endpoint);
1584                                 //ps->recv_endpoint=NULL;
1585                           }
1586                         }
1587                         if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1588                           remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1589                         break;
1590                   default:
1591                         break;
1592                   }
1593           }
1594         while ( (msgs_in_queue > 0) );
1595 }
1596
1597
1598 /**
1599  * Task performing curl operations
1600  * @param cls plugin as closure
1601  * @param tc gnunet scheduler task context
1602  */
1603 static void curl_perform (void *cls,
1604              const struct GNUNET_SCHEDULER_TaskContext *tc)
1605 {
1606   struct Plugin *plugin = cls;
1607   static unsigned int handles_last_run;
1608   int running;
1609   CURLMcode mret;
1610
1611   GNUNET_assert(cls !=NULL);
1612
1613   plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1614   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1615     return;
1616   do
1617     {
1618       running = 0;
1619       mret = curl_multi_perform (plugin->multi_handle, &running);
1620       if ((running < handles_last_run) && (running>0))
1621           curl_handle_finished(plugin);
1622       handles_last_run = running;
1623     }
1624   while (mret == CURLM_CALL_MULTI_PERFORM);
1625   curl_schedule(plugin);
1626 }
1627
1628
1629 /**
1630  * Function setting up file descriptors and scheduling task to run
1631  *
1632  * @param cls plugin as closure
1633  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1634  */
1635 static int curl_schedule(struct Plugin *plugin)
1636 {
1637   fd_set rs;
1638   fd_set ws;
1639   fd_set es;
1640   int max;
1641   struct GNUNET_NETWORK_FDSet *grs;
1642   struct GNUNET_NETWORK_FDSet *gws;
1643   long to;
1644   CURLMcode mret;
1645
1646   /* Cancel previous scheduled task */
1647   if (plugin->http_curl_task !=  GNUNET_SCHEDULER_NO_TASK)
1648   {
1649           GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1650           plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1651   }
1652
1653   max = -1;
1654   FD_ZERO (&rs);
1655   FD_ZERO (&ws);
1656   FD_ZERO (&es);
1657   mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1658   if (mret != CURLM_OK)
1659     {
1660       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1661                   _("%s failed at %s:%d: `%s'\n"),
1662                   "curl_multi_fdset", __FILE__, __LINE__,
1663                   curl_multi_strerror (mret));
1664       return GNUNET_SYSERR;
1665     }
1666   mret = curl_multi_timeout (plugin->multi_handle, &to);
1667   if (mret != CURLM_OK)
1668     {
1669       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1670                   _("%s failed at %s:%d: `%s'\n"),
1671                   "curl_multi_timeout", __FILE__, __LINE__,
1672                   curl_multi_strerror (mret));
1673       return GNUNET_SYSERR;
1674     }
1675
1676   grs = GNUNET_NETWORK_fdset_create ();
1677   gws = GNUNET_NETWORK_fdset_create ();
1678   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1679   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1680   plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1681                                    GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1682                                    GNUNET_SCHEDULER_NO_TASK,
1683                                                                     (to == -1) ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
1684                                    grs,
1685                                    gws,
1686                                    &curl_perform,
1687                                    plugin);
1688   GNUNET_NETWORK_fdset_destroy (gws);
1689   GNUNET_NETWORK_fdset_destroy (grs);
1690   return GNUNET_OK;
1691 }
1692
1693 /**
1694  * Function to log curl debug messages with GNUNET_log
1695  * @param curl handle
1696  * @param type curl_infotype
1697  * @param data data
1698  * @param size size
1699  * @param cls  closure
1700  * @return 0
1701  */
1702 int curl_logger (CURL * curl, curl_infotype type , char * data, size_t size , void * cls)
1703 {
1704         char * text = GNUNET_malloc(size+2);
1705         if (type == CURLINFO_TEXT)
1706         {
1707                 memcpy(text,data,size);
1708                 if (text[size-1] == '\n')
1709                         text[size] = '\0';
1710                 else
1711                 {
1712                         text[size] = '\n';
1713                         text[size+1] = '\0';
1714                 }
1715                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"CURL: Connection %X - %s", cls, text);
1716                 GNUNET_free(text);
1717         }
1718         return 0;
1719 }
1720
1721 /**
1722  * Function setting up curl handle and selecting message to send
1723  *
1724  * @param plugin plugin
1725  * @param ps session
1726  * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1727  */
1728 static int send_check_connections (struct Plugin *plugin, struct Session *ps)
1729 {
1730   CURLMcode mret;
1731   struct HTTP_Message * msg;
1732
1733   struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1734
1735   if (ps->direction == OUTBOUND)
1736   {
1737     /* RECV DIRECTION */
1738     /* Check if session is connected to receive data, otherwise connect to peer */
1739     if (ps->recv_connected == GNUNET_NO)
1740     {
1741         int fresh = GNUNET_NO;
1742         if (ps->recv_endpoint == NULL)
1743         {
1744             fresh = GNUNET_YES;
1745                 ps->recv_endpoint = curl_easy_init();
1746         }
1747 #if DEBUG_CURL
1748         curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1749         curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
1750         curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
1751 #endif
1752 #if BUILD_HTTPS
1753         curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1754                 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1755                 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1756 #endif
1757         curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1758         curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1759         curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1760         curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1761         curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1762         curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1763         curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1764         curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1765         curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1766         curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1767         curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
1768 #if CURL_TCP_NODELAY
1769         curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1770 #endif
1771
1772         if (fresh==GNUNET_YES)
1773         {
1774                         mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1775                         if (mret != CURLM_OK)
1776                         {
1777                           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1778                                                   _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1779                                                   ps,
1780                                                   "curl_multi_add_handle", __FILE__, __LINE__,
1781                                                   curl_multi_strerror (mret));
1782                           return GNUNET_SYSERR;
1783                         }
1784         }
1785                 if (plugin->http_curl_task !=  GNUNET_SCHEDULER_NO_TASK)
1786                 {
1787                   GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1788                   plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1789                 }
1790                 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
1791     }
1792
1793     /* waiting for receive direction */
1794     if (ps->recv_connected==GNUNET_NO)
1795       return GNUNET_NO;
1796
1797     /* SEND DIRECTION */
1798     /* Check if session is connected to send data, otherwise connect to peer */
1799     if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1800     {
1801       if (ps->send_active == GNUNET_YES)
1802       {
1803 #if DEBUG_CONNECTIONS
1804         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1805 #endif
1806         return GNUNET_YES;
1807       }
1808       if (ps->send_active == GNUNET_NO)
1809       {
1810 #if DEBUG_CONNECTIONS
1811         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1812 #endif
1813         if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1814         {
1815                         ps->send_active=GNUNET_YES;
1816                         if (plugin->http_curl_task !=  GNUNET_SCHEDULER_NO_TASK)
1817                         {
1818                           GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1819                           plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1820                         }
1821                         plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
1822                         return GNUNET_YES;
1823         }
1824         else
1825                 return GNUNET_SYSERR;
1826       }
1827     }
1828     /* not connected, initiate connection */
1829     if (ps->send_connected==GNUNET_NO)
1830     {
1831         int fresh = GNUNET_NO;
1832         if (NULL == ps->send_endpoint)
1833         {
1834                 ps->send_endpoint = curl_easy_init();
1835                 fresh = GNUNET_YES;
1836         }
1837                 GNUNET_assert (ps->send_endpoint != NULL);
1838                 GNUNET_assert (NULL != ps->pending_msgs_tail);
1839 #if DEBUG_CONNECTIONS
1840                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1841 #endif
1842                 ps->send_active = GNUNET_NO;
1843                 msg = ps->pending_msgs_tail;
1844
1845 #if DEBUG_CURL
1846                 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1847         curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
1848         curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
1849 #endif
1850 #if BUILD_HTTPS
1851         curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1852                 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1853                 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1854 #endif
1855                 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1856                 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1857                 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1858                 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1859                 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1860                 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1861                 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1862                 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1863                 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1864                 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1865                 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1866                 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1867 #if CURL_TCP_NODELAY
1868                 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
1869 #endif
1870
1871                 if (fresh==GNUNET_YES)
1872                 {
1873                         mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1874                         if (mret != CURLM_OK)
1875                         {
1876                           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1877                                                   _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1878                                                   ps,
1879                                                   "curl_multi_add_handle", __FILE__, __LINE__,
1880                                                   curl_multi_strerror (mret));
1881                           return GNUNET_SYSERR;
1882                         }
1883                 }
1884     }
1885         if (plugin->http_curl_task !=  GNUNET_SCHEDULER_NO_TASK)
1886         {
1887           GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1888           plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1889         }
1890         plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
1891     return GNUNET_YES;
1892   }
1893   if (ps->direction == INBOUND)
1894   {
1895     GNUNET_assert (NULL != ps->pending_msgs_tail);
1896     if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1897         (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1898         return GNUNET_YES;
1899   }
1900   return GNUNET_SYSERR;
1901 }
1902
1903 /**
1904  * select best session to transmit data to peer
1905  *
1906  * @param cls closure
1907  * @param pc peer context of target peer
1908  * @param addr address of target peer
1909  * @param addrlen address length
1910  * @param force_address does transport service enforce address?
1911  * @param session session passed by transport service
1912  * @return selected session
1913  *
1914  */
1915 static struct Session * send_select_session (struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1916 {
1917         struct Session * tmp = NULL;
1918         int addr_given = GNUNET_NO;
1919
1920         if ((addr!=NULL) && (addrlen>0))
1921                 addr_given = GNUNET_YES;
1922
1923         if (force_address == GNUNET_YES)
1924         {
1925                 /* check session given as argument */
1926                 if ((session != NULL) && (addr_given == GNUNET_YES))
1927                 {
1928                       if (0 == memcmp(session->addr, addr, addrlen))
1929                       {
1930                         /* connection can not be used, since it is disconnected */
1931                         if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1932                         {
1933 #if DEBUG_SESSION_SELECTION
1934                                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1935 #endif
1936                                 return session;
1937                         }
1938                       }
1939                 }
1940                 /* check last session used */
1941                 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1942                 {
1943                       if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1944                       {
1945                         /* connection can not be used, since it is disconnected */
1946                         if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1947                         {
1948 #if DEBUG_SESSION_SELECTION
1949                                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1950 #endif
1951                                 return pc->last_session;
1952                         }
1953                       }
1954                 }
1955                 /* find session in existing sessions */
1956                 tmp = pc->head;
1957                 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1958                 {
1959
1960                           if (0 == memcmp(tmp->addr, addr, addrlen))
1961                       {
1962                         /* connection can not be used, since it is disconnected */
1963                         if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1964                         {
1965 #if DEBUG_SESSION_SELECTION
1966                                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1967 #endif
1968                                   return session;
1969                         }
1970
1971                       }
1972                           tmp=tmp->next;
1973                 }
1974                 /* no session to use */
1975                 return NULL;
1976         }
1977         if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1978         {
1979                 /* check session given as argument */
1980                 if (session != NULL)
1981                 {
1982                         /* connection can not be used, since it is disconnected */
1983                         if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1984                         {
1985 #if DEBUG_SESSION_SELECTION
1986                                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1987 #endif
1988                                   return session;
1989                         }
1990
1991                 }
1992                 /* check last session used */
1993                 if (pc->last_session != NULL)
1994                 {
1995                         /* connection can not be used, since it is disconnected */
1996                         if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1997                         {
1998 #if DEBUG_SESSION_SELECTION
1999                                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
2000 #endif
2001                                 return pc->last_session;
2002                         }
2003                 }
2004                 /* find session in existing sessions */
2005                 tmp = pc->head;
2006                 while (tmp!=NULL)
2007                 {
2008                         /* connection can not be used, since it is disconnected */
2009                         if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
2010                         {
2011 #if DEBUG_SESSION_SELECTION
2012                                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
2013 #endif
2014                                 return tmp;
2015                         }
2016                         tmp=tmp->next;
2017                 }
2018                 return NULL;
2019         }
2020         return NULL;
2021 }
2022
2023 /**
2024  * Function that can be used by the transport service to transmit
2025  * a message using the plugin.   Note that in the case of a
2026  * peer disconnecting, the continuation MUST be called
2027  * prior to the disconnect notification itself.  This function
2028  * will be called with this peer's HELLO message to initiate
2029  * a fresh connection to another peer.
2030  *
2031  * @param cls closure
2032  * @param target who should receive this message
2033  * @param msgbuf the message to transmit
2034  * @param msgbuf_size number of bytes in 'msgbuf'
2035  * @param priority how important is the message (most plugins will
2036  *                 ignore message priority and just FIFO)
2037  * @param to how long to wait at most for the transmission (does not
2038  *                require plugins to discard the message after the timeout,
2039  *                just advisory for the desired delay; most plugins will ignore
2040  *                this as well)
2041  * @param session which session must be used (or NULL for "any")
2042  * @param addr the address to use (can be NULL if the plugin
2043  *                is "on its own" (i.e. re-use existing TCP connection))
2044  * @param addrlen length of the address in bytes
2045  * @param force_address GNUNET_YES if the plugin MUST use the given address,
2046  *                GNUNET_NO means the plugin may use any other address and
2047  *                GNUNET_SYSERR means that only reliable existing
2048  *                bi-directional connections should be used (regardless
2049  *                of address)
2050  * @param cont continuation to call once the message has
2051  *        been transmitted (or if the transport is ready
2052  *        for the next transmission call; or if the
2053  *        peer disconnected...); can be NULL
2054  * @param cont_cls closure for cont
2055  * @return number of bytes used (on the physical network, with overheads);
2056  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
2057  *         and does NOT mean that the message was not transmitted (DV)
2058  */
2059 static ssize_t
2060 http_plugin_send (void *cls,
2061                   const struct GNUNET_PeerIdentity *target,
2062                   const char *msgbuf,
2063                   size_t msgbuf_size,
2064                   unsigned int priority,
2065                   struct GNUNET_TIME_Relative to,
2066                   struct Session *session,
2067                   const void *addr,
2068                   size_t addrlen,
2069                   int force_address,
2070                   GNUNET_TRANSPORT_TransmitContinuation cont,
2071                   void *cont_cls)
2072 {
2073   struct Plugin *plugin = cls;
2074   struct HTTP_Message *msg;
2075   struct HTTP_PeerContext * pc;
2076   struct Session * ps = NULL;
2077
2078   GNUNET_assert(cls !=NULL);
2079
2080 #if DEBUG_HTTP
2081   char * force;
2082   if (force_address == GNUNET_YES)
2083           GNUNET_asprintf(&force, "forced addr.");
2084   if (force_address == GNUNET_NO)
2085           GNUNET_asprintf(&force, "any addr.");
2086   if (force_address == GNUNET_SYSERR)
2087           GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2088
2089   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2090                                       msgbuf_size,
2091                                       GNUNET_i2s(target),
2092                                       force,
2093                                       http_plugin_address_to_string(NULL, addr, addrlen),
2094                                       session);
2095
2096   GNUNET_free(force);
2097 #endif
2098
2099   pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2100   /* Peer unknown */
2101   if (pc==NULL)
2102   {
2103     pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2104     pc->plugin = plugin;
2105     pc->session_id_counter=1;
2106     pc->last_session = NULL;
2107     memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2108     GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2109     GNUNET_STATISTICS_update (plugin->env->stats,
2110                             gettext_noop ("# HTTP peers active"),
2111                             1,
2112                             GNUNET_NO);
2113   }
2114
2115   ps = send_select_session (pc, addr, addrlen, force_address, session);
2116
2117   /* session not existing, but address forced -> creating new session */
2118   if (ps==NULL)
2119   {
2120         if ((addr!=NULL) && (addrlen!=0))
2121         {
2122       ps = GNUNET_malloc(sizeof (struct Session));
2123 #if DEBUG_SESSION_SELECTION
2124       if (force_address == GNUNET_YES)
2125          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2126       if (force_address != GNUNET_YES)
2127          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2128 #endif
2129       if ((addrlen!=0) && (addr!=NULL))
2130       {
2131          ps->addr = GNUNET_malloc(addrlen);
2132          memcpy(ps->addr,addr,addrlen);
2133          ps->addrlen = addrlen;
2134       }
2135           else
2136           {
2137                 ps->addr = NULL;
2138                 ps->addrlen = 0;
2139           }
2140           ps->direction=OUTBOUND;
2141           ps->recv_connected = GNUNET_NO;
2142           ps->recv_force_disconnect = GNUNET_NO;
2143           ps->send_connected = GNUNET_NO;
2144           ps->send_force_disconnect = GNUNET_NO;
2145           ps->pending_msgs_head = NULL;
2146           ps->pending_msgs_tail = NULL;
2147           ps->peercontext=pc;
2148           ps->session_id = pc->session_id_counter;
2149           pc->session_id_counter++;
2150           ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2151           if (ps->msgtok == NULL)
2152                         ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2153           GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2154           GNUNET_STATISTICS_update (plugin->env->stats,
2155                                                                 gettext_noop ("# HTTP outbound sessions for peers active"),
2156                                                                 1,
2157                                                                 GNUNET_NO);
2158         }
2159         else
2160         {
2161 #if DEBUG_HTTP
2162                 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));
2163 #endif
2164                 return GNUNET_SYSERR;
2165     }
2166   }
2167
2168   /* create msg */
2169   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2170   msg->next = NULL;
2171   msg->size = msgbuf_size;
2172   msg->pos = 0;
2173   msg->buf = (char *) &msg[1];
2174   msg->transmit_cont = cont;
2175   msg->transmit_cont_cls = cont_cls;
2176   memcpy (msg->buf,msgbuf, msgbuf_size);
2177   GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
2178
2179   if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2180           return GNUNET_SYSERR;
2181           if (force_address != GNUNET_YES)
2182                   pc->last_session = ps;
2183
2184           if (pc->last_session==NULL)
2185                   pc->last_session = ps;
2186           return msg->size;
2187 }
2188
2189
2190
2191 /**
2192  * Function that can be used to force the plugin to disconnect
2193  * from the given peer and cancel all previous transmissions
2194  * (and their continuationc).
2195  *
2196  * @param cls closure
2197  * @param target peer from which to disconnect
2198  */
2199 static void
2200 http_plugin_disconnect (void *cls,
2201                             const struct GNUNET_PeerIdentity *target)
2202 {
2203
2204
2205   struct Plugin *plugin = cls;
2206   struct HTTP_PeerContext *pc = NULL;
2207   struct Session *ps = NULL;
2208   //struct Session *tmp = NULL;
2209
2210   pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2211   if (pc==NULL)
2212     return;
2213   ps = pc->head;
2214
2215   while (ps!=NULL)
2216   {
2217     /* Telling transport that session is getting disconnected */
2218     plugin->env->session_end(plugin, target, ps);
2219     if (ps->direction==OUTBOUND)
2220     {
2221       if (ps->send_endpoint!=NULL)
2222       {
2223         //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2224         //curl_easy_cleanup(ps->send_endpoint);
2225         //ps->send_endpoint=NULL;
2226         ps->send_force_disconnect = GNUNET_YES;
2227       }
2228       if (ps->recv_endpoint!=NULL)
2229       {
2230        //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2231        //curl_easy_cleanup(ps->recv_endpoint);
2232        //ps->recv_endpoint=NULL;
2233        ps->recv_force_disconnect = GNUNET_YES;
2234       }
2235     }
2236
2237     if (ps->direction==INBOUND)
2238     {
2239       ps->recv_force_disconnect = GNUNET_YES;
2240       ps->send_force_disconnect = GNUNET_YES;
2241     }
2242
2243     while (ps->pending_msgs_head!=NULL)
2244     {
2245       remove_http_message(ps, ps->pending_msgs_head);
2246     }
2247     ps->recv_active = GNUNET_NO;
2248     ps->send_active = GNUNET_NO;
2249     ps=ps->next;
2250   }
2251 }
2252
2253
2254 /**
2255  * Convert the transports address to a nice, human-readable
2256  * format.
2257  *
2258  * @param cls closure
2259  * @param type name of the transport that generated the address
2260  * @param addr one of the addresses of the host, NULL for the last address
2261  *        the specific address format depends on the transport
2262  * @param addrlen length of the address
2263  * @param numeric should (IP) addresses be displayed in numeric form?
2264  * @param timeout after how long should we give up?
2265  * @param asc function to call on each string
2266  * @param asc_cls closure for asc
2267  */
2268 static void
2269 http_plugin_address_pretty_printer (void *cls,
2270                                         const char *type,
2271                                         const void *addr,
2272                                         size_t addrlen,
2273                                         int numeric,
2274                                         struct GNUNET_TIME_Relative timeout,
2275                                         GNUNET_TRANSPORT_AddressStringCallback
2276                                         asc, void *asc_cls)
2277 {
2278   const struct IPv4HttpAddress *t4;
2279   const struct IPv6HttpAddress *t6;
2280   struct sockaddr_in a4;
2281   struct sockaddr_in6 a6;
2282   char * address;
2283   char * ret;
2284   unsigned int port;
2285   unsigned int res;
2286
2287   GNUNET_assert(cls !=NULL);
2288   if (addrlen == sizeof (struct IPv6HttpAddress))
2289   {
2290     address = GNUNET_malloc (INET6_ADDRSTRLEN);
2291     t6 = addr;
2292     a6.sin6_addr = t6->ipv6_addr;
2293     inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2294     port = ntohs(t6->u6_port);
2295   }
2296   else if (addrlen == sizeof (struct IPv4HttpAddress))
2297   {
2298     address = GNUNET_malloc (INET_ADDRSTRLEN);
2299     t4 = addr;
2300     a4.sin_addr.s_addr =  t4->ipv4_addr;
2301     inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2302     port = ntohs(t4->u_port);
2303   }
2304   else
2305   {
2306     /* invalid address */
2307     GNUNET_break_op (0);
2308     asc (asc_cls, NULL);
2309     return;
2310   }
2311   res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2312   GNUNET_free (address);
2313   GNUNET_assert(res != 0);
2314   asc (asc_cls, ret);
2315   GNUNET_free_non_null (ret);
2316 }
2317
2318
2319
2320 /**
2321  * Another peer has suggested an address for this
2322  * peer and transport plugin.  Check that this could be a valid
2323  * address.  If so, consider adding it to the list
2324  * of addresses.
2325  *
2326  * @param cls closure
2327  * @param addr pointer to the address
2328  * @param addrlen length of addr
2329  * @return GNUNET_OK if this is a plausible address for this peer
2330  *         and transport
2331  */
2332 static int
2333 http_plugin_address_suggested (void *cls,
2334                                const void *addr, size_t addrlen)
2335 {
2336   struct Plugin *plugin = cls;
2337   struct IPv4HttpAddress *v4;
2338   struct IPv6HttpAddress *v6;
2339   unsigned int port;
2340
2341   GNUNET_assert(cls !=NULL);
2342   if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2343       (addrlen != sizeof (struct IPv6HttpAddress)))
2344     {
2345       return GNUNET_SYSERR;
2346     }
2347   if (addrlen == sizeof (struct IPv4HttpAddress))
2348     {
2349       v4 = (struct IPv4HttpAddress *) addr;
2350       /* Not skipping loopback
2351       if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2352       {
2353         return GNUNET_SYSERR;
2354       } */
2355       port = ntohs (v4->u_port);
2356       if (port != plugin->port_inbound)
2357       {
2358         return GNUNET_SYSERR;
2359       }
2360     }
2361   if (addrlen == sizeof (struct IPv6HttpAddress))
2362     {
2363       v6 = (struct IPv6HttpAddress *) addr;
2364       if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2365         {
2366           return GNUNET_SYSERR;
2367         }
2368       port = ntohs (v6->u6_port);
2369       if (port != plugin->port_inbound)
2370       {
2371         return GNUNET_SYSERR;
2372       }
2373     }
2374
2375   return GNUNET_OK;
2376 }
2377
2378
2379 /**
2380  * Function called for a quick conversion of the binary address to
2381  * a numeric address.  Note that the caller must not free the
2382  * address and that the next call to this function is allowed
2383  * to override the address again.
2384  *
2385  * @param cls closure
2386  * @param addr binary address
2387  * @param addrlen length of the address
2388  * @return string representing the same address
2389  */
2390 static const char*
2391 http_plugin_address_to_string (void *cls,
2392                                    const void *addr,
2393                                    size_t addrlen)
2394 {
2395   const struct IPv4HttpAddress *t4;
2396   const struct IPv6HttpAddress *t6;
2397   struct sockaddr_in a4;
2398   struct sockaddr_in6 a6;
2399   char * address;
2400   char * ret;
2401   uint16_t port;
2402   unsigned int res;
2403
2404   if (addrlen == sizeof (struct IPv6HttpAddress))
2405     {
2406       address = GNUNET_malloc (INET6_ADDRSTRLEN);
2407       t6 = addr;
2408       a6.sin6_addr = t6->ipv6_addr;
2409       inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2410       port = ntohs(t6->u6_port);
2411     }
2412   else if (addrlen == sizeof (struct IPv4HttpAddress))
2413     {
2414       address = GNUNET_malloc (INET_ADDRSTRLEN);
2415       t4 = addr;
2416       a4.sin_addr.s_addr =  t4->ipv4_addr;
2417       inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2418       port = ntohs(t4->u_port);
2419     }
2420   else
2421     {
2422       /* invalid address */
2423       return NULL;
2424     }
2425   res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2426   GNUNET_free (address);
2427   GNUNET_assert(res != 0);
2428   return ret;
2429 }
2430
2431
2432 /**
2433  * Exit point from the plugin.
2434  */
2435 void *
2436 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2437 {
2438   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2439   struct Plugin *plugin = api->cls;
2440   CURLMcode mret;
2441   GNUNET_assert(cls !=NULL);
2442
2443   if (plugin->http_server_daemon_v4 != NULL)
2444   {
2445     MHD_stop_daemon (plugin->http_server_daemon_v4);
2446     plugin->http_server_daemon_v4 = NULL;
2447   }
2448   if (plugin->http_server_daemon_v6 != NULL)
2449   {
2450     MHD_stop_daemon (plugin->http_server_daemon_v6);
2451     plugin->http_server_daemon_v6 = NULL;
2452   }
2453
2454   if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2455   {
2456     GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2457     plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2458   }
2459
2460   if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2461   {
2462     GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2463     plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2464   }
2465
2466   /* free all peer information */
2467   if (plugin->peers!=NULL)
2468   {
2469           GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2470                                                                                          &remove_peer_context_Iterator,
2471                                                                                          plugin);
2472           GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2473   }
2474   if (plugin->multi_handle!=NULL)
2475   {
2476           mret = curl_multi_cleanup(plugin->multi_handle);
2477 #if DEBUG_HTTP
2478           if ( CURLM_OK != mret)
2479                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2480 #endif
2481           plugin->multi_handle = NULL;
2482   }
2483   curl_global_cleanup();
2484
2485   if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2486   {
2487     GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2488     plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2489   }
2490
2491   GNUNET_free_non_null (plugin->bind4_address);
2492   GNUNET_free_non_null (plugin->bind6_address);
2493   GNUNET_free_non_null(plugin->bind_hostname);
2494 #if BUILD_HTTPS
2495   GNUNET_free_non_null (plugin->crypto_init);
2496   GNUNET_free_non_null (plugin->cert);
2497   GNUNET_free_non_null (plugin->key);
2498 #endif
2499   GNUNET_free (plugin);
2500   GNUNET_free (api);
2501 #if DEBUG_HTTP
2502   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload %s plugin complete...\n", PROTOCOL_PREFIX);
2503 #endif
2504   return NULL;
2505 }
2506
2507 #if BUILD_HTTPS
2508 static char *
2509 load_certificate( const char * file )
2510 {
2511   struct GNUNET_DISK_FileHandle * gn_file;
2512
2513   struct stat fstat;
2514   char * text = NULL;
2515
2516   if (0!=STAT(file, &fstat))
2517           return NULL;
2518   text = GNUNET_malloc (fstat.st_size+1);
2519   gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2520   if (gn_file==NULL)
2521   {
2522           GNUNET_free(text);
2523           return NULL;
2524   }
2525   if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2526   {
2527           GNUNET_free(text);
2528           GNUNET_DISK_file_close(gn_file);
2529           return NULL;
2530   }
2531   text[fstat.st_size] = '\0';
2532   GNUNET_DISK_file_close(gn_file);
2533
2534   return text;
2535 }
2536 #endif
2537
2538
2539 /**
2540  * Entry point for the plugin.
2541  */
2542 void *
2543 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2544 {
2545   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2546   struct Plugin *plugin;
2547   struct GNUNET_TRANSPORT_PluginFunctions *api;
2548   struct GNUNET_TIME_Relative gn_timeout;
2549   long long unsigned int port;
2550   char * component_name;
2551 #if BUILD_HTTPS
2552   char * key_file = NULL;
2553   char * cert_file = NULL;
2554 #endif
2555
2556   GNUNET_assert(cls !=NULL);
2557 #if DEBUG_HTTP
2558   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting %s plugin...\n", PROTOCOL_PREFIX);
2559 #endif
2560   GNUNET_asprintf(&component_name,"transport-%s",PROTOCOL_PREFIX);
2561
2562   plugin = GNUNET_malloc (sizeof (struct Plugin));
2563   plugin->stats = env->stats;
2564   plugin->env = env;
2565   plugin->peers = NULL;
2566   plugin->bind4_address = NULL;
2567   plugin->use_ipv6  = GNUNET_YES;
2568   plugin->use_ipv4  = GNUNET_YES;
2569
2570   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2571   api->cls = plugin;
2572   api->send = &http_plugin_send;
2573   api->disconnect = &http_plugin_disconnect;
2574   api->address_pretty_printer = &http_plugin_address_pretty_printer;
2575   api->check_address = &http_plugin_address_suggested;
2576   api->address_to_string = &http_plugin_address_to_string;
2577
2578   /* Hashing our identity to use it in URLs */
2579   GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2580
2581   /* Use IPv6? */
2582   if (GNUNET_CONFIGURATION_have_value (env->cfg,
2583                                                                            component_name, "USE_IPv6"))
2584     {
2585           plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2586                                                                                                                            component_name,
2587                                                                                                                            "USE_IPv6");
2588     }
2589   /* Use IPv4? */
2590   if (GNUNET_CONFIGURATION_have_value (env->cfg,
2591                                                                            component_name, "USE_IPv4"))
2592     {
2593           plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2594                                                         component_name,"USE_IPv4");
2595     }
2596   /* Reading port number from config file */
2597   if ((GNUNET_OK !=
2598        GNUNET_CONFIGURATION_get_value_number (env->cfg,
2599                                                                                           component_name,
2600                                               "PORT",
2601                                               &port)) ||
2602       (port > 65535) )
2603     {
2604       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2605                                            component_name,
2606                        _("Require valid port number for transport plugin `%s' in configuration!\n"),
2607                        PROTOCOL_PREFIX);
2608       GNUNET_free(component_name);
2609       LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2610       return NULL;
2611     }
2612
2613   /* Reading ipv4 addresse to bind to from config file */
2614   if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2615                                                                                                           component_name, "BINDTO4")))
2616   {
2617           GNUNET_break (GNUNET_OK ==
2618                                         GNUNET_CONFIGURATION_get_value_string (env->cfg,
2619                                                                                                                    component_name,
2620                                                                                                                    "BINDTO4",
2621                                                                                                                    &plugin->bind_hostname));
2622           plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2623           plugin->bind4_address->sin_family = AF_INET;
2624           plugin->bind4_address->sin_port = htons (port);
2625
2626           if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2627           {
2628                   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2629                                                    component_name,
2630                                                    _("Misconfigured address to bind to in configuration!\n"));
2631                   GNUNET_free(plugin->bind4_address);
2632                   GNUNET_free(plugin->bind_hostname);
2633                   plugin->bind_hostname = NULL;
2634                   plugin->bind4_address = NULL;
2635           }
2636   }
2637
2638   /* Reading ipv4 addresse to bind to from config file */
2639   if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2640                   component_name, "BINDTO6")))
2641   {
2642           if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2643                                                                                                                           component_name,
2644                                                                                                                           "BINDTO6",
2645                                                                                                                           &plugin->bind_hostname))
2646           {
2647                   plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2648                   plugin->bind6_address->sin6_family = AF_INET6;
2649                   plugin->bind6_address->sin6_port = htons (port);
2650
2651                   if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2652                   {
2653                           GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2654                                                            component_name,
2655                                                            _("Misconfigured address to bind to in configuration!\n"));
2656                           GNUNET_free(plugin->bind6_address);
2657                           GNUNET_free(plugin->bind_hostname);
2658                           plugin->bind_hostname = NULL;
2659                           plugin->bind6_address = NULL;
2660                   }
2661           }
2662   }
2663
2664 #if BUILD_HTTPS
2665   /* Reading HTTPS crypto related configuration */
2666   /* Get crypto init string from config */
2667   if (GNUNET_CONFIGURATION_have_value (env->cfg,
2668                                                                            "transport-https", "CRYPTO_INIT"))
2669   {
2670                 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2671                                                                                            "transport-https",
2672                                                                                            "CRYPTO_INIT",
2673                                                                                            &plugin->crypto_init);
2674   }
2675   else
2676   {
2677           GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
2678   }
2679
2680 /* Get private key file from config */
2681   if (GNUNET_CONFIGURATION_have_value (env->cfg,
2682                                                                            "transport-https", "KEY_FILE"))
2683   {
2684                 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2685                                                                                            "transport-https",
2686                                                                                            "KEY_FILE",
2687                                                                                            &key_file);
2688   }
2689   if (key_file==NULL)
2690           GNUNET_asprintf(&key_file,"https.key");
2691
2692 /* Get private key file from config */
2693   if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
2694   {
2695           GNUNET_CONFIGURATION_get_value_string (env->cfg,
2696                                                                                          "transport-https",
2697                                                                                          "CERT_FILE",
2698                                                                                          &cert_file);
2699   }
2700   if (cert_file==NULL)
2701           GNUNET_asprintf(&cert_file,"https.cert");
2702
2703   /* read key & certificates from file */
2704   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2705
2706   plugin->key = load_certificate( key_file );
2707   plugin->cert = load_certificate( cert_file );
2708
2709   if ((plugin->key==NULL) || (plugin->cert==NULL))
2710   {
2711           char * cmd;
2712           int ret = 0;
2713           GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2714           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2715           ret = system(cmd);
2716
2717           if (ret != 0)
2718           {
2719                   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2720                                            "https",
2721                                                    _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
2722                                                    "transport-https");
2723                   GNUNET_free (key_file);
2724                   GNUNET_free (cert_file);
2725                   GNUNET_free (component_name);
2726
2727                   LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
2728                   GNUNET_free (cmd);
2729                   return NULL;
2730           }
2731
2732           GNUNET_free (cmd);
2733
2734           plugin->key = load_certificate( key_file );
2735           plugin->cert = load_certificate( cert_file );
2736
2737           if ((plugin->key==NULL) || (plugin->cert==NULL))
2738           {
2739                   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2740                                            "https",
2741                                                    _("No usable TLS certificate found and creating one failed! \n"),
2742                                                    "transport-https");
2743                   GNUNET_free (key_file);
2744                   GNUNET_free (cert_file);
2745                   GNUNET_free (component_name);
2746
2747                   LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
2748                   return NULL;
2749           }
2750   }
2751   GNUNET_free (key_file);
2752   GNUNET_free (cert_file);
2753
2754   GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2755   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2756 #endif
2757
2758   GNUNET_assert ((port > 0) && (port <= 65535));
2759   plugin->port_inbound = port;
2760   gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2761   unsigned int timeout = (gn_timeout.value) / 1000;
2762   if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2763   {
2764         struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2765     plugin->http_server_daemon_v6 = MHD_start_daemon (
2766 #if DEBUG_MHD
2767                                                                    MHD_USE_DEBUG |
2768 #endif
2769 #if BUILD_HTTPS
2770                                                                    MHD_USE_SSL |
2771 #endif
2772                                                                    MHD_USE_IPv6,
2773                                        port,
2774                                        &mhd_accept_cb,
2775                                        plugin , &mdh_access_cb, plugin,
2776                                        MHD_OPTION_SOCK_ADDR, tmp,
2777                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2778                                        //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2779 #if BUILD_HTTPS
2780                                        MHD_OPTION_HTTPS_PRIORITIES,  plugin->crypto_init,
2781                                        MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2782                                        MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2783 #endif
2784                                        MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2785                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
2786                                        MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2787                                        MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
2788                                        MHD_OPTION_END);
2789   }
2790   if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2791   {
2792   plugin->http_server_daemon_v4 = MHD_start_daemon (
2793 #if DEBUG_MHD
2794                                                                    MHD_USE_DEBUG |
2795 #endif
2796 #if BUILD_HTTPS
2797                                                                    MHD_USE_SSL |
2798 #endif
2799                                                                    MHD_NO_FLAG,
2800                                        port,
2801                                        &mhd_accept_cb,
2802                                        plugin , &mdh_access_cb, plugin,
2803                                        MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2804                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2805                                        //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2806 #if BUILD_HTTPS
2807                                        MHD_OPTION_HTTPS_PRIORITIES,  plugin->crypto_init,
2808                                        MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2809                                        MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2810 #endif
2811                                        MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2812                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
2813                                        MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2814                                        MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
2815                                        MHD_OPTION_END);
2816   }
2817   if (plugin->http_server_daemon_v4 != NULL)
2818     plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2819   if (plugin->http_server_daemon_v6 != NULL)
2820     plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2821
2822
2823   if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2824   {
2825 #if DEBUG_HTTP
2826           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);
2827 #endif
2828   }
2829   else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2830   {
2831 #if DEBUG_HTTP
2832     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);
2833 #endif
2834   }
2835   else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2836   {
2837 #if DEBUG_HTTP
2838     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);
2839 #endif
2840   }
2841   else
2842   {
2843         char * tmp = NULL;
2844         if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
2845                 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
2846         if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
2847                 GNUNET_asprintf(&tmp,"with IPv4 enabled");
2848         if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
2849                 GNUNET_asprintf(&tmp,"with IPv6 enabled");
2850         if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
2851                 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
2852         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);
2853         GNUNET_free (tmp);
2854     GNUNET_free (component_name);
2855     LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2856     return NULL;
2857   }
2858
2859   /* Initializing cURL */
2860   curl_global_init(CURL_GLOBAL_ALL);
2861   plugin->multi_handle = curl_multi_init();
2862
2863   if ( NULL == plugin->multi_handle )
2864   {
2865     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2866                                          component_name,
2867                                          _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
2868                                          PROTOCOL_PREFIX);
2869     GNUNET_free(component_name);
2870     LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2871     return NULL;
2872   }
2873
2874   plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2875   GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2876
2877   GNUNET_free(component_name);
2878   return api;
2879 }
2880
2881 /* end of plugin_transport_http.c */