GNUnet 0.21.1
gns_api.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2020 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 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_constants.h"
29#include "gnunet_arm_service.h"
30#include "gnunet_protocols.h"
31#include "gnunet_dht_service.h"
32#include "gns.h"
33#include "gns_api.h"
34
35
36#define LOG(kind, ...) GNUNET_log_from (kind, "gns-api", __VA_ARGS__)
37
42#define DEFAULT_LIMIT 128
43
48{
53
58
63
68
72 void *proc_cls;
73
78
82 uint32_t r_id;
83};
84
85
91static void
93
94
100static void
101reconnect_task (void *cls)
102{
103 struct GNUNET_GNS_Handle *handle = cls;
104
105 handle->reconnect_task = NULL;
107}
108
109
115static void
117{
119 handle->mq = NULL;
120 handle->reconnect_backoff
121 = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
122 handle->reconnect_task
123 = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
125 handle);
126}
127
128
137static void
139 enum GNUNET_MQ_Error error)
140{
141 struct GNUNET_GNS_Handle *handle = cls;
142
144 "Problem with message queue. error: %i\n",
145 error);
147}
148
149
156static int
157check_result (void *cls,
158 const struct LookupResultMessage *lookup_msg)
159{
160 size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg);
161 uint32_t rd_count = ntohl (lookup_msg->rd_count);
163
164 (void) cls;
165 if (GNUNET_SYSERR ==
167 (const char *) &lookup_msg[1],
168 rd_count,
169 rd))
170 {
171 GNUNET_break (0);
172 return GNUNET_SYSERR;
173 }
174 return GNUNET_OK;
175}
176
177
184static void
185handle_result (void *cls,
186 const struct LookupResultMessage *lookup_msg)
187{
188 struct GNUNET_GNS_Handle *handle = cls;
189 size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg);
190 uint32_t rd_count = ntohl (lookup_msg->rd_count);
192 uint32_t r_id = ntohl (lookup_msg->id);
195 void *proc_cls;
196
198 "Received lookup reply from GNS service (%u records)\n",
199 (unsigned int) rd_count);
200 for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
201 if (lr->r_id == r_id)
202 break;
203 if (NULL == lr)
204 return;
205 proc = lr->lookup_proc;
206 proc_cls = lr->proc_cls;
207
210 (const
211 char *) &lookup_msg[1],
212 rd_count,
213 rd));
214 proc (proc_cls,
215 rd_count,
216 rd);
218 handle->lookup_tail,
219 lr);
220 if (NULL != lr->env)
221 GNUNET_MQ_discard (lr->env);
222 GNUNET_free (lr);
223}
224
225
231static void
233{
237 struct LookupResultMessage,
238 handle),
240 };
241
242 GNUNET_assert (NULL == handle->mq);
244 "Trying to connect to GNS\n");
246 "gns",
247 handlers,
249 handle);
250 if (NULL == handle->mq)
251 return;
252 for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head;
253 NULL != lh;
254 lh = lh->next)
256 lh->env);
257}
258
259
266struct GNUNET_GNS_Handle *
268{
270
272 handle->cfg = cfg;
274 if (NULL == handle->mq)
275 {
277 return NULL;
278 }
279 return handle;
280}
281
282
288void
290{
291 if (NULL != handle->mq)
292 {
294 handle->mq = NULL;
295 }
296 if (NULL != handle->reconnect_task)
297 {
298 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
299 handle->reconnect_task = NULL;
300 }
301 GNUNET_assert (NULL == handle->lookup_head);
303}
304
305
312void *
314{
316 void *ret;
317
319 handle->lookup_tail,
320 lr);
321 GNUNET_MQ_discard (lr->env);
322 ret = lr->proc_cls;
323 GNUNET_free (lr);
324 return ret;
325}
326
327
344 const char *name,
345 const struct GNUNET_CRYPTO_PublicKey *zone,
346 uint32_t type,
348 uint16_t recursion_depth_limit,
350 void *proc_cls)
351{
352 /* IPC to shorten gns names, return shorten_handle */
353 struct LookupMessage *lookup_msg;
355 size_t nlen;
356 size_t key_len;
357 ssize_t written;
358 char *buf;
359
360 if (NULL == name)
361 {
362 GNUNET_break (0);
363 return NULL;
364 }
366 "Trying to lookup `%s' in GNS\n",
367 name);
368 nlen = strlen (name) + 1;
369 if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*lr))
370 {
371 GNUNET_break (0);
372 return NULL;
373 }
376 lr->lookup_proc = proc;
377 lr->proc_cls = proc_cls;
378 lr->r_id = handle->r_id_gen++;
380 lr->env = GNUNET_MQ_msg_extra (lookup_msg,
381 nlen + key_len,
383 buf = (char *) &lookup_msg[1];
384 lookup_msg->id = htonl (lr->r_id);
385 lookup_msg->options = htons ((uint16_t) options);
386 lookup_msg->recursion_depth_limit
387 = htons (recursion_depth_limit);
388 lookup_msg->key_len = htonl (key_len);
390 buf,
391 key_len);
392 GNUNET_assert (0 <= written);
393 buf += written;
394 lookup_msg->type = htonl (type);
395 GNUNET_memcpy (buf,
396 name,
397 nlen);
399 handle->lookup_tail,
400 lr);
401 if (NULL != handle->mq)
403 lr->env);
404 return lr;
405}
406
407
422 const char *name,
423 const struct GNUNET_CRYPTO_PublicKey *zone,
424 uint32_t type,
427 void *proc_cls)
428{
430 name,
431 zone,
432 type,
433 options,
435 proc,
436 proc_cls);
437}
438
439
440/* end of gns_api.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
struct GNUNET_MQ_MessageHandlers handlers[]
Definition: 003.c:1
IPC messages between GNS API and GNS service.
static void handle_result(void *cls, const struct LookupResultMessage *lookup_msg)
Handler for messages received from the GNS service.
Definition: gns_api.c:185
static void force_reconnect(struct GNUNET_GNS_Handle *handle)
Disconnect from service and then reconnect.
Definition: gns_api.c:116
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
Definition: gns_api.c:138
static int check_result(void *cls, const struct LookupResultMessage *lookup_msg)
Check validity of message received from the GNS service.
Definition: gns_api.c:157
static void reconnect(struct GNUNET_GNS_Handle *handle)
Reconnect to GNS service.
Definition: gns_api.c:232
#define LOG(kind,...)
Definition: gns_api.c:36
#define DEFAULT_LIMIT
Default recursion depth limit to apply if the application does not specify any.
Definition: gns_api.c:42
static void reconnect_task(void *cls)
Reconnect to GNS.
Definition: gns_api.c:101
shared data structures of libgnunetgns
static int ret
Final status code.
Definition: gnunet-arm.c:94
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
static struct GNUNET_GNS_LookupWithTldRequest * lr
Handle to lookup request.
Definition: gnunet-gns.c:98
static char * name
Name (label) of the records to list.
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 uint32_t type
Type string converted to DNS type value.
static int result
Global testing status.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Definition: gnunet-vpn.c:35
API to the DHT service.
Constants for network protocols.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:1057
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_GNS_disconnect(struct GNUNET_GNS_Handle *handle)
Shutdown connection with the GNS service.
Definition: gns_api.c:289
struct GNUNET_GNS_LookupRequest * GNUNET_GNS_lookup(struct GNUNET_GNS_Handle *handle, const char *name, const struct GNUNET_CRYPTO_PublicKey *zone, uint32_t type, enum GNUNET_GNS_LocalOptions options, GNUNET_GNS_LookupResultProcessor proc, void *proc_cls)
Perform an asynchronous lookup operation on the GNS.
Definition: gns_api.c:421
void(* GNUNET_GNS_LookupResultProcessor)(void *cls, uint32_t rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Iterator called on obtained result for a GNS lookup.
void * GNUNET_GNS_lookup_cancel(struct GNUNET_GNS_LookupRequest *lr)
Cancel pending lookup request.
Definition: gns_api.c:313
GNUNET_GNS_LocalOptions
Options for the GNS lookup.
struct GNUNET_GNS_LookupRequest * GNUNET_GNS_lookup_limited(struct GNUNET_GNS_Handle *handle, const char *name, const struct GNUNET_CRYPTO_PublicKey *zone, uint32_t type, enum GNUNET_GNS_LocalOptions options, uint16_t recursion_depth_limit, GNUNET_GNS_LookupResultProcessor proc, void *proc_cls)
Perform an asynchronous lookup operation on the GNS.
Definition: gns_api.c:343
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
int GNUNET_GNSRECORD_records_deserialize(size_t len, const char *src, unsigned int rd_count, struct GNUNET_GNSRECORD_Data *dest)
Deserialize the given records to the given destination.
ssize_t GNUNET_CRYPTO_public_key_get_length(const struct GNUNET_CRYPTO_PublicKey *key)
Get the compacted length of a GNUNET_CRYPTO_PublicKey.
Definition: crypto_pkey.c:68
ssize_t GNUNET_CRYPTO_write_public_key_to_buffer(const struct GNUNET_CRYPTO_PublicKey *key, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_PublicKey to a compact buffer.
Definition: crypto_pkey.c:128
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_MQ_send_copy(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MQ_Envelope *ev)
Send a copy of a message with the given message queue.
Definition: mq.c:370
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
void GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *mqm)
Discard the message queue message, free all allocated resources.
Definition: mq.c:285
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:63
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
#define GNUNET_MESSAGE_TYPE_GNS_LOOKUP
Client would like to resolve a name.
#define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT
Service response to name resolution request from client.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:981
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:1278
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
An identity key as per LSD0001.
Connection to the GNS service.
Definition: gns_api.h:36
Handle to a lookup request.
Definition: gns_api.c:48
void * proc_cls
lookup_proc closure
Definition: gns_api.c:72
struct GNUNET_GNS_LookupRequest * next
DLL.
Definition: gns_api.c:52
struct GNUNET_GNS_LookupRequest * prev
DLL.
Definition: gns_api.c:57
GNUNET_GNS_LookupResultProcessor lookup_proc
processor to call on lookup result
Definition: gns_api.c:67
struct GNUNET_MQ_Envelope * env
Envelope with the message for this queue entry.
Definition: gns_api.c:77
uint32_t r_id
request id
Definition: gns_api.c:82
struct GNUNET_GNS_Handle * gns_handle
handle to gns
Definition: gns_api.c:62
GNUNET_GNS_LookupResultProcessor2 lookup_proc
processor to call on lookup result
Definition: gns_tld_api.c:53
struct GNUNET_GNS_Handle * gns_handle
handle to gns
Definition: gns_tld_api.c:48
Message handler for a specific message type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: vpn_api.c:39
struct GNUNET_MQ_Handle * mq
Connection to VPN service.
Definition: vpn_api.c:44
Message from client to GNS service to lookup records.
Definition: gns.h:37
int16_t options
Local options for where to look for results (an enum GNUNET_GNS_LocalOptions in NBO).
Definition: gns.h:52
uint32_t id
Unique identifier for this request (for key collisions).
Definition: gns.h:46
uint32_t key_len
Length of the zone key.
Definition: gns.h:69
uint16_t recursion_depth_limit
Recursion depth limit, i.e.
Definition: gns.h:59
int32_t type
the type of record to look up
Definition: gns.h:64
Message from GNS service to client: new results.
Definition: gns.h:80
struct GNUNET_MessageHeader header
Header of type GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT.
Definition: gns.h:84
uint32_t id
Unique identifier for this request (for key collisions).
Definition: gns.h:89
uint32_t rd_count
The number of records contained in response.
Definition: gns.h:96