aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-03-05 20:00:21 +0100
committerChristian Grothoff <christian@grothoff.org>2018-03-05 20:00:34 +0100
commit0f4032609d5372978e0c038ee05f51ce9c131876 (patch)
tree3fb3beaf77177f4aca197a350b6dd0381fe1a502 /src
parent044170c00077438d30ab0c832a7193eb093aae30 (diff)
downloadgnunet-0f4032609d5372978e0c038ee05f51ce9c131876.tar.gz
gnunet-0f4032609d5372978e0c038ee05f51ce9c131876.zip
add convenience API to gnunetgns lib
Diffstat (limited to 'src')
-rw-r--r--src/gns/Makefile.am5
-rw-r--r--src/gns/gns_api.c52
-rw-r--r--src/gns/gnunet-gns.c290
-rw-r--r--src/include/gnunet_gns_service.h54
4 files changed, 104 insertions, 297 deletions
diff --git a/src/gns/Makefile.am b/src/gns/Makefile.am
index e89192414..57b096b96 100644
--- a/src/gns/Makefile.am
+++ b/src/gns/Makefile.am
@@ -111,7 +111,6 @@ gnunet_gns_SOURCES = \
111gnunet_gns_LDADD = \ 111gnunet_gns_LDADD = \
112 libgnunetgns.la \ 112 libgnunetgns.la \
113 $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ 113 $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \
114 $(top_builddir)/src/identity/libgnunetidentity.la \
115 $(top_builddir)/src/util/libgnunetutil.la \ 114 $(top_builddir)/src/util/libgnunetutil.la \
116 $(GN_LIBINTL) 115 $(GN_LIBINTL)
117 116
@@ -214,9 +213,11 @@ libw32nsp_la_LDFLAGS = \
214 -no-undefined -static-libgcc 213 -no-undefined -static-libgcc
215 214
216libgnunetgns_la_SOURCES = \ 215libgnunetgns_la_SOURCES = \
217 gns_api.c gns.h 216 gns_api.c gns_api.h \
217 gns_tld_api.c gns.h
218libgnunetgns_la_LIBADD = \ 218libgnunetgns_la_LIBADD = \
219 $(top_builddir)/src/util/libgnunetutil.la $(XLIB) \ 219 $(top_builddir)/src/util/libgnunetutil.la $(XLIB) \
220 $(top_builddir)/src/identity/libgnunetidentity.la \
220 $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la 221 $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la
221libgnunetgns_la_LDFLAGS = \ 222libgnunetgns_la_LDFLAGS = \
222 $(GN_LIB_LDFLAGS) 223 $(GN_LIB_LDFLAGS)
diff --git a/src/gns/gns_api.c b/src/gns/gns_api.c
index 84c4ae189..ff67f0205 100644
--- a/src/gns/gns_api.c
+++ b/src/gns/gns_api.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 GNUnet e.V. 3 Copyright (C) 2009-2013, 2016, 2018 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -31,7 +31,7 @@
31#include "gnunet_protocols.h" 31#include "gnunet_protocols.h"
32#include "gnunet_dht_service.h" 32#include "gnunet_dht_service.h"
33#include "gns.h" 33#include "gns.h"
34#include "gnunet_gns_service.h" 34#include "gns_api.h"
35 35
36 36
37#define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__) 37#define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
@@ -81,50 +81,6 @@ struct GNUNET_GNS_LookupRequest
81 81
82 82
83/** 83/**
84 * Connection to the GNS service.
85 */
86struct GNUNET_GNS_Handle
87{
88
89 /**
90 * Configuration to use.
91 */
92 const struct GNUNET_CONFIGURATION_Handle *cfg;
93
94 /**
95 * Connection to service (if available).
96 */
97 struct GNUNET_MQ_Handle *mq;
98
99 /**
100 * Head of linked list of active lookup requests.
101 */
102 struct GNUNET_GNS_LookupRequest *lookup_head;
103
104 /**
105 * Tail of linked list of active lookup requests.
106 */
107 struct GNUNET_GNS_LookupRequest *lookup_tail;
108
109 /**
110 * Reconnect task
111 */
112 struct GNUNET_SCHEDULER_Task *reconnect_task;
113
114 /**
115 * How long do we wait until we try to reconnect?
116 */
117 struct GNUNET_TIME_Relative reconnect_backoff;
118
119 /**
120 * Request Id generator. Incremented by one for each request.
121 */
122 uint32_t r_id_gen;
123
124};
125
126
127/**
128 * Reconnect to GNS service. 84 * Reconnect to GNS service.
129 * 85 *
130 * @param handle the handle to the GNS service 86 * @param handle the handle to the GNS service
@@ -201,7 +157,8 @@ check_result (void *cls,
201 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg); 157 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
202 uint32_t rd_count = ntohl (lookup_msg->rd_count); 158 uint32_t rd_count = ntohl (lookup_msg->rd_count);
203 struct GNUNET_GNSRECORD_Data rd[rd_count]; 159 struct GNUNET_GNSRECORD_Data rd[rd_count];
204 160
161 (void) cls;
205 if (GNUNET_SYSERR == 162 if (GNUNET_SYSERR ==
206 GNUNET_GNSRECORD_records_deserialize (mlen, 163 GNUNET_GNSRECORD_records_deserialize (mlen,
207 (const char*) &lookup_msg[1], 164 (const char*) &lookup_msg[1],
@@ -423,4 +380,5 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
423 return lr; 380 return lr;
424} 381}
425 382
383
426/* end of gns_api.c */ 384/* end of gns_api.c */
diff --git a/src/gns/gnunet-gns.c b/src/gns/gnunet-gns.c
index 202e02a50..32cfba5bb 100644
--- a/src/gns/gnunet-gns.c
+++ b/src/gns/gnunet-gns.c
@@ -25,7 +25,6 @@
25#include "platform.h" 25#include "platform.h"
26#include <gnunet_util_lib.h> 26#include <gnunet_util_lib.h>
27#include <gnunet_dnsparser_lib.h> 27#include <gnunet_dnsparser_lib.h>
28#include <gnunet_identity_service.h>
29#include <gnunet_gnsrecord_lib.h> 28#include <gnunet_gnsrecord_lib.h>
30#include <gnunet_namestore_service.h> 29#include <gnunet_namestore_service.h>
31#include <gnunet_gns_service.h> 30#include <gnunet_gns_service.h>
@@ -41,11 +40,6 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
41static struct GNUNET_GNS_Handle *gns; 40static struct GNUNET_GNS_Handle *gns;
42 41
43/** 42/**
44 * Desired timeout for the lookup (default is no timeout).
45 */
46static struct GNUNET_TIME_Relative timeout;
47
48/**
49 * GNS name to lookup. (-u option) 43 * GNS name to lookup. (-u option)
50 */ 44 */
51static char *lookup_name; 45static char *lookup_name;
@@ -66,41 +60,20 @@ static enum GNUNET_GNS_LocalOptions local_options;
66static int raw; 60static int raw;
67 61
68/** 62/**
69 * Requested record type. 63 * Desired record type.
70 */ 64 */
71static int rtype; 65static uint32_t rtype;
72 66
73/** 67/**
74 * Handle to lookup request 68 * Handle to lookup request
75 */ 69 */
76static struct GNUNET_GNS_LookupRequest *lookup_request; 70static struct GNUNET_GNS_LookupWithTldRequest *lr;
77
78/**
79 * Lookup an ego with the identity service.
80 */
81static struct GNUNET_IDENTITY_EgoLookup *el;
82
83/**
84 * Handle for identity service.
85 */
86static struct GNUNET_IDENTITY_Handle *identity;
87
88/**
89 * Active operation on identity service.
90 */
91static struct GNUNET_IDENTITY_Operation *id_op;
92
93/**
94 * Task scheduled to handle timeout.
95 */
96static struct GNUNET_SCHEDULER_Task *tt;
97 71
98/** 72/**
99 * Global return value. 73 * Global return value.
100 * 0 on success (default), 74 * 0 on success (default),
101 * 1 on internal failures, 2 on launch failure, 75 * 1 on internal failures, 2 on launch failure,
102 * 3 if the name is not a GNS-supported TLD, 76 * 3 if the name is not a GNS-supported TLD,
103 * 4 on timeout
104 */ 77 */
105static int global_ret; 78static int global_ret;
106 79
@@ -113,50 +86,17 @@ static int global_ret;
113static void 86static void
114do_shutdown (void *cls) 87do_shutdown (void *cls)
115{ 88{
116 if (NULL != el) 89 (void) cls;
90 if (NULL != lr)
117 { 91 {
118 GNUNET_IDENTITY_ego_lookup_cancel (el); 92 GNUNET_GNS_lookup_with_tld_cancel (lr);
119 el = NULL; 93 lr = NULL;
120 }
121 if (NULL != id_op)
122 {
123 GNUNET_IDENTITY_cancel (id_op);
124 id_op = NULL;
125 }
126 if (NULL != lookup_request)
127 {
128 GNUNET_GNS_lookup_cancel (lookup_request);
129 lookup_request = NULL;
130 }
131 if (NULL != identity)
132 {
133 GNUNET_IDENTITY_disconnect (identity);
134 identity = NULL;
135 } 94 }
136 if (NULL != gns) 95 if (NULL != gns)
137 { 96 {
138 GNUNET_GNS_disconnect (gns); 97 GNUNET_GNS_disconnect (gns);
139 gns = NULL; 98 gns = NULL;
140 } 99 }
141 if (NULL != tt)
142 {
143 GNUNET_SCHEDULER_cancel (tt);
144 tt = NULL;
145 }
146}
147
148
149/**
150 * Task run on timeout. Triggers shutdown.
151 *
152 * @param cls unused
153 */
154static void
155do_timeout (void *cls)
156{
157 tt = NULL;
158 GNUNET_SCHEDULER_shutdown ();
159 global_ret = 4;
160} 100}
161 101
162 102
@@ -164,11 +104,13 @@ do_timeout (void *cls)
164 * Function called with the result of a GNS lookup. 104 * Function called with the result of a GNS lookup.
165 * 105 *
166 * @param cls the 'const char *' name that was resolved 106 * @param cls the 'const char *' name that was resolved
107 * @param was_gns #GNUNET_NO if TLD did not indicate use of GNS
167 * @param rd_count number of records returned 108 * @param rd_count number of records returned
168 * @param rd array of @a rd_count records with the results 109 * @param rd array of @a rd_count records with the results
169 */ 110 */
170static void 111static void
171process_lookup_result (void *cls, 112process_lookup_result (void *cls,
113 int was_gns,
172 uint32_t rd_count, 114 uint32_t rd_count,
173 const struct GNUNET_GNSRECORD_Data *rd) 115 const struct GNUNET_GNSRECORD_Data *rd)
174{ 116{
@@ -176,8 +118,14 @@ process_lookup_result (void *cls,
176 const char *typename; 118 const char *typename;
177 char* string_val; 119 char* string_val;
178 120
179 lookup_request = NULL; 121 lr = NULL;
180 if (!raw) 122 if (GNUNET_NO == was_gns)
123 {
124 global_ret = 3;
125 GNUNET_SCHEDULER_shutdown ();
126 return;
127 }
128 if (! raw)
181 { 129 {
182 if (0 == rd_count) 130 if (0 == rd_count)
183 printf ("No results.\n"); 131 printf ("No results.\n");
@@ -216,117 +164,6 @@ process_lookup_result (void *cls,
216 164
217 165
218/** 166/**
219 * Perform the actual resolution, starting with the zone
220 * identified by the given public key and the shorten zone.
221 *
222 * @param pkey public key to use for the zone, can be NULL
223 */
224static void
225lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
226{
227 if (NULL != lookup_type)
228 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
229 else
230 rtype = GNUNET_DNSPARSER_TYPE_A;
231 if (UINT32_MAX == rtype)
232 {
233 fprintf (stderr,
234 _("Invalid typename specified, assuming `ANY'\n"));
235 rtype = GNUNET_GNSRECORD_TYPE_ANY;
236 }
237
238 if (NULL != lookup_name)
239 {
240 lookup_request = GNUNET_GNS_lookup (gns,
241 lookup_name,
242 pkey,
243 rtype,
244 local_options,
245 &process_lookup_result,
246 lookup_name);
247 }
248 else
249 {
250 fprintf (stderr,
251 _("Please specify name to lookup!\n"));
252 GNUNET_SCHEDULER_shutdown ();
253 return;
254 }
255}
256
257
258/**
259 * Method called to with the ego we are to use for the lookup,
260 * when the ego is determined by a name.
261 *
262 * @param cls closure (NULL, unused)
263 * @param ego ego handle, NULL if not found
264 */
265static void
266identity_zone_cb (void *cls,
267 const struct GNUNET_IDENTITY_Ego *ego)
268{
269 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
270
271 el = NULL;
272 if (NULL == ego)
273 {
274 global_ret = 3; /* Not a GNS TLD */
275 GNUNET_SCHEDULER_shutdown ();
276 }
277 else
278 {
279 GNUNET_IDENTITY_ego_get_public_key (ego,
280 &pkey);
281 lookup_with_public_key (&pkey);
282 }
283}
284
285
286/**
287 * Obtain the TLD of the given @a name.
288 *
289 * @param name a name
290 * @return the part of @a name after the last ".",
291 * or @a name if @a name does not contain a "."
292 */
293static const char *
294get_tld (const char *name)
295{
296 const char *tld;
297
298 tld = strrchr (name,
299 (unsigned char) '.');
300 if (NULL == tld)
301 tld = name;
302 else
303 tld++; /* skip the '.' */
304 return tld;
305}
306
307
308/**
309 * Eat the TLD of the given @a name.
310 *
311 * @param name a name
312 */
313static void
314eat_tld (char *name)
315{
316 char *tld;
317
318 GNUNET_assert (0 < strlen (name));
319 tld = strrchr (name,
320 (unsigned char) '.');
321 if (NULL == tld)
322 strcpy (name,
323 GNUNET_GNS_MASTERZONE_STR);
324 else
325 *tld = '\0';
326}
327
328
329/**
330 * Main function that will be run. 167 * Main function that will be run.
331 * 168 *
332 * @param cls closure 169 * @param cls closure
@@ -340,82 +177,45 @@ run (void *cls,
340 const char *cfgfile, 177 const char *cfgfile,
341 const struct GNUNET_CONFIGURATION_Handle *c) 178 const struct GNUNET_CONFIGURATION_Handle *c)
342{ 179{
343 struct GNUNET_CRYPTO_EcdsaPublicKey pkey; 180 (void) cls;
344 const char *tld; 181 (void) args;
345 char *dot_tld; 182 (void) cfgfile;
346 char *zonestr; 183
347
348 cfg = c; 184 cfg = c;
349 gns = GNUNET_GNS_connect (cfg); 185 gns = GNUNET_GNS_connect (cfg);
350 if (NULL == gns) 186 if (NULL == gns)
351 { 187 {
352 fprintf (stderr, 188 fprintf (stderr,
353 _("Failed to connect to GNS\n")); 189 _("Failed to connect to GNS\n"));
190 global_ret = 2;
354 return; 191 return;
355 } 192 }
356 tt = GNUNET_SCHEDULER_add_delayed (timeout, 193
357 &do_timeout,
358 NULL);
359 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 194 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
360 NULL); 195 NULL);
361 /* start with trivial case: TLD is zkey */ 196
362 tld = get_tld (lookup_name); 197 if (NULL != lookup_type)
363 if (GNUNET_OK == 198 rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
364 GNUNET_CRYPTO_ecdsa_public_key_from_string (tld, 199 else
365 strlen (tld), 200 rtype = GNUNET_DNSPARSER_TYPE_A;
366 &pkey)) 201 if (UINT32_MAX == rtype)
367 { 202 {
368 eat_tld (lookup_name); 203 fprintf (stderr,
369 lookup_with_public_key (&pkey); 204 _("Invalid typename specified, assuming `ANY'\n"));
370 return; 205 rtype = GNUNET_GNSRECORD_TYPE_ANY;
371 } 206 }
372 207 lr = GNUNET_GNS_lookup_with_tld (gns,
373 /* second case: TLD is mapped in our configuration file */ 208 lookup_name,
374 GNUNET_asprintf (&dot_tld, 209 rtype,
375 ".%s", 210 local_options,
376 tld); 211 &process_lookup_result,
377 if (GNUNET_OK == 212 NULL);
378 GNUNET_CONFIGURATION_get_value_string (cfg, 213 if (NULL == lr)
379 "gns",
380 dot_tld,
381 &zonestr))
382 { 214 {
383 if (GNUNET_OK != 215 global_ret = 2;
384 GNUNET_CRYPTO_ecdsa_public_key_from_string (zonestr, 216 GNUNET_SCHEDULER_shutdown ();
385 strlen (zonestr),
386 &pkey))
387 {
388 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
389 "gns",
390 dot_tld,
391 _("Expected a base32-encoded public zone key\n"));
392 GNUNET_free (zonestr);
393 GNUNET_free (dot_tld);
394 GNUNET_SCHEDULER_shutdown ();
395 return;
396 }
397 GNUNET_free (dot_tld);
398 GNUNET_free (zonestr);
399 eat_tld (lookup_name);
400 lookup_with_public_key (&pkey);
401 return; 217 return;
402 } 218 }
403 GNUNET_free (dot_tld);
404
405 /* Final case: TLD matches one of our egos */
406 eat_tld (lookup_name);
407
408 /* if the name is of the form 'label.gnu', never go to the DHT */
409 if (NULL == strchr (lookup_name,
410 (unsigned char) '.'))
411 local_options = GNUNET_GNS_LO_NO_DHT;
412 identity = GNUNET_IDENTITY_connect (cfg,
413 NULL,
414 NULL);
415 el = GNUNET_IDENTITY_ego_lookup (cfg,
416 tld,
417 &identity_zone_cb,
418 NULL);
419} 219}
420 220
421 221
@@ -442,11 +242,6 @@ main (int argc,
442 "TYPE", 242 "TYPE",
443 gettext_noop ("Specify the type of the record to lookup"), 243 gettext_noop ("Specify the type of the record to lookup"),
444 &lookup_type), 244 &lookup_type),
445 GNUNET_GETOPT_option_relative_time ('T',
446 "timeout",
447 "DELAY",
448 gettext_noop ("Specify timeout for the lookup"),
449 &timeout),
450 GNUNET_GETOPT_option_flag ('r', 245 GNUNET_GETOPT_option_flag ('r',
451 "raw", 246 "raw",
452 gettext_noop ("No unneeded output"), 247 gettext_noop ("No unneeded output"),
@@ -455,7 +250,6 @@ main (int argc,
455 }; 250 };
456 int ret; 251 int ret;
457 252
458 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
459 if (GNUNET_OK != 253 if (GNUNET_OK !=
460 GNUNET_STRINGS_get_utf8_args (argc, argv, 254 GNUNET_STRINGS_get_utf8_args (argc, argv,
461 &argc, &argv)) 255 &argc, &argv))
diff --git a/src/include/gnunet_gns_service.h b/src/include/gnunet_gns_service.h
index 2ef946a82..1eb67ed50 100644
--- a/src/include/gnunet_gns_service.h
+++ b/src/include/gnunet_gns_service.h
@@ -66,6 +66,12 @@ struct GNUNET_GNS_Handle;
66 */ 66 */
67struct GNUNET_GNS_LookupRequest; 67struct GNUNET_GNS_LookupRequest;
68 68
69/**
70 * Handle to control a lookup operation where the
71 * TLD is resolved to a zone as part of the lookup operation.
72 */
73struct GNUNET_GNS_LookupWithTldRequest;
74
69 75
70/** 76/**
71 * Initialize the connection with the GNS service. 77 * Initialize the connection with the GNS service.
@@ -154,6 +160,54 @@ void
154GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr); 160GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr);
155 161
156 162
163/**
164 * Iterator called on obtained result for a GNS lookup
165 * where "not GNS" is a valid answer.
166 *
167 * @param cls closure
168 * @param gns_tld #GNUNET_YES if a GNS lookup was attempted,
169 * #GNUNET_NO if the TLD is not configured for GNS
170 * @param rd_count number of records in @a rd
171 * @param rd the records in the reply
172 */
173typedef void
174(*GNUNET_GNS_LookupResultProcessor2) (void *cls,
175 int gns_tld,
176 uint32_t rd_count,
177 const struct GNUNET_GNSRECORD_Data *rd);
178
179
180/**
181 * Perform an asynchronous lookup operation on the GNS,
182 * determining the zone using the TLD of the given name
183 * and the current configuration to resolve TLDs to zones.
184 *
185 * @param handle handle to the GNS service
186 * @param name the name to look up, including TLD
187 * @param type the record type to look up
188 * @param options local options for the lookup
189 * @param proc processor to call on result
190 * @param proc_cls closure for @a proc
191 * @return handle to the get request
192 */
193struct GNUNET_GNS_LookupWithTldRequest*
194GNUNET_GNS_lookup_with_tld (struct GNUNET_GNS_Handle *handle,
195 const char *name,
196 uint32_t type,
197 enum GNUNET_GNS_LocalOptions options,
198 GNUNET_GNS_LookupResultProcessor2 proc,
199 void *proc_cls);
200
201
202/**
203 * Cancel pending lookup request
204 *
205 * @param ltr the lookup request to cancel
206 */
207void
208GNUNET_GNS_lookup_with_tld_cancel (struct GNUNET_GNS_LookupWithTldRequest *ltr);
209
210
157#if 0 /* keep Emacsens' auto-indent happy */ 211#if 0 /* keep Emacsens' auto-indent happy */
158{ 212{
159#endif 213#endif