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
29 #include <gnunet_util_lib.h>
30 #include <gnunet_dnsparser_lib.h>
31 #include <gnunet_namestore_service.h>
32 #include <gnunet_gns_service.h>
35 * Handle to GNS service.
37 static struct GNUNET_GNS_Handle *gns;
40 * GNS name to shorten. (-s option)
42 static char *shorten_name;
45 * GNS name to lookup. (-u option)
47 static char *lookup_name;
51 * record type to look up (-t option)
53 static char *lookup_type;
56 * name to look up authority for (-a option)
58 static char *auth_name;
65 static enum GNUNET_GNS_RecordType rtype;
68 * Task run on shutdown. Cleans up everything.
71 * @param tc scheduler context
74 do_shutdown (void *cls,
75 const struct GNUNET_SCHEDULER_TaskContext *tc)
79 GNUNET_GNS_disconnect (gns);
86 process_shorten_result(void* cls, const char* nshort)
91 printf("%s shortened to %s\n", (char*) cls, nshort);
92 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
96 process_lookup_result(void* cls, uint32_t rd_count,
97 const struct GNUNET_NAMESTORE_RecordData *rd)
100 char* name = (char*) cls;
101 const char* typename;
106 printf("No results.\n");
108 printf("%s:\n", name);
113 for (i=0; i<rd_count; i++)
115 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
116 string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
120 printf("%s\n", string_val);
122 printf("Got %s record: %s\n", typename, string_val);
126 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
130 process_auth_result(void* cls, const char* auth)
132 printf ("%s\n", auth);
133 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
137 * Main function that will be run.
140 * @param args remaining command-line arguments
141 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
142 * @param cfg configuration
145 run (void *cls, char *const *args, const char *cfgfile,
146 const struct GNUNET_CONFIGURATION_Handle *cfg)
149 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
150 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
151 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
152 struct GNUNET_CRYPTO_ShortHashCode user_zone;
153 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
154 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key = NULL;
155 struct GNUNET_CRYPTO_RsaPrivateKey *private_key = NULL;
156 struct GNUNET_CRYPTO_ShortHashCode private_zone;
157 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
159 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
160 "ZONEKEY", &keyfile))
163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
164 "No private key for root zone found, using default!\n");
169 if (GNUNET_YES == GNUNET_DISK_file_test (keyfile))
171 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
172 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
173 GNUNET_CRYPTO_short_hash(&pkey,
174 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
177 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Using zone: %s!\n", &zonename);
181 GNUNET_CRYPTO_rsa_key_free(key);
183 GNUNET_free(keyfile);
186 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
187 "SHORTEN_ZONEKEY", &keyfile))
190 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
191 "No shorten key found!\n");
196 if (GNUNET_YES == GNUNET_DISK_file_test (keyfile))
198 shorten_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
199 GNUNET_CRYPTO_rsa_key_get_public (shorten_key, &pkey);
200 GNUNET_CRYPTO_short_hash(&pkey,
201 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
203 GNUNET_CRYPTO_short_hash_to_enc (&shorten_zone, &zonename);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Using shorten zone: %s!\n", &zonename);
209 GNUNET_free(keyfile);
213 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
214 "PRIVATE_ZONEKEY", &keyfile))
217 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
218 "No private zone key file name specified in configuration!\n");
223 if (GNUNET_YES == GNUNET_DISK_file_test (keyfile))
225 private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
226 GNUNET_CRYPTO_rsa_key_get_public (private_key, &pkey);
227 GNUNET_CRYPTO_short_hash(&pkey,
228 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
230 GNUNET_CRYPTO_short_hash_to_enc (&private_zone, &zonename);
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Using private zone: %s!\n", &zonename);
237 /* FIXME: shouldn't we just create the private key in this case? */
240 _("Key file `%s' for private zone does not exist!\n"),
244 GNUNET_free(keyfile);
245 if (NULL != private_key)
247 GNUNET_CRYPTO_rsa_key_free (private_key);
253 gns = GNUNET_GNS_connect (cfg);
254 if (NULL != lookup_type)
255 rtype = GNUNET_NAMESTORE_typename_to_number (lookup_type);
257 rtype = GNUNET_GNS_RECORD_A;
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262 _("Failed to connect to GNS\n"));
266 if (NULL != shorten_name)
268 GNUNET_GNS_shorten_zone (gns, shorten_name,
272 &process_shorten_result,
276 if (NULL != lookup_name)
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 GNUNET_GNS_lookup_zone (gns, lookup_name,
285 &process_lookup_result, lookup_name);
288 if (NULL != auth_name)
290 GNUNET_GNS_get_authority(gns, auth_name, &process_auth_result, auth_name);
293 if (NULL != shorten_key)
294 GNUNET_CRYPTO_rsa_key_free (shorten_key);
296 // FIXME: do work here...
297 //GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
302 * The main function for gnunet-gns.
304 * @param argc number of arguments from the command line
305 * @param argv command line arguments
306 * @return 0 ok, 1 on error
309 main (int argc, char *const *argv)
311 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
312 {'s', "shorten", NULL,
313 gettext_noop ("try to shorten a given GNS name"), 1,
314 &GNUNET_GETOPT_set_string, &shorten_name},
315 {'u', "lookup", NULL,
316 gettext_noop ("Lookup a record using GNS (NOT IMPLEMENTED)"), 1,
317 &GNUNET_GETOPT_set_string, &lookup_name},
318 {'a', "authority", NULL,
319 gettext_noop ("Get the authority of a particular name"), 1,
320 &GNUNET_GETOPT_set_string, &auth_name},
322 gettext_noop ("Specify the type of the record lookup"), 1,
323 &GNUNET_GETOPT_set_string, &lookup_type},
325 gettext_noop ("No unneeded output"), 0,
326 &GNUNET_GETOPT_set_one, &raw},
327 GNUNET_GETOPT_OPTION_END
332 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
335 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
338 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
339 _("GNUnet GNS access tool"),
341 &run, NULL)) ? 0 : 1;
346 /* end of gnunet-gns.c */