GNUnet  0.20.0
namestore_api_monitor.c File Reference

API to monitor changes in the NAMESTORE. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_constants.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_IDENTITY_PrivateKey *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)
 
struct GNUNET_NAMESTORE_ZoneMonitorGNUNET_NAMESTORE_zone_monitor_start2 (const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_IDENTITY_PrivateKey *zone, int iterate_first, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordSetMonitor monitor, void *monitor_cls, GNUNET_SCHEDULER_TaskCallback sync_cb, void *sync_cb_cls, enum GNUNET_GNSRECORD_Filter filter)
 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 274 of file namestore_api_monitor.c.

275 {
277  { GNUNET_MQ_hd_fixed_size (sync,
279  struct GNUNET_MessageHeader,
280  zm),
283  struct RecordResultMessage,
284  zm),
286  struct GNUNET_MQ_Envelope *env;
287  struct ZoneMonitorStartMessage *sm;
288 
289  if (NULL != zm->mq)
290  {
293  }
295  "namestore",
296  handlers,
298  zm);
299  if (NULL == zm->mq)
300  return;
301  env = GNUNET_MQ_msg_extra (sm,
302  zm->key_len,
304  sm->iterate_first = htonl (zm->iterate_first);
305  if (0 < zm->key_len)
307  &sm[1],
308  zm->key_len);
309  sm->key_len = htons (zm->key_len);
310  sm->filter = htons (zm->filter);
311  GNUNET_MQ_send (zm->mq, env);
312 }
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_NAMESTORE_ZoneMonitor * zm
Monitor handle.
static int result
Global testing status.
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
ssize_t GNUNET_IDENTITY_write_private_key_to_buffer(const struct GNUNET_IDENTITY_PrivateKey *key, void *buffer, size_t len)
Writes a GNUNET_IDENTITY_PrivateKey to a compact buffer.
Definition: identity_api.c:933
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:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#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:63
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
Service to client: here is a (plaintext) record you requested.
#define GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START
Client to service: start monitoring (yields sequence of "ZONE_ITERATION_RESPONSES" — forever).
#define GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC
Service to client: you're now in sync.
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...
Message handler for a specific message type.
Header for all communications.
struct GNUNET_IDENTITY_PrivateKey zone
Monitored zone.
struct GNUNET_MQ_Handle * mq
Handle to namestore service.
void * error_cb_cls
Closure for error_cb.
enum GNUNET_GNSRECORD_Filter filter
Record set filter for this monitor.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration (to reconnect).
uint32_t key_len
Zone key length.
int iterate_first
Do we first iterate over all existing records?
GNUNET_SCHEDULER_TaskCallback error_cb
Function to call on errors.
Record is returned from the namestore (as authority).
Definition: namestore.h:292
Start monitoring a zone.
Definition: namestore.h:376
uint32_t iterate_first
GNUNET_YES to first iterate over all records, GNUNET_NO to only monitor changes.o
Definition: namestore.h:386
uint16_t filter
Record set filter control flags.
Definition: namestore.h:392
uint16_t key_len
Length of the zone key.
Definition: namestore.h:397

References GNUNET_NAMESTORE_ZoneMonitor::cfg, env, GNUNET_NAMESTORE_ZoneMonitor::error_cb, GNUNET_NAMESTORE_ZoneMonitor::error_cb_cls, ZoneMonitorStartMessage::filter, GNUNET_NAMESTORE_ZoneMonitor::filter, GNUNET_CLIENT_connect(), GNUNET_IDENTITY_write_private_key_to_buffer(), 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_extra, GNUNET_MQ_send(), handlers, ZoneMonitorStartMessage::iterate_first, GNUNET_NAMESTORE_ZoneMonitor::iterate_first, ZoneMonitorStartMessage::key_len, GNUNET_NAMESTORE_ZoneMonitor::key_len, GNUNET_NAMESTORE_ZoneMonitor::mq, mq_error_handler(), result, zm, and GNUNET_NAMESTORE_ZoneMonitor::zone.

Referenced by GNUNET_NAMESTORE_zone_monitor_start(), GNUNET_NAMESTORE_zone_monitor_start2(), and mq_error_handler().

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 123 of file namestore_api_monitor.c.

124 {
125  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
126 
127  (void) cls;
128  (void) msg;
129  if (NULL != zm->sync_cb)
130  zm->sync_cb (zm->sync_cb_cls);
131 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
Handle for a monitoring activity.
GNUNET_SCHEDULER_TaskCallback sync_cb
Function called when we've synchronized.
void * sync_cb_cls
Closure for sync_cb.

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

◆ 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 142 of file namestore_api_monitor.c.

143 {
144  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
145  size_t lrm_len;
146  size_t exp_lrm_len;
147  size_t name_len;
148  size_t rd_len;
149  unsigned rd_count;
150  const char *name_tmp;
151  const char *rd_ser_tmp;
152  size_t key_len;
153 
154  (void) zm;
155  key_len = ntohs (lrm->key_len);
156  (void) cls;
157  if (0 == key_len)
158  {
159  GNUNET_break (0);
160  return GNUNET_SYSERR;
161  }
162  lrm_len = ntohs (lrm->gns_header.header.size);
163  rd_len = ntohs (lrm->rd_len);
164  rd_count = ntohs (lrm->rd_count);
165  name_len = ntohs (lrm->name_len);
166  if (name_len > MAX_NAME_LEN)
167  {
168  GNUNET_break (0);
169  return GNUNET_SYSERR;
170  }
171  exp_lrm_len = sizeof(struct RecordResultMessage) + name_len + rd_len + key_len;
172  if (lrm_len != exp_lrm_len)
173  {
174  GNUNET_break (0);
175  return GNUNET_SYSERR;
176  }
177  if (0 == name_len)
178  {
179  GNUNET_break (0);
180  return GNUNET_SYSERR;
181  }
182  name_tmp = (const char *) &lrm[1] + key_len;
183  if (name_tmp[name_len - 1] != '\0')
184  {
185  GNUNET_break (0);
186  return GNUNET_SYSERR;
187  }
188  rd_ser_tmp = (const char *) &name_tmp[name_len];
189  {
191 
192  if (GNUNET_OK !=
193  GNUNET_GNSRECORD_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
194  {
195  GNUNET_break (0);
196  return GNUNET_SYSERR;
197  }
198  }
199  return GNUNET_OK;
200 }
static unsigned int rd_count
Number of records for currently parsed set.
static struct GNUNET_GNSRECORD_Data rd[50]
The record data under a single label.
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_OK
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define MAX_NAME_LEN
Maximum length of any name, including 0-termination.
Definition: namecache.h:33
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
struct GNUNET_MessageHeader header
header.type will be GNUNET_MESSAGE_TYPE_NAMESTORE_* header.size will be message size
Definition: namestore.h:46
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:312
uint16_t name_len
Name length.
Definition: namestore.h:307
uint16_t rd_count
Number of records contained.
Definition: namestore.h:317
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT.
Definition: namestore.h:296
uint16_t key_len
Length of the zone key.
Definition: namestore.h:322

References RecordResultMessage::gns_header, GNUNET_break, GNUNET_GNSRECORD_records_deserialize(), GNUNET_OK, GNUNET_SYSERR, GNUNET_NAMESTORE_Header::header, RecordResultMessage::key_len, GNUNET_NAMESTORE_ZoneMonitor::key_len, MAX_NAME_LEN, RecordResultMessage::name_len, rd, rd_count, RecordResultMessage::rd_count, RecordResultMessage::rd_len, GNUNET_MessageHeader::size, and zm.

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 211 of file namestore_api_monitor.c.

212 {
213  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
214  struct GNUNET_IDENTITY_PrivateKey private_key;
215  size_t name_len;
216  size_t rd_len;
217  size_t key_len;
218  size_t kbytes_read;
219  unsigned rd_count;
220  const char *name_tmp;
221  const char *rd_ser_tmp;
222 
223  key_len = ntohs (lrm->key_len);
224  rd_len = ntohs (lrm->rd_len);
225  rd_count = ntohs (lrm->rd_count);
226  name_len = ntohs (lrm->name_len);
227  name_tmp = (const char *) &lrm[1] + key_len;
230  key_len,
231  &private_key,
232  &kbytes_read));
233  GNUNET_assert (kbytes_read == key_len);
234  rd_ser_tmp = (const char *) &name_tmp[name_len];
235  {
237 
238  GNUNET_assert (
239  GNUNET_OK ==
240  GNUNET_GNSRECORD_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd));
241  if (NULL != zm->monitor2)
242  zm->monitor2 (zm->monitor_cls, &private_key, name_tmp,
244  else
245  zm->monitor (zm->monitor_cls, &private_key, name_tmp, rd_count, rd);
246  }
247 }
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_read_private_key_from_buffer(const void *buffer, size_t len, struct GNUNET_IDENTITY_PrivateKey *key, size_t *kb_read)
Reads a GNUNET_IDENTITY_PrivateKey from a compact buffer.
Definition: identity_api.c:908
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:737
A private key for an identity as per LSD0001.
void * monitor_cls
Closure for monitor.
GNUNET_NAMESTORE_RecordMonitor monitor
Function to call on events.
GNUNET_NAMESTORE_RecordSetMonitor monitor2
Function to call on events.
struct GNUNET_TIME_AbsoluteNBO expire
Expiration time if the record result (if any).
Definition: namestore.h:302

References RecordResultMessage::expire, GNUNET_assert, GNUNET_GNSRECORD_records_deserialize(), GNUNET_IDENTITY_read_private_key_from_buffer(), GNUNET_OK, GNUNET_SYSERR, GNUNET_TIME_absolute_ntoh(), RecordResultMessage::key_len, GNUNET_NAMESTORE_ZoneMonitor::monitor, GNUNET_NAMESTORE_ZoneMonitor::monitor2, GNUNET_NAMESTORE_ZoneMonitor::monitor_cls, RecordResultMessage::name_len, rd, rd_count, RecordResultMessage::rd_count, RecordResultMessage::rd_len, and zm.

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 259 of file namestore_api_monitor.c.

260 {
261  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
262 
263  (void) error;
264  reconnect (zm);
265 }
static void reconnect(struct GNUNET_NAMESTORE_ZoneMonitor *zm)
Reconnect to the namestore service.

References reconnect(), and zm.

Referenced by reconnect().

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