GNUnet  0.11.x
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_crypto_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_dnsparser_lib.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_signatures.h"
34 #include "namestore.h"
35 
36 
41 {
46 
51 
56 
60  void *error_cb_cls;
61 
66 
70  void *monitor_cls;
71 
76 
80  void *sync_cb_cls;
81 
86 
91 };
92 
93 
99 static void
101 
102 
109 static void
110 handle_sync (void *cls, const struct GNUNET_MessageHeader *msg)
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 }
119 
120 
128 static int
129 check_result (void *cls, const struct RecordResultMessage *lrm)
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 }
186 
187 
195 static void
196 handle_result (void *cls, const struct RecordResultMessage *lrm)
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 }
219 
220 
229 static void
230 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
231 {
232  struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
233 
234  (void) error;
235  reconnect (zm);
236 }
237 
238 
244 static void
246 {
247  struct GNUNET_MQ_MessageHandler handlers[] =
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 }
277 
278 
304  const struct GNUNET_CONFIGURATION_Handle *cfg,
305  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
306  int iterate_first,
308  void *error_cb_cls,
310  void *monitor_cls,
312  void *sync_cb_cls)
313 {
315 
317  if (NULL != zone)
318  zm->zone = *zone;
320  zm->error_cb = error_cb;
322  zm->monitor = monitor;
323  zm->monitor_cls = monitor_cls;
324  zm->sync_cb = sync_cb;
325  zm->sync_cb_cls = sync_cb_cls;
326  zm->cfg = cfg;
327  reconnect (zm);
328  if (NULL == zm->mq)
329  {
330  GNUNET_free (zm);
331  return NULL;
332  }
333  return zm;
334 }
335 
336 
358 void
360  uint64_t limit)
361 {
362  struct GNUNET_MQ_Envelope *env;
363  struct ZoneMonitorNextMessage *nm;
364 
366  nm->limit = GNUNET_htonll (limit);
367  GNUNET_MQ_send (zm->mq, env);
368 }
369 
370 
376 void
378 {
379  if (NULL != zm->mq)
380  {
381  GNUNET_MQ_destroy (zm->mq);
382  zm->mq = NULL;
383  }
384  GNUNET_free (zm);
385 }
386 
387 
388 /* end of namestore_api_monitor.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
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_NAMESTORE_ZoneMonitor * GNUNET_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.
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_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT
Client to service: I am now ready for the next (set of) monitor events.
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void(* GNUNET_NAMESTORE_RecordMonitor)(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Process a record that was stored in the namestore.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
void GNUNET_NAMESTORE_zone_monitor_stop(struct GNUNET_NAMESTORE_ZoneMonitor *zm)
Stop monitoring a zone for changes.
static struct GNUNET_NAMESTORE_ZoneMonitor * zm
Monitor handle.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static void reconnect(struct GNUNET_NAMESTORE_ZoneMonitor *zm)
Reconnect to the namestore service.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Private ECC key encoded for transmission.
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...
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration (to reconnect).
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
GNUNET_SCHEDULER_TaskCallback sync_cb
Function called when we&#39;ve synchronized.
uint16_t name_len
Name length.
Definition: namestore.h:266
GNUNET_NAMESTORE_RecordMonitor monitor
Function to call on events.
common internal definitions for namestore service
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
uint64_t limit
Number of records to return to the iterator in one shot (before #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_M...
Definition: namestore.h:338
cryptographic primitives for GNUnet
Handle for a monitoring activity.
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...
uint16_t rd_count
Number of records contained.
Definition: namestore.h:276
#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.
static void handle_result(void *cls, const struct RecordResultMessage *lrm)
We&#39;ve received a notification about a change to our zone.
Message handler for a specific message type.
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
void * sync_cb_cls
Closure for sync_cb.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static void handle_sync(void *cls, const struct GNUNET_MessageHeader *msg)
Handle SYNC message from the namestore service.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
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.
uint32_t iterate_first
GNUNET_YES to first iterate over all records, GNUNET_NO to only monitor changes.o ...
Definition: namestore.h:309
Ask for next result of zone iteration for the given operation.
Definition: namestore.h:321
static int check_result(void *cls, const struct RecordResultMessage *lrm)
We&#39;ve received a notification about a change to our zone.
Handle to a message queue.
Definition: mq.c:85
configuration data
Definition: configuration.c:85
#define MAX_NAME_LEN
Maximum length of any name, including 0-termination.
Definition: namecache.h:33
#define GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC
Service to client: you&#39;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.
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_MQ_handler_end()
End-marker for the handlers array.
void * monitor_cls
Closure for monitor.
#define GNUNET_free(ptr)
Wrapper around free.