GNUnet  0.17.6
gnunet-gns.c
Go to the documentation of this file.
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  */
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_dnsparser_lib.h>
41 #include <gnunet_gnsrecord_lib.h>
43 #include <gnunet_gns_service.h>
44 
45 
49 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50 
54 static struct GNUNET_GNS_Handle *gns;
55 
59 static char *lookup_name;
60 
64 char *idna_name;
65 
69 static int dns_compat;
70 
74 static char *lookup_type;
75 
79 static int raw;
80 
84 static uint32_t rtype;
85 
89 static struct GNUNET_TIME_Relative timeout;
90 
95 
100 
108 static int global_ret;
109 
110 
116 static void
117 do_shutdown (void *cls)
118 {
119  (void) cls;
120  if (NULL != to_task)
121  {
123  to_task = NULL;
124  }
125  if (NULL != lr)
126  {
128  lr = NULL;
129  }
130  if (NULL != gns)
131  {
133  gns = NULL;
134  }
135  if (NULL != idna_name)
136  {
138  idna_name = NULL;
139  }
140 }
141 
142 
148 static void
149 do_timeout (void*cls)
150 {
151  to_task = NULL;
152  global_ret = 3; // Timeout
154 }
155 
156 
165 static void
167  int was_gns,
168  uint32_t rd_count,
169  const struct GNUNET_GNSRECORD_Data *rd)
170 {
171  const char *name = cls;
172  const char *typename;
173  char *string_val;
174 
175  lr = NULL;
176  if (GNUNET_NO == was_gns)
177  {
178  global_ret = 4; /* not for GNS */
180  return;
181  }
182  if (! raw)
183  {
184  if (0 == rd_count)
185  printf ("No results.\n");
186  else
187  printf ("%s:\n", name);
188  }
189  for (uint32_t i = 0; i < rd_count; i++)
190  {
191  if ((rd[i].record_type != rtype) && (GNUNET_GNSRECORD_TYPE_ANY != rtype))
192  continue;
193  typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
194  string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
195  rd[i].data,
196  rd[i].data_size);
197  if (NULL == string_val)
198  {
199  fprintf (stderr,
200  "Record %u of type %d malformed, skipping\n",
201  (unsigned int) i,
202  (int) rd[i].record_type);
203  continue;
204  }
205  if (raw)
206  printf ("%s\n", string_val);
207  else
208  printf ("Got `%s' record: %s%s\n",
209  typename,
210  string_val,
211  (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SUPPLEMENTAL)) ?
212  " (supplemental)" : "");
213  GNUNET_free (string_val);
214  }
216 }
217 
218 
227 static void
228 run (void *cls,
229  char *const *args,
230  const char *cfgfile,
231  const struct GNUNET_CONFIGURATION_Handle *c)
232 {
233  (void) cls;
234  (void) args;
235  (void) cfgfile;
236 
237  cfg = c;
238  to_task = NULL;
239  {
240  char *colon;
241 
242  if (NULL != (colon = strchr (lookup_name, ':')))
243  *colon = '\0';
244  }
245 
250  if (GNUNET_YES == dns_compat)
251  {
252  Idna_rc rc;
253 
255  {
256  fprintf (stderr,
257  _ ("`%s' is not a valid DNS domain name\n"),
258  lookup_name);
259  global_ret = 3;
260  return;
261  }
262  if (IDNA_SUCCESS !=
263  (rc = idna_to_unicode_8z8z (lookup_name, &idna_name,
264  IDNA_ALLOW_UNASSIGNED)))
265  {
266  fprintf (stderr,
267  _ ("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
268  lookup_name,
269  idna_strerror (rc));
270  global_ret = 4;
271  return;
272  }
274  }
275 
276  if (GNUNET_YES !=
278  "arm"))
279  {
281  _ ("Cannot resolve using GNS: GNUnet peer not running\n"));
282  global_ret = 5;
283  return;
284  }
286  &do_timeout,
287  NULL);
289  if (NULL == gns)
290  {
291  fprintf (stderr,
292  _ ("Failed to connect to GNS\n"));
293  global_ret = 2;
294  return;
295  }
297  NULL);
298  if (NULL != lookup_type)
300  else
302  if (UINT32_MAX == rtype)
303  {
304  fprintf (stderr,
305  _ ("Invalid typename specified, assuming `ANY'\n"));
307  }
309  lookup_name,
310  rtype,
313  lookup_name);
314  if (NULL == lr)
315  {
316  global_ret = 2;
318  return;
319  }
320 }
321 
322 
330 int
331 main (int argc, char *const *argv)
332 {
337  "lookup",
338  "NAME",
339  gettext_noop (
340  "Lookup a record for the given name"),
341  &lookup_name)),
343  "type",
344  "TYPE",
345  gettext_noop (
346  "Specify the type of the record to lookup"),
347  &lookup_type),
349  "timeout",
350  "TIMEOUT",
351  gettext_noop (
352  "Specify a timeout for the lookup"),
353  &timeout),
355  "raw",
356  gettext_noop ("No unneeded output"),
357  &raw),
359  "dns",
360  gettext_noop (
361  "DNS Compatibility: Name is passed in IDNA instead of UTF-8"),
362  &dns_compat),
364  int ret;
365 
366  if (GNUNET_OK !=
367  GNUNET_STRINGS_get_utf8_args (argc, argv,
368  &argc, &argv))
369  return 2;
370 
371  GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
372  ret = GNUNET_PROGRAM_run (argc,
373  argv,
374  "gnunet-gns",
375  _ ("GNUnet GNS resolver tool"),
376  options,
377  &run,
378  NULL);
379  GNUNET_free_nz ((void *) argv);
380  if (GNUNET_OK != ret)
381  return 1;
382  return global_ret;
383 }
384 
385 
386 /* end of gnunet-gns.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:69
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
static struct GNUNET_GNS_Handle * gns
Handle to GNS service.
Definition: gnunet-gns.c:54
static int raw
raw output
Definition: gnunet-gns.c:79
static void do_timeout(void *cls)
Task to run on timeout.
Definition: gnunet-gns.c:149
static char * lookup_name
GNS name to lookup.
Definition: gnunet-gns.c:59
static int global_ret
Global return value.
Definition: gnunet-gns.c:108
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-gns.c:49
static void do_shutdown(void *cls)
Task run on shutdown.
Definition: gnunet-gns.c:117
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run.
Definition: gnunet-gns.c:228
static int dns_compat
DNS compatibility (name is given as DNS name, possible IDNA).
Definition: gnunet-gns.c:69
static struct GNUNET_TIME_Relative timeout
Timeout for lookup.
Definition: gnunet-gns.c:89
static struct GNUNET_SCHEDULER_Task * to_task
Timeout task.
Definition: gnunet-gns.c:94
char * idna_name
DNS IDNA name to lookup.
Definition: gnunet-gns.c:64
int main(int argc, char *const *argv)
The main function for gnunet-gns.
Definition: gnunet-gns.c:331
static void process_lookup_result(void *cls, int was_gns, uint32_t rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Function called with the result of a GNS lookup.
Definition: gnunet-gns.c:166
static char * lookup_type
record type to look up (-t option)
Definition: gnunet-gns.c:74
static struct GNUNET_GNS_LookupWithTldRequest * lr
Handle to lookup request.
Definition: gnunet-gns.c:99
static uint32_t rtype
Desired record type.
Definition: gnunet-gns.c:84
uint32_t data
The data value.
API for helper library to parse DNS packets.
API to the GNS service.
API that can be used to manipulate GNS record data.
API that can be used to store naming information on a GNUnet node;.
int GNUNET_CLIENT_test(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name)
Test if the port or UNIXPATH of the given service_name is in use and thus (most likely) the respectiv...
Definition: client.c:901
#define GNUNET_DNSPARSER_TYPE_A
int GNUNET_DNSPARSER_check_name(const char *name)
Check if a hostname in UTF-8 format can be coded into valid IDNA.
Definition: dnsparser.c:79
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_mandatory(struct GNUNET_GETOPT_CommandLineOption opt)
Make the given option mandatory.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time).
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
void GNUNET_GNS_disconnect(struct GNUNET_GNS_Handle *handle)
Shutdown connection with the GNS service.
Definition: gns_api.c:290
void * GNUNET_GNS_lookup_with_tld_cancel(struct GNUNET_GNS_LookupWithTldRequest *ltr)
Cancel pending lookup request.
Definition: gns_tld_api.c:332
struct GNUNET_GNS_LookupWithTldRequest * GNUNET_GNS_lookup_with_tld(struct GNUNET_GNS_Handle *handle, const char *name, uint32_t type, enum GNUNET_GNS_LocalOptions options, GNUNET_GNS_LookupResultProcessor2 proc, void *proc_cls)
Perform an asynchronous lookup operation on the GNS, determining the zone using the TLD of the given ...
Definition: gns_tld_api.c:241
struct GNUNET_GNS_Handle * GNUNET_GNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the GNS service.
Definition: gns_api.c:268
@ GNUNET_GNS_LO_DEFAULT
Defaults, look in cache, then in DHT.
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number to the corresponding type string (e.g.
Definition: gnsrecord.c:244
char * GNUNET_GNSRECORD_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the 'value' of a record to a string.
Definition: gnsrecord.c:156
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (e.g "AAAA") to the corresponding number.
Definition: gnsrecord.c:217
#define GNUNET_GNSRECORD_TYPE_ANY
Record type indicating any record/'*'.
@ GNUNET_GNSRECORD_RF_SUPPLEMENTAL
This is a supplemental record.
#define GNUNET_log(kind,...)
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_YES
@ GNUNET_NO
Definition: gnunet_common.h:98
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
@ GNUNET_ERROR_TYPE_WARNING
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:399
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:533
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1316
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:957
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1254
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1223
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
const char * name
Definition of a command line option.
Connection to the GNS service.
Definition: gns_api.h:36
Handle to a lookup request.
Definition: gns_tld_api.c:45
Entry in list of pending tasks.
Definition: scheduler.c:135
Time for relative time used by GNUnet, in microseconds.