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