GNUnet  0.19.5
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_hello_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_dht_service.h"
33 #include "gns.h"
34 #include "gns_api.h"
35 
36 
37 #define LOG(kind, ...) GNUNET_log_from (kind, "gns-api", __VA_ARGS__)
38 
43 #define DEFAULT_LIMIT 128
44 
49 {
54 
59 
64 
69 
73  void *proc_cls;
74 
79 
83  uint32_t r_id;
84 };
85 
86 
92 static void
94 
95 
101 static void
102 reconnect_task (void *cls)
103 {
104  struct GNUNET_GNS_Handle *handle = cls;
105 
106  handle->reconnect_task = NULL;
107  reconnect (handle);
108 }
109 
110 
116 static void
118 {
120  handle->mq = NULL;
121  handle->reconnect_backoff
122  = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
124  = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
126  handle);
127 }
128 
129 
138 static void
139 mq_error_handler (void *cls,
140  enum GNUNET_MQ_Error error)
141 {
142  struct GNUNET_GNS_Handle *handle = cls;
143 
145  "Problem with message queue. error: %i\n",
146  error);
148 }
149 
150 
157 static int
158 check_result (void *cls,
159  const struct LookupResultMessage *lookup_msg)
160 {
161  size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg);
162  uint32_t rd_count = ntohl (lookup_msg->rd_count);
164 
165  (void) cls;
166  if (GNUNET_SYSERR ==
168  (const char *) &lookup_msg[1],
169  rd_count,
170  rd))
171  {
172  GNUNET_break (0);
173  return GNUNET_SYSERR;
174  }
175  return GNUNET_OK;
176 }
177 
178 
185 static void
186 handle_result (void *cls,
187  const struct LookupResultMessage *lookup_msg)
188 {
189  struct GNUNET_GNS_Handle *handle = cls;
190  size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg);
191  uint32_t rd_count = ntohl (lookup_msg->rd_count);
193  uint32_t r_id = ntohl (lookup_msg->id);
196  void *proc_cls;
197 
199  "Received lookup reply from GNS service (%u records)\n",
200  (unsigned int) rd_count);
201  for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
202  if (lr->r_id == r_id)
203  break;
204  if (NULL == lr)
205  return;
206  proc = lr->lookup_proc;
207  proc_cls = lr->proc_cls;
208 
211  (const
212  char *) &lookup_msg[1],
213  rd_count,
214  rd));
215  proc (proc_cls,
216  rd_count,
217  rd);
218  GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
219  handle->lookup_tail,
220  lr);
221  if (NULL != lr->env)
222  GNUNET_MQ_discard (lr->env);
223  GNUNET_free (lr);
224 }
225 
226 
232 static void
234 {
238  struct LookupResultMessage,
239  handle),
241  };
242 
243  GNUNET_assert (NULL == handle->mq);
245  "Trying to connect to GNS\n");
247  "gns",
248  handlers,
250  handle);
251  if (NULL == handle->mq)
252  return;
253  for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head;
254  NULL != lh;
255  lh = lh->next)
257  lh->env);
258 }
259 
260 
267 struct GNUNET_GNS_Handle *
269 {
270  struct GNUNET_GNS_Handle *handle;
271 
273  handle->cfg = cfg;
274  reconnect (handle);
275  if (NULL == handle->mq)
276  {
278  return NULL;
279  }
280  return handle;
281 }
282 
283 
289 void
291 {
292  if (NULL != handle->mq)
293  {
295  handle->mq = NULL;
296  }
297  if (NULL != handle->reconnect_task)
298  {
300  handle->reconnect_task = NULL;
301  }
302  GNUNET_assert (NULL == handle->lookup_head);
304 }
305 
306 
313 void *
315 {
317  void *ret;
318 
319  GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
320  handle->lookup_tail,
321  lr);
322  GNUNET_MQ_discard (lr->env);
323  ret = lr->proc_cls;
324  GNUNET_free (lr);
325  return ret;
326 }
327 
328 
345  const char *name,
346  const struct GNUNET_IDENTITY_PublicKey *zone,
347  uint32_t type,
349  uint16_t recursion_depth_limit,
351  void *proc_cls)
352 {
353  /* IPC to shorten gns names, return shorten_handle */
354  struct LookupMessage *lookup_msg;
356  size_t nlen;
357  size_t key_len;
358  ssize_t written;
359  char *buf;
360 
361  if (NULL == name)
362  {
363  GNUNET_break (0);
364  return NULL;
365  }
367  "Trying to lookup `%s' in GNS\n",
368  name);
369  nlen = strlen (name) + 1;
370  if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*lr))
371  {
372  GNUNET_break (0);
373  return NULL;
374  }
376  lr->gns_handle = handle;
377  lr->lookup_proc = proc;
378  lr->proc_cls = proc_cls;
379  lr->r_id = handle->r_id_gen++;
381  lr->env = GNUNET_MQ_msg_extra (lookup_msg,
382  nlen + key_len,
384  buf = (char *) &lookup_msg[1];
385  lookup_msg->id = htonl (lr->r_id);
386  lookup_msg->options = htons ((uint16_t) options);
387  lookup_msg->recursion_depth_limit
388  = htons (recursion_depth_limit);
389  lookup_msg->key_len = htonl (key_len);
391  buf,
392  key_len);
393  GNUNET_assert (0 <= written);
394  buf += written;
395  lookup_msg->type = htonl (type);
397  name,
398  nlen);
399  GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
400  handle->lookup_tail,
401  lr);
402  if (NULL != handle->mq)
404  lr->env);
405  return lr;
406 }
407 
408 
423  const char *name,
424  const struct GNUNET_IDENTITY_PublicKey *zone,
425  uint32_t type,
428  void *proc_cls)
429 {
431  name,
432  zone,
433  type,
434  options,
436  proc,
437  proc_cls);
438 }
439 
440 
441 /* end of gns_api.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
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:186
static void force_reconnect(struct GNUNET_GNS_Handle *handle)
Disconnect from service and then reconnect.
Definition: gns_api.c:117
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:139
static int check_result(void *cls, const struct LookupResultMessage *lookup_msg)
Check validity of message received from the GNS service.
Definition: gns_api.c:158
static void reconnect(struct GNUNET_GNS_Handle *handle)
Reconnect to GNS service.
Definition: gns_api.c:233
#define LOG(kind,...)
Definition: gns_api.c:37
#define DEFAULT_LIMIT
Default recursion depth limit to apply if the application does not specify any.
Definition: gns_api.c:43
static void reconnect_task(void *cls)
Reconnect to GNS.
Definition: gns_api.c:102
shared data structures of libgnunetgns
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static struct GNUNET_GNS_LookupWithTldRequest * lr
Handle to lookup request.
Definition: gnunet-gns.c:98
static char * zone
Name of the zone being managed.
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 int result
Global testing status.
static char buf[2048]
API to the DHT service.
Helper library for handling HELLOs.
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.
struct GNUNET_GNS_LookupRequest * GNUNET_GNS_lookup(struct GNUNET_GNS_Handle *handle, const char *name, const struct GNUNET_IDENTITY_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:422
void GNUNET_GNS_disconnect(struct GNUNET_GNS_Handle *handle)
Shutdown connection with the GNS service.
Definition: gns_api.c:290
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:314
struct GNUNET_GNS_LookupRequest * GNUNET_GNS_lookup_limited(struct GNUNET_GNS_Handle *handle, const char *name, const struct GNUNET_IDENTITY_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:344
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_LocalOptions
Options for the GNS lookup.
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_IDENTITY_write_public_key_to_buffer(const struct GNUNET_IDENTITY_PublicKey *key, void *buffer, size_t len)
Writes a GNUNET_IDENTITY_PublicKey to a compact buffer.
Definition: identity_api.c:890
ssize_t GNUNET_IDENTITY_public_key_get_length(const struct GNUNET_IDENTITY_PublicKey *key)
Get the compacted length of a GNUNET_IDENTITY_PublicKey.
Definition: identity_api.c:830
#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:62
#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:975
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 GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
const char * name
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect to the service.
Definition: dns_api.c:81
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: dns_api.c:66
struct GNUNET_MQ_Handle * mq
Connection to DNS service, or NULL.
Definition: dns_api.c:61
Connection to the GNS service.
Definition: gns_api.h:36
Handle to a lookup request.
Definition: gns_api.c:49
void * proc_cls
lookup_proc closure
Definition: gns_api.c:73
struct GNUNET_GNS_LookupRequest * next
DLL.
Definition: gns_api.c:53
struct GNUNET_GNS_LookupRequest * prev
DLL.
Definition: gns_api.c:58
GNUNET_GNS_LookupResultProcessor lookup_proc
processor to call on lookup result
Definition: gns_api.c:68
struct GNUNET_MQ_Envelope * env
Envelope with the message for this queue entry.
Definition: gns_api.c:78
uint32_t r_id
request id
Definition: gns_api.c:83
struct GNUNET_GNS_Handle * gns_handle
handle to gns
Definition: gns_api.c:63
GNUNET_GNS_LookupResultProcessor2 lookup_proc
processor to call on lookup result
Definition: gns_tld_api.c:54
struct GNUNET_GNS_Handle * gns_handle
handle to gns
Definition: gns_tld_api.c:49
An identity key as per LSD0001.
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.
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
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model