Changed Namestore API, changed error handling, changed gns record json
[oweals/gnunet.git] / src / gns / gnunet-gns.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2013, 2017-2018 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file gnunet-gns.c
20  * @brief command line tool to access distributed GNS
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include <gnunet_util_lib.h>
25 #include <gnunet_dnsparser_lib.h>
26 #include <gnunet_gnsrecord_lib.h>
27 #include <gnunet_namestore_service.h>
28 #include <gnunet_gns_service.h>
29
30 /**
31  * Configuration we are using.
32  */
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
34
35 /**
36  * Handle to GNS service.
37  */
38 static struct GNUNET_GNS_Handle *gns;
39
40 /**
41  * GNS name to lookup. (-u option)
42  */
43 static char *lookup_name;
44
45 /**
46  * record type to look up (-t option)
47  */
48 static char *lookup_type;
49
50 /**
51  * raw output
52  */
53 static int raw;
54
55 /**
56  * Desired record type.
57  */
58 static uint32_t rtype;
59
60 /**
61  * Handle to lookup request
62  */
63 static struct GNUNET_GNS_LookupWithTldRequest *lr;
64
65 /**
66  * Global return value.
67  * 0 on success (default),
68  * 1 on internal failures
69  * 2 on launch failure,
70  * 4 if the name is not a GNS-supported TLD,
71  */
72 static int global_ret;
73
74
75 /**
76  * Task run on shutdown.  Cleans up everything.
77  *
78  * @param cls unused
79  */
80 static void
81 do_shutdown (void *cls)
82 {
83   (void) cls;
84   if (NULL != lr)
85   {
86     GNUNET_GNS_lookup_with_tld_cancel (lr);
87     lr = NULL;
88   }
89   if (NULL != gns)
90   {
91     GNUNET_GNS_disconnect (gns);
92     gns = NULL;
93   }
94 }
95
96
97 /**
98  * Function called with the result of a GNS lookup.
99  *
100  * @param cls the 'const char *' name that was resolved
101  * @param was_gns #GNUNET_NO if TLD did not indicate use of GNS
102  * @param rd_count number of records returned
103  * @param rd array of @a rd_count records with the results
104  */
105 static void
106 process_lookup_result (void *cls,
107                        int was_gns,
108                        uint32_t rd_count,
109                        const struct GNUNET_GNSRECORD_Data *rd)
110 {
111   const char *name = cls;
112   const char *typename;
113   char* string_val;
114
115   lr = NULL;
116   if (GNUNET_NO == was_gns)
117   {
118     global_ret = 4; /* not for GNS */
119     GNUNET_SCHEDULER_shutdown ();
120     return;
121   }
122   if (! raw)
123   {
124     if (0 == rd_count)
125       printf ("No results.\n");
126     else
127       printf ("%s:\n",
128               name);
129   }
130   for (uint32_t i=0; i<rd_count; i++)
131   {
132     if ( (rd[i].record_type != rtype) &&
133          (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
134       continue;
135     typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
136     string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
137                                                    rd[i].data,
138                                                    rd[i].data_size);
139     if (NULL == string_val)
140     {
141       fprintf (stderr,
142                "Record %u of type %d malformed, skipping\n",
143                (unsigned int) i,
144                (int) rd[i].record_type);
145       continue;
146     }
147     if (raw)
148       printf ("%s\n",
149               string_val);
150     else
151       printf ("Got `%s' record: %s\n",
152               typename,
153               string_val);
154     GNUNET_free (string_val);
155   }
156   GNUNET_SCHEDULER_shutdown ();
157 }
158
159
160 /**
161  * Main function that will be run.
162  *
163  * @param cls closure
164  * @param args remaining command-line arguments
165  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
166  * @param c configuration
167  */
168 static void
169 run (void *cls,
170      char *const *args,
171      const char *cfgfile,
172      const struct GNUNET_CONFIGURATION_Handle *c)
173 {
174   (void) cls;
175   (void) args;
176   (void) cfgfile;
177
178   cfg = c;
179   gns = GNUNET_GNS_connect (cfg);
180   if (NULL == gns)
181   {
182     fprintf (stderr,
183              _("Failed to connect to GNS\n"));
184     global_ret = 2;
185     return;
186   }
187   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
188                                  NULL);
189
190   if (NULL != lookup_type)
191     rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
192   else
193     rtype = GNUNET_DNSPARSER_TYPE_A;
194   if (UINT32_MAX == rtype)
195   {
196     fprintf (stderr,
197              _("Invalid typename specified, assuming `ANY'\n"));
198     rtype = GNUNET_GNSRECORD_TYPE_ANY;
199   }
200   lr = GNUNET_GNS_lookup_with_tld (gns,
201                                    lookup_name,
202                                    rtype,
203                                    GNUNET_GNS_LO_DEFAULT,
204                                    &process_lookup_result,
205                                    lookup_name);
206   if (NULL == lr)
207   {
208     global_ret = 2;
209     GNUNET_SCHEDULER_shutdown ();
210     return;
211   }
212 }
213
214
215 /**
216  * The main function for gnunet-gns.
217  *
218  * @param argc number of arguments from the command line
219  * @param argv command line arguments
220  * @return 0 ok, 1 on error
221  */
222 int
223 main (int argc,
224       char *const *argv)
225 {
226   struct GNUNET_GETOPT_CommandLineOption options[] = {
227     GNUNET_GETOPT_option_mandatory
228     (GNUNET_GETOPT_option_string ('u',
229                                   "lookup",
230                                   "NAME",
231                                   gettext_noop ("Lookup a record for the given name"),
232                                   &lookup_name)),
233     GNUNET_GETOPT_option_string ('t',
234                                  "type",
235                                  "TYPE",
236                                  gettext_noop ("Specify the type of the record to lookup"),
237                                  &lookup_type),
238     GNUNET_GETOPT_option_flag ('r',
239                                "raw",
240                                gettext_noop ("No unneeded output"),
241                                &raw),
242     GNUNET_GETOPT_OPTION_END
243   };
244   int ret;
245
246   if (GNUNET_OK !=
247       GNUNET_STRINGS_get_utf8_args (argc, argv,
248                                     &argc, &argv))
249     return 2;
250
251   GNUNET_log_setup ("gnunet-gns",
252                     "WARNING",
253                     NULL);
254   ret = GNUNET_PROGRAM_run (argc, argv,
255                             "gnunet-gns",
256                             _("GNUnet GNS resolver tool"),
257                             options,
258                             &run, NULL);
259   GNUNET_free ((void*) argv);
260   if (GNUNET_OK != ret)
261     return 1;
262   return global_ret;
263 }
264
265 /* end of gnunet-gns.c */