2 This file is part of GNUnet.
3 Copyright (C) 2013, 2016, 2018 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file namestore/namestore_api_monitor.c
22 * @brief API to monitor changes in the NAMESTORE
23 * @author Christian Grothoff
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"
33 #include "gnunet_namestore_service.h"
34 #include "namestore.h"
38 * Handle for a monitoring activity.
40 struct GNUNET_NAMESTORE_ZoneMonitor
43 * Configuration (to reconnect).
45 const struct GNUNET_CONFIGURATION_Handle *cfg;
48 * Handle to namestore service.
50 struct GNUNET_MQ_Handle *mq;
53 * Function to call on errors.
55 GNUNET_SCHEDULER_TaskCallback error_cb;
58 * Closure for @e error_cb.
63 * Function to call on events.
65 GNUNET_NAMESTORE_RecordMonitor monitor;
68 * Closure for @e monitor.
73 * Function called when we've synchronized.
75 GNUNET_SCHEDULER_TaskCallback sync_cb;
78 * Closure for @e sync_cb.
85 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
88 * Do we first iterate over all existing records?
96 * Reconnect to the namestore service.
98 * @param zm monitor to reconnect
101 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
105 * Handle SYNC message from the namestore service.
107 * @param cls the monitor
108 * @param msg the sync message
111 handle_sync (void *cls,
112 const struct GNUNET_MessageHeader *msg)
114 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
118 if (NULL != zm->sync_cb)
119 zm->sync_cb (zm->sync_cb_cls);
124 * We've received a notification about a change to our zone.
125 * Check that it is well-formed.
127 * @param cls the zone monitor handle
128 * @param lrm the message from the service.
131 check_result (void *cls,
132 const struct RecordResultMessage *lrm)
139 const char *name_tmp;
140 const char *rd_ser_tmp;
143 lrm_len = ntohs (lrm->gns_header.header.size);
144 rd_len = ntohs (lrm->rd_len);
145 rd_count = ntohs (lrm->rd_count);
146 name_len = ntohs (lrm->name_len);
147 if (name_len > MAX_NAME_LEN)
150 return GNUNET_SYSERR;
152 exp_lrm_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
153 if (lrm_len != exp_lrm_len)
156 return GNUNET_SYSERR;
161 return GNUNET_SYSERR;
163 name_tmp = (const char *) &lrm[1];
164 if (name_tmp[name_len -1] != '\0')
167 return GNUNET_SYSERR;
169 rd_ser_tmp = (const char *) &name_tmp[name_len];
171 struct GNUNET_GNSRECORD_Data rd[rd_count];
174 GNUNET_GNSRECORD_records_deserialize (rd_len,
180 return GNUNET_SYSERR;
188 * We've received a notification about a change to our zone.
189 * Forward to monitor callback.
191 * @param cls the zone monitor handle
192 * @param lrm the message from the service.
195 handle_result (void *cls,
196 const struct RecordResultMessage *lrm)
198 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
202 const char *name_tmp;
203 const char *rd_ser_tmp;
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];
211 struct GNUNET_GNSRECORD_Data rd[rd_count];
213 GNUNET_assert (GNUNET_OK ==
214 GNUNET_GNSRECORD_records_deserialize (rd_len,
218 zm->monitor (zm->monitor_cls,
228 * Generic error handler, called with the appropriate error code and
229 * the same closure specified at the creation of the message queue.
230 * Not every message queue implementation supports an error handler.
232 * @param cls closure with the `struct GNUNET_NAMESTORE_ZoneMonitor *`
233 * @param error error code
236 mq_error_handler (void *cls,
237 enum GNUNET_MQ_Error error)
239 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
247 * Reconnect to the namestore service.
249 * @param zm monitor to reconnect
252 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
254 struct GNUNET_MQ_MessageHandler handlers[] = {
255 GNUNET_MQ_hd_fixed_size (sync,
256 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC,
257 struct GNUNET_MessageHeader,
259 GNUNET_MQ_hd_var_size (result,
260 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
261 struct RecordResultMessage,
263 GNUNET_MQ_handler_end ()
265 struct GNUNET_MQ_Envelope *env;
266 struct ZoneMonitorStartMessage *sm;
270 GNUNET_MQ_destroy (zm->mq);
271 zm->error_cb (zm->error_cb_cls);
273 zm->mq = GNUNET_CLIENT_connect (zm->cfg,
280 env = GNUNET_MQ_msg (sm,
281 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START);
282 sm->iterate_first = htonl (zm->iterate_first);
284 GNUNET_MQ_send (zm->mq,
291 * Begin monitoring a zone for changes. If @a iterate_first is set,
292 * we Will first call the @a monitor function on all existing records
293 * in the selected zone(s). In any case, we will call @a sync and
294 * afterwards call @a monitor whenever a record changes.
296 * @param cfg configuration to use to connect to namestore
297 * @param zone zone to monitor
298 * @param iterate_first #GNUNET_YES to first iterate over all existing records,
299 * #GNUNET_NO to only return changes that happen from now on
300 * @param error_cb function to call on error (i.e. disconnect); note that
301 * unlike the other error callbacks in this API, a call to this
302 * function does NOT destroy the monitor handle, it merely signals
303 * that monitoring is down. You need to still explicitly call
304 * #GNUNET_NAMESTORE_zone_monitor_stop().
305 * @param error_cb_cls closure for @a error_cb
306 * @param monitor function to call on zone changes
307 * @param monitor_cls closure for @a monitor
308 * @param sync_cb function called when we're in sync with the namestore
309 * @param cls closure for @a sync_cb
310 * @return handle to stop monitoring
312 struct GNUNET_NAMESTORE_ZoneMonitor *
313 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
314 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
316 GNUNET_SCHEDULER_TaskCallback error_cb,
318 GNUNET_NAMESTORE_RecordMonitor monitor,
320 GNUNET_SCHEDULER_TaskCallback sync_cb,
323 struct GNUNET_NAMESTORE_ZoneMonitor *zm;
325 zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
328 zm->iterate_first = iterate_first;
329 zm->error_cb = error_cb;
330 zm->error_cb_cls = error_cb_cls;
331 zm->monitor = monitor;
332 zm->monitor_cls = monitor_cls;
333 zm->sync_cb = sync_cb;
334 zm->sync_cb_cls = sync_cb_cls;
347 * Calls the monitor processor specified in #GNUNET_NAMESTORE_zone_monitor_start
348 * for the next record(s). This function is used to allow clients that merely
349 * monitor the NAMESTORE to still throttle namestore operations, so we can be
350 * sure that the monitors can keep up.
352 * Note that #GNUNET_NAMESTORE_records_store() only waits for this
353 * call if the previous limit set by the client was already reached.
354 * Thus, by using a @a limit greater than 1, monitors basically enable
355 * a queue of notifications to be processed asynchronously with some
356 * delay. Note that even with a limit of 1 the
357 * #GNUNET_NAMESTORE_records_store() function will run asynchronously
358 * and the continuation may be invoked before the monitors completed
359 * (or even started) processing the notification. Thus, monitors will
360 * only closely track the current state of the namestore, but not
361 * be involved in the transactions.
363 * @param zm the monitor
364 * @param limit number of records to return to the iterator in one shot
365 * (before #GNUNET_NAMESTORE_zone_monitor_next is to be called again)
368 GNUNET_NAMESTORE_zone_monitor_next (struct GNUNET_NAMESTORE_ZoneMonitor *zm,
371 struct GNUNET_MQ_Envelope *env;
372 struct ZoneMonitorNextMessage *nm;
374 env = GNUNET_MQ_msg (nm,
375 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT);
376 nm->limit = GNUNET_htonll (limit);
377 GNUNET_MQ_send (zm->mq,
383 * Stop monitoring a zone for changes.
385 * @param zm handle to the monitor activity to stop
388 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
392 GNUNET_MQ_destroy (zm->mq);
398 /* end of namestore_api_monitor.c */