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;
71 static char *reverse_key;
76 static struct GNUNET_CRYPTO_EcdsaPublicKey rkey;
79 * Set to GNUNET_GNS_LO_LOCAL_MASTER if we are looking up in the master zone.
81 static enum GNUNET_GNS_LocalOptions local_options;
89 * Requested record type.
94 * Handle to lookup request
96 static struct GNUNET_GNS_LookupRequest *lookup_request;
99 * Handle to reverse lookup request
101 static struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_request;
104 * Lookup an ego with the identity service.
106 static struct GNUNET_IDENTITY_EgoLookup *el;
109 * Handle for identity service.
111 static struct GNUNET_IDENTITY_Handle *identity;
114 * Active operation on identity service.
116 static struct GNUNET_IDENTITY_Operation *id_op;
119 * Task scheduled to handle timeout.
121 static struct GNUNET_SCHEDULER_Task *tt;
125 * Task run on shutdown. Cleans up everything.
130 do_shutdown (void *cls)
134 GNUNET_IDENTITY_ego_lookup_cancel (el);
139 GNUNET_IDENTITY_cancel (id_op);
142 if (NULL != lookup_request)
144 GNUNET_GNS_lookup_cancel (lookup_request);
145 lookup_request = NULL;
147 if (NULL != identity)
149 GNUNET_IDENTITY_disconnect (identity);
154 GNUNET_GNS_disconnect (gns);
159 GNUNET_SCHEDULER_cancel (tt);
166 * Task run on timeout. Triggers shutdown.
171 do_timeout (void *cls)
174 GNUNET_SCHEDULER_shutdown ();
178 process_reverse_result (void *cls,
181 rev_lookup_request = NULL;
184 printf ("No name found.\n");
188 printf ("%s\n", name);
190 printf ("%s is known as %s\n",
193 GNUNET_SCHEDULER_shutdown ();
197 * Function called with the result of a GNS lookup.
199 * @param cls the 'const char *' name that was resolved
200 * @param rd_count number of records returned
201 * @param rd array of @a rd_count records with the results
204 process_lookup_result (void *cls, uint32_t rd_count,
205 const struct GNUNET_GNSRECORD_Data *rd)
207 const char *name = cls;
209 const char *typename;
212 lookup_request = NULL;
216 printf ("No results.\n");
221 for (i=0; i<rd_count; i++)
223 if ( (rd[i].record_type != rtype) &&
224 (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
226 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
227 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
230 if (NULL == string_val)
233 "Record %u of type %d malformed, skipping\n",
235 (int) rd[i].record_type);
242 printf ("Got `%s' record: %s\n",
245 GNUNET_free (string_val);
247 GNUNET_SCHEDULER_shutdown ();
252 * Perform the actual resolution, starting with the zone
253 * identified by the given public key and the shorten zone.
255 * @param pkey public key to use for the zone, can be NULL
256 * @param shorten_key private key used for shortening, can be NULL
259 lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
260 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
262 if (NULL != lookup_type)
263 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
265 rtype = GNUNET_DNSPARSER_TYPE_A;
266 if (UINT32_MAX == rtype)
269 _("Invalid typename specified, assuming `ANY'\n"));
270 rtype = GNUNET_GNSRECORD_TYPE_ANY;
273 if (NULL != lookup_name)
275 lookup_request = GNUNET_GNS_lookup (gns,
281 &process_lookup_result,
284 else if (NULL != reverse_key)
286 rev_lookup_request = GNUNET_GNS_reverse_lookup (gns,
289 &process_reverse_result,
295 _("Please specify name to lookup!\n"));
296 GNUNET_SCHEDULER_shutdown ();
303 * Method called to with the ego we are to use for shortening
306 * @param cls closure contains the public key to use
307 * @param ego ego handle, NULL if not found
308 * @param ctx context for application to store data for this ego
309 * (during the lifetime of this process, initially NULL)
310 * @param name name assigned by the user for this ego,
311 * NULL if the user just deleted the ego and it
312 * must thus no longer be used
315 identity_shorten_cb (void *cls,
316 struct GNUNET_IDENTITY_Ego *ego,
320 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
324 lookup_with_keys (pkeym, NULL);
326 lookup_with_keys (pkeym,
327 GNUNET_IDENTITY_ego_get_private_key (ego));
333 * Perform the actual resolution, starting with the zone
334 * identified by the given public key.
336 * @param pkey public key to use for the zone
339 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
341 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
343 GNUNET_assert (NULL != pkey);
344 pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
346 GNUNET_break (NULL == id_op);
347 id_op = GNUNET_IDENTITY_get (identity,
349 &identity_shorten_cb,
354 lookup_with_keys (pkey, NULL);
360 * Method called to with the ego we are to use for the lookup,
361 * when the ego is determined by a name.
363 * @param cls closure (NULL, unused)
364 * @param ego ego handle, NULL if not found
367 identity_zone_cb (void *cls,
368 const struct GNUNET_IDENTITY_Ego *ego)
370 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
376 _("Ego for `%s' not found, cannot perform lookup.\n"),
378 GNUNET_SCHEDULER_shutdown ();
382 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
383 lookup_with_public_key (&pkey);
385 GNUNET_free_non_null (zone_ego_name);
386 zone_ego_name = NULL;
391 * Method called to with the ego we are to use for the lookup,
392 * when the ego is the one for the default master zone.
394 * @param cls closure (NULL, unused)
395 * @param ego ego handle, NULL if not found
396 * @param ctx context for application to store data for this ego
397 * (during the lifetime of this process, initially NULL)
398 * @param name name assigned by the user for this ego,
399 * NULL if the user just deleted the ego and it
400 * must thus no longer be used
403 identity_master_cb (void *cls,
404 struct GNUNET_IDENTITY_Ego *ego,
408 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
415 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
416 GNUNET_SCHEDULER_shutdown ();
419 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
420 /* main name is our own master zone, do no look for that in the DHT */
421 local_options = GNUNET_GNS_LO_LOCAL_MASTER;
423 /* if the name is of the form 'label.gnu', never go to the DHT */
425 if (NULL != lookup_name)
426 dot = strchr (lookup_name, '.');
427 if ( (NULL != dot) &&
428 (0 == strcasecmp (dot, ".gnu")) )
429 local_options = GNUNET_GNS_LO_NO_DHT;
430 lookup_with_public_key (&pkey);
435 * Main function that will be run.
438 * @param args remaining command-line arguments
439 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
440 * @param c configuration
446 const struct GNUNET_CONFIGURATION_Handle *c)
448 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
451 gns = GNUNET_GNS_connect (cfg);
452 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
456 _("Failed to connect to GNS\n"));
459 tt = GNUNET_SCHEDULER_add_delayed (timeout,
461 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
462 if (NULL != reverse_key)
465 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_key,
466 strlen (reverse_key),
470 _("Reverse key `%s' is not well-formed\n"),
472 GNUNET_SCHEDULER_shutdown ();
476 if (NULL != public_key)
479 GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
484 _("Public key `%s' is not well-formed\n"),
486 GNUNET_SCHEDULER_shutdown ();
489 lookup_with_public_key (&pkey);
492 if (NULL != reverse_key)
495 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_key,
496 strlen (reverse_key),
500 _("Reverse key `%s' is not well-formed\n"),
502 GNUNET_SCHEDULER_shutdown ();
506 if (NULL != zone_ego_name)
508 el = GNUNET_IDENTITY_ego_lookup (cfg,
514 if ( (NULL != lookup_name) &&
515 (strlen (lookup_name) > 4) &&
516 (0 == strcmp (".zkey",
517 &lookup_name[strlen (lookup_name) - 4])) )
519 /* no zone required, use 'anonymous' zone */
520 GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
522 lookup_with_public_key (&pkey);
526 GNUNET_break (NULL == id_op);
527 id_op = GNUNET_IDENTITY_get (identity,
531 GNUNET_assert (NULL != id_op);
537 * The main function for gnunet-gns.
539 * @param argc number of arguments from the command line
540 * @param argv command line arguments
541 * @return 0 ok, 1 on error
544 main (int argc, char *const *argv)
546 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
547 {'u', "lookup", "NAME",
548 gettext_noop ("Lookup a record for the given name"), 1,
549 &GNUNET_GETOPT_set_string, &lookup_name},
550 {'t', "type", "TYPE",
551 gettext_noop ("Specify the type of the record to lookup"), 1,
552 &GNUNET_GETOPT_set_string, &lookup_type},
553 { 'T', "timeout", "DELAY",
554 gettext_noop ("Specify timeout for the lookup"), 1,
555 &GNUNET_GETOPT_set_relative_time, &timeout },
557 gettext_noop ("No unneeded output"), 0,
558 &GNUNET_GETOPT_set_one, &raw},
559 {'p', "public-key", "PKEY",
560 gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
561 &GNUNET_GETOPT_set_string, &public_key},
562 {'z', "zone", "NAME",
563 gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
564 &GNUNET_GETOPT_set_string, &zone_ego_name},
565 {'R', "reverse", "PKEY",
566 gettext_noop ("Specify the public key of the zone to reverse lookup a name for"), 1,
567 &GNUNET_GETOPT_set_string, &reverse_key},
568 GNUNET_GETOPT_OPTION_END
572 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
573 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
576 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
579 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
580 _("GNUnet GNS resolver tool"),
582 &run, NULL)) ? 0 : 1;
583 GNUNET_free ((void*) argv);
587 /* end of gnunet-gns.c */