diff options
Diffstat (limited to 'src/namestore/gnunet-namestore-fcfsd.c')
-rw-r--r-- | src/namestore/gnunet-namestore-fcfsd.c | 160 |
1 files changed, 98 insertions, 62 deletions
diff --git a/src/namestore/gnunet-namestore-fcfsd.c b/src/namestore/gnunet-namestore-fcfsd.c index 903253b1ae..f4cdf26b47 100644 --- a/src/namestore/gnunet-namestore-fcfsd.c +++ b/src/namestore/gnunet-namestore-fcfsd.c @@ -2,20 +2,18 @@ This file is part of GNUnet. Copyright (C) 2012-2014 GNUnet e.V. - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * @file gnunet-namestore-fcfsd.c @@ -161,11 +159,6 @@ struct Request struct ZoneinfoRequest { /** - * Connection - */ - struct MHD_Connection *connection; - - /** * List iterator */ struct GNUNET_NAMESTORE_ZoneIterator *list_it; @@ -212,6 +205,16 @@ static struct GNUNET_CRYPTO_EcdsaPrivateKey fcfs_zone_pkey; static struct GNUNET_IDENTITY_Handle *identity; /** + * Zoneinfo page we currently use. + */ +static struct MHD_Response *info_page; + +/** + * Task that runs #update_zoneinfo_page peridicially. + */ +static struct GNUNET_SCHEDULER_Task *uzp_task; + +/** * Request for our ego. */ static struct GNUNET_IDENTITY_Operation *id_op; @@ -221,6 +224,11 @@ static struct GNUNET_IDENTITY_Operation *id_op; */ static unsigned long long port; +/** + * Name of the zone we manage. + */ +static char *zone; + /** * Task run whenever HTTP server operations are pending. @@ -247,25 +255,25 @@ run_httpd_now () /** + * Create fresh version of zone information. + */ +static void +update_zoneinfo_page (void *cls); + + +/** * Function called on error in zone iteration. */ static void zone_iteration_error (void *cls) { struct ZoneinfoRequest *zr = cls; - struct MHD_Response *response; zr->list_it = NULL; - response = MHD_create_response_from_buffer (strlen ("internal error"), - (void *) "internal error", - MHD_RESPMEM_PERSISTENT); - MHD_queue_response (zr->connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, - response); - MHD_destroy_response (response); GNUNET_free (zr->zoneinfo); - GNUNET_free (zr); - run_httpd_now (); + GNUNET_SCHEDULER_cancel (uzp_task); + uzp_task = GNUNET_SCHEDULER_add_now (&update_zoneinfo_page, + NULL); } @@ -292,13 +300,9 @@ zone_iteration_end (void *cls) MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, MIME_HTML); - MHD_queue_response (zr->connection, - MHD_HTTP_OK, - response); - MHD_destroy_response (response); + MHD_destroy_response (info_page); + info_page = response; GNUNET_free (zr->zoneinfo); - GNUNET_free (zr); - run_httpd_now (); } @@ -373,27 +377,41 @@ iterate_cb (void *cls, * Handler that returns FCFS zoneinfo page. * * @param connection connection to use - * @return MHD_YES on success */ -static int +static int serve_zoneinfo_page (struct MHD_Connection *connection) { - struct ZoneinfoRequest *zr; - - zr = GNUNET_new (struct ZoneinfoRequest); - zr->zoneinfo = GNUNET_malloc (DEFAULT_ZONEINFO_BUFSIZE); - zr->buf_len = DEFAULT_ZONEINFO_BUFSIZE; - zr->connection = connection; - zr->write_offset = 0; - zr->list_it = GNUNET_NAMESTORE_zone_iteration_start (ns, - &fcfs_zone_pkey, - &zone_iteration_error, - zr, - &iterate_cb, - zr, - &zone_iteration_end, - zr); - return MHD_YES; + return MHD_queue_response (connection, + MHD_HTTP_OK, + info_page); +} + + +/** + * Create fresh version of zone information. + */ +static void +update_zoneinfo_page (void *cls) +{ + static struct ZoneinfoRequest zr; + + (void) cls; + uzp_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, + &update_zoneinfo_page, + NULL); + if (NULL != zr.list_it) + return; + zr.zoneinfo = GNUNET_malloc (DEFAULT_ZONEINFO_BUFSIZE); + zr.buf_len = DEFAULT_ZONEINFO_BUFSIZE; + zr.write_offset = 0; + zr.list_it = GNUNET_NAMESTORE_zone_iteration_start (ns, + &fcfs_zone_pkey, + &zone_iteration_error, + &zr, + &iterate_cb, + &zr, + &zone_iteration_end, + &zr); } @@ -401,7 +419,7 @@ serve_zoneinfo_page (struct MHD_Connection *connection) * Handler that returns a simple static HTTP page. * * @param connection connection to use - * @return MHD_YES on success + * @return #MHD_YES on success */ static int serve_main_page (struct MHD_Connection *connection) @@ -634,14 +652,14 @@ lookup_block_error (void *cls) * and continue to process the result. * * @param cls the 'struct Request' we are processing - * @param zone private key of the zone; NULL on disconnect + * @param zonekey private key of the zone; NULL on disconnect * @param label label of the records; NULL on disconnect * @param rd_count number of entries in @a rd array, 0 if label was deleted * @param rd array of records with data to store */ static void lookup_block_processor (void *cls, - const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, + const struct GNUNET_CRYPTO_EcdsaPrivateKey *zonekey, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd) @@ -650,7 +668,7 @@ lookup_block_processor (void *cls, (void) label; (void) rd; - (void) zone; + (void) zonekey; request->qe = NULL; if (0 == rd_count) { @@ -703,8 +721,8 @@ lookup_block_processor (void *cls, * @a upload_data provided; the method must update this * value to the number of bytes NOT processed; * @param ptr pointer to location where we store the 'struct Request' - * @return MHD_YES if the connection was handled successfully, - * MHD_NO if the socket must be closed due to a serious + * @return #MHD_YES if the connection was handled successfully, + * #MHD_NO if the socket must be closed due to a serious * error while handling the request */ static int @@ -959,6 +977,11 @@ do_shutdown (void *cls) GNUNET_SCHEDULER_cancel (httpd_task); httpd_task = NULL; } + if (NULL != uzp_task) + { + GNUNET_SCHEDULER_cancel (uzp_task); + uzp_task = NULL; + } if (NULL != ns) { GNUNET_NAMESTORE_disconnect (ns); @@ -1011,12 +1034,16 @@ identity_cb (void *cls, (void) cls; (void) ctx; - (void) name; - id_op = NULL; + if (NULL == name) + return; + if (0 != strcmp (name, + zone)) + return; if (NULL == ego) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No ego configured for `fcfsd` subsystem\n")); + GNUNET_SCHEDULER_shutdown (); return; } fcfs_zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego); @@ -1041,6 +1068,7 @@ identity_cb (void *cls, while (NULL == httpd); if (NULL == httpd) { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start HTTP server\n")); GNUNET_SCHEDULER_shutdown (); @@ -1085,16 +1113,18 @@ run (void *cls, return; } identity = GNUNET_IDENTITY_connect (cfg, - NULL, NULL); + &identity_cb, + NULL); if (NULL == identity) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to connect to identity\n")); return; } - id_op = GNUNET_IDENTITY_get (identity, "fcfsd", - &identity_cb, NULL); - GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); + uzp_task = GNUNET_SCHEDULER_add_now (&update_zoneinfo_page, + NULL); + GNUNET_SCHEDULER_add_shutdown (&do_shutdown, + NULL); } @@ -1109,7 +1139,13 @@ int main (int argc, char *const *argv) { - static const struct GNUNET_GETOPT_CommandLineOption options[] = { + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_option_mandatory + (GNUNET_GETOPT_option_string ('z', + "zone", + "EGO", + gettext_noop ("name of the zone that is to be managed by FCFSD"), + &zone)), GNUNET_GETOPT_OPTION_END }; int ret; |