2 This file is part of GNUnet.
3 (C) 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file hostlist/hostlist-server.c
23 * @author Christian Grothoff
24 * @brief application to provide an integrated hostlist HTTP server
28 #include <microhttpd.h>
29 #include "hostlist-server.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet-daemon-hostlist.h"
33 #include "gnunet_resolver_service.h"
35 #define DEBUG_HOSTLIST_SERVER GNUNET_NO
38 * How often should we recalculate our response to hostlist requests?
40 #define RESPONSE_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
43 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
45 static struct MHD_Daemon *daemon_handle_v6;
48 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
50 static struct MHD_Daemon *daemon_handle_v4;
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
60 static struct GNUNET_SCHEDULER_Handle *sched;
63 * For keeping statistics.
65 static struct GNUNET_STATISTICS_Handle *stats;
68 * Our primary task for IPv4.
70 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
73 * Our primary task for IPv6.
75 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
78 * Task that updates our HTTP response.
80 static GNUNET_SCHEDULER_TaskIdentifier response_task;
83 * Our canonical response.
85 static struct MHD_Response *response;
88 * NULL if we are not currenlty iterating over peer information.
90 static struct GNUNET_PEERINFO_IteratorContext *pitr;
93 * Context for host processor.
103 * Task that will produce a new response object.
106 update_response (void *cls,
107 const struct GNUNET_SCHEDULER_TaskContext *tc);
110 * Function that assembles our hostlist adv message.
113 create_hostlist_adv_message (void)
116 unsigned long long port;
119 char hostname[HOST_NAME_MAX];
120 char *protocol = "http://";
121 char *port_s = malloc(6 * sizeof(char));
123 if (0 != gethostname (hostname, sizeof (hostname) - 1))
125 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
126 "Could not get system's hostname, unable to create advertisement message");
129 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
133 return GNUNET_SYSERR;
135 sprintf(port_s, "%llu", port);
136 length = strlen(hostname)+strlen(protocol)+strlen(port_s)+2;
138 uri = malloc(length * sizeof(char));
139 uri = strcpy(uri, protocol);
140 uri = strcat(uri, hostname);
141 uri = strcat(uri, ":");
142 uri = strcat(uri, port_s);
143 uri = strcat(uri, "/");
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Address to obtain hostlist: %s\n", uri);
151 * Function that assembles our response.
154 finish_response (struct HostSet *results)
156 struct GNUNET_TIME_Relative freq;
158 if (response != NULL)
159 MHD_destroy_response (response);
160 #if DEBUG_HOSTLIST_SERVER
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "Creating hostlist response with %u bytes\n",
163 (unsigned int) results->size);
165 response = MHD_create_response_from_data (results->size,
166 results->data, MHD_YES, MHD_NO);
167 if ( (daemon_handle_v4 != NULL) ||
168 (daemon_handle_v6 != NULL) )
170 freq = RESPONSE_UPDATE_FREQUENCY;
171 if (results->size == 0)
172 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
173 /* schedule next update of the response */
174 response_task = GNUNET_SCHEDULER_add_delayed (sched,
181 /* already past shutdown */
182 MHD_destroy_response (response);
185 GNUNET_STATISTICS_set (stats,
186 gettext_noop("bytes in hostlist"),
189 GNUNET_free (results);
194 * Set 'cls' to GNUNET_YES (we have an address!).
196 * @param cls closure, an 'int*'
197 * @param tname name of the transport (ignored)
198 * @param expiration expiration time (call is ignored if this is in the past)
199 * @param addr the address (ignored)
200 * @param addrlen length of the address (ignored)
201 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
204 check_has_addr (void *cls,
206 struct GNUNET_TIME_Absolute expiration,
207 const void *addr, size_t addrlen)
211 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
213 GNUNET_STATISTICS_update (stats,
214 gettext_noop("expired addresses encountered"),
217 return GNUNET_YES; /* ignore this address */
220 return GNUNET_SYSERR;
225 * Callback that processes each of the known HELLOs for the
226 * hostlist response construction.
229 host_processor (void *cls,
230 const struct GNUNET_PeerIdentity * peer,
231 const struct GNUNET_HELLO_Message *hello,
234 struct HostSet *results = cls;
242 finish_response (results);
245 has_addr = GNUNET_NO;
246 GNUNET_HELLO_iterate_addresses (hello,
250 if (GNUNET_NO == has_addr)
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
255 GNUNET_STATISTICS_update (stats,
256 gettext_noop("HELLOs without addresses encountered (ignored)"),
262 s = GNUNET_HELLO_size(hello);
263 #if DEBUG_HOSTLIST_SERVER
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
270 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
272 GNUNET_STATISTICS_update (stats,
273 gettext_noop("bytes not included in hostlist (size limit)"),
276 return; /* too large, skip! */
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
279 "Adding peer `%s' to hostlist (%u bytes)\n",
282 GNUNET_array_grow (results->data,
285 memcpy (&results->data[old], hello, s);
290 * Task that will produce a new response object.
293 update_response (void *cls,
294 const struct GNUNET_SCHEDULER_TaskContext *tc)
296 struct HostSet *results;
298 response_task = GNUNET_SCHEDULER_NO_TASK;
299 results = GNUNET_malloc(sizeof(struct HostSet));
300 pitr = GNUNET_PEERINFO_iterate (cfg, sched,
303 GNUNET_TIME_UNIT_MINUTES,
310 * Hostlist access policy (very permissive, allows everything).
313 accept_policy_callback (void *cls,
314 const struct sockaddr *addr, socklen_t addrlen)
316 if (NULL == response)
318 #if DEBUG_HOSTLIST_SERVER
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Received request for hostlist, but I am not yet ready; rejecting!\n");
324 return MHD_YES; /* accept all */
329 * Main request handler.
332 access_handler_callback (void *cls,
333 struct MHD_Connection *connection,
337 const char *upload_data,
338 size_t*upload_data_size, void **con_cls)
342 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
344 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
345 _("Refusing `%s' request to hostlist server\n"),
347 GNUNET_STATISTICS_update (stats,
348 gettext_noop("hostlist requests refused (not HTTP GET)"),
353 if (NULL == *con_cls)
356 #if DEBUG_HOSTLIST_SERVER
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 _("Sending 100 CONTINUE reply\n"));
360 return MHD_YES; /* send 100 continue */
362 if (*upload_data_size != 0)
364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
365 _("Refusing `%s' request with %llu bytes of upload data\n"),
367 (unsigned long long) *upload_data_size);
368 GNUNET_STATISTICS_update (stats,
369 gettext_noop("hostlist requests refused (upload data)"),
372 return MHD_NO; /* do not support upload data */
374 if (response == NULL)
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
377 _("Could not handle hostlist request since I do not have a response yet\n"));
378 GNUNET_STATISTICS_update (stats,
379 gettext_noop("hostlist requests refused (not ready)"),
382 return MHD_NO; /* internal error, no response yet */
384 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
385 _("Received request for our hostlist\n"));
386 GNUNET_STATISTICS_update (stats,
387 gettext_noop("hostlist requests processed"),
390 return MHD_queue_response (connection, MHD_HTTP_OK, response);
394 * Method called whenever a given peer connects.
397 * @param peer peer identity this notification is about
398 * @param latency reported latency of the connection with 'other'
399 * @param distance reported distance (DV) to 'other'
402 connect_handler (void *cls,
404 GNUNET_PeerIdentity * peer,
405 struct GNUNET_TIME_Relative latency,
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "A new peer connected to the server, preparing to send hostlist advertisement\n");
410 /* create a new advertisement message */
411 create_hostlist_adv_message();
417 * Method called whenever a given peer disconnects.
420 * @param peer peer identity this notification is about
423 disconnect_handler (void *cls,
425 GNUNET_PeerIdentity * peer)
432 * Function that queries MHD's select sets and
433 * starts the task waiting for them.
435 static GNUNET_SCHEDULER_TaskIdentifier
436 prepare_daemon (struct MHD_Daemon *daemon_handle);
439 * Call MHD to process pending requests and then go back
440 * and schedule the next run.
443 run_daemon (void *cls,
444 const struct GNUNET_SCHEDULER_TaskContext *tc)
446 struct MHD_Daemon *daemon_handle = cls;
448 if (daemon_handle == daemon_handle_v4)
449 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
451 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
453 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
455 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
456 if (daemon_handle == daemon_handle_v4)
457 hostlist_task_v4 = prepare_daemon (daemon_handle);
459 hostlist_task_v6 = prepare_daemon (daemon_handle);
464 * Function that queries MHD's select sets and
465 * starts the task waiting for them.
467 static GNUNET_SCHEDULER_TaskIdentifier
468 prepare_daemon (struct MHD_Daemon *daemon_handle)
470 GNUNET_SCHEDULER_TaskIdentifier ret;
474 struct GNUNET_NETWORK_FDSet *wrs;
475 struct GNUNET_NETWORK_FDSet *wws;
476 struct GNUNET_NETWORK_FDSet *wes;
478 unsigned long long timeout;
480 struct GNUNET_TIME_Relative tv;
485 wrs = GNUNET_NETWORK_fdset_create ();
486 wes = GNUNET_NETWORK_fdset_create ();
487 wws = GNUNET_NETWORK_fdset_create ();
489 GNUNET_assert (MHD_YES ==
490 MHD_get_fdset (daemon_handle,
495 haveto = MHD_get_timeout (daemon_handle, &timeout);
496 if (haveto == MHD_YES)
497 tv.value = (uint64_t) timeout;
499 tv = GNUNET_TIME_UNIT_FOREVER_REL;
500 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
501 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
502 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
503 ret = GNUNET_SCHEDULER_add_select (sched,
504 GNUNET_SCHEDULER_PRIORITY_HIGH,
505 GNUNET_SCHEDULER_NO_TASK,
511 GNUNET_NETWORK_fdset_destroy (wrs);
512 GNUNET_NETWORK_fdset_destroy (wws);
513 GNUNET_NETWORK_fdset_destroy (wes);
520 * Start server offering our hostlist.
522 * @return GNUNET_OK on success
525 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
526 struct GNUNET_SCHEDULER_Handle *s,
527 struct GNUNET_STATISTICS_Handle *st,
528 GNUNET_CORE_ConnectEventHandler *server_ch,
529 GNUNET_CORE_DisconnectEventHandler *server_dh)
531 unsigned long long port;
536 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
540 return GNUNET_SYSERR;
541 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
542 _("Hostlist service starts on port %llu\n"),
544 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
545 #if DEBUG_HOSTLIST_SERVER
549 (unsigned short) port,
550 &accept_policy_callback,
552 &access_handler_callback,
554 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
555 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
556 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
557 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
559 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
560 #if DEBUG_HOSTLIST_SERVER
564 (unsigned short) port,
565 &accept_policy_callback,
567 &access_handler_callback,
569 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
570 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
571 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
572 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
575 if ( (daemon_handle_v6 == NULL) &&
576 (daemon_handle_v4 == NULL) )
578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
579 _("Could not start hostlist HTTP server on port %u\n"),
580 (unsigned short) port);
581 return GNUNET_SYSERR;
584 *server_ch = &connect_handler;
585 *server_dh = &disconnect_handler;
587 if (daemon_handle_v4 != NULL)
588 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
589 if (daemon_handle_v6 != NULL)
590 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
591 response_task = GNUNET_SCHEDULER_add_now (sched,
598 * Stop server offering our hostlist.
601 GNUNET_HOSTLIST_server_stop ()
603 #if DEBUG_HOSTLIST_SERVER
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Hostlist server shutdown\n");
607 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
609 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
610 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
612 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
614 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
615 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
619 GNUNET_PEERINFO_iterate_cancel (pitr);
622 if (GNUNET_SCHEDULER_NO_TASK != response_task)
624 GNUNET_SCHEDULER_cancel (sched, response_task);
625 response_task = GNUNET_SCHEDULER_NO_TASK;
627 if (NULL != daemon_handle_v4)
629 MHD_stop_daemon (daemon_handle_v4);
630 daemon_handle_v4 = NULL;
632 if (NULL != daemon_handle_v6)
634 MHD_stop_daemon (daemon_handle_v6);
635 daemon_handle_v6 = NULL;
637 if (response != NULL)
639 MHD_destroy_response (response);
644 /* end of hostlist-server.c */