-fix inconsisten record naming
[oweals/gnunet.git] / src / gns / gnunet-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file gnunet-gns.c
22  * @brief command line tool to access distributed GNS
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - everything
27  */
28 #include "platform.h"
29 #include <gnunet_util_lib.h>
30 #include <gnunet_dnsparser_lib.h>
31 #include <gnunet_namestore_service.h>
32 #include <gnunet_gns_service.h>
33
34 /**
35  * Handle to GNS service.
36  */
37 static struct GNUNET_GNS_Handle *gns;
38
39 /**
40  * GNS name to shorten. (-s option)
41  */
42 static char *shorten_name;
43
44 /**
45  * GNS name to lookup. (-u option)
46  */
47 static char *lookup_name;
48
49
50 /**
51  * record type to look up (-t option)
52  */
53 static char *lookup_type;
54
55 /**
56  * name to look up authority for (-a option)
57  */
58 static char *auth_name;
59
60 /**
61  * raw output
62  */
63 static int raw = 0;
64
65 static enum GNUNET_GNS_RecordType rtype;
66
67 /**
68  * Task run on shutdown.  Cleans up everything.
69  *
70  * @param cls unused
71  * @param tc scheduler context
72  */
73 static void
74 do_shutdown (void *cls,
75              const struct GNUNET_SCHEDULER_TaskContext *tc)
76 {
77   if (NULL != gns)
78   {
79     GNUNET_GNS_disconnect (gns);
80     gns = NULL;
81   }
82 }
83
84
85 static void
86 process_shorten_result(void* cls, const char* nshort)
87 {
88   if (raw)
89     printf("%s", nshort);
90   else
91     printf("%s shortened to %s\n", (char*) cls, nshort);
92   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
93 }
94
95 static void
96 process_lookup_result(void* cls, uint32_t rd_count,
97                       const struct GNUNET_NAMESTORE_RecordData *rd)
98 {
99   int i;
100   char* name = (char*) cls;
101   const char* typename;
102   char* string_val;
103   
104   if (!raw) {
105     if (rd_count == 0)
106       printf("No results.\n");
107     else
108       printf("%s:\n", name);
109   }
110
111
112
113   for (i=0; i<rd_count; i++)
114   {
115     typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
116     string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
117                                                   rd[i].data,
118                                                   rd[i].data_size);
119     if (raw)
120       printf("%s\n", string_val);
121     else
122       printf("Got %s record: %s\n", typename, string_val);
123
124   }
125
126   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
127 }
128
129 static void
130 process_auth_result(void* cls, const char* auth)
131 {
132   printf ("%s\n", auth);
133   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
134 }
135
136 /**
137  * Main function that will be run.
138  *
139  * @param cls closure
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
143  */
144 static void
145 run (void *cls, char *const *args, const char *cfgfile,
146      const struct GNUNET_CONFIGURATION_Handle *cfg)
147 {
148   char* keyfile;
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
156   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
157                                                            "ZONEKEY", &keyfile))
158   {
159     if (!raw)
160       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
161                   "No private key for root zone found, using default!\n");
162     zone = NULL;
163   }
164   else
165   {
166     if (GNUNET_YES == GNUNET_DISK_file_test (keyfile))
167     {
168       key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
169       GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
170       GNUNET_CRYPTO_short_hash(&pkey,
171                          sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
172                          &user_zone);
173       zone = &user_zone;
174       GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
175       if (!raw)
176         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177                     "Using zone: %s!\n", &zonename);
178       GNUNET_CRYPTO_rsa_key_free(key);
179     }
180     GNUNET_free(keyfile);
181   }
182   
183   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
184                                                    "SHORTEN_ZONEKEY", &keyfile))
185   {
186     if (!raw)
187       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188                   "No shorten key found!\n");
189     shorten_key = NULL;
190   }
191   else
192   {
193     if (GNUNET_YES == GNUNET_DISK_file_test (keyfile))
194     {
195       shorten_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
196     }
197     GNUNET_free(keyfile);
198   }
199   
200   gns = GNUNET_GNS_connect (cfg);
201   if (lookup_type != NULL)
202     rtype = GNUNET_NAMESTORE_typename_to_number(lookup_type);
203   else
204     rtype = GNUNET_GNS_RECORD_A;
205
206   if (NULL == gns)
207   {
208     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209                 _("Failed to connect to GNS\n"));
210     return;
211   }
212   
213   if (shorten_name != NULL)
214   {
215     /** shorten name */
216     GNUNET_GNS_shorten_zone (gns, shorten_name,
217                              zone,
218                              &process_shorten_result,
219                              shorten_name);
220   }
221
222   if (lookup_name != NULL)
223   {
224     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225                 "Lookup\n");
226     GNUNET_GNS_lookup_zone (gns, lookup_name,
227                             zone,
228                             rtype,
229                             GNUNET_YES, //Use DHT
230                             shorten_key,
231                             &process_lookup_result, lookup_name);
232   }
233
234   if (auth_name != NULL)
235   {
236     GNUNET_GNS_get_authority(gns, auth_name, &process_auth_result, auth_name);
237   }
238   
239   // FIXME: do work here...
240   //GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
241 }
242
243
244 /**
245  * The main function for gnunet-gns.
246  *
247  * @param argc number of arguments from the command line
248  * @param argv command line arguments
249  * @return 0 ok, 1 on error
250  */
251 int
252 main (int argc, char *const *argv)
253 {
254   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
255     {'s', "shorten", NULL,
256      gettext_noop ("try to shorten a given GNS name"), 1,
257      &GNUNET_GETOPT_set_string, &shorten_name},
258     {'u', "lookup", NULL,
259       gettext_noop ("Lookup a record using GNS (NOT IMPLEMENTED)"), 1,
260       &GNUNET_GETOPT_set_string, &lookup_name},
261     {'a', "authority", NULL,
262       gettext_noop ("Get the authority of a particular name"), 1,
263       &GNUNET_GETOPT_set_string, &auth_name},
264     {'t', "type", NULL,
265       gettext_noop ("Specify the type of the record lookup"), 1,
266       &GNUNET_GETOPT_set_string, &lookup_type},
267     {'r', "raw", NULL,
268       gettext_noop ("No unneeded output"), 0,
269       &GNUNET_GETOPT_set_one, &raw},
270     GNUNET_GETOPT_OPTION_END
271   };
272
273   int ret;
274
275   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
276     return 2;
277
278   GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
279   ret =
280       (GNUNET_OK ==
281        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
282                            _("GNUnet GNS access tool"), 
283                            options,
284                            &run, NULL)) ? 0 : 1;
285
286   return ret;
287 }
288
289 /* end of gnunet-gns.c */