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