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 * GNS name to lookup. (-u option)
46 static char *lookup_name;
49 * record type to look up (-t option)
51 static char *lookup_type;
54 * Identity of the zone to use for the lookup (-z option)
56 static char *zone_ego_name;
59 * Public key of the zone to use for the lookup (-p option)
61 static char *public_key;
69 * Requested record type.
74 * Handle to lookup request
76 static struct GNUNET_GNS_LookupRequest *lookup_request;
79 * Lookup an ego with the identity service.
81 static struct GNUNET_IDENTITY_EgoLookup *el;
84 * Handle for identity service.
86 static struct GNUNET_IDENTITY_Handle *identity;
89 * Active operation on identity service.
91 static struct GNUNET_IDENTITY_Operation *id_op;
95 * Task run on shutdown. Cleans up everything.
98 * @param tc scheduler context
101 do_shutdown (void *cls,
102 const struct GNUNET_SCHEDULER_TaskContext *tc)
106 GNUNET_IDENTITY_ego_lookup_cancel (el);
111 GNUNET_IDENTITY_cancel (id_op);
114 if (NULL != lookup_request)
116 GNUNET_GNS_lookup_cancel (lookup_request);
117 lookup_request = NULL;
119 if (NULL != identity)
121 GNUNET_IDENTITY_disconnect (identity);
126 GNUNET_GNS_disconnect (gns);
133 * Function called with the result of a GNS lookup.
135 * @param cls the 'const char *' name that was resolved
136 * @param rd_count number of records returned
137 * @param rd array of @a rd_count records with the results
140 process_lookup_result (void *cls, uint32_t rd_count,
141 const struct GNUNET_NAMESTORE_RecordData *rd)
143 const char *name = cls;
145 const char *typename;
148 lookup_request = NULL;
152 printf ("No results.\n");
157 for (i=0; i<rd_count; i++)
159 if ( (rd[i].record_type != rtype) &&
160 (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
162 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
163 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
166 if (NULL == string_val)
169 "Record %u of type %d malformed, skipping\n",
171 (int) rd[i].record_type);
178 printf ("Got `%s' record: %s\n",
181 GNUNET_free (string_val);
183 GNUNET_SCHEDULER_shutdown ();
188 * Perform the actual resolution, starting with the zone
189 * identified by the given public key and the shorten zone.
191 * @param pkey public key to use for the zone, can be NULL
192 * @param shorten_key private key used for shortening, can be NULL
195 lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
196 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
198 if (NULL != lookup_type)
199 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
201 rtype = GNUNET_DNSPARSER_TYPE_A;
203 if (NULL != lookup_name)
205 lookup_request = GNUNET_GNS_lookup (gns,
209 GNUNET_NO, /* Use DHT */
211 &process_lookup_result,
217 _("Please specify name to lookup!\n"));
218 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
225 * Method called to with the ego we are to use for shortening
228 * @param cls closure contains the public key to use
229 * @param ego ego handle, NULL if not found
230 * @param ctx context for application to store data for this ego
231 * (during the lifetime of this process, initially NULL)
232 * @param name name assigned by the user for this ego,
233 * NULL if the user just deleted the ego and it
234 * must thus no longer be used
237 identity_shorten_cb (void *cls,
238 struct GNUNET_IDENTITY_Ego *ego,
242 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
246 lookup_with_keys (pkeym, NULL);
248 lookup_with_keys (pkeym,
249 GNUNET_IDENTITY_ego_get_private_key (ego));
255 * Perform the actual resolution, starting with the zone
256 * identified by the given public key.
258 * @param pkey public key to use for the zone
261 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
263 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
265 GNUNET_assert (NULL != pkey);
266 pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
268 id_op = GNUNET_IDENTITY_get (identity,
270 &identity_shorten_cb,
275 lookup_with_keys (pkey, NULL);
281 * Method called to with the ego we are to use for the lookup,
282 * when the ego is determined by a name.
284 * @param cls closure (NULL, unused)
285 * @param ego ego handle, NULL if not found
288 identity_zone_cb (void *cls,
289 const struct GNUNET_IDENTITY_Ego *ego)
291 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
297 _("Ego for `%s' not found, cannot perform lookup.\n"),
299 GNUNET_SCHEDULER_shutdown ();
303 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
304 lookup_with_public_key (&pkey);
306 GNUNET_free_non_null (zone_ego_name);
307 zone_ego_name = NULL;
312 * Method called to with the ego we are to use for the lookup,
313 * when the ego is the one for the default master zone.
315 * @param cls closure (NULL, unused)
316 * @param ego ego handle, NULL if not found
317 * @param ctx context for application to store data for this ego
318 * (during the lifetime of this process, initially NULL)
319 * @param name name assigned by the user for this ego,
320 * NULL if the user just deleted the ego and it
321 * must thus no longer be used
324 identity_master_cb (void *cls,
325 struct GNUNET_IDENTITY_Ego *ego,
329 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
335 _("Ego for `master-zone' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
336 GNUNET_SCHEDULER_shutdown ();
339 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
340 lookup_with_public_key (&pkey);
345 * Main function that will be run.
348 * @param args remaining command-line arguments
349 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
350 * @param c configuration
353 run (void *cls, char *const *args, const char *cfgfile,
354 const struct GNUNET_CONFIGURATION_Handle *c)
356 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
359 gns = GNUNET_GNS_connect (cfg);
360 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
364 _("Failed to connect to GNS\n"));
367 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
369 if (NULL != public_key)
372 GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
377 _("Public key `%s' is not well-formed\n"),
379 GNUNET_SCHEDULER_shutdown ();
382 lookup_with_public_key (&pkey);
385 if (NULL != zone_ego_name)
387 el = GNUNET_IDENTITY_ego_lookup (cfg,
393 if ( (NULL != lookup_name) &&
394 (strlen (lookup_name) > 4) &&
395 (0 == strcmp (".zkey",
396 &lookup_name[strlen (lookup_name) - 4])) )
398 /* no zone required, use 'anonymous' zone */
399 GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
401 lookup_with_public_key (&pkey);
405 id_op = GNUNET_IDENTITY_get (identity,
409 GNUNET_assert (NULL != id_op);
415 * The main function for gnunet-gns.
417 * @param argc number of arguments from the command line
418 * @param argv command line arguments
419 * @return 0 ok, 1 on error
422 main (int argc, char *const *argv)
424 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
425 {'u', "lookup", "NAME",
426 gettext_noop ("Lookup a record for the given name"), 1,
427 &GNUNET_GETOPT_set_string, &lookup_name},
428 {'t', "type", "TYPE",
429 gettext_noop ("Specify the type of the record to lookup"), 1,
430 &GNUNET_GETOPT_set_string, &lookup_type},
432 gettext_noop ("No unneeded output"), 0,
433 &GNUNET_GETOPT_set_one, &raw},
434 {'p', "public-key", "PKEY",
435 gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
436 &GNUNET_GETOPT_set_string, &public_key},
437 {'z', "zone", "NAME",
438 gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
439 &GNUNET_GETOPT_set_string, &zone_ego_name},
440 GNUNET_GETOPT_OPTION_END
444 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
447 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
450 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
451 _("GNUnet GNS resolver tool"),
453 &run, NULL)) ? 0 : 1;
454 GNUNET_free ((void*) argv);
458 /* end of gnunet-gns.c */