2 This file is part of GNUnet.
3 Copyright (C) 2012-2013, 2017-2018 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 * Set to GNUNET_GNS_LO_LOCAL_MASTER if we are looking up in the master zone.
61 static enum GNUNET_GNS_LocalOptions local_options;
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;
94 * Task scheduled to handle timeout.
96 static struct GNUNET_SCHEDULER_Task *tt;
99 * Global return value.
100 * 0 on success (default),
101 * 1 on internal failures, 2 on launch failure,
102 * 3 if the name is not a GNS-supported TLD,
105 static int global_ret;
109 * Task run on shutdown. Cleans up everything.
114 do_shutdown (void *cls)
118 GNUNET_IDENTITY_ego_lookup_cancel (el);
123 GNUNET_IDENTITY_cancel (id_op);
126 if (NULL != lookup_request)
128 GNUNET_GNS_lookup_cancel (lookup_request);
129 lookup_request = NULL;
131 if (NULL != identity)
133 GNUNET_IDENTITY_disconnect (identity);
138 GNUNET_GNS_disconnect (gns);
143 GNUNET_SCHEDULER_cancel (tt);
150 * Task run on timeout. Triggers shutdown.
155 do_timeout (void *cls)
158 GNUNET_SCHEDULER_shutdown ();
164 * Function called with the result of a GNS lookup.
166 * @param cls the 'const char *' name that was resolved
167 * @param rd_count number of records returned
168 * @param rd array of @a rd_count records with the results
171 process_lookup_result (void *cls,
173 const struct GNUNET_GNSRECORD_Data *rd)
175 const char *name = cls;
176 const char *typename;
179 lookup_request = NULL;
183 printf ("No results.\n");
188 for (uint32_t i=0; i<rd_count; i++)
190 if ( (rd[i].record_type != rtype) &&
191 (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
193 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
194 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
197 if (NULL == string_val)
200 "Record %u of type %d malformed, skipping\n",
202 (int) rd[i].record_type);
209 printf ("Got `%s' record: %s\n",
212 GNUNET_free (string_val);
214 GNUNET_SCHEDULER_shutdown ();
219 * Perform the actual resolution, starting with the zone
220 * identified by the given public key and the shorten zone.
222 * @param pkey public key to use for the zone, can be NULL
225 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
227 if (NULL != lookup_type)
228 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
230 rtype = GNUNET_DNSPARSER_TYPE_A;
231 if (UINT32_MAX == rtype)
234 _("Invalid typename specified, assuming `ANY'\n"));
235 rtype = GNUNET_GNSRECORD_TYPE_ANY;
238 if (NULL != lookup_name)
240 lookup_request = GNUNET_GNS_lookup (gns,
245 &process_lookup_result,
251 _("Please specify name to lookup!\n"));
252 GNUNET_SCHEDULER_shutdown ();
259 * Method called to with the ego we are to use for the lookup,
260 * when the ego is determined by a name.
262 * @param cls closure (NULL, unused)
263 * @param ego ego handle, NULL if not found
266 identity_zone_cb (void *cls,
267 const struct GNUNET_IDENTITY_Ego *ego)
269 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
274 global_ret = 3; /* Not a GNS TLD */
275 GNUNET_SCHEDULER_shutdown ();
279 GNUNET_IDENTITY_ego_get_public_key (ego,
281 lookup_with_public_key (&pkey);
287 * Obtain the TLD of the given @a name.
290 * @return the part of @a name after the last ".",
291 * or @a name if @a name does not contain a "."
294 get_tld (const char *name)
299 (unsigned char) '.');
303 tld++; /* skip the '.' */
309 * Eat the TLD of the given @a name.
318 GNUNET_assert (0 < strlen (name));
320 (unsigned char) '.');
323 GNUNET_GNS_MASTERZONE_STR);
330 * Main function that will be run.
333 * @param args remaining command-line arguments
334 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
335 * @param c configuration
341 const struct GNUNET_CONFIGURATION_Handle *c)
343 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
349 gns = GNUNET_GNS_connect (cfg);
353 _("Failed to connect to GNS\n"));
356 tt = GNUNET_SCHEDULER_add_delayed (timeout,
359 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
361 /* start with trivial case: TLD is zkey */
362 tld = get_tld (lookup_name);
364 GNUNET_CRYPTO_ecdsa_public_key_from_string (tld,
368 eat_tld (lookup_name);
369 lookup_with_public_key (&pkey);
373 /* second case: TLD is mapped in our configuration file */
374 GNUNET_asprintf (&dot_tld,
378 GNUNET_CONFIGURATION_get_value_string (cfg,
384 GNUNET_CRYPTO_ecdsa_public_key_from_string (zonestr,
388 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
391 _("Expected a base32-encoded public zone key\n"));
392 GNUNET_free (zonestr);
393 GNUNET_free (dot_tld);
394 GNUNET_SCHEDULER_shutdown ();
397 GNUNET_free (dot_tld);
398 GNUNET_free (zonestr);
399 eat_tld (lookup_name);
400 lookup_with_public_key (&pkey);
403 GNUNET_free (dot_tld);
405 /* Final case: TLD matches one of our egos */
406 eat_tld (lookup_name);
408 /* if the name is of the form 'label.gnu', never go to the DHT */
409 if (NULL == strchr (lookup_name,
410 (unsigned char) '.'))
411 local_options = GNUNET_GNS_LO_NO_DHT;
412 identity = GNUNET_IDENTITY_connect (cfg,
415 el = GNUNET_IDENTITY_ego_lookup (cfg,
423 * The main function for gnunet-gns.
425 * @param argc number of arguments from the command line
426 * @param argv command line arguments
427 * @return 0 ok, 1 on error
433 struct GNUNET_GETOPT_CommandLineOption options[] = {
434 GNUNET_GETOPT_option_mandatory
435 (GNUNET_GETOPT_option_string ('u',
438 gettext_noop ("Lookup a record for the given name"),
440 GNUNET_GETOPT_option_string ('t',
443 gettext_noop ("Specify the type of the record to lookup"),
445 GNUNET_GETOPT_option_relative_time ('T',
448 gettext_noop ("Specify timeout for the lookup"),
450 GNUNET_GETOPT_option_flag ('r',
452 gettext_noop ("No unneeded output"),
454 GNUNET_GETOPT_OPTION_END
458 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
460 GNUNET_STRINGS_get_utf8_args (argc, argv,
464 GNUNET_log_setup ("gnunet-gns",
467 ret = GNUNET_PROGRAM_run (argc, argv,
469 _("GNUnet GNS resolver tool"),
472 GNUNET_free ((void*) argv);
473 if (GNUNET_OK != ret)
478 /* end of gnunet-gns.c */