aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gnunet-gns.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <schanzen@gnunet.org>2023-10-19 11:33:18 +0200
committerMartin Schanzenbach <schanzen@gnunet.org>2023-10-19 11:33:18 +0200
commit7c7d819e8e03dadb91935d5ae91aa921cc7b86c7 (patch)
tree9327ae110e5e64c99901cd853d3d36e23f39aaee /src/gns/gnunet-gns.c
parentdf59c19d712a4339f7c75c76942c1a4f86bf2e5b (diff)
downloadgnunet-7c7d819e8e03dadb91935d5ae91aa921cc7b86c7.tar.gz
gnunet-7c7d819e8e03dadb91935d5ae91aa921cc7b86c7.zip
BUILD: Move gns/zonemaster to service
Diffstat (limited to 'src/gns/gnunet-gns.c')
-rw-r--r--src/gns/gnunet-gns.c385
1 files changed, 0 insertions, 385 deletions
diff --git a/src/gns/gnunet-gns.c b/src/gns/gnunet-gns.c
deleted file mode 100644
index c71676a08..000000000
--- a/src/gns/gnunet-gns.c
+++ /dev/null
@@ -1,385 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file gnunet-gns.c
22 * @brief command line tool to access distributed GNS
23 * @author Christian Grothoff
24 */
25#include "platform.h"
26#if HAVE_LIBIDN2
27#if HAVE_IDN2_H
28#include <idn2.h>
29#elif HAVE_IDN2_IDN2_H
30#include <idn2/idn2.h>
31#endif
32#elif HAVE_LIBIDN
33#if HAVE_IDNA_H
34#include <idna.h>
35#elif HAVE_IDN_IDNA_H
36#include <idn/idna.h>
37#endif
38#endif
39#include <gnunet_util_lib.h>
40#include <gnunet_gnsrecord_lib.h>
41#include <gnunet_namestore_service.h>
42#include <gnunet_gns_service.h>
43
44
45/**
46 * Configuration we are using.
47 */
48static const struct GNUNET_CONFIGURATION_Handle *cfg;
49
50/**
51 * Handle to GNS service.
52 */
53static struct GNUNET_GNS_Handle *gns;
54
55/**
56 * GNS name to lookup. (-u option)
57 */
58static char *lookup_name;
59
60/**
61 * DNS IDNA name to lookup. (set if -d option is set)
62 */
63char *idna_name;
64
65/**
66 * DNS compatibility (name is given as DNS name, possible IDNA).
67 */
68static int dns_compat;
69
70/**
71 * record type to look up (-t option)
72 */
73static char *lookup_type;
74
75/**
76 * raw output
77 */
78static int raw;
79
80/**
81 * Desired record type.
82 */
83static uint32_t rtype;
84
85/**
86 * Timeout for lookup
87 */
88static struct GNUNET_TIME_Relative timeout;
89
90/**
91 * Timeout task
92 */
93static struct GNUNET_SCHEDULER_Task *to_task;
94
95/**
96 * Handle to lookup request
97 */
98static struct GNUNET_GNS_LookupWithTldRequest *lr;
99
100/**
101 * Global return value.
102 * 0 on success (default),
103 * 1 on internal failures
104 * 2 on launch failure,
105 * 4 if the name is not a GNS-supported TLD,
106 */
107static int global_ret;
108
109
110/**
111 * Task run on shutdown. Cleans up everything.
112 *
113 * @param cls unused
114 */
115static void
116do_shutdown (void *cls)
117{
118 (void) cls;
119 if (NULL != to_task)
120 {
121 GNUNET_SCHEDULER_cancel (to_task);
122 to_task = NULL;
123 }
124 if (NULL != lr)
125 {
126 GNUNET_GNS_lookup_with_tld_cancel (lr);
127 lr = NULL;
128 }
129 if (NULL != gns)
130 {
131 GNUNET_GNS_disconnect (gns);
132 gns = NULL;
133 }
134 if (NULL != idna_name)
135 {
136 GNUNET_free (idna_name);
137 idna_name = NULL;
138 }
139}
140
141
142/**
143 * Task to run on timeout
144 *
145 * @param cls unused
146 */
147static void
148do_timeout (void*cls)
149{
150 to_task = NULL;
151 global_ret = 3; // Timeout
152 GNUNET_SCHEDULER_shutdown ();
153}
154
155
156/**
157 * Function called with the result of a GNS lookup.
158 *
159 * @param cls the 'const char *' name that was resolved
160 * @param was_gns #GNUNET_NO if TLD did not indicate use of GNS
161 * @param rd_count number of records returned
162 * @param rd array of @a rd_count records with the results
163 */
164static void
165process_lookup_result (void *cls,
166 int was_gns,
167 uint32_t rd_count,
168 const struct GNUNET_GNSRECORD_Data *rd)
169{
170 const char *name = cls;
171 const char *typename;
172 char *string_val;
173
174 lr = NULL;
175 if (GNUNET_NO == was_gns)
176 {
177 global_ret = 4; /* not for GNS */
178 GNUNET_SCHEDULER_shutdown ();
179 return;
180 }
181 if (! raw)
182 {
183 if (0 == rd_count)
184 printf ("No results.\n");
185 else
186 printf ("%s:\n", name);
187 }
188 for (uint32_t i = 0; i < rd_count; i++)
189 {
190 if ((rd[i].record_type != rtype) && (GNUNET_GNSRECORD_TYPE_ANY != rtype))
191 continue;
192 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
193 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
194 rd[i].data,
195 rd[i].data_size);
196 if (NULL == string_val)
197 {
198 fprintf (stderr,
199 "Record %u of type %d malformed, skipping\n",
200 (unsigned int) i,
201 (int) rd[i].record_type);
202 continue;
203 }
204 if (raw)
205 printf ("%s\n", string_val);
206 else
207 printf ("Got `%s' record: %s%s\n",
208 typename,
209 string_val,
210 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SUPPLEMENTAL)) ?
211 " (supplemental)" : "");
212 GNUNET_free (string_val);
213 }
214 GNUNET_SCHEDULER_shutdown ();
215}
216
217
218/**
219 * Main function that will be run.
220 *
221 * @param cls closure
222 * @param args remaining command-line arguments
223 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
224 * @param c configuration
225 */
226static void
227run (void *cls,
228 char *const *args,
229 const char *cfgfile,
230 const struct GNUNET_CONFIGURATION_Handle *c)
231{
232 (void) cls;
233 (void) args;
234 (void) cfgfile;
235
236 cfg = c;
237 to_task = NULL;
238 {
239 char *colon;
240
241 if (NULL != (colon = strchr (lookup_name, ':')))
242 *colon = '\0';
243 }
244
245 /**
246 * If DNS compatibility is requested, we first verify that the
247 * lookup_name is in a DNS format. If yes, we convert it to UTF-8.
248 */
249 if (GNUNET_YES == dns_compat)
250 {
251 Idna_rc rc;
252
253 if (GNUNET_OK != GNUNET_DNSPARSER_check_name (lookup_name))
254 {
255 fprintf (stderr,
256 _ ("`%s' is not a valid DNS domain name\n"),
257 lookup_name);
258 global_ret = 3;
259 return;
260 }
261 if (IDNA_SUCCESS !=
262 (rc = idna_to_unicode_8z8z (lookup_name, &idna_name,
263 IDNA_ALLOW_UNASSIGNED)))
264 {
265 fprintf (stderr,
266 _ ("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
267 lookup_name,
268 idna_strerror (rc));
269 global_ret = 4;
270 return;
271 }
272 lookup_name = idna_name;
273 }
274
275 if (GNUNET_YES !=
276 GNUNET_CLIENT_test (cfg,
277 "arm"))
278 {
279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
280 _ ("Cannot resolve using GNS: GNUnet peer not running\n"));
281 global_ret = 5;
282 return;
283 }
284 to_task = GNUNET_SCHEDULER_add_delayed (timeout,
285 &do_timeout,
286 NULL);
287 gns = GNUNET_GNS_connect (cfg);
288 if (NULL == gns)
289 {
290 fprintf (stderr,
291 _ ("Failed to connect to GNS\n"));
292 global_ret = 2;
293 return;
294 }
295 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
296 NULL);
297 if (NULL != lookup_type)
298 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
299 else
300 rtype = GNUNET_DNSPARSER_TYPE_A;
301 if (UINT32_MAX == rtype)
302 {
303 fprintf (stderr,
304 _ ("Invalid typename specified, assuming `ANY'\n"));
305 rtype = GNUNET_GNSRECORD_TYPE_ANY;
306 }
307 lr = GNUNET_GNS_lookup_with_tld (gns,
308 lookup_name,
309 rtype,
310 GNUNET_GNS_LO_DEFAULT,
311 &process_lookup_result,
312 lookup_name);
313 if (NULL == lr)
314 {
315 global_ret = 2;
316 GNUNET_SCHEDULER_shutdown ();
317 return;
318 }
319}
320
321
322/**
323 * The main function for gnunet-gns.
324 *
325 * @param argc number of arguments from the command line
326 * @param argv command line arguments
327 * @return 0 ok, 1 on error
328 */
329int
330main (int argc, char *const *argv)
331{
332 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
333 struct GNUNET_GETOPT_CommandLineOption options[] =
334 { GNUNET_GETOPT_option_mandatory (
335 GNUNET_GETOPT_option_string ('u',
336 "lookup",
337 "NAME",
338 gettext_noop (
339 "Lookup a record for the given name"),
340 &lookup_name)),
341 GNUNET_GETOPT_option_string ('t',
342 "type",
343 "TYPE",
344 gettext_noop (
345 "Specify the type of the record to lookup"),
346 &lookup_type),
347 GNUNET_GETOPT_option_relative_time ('T',
348 "timeout",
349 "TIMEOUT",
350 gettext_noop (
351 "Specify a timeout for the lookup"),
352 &timeout),
353 GNUNET_GETOPT_option_flag ('r',
354 "raw",
355 gettext_noop ("No unneeded output"),
356 &raw),
357 GNUNET_GETOPT_option_flag ('d',
358 "dns",
359 gettext_noop (
360 "DNS Compatibility: Name is passed in IDNA instead of UTF-8"),
361 &dns_compat),
362 GNUNET_GETOPT_OPTION_END };
363 int ret;
364
365 if (GNUNET_OK !=
366 GNUNET_STRINGS_get_utf8_args (argc, argv,
367 &argc, &argv))
368 return 2;
369
370 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
371 ret = GNUNET_PROGRAM_run (argc,
372 argv,
373 "gnunet-gns",
374 _ ("GNUnet GNS resolver tool"),
375 options,
376 &run,
377 NULL);
378 GNUNET_free_nz ((void *) argv);
379 if (GNUNET_OK != ret)
380 return 1;
381 return global_ret;
382}
383
384
385/* end of gnunet-gns.c */