GNUnet  0.19.2
namestore_api_monitor.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2013, 2016, 2018 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_signatures.h"
32 #include "namestore.h"
33 
34 
39 {
44 
49 
54 
58  void *error_cb_cls;
59 
64 
69 
74 
78  void *monitor_cls;
79 
84 
88  void *sync_cb_cls;
89 
94 
99 
103  uint32_t key_len;
104 };
105 
106 
112 static void
114 
115 
122 static void
123 handle_sync (void *cls, const struct GNUNET_MessageHeader *msg)
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 }
132 
133 
141 static int
142 check_result (void *cls, const struct RecordResultMessage *lrm)
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 }
201 
202 
210 static void
211 handle_result (void *cls, const struct RecordResultMessage *lrm)
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 }
248 
249 
258 static void
259 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
260 {
261  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
262 
263  (void) error;
264  reconnect (zm);
265 }
266 
267 
273 static void
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 }
313 
314 
317  const struct GNUNET_CONFIGURATION_Handle *cfg,
318  const struct GNUNET_IDENTITY_PrivateKey *zone,
319  int iterate_first,
321  void *error_cb_cls,
323  void *monitor_cls,
325  void *sync_cb_cls)
326 {
328 
330  if (NULL != zone)
331  {
333  zm->zone = *zone;
334  }
336  zm->error_cb = error_cb;
338  zm->monitor = monitor;
340  zm->sync_cb = sync_cb;
342  zm->cfg = cfg;
343  reconnect (zm);
344  if (NULL == zm->mq)
345  {
346  GNUNET_free (zm);
347  return NULL;
348  }
349  return zm;
350 }
351 
354  const struct GNUNET_CONFIGURATION_Handle *cfg,
355  const struct GNUNET_IDENTITY_PrivateKey *zone,
356  int iterate_first,
358  void *error_cb_cls,
360  void *monitor_cls,
362  void *sync_cb_cls,
364 {
366 
368  if (NULL != zone)
369  {
371  zm->zone = *zone;
372  }
374  zm->error_cb = error_cb;
376  zm->monitor2 = monitor;
378  zm->sync_cb = sync_cb;
380  zm->cfg = cfg;
381  zm->filter = filter;
382  reconnect (zm);
383  if (NULL == zm->mq)
384  {
385  GNUNET_free (zm);
386  return NULL;
387  }
388  return zm;
389 }
390 
391 
413 void
415  uint64_t limit)
416 {
417  struct GNUNET_MQ_Envelope *env;
418  struct ZoneMonitorNextMessage *nm;
419 
421  nm->limit = GNUNET_htonll (limit);
422  GNUNET_MQ_send (zm->mq, env);
423 }
424 
425 
431 void
433 {
434  if (NULL != zm->mq)
435  {
437  zm->mq = NULL;
438  }
439  GNUNET_free (zm);
440 }
441 
442 
443 /* end of namestore_api_monitor.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static void error_cb(void *cls)
Function called if lookup fails.
Definition: gnunet-abd.c:479
static int monitor
Monitor ARM activity.
Definition: gnunet-arm.c:64
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static char * zone
Name of the zone being managed.
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.
static void sync_cb(void *cls)
Function called once we are in sync in monitor mode.
static struct GNUNET_NAMESTORE_ZoneMonitor * zm
Monitor handle.
static int result
Global testing status.
static struct GNUNET_CONTAINER_BloomFilter * filter
Bloomfilter to quickly tell if we don't have the content.
API that can be used to store naming information on a GNUnet node;.
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
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_GNSRECORD_Filter
Filter for GNUNET_GNSRECORD_normalize_record_set().
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
ssize_t GNUNET_IDENTITY_private_key_get_length(const struct GNUNET_IDENTITY_PrivateKey *key)
Get the compacted length of a GNUNET_IDENTITY_PrivateKey.
Definition: identity_api.c:809
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
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:37
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
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:62
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:77
#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
void(* GNUNET_NAMESTORE_RecordSetMonitor)(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, struct GNUNET_TIME_Absolute expiry)
Process a record set that was stored in the namestore.
void GNUNET_NAMESTORE_zone_monitor_stop(struct GNUNET_NAMESTORE_ZoneMonitor *zm)
Stop monitoring a zone for changes.
struct GNUNET_NAMESTORE_ZoneMonitor * GNUNET_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.
void(* GNUNET_NAMESTORE_RecordMonitor)(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Process a record that was stored in the namestore.
struct GNUNET_NAMESTORE_ZoneMonitor * GNUNET_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)
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).
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
Service to client: here is a (plaintext) record you requested.
#define GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT
Client to service: I am now ready for the next (set of) monitor events.
#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.
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:737
#define MAX_NAME_LEN
Maximum length of any name, including 0-termination.
Definition: namecache.h:33
common internal definitions for namestore service
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 void handle_sync(void *cls, const struct GNUNET_MessageHeader *msg)
Handle SYNC message from the namestore service.
static void handle_result(void *cls, const struct RecordResultMessage *lrm)
We've received a notification about a change to our zone.
static void reconnect(struct GNUNET_NAMESTORE_ZoneMonitor *zm)
Reconnect to the namestore service.
static int check_result(void *cls, const struct RecordResultMessage *lrm)
We've received a notification about a change to our zone.
A private key for an identity as per LSD0001.
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
Header for all communications.
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
Handle for a monitoring activity.
GNUNET_SCHEDULER_TaskCallback sync_cb
Function called when we've synchronized.
void * monitor_cls
Closure for monitor.
void * sync_cb_cls
Closure for sync_cb.
struct GNUNET_IDENTITY_PrivateKey zone
Monitored zone.
struct GNUNET_MQ_Handle * mq
Handle to namestore service.
GNUNET_NAMESTORE_RecordMonitor monitor
Function to call on events.
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.
GNUNET_NAMESTORE_RecordSetMonitor monitor2
Function to call on events.
Record is returned from the namestore (as authority).
Definition: namestore.h:292
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_TIME_AbsoluteNBO expire
Expiration time if the record result (if any).
Definition: namestore.h:302
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
Ask for next result of zone iteration for the given operation.
Definition: namestore.h:409
uint64_t limit
Number of records to return to the iterator in one shot (before #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_M...
Definition: namestore.h:425
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