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