GNUnet  0.11.x
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 #include <gnunet_util_lib.h>
27 #include <gnunet_dnsparser_lib.h>
28 #include <gnunet_gnsrecord_lib.h>
30 #include <gnunet_gns_service.h>
31 
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
36 
40 static struct GNUNET_GNS_Handle *gns;
41 
45 static char *lookup_name;
46 
50 static char *lookup_type;
51 
55 static int raw;
56 
60 static uint32_t rtype;
61 
66 
71 
76 
84 static int global_ret;
85 
86 
92 static void
93 do_shutdown (void *cls)
94 {
95  (void) cls;
96  if (NULL != to_task)
97  {
98  GNUNET_SCHEDULER_cancel (to_task);
99  to_task = NULL;
100  }
101  if (NULL != lr)
102  {
104  lr = NULL;
105  }
106  if (NULL != gns)
107  {
108  GNUNET_GNS_disconnect (gns);
109  gns = NULL;
110  }
111 }
112 
113 
119 static void
120 do_timeout (void*cls)
121 {
122  to_task = NULL;
123  global_ret = 3; // Timeout
125 }
126 
127 
136 static void
138  int was_gns,
139  uint32_t rd_count,
140  const struct GNUNET_GNSRECORD_Data *rd)
141 {
142  const char *name = cls;
143  const char *typename;
144  char *string_val;
145 
146  lr = NULL;
147  if (GNUNET_NO == was_gns)
148  {
149  global_ret = 4; /* not for GNS */
151  return;
152  }
153  if (! raw)
154  {
155  if (0 == rd_count)
156  printf ("No results.\n");
157  else
158  printf ("%s:\n", name);
159  }
160  for (uint32_t i = 0; i < rd_count; i++)
161  {
162  if ((rd[i].record_type != rtype) && (GNUNET_GNSRECORD_TYPE_ANY != rtype))
163  continue;
164  typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
165  string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
166  rd[i].data,
167  rd[i].data_size);
168  if (NULL == string_val)
169  {
170  fprintf (stderr,
171  "Record %u of type %d malformed, skipping\n",
172  (unsigned int) i,
173  (int) rd[i].record_type);
174  continue;
175  }
176  if (raw)
177  printf ("%s\n", string_val);
178  else
179  printf ("Got `%s' record: %s\n", typename, string_val);
180  GNUNET_free (string_val);
181  }
183 }
184 
185 
194 static void
195 run (void *cls,
196  char *const *args,
197  const char *cfgfile,
198  const struct GNUNET_CONFIGURATION_Handle *c)
199 {
200  (void) cls;
201  (void) args;
202  (void) cfgfile;
203 
204  cfg = c;
205  to_task = NULL;
206  {
207  char *colon;
208 
209  if (NULL != (colon = strchr (lookup_name, ':')))
210  *colon = '\0';
211  }
213  {
214  fprintf (stderr,
215  _ ("`%s' is not a valid domain name\n"),
216  lookup_name);
217  global_ret = 3;
218  return;
219  }
220  if (GNUNET_YES !=
221  GNUNET_CLIENT_test (cfg,
222  "arm"))
223  {
225  _ ("Cannot resolve using GNS: GNUnet peer not running\n"));
226  global_ret = 2;
227  return;
228  }
230  &do_timeout,
231  NULL);
232  gns = GNUNET_GNS_connect (cfg);
233  if (NULL == gns)
234  {
235  fprintf (stderr,
236  _ ("Failed to connect to GNS\n"));
237  global_ret = 2;
238  return;
239  }
241  NULL);
242  if (NULL != lookup_type)
244  else
246  if (UINT32_MAX == rtype)
247  {
248  fprintf (stderr,
249  _ ("Invalid typename specified, assuming `ANY'\n"));
251  }
252  lr = GNUNET_GNS_lookup_with_tld (gns,
253  lookup_name,
254  rtype,
257  lookup_name);
258  if (NULL == lr)
259  {
260  global_ret = 2;
262  return;
263  }
264 }
265 
266 
274 int
275 main (int argc, char *const *argv)
276 {
278  struct GNUNET_GETOPT_CommandLineOption options[] =
281  "lookup",
282  "NAME",
283  gettext_noop (
284  "Lookup a record for the given name"),
285  &lookup_name)),
287  "type",
288  "TYPE",
289  gettext_noop (
290  "Specify the type of the record to lookup"),
291  &lookup_type),
293  "timeout",
294  "TIMEOUT",
295  gettext_noop (
296  "Specify a timeout for the lookup"),
297  &timeout),
299  "raw",
300  gettext_noop ("No unneeded output"),
301  &raw),
303  int ret;
304 
305  if (GNUNET_OK !=
306  GNUNET_STRINGS_get_utf8_args (argc, argv,
307  &argc, &argv))
308  return 2;
309 
310  GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
311  ret = GNUNET_PROGRAM_run (argc,
312  argv,
313  "gnunet-gns",
314  _ ("GNUnet GNS resolver tool"),
315  options,
316  &run,
317  NULL);
318  GNUNET_free ((void *) argv);
319  if (GNUNET_OK != ret)
320  return 1;
321  return global_ret;
322 }
323 
324 
325 /* end of gnunet-gns.c */
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)...
Connection to the GNS service.
Definition: gns_api.h:35
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:195
static int raw
raw output
Definition: gnunet-gns.c:55
static struct GNUNET_GNS_LookupWithTldRequest * lr
Handle to lookup request.
Definition: gnunet-gns.c:75
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1300
void * GNUNET_GNS_lookup_with_tld_cancel(struct GNUNET_GNS_LookupWithTldRequest *ltr)
Cancel pending lookup request.
Definition: gns_tld_api.c:329
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1438
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (i.e.
Definition: gnsrecord.c:204
int main(int argc, char *const *argv)
The main function for gnunet-gns.
Definition: gnunet-gns.c:275
#define GNUNET_GNSRECORD_TYPE_ANY
Record type indicating any record/&#39;*&#39;.
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
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Definition of a command line option.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:526
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
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:1253
static struct GNUNET_TIME_Relative timeout
Timeout for lookup.
Definition: gnunet-gns.c:65
static struct GNUNET_GNS_Handle * gns
Handle to GNS service.
Definition: gnunet-gns.c:40
void GNUNET_GNS_disconnect(struct GNUNET_GNS_Handle *handle)
Shutdown connection with the GNS service.
Definition: gns_api.c:284
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_mandatory(struct GNUNET_GETOPT_CommandLineOption opt)
Make the given option mandatory.
struct GNUNET_GNS_Handle * GNUNET_GNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the GNS service.
Definition: gns_api.c:262
static struct GNUNET_SCHEDULER_Task * to_task
Timeout task.
Definition: gnunet-gns.c:70
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:892
static void do_shutdown(void *cls)
Task run on shutdown.
Definition: gnunet-gns.c:93
static void do_timeout(void *cls)
Task to run on timeout.
Definition: gnunet-gns.c:120
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
static char * lookup_name
GNS name to lookup.
Definition: gnunet-gns.c:45
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:137
configuration data
Definition: configuration.c:85
const char * name
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.
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
int 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, parse options).
Definition: program.c:367
#define GNUNET_YES
Definition: gnunet_common.h:77
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-gns.c:35
Defaults, look in cache, then in DHT.
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
Handle to a lookup request.
Definition: gns_tld_api.c:44
uint32_t data
The data value.
char * GNUNET_GNSRECORD_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the &#39;value&#39; of a record to a string.
Definition: gnsrecord.c:143
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number (i.e.
Definition: gnsrecord.c:231
static uint32_t rtype
Desired record type.
Definition: gnunet-gns.c:60
#define GNUNET_DNSPARSER_TYPE_A
#define GNUNET_free(ptr)
Wrapper around free.
static char * lookup_type
record type to look up (-t option)
Definition: gnunet-gns.c:50
Time for relative time used by GNUnet, in microseconds.
#define gettext_noop(String)
Definition: gettext.h:69
static int global_ret
Global return value.
Definition: gnunet-gns.c:84
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:966