2 This file is part of GNUnet.
3 Copyright (C) 2012-2013, 2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @brief command line tool to access distributed GNS
23 * @author Christian Grothoff
26 #include <gnunet_util_lib.h>
27 #include <gnunet_dnsparser_lib.h>
28 #include <gnunet_identity_service.h>
29 #include <gnunet_gnsrecord_lib.h>
30 #include <gnunet_namestore_service.h>
31 #include <gnunet_gns_service.h>
34 * Configuration we are using.
36 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 * Handle to GNS service.
41 static struct GNUNET_GNS_Handle *gns;
44 * Desired timeout for the lookup (default is no timeout).
46 static struct GNUNET_TIME_Relative timeout;
49 * GNS name to lookup. (-u option)
51 static char *lookup_name;
54 * record type to look up (-t option)
56 static char *lookup_type;
59 * Identity of the zone to use for the lookup (-z option)
61 static char *zone_ego_name;
64 * Public key of the zone to use for the lookup (-p option)
66 static char *public_key;
69 * Set to GNUNET_GNS_LO_LOCAL_MASTER if we are looking up in the master zone.
71 static enum GNUNET_GNS_LocalOptions local_options;
79 * Requested record type.
84 * Handle to lookup request
86 static struct GNUNET_GNS_LookupRequest *lookup_request;
89 * Lookup an ego with the identity service.
91 static struct GNUNET_IDENTITY_EgoLookup *el;
94 * Handle for identity service.
96 static struct GNUNET_IDENTITY_Handle *identity;
99 * Active operation on identity service.
101 static struct GNUNET_IDENTITY_Operation *id_op;
104 * Task scheduled to handle timeout.
106 static struct GNUNET_SCHEDULER_Task *tt;
110 * Task run on shutdown. Cleans up everything.
115 do_shutdown (void *cls)
119 GNUNET_IDENTITY_ego_lookup_cancel (el);
124 GNUNET_IDENTITY_cancel (id_op);
127 if (NULL != lookup_request)
129 GNUNET_GNS_lookup_cancel (lookup_request);
130 lookup_request = NULL;
132 if (NULL != identity)
134 GNUNET_IDENTITY_disconnect (identity);
139 GNUNET_GNS_disconnect (gns);
144 GNUNET_SCHEDULER_cancel (tt);
151 * Task run on timeout. Triggers shutdown.
156 do_timeout (void *cls)
159 GNUNET_SCHEDULER_shutdown ();
164 * Function called with the result of a GNS lookup.
166 * @param cls the 'const char *' name that was resolved
167 * @param rd_count number of records returned
168 * @param rd array of @a rd_count records with the results
171 process_lookup_result (void *cls,
173 const struct GNUNET_GNSRECORD_Data *rd)
175 const char *name = cls;
177 const char *typename;
180 lookup_request = NULL;
184 printf ("No results.\n");
189 for (i=0; i<rd_count; i++)
191 if ( (rd[i].record_type != rtype) &&
192 (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
194 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
195 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
198 if (NULL == string_val)
201 "Record %u of type %d malformed, skipping\n",
203 (int) rd[i].record_type);
210 printf ("Got `%s' record: %s\n",
213 GNUNET_free (string_val);
215 GNUNET_SCHEDULER_shutdown ();
220 * Perform the actual resolution, starting with the zone
221 * identified by the given public key and the shorten zone.
223 * @param pkey public key to use for the zone, can be NULL
226 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
228 if (NULL != lookup_type)
229 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
231 rtype = GNUNET_DNSPARSER_TYPE_A;
232 if (UINT32_MAX == rtype)
235 _("Invalid typename specified, assuming `ANY'\n"));
236 rtype = GNUNET_GNSRECORD_TYPE_ANY;
239 if (NULL != lookup_name)
241 lookup_request = GNUNET_GNS_lookup (gns,
246 &process_lookup_result,
252 _("Please specify name to lookup!\n"));
253 GNUNET_SCHEDULER_shutdown ();
260 * Method called to with the ego we are to use for the lookup,
261 * when the ego is determined by a name.
263 * @param cls closure (NULL, unused)
264 * @param ego ego handle, NULL if not found
267 identity_zone_cb (void *cls,
268 const struct GNUNET_IDENTITY_Ego *ego)
270 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
276 _("Ego for `%s' not found, cannot perform lookup.\n"),
278 GNUNET_SCHEDULER_shutdown ();
282 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
283 lookup_with_public_key (&pkey);
285 GNUNET_free_non_null (zone_ego_name);
286 zone_ego_name = NULL;
291 * Method called to with the ego we are to use for the lookup,
292 * when the ego is the one for the default master zone.
294 * @param cls closure (NULL, unused)
295 * @param ego ego handle, NULL if not found
296 * @param ctx context for application to store data for this ego
297 * (during the lifetime of this process, initially NULL)
298 * @param name name assigned by the user for this ego,
299 * NULL if the user just deleted the ego and it
300 * must thus no longer be used
303 identity_master_cb (void *cls,
304 struct GNUNET_IDENTITY_Ego *ego,
308 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
315 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
316 GNUNET_SCHEDULER_shutdown ();
319 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
320 /* main name is our own master zone, do no look for that in the DHT */
321 local_options = GNUNET_GNS_LO_LOCAL_MASTER;
323 /* if the name is of the form 'label.gnu', never go to the DHT */
325 if (NULL != lookup_name)
326 dot = strchr (lookup_name, '.');
327 if ( (NULL != dot) &&
328 (0 == strcasecmp (dot, ".gnu")) )
329 local_options = GNUNET_GNS_LO_NO_DHT;
330 lookup_with_public_key (&pkey);
335 * Main function that will be run.
338 * @param args remaining command-line arguments
339 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
340 * @param c configuration
346 const struct GNUNET_CONFIGURATION_Handle *c)
348 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
351 gns = GNUNET_GNS_connect (cfg);
355 _("Failed to connect to GNS\n"));
358 tt = GNUNET_SCHEDULER_add_delayed (timeout,
361 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
363 identity = GNUNET_IDENTITY_connect (cfg,
366 if (NULL != public_key)
369 GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
374 _("Public key `%s' is not well-formed\n"),
376 GNUNET_SCHEDULER_shutdown ();
379 lookup_with_public_key (&pkey);
382 if (NULL != zone_ego_name)
384 el = GNUNET_IDENTITY_ego_lookup (cfg,
390 if ( (NULL != lookup_name) &&
391 (strlen (lookup_name) > 4) &&
392 (0 == strcmp (".zkey",
393 &lookup_name[strlen (lookup_name) - 4])) )
395 /* no zone required, use 'anonymous' zone */
396 GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
398 lookup_with_public_key (&pkey);
402 GNUNET_break (NULL == id_op);
403 id_op = GNUNET_IDENTITY_get (identity,
407 GNUNET_assert (NULL != id_op);
413 * The main function for gnunet-gns.
415 * @param argc number of arguments from the command line
416 * @param argv command line arguments
417 * @return 0 ok, 1 on error
423 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
424 {'u', "lookup", "NAME",
425 gettext_noop ("Lookup a record for the given name"), 1,
426 &GNUNET_GETOPT_set_string, &lookup_name},
427 {'t', "type", "TYPE",
428 gettext_noop ("Specify the type of the record to lookup"), 1,
429 &GNUNET_GETOPT_set_string, &lookup_type},
430 { 'T', "timeout", "DELAY",
431 gettext_noop ("Specify timeout for the lookup"), 1,
432 &GNUNET_GETOPT_set_relative_time, &timeout },
434 gettext_noop ("No unneeded output"), 0,
435 &GNUNET_GETOPT_set_one, &raw},
436 {'p', "public-key", "PKEY",
437 gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
438 &GNUNET_GETOPT_set_string, &public_key},
439 {'z', "zone", "NAME",
440 gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
441 &GNUNET_GETOPT_set_string, &zone_ego_name},
442 GNUNET_GETOPT_OPTION_END
446 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
447 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
451 GNUNET_log_setup ("gnunet-gns",
456 GNUNET_PROGRAM_run (argc, argv,
458 _("GNUnet GNS resolver tool"),
460 &run, NULL)) ? 0 : 1;
461 GNUNET_free ((void*) argv);
465 /* end of gnunet-gns.c */