GNUnet  0.10.x
Data Structures | Macros | Functions
gns_api.c File Reference

library to access the GNS service More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_constants.h"
#include "gnunet_arm_service.h"
#include "gnunet_hello_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_dht_service.h"
#include "gns.h"
#include "gns_api.h"
Include dependency graph for gns_api.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_GNS_LookupRequest
 Handle to a lookup request. More...
 

Macros

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

Functions

static void reconnect (struct GNUNET_GNS_Handle *handle)
 Reconnect to GNS service. More...
 
static void reconnect_task (void *cls)
 Reconnect to GNS. More...
 
static void force_reconnect (struct GNUNET_GNS_Handle *handle)
 Disconnect from service and then reconnect. 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 int check_result (void *cls, const struct LookupResultMessage *lookup_msg)
 Check validity of message received from the GNS service. More...
 
static void handle_result (void *cls, const struct LookupResultMessage *lookup_msg)
 Handler for messages received from the GNS service. More...
 
struct GNUNET_GNS_HandleGNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Initialize the connection with the GNS service. More...
 
void GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
 Shutdown connection with the GNS service. More...
 
void * GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
 Cancel pending lookup request. More...
 
struct GNUNET_GNS_LookupRequestGNUNET_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. More...
 

Detailed Description

library to access the GNS service

Author
Martin Schanzenbach
Christian Grothoff

Definition in file gns_api.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 37 of file gns_api.c.

Referenced by GNUNET_GNS_lookup(), handle_result(), mq_error_handler(), and reconnect().

Function Documentation

◆ reconnect()

static void reconnect ( struct GNUNET_GNS_Handle handle)
static

Reconnect to GNS service.

Parameters
handlethe handle to the GNS service

Definition at line 228 of file gns_api.c.

References GNUNET_GNS_Handle::cfg, GNUNET_assert, GNUNET_CLIENT_connect(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT, GNUNET_MQ_handler_end, GNUNET_MQ_hd_var_size, GNUNET_MQ_send_copy(), LOG, GNUNET_GNS_Handle::lookup_head, GNUNET_GNS_Handle::mq, mq_error_handler(), GNUNET_GNS_LookupRequest::next, and result.

Referenced by GNUNET_GNS_connect(), and reconnect_task().

229 {
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 }
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
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_GNS_LookupRequest * next
DLL.
Definition: gns_api.c:48
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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.
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
struct GNUNET_MQ_Handle * mq
Connection to service (if available).
Definition: gns_api.h:46
static int result
Global testing status.
Message handler for a specific message type.
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
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
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
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
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

Reconnect to GNS.

Parameters
clsthe handle

Definition at line 98 of file gns_api.c.

References handle, reconnect(), and GNUNET_GNS_Handle::reconnect_task.

Referenced by force_reconnect().

99 {
100  struct GNUNET_GNS_Handle *handle = cls;
101 
102  handle->reconnect_task = NULL;
103  reconnect (handle);
104 }
Connection to the GNS service.
Definition: gns_api.h:35
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static void reconnect(struct GNUNET_GNS_Handle *handle)
Reconnect to GNS service.
Definition: gns_api.c:228
struct GNUNET_SCHEDULER_Task * reconnect_task
Reconnect task.
Definition: gns_api.h:61
Here is the call graph for this function:
Here is the caller graph for this function:

◆ force_reconnect()

static void force_reconnect ( struct GNUNET_GNS_Handle handle)
static

Disconnect from service and then reconnect.

Parameters
handleour handle

Definition at line 113 of file gns_api.c.

References GNUNET_MQ_destroy(), GNUNET_SCHEDULER_add_delayed(), GNUNET_TIME_STD_BACKOFF, GNUNET_GNS_Handle::mq, GNUNET_GNS_Handle::reconnect_backoff, GNUNET_GNS_Handle::reconnect_task, and reconnect_task().

Referenced by mq_error_handler().

114 {
115  GNUNET_MQ_destroy (handle->mq);
116  handle->mq = NULL;
117  handle->reconnect_backoff
119  handle->reconnect_task
122  handle);
123 }
struct GNUNET_TIME_Relative reconnect_backoff
How long do we wait until we try to reconnect?
Definition: gns_api.h:66
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
struct GNUNET_MQ_Handle * mq
Connection to service (if available).
Definition: gns_api.h:46
static void reconnect_task(void *cls)
Reconnect to GNS.
Definition: gns_api.c:98
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
struct GNUNET_SCHEDULER_Task * reconnect_task
Reconnect task.
Definition: gns_api.h:61
Here is the call graph for this function:
Here is the caller 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_GNS_Handle *
errorerror code

Definition at line 135 of file gns_api.c.

References force_reconnect(), GNUNET_ERROR_TYPE_WARNING, handle, and LOG.

Referenced by reconnect().

137 {
138  struct GNUNET_GNS_Handle *handle = cls;
139 
141  "Problem with message queue. error: %i\n",
142  error);
143  force_reconnect (handle);
144 }
Connection to the GNS service.
Definition: gns_api.h:35
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static void force_reconnect(struct GNUNET_GNS_Handle *handle)
Disconnect from service and then reconnect.
Definition: gns_api.c:113
#define LOG(kind,...)
Definition: gns_api.c:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_result()

static int check_result ( void *  cls,
const struct LookupResultMessage lookup_msg 
)
static

Check validity of message received from the GNS service.

Parameters
clsthe struct GNUNET_GNS_Handle *
loookup_msgthe incoming message

Definition at line 154 of file gns_api.c.

References GNUNET_break, GNUNET_GNSRECORD_records_deserialize(), GNUNET_OK, GNUNET_SYSERR, LookupResultMessage::header, LookupResultMessage::rd_count, and GNUNET_MessageHeader::size.

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 }
uint32_t rd_count
The number of records contained in response.
Definition: gns.h:91
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
struct GNUNET_MessageHeader header
Header of type GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT.
Definition: gns.h:81
Here is the call graph for this function:

◆ handle_result()

static void handle_result ( void *  cls,
const struct LookupResultMessage lookup_msg 
)
static

Handler for messages received from the GNS service.

Parameters
clsthe struct GNUNET_GNS_Handle *
loookup_msgthe incoming message

Definition at line 182 of file gns_api.c.

References GNUNET_GNS_LookupRequest::env, GNUNET_assert, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_GNSRECORD_records_deserialize(), GNUNET_MQ_discard(), GNUNET_OK, handle, LookupResultMessage::header, LookupResultMessage::id, LOG, GNUNET_GNS_Handle::lookup_head, GNUNET_GNS_LookupRequest::lookup_proc, GNUNET_GNS_Handle::lookup_tail, lr, GNUNET_GNS_LookupRequest::next, GNUNET_GNS_LookupRequest::proc_cls, GNUNET_GNS_LookupRequest::r_id, LookupResultMessage::rd_count, and GNUNET_MessageHeader::size.

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 }
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
uint32_t rd_count
The number of records contained in response.
Definition: gns.h:91
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_GNS_LookupRequest * next
DLL.
Definition: gns_api.c:48
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Handle to a lookup request.
Definition: gns_api.c:42
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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...
uint32_t id
Unique identifier for this request (for key collisions).
Definition: gns.h:86
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
struct GNUNET_GNS_LookupRequest * lookup_tail
Tail of linked list of active lookup requests.
Definition: gns_api.h:56
void GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *mqm)
Discard the message queue message, free all allocated resources.
Definition: mq.c:321
struct GNUNET_MQ_Envelope * env
Envelope with the message for this queue entry.
Definition: gns_api.c:73
struct GNUNET_MessageHeader header
Header of type GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT.
Definition: gns.h:81
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
void * proc_cls
lookup_proc closure
Definition: gns_api.c:68
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function: