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