GNUnet  0.10.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 244 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().

245 {
249  struct GNUNET_MessageHeader,
250  zm),
253  struct RecordResultMessage,
254  zm),
256  struct GNUNET_MQ_Envelope *env;
257  struct ZoneMonitorStartMessage *sm;
258 
259  if (NULL != zm->mq)
260  {
261  GNUNET_MQ_destroy(zm->mq);
262  zm->error_cb(zm->error_cb_cls);
263  }
264  zm->mq = GNUNET_CLIENT_connect(zm->cfg,
265  "namestore",
266  handlers,
268  zm);
269  if (NULL == zm->mq)
270  return;
272  sm->iterate_first = htonl(zm->iterate_first);
273  sm->zone = zm->zone;
274  GNUNET_MQ_send(zm->mq, env);
275 }
Start monitoring a zone.
Definition: namestore.h:291
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:900
#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:306
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:301
#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:821
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:351
#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:250
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 109 of file namestore_api_monitor.c.

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

110 {
111  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
112 
113  (void)cls;
114  (void)msg;
115  if (NULL != zm->sync_cb)
116  zm->sync_cb(zm->sync_cb_cls);
117 }
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 128 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.

129 {
130  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
131  size_t lrm_len;
132  size_t exp_lrm_len;
133  size_t name_len;
134  size_t rd_len;
135  unsigned rd_count;
136  const char *name_tmp;
137  const char *rd_ser_tmp;
138 
139  (void)cls;
140  if ((0 != GNUNET_memcmp(&lrm->private_key, &zm->zone)) &&
141  (0 != GNUNET_is_zero(&zm->zone)))
142  {
143  GNUNET_break(0);
144  return GNUNET_SYSERR;
145  }
146  lrm_len = ntohs(lrm->gns_header.header.size);
147  rd_len = ntohs(lrm->rd_len);
148  rd_count = ntohs(lrm->rd_count);
149  name_len = ntohs(lrm->name_len);
150  if (name_len > MAX_NAME_LEN)
151  {
152  GNUNET_break(0);
153  return GNUNET_SYSERR;
154  }
155  exp_lrm_len = sizeof(struct RecordResultMessage) + name_len + rd_len;
156  if (lrm_len != exp_lrm_len)
157  {
158  GNUNET_break(0);
159  return GNUNET_SYSERR;
160  }
161  if (0 == name_len)
162  {
163  GNUNET_break(0);
164  return GNUNET_SYSERR;
165  }
166  name_tmp = (const char *)&lrm[1];
167  if (name_tmp[name_len - 1] != '\0')
168  {
169  GNUNET_break(0);
170  return GNUNET_SYSERR;
171  }
172  rd_ser_tmp = (const char *)&name_tmp[name_len];
173  {
174  struct GNUNET_GNSRECORD_Data rd[rd_count];
175 
176  if (GNUNET_OK !=
177  GNUNET_GNSRECORD_records_deserialize(rd_len, rd_ser_tmp, rd_count, rd))
178  {
179  GNUNET_break(0);
180  return GNUNET_SYSERR;
181  }
182  }
183  return GNUNET_OK;
184 }
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:259
struct GNUNET_MessageHeader header
header.type will be GNUNET_MESSAGE_TYPE_NAMESTORE_* header.size will be message size ...
Definition: namestore.h:45
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
The private key of the authority.
Definition: namestore.h:279
Handle for a monitoring activity.
uint16_t rd_count
Number of records contained.
Definition: namestore.h:269
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:264
#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:254
#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:250
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 195 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.

196 {
197  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
198  size_t name_len;
199  size_t rd_len;
200  unsigned rd_count;
201  const char *name_tmp;
202  const char *rd_ser_tmp;
203 
204  rd_len = ntohs(lrm->rd_len);
205  rd_count = ntohs(lrm->rd_count);
206  name_len = ntohs(lrm->name_len);
207  name_tmp = (const char *)&lrm[1];
208  rd_ser_tmp = (const char *)&name_tmp[name_len];
209  {
210  struct GNUNET_GNSRECORD_Data rd[rd_count];
211 
213  GNUNET_OK ==
214  GNUNET_GNSRECORD_records_deserialize(rd_len, rd_ser_tmp, rd_count, rd));
215  zm->monitor(zm->monitor_cls, &lrm->private_key, name_tmp, rd_count, rd);
216  }
217 }
#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:259
GNUNET_NAMESTORE_RecordMonitor monitor
Function to call on events.
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
The private key of the authority.
Definition: namestore.h:279
Handle for a monitoring activity.
uint16_t rd_count
Number of records contained.
Definition: namestore.h:269
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:264
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 229 of file namestore_api_monitor.c.

References reconnect(), and zm.

Referenced by reconnect().

230 {
231  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
232 
233  (void)error;
234  reconnect(zm);
235 }
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: