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)
88 printf("%s shortened to %s\n", (char*) cls, nshort);
89 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
93 process_lookup_result(void* cls, uint32_t rd_count,
94 const struct GNUNET_NAMESTORE_RecordData *rd)
97 char* name = (char*) cls;
103 printf("No results.\n");
105 printf("%s:\n", name);
110 for (i=0; i<rd_count; i++)
112 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
113 string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
117 printf("%s\n", string_val);
119 printf("Got %s record: %s\n", typename, string_val);
123 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
127 process_auth_result(void* cls, const char* auth)
129 printf ("%s\n", auth);
130 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
134 * Main function that will be run.
137 * @param args remaining command-line arguments
138 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
139 * @param cfg configuration
142 run (void *cls, char *const *args, const char *cfgfile,
143 const struct GNUNET_CONFIGURATION_Handle *cfg)
146 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
147 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
148 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
149 struct GNUNET_CRYPTO_ShortHashCode user_zone;
150 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
152 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
153 "ZONEKEY", &keyfile))
156 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
157 "No private key for root zone found, using default!\n");
162 if (GNUNET_YES == GNUNET_DISK_file_test (keyfile))
164 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
165 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
166 GNUNET_CRYPTO_short_hash(&pkey,
167 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
170 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 "Using zone: %s!\n", &zonename);
175 GNUNET_CRYPTO_rsa_key_free(key);
177 GNUNET_free(keyfile);
180 gns = GNUNET_GNS_connect (cfg);
181 if (lookup_type != NULL)
182 rtype = GNUNET_NAMESTORE_typename_to_number(lookup_type);
184 rtype = GNUNET_GNS_RECORD_TYPE_A;
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 _("Failed to connect to GNS\n"));
193 if (shorten_name != NULL)
196 GNUNET_GNS_shorten_zone (gns, shorten_name, zone, &process_shorten_result,
200 if (lookup_name != NULL)
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 GNUNET_GNS_lookup_zone (gns, lookup_name, zone, rtype,
205 &process_lookup_result, lookup_name);
208 if (auth_name != NULL)
210 GNUNET_GNS_get_authority(gns, auth_name, &process_auth_result, auth_name);
213 // FIXME: do work here...
214 //GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
219 * The main function for gnunet-gns.
221 * @param argc number of arguments from the command line
222 * @param argv command line arguments
223 * @return 0 ok, 1 on error
226 main (int argc, char *const *argv)
228 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
229 {'s', "shorten", NULL,
230 gettext_noop ("try to shorten a given GNS name"), 1,
231 &GNUNET_GETOPT_set_string, &shorten_name},
232 {'u', "lookup", NULL,
233 gettext_noop ("Lookup a record using GNS (NOT IMPLEMENTED)"), 1,
234 &GNUNET_GETOPT_set_string, &lookup_name},
235 {'a', "authority", NULL,
236 gettext_noop ("Get the authority of a particular name"), 1,
237 &GNUNET_GETOPT_set_string, &auth_name},
239 gettext_noop ("Specify the type of the record lookup"), 1,
240 &GNUNET_GETOPT_set_string, &lookup_type},
242 gettext_noop ("No unneeded output"), 0,
243 &GNUNET_GETOPT_set_one, &raw},
244 GNUNET_GETOPT_OPTION_END
249 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
252 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
253 _("GNUnet GNS access tool"),
255 &run, NULL)) ? 0 : 1;
260 /* end of gnunet-gns.c */