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