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