2 This file is part of GNUnet.
3 (C) 2012 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 * Hash of the public key of our zone.
44 static struct GNUNET_CRYPTO_ShortHashCode zone;
47 * Private key for the our zone.
49 static struct GNUNET_CRYPTO_EccPrivateKey *zone_pkey;
52 * Keyfile to manipulate.
57 * Desired action is to add a record.
62 * Queue entry for the 'add' operation.
64 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
67 * Queue entry for the 'add-uri' operation.
69 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
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.
172 * Task run on shutdown. Cleans up everything.
175 * @param tc scheduler context
178 do_shutdown (void *cls,
179 const struct GNUNET_SCHEDULER_TaskContext *tc)
183 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
188 GNUNET_NAMESTORE_cancel (add_qe);
191 if (NULL != add_qe_uri)
193 GNUNET_NAMESTORE_cancel (add_qe_uri);
198 GNUNET_NAMESTORE_cancel (del_qe);
203 GNUNET_NAMESTORE_disconnect (ns);
206 if (NULL != zone_pkey)
208 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
218 GNUNET_NAMESTORE_zone_monitor_stop (zm);
230 * Continuation called to notify client about result of the
233 * @param cls closure, location of the QueueEntry pointer to NULL out
234 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
235 * GNUNET_NO if content was already there
236 * GNUNET_YES (or other positive value) on success
237 * @param emsg NULL on success, otherwise an error message
240 add_continuation (void *cls,
244 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
247 if (GNUNET_YES != success)
250 _("Adding record failed: %s\n"),
251 (GNUNET_NO == success) ? "record exists" : emsg);
252 if (GNUNET_NO != success)
255 if ( (NULL == add_qe) &&
256 (NULL == add_qe_uri) &&
259 GNUNET_SCHEDULER_shutdown ();
264 * Continuation called to notify client about result of the
267 * @param cls closure, unused
268 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
269 * GNUNET_NO if content was already there
270 * GNUNET_YES (or other positive value) on success
271 * @param emsg NULL on success, otherwise an error message
274 del_continuation (void *cls,
279 if (success != GNUNET_YES)
281 _("Deleting record failed: %s\n"),
283 if ( (NULL == add_qe) &&
284 (NULL == add_qe_uri) &&
286 GNUNET_SCHEDULER_shutdown ();
291 * Process a record that was stored in the namestore.
294 * @param zone_key public key of the zone
295 * @param expire when does the corresponding block in the DHT expire (until
296 * when should we never do a DHT lookup for the same name again)?;
297 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
298 * or the expiration time of the block in the namestore (even if there are zero
299 * records matching the desired record type)
300 * @param name name that is being mapped (at most 255 characters long)
301 * @param rd_len number of entries in 'rd' array
302 * @param rd array of records with data to store
303 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
304 * because the user queried for a particular record type only)
307 display_record (void *cls,
308 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
309 struct GNUNET_TIME_Absolute expire,
312 const struct GNUNET_NAMESTORE_RecordData *rd,
313 const struct GNUNET_CRYPTO_EccSignature *signature)
315 const char *typestring;
319 struct GNUNET_TIME_Absolute aex;
320 struct GNUNET_TIME_Relative rex;
325 if ( (NULL == del_qe) &&
326 (NULL == add_qe_uri) &&
328 GNUNET_SCHEDULER_shutdown ();
334 for (i=0;i<rd_len;i++)
336 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
337 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
342 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
343 (unsigned int) rd[i].record_type);
346 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
348 rex.rel_value_us = rd[i].expiration_time;
349 etime = GNUNET_STRINGS_relative_time_to_string (rex, GNUNET_YES);
353 aex.abs_value_us = rd[i].expiration_time;
354 etime = GNUNET_STRINGS_absolute_time_to_string (aex);
356 FPRINTF (stdout, "\t%s: %s (%s %s)\n", typestring, s,
357 (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
358 ? _(/* what follows is relative expiration */ "for at least")
359 : _(/* what follows is absolute expiration */ "until"),
363 FPRINTF (stdout, "%s", "\n");
364 GNUNET_NAMESTORE_zone_iterator_next (list_it);
369 * Function called once we are in sync in monitor mode.
376 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
381 * We're storing a record; this function is given the existing record
382 * so that we can merge the information.
384 * @param cls closure, unused
385 * @param zone_key public key of the zone
386 * @param freshness when does the corresponding block in the DHT expire (until
387 * when should we never do a DHT lookup for the same name again)?;
388 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
389 * or the expiration time of the block in the namestore (even if there are zero
390 * records matching the desired record type)
391 * @param rec_name name that is being mapped (at most 255 characters long)
392 * @param rd_count number of entries in 'rd' array
393 * @param rd array of records with data to store
394 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
395 * because the user queried for a particular record type only)
398 get_existing_record (void *cls,
399 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
400 struct GNUNET_TIME_Absolute freshness,
401 const char *rec_name,
402 unsigned int rd_count,
403 const struct GNUNET_NAMESTORE_RecordData *rd,
404 const struct GNUNET_CRYPTO_EccSignature *signature)
406 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
407 struct GNUNET_NAMESTORE_RecordData *rde;
410 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
411 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
412 /* FIXME: should add some logic to overwrite records if there
413 can only be one record of a particular type, and to check
414 if the combination of records is valid to begin with... */
417 rde->data_size = data_size;
418 rde->record_type = type;
419 if (GNUNET_YES == etime_is_rel)
421 rde->expiration_time = etime_rel.rel_value_us;
422 rde->flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
424 else if (GNUNET_NO == etime_is_rel)
426 rde->expiration_time = etime_abs.abs_value_us;
428 if (1 != nonauthority)
429 rde->flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
431 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
432 GNUNET_assert (NULL != name);
433 add_qe = GNUNET_NAMESTORE_record_put_by_authority (ns,
444 * Function called with the result from the check if the namestore
445 * service is actually running. If it is, we start the actual
448 * @param cls closure with our configuration
449 * @param result GNUNET_YES if the namestore service is running
452 testservice_task (void *cls,
455 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
456 struct GNUNET_CRYPTO_EccPublicKey pub;
457 struct GNUNET_NAMESTORE_RecordData rd;
459 if (GNUNET_YES != result)
461 FPRINTF (stderr, _("Service `%s' is not running\n"),
467 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
468 "ZONEKEY", &keyfile))
470 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
475 _("Using default zone file `%s'\n"),
478 zone_pkey = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
480 if (! (add|del|list|(NULL != uri)))
482 /* nothing more to be done */
484 _("No options given\n"));
485 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
489 if (NULL == zone_pkey)
491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
492 _("Failed to read or create private zone key\n"));
495 GNUNET_CRYPTO_ecc_key_get_public (zone_pkey,
497 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
499 ns = GNUNET_NAMESTORE_connect (cfg);
502 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
503 _("Failed to connect to namestore\n"));
506 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
513 _("Missing option `%s' for operation `%s'\n"),
515 GNUNET_SCHEDULER_shutdown ();
519 if (NULL == typestring)
522 _("Missing option `%s' for operation `%s'\n"),
524 GNUNET_SCHEDULER_shutdown ();
528 type = GNUNET_NAMESTORE_typename_to_number (typestring);
529 if (UINT32_MAX == type)
531 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
532 GNUNET_SCHEDULER_shutdown ();
539 _("Missing option `%s' for operation `%s'\n"),
542 GNUNET_SCHEDULER_shutdown ();
546 GNUNET_NAMESTORE_string_to_value (type,
551 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
554 GNUNET_SCHEDULER_shutdown ();
558 if (NULL == expirationstring)
561 _("Missing option `%s' for operation `%s'\n"),
563 GNUNET_SCHEDULER_shutdown ();
567 if (0 == strcmp (expirationstring, "never"))
569 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
570 etime_is_rel = GNUNET_NO;
572 else if (GNUNET_OK ==
573 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
576 etime_is_rel = GNUNET_YES;
578 else if (GNUNET_OK ==
579 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
582 etime_is_rel = GNUNET_NO;
587 _("Invalid time format `%s'\n"),
589 GNUNET_SCHEDULER_shutdown ();
593 add_qe = GNUNET_NAMESTORE_lookup_record (ns,
597 &get_existing_record,
605 _("Missing option `%s' for operation `%s'\n"),
607 GNUNET_SCHEDULER_shutdown ();
611 del_qe = GNUNET_NAMESTORE_record_put_by_authority (ns,
620 uint32_t must_not_flags = 0;
622 if (1 == nonauthority) /* List non-authority records */
623 must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
626 must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
628 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
630 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION,
639 struct GNUNET_CRYPTO_ShortHashCode sc;
641 if ( (2 != (sscanf (uri,
642 "gnunet://gns/%52s/%63s",
646 GNUNET_CRYPTO_short_hash_from_string (sh, &sc)) )
649 _("Invalid URI `%s'\n"),
651 GNUNET_SCHEDULER_shutdown ();
655 memset (&rd, 0, sizeof (rd));
657 rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
658 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
659 if (GNUNET_YES == etime_is_rel)
661 rd.expiration_time = etime_rel.rel_value_us;
662 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
664 else if (GNUNET_NO == etime_is_rel)
665 rd.expiration_time = etime_abs.abs_value_us;
667 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
668 if (1 != nonauthority)
669 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
670 add_qe_uri = GNUNET_NAMESTORE_record_put_by_authority (ns,
680 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
690 * Main function that will be run.
693 * @param args remaining command-line arguments
694 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
695 * @param cfg configuration
698 run (void *cls, char *const *args, const char *cfgfile,
699 const struct GNUNET_CONFIGURATION_Handle *cfg)
702 if ( (NULL != args[0]) && (NULL == uri) )
703 uri = GNUNET_strdup (args[0]);
705 GNUNET_CLIENT_service_test ("namestore", cfg,
706 GNUNET_TIME_UNIT_SECONDS,
713 * The main function for gnunet-namestore.
715 * @param argc number of arguments from the command line
716 * @param argv command line arguments
717 * @return 0 ok, 1 on error
720 main (int argc, char *const *argv)
725 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
727 gettext_noop ("add record"), 0,
728 &GNUNET_GETOPT_set_one, &add},
729 {'d', "delete", NULL,
730 gettext_noop ("delete record"), 0,
731 &GNUNET_GETOPT_set_one, &del},
732 {'D', "display", NULL,
733 gettext_noop ("display records"), 0,
734 &GNUNET_GETOPT_set_one, &list},
735 {'e', "expiration", "TIME",
736 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
737 &GNUNET_GETOPT_set_string, &expirationstring},
738 {'m', "monitor", NULL,
739 gettext_noop ("monitor changes in the namestore"), 0,
740 &GNUNET_GETOPT_set_one, &monitor},
741 {'n', "name", "NAME",
742 gettext_noop ("name of the record to add/delete/display"), 1,
743 &GNUNET_GETOPT_set_string, &name},
744 {'t', "type", "TYPE",
745 gettext_noop ("type of the record to add/delete/display"), 1,
746 &GNUNET_GETOPT_set_string, &typestring},
748 gettext_noop ("URI to import into our zone"), 1,
749 &GNUNET_GETOPT_set_string, &uri},
750 {'V', "value", "VALUE",
751 gettext_noop ("value of the record to add/delete"), 1,
752 &GNUNET_GETOPT_set_string, &value},
753 {'p', "public", NULL,
754 gettext_noop ("create or list public record"), 0,
755 &GNUNET_GETOPT_set_one, &public},
756 {'N', "non-authority", NULL,
757 gettext_noop ("create or list non-authority record"), 0,
758 &GNUNET_GETOPT_set_one, &nonauthority},
759 {'z', "zonekey", "FILENAME",
760 gettext_noop ("filename with the zone key"), 1,
761 &GNUNET_GETOPT_set_string, &keyfile},
762 GNUNET_GETOPT_OPTION_END
765 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
768 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
770 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
771 _("GNUnet zone manipulation tool"),
775 GNUNET_free ((void*) argv);
778 GNUNET_free ((void*) argv);
782 /* end of gnunet-namestore.c */