2 This file is part of GNUnet.
3 (C) 2012, 2013 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 3, 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.
21 * @file gnunet-namestore.c
22 * @brief command line tool to manipulate the local zone
23 * @author Christian Grothoff
26 * - allow users to set record options (not just 'RF_AUTHORITY')
28 * - add options to list/lookup individual records
31 #include <gnunet_util_lib.h>
32 #include <gnunet_dnsparser_lib.h>
33 #include <gnunet_namestore_service.h>
37 * Handle to the namestore.
39 static struct GNUNET_NAMESTORE_Handle *ns;
42 * Private key for the our zone.
44 static struct GNUNET_CRYPTO_EccPrivateKey *zone_pkey;
47 * Keyfile to manipulate. FIXME: change to ego's name!
52 * Desired action is to add a record.
57 * Iterator for the 'add' operation.
59 static struct GNUNET_NAMESTORE_ZoneIterator *add_zit;
62 * Queue entry for the 'add-uri' operation.
64 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
67 * Queue entry for the 'add' operation.
69 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
72 * Desired action is to list records.
77 * List iterator for the 'list' operation.
79 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
82 * Desired action is to remove a record.
94 static int nonauthority;
97 * Queue entry for the 'del' operation.
99 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
102 * Name of the records to add/list/remove.
107 * Value of the record to add/remove.
117 * Type of the record to add/remove, NULL to remove all.
119 static char *typestring;
122 * Desired expiration time.
124 static char *expirationstring;
127 * Global return value
132 * Type string converted to DNS type value.
134 static uint32_t type;
137 * Value in binary format.
142 * Number of bytes in 'data'.
144 static size_t data_size;
147 * Expirationstring converted to relative time.
149 static struct GNUNET_TIME_Relative etime_rel;
152 * Expirationstring converted to absolute time.
154 static struct GNUNET_TIME_Absolute etime_abs;
157 * Is expiration time relative or absolute time?
159 static int etime_is_rel = GNUNET_SYSERR;
164 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
167 * Enables monitor mode.
173 * Task run on shutdown. Cleans up everything.
176 * @param tc scheduler context
179 do_shutdown (void *cls,
180 const struct GNUNET_SCHEDULER_TaskContext *tc)
184 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
189 GNUNET_NAMESTORE_cancel (add_qe);
192 if (NULL != add_qe_uri)
194 GNUNET_NAMESTORE_cancel (add_qe_uri);
199 GNUNET_NAMESTORE_cancel (del_qe);
204 GNUNET_NAMESTORE_disconnect (ns);
207 if (NULL != zone_pkey)
209 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
219 GNUNET_NAMESTORE_zone_monitor_stop (zm);
231 * Continuation called to notify client about result of the
234 * @param cls closure, location of the QueueEntry pointer to NULL out
235 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
236 * GNUNET_NO if content was already there
237 * GNUNET_YES (or other positive value) on success
238 * @param emsg NULL on success, otherwise an error message
241 add_continuation (void *cls,
245 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
248 if (GNUNET_YES != success)
251 _("Adding record failed: %s\n"),
252 (GNUNET_NO == success) ? "record exists" : emsg);
253 if (GNUNET_NO != success)
256 if ( (NULL == add_qe) &&
257 (NULL == add_qe_uri) &&
260 GNUNET_SCHEDULER_shutdown ();
265 * Continuation called to notify client about result of the
268 * @param cls closure, unused
269 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
270 * GNUNET_NO if content was already there
271 * GNUNET_YES (or other positive value) on success
272 * @param emsg NULL on success, otherwise an error message
275 del_continuation (void *cls,
280 if (success != GNUNET_YES)
282 _("Deleting record failed: %s\n"),
284 if ( (NULL == add_qe) &&
285 (NULL == add_qe_uri) &&
287 GNUNET_SCHEDULER_shutdown ();
292 * Process a record that was stored in the namestore.
295 * @param zone_key private key of the zone
296 * @param name name that is being mapped (at most 255 characters long)
297 * @param rd_len number of entries in 'rd' array
298 * @param rd array of records with data to store
301 display_record (void *cls,
302 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
305 const struct GNUNET_NAMESTORE_RecordData *rd)
307 const char *typestring;
314 if ( (NULL == del_qe) &&
315 (NULL == add_qe_uri) &&
317 GNUNET_SCHEDULER_shutdown ();
323 for (i=0;i<rd_len;i++)
325 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
326 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
331 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
332 (unsigned int) rd[i].record_type);
341 FPRINTF (stdout, "%s", "\n");
342 GNUNET_NAMESTORE_zone_iterator_next (list_it);
347 * Function called once we are in sync in monitor mode.
354 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
359 * We're storing a record; this function is given the existing record
360 * so that we can merge the information.
362 * @param cls closure, unused
363 * @param zone_key private key of the zone
364 * @param rec_name name that is being mapped (at most 255 characters long)
365 * @param rd_count number of entries in 'rd' array
366 * @param rd array of records with data to store
369 get_existing_record (void *cls,
370 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
371 const char *rec_name,
372 unsigned int rd_count,
373 const struct GNUNET_NAMESTORE_RecordData *rd)
375 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
376 struct GNUNET_NAMESTORE_RecordData *rde;
378 if ( (NULL != zone_key) &&
379 (0 != strcmp (rec_name, name)) )
381 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
384 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
385 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
386 /* FIXME: should add some logic to overwrite records if there
387 can only be one record of a particular type, and to check
388 if the combination of records is valid to begin with... */
391 rde->data_size = data_size;
392 rde->record_type = type;
393 if (1 != nonauthority)
394 rde->flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
396 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
397 GNUNET_assert (NULL != name);
398 add_qe = GNUNET_NAMESTORE_records_store (ns,
405 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
411 * Function called with the result from the check if the namestore
412 * service is actually running. If it is, we start the actual
415 * @param cls closure with our configuration
416 * @param result GNUNET_YES if the namestore service is running
419 testservice_task (void *cls,
422 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
423 struct GNUNET_CRYPTO_EccPublicKey pub;
424 struct GNUNET_NAMESTORE_RecordData rd;
426 if (GNUNET_YES != result)
428 FPRINTF (stderr, _("Service `%s' is not running\n"),
434 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
435 "ZONEKEY", &keyfile))
437 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
442 _("Using default zone file `%s'\n"),
445 zone_pkey = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
447 if (! (add|del|list|(NULL != uri)))
449 /* nothing more to be done */
451 _("No options given\n"));
452 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
456 if (NULL == zone_pkey)
458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
459 _("Failed to read or create private zone key\n"));
462 GNUNET_CRYPTO_ecc_key_get_public (zone_pkey,
465 ns = GNUNET_NAMESTORE_connect (cfg);
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
469 _("Failed to connect to namestore\n"));
472 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
479 _("Missing option `%s' for operation `%s'\n"),
481 GNUNET_SCHEDULER_shutdown ();
485 if (NULL == typestring)
488 _("Missing option `%s' for operation `%s'\n"),
490 GNUNET_SCHEDULER_shutdown ();
494 type = GNUNET_NAMESTORE_typename_to_number (typestring);
495 if (UINT32_MAX == type)
497 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
498 GNUNET_SCHEDULER_shutdown ();
505 _("Missing option `%s' for operation `%s'\n"),
508 GNUNET_SCHEDULER_shutdown ();
512 GNUNET_NAMESTORE_string_to_value (type,
517 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
520 GNUNET_SCHEDULER_shutdown ();
524 if (NULL == expirationstring)
527 _("Missing option `%s' for operation `%s'\n"),
529 GNUNET_SCHEDULER_shutdown ();
533 if (0 == strcmp (expirationstring, "never"))
535 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
536 etime_is_rel = GNUNET_NO;
538 else if (GNUNET_OK ==
539 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
542 etime_is_rel = GNUNET_YES;
544 else if (GNUNET_OK ==
545 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
548 etime_is_rel = GNUNET_NO;
553 _("Invalid time format `%s'\n"),
555 GNUNET_SCHEDULER_shutdown ();
559 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
561 &get_existing_record,
569 _("Missing option `%s' for operation `%s'\n"),
571 GNUNET_SCHEDULER_shutdown ();
575 del_qe = GNUNET_NAMESTORE_records_store (ns,
584 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
593 struct GNUNET_CRYPTO_EccPublicKey pkey;
595 if ( (2 != (sscanf (uri,
596 "gnunet://gns/%104s/%63s",
600 GNUNET_CRYPTO_ecc_public_key_from_string (sh, strlen (sh), &pkey)) )
603 _("Invalid URI `%s'\n"),
605 GNUNET_SCHEDULER_shutdown ();
609 memset (&rd, 0, sizeof (rd));
611 rd.data_size = sizeof (struct GNUNET_CRYPTO_EccPublicKey);
612 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
613 if (GNUNET_YES == etime_is_rel)
615 rd.expiration_time = etime_rel.rel_value_us;
616 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
618 else if (GNUNET_NO == etime_is_rel)
619 rd.expiration_time = etime_abs.abs_value_us;
621 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
622 if (1 != nonauthority)
623 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
624 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
634 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
644 * Main function that will be run.
647 * @param args remaining command-line arguments
648 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
649 * @param cfg configuration
652 run (void *cls, char *const *args, const char *cfgfile,
653 const struct GNUNET_CONFIGURATION_Handle *cfg)
656 if ( (NULL != args[0]) && (NULL == uri) )
657 uri = GNUNET_strdup (args[0]);
659 GNUNET_CLIENT_service_test ("namestore", cfg,
660 GNUNET_TIME_UNIT_SECONDS,
667 * The main function for gnunet-namestore.
669 * @param argc number of arguments from the command line
670 * @param argv command line arguments
671 * @return 0 ok, 1 on error
674 main (int argc, char *const *argv)
679 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
681 gettext_noop ("add record"), 0,
682 &GNUNET_GETOPT_set_one, &add},
683 {'d', "delete", NULL,
684 gettext_noop ("delete record"), 0,
685 &GNUNET_GETOPT_set_one, &del},
686 {'D', "display", NULL,
687 gettext_noop ("display records"), 0,
688 &GNUNET_GETOPT_set_one, &list},
689 {'e', "expiration", "TIME",
690 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
691 &GNUNET_GETOPT_set_string, &expirationstring},
692 {'m', "monitor", NULL,
693 gettext_noop ("monitor changes in the namestore"), 0,
694 &GNUNET_GETOPT_set_one, &monitor},
695 {'n', "name", "NAME",
696 gettext_noop ("name of the record to add/delete/display"), 1,
697 &GNUNET_GETOPT_set_string, &name},
698 {'t', "type", "TYPE",
699 gettext_noop ("type of the record to add/delete/display"), 1,
700 &GNUNET_GETOPT_set_string, &typestring},
702 gettext_noop ("URI to import into our zone"), 1,
703 &GNUNET_GETOPT_set_string, &uri},
704 {'V', "value", "VALUE",
705 gettext_noop ("value of the record to add/delete"), 1,
706 &GNUNET_GETOPT_set_string, &value},
707 {'p', "public", NULL,
708 gettext_noop ("create or list public record"), 0,
709 &GNUNET_GETOPT_set_one, &public},
710 {'N', "non-authority", NULL,
711 gettext_noop ("create or list non-authority record"), 0,
712 &GNUNET_GETOPT_set_one, &nonauthority},
713 {'z', "zonekey", "FILENAME",
714 gettext_noop ("filename with the zone key"), 1,
715 &GNUNET_GETOPT_set_string, &keyfile},
716 GNUNET_GETOPT_OPTION_END
719 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
722 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
724 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
725 _("GNUnet zone manipulation tool"),
729 GNUNET_free ((void*) argv);
732 GNUNET_free ((void*) argv);
736 /* end of gnunet-namestore.c */