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 (struct GNUNET_HOSTLIST_ADV_Message *adv_msg)
117 unsigned long long port;
120 char hostname[HOST_NAME_MAX];
121 char *protocol = "http://";
122 char *port_s = GNUNET_malloc(6 * sizeof(char));
124 if (0 != gethostname (hostname, sizeof (hostname) - 1))
126 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
127 "Could not get system's hostname, unable to create advertisement message");
130 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
134 return GNUNET_SYSERR;
136 sprintf(port_s, "%llu", port);
137 length = strlen(hostname)+strlen(protocol)+strlen(port_s)+2;
138 size = (length+1) * sizeof (char);
139 uri = GNUNET_malloc(size);
140 uri = strcpy(uri, protocol);
141 uri = strcat(uri, hostname);
142 uri = strcat(uri, ":");
143 uri = strcat(uri, port_s);
144 uri = strcat(uri, "/");
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Address to obtain hostlist: %s\n", uri);
148 adv_msg = GNUNET_malloc ( sizeof(struct GNUNET_HOSTLIST_ADV_Message) + size);
149 if ( NULL == adv_msg)
151 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
152 "Could not allocate memory for the message");
155 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
156 "size ADV_Message: %u\n",sizeof(struct GNUNET_HOSTLIST_ADV_Message));
157 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
158 "size uri: %u\n", (length + 1) * sizeof (char));
161 if ( ( size + sizeof( struct GNUNET_HOSTLIST_ADV_Message )) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
163 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
164 "Advertisement message is bigger than GNUNET allows");
168 adv_msg->header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
169 adv_msg->header.size = htons (sizeof (struct GNUNET_HOSTLIST_ADV_Message) + size);
170 memcpy(&adv_msg[1],uri,size);
176 * Function that assembles our response.
179 finish_response (struct HostSet *results)
181 struct GNUNET_TIME_Relative freq;
183 if (response != NULL)
184 MHD_destroy_response (response);
185 #if DEBUG_HOSTLIST_SERVER
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Creating hostlist response with %u bytes\n",
188 (unsigned int) results->size);
190 response = MHD_create_response_from_data (results->size,
191 results->data, MHD_YES, MHD_NO);
192 if ( (daemon_handle_v4 != NULL) ||
193 (daemon_handle_v6 != NULL) )
195 freq = RESPONSE_UPDATE_FREQUENCY;
196 if (results->size == 0)
197 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
198 /* schedule next update of the response */
199 response_task = GNUNET_SCHEDULER_add_delayed (sched,
206 /* already past shutdown */
207 MHD_destroy_response (response);
210 GNUNET_STATISTICS_set (stats,
211 gettext_noop("bytes in hostlist"),
214 GNUNET_free (results);
219 * Set 'cls' to GNUNET_YES (we have an address!).
221 * @param cls closure, an 'int*'
222 * @param tname name of the transport (ignored)
223 * @param expiration expiration time (call is ignored if this is in the past)
224 * @param addr the address (ignored)
225 * @param addrlen length of the address (ignored)
226 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
229 check_has_addr (void *cls,
231 struct GNUNET_TIME_Absolute expiration,
232 const void *addr, size_t addrlen)
236 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
238 GNUNET_STATISTICS_update (stats,
239 gettext_noop("expired addresses encountered"),
242 return GNUNET_YES; /* ignore this address */
245 return GNUNET_SYSERR;
250 * Callback that processes each of the known HELLOs for the
251 * hostlist response construction.
254 host_processor (void *cls,
255 const struct GNUNET_PeerIdentity * peer,
256 const struct GNUNET_HELLO_Message *hello,
259 struct HostSet *results = cls;
267 finish_response (results);
270 has_addr = GNUNET_NO;
271 GNUNET_HELLO_iterate_addresses (hello,
275 if (GNUNET_NO == has_addr)
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
280 GNUNET_STATISTICS_update (stats,
281 gettext_noop("HELLOs without addresses encountered (ignored)"),
287 s = GNUNET_HELLO_size(hello);
288 #if DEBUG_HOSTLIST_SERVER
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
295 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
297 GNUNET_STATISTICS_update (stats,
298 gettext_noop("bytes not included in hostlist (size limit)"),
301 return; /* too large, skip! */
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304 "Adding peer `%s' to hostlist (%u bytes)\n",
307 GNUNET_array_grow (results->data,
310 memcpy (&results->data[old], hello, s);
315 * Task that will produce a new response object.
318 update_response (void *cls,
319 const struct GNUNET_SCHEDULER_TaskContext *tc)
321 struct HostSet *results;
323 response_task = GNUNET_SCHEDULER_NO_TASK;
324 results = GNUNET_malloc(sizeof(struct HostSet));
325 pitr = GNUNET_PEERINFO_iterate (cfg, sched,
328 GNUNET_TIME_UNIT_MINUTES,
335 * Hostlist access policy (very permissive, allows everything).
338 accept_policy_callback (void *cls,
339 const struct sockaddr *addr, socklen_t addrlen)
341 if (NULL == response)
343 #if DEBUG_HOSTLIST_SERVER
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Received request for hostlist, but I am not yet ready; rejecting!\n");
349 return MHD_YES; /* accept all */
354 * Main request handler.
357 access_handler_callback (void *cls,
358 struct MHD_Connection *connection,
362 const char *upload_data,
363 size_t*upload_data_size, void **con_cls)
367 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
370 _("Refusing `%s' request to hostlist server\n"),
372 GNUNET_STATISTICS_update (stats,
373 gettext_noop("hostlist requests refused (not HTTP GET)"),
378 if (NULL == *con_cls)
381 #if DEBUG_HOSTLIST_SERVER
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 _("Sending 100 CONTINUE reply\n"));
385 return MHD_YES; /* send 100 continue */
387 if (*upload_data_size != 0)
389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390 _("Refusing `%s' request with %llu bytes of upload data\n"),
392 (unsigned long long) *upload_data_size);
393 GNUNET_STATISTICS_update (stats,
394 gettext_noop("hostlist requests refused (upload data)"),
397 return MHD_NO; /* do not support upload data */
399 if (response == NULL)
401 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
402 _("Could not handle hostlist request since I do not have a response yet\n"));
403 GNUNET_STATISTICS_update (stats,
404 gettext_noop("hostlist requests refused (not ready)"),
407 return MHD_NO; /* internal error, no response yet */
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
410 _("Received request for our hostlist\n"));
411 GNUNET_STATISTICS_update (stats,
412 gettext_noop("hostlist requests processed"),
415 return MHD_queue_response (connection, MHD_HTTP_OK, response);
419 * Method called whenever a given peer connects.
422 * @param peer peer identity this notification is about
423 * @param latency reported latency of the connection with 'other'
424 * @param distance reported distance (DV) to 'other'
427 connect_handler (void *cls,
429 GNUNET_PeerIdentity * peer,
430 struct GNUNET_TIME_Relative latency,
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "A new peer connected to the server, preparing to send hostlist advertisement\n");
435 /* create a new advertisement message */
436 struct GNUNET_HOSTLIST_ADV_Message *adv_msg;
437 create_hostlist_adv_message(adv_msg);
443 * Method called whenever a given peer disconnects.
446 * @param peer peer identity this notification is about
449 disconnect_handler (void *cls,
451 GNUNET_PeerIdentity * peer)
458 * Function that queries MHD's select sets and
459 * starts the task waiting for them.
461 static GNUNET_SCHEDULER_TaskIdentifier
462 prepare_daemon (struct MHD_Daemon *daemon_handle);
465 * Call MHD to process pending requests and then go back
466 * and schedule the next run.
469 run_daemon (void *cls,
470 const struct GNUNET_SCHEDULER_TaskContext *tc)
472 struct MHD_Daemon *daemon_handle = cls;
474 if (daemon_handle == daemon_handle_v4)
475 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
477 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
479 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
481 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
482 if (daemon_handle == daemon_handle_v4)
483 hostlist_task_v4 = prepare_daemon (daemon_handle);
485 hostlist_task_v6 = prepare_daemon (daemon_handle);
490 * Function that queries MHD's select sets and
491 * starts the task waiting for them.
493 static GNUNET_SCHEDULER_TaskIdentifier
494 prepare_daemon (struct MHD_Daemon *daemon_handle)
496 GNUNET_SCHEDULER_TaskIdentifier ret;
500 struct GNUNET_NETWORK_FDSet *wrs;
501 struct GNUNET_NETWORK_FDSet *wws;
502 struct GNUNET_NETWORK_FDSet *wes;
504 unsigned long long timeout;
506 struct GNUNET_TIME_Relative tv;
511 wrs = GNUNET_NETWORK_fdset_create ();
512 wes = GNUNET_NETWORK_fdset_create ();
513 wws = GNUNET_NETWORK_fdset_create ();
515 GNUNET_assert (MHD_YES ==
516 MHD_get_fdset (daemon_handle,
521 haveto = MHD_get_timeout (daemon_handle, &timeout);
522 if (haveto == MHD_YES)
523 tv.value = (uint64_t) timeout;
525 tv = GNUNET_TIME_UNIT_FOREVER_REL;
526 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
527 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
528 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
529 ret = GNUNET_SCHEDULER_add_select (sched,
530 GNUNET_SCHEDULER_PRIORITY_HIGH,
531 GNUNET_SCHEDULER_NO_TASK,
537 GNUNET_NETWORK_fdset_destroy (wrs);
538 GNUNET_NETWORK_fdset_destroy (wws);
539 GNUNET_NETWORK_fdset_destroy (wes);
546 * Start server offering our hostlist.
548 * @return GNUNET_OK on success
551 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
552 struct GNUNET_SCHEDULER_Handle *s,
553 struct GNUNET_STATISTICS_Handle *st,
554 GNUNET_CORE_ConnectEventHandler *server_ch,
555 GNUNET_CORE_DisconnectEventHandler *server_dh)
557 unsigned long long port;
562 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
566 return GNUNET_SYSERR;
567 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
568 _("Hostlist service starts on port %llu\n"),
570 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
571 #if DEBUG_HOSTLIST_SERVER
575 (unsigned short) port,
576 &accept_policy_callback,
578 &access_handler_callback,
580 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
581 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
582 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
583 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
585 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
586 #if DEBUG_HOSTLIST_SERVER
590 (unsigned short) port,
591 &accept_policy_callback,
593 &access_handler_callback,
595 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
596 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
597 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
598 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
601 if ( (daemon_handle_v6 == NULL) &&
602 (daemon_handle_v4 == NULL) )
604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
605 _("Could not start hostlist HTTP server on port %u\n"),
606 (unsigned short) port);
607 return GNUNET_SYSERR;
610 *server_ch = &connect_handler;
611 *server_dh = &disconnect_handler;
613 if (daemon_handle_v4 != NULL)
614 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
615 if (daemon_handle_v6 != NULL)
616 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
617 response_task = GNUNET_SCHEDULER_add_now (sched,
624 * Stop server offering our hostlist.
627 GNUNET_HOSTLIST_server_stop ()
629 #if DEBUG_HOSTLIST_SERVER
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Hostlist server shutdown\n");
633 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
635 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
636 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
638 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
640 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
641 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
645 GNUNET_PEERINFO_iterate_cancel (pitr);
648 if (GNUNET_SCHEDULER_NO_TASK != response_task)
650 GNUNET_SCHEDULER_cancel (sched, response_task);
651 response_task = GNUNET_SCHEDULER_NO_TASK;
653 if (NULL != daemon_handle_v4)
655 MHD_stop_daemon (daemon_handle_v4);
656 daemon_handle_v4 = NULL;
658 if (NULL != daemon_handle_v6)
660 MHD_stop_daemon (daemon_handle_v6);
661 daemon_handle_v6 = NULL;
663 if (response != NULL)
665 MHD_destroy_response (response);
670 /* end of hostlist-server.c */