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)
137 const char *name_tmp;
138 const char *rd_ser_tmp;
141 lrm_len = ntohs (lrm->gns_header.header.size);
142 rd_len = ntohs (lrm->rd_len);
143 rd_count = ntohs (lrm->rd_count);
144 name_len = ntohs (lrm->name_len);
145 if (name_len > MAX_NAME_LEN)
148 return GNUNET_SYSERR;
150 exp_lrm_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
151 if (lrm_len != exp_lrm_len)
154 return GNUNET_SYSERR;
159 return GNUNET_SYSERR;
161 name_tmp = (const char *) &lrm[1];
162 if (name_tmp[name_len -1] != '\0')
165 return GNUNET_SYSERR;
167 rd_ser_tmp = (const char *) &name_tmp[name_len];
169 struct GNUNET_GNSRECORD_Data rd[rd_count];
172 GNUNET_GNSRECORD_records_deserialize (rd_len,
178 return GNUNET_SYSERR;
186 * We've received a notification about a change to our zone.
187 * Forward to monitor callback.
189 * @param cls the zone monitor handle
190 * @param lrm the message from the service.
193 handle_result (void *cls,
194 const struct RecordResultMessage *lrm)
196 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
200 const char *name_tmp;
201 const char *rd_ser_tmp;
203 rd_len = ntohs (lrm->rd_len);
204 rd_count = ntohs (lrm->rd_count);
205 name_len = ntohs (lrm->name_len);
206 name_tmp = (const char *) &lrm[1];
207 rd_ser_tmp = (const char *) &name_tmp[name_len];
209 struct GNUNET_GNSRECORD_Data rd[rd_count];
211 GNUNET_assert (GNUNET_OK ==
212 GNUNET_GNSRECORD_records_deserialize (rd_len,
216 zm->monitor (zm->monitor_cls,
226 * Generic error handler, called with the appropriate error code and
227 * the same closure specified at the creation of the message queue.
228 * Not every message queue implementation supports an error handler.
230 * @param cls closure with the `struct GNUNET_NAMESTORE_ZoneMonitor *`
231 * @param error error code
234 mq_error_handler (void *cls,
235 enum GNUNET_MQ_Error error)
237 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
245 * Reconnect to the namestore service.
247 * @param zm monitor to reconnect
250 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
252 struct GNUNET_MQ_MessageHandler handlers[] = {
253 GNUNET_MQ_hd_fixed_size (sync,
254 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC,
255 struct GNUNET_MessageHeader,
257 GNUNET_MQ_hd_var_size (result,
258 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
259 struct RecordResultMessage,
261 GNUNET_MQ_handler_end ()
263 struct GNUNET_MQ_Envelope *env;
264 struct ZoneMonitorStartMessage *sm;
268 GNUNET_MQ_destroy (zm->mq);
269 zm->error_cb (zm->error_cb_cls);
271 zm->mq = GNUNET_CLIENT_connect (zm->cfg,
278 env = GNUNET_MQ_msg (sm,
279 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START);
280 sm->iterate_first = htonl (zm->iterate_first);
282 GNUNET_MQ_send (zm->mq,
289 * Begin monitoring a zone for changes. If @a iterate_first is set,
290 * we Will first call the @a monitor function on all existing records
291 * in the selected zone(s). In any case, we will call @a sync and
292 * afterwards call @a monitor whenever a record changes.
294 * @param cfg configuration to use to connect to namestore
295 * @param zone zone to monitor
296 * @param iterate_first #GNUNET_YES to first iterate over all existing records,
297 * #GNUNET_NO to only return changes that happen from now on
298 * @param error_cb function to call on error (i.e. disconnect); note that
299 * unlike the other error callbacks in this API, a call to this
300 * function does NOT destroy the monitor handle, it merely signals
301 * that monitoring is down. You need to still explicitly call
302 * #GNUNET_NAMESTORE_zone_monitor_stop().
303 * @param error_cb_cls closure for @a error_cb
304 * @param monitor function to call on zone changes
305 * @param monitor_cls closure for @a monitor
306 * @param sync_cb function called when we're in sync with the namestore
307 * @param cls closure for @a sync_cb
308 * @return handle to stop monitoring
310 struct GNUNET_NAMESTORE_ZoneMonitor *
311 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
312 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
314 GNUNET_SCHEDULER_TaskCallback error_cb,
316 GNUNET_NAMESTORE_RecordMonitor monitor,
318 GNUNET_SCHEDULER_TaskCallback sync_cb,
321 struct GNUNET_NAMESTORE_ZoneMonitor *zm;
323 zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
326 zm->iterate_first = iterate_first;
327 zm->error_cb = error_cb;
328 zm->error_cb_cls = error_cb_cls;
329 zm->monitor = monitor;
330 zm->monitor_cls = monitor_cls;
331 zm->sync_cb = sync_cb;
332 zm->sync_cb_cls = sync_cb_cls;
345 * Calls the monitor processor specified in #GNUNET_NAMESTORE_zone_monitor_start
346 * for the next record(s). This function is used to allow clients that merely
347 * monitor the NAMESTORE to still throttle namestore operations, so we can be
348 * sure that the monitors can keep up.
350 * Note that #GNUNET_NAMESTORE_records_store() only waits for this
351 * call if the previous limit set by the client was already reached.
352 * Thus, by using a @a limit greater than 1, monitors basically enable
353 * a queue of notifications to be processed asynchronously with some
354 * delay. Note that even with a limit of 1 the
355 * #GNUNET_NAMESTORE_records_store() function will run asynchronously
356 * and the continuation may be invoked before the monitors completed
357 * (or even started) processing the notification. Thus, monitors will
358 * only closely track the current state of the namestore, but not
359 * be involved in the transactions.
361 * @param zm the monitor
362 * @param limit number of records to return to the iterator in one shot
363 * (before #GNUNET_NAMESTORE_zone_monitor_next is to be called again)
366 GNUNET_NAMESTORE_zone_monitor_next (struct GNUNET_NAMESTORE_ZoneMonitor *zm,
369 struct GNUNET_MQ_Envelope *env;
370 struct ZoneMonitorNextMessage *nm;
372 env = GNUNET_MQ_msg (nm,
373 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT);
374 nm->limit = GNUNET_htonll (limit);
375 GNUNET_MQ_send (zm->mq,
381 * Stop monitoring a zone for changes.
383 * @param zm handle to the monitor activity to stop
386 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
390 GNUNET_MQ_destroy (zm->mq);
396 /* end of namestore_api_monitor.c */