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_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;
63 * Handle to lookup request
65 static struct GNUNET_GNS_LookupWithTldRequest *lr;
68 * Global return value.
69 * 0 on success (default),
70 * 1 on internal failures, 2 on launch failure,
71 * 3 if the name is not a GNS-supported TLD,
73 static int global_ret;
77 * Task run on shutdown. Cleans up everything.
82 do_shutdown (void *cls)
87 GNUNET_GNS_lookup_with_tld_cancel (lr);
92 GNUNET_GNS_disconnect (gns);
99 * Function called with the result of a GNS lookup.
101 * @param cls the 'const char *' name that was resolved
102 * @param was_gns #GNUNET_NO if TLD did not indicate use of GNS
103 * @param rd_count number of records returned
104 * @param rd array of @a rd_count records with the results
107 process_lookup_result (void *cls,
110 const struct GNUNET_GNSRECORD_Data *rd)
112 const char *name = cls;
113 const char *typename;
117 if (GNUNET_NO == was_gns)
120 GNUNET_SCHEDULER_shutdown ();
126 printf ("No results.\n");
131 for (uint32_t i=0; i<rd_count; i++)
133 if ( (rd[i].record_type != rtype) &&
134 (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
136 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
137 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
140 if (NULL == string_val)
143 "Record %u of type %d malformed, skipping\n",
145 (int) rd[i].record_type);
152 printf ("Got `%s' record: %s\n",
155 GNUNET_free (string_val);
157 GNUNET_SCHEDULER_shutdown ();
162 * Main function that will be run.
165 * @param args remaining command-line arguments
166 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
167 * @param c configuration
173 const struct GNUNET_CONFIGURATION_Handle *c)
180 gns = GNUNET_GNS_connect (cfg);
184 _("Failed to connect to GNS\n"));
189 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
192 if (NULL != lookup_type)
193 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
195 rtype = GNUNET_DNSPARSER_TYPE_A;
196 if (UINT32_MAX == rtype)
199 _("Invalid typename specified, assuming `ANY'\n"));
200 rtype = GNUNET_GNSRECORD_TYPE_ANY;
202 lr = GNUNET_GNS_lookup_with_tld (gns,
205 GNUNET_GNS_LO_DEFAULT,
206 &process_lookup_result,
211 GNUNET_SCHEDULER_shutdown ();
218 * The main function for gnunet-gns.
220 * @param argc number of arguments from the command line
221 * @param argv command line arguments
222 * @return 0 ok, 1 on error
228 struct GNUNET_GETOPT_CommandLineOption options[] = {
229 GNUNET_GETOPT_option_mandatory
230 (GNUNET_GETOPT_option_string ('u',
233 gettext_noop ("Lookup a record for the given name"),
235 GNUNET_GETOPT_option_string ('t',
238 gettext_noop ("Specify the type of the record to lookup"),
240 GNUNET_GETOPT_option_flag ('r',
242 gettext_noop ("No unneeded output"),
244 GNUNET_GETOPT_OPTION_END
249 GNUNET_STRINGS_get_utf8_args (argc, argv,
253 GNUNET_log_setup ("gnunet-gns",
256 ret = GNUNET_PROGRAM_run (argc, argv,
258 _("GNUnet GNS resolver tool"),
261 GNUNET_free ((void*) argv);
262 if (GNUNET_OK != ret)
267 /* end of gnunet-gns.c */