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-namecache.c
22 * @brief command line tool to inspect the name cache
23 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dnsparser_lib.h"
31 #include "gnunet_identity_service.h"
32 #include "gnunet_gnsrecord_lib.h"
33 #include "gnunet_namecache_service.h"
37 * Handle to the namecache.
39 static struct GNUNET_NAMECACHE_Handle *ns;
42 * Queue entry for the 'query' operation.
44 static struct GNUNET_NAMECACHE_QueueEntry *qe;
47 * Name (label) of the records to list.
52 * Public key of the zone to look in.
54 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
57 * Public key of the zone to look in, in ASCII.
68 * Task run on shutdown. Cleans up everything.
71 * @param tc scheduler context
74 do_shutdown (void *cls,
75 const struct GNUNET_SCHEDULER_TaskContext *tc)
79 GNUNET_NAMECACHE_cancel (qe);
84 GNUNET_NAMECACHE_disconnect (ns);
91 * Process a record that was stored in the namecache in a block.
93 * @param cls closure, NULL
94 * @param rd_len number of entries in @a rd array
95 * @param rd array of records with data to store
98 display_records_from_block (void *cls,
100 const struct GNUNET_GNSRECORD_Data *rd)
102 const char *typestring;
109 _("No records found for `%s'"),
116 for (i=0;i<rd_len;i++)
118 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
119 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
124 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
125 (unsigned int) rd[i].record_type);
134 FPRINTF (stdout, "%s", "\n");
139 * Display block obtained from listing (by name).
142 * @param block NULL if not found
145 handle_block (void *cls,
146 const struct GNUNET_GNSRECORD_Block *block)
152 "No matching block found\n");
154 else if (GNUNET_OK !=
155 GNUNET_GNSRECORD_block_decrypt (block,
158 &display_records_from_block,
162 "Failed to decrypt block!\n");
164 GNUNET_SCHEDULER_shutdown ();
169 * Main function that will be run.
172 * @param args remaining command-line arguments
173 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
174 * @param cfg configuration
177 run (void *cls, char *const *args, const char *cfgfile,
178 const struct GNUNET_CONFIGURATION_Handle *cfg)
180 struct GNUNET_HashCode dhash;
185 _("You must specify which zone should be accessed\n"));
190 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
195 _("Invalid public key for reverse lookup `%s'\n"),
197 GNUNET_SCHEDULER_shutdown ();
200 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
203 GNUNET_GNSRECORD_query_from_public_key (&pubkey,
206 qe = GNUNET_NAMECACHE_lookup_block (ns,
214 * The main function for gnunet-namecache.
216 * @param argc number of arguments from the command line
217 * @param argv command line arguments
218 * @return 0 ok, 1 on error
221 main (int argc, char *const *argv)
223 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
224 {'n', "name", "NAME",
225 gettext_noop ("name of the record to add/delete/display"), 1,
226 &GNUNET_GETOPT_set_string, &name},
227 {'z', "zone", "PKEY",
228 gettext_noop ("spezifies the public key of the zone to look in"), 1,
229 &GNUNET_GETOPT_set_string, &pkey},
230 GNUNET_GETOPT_OPTION_END
233 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
236 GNUNET_log_setup ("gnunet-namecache", "WARNING", NULL);
238 GNUNET_PROGRAM_run (argc, argv, "gnunet-namecache",
239 _("GNUnet zone manipulation tool"),
243 GNUNET_free ((void*) argv);
246 GNUNET_free ((void*) argv);
250 /* end of gnunet-namecache.c */