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/>.
20 * @brief command line tool to access distributed GNS
21 * @author Christian Grothoff
24 #include <gnunet_util_lib.h>
25 #include <gnunet_dnsparser_lib.h>
26 #include <gnunet_gnsrecord_lib.h>
27 #include <gnunet_namestore_service.h>
28 #include <gnunet_gns_service.h>
31 * Configuration we are using.
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
36 * Handle to GNS service.
38 static struct GNUNET_GNS_Handle *gns;
41 * GNS name to lookup. (-u option)
43 static char *lookup_name;
46 * record type to look up (-t option)
48 static char *lookup_type;
56 * Desired record type.
58 static uint32_t rtype;
61 * Handle to lookup request
63 static struct GNUNET_GNS_LookupWithTldRequest *lr;
66 * Global return value.
67 * 0 on success (default),
68 * 1 on internal failures, 2 on launch failure,
69 * 3 if the name is not a GNS-supported TLD,
71 static int global_ret;
75 * Task run on shutdown. Cleans up everything.
80 do_shutdown (void *cls)
85 GNUNET_GNS_lookup_with_tld_cancel (lr);
90 GNUNET_GNS_disconnect (gns);
97 * Function called with the result of a GNS lookup.
99 * @param cls the 'const char *' name that was resolved
100 * @param was_gns #GNUNET_NO if TLD did not indicate use of GNS
101 * @param rd_count number of records returned
102 * @param rd array of @a rd_count records with the results
105 process_lookup_result (void *cls,
108 const struct GNUNET_GNSRECORD_Data *rd)
110 const char *name = cls;
111 const char *typename;
115 if (GNUNET_NO == was_gns)
118 GNUNET_SCHEDULER_shutdown ();
124 printf ("No results.\n");
129 for (uint32_t i=0; i<rd_count; i++)
131 if ( (rd[i].record_type != rtype) &&
132 (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
134 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
135 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
138 if (NULL == string_val)
141 "Record %u of type %d malformed, skipping\n",
143 (int) rd[i].record_type);
150 printf ("Got `%s' record: %s\n",
153 GNUNET_free (string_val);
155 GNUNET_SCHEDULER_shutdown ();
160 * Main function that will be run.
163 * @param args remaining command-line arguments
164 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
165 * @param c configuration
171 const struct GNUNET_CONFIGURATION_Handle *c)
178 gns = GNUNET_GNS_connect (cfg);
182 _("Failed to connect to GNS\n"));
187 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
190 if (NULL != lookup_type)
191 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
193 rtype = GNUNET_DNSPARSER_TYPE_A;
194 if (UINT32_MAX == rtype)
197 _("Invalid typename specified, assuming `ANY'\n"));
198 rtype = GNUNET_GNSRECORD_TYPE_ANY;
200 lr = GNUNET_GNS_lookup_with_tld (gns,
203 GNUNET_GNS_LO_DEFAULT,
204 &process_lookup_result,
209 GNUNET_SCHEDULER_shutdown ();
216 * The main function for gnunet-gns.
218 * @param argc number of arguments from the command line
219 * @param argv command line arguments
220 * @return 0 ok, 1 on error
226 struct GNUNET_GETOPT_CommandLineOption options[] = {
227 GNUNET_GETOPT_option_mandatory
228 (GNUNET_GETOPT_option_string ('u',
231 gettext_noop ("Lookup a record for the given name"),
233 GNUNET_GETOPT_option_string ('t',
236 gettext_noop ("Specify the type of the record to lookup"),
238 GNUNET_GETOPT_option_flag ('r',
240 gettext_noop ("No unneeded output"),
242 GNUNET_GETOPT_OPTION_END
247 GNUNET_STRINGS_get_utf8_args (argc, argv,
251 GNUNET_log_setup ("gnunet-gns",
254 ret = GNUNET_PROGRAM_run (argc, argv,
256 _("GNUnet GNS resolver tool"),
259 GNUNET_free ((void*) argv);
260 if (GNUNET_OK != ret)
265 /* end of gnunet-gns.c */