GNUnet 0.22.2
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>
42#include <gnunet_gns_service.h>
43
44
48static const struct GNUNET_CONFIGURATION_Handle *cfg;
49
53static struct GNUNET_GNS_Handle *gns;
54
58static char *lookup_name;
59
64
68static int dns_compat;
69
73static char *lookup_type;
74
78static int raw;
79
83static uint32_t rtype;
84
89
94
99
107static int global_ret;
108
109
115static void
116do_shutdown (void *cls)
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}
140
141
147static void
148do_timeout (void*cls)
149{
150 to_task = NULL;
151 global_ret = 3; // Timeout
153}
154
155
164static void
166 int was_gns,
167 uint32_t rd_count,
168 const struct GNUNET_GNSRECORD_Data *rd)
169{
170 struct GNUNET_TIME_Relative block_exp;
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 }
183 rd_count,
184 rd,
186 if (! raw)
187 {
188 printf ("<<< %u record(s) found:\n\n", rd_count);
189 }
190 for (uint32_t i = 0; i < rd_count; i++)
191 {
193 record_type);
194 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type
195 ,
196 rd[i].data,
197 rd[i].data_size);
198 if (NULL == string_val)
199 {
200 fprintf (stderr,
201 "Record %u of type %d malformed, skipping\n",
202 (unsigned int) i,
203 (int) rd[i].record_type);
204 continue;
205 }
206 if (raw)
207 printf ("%s\n", string_val);
208 else
209 printf ("%s: `%s' %s\n",
210 typename,
211 string_val,
212 (0 != (rd[i].flags
213 &
215 ) ?
216 "(supplemental)" : "");
217 GNUNET_free (string_val);
218 }
219 if (! raw)
220 {
221 if (0 != rd_count)
222 printf ("\nRecord set expires in %s.\n",
224 block_exp, GNUNET_YES));
225 }
227}
228
229
238static void
239run (void *cls,
240 char *const *args,
241 const char *cfgfile,
242 const struct GNUNET_CONFIGURATION_Handle *c)
243{
244 const char *effective_lookup_type;
245 (void) cls;
246 (void) args;
247 (void) cfgfile;
248
249 cfg = c;
250 to_task = NULL;
251 {
252 char *colon;
253
254 if (NULL != (colon = strchr (lookup_name, ':')))
255 *colon = '\0';
256 }
257
262 if (GNUNET_YES == dns_compat)
263 {
264 Idna_rc rc;
265
267 {
268 fprintf (stderr,
269 _ ("`%s' is not a valid DNS domain name\n"),
271 global_ret = 3;
272 return;
273 }
274 if (IDNA_SUCCESS !=
275 (rc = idna_to_unicode_8z8z (lookup_name, &idna_name,
276 IDNA_ALLOW_UNASSIGNED)))
277 {
278 fprintf (stderr,
279 _ (
280 "Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
282 idna_strerror (rc));
283 global_ret = 4;
284 return;
285 }
287 }
288
289 if (GNUNET_YES !=
291 "arm"))
292 {
294 _ (
295 "Cannot resolve using GNS: GNUnet peer not running\n"));
296 global_ret = 5;
297 return;
298 }
300 &do_timeout,
301 NULL);
303 if (NULL == gns)
304 {
305 fprintf (stderr,
306 _ ("Failed to connect to GNS\n"));
307 global_ret = 2;
308 return;
309 }
311 NULL);
312 if (NULL != lookup_type)
313 {
314 effective_lookup_type = lookup_type;
316 }
317 else
318 {
319 effective_lookup_type = "A";
321 }
322 if (UINT32_MAX == rtype)
323 {
324 fprintf (stderr,
325 _ ("Invalid typename specified, assuming `ANY'\n"));
327 }
328 if (! raw)
329 {
330 printf (">>> Looking for `%s' records under `%s'\n",
331 effective_lookup_type, lookup_name);
332 }
335 rtype,
339 if (NULL == lr)
340 {
341 global_ret = 2;
343 return;
344 }
345}
346
347
355int
356main (int argc, char *const *argv)
357{
361 "lookup",
362 "NAME",
364 "Lookup a record for the given name"),
365 &lookup_name)),
367 "type",
368 "TYPE",
370 "Specify the type of the record to lookup"),
371 &lookup_type),
373 "timeout",
374 "TIMEOUT",
376 "Specify a timeout for the lookup"),
377 &timeout),
379 "raw",
380 gettext_noop ("No unneeded output"),
381 &raw),
383 "dns",
385 "DNS Compatibility: Name is passed in IDNA instead of UTF-8"),
386 &dns_compat),
388 int ret;
390
391 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
393 argc,
394 argv,
395 "gnunet-gns",
396 _ ("GNUnet GNS resolver tool"),
397 options,
398 &run,
399 NULL);
400 if (GNUNET_OK != ret)
401 return 1;
402 return global_ret;
403}
404
405
406/* 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:74
static int ret
Final status code.
Definition: gnunet-arm.c:93
static char * data
The data to insert into the dht.
static struct GNUNET_GNS_Handle * gns
Handle to GNS service.
Definition: gnunet-gns.c:53
static int raw
raw output
Definition: gnunet-gns.c:78
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 int global_ret
Global return value.
Definition: gnunet-gns.c:107
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 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:239
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 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
int main(int argc, char *const *argv)
The main function for gnunet-gns.
Definition: gnunet-gns.c:356
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
static struct GNUNET_GNS_LookupWithTldRequest * lr
Handle to lookup request.
Definition: gnunet-gns.c:98
static uint32_t rtype
Desired record type.
Definition: gnunet-gns.c:83
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.
static size_t data_size
Number of bytes in data.
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;.
enum GNUNET_GenericReturnValue 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_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:289
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:240
void * GNUNET_GNS_lookup_with_tld_cancel(struct GNUNET_GNS_LookupWithTldRequest *ltr)
Cancel pending lookup request.
Definition: gns_tld_api.c:331
struct GNUNET_GNS_Handle * GNUNET_GNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the GNS service.
Definition: gns_api.c:267
@ 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:219
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (e.g.
Definition: gnsrecord.c:192
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_record_get_expiration_time(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, struct GNUNET_TIME_Absolute min)
Returns the expiration time of the given block of records.
#define GNUNET_GNSRECORD_TYPE_ANY
Record type indicating any record/'*'.
char * GNUNET_GNSRECORD_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the binary value data of a record of type type to a human-readable string.
Definition: gnsrecord.c:147
@ GNUNET_GNSRECORD_RF_SUPPLEMENTAL
This is a supplemental record.
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
enum GNUNET_GenericReturnValue 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.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, 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:407
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
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:1339
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980
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:1277
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:406
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:579
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
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:136
Time for relative time used by GNUnet, in microseconds.