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