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