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