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
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, uint32_t rd_count,
172 const struct GNUNET_GNSRECORD_Data *rd)
174 const char *name = cls;
176 const char *typename;
179 lookup_request = NULL;
183 printf ("No results.\n");
188 for (i=0; i<rd_count; i++)
190 if ( (rd[i].record_type != rtype) &&
191 (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
193 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
194 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
197 if (NULL == string_val)
200 "Record %u of type %d malformed, skipping\n",
202 (int) rd[i].record_type);
209 printf ("Got `%s' record: %s\n",
212 GNUNET_free (string_val);
214 GNUNET_SCHEDULER_shutdown ();
219 * Perform the actual resolution, starting with the zone
220 * identified by the given public key and the shorten zone.
222 * @param pkey public key to use for the zone, can be NULL
223 * @param shorten_key private key used for shortening, can be NULL
226 lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
227 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
229 if (NULL != lookup_type)
230 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
232 rtype = GNUNET_DNSPARSER_TYPE_A;
233 if (UINT32_MAX == rtype)
236 _("Invalid typename specified, assuming `ANY'\n"));
237 rtype = GNUNET_GNSRECORD_TYPE_ANY;
240 if (NULL != lookup_name)
242 lookup_request = GNUNET_GNS_lookup (gns,
248 &process_lookup_result,
254 _("Please specify name to lookup!\n"));
255 GNUNET_SCHEDULER_shutdown ();
262 * Method called to with the ego we are to use for shortening
265 * @param cls closure contains the public key to use
266 * @param ego ego handle, NULL if not found
267 * @param ctx context for application to store data for this ego
268 * (during the lifetime of this process, initially NULL)
269 * @param name name assigned by the user for this ego,
270 * NULL if the user just deleted the ego and it
271 * must thus no longer be used
274 identity_shorten_cb (void *cls,
275 struct GNUNET_IDENTITY_Ego *ego,
279 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
283 lookup_with_keys (pkeym, NULL);
285 lookup_with_keys (pkeym,
286 GNUNET_IDENTITY_ego_get_private_key (ego));
292 * Perform the actual resolution, starting with the zone
293 * identified by the given public key.
295 * @param pkey public key to use for the zone
298 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
300 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
302 GNUNET_assert (NULL != pkey);
303 pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
305 GNUNET_break (NULL == id_op);
306 id_op = GNUNET_IDENTITY_get (identity,
308 &identity_shorten_cb,
313 lookup_with_keys (pkey, NULL);
319 * Method called to with the ego we are to use for the lookup,
320 * when the ego is determined by a name.
322 * @param cls closure (NULL, unused)
323 * @param ego ego handle, NULL if not found
326 identity_zone_cb (void *cls,
327 const struct GNUNET_IDENTITY_Ego *ego)
329 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
335 _("Ego for `%s' not found, cannot perform lookup.\n"),
337 GNUNET_SCHEDULER_shutdown ();
341 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
342 lookup_with_public_key (&pkey);
344 GNUNET_free_non_null (zone_ego_name);
345 zone_ego_name = NULL;
350 * Method called to with the ego we are to use for the lookup,
351 * when the ego is the one for the default master zone.
353 * @param cls closure (NULL, unused)
354 * @param ego ego handle, NULL if not found
355 * @param ctx context for application to store data for this ego
356 * (during the lifetime of this process, initially NULL)
357 * @param name name assigned by the user for this ego,
358 * NULL if the user just deleted the ego and it
359 * must thus no longer be used
362 identity_master_cb (void *cls,
363 struct GNUNET_IDENTITY_Ego *ego,
367 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
374 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
375 GNUNET_SCHEDULER_shutdown ();
378 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
379 /* main name is our own master zone, do no look for that in the DHT */
380 local_options = GNUNET_GNS_LO_LOCAL_MASTER;
382 /* if the name is of the form 'label.gnu', never go to the DHT */
384 if (NULL != lookup_name)
385 dot = strchr (lookup_name, '.');
386 if ( (NULL != dot) &&
387 (0 == strcasecmp (dot, ".gnu")) )
388 local_options = GNUNET_GNS_LO_NO_DHT;
389 lookup_with_public_key (&pkey);
394 * Main function that will be run.
397 * @param args remaining command-line arguments
398 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
399 * @param c configuration
405 const struct GNUNET_CONFIGURATION_Handle *c)
407 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
410 gns = GNUNET_GNS_connect (cfg);
411 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
415 _("Failed to connect to GNS\n"));
418 tt = GNUNET_SCHEDULER_add_delayed (timeout,
420 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
421 if (NULL != public_key)
424 GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
429 _("Public key `%s' is not well-formed\n"),
431 GNUNET_SCHEDULER_shutdown ();
434 lookup_with_public_key (&pkey);
437 if (NULL != zone_ego_name)
439 el = GNUNET_IDENTITY_ego_lookup (cfg,
445 if ( (NULL != lookup_name) &&
446 (strlen (lookup_name) > 4) &&
447 (0 == strcmp (".zkey",
448 &lookup_name[strlen (lookup_name) - 4])) )
450 /* no zone required, use 'anonymous' zone */
451 GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
453 lookup_with_public_key (&pkey);
457 GNUNET_break (NULL == id_op);
458 id_op = GNUNET_IDENTITY_get (identity,
462 GNUNET_assert (NULL != id_op);
468 * The main function for gnunet-gns.
470 * @param argc number of arguments from the command line
471 * @param argv command line arguments
472 * @return 0 ok, 1 on error
475 main (int argc, char *const *argv)
477 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
478 {'u', "lookup", "NAME",
479 gettext_noop ("Lookup a record for the given name"), 1,
480 &GNUNET_GETOPT_set_string, &lookup_name},
481 {'t', "type", "TYPE",
482 gettext_noop ("Specify the type of the record to lookup"), 1,
483 &GNUNET_GETOPT_set_string, &lookup_type},
484 { 'T', "timeout", "DELAY",
485 gettext_noop ("Specify timeout for the lookup"), 1,
486 &GNUNET_GETOPT_set_relative_time, &timeout },
488 gettext_noop ("No unneeded output"), 0,
489 &GNUNET_GETOPT_set_one, &raw},
490 {'p', "public-key", "PKEY",
491 gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
492 &GNUNET_GETOPT_set_string, &public_key},
493 {'z', "zone", "NAME",
494 gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
495 &GNUNET_GETOPT_set_string, &zone_ego_name},
496 GNUNET_GETOPT_OPTION_END
500 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
501 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
504 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
507 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
508 _("GNUnet GNS resolver tool"),
510 &run, NULL)) ? 0 : 1;
511 GNUNET_free ((void*) argv);
515 /* end of gnunet-gns.c */