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;
213 if (NULL != lookup_name)
215 lookup_request = GNUNET_GNS_lookup (gns,
221 &process_lookup_result,
227 _("Please specify name to lookup!\n"));
228 GNUNET_SCHEDULER_shutdown ();
235 * Method called to with the ego we are to use for shortening
238 * @param cls closure contains the public key to use
239 * @param ego ego handle, NULL if not found
240 * @param ctx context for application to store data for this ego
241 * (during the lifetime of this process, initially NULL)
242 * @param name name assigned by the user for this ego,
243 * NULL if the user just deleted the ego and it
244 * must thus no longer be used
247 identity_shorten_cb (void *cls,
248 struct GNUNET_IDENTITY_Ego *ego,
252 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
256 lookup_with_keys (pkeym, NULL);
258 lookup_with_keys (pkeym,
259 GNUNET_IDENTITY_ego_get_private_key (ego));
265 * Perform the actual resolution, starting with the zone
266 * identified by the given public key.
268 * @param pkey public key to use for the zone
271 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
273 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
275 GNUNET_assert (NULL != pkey);
276 pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
278 GNUNET_break (NULL == id_op);
279 id_op = GNUNET_IDENTITY_get (identity,
281 &identity_shorten_cb,
286 lookup_with_keys (pkey, NULL);
292 * Method called to with the ego we are to use for the lookup,
293 * when the ego is determined by a name.
295 * @param cls closure (NULL, unused)
296 * @param ego ego handle, NULL if not found
299 identity_zone_cb (void *cls,
300 const struct GNUNET_IDENTITY_Ego *ego)
302 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
308 _("Ego for `%s' not found, cannot perform lookup.\n"),
310 GNUNET_SCHEDULER_shutdown ();
314 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
315 lookup_with_public_key (&pkey);
317 GNUNET_free_non_null (zone_ego_name);
318 zone_ego_name = NULL;
323 * Method called to with the ego we are to use for the lookup,
324 * when the ego is the one for the default master zone.
326 * @param cls closure (NULL, unused)
327 * @param ego ego handle, NULL if not found
328 * @param ctx context for application to store data for this ego
329 * (during the lifetime of this process, initially NULL)
330 * @param name name assigned by the user for this ego,
331 * NULL if the user just deleted the ego and it
332 * must thus no longer be used
335 identity_master_cb (void *cls,
336 struct GNUNET_IDENTITY_Ego *ego,
340 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
347 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
348 GNUNET_SCHEDULER_shutdown ();
351 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
352 /* main name is our own master zone, do no look for that in the DHT */
353 local_options = GNUNET_GNS_LO_LOCAL_MASTER;
355 /* if the name is of the form 'label.gnu', never go to the DHT */
357 if (NULL != lookup_name)
358 dot = strchr (lookup_name, '.');
359 if ( (NULL != dot) &&
360 (0 == strcasecmp (dot, ".gnu")) )
361 local_options = GNUNET_GNS_LO_NO_DHT;
362 lookup_with_public_key (&pkey);
367 * Main function that will be run.
370 * @param args remaining command-line arguments
371 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
372 * @param c configuration
375 run (void *cls, char *const *args, const char *cfgfile,
376 const struct GNUNET_CONFIGURATION_Handle *c)
378 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
381 gns = GNUNET_GNS_connect (cfg);
382 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
386 _("Failed to connect to GNS\n"));
389 GNUNET_SCHEDULER_add_delayed (timeout,
391 if (NULL != public_key)
394 GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
399 _("Public key `%s' is not well-formed\n"),
401 GNUNET_SCHEDULER_shutdown ();
404 lookup_with_public_key (&pkey);
407 if (NULL != zone_ego_name)
409 el = GNUNET_IDENTITY_ego_lookup (cfg,
415 if ( (NULL != lookup_name) &&
416 (strlen (lookup_name) > 4) &&
417 (0 == strcmp (".zkey",
418 &lookup_name[strlen (lookup_name) - 4])) )
420 /* no zone required, use 'anonymous' zone */
421 GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
423 lookup_with_public_key (&pkey);
427 GNUNET_break (NULL == id_op);
428 id_op = GNUNET_IDENTITY_get (identity,
432 GNUNET_assert (NULL != id_op);
438 * The main function for gnunet-gns.
440 * @param argc number of arguments from the command line
441 * @param argv command line arguments
442 * @return 0 ok, 1 on error
445 main (int argc, char *const *argv)
447 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
448 {'u', "lookup", "NAME",
449 gettext_noop ("Lookup a record for the given name"), 1,
450 &GNUNET_GETOPT_set_string, &lookup_name},
451 {'t', "type", "TYPE",
452 gettext_noop ("Specify the type of the record to lookup"), 1,
453 &GNUNET_GETOPT_set_string, &lookup_type},
454 { 'T', "timeout", "DELAY",
455 gettext_noop ("Specify timeout for the lookup"), 1,
456 &GNUNET_GETOPT_set_relative_time, &timeout },
458 gettext_noop ("No unneeded output"), 0,
459 &GNUNET_GETOPT_set_one, &raw},
460 {'p', "public-key", "PKEY",
461 gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
462 &GNUNET_GETOPT_set_string, &public_key},
463 {'z', "zone", "NAME",
464 gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
465 &GNUNET_GETOPT_set_string, &zone_ego_name},
466 GNUNET_GETOPT_OPTION_END
470 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
471 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
474 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
477 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
478 _("GNUnet GNS resolver tool"),
480 &run, NULL)) ? 0 : 1;
481 GNUNET_free ((void*) argv);
485 /* end of gnunet-gns.c */