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_namestore_service.h>
30 #include <gnunet_gns_service.h>
33 * Configuration we are using.
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38 * Handle to GNS service.
40 static struct GNUNET_GNS_Handle *gns;
43 * GNS name to lookup. (-u option)
45 static char *lookup_name;
48 * record type to look up (-t option)
50 static char *lookup_type;
53 * Identity of the zone to use for the lookup (-z option)
55 static char *zone_ego_name;
58 * Public key of the zone to use for the lookup (-p option)
60 static char *public_key;
68 * Requested record type.
73 * Handle to lookup request
75 static struct GNUNET_GNS_LookupRequest *lookup_request;
78 * Lookup an ego with the identity service.
80 static struct GNUNET_IDENTITY_EgoLookup *el;
83 * Handle for identity service.
85 static struct GNUNET_IDENTITY_Handle *identity;
88 * Active operation on identity service.
90 static struct GNUNET_IDENTITY_Operation *id_op;
94 * Task run on shutdown. Cleans up everything.
97 * @param tc scheduler context
100 do_shutdown (void *cls,
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
105 GNUNET_IDENTITY_ego_lookup_cancel (el);
110 GNUNET_IDENTITY_cancel (id_op);
113 if (NULL != lookup_request)
115 GNUNET_GNS_lookup_cancel (lookup_request);
116 lookup_request = NULL;
118 if (NULL != identity)
120 GNUNET_IDENTITY_disconnect (identity);
125 GNUNET_GNS_disconnect (gns);
132 * Function called with the result of a GNS lookup.
134 * @param cls the 'const char *' name that was resolved
135 * @param rd_count number of records returned
136 * @param rd array of @a rd_count records with the results
139 process_lookup_result (void *cls, uint32_t rd_count,
140 const struct GNUNET_NAMESTORE_RecordData *rd)
142 const char *name = cls;
144 const char *typename;
147 lookup_request = NULL;
151 printf ("No results.\n");
156 for (i=0; i<rd_count; i++)
158 if ( (rd[i].record_type != rtype) &&
159 (GNUNET_NAMESTORE_TYPE_ANY != rtype) )
161 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
162 string_val = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
165 if (NULL == string_val)
168 "Record %u of type %d malformed, skipping\n",
170 (int) rd[i].record_type);
177 printf ("Got `%s' record: %s\n",
180 GNUNET_free (string_val);
182 GNUNET_SCHEDULER_shutdown ();
187 * Perform the actual resolution, starting with the zone
188 * identified by the given public key and the shorten zone.
190 * @param pkey public key to use for the zone, can be NULL
191 * @param shorten_key private key used for shortening, can be NULL
194 lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
195 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
197 if (NULL != lookup_type)
198 rtype = GNUNET_NAMESTORE_typename_to_number (lookup_type);
200 rtype = GNUNET_DNSPARSER_TYPE_A;
202 if (NULL != lookup_name)
204 lookup_request = GNUNET_GNS_lookup (gns,
208 GNUNET_NO, /* Use DHT */
210 &process_lookup_result,
216 _("Please specify name to lookup!\n"));
217 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
224 * Method called to with the ego we are to use for shortening
227 * @param cls closure contains the public key to use
228 * @param ego ego handle, NULL if not found
229 * @param ctx context for application to store data for this ego
230 * (during the lifetime of this process, initially NULL)
231 * @param name name assigned by the user for this ego,
232 * NULL if the user just deleted the ego and it
233 * must thus no longer be used
236 identity_shorten_cb (void *cls,
237 struct GNUNET_IDENTITY_Ego *ego,
241 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
245 lookup_with_keys (pkeym, NULL);
247 lookup_with_keys (pkeym,
248 GNUNET_IDENTITY_ego_get_private_key (ego));
254 * Perform the actual resolution, starting with the zone
255 * identified by the given public key.
257 * @param pkey public key to use for the zone
260 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
262 struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
264 GNUNET_assert (NULL != pkey);
265 pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
267 id_op = GNUNET_IDENTITY_get (identity,
269 &identity_shorten_cb,
274 lookup_with_keys (pkey, NULL);
280 * Method called to with the ego we are to use for the lookup,
281 * when the ego is determined by a name.
283 * @param cls closure (NULL, unused)
284 * @param ego ego handle, NULL if not found
287 identity_zone_cb (void *cls,
288 const struct GNUNET_IDENTITY_Ego *ego)
290 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
296 _("Ego for `%s' not found, cannot perform lookup.\n"),
298 GNUNET_SCHEDULER_shutdown ();
302 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
303 lookup_with_public_key (&pkey);
305 GNUNET_free_non_null (zone_ego_name);
306 zone_ego_name = NULL;
311 * Method called to with the ego we are to use for the lookup,
312 * when the ego is the one for the default master zone.
314 * @param cls closure (NULL, unused)
315 * @param ego ego handle, NULL if not found
316 * @param ctx context for application to store data for this ego
317 * (during the lifetime of this process, initially NULL)
318 * @param name name assigned by the user for this ego,
319 * NULL if the user just deleted the ego and it
320 * must thus no longer be used
323 identity_master_cb (void *cls,
324 struct GNUNET_IDENTITY_Ego *ego,
328 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
334 _("Ego for `master-zone' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
335 GNUNET_SCHEDULER_shutdown ();
338 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
339 lookup_with_public_key (&pkey);
344 * Main function that will be run.
347 * @param args remaining command-line arguments
348 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
349 * @param c configuration
352 run (void *cls, char *const *args, const char *cfgfile,
353 const struct GNUNET_CONFIGURATION_Handle *c)
355 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
358 gns = GNUNET_GNS_connect (cfg);
359 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
363 _("Failed to connect to GNS\n"));
366 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
368 if (NULL != public_key)
371 GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
376 _("Public key `%s' is not well-formed\n"),
378 GNUNET_SCHEDULER_shutdown ();
381 lookup_with_public_key (&pkey);
384 if (NULL != zone_ego_name)
386 el = GNUNET_IDENTITY_ego_lookup (cfg,
392 if ( (NULL != lookup_name) &&
393 (strlen (lookup_name) > 4) &&
394 (0 == strcmp (".zkey",
395 &lookup_name[strlen (lookup_name) - 4])) )
397 /* no zone required, use 'anonymous' zone */
398 GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
400 lookup_with_public_key (&pkey);
404 id_op = GNUNET_IDENTITY_get (identity,
408 GNUNET_assert (NULL != id_op);
414 * The main function for gnunet-gns.
416 * @param argc number of arguments from the command line
417 * @param argv command line arguments
418 * @return 0 ok, 1 on error
421 main (int argc, char *const *argv)
423 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
424 {'u', "lookup", "NAME",
425 gettext_noop ("Lookup a record for the given name"), 1,
426 &GNUNET_GETOPT_set_string, &lookup_name},
427 {'t', "type", "TYPE",
428 gettext_noop ("Specify the type of the record to lookup"), 1,
429 &GNUNET_GETOPT_set_string, &lookup_type},
431 gettext_noop ("No unneeded output"), 0,
432 &GNUNET_GETOPT_set_one, &raw},
433 {'p', "public-key", "PKEY",
434 gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
435 &GNUNET_GETOPT_set_string, &public_key},
436 {'z', "zone", "NAME",
437 gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
438 &GNUNET_GETOPT_set_string, &zone_ego_name},
439 GNUNET_GETOPT_OPTION_END
443 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
446 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
449 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
450 _("GNUnet GNS resolver tool"),
452 &run, NULL)) ? 0 : 1;
453 GNUNET_free ((void*) argv);
457 /* end of gnunet-gns.c */