2 This file is part of GNUnet.
3 Copyright (C) 2013, 2016 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.
22 * @file namestore/namestore_api_monitor.c
23 * @brief API to monitor changes in the NAMESTORE
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_signatures.h"
34 #include "gnunet_namestore_service.h"
35 #include "namestore.h"
39 * Handle for a monitoring activity.
41 struct GNUNET_NAMESTORE_ZoneMonitor
44 * Configuration (to reconnect).
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Handle to namestore service.
51 struct GNUNET_MQ_Handle *mq;
54 * Function to call on errors.
56 GNUNET_SCHEDULER_TaskCallback error_cb;
59 * Closure for @e error_cb.
64 * Function to call on events.
66 GNUNET_NAMESTORE_RecordMonitor monitor;
69 * Closure for @e monitor.
74 * Function called when we've synchronized.
76 GNUNET_SCHEDULER_TaskCallback sync_cb;
79 * Closure for @e sync_cb.
86 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
89 * Do we first iterate over all existing records?
97 * Reconnect to the namestore service.
99 * @param zm monitor to reconnect
102 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
106 * Handle SYNC message from the namestore service.
108 * @param cls the monitor
109 * @param msg the sync message
112 handle_sync (void *cls,
113 const struct GNUNET_MessageHeader *msg)
115 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
117 if (NULL != zm->sync_cb)
118 zm->sync_cb (zm->sync_cb_cls);
123 * We've received a notification about a change to our zone.
124 * Check that it is well-formed.
126 * @param cls the zone monitor handle
127 * @param lrm the message from the service.
130 check_result (void *cls,
131 const struct RecordResultMessage *lrm)
138 const char *name_tmp;
139 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 exp_lrm_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
146 if (lrm_len != exp_lrm_len)
149 return GNUNET_SYSERR;
154 return GNUNET_SYSERR;
156 name_tmp = (const char *) &lrm[1];
157 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
160 return GNUNET_SYSERR;
162 rd_ser_tmp = (const char *) &name_tmp[name_len];
164 struct GNUNET_GNSRECORD_Data rd[rd_count];
167 GNUNET_GNSRECORD_records_deserialize (rd_len,
173 return GNUNET_SYSERR;
181 * We've received a notification about a change to our zone.
182 * Forward to monitor callback.
184 * @param cls the zone monitor handle
185 * @param lrm the message from the service.
188 handle_result (void *cls,
189 const struct RecordResultMessage *lrm)
191 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
195 const char *name_tmp;
196 const char *rd_ser_tmp;
198 rd_len = ntohs (lrm->rd_len);
199 rd_count = ntohs (lrm->rd_count);
200 name_len = ntohs (lrm->name_len);
201 name_tmp = (const char *) &lrm[1];
202 rd_ser_tmp = (const char *) &name_tmp[name_len];
204 struct GNUNET_GNSRECORD_Data rd[rd_count];
206 GNUNET_assert (GNUNET_OK ==
207 GNUNET_GNSRECORD_records_deserialize (rd_len,
211 zm->monitor (zm->monitor_cls,
221 * Generic error handler, called with the appropriate error code and
222 * the same closure specified at the creation of the message queue.
223 * Not every message queue implementation supports an error handler.
225 * @param cls closure with the `struct GNUNET_NAMESTORE_ZoneMonitor *`
226 * @param error error code
229 mq_error_handler (void *cls,
230 enum GNUNET_MQ_Error error)
232 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
239 * Reconnect to the namestore service.
241 * @param zm monitor to reconnect
244 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
246 struct GNUNET_MQ_MessageHandler handlers[] = {
247 GNUNET_MQ_hd_fixed_size (sync,
248 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC,
249 struct GNUNET_MessageHeader,
251 GNUNET_MQ_hd_var_size (result,
252 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
253 struct RecordResultMessage,
255 GNUNET_MQ_handler_end ()
257 struct GNUNET_MQ_Envelope *env;
258 struct ZoneMonitorStartMessage *sm;
262 GNUNET_MQ_destroy (zm->mq);
263 zm->error_cb (zm->error_cb_cls);
265 zm->mq = GNUNET_CLIENT_connect (zm->cfg,
272 env = GNUNET_MQ_msg (sm,
273 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START);
274 sm->iterate_first = htonl (zm->iterate_first);
276 GNUNET_MQ_send (zm->mq,
283 * Begin monitoring a zone for changes. If @a iterate_first is set,
284 * we Will first call the @a monitor function on all existing records
285 * in the selected zone(s). In any case, we will call @a sync and
286 * afterwards call @a monitor whenever a record changes.
288 * @param cfg configuration to use to connect to namestore
289 * @param zone zone to monitor
290 * @param iterate_first #GNUNET_YES to first iterate over all existing records,
291 * #GNUNET_NO to only return changes that happen from now on
292 * @param error_cb function to call on error (i.e. disconnect); note that
293 * unlike the other error callbacks in this API, a call to this
294 * function does NOT destroy the monitor handle, it merely signals
295 * that monitoring is down. You need to still explicitly call
296 * #GNUNET_NAMESTORE_zone_monitor_stop().
297 * @param error_cb_cls closure for @a error_cb
298 * @param monitor function to call on zone changes
299 * @param monitor_cls closure for @a monitor
300 * @param sync_cb function called when we're in sync with the namestore
301 * @param cls closure for @a sync_cb
302 * @return handle to stop monitoring
304 struct GNUNET_NAMESTORE_ZoneMonitor *
305 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
306 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
308 GNUNET_SCHEDULER_TaskCallback error_cb,
310 GNUNET_NAMESTORE_RecordMonitor monitor,
312 GNUNET_SCHEDULER_TaskCallback sync_cb,
315 struct GNUNET_NAMESTORE_ZoneMonitor *zm;
317 zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
320 zm->iterate_first = iterate_first;
321 zm->error_cb = error_cb;
322 zm->error_cb_cls = error_cb_cls;
323 zm->monitor = monitor;
324 zm->monitor_cls = monitor_cls;
325 zm->sync_cb = sync_cb;
326 zm->sync_cb_cls = sync_cb_cls;
339 * Stop monitoring a zone for changes.
341 * @param zm handle to the monitor activity to stop
344 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
348 GNUNET_MQ_destroy (zm->mq);
354 /* end of namestore_api_monitor.c */