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_identity_service.h>
34 #include <gnunet_namestore_service.h>
38 * Handle to the namestore.
40 static struct GNUNET_NAMESTORE_Handle *ns;
43 * Private key for the our zone.
45 static struct GNUNET_CRYPTO_EccPrivateKey zone_pkey;
48 * Handle to identity service.
50 static struct GNUNET_IDENTITY_Handle *identity;
53 * Name of the ego controlling the zone.
55 static char *ego_name;
58 * Desired action is to add a record.
63 * Iterator for the 'add' operation.
65 static struct GNUNET_NAMESTORE_ZoneIterator *add_zit;
68 * Queue entry for the 'add-uri' operation.
70 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
73 * Queue entry for the 'add' operation.
75 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
78 * Desired action is to list records.
83 * List iterator for the 'list' operation.
85 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
88 * Desired action is to remove a record.
100 static int nonauthority;
103 * Queue entry for the 'del' operation.
105 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
108 * Name of the records to add/list/remove.
113 * Value of the record to add/remove.
123 * Type of the record to add/remove, NULL to remove all.
125 static char *typestring;
128 * Desired expiration time.
130 static char *expirationstring;
133 * Global return value
138 * Type string converted to DNS type value.
140 static uint32_t type;
143 * Value in binary format.
148 * Number of bytes in 'data'.
150 static size_t data_size;
153 * Expirationstring converted to relative time.
155 static struct GNUNET_TIME_Relative etime_rel;
158 * Expirationstring converted to absolute time.
160 static struct GNUNET_TIME_Absolute etime_abs;
163 * Is expiration time relative or absolute time?
165 static int etime_is_rel = GNUNET_SYSERR;
170 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
173 * Enables monitor mode.
179 * Task run on shutdown. Cleans up everything.
182 * @param tc scheduler context
185 do_shutdown (void *cls,
186 const struct GNUNET_SCHEDULER_TaskContext *tc)
190 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
195 GNUNET_NAMESTORE_cancel (add_qe);
198 if (NULL != add_qe_uri)
200 GNUNET_NAMESTORE_cancel (add_qe_uri);
205 GNUNET_NAMESTORE_cancel (del_qe);
210 GNUNET_NAMESTORE_disconnect (ns);
213 memset (&zone_pkey, 0, sizeof (zone_pkey));
221 GNUNET_NAMESTORE_zone_monitor_stop (zm);
233 * Continuation called to notify client about result of the
236 * @param cls closure, location of the QueueEntry pointer to NULL out
237 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
238 * GNUNET_NO if content was already there
239 * GNUNET_YES (or other positive value) on success
240 * @param emsg NULL on success, otherwise an error message
243 add_continuation (void *cls,
247 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
250 if (GNUNET_YES != success)
253 _("Adding record failed: %s\n"),
254 (GNUNET_NO == success) ? "record exists" : emsg);
255 if (GNUNET_NO != success)
258 if ( (NULL == add_qe) &&
259 (NULL == add_qe_uri) &&
262 GNUNET_SCHEDULER_shutdown ();
267 * Continuation called to notify client about result of the
270 * @param cls closure, unused
271 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
272 * GNUNET_NO if content was already there
273 * GNUNET_YES (or other positive value) on success
274 * @param emsg NULL on success, otherwise an error message
277 del_continuation (void *cls,
282 if (success != GNUNET_YES)
284 _("Deleting record failed: %s\n"),
286 if ( (NULL == add_qe) &&
287 (NULL == add_qe_uri) &&
289 GNUNET_SCHEDULER_shutdown ();
294 * Process a record that was stored in the namestore.
297 * @param zone_key private key of the zone
298 * @param name name that is being mapped (at most 255 characters long)
299 * @param rd_len number of entries in 'rd' array
300 * @param rd array of records with data to store
303 display_record (void *cls,
304 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
307 const struct GNUNET_NAMESTORE_RecordData *rd)
309 const char *typestring;
316 if ( (NULL == del_qe) &&
317 (NULL == add_qe_uri) &&
319 GNUNET_SCHEDULER_shutdown ();
325 for (i=0;i<rd_len;i++)
327 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
328 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
333 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
334 (unsigned int) rd[i].record_type);
343 FPRINTF (stdout, "%s", "\n");
344 GNUNET_NAMESTORE_zone_iterator_next (list_it);
349 * Function called once we are in sync in monitor mode.
356 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
361 * We're storing a record; this function is given the existing record
362 * so that we can merge the information.
364 * @param cls closure, unused
365 * @param zone_key private key of the zone
366 * @param rec_name name that is being mapped (at most 255 characters long)
367 * @param rd_count number of entries in 'rd' array
368 * @param rd array of records with data to store
371 get_existing_record (void *cls,
372 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
373 const char *rec_name,
374 unsigned int rd_count,
375 const struct GNUNET_NAMESTORE_RecordData *rd)
377 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
378 struct GNUNET_NAMESTORE_RecordData *rde;
380 if ( (NULL != zone_key) &&
381 (0 != strcmp (rec_name, name)) )
383 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
386 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
387 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
388 /* FIXME: should add some logic to overwrite records if there
389 can only be one record of a particular type, and to check
390 if the combination of records is valid to begin with... */
393 rde->data_size = data_size;
394 rde->record_type = type;
395 if (1 != nonauthority)
396 rde->flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
398 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
399 GNUNET_assert (NULL != name);
400 add_qe = GNUNET_NAMESTORE_records_store (ns,
407 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
413 * Function called with the result from the check if the namestore
414 * service is actually running. If it is, we start the actual
417 * @param cls closure with our configuration
418 * @param result GNUNET_YES if the namestore service is running
421 testservice_task (void *cls,
424 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
425 struct GNUNET_CRYPTO_EccPublicKey pub;
426 struct GNUNET_NAMESTORE_RecordData rd;
428 if (GNUNET_YES != result)
430 FPRINTF (stderr, _("Service `%s' is not running\n"),
434 if (! (add|del|list|(NULL != uri)))
436 /* nothing more to be done */
438 _("No options given\n"));
441 GNUNET_CRYPTO_ecc_key_get_public (&zone_pkey,
444 ns = GNUNET_NAMESTORE_connect (cfg);
447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
448 _("Failed to connect to namestore\n"));
451 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
458 _("Missing option `%s' for operation `%s'\n"),
460 GNUNET_SCHEDULER_shutdown ();
464 if (NULL == typestring)
467 _("Missing option `%s' for operation `%s'\n"),
469 GNUNET_SCHEDULER_shutdown ();
473 type = GNUNET_NAMESTORE_typename_to_number (typestring);
474 if (UINT32_MAX == type)
476 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
477 GNUNET_SCHEDULER_shutdown ();
484 _("Missing option `%s' for operation `%s'\n"),
487 GNUNET_SCHEDULER_shutdown ();
491 GNUNET_NAMESTORE_string_to_value (type,
496 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
499 GNUNET_SCHEDULER_shutdown ();
503 if (NULL == expirationstring)
506 _("Missing option `%s' for operation `%s'\n"),
508 GNUNET_SCHEDULER_shutdown ();
512 if (0 == strcmp (expirationstring, "never"))
514 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
515 etime_is_rel = GNUNET_NO;
517 else if (GNUNET_OK ==
518 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
521 etime_is_rel = GNUNET_YES;
523 else if (GNUNET_OK ==
524 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
527 etime_is_rel = GNUNET_NO;
532 _("Invalid time format `%s'\n"),
534 GNUNET_SCHEDULER_shutdown ();
538 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
540 &get_existing_record,
548 _("Missing option `%s' for operation `%s'\n"),
550 GNUNET_SCHEDULER_shutdown ();
554 del_qe = GNUNET_NAMESTORE_records_store (ns,
563 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
572 struct GNUNET_CRYPTO_EccPublicKey pkey;
574 if ( (2 != (sscanf (uri,
575 "gnunet://gns/%104s/%63s",
579 GNUNET_CRYPTO_ecc_public_key_from_string (sh, strlen (sh), &pkey)) )
582 _("Invalid URI `%s'\n"),
584 GNUNET_SCHEDULER_shutdown ();
588 memset (&rd, 0, sizeof (rd));
590 rd.data_size = sizeof (struct GNUNET_CRYPTO_EccPublicKey);
591 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
592 if (GNUNET_YES == etime_is_rel)
594 rd.expiration_time = etime_rel.rel_value_us;
595 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
597 else if (GNUNET_NO == etime_is_rel)
598 rd.expiration_time = etime_abs.abs_value_us;
600 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
601 if (1 != nonauthority)
602 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
603 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
613 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
623 * Callback invoked from identity service with ego information.
624 * An @a ego of NULL and a @a name of NULL indicate the end of
625 * the initial iteration over known egos.
627 * @param cls closure with the configuration
628 * @param ego an ego known to identity service, or NULL
629 * @param ctx per-ego user context (unused)
630 * @param name name of the ego, or NULL
633 identity_cb (void *cls,
634 struct GNUNET_IDENTITY_Ego *ego,
638 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
640 if ( (NULL != ego_name) &&
644 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
645 GNUNET_free (ego_name);
647 GNUNET_CLIENT_service_test ("namestore", cfg,
648 GNUNET_TIME_UNIT_SECONDS,
651 GNUNET_IDENTITY_disconnect (identity);
654 if ( (NULL != ego_name) &&
659 _("Ego `%s' not known to identity service\n"),
661 GNUNET_IDENTITY_disconnect (identity);
669 * Main function that will be run.
672 * @param args remaining command-line arguments
673 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
674 * @param cfg configuration
677 run (void *cls, char *const *args, const char *cfgfile,
678 const struct GNUNET_CONFIGURATION_Handle *cfg)
680 if (NULL == ego_name)
683 _("You must specify which zone should be accessed\n"));
686 if ( (NULL != args[0]) && (NULL == uri) )
687 uri = GNUNET_strdup (args[0]);
688 identity = GNUNET_IDENTITY_connect (cfg,
695 * The main function for gnunet-namestore.
697 * @param argc number of arguments from the command line
698 * @param argv command line arguments
699 * @return 0 ok, 1 on error
702 main (int argc, char *const *argv)
707 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
709 gettext_noop ("add record"), 0,
710 &GNUNET_GETOPT_set_one, &add},
711 {'d', "delete", NULL,
712 gettext_noop ("delete record"), 0,
713 &GNUNET_GETOPT_set_one, &del},
714 {'D', "display", NULL,
715 gettext_noop ("display records"), 0,
716 &GNUNET_GETOPT_set_one, &list},
717 {'e', "expiration", "TIME",
718 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
719 &GNUNET_GETOPT_set_string, &expirationstring},
720 {'m', "monitor", NULL,
721 gettext_noop ("monitor changes in the namestore"), 0,
722 &GNUNET_GETOPT_set_one, &monitor},
723 {'n', "name", "NAME",
724 gettext_noop ("name of the record to add/delete/display"), 1,
725 &GNUNET_GETOPT_set_string, &name},
726 {'t', "type", "TYPE",
727 gettext_noop ("type of the record to add/delete/display"), 1,
728 &GNUNET_GETOPT_set_string, &typestring},
730 gettext_noop ("URI to import into our zone"), 1,
731 &GNUNET_GETOPT_set_string, &uri},
732 {'V', "value", "VALUE",
733 gettext_noop ("value of the record to add/delete"), 1,
734 &GNUNET_GETOPT_set_string, &value},
735 {'p', "public", NULL,
736 gettext_noop ("create or list public record"), 0,
737 &GNUNET_GETOPT_set_one, &public},
738 {'N', "non-authority", NULL,
739 gettext_noop ("create or list non-authority record"), 0,
740 &GNUNET_GETOPT_set_one, &nonauthority},
742 gettext_noop ("name of the ego controlling the zone"), 1,
743 &GNUNET_GETOPT_set_string, &ego_name},
744 GNUNET_GETOPT_OPTION_END
747 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
750 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
752 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
753 _("GNUnet zone manipulation tool"),
757 GNUNET_free ((void*) argv);
760 GNUNET_free ((void*) argv);
764 /* end of gnunet-namestore.c */