GNUnet  0.19.4
gnunet-gns.c File Reference

binary version of gnunet-gns-import.sh (for OSes that have no POSIX shell). More...

Include dependency graph for gnunet-gns.c:

Go to the source code of this file.

Functions

static void do_shutdown (void *cls)
 Task run on shutdown. More...
 
static void do_timeout (void *cls)
 Task to run on timeout. More...
 
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. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
 Main function that will be run. More...
 
int main (int argc, char *const *argv)
 The main function for gnunet-gns. More...
 

Variables

static const struct GNUNET_CONFIGURATION_Handlecfg
 Configuration we are using. More...
 
static struct GNUNET_GNS_Handlegns
 Handle to GNS service. More...
 
static char * lookup_name
 GNS name to lookup. More...
 
char * idna_name
 DNS IDNA name to lookup. More...
 
static int dns_compat
 DNS compatibility (name is given as DNS name, possible IDNA). More...
 
static char * lookup_type
 record type to look up (-t option) More...
 
static int raw
 raw output More...
 
static uint32_t rtype
 Desired record type. More...
 
static struct GNUNET_TIME_Relative timeout
 Timeout for lookup. More...
 
static struct GNUNET_SCHEDULER_Taskto_task
 Timeout task. More...
 
static struct GNUNET_GNS_LookupWithTldRequestlr
 Handle to lookup request. More...
 
static int global_ret
 Global return value. More...
 

Detailed Description

binary version of gnunet-gns-import.sh (for OSes that have no POSIX shell).

command line tool to access distributed GNS

Author
LRN
Christian Grothoff

Definition in file gnunet-gns.c.

Function Documentation

◆ do_shutdown()

static void do_shutdown ( void *  cls)
static

Task run on shutdown.

Cleans up everything.

Parameters
clsunused

Definition at line 116 of file gnunet-gns.c.

117 {
118  (void) cls;
119  if (NULL != to_task)
120  {
122  to_task = NULL;
123  }
124  if (NULL != lr)
125  {
127  lr = NULL;
128  }
129  if (NULL != gns)
130  {
132  gns = NULL;
133  }
134  if (NULL != idna_name)
135  {
137  idna_name = NULL;
138  }
139 }
static struct GNUNET_GNS_Handle * gns
Handle to GNS service.
Definition: gnunet-gns.c:53
static struct GNUNET_SCHEDULER_Task * to_task
Timeout task.
Definition: gnunet-gns.c:93
char * idna_name
DNS IDNA name to lookup.
Definition: gnunet-gns.c:63
static struct GNUNET_GNS_LookupWithTldRequest * lr
Handle to lookup request.
Definition: gnunet-gns.c:98
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
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975

References gns, GNUNET_free, GNUNET_GNS_disconnect(), GNUNET_GNS_lookup_with_tld_cancel(), GNUNET_SCHEDULER_cancel(), idna_name, lr, and to_task.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_timeout()

static void do_timeout ( void *  cls)
static

Task to run on timeout.

Parameters
clsunused

Definition at line 148 of file gnunet-gns.c.

149 {
150  to_task = NULL;
151  global_ret = 3; // Timeout
153 }
static int global_ret
Global return value.
Definition: gnunet-gns.c:107
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:562

References global_ret, GNUNET_SCHEDULER_shutdown(), and to_task.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_lookup_result()

static void process_lookup_result ( void *  cls,
int  was_gns,
uint32_t  rd_count,
const struct GNUNET_GNSRECORD_Data rd 
)
static

Function called with the result of a GNS lookup.

Parameters
clsthe 'const char *' name that was resolved
was_gnsGNUNET_NO if TLD did not indicate use of GNS
rd_countnumber of records returned
rdarray of rd_count records with the results

Definition at line 165 of file gnunet-gns.c.

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 */
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  }
215 }
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
static int raw
raw output
Definition: gnunet-gns.c:78
static uint32_t rtype
Desired record type.
Definition: gnunet-gns.c:83
uint32_t data
The data value.
static unsigned int rd_count
Number of records for currently parsed set.
static struct GNUNET_GNSRECORD_Data rd[50]
The record data under a single label.
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number to the corresponding type string (e.g.
Definition: gnsrecord.c:227
@ GNUNET_GNSRECORD_RF_SUPPLEMENTAL
This is a supplemental record.
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:155
#define GNUNET_GNSRECORD_TYPE_ANY
Record type indicating any record/'*'.
@ GNUNET_NO
const char * name

References data, data_size, global_ret, GNUNET_free, GNUNET_GNSRECORD_number_to_typename(), GNUNET_GNSRECORD_RF_SUPPLEMENTAL, GNUNET_GNSRECORD_TYPE_ANY, GNUNET_GNSRECORD_value_to_string(), GNUNET_NO, GNUNET_SCHEDULER_shutdown(), lr, name, raw, rd, rd_count, and rtype.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle c 
)
static

Main function that will be run.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cconfiguration

If DNS compatibility is requested, we first verify that the lookup_name is in a DNS format. If yes, we convert it to UTF-8.

Definition at line 227 of file gnunet-gns.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 
249  if (GNUNET_YES == dns_compat)
250  {
251  Idna_rc rc;
252 
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  }
273  }
274 
275  if (GNUNET_YES !=
277  "arm"))
278  {
280  _ ("Cannot resolve using GNS: GNUnet peer not running\n"));
281  global_ret = 5;
282  return;
283  }
285  &do_timeout,
286  NULL);
288  if (NULL == gns)
289  {
290  fprintf (stderr,
291  _ ("Failed to connect to GNS\n"));
292  global_ret = 2;
293  return;
294  }
296  NULL);
297  if (NULL != lookup_type)
299  else
301  if (UINT32_MAX == rtype)
302  {
303  fprintf (stderr,
304  _ ("Invalid typename specified, assuming `ANY'\n"));
306  }
308  lookup_name,
309  rtype,
312  lookup_name);
313  if (NULL == lr)
314  {
315  global_ret = 2;
317  return;
318  }
319 }
static void do_timeout(void *cls)
Task to run on timeout.
Definition: gnunet-gns.c:148
static char * lookup_name
GNS name to lookup.
Definition: gnunet-gns.c:58
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-gns.c:48
static void do_shutdown(void *cls)
Task run on shutdown.
Definition: gnunet-gns.c:116
static int dns_compat
DNS compatibility (name is given as DNS name, possible IDNA).
Definition: gnunet-gns.c:68
static struct GNUNET_TIME_Relative timeout
Timeout for lookup.
Definition: gnunet-gns.c:88
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:165
static char * lookup_type
record type to look up (-t option)
Definition: gnunet-gns.c:73
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:903
#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_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.
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (e.g.
Definition: gnsrecord.c:200
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_ERROR_TYPE_WARNING
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:1334
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:1272
#define _(String)
GNU gettext support macro.
Definition: platform.h:177

References _, consensus-simulation::args, cfg, dns_compat, do_shutdown(), do_timeout(), global_ret, gns, GNUNET_CLIENT_test(), GNUNET_DNSPARSER_check_name(), GNUNET_DNSPARSER_TYPE_A, GNUNET_ERROR_TYPE_WARNING, GNUNET_GNS_connect(), GNUNET_GNS_LO_DEFAULT, GNUNET_GNS_lookup_with_tld(), GNUNET_GNSRECORD_TYPE_ANY, GNUNET_GNSRECORD_typename_to_number(), GNUNET_log, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_YES, idna_name, lookup_name, lookup_type, lr, process_lookup_result(), rtype, timeout, and to_task.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

The main function for gnunet-gns.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 330 of file gnunet-gns.c.

331 {
336  "lookup",
337  "NAME",
338  gettext_noop (
339  "Lookup a record for the given name"),
340  &lookup_name)),
342  "type",
343  "TYPE",
344  gettext_noop (
345  "Specify the type of the record to lookup"),
346  &lookup_type),
348  "timeout",
349  "TIMEOUT",
350  gettext_noop (
351  "Specify a timeout for the lookup"),
352  &timeout),
354  "raw",
355  gettext_noop ("No unneeded output"),
356  &raw),
358  "dns",
359  gettext_noop (
360  "DNS Compatibility: Name is passed in IDNA instead of UTF-8"),
361  &dns_compat),
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 }
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:70
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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:227
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.
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
#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:400
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:1222
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
Definition of a command line option.

References _, dns_compat, gettext_noop, global_ret, GNUNET_free_nz, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_flag(), GNUNET_GETOPT_option_mandatory(), GNUNET_GETOPT_option_relative_time(), GNUNET_GETOPT_option_string(), GNUNET_log_setup(), GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), GNUNET_TIME_UNIT_FOREVER_REL, lookup_name, lookup_type, options, raw, ret, run(), and timeout.

Here is the call graph for this function:

Variable Documentation

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Configuration we are using.

Definition at line 48 of file gnunet-gns.c.

Referenced by run().

◆ gns

struct GNUNET_GNS_Handle* gns
static

Handle to GNS service.

Definition at line 53 of file gnunet-gns.c.

Referenced by do_shutdown(), and run().

◆ lookup_name

char* lookup_name
static

GNS name to lookup.

(-u option)

Definition at line 58 of file gnunet-gns.c.

Referenced by main(), and run().

◆ idna_name

char* idna_name

DNS IDNA name to lookup.

(set if -d option is set)

Definition at line 63 of file gnunet-gns.c.

Referenced by do_shutdown(), GNUNET_DNSPARSER_builder_add_name(), and run().

◆ dns_compat

int dns_compat
static

DNS compatibility (name is given as DNS name, possible IDNA).

Definition at line 68 of file gnunet-gns.c.

Referenced by main(), and run().

◆ lookup_type

char* lookup_type
static

record type to look up (-t option)

Definition at line 73 of file gnunet-gns.c.

Referenced by main(), and run().

◆ raw

int raw
static

◆ rtype

uint32_t rtype
static

Desired record type.

Definition at line 83 of file gnunet-gns.c.

Referenced by process_lookup_result(), and run().

◆ timeout

struct GNUNET_TIME_Relative timeout
static

Timeout for lookup.

Definition at line 83 of file gnunet-gns.c.

Referenced by main(), and run().

◆ to_task

struct GNUNET_SCHEDULER_Task* to_task
static

Timeout task.

Definition at line 93 of file gnunet-gns.c.

Referenced by do_shutdown(), do_timeout(), and run().

◆ lr

◆ global_ret

int global_ret
static

Global return value.

0 on success (default), 1 on internal failures 2 on launch failure, 4 if the name is not a GNS-supported TLD,

Definition at line 107 of file gnunet-gns.c.

Referenced by do_timeout(), main(), process_lookup_result(), and run().