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
27 #include <gnunet_util_lib.h>
28 #include <gnunet_dnsparser_lib.h>
29 #include <gnunet_namestore_service.h>
30 #include <gnunet_gns_service.h>
33 * Handle to GNS service.
35 static struct GNUNET_GNS_Handle *gns;
38 * GNS name to lookup. (-u option)
40 static char *lookup_name;
43 * record type to look up (-t option)
45 static char *lookup_type;
53 * Requested record type.
58 * Handle to lookup request
60 static struct GNUNET_GNS_LookupRequest *lookup_request;
64 * Task run on shutdown. Cleans up everything.
67 * @param tc scheduler context
70 do_shutdown (void *cls,
71 const struct GNUNET_SCHEDULER_TaskContext *tc)
73 if (NULL != lookup_request)
75 GNUNET_GNS_lookup_cancel (lookup_request);
76 lookup_request = NULL;
80 GNUNET_GNS_disconnect (gns);
87 * Function called with the result of a GADS lookup.
89 * @param cls the 'const char *' name that was resolved
90 * @param rd_count number of records returned
91 * @param rd array of 'rd_count' records with the results
94 process_lookup_result (void *cls, uint32_t rd_count,
95 const struct GNUNET_NAMESTORE_RecordData *rd)
97 const char *name = cls;
102 lookup_request = NULL;
106 printf ("No results.\n");
111 for (i=0; i<rd_count; i++)
113 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
114 string_val = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
121 printf ("Got `%s' record: %s\n",
124 GNUNET_free_non_null (string_val);
126 GNUNET_SCHEDULER_shutdown ();
131 * Main function that will be run.
134 * @param args remaining command-line arguments
135 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
136 * @param cfg configuration
139 run (void *cls, char *const *args, const char *cfgfile,
140 const struct GNUNET_CONFIGURATION_Handle *cfg)
143 struct GNUNET_CRYPTO_EccPrivateKey *key;
144 struct GNUNET_CRYPTO_EccPublicKey pkey;
145 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
147 gns = GNUNET_GNS_connect (cfg);
151 _("Failed to connect to GNS\n"));
154 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
155 "ZONEKEY", &keyfile))
158 "Need zone to perform lookup in!\n");
159 GNUNET_SCHEDULER_shutdown ();
162 /* FIXME: use identity service and/or allow user to specify public key! */
163 key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
164 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
165 GNUNET_CRYPTO_ecc_key_free (key);
166 GNUNET_free (keyfile);
169 GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
170 "SHORTEN_ZONEKEY", &keyfile))
176 // FIXME: use identity service!
177 shorten_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
178 GNUNET_free (keyfile);
181 if (NULL != lookup_type)
182 rtype = GNUNET_NAMESTORE_typename_to_number (lookup_type);
184 rtype = GNUNET_DNSPARSER_TYPE_A;
186 if (NULL != lookup_name)
188 lookup_request = GNUNET_GNS_lookup (gns,
192 GNUNET_NO, /* Use DHT */
194 &process_lookup_result,
200 _("Please specify name to lookup!\n"));
201 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
204 if (NULL != shorten_key)
205 GNUNET_CRYPTO_ecc_key_free (shorten_key);
206 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
212 * The main function for gnunet-gns.
214 * @param argc number of arguments from the command line
215 * @param argv command line arguments
216 * @return 0 ok, 1 on error
219 main (int argc, char *const *argv)
221 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
222 {'u', "lookup", "NAME",
223 gettext_noop ("Lookup a record for the given name"), 1,
224 &GNUNET_GETOPT_set_string, &lookup_name},
225 {'t', "type", "TYPE",
226 gettext_noop ("Specify the type of the record to lookup"), 1,
227 &GNUNET_GETOPT_set_string, &lookup_type},
229 gettext_noop ("No unneeded output"), 0,
230 &GNUNET_GETOPT_set_one, &raw},
231 GNUNET_GETOPT_OPTION_END
235 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
238 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
241 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
242 _("GNUnet GNS resolver tool"),
244 &run, NULL)) ? 0 : 1;
245 GNUNET_free ((void*) argv);
249 /* end of gnunet-gns.c */