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 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.
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.
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/>.
19 * @file namestore/namestore_api_monitor.c
20 * @brief API to monitor changes in the NAMESTORE
21 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_crypto_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_dnsparser_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_signatures.h"
31 #include "gnunet_namestore_service.h"
32 #include "namestore.h"
36 * Handle for a monitoring activity.
38 struct GNUNET_NAMESTORE_ZoneMonitor
41 * Configuration (to reconnect).
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
46 * Handle to namestore service.
48 struct GNUNET_MQ_Handle *mq;
51 * Function to call on errors.
53 GNUNET_SCHEDULER_TaskCallback error_cb;
56 * Closure for @e error_cb.
61 * Function to call on events.
63 GNUNET_NAMESTORE_RecordMonitor monitor;
66 * Closure for @e monitor.
71 * Function called when we've synchronized.
73 GNUNET_SCHEDULER_TaskCallback sync_cb;
76 * Closure for @e sync_cb.
83 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
86 * Do we first iterate over all existing records?
94 * Reconnect to the namestore service.
96 * @param zm monitor to reconnect
99 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
103 * Handle SYNC message from the namestore service.
105 * @param cls the monitor
106 * @param msg the sync message
109 handle_sync (void *cls,
110 const struct GNUNET_MessageHeader *msg)
112 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
116 if (NULL != zm->sync_cb)
117 zm->sync_cb (zm->sync_cb_cls);
122 * We've received a notification about a change to our zone.
123 * Check that it is well-formed.
125 * @param cls the zone monitor handle
126 * @param lrm the message from the service.
129 check_result (void *cls,
130 const struct RecordResultMessage *lrm)
132 static struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
133 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
139 const char *name_tmp;
140 const char *rd_ser_tmp;
143 if ( (0 != memcmp (&lrm->private_key,
145 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
148 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
151 return GNUNET_SYSERR;
153 lrm_len = ntohs (lrm->gns_header.header.size);
154 rd_len = ntohs (lrm->rd_len);
155 rd_count = ntohs (lrm->rd_count);
156 name_len = ntohs (lrm->name_len);
157 if (name_len > MAX_NAME_LEN)
160 return GNUNET_SYSERR;
162 exp_lrm_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
163 if (lrm_len != exp_lrm_len)
166 return GNUNET_SYSERR;
171 return GNUNET_SYSERR;
173 name_tmp = (const char *) &lrm[1];
174 if (name_tmp[name_len -1] != '\0')
177 return GNUNET_SYSERR;
179 rd_ser_tmp = (const char *) &name_tmp[name_len];
181 struct GNUNET_GNSRECORD_Data rd[rd_count];
184 GNUNET_GNSRECORD_records_deserialize (rd_len,
190 return GNUNET_SYSERR;
198 * We've received a notification about a change to our zone.
199 * Forward to monitor callback.
201 * @param cls the zone monitor handle
202 * @param lrm the message from the service.
205 handle_result (void *cls,
206 const struct RecordResultMessage *lrm)
208 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
212 const char *name_tmp;
213 const char *rd_ser_tmp;
215 rd_len = ntohs (lrm->rd_len);
216 rd_count = ntohs (lrm->rd_count);
217 name_len = ntohs (lrm->name_len);
218 name_tmp = (const char *) &lrm[1];
219 rd_ser_tmp = (const char *) &name_tmp[name_len];
221 struct GNUNET_GNSRECORD_Data rd[rd_count];
223 GNUNET_assert (GNUNET_OK ==
224 GNUNET_GNSRECORD_records_deserialize (rd_len,
228 zm->monitor (zm->monitor_cls,
238 * Generic error handler, called with the appropriate error code and
239 * the same closure specified at the creation of the message queue.
240 * Not every message queue implementation supports an error handler.
242 * @param cls closure with the `struct GNUNET_NAMESTORE_ZoneMonitor *`
243 * @param error error code
246 mq_error_handler (void *cls,
247 enum GNUNET_MQ_Error error)
249 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
257 * Reconnect to the namestore service.
259 * @param zm monitor to reconnect
262 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
264 struct GNUNET_MQ_MessageHandler handlers[] = {
265 GNUNET_MQ_hd_fixed_size (sync,
266 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC,
267 struct GNUNET_MessageHeader,
269 GNUNET_MQ_hd_var_size (result,
270 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
271 struct RecordResultMessage,
273 GNUNET_MQ_handler_end ()
275 struct GNUNET_MQ_Envelope *env;
276 struct ZoneMonitorStartMessage *sm;
280 GNUNET_MQ_destroy (zm->mq);
281 zm->error_cb (zm->error_cb_cls);
283 zm->mq = GNUNET_CLIENT_connect (zm->cfg,
290 env = GNUNET_MQ_msg (sm,
291 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START);
292 sm->iterate_first = htonl (zm->iterate_first);
294 GNUNET_MQ_send (zm->mq,
300 * Begin monitoring a zone for changes. If @a iterate_first is set,
301 * we Will first call the @a monitor function on all existing records
302 * in the selected zone(s). In any case, we will call @a sync and
303 * afterwards call @a monitor whenever a record changes.
305 * @param cfg configuration to use to connect to namestore
306 * @param zone zone to monitor
307 * @param iterate_first #GNUNET_YES to first iterate over all existing records,
308 * #GNUNET_NO to only return changes that happen from now on
309 * @param error_cb function to call on error (i.e. disconnect); note that
310 * unlike the other error callbacks in this API, a call to this
311 * function does NOT destroy the monitor handle, it merely signals
312 * that monitoring is down. You need to still explicitly call
313 * #GNUNET_NAMESTORE_zone_monitor_stop().
314 * @param error_cb_cls closure for @a error_cb
315 * @param monitor function to call on zone changes
316 * @param monitor_cls closure for @a monitor
317 * @param sync_cb function called when we're in sync with the namestore
318 * @param cls closure for @a sync_cb
319 * @return handle to stop monitoring
321 struct GNUNET_NAMESTORE_ZoneMonitor *
322 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
323 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
325 GNUNET_SCHEDULER_TaskCallback error_cb,
327 GNUNET_NAMESTORE_RecordMonitor monitor,
329 GNUNET_SCHEDULER_TaskCallback sync_cb,
332 struct GNUNET_NAMESTORE_ZoneMonitor *zm;
334 zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
337 zm->iterate_first = iterate_first;
338 zm->error_cb = error_cb;
339 zm->error_cb_cls = error_cb_cls;
340 zm->monitor = monitor;
341 zm->monitor_cls = monitor_cls;
342 zm->sync_cb = sync_cb;
343 zm->sync_cb_cls = sync_cb_cls;
356 * Calls the monitor processor specified in #GNUNET_NAMESTORE_zone_monitor_start
357 * for the next record(s). This function is used to allow clients that merely
358 * monitor the NAMESTORE to still throttle namestore operations, so we can be
359 * sure that the monitors can keep up.
361 * Note that #GNUNET_NAMESTORE_records_store() only waits for this
362 * call if the previous limit set by the client was already reached.
363 * Thus, by using a @a limit greater than 1, monitors basically enable
364 * a queue of notifications to be processed asynchronously with some
365 * delay. Note that even with a limit of 1 the
366 * #GNUNET_NAMESTORE_records_store() function will run asynchronously
367 * and the continuation may be invoked before the monitors completed
368 * (or even started) processing the notification. Thus, monitors will
369 * only closely track the current state of the namestore, but not
370 * be involved in the transactions.
372 * @param zm the monitor
373 * @param limit number of records to return to the iterator in one shot
374 * (before #GNUNET_NAMESTORE_zone_monitor_next is to be called again)
377 GNUNET_NAMESTORE_zone_monitor_next (struct GNUNET_NAMESTORE_ZoneMonitor *zm,
380 struct GNUNET_MQ_Envelope *env;
381 struct ZoneMonitorNextMessage *nm;
383 env = GNUNET_MQ_msg (nm,
384 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT);
385 nm->limit = GNUNET_htonll (limit);
386 GNUNET_MQ_send (zm->mq,
392 * Stop monitoring a zone for changes.
394 * @param zm handle to the monitor activity to stop
397 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
401 GNUNET_MQ_destroy (zm->mq);
407 /* end of namestore_api_monitor.c */