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