GNUnet  0.10.x
gns_api.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009-2013, 2016, 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 */
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 {
44 
49 
54 
59 
64 
68  void *proc_cls;
69 
74 
78  uint32_t r_id;
79 
80 };
81 
82 
88 static void
90 
91 
97 static void
98 reconnect_task (void *cls)
99 {
100  struct GNUNET_GNS_Handle *handle = cls;
101 
102  handle->reconnect_task = NULL;
103  reconnect (handle);
104 }
105 
106 
112 static void
114 {
115  GNUNET_MQ_destroy (handle->mq);
116  handle->mq = NULL;
117  handle->reconnect_backoff
119  handle->reconnect_task
122  handle);
123 }
124 
125 
134 static void
135 mq_error_handler (void *cls,
136  enum GNUNET_MQ_Error error)
137 {
138  struct GNUNET_GNS_Handle *handle = cls;
139 
141  "Problem with message queue. error: %i\n",
142  error);
143  force_reconnect (handle);
144 }
145 
146 
153 static int
154 check_result (void *cls,
155  const struct LookupResultMessage *lookup_msg)
156 {
157  size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
158  uint32_t rd_count = ntohl (lookup_msg->rd_count);
159  struct GNUNET_GNSRECORD_Data rd[rd_count];
160 
161  (void) cls;
162  if (GNUNET_SYSERR ==
164  (const char*) &lookup_msg[1],
165  rd_count,
166  rd))
167  {
168  GNUNET_break (0);
169  return GNUNET_SYSERR;
170  }
171  return GNUNET_OK;
172 }
173 
174 
181 static void
182 handle_result (void *cls,
183  const struct LookupResultMessage *lookup_msg)
184 {
185  struct GNUNET_GNS_Handle *handle = cls;
186  size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
187  uint32_t rd_count = ntohl (lookup_msg->rd_count);
188  struct GNUNET_GNSRECORD_Data rd[rd_count];
189  uint32_t r_id = ntohl (lookup_msg->id);
192  void *proc_cls;
193 
195  "Received lookup reply from GNS service (%u records)\n",
196  (unsigned int) rd_count);
197  for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
198  if (lr->r_id == r_id)
199  break;
200  if (NULL == lr)
201  return;
202  proc = lr->lookup_proc;
203  proc_cls = lr->proc_cls;
204 
207  (const char*) &lookup_msg[1],
208  rd_count,
209  rd));
210  proc (proc_cls,
211  rd_count,
212  rd);
214  handle->lookup_tail,
215  lr);
216  if (NULL != lr->env)
217  GNUNET_MQ_discard (lr->env);
218  GNUNET_free (lr);
219 }
220 
221 
227 static void
229 {
230  struct GNUNET_MQ_MessageHandler handlers[] = {
233  struct LookupResultMessage,
234  handle),
236  };
237 
238  GNUNET_assert (NULL == handle->mq);
240  "Trying to connect to GNS\n");
241  handle->mq = GNUNET_CLIENT_connect (handle->cfg,
242  "gns",
243  handlers,
245  handle);
246  if (NULL == handle->mq)
247  return;
248  for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head;
249  NULL != lh;
250  lh = lh->next)
251  GNUNET_MQ_send_copy (handle->mq,
252  lh->env);
253 }
254 
255 
262 struct GNUNET_GNS_Handle *
264 {
265  struct GNUNET_GNS_Handle *handle;
266 
267  handle = GNUNET_new (struct GNUNET_GNS_Handle);
268  handle->cfg = cfg;
269  reconnect (handle);
270  if (NULL == handle->mq)
271  {
272  GNUNET_free (handle);
273  return NULL;
274  }
275  return handle;
276 }
277 
278 
284 void
286 {
287  if (NULL != handle->mq)
288  {
289  GNUNET_MQ_destroy (handle->mq);
290  handle->mq = NULL;
291  }
292  if (NULL != handle->reconnect_task)
293  {
295  handle->reconnect_task = NULL;
296  }
297  GNUNET_assert (NULL == handle->lookup_head);
298  GNUNET_free (handle);
299 }
300 
301 
308 void *
310 {
311  struct GNUNET_GNS_Handle *handle = lr->gns_handle;
312  void *ret;
313 
315  handle->lookup_tail,
316  lr);
317  GNUNET_MQ_discard (lr->env);
318  ret = lr->proc_cls;
319  GNUNET_free (lr);
320  return ret;
321 }
322 
323 
338  const char *name,
339  const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
340  uint32_t type,
343  void *proc_cls)
344 {
345  /* IPC to shorten gns names, return shorten_handle */
346  struct LookupMessage *lookup_msg;
348  size_t nlen;
349 
350  if (NULL == name)
351  {
352  GNUNET_break (0);
353  return NULL;
354  }
356  "Trying to lookup `%s' in GNS\n",
357  name);
358  nlen = strlen (name) + 1;
359  if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*lr))
360  {
361  GNUNET_break (0);
362  return NULL;
363  }
364  lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
365  lr->gns_handle = handle;
366  lr->lookup_proc = proc;
367  lr->proc_cls = proc_cls;
368  lr->r_id = handle->r_id_gen++;
369  lr->env = GNUNET_MQ_msg_extra (lookup_msg,
370  nlen,
372  lookup_msg->id = htonl (lr->r_id);
373  lookup_msg->options = htons ((uint16_t) options);
374  lookup_msg->zone = *zone;
375  lookup_msg->type = htonl (type);
376  GNUNET_memcpy (&lookup_msg[1],
377  name,
378  nlen);
380  handle->lookup_tail,
381  lr);
382  if (NULL != handle->mq)
383  GNUNET_MQ_send_copy (handle->mq,
384  lr->env);
385  return lr;
386 }
387 
388 
389 /* end of gns_api.c */
uint32_t r_id
request id
Definition: gns_api.c:78
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
Connection to the GNS service.
Definition: gns_api.h:35
Message from client to GNS service to lookup records.
Definition: gns.h:36
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: gns_api.h:41
Message from GNS service to client: new results.
Definition: gns.h:76
uint32_t id
Unique identifier for this request (for key collisions).
Definition: gns.h:46
struct GNUNET_GNS_Handle * gns_handle
handle to gns
Definition: gns_api.c:58
uint32_t rd_count
The number of records contained in response.
Definition: gns.h:91
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
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.
static struct GNUNET_GNS_LookupWithTldRequest * lr
Handle to lookup request.
Definition: gnunet-gns.c:75
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
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:901
struct GNUNET_CRYPTO_EcdsaPublicKey zone
Zone that is to be used for lookup.
Definition: gns.h:51
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_MESSAGE_TYPE_GNS_LOOKUP
Client would like to resolve a name.
struct GNUNET_GNS_LookupRequest * next
DLL.
Definition: gns_api.c:48
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_TIME_Relative reconnect_backoff
How long do we wait until we try to reconnect?
Definition: gns_api.h:66
shared data structures of libgnunetgns
Handle to a lookup request.
Definition: gns_api.c:42
#define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT
Service response to name resolution request from client.
GNUNET_GNS_LocalOptions
Options for the GNS lookup.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
int32_t type
the type of record to look up
Definition: gns.h:67
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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.
GNUNET_GNS_LookupResultProcessor lookup_proc
processor to call on lookup result
Definition: gns_api.c:63
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static int ret
Final status code.
Definition: gnunet-arm.c:89
IPC messages between GNS API and GNS service.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static char * zone
Name of the zone we manage.
#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:52
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:1246
#define GNUNET_memcpy(dst, src, n)
uint32_t id
Unique identifier for this request (for key collisions).
Definition: gns.h:86
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void GNUNET_GNS_disconnect(struct GNUNET_GNS_Handle *handle)
Shutdown connection with the GNS service.
Definition: gns_api.c:285
struct GNUNET_MQ_Handle * mq
Connection to service (if available).
Definition: gns_api.h:46
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static int result
Global testing status.
static void force_reconnect(struct GNUNET_GNS_Handle *handle)
Disconnect from service and then reconnect.
Definition: gns_api.c:113
static void reconnect(struct GNUNET_GNS_Handle *handle)
Reconnect to GNS service.
Definition: gns_api.c:228
struct GNUNET_GNS_Handle * GNUNET_GNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the GNS service.
Definition: gns_api.c:263
struct GNUNET_GNS_LookupRequest * lookup_tail
Tail of linked list of active lookup requests.
Definition: gns_api.h:56
struct GNUNET_GNS_LookupRequest * prev
DLL.
Definition: gns_api.c:53
Message handler for a specific message type.
int16_t options
Local options for where to look for results (an enum GNUNET_GNS_LocalOptions in NBO).
Definition: gns.h:57
void GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *mqm)
Discard the message queue message, free all allocated resources.
Definition: mq.c:321
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
struct GNUNET_MQ_Envelope * env
Envelope with the message for this queue entry.
Definition: gns_api.c:73
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static int check_result(void *cls, const struct LookupResultMessage *lookup_msg)
Check validity of message received from the GNS service.
Definition: gns_api.c:154
const char * name
static void reconnect_task(void *cls)
Reconnect to GNS.
Definition: gns_api.c:98
struct GNUNET_MessageHeader header
Header of type GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT.
Definition: gns.h:81
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
struct GNUNET_GNS_LookupRequest * lookup_head
Head of linked list of active lookup requests.
Definition: gns_api.h:51
#define LOG(kind,...)
Definition: gns_api.c:37
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
configuration data
Definition: configuration.c:85
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
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:432
uint32_t r_id_gen
Request Id generator.
Definition: gns_api.h:71
void * GNUNET_GNS_lookup_cancel(struct GNUNET_GNS_LookupRequest *lr)
Cancel pending lookup request.
Definition: gns_api.c:309
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
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:135
struct GNUNET_GNS_LookupRequest * GNUNET_GNS_lookup(struct GNUNET_GNS_Handle *handle, const char *name, const struct GNUNET_CRYPTO_EcdsaPublicKey *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:337
static void handle_result(void *cls, const struct LookupResultMessage *lookup_msg)
Handler for messages received from the GNS service.
Definition: gns_api.c:182
struct GNUNET_SCHEDULER_Task * reconnect_task
Reconnect task.
Definition: gns_api.h:61
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
void * proc_cls
lookup_proc closure
Definition: gns_api.c:68
#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:965