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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_gnsrecord_lib.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;
58 * Desired record type.
60 static uint32_t rtype;
65 static struct GNUNET_TIME_Relative timeout;
70 static struct GNUNET_SCHEDULER_Task *to_task;
73 * Handle to lookup request
75 static struct GNUNET_GNS_LookupWithTldRequest *lr;
78 * Global return value.
79 * 0 on success (default),
80 * 1 on internal failures
81 * 2 on launch failure,
82 * 4 if the name is not a GNS-supported TLD,
84 static int global_ret;
88 * Task run on shutdown. Cleans up everything.
93 do_shutdown (void *cls)
98 GNUNET_SCHEDULER_cancel (to_task);
103 GNUNET_GNS_lookup_with_tld_cancel (lr);
108 GNUNET_GNS_disconnect (gns);
115 * Task to run on timeout
120 do_timeout (void*cls)
123 global_ret = 3; // Timeout
124 GNUNET_SCHEDULER_shutdown ();
129 * Function called with the result of a GNS lookup.
131 * @param cls the 'const char *' name that was resolved
132 * @param was_gns #GNUNET_NO if TLD did not indicate use of GNS
133 * @param rd_count number of records returned
134 * @param rd array of @a rd_count records with the results
137 process_lookup_result (void *cls,
140 const struct GNUNET_GNSRECORD_Data *rd)
142 const char *name = cls;
143 const char *typename;
147 if (GNUNET_NO == was_gns)
149 global_ret = 4; /* not for GNS */
150 GNUNET_SCHEDULER_shutdown ();
156 printf ("No results.\n");
158 printf ("%s:\n", name);
160 for (uint32_t i = 0; i < rd_count; i++)
162 if ((rd[i].record_type != rtype) && (GNUNET_GNSRECORD_TYPE_ANY != rtype))
164 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
165 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
168 if (NULL == string_val)
171 "Record %u of type %d malformed, skipping\n",
173 (int) rd[i].record_type);
177 printf ("%s\n", string_val);
179 printf ("Got `%s' record: %s\n", typename, string_val);
180 GNUNET_free (string_val);
182 GNUNET_SCHEDULER_shutdown ();
187 * Main function that will be run.
190 * @param args remaining command-line arguments
191 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
192 * @param c configuration
198 const struct GNUNET_CONFIGURATION_Handle *c)
209 if (NULL != (colon = strchr (lookup_name, ':')))
212 if (GNUNET_OK != GNUNET_DNSPARSER_check_name (lookup_name))
215 _ ("`%s' is not a valid domain name\n"),
221 GNUNET_CLIENT_test (cfg,
224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
225 _ ("Cannot resolve using GNS: GNUnet peer not running\n"));
229 to_task = GNUNET_SCHEDULER_add_delayed (timeout,
232 gns = GNUNET_GNS_connect (cfg);
236 _ ("Failed to connect to GNS\n"));
240 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
242 if (NULL != lookup_type)
243 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
245 rtype = GNUNET_DNSPARSER_TYPE_A;
246 if (UINT32_MAX == rtype)
249 _ ("Invalid typename specified, assuming `ANY'\n"));
250 rtype = GNUNET_GNSRECORD_TYPE_ANY;
252 lr = GNUNET_GNS_lookup_with_tld (gns,
255 GNUNET_GNS_LO_DEFAULT,
256 &process_lookup_result,
261 GNUNET_SCHEDULER_shutdown ();
268 * The main function for gnunet-gns.
270 * @param argc number of arguments from the command line
271 * @param argv command line arguments
272 * @return 0 ok, 1 on error
275 main (int argc, char *const *argv)
277 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
278 struct GNUNET_GETOPT_CommandLineOption options[] =
279 { GNUNET_GETOPT_option_mandatory (
280 GNUNET_GETOPT_option_string ('u',
284 "Lookup a record for the given name"),
286 GNUNET_GETOPT_option_string ('t',
290 "Specify the type of the record to lookup"),
292 GNUNET_GETOPT_option_relative_time ('T',
296 "Specify a timeout for the lookup"),
298 GNUNET_GETOPT_option_flag ('r',
300 gettext_noop ("No unneeded output"),
302 GNUNET_GETOPT_OPTION_END };
306 GNUNET_STRINGS_get_utf8_args (argc, argv,
310 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
311 ret = GNUNET_PROGRAM_run (argc,
314 _ ("GNUnet GNS resolver tool"),
318 GNUNET_free ((void *) argv);
319 if (GNUNET_OK != ret)
325 /* end of gnunet-gns.c */