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);
225 * Continuation called to notify client about result of the
228 * @param cls closure, location of the QueueEntry pointer to NULL out
229 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
230 * GNUNET_NO if content was already there
231 * GNUNET_YES (or other positive value) on success
232 * @param emsg NULL on success, otherwise an error message
235 add_continuation (void *cls,
239 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
242 if (GNUNET_YES != success)
245 _("Adding record failed: %s\n"),
246 (GNUNET_NO == success) ? "record exists" : emsg);
247 if (GNUNET_NO != success)
250 if ( (NULL == add_qe) &&
251 (NULL == add_qe_uri) &&
254 GNUNET_SCHEDULER_shutdown ();
259 * Continuation called to notify client about result of the
262 * @param cls closure, unused
263 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
264 * GNUNET_NO if content was already there
265 * GNUNET_YES (or other positive value) on success
266 * @param emsg NULL on success, otherwise an error message
269 del_continuation (void *cls,
274 if (success != GNUNET_YES)
276 _("Deleting record failed: %s\n"),
278 if ( (NULL == add_qe) &&
279 (NULL == add_qe_uri) &&
281 GNUNET_SCHEDULER_shutdown ();
286 * Process a record that was stored in the namestore.
289 * @param zone_key public key of the zone
290 * @param expire when does the corresponding block in the DHT expire (until
291 * when should we never do a DHT lookup for the same name again)?;
292 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
293 * or the expiration time of the block in the namestore (even if there are zero
294 * records matching the desired record type)
295 * @param name name that is being mapped (at most 255 characters long)
296 * @param rd_len number of entries in 'rd' array
297 * @param rd array of records with data to store
298 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
299 * because the user queried for a particular record type only)
302 display_record (void *cls,
303 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
304 struct GNUNET_TIME_Absolute expire,
307 const struct GNUNET_NAMESTORE_RecordData *rd,
308 const struct GNUNET_CRYPTO_EccSignature *signature)
310 const char *typestring;
314 struct GNUNET_TIME_Absolute aex;
315 struct GNUNET_TIME_Relative rex;
320 if ( (NULL == del_qe) &&
321 (NULL == add_qe_uri) &&
323 GNUNET_SCHEDULER_shutdown ();
329 for (i=0;i<rd_len;i++)
331 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
332 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
337 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
338 (unsigned int) rd[i].record_type);
341 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
343 rex.rel_value = rd[i].expiration_time;
344 etime = GNUNET_STRINGS_relative_time_to_string (rex, GNUNET_YES);
348 aex.abs_value = rd[i].expiration_time;
349 etime = GNUNET_STRINGS_absolute_time_to_string (aex);
351 FPRINTF (stdout, "\t%s: %s (%s %s)\n", typestring, s,
352 (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
353 ? _(/* what follows is relative expiration */ "for at least")
354 : _(/* what follows is absolute expiration */ "until"),
358 FPRINTF (stdout, "%s", "\n");
359 GNUNET_NAMESTORE_zone_iterator_next (list_it);
364 * Function called once we are in sync in monitor mode.
371 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
376 * We're storing a record; this function is given the existing record
377 * so that we can merge the information.
379 * @param cls closure, unused
380 * @param zone_key public key of the zone
381 * @param freshness when does the corresponding block in the DHT expire (until
382 * when should we never do a DHT lookup for the same name again)?;
383 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
384 * or the expiration time of the block in the namestore (even if there are zero
385 * records matching the desired record type)
386 * @param rec_name name that is being mapped (at most 255 characters long)
387 * @param rd_count number of entries in 'rd' array
388 * @param rd array of records with data to store
389 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
390 * because the user queried for a particular record type only)
393 get_existing_record (void *cls,
394 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
395 struct GNUNET_TIME_Absolute freshness,
396 const char *rec_name,
397 unsigned int rd_count,
398 const struct GNUNET_NAMESTORE_RecordData *rd,
399 const struct GNUNET_CRYPTO_EccSignature *signature)
401 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
402 struct GNUNET_NAMESTORE_RecordData *rde;
405 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
406 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
407 /* FIXME: should add some logic to overwrite records if there
408 can only be one record of a particular type, and to check
409 if the combination of records is valid to begin with... */
412 rde->data_size = data_size;
413 rde->record_type = type;
414 if (GNUNET_YES == etime_is_rel)
416 rde->expiration_time = etime_rel.rel_value;
417 rde->flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
419 else if (GNUNET_NO == etime_is_rel)
421 rde->expiration_time = etime_abs.abs_value;
423 if (1 != nonauthority)
424 rde->flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
426 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
427 GNUNET_assert (NULL != name);
428 add_qe = GNUNET_NAMESTORE_record_put_by_authority (ns,
439 * Function called with the result from the check if the namestore
440 * service is actually running. If it is, we start the actual
443 * @param cls closure with our configuration
444 * @param result GNUNET_YES if the namestore service is running
447 testservice_task (void *cls,
450 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
451 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
452 struct GNUNET_NAMESTORE_RecordData rd;
454 if (GNUNET_YES != result)
456 FPRINTF (stderr, _("Service `%s' is not running\n"),
462 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
463 "ZONEKEY", &keyfile))
465 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
470 _("Using default zone file `%s'\n"),
473 zone_pkey = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
475 if (! (add|del|list|(NULL != uri)))
477 /* nothing more to be done */
479 _("No options given\n"));
480 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
484 if (NULL == zone_pkey)
486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
487 _("Failed to read or create private zone key\n"));
490 GNUNET_CRYPTO_ecc_key_get_public (zone_pkey,
492 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
494 ns = GNUNET_NAMESTORE_connect (cfg);
497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
498 _("Failed to connect to namestore\n"));
501 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
508 _("Missing option `%s' for operation `%s'\n"),
510 GNUNET_SCHEDULER_shutdown ();
514 if (NULL == typestring)
517 _("Missing option `%s' for operation `%s'\n"),
519 GNUNET_SCHEDULER_shutdown ();
523 type = GNUNET_NAMESTORE_typename_to_number (typestring);
524 if (UINT32_MAX == type)
526 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
527 GNUNET_SCHEDULER_shutdown ();
534 _("Missing option `%s' for operation `%s'\n"),
537 GNUNET_SCHEDULER_shutdown ();
541 GNUNET_NAMESTORE_string_to_value (type,
546 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
549 GNUNET_SCHEDULER_shutdown ();
553 if (NULL == expirationstring)
556 _("Missing option `%s' for operation `%s'\n"),
558 GNUNET_SCHEDULER_shutdown ();
562 if (0 == strcmp (expirationstring, "never"))
564 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
565 etime_is_rel = GNUNET_NO;
567 else if (GNUNET_OK ==
568 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
571 etime_is_rel = GNUNET_YES;
573 else if (GNUNET_OK ==
574 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
577 etime_is_rel = GNUNET_NO;
582 _("Invalid time format `%s'\n"),
584 GNUNET_SCHEDULER_shutdown ();
588 add_qe = GNUNET_NAMESTORE_lookup_record (ns,
592 &get_existing_record,
600 _("Missing option `%s' for operation `%s'\n"),
602 GNUNET_SCHEDULER_shutdown ();
606 del_qe = GNUNET_NAMESTORE_record_put_by_authority (ns,
615 uint32_t must_not_flags = 0;
617 if (1 == nonauthority) /* List non-authority records */
618 must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
621 must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
623 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
625 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION,
634 struct GNUNET_CRYPTO_ShortHashCode sc;
636 if ( (2 != (sscanf (uri,
637 "gnunet://gns/%52s/%63s",
641 GNUNET_CRYPTO_short_hash_from_string (sh, &sc)) )
644 _("Invalid URI `%s'\n"),
646 GNUNET_SCHEDULER_shutdown ();
650 memset (&rd, 0, sizeof (rd));
652 rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
653 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
654 if (GNUNET_YES == etime_is_rel)
656 rd.expiration_time = etime_rel.rel_value;
657 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
659 else if (GNUNET_NO == etime_is_rel)
660 rd.expiration_time = etime_abs.abs_value;
662 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
663 if (1 != nonauthority)
664 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
665 add_qe_uri = GNUNET_NAMESTORE_record_put_by_authority (ns,
673 GNUNET_free_non_null (data);
676 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
686 * Main function that will be run.
689 * @param args remaining command-line arguments
690 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
691 * @param cfg configuration
694 run (void *cls, char *const *args, const char *cfgfile,
695 const struct GNUNET_CONFIGURATION_Handle *cfg)
698 if ( (NULL != args[0]) && (NULL == uri) )
699 uri = GNUNET_strdup (args[0]);
701 GNUNET_CLIENT_service_test ("namestore", cfg,
702 GNUNET_TIME_UNIT_SECONDS,
709 * The main function for gnunet-namestore.
711 * @param argc number of arguments from the command line
712 * @param argv command line arguments
713 * @return 0 ok, 1 on error
716 main (int argc, char *const *argv)
721 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
723 gettext_noop ("add record"), 0,
724 &GNUNET_GETOPT_set_one, &add},
725 {'d', "delete", NULL,
726 gettext_noop ("delete record"), 0,
727 &GNUNET_GETOPT_set_one, &del},
728 {'D', "display", NULL,
729 gettext_noop ("display records"), 0,
730 &GNUNET_GETOPT_set_one, &list},
731 {'e', "expiration", "TIME",
732 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
733 &GNUNET_GETOPT_set_string, &expirationstring},
734 {'m', "monitor", NULL,
735 gettext_noop ("monitor changes in the namestore"), 0,
736 &GNUNET_GETOPT_set_one, &monitor},
737 {'n', "name", "NAME",
738 gettext_noop ("name of the record to add/delete/display"), 1,
739 &GNUNET_GETOPT_set_string, &name},
740 {'t', "type", "TYPE",
741 gettext_noop ("type of the record to add/delete/display"), 1,
742 &GNUNET_GETOPT_set_string, &typestring},
744 gettext_noop ("URI to import into our zone"), 1,
745 &GNUNET_GETOPT_set_string, &uri},
746 {'V', "value", "VALUE",
747 gettext_noop ("value of the record to add/delete"), 1,
748 &GNUNET_GETOPT_set_string, &value},
749 {'p', "public", NULL,
750 gettext_noop ("create or list public record"), 0,
751 &GNUNET_GETOPT_set_one, &public},
752 {'N', "non-authority", NULL,
753 gettext_noop ("create or list non-authority record"), 0,
754 &GNUNET_GETOPT_set_one, &nonauthority},
755 {'z', "zonekey", "FILENAME",
756 gettext_noop ("filename with the zone key"), 1,
757 &GNUNET_GETOPT_set_string, &keyfile},
758 GNUNET_GETOPT_OPTION_END
761 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
764 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
766 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
767 _("GNUnet zone manipulation tool"),
771 GNUNET_free ((void*) argv);
774 GNUNET_free ((void*) argv);
778 /* end of gnunet-namestore.c */