-fixing shutdown sequence
[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 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file hostlist/hostlist-server.c
23  * @author Christian Grothoff, Matthias Wachs
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
36 /**
37  * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
38  */
39 static struct MHD_Daemon *daemon_handle_v6;
40
41 /**
42  * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
43  */
44 static struct MHD_Daemon *daemon_handle_v4;
45
46 /**
47  * Our configuration.
48  */
49 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50
51 /**
52  * For keeping statistics.
53  */
54 static struct GNUNET_STATISTICS_Handle *stats;
55
56 /**
57  * Handle to the core service (NULL until we've connected to it).
58  */
59 static struct GNUNET_CORE_Handle *core;
60
61 /**
62  * Handle to the peerinfo notify service (NULL until we've connected to it).
63  */
64 static struct GNUNET_PEERINFO_NotifyContext *notify;
65
66 /**
67  * Our primary task for IPv4.
68  */
69 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
70
71 /**
72  * Our primary task for IPv6.
73  */
74 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
75
76 /**
77  * Our canonical response.
78  */
79 static struct MHD_Response *response;
80
81 /**
82  * NULL if we are not currenlty iterating over peer information.
83  */
84 static struct GNUNET_PEERINFO_IteratorContext *pitr;
85
86 /**
87  * Handle for accessing peerinfo service.
88  */
89 static struct GNUNET_PEERINFO_Handle *peerinfo;
90
91 /**
92  * Context for host processor.
93  */
94 struct HostSet
95 {
96   unsigned int size;
97
98   char *data;
99 };
100
101 /**
102  * Set if we are allowed to advertise our hostlist to others.
103  */
104 static int advertising;
105
106 /**
107  * Buffer for the hostlist address
108  */
109 static char *hostlist_uri;
110
111
112 /**
113  * Function that assembles our response.
114  */
115 static void
116 finish_response (struct HostSet *results)
117 {
118   if (response != NULL)
119     MHD_destroy_response (response);
120   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121               "Creating hostlist response with %u bytes\n",
122               (unsigned int) results->size);
123   response =
124       MHD_create_response_from_data (results->size, results->data, MHD_YES,
125                                      MHD_NO);
126   if ((daemon_handle_v4 == NULL) && (daemon_handle_v6 == NULL))
127   {
128     MHD_destroy_response (response);
129     response = NULL;
130   }
131   GNUNET_STATISTICS_set (stats, gettext_noop ("bytes in hostlist"),
132                          results->size, GNUNET_YES);
133   GNUNET_free (results);
134 }
135
136
137 /**
138  * Set 'cls' to GNUNET_YES (we have an address!).
139  *
140  * @param cls closure, an 'int*'
141  * @param address the address (ignored)
142  * @param expiration expiration time (call is ignored if this is in the past)
143  * @return  GNUNET_SYSERR to stop iterating (unless expiration has occured)
144  */
145 static int
146 check_has_addr (void *cls, const struct GNUNET_HELLO_Address *address,
147                 struct GNUNET_TIME_Absolute expiration)
148 {
149   int *arg = cls;
150
151   if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
152   {
153     GNUNET_STATISTICS_update (stats,
154                               gettext_noop ("expired addresses encountered"), 1,
155                               GNUNET_YES);
156     return GNUNET_YES;          /* ignore this address */
157   }
158   *arg = GNUNET_YES;
159   return GNUNET_SYSERR;
160 }
161
162
163 /**
164  * Callback that processes each of the known HELLOs for the
165  * hostlist response construction.
166  */
167 static void
168 host_processor (void *cls, const struct GNUNET_PeerIdentity *peer,
169                 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
170 {
171   struct HostSet *results = cls;
172   size_t old;
173   size_t s;
174   int has_addr;
175
176   if (err_msg != NULL)
177   {
178     GNUNET_assert (NULL == peer);
179     pitr = NULL;
180     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181                 _("Error in communication with PEERINFO service: %s\n"),
182                 err_msg);
183     return;
184   }
185   if (peer == NULL)
186   {
187     pitr = NULL;
188     finish_response (results);
189     return;
190   }
191   if (hello == NULL)
192     return;
193   has_addr = GNUNET_NO;
194   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_has_addr, &has_addr);
195   if (GNUNET_NO == has_addr)
196   {
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198                 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
199                 GNUNET_i2s (peer));
200     GNUNET_STATISTICS_update (stats,
201                               gettext_noop
202                               ("HELLOs without addresses encountered (ignored)"),
203                               1, GNUNET_NO);
204     return;
205   }
206   old = results->size;
207   s = GNUNET_HELLO_size (hello);
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209               "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
210               (unsigned int) s, "HELLO", GNUNET_i2s (peer));
211   if ((old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
212       (old + s >= MAX_BYTES_PER_HOSTLISTS))
213   {
214     GNUNET_STATISTICS_update (stats,
215                               gettext_noop
216                               ("bytes not included in hostlist (size limit)"),
217                               s, GNUNET_NO);
218     return;                     /* too large, skip! */
219   }
220   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
221               "Adding peer `%s' to hostlist (%u bytes)\n", GNUNET_i2s (peer),
222               (unsigned int) s);
223   GNUNET_array_grow (results->data, results->size, old + s);
224   memcpy (&results->data[old], hello, s);
225 }
226
227
228
229 /**
230  * Hostlist access policy (very permissive, allows everything).
231  */
232 static int
233 accept_policy_callback (void *cls, const struct sockaddr *addr,
234                         socklen_t addrlen)
235 {
236   if (NULL == response)
237   {
238     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239                 "Received request for hostlist, but I am not yet ready; rejecting!\n");
240     return MHD_NO;
241   }
242   return MHD_YES;               /* accept all */
243 }
244
245
246 /**
247  * Main request handler.
248  */
249 static int
250 access_handler_callback (void *cls, struct MHD_Connection *connection,
251                          const char *url, const char *method,
252                          const char *version, const char *upload_data,
253                          size_t * upload_data_size, void **con_cls)
254 {
255   static int dummy;
256
257   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
258   {
259     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
260                 _("Refusing `%s' request to hostlist server\n"), method);
261     GNUNET_STATISTICS_update (stats,
262                               gettext_noop
263                               ("hostlist requests refused (not HTTP GET)"), 1,
264                               GNUNET_YES);
265     return MHD_NO;
266   }
267   if (NULL == *con_cls)
268   {
269     (*con_cls) = &dummy;
270     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Sending 100 CONTINUE reply\n"));
271     return MHD_YES;             /* send 100 continue */
272   }
273   if (*upload_data_size != 0)
274   {
275     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
276                 _("Refusing `%s' request with %llu bytes of upload data\n"),
277                 method, (unsigned long long) *upload_data_size);
278     GNUNET_STATISTICS_update (stats,
279                               gettext_noop
280                               ("hostlist requests refused (upload data)"), 1,
281                               GNUNET_YES);
282     return MHD_NO;              /* do not support upload data */
283   }
284   if (response == NULL)
285   {
286     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287                 _
288                 ("Could not handle hostlist request since I do not have a response yet\n"));
289     GNUNET_STATISTICS_update (stats,
290                               gettext_noop
291                               ("hostlist requests refused (not ready)"), 1,
292                               GNUNET_YES);
293     return MHD_NO;              /* internal error, no response yet */
294   }
295   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received request for our hostlist\n"));
296   GNUNET_STATISTICS_update (stats, gettext_noop ("hostlist requests processed"),
297                             1, GNUNET_YES);
298   return MHD_queue_response (connection, MHD_HTTP_OK, response);
299 }
300
301
302 /**
303  * Handler called by core when core is ready to transmit message
304  * @param cls   closure
305  * @param size  size of buffer to copy message to
306  * @param buf   buffer to copy message to
307  */
308 static size_t
309 adv_transmit_ready (void *cls, size_t size, void *buf)
310 {
311   static uint64_t hostlist_adv_count;
312
313   size_t transmission_size;
314   size_t uri_size;              /* Including \0 termination! */
315   struct GNUNET_MessageHeader header;
316   char *cbuf;
317
318   if (buf == NULL)
319   {
320     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321                 "Transmission failed, buffer invalid!\n");
322     return 0;
323   }
324   uri_size = strlen (hostlist_uri) + 1;
325   transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
326   header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
327   header.size = htons (transmission_size);
328   GNUNET_assert (size >= transmission_size);
329   memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
330   cbuf = buf;
331   memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)], hostlist_uri, uri_size);
332   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333               "Sent advertisement message: Copied %u bytes into buffer!\n",
334               (unsigned int) transmission_size);
335   hostlist_adv_count++;
336   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " # Sent advertisement message: %u\n",
337               hostlist_adv_count);
338   GNUNET_STATISTICS_update (stats,
339                             gettext_noop ("# hostlist advertisements send"), 1,
340                             GNUNET_NO);
341   return transmission_size;
342 }
343
344
345 /**
346  * Method called whenever a given peer connects.
347  *
348  * @param cls closure
349  * @param peer peer identity this notification is about
350  * @param atsi performance data
351  * @param atsi_count number of records in 'atsi'
352  */
353 static void
354 connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
355                  const struct GNUNET_ATS_Information *atsi,
356                  unsigned int atsi_count)
357 {
358   size_t size;
359
360   if (!advertising)
361     return;
362   if (hostlist_uri == NULL)
363     return;
364   size = strlen (hostlist_uri) + 1;
365   if (size + sizeof (struct GNUNET_MessageHeader) >=
366       GNUNET_SERVER_MAX_MESSAGE_SIZE)
367   {
368     GNUNET_break (0);
369     return;
370   }
371   size += sizeof (struct GNUNET_MessageHeader);
372   if (NULL == core)
373   {
374     GNUNET_break (0);
375     return;
376   }
377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378               "Asked core to transmit advertisement message with a size of %u bytes to peer `%s'\n",
379               size, GNUNET_i2s (peer));
380   if (NULL ==
381       GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES, 0,
382                                          GNUNET_ADV_TIMEOUT, peer, size,
383                                          &adv_transmit_ready, NULL))
384   {
385     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
386                 _("Advertisement message could not be queued by core\n"));
387   }
388 }
389
390
391 /**
392  * Method called whenever a given peer disconnects.
393  *
394  * @param cls closure
395  * @param peer peer identity this notification is about
396  */
397 static void
398 disconnect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
399 {
400   /* nothing to do */
401 }
402
403 /**
404  * PEERINFO calls this function to let us know about a possible peer
405  * that we might want to connect to.
406  *
407  * @param cls closure (not used)
408  * @param peer potential peer to connect to
409  * @param hello HELLO for this peer (or NULL)
410  * @param err_msg NULL if successful, otherwise contains error message
411  */
412 static void
413 process_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
414                 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
415 {
416   struct HostSet *results;
417
418   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419               "Peerinfo is notifying us to rebuild our hostlist\n");
420   if (err_msg != NULL)
421   {
422     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423                 _("Error in communication with PEERINFO service\n"));
424     /* return; */
425   }
426   results = GNUNET_malloc (sizeof (struct HostSet));
427   GNUNET_assert (peerinfo != NULL);
428   pitr =
429       GNUNET_PEERINFO_iterate (peerinfo, NULL, GNUNET_TIME_UNIT_MINUTES,
430                                &host_processor, results);
431 }
432
433 /**
434  * Function that queries MHD's select sets and
435  * starts the task waiting for them.
436  */
437 static GNUNET_SCHEDULER_TaskIdentifier
438 prepare_daemon (struct MHD_Daemon *daemon_handle);
439
440
441 /**
442  * Call MHD to process pending requests and then go back
443  * and schedule the next run.
444  */
445 static void
446 run_daemon (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
447 {
448   struct MHD_Daemon *daemon_handle = cls;
449
450   if (daemon_handle == daemon_handle_v4)
451     hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
452   else
453     hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
454
455   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
456     return;
457   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
458   if (daemon_handle == daemon_handle_v4)
459     hostlist_task_v4 = prepare_daemon (daemon_handle);
460   else
461     hostlist_task_v6 = prepare_daemon (daemon_handle);
462 }
463
464
465 /**
466  * Function that queries MHD's select sets and
467  * starts the task waiting for them.
468  */
469 static GNUNET_SCHEDULER_TaskIdentifier
470 prepare_daemon (struct MHD_Daemon *daemon_handle)
471 {
472   GNUNET_SCHEDULER_TaskIdentifier ret;
473   fd_set rs;
474   fd_set ws;
475   fd_set es;
476   struct GNUNET_NETWORK_FDSet *wrs;
477   struct GNUNET_NETWORK_FDSet *wws;
478   struct GNUNET_NETWORK_FDSet *wes;
479   int max;
480   unsigned MHD_LONG_LONG timeout;
481   int haveto;
482   struct GNUNET_TIME_Relative tv;
483
484   FD_ZERO (&rs);
485   FD_ZERO (&ws);
486   FD_ZERO (&es);
487   wrs = GNUNET_NETWORK_fdset_create ();
488   wes = GNUNET_NETWORK_fdset_create ();
489   wws = GNUNET_NETWORK_fdset_create ();
490   max = -1;
491   GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
492   haveto = MHD_get_timeout (daemon_handle, &timeout);
493   if (haveto == MHD_YES)
494     tv.rel_value = (uint64_t) timeout;
495   else
496     tv = GNUNET_TIME_UNIT_FOREVER_REL;
497   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
498   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
499   GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
500   ret =
501       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
502                                    tv, wrs, wws,
503                                    &run_daemon, daemon_handle);
504   GNUNET_NETWORK_fdset_destroy (wrs);
505   GNUNET_NETWORK_fdset_destroy (wws);
506   GNUNET_NETWORK_fdset_destroy (wes);
507   return ret;
508 }
509
510
511 /**
512  * Start server offering our hostlist.
513  *
514  * @return GNUNET_OK on success
515  */
516 int
517 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
518                               struct GNUNET_STATISTICS_Handle *st,
519                               struct GNUNET_CORE_Handle *co,
520                               GNUNET_CORE_ConnectEventHandler *server_ch,
521                               GNUNET_CORE_DisconnectEventHandler *server_dh,
522                               int advertise)
523 {
524   unsigned long long port;
525   char *hostname;
526   char *ip;
527   size_t size;
528   struct in_addr i4;
529   struct in6_addr i6;
530   struct sockaddr_in v4;
531   struct sockaddr_in6 v6;
532   const struct sockaddr *sa;
533
534   advertising = advertise;
535   if (!advertising)
536     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537                 "Advertising not enabled on this hostlist server\n");
538   else
539     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540                 "Advertising enabled on this hostlist server\n");
541   cfg = c;
542   stats = st;
543   peerinfo = GNUNET_PEERINFO_connect (cfg);
544   if (peerinfo == NULL)
545   {
546     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
547                 _("Could not access PEERINFO service.  Exiting.\n"));
548     return GNUNET_SYSERR;
549   }
550   if (GNUNET_OK !=
551       GNUNET_CONFIGURATION_get_value_number (cfg, "HOSTLIST", "HTTPPORT",
552                                              &port))
553     return GNUNET_SYSERR;
554   if ((port == 0) || (port > UINT16_MAX))
555   {
556     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
557                 _("Invalid port number %llu.  Exiting.\n"), port);
558     return GNUNET_SYSERR;
559   }
560
561   if (GNUNET_SYSERR ==
562       GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
563                                              "EXTERNAL_DNS_NAME", &hostname))
564     hostname = GNUNET_RESOLVER_local_fqdn_get ();
565
566   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Hostlist service starts on %s:%llu\n"),
567               hostname, port);
568   if (NULL != hostname)
569   {
570     size = strlen (hostname);
571     if (size + 15 > MAX_URL_LEN)
572     {
573       GNUNET_break (0);
574     }
575     else
576     {
577       GNUNET_asprintf (&hostlist_uri, "http://%s:%u/", hostname,
578                        (unsigned int) port);
579       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
580                   _("Address to obtain hostlist: `%s'\n"), hostlist_uri);
581     }
582     GNUNET_free (hostname);
583   }
584
585   if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIP"))
586   {
587     GNUNET_break (GNUNET_OK ==
588                   GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
589                                                          "BINDTOIP", &ip));
590   }
591   else 
592     ip = NULL;
593   if (ip != NULL)
594   {
595     if (1 == inet_pton (AF_INET, ip, &i4))
596     {
597       memset (&v4, 0, sizeof (v4));
598       v4.sin_family = AF_INET;
599       v4.sin_addr = i4;
600       v4.sin_port = htons (port);
601 #if HAVE_SOCKADDR_IN_SIN_LEN
602       v4.sin_len = sizeof (v4);
603 #endif
604       sa = (const struct sockaddr *) &v4;
605     }
606     else if (1 == inet_pton (AF_INET6, ip, &i6))
607     {
608       memset (&v6, 0, sizeof (v6));
609       v6.sin6_family = AF_INET6;
610       v6.sin6_addr = i6;
611       v6.sin6_port = htons (port);
612 #if HAVE_SOCKADDR_IN_SIN_LEN
613       v6.sin6_len = sizeof (v6);
614 #endif
615       sa = (const struct sockaddr *) &v6;
616     }
617     else
618     {
619       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
620                   _("`%s' is not a valid IP address! Ignoring BINDTOIP.\n"),
621                   ip);
622       sa = NULL;
623     }
624   }
625   else
626     sa = NULL;
627
628   daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 | MHD_USE_DEBUG,
629                                        (uint16_t) port,
630                                        &accept_policy_callback, NULL,
631                                        &access_handler_callback, NULL,
632                                        MHD_OPTION_CONNECTION_LIMIT,
633                                        (unsigned int) 16,
634                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT,
635                                        (unsigned int) 1,
636                                        MHD_OPTION_CONNECTION_TIMEOUT,
637                                        (unsigned int) 16,
638                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
639                                        (size_t) (16 * 1024),
640                                        MHD_OPTION_SOCK_ADDR,
641                                        sa,
642                                        MHD_OPTION_END);
643   daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG | MHD_USE_DEBUG,
644                                        (uint16_t) port,
645                                        &accept_policy_callback, NULL,
646                                        &access_handler_callback, NULL,
647                                        MHD_OPTION_CONNECTION_LIMIT,
648                                        (unsigned int) 16,
649                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT,
650                                        (unsigned int) 1,
651                                        MHD_OPTION_CONNECTION_TIMEOUT,
652                                        (unsigned int) 16,
653                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
654                                        (size_t) (16 * 1024),
655                                        MHD_OPTION_SOCK_ADDR,
656                                        sa,
657                                        MHD_OPTION_END);
658
659   if ((daemon_handle_v6 == NULL) && (daemon_handle_v4 == NULL))
660   {
661     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662                 _("Could not start hostlist HTTP server on port %u\n"),
663                 (unsigned short) port);
664     return GNUNET_SYSERR;
665   }
666
667   core = co;
668   *server_ch = &connect_handler;
669   *server_dh = &disconnect_handler;
670   if (daemon_handle_v4 != NULL)
671     hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
672   if (daemon_handle_v6 != NULL)
673     hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
674
675   notify = GNUNET_PEERINFO_notify (cfg, process_notify, NULL);
676
677   return GNUNET_OK;
678 }
679
680
681 /**
682  * Stop server offering our hostlist.
683  */
684 void
685 GNUNET_HOSTLIST_server_stop ()
686 {
687   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist server shutdown\n");
688   if (NULL != notify)
689   {
690     GNUNET_PEERINFO_notify_cancel (notify);
691     notify = NULL;
692   }
693   if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
694   {
695     GNUNET_SCHEDULER_cancel (hostlist_task_v6);
696     hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
697   }
698   if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
699   {
700     GNUNET_SCHEDULER_cancel (hostlist_task_v4);
701     hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
702   }
703   if (pitr != NULL)
704   {
705     GNUNET_PEERINFO_iterate_cancel (pitr);
706     pitr = NULL;
707   }
708   if (NULL != daemon_handle_v4)
709   {
710     MHD_stop_daemon (daemon_handle_v4);
711     daemon_handle_v4 = NULL;
712   }
713   if (NULL != daemon_handle_v6)
714   {
715     MHD_stop_daemon (daemon_handle_v6);
716     daemon_handle_v6 = NULL;
717   }
718   if (response != NULL)
719   {
720     MHD_destroy_response (response);
721     response = NULL;
722   }
723   if (peerinfo != NULL)
724   {
725     GNUNET_PEERINFO_disconnect (peerinfo);
726     peerinfo = NULL;
727   }
728   cfg = NULL;
729   stats = NULL;
730   core = NULL;
731 }
732
733 /* end of hostlist-server.c */