GNUnet  0.11.x
Data Structures | Functions
namestore_api_monitor.c File Reference

API to monitor changes in the NAMESTORE. 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_arm_service.h"
#include "gnunet_signatures.h"
#include "gnunet_namestore_service.h"
#include "namestore.h"
Include dependency graph for namestore_api_monitor.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_NAMESTORE_ZoneMonitor
 Handle for a monitoring activity. More...
 

Functions

static void reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
 Reconnect to the namestore service. More...
 
static void handle_sync (void *cls, const struct GNUNET_MessageHeader *msg)
 Handle SYNC message from the namestore service. More...
 
static int check_result (void *cls, const struct RecordResultMessage *lrm)
 We've received a notification about a change to our zone. More...
 
static void handle_result (void *cls, const struct RecordResultMessage *lrm)
 We've received a notification about a change to our zone. 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...
 
struct GNUNET_NAMESTORE_ZoneMonitorGNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, int iterate_first, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor monitor, void *monitor_cls, GNUNET_SCHEDULER_TaskCallback sync_cb, void *sync_cb_cls)
 Begin monitoring a zone for changes. More...
 
void GNUNET_NAMESTORE_zone_monitor_next (struct GNUNET_NAMESTORE_ZoneMonitor *zm, uint64_t limit)
 Calls the monitor processor specified in GNUNET_NAMESTORE_zone_monitor_start for the next record(s). More...
 
void GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
 Stop monitoring a zone for changes. More...
 

Detailed Description

API to monitor changes in the NAMESTORE.

Author
Christian Grothoff

Definition in file namestore_api_monitor.c.

Function Documentation

◆ reconnect()

static void reconnect ( struct GNUNET_NAMESTORE_ZoneMonitor zm)
static

Reconnect to the namestore service.

Parameters
zmmonitor to reconnect

Definition at line 245 of file namestore_api_monitor.c.

References GNUNET_NAMESTORE_ZoneMonitor::cfg, env, GNUNET_NAMESTORE_ZoneMonitor::error_cb, GNUNET_NAMESTORE_ZoneMonitor::error_cb_cls, GNUNET_CLIENT_connect(), GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC, GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT, GNUNET_MQ_destroy(), GNUNET_MQ_handler_end, GNUNET_MQ_hd_fixed_size, GNUNET_MQ_hd_var_size, GNUNET_MQ_msg, GNUNET_MQ_send(), GNUNET_NAMESTORE_ZoneMonitor::iterate_first, ZoneMonitorStartMessage::iterate_first, GNUNET_NAMESTORE_ZoneMonitor::mq, mq_error_handler(), result, GNUNET_NAMESTORE_ZoneMonitor::zone, and ZoneMonitorStartMessage::zone.

Referenced by GNUNET_NAMESTORE_zone_monitor_start(), and mq_error_handler().

246 {
248  { GNUNET_MQ_hd_fixed_size (sync,
250  struct GNUNET_MessageHeader,
251  zm),
254  struct RecordResultMessage,
255  zm),
257  struct GNUNET_MQ_Envelope *env;
258  struct ZoneMonitorStartMessage *sm;
259 
260  if (NULL != zm->mq)
261  {
262  GNUNET_MQ_destroy (zm->mq);
263  zm->error_cb (zm->error_cb_cls);
264  }
265  zm->mq = GNUNET_CLIENT_connect (zm->cfg,
266  "namestore",
267  handlers,
269  zm);
270  if (NULL == zm->mq)
271  return;
273  sm->iterate_first = htonl (zm->iterate_first);
274  sm->zone = zm->zone;
275  GNUNET_MQ_send (zm->mq, env);
276 }
Start monitoring a zone.
Definition: namestore.h:298
struct GNUNET_MQ_Handle * mq
Handle to namestore service.
void * error_cb_cls
Closure for error_cb.
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_MQ_hd_fixed_size(name, code, str, ctx)
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration (to reconnect).
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...
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
int iterate_first
Do we first iterate over all existing records?
struct GNUNET_CRYPTO_EcdsaPrivateKey zone
Zone key.
Definition: namestore.h:314
static int result
Global testing status.
Message handler for a specific message type.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
uint32_t iterate_first
GNUNET_YES to first iterate over all records, GNUNET_NO to only monitor changes.o ...
Definition: namestore.h:309
#define GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC
Service to client: you're now in sync.
#define GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START
Client to service: start monitoring (yields sequence of "ZONE_ITERATION_RESPONSES" — forever)...
GNUNET_SCHEDULER_TaskCallback error_cb
Function to call on errors.
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
Service to client: here is a (plaintext) record you requested.
Record is returned from the namestore (as authority).
Definition: namestore.h:256
struct GNUNET_CRYPTO_EcdsaPrivateKey zone
Monitored zone.
#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:

◆ handle_sync()

static void handle_sync ( void *  cls,
const struct GNUNET_MessageHeader msg 
)
static

Handle SYNC message from the namestore service.

Parameters
clsthe monitor
msgthe sync message

Definition at line 110 of file namestore_api_monitor.c.

References GNUNET_NAMESTORE_ZoneMonitor::sync_cb, GNUNET_NAMESTORE_ZoneMonitor::sync_cb_cls, and zm.

111 {
112  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
113 
114  (void) cls;
115  (void) msg;
116  if (NULL != zm->sync_cb)
117  zm->sync_cb (zm->sync_cb_cls);
118 }
static struct GNUNET_NAMESTORE_ZoneMonitor * zm
Monitor handle.
GNUNET_SCHEDULER_TaskCallback sync_cb
Function called when we've synchronized.
Handle for a monitoring activity.
void * sync_cb_cls
Closure for sync_cb.

◆ check_result()

static int check_result ( void *  cls,
const struct RecordResultMessage lrm 
)
static

We've received a notification about a change to our zone.

Check that it is well-formed.

Parameters
clsthe zone monitor handle
lrmthe message from the service.

Definition at line 129 of file namestore_api_monitor.c.

References RecordResultMessage::gns_header, GNUNET_break, GNUNET_GNSRECORD_records_deserialize(), GNUNET_is_zero, GNUNET_memcmp, GNUNET_OK, GNUNET_SYSERR, GNUNET_NAMESTORE_Header::header, MAX_NAME_LEN, RecordResultMessage::name_len, RecordResultMessage::private_key, RecordResultMessage::rd_count, RecordResultMessage::rd_len, GNUNET_MessageHeader::size, zm, and GNUNET_NAMESTORE_ZoneMonitor::zone.

130 {
131  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
132  size_t lrm_len;
133  size_t exp_lrm_len;
134  size_t name_len;
135  size_t rd_len;
136  unsigned rd_count;
137  const char *name_tmp;
138  const char *rd_ser_tmp;
139 
140  (void) cls;
141  if ((0 != GNUNET_memcmp (&lrm->private_key, &zm->zone)) &&
142  (0 != GNUNET_is_zero (&zm->zone)))
143  {
144  GNUNET_break (0);
145  return GNUNET_SYSERR;
146  }
147  lrm_len = ntohs (lrm->gns_header.header.size);
148  rd_len = ntohs (lrm->rd_len);
149  rd_count = ntohs (lrm->rd_count);
150  name_len = ntohs (lrm->name_len);
151  if (name_len > MAX_NAME_LEN)
152  {
153  GNUNET_break (0);
154  return GNUNET_SYSERR;
155  }
156  exp_lrm_len = sizeof(struct RecordResultMessage) + name_len + rd_len;
157  if (lrm_len != exp_lrm_len)
158  {
159  GNUNET_break (0);
160  return GNUNET_SYSERR;
161  }
162  if (0 == name_len)
163  {
164  GNUNET_break (0);
165  return GNUNET_SYSERR;
166  }
167  name_tmp = (const char *) &lrm[1];
168  if (name_tmp[name_len - 1] != '\0')
169  {
170  GNUNET_break (0);
171  return GNUNET_SYSERR;
172  }
173  rd_ser_tmp = (const char *) &name_tmp[name_len];
174  {
175  struct GNUNET_GNSRECORD_Data rd[rd_count];
176 
177  if (GNUNET_OK !=
178  GNUNET_GNSRECORD_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
179  {
180  GNUNET_break (0);
181  return GNUNET_SYSERR;
182  }
183  }
184  return GNUNET_OK;
185 }
static struct GNUNET_NAMESTORE_ZoneMonitor * zm
Monitor handle.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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...
uint16_t name_len
Name length.
Definition: namestore.h:266
struct GNUNET_MessageHeader header
header.type will be GNUNET_MESSAGE_TYPE_NAMESTORE_* header.size will be message size ...
Definition: namestore.h:46
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
The private key of the authority.
Definition: namestore.h:286
Handle for a monitoring activity.
uint16_t rd_count
Number of records contained.
Definition: namestore.h:276
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:271
#define GNUNET_is_zero(a)
Check that memory in a is all zeros.
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT.
Definition: namestore.h:261
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define MAX_NAME_LEN
Maximum length of any name, including 0-termination.
Definition: namecache.h:33
Record is returned from the namestore (as authority).
Definition: namestore.h:256
struct GNUNET_CRYPTO_EcdsaPrivateKey zone
Monitored zone.
Here is the call graph for this function:

◆ handle_result()

static void handle_result ( void *  cls,
const struct RecordResultMessage lrm 
)
static

We've received a notification about a change to our zone.

Forward to monitor callback.

Parameters
clsthe zone monitor handle
lrmthe message from the service.

Definition at line 196 of file namestore_api_monitor.c.

References GNUNET_assert, GNUNET_GNSRECORD_records_deserialize(), GNUNET_OK, GNUNET_NAMESTORE_ZoneMonitor::monitor, GNUNET_NAMESTORE_ZoneMonitor::monitor_cls, RecordResultMessage::name_len, RecordResultMessage::private_key, RecordResultMessage::rd_count, RecordResultMessage::rd_len, and zm.

197 {
198  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
199  size_t name_len;
200  size_t rd_len;
201  unsigned rd_count;
202  const char *name_tmp;
203  const char *rd_ser_tmp;
204 
205  rd_len = ntohs (lrm->rd_len);
206  rd_count = ntohs (lrm->rd_count);
207  name_len = ntohs (lrm->name_len);
208  name_tmp = (const char *) &lrm[1];
209  rd_ser_tmp = (const char *) &name_tmp[name_len];
210  {
211  struct GNUNET_GNSRECORD_Data rd[rd_count];
212 
213  GNUNET_assert (
214  GNUNET_OK ==
215  GNUNET_GNSRECORD_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd));
216  zm->monitor (zm->monitor_cls, &lrm->private_key, name_tmp, rd_count, rd);
217  }
218 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct GNUNET_NAMESTORE_ZoneMonitor * zm
Monitor handle.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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 name_len
Name length.
Definition: namestore.h:266
GNUNET_NAMESTORE_RecordMonitor monitor
Function to call on events.
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
The private key of the authority.
Definition: namestore.h:286
Handle for a monitoring activity.
uint16_t rd_count
Number of records contained.
Definition: namestore.h:276
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:271
void * monitor_cls
Closure for monitor.
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_NAMESTORE_ZoneMonitor *
errorerror code

Definition at line 230 of file namestore_api_monitor.c.

References reconnect(), and zm.

Referenced by reconnect().

231 {
232  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
233 
234  (void) error;
235  reconnect (zm);
236 }
static struct GNUNET_NAMESTORE_ZoneMonitor * zm
Monitor handle.
static void reconnect(struct GNUNET_NAMESTORE_ZoneMonitor *zm)
Reconnect to the namestore service.
Handle for a monitoring activity.
Here is the call graph for this function:
Here is the caller graph for this function: