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