aboutsummaryrefslogtreecommitdiff
path: root/src/cli/namecache/gnunet-namecache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli/namecache/gnunet-namecache.c')
-rw-r--r--src/cli/namecache/gnunet-namecache.c245
1 files changed, 245 insertions, 0 deletions
diff --git a/src/cli/namecache/gnunet-namecache.c b/src/cli/namecache/gnunet-namecache.c
new file mode 100644
index 000000000..0236609aa
--- /dev/null
+++ b/src/cli/namecache/gnunet-namecache.c
@@ -0,0 +1,245 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file gnunet-namecache.c
22 * @brief command line tool to inspect the name cache
23 * @author Christian Grothoff
24 *
25 * TODO:
26 * - test
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_identity_service.h"
31#include "gnunet_gnsrecord_lib.h"
32#include "gnunet_namecache_service.h"
33
34
35/**
36 * Handle to the namecache.
37 */
38static struct GNUNET_NAMECACHE_Handle *ns;
39
40/**
41 * Queue entry for the 'query' operation.
42 */
43static struct GNUNET_NAMECACHE_QueueEntry *qe;
44
45/**
46 * Name (label) of the records to list.
47 */
48static char *name;
49
50/**
51 * Public key of the zone to look in.
52 */
53static struct GNUNET_CRYPTO_PublicKey pubkey;
54
55/**
56 * Public key of the zone to look in, in ASCII.
57 */
58static char *pkey;
59
60/**
61 * Global return value
62 */
63static int ret;
64
65
66/**
67 * Task run on shutdown. Cleans up everything.
68 *
69 * @param cls unused
70 */
71static void
72do_shutdown (void *cls)
73{
74 if (NULL != qe)
75 {
76 GNUNET_NAMECACHE_cancel (qe);
77 qe = NULL;
78 }
79 if (NULL != ns)
80 {
81 GNUNET_NAMECACHE_disconnect (ns);
82 ns = NULL;
83 }
84}
85
86
87/**
88 * Process a record that was stored in the namecache in a block.
89 *
90 * @param cls closure, NULL
91 * @param rd_len number of entries in @a rd array
92 * @param rd array of records with data to store
93 */
94static void
95display_records_from_block (void *cls,
96 unsigned int rd_len,
97 const struct GNUNET_GNSRECORD_Data *rd)
98{
99 const char *typestring;
100 char *s;
101 unsigned int i;
102
103 if (0 == rd_len)
104 {
105 fprintf (stdout, _ ("No records found for `%s'"), name);
106 return;
107 }
108 fprintf (stdout, "%s:\n", name);
109 for (i = 0; i < rd_len; i++)
110 {
111 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
112 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
113 rd[i].data,
114 rd[i].data_size);
115 if (NULL == s)
116 {
117 fprintf (stdout,
118 _ ("\tCorrupt or unsupported record of type %u\n"),
119 (unsigned int) rd[i].record_type);
120 continue;
121 }
122 fprintf (stdout, "\t%s: %s\n", typestring, s);
123 GNUNET_free (s);
124 }
125 fprintf (stdout, "%s", "\n");
126}
127
128
129/**
130 * Display block obtained from listing (by name).
131 *
132 * @param cls NULL
133 * @param block NULL if not found
134 */
135static void
136handle_block (void *cls, const struct GNUNET_GNSRECORD_Block *block)
137{
138 qe = NULL;
139 if (NULL == block)
140 {
141 fprintf (stderr, "No matching block found\n");
142 }
143 else if (GNUNET_OK !=
144 GNUNET_GNSRECORD_block_decrypt (block,
145 &pubkey,
146 name,
147 &display_records_from_block,
148 NULL))
149 {
150 fprintf (stderr, "Failed to decrypt block!\n");
151 }
152 GNUNET_SCHEDULER_shutdown ();
153}
154
155
156/**
157 * Main function that will be run.
158 *
159 * @param cls closure
160 * @param args remaining command-line arguments
161 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
162 * @param cfg configuration
163 */
164static void
165run (void *cls,
166 char *const *args,
167 const char *cfgfile,
168 const struct GNUNET_CONFIGURATION_Handle *cfg)
169{
170 struct GNUNET_HashCode dhash;
171
172 if (NULL == pkey)
173 {
174 fprintf (stderr, _ ("You must specify which zone should be accessed\n"));
175 return;
176 }
177
178 if (GNUNET_OK !=
179 GNUNET_CRYPTO_public_key_from_string (pkey, &pubkey))
180 {
181 fprintf (stderr, _ ("Invalid public key for zone `%s'\n"), pkey);
182 GNUNET_SCHEDULER_shutdown ();
183 return;
184 }
185 if (NULL == name)
186 {
187 fprintf (stderr, _ ("You must specify a name\n"));
188 return;
189 }
190
191 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
192 ns = GNUNET_NAMECACHE_connect (cfg);
193 GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &dhash);
194 qe = GNUNET_NAMECACHE_lookup_block (ns, &dhash, &handle_block, NULL);
195}
196
197
198/**
199 * The main function for gnunet-namecache.
200 *
201 * @param argc number of arguments from the command line
202 * @param argv command line arguments
203 * @return 0 ok, 1 on error
204 */
205int
206main (int argc, char *const *argv)
207{
208 struct GNUNET_GETOPT_CommandLineOption options[] =
209 { GNUNET_GETOPT_option_string ('n',
210 "name",
211 "NAME",
212 gettext_noop (
213 "name of the record to add/delete/display"),
214 &name),
215
216 GNUNET_GETOPT_option_string (
217 'z',
218 "zone",
219 "PKEY",
220 gettext_noop ("specifies the public key of the zone to look in"),
221 &pkey),
222
223 GNUNET_GETOPT_OPTION_END };
224
225 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
226 return 2;
227
228 GNUNET_log_setup ("gnunet-namecache", "WARNING", NULL);
229 if (GNUNET_OK != GNUNET_PROGRAM_run (argc,
230 argv,
231 "gnunet-namecache",
232 _ ("GNUnet zone manipulation tool"),
233 options,
234 &run,
235 NULL))
236 {
237 GNUNET_free_nz ((void *) argv);
238 return 1;
239 }
240 GNUNET_free_nz ((void *) argv);
241 return ret;
242}
243
244
245/* end of gnunet-namecache.c */