GNUnet  0.11.x
Data Structures | Macros | Functions
namecache_api.c File Reference

API to access the NAMECACHE service. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_constants.h"
#include "gnunet_dnsparser_lib.h"
#include "gnunet_gnsrecord_lib.h"
#include "gnunet_signatures.h"
#include "gnunet_namecache_service.h"
#include "namecache.h"
Include dependency graph for namecache_api.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_NAMECACHE_QueueEntry
 An QueueEntry used to store information for a pending NAMECACHE record operation. More...
 
struct  GNUNET_NAMECACHE_Handle
 Connection to the NAMECACHE service. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "namecache-api", __VA_ARGS__)
 

Functions

static void force_reconnect (struct GNUNET_NAMECACHE_Handle *h)
 Disconnect from service and then reconnect. More...
 
static struct GNUNET_NAMECACHE_QueueEntryfind_qe (struct GNUNET_NAMECACHE_Handle *h, uint32_t rid)
 Find queue entry for the given rid. More...
 
static int check_lookup_block_response (void *cls, const struct LookupBlockResponseMessage *msg)
 Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE. More...
 
static void handle_lookup_block_response (void *cls, const struct LookupBlockResponseMessage *msg)
 Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE. More...
 
static void handle_block_cache_response (void *cls, const struct BlockCacheResponseMessage *msg)
 Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE. More...
 
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 creation of the message queue. More...
 
static void reconnect (struct GNUNET_NAMECACHE_Handle *h)
 Reconnect to namecache service. More...
 
static void reconnect_task (void *cls)
 Re-establish the connection to the service. More...
 
static uint32_t get_op_id (struct GNUNET_NAMECACHE_Handle *h)
 Get a fresh operation id to distinguish between namecache requests. More...
 
struct GNUNET_NAMECACHE_HandleGNUNET_NAMECACHE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Initialize the connection with the NAMECACHE service. More...
 
void GNUNET_NAMECACHE_disconnect (struct GNUNET_NAMECACHE_Handle *h)
 Disconnect from the namecache service (and free associated resources). More...
 
struct GNUNET_NAMECACHE_QueueEntryGNUNET_NAMECACHE_block_cache (struct GNUNET_NAMECACHE_Handle *h, const struct GNUNET_GNSRECORD_Block *block, GNUNET_NAMECACHE_ContinuationWithStatus cont, void *cont_cls)
 Store an item in the namecache. More...
 
struct GNUNET_NAMECACHE_QueueEntryGNUNET_NAMECACHE_lookup_block (struct GNUNET_NAMECACHE_Handle *h, const struct GNUNET_HashCode *derived_hash, GNUNET_NAMECACHE_BlockProcessor proc, void *proc_cls)
 Get a result for a particular key from the namecache. More...
 
void GNUNET_NAMECACHE_cancel (struct GNUNET_NAMECACHE_QueueEntry *qe)
 Cancel a namecache operation. More...
 

Detailed Description

API to access the NAMECACHE service.

Author
Martin Schanzenbach
Matthias Wachs
Christian Grothoff

Definition in file namecache_api.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "namecache-api", __VA_ARGS__)

Definition at line 40 of file namecache_api.c.

Function Documentation

◆ force_reconnect()

static void force_reconnect ( struct GNUNET_NAMECACHE_Handle h)
static

Disconnect from service and then reconnect.

Parameters
hour handle

Definition at line 359 of file namecache_api.c.

360 {
362 
363  h->reconnect = GNUNET_NO;
365  h->mq = NULL;
366  while (NULL != (qe = h->op_head))
367  {
368  GNUNET_CONTAINER_DLL_remove (h->op_head,
369  h->op_tail,
370  qe);
371  if (NULL != qe->cont)
372  qe->cont (qe->cont_cls,
374  _ ("Error communicating with namecache service"));
375  GNUNET_free (qe);
376  }
378  "Reconnecting to namecache\n");
379  h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
380  h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
382  h);
383 }
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static struct GNUNET_DATASTORE_QueueEntry * qe
Current operation.
#define GNUNET_log(kind,...)
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:837
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:1269
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
static void reconnect_task(void *cls)
Re-establish the connection to the service.
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
struct GNUNET_MQ_Handle * mq
Our connection to the ARM service.
Definition: arm_api.c:107
struct GNUNET_SCHEDULER_Task * reconnect_task
ID of the reconnect task (if any).
Definition: arm_api.c:147
GNUNET_DATASTORE_ContinuationWithStatus cont
Function to call after transmission of the request.
void * cont_cls
Closure for cont.
An QueueEntry used to store information for a pending NAMECACHE record operation.
Definition: namecache_api.c:48

References _, GNUNET_DATASTORE_QueueEntry::cont, GNUNET_DATASTORE_QueueEntry::cont_cls, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_MQ_destroy(), GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_SYSERR, GNUNET_TIME_STD_BACKOFF, h, GNUNET_ARM_Handle::mq, qe, GNUNET_ARM_Handle::reconnect_task, and reconnect_task().

Referenced by handle_lookup_block_response(), and mq_error_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_qe()

static struct GNUNET_NAMECACHE_QueueEntry* find_qe ( struct GNUNET_NAMECACHE_Handle h,
uint32_t  rid 
)
static

Find queue entry for the given rid.

Parameters
hhandle to search
ridrequest ID to look for
Returns
NULL if not found, otherwise the queue entry (removed from the queue)

Definition at line 160 of file namecache_api.c.

162 {
164 
165  for (qe = h->op_head; qe != NULL; qe = qe->next)
166  {
167  if (qe->op_id == rid)
168  {
169  GNUNET_CONTAINER_DLL_remove (h->op_head,
170  h->op_tail,
171  qe);
172  return qe;
173  }
174  }
175  return NULL;
176 }
struct GNUNET_DATASTORE_QueueEntry * next
This is a linked list.

References GNUNET_CONTAINER_DLL_remove, h, GNUNET_DATASTORE_QueueEntry::next, and qe.

Referenced by handle_block_cache_response(), and handle_lookup_block_response().

Here is the caller graph for this function:

◆ check_lookup_block_response()

static int check_lookup_block_response ( void *  cls,
const struct LookupBlockResponseMessage msg 
)
static

Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.

Parameters
clsthe struct GNUNET_NAMECACHE_Handle
msgthe message we received

Definition at line 187 of file namecache_api.c.

189 {
190  /* any length will do, format validation is in handler */
191  return GNUNET_OK;
192 }
@ GNUNET_OK
Definition: gnunet_common.h:95

References GNUNET_OK.

◆ handle_lookup_block_response()

static void handle_lookup_block_response ( void *  cls,
const struct LookupBlockResponseMessage msg 
)
static

Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.

Parameters
clsthe struct GNUNET_NAMECACHE_Handle
msgthe message we received

Definition at line 203 of file namecache_api.c.

205 {
206  struct GNUNET_NAMECACHE_Handle *h = cls;
207  size_t size;
209 
211  "Received LOOKUP_BLOCK_RESPONSE\n");
212  qe = find_qe (h,
213  ntohl (msg->gns_header.r_id));
214  if (NULL == qe)
215  return;
216  if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
217  {
218  /* no match found */
219  if (NULL != qe->block_proc)
220  qe->block_proc (qe->block_proc_cls,
221  NULL);
222  GNUNET_free (qe);
223  return;
224  }
225  size = ntohs (msg->gns_header.header.size)
226  - sizeof(struct LookupBlockResponseMessage);
227  {
228  char buf[size] GNUNET_ALIGN;
229  struct GNUNET_GNSRECORD_Block *block;
230 
231  memset (buf, 0, size);
232  block = (struct GNUNET_GNSRECORD_Block *) buf;
233  GNUNET_memcpy (block,
234  &msg[1],
235  size);
236  if (GNUNET_OK !=
238  {
239  GNUNET_break (0);
240  if (NULL != qe->block_proc)
241  qe->block_proc (qe->block_proc_cls,
242  NULL);
243  force_reconnect (h);
244  }
245  else
246  {
247  if (NULL != qe->block_proc)
248  qe->block_proc (qe->block_proc_cls,
249  block);
250  }
251  }
252  GNUNET_free (qe);
253 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static char buf[2048]
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_verify(const struct GNUNET_GNSRECORD_Block *block)
Check if a signature is valid.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:542
static struct GNUNET_NAMECACHE_QueueEntry * find_qe(struct GNUNET_NAMECACHE_Handle *h, uint32_t rid)
Find queue entry for the given rid.
static void force_reconnect(struct GNUNET_NAMECACHE_Handle *h)
Disconnect from service and then reconnect.
#define LOG(kind,...)
Definition: namecache_api.c:40
static unsigned int size
Size of the "table".
Definition: peer.c:67
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
Connection to the NAMECACHE service.
Definition: namecache_api.c:95
uint64_t abs_value_us
The actual value.
Lookup response.
Definition: namecache.h:76

References GNUNET_TIME_Absolute::abs_value_us, buf, find_qe(), force_reconnect(), GNUNET_ALIGN, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_GNSRECORD_block_verify(), GNUNET_memcpy, GNUNET_OK, GNUNET_TIME_absolute_ntoh(), h, LOG, msg, qe, GNUNET_MessageHeader::size, and size.

Here is the call graph for this function:

◆ handle_block_cache_response()

static void handle_block_cache_response ( void *  cls,
const struct BlockCacheResponseMessage msg 
)
static

Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE.

Parameters
clsthe struct GNUNET_NAMECACHE_Handle
msgthe message we received
sizethe message size
Returns
GNUNET_OK on success, GNUNET_SYSERR on error and we did NOT notify the client

Definition at line 266 of file namecache_api.c.

268 {
269  struct GNUNET_NAMECACHE_Handle *h = cls;
271  int res;
272 
274  "Received BLOCK_CACHE_RESPONSE\n");
275  qe = find_qe (h,
276  ntohl (msg->gns_header.r_id));
277  if (NULL == qe)
278  return;
279  res = ntohl (msg->op_result);
280  /* TODO: add actual error message from namecache to response... */
281  if (NULL != qe->cont)
282  qe->cont (qe->cont_cls,
283  res,
284  (GNUNET_OK == res)
285  ? NULL
286  : _ ("Namecache failed to cache block"));
287  GNUNET_free (qe);
288 }
static int res

References _, GNUNET_DATASTORE_QueueEntry::cont, GNUNET_DATASTORE_QueueEntry::cont_cls, find_qe(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_OK, h, LOG, msg, qe, and res.

Here is the call graph for this function:

◆ mq_error_handler()

static void mq_error_handler ( void *  cls,
enum GNUNET_MQ_Error  error 
)
static

Generic error handler, called with the appropriate error code and the same closure specified at the creation of the message queue.

Not every message queue implementation supports an error handler.

Parameters
clsclosure with the struct GNUNET_NAMECACHE_Handle *
errorerror code

Definition at line 300 of file namecache_api.c.

302 {
303  struct GNUNET_NAMECACHE_Handle *h = cls;
304 
305  force_reconnect (h);
306 }

References force_reconnect(), and h.

Referenced by reconnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reconnect()

static void reconnect ( struct GNUNET_NAMECACHE_Handle h)
static

Reconnect to namecache service.

Parameters
hthe handle to the NAMECACHE service

Definition at line 315 of file namecache_api.c.

316 {
318  GNUNET_MQ_hd_var_size (lookup_block_response,
321  h),
322  GNUNET_MQ_hd_fixed_size (block_cache_response,
325  h),
327  };
328 
329  GNUNET_assert (NULL == h->mq);
331  "namecache",
332  handlers,
334  h);
335 }
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
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:1064
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
#define GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE
Service to client: result of block cache request.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE
Service to client: result of block lookup.
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...
Response to a request to cache a block.
Definition: namecache.h:134
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration that we are using.
Definition: arm_api.c:112
Message handler for a specific message type.

References GNUNET_ARM_Handle::cfg, GNUNET_assert, GNUNET_CLIENT_connect(), GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE, GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE, GNUNET_MQ_handler_end, GNUNET_MQ_hd_fixed_size, GNUNET_MQ_hd_var_size, h, handlers, GNUNET_ARM_Handle::mq, and mq_error_handler().

Referenced by GNUNET_NAMECACHE_connect(), and reconnect_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reconnect_task()

static void reconnect_task ( void *  cls)
static

Re-establish the connection to the service.

Parameters
clshandle to use to re-connect.

Definition at line 344 of file namecache_api.c.

345 {
346  struct GNUNET_NAMECACHE_Handle *h = cls;
347 
348  h->reconnect_task = NULL;
349  reconnect (h);
350 }
static void reconnect(struct GNUNET_NAMECACHE_Handle *h)
Reconnect to namecache service.

References h, reconnect(), and GNUNET_ARM_Handle::reconnect_task.

Referenced by force_reconnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_op_id()

static uint32_t get_op_id ( struct GNUNET_NAMECACHE_Handle h)
static

Get a fresh operation id to distinguish between namecache requests.

Parameters
hthe namecache handle
Returns
next operation id to use

Definition at line 393 of file namecache_api.c.

394 {
395  return h->last_op_id_used++;
396 }

References h.

Referenced by GNUNET_NAMECACHE_block_cache(), and GNUNET_NAMECACHE_lookup_block().

Here is the caller graph for this function: