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;
49 * record type to look up (-t option)
51 static char *lookup_type;
54 * name to look up authority for (-a option)
56 static char *auth_name;
63 static enum GNUNET_GNS_RecordType rtype;
65 /* Handle to lookup request */
66 static struct GNUNET_GNS_LookupRequest *lookup_request;
68 /* Handle to shorten request */
69 static struct GNUNET_GNS_ShortenRequest *shorten_request;
71 /* Handle to get authority request */
72 static struct GNUNET_GNS_GetAuthRequest *getauth_request;
76 * Task run on shutdown. Cleans up everything.
79 * @param tc scheduler context
82 do_shutdown (void *cls,
83 const struct GNUNET_SCHEDULER_TaskContext *tc)
85 if (NULL != lookup_request)
86 GNUNET_GNS_cancel_lookup_request (lookup_request);
88 if (NULL != shorten_request)
89 GNUNET_GNS_cancel_shorten_request (shorten_request);
91 if (NULL != getauth_request)
92 GNUNET_GNS_cancel_get_auth_request (getauth_request);
95 GNUNET_GNS_disconnect (gns);
100 process_shorten_result (void* cls, const char* nshort)
102 const char *original_name = cls;
104 shorten_request = NULL;
106 printf("%s", nshort);
108 printf("%s shortened to %s\n", original_name, nshort);
109 GNUNET_SCHEDULER_shutdown ();
114 process_lookup_result (void* cls, uint32_t rd_count,
115 const struct GNUNET_NAMESTORE_RecordData *rd)
117 const char* name = cls;
119 const char* typename;
122 lookup_request = NULL;
126 printf("No results.\n");
128 printf("%s:\n", name);
130 for (i=0; i<rd_count; i++)
132 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
133 string_val = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
137 printf ("%s\n", string_val);
139 printf ("Got `%s' record: %s\n", typename, string_val);
140 GNUNET_free_non_null (string_val);
142 GNUNET_SCHEDULER_shutdown ();
147 process_auth_result (void* cls, const char* auth)
149 getauth_request = NULL;
150 printf ("%s\n", auth);
151 GNUNET_SCHEDULER_shutdown ();
156 * Main function that will be run.
159 * @param args remaining command-line arguments
160 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
161 * @param cfg configuration
164 run (void *cls, char *const *args, const char *cfgfile,
165 const struct GNUNET_CONFIGURATION_Handle *cfg)
168 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
169 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
170 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
171 struct GNUNET_CRYPTO_ShortHashCode user_zone;
172 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
173 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key = NULL;
174 struct GNUNET_CRYPTO_RsaPrivateKey *private_key = NULL;
175 struct GNUNET_CRYPTO_ShortHashCode *private_zone = NULL;
176 struct GNUNET_CRYPTO_ShortHashCode *shorten_zone = NULL;
178 gns = GNUNET_GNS_connect (cfg);
182 _("Failed to connect to GNS\n"));
185 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
186 "ZONEKEY", &keyfile))
189 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
190 "No private key for root zone found, using default!\n");
194 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
195 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
196 GNUNET_CRYPTO_short_hash (&pkey,
197 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
200 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Using zone: %s!\n", &zonename);
204 GNUNET_CRYPTO_rsa_key_free (key);
205 GNUNET_free(keyfile);
208 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
209 "SHORTEN_ZONEKEY", &keyfile))
212 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
213 "No shorten key found!\n");
217 shorten_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
218 GNUNET_CRYPTO_rsa_key_get_public (shorten_key, &pkey);
219 shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
220 GNUNET_CRYPTO_short_hash(&pkey,
221 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
223 GNUNET_CRYPTO_short_hash_to_enc (shorten_zone, &zonename);
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Using shorten zone: %s!\n", &zonename);
227 GNUNET_free (keyfile);
230 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
231 "PRIVATE_ZONEKEY", &keyfile))
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 "No private zone key file name specified in configuration!\n");
239 private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
240 GNUNET_CRYPTO_rsa_key_get_public (private_key, &pkey);
241 private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
242 GNUNET_CRYPTO_short_hash(&pkey,
243 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
245 GNUNET_CRYPTO_short_hash_to_enc (private_zone, &zonename);
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Using private zone: %s!\n", &zonename);
251 if (NULL != lookup_type)
252 rtype = GNUNET_NAMESTORE_typename_to_number (lookup_type);
254 rtype = GNUNET_GNS_RECORD_A;
256 if ((NULL != shorten_name) && (NULL != shorten_zone) && (NULL != private_zone))
258 shorten_request = GNUNET_GNS_shorten_zone (gns, shorten_name,
262 &process_shorten_result,
265 if (NULL != lookup_name)
267 lookup_request = GNUNET_GNS_lookup_zone (gns, lookup_name,
270 GNUNET_NO, /* Use DHT */
272 &process_lookup_result, lookup_name);
274 if (NULL != auth_name)
276 getauth_request = GNUNET_GNS_get_authority (gns, auth_name,
277 &process_auth_result, auth_name);
280 if (NULL != shorten_key)
281 GNUNET_CRYPTO_rsa_key_free (shorten_key);
282 if (NULL != shorten_zone)
283 GNUNET_free (shorten_zone);
284 if (NULL != private_zone)
285 GNUNET_free (private_zone);
287 if ((NULL == auth_name) &&
288 (NULL == shorten_name) &&
289 (NULL == lookup_name))
292 _("Please specify lookup, shorten or authority operation!\n"));
293 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
297 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
303 * The main function for gnunet-gns.
305 * @param argc number of arguments from the command line
306 * @param argv command line arguments
307 * @return 0 ok, 1 on error
310 main (int argc, char *const *argv)
312 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
313 {'s', "shorten", NULL,
314 gettext_noop ("try to shorten a given name"), 1,
315 &GNUNET_GETOPT_set_string, &shorten_name},
316 {'u', "lookup", NULL,
317 gettext_noop ("Lookup a record for the given name"), 1,
318 &GNUNET_GETOPT_set_string, &lookup_name},
319 {'a', "authority", NULL,
320 gettext_noop ("Get the authority of a particular name"), 1,
321 &GNUNET_GETOPT_set_string, &auth_name},
323 gettext_noop ("Specify the type of the record to lookup"), 1,
324 &GNUNET_GETOPT_set_string, &lookup_type},
326 gettext_noop ("No unneeded output"), 0,
327 &GNUNET_GETOPT_set_one, &raw},
328 GNUNET_GETOPT_OPTION_END
333 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
336 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
339 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
340 _("GNUnet GNS access tool"),
342 &run, NULL)) ? 0 : 1;
347 /* end of gnunet-gns.c */