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