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