2 This file is part of GNUnet.
3 (C) 2012-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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;
105 * Task run on shutdown. Cleans up everything.
108 * @param tc scheduler context
111 do_shutdown (void *cls,
112 const struct GNUNET_SCHEDULER_TaskContext *tc)
116 GNUNET_IDENTITY_ego_lookup_cancel (el);
121 GNUNET_IDENTITY_cancel (id_op);
124 if (NULL != lookup_request)
126 GNUNET_GNS_lookup_cancel (lookup_request);
127 lookup_request = NULL;
129 if (NULL != identity)
131 GNUNET_IDENTITY_disconnect (identity);
136 GNUNET_GNS_disconnect (gns);
143 * Function called with the result of a GNS lookup.
145 * @param cls the 'const char *' name that was resolved
146 * @param rd_count number of records returned
147 * @param rd array of @a rd_count records with the results
150 process_lookup_result (void *cls, uint32_t rd_count,
151 const struct GNUNET_GNSRECORD_Data *rd)
153 const char *name = cls;
155 const char *typename;
158 lookup_request = NULL;
162 printf ("No results.\n");
167 for (i=0; i<rd_count; i++)
169 if ( (rd[i].record_type != rtype) &&
170 (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
172 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
173 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
176 if (NULL == string_val)
179 "Record %u of type %d malformed, skipping\n",
181 (int) rd[i].record_type);
188 printf ("Got `%s' record: %s\n",
191 GNUNET_free (string_val);
193 GNUNET_SCHEDULER_shutdown ();
198 * Perform the actual resolution, starting with the zone
199 * identified by the given public key and the shorten zone.
201 * @param pkey public key to use for the zone, can be NULL
202 * @param shorten_key private key used for shortening, can be NULL
205 lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
206 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
208 if (NULL != lookup_type)
209 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
211 rtype = GNUNET_DNSPARSER_TYPE_A;
212 if (UINT32_MAX == rtype)
215 _("Invalid typename specified, assuming `ANY'\n"));
216 rtype = GNUNET_GNSRECORD_TYPE_ANY;
219 if (NULL != lookup_name)
221 lookup_request = GNUNET_GNS_lookup (gns,
227 &process_lookup_result,
233 _("Please specify name to lookup!\n"));
234 GNUNET_SCHEDULER_shutdown ();
241 * Method called to with the ego we are to use for shortening
244 * @param cls closure contains the public key to use
245 * @param ego ego handle, NULL if not found
246 * @param ctx context for application to store data for this ego
247 * (during the lifetime of this process, initially NULL)
248 * @param name name assigned by the user for this ego,
249 * NULL if the user just deleted the ego and it
250 * must thus no longer be used
253 identity_shorten_cb (void *cls,
254 struct GNUNET_IDENTITY_Ego *ego,
258 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
262 lookup_with_keys (pkeym, NULL);
264 lookup_with_keys (pkeym,
265 GNUNET_IDENTITY_ego_get_private_key (ego));
271 * Perform the actual resolution, starting with the zone
272 * identified by the given public key.
274 * @param pkey public key to use for the zone
277 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
279 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
281 GNUNET_assert (NULL != pkey);
282 pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
284 GNUNET_break (NULL == id_op);
285 id_op = GNUNET_IDENTITY_get (identity,
287 &identity_shorten_cb,
292 lookup_with_keys (pkey, NULL);
298 * Method called to with the ego we are to use for the lookup,
299 * when the ego is determined by a name.
301 * @param cls closure (NULL, unused)
302 * @param ego ego handle, NULL if not found
305 identity_zone_cb (void *cls,
306 const struct GNUNET_IDENTITY_Ego *ego)
308 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
314 _("Ego for `%s' not found, cannot perform lookup.\n"),
316 GNUNET_SCHEDULER_shutdown ();
320 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
321 lookup_with_public_key (&pkey);
323 GNUNET_free_non_null (zone_ego_name);
324 zone_ego_name = NULL;
329 * Method called to with the ego we are to use for the lookup,
330 * when the ego is the one for the default master zone.
332 * @param cls closure (NULL, unused)
333 * @param ego ego handle, NULL if not found
334 * @param ctx context for application to store data for this ego
335 * (during the lifetime of this process, initially NULL)
336 * @param name name assigned by the user for this ego,
337 * NULL if the user just deleted the ego and it
338 * must thus no longer be used
341 identity_master_cb (void *cls,
342 struct GNUNET_IDENTITY_Ego *ego,
346 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
353 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
354 GNUNET_SCHEDULER_shutdown ();
357 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
358 /* main name is our own master zone, do no look for that in the DHT */
359 local_options = GNUNET_GNS_LO_LOCAL_MASTER;
361 /* if the name is of the form 'label.gnu', never go to the DHT */
363 if (NULL != lookup_name)
364 dot = strchr (lookup_name, '.');
365 if ( (NULL != dot) &&
366 (0 == strcasecmp (dot, ".gnu")) )
367 local_options = GNUNET_GNS_LO_NO_DHT;
368 lookup_with_public_key (&pkey);
373 * Main function that will be run.
376 * @param args remaining command-line arguments
377 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
378 * @param c configuration
381 run (void *cls, char *const *args, const char *cfgfile,
382 const struct GNUNET_CONFIGURATION_Handle *c)
384 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
387 gns = GNUNET_GNS_connect (cfg);
388 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
392 _("Failed to connect to GNS\n"));
395 GNUNET_SCHEDULER_add_delayed (timeout,
397 if (NULL != public_key)
400 GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
405 _("Public key `%s' is not well-formed\n"),
407 GNUNET_SCHEDULER_shutdown ();
410 lookup_with_public_key (&pkey);
413 if (NULL != zone_ego_name)
415 el = GNUNET_IDENTITY_ego_lookup (cfg,
421 if ( (NULL != lookup_name) &&
422 (strlen (lookup_name) > 4) &&
423 (0 == strcmp (".zkey",
424 &lookup_name[strlen (lookup_name) - 4])) )
426 /* no zone required, use 'anonymous' zone */
427 GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
429 lookup_with_public_key (&pkey);
433 GNUNET_break (NULL == id_op);
434 id_op = GNUNET_IDENTITY_get (identity,
438 GNUNET_assert (NULL != id_op);
444 * The main function for gnunet-gns.
446 * @param argc number of arguments from the command line
447 * @param argv command line arguments
448 * @return 0 ok, 1 on error
451 main (int argc, char *const *argv)
453 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
454 {'u', "lookup", "NAME",
455 gettext_noop ("Lookup a record for the given name"), 1,
456 &GNUNET_GETOPT_set_string, &lookup_name},
457 {'t', "type", "TYPE",
458 gettext_noop ("Specify the type of the record to lookup"), 1,
459 &GNUNET_GETOPT_set_string, &lookup_type},
460 { 'T', "timeout", "DELAY",
461 gettext_noop ("Specify timeout for the lookup"), 1,
462 &GNUNET_GETOPT_set_relative_time, &timeout },
464 gettext_noop ("No unneeded output"), 0,
465 &GNUNET_GETOPT_set_one, &raw},
466 {'p', "public-key", "PKEY",
467 gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
468 &GNUNET_GETOPT_set_string, &public_key},
469 {'z', "zone", "NAME",
470 gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
471 &GNUNET_GETOPT_set_string, &zone_ego_name},
472 GNUNET_GETOPT_OPTION_END
476 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
477 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
480 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
483 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
484 _("GNUnet GNS resolver tool"),
486 &run, NULL)) ? 0 : 1;
487 GNUNET_free ((void*) argv);
491 /* end of gnunet-gns.c */