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