2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file gnunet-namestore.c
20 * @brief command line tool to manipulate the local zone
21 * @author Christian Grothoff
27 #include <gnunet_util_lib.h>
28 #include <gnunet_dnsparser_lib.h>
29 #include <gnunet_identity_service.h>
30 #include <gnunet_gnsrecord_lib.h>
31 #include <gnunet_gns_service.h>
32 #include <gnunet_namestore_service.h>
36 * Entry in record set for bulk processing.
41 * Kept in a linked list.
43 struct RecordSetEntry *next;
46 * The record to add/remove.
48 struct GNUNET_GNSRECORD_Data record;
54 * Handle to the namestore.
56 static struct GNUNET_NAMESTORE_Handle *ns;
59 * Private key for the our zone.
61 static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
64 * Handle to identity lookup.
66 static struct GNUNET_IDENTITY_EgoLookup *el;
69 * Identity service handle
71 static struct GNUNET_IDENTITY_Handle *idh;
76 struct GNUNET_IDENTITY_Operation *get_default;
79 * Name of the ego controlling the zone.
81 static char *ego_name;
84 * Desired action is to add a record.
89 * Queue entry for the 'add-uri' operation.
91 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
94 * Queue entry for the 'add' operation.
96 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
99 * Queue entry for the 'lookup' operation.
101 static struct GNUNET_NAMESTORE_QueueEntry *get_qe;
104 * Queue entry for the 'reverse lookup' operation (in combination with a name).
106 static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
109 * Desired action is to list records.
114 * List iterator for the 'list' operation.
116 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
119 * Desired action is to remove a record.
124 * Is record public (opposite of #GNUNET_GNSRECORD_RF_PRIVATE)
126 static int is_public;
129 * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
131 static int is_shadow;
134 * Queue entry for the 'del' operation.
136 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
139 * Queue entry for the 'set/replace' operation.
141 static struct GNUNET_NAMESTORE_QueueEntry *set_qe;
144 * Name of the records to add/list/remove.
149 * Value of the record to add/remove.
159 * Reverse lookup to perform.
161 static char *reverse_pkey;
164 * Type of the record to add/remove, NULL to remove all.
166 static char *typestring;
169 * Desired expiration time.
171 static char *expirationstring;
176 static char *nickstring;
179 * Global return value
184 * Type string converted to DNS type value.
186 static uint32_t type;
189 * Value in binary format.
194 * Number of bytes in #data.
196 static size_t data_size;
199 * Expiration string converted to numeric value.
201 static uint64_t etime;
204 * Is expiration time relative or absolute time?
206 static int etime_is_rel = GNUNET_SYSERR;
211 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
214 * Enables monitor mode.
219 * Entry in record set for processing records in bulk.
221 static struct RecordSetEntry *recordset;
225 * Task run on shutdown. Cleans up everything.
230 do_shutdown (void *cls)
233 if (NULL != get_default)
235 GNUNET_IDENTITY_cancel (get_default);
240 GNUNET_IDENTITY_disconnect (idh);
245 GNUNET_IDENTITY_ego_lookup_cancel (el);
250 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
255 GNUNET_NAMESTORE_cancel (add_qe);
260 GNUNET_NAMESTORE_cancel (set_qe);
263 if (NULL != add_qe_uri)
265 GNUNET_NAMESTORE_cancel (add_qe_uri);
270 GNUNET_NAMESTORE_cancel (get_qe);
275 GNUNET_NAMESTORE_cancel (del_qe);
280 GNUNET_NAMESTORE_disconnect (ns);
283 memset (&zone_pkey, 0, sizeof (zone_pkey));
291 GNUNET_NAMESTORE_zone_monitor_stop (zm);
303 * Check if we are finished, and if so, perform shutdown.
308 if ( (NULL == add_qe) &&
309 (NULL == add_qe_uri) &&
312 (NULL == reverse_qe) &&
314 GNUNET_SCHEDULER_shutdown ();
319 * Continuation called to notify client about result of the
322 * @param cls closure, location of the QueueEntry pointer to NULL out
323 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
324 * #GNUNET_NO if content was already there
325 * #GNUNET_YES (or other positive value) on success
326 * @param emsg NULL on success, otherwise an error message
329 add_continuation (void *cls,
333 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
336 if (GNUNET_YES != success)
339 _("Adding record failed: %s\n"),
340 (GNUNET_NO == success) ? "record exists" : emsg);
341 if (GNUNET_NO != success)
350 * Continuation called to notify client about result of the
353 * @param cls closure, unused
354 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
355 * #GNUNET_NO if content was already there
356 * #GNUNET_YES (or other positive value) on success
357 * @param emsg NULL on success, otherwise an error message
360 del_continuation (void *cls,
366 if (GNUNET_NO == success)
369 _("Deleting record failed, record does not exist%s%s\n"),
370 (NULL != emsg) ? ": " : "",
371 (NULL != emsg) ? emsg : "");
373 if (GNUNET_SYSERR == success)
376 _("Deleting record failed%s%s\n"),
377 (NULL != emsg) ? ": " : "",
378 (NULL != emsg) ? emsg : "");
385 * Function called when we are done with a zone iteration.
388 zone_iteration_finished (void *cls)
397 * Function called when we encountered an error in a zone iteration.
400 zone_iteration_error_cb (void *cls)
405 "Error iterating over zone\n");
412 * Process a record that was stored in the namestore.
414 * @param rname name that is being mapped (at most 255 characters long)
415 * @param rd_len number of entries in @a rd array
416 * @param rd array of records with data to store
419 display_record (const char *rname,
421 const struct GNUNET_GNSRECORD_Data *rd)
423 const char *typestring;
426 struct GNUNET_TIME_Absolute at;
427 struct GNUNET_TIME_Relative rt;
429 if ( (NULL != name) &&
430 (0 != strcmp (name, rname)) )
432 GNUNET_NAMESTORE_zone_iterator_next (list_it,
439 for (unsigned int i=0;i<rd_len;i++)
441 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
443 GNUNET_GNS_EMPTY_LABEL_AT)) )
445 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
446 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
452 _("\tCorrupt or unsupported record of type %u\n"),
453 (unsigned int) rd[i].record_type);
456 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
458 rt.rel_value_us = rd[i].expiration_time;
459 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
463 at.abs_value_us = rd[i].expiration_time;
464 ets = GNUNET_STRINGS_absolute_time_to_string (at);
467 "\t%s: %s (%s)\t%s\t%s\n",
471 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
472 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "");
475 FPRINTF (stdout, "%s", "\n");
480 * Process a record that was stored in the namestore.
483 * @param zone_key private key of the zone
484 * @param rname name that is being mapped (at most 255 characters long)
485 * @param rd_len number of entries in @a rd array
486 * @param rd array of records with data to store
489 display_record_iterator (void *cls,
490 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
493 const struct GNUNET_GNSRECORD_Data *rd)
497 display_record (rname,
500 GNUNET_NAMESTORE_zone_iterator_next (list_it,
506 * Process a record that was stored in the namestore.
509 * @param zone_key private key of the zone
510 * @param rname name that is being mapped (at most 255 characters long)
511 * @param rd_len number of entries in @a rd array
512 * @param rd array of records with data to store
515 display_record_monitor (void *cls,
516 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
519 const struct GNUNET_GNSRECORD_Data *rd)
523 display_record (rname,
526 GNUNET_NAMESTORE_zone_monitor_next (zm,
532 * Process a record that was stored in the namestore.
535 * @param zone_key private key of the zone
536 * @param rname name that is being mapped (at most 255 characters long)
537 * @param rd_len number of entries in @a rd array
538 * @param rd array of records with data to store
541 display_record_lookup (void *cls,
542 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
545 const struct GNUNET_GNSRECORD_Data *rd)
550 display_record (rname,
558 * Function called once we are in sync in monitor mode.
568 "Monitor is now in sync.\n");
573 * Function called on errors while monitoring.
578 monitor_error_cb (void *cls)
583 "Monitor disconnected and out of sync.\n");
588 * Function called on errors while monitoring.
593 lookup_error_cb (void *cls)
599 "Failed to lookup record.\n");
605 * Function called if lookup fails.
608 add_error_cb (void *cls)
619 * We're storing a record; this function is given the existing record
620 * so that we can merge the information.
622 * @param cls closure, unused
623 * @param zone_key private key of the zone
624 * @param rec_name name that is being mapped (at most 255 characters long)
625 * @param rd_count number of entries in @a rd array
626 * @param rd array of records with data to store
629 get_existing_record (void *cls,
630 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
631 const char *rec_name,
632 unsigned int rd_count,
633 const struct GNUNET_GNSRECORD_Data *rd)
635 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
636 struct GNUNET_GNSRECORD_Data *rde;
641 if (0 != strcmp (rec_name, name))
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Received %u records for name `%s'\n",
652 for (unsigned int i=0;i<rd_count;i++)
654 switch (rd[i].record_type)
656 case GNUNET_DNSPARSER_TYPE_CNAME:
658 _("A %s record exists already under `%s', no other records can be added.\n"),
664 case GNUNET_GNSRECORD_TYPE_PKEY:
666 _("A %s record exists already under `%s', no other records can be added.\n"),
676 case GNUNET_DNSPARSER_TYPE_CNAME:
680 _("Records already exist under `%s', cannot add `%s' record.\n"),
688 case GNUNET_GNSRECORD_TYPE_PKEY:
692 _("Records already exist under `%s', cannot add `%s' record.\n"),
700 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
701 for (unsigned int i=0;i<rd_count;i++)
702 if (GNUNET_GNSRECORD_TYPE_GNS2DNS != rd[i].record_type)
705 _("Non-GNS2DNS records already exist under `%s', cannot add GNS2DNS record.\n"),
715 sizeof (struct GNUNET_GNSRECORD_Data));
716 GNUNET_memcpy (&rdn[1],
718 rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
721 rde->data_size = data_size;
722 rde->record_type = type;
724 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
726 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
727 rde->expiration_time = etime;
728 if (GNUNET_YES == etime_is_rel)
729 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
730 else if (GNUNET_NO != etime_is_rel)
731 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
732 GNUNET_assert (NULL != name);
733 add_qe = GNUNET_NAMESTORE_records_store (ns,
744 * Function called if we encountered an error in zone-to-name.
747 reverse_error_cb (void *cls)
758 * Function called with the result of our attempt to obtain a name for a given
762 * @param zone private key of the zone; NULL on disconnect
763 * @param label label of the records; NULL on disconnect
764 * @param rd_count number of entries in @a rd array, 0 if label was deleted
765 * @param rd array of records with data to store
768 handle_reverse_lookup (void *cls,
769 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
771 unsigned int rd_count,
772 const struct GNUNET_GNSRECORD_Data *rd)
793 * Function called if lookup for deletion fails.
796 del_lookup_error_cb (void *cls)
807 * We were asked to delete something; this function is called with
808 * the existing records. Now we should determine what should be
809 * deleted and then issue the deletion operation.
812 * @param zone private key of the zone we are deleting from
813 * @param label name of the records we are editing
814 * @param rd_count size of the @a rd array
815 * @param rd existing records
818 del_monitor (void *cls,
819 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
821 unsigned int rd_count,
822 const struct GNUNET_GNSRECORD_Data *rd)
824 struct GNUNET_GNSRECORD_Data rdx[rd_count];
825 unsigned int rd_left;
835 _("There are no records under label `%s' that could be deleted.\n"),
841 if ( (NULL == value) &&
842 (NULL == typestring) )
844 /* delete everything */
845 del_qe = GNUNET_NAMESTORE_records_store (ns,
855 if (NULL != typestring)
856 type = GNUNET_GNSRECORD_typename_to_number (typestring);
858 type = GNUNET_GNSRECORD_TYPE_ANY;
859 for (unsigned int i=0;i<rd_count;i++)
862 if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
863 (rd[i].record_type == type) ) &&
865 (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
867 rd[i].data_size)))) ||
868 (0 == strcmp (vs, value)) ) ) )
869 rdx[rd_left++] = rd[i];
870 GNUNET_free_non_null (vs);
872 if (rd_count == rd_left)
874 /* nothing got deleted */
876 _("There are no records under label `%s' that match the request for deletion.\n"),
881 /* delete everything but what we copied to 'rdx' */
882 del_qe = GNUNET_NAMESTORE_records_store (ns,
893 * Parse expiration time.
895 * @param expirationstring text to parse
896 * @param etime_is_rel[out] set to #GNUNET_YES if time is relative
897 * @param etime[out] set to expiration time (abs or rel)
898 * @return #GNUNET_OK on success
901 parse_expiration (const char *expirationstring,
905 struct GNUNET_TIME_Relative etime_rel;
906 struct GNUNET_TIME_Absolute etime_abs;
908 if (0 == strcmp (expirationstring,
911 *etime = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
912 *etime_is_rel = GNUNET_NO;
916 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
919 *etime_is_rel = GNUNET_YES;
920 *etime = etime_rel.rel_value_us;
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Storing record with relative expiration time of %s\n",
923 GNUNET_STRINGS_relative_time_to_string (etime_rel,
928 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
931 *etime_is_rel = GNUNET_NO;
932 *etime = etime_abs.abs_value_us;
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Storing record with absolute expiration time of %s\n",
935 GNUNET_STRINGS_absolute_time_to_string (etime_abs));
938 return GNUNET_SYSERR;
943 * Function called when namestore is done with the replace
947 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
948 * #GNUNET_NO if content was already there or not found
949 * #GNUNET_YES (or other positive value) on success
950 * @param emsg NULL on success, otherwise an error message
953 replace_cont (void *cls,
960 if (GNUNET_OK != success)
962 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
963 _("Failed to replace records: %s\n"),
965 ret = 1; /* fail from 'main' */
967 GNUNET_SCHEDULER_shutdown ();
972 * Callback invoked from identity service with ego information.
973 * An @a ego of NULL means the ego was not found.
975 * @param cls closure with the configuration
976 * @param ego an ego known to identity service, or NULL
979 identity_cb (void *cls,
980 const struct GNUNET_IDENTITY_Ego *ego)
982 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
983 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
984 struct GNUNET_GNSRECORD_Data rd;
989 if (NULL != ego_name)
992 _("Ego `%s' not known to identity service\n"),
995 GNUNET_SCHEDULER_shutdown ();
999 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
1000 GNUNET_free_non_null (ego_name);
1003 if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey))|(NULL != recordset) )
1005 /* nothing more to be done */
1007 _("No options given\n"));
1008 GNUNET_SCHEDULER_shutdown ();
1011 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
1013 ns = GNUNET_NAMESTORE_connect (cfg);
1016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1017 _("Failed to connect to namestore\n"));
1021 if (NULL != recordset)
1023 /* replace entire record set */
1024 unsigned int rd_count;
1025 struct GNUNET_GNSRECORD_Data *rd;
1030 _("Missing option `%s' for operation `%s'\n"),
1031 "-n", _("replace"));
1032 GNUNET_SCHEDULER_shutdown ();
1037 for (struct RecordSetEntry *e = recordset; NULL != e; e = e->next)
1039 rd = GNUNET_new_array (rd_count,
1040 struct GNUNET_GNSRECORD_Data);
1042 for (struct RecordSetEntry *e = recordset; NULL != e; e = e->next)
1044 rd[rd_count] = e->record;
1047 set_qe = GNUNET_NAMESTORE_records_store (ns,
1063 _("Missing option `%s' for operation `%s'\n"),
1065 GNUNET_SCHEDULER_shutdown ();
1069 if (NULL == typestring)
1072 _("Missing option `%s' for operation `%s'\n"),
1074 GNUNET_SCHEDULER_shutdown ();
1078 type = GNUNET_GNSRECORD_typename_to_number (typestring);
1079 if (UINT32_MAX == type)
1082 _("Unsupported type `%s'\n"),
1084 GNUNET_SCHEDULER_shutdown ();
1091 _("Missing option `%s' for operation `%s'\n"),
1094 GNUNET_SCHEDULER_shutdown ();
1098 GNUNET_GNSRECORD_string_to_value (type,
1104 _("Value `%s' invalid for record type `%s'\n"),
1107 GNUNET_SCHEDULER_shutdown ();
1111 if (NULL == expirationstring)
1114 _("Missing option `%s' for operation `%s'\n"),
1117 GNUNET_SCHEDULER_shutdown ();
1122 parse_expiration (expirationstring,
1127 _("Invalid time format `%s'\n"),
1129 GNUNET_SCHEDULER_shutdown ();
1133 add_qe = GNUNET_NAMESTORE_records_lookup (ns,
1138 &get_existing_record,
1146 _("Missing option `%s' for operation `%s'\n"),
1148 GNUNET_SCHEDULER_shutdown ();
1152 del_qe = GNUNET_NAMESTORE_records_lookup (ns,
1155 &del_lookup_error_cb,
1163 get_qe = GNUNET_NAMESTORE_records_lookup (ns,
1168 &display_record_lookup,
1171 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
1173 &zone_iteration_error_cb,
1175 &display_record_iterator,
1177 &zone_iteration_finished,
1180 if (NULL != reverse_pkey)
1182 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1185 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
1186 strlen (reverse_pkey),
1190 _("Invalid public key for reverse lookup `%s'\n"),
1192 GNUNET_SCHEDULER_shutdown ();
1194 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
1199 &handle_reverse_lookup,
1206 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1208 GNUNET_STRINGS_utf8_tolower (uri, uri);
1209 if ( (2 != (sscanf (uri,
1210 "gnunet://gns/%52s/%63s",
1214 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh,
1219 _("Invalid URI `%s'\n"),
1221 GNUNET_SCHEDULER_shutdown ();
1225 memset (&rd, 0, sizeof (rd));
1227 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1228 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
1229 rd.expiration_time = etime;
1230 if (GNUNET_YES == etime_is_rel)
1231 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1233 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
1234 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
1242 if (NULL != nickstring)
1244 if (0 == strlen(nickstring))
1247 _("Invalid nick `%s'\n"),
1249 GNUNET_SCHEDULER_shutdown ();
1253 add_qe_uri = GNUNET_NAMESTORE_set_nick (ns,
1261 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
1266 &display_record_monitor,
1275 default_ego_cb (void *cls,
1276 struct GNUNET_IDENTITY_Ego *ego,
1287 _("No default ego configured in identity service\n"));
1288 GNUNET_SCHEDULER_shutdown ();
1294 identity_cb (cls, ego);
1300 id_connect_cb (void *cls,
1301 struct GNUNET_IDENTITY_Ego *ego,
1305 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1312 get_default = GNUNET_IDENTITY_get (idh,
1321 * Main function that will be run.
1323 * @param cls closure
1324 * @param args remaining command-line arguments
1325 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1326 * @param cfg configuration
1331 const char *cfgfile,
1332 const struct GNUNET_CONFIGURATION_Handle *cfg)
1337 if (NULL != args[0])
1338 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1339 _("Superfluous command line arguments (starting with `%s') ignored\n"),
1341 if ( (NULL != args[0]) &&
1343 uri = GNUNET_strdup (args[0]);
1345 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
1348 if (NULL == ego_name)
1350 idh = GNUNET_IDENTITY_connect (cfg,
1355 _("Cannot connect to identity service\n"));
1359 el = GNUNET_IDENTITY_ego_lookup (cfg,
1367 * Command-line option parser function that allows the user to specify
1368 * a complete record as one argument for adding/removing. A pointer
1369 * to the head of the list of record sets must be passed as the "scls"
1372 * @param ctx command line processor context
1373 * @param scls must be of type "struct GNUNET_FS_Uri **"
1374 * @param option name of the option (typically 'R')
1375 * @param value command line argument given; format is
1376 * "TTL TYPE FLAGS VALUE" where TTL is an expiration time (rel or abs),
1377 * TYPE is a DNS/GNS record type, FLAGS is either "n" for no flags or
1378 * a combination of 's' (shadow) and 'p' (public) and VALUE is the
1379 * value (in human-readable format)
1380 * @return #GNUNET_OK on success
1383 multirecord_process (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
1388 struct RecordSetEntry **head = scls;
1389 struct RecordSetEntry *r;
1390 struct GNUNET_GNSRECORD_Data record;
1398 cp = GNUNET_strdup (value);
1399 tok = strtok (cp, " ");
1402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1403 _("Empty record line argument is not allowed.\n"));
1405 return GNUNET_SYSERR;
1408 parse_expiration (tok,
1410 &record.expiration_time))
1412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1413 _("Invalid expiration time `%s'\n"),
1416 return GNUNET_SYSERR;
1418 tok = strtok (NULL, " ");
1421 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1422 _("Missing entries in record line `%s'.\n"),
1425 return GNUNET_SYSERR;
1427 record.record_type = GNUNET_GNSRECORD_typename_to_number (tok);
1428 if (UINT32_MAX == record.record_type)
1430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1431 _("Unknown record type `%s'\n"),
1434 return GNUNET_SYSERR;
1436 tok = strtok (NULL, " ");
1439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1440 _("Missing entries in record line `%s'.\n"),
1443 return GNUNET_SYSERR;
1445 record.flags = GNUNET_GNSRECORD_RF_NONE;
1447 record.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1448 if (NULL == strchr (tok, (unsigned char) 'p')) /* p = public */
1449 record.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1450 if (NULL != strchr (tok, (unsigned char) 's'))
1451 record.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
1452 /* find beginning of record value */
1453 tok = strchr (&value[tok - cp], (unsigned char) ' ');
1456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1457 _("Missing entries in record line `%s'.\n"),
1460 return GNUNET_SYSERR;
1463 tok++; /* skip space */
1465 GNUNET_GNSRECORD_string_to_value (record.record_type,
1470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1471 _("Invalid record data for type %s: `%s'.\n"),
1472 GNUNET_GNSRECORD_number_to_typename (record.record_type),
1474 return GNUNET_SYSERR;
1477 r = GNUNET_malloc (sizeof (struct RecordSetEntry) + record.data_size);
1479 record.data = &r[1];
1483 GNUNET_free (raw_data);
1491 * Allow user to specify keywords.
1493 * @param shortName short name of the option
1494 * @param name long name of the option
1495 * @param argumentHelp help text for the option argument
1496 * @param description long help text for the option
1497 * @param[out] topKeywords set to the desired value
1499 struct GNUNET_GETOPT_CommandLineOption
1500 multirecord_option (char shortName,
1502 const char *argumentHelp,
1503 const char *description,
1504 struct RecordSetEntry **rs)
1506 struct GNUNET_GETOPT_CommandLineOption clo = {
1507 .shortName = shortName,
1509 .argumentHelp = argumentHelp,
1510 .description = description,
1511 .require_argument = 1,
1512 .processor = &multirecord_process,
1522 * The main function for gnunet-namestore.
1524 * @param argc number of arguments from the command line
1525 * @param argv command line arguments
1526 * @return 0 ok, 1 on error
1532 struct GNUNET_GETOPT_CommandLineOption options[] = {
1533 GNUNET_GETOPT_option_flag ('a',
1535 gettext_noop ("add record"),
1537 GNUNET_GETOPT_option_flag ('d',
1539 gettext_noop ("delete record"),
1541 GNUNET_GETOPT_option_flag ('D',
1543 gettext_noop ("display records"),
1545 GNUNET_GETOPT_option_string ('e',
1548 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"),
1550 GNUNET_GETOPT_option_string ('i',
1553 gettext_noop ("set the desired nick name for the zone"),
1555 GNUNET_GETOPT_option_flag ('m',
1557 gettext_noop ("monitor changes in the namestore"),
1559 GNUNET_GETOPT_option_string ('n',
1562 gettext_noop ("name of the record to add/delete/display"),
1564 GNUNET_GETOPT_option_string ('r',
1567 gettext_noop ("determine our name for the given PKEY"),
1569 multirecord_option ('R',
1572 gettext_noop ("set record set to values given by (possibly multiple) RECORDLINES; can be specified multiple times"),
1574 GNUNET_GETOPT_option_string ('t',
1577 gettext_noop ("type of the record to add/delete/display"),
1579 GNUNET_GETOPT_option_string ('u',
1582 gettext_noop ("URI to import into our zone"),
1584 GNUNET_GETOPT_option_string ('V',
1587 gettext_noop ("value of the record to add/delete"),
1589 GNUNET_GETOPT_option_flag ('p',
1591 gettext_noop ("create or list public record"),
1593 GNUNET_GETOPT_option_flag ('s',
1595 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"),
1597 GNUNET_GETOPT_option_string ('z',
1600 gettext_noop ("name of the ego controlling the zone"),
1602 GNUNET_GETOPT_OPTION_END
1606 GNUNET_STRINGS_get_utf8_args (argc, argv,
1612 GNUNET_log_setup ("gnunet-namestore",
1616 GNUNET_PROGRAM_run (argc,
1619 _("GNUnet zone manipulation tool"),
1623 GNUNET_free ((void*) argv);
1624 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1627 GNUNET_free ((void*) argv);
1628 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1632 /* end of gnunet-namestore.c */