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