2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013 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.
16 * @file gnunet-namecache.c
17 * @brief command line tool to inspect the name cache
18 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_dnsparser_lib.h"
26 #include "gnunet_identity_service.h"
27 #include "gnunet_gnsrecord_lib.h"
28 #include "gnunet_namecache_service.h"
32 * Handle to the namecache.
34 static struct GNUNET_NAMECACHE_Handle *ns;
37 * Queue entry for the 'query' operation.
39 static struct GNUNET_NAMECACHE_QueueEntry *qe;
42 * Name (label) of the records to list.
47 * Public key of the zone to look in.
49 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
52 * Public key of the zone to look in, in ASCII.
63 * Task run on shutdown. Cleans up everything.
68 do_shutdown (void *cls)
72 GNUNET_NAMECACHE_cancel (qe);
77 GNUNET_NAMECACHE_disconnect (ns);
84 * Process a record that was stored in the namecache in a block.
86 * @param cls closure, NULL
87 * @param rd_len number of entries in @a rd array
88 * @param rd array of records with data to store
91 display_records_from_block (void *cls,
93 const struct GNUNET_GNSRECORD_Data *rd)
95 const char *typestring;
102 _("No records found for `%s'"),
109 for (i=0;i<rd_len;i++)
111 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
112 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
117 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
118 (unsigned int) rd[i].record_type);
127 FPRINTF (stdout, "%s", "\n");
132 * Display block obtained from listing (by name).
135 * @param block NULL if not found
138 handle_block (void *cls,
139 const struct GNUNET_GNSRECORD_Block *block)
145 "No matching block found\n");
147 else if (GNUNET_OK !=
148 GNUNET_GNSRECORD_block_decrypt (block,
151 &display_records_from_block,
155 "Failed to decrypt block!\n");
157 GNUNET_SCHEDULER_shutdown ();
162 * Main function that will be run.
165 * @param args remaining command-line arguments
166 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
167 * @param cfg configuration
170 run (void *cls, char *const *args, const char *cfgfile,
171 const struct GNUNET_CONFIGURATION_Handle *cfg)
173 struct GNUNET_HashCode dhash;
178 _("You must specify which zone should be accessed\n"));
183 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
188 _("Invalid public key for zone `%s'\n"),
190 GNUNET_SCHEDULER_shutdown ();
196 _("You must specify a name\n"));
200 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
202 ns = GNUNET_NAMECACHE_connect (cfg);
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 struct GNUNET_GETOPT_CommandLineOption options[] = {
224 GNUNET_GETOPT_option_string ('n',
227 gettext_noop ("name of the record to add/delete/display"),
230 GNUNET_GETOPT_option_string ('z',
233 gettext_noop ("spezifies the public key of the zone to look in"),
236 GNUNET_GETOPT_OPTION_END
239 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
242 GNUNET_log_setup ("gnunet-namecache", "WARNING", NULL);
244 GNUNET_PROGRAM_run (argc, argv, "gnunet-namecache",
245 _("GNUnet zone manipulation tool"),
249 GNUNET_free ((void*) argv);
252 GNUNET_free ((void*) argv);
256 /* end of gnunet-namecache.c */