726a9dbe27a0cb203e764c50bb8ef8cfa1789244
[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 2, 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_template.c
23  * @brief template for a new transport service
24  * @author Christian Grothoff
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_server_lib.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_resolver_service.h"
36 #include "plugin_transport.h"
37 #include "microhttpd.h"
38 #include <curl/curl.h>
39
40 #define DEBUG_HTTP GNUNET_NO
41
42 /**
43  * Text of the response sent back after the last bytes of a PUT
44  * request have been received (just to formally obey the HTTP
45  * protocol).
46  */
47 #define HTTP_PUT_RESPONSE "Thank you!"
48
49 /**
50  * After how long do we expire an address that we
51  * learned from another peer if it is not reconfirmed
52  * by anyone?
53  */
54 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
55
56 /**
57  * Page returned if request invalid
58  */
59 #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>"
60
61 /**
62  * Timeout for a http connect
63  */
64 #define HTTP_CONNECT_TIMEOUT 30
65
66
67 /**
68  * Encapsulation of all of the state of the plugin.
69  */
70 struct Plugin;
71
72 /**
73  *  Message to send using http
74  */
75 struct HTTP_Message
76 {
77   /**
78    * Next field for linked list
79    */
80   struct HTTP_Message * next;
81
82   /**
83    * buffer containing data to send
84    */
85   char *buf;
86
87   /**
88    * amount of data already sent
89    */
90   size_t pos;
91
92   /**
93    * amount of data to sent
94    */
95   size_t size;
96
97   size_t len;
98 };
99
100
101 /**
102  * Session handle for connections.
103  */
104 struct Session
105 {
106
107   /**
108    * Stored in a linked list.
109    */
110   struct Session *next;
111
112   /**
113    * Pointer to the global plugin struct.
114    */
115   struct Plugin *plugin;
116
117   /**
118    * Continuation function to call once the transmission buffer
119    * has again space available.  NULL if there is no
120    * continuation to call.
121    */
122   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
123
124   /**
125    * Closure for transmit_cont.
126    */
127   void *transmit_cont_cls;
128
129   /**
130    * To whom are we talking to (set to our identity
131    * if we are still waiting for the welcome message)
132    */
133   struct GNUNET_PeerIdentity sender;
134
135   /**
136    * Sender's url
137    */
138   char * url;
139
140   /**
141    * Sender's ip address to distinguish between incoming connections
142    */
143   char * ip;
144
145   /**
146    * Sender's ip address to distinguish between incoming connections
147    */
148   struct sockaddr_in * addr;
149
150   /**
151    * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
152    */
153   unsigned int is_client;
154
155   /**
156    * Is the connection active (GNUNET_YES) or terminated (GNUNET_NO)?
157    */
158   unsigned int is_active;
159
160   /**
161    * At what time did we reset last_received last?
162    */
163   struct GNUNET_TIME_Absolute last_quota_update;
164
165   /**
166    * How many bytes have we received since the "last_quota_update"
167    * timestamp?
168    */
169   uint64_t last_received;
170
171   /**
172    * Number of bytes per ms that this peer is allowed
173    * to send to us.
174    */
175   uint32_t quota;
176
177   /**
178    * Is there a HTTP/PUT in progress?
179    */
180   unsigned int is_put_in_progress;
181
182   /**
183    * Encoded hash
184    */
185   struct GNUNET_CRYPTO_HashAsciiEncoded hash;
186
187   struct HTTP_Message * pending_outbound_msg;;
188
189   CURL *curl_handle;
190 };
191
192 /**
193  * Encapsulation of all of the state of the plugin.
194  */
195 struct Plugin
196 {
197   /**
198    * Our environment.
199    */
200   struct GNUNET_TRANSPORT_PluginEnvironment *env;
201
202   /**
203    * Handle to the network service.
204    */
205   struct GNUNET_SERVICE_Context *service;
206
207   /**
208    * List of open sessions.
209    */
210   struct Session *sessions;
211
212   /**
213    * Number of active sessions
214    */
215
216   unsigned int session_count;
217 };
218
219 /**
220  * Daemon for listening for new IPv4 connections.
221  */
222 static struct MHD_Daemon *http_daemon_v4;
223
224 /**
225  * Daemon for listening for new IPv6connections.
226  */
227 static struct MHD_Daemon *http_daemon_v6;
228
229 /**
230  * Our primary task for http daemon handling IPv4 connections
231  */
232 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
233
234 /**
235  * Our primary task for http daemon handling IPv6 connections
236  */
237 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
238
239
240 /**
241  * Our primary task for http daemon handling IPv6 connections
242  */
243 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
244
245
246 /**
247  * Information about this plugin
248  */
249 static struct Plugin *plugin;
250
251 /**
252  * cURL Multihandle
253  */
254 static CURLM *multi_handle;
255
256 /**
257  * Our hostname
258  */
259 static char * hostname;
260
261 /**
262  * Our ASCII encoded, hashed peer identity
263  * This string is used to distinguish between connections and is added to the urls
264  */
265 static struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
266
267 struct GNUNET_TIME_Relative timeout;
268
269 /**
270  * Finds a http session in our linked list using peer identity as a key
271  * @param peer peeridentity
272  * @return http session corresponding to peer identity
273  */
274 static struct Session * find_session_by_pi( const struct GNUNET_PeerIdentity *peer )
275 {
276   struct Session * cur;
277   GNUNET_HashCode hc_peer;
278   GNUNET_HashCode hc_current;
279
280   cur = plugin->sessions;
281   hc_peer = peer->hashPubKey;
282   while (cur != NULL)
283   {
284     hc_current = cur->sender.hashPubKey;
285     if ( 0 == GNUNET_CRYPTO_hash_cmp( &hc_peer, &hc_current))
286       return cur;
287     cur = plugin->sessions->next;
288   }
289   return NULL;
290 }
291
292 /**
293  * Finds a http session in our linked list using libcurl handle as a key
294  * Needed when sending data with libcurl to differentiate between sessions
295  * @param peer peeridentity
296  * @return http session corresponding to peer identity
297  */
298 static struct Session * find_session_by_curlhandle( CURL* handle )
299 {
300   struct Session * cur;
301
302   cur = plugin->sessions;
303   while (cur != NULL)
304   {
305     if ( handle == cur->curl_handle )
306       return cur;
307     cur = plugin->sessions->next;
308   }
309   return NULL;
310 }
311
312 /**
313  * Create a new session
314  *
315  * @param address address the peer is using
316  * @peer  peer identity
317  * @return created session object
318  */
319 static struct Session * create_session (struct sockaddr_in *address, const struct GNUNET_PeerIdentity *peer)
320 {
321   struct sockaddr_in  *addrin;
322   struct sockaddr_in6 *addrin6;
323
324   struct Session * ses = GNUNET_malloc ( sizeof( struct Session) );
325   ses->addr = GNUNET_malloc ( sizeof (struct sockaddr_in) );
326
327   ses->next = NULL;
328   ses->plugin = plugin;
329
330   memcpy(ses->addr, address, sizeof (struct sockaddr_in));
331   if ( AF_INET == address->sin_family)
332   {
333     ses->ip = GNUNET_malloc (INET_ADDRSTRLEN);
334     addrin = address;
335     inet_ntop(addrin->sin_family,&(addrin->sin_addr),ses->ip,INET_ADDRSTRLEN);
336   }
337   if ( AF_INET6 == address->sin_family)
338   {
339     ses->ip = GNUNET_malloc (INET6_ADDRSTRLEN);
340     addrin6 = (struct sockaddr_in6 *) address;
341     inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr) ,ses->ip,INET6_ADDRSTRLEN);
342   }
343   memcpy(&ses->sender, peer, sizeof (struct GNUNET_PeerIdentity));
344   GNUNET_CRYPTO_hash_to_enc(&ses->sender.hashPubKey,&(ses->hash));
345   ses->is_active = GNUNET_NO;
346
347   return ses;
348 }
349
350 /**
351  * Callback called by MHD when a connection is terminated
352  */
353 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
354 {
355   struct Session * cs;
356
357   cs = *httpSessionCache;
358   if (cs != NULL)
359   {
360     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&cs->sender));
361     /* session set to inactive */
362     cs->is_active = GNUNET_NO;
363     cs->is_put_in_progress = GNUNET_NO;
364   }
365   return;
366 }
367
368 /**
369  * Check if we are allowed to connect to the given IP.
370  */
371 static int
372 acceptPolicyCallback (void *cls,
373                       const struct sockaddr *addr, socklen_t addr_len)
374 {
375   /* Every connection is accepted, nothing more to do here */
376   return MHD_YES;
377 }
378
379
380 /**
381  * Process GET or PUT request received via MHD.  For
382  * GET, queue response that will send back our pending
383  * messages.  For PUT, process incoming data and send
384  * to GNUnet core.  In either case, check if a session
385  * already exists and create a new one if not.
386  */
387 static int
388 accessHandlerCallback (void *cls,
389                        struct MHD_Connection *session,
390                        const char *url,
391                        const char *method,
392                        const char *version,
393                        const char *upload_data,
394                        size_t * upload_data_size, void **httpSessionCache)
395 {
396   struct MHD_Response *response;
397   struct Session * cs;
398   struct Session * cs_temp;
399   const union MHD_ConnectionInfo * conn_info;
400   struct sockaddr_in  *addrin;
401   struct sockaddr_in6 *addrin6;
402   char * address = NULL;
403   struct GNUNET_PeerIdentity pi_in;
404   int res = GNUNET_NO;
405   size_t bytes_recv;
406   struct GNUNET_MessageHeader *gn_msg;
407   int send_error_to_client;
408
409   gn_msg = NULL;
410   send_error_to_client = GNUNET_NO;
411   if ( NULL == *httpSessionCache)
412   {
413     /* check url for peer identity */
414     res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
415     if ( GNUNET_SYSERR == res )
416     {
417       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident\n");
418       response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
419       res = MHD_queue_response (session, MHD_HTTP_NOT_FOUND, response);
420       MHD_destroy_response (response);
421       return res;
422     }
423
424     conn_info = MHD_get_connection_info(session, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
425     /* Incoming IPv4 connection */
426     if ( AF_INET == conn_info->client_addr->sin_family)
427     {
428       address = GNUNET_malloc (INET_ADDRSTRLEN);
429       addrin = conn_info->client_addr;
430       inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
431     }
432     /* Incoming IPv6 connection */
433     if ( AF_INET6 == conn_info->client_addr->sin_family)
434     {
435       address = GNUNET_malloc (INET6_ADDRSTRLEN);
436       addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
437       inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
438     }
439     /* find existing session for address */
440     cs = NULL;
441     if (plugin->session_count > 0)
442     {
443       cs = plugin->sessions;
444       while ( NULL != cs)
445       {
446
447         /* Comparison based on ip address */
448         // res = (0 == memcmp(&(conn_info->client_addr->sin_addr),&(cs->addr->sin_addr), sizeof (struct in_addr))) ? GNUNET_YES : GNUNET_NO;
449
450         /* Comparison based on ip address, port number and address family */
451         // res = (0 == memcmp((conn_info->client_addr),(cs->addr), sizeof (struct sockaddr_in))) ? GNUNET_YES : GNUNET_NO;
452
453         /* Comparison based on PeerIdentity */
454         res = (0 == memcmp(&pi_in,&(cs->sender), sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_YES : GNUNET_NO;
455
456         if ( GNUNET_YES  == res)
457         {
458           /* existing session for this address found */
459           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session `%s' found\n",address);
460           break;
461         }
462         cs = cs->next;
463       }
464     }
465     /* no existing session, create a new one*/
466     if (cs == NULL )
467     {
468       /* create new session object */
469       cs = create_session(conn_info->client_addr, &pi_in);
470
471       /* Insert session into linked list */
472       if ( plugin->sessions == NULL)
473       {
474         plugin->sessions = cs;
475         plugin->session_count = 1;
476       }
477       cs_temp = plugin->sessions;
478       while ( cs_temp->next != NULL )
479       {
480         cs_temp = cs_temp->next;
481       }
482       if (cs_temp != cs )
483       {
484         cs_temp->next = cs;
485         plugin->session_count++;
486       }
487       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", address, plugin->session_count);
488     }
489     /* Set closure */
490     if (*httpSessionCache == NULL)
491     {
492       *httpSessionCache = cs;
493     }
494     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`[%s]:%u')\n",method, GNUNET_i2s(&cs->sender),cs->ip,cs->addr->sin_port);
495   }
496   else
497   {
498     cs = *httpSessionCache;
499   }
500   /* Is it a PUT or a GET request */
501   if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
502   {
503     /* New  */
504     if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_NO))
505     {
506       /* not yet ready */
507       cs->is_put_in_progress = GNUNET_YES;
508       cs->is_active = GNUNET_YES;
509       return MHD_YES;
510     }
511     if ( *upload_data_size > 0 )
512     {
513       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT URL: `%s'\n",url);
514       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT Request: %lu bytes: `%s' \n", (*upload_data_size), upload_data);
515       /* No data left */
516       bytes_recv = *upload_data_size ;
517       *upload_data_size = 0;
518
519       /* checking size */
520       if (bytes_recv < sizeof (struct GNUNET_MessageHeader))
521       {
522         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message too small, is %u bytes, has to be at least %u '\n",bytes_recv, sizeof(struct GNUNET_MessageHeader));
523         send_error_to_client = GNUNET_YES;
524       }
525
526       if ( bytes_recv > GNUNET_SERVER_MAX_MESSAGE_SIZE)
527       {
528         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message too big, is %u bytes, maximum %u '\n",bytes_recv, GNUNET_SERVER_MAX_MESSAGE_SIZE);
529         send_error_to_client = GNUNET_YES;
530       }
531
532       struct GNUNET_MessageHeader * gn_msg = GNUNET_malloc (bytes_recv);
533       memcpy (gn_msg,upload_data,bytes_recv);
534
535       if ( ntohs(gn_msg->size) != bytes_recv )
536       {
537         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message has incorrect size, is %u bytes vs %u recieved'\n",ntohs(gn_msg->size) , bytes_recv);
538         send_error_to_client = GNUNET_YES;
539       }
540
541       if ( GNUNET_YES == send_error_to_client)
542       {
543         response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
544         res = MHD_queue_response (session, MHD_HTTP_BAD_REQUEST, response);
545         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
546         MHD_destroy_response (response);
547         GNUNET_free (gn_msg);
548         return MHD_NO;
549       }
550       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved GNUnet message type %u size %u and payload %u \n",ntohs (gn_msg->type), ntohs (gn_msg->size), ntohs (gn_msg->size)-sizeof(struct GNUNET_MessageHeader));
551
552       /* forwarding message to transport */
553       plugin->env->receive(plugin->env, &(cs->sender), gn_msg, 1, cs , cs->ip, strlen(cs->ip) );
554       return MHD_YES;
555     }
556     if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES))
557     {
558       cs->is_put_in_progress = GNUNET_NO;
559       response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
560       res = MHD_queue_response (session, MHD_HTTP_OK, response);
561       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
562       MHD_destroy_response (response);
563       return res;
564     }
565   }
566   if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
567   {
568     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
569     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
570     response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
571     res = MHD_queue_response (session, MHD_HTTP_OK, response);
572     MHD_destroy_response (response);
573     return res;
574   }
575   return MHD_NO;
576 }
577
578
579 /**
580  * Call MHD to process pending requests and then go back
581  * and schedule the next run.
582  */
583 static void http_daemon_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
584
585 /**
586  * Function that queries MHD's select sets and
587  * starts the task waiting for them.
588  */
589 static GNUNET_SCHEDULER_TaskIdentifier
590 http_daemon_prepare (struct MHD_Daemon *daemon_handle)
591 {
592   GNUNET_SCHEDULER_TaskIdentifier ret;
593   fd_set rs;
594   fd_set ws;
595   fd_set es;
596   struct GNUNET_NETWORK_FDSet *wrs;
597   struct GNUNET_NETWORK_FDSet *wws;
598   struct GNUNET_NETWORK_FDSet *wes;
599   int max;
600   unsigned long long timeout;
601   int haveto;
602   struct GNUNET_TIME_Relative tv;
603
604   FD_ZERO(&rs);
605   FD_ZERO(&ws);
606   FD_ZERO(&es);
607   wrs = GNUNET_NETWORK_fdset_create ();
608   wes = GNUNET_NETWORK_fdset_create ();
609   wws = GNUNET_NETWORK_fdset_create ();
610   max = -1;
611   GNUNET_assert (MHD_YES ==
612                  MHD_get_fdset (daemon_handle,
613                                 &rs,
614                                 &ws,
615                                 &es,
616                                 &max));
617   haveto = MHD_get_timeout (daemon_handle, &timeout);
618   if (haveto == MHD_YES)
619     tv.value = (uint64_t) timeout;
620   else
621     tv = GNUNET_TIME_UNIT_FOREVER_REL;
622   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
623   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
624   GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
625   ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
626                                      GNUNET_SCHEDULER_PRIORITY_HIGH,
627                                      GNUNET_SCHEDULER_NO_TASK,
628                                      tv,
629                                      wrs,
630                                      wws,
631                                      &http_daemon_run,
632                                      daemon_handle);
633   GNUNET_NETWORK_fdset_destroy (wrs);
634   GNUNET_NETWORK_fdset_destroy (wws);
635   GNUNET_NETWORK_fdset_destroy (wes);
636   return ret;
637 }
638
639 /**
640  * Call MHD to process pending requests and then go back
641  * and schedule the next run.
642  */
643 static void http_daemon_run (void *cls,
644                              const struct GNUNET_SCHEDULER_TaskContext *tc)
645 {
646   struct MHD_Daemon *daemon_handle = cls;
647
648   if (daemon_handle == http_daemon_v4)
649     http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
650
651   if (daemon_handle == http_daemon_v6)
652     http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
653
654   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
655     return;
656
657   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
658   if (daemon_handle == http_daemon_v4)
659     http_task_v4 = http_daemon_prepare (daemon_handle);
660   if (daemon_handle == http_daemon_v6)
661     http_task_v6 = http_daemon_prepare (daemon_handle);
662   return;
663 }
664
665 /**
666  * Removes a message from the linked list of messages
667  * @param ses session to remove message from
668  * @param msg message to remove
669  * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
670  */
671
672 static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
673 {
674   struct HTTP_Message * cur;
675   struct HTTP_Message * next;
676
677   cur = ses->pending_outbound_msg;
678   next = NULL;
679
680   if (cur == NULL)
681     return GNUNET_SYSERR;
682
683   if (cur == msg)
684   {
685     ses->pending_outbound_msg = cur->next;
686     GNUNET_free (cur->buf);
687     GNUNET_free (cur);
688     cur = NULL;
689     return GNUNET_OK;
690   }
691
692   while (cur->next!=msg)
693   {
694     if (cur->next != NULL)
695       cur = cur->next;
696     else
697       return GNUNET_SYSERR;
698   }
699
700   cur->next = cur->next->next;
701   GNUNET_free (cur->next->buf);
702   GNUNET_free (cur->next);
703   cur->next = NULL;
704   return GNUNET_OK;
705 }
706
707
708 /**
709  * Callback method used with libcurl
710  * Method is called when libcurl needs to read data during sending
711  * @param stream pointer where to write data
712  * @param size_t size of an individual element
713  * @param nmemb count of elements that can be written to the buffer
714  * @param ptr source pointer, passed to the libcurl handle
715  * @return bytes written to stream
716  */
717 static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
718 {
719   struct Session * ses = ptr;
720   struct HTTP_Message * msg = ses->pending_outbound_msg;
721   unsigned int bytes_sent;
722
723   bytes_sent = 0;
724   if (msg->len > (size * nmemb))
725     return CURL_READFUNC_ABORT;
726
727   if (( msg->pos < msg->len) && (msg->len < (size * nmemb)))
728   {
729     memcpy(stream, msg->buf, msg->len);
730     msg->pos = msg->len;
731     bytes_sent = msg->len;
732   }
733
734   return bytes_sent;
735 }
736
737 /**
738 * Callback method used with libcurl
739 * Method is called when libcurl needs to write data during sending
740 * @param stream pointer where to write data
741 * @param size_t size of an individual element
742 * @param nmemb count of elements that can be written to the buffer
743 * @param ptr destination pointer, passed to the libcurl handle
744 * @return bytes read from stream
745 */
746 static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
747 {
748   char * data = malloc(size*nmemb +1);
749
750   memcpy( data, stream, size*nmemb);
751   data[size*nmemb] = '\0';
752   /* Just a dummy print for the response recieved for the PUT message */
753   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Recieved %u bytes: `%s' \n", size * nmemb, data);
754   free (data);
755   return (size * nmemb);
756
757 }
758
759 /**
760  * Function setting up file descriptors and scheduling task to run
761  * @param ses session to send data to
762  * @return bytes sent to peer
763  */
764 static size_t send_prepare(struct Session* session );
765
766 /**
767  * Function setting up curl handle and selecting message to send
768  * @param ses session to send data to
769  * @return bytes sent to peer
770  */
771 static ssize_t send_select_init (struct Session* ses  )
772 {
773   char * url;
774   int bytes_sent = 0;
775   CURLMcode mret;
776   struct HTTP_Message * msg;
777
778   /* FIFO selection, send oldest msg first, perhaps priority here?  */
779   msg = ses->pending_outbound_msg;
780
781   url = GNUNET_malloc( 7 + strlen(ses->ip) + 7 + strlen ((char *) &(ses->hash)) + 1);
782   /* FIXME: use correct port number */
783   GNUNET_asprintf(&url,"http://%s:%u/%s",ses->ip,12389, (char *) &(ses->hash));
784
785   /* curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L); */
786   curl_easy_setopt(ses->curl_handle, CURLOPT_URL, url);
787   curl_easy_setopt(ses->curl_handle, CURLOPT_PUT, 1L);
788   curl_easy_setopt(ses->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
789   curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
790   curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
791   curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
792   curl_easy_setopt(ses->curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
793   curl_easy_setopt(ses->curl_handle, CURLOPT_TIMEOUT, (timeout.value / 1000 ));
794   curl_easy_setopt(ses->curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
795
796   mret = curl_multi_add_handle(multi_handle, ses->curl_handle);
797   if (mret != CURLM_OK)
798   {
799     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
800                 _("%s failed at %s:%d: `%s'\n"),
801                 "curl_multi_add_handle", __FILE__, __LINE__,
802                 curl_multi_strerror (mret));
803     return -1;
804   }
805   bytes_sent = send_prepare (ses );
806   GNUNET_free ( url );
807   return bytes_sent;
808 }
809
810 static void send_execute (void *cls,
811              const struct GNUNET_SCHEDULER_TaskContext *tc)
812 {
813   int running;
814   struct CURLMsg *msg;
815   CURLMcode mret;
816   struct Session * cs = NULL;
817
818   http_task_send = GNUNET_SCHEDULER_NO_TASK;
819   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
820     return;
821
822   do
823     {
824       running = 0;
825       mret = curl_multi_perform (multi_handle, &running);
826       if (running == 0)
827         {
828           do
829             {
830
831               msg = curl_multi_info_read (multi_handle, &running);
832               GNUNET_break (msg != NULL);
833               if (msg == NULL)
834                 break;
835               /* get session for affected curl handle */
836               cs = find_session_by_curlhandle (msg->easy_handle);
837               GNUNET_assert ( cs != NULL );
838               switch (msg->msg)
839                 {
840
841                 case CURLMSG_DONE:
842                   if ( (msg->data.result != CURLE_OK) &&
843                        (msg->data.result != CURLE_GOT_NOTHING) )
844                     {
845
846                     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
847                                _("%s failed for `%s' at %s:%d: `%s'\n"),
848                                "curl_multi_perform",
849                                cs->ip,
850                                __FILE__,
851                                __LINE__,
852                                curl_easy_strerror (msg->data.result));
853                     /* sending msg failed*/
854                     if ( NULL != cs->transmit_cont)
855                       cs->transmit_cont (NULL,&cs->sender,GNUNET_SYSERR);
856                     }
857                   else
858                     {
859                     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860                                 "Send to %s completed.\n", cs->ip);
861                     if (GNUNET_OK != remove_http_message(cs, cs->pending_outbound_msg))
862                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'", GNUNET_i2s(&cs->sender));
863
864                     curl_easy_cleanup(cs->curl_handle);
865                     cs->curl_handle=NULL;
866
867                     /* send pending messages */
868                     if (cs->pending_outbound_msg != NULL)
869                       send_select_init (cs);
870
871                     /* Calling transmit continuation  */
872                     if ( NULL != cs->transmit_cont)
873                       cs->transmit_cont (NULL,&cs->sender,GNUNET_OK);
874                     }
875                   return;
876                 default:
877                   break;
878                 }
879
880             }
881           while ( (running > 0) );
882         }
883     }
884   while (mret == CURLM_CALL_MULTI_PERFORM);
885   send_prepare(cls);
886 }
887
888
889 /**
890  * Function setting up file descriptors and scheduling task to run
891  * @param ses session to send data to
892  * @return bytes sent to peer
893  */
894 static size_t send_prepare(struct Session* session )
895 {
896   fd_set rs;
897   fd_set ws;
898   fd_set es;
899   int max;
900   struct GNUNET_NETWORK_FDSet *grs;
901   struct GNUNET_NETWORK_FDSet *gws;
902   long to;
903   CURLMcode mret;
904
905   max = -1;
906   FD_ZERO (&rs);
907   FD_ZERO (&ws);
908   FD_ZERO (&es);
909   mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
910   if (mret != CURLM_OK)
911     {
912       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
913                   _("%s failed at %s:%d: `%s'\n"),
914                   "curl_multi_fdset", __FILE__, __LINE__,
915                   curl_multi_strerror (mret));
916       return -1;
917     }
918   mret = curl_multi_timeout (multi_handle, &to);
919   if (mret != CURLM_OK)
920     {
921       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
922                   _("%s failed at %s:%d: `%s'\n"),
923                   "curl_multi_timeout", __FILE__, __LINE__,
924                   curl_multi_strerror (mret));
925       return -1;
926     }
927
928   grs = GNUNET_NETWORK_fdset_create ();
929   gws = GNUNET_NETWORK_fdset_create ();
930   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
931   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
932   http_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
933                                    GNUNET_SCHEDULER_PRIORITY_DEFAULT,
934                                    GNUNET_SCHEDULER_NO_TASK,
935                                    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
936                                    grs,
937                                    gws,
938                                    &send_execute,
939                                    session);
940   GNUNET_NETWORK_fdset_destroy (gws);
941   GNUNET_NETWORK_fdset_destroy (grs);
942
943   /* FIXME: return bytes REALLY sent */
944   return 0;
945 }
946
947 /**
948  * Function that can be used by the transport service to transmit
949  * a message using the plugin.
950  *
951  * @param cls closure
952  * @param target who should receive this message
953  * @param priority how important is the message
954  * @param msgbuf the message to transmit
955  * @param msgbuf_size number of bytes in 'msgbuf'
956  * @param timeout when should we time out
957  * @param session which session must be used (or NULL for "any")
958  * @param addr the address to use (can be NULL if the plugin
959  *                is "on its own" (i.e. re-use existing TCP connection))
960  * @param addrlen length of the address in bytes
961  * @param force_address GNUNET_YES if the plugin MUST use the given address,
962  *                otherwise the plugin may use other addresses or
963  *                existing connections (if available)
964  * @param cont continuation to call once the message has
965  *        been transmitted (or if the transport is ready
966  *        for the next transmission call; or if the
967  *        peer disconnected...)
968  * @param cont_cls closure for cont
969  * @return number of bytes used (on the physical network, with overheads);
970  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
971  *         and does NOT mean that the message was not transmitted (DV)
972  */
973 static ssize_t
974 http_plugin_send (void *cls,
975                       const struct GNUNET_PeerIdentity *target,
976                       const char *msgbuf,
977                       size_t msgbuf_size,
978                       unsigned int priority,
979                       struct GNUNET_TIME_Relative to,
980                       struct Session *session,
981                       const void *addr,
982                       size_t addrlen,
983                       int force_address,
984                       GNUNET_TRANSPORT_TransmitContinuation cont,
985                       void *cont_cls)
986 {
987   struct Session* ses;
988   struct Session* ses_temp;
989   struct HTTP_Message * msg;
990   struct HTTP_Message * tmp;
991   int bytes_sent = 0;
992
993   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport told plugin to send to peer `%s'\n",GNUNET_i2s(target));
994
995   /* find session for peer */
996   ses = find_session_by_pi (target);
997   if (NULL != ses )
998     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Existing session for peer `%s' found\n", GNUNET_i2s(target));
999   if ( ses == NULL)
1000   {
1001     /* create new session object */
1002
1003     /*FIXME: what is const void * really? Assuming struct sockaddr_in * ! */
1004     ses = create_session((struct sockaddr_in *) addr, target);
1005     ses->is_active = GNUNET_YES;
1006     ses->transmit_cont = cont;
1007
1008     /* Insert session into linked list */
1009     if ( plugin->sessions == NULL)
1010     {
1011       plugin->sessions = ses;
1012       plugin->session_count = 1;
1013     }
1014     ses_temp = plugin->sessions;
1015     while ( ses_temp->next != NULL )
1016     {
1017       ses_temp = ses_temp->next;
1018     }
1019     if (ses_temp != ses )
1020     {
1021       ses_temp->next = ses;
1022       plugin->session_count++;
1023     }
1024     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", GNUNET_i2s(target), plugin->session_count);
1025   }
1026
1027   ses->curl_handle = curl_easy_init();
1028   if( NULL == ses->curl_handle)
1029   {
1030     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
1031     return -1;
1032   }
1033
1034   if ( NULL != cont)
1035     ses->transmit_cont = cont;
1036   timeout = to;
1037   /* setting up message */
1038   msg = GNUNET_malloc (sizeof (struct HTTP_Message));
1039   msg->next = NULL;
1040   msg->len = msgbuf_size;
1041   msg->pos = 0;
1042   msg->buf = GNUNET_malloc (msgbuf_size);
1043   memcpy (msg->buf,msgbuf, msgbuf_size);
1044
1045   /* insert created message in list of pending messages */
1046
1047   if (ses->pending_outbound_msg == NULL)
1048   {
1049     ses->pending_outbound_msg = msg;
1050   }
1051   tmp = ses->pending_outbound_msg;
1052   while ( NULL != tmp->next)
1053   {
1054     tmp = tmp->next;
1055   }
1056   if ( tmp != msg)
1057     tmp->next = msg;
1058
1059   bytes_sent = send_select_init (ses);
1060   return bytes_sent;
1061
1062 }
1063
1064
1065
1066 /**
1067  * Function that can be used to force the plugin to disconnect
1068  * from the given peer and cancel all previous transmissions
1069  * (and their continuationc).
1070  *
1071  * @param cls closure
1072  * @param target peer from which to disconnect
1073  */
1074 static void
1075 http_plugin_disconnect (void *cls,
1076                             const struct GNUNET_PeerIdentity *target)
1077 {
1078   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_disconnect\n");
1079   // struct Plugin *plugin = cls;
1080   // FIXME
1081 }
1082
1083
1084 /**
1085  * Convert the transports address to a nice, human-readable
1086  * format.
1087  *
1088  * @param cls closure
1089  * @param type name of the transport that generated the address
1090  * @param addr one of the addresses of the host, NULL for the last address
1091  *        the specific address format depends on the transport
1092  * @param addrlen length of the address
1093  * @param numeric should (IP) addresses be displayed in numeric form?
1094  * @param timeout after how long should we give up?
1095  * @param asc function to call on each string
1096  * @param asc_cls closure for asc
1097  */
1098 static void
1099 http_plugin_address_pretty_printer (void *cls,
1100                                         const char *type,
1101                                         const void *addr,
1102                                         size_t addrlen,
1103                                         int numeric,
1104                                         struct GNUNET_TIME_Relative timeout,
1105                                         GNUNET_TRANSPORT_AddressStringCallback
1106                                         asc, void *asc_cls)
1107 {
1108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_address_pretty_printer\n");
1109   asc (asc_cls, NULL);
1110 }
1111
1112
1113
1114 /**
1115  * Another peer has suggested an address for this
1116  * peer and transport plugin.  Check that this could be a valid
1117  * address.  If so, consider adding it to the list
1118  * of addresses.
1119  *
1120  * @param cls closure
1121  * @param addr pointer to the address
1122  * @param addrlen length of addr
1123  * @return GNUNET_OK if this is a plausible address for this peer
1124  *         and transport
1125  */
1126 static int
1127 http_plugin_address_suggested (void *cls,
1128                                   void *addr, size_t addrlen)
1129 {
1130   /* struct Plugin *plugin = cls; */
1131
1132   /* check if the address is plausible; if so,
1133      add it to our list! */
1134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_address_suggested\n");
1135   return GNUNET_OK;
1136 }
1137
1138
1139 /**
1140  * Function called for a quick conversion of the binary address to
1141  * a numeric address.  Note that the caller must not free the
1142  * address and that the next call to this function is allowed
1143  * to override the address again.
1144  *
1145  * @param cls closure
1146  * @param addr binary address
1147  * @param addrlen length of the address
1148  * @return string representing the same address
1149  */
1150 static const char*
1151 http_plugin_address_to_string (void *cls,
1152                                    const void *addr,
1153                                    size_t addrlen)
1154 {
1155   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_address_to_string\n");
1156   GNUNET_break (0);
1157   return NULL;
1158 }
1159
1160 /**
1161  * Exit point from the plugin.
1162  */
1163 void *
1164 libgnunet_plugin_transport_http_done (void *cls)
1165 {
1166   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1167   struct Plugin *plugin = api->cls;
1168   struct Session * cs;
1169   struct Session * cs_next;
1170   CURLMcode mret;
1171
1172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unloading http plugin...\n");
1173
1174   if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1175   {
1176     GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
1177     http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1178   }
1179
1180   if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1181   {
1182     GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
1183     http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1184   }
1185
1186   if ( http_task_send != GNUNET_SCHEDULER_NO_TASK)
1187   {
1188     GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_send);
1189     http_task_send = GNUNET_SCHEDULER_NO_TASK;
1190   }
1191
1192   if (http_daemon_v4 != NULL)
1193   {
1194     MHD_stop_daemon (http_daemon_v4);
1195     http_daemon_v4 = NULL;
1196   }
1197   if (http_daemon_v6 != NULL)
1198   {
1199     MHD_stop_daemon (http_daemon_v6);
1200     http_daemon_v6 = NULL;
1201   }
1202
1203   mret = curl_multi_cleanup(multi_handle);
1204   if ( CURLM_OK != mret)
1205     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1206
1207   /* free all sessions */
1208   cs = plugin->sessions;
1209
1210   while ( NULL != cs)
1211     {
1212       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session to `%s'\n",cs->ip);
1213
1214       cs_next = cs->next;
1215       /* freeing messages */
1216       struct HTTP_Message *cur;
1217       struct HTTP_Message *tmp;
1218       cur = cs->pending_outbound_msg;
1219
1220       while (cur != NULL)
1221       {
1222          tmp = cur->next;
1223          if (NULL != cur->buf)
1224            GNUNET_free (cur->buf);
1225          GNUNET_free (cur);
1226          cur = tmp;
1227       }
1228
1229       GNUNET_free (cs->ip);
1230       GNUNET_free (cs->addr);
1231       GNUNET_free (cs);
1232       plugin->session_count--;
1233       cs = cs_next;
1234
1235     }
1236
1237   /* GNUNET_SERVICE_stop (plugin->service); */
1238   GNUNET_free (hostname);
1239   GNUNET_free (plugin);
1240   GNUNET_free (api);
1241   return NULL;
1242 }
1243
1244
1245 /**
1246  * Entry point for the plugin.
1247  */
1248 void *
1249 libgnunet_plugin_transport_http_init (void *cls)
1250 {
1251   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1252   struct GNUNET_TRANSPORT_PluginFunctions *api;
1253   struct GNUNET_SERVICE_Context *service;
1254   unsigned int timeout;
1255   struct GNUNET_TIME_Relative gn_timeout;
1256   long long unsigned int port;
1257
1258   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
1259
1260   service = NULL;
1261   /*
1262   service = GNUNET_SERVICE_start ("transport-http", env->sched, env->cfg);
1263   if (service == NULL)
1264     {
1265       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "", _
1266                        ("Failed to start service for `%s' transport plugin.\n"),
1267                        "http");
1268       return NULL;
1269     }
1270     */
1271
1272   plugin = GNUNET_malloc (sizeof (struct Plugin));
1273   plugin->env = env;
1274   plugin->sessions = NULL;
1275   plugin->service = service;
1276   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1277   api->cls = plugin;
1278   api->send = &http_plugin_send;
1279   api->disconnect = &http_plugin_disconnect;
1280   api->address_pretty_printer = &http_plugin_address_pretty_printer;
1281   api->check_address = &http_plugin_address_suggested;
1282   api->address_to_string = &http_plugin_address_to_string;
1283
1284   hostname = GNUNET_RESOLVER_local_fqdn_get ();
1285
1286   /* Hashing our identity to use it in URLs */
1287   GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &my_ascii_hash_ident);
1288
1289   /* Reading port number from config file */
1290   if ((GNUNET_OK !=
1291        GNUNET_CONFIGURATION_get_value_number (env->cfg,
1292                                               "transport-http",
1293                                               "PORT",
1294                                               &port)) ||
1295       (port > 65535) )
1296     {
1297       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1298                        "http",
1299                        _
1300                        ("Require valid port number for transport plugin `%s' in configuration!\n"),
1301                        "transport-http");
1302       libgnunet_plugin_transport_http_done (api);
1303       return NULL;
1304     }
1305
1306   gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1307   timeout = ( gn_timeout.value / 1000);
1308   if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
1309     {
1310     http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
1311                                        port,
1312                                        &acceptPolicyCallback,
1313                                        NULL , &accessHandlerCallback, NULL,
1314                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1315                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1316                                        MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1317                                        /* FIXME: set correct limit */
1318                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1319                                        MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1320                                        MHD_OPTION_END);
1321     http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
1322                                        port,
1323                                        &acceptPolicyCallback,
1324                                        NULL , &accessHandlerCallback, NULL,
1325                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1326                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1327                                        MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1328                                        /* FIXME: set correct limit */
1329                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1330                                        MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1331                                        MHD_OPTION_END);
1332     }
1333   if (http_daemon_v4 != NULL)
1334     http_task_v4 = http_daemon_prepare (http_daemon_v4);
1335   if (http_daemon_v6 != NULL)
1336     http_task_v6 = http_daemon_prepare (http_daemon_v6);
1337
1338   if (http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1339     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
1340   if (http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1341     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
1342
1343   /* Initializing cURL */
1344   multi_handle = curl_multi_init();
1345   if ( NULL == multi_handle )
1346   {
1347     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1348                      "http",
1349                      _("Could not initialize curl multi handle, failed to start http plugin!\n"),
1350                      "transport-http");
1351     libgnunet_plugin_transport_http_done (api);
1352     return NULL;
1353   }
1354   return api;
1355 }
1356
1357 /* end of plugin_transport_template.c */