(no commit message)
[oweals/gnunet.git] / src / hostlist / hostlist-server.c
1 /*
2      This file is part of GNUnet.
3      (C) 2008, 2009, 2010 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 hostlist/hostlist-server.c
23  * @author Christian Grothoff
24  * @brief application to provide an integrated hostlist HTTP server
25  */
26
27 #include "platform.h"
28 #include <microhttpd.h>
29 #include "hostlist-server.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet-daemon-hostlist.h"
33 #include "gnunet_resolver_service.h"
34
35 #define DEBUG_HOSTLIST_SERVER GNUNET_NO
36
37 /**
38  * How often should we recalculate our response to hostlist requests?
39  */
40 #define RESPONSE_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
41
42 /**
43  * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
44  */
45 static struct MHD_Daemon *daemon_handle_v6;
46
47 /**
48  * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
49  */
50 static struct MHD_Daemon *daemon_handle_v4;
51
52 /**
53  * Our configuration.
54  */
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
56
57 /**
58  * Our scheduler.
59  */
60 static struct GNUNET_SCHEDULER_Handle *sched;
61
62 /**
63  * For keeping statistics.
64  */ 
65 static struct GNUNET_STATISTICS_Handle *stats;
66
67 /**
68  * Handle to the core service (NULL until we've connected to it).
69  */
70 struct GNUNET_CORE_Handle *core;
71
72 /**
73  * Our primary task for IPv4.
74  */
75 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
76
77 /**
78  * Our primary task for IPv6.
79  */
80 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
81
82 /**
83  * Task that updates our HTTP response.
84  */
85 static GNUNET_SCHEDULER_TaskIdentifier response_task;
86
87 /**
88  * Our canonical response.
89  */
90 static struct MHD_Response *response;
91
92 /**
93  * NULL if we are not currenlty iterating over peer information.
94  */
95 static struct GNUNET_PEERINFO_IteratorContext *pitr;
96
97 /**
98  * Context for host processor.
99  */
100 struct HostSet
101 {
102   unsigned int size;
103
104   char *data;
105 };
106
107 /**
108  * Set if we are allowed to advertise our hostlist to others.
109  */
110 static int advertising;
111
112 /*
113  * How many times was the hostlist advertised?
114  */
115 static uint64_t hostlist_adv_count = 0;
116
117 /*
118  * Buffer for the hostlist address
119  */
120 char hostlist_uri[255];
121
122
123 /**
124  * Task that will produce a new response object.
125  */
126 static void
127 update_response (void *cls,
128                  const struct GNUNET_SCHEDULER_TaskContext *tc);
129
130
131 /**
132  * Function that assembles our response.
133  */
134 static void
135 finish_response (struct HostSet *results)
136 {
137   struct GNUNET_TIME_Relative freq;
138
139   if (response != NULL)
140     MHD_destroy_response (response);
141 #if DEBUG_HOSTLIST_SERVER
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143               "Creating hostlist response with %u bytes\n",
144               (unsigned int) results->size);
145 #endif
146   response = MHD_create_response_from_data (results->size,
147                                             results->data, MHD_YES, MHD_NO);
148   if ( (daemon_handle_v4 != NULL) ||
149        (daemon_handle_v6 != NULL) )
150     {
151       freq = RESPONSE_UPDATE_FREQUENCY;
152       if (results->size == 0)
153         freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
154       /* schedule next update of the response */
155       response_task = GNUNET_SCHEDULER_add_delayed (sched,
156                                                     freq,
157                                                     &update_response,
158                                                     NULL);
159     }
160   else
161     {
162       /* already past shutdown */
163       MHD_destroy_response (response);
164       response = NULL;
165     }
166   GNUNET_STATISTICS_set (stats,
167                          gettext_noop("bytes in hostlist"),
168                          results->size,
169                          GNUNET_YES);
170   GNUNET_free (results);
171 }
172
173
174 /**
175  * Set 'cls' to GNUNET_YES (we have an address!).
176  *
177  * @param cls closure, an 'int*'
178  * @param tname name of the transport (ignored)
179  * @param expiration expiration time (call is ignored if this is in the past)
180  * @param addr the address (ignored)
181  * @param addrlen length of the address (ignored)
182  * @return  GNUNET_SYSERR to stop iterating (unless expiration has occured)
183  */
184 static int
185 check_has_addr (void *cls,
186                 const char *tname,
187                 struct GNUNET_TIME_Absolute expiration,
188                 const void *addr, size_t addrlen)
189 {
190   int *arg = cls;
191
192   if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
193     {
194       GNUNET_STATISTICS_update (stats,
195                                 gettext_noop("expired addresses encountered"),
196                                 1,
197                                 GNUNET_YES);
198       return GNUNET_YES; /* ignore this address */
199     }
200   *arg = GNUNET_YES;
201   return GNUNET_SYSERR;
202 }
203
204
205 /**
206  * Callback that processes each of the known HELLOs for the
207  * hostlist response construction.
208  */
209 static void
210 host_processor (void *cls,
211                 const struct GNUNET_PeerIdentity * peer,
212                 const struct GNUNET_HELLO_Message *hello,
213                 uint32_t trust)
214 {
215   struct HostSet *results = cls;
216   size_t old;
217   size_t s;
218   int has_addr;
219   
220   if (peer == NULL)
221     {
222       pitr = NULL;
223       finish_response (results);
224       return;
225     }
226   if (hello == NULL)
227     return;
228   has_addr = GNUNET_NO;
229   GNUNET_HELLO_iterate_addresses (hello,
230                                   GNUNET_NO,
231                                   &check_has_addr,
232                                   &has_addr);
233   if (GNUNET_NO == has_addr)
234     {
235       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236                   "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
237                   GNUNET_i2s (peer));
238       GNUNET_STATISTICS_update (stats,
239                                 gettext_noop("HELLOs without addresses encountered (ignored)"),
240                                 1,
241                                 GNUNET_NO);
242       return; 
243     }
244   old = results->size;
245   s = GNUNET_HELLO_size(hello);
246 #if DEBUG_HOSTLIST_SERVER
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248               "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
249               (unsigned int) s,
250               "HELLO",
251               GNUNET_i2s (peer));
252 #endif
253   if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
254     {
255       GNUNET_STATISTICS_update (stats,
256                                 gettext_noop("bytes not included in hostlist (size limit)"),
257                                 s,
258                                 GNUNET_NO);
259       return; /* too large, skip! */
260     }
261   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
262               "Adding peer `%s' to hostlist (%u bytes)\n",
263               GNUNET_i2s (peer),
264               (unsigned int) s);
265   GNUNET_array_grow (results->data,
266                      results->size,
267                      old + s);
268   memcpy (&results->data[old], hello, s);
269 }
270
271
272 /**
273  * Task that will produce a new response object.
274  */
275 static void
276 update_response (void *cls,
277                  const struct GNUNET_SCHEDULER_TaskContext *tc)
278 {
279   struct HostSet *results;
280
281   response_task = GNUNET_SCHEDULER_NO_TASK;
282   results = GNUNET_malloc(sizeof(struct HostSet));
283   pitr = GNUNET_PEERINFO_iterate (cfg, sched, 
284                                   NULL,
285                                   0, 
286                                   GNUNET_TIME_UNIT_MINUTES,
287                                   &host_processor,
288                                   results);
289 }
290
291
292 /**
293  * Hostlist access policy (very permissive, allows everything).
294  */
295 static int
296 accept_policy_callback (void *cls,
297                         const struct sockaddr *addr, socklen_t addrlen)
298 {
299   if (NULL == response)
300     {
301 #if DEBUG_HOSTLIST_SERVER
302       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303                   "Received request for hostlist, but I am not yet ready; rejecting!\n");
304 #endif
305       return MHD_NO;
306     }
307   return MHD_YES;               /* accept all */
308 }
309
310
311 /**
312  * Main request handler.
313  */
314 static int
315 access_handler_callback (void *cls,
316                          struct MHD_Connection *connection,
317                          const char *url,
318                          const char *method,
319                          const char *version,
320                          const char *upload_data,
321                          size_t*upload_data_size, void **con_cls)
322 {
323   static int dummy;
324   
325   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
326     {
327       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
328                   _("Refusing `%s' request to hostlist server\n"),
329                   method);
330       GNUNET_STATISTICS_update (stats,
331                                 gettext_noop("hostlist requests refused (not HTTP GET)"),
332                                 1,
333                                 GNUNET_YES);
334       return MHD_NO;
335     }
336   if (NULL == *con_cls)
337     {
338       (*con_cls) = &dummy;
339 #if DEBUG_HOSTLIST_SERVER
340       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341                   _("Sending 100 CONTINUE reply\n"));
342 #endif
343       return MHD_YES;           /* send 100 continue */
344     }
345   if (*upload_data_size != 0)
346     {
347       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
348                   _("Refusing `%s' request with %llu bytes of upload data\n"),
349                   method,
350                   (unsigned long long) *upload_data_size);
351       GNUNET_STATISTICS_update (stats,
352                                 gettext_noop("hostlist requests refused (upload data)"),
353                                 1,
354                                 GNUNET_YES);
355       return MHD_NO;              /* do not support upload data */
356     }
357   if (response == NULL)
358     {
359       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
360                   _("Could not handle hostlist request since I do not have a response yet\n"));
361       GNUNET_STATISTICS_update (stats,
362                                 gettext_noop("hostlist requests refused (not ready)"),
363                                 1,
364                                 GNUNET_YES);
365       return MHD_NO;              /* internal error, no response yet */
366     }
367   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
368               _("Received request for our hostlist\n"));
369   GNUNET_STATISTICS_update (stats,
370                             gettext_noop("hostlist requests processed"),
371                             1,
372                             GNUNET_YES);
373   return MHD_queue_response (connection, MHD_HTTP_OK, response);
374 }
375
376 /**
377  * Handler called by core when core is ready to transmit message
378  * @param cls   closure
379  * @param size  size of buffer to copy message to
380  * @param buf   buffer to copy message to
381  */
382 static size_t
383 adv_transmit_ready ( void *cls, size_t size, void *buf)
384 {
385   size_t transmission_size;
386   size_t uri_size; /* Including \0 termination! */
387   uri_size = strlen ( hostlist_uri ) + 1;
388
389   struct GNUNET_HOSTLIST_ADV_Message * adv_message;
390   adv_message = GNUNET_malloc ( sizeof(struct GNUNET_HOSTLIST_ADV_Message) + uri_size);
391   if ( NULL == adv_message)
392    {
393    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
394        "Could not allocate memory for the message");
395    return GNUNET_NO;
396    }
397   transmission_size = sizeof (struct GNUNET_HOSTLIST_ADV_Message) + uri_size;
398
399   adv_message->header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
400   adv_message->header.size = htons (transmission_size);
401   memcpy(&adv_message[1],hostlist_uri,uri_size);
402
403   if (buf == NULL)
404     {
405       GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG, "Transmission failed, buffer invalid!\n" );
406       return 0;
407     }
408
409   if ( size >= transmission_size )
410     {
411       memcpy ( buf, adv_message, transmission_size );
412       GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG, "Sent advertisement message: Copied %d bytes into buffer!\n", transmission_size);
413       GNUNET_free ( adv_message );
414       return transmission_size;
415     }
416
417   hostlist_adv_count++;
418   GNUNET_STATISTICS_set (stats,
419                          gettext_noop("# hostlist advertisements send"),
420                          hostlist_adv_count,
421                          GNUNET_YES);
422
423   GNUNET_free (adv_message  );
424   return size;
425 }
426
427 /**
428  * Method that asks core service to transmit the message to the peer
429  * @param peer peer to transmit message to
430  * @param size size of the message
431  */
432 static size_t
433 adv_transmit_message ( const struct GNUNET_PeerIdentity * peer, size_t size )
434 {
435   /* transmit message to peer */
436   if ( NULL == core)
437     {
438       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
439                   _("Not connected to core, unable to send advertisement message\n"));
440       return GNUNET_NO;
441     }
442
443   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, GNUNET_ADV_TIMEOUT);
444   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445               _("Asked core to transmit advertisement message with a size of %u bytes\n"), size);
446   struct GNUNET_CORE_TransmitHandle * th;
447   th = GNUNET_CORE_notify_transmit_ready (core,
448                                      0,
449                                      timeout,
450                                      peer,
451                                      size,
452                                      &adv_transmit_ready, NULL);
453   if ( NULL == th )
454     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
455                 _("Advertisement message could not be queued by core\n"));
456     return GNUNET_NO;
457
458   return GNUNET_YES;
459 }
460
461 /**
462  * Method that assembles our hostlist advertisement message
463  * @param peer peer to send the hostlist advertisement
464  */
465 static size_t
466 adv_create_message ( const struct GNUNET_PeerIdentity * peer )
467
468 {
469   size_t length  = 0;
470   size_t size    = 0;
471   unsigned long long port;
472
473   char *uri;
474   char hostname[GNUNET_OS_get_hostname_max_length() + 1];
475   char *protocol = "http://";
476   char *port_s = GNUNET_malloc(6 * sizeof(char));
477
478   if (0 != gethostname (hostname, sizeof (hostname) - 1))
479   {
480     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
481         "Could not get system's hostname, unable to create advertisement message");
482     return GNUNET_NO;
483   }
484   if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
485                                                    "HOSTLIST",
486                                                    "HTTPPORT",
487                                                    &port))
488     return GNUNET_SYSERR;
489
490   sprintf(port_s, "%llu", port);
491   length = strlen(hostname)+strlen(protocol)+strlen(port_s)+2;
492   size = (length+1) * sizeof (char);
493   uri = GNUNET_malloc(size);
494   uri = strcpy(uri, protocol);
495   uri = strcat(uri, hostname);
496   uri = strcat(uri, ":");
497   uri = strcat(uri, port_s);
498   uri = strcat(uri, "/");
499   if ( length < 255);
500     strcpy(hostlist_uri,uri);
501
502   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Address to obtain hostlist: %s\n", hostlist_uri);
503
504   if ( ( size + sizeof( struct GNUNET_HOSTLIST_ADV_Message )) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
505     {
506       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
507           "Advertisement message is bigger than GNUNET allows");
508       return GNUNET_NO;
509     }
510
511   /* Request core to transmit message to peer */
512   size = size + sizeof ( struct GNUNET_HOSTLIST_ADV_Message );
513   adv_transmit_message(peer, size);
514
515   GNUNET_free ( port_s );
516   GNUNET_free ( uri );
517
518   return GNUNET_OK;
519 }
520
521 /**
522  * Method called whenever a given peer connects.
523  *
524  * @param cls closure
525  * @param peer peer identity this notification is about
526  * @param latency reported latency of the connection with 'other'
527  * @param distance reported distance (DV) to 'other'
528  */
529 static void
530 connect_handler (void *cls,
531                  const struct
532                  GNUNET_PeerIdentity * peer,
533                  struct GNUNET_TIME_Relative latency,
534                  uint32_t distance)
535 {
536   if ( !advertising )
537     return;
538
539   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540               "A new peer connected to the server, preparing to send hostlist advertisement\n");
541   /* create a new advertisement message */
542   if ( (GNUNET_OK != adv_create_message(peer)))
543   {
544     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
545                 _(" GNUNET_OK Could not create a hostlist advertisement message, impossible to advertise hostlist\n"));
546     return;
547   }
548 }
549
550
551 /**
552  * Method called whenever a given peer disconnects.
553  *
554  * @param cls closure
555  * @param peer peer identity this notification is about
556  */
557 static void
558 disconnect_handler (void *cls,
559                     const struct
560                     GNUNET_PeerIdentity * peer)
561 {
562
563 }
564
565
566 /**
567  * Function that queries MHD's select sets and
568  * starts the task waiting for them.
569  */
570 static GNUNET_SCHEDULER_TaskIdentifier
571 prepare_daemon (struct MHD_Daemon *daemon_handle);
572
573 /**
574  * Call MHD to process pending requests and then go back
575  * and schedule the next run.
576  */
577 static void
578 run_daemon (void *cls,
579             const struct GNUNET_SCHEDULER_TaskContext *tc)
580 {
581   struct MHD_Daemon *daemon_handle = cls;
582
583   if (daemon_handle == daemon_handle_v4)
584     hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
585   else
586     hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
587
588   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
589     return;    
590   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
591   if (daemon_handle == daemon_handle_v4)
592     hostlist_task_v4 = prepare_daemon (daemon_handle);
593   else
594     hostlist_task_v6 = prepare_daemon (daemon_handle);
595 }
596
597
598 /**
599  * Function that queries MHD's select sets and
600  * starts the task waiting for them.
601  */
602 static GNUNET_SCHEDULER_TaskIdentifier
603 prepare_daemon (struct MHD_Daemon *daemon_handle)
604 {
605   GNUNET_SCHEDULER_TaskIdentifier ret;
606   fd_set rs;
607   fd_set ws;
608   fd_set es;
609   struct GNUNET_NETWORK_FDSet *wrs;
610   struct GNUNET_NETWORK_FDSet *wws;
611   struct GNUNET_NETWORK_FDSet *wes;
612   int max;
613   unsigned long long timeout;
614   int haveto;
615   struct GNUNET_TIME_Relative tv;
616   
617   FD_ZERO(&rs);
618   FD_ZERO(&ws);
619   FD_ZERO(&es);
620   wrs = GNUNET_NETWORK_fdset_create ();
621   wes = GNUNET_NETWORK_fdset_create ();
622   wws = GNUNET_NETWORK_fdset_create ();
623   max = -1;
624   GNUNET_assert (MHD_YES ==
625                  MHD_get_fdset (daemon_handle,
626                                 &rs,
627                                 &ws,
628                                 &es,
629                                 &max));
630   haveto = MHD_get_timeout (daemon_handle, &timeout);
631   if (haveto == MHD_YES)
632     tv.value = (uint64_t) timeout;
633   else
634     tv = GNUNET_TIME_UNIT_FOREVER_REL;
635   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
636   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
637   GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
638   ret = GNUNET_SCHEDULER_add_select (sched,
639                                      GNUNET_SCHEDULER_PRIORITY_HIGH,
640                                      GNUNET_SCHEDULER_NO_TASK,
641                                      tv,
642                                      wrs,
643                                      wws,
644                                      &run_daemon,
645                                      daemon_handle);
646   GNUNET_NETWORK_fdset_destroy (wrs);
647   GNUNET_NETWORK_fdset_destroy (wws);
648   GNUNET_NETWORK_fdset_destroy (wes);
649   return ret;
650 }
651
652
653
654 /**
655  * Start server offering our hostlist.
656  *
657  * @return GNUNET_OK on success
658  */
659 int
660 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
661                               struct GNUNET_SCHEDULER_Handle *s,
662                               struct GNUNET_STATISTICS_Handle *st,
663                               struct GNUNET_CORE_Handle *co,
664                               GNUNET_CORE_ConnectEventHandler *server_ch,
665                               GNUNET_CORE_DisconnectEventHandler *server_dh,
666                               int advertise)
667 {
668   unsigned long long port;
669
670   advertising = advertise;
671   if  ( !advertising )
672     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673               "Advertising not enabled on this hostlist server\n");
674   else
675     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676               "Advertising enabled on this hostlist server\n");
677   sched = s;
678   cfg = c;
679   stats = st;
680   if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
681                                                    "HOSTLIST",
682                                                    "HTTPPORT", 
683                                                    &port))
684     return GNUNET_SYSERR;
685   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
686               _("Hostlist service starts on port %llu\n"),
687               port);
688   daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 
689 #if DEBUG_HOSTLIST_SERVER
690                                        | MHD_USE_DEBUG
691 #endif
692                                        ,
693                                        (unsigned short) port,
694                                        &accept_policy_callback,
695                                        NULL,
696                                        &access_handler_callback,
697                                        NULL,
698                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
699                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
700                                        MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
701                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
702                                        MHD_OPTION_END);
703   daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
704 #if DEBUG_HOSTLIST_SERVER
705                                        | MHD_USE_DEBUG
706 #endif
707                                        ,
708                                        (unsigned short) port,
709                                        &accept_policy_callback,
710                                        NULL,
711                                        &access_handler_callback,
712                                        NULL,
713                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
714                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
715                                        MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
716                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
717                                        MHD_OPTION_END);
718
719   if ( (daemon_handle_v6 == NULL) &&
720        (daemon_handle_v4 == NULL) )
721     {
722       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
723                   _("Could not start hostlist HTTP server on port %u\n"),
724                   (unsigned short) port);
725       return GNUNET_SYSERR;    
726     }
727
728   core=co;
729
730   *server_ch = &connect_handler;
731   *server_dh = &disconnect_handler;
732
733   if (daemon_handle_v4 != NULL)
734     hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
735   if (daemon_handle_v6 != NULL)
736     hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
737   response_task = GNUNET_SCHEDULER_add_now (sched,
738                                             &update_response,
739                                             NULL);
740   return GNUNET_OK;
741 }
742
743 /**
744  * Stop server offering our hostlist.
745  */
746 void
747 GNUNET_HOSTLIST_server_stop ()
748 {
749 #if DEBUG_HOSTLIST_SERVER
750   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751               "Hostlist server shutdown\n");
752 #endif
753   if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
754     {
755       GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
756       hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
757     }
758   if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
759     {
760       GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
761       hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
762     }
763   if (pitr != NULL)
764     {
765       GNUNET_PEERINFO_iterate_cancel (pitr);
766       pitr = NULL;
767     }
768   if (GNUNET_SCHEDULER_NO_TASK != response_task)
769     {
770       GNUNET_SCHEDULER_cancel (sched, response_task);
771       response_task = GNUNET_SCHEDULER_NO_TASK;
772     }
773   if (NULL != daemon_handle_v4)
774     {
775       MHD_stop_daemon (daemon_handle_v4);
776       daemon_handle_v4 = NULL;
777     }
778   if (NULL != daemon_handle_v6)
779     {
780       MHD_stop_daemon (daemon_handle_v6);
781       daemon_handle_v6 = NULL;
782     }
783   if (response != NULL)
784     {
785       MHD_destroy_response (response);
786       response = NULL;
787     }
788 }
789
790 /* end of hostlist-server.c */