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
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 lookup.
50 static struct GNUNET_IDENTITY_EgoLookup *el;
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.
93 * Is record public (opposite of #GNUNET_NAMESTORE_RF_PRIVATE)
98 * Is record a shadow record (#GNUNET_NAMESTORE_RF_SHADOW_RECORD)
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_IDENTITY_ego_lookup_cancel (el);
195 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
200 GNUNET_NAMESTORE_cancel (add_qe);
203 if (NULL != add_qe_uri)
205 GNUNET_NAMESTORE_cancel (add_qe_uri);
210 GNUNET_NAMESTORE_cancel (del_qe);
215 GNUNET_NAMESTORE_disconnect (ns);
218 memset (&zone_pkey, 0, sizeof (zone_pkey));
226 GNUNET_NAMESTORE_zone_monitor_stop (zm);
238 * Continuation called to notify client about result of the
241 * @param cls closure, location of the QueueEntry pointer to NULL out
242 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
243 * #GNUNET_NO if content was already there
244 * #GNUNET_YES (or other positive value) on success
245 * @param emsg NULL on success, otherwise an error message
248 add_continuation (void *cls,
252 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
255 if (GNUNET_YES != success)
258 _("Adding record failed: %s\n"),
259 (GNUNET_NO == success) ? "record exists" : emsg);
260 if (GNUNET_NO != success)
263 if ( (NULL == add_qe) &&
264 (NULL == add_qe_uri) &&
267 GNUNET_SCHEDULER_shutdown ();
272 * Continuation called to notify client about result of the
275 * @param cls closure, unused
276 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
277 * #GNUNET_NO if content was already there
278 * #GNUNET_YES (or other positive value) on success
279 * @param emsg NULL on success, otherwise an error message
282 del_continuation (void *cls,
287 if (success != GNUNET_YES)
289 _("Deleting record failed: %s\n"),
291 if ( (NULL == add_qe) &&
292 (NULL == add_qe_uri) &&
294 GNUNET_SCHEDULER_shutdown ();
299 * Process a record that was stored in the namestore.
302 * @param zone_key private key of the zone
303 * @param name name that is being mapped (at most 255 characters long)
304 * @param rd_len number of entries in @a rd array
305 * @param rd array of records with data to store
308 display_record (void *cls,
309 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
312 const struct GNUNET_NAMESTORE_RecordData *rd)
314 const char *typestring;
321 if ( (NULL == del_qe) &&
322 (NULL == add_qe_uri) &&
324 GNUNET_SCHEDULER_shutdown ();
330 for (i=0;i<rd_len;i++)
332 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
333 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
338 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
339 (unsigned int) rd[i].record_type);
348 FPRINTF (stdout, "%s", "\n");
349 GNUNET_NAMESTORE_zone_iterator_next (list_it);
354 * Function called once we are in sync in monitor mode.
361 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
366 * We're storing a record; this function is given the existing record
367 * so that we can merge the information.
369 * @param cls closure, unused
370 * @param zone_key private key of the zone
371 * @param rec_name name that is being mapped (at most 255 characters long)
372 * @param rd_count number of entries in @a rd array
373 * @param rd array of records with data to store
376 get_existing_record (void *cls,
377 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
378 const char *rec_name,
379 unsigned int rd_count,
380 const struct GNUNET_NAMESTORE_RecordData *rd)
382 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
383 struct GNUNET_NAMESTORE_RecordData *rde;
385 if ( (NULL != zone_key) &&
386 (0 != strcmp (rec_name, name)) )
388 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
391 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
392 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
393 /* FIXME: should add some logic to overwrite records if there
394 can only be one record of a particular type, and to check
395 if the combination of records is valid to begin with... */
398 rde->data_size = data_size;
399 rde->record_type = type;
401 rde->flags |= GNUNET_NAMESTORE_RF_SHADOW_RECORD;
403 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
404 GNUNET_assert (NULL != name);
405 add_qe = GNUNET_NAMESTORE_records_store (ns,
412 /* only cancel if we were not told that this
413 was the end of the iteration already */
414 if (NULL != rec_name)
415 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
421 * Function called with the result from the check if the namestore
422 * service is actually running. If it is, we start the actual
425 * @param cls closure with our configuration
426 * @param result #GNUNET_YES if the namestore service is running
429 testservice_task (void *cls,
432 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
433 struct GNUNET_CRYPTO_EccPublicKey pub;
434 struct GNUNET_NAMESTORE_RecordData rd;
436 if (GNUNET_YES != result)
438 FPRINTF (stderr, _("Service `%s' is not running\n"),
442 if (! (add|del|list|(NULL != uri)))
444 /* nothing more to be done */
446 _("No options given\n"));
449 GNUNET_CRYPTO_ecc_key_get_public (&zone_pkey,
452 ns = GNUNET_NAMESTORE_connect (cfg);
455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
456 _("Failed to connect to namestore\n"));
464 _("Missing option `%s' for operation `%s'\n"),
466 GNUNET_SCHEDULER_shutdown ();
470 if (NULL == typestring)
473 _("Missing option `%s' for operation `%s'\n"),
475 GNUNET_SCHEDULER_shutdown ();
479 type = GNUNET_NAMESTORE_typename_to_number (typestring);
480 if (UINT32_MAX == type)
482 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
483 GNUNET_SCHEDULER_shutdown ();
490 _("Missing option `%s' for operation `%s'\n"),
493 GNUNET_SCHEDULER_shutdown ();
497 GNUNET_NAMESTORE_string_to_value (type,
502 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
505 GNUNET_SCHEDULER_shutdown ();
509 if (NULL == expirationstring)
512 _("Missing option `%s' for operation `%s'\n"),
514 GNUNET_SCHEDULER_shutdown ();
518 if (0 == strcmp (expirationstring, "never"))
520 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
521 etime_is_rel = GNUNET_NO;
523 else if (GNUNET_OK ==
524 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
527 etime_is_rel = GNUNET_YES;
529 else if (GNUNET_OK ==
530 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
533 etime_is_rel = GNUNET_NO;
538 _("Invalid time format `%s'\n"),
540 GNUNET_SCHEDULER_shutdown ();
544 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
546 &get_existing_record,
554 _("Missing option `%s' for operation `%s'\n"),
556 GNUNET_SCHEDULER_shutdown ();
560 del_qe = GNUNET_NAMESTORE_records_store (ns,
569 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
578 struct GNUNET_CRYPTO_EccPublicKey pkey;
580 if ( (2 != (sscanf (uri,
581 "gnunet://gns/%104s/%63s",
585 GNUNET_CRYPTO_ecc_public_key_from_string (sh, strlen (sh), &pkey)) )
588 _("Invalid URI `%s'\n"),
590 GNUNET_SCHEDULER_shutdown ();
594 memset (&rd, 0, sizeof (rd));
596 rd.data_size = sizeof (struct GNUNET_CRYPTO_EccPublicKey);
597 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
598 if (GNUNET_YES == etime_is_rel)
600 rd.expiration_time = etime_rel.rel_value_us;
601 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
603 else if (GNUNET_NO == etime_is_rel)
604 rd.expiration_time = etime_abs.abs_value_us;
606 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
608 rd.flags |= GNUNET_NAMESTORE_RF_SHADOW_RECORD;
609 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
619 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
629 * Callback invoked from identity service with ego information.
630 * An @a ego of NULL means the ego was not found.
632 * @param cls closure with the configuration
633 * @param ego an ego known to identity service, or NULL
636 identity_cb (void *cls,
637 const struct GNUNET_IDENTITY_Ego *ego)
639 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
645 _("Ego `%s' not known to identity service\n"),
649 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
650 GNUNET_free (ego_name);
652 GNUNET_CLIENT_service_test ("namestore", cfg,
653 GNUNET_TIME_UNIT_SECONDS,
660 * Main function that will be run.
663 * @param args remaining command-line arguments
664 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
665 * @param cfg configuration
668 run (void *cls, char *const *args, const char *cfgfile,
669 const struct GNUNET_CONFIGURATION_Handle *cfg)
671 if (NULL == ego_name)
674 _("You must specify which zone should be accessed\n"));
677 if ( (NULL != args[0]) && (NULL == uri) )
678 uri = GNUNET_strdup (args[0]);
679 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
681 el = GNUNET_IDENTITY_ego_lookup (cfg,
689 * The main function for gnunet-namestore.
691 * @param argc number of arguments from the command line
692 * @param argv command line arguments
693 * @return 0 ok, 1 on error
696 main (int argc, char *const *argv)
700 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
702 gettext_noop ("add record"), 0,
703 &GNUNET_GETOPT_set_one, &add},
704 {'d', "delete", NULL,
705 gettext_noop ("delete record"), 0,
706 &GNUNET_GETOPT_set_one, &del},
707 {'D', "display", NULL,
708 gettext_noop ("display records"), 0,
709 &GNUNET_GETOPT_set_one, &list},
710 {'e', "expiration", "TIME",
711 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
712 &GNUNET_GETOPT_set_string, &expirationstring},
713 {'m', "monitor", NULL,
714 gettext_noop ("monitor changes in the namestore"), 0,
715 &GNUNET_GETOPT_set_one, &monitor},
716 {'n', "name", "NAME",
717 gettext_noop ("name of the record to add/delete/display"), 1,
718 &GNUNET_GETOPT_set_string, &name},
719 {'t', "type", "TYPE",
720 gettext_noop ("type of the record to add/delete/display"), 1,
721 &GNUNET_GETOPT_set_string, &typestring},
723 gettext_noop ("URI to import into our zone"), 1,
724 &GNUNET_GETOPT_set_string, &uri},
725 {'V', "value", "VALUE",
726 gettext_noop ("value of the record to add/delete"), 1,
727 &GNUNET_GETOPT_set_string, &value},
728 {'p', "public", NULL,
729 gettext_noop ("create or list public record"), 0,
730 &GNUNET_GETOPT_set_one, &public},
731 {'s', "shadow", NULL,
732 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
733 &GNUNET_GETOPT_set_one, &shadow},
735 gettext_noop ("name of the ego controlling the zone"), 1,
736 &GNUNET_GETOPT_set_string, &ego_name},
737 GNUNET_GETOPT_OPTION_END
740 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
743 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
745 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
746 _("GNUnet zone manipulation tool"),
750 GNUNET_free ((void*) argv);
753 GNUNET_free ((void*) argv);
757 /* end of gnunet-namestore.c */