3 This file is part of GNUnet.
4 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
22 * @file gnunet-namestore.c
23 * @brief command line tool to manipulate the local zone
24 * @author Christian Grothoff
27 * - allow users to set record options (not just 'RF_AUTHORITY')
29 * - add options to list/lookup individual records
32 #include <gnunet_util_lib.h>
33 #include <gnunet_dnsparser_lib.h>
34 #include <gnunet_identity_service.h>
35 #include <gnunet_namestore_service.h>
39 * Handle to the namestore.
41 static struct GNUNET_NAMESTORE_Handle *ns;
44 * Private key for the our zone.
46 static struct GNUNET_CRYPTO_EccPrivateKey zone_pkey;
49 * Handle to identity lookup.
51 static struct GNUNET_IDENTITY_EgoLookup *el;
54 * Name of the ego controlling the zone.
56 static char *ego_name;
59 * Desired action is to add a record.
64 * Iterator for the 'add' operation.
66 static struct GNUNET_NAMESTORE_ZoneIterator *add_zit;
69 * Queue entry for the 'add-uri' operation.
71 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
74 * Queue entry for the 'add' operation.
76 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
79 * Desired action is to list records.
84 * List iterator for the 'list' operation.
86 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
89 * Desired action is to remove a record.
101 static int nonauthority;
104 * Queue entry for the 'del' operation.
106 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
109 * Name of the records to add/list/remove.
114 * Value of the record to add/remove.
124 * Type of the record to add/remove, NULL to remove all.
126 static char *typestring;
129 * Desired expiration time.
131 static char *expirationstring;
134 * Global return value
139 * Type string converted to DNS type value.
141 static uint32_t type;
144 * Value in binary format.
149 * Number of bytes in 'data'.
151 static size_t data_size;
154 * Expirationstring converted to relative time.
156 static struct GNUNET_TIME_Relative etime_rel;
159 * Expirationstring converted to absolute time.
161 static struct GNUNET_TIME_Absolute etime_abs;
164 * Is expiration time relative or absolute time?
166 static int etime_is_rel = GNUNET_SYSERR;
171 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
174 * Enables monitor mode.
180 * Task run on shutdown. Cleans up everything.
183 * @param tc scheduler context
186 do_shutdown (void *cls,
187 const struct GNUNET_SCHEDULER_TaskContext *tc)
191 GNUNET_IDENTITY_ego_lookup_cancel (el);
196 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
201 GNUNET_NAMESTORE_cancel (add_qe);
204 if (NULL != add_qe_uri)
206 GNUNET_NAMESTORE_cancel (add_qe_uri);
211 GNUNET_NAMESTORE_cancel (del_qe);
216 GNUNET_NAMESTORE_disconnect (ns);
219 memset (&zone_pkey, 0, sizeof (zone_pkey));
227 GNUNET_NAMESTORE_zone_monitor_stop (zm);
239 * Continuation called to notify client about result of the
242 * @param cls closure, location of the QueueEntry pointer to NULL out
243 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
244 * GNUNET_NO if content was already there
245 * GNUNET_YES (or other positive value) on success
246 * @param emsg NULL on success, otherwise an error message
249 add_continuation (void *cls,
253 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
256 if (GNUNET_YES != success)
259 _("Adding record failed: %s\n"),
260 (GNUNET_NO == success) ? "record exists" : emsg);
261 if (GNUNET_NO != success)
264 if ( (NULL == add_qe) &&
265 (NULL == add_qe_uri) &&
268 GNUNET_SCHEDULER_shutdown ();
273 * Continuation called to notify client about result of the
276 * @param cls closure, unused
277 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
278 * GNUNET_NO if content was already there
279 * GNUNET_YES (or other positive value) on success
280 * @param emsg NULL on success, otherwise an error message
283 del_continuation (void *cls,
288 if (success != GNUNET_YES)
290 _("Deleting record failed: %s\n"),
292 if ( (NULL == add_qe) &&
293 (NULL == add_qe_uri) &&
295 GNUNET_SCHEDULER_shutdown ();
300 * Process a record that was stored in the namestore.
303 * @param zone_key private key of the zone
304 * @param name name that is being mapped (at most 255 characters long)
305 * @param rd_len number of entries in 'rd' array
306 * @param rd array of records with data to store
309 display_record (void *cls,
310 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
313 const struct GNUNET_NAMESTORE_RecordData *rd)
315 const char *typestring;
322 if ( (NULL == del_qe) &&
323 (NULL == add_qe_uri) &&
325 GNUNET_SCHEDULER_shutdown ();
331 for (i=0;i<rd_len;i++)
333 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
334 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
339 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
340 (unsigned int) rd[i].record_type);
349 FPRINTF (stdout, "%s", "\n");
350 GNUNET_NAMESTORE_zone_iterator_next (list_it);
355 * Function called once we are in sync in monitor mode.
362 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
367 * We're storing a record; this function is given the existing record
368 * so that we can merge the information.
370 * @param cls closure, unused
371 * @param zone_key private key of the zone
372 * @param rec_name name that is being mapped (at most 255 characters long)
373 * @param rd_count number of entries in 'rd' array
374 * @param rd array of records with data to store
377 get_existing_record (void *cls,
378 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
379 const char *rec_name,
380 unsigned int rd_count,
381 const struct GNUNET_NAMESTORE_RecordData *rd)
383 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
384 struct GNUNET_NAMESTORE_RecordData *rde;
386 if ( (NULL != zone_key) &&
387 (0 != strcmp (rec_name, name)) )
389 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
392 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
393 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
394 /* FIXME: should add some logic to overwrite records if there
395 can only be one record of a particular type, and to check
396 if the combination of records is valid to begin with... */
399 rde->data_size = data_size;
400 rde->record_type = type;
401 if (1 != nonauthority)
402 rde->flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
404 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
405 GNUNET_assert (NULL != name);
406 add_qe = GNUNET_NAMESTORE_records_store (ns,
413 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
419 * Function called with the result from the check if the namestore
420 * service is actually running. If it is, we start the actual
423 * @param cls closure with our configuration
424 * @param result GNUNET_YES if the namestore service is running
427 testservice_task (void *cls,
430 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
431 struct GNUNET_CRYPTO_EccPublicKey pub;
432 struct GNUNET_NAMESTORE_RecordData rd;
434 if (GNUNET_YES != result)
436 FPRINTF (stderr, _("Service `%s' is not running\n"),
440 if (! (add|del|list|(NULL != uri)))
442 /* nothing more to be done */
444 _("No options given\n"));
447 GNUNET_CRYPTO_ecc_key_get_public (&zone_pkey,
450 ns = GNUNET_NAMESTORE_connect (cfg);
453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
454 _("Failed to connect to namestore\n"));
462 _("Missing option `%s' for operation `%s'\n"),
464 GNUNET_SCHEDULER_shutdown ();
468 if (NULL == typestring)
471 _("Missing option `%s' for operation `%s'\n"),
473 GNUNET_SCHEDULER_shutdown ();
477 type = GNUNET_NAMESTORE_typename_to_number (typestring);
478 if (UINT32_MAX == type)
480 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
481 GNUNET_SCHEDULER_shutdown ();
488 _("Missing option `%s' for operation `%s'\n"),
491 GNUNET_SCHEDULER_shutdown ();
495 GNUNET_NAMESTORE_string_to_value (type,
500 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
503 GNUNET_SCHEDULER_shutdown ();
507 if (NULL == expirationstring)
510 _("Missing option `%s' for operation `%s'\n"),
512 GNUNET_SCHEDULER_shutdown ();
516 if (0 == strcmp (expirationstring, "never"))
518 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
519 etime_is_rel = GNUNET_NO;
521 else if (GNUNET_OK ==
522 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
525 etime_is_rel = GNUNET_YES;
527 else if (GNUNET_OK ==
528 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
531 etime_is_rel = GNUNET_NO;
536 _("Invalid time format `%s'\n"),
538 GNUNET_SCHEDULER_shutdown ();
542 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
544 &get_existing_record,
552 _("Missing option `%s' for operation `%s'\n"),
554 GNUNET_SCHEDULER_shutdown ();
558 del_qe = GNUNET_NAMESTORE_records_store (ns,
567 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
576 struct GNUNET_CRYPTO_EccPublicKey pkey;
578 if ( (2 != (sscanf (uri,
579 "gnunet://gns/%104s/%63s",
583 GNUNET_CRYPTO_ecc_public_key_from_string (sh, strlen (sh), &pkey)) )
586 _("Invalid URI `%s'\n"),
588 GNUNET_SCHEDULER_shutdown ();
592 memset (&rd, 0, sizeof (rd));
594 rd.data_size = sizeof (struct GNUNET_CRYPTO_EccPublicKey);
595 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
596 if (GNUNET_YES == etime_is_rel)
598 rd.expiration_time = etime_rel.rel_value_us;
599 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
601 else if (GNUNET_NO == etime_is_rel)
602 rd.expiration_time = etime_abs.abs_value_us;
604 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
605 if (1 != nonauthority)
606 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
607 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
617 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
627 * Callback invoked from identity service with ego information.
628 * An @a ego of NULL means the ego was not found.
630 * @param cls closure with the configuration
631 * @param ego an ego known to identity service, or NULL
634 identity_cb (void *cls,
635 const struct GNUNET_IDENTITY_Ego *ego)
637 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
643 _("Ego `%s' not known to identity service\n"),
647 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
648 GNUNET_free (ego_name);
650 GNUNET_CLIENT_service_test ("namestore", cfg,
651 GNUNET_TIME_UNIT_SECONDS,
658 * Main function that will be run.
661 * @param args remaining command-line arguments
662 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
663 * @param cfg configuration
666 run (void *cls, char *const *args, const char *cfgfile,
667 const struct GNUNET_CONFIGURATION_Handle *cfg)
669 if (NULL == ego_name)
672 _("You must specify which zone should be accessed\n"));
675 if ( (NULL != args[0]) && (NULL == uri) )
676 uri = GNUNET_strdup (args[0]);
677 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
679 el = GNUNET_IDENTITY_ego_lookup (cfg,
687 * The main function for gnunet-namestore.
689 * @param argc number of arguments from the command line
690 * @param argv command line arguments
691 * @return 0 ok, 1 on error
694 main (int argc, char *const *argv)
699 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
701 gettext_noop ("add record"), 0,
702 &GNUNET_GETOPT_set_one, &add},
703 {'d', "delete", NULL,
704 gettext_noop ("delete record"), 0,
705 &GNUNET_GETOPT_set_one, &del},
706 {'D', "display", NULL,
707 gettext_noop ("display records"), 0,
708 &GNUNET_GETOPT_set_one, &list},
709 {'e', "expiration", "TIME",
710 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
711 &GNUNET_GETOPT_set_string, &expirationstring},
712 {'m', "monitor", NULL,
713 gettext_noop ("monitor changes in the namestore"), 0,
714 &GNUNET_GETOPT_set_one, &monitor},
715 {'n', "name", "NAME",
716 gettext_noop ("name of the record to add/delete/display"), 1,
717 &GNUNET_GETOPT_set_string, &name},
718 {'t', "type", "TYPE",
719 gettext_noop ("type of the record to add/delete/display"), 1,
720 &GNUNET_GETOPT_set_string, &typestring},
722 gettext_noop ("URI to import into our zone"), 1,
723 &GNUNET_GETOPT_set_string, &uri},
724 {'V', "value", "VALUE",
725 gettext_noop ("value of the record to add/delete"), 1,
726 &GNUNET_GETOPT_set_string, &value},
727 {'p', "public", NULL,
728 gettext_noop ("create or list public record"), 0,
729 &GNUNET_GETOPT_set_one, &public},
730 {'N', "non-authority", NULL,
731 gettext_noop ("create or list non-authority record"), 0,
732 &GNUNET_GETOPT_set_one, &nonauthority},
734 gettext_noop ("name of the ego controlling the zone"), 1,
735 &GNUNET_GETOPT_set_string, &ego_name},
736 GNUNET_GETOPT_OPTION_END
739 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
742 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
744 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
745 _("GNUnet zone manipulation tool"),
749 GNUNET_free ((void*) argv);
752 GNUNET_free ((void*) argv);
756 /* end of gnunet-namestore.c */