add attestation API
[oweals/gnunet.git] / src / hostlist / gnunet-daemon-hostlist_server.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2008, 2009, 2010, 2014, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file hostlist/gnunet-daemon-hostlist_server.c
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  * @author David Barksdale
26  * @brief application to provide an integrated hostlist HTTP server
27  */
28 #include "platform.h"
29 #include <microhttpd.h>
30 #include "gnunet-daemon-hostlist_server.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet-daemon-hostlist.h"
34 #include "gnunet_resolver_service.h"
35
36
37 /**
38  * How long until our hostlist advertisment transmission via CORE should
39  * time out?
40  */
41 #define GNUNET_ADV_TIMEOUT \
42   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
43
44
45 /**
46  * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
47  */
48 static struct MHD_Daemon *daemon_handle_v6;
49
50 /**
51  * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
52  */
53 static struct MHD_Daemon *daemon_handle_v4;
54
55 /**
56  * Our configuration.
57  */
58 static const struct GNUNET_CONFIGURATION_Handle *cfg;
59
60 /**
61  * For keeping statistics.
62  */
63 static struct GNUNET_STATISTICS_Handle *stats;
64
65 /**
66  * Handle to the core service (NULL until we've connected to it).
67  */
68 static struct GNUNET_CORE_Handle *core;
69
70 /**
71  * Handle to the peerinfo notify service (NULL until we've connected to it).
72  */
73 static struct GNUNET_PEERINFO_NotifyContext *notify;
74
75 /**
76  * Our primary task for IPv4.
77  */
78 static struct GNUNET_SCHEDULER_Task *hostlist_task_v4;
79
80 /**
81  * Our primary task for IPv6.
82  */
83 static struct GNUNET_SCHEDULER_Task *hostlist_task_v6;
84
85 /**
86  * Our canonical response.
87  */
88 static struct MHD_Response *response;
89
90 /**
91  * Handle for accessing peerinfo service.
92  */
93 static struct GNUNET_PEERINFO_Handle *peerinfo;
94
95 /**
96  * Set if we are allowed to advertise our hostlist to others.
97  */
98 static int advertising;
99
100 /**
101  * Buffer for the hostlist address
102  */
103 static char *hostlist_uri;
104
105
106 /**
107  * Context for #host_processor().
108  */
109 struct HostSet
110 {
111   /**
112    * Iterator used to build @e data (NULL when done).
113    */
114   struct GNUNET_PEERINFO_IteratorContext *pitr;
115
116   /**
117    * Place where we accumulate all of the HELLO messages.
118    */
119   char *data;
120
121   /**
122    * Number of bytes in @e data.
123    */
124   unsigned int size;
125 };
126
127
128 /**
129  * NULL if we are not currenlty iterating over peer information.
130  */
131 static struct HostSet *builder;
132
133
134 /**
135  * Add headers to a request indicating that we allow Cross-Origin Resource
136  * Sharing.
137  *
138  * @param response response to add headers to
139  */
140 static void
141 add_cors_headers (struct MHD_Response *response)
142 {
143   MHD_add_response_header (response, "Access-Control-Allow-Origin", "*");
144   MHD_add_response_header (response,
145                            "Access-Control-Allow-Methods",
146                            "GET, OPTIONS");
147   MHD_add_response_header (response, "Access-Control-Max-Age", "86400");
148 }
149
150
151 /**
152  * Function that assembles our response.
153  */
154 static void
155 finish_response ()
156 {
157   if (NULL != response)
158     MHD_destroy_response (response);
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "Creating hostlist response with %u bytes\n",
161               (unsigned int) builder->size);
162   response = MHD_create_response_from_buffer (builder->size,
163                                               builder->data,
164                                               MHD_RESPMEM_MUST_FREE);
165   add_cors_headers (response);
166   if ((NULL == daemon_handle_v4) && (NULL == daemon_handle_v6))
167   {
168     MHD_destroy_response (response);
169     response = NULL;
170   }
171   GNUNET_STATISTICS_set (stats,
172                          gettext_noop ("bytes in hostlist"),
173                          builder->size,
174                          GNUNET_YES);
175   GNUNET_free (builder);
176   builder = NULL;
177 }
178
179
180 /**
181  * Set @a cls to #GNUNET_YES (we have an address!).
182  *
183  * @param cls closure, an `int *`
184  * @param address the address (ignored)
185  * @param expiration expiration time (call is ignored if this is in the past)
186  * @return  #GNUNET_SYSERR to stop iterating (unless expiration has occured)
187  */
188 static int
189 check_has_addr (void *cls,
190                 const struct GNUNET_HELLO_Address *address,
191                 struct GNUNET_TIME_Absolute expiration)
192 {
193   int *arg = cls;
194
195   if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
196   {
197     GNUNET_STATISTICS_update (stats,
198                               gettext_noop ("expired addresses encountered"),
199                               1,
200                               GNUNET_YES);
201     return GNUNET_YES;   /* ignore this address */
202   }
203   *arg = GNUNET_YES;
204   return GNUNET_SYSERR;
205 }
206
207
208 /**
209  * Callback that processes each of the known HELLOs for the
210  * hostlist response construction.
211  *
212  * @param cls closure, NULL
213  * @param peer id of the peer, NULL for last call
214  * @param hello hello message for the peer (can be NULL)
215  * @param err_msg message
216  */
217 static void
218 host_processor (void *cls,
219                 const struct GNUNET_PeerIdentity *peer,
220                 const struct GNUNET_HELLO_Message *hello,
221                 const char *err_msg)
222 {
223   size_t old;
224   size_t s;
225   int has_addr;
226
227   if (NULL != err_msg)
228   {
229     GNUNET_assert (NULL == peer);
230     builder->pitr = NULL;
231     GNUNET_free_non_null (builder->data);
232     GNUNET_free (builder);
233     builder = NULL;
234     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235                 _ ("Error in communication with PEERINFO service: %s\n"),
236                 err_msg);
237     return;
238   }
239   if (NULL == peer)
240   {
241     builder->pitr = NULL;
242     finish_response ();
243     return;
244   }
245   if (NULL == hello)
246     return;
247   has_addr = GNUNET_NO;
248   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_has_addr, &has_addr);
249   if (GNUNET_NO == has_addr)
250   {
251     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252                 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
253                 GNUNET_i2s (peer));
254     GNUNET_STATISTICS_update (stats,
255                               gettext_noop (
256                                 "HELLOs without addresses encountered (ignored)"),
257                               1,
258                               GNUNET_NO);
259     return;
260   }
261   old = builder->size;
262   s = GNUNET_HELLO_size (hello);
263   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264               "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
265               (unsigned int) s,
266               "HELLO",
267               GNUNET_i2s (peer));
268   if ((old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
269       (old + s >= MAX_BYTES_PER_HOSTLISTS))
270   {
271     /* too large, skip! */
272     GNUNET_STATISTICS_update (stats,
273                               gettext_noop (
274                                 "bytes not included in hostlist (size limit)"),
275                               s,
276                               GNUNET_NO);
277     return;
278   }
279   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280               "Adding peer `%s' to hostlist (%u bytes)\n",
281               GNUNET_i2s (peer),
282               (unsigned int) s);
283   GNUNET_array_grow (builder->data, builder->size, old + s);
284   GNUNET_memcpy (&builder->data[old], hello, s);
285 }
286
287
288 /**
289  * Hostlist access policy (very permissive, allows everything).
290  * Returns #MHD_NO only if we are not yet ready to serve.
291  *
292  * @param cls closure
293  * @param addr address information from the client
294  * @param addrlen length of @a addr
295  * @return #MHD_YES if connection is allowed, #MHD_NO if not (we are not ready)
296  */
297 static int
298 accept_policy_callback (void *cls,
299                         const struct sockaddr *addr,
300                         socklen_t addrlen)
301 {
302   if (NULL == response)
303   {
304     GNUNET_log (
305       GNUNET_ERROR_TYPE_DEBUG,
306       "Received request for hostlist, but I am not yet ready; rejecting!\n");
307     return MHD_NO;
308   }
309   return MHD_YES; /* accept all */
310 }
311
312
313 /**
314  * Main request handler.
315  *
316  * @param cls argument given together with the function
317  *        pointer when the handler was registered with MHD
318  * @param connection
319  * @param url the requested url
320  * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
321  *        #MHD_HTTP_METHOD_PUT, etc.)
322  * @param version the HTTP version string (i.e.
323  *        #MHD_HTTP_VERSION_1_1)
324  * @param upload_data the data being uploaded (excluding HEADERS,
325  *        for a POST that fits into memory and that is encoded
326  *        with a supported encoding, the POST data will NOT be
327  *        given in upload_data and is instead available as
328  *        part of #MHD_get_connection_values; very large POST
329  *        data *will* be made available incrementally in
330  *        @a upload_data)
331  * @param upload_data_size set initially to the size of the
332  *        @a upload_data provided; the method must update this
333  *        value to the number of bytes NOT processed;
334  * @param con_cls pointer that the callback can set to some
335  *        address and that will be preserved by MHD for future
336  *        calls for this request; since the access handler may
337  *        be called many times (i.e., for a PUT/POST operation
338  *        with plenty of upload data) this allows the application
339  *        to easily associate some request-specific state.
340  *        If necessary, this state can be cleaned up in the
341  *        global #MHD_RequestCompletedCallback (which
342  *        can be set with the #MHD_OPTION_NOTIFY_COMPLETED).
343  *        Initially, `*con_cls` will be NULL.
344  * @return #MHD_YES if the connection was handled successfully,
345  *         #MHD_NO if the socket must be closed due to a serios
346  *         error while handling the request
347  */
348 static int
349 access_handler_callback (void *cls,
350                          struct MHD_Connection *connection,
351                          const char *url,
352                          const char *method,
353                          const char *version,
354                          const char *upload_data,
355                          size_t *upload_data_size,
356                          void **con_cls)
357 {
358   static int dummy;
359
360   /* CORS pre-flight request */
361   if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
362   {
363     struct MHD_Response *options_response;
364     int rc;
365
366     options_response =
367       MHD_create_response_from_buffer (0, NULL, MHD_RESPMEM_PERSISTENT);
368     add_cors_headers (options_response);
369     rc = MHD_queue_response (connection, MHD_HTTP_OK, options_response);
370     MHD_destroy_response (options_response);
371     return rc;
372   }
373   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
374   {
375     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
376                 _ ("Refusing `%s' request to hostlist server\n"),
377                 method);
378     GNUNET_STATISTICS_update (stats,
379                               gettext_noop (
380                                 "hostlist requests refused (not HTTP GET)"),
381                               1,
382                               GNUNET_YES);
383     return MHD_NO;
384   }
385   if (NULL == *con_cls)
386   {
387     (*con_cls) = &dummy;
388     return MHD_YES;
389   }
390   if (0 != *upload_data_size)
391   {
392     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
393                 _ ("Refusing `%s' request with %llu bytes of upload data\n"),
394                 method,
395                 (unsigned long long) *upload_data_size);
396     GNUNET_STATISTICS_update (stats,
397                               gettext_noop (
398                                 "hostlist requests refused (upload data)"),
399                               1,
400                               GNUNET_YES);
401     return MHD_NO;   /* do not support upload data */
402   }
403   if (NULL == response)
404   {
405     GNUNET_log (
406       GNUNET_ERROR_TYPE_WARNING,
407       _ (
408         "Could not handle hostlist request since I do not have a response yet\n"));
409     GNUNET_STATISTICS_update (stats,
410                               gettext_noop (
411                                 "hostlist requests refused (not ready)"),
412                               1,
413                               GNUNET_YES);
414     return MHD_NO;   /* internal error, no response yet */
415   }
416   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
417               _ ("Received request for our hostlist\n"));
418   GNUNET_STATISTICS_update (stats,
419                             gettext_noop ("hostlist requests processed"),
420                             1,
421                             GNUNET_YES);
422   return MHD_queue_response (connection, MHD_HTTP_OK, response);
423 }
424
425
426 /**
427  * Handler called by CORE when CORE is ready to transmit message
428  *
429  * @param cls closure with the `const struct GNUNET_PeerIdentity *` of
430  *            the peer we are sending to
431  * @param size size of buffer to copy message to
432  * @param buf buffer to copy message to
433  * @return number of bytes copied to @a buf
434  */
435 static void
436 adv_transmit (struct GNUNET_MQ_Handle *mq)
437 {
438   static uint64_t hostlist_adv_count;
439   size_t uri_size; /* Including \0 termination! */
440   struct GNUNET_MessageHeader *header;
441   struct GNUNET_MQ_Envelope *env;
442
443   uri_size = strlen (hostlist_uri) + 1;
444   env = GNUNET_MQ_msg_extra (header,
445                              uri_size,
446                              GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
447   GNUNET_memcpy (&header[1], hostlist_uri, uri_size);
448   GNUNET_MQ_env_set_options (env,
449                              GNUNET_MQ_PREF_CORK_ALLOWED
450                              | GNUNET_MQ_PREF_UNRELIABLE);
451   GNUNET_MQ_send (mq, env);
452   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453               "Sent advertisement message: Copied %u bytes into buffer!\n",
454               (unsigned int) uri_size);
455   hostlist_adv_count++;
456   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457               " # Sent advertisement message: %llu\n",
458               (unsigned long long) hostlist_adv_count);
459   GNUNET_STATISTICS_update (stats,
460                             gettext_noop ("# hostlist advertisements send"),
461                             1,
462                             GNUNET_NO);
463 }
464
465
466 /**
467  * Method called whenever a given peer connects.
468  *
469  * @param cls closure
470  * @param peer peer identity this notification is about
471  * @param mq queue for transmission to @a peer
472  * @return NULL (must!)
473  */
474 static void *
475 connect_handler (void *cls,
476                  const struct GNUNET_PeerIdentity *peer,
477                  struct GNUNET_MQ_Handle *mq)
478 {
479   size_t size;
480
481   if (! advertising)
482     return NULL;
483   if (NULL == hostlist_uri)
484     return NULL;
485   size = strlen (hostlist_uri) + 1;
486   if (size + sizeof(struct GNUNET_MessageHeader) >= GNUNET_MAX_MESSAGE_SIZE)
487   {
488     GNUNET_break (0);
489     return NULL;
490   }
491   size += sizeof(struct GNUNET_MessageHeader);
492   if (NULL == core)
493   {
494     GNUNET_break (0);
495     return NULL;
496   }
497   GNUNET_log (
498     GNUNET_ERROR_TYPE_DEBUG,
499     "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n",
500     (unsigned int) size,
501     GNUNET_i2s (peer));
502   adv_transmit (mq);
503   return NULL;
504 }
505
506
507 /**
508  * PEERINFO calls this function to let us know about a possible peer
509  * that we might want to connect to.
510  *
511  * @param cls closure (not used)
512  * @param peer potential peer to connect to
513  * @param hello HELLO for this peer (or NULL)
514  * @param err_msg NULL if successful, otherwise contains error message
515  */
516 static void
517 process_notify (void *cls,
518                 const struct GNUNET_PeerIdentity *peer,
519                 const struct GNUNET_HELLO_Message *hello,
520                 const char *err_msg)
521 {
522   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523               "Peerinfo is notifying us to rebuild our hostlist\n");
524   if (NULL != err_msg)
525     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
526                 _ ("Error in communication with PEERINFO service: %s\n"),
527                 err_msg);
528   if (NULL != builder)
529   {
530     /* restart re-build already in progress ... */
531     if (NULL != builder->pitr)
532     {
533       GNUNET_PEERINFO_iterate_cancel (builder->pitr);
534       builder->pitr = NULL;
535     }
536     GNUNET_free_non_null (builder->data);
537     builder->size = 0;
538     builder->data = NULL;
539   }
540   else
541   {
542     builder = GNUNET_new (struct HostSet);
543   }
544   GNUNET_assert (NULL != peerinfo);
545   builder->pitr =
546     GNUNET_PEERINFO_iterate (peerinfo, GNUNET_NO, NULL, &host_processor, NULL);
547 }
548
549
550 /**
551  * Function that queries MHD's select sets and
552  * starts the task waiting for them.
553  */
554 static struct GNUNET_SCHEDULER_Task *
555 prepare_daemon (struct MHD_Daemon *daemon_handle);
556
557
558 /**
559  * Call MHD to process pending requests and then go back
560  * and schedule the next run.
561  *
562  * @param cls the `struct MHD_Daemon` of the HTTP server to run
563  */
564 static void
565 run_daemon (void *cls)
566 {
567   struct MHD_Daemon *daemon_handle = cls;
568
569   if (daemon_handle == daemon_handle_v4)
570     hostlist_task_v4 = NULL;
571   else
572     hostlist_task_v6 = NULL;
573   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
574   if (daemon_handle == daemon_handle_v4)
575     hostlist_task_v4 = prepare_daemon (daemon_handle);
576   else
577     hostlist_task_v6 = prepare_daemon (daemon_handle);
578 }
579
580
581 /**
582  * Function that queries MHD's select sets and
583  * starts the task waiting for them.
584  *
585  * @param daemon_handle HTTP server to prepare to run
586  */
587 static struct GNUNET_SCHEDULER_Task *
588 prepare_daemon (struct MHD_Daemon *daemon_handle)
589 {
590   struct GNUNET_SCHEDULER_Task *ret;
591   fd_set rs;
592   fd_set ws;
593   fd_set es;
594   struct GNUNET_NETWORK_FDSet *wrs;
595   struct GNUNET_NETWORK_FDSet *wws;
596   int max;
597   MHD_UNSIGNED_LONG_LONG timeout;
598   int haveto;
599   struct GNUNET_TIME_Relative tv;
600
601   FD_ZERO (&rs);
602   FD_ZERO (&ws);
603   FD_ZERO (&es);
604   wrs = GNUNET_NETWORK_fdset_create ();
605   wws = GNUNET_NETWORK_fdset_create ();
606   max = -1;
607   GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
608   haveto = MHD_get_timeout (daemon_handle, &timeout);
609   if (haveto == MHD_YES)
610     tv.rel_value_us = (uint64_t) timeout * 1000LL;
611   else
612     tv = GNUNET_TIME_UNIT_FOREVER_REL;
613   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
614   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
615   ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
616                                      tv,
617                                      wrs,
618                                      wws,
619                                      &run_daemon,
620                                      daemon_handle);
621   GNUNET_NETWORK_fdset_destroy (wrs);
622   GNUNET_NETWORK_fdset_destroy (wws);
623   return ret;
624 }
625
626
627 /**
628  * Start server offering our hostlist.
629  *
630  * @param c configuration to use
631  * @param st statistics handle to use
632  * @param co core handle to use
633  * @param[out] server_ch set to handler for CORE connect events
634  * @param advertise #GNUNET_YES if we should advertise our hostlist
635  * @return #GNUNET_OK on success
636  */
637 int
638 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
639                               struct GNUNET_STATISTICS_Handle *st,
640                               struct GNUNET_CORE_Handle *co,
641                               GNUNET_CORE_ConnectEventHandler *server_ch,
642                               int advertise)
643 {
644   unsigned long long port;
645   char *hostname;
646   char *ipv4;
647   char *ipv6;
648   size_t size;
649   struct in_addr i4;
650   struct in6_addr i6;
651   struct sockaddr_in v4;
652   struct sockaddr_in6 v6;
653   const struct sockaddr *sa4;
654   const struct sockaddr *sa6;
655
656   advertising = advertise;
657   if (! advertising)
658   {
659     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660                 "Advertising not enabled on this hostlist server\n");
661   }
662   else
663   {
664     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665                 "Advertising enabled on this hostlist server\n");
666   }
667   cfg = c;
668   stats = st;
669   peerinfo = GNUNET_PEERINFO_connect (cfg);
670   if (NULL == peerinfo)
671   {
672     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673                 _ ("Could not access PEERINFO service.  Exiting.\n"));
674     return GNUNET_SYSERR;
675   }
676   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
677                                                           "HOSTLIST",
678                                                           "HTTPPORT",
679                                                           &port))
680     return GNUNET_SYSERR;
681   if ((0 == port) || (port > UINT16_MAX))
682   {
683     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684                 _ ("Invalid port number %llu.  Exiting.\n"),
685                 port);
686     return GNUNET_SYSERR;
687   }
688
689   if (GNUNET_SYSERR ==
690       GNUNET_CONFIGURATION_get_value_string (cfg,
691                                              "HOSTLIST",
692                                              "EXTERNAL_DNS_NAME",
693                                              &hostname))
694     hostname = GNUNET_RESOLVER_local_fqdn_get ();
695   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
696               _ ("Hostlist service starts on %s:%llu\n"),
697               hostname,
698               port);
699   if (NULL != hostname)
700   {
701     size = strlen (hostname);
702     if (size + 15 > MAX_URL_LEN)
703     {
704       GNUNET_break (0);
705     }
706     else
707     {
708       GNUNET_asprintf (&hostlist_uri,
709                        "http://%s:%u/",
710                        hostname,
711                        (unsigned int) port);
712       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
713                   _ ("Address to obtain hostlist: `%s'\n"),
714                   hostlist_uri);
715     }
716     GNUNET_free (hostname);
717   }
718
719   if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV4"))
720   {
721     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
722                                                             "HOSTLIST",
723                                                             "BINDTOIP",
724                                                             &ipv4))
725     {
726       GNUNET_log (
727         GNUNET_ERROR_TYPE_WARNING,
728         _ ("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV4.\n"));
729     }
730   }
731   else
732     ipv4 = NULL;
733   if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV6"))
734   {
735     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
736                                                             "HOSTLIST",
737                                                             "BINDTOIP",
738                                                             &ipv6))
739     {
740       GNUNET_log (
741         GNUNET_ERROR_TYPE_WARNING,
742         _ ("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n"));
743     }
744   }
745   else
746     ipv6 = NULL;
747   sa4 = NULL;
748   if (NULL != ipv4)
749   {
750     if (1 == inet_pton (AF_INET, ipv4, &i4))
751     {
752       memset (&v4, 0, sizeof(v4));
753       v4.sin_family = AF_INET;
754       v4.sin_addr = i4;
755       v4.sin_port = htons (port);
756 #if HAVE_SOCKADDR_IN_SIN_LEN
757       v4.sin_len = sizeof(v4);
758 #endif
759       sa4 = (const struct sockaddr *) &v4;
760     }
761     else
762       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
763                   _ (
764                     "`%s' is not a valid IPv4 address! Ignoring BINDTOIPV4.\n"),
765                   ipv4);
766     GNUNET_free (ipv4);
767   }
768   sa6 = NULL;
769   if (NULL != ipv6)
770   {
771     if (1 == inet_pton (AF_INET6, ipv6, &i6))
772     {
773       memset (&v6, 0, sizeof(v6));
774       v6.sin6_family = AF_INET6;
775       v6.sin6_addr = i6;
776       v6.sin6_port = htons (port);
777 #if HAVE_SOCKADDR_IN_SIN_LEN
778       v6.sin6_len = sizeof(v6);
779 #endif
780       sa6 = (const struct sockaddr *) &v6;
781     }
782     else
783       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
784                   _ (
785                     "`%s' is not a valid IPv6 address! Ignoring BINDTOIPV6.\n"),
786                   ipv6);
787     GNUNET_free (ipv6);
788   }
789
790   daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 | MHD_USE_DEBUG,
791                                        (uint16_t) port,
792                                        &accept_policy_callback,
793                                        NULL,
794                                        &access_handler_callback,
795                                        NULL,
796                                        MHD_OPTION_CONNECTION_LIMIT,
797                                        (unsigned int) 128,
798                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT,
799                                        (unsigned int) 32,
800                                        MHD_OPTION_CONNECTION_TIMEOUT,
801                                        (unsigned int) 16,
802                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
803                                        (size_t) (16 * 1024),
804                                        MHD_OPTION_SOCK_ADDR,
805                                        sa6,
806                                        MHD_OPTION_END);
807   daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG | MHD_USE_DEBUG,
808                                        (uint16_t) port,
809                                        &accept_policy_callback,
810                                        NULL,
811                                        &access_handler_callback,
812                                        NULL,
813                                        MHD_OPTION_CONNECTION_LIMIT,
814                                        (unsigned int) 128,
815                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT,
816                                        (unsigned int) 32,
817                                        MHD_OPTION_CONNECTION_TIMEOUT,
818                                        (unsigned int) 16,
819                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
820                                        (size_t) (16 * 1024),
821                                        MHD_OPTION_SOCK_ADDR,
822                                        sa4,
823                                        MHD_OPTION_END);
824
825   if ((NULL == daemon_handle_v6) && (NULL == daemon_handle_v4))
826   {
827     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
828                 _ ("Could not start hostlist HTTP server on port %u\n"),
829                 (unsigned short) port);
830     return GNUNET_SYSERR;
831   }
832
833   core = co;
834   *server_ch = &connect_handler;
835   if (NULL != daemon_handle_v4)
836     hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
837   if (NULL != daemon_handle_v6)
838     hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
839   notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_notify, NULL);
840   return GNUNET_OK;
841 }
842
843
844 /**
845  * Stop server offering our hostlist.
846  */
847 void
848 GNUNET_HOSTLIST_server_stop ()
849 {
850   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist server shutdown\n");
851   if (NULL != hostlist_task_v6)
852   {
853     GNUNET_SCHEDULER_cancel (hostlist_task_v6);
854     hostlist_task_v6 = NULL;
855   }
856   if (NULL != hostlist_task_v4)
857   {
858     GNUNET_SCHEDULER_cancel (hostlist_task_v4);
859     hostlist_task_v4 = NULL;
860   }
861   if (NULL != daemon_handle_v4)
862   {
863     MHD_stop_daemon (daemon_handle_v4);
864     daemon_handle_v4 = NULL;
865   }
866   if (NULL != daemon_handle_v6)
867   {
868     MHD_stop_daemon (daemon_handle_v6);
869     daemon_handle_v6 = NULL;
870   }
871   if (NULL != response)
872   {
873     MHD_destroy_response (response);
874     response = NULL;
875   }
876   if (NULL != notify)
877   {
878     GNUNET_PEERINFO_notify_cancel (notify);
879     notify = NULL;
880   }
881   if (NULL != builder)
882   {
883     if (NULL != builder->pitr)
884     {
885       GNUNET_PEERINFO_iterate_cancel (builder->pitr);
886       builder->pitr = NULL;
887     }
888     GNUNET_free_non_null (builder->data);
889     GNUNET_free (builder);
890     builder = NULL;
891   }
892   if (NULL != peerinfo)
893   {
894     GNUNET_PEERINFO_disconnect (peerinfo);
895     peerinfo = NULL;
896   }
897   cfg = NULL;
898   stats = NULL;
899   core = NULL;
900 }
901
902
903 /* end of gnunet-daemon-hostlist_server.c */