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 * Handle to the core service (NULL until we've connected to it).
70 struct GNUNET_CORE_Handle *core;
73 * Our primary task for IPv4.
75 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
78 * Our primary task for IPv6.
80 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
83 * Task that updates our HTTP response.
85 static GNUNET_SCHEDULER_TaskIdentifier response_task;
88 * Our canonical response.
90 static struct MHD_Response *response;
93 * NULL if we are not currenlty iterating over peer information.
95 static struct GNUNET_PEERINFO_IteratorContext *pitr;
98 * Context for host processor.
108 * Task that will produce a new response object.
111 update_response (void *cls,
112 const struct GNUNET_SCHEDULER_TaskContext *tc);
115 * Function that assembles our response.
118 finish_response (struct HostSet *results)
120 struct GNUNET_TIME_Relative freq;
122 if (response != NULL)
123 MHD_destroy_response (response);
124 #if DEBUG_HOSTLIST_SERVER
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "Creating hostlist response with %u bytes\n",
127 (unsigned int) results->size);
129 response = MHD_create_response_from_data (results->size,
130 results->data, MHD_YES, MHD_NO);
131 if ( (daemon_handle_v4 != NULL) ||
132 (daemon_handle_v6 != NULL) )
134 freq = RESPONSE_UPDATE_FREQUENCY;
135 if (results->size == 0)
136 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
137 /* schedule next update of the response */
138 response_task = GNUNET_SCHEDULER_add_delayed (sched,
145 /* already past shutdown */
146 MHD_destroy_response (response);
149 GNUNET_STATISTICS_set (stats,
150 gettext_noop("bytes in hostlist"),
153 GNUNET_free (results);
158 * Set 'cls' to GNUNET_YES (we have an address!).
160 * @param cls closure, an 'int*'
161 * @param tname name of the transport (ignored)
162 * @param expiration expiration time (call is ignored if this is in the past)
163 * @param addr the address (ignored)
164 * @param addrlen length of the address (ignored)
165 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
168 check_has_addr (void *cls,
170 struct GNUNET_TIME_Absolute expiration,
171 const void *addr, size_t addrlen)
175 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
177 GNUNET_STATISTICS_update (stats,
178 gettext_noop("expired addresses encountered"),
181 return GNUNET_YES; /* ignore this address */
184 return GNUNET_SYSERR;
189 * Callback that processes each of the known HELLOs for the
190 * hostlist response construction.
193 host_processor (void *cls,
194 const struct GNUNET_PeerIdentity * peer,
195 const struct GNUNET_HELLO_Message *hello,
198 struct HostSet *results = cls;
206 finish_response (results);
211 has_addr = GNUNET_NO;
212 GNUNET_HELLO_iterate_addresses (hello,
216 if (GNUNET_NO == has_addr)
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
221 GNUNET_STATISTICS_update (stats,
222 gettext_noop("HELLOs without addresses encountered (ignored)"),
228 s = GNUNET_HELLO_size(hello);
229 #if DEBUG_HOSTLIST_SERVER
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
236 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
238 GNUNET_STATISTICS_update (stats,
239 gettext_noop("bytes not included in hostlist (size limit)"),
242 return; /* too large, skip! */
244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
245 "Adding peer `%s' to hostlist (%u bytes)\n",
248 GNUNET_array_grow (results->data,
251 memcpy (&results->data[old], hello, s);
256 * Task that will produce a new response object.
259 update_response (void *cls,
260 const struct GNUNET_SCHEDULER_TaskContext *tc)
262 struct HostSet *results;
264 response_task = GNUNET_SCHEDULER_NO_TASK;
265 results = GNUNET_malloc(sizeof(struct HostSet));
266 pitr = GNUNET_PEERINFO_iterate (cfg, sched,
269 GNUNET_TIME_UNIT_MINUTES,
276 * Hostlist access policy (very permissive, allows everything).
279 accept_policy_callback (void *cls,
280 const struct sockaddr *addr, socklen_t addrlen)
282 if (NULL == response)
284 #if DEBUG_HOSTLIST_SERVER
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286 "Received request for hostlist, but I am not yet ready; rejecting!\n");
290 return MHD_YES; /* accept all */
295 * Main request handler.
298 access_handler_callback (void *cls,
299 struct MHD_Connection *connection,
303 const char *upload_data,
304 size_t*upload_data_size, void **con_cls)
308 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
310 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311 _("Refusing `%s' request to hostlist server\n"),
313 GNUNET_STATISTICS_update (stats,
314 gettext_noop("hostlist requests refused (not HTTP GET)"),
319 if (NULL == *con_cls)
322 #if DEBUG_HOSTLIST_SERVER
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 _("Sending 100 CONTINUE reply\n"));
326 return MHD_YES; /* send 100 continue */
328 if (*upload_data_size != 0)
330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
331 _("Refusing `%s' request with %llu bytes of upload data\n"),
333 (unsigned long long) *upload_data_size);
334 GNUNET_STATISTICS_update (stats,
335 gettext_noop("hostlist requests refused (upload data)"),
338 return MHD_NO; /* do not support upload data */
340 if (response == NULL)
342 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
343 _("Could not handle hostlist request since I do not have a response yet\n"));
344 GNUNET_STATISTICS_update (stats,
345 gettext_noop("hostlist requests refused (not ready)"),
348 return MHD_NO; /* internal error, no response yet */
350 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
351 _("Received request for our hostlist\n"));
352 GNUNET_STATISTICS_update (stats,
353 gettext_noop("hostlist requests processed"),
356 return MHD_queue_response (connection, MHD_HTTP_OK, response);
360 * Buffer for the hostlist address
362 char hostlist_uri[255];
365 * Handler called by core when core is ready to transmit message
367 * @param size size of buffer to copy message to
368 * @param buf buffer to copy message to
371 adv_transmit_ready ( void *cls, size_t size, void *buf)
373 size_t transmission_size;
374 size_t uri_size; /* Including \0 termination! */
375 uri_size = strlen ( hostlist_uri ) + 1;
377 struct GNUNET_HOSTLIST_ADV_Message * adv_message;
378 adv_message = GNUNET_malloc ( sizeof(struct GNUNET_HOSTLIST_ADV_Message) + uri_size);
379 if ( NULL == adv_message)
381 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
382 "Could not allocate memory for the message");
385 transmission_size = sizeof (struct GNUNET_HOSTLIST_ADV_Message) + uri_size;
387 adv_message->header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
388 adv_message->header.size = htons (transmission_size);
389 memcpy(&adv_message[1],hostlist_uri,uri_size);
393 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG, "Transmission failed, buffer invalid!\n" );
397 if ( size >= transmission_size )
399 memcpy ( buf, adv_message, transmission_size );
400 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG, "Sent advertisement message: Copied %d bytes into buffer!\n", transmission_size);
401 GNUNET_free ( adv_message );
402 return transmission_size;
405 GNUNET_free (adv_message );
410 * Method that asks core service to transmit the message to the peer
411 * @param peer peer to transmit message to
412 * @param size size of the message
415 adv_transmit_message ( const struct GNUNET_PeerIdentity * peer, size_t size )
417 /* transmit message to peer */
420 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
421 _("Not connected to core, unable to send advertisement message\n"));
425 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, GNUNET_ADV_TIMEOUT);
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 _("Asked core to transmit advertisement message with a size of %u bytes\n"), size);
428 struct GNUNET_CORE_TransmitHandle * th;
429 th = GNUNET_CORE_notify_transmit_ready (core,
434 &adv_transmit_ready, NULL);
436 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
437 _("Advertisement message could not be queued by core\n"));
444 * Method that assembles our hostlist advertisement message
445 * @param peer peer to send the hostlist advertisement
448 adv_create_message ( const struct GNUNET_PeerIdentity * peer )
453 unsigned long long port;
456 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
457 char *protocol = "http://";
458 char *port_s = GNUNET_malloc(6 * sizeof(char));
460 if (0 != gethostname (hostname, sizeof (hostname) - 1))
462 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
463 "Could not get system's hostname, unable to create advertisement message");
466 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
470 return GNUNET_SYSERR;
472 sprintf(port_s, "%llu", port);
473 length = strlen(hostname)+strlen(protocol)+strlen(port_s)+2;
474 size = (length+1) * sizeof (char);
475 uri = GNUNET_malloc(size);
476 uri = strcpy(uri, protocol);
477 uri = strcat(uri, hostname);
478 uri = strcat(uri, ":");
479 uri = strcat(uri, port_s);
480 uri = strcat(uri, "/");
481 strcpy(hostlist_uri,uri);
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Address to obtain hostlist: %s\n", hostlist_uri);
484 if ( ( size + sizeof( struct GNUNET_HOSTLIST_ADV_Message )) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
486 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
487 "Advertisement message is bigger than GNUNET allows");
491 /* Request core to transmit message to peer */
492 size = size + sizeof ( struct GNUNET_HOSTLIST_ADV_Message );
493 adv_transmit_message(peer, size);
495 GNUNET_free ( port_s );
502 * Method called whenever a given peer connects.
505 * @param peer peer identity this notification is about
506 * @param latency reported latency of the connection with 'other'
507 * @param distance reported distance (DV) to 'other'
510 connect_handler (void *cls,
512 GNUNET_PeerIdentity * peer,
513 struct GNUNET_TIME_Relative latency,
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "A new peer connected to the server, preparing to send hostlist advertisement\n");
518 /* create a new advertisement message */
519 if ( (GNUNET_OK != adv_create_message(peer)))
521 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
522 _(" GNUNET_OK Could not create a hostlist advertisement message, impossible to advertise hostlist\n"));
529 * Method called whenever a given peer disconnects.
532 * @param peer peer identity this notification is about
535 disconnect_handler (void *cls,
537 GNUNET_PeerIdentity * peer)
544 * Function that queries MHD's select sets and
545 * starts the task waiting for them.
547 static GNUNET_SCHEDULER_TaskIdentifier
548 prepare_daemon (struct MHD_Daemon *daemon_handle);
551 * Call MHD to process pending requests and then go back
552 * and schedule the next run.
555 run_daemon (void *cls,
556 const struct GNUNET_SCHEDULER_TaskContext *tc)
558 struct MHD_Daemon *daemon_handle = cls;
560 if (daemon_handle == daemon_handle_v4)
561 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
563 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
565 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
567 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
568 if (daemon_handle == daemon_handle_v4)
569 hostlist_task_v4 = prepare_daemon (daemon_handle);
571 hostlist_task_v6 = prepare_daemon (daemon_handle);
576 * Function that queries MHD's select sets and
577 * starts the task waiting for them.
579 static GNUNET_SCHEDULER_TaskIdentifier
580 prepare_daemon (struct MHD_Daemon *daemon_handle)
582 GNUNET_SCHEDULER_TaskIdentifier ret;
586 struct GNUNET_NETWORK_FDSet *wrs;
587 struct GNUNET_NETWORK_FDSet *wws;
588 struct GNUNET_NETWORK_FDSet *wes;
590 unsigned long long timeout;
592 struct GNUNET_TIME_Relative tv;
597 wrs = GNUNET_NETWORK_fdset_create ();
598 wes = GNUNET_NETWORK_fdset_create ();
599 wws = GNUNET_NETWORK_fdset_create ();
601 GNUNET_assert (MHD_YES ==
602 MHD_get_fdset (daemon_handle,
607 haveto = MHD_get_timeout (daemon_handle, &timeout);
608 if (haveto == MHD_YES)
609 tv.value = (uint64_t) timeout;
611 tv = GNUNET_TIME_UNIT_FOREVER_REL;
612 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
613 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
614 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
615 ret = GNUNET_SCHEDULER_add_select (sched,
616 GNUNET_SCHEDULER_PRIORITY_HIGH,
617 GNUNET_SCHEDULER_NO_TASK,
623 GNUNET_NETWORK_fdset_destroy (wrs);
624 GNUNET_NETWORK_fdset_destroy (wws);
625 GNUNET_NETWORK_fdset_destroy (wes);
632 * Start server offering our hostlist.
634 * @return GNUNET_OK on success
637 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
638 struct GNUNET_SCHEDULER_Handle *s,
639 struct GNUNET_STATISTICS_Handle *st,
640 struct GNUNET_CORE_Handle *co,
641 GNUNET_CORE_ConnectEventHandler *server_ch,
642 GNUNET_CORE_DisconnectEventHandler *server_dh)
644 unsigned long long port;
649 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
653 return GNUNET_SYSERR;
654 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
655 _("Hostlist service starts on port %llu\n"),
657 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
658 #if DEBUG_HOSTLIST_SERVER
662 (unsigned short) port,
663 &accept_policy_callback,
665 &access_handler_callback,
667 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
668 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
669 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
670 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
672 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
673 #if DEBUG_HOSTLIST_SERVER
677 (unsigned short) port,
678 &accept_policy_callback,
680 &access_handler_callback,
682 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
683 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
684 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
685 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
688 if ( (daemon_handle_v6 == NULL) &&
689 (daemon_handle_v4 == NULL) )
691 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
692 _("Could not start hostlist HTTP server on port %u\n"),
693 (unsigned short) port);
694 return GNUNET_SYSERR;
699 *server_ch = &connect_handler;
700 *server_dh = &disconnect_handler;
702 if (daemon_handle_v4 != NULL)
703 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
704 if (daemon_handle_v6 != NULL)
705 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
706 response_task = GNUNET_SCHEDULER_add_now (sched,
713 * Stop server offering our hostlist.
716 GNUNET_HOSTLIST_server_stop ()
718 #if DEBUG_HOSTLIST_SERVER
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Hostlist server shutdown\n");
722 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
724 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
725 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
727 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
729 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
730 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
734 GNUNET_PEERINFO_iterate_cancel (pitr);
737 if (GNUNET_SCHEDULER_NO_TASK != response_task)
739 GNUNET_SCHEDULER_cancel (sched, response_task);
740 response_task = GNUNET_SCHEDULER_NO_TASK;
742 if (NULL != daemon_handle_v4)
744 MHD_stop_daemon (daemon_handle_v4);
745 daemon_handle_v4 = NULL;
747 if (NULL != daemon_handle_v6)
749 MHD_stop_daemon (daemon_handle_v6);
750 daemon_handle_v6 = NULL;
752 if (response != NULL)
754 MHD_destroy_response (response);
759 /* end of hostlist-server.c */