2 This file is part of GNUnet.
3 (C) 2012 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 shorten. (-s option)
40 static char *shorten_name;
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;
53 * name to look up authority for (-a option)
55 static char *auth_name;
63 * Requested record type.
65 static enum GNUNET_GNS_RecordType rtype;
68 * Handle to lookup request
70 static struct GNUNET_GNS_LookupRequest *lookup_request;
73 * Handle to shorten request
75 static struct GNUNET_GNS_ShortenRequest *shorten_request;
78 * Handle to get authority request
80 static struct GNUNET_GNS_GetAuthRequest *getauth_request;
84 * Task run on shutdown. Cleans up everything.
87 * @param tc scheduler context
90 do_shutdown (void *cls,
91 const struct GNUNET_SCHEDULER_TaskContext *tc)
93 if (NULL != lookup_request)
95 GNUNET_GNS_cancel_lookup_request (lookup_request);
96 lookup_request = NULL;
98 if (NULL != shorten_request)
100 GNUNET_GNS_cancel_shorten_request (shorten_request);
101 shorten_request = NULL;
103 if (NULL != getauth_request)
105 GNUNET_GNS_cancel_get_auth_request (getauth_request);
106 getauth_request = NULL;
110 GNUNET_GNS_disconnect (gns);
117 * Function called with the result of a shorten operation.
120 * @param cls a 'const char *' with the original (long) name
121 * @param nshort the shortened name
124 process_shorten_result (void* cls, const char *nshort)
126 const char *original_name = cls;
128 shorten_request = NULL;
130 printf("%s", nshort);
132 printf("%s shortened to %s\n", original_name, nshort);
133 GNUNET_SCHEDULER_shutdown ();
138 * Function called with the result of a GADS lookup.
140 * @param cls the 'const char *' name that was resolved
141 * @param rd_count number of records returned
142 * @param rd array of 'rd_count' records with the results
145 process_lookup_result (void* cls, uint32_t rd_count,
146 const struct GNUNET_NAMESTORE_RecordData *rd)
148 const char* name = cls;
150 const char* typename;
153 lookup_request = NULL;
157 printf("No results.\n");
159 printf("%s:\n", name);
161 for (i=0; i<rd_count; i++)
163 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
164 string_val = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
168 printf ("%s\n", string_val);
170 printf ("Got `%s' record: %s\n", typename, string_val);
171 GNUNET_free_non_null (string_val);
173 GNUNET_SCHEDULER_shutdown ();
178 * Function called with the result of an authority lookup.
180 * @param cls the 'const char *' with the name for which the
181 * authority was resolved
182 * @param auth name of the authority
185 process_auth_result (void* cls,
188 getauth_request = NULL;
189 printf ("%s\n", auth);
190 GNUNET_SCHEDULER_shutdown ();
195 * Main function that will be run.
198 * @param args remaining command-line arguments
199 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
200 * @param cfg configuration
203 run (void *cls, char *const *args, const char *cfgfile,
204 const struct GNUNET_CONFIGURATION_Handle *cfg)
207 struct GNUNET_CRYPTO_EccPrivateKey *key = NULL;
208 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
209 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
210 struct GNUNET_CRYPTO_ShortHashCode user_zone;
211 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
212 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key = NULL;
213 struct GNUNET_CRYPTO_EccPrivateKey *private_key = NULL;
214 struct GNUNET_CRYPTO_ShortHashCode *private_zone = NULL;
215 struct GNUNET_CRYPTO_ShortHashCode *shorten_zone = NULL;
217 gns = GNUNET_GNS_connect (cfg);
221 _("Failed to connect to GNS\n"));
224 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
225 "ZONEKEY", &keyfile))
228 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
229 "No private key for root zone found, using default!\n");
233 key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
234 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
235 GNUNET_CRYPTO_short_hash (&pkey,
236 sizeof(struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
239 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Using zone: %s!\n", &zonename);
243 GNUNET_CRYPTO_ecc_key_free (key);
244 GNUNET_free(keyfile);
247 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
248 "SHORTEN_ZONEKEY", &keyfile))
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252 "No shorten key found!\n");
256 shorten_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
257 GNUNET_CRYPTO_ecc_key_get_public (shorten_key, &pkey);
258 shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
259 GNUNET_CRYPTO_short_hash(&pkey,
260 sizeof(struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
262 GNUNET_CRYPTO_short_hash_to_enc (shorten_zone, &zonename);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Using shorten zone: %s!\n", &zonename);
266 GNUNET_free (keyfile);
269 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
270 "PRIVATE_ZONEKEY", &keyfile))
273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
274 "No private zone key file name specified in configuration!\n");
278 private_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
279 GNUNET_CRYPTO_ecc_key_get_public (private_key, &pkey);
280 private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
281 GNUNET_CRYPTO_short_hash(&pkey,
282 sizeof(struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
284 GNUNET_CRYPTO_short_hash_to_enc (private_zone, &zonename);
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287 "Using private zone: %s!\n", &zonename);
288 GNUNET_CRYPTO_ecc_key_free (private_key);
291 if (NULL != lookup_type)
292 rtype = GNUNET_NAMESTORE_typename_to_number (lookup_type);
294 rtype = GNUNET_GNS_RECORD_A;
296 if ( (NULL != shorten_name) &&
297 (NULL != shorten_zone) &&
298 (NULL != private_zone) )
300 shorten_request = GNUNET_GNS_shorten_zone (gns, shorten_name,
304 &process_shorten_result,
307 if (NULL != lookup_name)
309 lookup_request = GNUNET_GNS_lookup_zone (gns, lookup_name,
312 GNUNET_NO, /* Use DHT */
314 &process_lookup_result,
317 if (NULL != auth_name)
319 getauth_request = GNUNET_GNS_get_authority (gns, auth_name,
320 &process_auth_result, auth_name);
323 if (NULL != shorten_key)
324 GNUNET_CRYPTO_ecc_key_free (shorten_key);
325 if (NULL != shorten_zone)
326 GNUNET_free (shorten_zone);
327 if (NULL != private_zone)
328 GNUNET_free (private_zone);
330 if ( (NULL == auth_name) &&
331 (NULL == shorten_name) &&
332 (NULL == lookup_name))
335 _("Please specify lookup, shorten or authority operation!\n"));
336 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
339 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
345 * The main function for gnunet-gns.
347 * @param argc number of arguments from the command line
348 * @param argv command line arguments
349 * @return 0 ok, 1 on error
352 main (int argc, char *const *argv)
354 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
355 {'s', "shorten", "NAME",
356 gettext_noop ("try to shorten a given name"), 1,
357 &GNUNET_GETOPT_set_string, &shorten_name},
358 {'u', "lookup", "NAME",
359 gettext_noop ("Lookup a record for the given name"), 1,
360 &GNUNET_GETOPT_set_string, &lookup_name},
361 {'a', "authority", "NAME",
362 gettext_noop ("Get the authority of a particular name"), 1,
363 &GNUNET_GETOPT_set_string, &auth_name},
364 {'t', "type", "TYPE",
365 gettext_noop ("Specify the type of the record to lookup"), 1,
366 &GNUNET_GETOPT_set_string, &lookup_type},
368 gettext_noop ("No unneeded output"), 0,
369 &GNUNET_GETOPT_set_one, &raw},
370 GNUNET_GETOPT_OPTION_END
374 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
377 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
380 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
381 _("GNUnet GNS access tool"),
383 &run, NULL)) ? 0 : 1;
384 GNUNET_free ((void*) argv);
388 /* end of gnunet-gns.c */