(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 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_protocols.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_server_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 "plugin_transport.h"
36 #include "microhttpd.h"
37 #include <curl/curl.h>
38
39 #define DEBUG_HTTP GNUNET_NO
40
41 /**
42  * Text of the response sent back after the last bytes of a PUT
43  * request have been received (just to formally obey the HTTP
44  * protocol).
45  */
46 #define HTTP_PUT_RESPONSE "Thank you!"
47
48 /**
49  * After how long do we expire an address that we
50  * learned from another peer if it is not reconfirmed
51  * by anyone?
52  */
53 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
54
55 /**
56  * Page returned if request invalid
57  */
58 #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>"
59
60
61 /**
62  * Encapsulation of all of the state of the plugin.
63  */
64 struct Plugin;
65
66
67 /**
68  * Session handle for connections.
69  */
70 struct Session
71 {
72
73   /**
74    * Stored in a linked list.
75    */
76   struct Session *next;
77
78   /**
79    * Pointer to the global plugin struct.
80    */
81   struct Plugin *plugin;
82
83   /**
84    * Continuation function to call once the transmission buffer
85    * has again space available.  NULL if there is no
86    * continuation to call.
87    */
88   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
89
90   /**
91    * Closure for transmit_cont.
92    */
93   void *transmit_cont_cls;
94
95   /**
96    * To whom are we talking to (set to our identity
97    * if we are still waiting for the welcome message)
98    */
99   struct GNUNET_PeerIdentity sender;
100
101   /**
102    * Sender's url
103    */
104   char * url;
105
106   /**
107    * Sender's ip address to distinguish between incoming connections
108    */
109   char * ip;
110
111   /**
112    * Sender's ip address to distinguish between incoming connections
113    */
114   struct sockaddr_in * addr;
115
116   /**
117    * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
118    */
119   unsigned int is_client;
120
121   /**
122    * Is the connection active (GNUNET_YES) or terminated (GNUNET_NO)?
123    */
124   unsigned int is_active;
125
126   /**
127    * At what time did we reset last_received last?
128    */
129   struct GNUNET_TIME_Absolute last_quota_update;
130
131   /**
132    * How many bytes have we received since the "last_quota_update"
133    * timestamp?
134    */
135   uint64_t last_received;
136
137   /**
138    * Number of bytes per ms that this peer is allowed
139    * to send to us.
140    */
141   uint32_t quota;
142
143 };
144
145 /**
146  * Encapsulation of all of the state of the plugin.
147  */
148 struct Plugin
149 {
150   /**
151    * Our environment.
152    */
153   struct GNUNET_TRANSPORT_PluginEnvironment *env;
154
155   /**
156    * List of open sessions.
157    */
158   struct Session *sessions;
159
160   /**
161    * Number of active sessions
162    */
163
164   unsigned int session_count;
165
166 };
167
168 /**
169  * Daemon for listening for new IPv4 connections.
170  */
171 static struct MHD_Daemon *http_daemon_v4;
172
173 /**
174  * Daemon for listening for new IPv6connections.
175  */
176 static struct MHD_Daemon *http_daemon_v6;
177
178 /**
179  * Our primary task for http daemon handling IPv4 connections
180  */
181 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
182
183 /**
184  * Our primary task for http daemon handling IPv6 connections
185  */
186 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
187
188
189 /**
190  * Information about this plugin
191  */
192 static struct Plugin *plugin;
193
194 /**
195  * cURL Multihandle
196  */
197 static CURLM *multi_handle;
198
199 /**
200  * Our hostname
201  */
202 static char * hostname;
203
204 /**
205  * Our ASCII encoded, hashed peer identity
206  * This string is used to distinguish between connections and is added to the urls
207  */
208 static struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
209
210
211 /**
212  * Finds a http session in our linked list using peer identity as a key
213  * @param peer peeridentity
214  * @return http session corresponding to peer identity
215  */
216 static struct Session * find_session_by_pi( const struct GNUNET_PeerIdentity *peer )
217 {
218   struct Session * cur;
219   GNUNET_HashCode hc_peer;
220   GNUNET_HashCode hc_current;
221
222   cur = plugin->sessions;
223   hc_peer = peer->hashPubKey;
224   while (cur != NULL)
225   {
226     hc_current = cur->sender.hashPubKey;
227     if ( 0 == GNUNET_CRYPTO_hash_cmp( &hc_peer, &hc_current))
228       return cur;
229     cur = plugin->sessions->next;
230   }
231   return NULL;
232 }
233
234 #if 0
235 /**
236  * Finds a http session in our linked list using peer identity as a key
237  * @param peer peeridentity
238  * @return http session corresponding to peer identity
239  */
240 static struct Session * find_session_by_ip( char * ip )
241 {
242   /*
243   struct Session * cur;
244
245   cur = plugin->sessions;
246   while (cur != NULL)
247   {
248     hc_current = cur->sender.hashPubKey;
249     if ( 0 == GNUNET_CRYPTO_hash_cmp( &hc_peer, &hc_current))
250       return cur;
251     cur = plugin->sessions->next;
252   }
253   */
254   return NULL;
255 }
256 #endif
257
258 /**
259  * Creates a http session in our linked list by peer identity
260  * Only peer is set here, all other  fields have to be set by calling method
261  * @param peer peeridentity
262  * @return created http session
263  */
264 static struct Session * create_session_by_pi( const struct GNUNET_PeerIdentity *peer )
265 {
266   struct Session * cur;
267   struct Session * last_in_list;
268   /* Create a new session object */
269   cur = GNUNET_malloc (sizeof (struct Session));
270   memcpy( &(cur->sender), peer, sizeof( struct GNUNET_PeerIdentity ) );
271
272   cur->next = NULL;
273
274   /* Insert into linked list */
275   last_in_list = plugin->sessions;
276   while (last_in_list->next != NULL)
277   {
278     last_in_list = last_in_list->next;
279   }
280   last_in_list->next = cur;
281
282   return cur;
283 }
284
285 #if 0
286 /**
287  * Creates a http session in our linked list by ip address
288  * Only ip is set here, all other fields have to be set by calling method
289  * @param peer peeridentity
290  * @return created http session
291  */
292 static struct Session * create_session_by_ip ( struct sockaddr_in * addr )
293 {
294   struct Session * cur;
295   struct Session * last_in_list;
296   /* Create a new session object */
297   cur = GNUNET_malloc (sizeof (struct Session));
298   // FIXME: memcpy( &(cur->ip), , sizeof( struct GNUNET_PeerIdentity ) );
299
300   cur->next = NULL;
301
302   /* Insert into linked list */
303   last_in_list = plugin->sessions;
304   while (last_in_list->next != NULL)
305   {
306     last_in_list = last_in_list->next;
307   }
308   last_in_list->next = cur;
309
310   return cur;
311 }
312 #endif
313
314 /**
315  * Callback called by MHD when a connection is terminated
316  */
317 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
318 {
319   struct Session * cs;
320
321   cs = *httpSessionCache;
322   if (cs != NULL)
323   {
324     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&cs->sender));
325     /* session set to inactive */
326     cs->is_active = GNUNET_NO;
327   }
328   else
329     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Not accepted connection was terminated\n");
330   return;
331 }
332
333 /**
334  * Check if we are allowed to connect to the given IP.
335  */
336 static int
337 acceptPolicyCallback (void *cls,
338                       const struct sockaddr *addr, socklen_t addr_len)
339 {
340   /* Every connection is accepted, nothing more to do here */
341   return MHD_YES;
342 }
343
344
345 /**
346  * Process GET or PUT request received via MHD.  For
347  * GET, queue response that will send back our pending
348  * messages.  For PUT, process incoming data and send
349  * to GNUnet core.  In either case, check if a session
350  * already exists and create a new one if not.
351  */
352 static int
353 accessHandlerCallback (void *cls,
354                        struct MHD_Connection *session,
355                        const char *url,
356                        const char *method,
357                        const char *version,
358                        const char *upload_data,
359                        size_t * upload_data_size, void **httpSessionCache)
360 {
361   struct MHD_Response *response;
362   struct Session * cs;
363   struct Session * cs_temp;
364   const union MHD_ConnectionInfo * conn_info;
365   struct sockaddr_in  *addrin;
366   struct sockaddr_in6 *addrin6;
367   char * address = NULL;
368   struct GNUNET_PeerIdentity pi_in;
369   int res = GNUNET_NO;
370
371   if ( NULL == *httpSessionCache)
372   {
373     /* check url for peer identity */
374     res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
375     if ( GNUNET_SYSERR == res )
376     {
377       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident\n");
378       response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
379       res = MHD_queue_response (session, MHD_HTTP_NOT_FOUND, response);
380       MHD_destroy_response (response);
381       return MHD_YES;
382     }
383
384     conn_info = MHD_get_connection_info(session, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
385     /* Incoming IPv4 connection */
386     if ( AF_INET == conn_info->client_addr->sin_family)
387     {
388       address = GNUNET_malloc (INET_ADDRSTRLEN);
389       addrin = conn_info->client_addr;
390       inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
391     }
392     /* Incoming IPv6 connection */
393     if ( AF_INET6 == conn_info->client_addr->sin_family)
394     {
395       address = GNUNET_malloc (INET6_ADDRSTRLEN);
396       addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
397       inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
398     }
399
400     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from peer `%s' from `[%s]:%u'\n",method, GNUNET_h2s(&pi_in.hashPubKey),address,conn_info->client_addr->sin_port);
401
402     /* find session for address */
403     cs = NULL;
404     if (plugin->session_count > 0)
405     {
406       cs = plugin->sessions;
407       while ( NULL != cs)
408       {
409
410         /* Comparison based on ip address */
411         // res = (0 == memcmp(&(conn_info->client_addr->sin_addr),&(cs->addr->sin_addr), sizeof (struct in_addr))) ? GNUNET_YES : GNUNET_NO;
412
413         /* Comparison based on ip address, port number and address family */
414         // res = (0 == memcmp((conn_info->client_addr),(cs->addr), sizeof (struct sockaddr_in))) ? GNUNET_YES : GNUNET_NO;
415
416         /* Comparison based on PeerIdentity */
417         res = (0 == memcmp(&pi_in,&(cs->sender), sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_YES : GNUNET_NO;
418
419         if ( GNUNET_YES  == res)
420         {
421           /* existing session for this address found */
422           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session `%s' found\n",address);
423           break;
424         }
425         cs = cs->next;
426       }
427     }
428
429     if (cs == NULL )
430     {
431       /* create new session object */
432       cs = GNUNET_malloc ( sizeof( struct Session) );
433       cs->addr = GNUNET_malloc ( sizeof (struct sockaddr_in) );
434
435       cs->ip = address;
436       memcpy(cs->addr, conn_info->client_addr, sizeof (struct sockaddr_in));
437       memcpy(&cs->sender, &pi_in, sizeof (struct GNUNET_PeerIdentity));
438       cs->next = NULL;
439       cs->is_active = GNUNET_YES;
440
441       /* Insert session into linked list */
442       if ( plugin->sessions == NULL)
443       {
444         plugin->sessions = cs;
445         plugin->session_count = 1;
446       }
447       cs_temp = plugin->sessions;
448       while ( cs_temp->next != NULL )
449       {
450         cs_temp = cs_temp->next;
451       }
452       if (cs_temp != cs )
453       {
454         cs_temp->next = cs;
455         plugin->session_count++;
456       }
457       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", address, plugin->session_count);
458     }
459     /* Set closure */
460     if (*httpSessionCache == NULL)
461       *httpSessionCache = cs;
462   }
463   else
464     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session already known");
465
466   /* Is it a PUT or a GET request */
467   if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
468   {
469     /* PUT method here */
470     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %lu \n",(*upload_data_size));
471     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
472     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT Request: `%s'\n",upload_data);
473     /* No data left */
474     *upload_data_size = 0;
475     response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
476     MHD_queue_response (session, MHD_HTTP_OK, response);
477     MHD_destroy_response (response);
478   }
479   if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
480   {
481     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
482     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
483
484     response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
485     MHD_queue_response (session, MHD_HTTP_OK, response);
486     MHD_destroy_response (response);
487   }
488
489   return MHD_YES;
490 }
491
492
493 /**
494  * Call MHD to process pending requests and then go back
495  * and schedule the next run.
496  */
497 static void http_daemon_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
498
499 /**
500  * Function that queries MHD's select sets and
501  * starts the task waiting for them.
502  */
503 static GNUNET_SCHEDULER_TaskIdentifier
504 http_daemon_prepare (struct MHD_Daemon *daemon_handle)
505 {
506   GNUNET_SCHEDULER_TaskIdentifier ret;
507   fd_set rs;
508   fd_set ws;
509   fd_set es;
510   struct GNUNET_NETWORK_FDSet *wrs;
511   struct GNUNET_NETWORK_FDSet *wws;
512   struct GNUNET_NETWORK_FDSet *wes;
513   int max;
514   unsigned long long timeout;
515   int haveto;
516   struct GNUNET_TIME_Relative tv;
517
518   FD_ZERO(&rs);
519   FD_ZERO(&ws);
520   FD_ZERO(&es);
521   wrs = GNUNET_NETWORK_fdset_create ();
522   wes = GNUNET_NETWORK_fdset_create ();
523   wws = GNUNET_NETWORK_fdset_create ();
524   max = -1;
525   GNUNET_assert (MHD_YES ==
526                  MHD_get_fdset (daemon_handle,
527                                 &rs,
528                                 &ws,
529                                 &es,
530                                 &max));
531   haveto = MHD_get_timeout (daemon_handle, &timeout);
532   if (haveto == MHD_YES)
533     tv.value = (uint64_t) timeout;
534   else
535     tv = GNUNET_TIME_UNIT_FOREVER_REL;
536   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
537   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
538   GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
539   ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
540                                      GNUNET_SCHEDULER_PRIORITY_HIGH,
541                                      GNUNET_SCHEDULER_NO_TASK,
542                                      tv,
543                                      wrs,
544                                      wws,
545                                      &http_daemon_run,
546                                      daemon_handle);
547   GNUNET_NETWORK_fdset_destroy (wrs);
548   GNUNET_NETWORK_fdset_destroy (wws);
549   GNUNET_NETWORK_fdset_destroy (wes);
550   return ret;
551 }
552
553 /**
554  * Call MHD to process pending requests and then go back
555  * and schedule the next run.
556  */
557 static void
558 http_daemon_run (void *cls,
559             const struct GNUNET_SCHEDULER_TaskContext *tc)
560 {
561   struct MHD_Daemon *daemon_handle = cls;
562
563   if (daemon_handle == http_daemon_v4)
564     http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
565
566   if (daemon_handle == http_daemon_v6)
567     http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
568
569   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
570     return;
571
572
573
574   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
575   if (daemon_handle == http_daemon_v4)
576     http_task_v4 = http_daemon_prepare (daemon_handle);
577   if (daemon_handle == http_daemon_v6)
578     http_task_v6 = http_daemon_prepare (daemon_handle);
579   return;
580 }
581
582 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
583 {
584   size_t retcode;
585   /*
586   fprintf(stdout, "*** Read callback: size %u, size nmemb: %u \n", size, nmemb);
587   retcode = fread(ptr, size, nmemb, stream);
588    */
589   retcode = 0;
590   return retcode;
591 }
592
593 /**
594  * Function that can be used by the transport service to transmit
595  * a message using the plugin.
596  *
597  * @param cls closure
598  * @param target who should receive this message
599  * @param priority how important is the message
600  * @param msgbuf the message to transmit
601  * @param msgbuf_size number of bytes in 'msgbuf'
602  * @param timeout when should we time out
603  * @param session which session must be used (or NULL for "any")
604  * @param addr the address to use (can be NULL if the plugin
605  *                is "on its own" (i.e. re-use existing TCP connection))
606  * @param addrlen length of the address in bytes
607  * @param force_address GNUNET_YES if the plugin MUST use the given address,
608  *                otherwise the plugin may use other addresses or
609  *                existing connections (if available)
610  * @param cont continuation to call once the message has
611  *        been transmitted (or if the transport is ready
612  *        for the next transmission call; or if the
613  *        peer disconnected...)
614  * @param cont_cls closure for cont
615  * @return number of bytes used (on the physical network, with overheads);
616  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
617  *         and does NOT mean that the message was not transmitted (DV)
618  */
619 static ssize_t
620 template_plugin_send (void *cls,
621                       const struct GNUNET_PeerIdentity *
622                       target,
623                       const char *msgbuf,
624                       size_t msgbuf_size,
625                       unsigned int priority,
626                       struct GNUNET_TIME_Relative timeout,
627                       struct Session *session,
628                       const void *addr,
629                       size_t addrlen,
630                       int force_address,
631                       GNUNET_TRANSPORT_TransmitContinuation
632                       cont, void *cont_cls)
633 {
634   struct Session* ses;
635   int bytes_sent = 0;
636   /*  struct Plugin *plugin = cls; */
637   CURL *curl_handle;
638   /* CURLcode res; */
639
640   /* find session for peer */
641   ses = find_session_by_pi (target);
642   if ( ses == NULL) create_session_by_pi (target);
643
644   char *url = "http://localhost:12389";
645
646   curl_handle = curl_easy_init();
647   if( NULL == curl_handle)
648   {
649     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
650     return -1;
651   }
652   curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
653   curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_callback);
654   curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1L);
655   curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
656   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
657   curl_easy_setopt(curl_handle, CURLOPT_READDATA, msgbuf);
658   curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE,
659                   (curl_off_t)msgbuf_size);
660
661
662
663   return bytes_sent;
664 }
665
666
667
668 /**
669  * Function that can be used to force the plugin to disconnect
670  * from the given peer and cancel all previous transmissions
671  * (and their continuationc).
672  *
673  * @param cls closure
674  * @param target peer from which to disconnect
675  */
676 static void
677 template_plugin_disconnect (void *cls,
678                             const struct GNUNET_PeerIdentity *target)
679 {
680   // struct Plugin *plugin = cls;
681   // FIXME
682 }
683
684
685 /**
686  * Convert the transports address to a nice, human-readable
687  * format.
688  *
689  * @param cls closure
690  * @param type name of the transport that generated the address
691  * @param addr one of the addresses of the host, NULL for the last address
692  *        the specific address format depends on the transport
693  * @param addrlen length of the address
694  * @param numeric should (IP) addresses be displayed in numeric form?
695  * @param timeout after how long should we give up?
696  * @param asc function to call on each string
697  * @param asc_cls closure for asc
698  */
699 static void
700 template_plugin_address_pretty_printer (void *cls,
701                                         const char *type,
702                                         const void *addr,
703                                         size_t addrlen,
704                                         int numeric,
705                                         struct GNUNET_TIME_Relative timeout,
706                                         GNUNET_TRANSPORT_AddressStringCallback
707                                         asc, void *asc_cls)
708 {
709   asc (asc_cls, NULL);
710 }
711
712
713
714 /**
715  * Another peer has suggested an address for this
716  * peer and transport plugin.  Check that this could be a valid
717  * address.  If so, consider adding it to the list
718  * of addresses.
719  *
720  * @param cls closure
721  * @param addr pointer to the address
722  * @param addrlen length of addr
723  * @return GNUNET_OK if this is a plausible address for this peer
724  *         and transport
725  */
726 static int
727 template_plugin_address_suggested (void *cls,
728                                   void *addr, size_t addrlen)
729 {
730   /* struct Plugin *plugin = cls; */
731
732   /* check if the address is plausible; if so,
733      add it to our list! */
734   return GNUNET_OK;
735 }
736
737
738 /**
739  * Function called for a quick conversion of the binary address to
740  * a numeric address.  Note that the caller must not free the
741  * address and that the next call to this function is allowed
742  * to override the address again.
743  *
744  * @param cls closure
745  * @param addr binary address
746  * @param addrlen length of the address
747  * @return string representing the same address
748  */
749 static const char*
750 template_plugin_address_to_string (void *cls,
751                                    const void *addr,
752                                    size_t addrlen)
753 {
754   GNUNET_break (0);
755   return NULL;
756 }
757
758 /**
759  * Exit point from the plugin.
760  */
761 void *
762 libgnunet_plugin_transport_http_done (void *cls)
763 {
764   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
765   struct Plugin *plugin = api->cls;
766   struct Session * cs;
767   struct Session * cs_next;
768
769   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unloading http plugin...\n");
770
771   if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
772   {
773     GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
774     http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
775   }
776
777   if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
778   {
779     GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
780     http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
781   }
782
783   if (http_daemon_v4 != NULL)
784   {
785     MHD_stop_daemon (http_daemon_v4);
786     http_daemon_v4 = NULL;
787   }
788   if (http_daemon_v6 != NULL)
789   {
790     MHD_stop_daemon (http_daemon_v6);
791     http_daemon_v6 = NULL;
792   }
793
794   curl_multi_cleanup(multi_handle);
795
796   /* free all sessions */
797   cs = plugin->sessions;
798   while ( NULL != cs)
799     {
800       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session to `%s'\n",cs->ip);
801       cs_next = cs->next;
802       GNUNET_free (cs->ip);
803       GNUNET_free (cs->addr);
804       GNUNET_free (cs);
805       plugin->session_count--;
806       cs = cs_next;
807     }
808
809   GNUNET_free (plugin);
810   GNUNET_free (api);
811   return NULL;
812 }
813
814
815 /**
816  * Entry point for the plugin.
817  */
818 void *
819 libgnunet_plugin_transport_http_init (void *cls)
820 {
821   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
822   struct GNUNET_TRANSPORT_PluginFunctions *api;
823
824   long long unsigned int port;
825
826   plugin = GNUNET_malloc (sizeof (struct Plugin));
827   plugin->env = env;
828   plugin->sessions = NULL;
829   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
830   api->cls = plugin;
831   api->send = &template_plugin_send;
832   api->disconnect = &template_plugin_disconnect;
833   api->address_pretty_printer = &template_plugin_address_pretty_printer;
834   api->check_address = &template_plugin_address_suggested;
835   api->address_to_string = &template_plugin_address_to_string;
836
837   hostname = GNUNET_RESOLVER_local_fqdn_get ();
838
839   /* Hashing our identity to use it in URLs */
840   GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &my_ascii_hash_ident);
841
842   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
843   /* Reading port number from config file */
844   if ((GNUNET_OK !=
845        GNUNET_CONFIGURATION_get_value_number (env->cfg,
846                                               "transport-http",
847                                               "PORT",
848                                               &port)) ||
849       (port > 65535) )
850     {
851       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
852                        "http",
853                        _
854                        ("Require valid port number for transport plugin `%s' in configuration!\n"),
855                        "transport-http");
856       libgnunet_plugin_transport_http_done (api);
857       return NULL;
858     }
859
860   if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
861     {
862     http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
863                                        port,
864                                        &acceptPolicyCallback,
865                                        NULL , &accessHandlerCallback, NULL,
866                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
867                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
868                                        MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
869                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
870                                        MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
871                                        MHD_OPTION_END);
872     http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
873                                        port,
874                                        &acceptPolicyCallback,
875                                        NULL , &accessHandlerCallback, NULL,
876                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
877                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
878                                        MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
879                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
880                                        MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
881                                        MHD_OPTION_END);
882     }
883   if (http_daemon_v4 != NULL)
884     http_task_v4 = http_daemon_prepare (http_daemon_v4);
885   if (http_daemon_v6 != NULL)
886     http_task_v6 = http_daemon_prepare (http_daemon_v6);
887
888   if (http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
889     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
890   if (http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
891     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
892
893   /* Initializing cURL */
894   multi_handle = curl_multi_init();
895
896   return api;
897 }
898
899 /* end of plugin_transport_template.c */